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.

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