Add additional sample rates to the frame len bits calculation code
[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
78 if (sample_rate <= 16000)
79 frame_len_bits = 9;
80 else if (sample_rate <= 22050 ||
81 (sample_rate <= 32000 && version == 1))
82 frame_len_bits = 10;
523c09ca 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) {
156 if (sample_rate1 >= 44100)
157 sample_rate1 = 44100;
158 else if (sample_rate1 >= 22050)
159 sample_rate1 = 22050;
160 else if (sample_rate1 >= 16000)
161 sample_rate1 = 16000;
162 else if (sample_rate1 >= 11025)
163 sample_rate1 = 11025;
164 else if (sample_rate1 >= 8000)
165 sample_rate1 = 8000;
166 }
167
168 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
169 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
170
171 /* compute high frequency value and choose if noise coding should
172 be activated */
173 bps1 = bps;
174 if (s->nb_channels == 2)
175 bps1 = bps * 1.6;
176 if (sample_rate1 == 44100) {
177 if (bps1 >= 0.61)
178 s->use_noise_coding = 0;
179 else
180 high_freq = high_freq * 0.4;
181 } else if (sample_rate1 == 22050) {
182 if (bps1 >= 1.16)
183 s->use_noise_coding = 0;
184 else if (bps1 >= 0.72)
185 high_freq = high_freq * 0.7;
186 else
187 high_freq = high_freq * 0.6;
188 } else if (sample_rate1 == 16000) {
189 if (bps > 0.5)
190 high_freq = high_freq * 0.5;
191 else
192 high_freq = high_freq * 0.3;
193 } else if (sample_rate1 == 11025) {
194 high_freq = high_freq * 0.7;
195 } else if (sample_rate1 == 8000) {
196 if (bps <= 0.625) {
197 high_freq = high_freq * 0.5;
198 } else if (bps > 0.75) {
199 s->use_noise_coding = 0;
200 } else {
201 high_freq = high_freq * 0.65;
202 }
203 } else {
204 if (bps >= 0.8) {
205 high_freq = high_freq * 0.75;
206 } else if (bps >= 0.6) {
207 high_freq = high_freq * 0.6;
208 } else {
209 high_freq = high_freq * 0.5;
210 }
211 }
318c5e05
MB
212 dprintf(s->avctx, "flags2=0x%x\n", flags2);
213 dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
ae1374ce
DB
214 s->version, s->nb_channels, s->sample_rate, s->bit_rate,
215 s->block_align);
318c5e05 216 dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
ae1374ce 217 bps, bps1, high_freq, s->byte_offset_bits);
318c5e05 218 dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
ae1374ce 219 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
59686073
MN
220
221 /* compute the scale factor band sizes for each MDCT block size */
222 {
223 int a, b, pos, lpos, k, block_len, i, j, n;
224 const uint8_t *table;
225
226 if (s->version == 1) {
227 s->coefs_start = 3;
228 } else {
229 s->coefs_start = 0;
230 }
ae1374ce 231 for (k = 0; k < s->nb_block_sizes; k++) {
59686073
MN
232 block_len = s->frame_len >> k;
233
234 if (s->version == 1) {
235 lpos = 0;
ae1374ce 236 for (i = 0; i < 25; i++) {
59686073
MN
237 a = wma_critical_freqs[i];
238 b = s->sample_rate;
ae1374ce 239 pos = ((block_len * 2 * a) + (b >> 1)) / b;
59686073
MN
240 if (pos > block_len)
241 pos = block_len;
242 s->exponent_bands[0][i] = pos - lpos;
243 if (pos >= block_len) {
244 i++;
245 break;
246 }
247 lpos = pos;
248 }
249 s->exponent_sizes[0] = i;
250 } else {
251 /* hardcoded tables */
252 table = NULL;
253 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
254 if (a < 3) {
255 if (s->sample_rate >= 44100)
256 table = exponent_band_44100[a];
257 else if (s->sample_rate >= 32000)
258 table = exponent_band_32000[a];
259 else if (s->sample_rate >= 22050)
260 table = exponent_band_22050[a];
261 }
262 if (table) {
263 n = *table++;
ae1374ce 264 for (i = 0; i < n; i++)
59686073
MN
265 s->exponent_bands[k][i] = table[i];
266 s->exponent_sizes[k] = n;
267 } else {
268 j = 0;
269 lpos = 0;
ae1374ce 270 for (i = 0; i < 25; i++) {
59686073
MN
271 a = wma_critical_freqs[i];
272 b = s->sample_rate;
ae1374ce 273 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
59686073
MN
274 pos <<= 2;
275 if (pos > block_len)
276 pos = block_len;
277 if (pos > lpos)
278 s->exponent_bands[k][j++] = pos - lpos;
279 if (pos >= block_len)
280 break;
281 lpos = pos;
282 }
283 s->exponent_sizes[k] = j;
284 }
285 }
286
287 /* max number of coefs */
288 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
289 /* high freq computation */
290 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
291 s->sample_rate + 0.5);
292 n = s->exponent_sizes[k];
293 j = 0;
294 pos = 0;
ae1374ce 295 for (i = 0; i < n; i++) {
59686073
MN
296 int start, end;
297 start = pos;
298 pos += s->exponent_bands[k][i];
299 end = pos;
300 if (start < s->high_band_start[k])
301 start = s->high_band_start[k];
302 if (end > s->coefs_end[k])
303 end = s->coefs_end[k];
304 if (end > start)
305 s->exponent_high_bands[k][j++] = end - start;
306 }
307 s->exponent_high_sizes[k] = j;
308#if 0
a9c9a240 309 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
ae1374ce
DB
310 s->frame_len >> k,
311 s->coefs_end[k],
312 s->high_band_start[k],
313 s->exponent_high_sizes[k]);
314 for (j = 0; j < s->exponent_high_sizes[k]; j++)
a9c9a240
MB
315 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
316 tprintf(s->avctx, "\n");
59686073
MN
317#endif
318 }
319 }
320
321#ifdef TRACE
322 {
323 int i, j;
ae1374ce 324 for (i = 0; i < s->nb_block_sizes; i++) {
a9c9a240 325 tprintf(s->avctx, "%5d: n=%2d:",
ae1374ce
DB
326 s->frame_len >> i,
327 s->exponent_sizes[i]);
328 for (j = 0; j < s->exponent_sizes[i]; j++)
a9c9a240
MB
329 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
330 tprintf(s->avctx, "\n");
59686073
MN
331 }
332 }
333#endif
334
335 /* init MDCT windows : simple sinus window */
ae1374ce 336 for (i = 0; i < s->nb_block_sizes; i++) {
ece6b83c 337 int n;
59686073 338 n = 1 << (s->frame_len_bits - i);
cbcbf439
RS
339 ff_sine_window_init(ff_sine_windows[s->frame_len_bits - i - 7], n);
340 s->windows[i] = ff_sine_windows[s->frame_len_bits - i - 7];
59686073
MN
341 }
342
343 s->reset_block_lengths = 1;
344
345 if (s->use_noise_coding) {
346
347 /* init the noise generator */
348 if (s->use_exp_vlc)
349 s->noise_mult = 0.02;
350 else
351 s->noise_mult = 0.04;
352
353#ifdef TRACE
ae1374ce 354 for (i = 0; i < NOISE_TAB_SIZE; i++)
59686073
MN
355 s->noise_table[i] = 1.0 * s->noise_mult;
356#else
357 {
358 unsigned int seed;
359 float norm;
360 seed = 1;
361 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
ae1374ce 362 for (i = 0; i < NOISE_TAB_SIZE; i++) {
59686073
MN
363 seed = seed * 314159 + 1;
364 s->noise_table[i] = (float)((int)seed) * norm;
365 }
366 }
367#endif
368 }
369
370 /* choose the VLC tables for the coefficients */
371 coef_vlc_table = 2;
372 if (s->sample_rate >= 32000) {
373 if (bps1 < 0.72)
374 coef_vlc_table = 0;
375 else if (bps1 < 1.16)
376 coef_vlc_table = 1;
377 }
378 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
379 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
380 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
381 s->coef_vlcs[0]);
382 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
383 s->coef_vlcs[1]);
384
385 return 0;
386}
387
ae1374ce
DB
388int ff_wma_total_gain_to_bits(int total_gain)
389{
59686073
MN
390 if (total_gain < 15) return 13;
391 else if (total_gain < 32) return 12;
392 else if (total_gain < 40) return 11;
393 else if (total_gain < 45) return 10;
394 else return 9;
395}
396
397int ff_wma_end(AVCodecContext *avctx)
398{
911b9faf 399 WMACodecContext *s = avctx->priv_data;
59686073
MN
400 int i;
401
ae1374ce 402 for (i = 0; i < s->nb_block_sizes; i++)
59686073 403 ff_mdct_end(&s->mdct_ctx[i]);
59686073
MN
404
405 if (s->use_exp_vlc) {
406 free_vlc(&s->exp_vlc);
407 }
408 if (s->use_noise_coding) {
409 free_vlc(&s->hgain_vlc);
410 }
ae1374ce 411 for (i = 0; i < 2; i++) {
59686073
MN
412 free_vlc(&s->coef_vlc[i]);
413 av_free(s->run_table[i]);
414 av_free(s->level_table[i]);
2c79288d 415 av_free(s->int_table[i]);
59686073
MN
416 }
417
418 return 0;
419}