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
22 #include "libavutil/attributes.h"
23
24 #include "avcodec.h"
25 #include "internal.h"
26 #include "sinewin.h"
27 #include "wma.h"
28 #include "wma_common.h"
29 #include "wma_freqs.h"
30 #include "wmadata.h"
31
32 /* XXX: use same run/length optimization as mpeg decoders */
33 // FIXME maybe split decode / encode or pass flag
init_coef_vlc(VLC * vlc,uint16_t ** prun_table,float ** plevel_table,uint16_t ** pint_table,const CoefVLCTable * vlc_table)34 static av_cold int init_coef_vlc(VLC *vlc, uint16_t **prun_table,
35 float **plevel_table, uint16_t **pint_table,
36 const CoefVLCTable *vlc_table)
37 {
38 int n = vlc_table->n;
39 const uint8_t *table_bits = vlc_table->huffbits;
40 const uint32_t *table_codes = vlc_table->huffcodes;
41 const uint16_t *levels_table = vlc_table->levels;
42 uint16_t *run_table, *int_table;
43 float *flevel_table;
44 int i, l, j, k, level;
45
46 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
47
48 run_table = av_malloc_array(n, sizeof(uint16_t));
49 flevel_table = av_malloc_array(n, sizeof(*flevel_table));
50 int_table = av_malloc_array(n, sizeof(uint16_t));
51 if (!run_table || !flevel_table || !int_table) {
52 av_freep(&run_table);
53 av_freep(&flevel_table);
54 av_freep(&int_table);
55 return AVERROR(ENOMEM);
56 }
57 i = 2;
58 level = 1;
59 k = 0;
60 while (i < n) {
61 int_table[k] = i;
62 l = levels_table[k++];
63 for (j = 0; j < l; j++) {
64 run_table[i] = j;
65 flevel_table[i] = level;
66 i++;
67 }
68 level++;
69 }
70 *prun_table = run_table;
71 *plevel_table = flevel_table;
72 *pint_table = int_table;
73
74 return 0;
75 }
76
ff_wma_init(AVCodecContext * avctx,int flags2)77 av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
78 {
79 WMACodecContext *s = avctx->priv_data;
80 int i, ret;
81 float bps1, high_freq;
82 volatile float bps;
83 int sample_rate1;
84 int coef_vlc_table;
85
86 if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
87 avctx->channels <= 0 || avctx->channels > 2 ||
88 avctx->bit_rate <= 0)
89 return -1;
90
91
92 if (avctx->codec->id == AV_CODEC_ID_WMAV1)
93 s->version = 1;
94 else
95 s->version = 2;
96
97 /* compute MDCT block size */
98 s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
99 s->version, 0);
100 s->next_block_len_bits = s->frame_len_bits;
101 s->prev_block_len_bits = s->frame_len_bits;
102 s->block_len_bits = s->frame_len_bits;
103
104 s->frame_len = 1 << s->frame_len_bits;
105 if (s->use_variable_block_len) {
106 int nb_max, nb;
107 nb = ((flags2 >> 3) & 3) + 1;
108 if ((avctx->bit_rate / avctx->channels) >= 32000)
109 nb += 2;
110 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
111 if (nb > nb_max)
112 nb = nb_max;
113 s->nb_block_sizes = nb + 1;
114 } else
115 s->nb_block_sizes = 1;
116
117 /* init rate dependent parameters */
118 s->use_noise_coding = 1;
119 high_freq = avctx->sample_rate * 0.5;
120
121 /* if version 2, then the rates are normalized */
122 sample_rate1 = avctx->sample_rate;
123 if (s->version == 2) {
124 if (sample_rate1 >= 44100)
125 sample_rate1 = 44100;
126 else if (sample_rate1 >= 22050)
127 sample_rate1 = 22050;
128 else if (sample_rate1 >= 16000)
129 sample_rate1 = 16000;
130 else if (sample_rate1 >= 11025)
131 sample_rate1 = 11025;
132 else if (sample_rate1 >= 8000)
133 sample_rate1 = 8000;
134 }
135
136 bps = (float) avctx->bit_rate /
137 (float) (avctx->channels * avctx->sample_rate);
138 s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
139 if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
140 av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
141 return AVERROR_PATCHWELCOME;
142 }
143
144 /* compute high frequency value and choose if noise coding should
145 * be activated */
146 bps1 = bps;
147 if (avctx->channels == 2)
148 bps1 = bps * 1.6;
149 if (sample_rate1 == 44100) {
150 if (bps1 >= 0.61)
151 s->use_noise_coding = 0;
152 else
153 high_freq = high_freq * 0.4;
154 } else if (sample_rate1 == 22050) {
155 if (bps1 >= 1.16)
156 s->use_noise_coding = 0;
157 else if (bps1 >= 0.72)
158 high_freq = high_freq * 0.7;
159 else
160 high_freq = high_freq * 0.6;
161 } else if (sample_rate1 == 16000) {
162 if (bps > 0.5)
163 high_freq = high_freq * 0.5;
164 else
165 high_freq = high_freq * 0.3;
166 } else if (sample_rate1 == 11025)
167 high_freq = high_freq * 0.7;
168 else if (sample_rate1 == 8000) {
169 if (bps <= 0.625)
170 high_freq = high_freq * 0.5;
171 else if (bps > 0.75)
172 s->use_noise_coding = 0;
173 else
174 high_freq = high_freq * 0.65;
175 } else {
176 if (bps >= 0.8)
177 high_freq = high_freq * 0.75;
178 else if (bps >= 0.6)
179 high_freq = high_freq * 0.6;
180 else
181 high_freq = high_freq * 0.5;
182 }
183 ff_dlog(s->avctx, "flags2=0x%x\n", flags2);
184 ff_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%"PRId64" block_align=%d\n",
185 s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
186 avctx->block_align);
187 ff_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
188 bps, bps1, high_freq, s->byte_offset_bits);
189 ff_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
190 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
191
192 /* compute the scale factor band sizes for each MDCT block size */
193 {
194 int a, b, pos, lpos, k, block_len, i, j, n;
195 const uint8_t *table;
196
197 if (s->version == 1)
198 s->coefs_start = 3;
199 else
200 s->coefs_start = 0;
201 for (k = 0; k < s->nb_block_sizes; k++) {
202 block_len = s->frame_len >> k;
203
204 if (s->version == 1) {
205 lpos = 0;
206 for (i = 0; i < 25; i++) {
207 a = ff_wma_critical_freqs[i];
208 b = avctx->sample_rate;
209 pos = ((block_len * 2 * a) + (b >> 1)) / b;
210 if (pos > block_len)
211 pos = block_len;
212 s->exponent_bands[0][i] = pos - lpos;
213 if (pos >= block_len) {
214 i++;
215 break;
216 }
217 lpos = pos;
218 }
219 s->exponent_sizes[0] = i;
220 } else {
221 /* hardcoded tables */
222 table = NULL;
223 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
224 if (a < 3) {
225 if (avctx->sample_rate >= 44100)
226 table = exponent_band_44100[a];
227 else if (avctx->sample_rate >= 32000)
228 table = exponent_band_32000[a];
229 else if (avctx->sample_rate >= 22050)
230 table = exponent_band_22050[a];
231 }
232 if (table) {
233 n = *table++;
234 for (i = 0; i < n; i++)
235 s->exponent_bands[k][i] = table[i];
236 s->exponent_sizes[k] = n;
237 } else {
238 j = 0;
239 lpos = 0;
240 for (i = 0; i < 25; i++) {
241 a = ff_wma_critical_freqs[i];
242 b = avctx->sample_rate;
243 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
244 pos <<= 2;
245 if (pos > block_len)
246 pos = block_len;
247 if (pos > lpos)
248 s->exponent_bands[k][j++] = pos - lpos;
249 if (pos >= block_len)
250 break;
251 lpos = pos;
252 }
253 s->exponent_sizes[k] = j;
254 }
255 }
256
257 /* max number of coefs */
258 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
259 /* high freq computation */
260 s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
261 avctx->sample_rate + 0.5);
262 n = s->exponent_sizes[k];
263 j = 0;
264 pos = 0;
265 for (i = 0; i < n; i++) {
266 int start, end;
267 start = pos;
268 pos += s->exponent_bands[k][i];
269 end = pos;
270 if (start < s->high_band_start[k])
271 start = s->high_band_start[k];
272 if (end > s->coefs_end[k])
273 end = s->coefs_end[k];
274 if (end > start)
275 s->exponent_high_bands[k][j++] = end - start;
276 }
277 s->exponent_high_sizes[k] = j;
278 }
279 }
280
281 #ifdef TRACE
282 {
283 int i, j;
284 for (i = 0; i < s->nb_block_sizes; i++) {
285 ff_tlog(s->avctx, "%5d: n=%2d:",
286 s->frame_len >> i,
287 s->exponent_sizes[i]);
288 for (j = 0; j < s->exponent_sizes[i]; j++)
289 ff_tlog(s->avctx, " %d", s->exponent_bands[i][j]);
290 ff_tlog(s->avctx, "\n");
291 }
292 }
293 #endif /* TRACE */
294
295 /* init MDCT windows : simple sine window */
296 for (i = 0; i < s->nb_block_sizes; i++) {
297 ff_init_ff_sine_windows(s->frame_len_bits - i);
298 s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
299 }
300
301 s->reset_block_lengths = 1;
302
303 if (s->use_noise_coding) {
304 /* init the noise generator */
305 if (s->use_exp_vlc)
306 s->noise_mult = 0.02;
307 else
308 s->noise_mult = 0.04;
309
310 #ifdef TRACE
311 for (i = 0; i < NOISE_TAB_SIZE; i++)
312 s->noise_table[i] = 1.0 * s->noise_mult;
313 #else
314 {
315 unsigned int seed;
316 float norm;
317 seed = 1;
318 norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
319 for (i = 0; i < NOISE_TAB_SIZE; i++) {
320 seed = seed * 314159 + 1;
321 s->noise_table[i] = (float) ((int) seed) * norm;
322 }
323 }
324 #endif /* TRACE */
325 }
326
327 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
328 if (!s->fdsp)
329 return AVERROR(ENOMEM);
330
331 /* choose the VLC tables for the coefficients */
332 coef_vlc_table = 2;
333 if (avctx->sample_rate >= 32000) {
334 if (bps1 < 0.72)
335 coef_vlc_table = 0;
336 else if (bps1 < 1.16)
337 coef_vlc_table = 1;
338 }
339 s->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
340 s->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
341 ret = init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
342 &s->int_table[0], s->coef_vlcs[0]);
343 if (ret < 0)
344 return ret;
345
346 return init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
347 &s->int_table[1], s->coef_vlcs[1]);
348 }
349
ff_wma_total_gain_to_bits(int total_gain)350 int ff_wma_total_gain_to_bits(int total_gain)
351 {
352 if (total_gain < 15)
353 return 13;
354 else if (total_gain < 32)
355 return 12;
356 else if (total_gain < 40)
357 return 11;
358 else if (total_gain < 45)
359 return 10;
360 else
361 return 9;
362 }
363
ff_wma_end(AVCodecContext * avctx)364 int ff_wma_end(AVCodecContext *avctx)
365 {
366 WMACodecContext *s = avctx->priv_data;
367 int i;
368
369 for (i = 0; i < s->nb_block_sizes; i++)
370 ff_mdct_end(&s->mdct_ctx[i]);
371
372 if (s->use_exp_vlc)
373 ff_free_vlc(&s->exp_vlc);
374 if (s->use_noise_coding)
375 ff_free_vlc(&s->hgain_vlc);
376 for (i = 0; i < 2; i++) {
377 ff_free_vlc(&s->coef_vlc[i]);
378 av_freep(&s->run_table[i]);
379 av_freep(&s->level_table[i]);
380 av_freep(&s->int_table[i]);
381 }
382 av_freep(&s->fdsp);
383
384 return 0;
385 }
386
387 /**
388 * Decode an uncompressed coefficient.
389 * @param gb GetBitContext
390 * @return the decoded coefficient
391 */
ff_wma_get_large_val(GetBitContext * gb)392 unsigned int ff_wma_get_large_val(GetBitContext *gb)
393 {
394 /** consumes up to 34 bits */
395 int n_bits = 8;
396 /** decode length */
397 if (get_bits1(gb)) {
398 n_bits += 8;
399 if (get_bits1(gb)) {
400 n_bits += 8;
401 if (get_bits1(gb))
402 n_bits += 7;
403 }
404 }
405 return get_bits_long(gb, n_bits);
406 }
407
408 /**
409 * Decode run level compressed coefficients.
410 * @param avctx codec context
411 * @param gb bitstream reader context
412 * @param vlc vlc table for get_vlc2
413 * @param level_table level codes
414 * @param run_table run codes
415 * @param version 0 for wma1,2 1 for wmapro
416 * @param ptr output buffer
417 * @param offset offset in the output buffer
418 * @param num_coefs number of input coefficients
419 * @param block_len input buffer length (2^n)
420 * @param frame_len_bits number of bits for escaped run codes
421 * @param coef_nb_bits number of bits for escaped level codes
422 * @return 0 on success, -1 otherwise
423 */
ff_wma_run_level_decode(AVCodecContext * avctx,GetBitContext * gb,VLC * vlc,const float * level_table,const uint16_t * run_table,int version,WMACoef * ptr,int offset,int num_coefs,int block_len,int frame_len_bits,int coef_nb_bits)424 int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
425 VLC *vlc, const float *level_table,
426 const uint16_t *run_table, int version,
427 WMACoef *ptr, int offset, int num_coefs,
428 int block_len, int frame_len_bits,
429 int coef_nb_bits)
430 {
431 int code, level, sign;
432 const uint32_t *ilvl = (const uint32_t *) level_table;
433 uint32_t *iptr = (uint32_t *) ptr;
434 const unsigned int coef_mask = block_len - 1;
435 for (; offset < num_coefs; offset++) {
436 code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
437 if (code > 1) {
438 /** normal code */
439 offset += run_table[code];
440 sign = get_bits1(gb) - 1;
441 iptr[offset & coef_mask] = ilvl[code] ^ (sign & 0x80000000);
442 } else if (code == 1) {
443 /** EOB */
444 break;
445 } else {
446 /** escape */
447 if (!version) {
448 level = get_bits(gb, coef_nb_bits);
449 /** NOTE: this is rather suboptimal. reading
450 * block_len_bits would be better */
451 offset += get_bits(gb, frame_len_bits);
452 } else {
453 level = ff_wma_get_large_val(gb);
454 /** escape decode */
455 if (get_bits1(gb)) {
456 if (get_bits1(gb)) {
457 if (get_bits1(gb)) {
458 av_log(avctx, AV_LOG_ERROR,
459 "broken escape sequence\n");
460 return AVERROR_INVALIDDATA;
461 } else
462 offset += get_bits(gb, frame_len_bits) + 4;
463 } else
464 offset += get_bits(gb, 2) + 1;
465 }
466 }
467 sign = get_bits1(gb) - 1;
468 ptr[offset & coef_mask] = (level ^ sign) - sign;
469 }
470 }
471 /** NOTE: EOB can be omitted */
472 if (offset > num_coefs) {
473 av_log(avctx, AV_LOG_ERROR,
474 "overflow (%d > %d) in spectral RLE, ignoring\n",
475 offset,
476 num_coefs
477 );
478 return AVERROR_INVALIDDATA;
479 }
480
481 return 0;
482 }
483