c0261b1175f7e8e7a60b88c960b9bfe6ce019c4e
[ffmpeg.git] / libavcodec / wma.c
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 "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
31 static void init_coef_vlc(VLC *vlc,
32                           uint16_t **prun_table, uint16_t **plevel_table, uint16_t **pint_table,
33                           const CoefVLCTable *vlc_table)
34 {
35     int n = vlc_table->n;
36     const uint8_t *table_bits = vlc_table->huffbits;
37     const uint32_t *table_codes = vlc_table->huffcodes;
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
44     run_table = av_malloc(n * sizeof(uint16_t));
45     level_table = av_malloc(n * sizeof(uint16_t));
46     int_table = av_malloc(n * sizeof(uint16_t));
47     i = 2;
48     level = 1;
49     k = 0;
50     while (i < n) {
51         int_table[k]= i;
52         l = levels_table[k++];
53         for(j=0;j<l;j++) {
54             run_table[i] = j;
55             level_table[i] = level;
56             i++;
57         }
58         level++;
59     }
60     *prun_table = run_table;
61     *plevel_table = level_table;
62     *pint_table= int_table;
63 }
64
65 int ff_wma_init(AVCodecContext * avctx, int flags2)
66 {
67     WMACodecContext *s = avctx->priv_data;
68     int i;
69     float *window;
70     float bps1, high_freq;
71     volatile float bps;
72     int sample_rate1;
73     int coef_vlc_table;
74
75     if(   avctx->sample_rate<=0 || avctx->sample_rate>50000
76        || avctx->channels<=0 || avctx->channels>8
77        || avctx->bit_rate<=0)
78         return -1;
79
80     s->sample_rate = avctx->sample_rate;
81     s->nb_channels = avctx->channels;
82     s->bit_rate = avctx->bit_rate;
83     s->block_align = avctx->block_align;
84
85     dsputil_init(&s->dsp, avctx);
86
87     if (avctx->codec->id == CODEC_ID_WMAV1) {
88         s->version = 1;
89     } else {
90         s->version = 2;
91     }
92
93     /* compute MDCT block size */
94     if (s->sample_rate <= 16000) {
95         s->frame_len_bits = 9;
96     } else if (s->sample_rate <= 22050 ||
97                (s->sample_rate <= 32000 && s->version == 1)) {
98         s->frame_len_bits = 10;
99     } else {
100         s->frame_len_bits = 11;
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 ((s->bit_rate / s->nb_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
116     /* init rate dependent parameters */
117     s->use_noise_coding = 1;
118     high_freq = s->sample_rate * 0.5;
119
120     /* if version 2, then the rates are normalized */
121     sample_rate1 = s->sample_rate;
122     if (s->version == 2) {
123         if (sample_rate1 >= 44100)
124             sample_rate1 = 44100;
125         else if (sample_rate1 >= 22050)
126             sample_rate1 = 22050;
127         else if (sample_rate1 >= 16000)
128             sample_rate1 = 16000;
129         else if (sample_rate1 >= 11025)
130             sample_rate1 = 11025;
131         else if (sample_rate1 >= 8000)
132             sample_rate1 = 8000;
133     }
134
135     bps = (float)s->bit_rate / (float)(s->nb_channels * s->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 (s->nb_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         }
170     } else {
171         if (bps >= 0.8) {
172             high_freq = high_freq * 0.75;
173         } else if (bps >= 0.6) {
174             high_freq = high_freq * 0.6;
175         } else {
176             high_freq = high_freq * 0.5;
177         }
178     }
179     dprintf(s->avctx, "flags2=0x%x\n", flags2);
180     dprintf(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
181            s->version, s->nb_channels, s->sample_rate, s->bit_rate,
182            s->block_align);
183     dprintf(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
184            bps, bps1, high_freq, s->byte_offset_bits);
185     dprintf(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
186            s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
187
188     /* compute the scale factor band sizes for each MDCT block size */
189     {
190         int a, b, pos, lpos, k, block_len, i, j, n;
191         const uint8_t *table;
192
193         if (s->version == 1) {
194             s->coefs_start = 3;
195         } else {
196             s->coefs_start = 0;
197         }
198         for(k = 0; k < s->nb_block_sizes; k++) {
199             block_len = s->frame_len >> k;
200
201             if (s->version == 1) {
202                 lpos = 0;
203                 for(i=0;i<25;i++) {
204                     a = wma_critical_freqs[i];
205                     b = s->sample_rate;
206                     pos = ((block_len * 2 * a)  + (b >> 1)) / b;
207                     if (pos > block_len)
208                         pos = block_len;
209                     s->exponent_bands[0][i] = pos - lpos;
210                     if (pos >= block_len) {
211                         i++;
212                         break;
213                     }
214                     lpos = pos;
215                 }
216                 s->exponent_sizes[0] = i;
217             } else {
218                 /* hardcoded tables */
219                 table = NULL;
220                 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
221                 if (a < 3) {
222                     if (s->sample_rate >= 44100)
223                         table = exponent_band_44100[a];
224                     else if (s->sample_rate >= 32000)
225                         table = exponent_band_32000[a];
226                     else if (s->sample_rate >= 22050)
227                         table = exponent_band_22050[a];
228                 }
229                 if (table) {
230                     n = *table++;
231                     for(i=0;i<n;i++)
232                         s->exponent_bands[k][i] = table[i];
233                     s->exponent_sizes[k] = n;
234                 } else {
235                     j = 0;
236                     lpos = 0;
237                     for(i=0;i<25;i++) {
238                         a = wma_critical_freqs[i];
239                         b = s->sample_rate;
240                         pos = ((block_len * 2 * a)  + (b << 1)) / (4 * b);
241                         pos <<= 2;
242                         if (pos > block_len)
243                             pos = block_len;
244                         if (pos > lpos)
245                             s->exponent_bands[k][j++] = pos - lpos;
246                         if (pos >= block_len)
247                             break;
248                         lpos = pos;
249                     }
250                     s->exponent_sizes[k] = j;
251                 }
252             }
253
254             /* max number of coefs */
255             s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
256             /* high freq computation */
257             s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
258                                           s->sample_rate + 0.5);
259             n = s->exponent_sizes[k];
260             j = 0;
261             pos = 0;
262             for(i=0;i<n;i++) {
263                 int start, end;
264                 start = pos;
265                 pos += s->exponent_bands[k][i];
266                 end = pos;
267                 if (start < s->high_band_start[k])
268                     start = s->high_band_start[k];
269                 if (end > s->coefs_end[k])
270                     end = s->coefs_end[k];
271                 if (end > start)
272                     s->exponent_high_bands[k][j++] = end - start;
273             }
274             s->exponent_high_sizes[k] = j;
275 #if 0
276             tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
277                   s->frame_len >> k,
278                   s->coefs_end[k],
279                   s->high_band_start[k],
280                   s->exponent_high_sizes[k]);
281             for(j=0;j<s->exponent_high_sizes[k];j++)
282                 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
283             tprintf(s->avctx, "\n");
284 #endif
285         }
286     }
287
288 #ifdef TRACE
289     {
290         int i, j;
291         for(i = 0; i < s->nb_block_sizes; i++) {
292             tprintf(s->avctx, "%5d: n=%2d:",
293                    s->frame_len >> i,
294                    s->exponent_sizes[i]);
295             for(j=0;j<s->exponent_sizes[i];j++)
296                 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
297             tprintf(s->avctx, "\n");
298         }
299     }
300 #endif
301
302     /* init MDCT windows : simple sinus window */
303     for(i = 0; i < s->nb_block_sizes; i++) {
304         int n;
305         n = 1 << (s->frame_len_bits - i);
306         window = av_malloc(sizeof(float) * n);
307         ff_sine_window_init(window, n);
308         s->windows[i] = window;
309     }
310
311     s->reset_block_lengths = 1;
312
313     if (s->use_noise_coding) {
314
315         /* init the noise generator */
316         if (s->use_exp_vlc)
317             s->noise_mult = 0.02;
318         else
319             s->noise_mult = 0.04;
320
321 #ifdef TRACE
322         for(i=0;i<NOISE_TAB_SIZE;i++)
323             s->noise_table[i] = 1.0 * s->noise_mult;
324 #else
325         {
326             unsigned int seed;
327             float norm;
328             seed = 1;
329             norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
330             for(i=0;i<NOISE_TAB_SIZE;i++) {
331                 seed = seed * 314159 + 1;
332                 s->noise_table[i] = (float)((int)seed) * norm;
333             }
334         }
335 #endif
336     }
337
338     /* choose the VLC tables for the coefficients */
339     coef_vlc_table = 2;
340     if (s->sample_rate >= 32000) {
341         if (bps1 < 0.72)
342             coef_vlc_table = 0;
343         else if (bps1 < 1.16)
344             coef_vlc_table = 1;
345     }
346     s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2    ];
347     s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
348     init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
349                   s->coef_vlcs[0]);
350     init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
351                   s->coef_vlcs[1]);
352
353     return 0;
354 }
355
356 int ff_wma_total_gain_to_bits(int total_gain){
357          if (total_gain < 15) return 13;
358     else if (total_gain < 32) return 12;
359     else if (total_gain < 40) return 11;
360     else if (total_gain < 45) return 10;
361     else                      return  9;
362 }
363
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     for(i = 0; i < s->nb_block_sizes; i++)
372         av_free(s->windows[i]);
373
374     if (s->use_exp_vlc) {
375         free_vlc(&s->exp_vlc);
376     }
377     if (s->use_noise_coding) {
378         free_vlc(&s->hgain_vlc);
379     }
380     for(i = 0;i < 2; i++) {
381         free_vlc(&s->coef_vlc[i]);
382         av_free(s->run_table[i]);
383         av_free(s->level_table[i]);
384         av_free(s->int_table[i]);
385     }
386
387     return 0;
388 }