encoder.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /***************************************************************************
  2. **
  3. ** ITU-T G.722.1 (2005-05) - Fixed point implementation for main body and Annex C
  4. ** > Software Release 2.1 (2008-06)
  5. ** (Simple repackaging; no change from 2005-05 Release 2.0 code)
  6. **
  7. ** © 2004 Polycom, Inc.
  8. **
  9. ** All rights reserved.
  10. **
  11. ***************************************************************************/
  12. /***************************************************************************
  13. Filename: encoder.c
  14. Purpose: Contains files used to implement the G.722.1 Annex C encoder
  15. Design Notes:
  16. ***************************************************************************/
  17. /***************************************************************************
  18. Include files
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include "defs.h"
  23. #include "huff_def.h"
  24. #include "tables.h"
  25. #include "count.h"
  26. /***************************************************************************
  27. Function: encoder
  28. Syntax: void encoder(Word16 number_of_available_bits,
  29. Word16 number_of_regions,
  30. Word16 mlt_coefs,
  31. Word16 mag_shift,
  32. Word16 out_words)
  33. inputs: number_of_available_bits
  34. number_of_regions
  35. mag_shift
  36. mlt_coefs[DCT_LENGTH]
  37. outputs: out_words[MAX_BITS_PER_FRAME/16]
  38. Description: Encodes the mlt coefs into out_words using G.722.1 Annex C
  39. WMOPS: 7kHz | 24kbit | 32kbit
  40. -------|--------------|----------------
  41. AVG | 0.93 | 1.04
  42. -------|--------------|----------------
  43. MAX | 1.20 | 1.28
  44. -------|--------------|----------------
  45. 14kHz | 24kbit | 32kbit | 48kbit
  46. -------|--------------|----------------|----------------
  47. AVG | 1.39 | 1.71 | 2.01
  48. -------|--------------|----------------|----------------
  49. MAX | 2.00 | 2.30 | 2.52
  50. -------|--------------|----------------|----------------
  51. ***************************************************************************/
  52. void encoder(Word16 number_of_available_bits,
  53. Word16 number_of_regions,
  54. Word16 *mlt_coefs,
  55. Word16 mag_shift,
  56. Word16 *out_words)
  57. {
  58. Word16 num_categorization_control_bits;
  59. Word16 num_categorization_control_possibilities;
  60. Word16 number_of_bits_per_frame;
  61. Word16 number_of_envelope_bits;
  62. Word16 categorization_control;
  63. Word16 region;
  64. Word16 absolute_region_power_index[MAX_NUMBER_OF_REGIONS];
  65. Word16 power_categories[MAX_NUMBER_OF_REGIONS];
  66. Word16 category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1];
  67. Word16 drp_num_bits[MAX_NUMBER_OF_REGIONS+1];
  68. UWord16 drp_code_bits[MAX_NUMBER_OF_REGIONS+1];
  69. Word16 region_mlt_bit_counts[MAX_NUMBER_OF_REGIONS];
  70. UWord32 region_mlt_bits[4*MAX_NUMBER_OF_REGIONS];
  71. Word16 mag_shift_offset;
  72. Word16 temp;
  73. /* initialize variables */
  74. test();
  75. if (number_of_regions == NUMBER_OF_REGIONS)
  76. {
  77. num_categorization_control_bits = NUM_CATEGORIZATION_CONTROL_BITS;
  78. move16();
  79. num_categorization_control_possibilities = NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
  80. move16();
  81. }
  82. else
  83. {
  84. num_categorization_control_bits = MAX_NUM_CATEGORIZATION_CONTROL_BITS;
  85. move16();
  86. num_categorization_control_possibilities = MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
  87. move16();
  88. }
  89. number_of_bits_per_frame = number_of_available_bits;
  90. move16();
  91. for (region=0; region<number_of_regions; region++)
  92. {
  93. region_mlt_bit_counts[region] = 0;
  94. move16();
  95. }
  96. /* Estimate power envelope. */
  97. number_of_envelope_bits = compute_region_powers(mlt_coefs,
  98. mag_shift,
  99. drp_num_bits,
  100. drp_code_bits,
  101. absolute_region_power_index,
  102. number_of_regions);
  103. /* Adjust number of available bits based on power envelope estimate */
  104. temp = sub(number_of_available_bits,number_of_envelope_bits);
  105. number_of_available_bits = sub(temp,num_categorization_control_bits);
  106. /* get categorizations */
  107. categorize(number_of_available_bits,
  108. number_of_regions,
  109. num_categorization_control_possibilities,
  110. absolute_region_power_index,
  111. power_categories,
  112. category_balances);
  113. /* Adjust absolute_region_category_index[] for mag_shift.
  114. This assumes that REGION_POWER_STEPSIZE_DB is defined
  115. to be exactly 3.010299957 or 20.0 times log base 10
  116. of square root of 2. */
  117. temp = shl_nocheck(mag_shift,1);
  118. mag_shift_offset = add(temp,REGION_POWER_TABLE_NUM_NEGATIVES);
  119. for (region=0; region<number_of_regions; region++)
  120. {
  121. absolute_region_power_index[region] = add(absolute_region_power_index[region],mag_shift_offset);
  122. move16();
  123. }
  124. /* adjust the absolute power region index based on the mlt coefs */
  125. adjust_abs_region_power_index(absolute_region_power_index,mlt_coefs,number_of_regions);
  126. /* quantize and code the mlt coefficients based on categorizations */
  127. vector_quantize_mlts(number_of_available_bits,
  128. number_of_regions,
  129. num_categorization_control_possibilities,
  130. mlt_coefs,
  131. absolute_region_power_index,
  132. power_categories,
  133. category_balances,
  134. &categorization_control,
  135. region_mlt_bit_counts,
  136. region_mlt_bits);
  137. /* stuff bits into words */
  138. bits_to_words(region_mlt_bits,
  139. region_mlt_bit_counts,
  140. drp_num_bits,
  141. drp_code_bits,
  142. out_words,
  143. categorization_control,
  144. number_of_regions,
  145. num_categorization_control_bits,
  146. number_of_bits_per_frame);
  147. }
  148. /***************************************************************************
  149. Function: bits_to_words
  150. Syntax: bits_to_words(UWord32 *region_mlt_bits,
  151. Word16 *region_mlt_bit_counts,
  152. Word16 *drp_num_bits,
  153. UWord16 *drp_code_bits,
  154. Word16 *out_words,
  155. Word16 categorization_control,
  156. Word16 number_of_regions,
  157. Word16 num_categorization_control_bits,
  158. Word16 number_of_bits_per_frame)
  159. Description: Stuffs the bits into words for output
  160. WMOPS: 7kHz | 24kbit | 32kbit
  161. -------|--------------|----------------
  162. AVG | 0.09 | 0.12
  163. -------|--------------|----------------
  164. MAX | 0.10 | 0.13
  165. -------|--------------|----------------
  166. 14kHz | 24kbit | 32kbit | 48kbit
  167. -------|--------------|----------------|----------------
  168. AVG | 0.12 | 0.15 | 0.19
  169. -------|--------------|----------------|----------------
  170. MAX | 0.14 | 0.17 | 0.21
  171. -------|--------------|----------------|----------------
  172. ***************************************************************************/
  173. void bits_to_words(UWord32 *region_mlt_bits,
  174. Word16 *region_mlt_bit_counts,
  175. Word16 *drp_num_bits,
  176. UWord16 *drp_code_bits,
  177. Word16 *out_words,
  178. Word16 categorization_control,
  179. Word16 number_of_regions,
  180. Word16 num_categorization_control_bits,
  181. Word16 number_of_bits_per_frame)
  182. {
  183. Word16 out_word_index = 0;
  184. Word16 j;
  185. Word16 region;
  186. Word16 out_word;
  187. Word16 region_bit_count;
  188. Word16 current_word_bits_left;
  189. UWord16 slice;
  190. Word16 out_word_bits_free = 16;
  191. UWord32 *in_word_ptr;
  192. UWord32 current_word;
  193. Word32 acca = 0;
  194. Word32 accb;
  195. Word16 temp;
  196. /* First set up the categorization control bits to look like one more set of region power bits. */
  197. out_word = 0;
  198. move16();
  199. drp_num_bits[number_of_regions] = num_categorization_control_bits;
  200. move16();
  201. drp_code_bits[number_of_regions] = (UWord16)categorization_control;
  202. move16();
  203. /* These code bits are right justified. */
  204. for (region=0; region <= number_of_regions; region++)
  205. {
  206. current_word_bits_left = drp_num_bits[region];
  207. move16();
  208. current_word = (UWord32)drp_code_bits[region];
  209. move16();
  210. j = sub(current_word_bits_left,out_word_bits_free);
  211. test();
  212. if (j >= 0)
  213. {
  214. temp = extract_l(L_shr_nocheck(current_word,j));
  215. out_word = add(out_word,temp);
  216. out_words[out_word_index++] = out_word;
  217. move16();
  218. out_word_bits_free = 16;
  219. move16();
  220. out_word_bits_free = sub(out_word_bits_free,j);
  221. acca = (current_word << out_word_bits_free);
  222. out_word = extract_l(acca);
  223. }
  224. else
  225. {
  226. j = negate(j);
  227. acca = (current_word << j);
  228. accb = L_deposit_l(out_word);
  229. acca = L_add(accb,acca);
  230. out_word = extract_l(acca);
  231. out_word_bits_free = sub(out_word_bits_free,current_word_bits_left);
  232. }
  233. }
  234. /* These code bits are left justified. */
  235. for (region=0;region<number_of_regions; region++)
  236. {
  237. accb = L_deposit_l(out_word_index);
  238. accb = L_shl_nocheck(accb,4);
  239. accb = L_sub(accb,number_of_bits_per_frame);
  240. test();
  241. if(accb < 0)
  242. {
  243. temp = shl_nocheck(region,2);
  244. in_word_ptr = &region_mlt_bits[temp];
  245. region_bit_count = region_mlt_bit_counts[region];
  246. move16();
  247. temp = sub(32,region_bit_count);
  248. test();
  249. if(temp > 0)
  250. current_word_bits_left = region_bit_count;
  251. else
  252. current_word_bits_left = 32;
  253. current_word = *in_word_ptr++;
  254. acca = L_deposit_l(out_word_index);
  255. acca = L_shl_nocheck(acca,4);
  256. acca = L_sub(acca,number_of_bits_per_frame);
  257. /* from while loop */
  258. test();
  259. test();
  260. logic16();
  261. while ((region_bit_count > 0) && (acca < 0))
  262. {
  263. /* from while loop */
  264. test();
  265. test();
  266. logic16();
  267. temp = sub(current_word_bits_left,out_word_bits_free);
  268. test();
  269. if (temp >= 0)
  270. {
  271. temp = sub(32,out_word_bits_free);
  272. accb = LU_shr(current_word,temp);
  273. slice = (UWord16)extract_l(accb);
  274. out_word = add(out_word,slice);
  275. test();
  276. current_word <<= out_word_bits_free;
  277. current_word_bits_left = sub(current_word_bits_left,out_word_bits_free);
  278. out_words[out_word_index++] = extract_l(out_word);
  279. move16();
  280. out_word = 0;
  281. move16();
  282. out_word_bits_free = 16;
  283. move16();
  284. }
  285. else
  286. {
  287. temp = sub(32,current_word_bits_left);
  288. accb = LU_shr(current_word,temp);
  289. slice = (UWord16)extract_l(accb);
  290. temp = sub(out_word_bits_free,current_word_bits_left);
  291. test();
  292. accb = slice << temp;
  293. acca = L_deposit_l(out_word);
  294. acca = L_add(acca,accb);
  295. out_word = extract_l(acca);
  296. out_word_bits_free = sub(out_word_bits_free,current_word_bits_left);
  297. current_word_bits_left = 0;
  298. move16();
  299. }
  300. test();
  301. if (current_word_bits_left == 0)
  302. {
  303. current_word = *in_word_ptr++;
  304. region_bit_count = sub(region_bit_count,32);
  305. /* current_word_bits_left = MIN(32,region_bit_count); */
  306. temp = sub(32,region_bit_count);
  307. test();
  308. if(temp > 0)
  309. current_word_bits_left = region_bit_count;
  310. else
  311. current_word_bits_left = 32;
  312. }
  313. acca = L_deposit_l(out_word_index);
  314. acca = L_shl_nocheck(acca,4);
  315. acca = L_sub(acca,number_of_bits_per_frame);
  316. }
  317. accb = L_deposit_l(out_word_index);
  318. accb = L_shl_nocheck(accb,4);
  319. accb = L_sub(accb,number_of_bits_per_frame);
  320. }
  321. }
  322. /* Fill out with 1's. */
  323. test();
  324. while (acca < 0)
  325. {
  326. test();
  327. current_word = 0x0000ffff;
  328. move32();
  329. temp = sub(16,out_word_bits_free);
  330. acca = LU_shr(current_word,temp);
  331. slice = (UWord16)extract_l(acca);
  332. out_word = add(out_word,slice);
  333. out_words[out_word_index++] = out_word;
  334. move16();
  335. out_word = 0;
  336. move16();
  337. out_word_bits_free = 16;
  338. move16();
  339. acca = L_deposit_l(out_word_index);
  340. acca = L_shl_nocheck(acca,4);
  341. acca = L_sub(acca,number_of_bits_per_frame);
  342. }
  343. }
  344. /***************************************************************************
  345. Function: adjust_abs_region_power_index
  346. Syntax: adjust_abs_region_power_index(Word16 *absolute_region_power_index,
  347. Word16 *mlt_coefs,
  348. Word16 number_of_regions)
  349. inputs: *mlt_coefs
  350. *absolute_region_power_index
  351. number_of_regions
  352. outputs: *absolute_region_power_index
  353. Description: Adjusts the absolute power index
  354. WMOPS: 7kHz | 24kbit | 32kbit
  355. -------|--------------|----------------
  356. AVG | 0.03 | 0.03
  357. -------|--------------|----------------
  358. MAX | 0.12 | 0.12
  359. -------|--------------|----------------
  360. 14kHz | 24kbit | 32kbit | 48kbit
  361. -------|--------------|----------------|----------------
  362. AVG | 0.03 | 0.03 | 0.03
  363. -------|--------------|----------------|----------------
  364. MAX | 0.14 | 0.14 | 0.14
  365. -------|--------------|----------------|----------------
  366. ***************************************************************************/
  367. void adjust_abs_region_power_index(Word16 *absolute_region_power_index,Word16 *mlt_coefs,Word16 number_of_regions)
  368. {
  369. Word16 n,i;
  370. Word16 region;
  371. Word16 *raw_mlt_ptr;
  372. Word32 acca;
  373. Word16 temp;
  374. for (region=0; region<number_of_regions; region++)
  375. {
  376. n = sub(absolute_region_power_index[region],39);
  377. n = shr_nocheck(n,1);
  378. test();
  379. if (n > 0)
  380. {
  381. temp = extract_l(L_mult0(region,REGION_SIZE));
  382. raw_mlt_ptr = &mlt_coefs[temp];
  383. for (i=0; i<REGION_SIZE; i++)
  384. {
  385. acca = L_shl_nocheck(*raw_mlt_ptr,16);
  386. acca = L_add(acca,32768L);
  387. acca = L_shr_nocheck(acca,n);
  388. acca = L_shr_nocheck(acca,16);
  389. *raw_mlt_ptr++ = extract_l(acca);
  390. }
  391. temp = shl_nocheck(n,1);
  392. temp = sub(absolute_region_power_index[region],temp);
  393. absolute_region_power_index[region] = temp;
  394. move16();
  395. }
  396. }
  397. }
  398. /***************************************************************************
  399. Function: compute_region_powers
  400. Syntax: Word16 compute_region_powers(Word16 *mlt_coefs,
  401. Word16 mag_shift,
  402. Word16 *drp_num_bits,
  403. UWord16 *drp_code_bits,
  404. Word16 *absolute_region_power_index,
  405. Word16 number_of_regions)
  406. mlt_coefs[DCT_LENGTH];
  407. mag_shift;
  408. drp_num_bits[MAX_NUMBER_OF_REGIONS];
  409. drp_code_bits[MAX_NUMBER_OF_REGIONS];
  410. absolute_region_power_index[MAX_NUMBER_OF_REGIONS];
  411. number_of_regions;
  412. Description: Computes the power for each of the regions
  413. WMOPS: 7kHz | 24kbit | 32kbit
  414. -------|--------------|----------------
  415. AVG | 0.09 | 0.09
  416. -------|--------------|----------------
  417. MAX | 0.13 | 0.13
  418. -------|--------------|----------------
  419. 14kHz | 24kbit | 32kbit | 48kbit
  420. -------|--------------|----------------|----------------
  421. AVG | 0.20 | 0.20 | 0.20
  422. -------|--------------|----------------|----------------
  423. MAX | 0.29 | 0.29 | 0.29
  424. -------|--------------|----------------|----------------
  425. ***************************************************************************/
  426. Word16 compute_region_powers(Word16 *mlt_coefs,
  427. Word16 mag_shift,
  428. Word16 *drp_num_bits,
  429. UWord16 *drp_code_bits,
  430. Word16 *absolute_region_power_index,
  431. Word16 number_of_regions)
  432. {
  433. Word16 *input_ptr;
  434. Word32 long_accumulator;
  435. Word16 itemp1;
  436. Word16 power_shift;
  437. Word16 region;
  438. Word16 j;
  439. Word16 differential_region_power_index[MAX_NUMBER_OF_REGIONS];
  440. Word16 number_of_bits;
  441. Word32 acca;
  442. Word16 temp;
  443. Word16 temp1;
  444. Word16 temp2;
  445. input_ptr = mlt_coefs;
  446. for (region=0; region<number_of_regions; region++)
  447. {
  448. long_accumulator = L_deposit_l(0);
  449. for (j=0; j<REGION_SIZE; j++)
  450. {
  451. itemp1 = *input_ptr++;
  452. move16();
  453. long_accumulator = L_mac0(long_accumulator,itemp1,itemp1);
  454. }
  455. power_shift = 0;
  456. move16();
  457. acca = (long_accumulator & 0x7fff0000L);
  458. logic32();
  459. test();
  460. while (acca > 0)
  461. {
  462. test();
  463. long_accumulator = L_shr_nocheck(long_accumulator,1);
  464. acca = (long_accumulator & 0x7fff0000L);
  465. logic32();
  466. power_shift = add(power_shift,1);
  467. }
  468. acca = L_sub(long_accumulator,32767);
  469. temp = add(power_shift,15);
  470. test();
  471. test();
  472. logic16();
  473. while ((acca <= 0) && (temp >= 0))
  474. {
  475. test();
  476. test();
  477. logic16();
  478. long_accumulator = L_shl_nocheck(long_accumulator,1);
  479. acca = L_sub(long_accumulator,32767);
  480. power_shift--;
  481. temp = add(power_shift,15);
  482. }
  483. long_accumulator = L_shr_nocheck(long_accumulator,1);
  484. /* 28963 corresponds to square root of 2 times REGION_SIZE(20). */
  485. acca = L_sub(long_accumulator,28963);
  486. test();
  487. if (acca >= 0)
  488. power_shift = add(power_shift,1);
  489. acca = L_deposit_l(mag_shift);
  490. acca = L_shl_nocheck(acca,1);
  491. acca = L_sub(power_shift,acca);
  492. acca = L_add(35,acca);
  493. acca = L_sub(acca,REGION_POWER_TABLE_NUM_NEGATIVES);
  494. absolute_region_power_index[region] = extract_l(acca);
  495. }
  496. /* Before we differentially encode the quantized region powers, adjust upward the
  497. valleys to make sure all the peaks can be accurately represented. */
  498. temp = sub(number_of_regions,2);
  499. for (region = temp; region >= 0; region--)
  500. {
  501. temp1 = sub(absolute_region_power_index[region+1],DRP_DIFF_MAX);
  502. temp2 = sub(absolute_region_power_index[region],temp1);
  503. test();
  504. if (temp2 < 0)
  505. {
  506. absolute_region_power_index[region] = temp1;
  507. move16();
  508. }
  509. }
  510. /* The MLT is currently scaled too low by the factor
  511. ENCODER_SCALE_FACTOR(=18318)/32768 * (1./sqrt(160).
  512. This is the ninth power of 1 over the square root of 2.
  513. So later we will add ESF_ADJUSTMENT_TO_RMS_INDEX (now 9)
  514. to drp_code_bits[0]. */
  515. /* drp_code_bits[0] can range from 1 to 31. 0 will be used only as an escape sequence. */
  516. temp1 = sub(1,ESF_ADJUSTMENT_TO_RMS_INDEX);
  517. temp2 = sub(absolute_region_power_index[0],temp1);
  518. test();
  519. if (temp2 < 0)
  520. {
  521. absolute_region_power_index[0] = temp1;
  522. move16();
  523. }
  524. temp1 = sub(31,ESF_ADJUSTMENT_TO_RMS_INDEX);
  525. /*
  526. * The next line was corrected in Release 1.2
  527. */
  528. temp2 = sub(absolute_region_power_index[0], temp1);
  529. test();
  530. if (temp2 > 0)
  531. {
  532. absolute_region_power_index[0] = temp1;
  533. move16();
  534. }
  535. differential_region_power_index[0] = absolute_region_power_index[0];
  536. move16();
  537. number_of_bits = 5;
  538. move16();
  539. drp_num_bits[0] = 5;
  540. move16();
  541. drp_code_bits[0] = (UWord16)add(absolute_region_power_index[0],ESF_ADJUSTMENT_TO_RMS_INDEX);
  542. move16();
  543. /* Lower limit the absolute region power indices to -8 and upper limit them to 31. Such extremes
  544. may be mathematically impossible anyway.*/
  545. for (region=1; region<number_of_regions; region++)
  546. {
  547. temp1 = sub(-8,ESF_ADJUSTMENT_TO_RMS_INDEX);
  548. temp2 = sub(absolute_region_power_index[region],temp1);
  549. test();
  550. if (temp2 < 0)
  551. {
  552. absolute_region_power_index[region] = temp1;
  553. move16();
  554. }
  555. temp1 = sub(31,ESF_ADJUSTMENT_TO_RMS_INDEX);
  556. temp2 = sub(absolute_region_power_index[region],temp1);
  557. test();
  558. if (temp2 > 0)
  559. {
  560. absolute_region_power_index[region] = temp1;
  561. move16();
  562. }
  563. }
  564. for (region=1; region<number_of_regions; region++)
  565. {
  566. j = sub(absolute_region_power_index[region],absolute_region_power_index[region-1]);
  567. temp = sub(j,DRP_DIFF_MIN);
  568. test();
  569. if (temp < 0)
  570. {
  571. j = DRP_DIFF_MIN;
  572. }
  573. j = sub(j,DRP_DIFF_MIN);
  574. move16();
  575. differential_region_power_index[region] = j;
  576. move16();
  577. temp = add(absolute_region_power_index[region-1],differential_region_power_index[region]);
  578. temp = add(temp,DRP_DIFF_MIN);
  579. absolute_region_power_index[region] = temp;
  580. move16();
  581. number_of_bits = add(number_of_bits,differential_region_power_bits[region][j]);
  582. drp_num_bits[region] = differential_region_power_bits[region][j];
  583. move16();
  584. drp_code_bits[region] = differential_region_power_codes[region][j];
  585. move16();
  586. }
  587. return (number_of_bits);
  588. }
  589. /***************************************************************************
  590. Function: vector_quantize_mlts
  591. Syntax: void vector_quantize_mlts(number_of_available_bits,
  592. number_of_regions,
  593. num_categorization_control_possibilities,
  594. mlt_coefs,
  595. absolute_region_power_index,
  596. power_categories,
  597. category_balances,
  598. p_categorization_control,
  599. region_mlt_bit_counts,
  600. region_mlt_bits)
  601. Word16 number_of_available_bits;
  602. Word16 number_of_regions;
  603. Word16 num_categorization_control_possibilities;
  604. Word16 mlt_coefs[DCT_LENGTH];
  605. Word16 absolute_region_power_index[MAX_NUMBER_OF_REGIONS];
  606. Word16 power_categories[MAX_NUMBER_OF_REGIONS];
  607. Word16 category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1];
  608. Word16 *p_categorization_control;
  609. Word16 region_mlt_bit_counts[MAX_NUMBER_OF_REGIONS];
  610. Word32 region_mlt_bits[4*MAX_NUMBER_OF_REGIONS];
  611. Description: Scalar quantized vector Huffman coding (SQVH)
  612. WMOPS: 7kHz | 24kbit | 32kbit
  613. -------|--------------|----------------
  614. AVG | 0.57 | 0.65
  615. -------|--------------|----------------
  616. MAX | 0.78 | 0.83
  617. -------|--------------|----------------
  618. 14kHz | 24kbit | 32kbit | 48kbit
  619. -------|--------------|----------------|----------------
  620. AVG | 0.62 | 0.90 | 1.11
  621. -------|--------------|----------------|----------------
  622. MAX | 1.16 | 1.39 | 1.54
  623. -------|--------------|----------------|----------------
  624. ***************************************************************************/
  625. void vector_quantize_mlts(Word16 number_of_available_bits,
  626. Word16 number_of_regions,
  627. Word16 num_categorization_control_possibilities,
  628. Word16 *mlt_coefs,
  629. Word16 *absolute_region_power_index,
  630. Word16 *power_categories,
  631. Word16 *category_balances,
  632. Word16 *p_categorization_control,
  633. Word16 *region_mlt_bit_counts,
  634. UWord32 *region_mlt_bits)
  635. {
  636. Word16 *raw_mlt_ptr;
  637. Word16 region;
  638. Word16 category;
  639. Word16 total_mlt_bits = 0;
  640. Word16 temp;
  641. Word16 temp1;
  642. Word16 temp2;
  643. /* Start in the middle of the categorization control range. */
  644. temp = shr_nocheck(num_categorization_control_possibilities,1);
  645. temp = sub(temp,1);
  646. for (*p_categorization_control = 0; *p_categorization_control < temp; (*p_categorization_control)++)
  647. {
  648. region = category_balances[*p_categorization_control];
  649. move16();
  650. power_categories[region] = add(power_categories[region],1);
  651. move16();
  652. }
  653. for (region=0; region<number_of_regions; region++)
  654. {
  655. category = power_categories[region];
  656. move16();
  657. temp = extract_l(L_mult0(region,REGION_SIZE));
  658. raw_mlt_ptr = &mlt_coefs[temp];
  659. move16();
  660. temp = sub(category,(NUM_CATEGORIES-1));
  661. test();
  662. if (temp < 0)
  663. {
  664. region_mlt_bit_counts[region] =
  665. vector_huffman(category, absolute_region_power_index[region],raw_mlt_ptr,
  666. &region_mlt_bits[shl_nocheck(region,2)]);
  667. }
  668. else
  669. {
  670. region_mlt_bit_counts[region] = 0;
  671. move16();
  672. }
  673. total_mlt_bits = add(total_mlt_bits,region_mlt_bit_counts[region]);
  674. }
  675. /* If too few bits... */
  676. temp = sub(total_mlt_bits,number_of_available_bits);
  677. test();
  678. test();
  679. logic16();
  680. while ((temp < 0) && (*p_categorization_control > 0))
  681. {
  682. test();
  683. test();
  684. logic16();
  685. (*p_categorization_control)--;
  686. region = category_balances[*p_categorization_control];
  687. move16();
  688. power_categories[region] = sub(power_categories[region],1);
  689. move16();
  690. total_mlt_bits = sub(total_mlt_bits,region_mlt_bit_counts[region]);
  691. category = power_categories[region];
  692. move16();
  693. raw_mlt_ptr = &mlt_coefs[region*REGION_SIZE];
  694. move16();
  695. temp = sub(category,(NUM_CATEGORIES-1));
  696. test();
  697. if (temp < 0)
  698. {
  699. region_mlt_bit_counts[region] =
  700. vector_huffman(category, absolute_region_power_index[region],raw_mlt_ptr,
  701. &region_mlt_bits[shl_nocheck(region,2)]);
  702. }
  703. else
  704. {
  705. region_mlt_bit_counts[region] = 0;
  706. move16();
  707. }
  708. total_mlt_bits = add(total_mlt_bits,region_mlt_bit_counts[region]);
  709. temp = sub(total_mlt_bits,number_of_available_bits);
  710. }
  711. /* If too many bits... */
  712. /* Set up for while loop test */
  713. temp1 = sub(total_mlt_bits,number_of_available_bits);
  714. temp2 = sub(*p_categorization_control,sub(num_categorization_control_possibilities,1));
  715. test();
  716. test();
  717. logic16();
  718. while ((temp1 > 0) && (temp2 < 0))
  719. {
  720. /* operations for while contitions */
  721. test();
  722. test();
  723. logic16();
  724. region = category_balances[*p_categorization_control];
  725. move16();
  726. power_categories[region] = add(power_categories[region],1);
  727. move16();
  728. total_mlt_bits = sub(total_mlt_bits,region_mlt_bit_counts[region]);
  729. category = power_categories[region];
  730. move16();
  731. temp = extract_l(L_mult0(region,REGION_SIZE));
  732. raw_mlt_ptr = &mlt_coefs[temp];
  733. move16();
  734. temp = sub(category,(NUM_CATEGORIES-1));
  735. test();
  736. if (temp < 0)
  737. {
  738. region_mlt_bit_counts[region] =
  739. vector_huffman(category, absolute_region_power_index[region],raw_mlt_ptr,
  740. &region_mlt_bits[shl_nocheck(region,2)]);
  741. }
  742. else
  743. {
  744. region_mlt_bit_counts[region] = 0;
  745. move16();
  746. }
  747. total_mlt_bits = add(total_mlt_bits,region_mlt_bit_counts[region]);
  748. (*p_categorization_control)++;
  749. temp1 = sub(total_mlt_bits,number_of_available_bits);
  750. temp2 = sub(*p_categorization_control,sub(num_categorization_control_possibilities,1));
  751. }
  752. }
  753. /***************************************************************************
  754. Function: vector_huffman
  755. Syntax: Word16 vector_huffman(Word16 category,
  756. Word16 power_index,
  757. Word16 *raw_mlt_ptr,
  758. UWord32 *word_ptr)
  759. inputs: Word16 category
  760. Word16 power_index
  761. Word16 *raw_mlt_ptr
  762. outputs: number_of_region_bits
  763. *word_ptr
  764. Description: Huffman encoding for each region based on category and power_index
  765. WMOPS: 7kHz | 24kbit | 32kbit
  766. -------|--------------|----------------
  767. AVG | 0.03 | 0.03
  768. -------|--------------|----------------
  769. MAX | 0.04 | 0.04
  770. -------|--------------|----------------
  771. 14kHz | 24kbit | 32kbit | 48kbit
  772. -------|--------------|----------------|----------------
  773. AVG | 0.03 | 0.03 | 0.03
  774. -------|--------------|----------------|----------------
  775. MAX | 0.04 | 0.04 | 0.04
  776. -------|--------------|----------------|----------------
  777. ***************************************************************************/
  778. Word16 vector_huffman(Word16 category,
  779. Word16 power_index,
  780. Word16 *raw_mlt_ptr,
  781. UWord32 *word_ptr)
  782. {
  783. Word16 inv_of_step_size_times_std_dev;
  784. Word16 j,n;
  785. Word16 k;
  786. Word16 number_of_region_bits;
  787. Word16 number_of_non_zero;
  788. Word16 vec_dim;
  789. Word16 num_vecs;
  790. Word16 kmax, kmax_plus_one;
  791. Word16 index,signs_index;
  792. Word16 *bitcount_table_ptr;
  793. UWord16 *code_table_ptr;
  794. Word32 code_bits;
  795. Word16 number_of_code_bits;
  796. UWord32 current_word;
  797. Word16 current_word_bits_free;
  798. Word32 acca;
  799. Word32 accb;
  800. Word16 temp;
  801. Word16 mytemp; /* new variable in Release 1.2 */
  802. Word16 myacca; /* new variable in Release 1.2 */
  803. /* initialize variables */
  804. vec_dim = vector_dimension[category];
  805. move16();
  806. num_vecs = number_of_vectors[category];
  807. move16();
  808. kmax = max_bin[category];
  809. move16();
  810. kmax_plus_one = add(kmax,1);
  811. move16();
  812. current_word = 0L;
  813. move16();
  814. current_word_bits_free = 32;
  815. move16();
  816. number_of_region_bits = 0;
  817. move16();
  818. /* set up table pointers */
  819. bitcount_table_ptr = (Word16 *)table_of_bitcount_tables[category];
  820. code_table_ptr = (UWord16 *) table_of_code_tables[category];
  821. /* compute inverse of step size * standard deviation */
  822. acca = L_mult(step_size_inverse_table[category],standard_deviation_inverse_table[power_index]);
  823. acca = L_shr_nocheck(acca,1);
  824. acca = L_add(acca,4096);
  825. acca = L_shr_nocheck(acca,13);
  826. /*
  827. * The next two lines are new to Release 1.2
  828. */
  829. mytemp = (Word16)(acca & 0x3);
  830. acca = L_shr_nocheck(acca,2);
  831. inv_of_step_size_times_std_dev = extract_l(acca);
  832. for (n=0; n<num_vecs; n++)
  833. {
  834. index = 0;
  835. move16();
  836. signs_index = 0;
  837. move16();
  838. number_of_non_zero = 0;
  839. move16();
  840. for (j=0; j<vec_dim; j++)
  841. {
  842. k = abs_s(*raw_mlt_ptr);
  843. acca = L_mult(k,inv_of_step_size_times_std_dev);
  844. acca = L_shr_nocheck(acca,1);
  845. /*
  846. * The next four lines are new to Release 1.2
  847. */
  848. myacca = (Word16)L_mult(k,mytemp);
  849. myacca = (Word16)L_shr_nocheck(myacca,1);
  850. myacca = (Word16)L_add(myacca,int_dead_zone_low_bits[category]);
  851. myacca = (Word16)L_shr_nocheck(myacca,2);
  852. acca = L_add(acca,int_dead_zone[category]);
  853. /*
  854. * The next two lines are new to Release 1.2
  855. */
  856. acca = L_add(acca,myacca);
  857. acca = L_shr_nocheck(acca,13);
  858. k = extract_l(acca);
  859. test();
  860. if (k != 0)
  861. {
  862. number_of_non_zero = add(number_of_non_zero,1);
  863. signs_index = shl_nocheck(signs_index,1);
  864. test();
  865. if (*raw_mlt_ptr > 0)
  866. {
  867. signs_index = add(signs_index,1);
  868. }
  869. temp = sub(k,kmax);
  870. test();
  871. if (temp > 0)
  872. {
  873. k = kmax;
  874. move16();
  875. }
  876. }
  877. acca = L_shr_nocheck(L_mult(index,(kmax_plus_one)),1);
  878. index = extract_l(acca);
  879. index = add(index,k);
  880. raw_mlt_ptr++;
  881. }
  882. code_bits = *(code_table_ptr+index);
  883. number_of_code_bits = add((*(bitcount_table_ptr+index)),number_of_non_zero);
  884. number_of_region_bits = add(number_of_region_bits,number_of_code_bits);
  885. acca = code_bits << number_of_non_zero;
  886. accb = L_deposit_l(signs_index);
  887. acca = L_add(acca,accb);
  888. code_bits = acca;
  889. move32();
  890. /* msb of codebits is transmitted first. */
  891. j = sub(current_word_bits_free,number_of_code_bits);
  892. test();
  893. if (j >= 0)
  894. {
  895. test();
  896. acca = code_bits << j;
  897. current_word = L_add(current_word,acca);
  898. current_word_bits_free = j;
  899. move16();
  900. }
  901. else
  902. {
  903. j = negate(j);
  904. acca = L_shr_nocheck(code_bits,j);
  905. current_word = L_add(current_word,acca);
  906. *word_ptr++ = current_word;
  907. move16();
  908. current_word_bits_free = sub(32,j);
  909. test();
  910. current_word = code_bits << current_word_bits_free;
  911. }
  912. }
  913. *word_ptr++ = current_word;
  914. move16();
  915. return (number_of_region_bits);
  916. }