common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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: common.c
  14. Purpose: Contains the functions used for both G.722.1 Annex C encoder and decoder
  15. Design Notes:
  16. ****************************************************************************************/
  17. /****************************************************************************************
  18. Include files
  19. ****************************************************************************************/
  20. #include "defs.h"
  21. #include "huff_def.h"
  22. #include "huff_tab.h"
  23. #include "tables.h"
  24. #include "count.h"
  25. /****************************************************************************************
  26. Function: categorize
  27. Syntax: void categorize(Word16 number_of_available_bits,
  28. Word16 number_of_regions,
  29. Word16 num_categorization_control_possibilities,
  30. Word16 rms_index,
  31. Word16 power_categories,
  32. Word16 category_balances)
  33. inputs: number_of_regions
  34. num_categorization_control_possibilities
  35. number_of_available_bits
  36. rms_index[MAX_NUMBER_OF_REGIONS]
  37. outputs: power_categories[MAX_NUMBER_OF_REGIONS]
  38. category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1]
  39. Description: Computes a series of categorizations
  40. WMOPS: 7kHz | 24kbit | 32kbit
  41. -------|--------------|----------------
  42. AVG | 0.14 | 0.14
  43. -------|--------------|----------------
  44. MAX | 0.15 | 0.15
  45. -------|--------------|----------------
  46. 14kHz | 24kbit | 32kbit | 48kbit
  47. -------|--------------|----------------|----------------
  48. AVG | 0.42 | 0.45 | 0.48
  49. -------|--------------|----------------|----------------
  50. MAX | 0.47 | 0.52 | 0.52
  51. -------|--------------|----------------|----------------
  52. ****************************************************************************************/
  53. void categorize(Word16 number_of_available_bits,
  54. Word16 number_of_regions,
  55. Word16 num_categorization_control_possibilities,
  56. Word16 *rms_index,
  57. Word16 *power_categories,
  58. Word16 *category_balances)
  59. {
  60. Word16 offset;
  61. Word16 temp;
  62. Word16 frame_size;
  63. /* At higher bit rates, there is an increase for most categories in average bit
  64. consumption per region. We compensate for this by pretending we have fewer
  65. available bits. */
  66. test();
  67. if (number_of_regions == NUMBER_OF_REGIONS)
  68. {
  69. frame_size = DCT_LENGTH;
  70. }
  71. else
  72. {
  73. frame_size = MAX_DCT_LENGTH;
  74. }
  75. temp = sub(number_of_available_bits,frame_size);
  76. test();
  77. if (temp > 0)
  78. {
  79. number_of_available_bits = sub(number_of_available_bits,frame_size);
  80. number_of_available_bits = extract_l(L_mult0(number_of_available_bits,5));
  81. number_of_available_bits = shr_nocheck(number_of_available_bits,3);
  82. number_of_available_bits = add(number_of_available_bits,frame_size);
  83. }
  84. /* calculate the offset using the original category assignments */
  85. offset = calc_offset(rms_index,number_of_regions,number_of_available_bits);
  86. /* compute the power categories based on the uniform offset */
  87. compute_raw_pow_categories(power_categories,rms_index,number_of_regions,offset);
  88. /* adjust the category assignments */
  89. /* compute the new power categories and category balances */
  90. comp_powercat_and_catbalance(power_categories,category_balances,rms_index,number_of_available_bits,number_of_regions,num_categorization_control_possibilities,offset);
  91. }
  92. /***************************************************************************
  93. Function: comp_powercat_and_catbalance
  94. Syntax: void comp_powercat_and_catbalance(Word16 *power_categories,
  95. Word16 *category_balances,
  96. Word16 *rms_index,
  97. Word16 number_of_available_bits,
  98. Word16 number_of_regions,
  99. Word16 num_categorization_control_possibilities,
  100. Word16 offset)
  101. inputs: *rms_index
  102. number_of_available_bits
  103. number_of_regions
  104. num_categorization_control_possibilities
  105. offset
  106. outputs: *power_categories
  107. *category_balances
  108. Description: Computes the power_categories and the category balances
  109. WMOPS: 7kHz | 24kbit | 32kbit
  110. -------|--------------|----------------
  111. AVG | 0.10 | 0.10
  112. -------|--------------|----------------
  113. MAX | 0.11 | 0.11
  114. -------|--------------|----------------
  115. 14kHz | 24kbit | 32kbit | 48kbit
  116. -------|--------------|----------------|----------------
  117. AVG | 0.32 | 0.35 | 0.38
  118. -------|--------------|----------------|----------------
  119. MAX | 0.38 | 0.42 | 0.43
  120. -------|--------------|----------------|----------------
  121. ***************************************************************************/
  122. void comp_powercat_and_catbalance(Word16 *power_categories,
  123. Word16 *category_balances,
  124. Word16 *rms_index,
  125. Word16 number_of_available_bits,
  126. Word16 number_of_regions,
  127. Word16 num_categorization_control_possibilities,
  128. Word16 offset)
  129. {
  130. Word16 expected_number_of_code_bits;
  131. Word16 region;
  132. Word16 max_region;
  133. Word16 j;
  134. Word16 max_rate_categories[MAX_NUMBER_OF_REGIONS];
  135. Word16 min_rate_categories[MAX_NUMBER_OF_REGIONS];
  136. Word16 temp_category_balances[2*MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES];
  137. Word16 raw_max, raw_min;
  138. Word16 raw_max_index=0, raw_min_index=0;
  139. Word16 max_rate_pointer, min_rate_pointer;
  140. Word16 max, min;
  141. Word16 itemp0;
  142. Word16 itemp1;
  143. Word16 min_plus_max;
  144. Word16 two_x_number_of_available_bits;
  145. Word16 temp;
  146. expected_number_of_code_bits = 0;
  147. move16();
  148. for (region=0; region<number_of_regions; region++)
  149. expected_number_of_code_bits = add(expected_number_of_code_bits,expected_bits_table[power_categories[region]]);
  150. for (region=0; region<number_of_regions; region++)
  151. {
  152. max_rate_categories[region] = power_categories[region];
  153. move16();
  154. min_rate_categories[region] = power_categories[region];
  155. move16();
  156. }
  157. max = expected_number_of_code_bits;
  158. move16();
  159. min = expected_number_of_code_bits;
  160. move16();
  161. max_rate_pointer = num_categorization_control_possibilities;
  162. move16();
  163. min_rate_pointer = num_categorization_control_possibilities;
  164. move16();
  165. for (j=0; j<num_categorization_control_possibilities-1; j++)
  166. {
  167. min_plus_max = add(max,min);
  168. two_x_number_of_available_bits = shl_nocheck(number_of_available_bits,1);
  169. temp = sub(min_plus_max,two_x_number_of_available_bits);
  170. test();
  171. if (temp <= 0)
  172. {
  173. raw_min = 99;
  174. move16();
  175. /* Search from lowest freq regions to highest for best */
  176. /* region to reassign to a higher bit rate category. */
  177. for (region=0; region<number_of_regions; region++)
  178. {
  179. test();
  180. if (max_rate_categories[region] > 0)
  181. {
  182. itemp0 = shl_nocheck(max_rate_categories[region],1);
  183. itemp1 = sub(offset,rms_index[region]);
  184. itemp0 = sub(itemp1,itemp0);
  185. temp = sub(itemp0,raw_min);
  186. test();
  187. if (temp < 0)
  188. {
  189. raw_min = itemp0;
  190. raw_min_index = region;
  191. }
  192. }
  193. }
  194. max_rate_pointer = sub(max_rate_pointer,1);
  195. temp_category_balances[max_rate_pointer] = raw_min_index;
  196. move16();
  197. max = sub(max,expected_bits_table[max_rate_categories[raw_min_index]]);
  198. max_rate_categories[raw_min_index] = sub(max_rate_categories[raw_min_index],1);
  199. move16();
  200. max = add(max,expected_bits_table[max_rate_categories[raw_min_index]]);
  201. }
  202. else
  203. {
  204. raw_max = -99;
  205. move16();
  206. /* Search from highest freq regions to lowest for best region to reassign to
  207. a lower bit rate category. */
  208. max_region = sub(number_of_regions,1);
  209. for (region= max_region; region >= 0; region--)
  210. {
  211. temp = sub(min_rate_categories[region],(NUM_CATEGORIES-1));
  212. test();
  213. if (temp < 0)
  214. {
  215. itemp0 = shl_nocheck(min_rate_categories[region],1);
  216. itemp1 = sub(offset,rms_index[region]);
  217. itemp0 = sub(itemp1,itemp0);
  218. temp = sub(itemp0,raw_max);
  219. test();
  220. if (temp > 0)
  221. {
  222. raw_max = itemp0;
  223. move16();
  224. raw_max_index = region;
  225. move16();
  226. }
  227. }
  228. }
  229. temp_category_balances[min_rate_pointer] = raw_max_index;
  230. move16();
  231. min_rate_pointer = add(min_rate_pointer,1);
  232. min = sub(min,expected_bits_table[min_rate_categories[raw_max_index]]);
  233. min_rate_categories[raw_max_index] = add(min_rate_categories[raw_max_index],1);
  234. move16();
  235. min = add(min,expected_bits_table[min_rate_categories[raw_max_index]]);
  236. }
  237. }
  238. for (region=0; region<number_of_regions; region++)
  239. {
  240. power_categories[region] = max_rate_categories[region];
  241. move16();
  242. }
  243. for (j=0; j<num_categorization_control_possibilities-1; j++)
  244. {
  245. category_balances[j] = temp_category_balances[max_rate_pointer++];
  246. move16();
  247. }
  248. }
  249. /***************************************************************************
  250. Function: calc_offset
  251. Syntax: offset=calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits)
  252. input: Word16 *rms_index
  253. Word16 number_of_regions
  254. Word16 available_bits
  255. output: Word16 offset
  256. Description: Calculates the the category offset. This is the shift required
  257. To get the most out of the number of available bits. A binary
  258. type search is used to find the offset.
  259. WMOPS: 7kHz | 24kbit | 32kbit
  260. -------|--------------|----------------
  261. AVG | 0.04 | 0.04
  262. -------|--------------|----------------
  263. MAX | 0.04 | 0.04
  264. -------|--------------|----------------
  265. 14kHz | 24kbit | 32kbit | 48kbit
  266. -------|--------------|----------------|----------------
  267. AVG | 0.08 | 0.08 | 0.08
  268. -------|--------------|----------------|----------------
  269. MAX | 0.09 | 0.09 | 0.09
  270. -------|--------------|----------------|----------------
  271. ***************************************************************************/
  272. Word16 calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits)
  273. {
  274. Word16 answer;
  275. Word16 delta;
  276. Word16 test_offset;
  277. Word16 region,j;
  278. Word16 power_cats[MAX_NUMBER_OF_REGIONS];
  279. Word16 bits;
  280. Word16 offset;
  281. Word16 temp;
  282. /* initialize vars */
  283. answer = -32;
  284. move16();
  285. delta = 32;
  286. move16();
  287. do
  288. {
  289. test_offset = add(answer,delta);
  290. /* obtain a category for each region */
  291. /* using the test offset */
  292. for (region=0; region<number_of_regions; region++)
  293. {
  294. j = sub(test_offset,rms_index[region]);
  295. j = shr_nocheck(j,1);
  296. /* Ensure j is between 0 and NUM_CAT-1 */
  297. test();
  298. if (j < 0)
  299. {
  300. j = 0;
  301. move16();
  302. }
  303. temp = sub(j,NUM_CATEGORIES-1);
  304. test();
  305. if (temp > 0)
  306. {
  307. j = sub(NUM_CATEGORIES,1);
  308. move16();
  309. }
  310. power_cats[region] = j;
  311. move16();
  312. }
  313. bits = 0;
  314. move16();
  315. /* compute the number of bits that will be used given the cat assignments */
  316. for (region=0; region<number_of_regions; region++)
  317. bits = add(bits,expected_bits_table[power_cats[region]]);
  318. /* if (bits > available_bits - 32) then divide the offset region for the bin search */
  319. offset = sub(available_bits,32);
  320. temp = sub(bits,offset);
  321. test();
  322. if (temp >= 0)
  323. {
  324. answer = test_offset;
  325. move16();
  326. }
  327. delta = shr_nocheck(delta,1);
  328. test(); /* for the while loop */
  329. } while (delta > 0);
  330. return(answer);
  331. }
  332. /***************************************************************************
  333. Function: compute_raw_pow_categories
  334. Syntax: void compute_raw_pow_categories(Word16 *power_categories,
  335. Word16 *rms_index,
  336. Word16 number_of_regions,
  337. Word16 offset)
  338. inputs: *rms_index
  339. number_of_regions
  340. offset
  341. outputs: *power_categories
  342. Description: This function computes the power categories given the offset
  343. This is kind of redundant since they were already computed
  344. in calc_offset to determine the offset.
  345. WMOPS: | 24kbit | 32kbit
  346. -------|--------------|----------------
  347. AVG | 0.01 | 0.01
  348. -------|--------------|----------------
  349. MAX | 0.01 | 0.01
  350. -------|--------------|----------------
  351. 14kHz | 24kbit | 32kbit | 48kbit
  352. -------|--------------|----------------|----------------
  353. AVG | 0.01 | 0.01 | 0.01
  354. -------|--------------|----------------|----------------
  355. MAX | 0.01 | 0.01 | 0.01
  356. -------|--------------|----------------|----------------
  357. ***************************************************************************/
  358. void compute_raw_pow_categories(Word16 *power_categories,Word16 *rms_index,Word16 number_of_regions,Word16 offset)
  359. {
  360. Word16 region;
  361. Word16 j;
  362. Word16 temp;
  363. for (region=0; region<number_of_regions; region++)
  364. {
  365. j = sub(offset,rms_index[region]);
  366. j = shr_nocheck(j,1);
  367. /* make sure j is between 0 and NUM_CAT-1 */
  368. test();
  369. if (j < 0)
  370. {
  371. j = 0;
  372. move16();
  373. }
  374. temp = sub(j,(NUM_CATEGORIES-1));
  375. test();
  376. if (temp > 0)
  377. j = sub(NUM_CATEGORIES,1);
  378. power_categories[region] = j;
  379. move16();
  380. }
  381. }