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