wma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 bps1, high_freq;
  64. volatile float bps;
  65. int sample_rate1;
  66. int coef_vlc_table;
  67. if( avctx->sample_rate<=0 || avctx->sample_rate>50000
  68. || avctx->channels<=0 || avctx->channels>8
  69. || avctx->bit_rate<=0)
  70. return -1;
  71. s->sample_rate = avctx->sample_rate;
  72. s->nb_channels = avctx->channels;
  73. s->bit_rate = avctx->bit_rate;
  74. s->block_align = avctx->block_align;
  75. dsputil_init(&s->dsp, avctx);
  76. if (avctx->codec->id == CODEC_ID_WMAV1) {
  77. s->version = 1;
  78. } else {
  79. s->version = 2;
  80. }
  81. /* compute MDCT block size */
  82. if (s->sample_rate <= 16000) {
  83. s->frame_len_bits = 9;
  84. } else if (s->sample_rate <= 22050 ||
  85. (s->sample_rate <= 32000 && s->version == 1)) {
  86. s->frame_len_bits = 10;
  87. } else {
  88. s->frame_len_bits = 11;
  89. }
  90. s->frame_len = 1 << s->frame_len_bits;
  91. if (s->use_variable_block_len) {
  92. int nb_max, nb;
  93. nb = ((flags2 >> 3) & 3) + 1;
  94. if ((s->bit_rate / s->nb_channels) >= 32000)
  95. nb += 2;
  96. nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
  97. if (nb > nb_max)
  98. nb = nb_max;
  99. s->nb_block_sizes = nb + 1;
  100. } else {
  101. s->nb_block_sizes = 1;
  102. }
  103. /* init rate dependent parameters */
  104. s->use_noise_coding = 1;
  105. high_freq = s->sample_rate * 0.5;
  106. /* if version 2, then the rates are normalized */
  107. sample_rate1 = s->sample_rate;
  108. if (s->version == 2) {
  109. if (sample_rate1 >= 44100)
  110. sample_rate1 = 44100;
  111. else if (sample_rate1 >= 22050)
  112. sample_rate1 = 22050;
  113. else if (sample_rate1 >= 16000)
  114. sample_rate1 = 16000;
  115. else if (sample_rate1 >= 11025)
  116. sample_rate1 = 11025;
  117. else if (sample_rate1 >= 8000)
  118. sample_rate1 = 8000;
  119. }
  120. bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
  121. s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
  122. if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
  123. av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
  124. return AVERROR_PATCHWELCOME;
  125. }
  126. /* compute high frequency value and choose if noise coding should
  127. be activated */
  128. bps1 = bps;
  129. if (s->nb_channels == 2)
  130. bps1 = bps * 1.6;
  131. if (sample_rate1 == 44100) {
  132. if (bps1 >= 0.61)
  133. s->use_noise_coding = 0;
  134. else
  135. high_freq = high_freq * 0.4;
  136. } else if (sample_rate1 == 22050) {
  137. if (bps1 >= 1.16)
  138. s->use_noise_coding = 0;
  139. else if (bps1 >= 0.72)
  140. high_freq = high_freq * 0.7;
  141. else
  142. high_freq = high_freq * 0.6;
  143. } else if (sample_rate1 == 16000) {
  144. if (bps > 0.5)
  145. high_freq = high_freq * 0.5;
  146. else
  147. high_freq = high_freq * 0.3;
  148. } else if (sample_rate1 == 11025) {
  149. high_freq = high_freq * 0.7;
  150. } else if (sample_rate1 == 8000) {
  151. if (bps <= 0.625) {
  152. high_freq = high_freq * 0.5;
  153. } else if (bps > 0.75) {
  154. s->use_noise_coding = 0;
  155. } else {
  156. high_freq = high_freq * 0.65;
  157. }
  158. } else {
  159. if (bps >= 0.8) {
  160. high_freq = high_freq * 0.75;
  161. } else if (bps >= 0.6) {
  162. high_freq = high_freq * 0.6;
  163. } else {
  164. high_freq = high_freq * 0.5;
  165. }
  166. }
  167. dprintf(s->avctx, "flags2=0x%x\n", flags2);
  168. dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
  169. s->version, s->nb_channels, s->sample_rate, s->bit_rate,
  170. s->block_align);
  171. dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
  172. bps, bps1, high_freq, s->byte_offset_bits);
  173. dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
  174. s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
  175. /* compute the scale factor band sizes for each MDCT block size */
  176. {
  177. int a, b, pos, lpos, k, block_len, i, j, n;
  178. const uint8_t *table;
  179. if (s->version == 1) {
  180. s->coefs_start = 3;
  181. } else {
  182. s->coefs_start = 0;
  183. }
  184. for(k = 0; k < s->nb_block_sizes; k++) {
  185. block_len = s->frame_len >> k;
  186. if (s->version == 1) {
  187. lpos = 0;
  188. for(i=0;i<25;i++) {
  189. a = wma_critical_freqs[i];
  190. b = s->sample_rate;
  191. pos = ((block_len * 2 * a) + (b >> 1)) / b;
  192. if (pos > block_len)
  193. pos = block_len;
  194. s->exponent_bands[0][i] = pos - lpos;
  195. if (pos >= block_len) {
  196. i++;
  197. break;
  198. }
  199. lpos = pos;
  200. }
  201. s->exponent_sizes[0] = i;
  202. } else {
  203. /* hardcoded tables */
  204. table = NULL;
  205. a = s->frame_len_bits - BLOCK_MIN_BITS - k;
  206. if (a < 3) {
  207. if (s->sample_rate >= 44100)
  208. table = exponent_band_44100[a];
  209. else if (s->sample_rate >= 32000)
  210. table = exponent_band_32000[a];
  211. else if (s->sample_rate >= 22050)
  212. table = exponent_band_22050[a];
  213. }
  214. if (table) {
  215. n = *table++;
  216. for(i=0;i<n;i++)
  217. s->exponent_bands[k][i] = table[i];
  218. s->exponent_sizes[k] = n;
  219. } else {
  220. j = 0;
  221. lpos = 0;
  222. for(i=0;i<25;i++) {
  223. a = wma_critical_freqs[i];
  224. b = s->sample_rate;
  225. pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
  226. pos <<= 2;
  227. if (pos > block_len)
  228. pos = block_len;
  229. if (pos > lpos)
  230. s->exponent_bands[k][j++] = pos - lpos;
  231. if (pos >= block_len)
  232. break;
  233. lpos = pos;
  234. }
  235. s->exponent_sizes[k] = j;
  236. }
  237. }
  238. /* max number of coefs */
  239. s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
  240. /* high freq computation */
  241. s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
  242. s->sample_rate + 0.5);
  243. n = s->exponent_sizes[k];
  244. j = 0;
  245. pos = 0;
  246. for(i=0;i<n;i++) {
  247. int start, end;
  248. start = pos;
  249. pos += s->exponent_bands[k][i];
  250. end = pos;
  251. if (start < s->high_band_start[k])
  252. start = s->high_band_start[k];
  253. if (end > s->coefs_end[k])
  254. end = s->coefs_end[k];
  255. if (end > start)
  256. s->exponent_high_bands[k][j++] = end - start;
  257. }
  258. s->exponent_high_sizes[k] = j;
  259. #if 0
  260. tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
  261. s->frame_len >> k,
  262. s->coefs_end[k],
  263. s->high_band_start[k],
  264. s->exponent_high_sizes[k]);
  265. for(j=0;j<s->exponent_high_sizes[k];j++)
  266. tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
  267. tprintf(s->avctx, "\n");
  268. #endif
  269. }
  270. }
  271. #ifdef TRACE
  272. {
  273. int i, j;
  274. for(i = 0; i < s->nb_block_sizes; i++) {
  275. tprintf(s->avctx, "%5d: n=%2d:",
  276. s->frame_len >> i,
  277. s->exponent_sizes[i]);
  278. for(j=0;j<s->exponent_sizes[i];j++)
  279. tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
  280. tprintf(s->avctx, "\n");
  281. }
  282. }
  283. #endif
  284. /* init MDCT windows : simple sinus window */
  285. for(i = 0; i < s->nb_block_sizes; i++) {
  286. int n;
  287. n = 1 << (s->frame_len_bits - i);
  288. ff_sine_window_init(ff_sine_windows[s->frame_len_bits - i - 7], n);
  289. s->windows[i] = ff_sine_windows[s->frame_len_bits - i - 7];
  290. }
  291. s->reset_block_lengths = 1;
  292. if (s->use_noise_coding) {
  293. /* init the noise generator */
  294. if (s->use_exp_vlc)
  295. s->noise_mult = 0.02;
  296. else
  297. s->noise_mult = 0.04;
  298. #ifdef TRACE
  299. for(i=0;i<NOISE_TAB_SIZE;i++)
  300. s->noise_table[i] = 1.0 * s->noise_mult;
  301. #else
  302. {
  303. unsigned int seed;
  304. float norm;
  305. seed = 1;
  306. norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
  307. for(i=0;i<NOISE_TAB_SIZE;i++) {
  308. seed = seed * 314159 + 1;
  309. s->noise_table[i] = (float)((int)seed) * norm;
  310. }
  311. }
  312. #endif
  313. }
  314. /* choose the VLC tables for the coefficients */
  315. coef_vlc_table = 2;
  316. if (s->sample_rate >= 32000) {
  317. if (bps1 < 0.72)
  318. coef_vlc_table = 0;
  319. else if (bps1 < 1.16)
  320. coef_vlc_table = 1;
  321. }
  322. s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
  323. s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
  324. init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
  325. s->coef_vlcs[0]);
  326. init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
  327. s->coef_vlcs[1]);
  328. return 0;
  329. }
  330. int ff_wma_total_gain_to_bits(int total_gain){
  331. if (total_gain < 15) return 13;
  332. else if (total_gain < 32) return 12;
  333. else if (total_gain < 40) return 11;
  334. else if (total_gain < 45) return 10;
  335. else return 9;
  336. }
  337. int ff_wma_end(AVCodecContext *avctx)
  338. {
  339. WMACodecContext *s = avctx->priv_data;
  340. int i;
  341. for(i = 0; i < s->nb_block_sizes; i++)
  342. ff_mdct_end(&s->mdct_ctx[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. av_free(s->int_table[i]);
  354. }
  355. return 0;
  356. }