sam2coef.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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: samples_to_rmlt_coefs.c
  14. *
  15. * Purpose: Convert Samples to Reversed MLT (Modulated Lapped Transform)
  16. * Coefficients
  17. *
  18. * The "Reversed MLT" is an overlapped block transform which uses
  19. * even symmetry * on the left, odd symmetry on the right and a
  20. * Type IV DCT as the block transform. * It is thus similar to a
  21. * MLT which uses odd symmetry on the left, even symmetry * on the
  22. * right and a Type IV DST as the block transform. In fact, it is
  23. * equivalent * to reversing the order of the samples, performing
  24. * an MLT and then negating all * the even-numbered coefficients.
  25. *
  26. ******************************************************************************/
  27. /***************************************************************************
  28. Include files
  29. ***************************************************************************/
  30. #include "defs.h"
  31. #include "tables.h"
  32. #include "count.h"
  33. /***************************************************************************
  34. Function: samples_to_rmlt_coefs
  35. Syntax: Word16 samples_to_rmlt_coefs(new_samples,
  36. old_samples,
  37. coefs,
  38. dct_length)
  39. Word16 *new_samples;
  40. Word16 *old_samples;
  41. Word16 *coefs;
  42. Word16 dct_length;
  43. Description: Convert samples to MLT coefficients
  44. Design Notes:
  45. WMOPS: 7kHz | 24kbit | 32kbit
  46. -------|--------------|----------------
  47. AVG | 1.40 | 1.40
  48. -------|--------------|----------------
  49. MAX | 1.40 | 1.40
  50. -------|--------------|----------------
  51. 14kHz | 24kbit | 32kbit | 48kbit
  52. -------|--------------|----------------|----------------
  53. AVG | 3.07 | 3.07 | 3.07
  54. -------|--------------|----------------|----------------
  55. MAX | 3.10 | 3.10 | 3.10
  56. -------|--------------|----------------|----------------
  57. ***************************************************************************/
  58. Word16 samples_to_rmlt_coefs(const Word16 *new_samples,Word16 *old_samples,Word16 *coefs,Word16 dct_length)
  59. {
  60. Word16 index, vals_left,mag_shift,n;
  61. Word16 windowed_data[MAX_DCT_LENGTH];
  62. Word16 *old_ptr;
  63. const Word16 *new_ptr, *sam_low, *sam_high;
  64. Word16 *win_low, *win_high;
  65. Word16 *dst_ptr;
  66. Word16 neg_win_low;
  67. Word16 samp_high;
  68. Word16 half_dct_size;
  69. Word32 acca;
  70. Word32 accb;
  71. Word16 temp;
  72. Word16 temp1;
  73. Word16 temp2;
  74. Word16 temp5;
  75. half_dct_size = shr_nocheck(dct_length,1);
  76. /*++++++++++++++++++++++++++++++++++++++++++++*/
  77. /* Get the first half of the windowed samples */
  78. /*++++++++++++++++++++++++++++++++++++++++++++*/
  79. dst_ptr = windowed_data;
  80. move16();
  81. /* address arithmetic */
  82. test();
  83. if (dct_length==DCT_LENGTH)
  84. {
  85. win_high = samples_to_rmlt_window + half_dct_size;
  86. }
  87. else
  88. {
  89. win_high = max_samples_to_rmlt_window + half_dct_size;
  90. }
  91. win_low = win_high;
  92. move16();
  93. /* address arithmetic */
  94. sam_high = old_samples + half_dct_size;
  95. sam_low = sam_high;
  96. move16();
  97. for (vals_left = half_dct_size;vals_left > 0;vals_left--)
  98. {
  99. acca = 0L;
  100. move32();
  101. acca = L_mac(acca,*--win_low, *--sam_low);
  102. acca = L_mac(acca,*win_high++, *sam_high++);
  103. temp = itu_round(acca);
  104. *dst_ptr++ = temp;
  105. move16();
  106. }
  107. /*+++++++++++++++++++++++++++++++++++++++++++++*/
  108. /* Get the second half of the windowed samples */
  109. /*+++++++++++++++++++++++++++++++++++++++++++++*/
  110. sam_low = new_samples;
  111. move16();
  112. /* address arithmetic */
  113. sam_high = new_samples + dct_length;
  114. for (vals_left = half_dct_size; vals_left > 0; vals_left--)
  115. {
  116. acca = 0L;
  117. move32();
  118. acca = L_mac(acca,*--win_high, *sam_low++);
  119. neg_win_low = negate(*win_low++);
  120. samp_high = *--sam_high;
  121. acca = L_mac(acca, neg_win_low, samp_high);
  122. temp = itu_round(acca);
  123. *dst_ptr++=temp;
  124. move16();
  125. }
  126. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  127. /* Save the new samples for next time, when they will be the old samples */
  128. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  129. new_ptr = new_samples;
  130. move16();
  131. old_ptr = old_samples;
  132. move16();
  133. for (vals_left = dct_length;vals_left > 0;vals_left--)
  134. {
  135. *old_ptr++ = *new_ptr++;
  136. move16();
  137. }
  138. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  139. /* Calculate how many bits to shift up the input to the DCT. */
  140. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  141. temp1=0;
  142. move16();
  143. for(index=0;index<dct_length;index++)
  144. {
  145. temp2 = abs_s(windowed_data[index]);
  146. temp = sub(temp2,temp1);
  147. test();
  148. if(temp > 0)
  149. {
  150. move16();
  151. temp1 = temp2;
  152. }
  153. }
  154. mag_shift=0;
  155. move16();
  156. temp = sub(temp1,14000);
  157. test();
  158. if (temp >= 0)
  159. {
  160. mag_shift = 0;
  161. move16();
  162. }
  163. else
  164. {
  165. temp = sub(temp1,438);
  166. test();
  167. if(temp < 0)
  168. temp = add(temp1,1);
  169. else
  170. {
  171. temp = temp1;
  172. move16();
  173. }
  174. accb = L_mult(temp,9587);
  175. acca = L_shr_nocheck(accb,20);
  176. temp5 = extract_l(acca);
  177. temp = norm_s(temp5);
  178. test();
  179. if (temp == 0)
  180. {
  181. mag_shift = 9;
  182. move16();
  183. }
  184. else
  185. mag_shift = sub(temp,6);
  186. }
  187. acca = 0L;
  188. move32();
  189. for(index=0; index<dct_length; index++)
  190. {
  191. temp = abs_s( windowed_data[index]);
  192. acca = L_add(acca,temp);
  193. }
  194. acca = L_shr_nocheck(acca,7);
  195. test();
  196. if (temp1 < acca)
  197. {
  198. mag_shift = sub(mag_shift,1);
  199. }
  200. test();
  201. if (mag_shift > 0)
  202. {
  203. for(index=0;index<dct_length;index++)
  204. {
  205. windowed_data[index] = shl_nocheck(windowed_data[index],mag_shift);
  206. }
  207. }
  208. else
  209. {
  210. test();
  211. if (mag_shift < 0)
  212. {
  213. n = negate(mag_shift);
  214. for(index=0;index<dct_length;index++)
  215. {
  216. windowed_data[index] = shr_nocheck(windowed_data[index],n);
  217. move16();
  218. }
  219. }
  220. }
  221. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  222. /* Perform a Type IV DCT on the windowed data to get the coefficients */
  223. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  224. dct_type_iv_a(windowed_data, coefs, dct_length);
  225. return(mag_shift);
  226. }