decoder.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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: decoder.c
  14. Purpose: Contains files used to implement the G.722.1 Annex C decoder
  15. Design Notes:
  16. ***************************************************************************/
  17. /***************************************************************************
  18. Include files
  19. ***************************************************************************/
  20. #include "defs.h"
  21. #include "tables.h"
  22. #include "huff_def.h"
  23. #include "count.h"
  24. /***************************************************************************
  25. Function: decoder
  26. Syntax: void decoder(Bit_Obj *bitobj,
  27. Rand_Obj *randobj,
  28. Word16 number_of_regions,
  29. Word16 *decoder_mlt_coefs,
  30. Word16 *p_mag_shift,
  31. Word16 *p_old_mag_shift,
  32. Word16 *old_decoder_mlt_coefs,
  33. Word16 frame_error_flag)
  34. inputs: Bit_Obj *bitobj
  35. Rand_Obj *randobj
  36. Word16 number_of_regions
  37. Word16 *p_old_mag_shift
  38. Word16 *old_decoder_mlt_coefs
  39. Word16 frame_error_flag
  40. outputs: Word16 *decoder_mlt_coefs,
  41. Word16 *p_mag_shift,
  42. Description: Decodes the out_words into mlt coefs using G.722.1 Annex C
  43. Design Notes:
  44. WMOPS: 7kHz | 24kbit | 32kbit
  45. -------|-------------|----------------
  46. AVG | 0.84 | 0.94
  47. -------|-------------|----------------
  48. MAX | 0.90 | 1.00
  49. -------|-------------|----------------
  50. 14kHz | 24kbit | 32kbit | 48kbit
  51. -------|-------------|----------------|----------------
  52. AVG | 1.31 | 1.56 | 1.88
  53. -------|-------------|----------------|----------------
  54. MAX | 1.59 | 1.80 | 1.98
  55. -------|-------------|----------------|----------------
  56. ***************************************************************************/
  57. void decoder(Bit_Obj *bitobj,
  58. Rand_Obj *randobj,
  59. Word16 number_of_regions,
  60. Word16 *decoder_mlt_coefs,
  61. Word16 *p_mag_shift,
  62. Word16 *p_old_mag_shift,
  63. Word16 *old_decoder_mlt_coefs,
  64. Word16 frame_error_flag)
  65. {
  66. Word16 absolute_region_power_index[MAX_NUMBER_OF_REGIONS];
  67. Word16 decoder_power_categories[MAX_NUMBER_OF_REGIONS];
  68. Word16 decoder_category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1];
  69. UWord16 categorization_control;
  70. Word16 decoder_region_standard_deviation[MAX_NUMBER_OF_REGIONS];
  71. Word16 i;
  72. Word16 num_categorization_control_bits;
  73. Word16 num_categorization_control_possibilities;
  74. Word16 number_of_coefs;
  75. Word16 number_of_valid_coefs;
  76. test();
  77. if (number_of_regions==NUMBER_OF_REGIONS)
  78. {
  79. num_categorization_control_bits = NUM_CATEGORIZATION_CONTROL_BITS;
  80. move16();
  81. num_categorization_control_possibilities = NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
  82. move16();
  83. number_of_coefs = DCT_LENGTH;
  84. move16();
  85. number_of_valid_coefs = NUMBER_OF_VALID_COEFS;
  86. move16();
  87. }
  88. else
  89. {
  90. num_categorization_control_bits = MAX_NUM_CATEGORIZATION_CONTROL_BITS;
  91. move16();
  92. num_categorization_control_possibilities = MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
  93. move16();
  94. number_of_coefs = MAX_DCT_LENGTH;
  95. move16();
  96. number_of_valid_coefs = MAX_NUMBER_OF_VALID_COEFS;
  97. move16();
  98. }
  99. test();
  100. if (frame_error_flag == 0)
  101. {
  102. /* convert the bits to absolute region power index and decoder_region_standard_deviation */
  103. decode_envelope(bitobj,
  104. number_of_regions,
  105. decoder_region_standard_deviation,
  106. absolute_region_power_index,
  107. p_mag_shift);
  108. /* fill the categorization_control with NUM_CATEGORIZATION_CONTROL_BITS */
  109. categorization_control = 0;
  110. for (i=0; i<num_categorization_control_bits; i++)
  111. {
  112. get_next_bit(bitobj);
  113. categorization_control = shl_nocheck(categorization_control,1);
  114. categorization_control = add(categorization_control,bitobj->next_bit);
  115. }
  116. bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,num_categorization_control_bits);
  117. /* obtain decoder power categories and category balances */
  118. /* based on the absolute region power index */
  119. categorize(bitobj->number_of_bits_left,
  120. number_of_regions,
  121. num_categorization_control_possibilities,
  122. absolute_region_power_index,
  123. decoder_power_categories,
  124. decoder_category_balances);
  125. /* perform adjustmaents to the power categories and category balances based on the cat control */
  126. rate_adjust_categories(categorization_control,
  127. decoder_power_categories,
  128. decoder_category_balances);
  129. /* decode the quantized bits into mlt coefs */
  130. decode_vector_quantized_mlt_indices(bitobj,
  131. randobj,
  132. number_of_regions,
  133. decoder_region_standard_deviation,
  134. decoder_power_categories,
  135. decoder_mlt_coefs);
  136. /* test for frame errors */
  137. test_4_frame_errors(bitobj,
  138. number_of_regions,
  139. num_categorization_control_possibilities,
  140. &frame_error_flag,
  141. categorization_control,
  142. absolute_region_power_index);
  143. }
  144. /* perform error handling operations */
  145. error_handling(number_of_coefs,
  146. number_of_valid_coefs,
  147. &frame_error_flag,
  148. decoder_mlt_coefs,
  149. old_decoder_mlt_coefs,
  150. p_mag_shift,
  151. p_old_mag_shift);
  152. }
  153. /***************************************************************************
  154. Function: decode_envelope
  155. Syntax: void decode_envelope(Bit_Obj *bitobj,
  156. Word16 number_of_regions,
  157. Word16 *decoder_region_standard_deviation,
  158. Word16 *absolute_region_power_index,
  159. Word16 *p_mag_shift)
  160. inputs: Bit_Obj *bitobj
  161. Word16 number_of_regions
  162. outputs: Word16 *decoder_region_standard_deviation
  163. Word16 *absolute_region_power_index
  164. Word16 *p_mag_shift
  165. Description: Recover differential_region_power_index from code bits
  166. Design Notes:
  167. WMOPS: 7kHz | 24kbit | 32kbit
  168. -------|--------------|----------------
  169. AVG | 0.04 | 0.04
  170. -------|--------------|----------------
  171. MAX | 0.05 | 0.05
  172. -------|--------------|----------------
  173. 14kHz | 24kbit | 32kbit | 48kbit
  174. -------|--------------|----------------|----------------
  175. AVG | 0.08 | 0.08 | 0.08
  176. -------|--------------|----------------|----------------
  177. MAX | 0.10 | 0.10 | 0.10
  178. -------|--------------|----------------|----------------
  179. ***************************************************************************/
  180. void decode_envelope(Bit_Obj *bitobj,
  181. Word16 number_of_regions,
  182. Word16 *decoder_region_standard_deviation,
  183. Word16 *absolute_region_power_index,
  184. Word16 *p_mag_shift)
  185. {
  186. Word16 region;
  187. Word16 i;
  188. Word16 index;
  189. Word16 differential_region_power_index[MAX_NUMBER_OF_REGIONS];
  190. Word16 max_index;
  191. Word16 temp;
  192. Word16 temp1;
  193. Word16 temp2;
  194. Word32 acca;
  195. index = 0;
  196. move16();
  197. /* get 5 bits from the current code word */
  198. for (i=0; i<5; i++)
  199. {
  200. get_next_bit(bitobj);
  201. index = shl_nocheck(index,1);
  202. index = add(index,bitobj->next_bit);
  203. }
  204. bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,5);
  205. /* ESF_ADJUSTMENT_TO_RMS_INDEX compensates for the current (9/30/96)
  206. IMLT being scaled to high by the ninth power of sqrt(2). */
  207. differential_region_power_index[0] = sub(index,ESF_ADJUSTMENT_TO_RMS_INDEX);
  208. move16();
  209. /* obtain differential_region_power_index */
  210. for (region=1; region<number_of_regions; region++)
  211. {
  212. index = 0;
  213. move16();
  214. do
  215. {
  216. get_next_bit(bitobj);
  217. test();
  218. if (bitobj->next_bit == 0)
  219. {
  220. index = differential_region_power_decoder_tree[region][index][0];
  221. move16();
  222. }
  223. else
  224. {
  225. index = differential_region_power_decoder_tree[region][index][1];
  226. move16();
  227. }
  228. bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
  229. test();
  230. } while (index > 0);
  231. differential_region_power_index[region] = negate(index);
  232. move16();
  233. }
  234. /* Reconstruct absolute_region_power_index[] from differential_region_power_index[]. */
  235. absolute_region_power_index[0] = differential_region_power_index[0];
  236. move16();
  237. for (region=1; region<number_of_regions; region++)
  238. {
  239. acca = L_add(absolute_region_power_index[region-1],differential_region_power_index[region]);
  240. acca = L_add(acca,DRP_DIFF_MIN);
  241. absolute_region_power_index[region] = extract_l(acca);
  242. }
  243. /* Reconstruct decoder_region_standard_deviation[] from absolute_region_power_index[]. */
  244. /* DEBUG!!!! - This integer method jointly computes the mag_shift
  245. and the standard deviations already mag_shift compensated. It
  246. relies on REGION_POWER_STEPSIZE_DB being exactly 3.010299957 db
  247. or a square root of 2 chnage in standard deviation. If
  248. REGION_POWER_STEPSIZE_DB changes, this software must be
  249. reworked. */
  250. temp = 0;
  251. move16();
  252. max_index = 0;
  253. move16();
  254. for (region=0; region<number_of_regions; region++)
  255. {
  256. acca = L_add(absolute_region_power_index[region],REGION_POWER_TABLE_NUM_NEGATIVES);
  257. i = extract_l(acca);
  258. temp1 = sub(i,max_index);
  259. test();
  260. if (temp1 > 0)
  261. {
  262. max_index = i;
  263. move16();
  264. }
  265. temp = add(temp,int_region_standard_deviation_table[i]);
  266. }
  267. i = 9;
  268. move16();
  269. temp1 = sub(temp,8);
  270. temp2 = sub(max_index,28);
  271. test();
  272. test();
  273. logic16();
  274. test();
  275. logic16();
  276. while ((i >= 0) && ((temp1 >= 0) || (temp2 > 0)))
  277. {
  278. i = sub(i,1);
  279. temp = shr_nocheck(temp,1);
  280. max_index = sub(max_index,2);
  281. temp1 = sub(temp,8);
  282. temp2 = sub(max_index,28);
  283. test();
  284. test();
  285. logic16();
  286. test();
  287. logic16();
  288. }
  289. *p_mag_shift = i;
  290. move16();
  291. /* pointer arithmetic */
  292. temp = (Word16 )(REGION_POWER_TABLE_NUM_NEGATIVES + (*p_mag_shift * 2));
  293. for (region=0; region<number_of_regions; region++)
  294. {
  295. acca = L_add(absolute_region_power_index[region],temp);
  296. i = extract_l(acca);
  297. decoder_region_standard_deviation[region] = int_region_standard_deviation_table[i];
  298. move16();
  299. }
  300. }
  301. /***************************************************************************
  302. Function: rate_adjust_categories
  303. Syntax: void rate_adjust_categories(Word16 categorization_control,
  304. Word16 *decoder_power_categories,
  305. Word16 *decoder_category_balances)
  306. inputs: Word16 categorization_control,
  307. Word16 *decoder_power_categories,
  308. Word16 *decoder_category_balances
  309. outputs: Word16 categorization_control,
  310. Word16 *decoder_power_categories,
  311. Description: Adjust the power categories based on the categorization control
  312. Design Notes:
  313. WMOPS: 7kHz | 24kbit | 32kbit
  314. -------|--------------|----------------
  315. AVG | 0.00 | 0.00
  316. -------|--------------|----------------
  317. MAX | 0.00 | 0.00
  318. -------|--------------|----------------
  319. 14kHz | 24kbit | 32kbit | 48kbit
  320. -------|--------------|----------------|----------------
  321. AVG | 0.00 | 0.00 | 0.00
  322. -------|--------------|----------------|----------------
  323. MAX | 0.01 | 0.01 | 0.01
  324. -------|--------------|----------------|----------------
  325. ***************************************************************************/
  326. void rate_adjust_categories(Word16 categorization_control,
  327. Word16 *decoder_power_categories,
  328. Word16 *decoder_category_balances)
  329. {
  330. Word16 i;
  331. Word16 region;
  332. i = 0;
  333. move16();
  334. test();
  335. while (categorization_control > 0)
  336. {
  337. region = decoder_category_balances[i++];
  338. move16();
  339. decoder_power_categories[region] = add(decoder_power_categories[region],1);
  340. move16();
  341. categorization_control = sub(categorization_control,1);
  342. }
  343. }
  344. /***************************************************************************
  345. Function: decode_vector_quantized_mlt_indices
  346. Syntax: void decode_vector_quantized_mlt_indices(Bit_Obj *bitobj,
  347. Rand_Obj *randobj,
  348. Word16 number_of_regions,
  349. Word16 *decoder_region_standard_deviation,
  350. Word16 *decoder_power_categories,
  351. Word16 *decoder_mlt_coefs)
  352. inputs: Bit_Obj *bitobj
  353. Rand_Obj *randobj
  354. Word16 number_of_regions
  355. Word16 *decoder_region_standard_deviation
  356. Word16 *decoder_power_categories
  357. outputs: Word16 *decoder_mlt_coefs
  358. Description: Decode MLT coefficients
  359. Design Notes:
  360. WMOPS: 7kHz | 24kbit | 32kbit
  361. -------|--------------|----------------
  362. AVG | 0.60 | 0.72
  363. -------|--------------|----------------
  364. MAX | 0.67 | 0.76
  365. -------|--------------|----------------
  366. 14kHz | 24kbit | 32kbit | 48kbit
  367. -------|--------------|----------------|----------------
  368. AVG | 0.77 | 0.98 | 1.28
  369. -------|--------------|----------------|----------------
  370. MAX | 1.05 | 1.18 | 1.36
  371. -------|--------------|----------------|----------------
  372. ***************************************************************************/
  373. void decode_vector_quantized_mlt_indices(Bit_Obj *bitobj,
  374. Rand_Obj *randobj,
  375. Word16 number_of_regions,
  376. Word16 *decoder_region_standard_deviation,
  377. Word16 *decoder_power_categories,
  378. Word16 *decoder_mlt_coefs)
  379. {
  380. Word16 standard_deviation;
  381. Word16 *decoder_mlt_ptr;
  382. Word16 decoder_mlt_value;
  383. Word16 noifillpos;
  384. Word16 noifillneg;
  385. Word16 noise_fill_factor[3] = {5793,8192,23170};
  386. Word16 region;
  387. Word16 category;
  388. Word16 j,n;
  389. Word16 k[MAX_VECTOR_DIMENSION];
  390. Word16 vec_dim;
  391. Word16 num_vecs;
  392. Word16 index;
  393. Word16 bit=0;
  394. Word16 signs_index=0;
  395. Word16 num_sign_bits;
  396. Word16 ran_out_of_bits_flag;
  397. Word16 *decoder_table_ptr;
  398. Word16 random_word;
  399. Word16 temp1;
  400. Word16 temp;
  401. Word32 acca;
  402. ran_out_of_bits_flag = 0;
  403. move16();
  404. for (region=0; region<number_of_regions; region++)
  405. {
  406. category = (Word16)decoder_power_categories[region];
  407. move16();
  408. acca = L_mult0(region,REGION_SIZE);
  409. index = extract_l(acca);
  410. decoder_mlt_ptr = &decoder_mlt_coefs[index];
  411. move16();
  412. standard_deviation = decoder_region_standard_deviation[region];
  413. move16();
  414. temp = sub(category,7);
  415. test();
  416. if (temp < 0)
  417. {
  418. /* Get the proper table of decoder tables, vec_dim, and num_vecs for the cat */
  419. decoder_table_ptr = (Word16 *) table_of_decoder_tables[category];
  420. move16();
  421. vec_dim = vector_dimension[category];
  422. move16();
  423. num_vecs = number_of_vectors[category];
  424. move16();
  425. for (n=0; n<num_vecs; n++)
  426. {
  427. index = 0;
  428. move16();
  429. /* get index */
  430. do
  431. {
  432. test();
  433. if (bitobj->number_of_bits_left <= 0)
  434. {
  435. ran_out_of_bits_flag = 1;
  436. move16();
  437. break;
  438. }
  439. get_next_bit(bitobj);
  440. test();
  441. if (bitobj->next_bit == 0)
  442. {
  443. temp = shl_nocheck(index,1);
  444. index = (Word16)*(decoder_table_ptr + temp);
  445. move16();
  446. }
  447. else
  448. {
  449. temp = shl_nocheck(index,1);
  450. index = (Word16)*(decoder_table_ptr + temp + 1);
  451. move16();
  452. }
  453. bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
  454. test();
  455. } while (index > 0);
  456. test();
  457. if (ran_out_of_bits_flag != 0)
  458. break;
  459. index = negate(index);
  460. /* convert index into array used to access the centroid table */
  461. /* get the number of sign bits in the index */
  462. num_sign_bits = index_to_array(index,k,category);
  463. temp = sub(bitobj->number_of_bits_left,num_sign_bits);
  464. test();
  465. if (temp >= 0)
  466. {
  467. test();
  468. if (num_sign_bits != 0)
  469. {
  470. signs_index = 0;
  471. move16();
  472. for (j=0; j<num_sign_bits; j++)
  473. {
  474. get_next_bit(bitobj);
  475. signs_index = shl_nocheck(signs_index,1);
  476. signs_index = add(signs_index,bitobj->next_bit);
  477. bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
  478. }
  479. temp = sub(num_sign_bits,1);
  480. bit = shl_nocheck(1,(temp));
  481. }
  482. for (j=0; j<vec_dim; j++)
  483. {
  484. acca = L_mult0(standard_deviation,mlt_quant_centroid[category][k[j]]);
  485. acca = L_shr_nocheck(acca,12);
  486. decoder_mlt_value = extract_l(acca);
  487. test();
  488. if (decoder_mlt_value != 0)
  489. {
  490. test();
  491. if ((signs_index & bit) == 0)
  492. decoder_mlt_value = negate(decoder_mlt_value);
  493. bit = shr_nocheck(bit,1);
  494. }
  495. *decoder_mlt_ptr++ = decoder_mlt_value;
  496. move16();
  497. }
  498. }
  499. else
  500. {
  501. ran_out_of_bits_flag = 1;
  502. move16();
  503. break;
  504. }
  505. }
  506. /* If ran out of bits during decoding do noise fill for remaining regions. */
  507. /* DEBUG!! - For now also redo all of last region with all noise fill. */
  508. test();
  509. if (ran_out_of_bits_flag != 0)
  510. {
  511. temp = add(region,1);
  512. for (j=temp; j<number_of_regions; j++)
  513. {
  514. decoder_power_categories[j] = 7;
  515. move16();
  516. }
  517. category = 7;
  518. move16();
  519. decoder_mlt_ptr = &decoder_mlt_coefs[region*REGION_SIZE];
  520. move16();
  521. }
  522. }
  523. temp = sub(category,5);
  524. temp1 = sub(category,6);
  525. test();
  526. test();
  527. logic16();
  528. if ((temp == 0) || (temp1 == 0))
  529. {
  530. decoder_mlt_ptr = &decoder_mlt_coefs[region*REGION_SIZE];
  531. move16();
  532. noifillpos = mult(standard_deviation,noise_fill_factor[category - 5]);
  533. noifillneg = negate(noifillpos);
  534. random_word = get_rand(randobj);
  535. for (j=0; j<10; j++)
  536. {
  537. test();
  538. if (*decoder_mlt_ptr == 0)
  539. {
  540. logic16();
  541. test();
  542. if ((random_word & 1) == 0)
  543. {
  544. temp1 = noifillneg;
  545. move16();
  546. }
  547. else
  548. {
  549. temp1 = noifillpos;
  550. move16();
  551. }
  552. *decoder_mlt_ptr = temp1;
  553. move16();
  554. random_word = shr_nocheck(random_word,1);
  555. }
  556. /* pointer arithmetic */
  557. decoder_mlt_ptr++;
  558. }
  559. random_word = get_rand(randobj);
  560. for (j=0; j<10; j++)
  561. {
  562. test();
  563. if (*decoder_mlt_ptr == 0)
  564. {
  565. logic16();
  566. test();
  567. if ((random_word & 1) == 0)
  568. {
  569. temp1 = noifillneg;
  570. move16();
  571. }
  572. else
  573. {
  574. temp1 = noifillpos;
  575. move16();
  576. }
  577. *decoder_mlt_ptr = temp1;
  578. move16();
  579. random_word = shr_nocheck(random_word,1);
  580. }
  581. /* pointer arithmetic */
  582. decoder_mlt_ptr++;
  583. }
  584. }
  585. /* if (category == 7) */
  586. temp1 = sub(category,7);
  587. test();
  588. if (temp1 == 0)
  589. {
  590. index = sub(category,5);
  591. noifillpos = mult(standard_deviation,noise_fill_factor[index]);
  592. noifillneg = negate(noifillpos);
  593. random_word = get_rand(randobj);
  594. for (j=0; j<10; j++)
  595. {
  596. logic16();
  597. test();
  598. if ((random_word & 1) == 0)
  599. {
  600. temp1 = noifillneg;
  601. move16();
  602. }
  603. else
  604. {
  605. temp1 = noifillpos;
  606. move16();
  607. }
  608. *decoder_mlt_ptr++ = temp1;
  609. move16();
  610. random_word = shr_nocheck(random_word,1);
  611. }
  612. random_word = get_rand(randobj);
  613. for (j=0; j<10; j++)
  614. {
  615. logic16();
  616. test();
  617. if ((random_word & 1) == 0)
  618. {
  619. temp1 = noifillneg;
  620. move16();
  621. }
  622. else
  623. {
  624. temp1 = noifillpos;
  625. move16();
  626. }
  627. *decoder_mlt_ptr++ = temp1;
  628. move16();
  629. random_word = shr_nocheck(random_word,1);
  630. }
  631. }
  632. }
  633. test();
  634. if (ran_out_of_bits_flag)
  635. bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
  636. }
  637. /****************************************************************************************
  638. Function: index_to_array
  639. Syntax: number_of_non_zero = index_to_array(Word16 index,
  640. Word16 array[MAX_VECTOR_DIMENSION],
  641. Word16 category)
  642. inputs: Word16 index
  643. Word16 category
  644. outputs: Word16 array[MAX_VECTOR_DIMENSION] - used in decoder to access
  645. mlt_quant_centroid table
  646. Word16 number_of_non_zero - number of non zero elements
  647. in the array
  648. Description: Computes an array of sign bits with the length of the category vector
  649. Returns the number of sign bits and the array
  650. WMOPS: 7kHz | 24kbit | 32kbit
  651. -------|--------------|----------------
  652. AVG | 0.00 | 0.00
  653. -------|--------------|----------------
  654. MAX | 0.00 | 0.00
  655. -------|--------------|----------------
  656. 14kHz | 24kbit | 32kbit | 48kbit
  657. -------|--------------|----------------|----------------
  658. AVG | 0.00 | 0.00 | 0.00
  659. -------|--------------|----------------|----------------
  660. MAX | 0.00 | 0.00 | 0.00
  661. -------|--------------|----------------|----------------
  662. ****************************************************************************************/
  663. Word16 index_to_array(Word16 index,Word16 *array,Word16 category)
  664. {
  665. Word16 j,q,p;
  666. Word16 number_of_non_zero;
  667. Word16 max_bin_plus_one;
  668. Word16 inverse_of_max_bin_plus_one;
  669. Word16 temp;
  670. number_of_non_zero = 0;
  671. move16();
  672. p = index;
  673. move16();
  674. max_bin_plus_one = add(max_bin[category],1);
  675. inverse_of_max_bin_plus_one = max_bin_plus_one_inverse[category];
  676. move16();
  677. temp = sub(vector_dimension[category],1);
  678. for (j=temp; j>=0; j--)
  679. {
  680. q = mult(p,inverse_of_max_bin_plus_one);
  681. temp = extract_l(L_mult0(q,max_bin_plus_one));
  682. array[j] = sub(p,temp);
  683. move16();
  684. p = q;
  685. move16();
  686. temp = array[j];
  687. move16();
  688. test();
  689. if (temp != 0)
  690. number_of_non_zero = add(number_of_non_zero,1);
  691. }
  692. return(number_of_non_zero);
  693. }
  694. /***************************************************************************
  695. Function: test_4_frame_errors
  696. Syntax: void test_4_frame_errors(Bit_Obj *bitobj,
  697. Word16 number_of_regions,
  698. Word16 num_categorization_control_possibilities,
  699. Word16 *frame_error_flag,
  700. Word16 categorization_control,
  701. Word16 *absolute_region_power_index)
  702. inputs: bit_obj
  703. number_of_regions
  704. num_categorization_control_possibilities
  705. frame_error_flag
  706. categorization_control
  707. absolute_region_power_index
  708. outputs: frame_error_flag
  709. Description: Tests for error conditions and sets the frame_error_flag accordingly
  710. Design Notes:
  711. WMOPS: 7kHz | 24kbit | 32kbit
  712. -------|--------------|----------------
  713. AVG | 0.01 | 0.01
  714. -------|--------------|----------------
  715. MAX | 0.04 | 0.08
  716. -------|--------------|----------------
  717. 14kHz | 24kbit | 32kbit | 48kbit
  718. -------|--------------|----------------|----------------
  719. AVG | 0.01 | 0.01 | 0.01
  720. -------|--------------|----------------|----------------
  721. MAX | 0.02 | 0.06 | 0.08
  722. -------|--------------|----------------|----------------
  723. ***************************************************************************/
  724. void test_4_frame_errors(Bit_Obj *bitobj,
  725. Word16 number_of_regions,
  726. Word16 num_categorization_control_possibilities,
  727. Word16 *frame_error_flag,
  728. Word16 categorization_control,
  729. Word16 *absolute_region_power_index)
  730. {
  731. Word16 region;
  732. Word16 i;
  733. Word16 temp;
  734. Word32 acca;
  735. Word32 accb;
  736. /* Test for bit stream errors. */
  737. test();
  738. if (bitobj->number_of_bits_left > 0)
  739. {
  740. for (i=0; i<bitobj->number_of_bits_left; i++)
  741. {
  742. get_next_bit(bitobj);
  743. test();
  744. if (bitobj->next_bit == 0)
  745. {
  746. *frame_error_flag = 1;
  747. move16();
  748. }
  749. }
  750. }
  751. else
  752. {
  753. temp = sub(categorization_control,sub(num_categorization_control_possibilities,1));
  754. test();
  755. if (temp < 0)
  756. {
  757. test();
  758. if (bitobj->number_of_bits_left < 0)
  759. {
  760. *frame_error_flag |= 2;
  761. logic16();
  762. }
  763. }
  764. }
  765. /* checks to ensure that abs_region_power_index is within range */
  766. /* the error flag is set if it is out of range */
  767. for (region=0; region<number_of_regions; region++)
  768. {
  769. /* the next two lines of comments were modified in release 1.2
  770. * to correct the description of the range of
  771. * absolute_region_power_index[] to be tested in the next
  772. * 9 lines of code.
  773. */
  774. /* if ((absolute_region_power_index[region] > 31) ||
  775. (absolute_region_power_index[region] < -8) */
  776. acca = L_add(absolute_region_power_index[region],ESF_ADJUSTMENT_TO_RMS_INDEX);
  777. accb = L_sub(acca,31);
  778. acca = L_add(acca,8);
  779. test();
  780. /* the next line was modifed in release 1.2 to
  781. * correct miss typed code and error checking.
  782. */
  783. if ((accb > 0) || (acca < 0))
  784. {
  785. *frame_error_flag |= 4;
  786. logic16();
  787. }
  788. }
  789. }
  790. /***************************************************************************
  791. Function: error_handling
  792. Syntax: void error_handling(Word16 number_of_coefs,
  793. Word16 number_of_valid_coefs,
  794. Word16 *frame_error_flag,
  795. Word16 *decoder_mlt_coefs,
  796. Word16 *old_decoder_mlt_coefs,
  797. Word16 *p_mag_shift,
  798. Word16 *p_old_mag_shift)
  799. inputs: number_of_coefs
  800. number_of_valid_coefs
  801. frame_error_flag
  802. old_decoder_mlt_coefs
  803. p_old_mag_shift
  804. outputs: decoder_mlt_coefs
  805. old_decoder_mlt_coefs
  806. p_mag_shift
  807. p_old_mag_shift
  808. Description: If both the current and previous frames are errored,
  809. set the mlt coefficients to 0. If only the current frame
  810. is errored, then repeat the previous frame's mlt coefficients.
  811. Design Notes:
  812. WMOPS: 7kHz | 24kbit | 32kbit
  813. -------|--------------|----------------
  814. AVG | 0.02 | 0.02
  815. -------|--------------|----------------
  816. MAX | 0.03 | 0.03
  817. -------|--------------|----------------
  818. 14kHz | 24kbit | 32kbit | 48kbit
  819. -------|--------------|----------------|----------------
  820. AVG | 0.03 | 0.03 | 0.03
  821. -------|--------------|----------------|----------------
  822. MAX | 0.03 | 0.03 | 0.06
  823. -------|--------------|----------------|----------------
  824. ***************************************************************************/
  825. void error_handling(Word16 number_of_coefs,
  826. Word16 number_of_valid_coefs,
  827. Word16 *frame_error_flag,
  828. Word16 *decoder_mlt_coefs,
  829. Word16 *old_decoder_mlt_coefs,
  830. Word16 *p_mag_shift,
  831. Word16 *p_old_mag_shift)
  832. {
  833. Word16 i;
  834. test();
  835. if (*frame_error_flag != 0)
  836. {
  837. for (i = 0; i < number_of_valid_coefs; i++)
  838. {
  839. decoder_mlt_coefs[i] = old_decoder_mlt_coefs[i];
  840. move16();
  841. }
  842. for (i = 0; i < number_of_valid_coefs; i++)
  843. {
  844. old_decoder_mlt_coefs[i] = 0;
  845. move16();
  846. }
  847. *p_mag_shift = *p_old_mag_shift;
  848. move16();
  849. *p_old_mag_shift = 0;
  850. move16();
  851. }
  852. else
  853. {
  854. /* Store in case next frame is errored. */
  855. for (i = 0; i < number_of_valid_coefs; i++)
  856. {
  857. old_decoder_mlt_coefs[i] = decoder_mlt_coefs[i];
  858. move16();
  859. }
  860. *p_old_mag_shift = *p_mag_shift;
  861. move16();
  862. }
  863. /* Zero out the upper 1/8 of the spectrum. */
  864. for (i = number_of_valid_coefs; i < number_of_coefs; i++)
  865. {
  866. decoder_mlt_coefs[i] = 0;
  867. move16();
  868. }
  869. }
  870. /****************************************************************************************
  871. Function: get_next_bit
  872. Syntax: void get_next_bit(Bit_Obj *bitobj)
  873. Description: Returns the next bit in the current word inside the bit object
  874. WMOPS: 7kHz | 24kbit | 32kbit
  875. -------|--------------|----------------
  876. AVG | 0.00 | 0.00
  877. -------|--------------|----------------
  878. MAX | 0.00 | 0.00
  879. -------|--------------|----------------
  880. 14kHz | 24kbit | 32kbit | 48kbit
  881. -------|--------------|----------------|----------------
  882. AVG | 0.00 | 0.00 | 0.00
  883. -------|--------------|----------------|----------------
  884. MAX | 0.00 | 0.00 | 0.00
  885. -------|--------------|----------------|----------------
  886. ****************************************************************************************/
  887. void get_next_bit(Bit_Obj *bitobj)
  888. {
  889. Word16 temp;
  890. test();
  891. if (bitobj->code_bit_count == 0)
  892. {
  893. bitobj->current_word = *bitobj->code_word_ptr++;
  894. move16();
  895. bitobj->code_bit_count = 16;
  896. move16();
  897. }
  898. bitobj->code_bit_count = sub(bitobj->code_bit_count,1);
  899. temp = shr_nocheck(bitobj->current_word,bitobj->code_bit_count);
  900. logic16();
  901. bitobj->next_bit = (Word16 )(temp & 1);
  902. }
  903. /****************************************************************************************
  904. Function: get_rand
  905. Syntax: Word16 get_rand(Rand_Obj *randobj)
  906. Description: Returns a random Word16 based on the seeds inside the rand object
  907. WMOPS: 7kHz | 24kbit | 32kbit
  908. -------|--------------|----------------
  909. AVG | 0.00 | 0.00
  910. -------|--------------|----------------
  911. MAX | 0.00 | 0.00
  912. -------|--------------|----------------
  913. 14kHz | 24kbit | 32kbit | 48kbit
  914. -------|--------------|----------------|----------------
  915. AVG | 0.00 | 0.00 | 0.00
  916. -------|--------------|----------------|----------------
  917. MAX | 0.00 | 0.00 | 0.00
  918. -------|--------------|----------------|----------------
  919. ****************************************************************************************/
  920. Word16 get_rand(Rand_Obj *randobj)
  921. {
  922. Word16 random_word;
  923. Word32 acca;
  924. acca = L_add(randobj->seed0,randobj->seed3);
  925. random_word = extract_l(acca);
  926. logic16();
  927. test();
  928. if ((random_word & 32768L) != 0)
  929. random_word = add(random_word,1);
  930. randobj->seed3 = randobj->seed2;
  931. move16();
  932. randobj->seed2 = randobj->seed1;
  933. move16();
  934. randobj->seed1 = randobj->seed0;
  935. move16();
  936. randobj->seed0 = random_word;
  937. move16();
  938. return(random_word);
  939. }