You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
12KB

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