wma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * WMA compatible codec
  3. * Copyright (c) 2002-2007 The FFmpeg Project.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "wma.h"
  23. #include "wmadata.h"
  24. #undef NDEBUG
  25. #include <assert.h>
  26. /* XXX: use same run/length optimization as mpeg decoders */
  27. //FIXME maybe split decode / encode or pass flag
  28. static void init_coef_vlc(VLC *vlc,
  29. uint16_t **prun_table, uint16_t **plevel_table, uint16_t **pint_table,
  30. const CoefVLCTable *vlc_table)
  31. {
  32. int n = vlc_table->n;
  33. const uint8_t *table_bits = vlc_table->huffbits;
  34. const uint32_t *table_codes = vlc_table->huffcodes;
  35. const uint16_t *levels_table = vlc_table->levels;
  36. uint16_t *run_table, *level_table, *int_table;
  37. int i, l, j, k, level;
  38. init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
  39. run_table = av_malloc(n * sizeof(uint16_t));
  40. level_table = av_malloc(n * sizeof(uint16_t));
  41. int_table = av_malloc(n * sizeof(uint16_t));
  42. i = 2;
  43. level = 1;
  44. k = 0;
  45. while (i < n) {
  46. int_table[k]= i;
  47. l = levels_table[k++];
  48. for(j=0;j<l;j++) {
  49. run_table[i] = j;
  50. level_table[i] = level;
  51. i++;
  52. }
  53. level++;
  54. }
  55. *prun_table = run_table;
  56. *plevel_table = level_table;
  57. *pint_table= int_table;
  58. }
  59. int ff_wma_init(AVCodecContext * avctx, int flags2)
  60. {
  61. WMACodecContext *s = avctx->priv_data;
  62. int i;
  63. float *window;
  64. float bps1, high_freq;
  65. volatile float bps;
  66. int sample_rate1;
  67. int coef_vlc_table;
  68. s->sample_rate = avctx->sample_rate;
  69. s->nb_channels = avctx->channels;
  70. s->bit_rate = avctx->bit_rate;
  71. s->block_align = avctx->block_align;
  72. dsputil_init(&s->dsp, avctx);
  73. if (avctx->codec->id == CODEC_ID_WMAV1) {
  74. s->version = 1;
  75. } else {
  76. s->version = 2;
  77. }
  78. /* compute MDCT block size */
  79. if (s->sample_rate <= 16000) {
  80. s->frame_len_bits = 9;
  81. } else if (s->sample_rate <= 22050 ||
  82. (s->sample_rate <= 32000 && s->version == 1)) {
  83. s->frame_len_bits = 10;
  84. } else {
  85. s->frame_len_bits = 11;
  86. }
  87. s->frame_len = 1 << s->frame_len_bits;
  88. if (s->use_variable_block_len) {
  89. int nb_max, nb;
  90. nb = ((flags2 >> 3) & 3) + 1;
  91. if ((s->bit_rate / s->nb_channels) >= 32000)
  92. nb += 2;
  93. nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
  94. if (nb > nb_max)
  95. nb = nb_max;
  96. s->nb_block_sizes = nb + 1;
  97. } else {
  98. s->nb_block_sizes = 1;
  99. }
  100. /* init rate dependent parameters */
  101. s->use_noise_coding = 1;
  102. high_freq = s->sample_rate * 0.5;
  103. /* if version 2, then the rates are normalized */
  104. sample_rate1 = s->sample_rate;
  105. if (s->version == 2) {
  106. if (sample_rate1 >= 44100)
  107. sample_rate1 = 44100;
  108. else if (sample_rate1 >= 22050)
  109. sample_rate1 = 22050;
  110. else if (sample_rate1 >= 16000)
  111. sample_rate1 = 16000;
  112. else if (sample_rate1 >= 11025)
  113. sample_rate1 = 11025;
  114. else if (sample_rate1 >= 8000)
  115. sample_rate1 = 8000;
  116. }
  117. bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
  118. s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
  119. /* compute high frequency value and choose if noise coding should
  120. be activated */
  121. bps1 = bps;
  122. if (s->nb_channels == 2)
  123. bps1 = bps * 1.6;
  124. if (sample_rate1 == 44100) {
  125. if (bps1 >= 0.61)
  126. s->use_noise_coding = 0;
  127. else
  128. high_freq = high_freq * 0.4;
  129. } else if (sample_rate1 == 22050) {
  130. if (bps1 >= 1.16)
  131. s->use_noise_coding = 0;
  132. else if (bps1 >= 0.72)
  133. high_freq = high_freq * 0.7;
  134. else
  135. high_freq = high_freq * 0.6;
  136. } else if (sample_rate1 == 16000) {
  137. if (bps > 0.5)
  138. high_freq = high_freq * 0.5;
  139. else
  140. high_freq = high_freq * 0.3;
  141. } else if (sample_rate1 == 11025) {
  142. high_freq = high_freq * 0.7;
  143. } else if (sample_rate1 == 8000) {
  144. if (bps <= 0.625) {
  145. high_freq = high_freq * 0.5;
  146. } else if (bps > 0.75) {
  147. s->use_noise_coding = 0;
  148. } else {
  149. high_freq = high_freq * 0.65;
  150. }
  151. } else {
  152. if (bps >= 0.8) {
  153. high_freq = high_freq * 0.75;
  154. } else if (bps >= 0.6) {
  155. high_freq = high_freq * 0.6;
  156. } else {
  157. high_freq = high_freq * 0.5;
  158. }
  159. }
  160. dprintf(s->avctx, "flags2=0x%x\n", flags2);
  161. dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
  162. s->version, s->nb_channels, s->sample_rate, s->bit_rate,
  163. s->block_align);
  164. dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
  165. bps, bps1, high_freq, s->byte_offset_bits);
  166. dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
  167. s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
  168. /* compute the scale factor band sizes for each MDCT block size */
  169. {
  170. int a, b, pos, lpos, k, block_len, i, j, n;
  171. const uint8_t *table;
  172. if (s->version == 1) {
  173. s->coefs_start = 3;
  174. } else {
  175. s->coefs_start = 0;
  176. }
  177. for(k = 0; k < s->nb_block_sizes; k++) {
  178. block_len = s->frame_len >> k;
  179. if (s->version == 1) {
  180. lpos = 0;
  181. for(i=0;i<25;i++) {
  182. a = wma_critical_freqs[i];
  183. b = s->sample_rate;
  184. pos = ((block_len * 2 * a) + (b >> 1)) / b;
  185. if (pos > block_len)
  186. pos = block_len;
  187. s->exponent_bands[0][i] = pos - lpos;
  188. if (pos >= block_len) {
  189. i++;
  190. break;
  191. }
  192. lpos = pos;
  193. }
  194. s->exponent_sizes[0] = i;
  195. } else {
  196. /* hardcoded tables */
  197. table = NULL;
  198. a = s->frame_len_bits - BLOCK_MIN_BITS - k;
  199. if (a < 3) {
  200. if (s->sample_rate >= 44100)
  201. table = exponent_band_44100[a];
  202. else if (s->sample_rate >= 32000)
  203. table = exponent_band_32000[a];
  204. else if (s->sample_rate >= 22050)
  205. table = exponent_band_22050[a];
  206. }
  207. if (table) {
  208. n = *table++;
  209. for(i=0;i<n;i++)
  210. s->exponent_bands[k][i] = table[i];
  211. s->exponent_sizes[k] = n;
  212. } else {
  213. j = 0;
  214. lpos = 0;
  215. for(i=0;i<25;i++) {
  216. a = wma_critical_freqs[i];
  217. b = s->sample_rate;
  218. pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
  219. pos <<= 2;
  220. if (pos > block_len)
  221. pos = block_len;
  222. if (pos > lpos)
  223. s->exponent_bands[k][j++] = pos - lpos;
  224. if (pos >= block_len)
  225. break;
  226. lpos = pos;
  227. }
  228. s->exponent_sizes[k] = j;
  229. }
  230. }
  231. /* max number of coefs */
  232. s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
  233. /* high freq computation */
  234. s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
  235. s->sample_rate + 0.5);
  236. n = s->exponent_sizes[k];
  237. j = 0;
  238. pos = 0;
  239. for(i=0;i<n;i++) {
  240. int start, end;
  241. start = pos;
  242. pos += s->exponent_bands[k][i];
  243. end = pos;
  244. if (start < s->high_band_start[k])
  245. start = s->high_band_start[k];
  246. if (end > s->coefs_end[k])
  247. end = s->coefs_end[k];
  248. if (end > start)
  249. s->exponent_high_bands[k][j++] = end - start;
  250. }
  251. s->exponent_high_sizes[k] = j;
  252. #if 0
  253. tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
  254. s->frame_len >> k,
  255. s->coefs_end[k],
  256. s->high_band_start[k],
  257. s->exponent_high_sizes[k]);
  258. for(j=0;j<s->exponent_high_sizes[k];j++)
  259. tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
  260. tprintf(s->avctx, "\n");
  261. #endif
  262. }
  263. }
  264. #ifdef TRACE
  265. {
  266. int i, j;
  267. for(i = 0; i < s->nb_block_sizes; i++) {
  268. tprintf(s->avctx, "%5d: n=%2d:",
  269. s->frame_len >> i,
  270. s->exponent_sizes[i]);
  271. for(j=0;j<s->exponent_sizes[i];j++)
  272. tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
  273. tprintf(s->avctx, "\n");
  274. }
  275. }
  276. #endif
  277. /* init MDCT windows : simple sinus window */
  278. for(i = 0; i < s->nb_block_sizes; i++) {
  279. int n, j;
  280. float alpha;
  281. n = 1 << (s->frame_len_bits - i);
  282. window = av_malloc(sizeof(float) * n);
  283. alpha = M_PI / (2.0 * n);
  284. for(j=0;j<n;j++) {
  285. window[j] = sin((j + 0.5) * alpha);
  286. }
  287. s->windows[i] = window;
  288. }
  289. s->reset_block_lengths = 1;
  290. if (s->use_noise_coding) {
  291. /* init the noise generator */
  292. if (s->use_exp_vlc)
  293. s->noise_mult = 0.02;
  294. else
  295. s->noise_mult = 0.04;
  296. #ifdef TRACE
  297. for(i=0;i<NOISE_TAB_SIZE;i++)
  298. s->noise_table[i] = 1.0 * s->noise_mult;
  299. #else
  300. {
  301. unsigned int seed;
  302. float norm;
  303. seed = 1;
  304. norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
  305. for(i=0;i<NOISE_TAB_SIZE;i++) {
  306. seed = seed * 314159 + 1;
  307. s->noise_table[i] = (float)((int)seed) * norm;
  308. }
  309. }
  310. #endif
  311. }
  312. /* choose the VLC tables for the coefficients */
  313. coef_vlc_table = 2;
  314. if (s->sample_rate >= 32000) {
  315. if (bps1 < 0.72)
  316. coef_vlc_table = 0;
  317. else if (bps1 < 1.16)
  318. coef_vlc_table = 1;
  319. }
  320. s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
  321. s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
  322. init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
  323. s->coef_vlcs[0]);
  324. init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
  325. s->coef_vlcs[1]);
  326. return 0;
  327. }
  328. int ff_wma_total_gain_to_bits(int total_gain){
  329. if (total_gain < 15) return 13;
  330. else if (total_gain < 32) return 12;
  331. else if (total_gain < 40) return 11;
  332. else if (total_gain < 45) return 10;
  333. else return 9;
  334. }
  335. int ff_wma_end(AVCodecContext *avctx)
  336. {
  337. WMACodecContext *s = avctx->priv_data;
  338. int i;
  339. for(i = 0; i < s->nb_block_sizes; i++)
  340. ff_mdct_end(&s->mdct_ctx[i]);
  341. for(i = 0; i < s->nb_block_sizes; i++)
  342. av_free(s->windows[i]);
  343. if (s->use_exp_vlc) {
  344. free_vlc(&s->exp_vlc);
  345. }
  346. if (s->use_noise_coding) {
  347. free_vlc(&s->hgain_vlc);
  348. }
  349. for(i = 0;i < 2; i++) {
  350. free_vlc(&s->coef_vlc[i]);
  351. av_free(s->run_table[i]);
  352. av_free(s->level_table[i]);
  353. }
  354. return 0;
  355. }