cosmetics: Add brackets between if/else
[libav.git] / libavcodec / wma.c
CommitLineData
59686073
MN
1/*
2 * WMA compatible codec
406792e7 3 * Copyright (c) 2002-2007 The FFmpeg Project
59686073
MN
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 "avcodec.h"
23#include "wma.h"
24#include "wmadata.h"
25
26#undef NDEBUG
27#include <assert.h>
28
29/* XXX: use same run/length optimization as mpeg decoders */
30//FIXME maybe split decode / encode or pass flag
ae1374ce
DB
31static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
32 uint16_t **plevel_table, uint16_t **pint_table,
59686073
MN
33 const CoefVLCTable *vlc_table)
34{
35 int n = vlc_table->n;
ae1374ce
DB
36 const uint8_t *table_bits = vlc_table->huffbits;
37 const uint32_t *table_codes = vlc_table->huffcodes;
59686073
MN
38 const uint16_t *levels_table = vlc_table->levels;
39 uint16_t *run_table, *level_table, *int_table;
40 int i, l, j, k, level;
41
42 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
43
ae1374ce 44 run_table = av_malloc(n * sizeof(uint16_t));
59686073 45 level_table = av_malloc(n * sizeof(uint16_t));
ae1374ce 46 int_table = av_malloc(n * sizeof(uint16_t));
59686073
MN
47 i = 2;
48 level = 1;
49 k = 0;
50 while (i < n) {
ae1374ce 51 int_table[k] = i;
59686073 52 l = levels_table[k++];
ae1374ce
DB
53 for (j = 0; j < l; j++) {
54 run_table[i] = j;
59686073
MN
55 level_table[i] = level;
56 i++;
57 }
58 level++;
59 }
ae1374ce 60 *prun_table = run_table;
59686073 61 *plevel_table = level_table;
ae1374ce 62 *pint_table = int_table;
59686073
MN
63}
64
cee4bb89
SS
65/**
66 *@brief Get the samples per frame for this stream.
67 *@param sample_rate output sample_rate
68 *@param version wma version
69 *@param decode_flags codec compression features
70 *@return log2 of the number of output samples per frame
71 */
72int av_cold ff_wma_get_frame_len_bits(int sample_rate, int version,
73 unsigned int decode_flags)
74{
75
76 int frame_len_bits;
77
e8c7f81c 78 if (sample_rate <= 16000) {
cee4bb89 79 frame_len_bits = 9;
e8c7f81c
SS
80 } else if (sample_rate <= 22050 ||
81 (sample_rate <= 32000 && version == 1)) {
cee4bb89 82 frame_len_bits = 10;
e8c7f81c 83 } else if (sample_rate <= 48000) {
cee4bb89 84 frame_len_bits = 11;
523c09ca
SS
85 } else if (sample_rate <= 96000) {
86 frame_len_bits = 12;
87 } else {
88 frame_len_bits = 13;
89 }
90
91 if (version == 3) {
92 int tmp = decode_flags & 0x6;
93 if (tmp == 0x2) {
94 ++frame_len_bits;
95 } else if (tmp == 0x4) {
96 --frame_len_bits;
97 } else if (tmp == 0x6) {
98 frame_len_bits -= 2;
99 }
100 }
cee4bb89
SS
101
102 return frame_len_bits;
103}
104
ae1374ce 105int ff_wma_init(AVCodecContext *avctx, int flags2)
59686073 106{
911b9faf 107 WMACodecContext *s = avctx->priv_data;
59686073 108 int i;
59686073
MN
109 float bps1, high_freq;
110 volatile float bps;
111 int sample_rate1;
112 int coef_vlc_table;
113
ae1374ce
DB
114 if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000
115 || avctx->channels <= 0 || avctx->channels > 8
116 || avctx->bit_rate <= 0)
47b777ce
MN
117 return -1;
118
59686073
MN
119 s->sample_rate = avctx->sample_rate;
120 s->nb_channels = avctx->channels;
ae1374ce 121 s->bit_rate = avctx->bit_rate;
59686073
MN
122 s->block_align = avctx->block_align;
123
124 dsputil_init(&s->dsp, avctx);
125
126 if (avctx->codec->id == CODEC_ID_WMAV1) {
127 s->version = 1;
128 } else {
129 s->version = 2;
130 }
131
132 /* compute MDCT block size */
cee4bb89
SS
133 s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0);
134
59686073
MN
135 s->frame_len = 1 << s->frame_len_bits;
136 if (s->use_variable_block_len) {
137 int nb_max, nb;
138 nb = ((flags2 >> 3) & 3) + 1;
139 if ((s->bit_rate / s->nb_channels) >= 32000)
140 nb += 2;
141 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
142 if (nb > nb_max)
143 nb = nb_max;
144 s->nb_block_sizes = nb + 1;
145 } else {
146 s->nb_block_sizes = 1;
147 }
148
eafcac6a 149 /* init rate dependent parameters */
59686073
MN
150 s->use_noise_coding = 1;
151 high_freq = s->sample_rate * 0.5;
152
153 /* if version 2, then the rates are normalized */
154 sample_rate1 = s->sample_rate;
155 if (s->version == 2) {
e8c7f81c 156 if (sample_rate1 >= 44100) {
59686073 157 sample_rate1 = 44100;
e8c7f81c 158 } else if (sample_rate1 >= 22050) {
59686073 159 sample_rate1 = 22050;
e8c7f81c 160 } else if (sample_rate1 >= 16000) {
59686073 161 sample_rate1 = 16000;
e8c7f81c 162 } else if (sample_rate1 >= 11025) {
59686073 163 sample_rate1 = 11025;
e8c7f81c 164 } else if (sample_rate1 >= 8000) {
59686073 165 sample_rate1 = 8000;
e8c7f81c 166 }
59686073
MN
167 }
168
169 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
170 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
171
172 /* compute high frequency value and choose if noise coding should
173 be activated */
174 bps1 = bps;
175 if (s->nb_channels == 2)
176 bps1 = bps * 1.6;
177 if (sample_rate1 == 44100) {
e8c7f81c 178 if (bps1 >= 0.61) {
59686073 179 s->use_noise_coding = 0;
e8c7f81c 180 } else {
59686073 181 high_freq = high_freq * 0.4;
e8c7f81c 182 }
59686073 183 } else if (sample_rate1 == 22050) {
e8c7f81c 184 if (bps1 >= 1.16) {
59686073 185 s->use_noise_coding = 0;
e8c7f81c 186 } else if (bps1 >= 0.72) {
59686073 187 high_freq = high_freq * 0.7;
e8c7f81c 188 } else {
59686073 189 high_freq = high_freq * 0.6;
e8c7f81c 190 }
59686073 191 } else if (sample_rate1 == 16000) {
e8c7f81c 192 if (bps > 0.5) {
59686073 193 high_freq = high_freq * 0.5;
e8c7f81c 194 } else {
59686073 195 high_freq = high_freq * 0.3;
e8c7f81c 196 }
59686073
MN
197 } else if (sample_rate1 == 11025) {
198 high_freq = high_freq * 0.7;
199 } else if (sample_rate1 == 8000) {
200 if (bps <= 0.625) {
201 high_freq = high_freq * 0.5;
202 } else if (bps > 0.75) {
203 s->use_noise_coding = 0;
204 } else {
205 high_freq = high_freq * 0.65;
206 }
207 } else {
208 if (bps >= 0.8) {
209 high_freq = high_freq * 0.75;
210 } else if (bps >= 0.6) {
211 high_freq = high_freq * 0.6;
212 } else {
213 high_freq = high_freq * 0.5;
214 }
215 }
318c5e05
MB
216 dprintf(s->avctx, "flags2=0x%x\n", flags2);
217 dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
ae1374ce
DB
218 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
219 s->block_align);
318c5e05 220 dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
ae1374ce 221 bps, bps1, high_freq, s->byte_offset_bits);
318c5e05 222 dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
ae1374ce 223 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
59686073
MN
224
225 /* compute the scale factor band sizes for each MDCT block size */
226 {
227 int a, b, pos, lpos, k, block_len, i, j, n;
228 const uint8_t *table;
229
230 if (s->version == 1) {
231 s->coefs_start = 3;
232 } else {
233 s->coefs_start = 0;
234 }
ae1374ce 235 for (k = 0; k < s->nb_block_sizes; k++) {
59686073
MN
236 block_len = s->frame_len >> k;
237
238 if (s->version == 1) {
239 lpos = 0;
ae1374ce 240 for (i = 0; i < 25; i++) {
59686073
MN
241 a = wma_critical_freqs[i];
242 b = s->sample_rate;
ae1374ce 243 pos = ((block_len * 2 * a) + (b >> 1)) / b;
59686073
MN
244 if (pos > block_len)
245 pos = block_len;
246 s->exponent_bands[0][i] = pos - lpos;
247 if (pos >= block_len) {
248 i++;
249 break;
250 }
251 lpos = pos;
252 }
253 s->exponent_sizes[0] = i;
254 } else {
255 /* hardcoded tables */
256 table = NULL;
257 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
258 if (a < 3) {
e8c7f81c 259 if (s->sample_rate >= 44100) {
59686073 260 table = exponent_band_44100[a];
e8c7f81c 261 } else if (s->sample_rate >= 32000) {
59686073 262 table = exponent_band_32000[a];
e8c7f81c 263 } else if (s->sample_rate >= 22050) {
59686073 264 table = exponent_band_22050[a];
e8c7f81c 265 }
59686073
MN
266 }
267 if (table) {
268 n = *table++;
ae1374ce 269 for (i = 0; i < n; i++)
59686073
MN
270 s->exponent_bands[k][i] = table[i];
271 s->exponent_sizes[k] = n;
272 } else {
273 j = 0;
274 lpos = 0;
ae1374ce 275 for (i = 0; i < 25; i++) {
59686073
MN
276 a = wma_critical_freqs[i];
277 b = s->sample_rate;
ae1374ce 278 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
59686073
MN
279 pos <<= 2;
280 if (pos > block_len)
281 pos = block_len;
282 if (pos > lpos)
283 s->exponent_bands[k][j++] = pos - lpos;
284 if (pos >= block_len)
285 break;
286 lpos = pos;
287 }
288 s->exponent_sizes[k] = j;
289 }
290 }
291
292 /* max number of coefs */
293 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
294 /* high freq computation */
295 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
296 s->sample_rate + 0.5);
297 n = s->exponent_sizes[k];
298 j = 0;
299 pos = 0;
ae1374ce 300 for (i = 0; i < n; i++) {
59686073
MN
301 int start, end;
302 start = pos;
303 pos += s->exponent_bands[k][i];
304 end = pos;
305 if (start < s->high_band_start[k])
306 start = s->high_band_start[k];
307 if (end > s->coefs_end[k])
308 end = s->coefs_end[k];
309 if (end > start)
310 s->exponent_high_bands[k][j++] = end - start;
311 }
312 s->exponent_high_sizes[k] = j;
313#if 0
a9c9a240 314 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
ae1374ce
DB
315 s->frame_len >> k,
316 s->coefs_end[k],
317 s->high_band_start[k],
318 s->exponent_high_sizes[k]);
319 for (j = 0; j < s->exponent_high_sizes[k]; j++)
a9c9a240
MB
320 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
321 tprintf(s->avctx, "\n");
59686073
MN
322#endif
323 }
324 }
325
326#ifdef TRACE
327 {
328 int i, j;
ae1374ce 329 for (i = 0; i < s->nb_block_sizes; i++) {
a9c9a240 330 tprintf(s->avctx, "%5d: n=%2d:",
ae1374ce
DB
331 s->frame_len >> i,
332 s->exponent_sizes[i]);
333 for (j = 0; j < s->exponent_sizes[i]; j++)
a9c9a240
MB
334 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
335 tprintf(s->avctx, "\n");
59686073
MN
336 }
337 }
338#endif
339
340 /* init MDCT windows : simple sinus window */
ae1374ce 341 for (i = 0; i < s->nb_block_sizes; i++) {
ece6b83c 342 int n;
59686073 343 n = 1 << (s->frame_len_bits - i);
cbcbf439
RS
344 ff_sine_window_init(ff_sine_windows[s->frame_len_bits - i - 7], n);
345 s->windows[i] = ff_sine_windows[s->frame_len_bits - i - 7];
59686073
MN
346 }
347
348 s->reset_block_lengths = 1;
349
350 if (s->use_noise_coding) {
351
352 /* init the noise generator */
e8c7f81c 353 if (s->use_exp_vlc) {
59686073 354 s->noise_mult = 0.02;
e8c7f81c 355 } else {
59686073 356 s->noise_mult = 0.04;
e8c7f81c 357 }
59686073
MN
358
359#ifdef TRACE
ae1374ce 360 for (i = 0; i < NOISE_TAB_SIZE; i++)
59686073
MN
361 s->noise_table[i] = 1.0 * s->noise_mult;
362#else
363 {
364 unsigned int seed;
365 float norm;
366 seed = 1;
367 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
ae1374ce 368 for (i = 0; i < NOISE_TAB_SIZE; i++) {
59686073
MN
369 seed = seed * 314159 + 1;
370 s->noise_table[i] = (float)((int)seed) * norm;
371 }
372 }
373#endif
374 }
375
376 /* choose the VLC tables for the coefficients */
377 coef_vlc_table = 2;
378 if (s->sample_rate >= 32000) {
e8c7f81c 379 if (bps1 < 0.72) {
59686073 380 coef_vlc_table = 0;
e8c7f81c 381 } else if (bps1 < 1.16) {
59686073 382 coef_vlc_table = 1;
e8c7f81c 383 }
59686073
MN
384 }
385 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
386 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
387 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
388 s->coef_vlcs[0]);
389 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
390 s->coef_vlcs[1]);
391
392 return 0;
393}
394
ae1374ce
DB
395int ff_wma_total_gain_to_bits(int total_gain)
396{
59686073
MN
397 if (total_gain < 15) return 13;
398 else if (total_gain < 32) return 12;
399 else if (total_gain < 40) return 11;
400 else if (total_gain < 45) return 10;
401 else return 9;
402}
403
404int ff_wma_end(AVCodecContext *avctx)
405{
911b9faf 406 WMACodecContext *s = avctx->priv_data;
59686073
MN
407 int i;
408
ae1374ce 409 for (i = 0; i < s->nb_block_sizes; i++)
59686073 410 ff_mdct_end(&s->mdct_ctx[i]);
59686073
MN
411
412 if (s->use_exp_vlc) {
413 free_vlc(&s->exp_vlc);
414 }
415 if (s->use_noise_coding) {
416 free_vlc(&s->hgain_vlc);
417 }
ae1374ce 418 for (i = 0; i < 2; i++) {
59686073
MN
419 free_vlc(&s->coef_vlc[i]);
420 av_free(s->run_table[i]);
421 av_free(s->level_table[i]);
2c79288d 422 av_free(s->int_table[i]);
59686073
MN
423 }
424
425 return 0;
426}