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