2
2
* WMA compatible decoder
3
3
* Copyright (c) 2002 The FFmpeg Project.
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
22
24
* WMA compatible decoder.
25
* This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
26
* WMA v1 is identified by audio format 0x160 in Microsoft media files
27
* (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
29
* To use this decoder, a calling application must supply the extra data
30
* bytes provided with the WMA data. These are the extra, codec-specific
31
* bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
32
* to the decoder using the extradata[_size] fields in AVCodecContext. There
33
* should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
25
36
#include "avcodec.h"
29
#define BLOCK_MIN_BITS 7
30
#define BLOCK_MAX_BITS 11
31
#define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
33
#define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
35
/* XXX: find exact max size */
36
#define HIGH_BAND_MAX_SIZE 16
38
#define NB_LSP_COEFS 10
40
/* XXX: is it a suitable value ? */
41
#define MAX_CODED_SUPERFRAME_SIZE 4096
43
#define MAX_CHANNELS 2
45
#define NOISE_TAB_SIZE 8192
47
#define LSP_POW_BITS 7
49
typedef struct WMADecodeContext {
54
int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
56
int use_bit_reservoir;
57
int use_variable_block_len;
58
int use_exp_vlc; /* exponent coding: 0 = lsp, 1 = vlc + delta */
59
int use_noise_coding; /* true if perceptual noise is added */
62
int exponent_sizes[BLOCK_NB_SIZES];
63
uint16_t exponent_bands[BLOCK_NB_SIZES][25];
64
int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
65
int coefs_start; /* first coded coef */
66
int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
67
int exponent_high_sizes[BLOCK_NB_SIZES];
68
int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
71
/* coded values in high bands */
72
int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
73
int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
75
/* there are two possible tables for spectral coefficients */
77
uint16_t *run_table[2];
78
uint16_t *level_table[2];
80
int frame_len; /* frame length in samples */
81
int frame_len_bits; /* frame_len = 1 << frame_len_bits */
82
int nb_block_sizes; /* number of block sizes */
84
int reset_block_lengths;
85
int block_len_bits; /* log2 of current block length */
86
int next_block_len_bits; /* log2 of next block length */
87
int prev_block_len_bits; /* log2 of prev block length */
88
int block_len; /* block length in samples */
89
int block_num; /* block number in current frame */
90
int block_pos; /* current position in frame */
91
uint8_t ms_stereo; /* true if mid/side stereo mode */
92
uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
93
float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
94
float max_exponent[MAX_CHANNELS];
95
int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
96
float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
97
MDCTContext mdct_ctx[BLOCK_NB_SIZES];
98
float *windows[BLOCK_NB_SIZES];
99
FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */
100
/* output buffer for one frame and the last for IMDCT windowing */
101
float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
102
/* last frame info */
103
uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
105
int last_superframe_len;
106
float noise_table[NOISE_TAB_SIZE];
108
float noise_mult; /* XXX: suppress that and integrate it in the noise array */
109
/* lsp_to_curve tables */
110
float lsp_cos_table[BLOCK_MAX_SIZE];
111
float lsp_pow_e_table[256];
112
float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
113
float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
120
typedef struct CoefVLCTable {
121
int n; /* total number of codes */
122
const uint32_t *huffcodes; /* VLC bit values */
123
const uint8_t *huffbits; /* VLC bit size */
124
const uint16_t *levels; /* table to build run/level tables */
127
static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
132
static void dump_shorts(const char *name, const short *tab, int n)
43
#define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
45
#define HGAINVLCBITS 9
46
#define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
48
static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
51
static void dump_shorts(WMADecodeContext *s, const char *name, const short *tab, int n)
136
tprintf("%s[%d]:\n", name, n);
55
tprintf(s->avctx, "%s[%d]:\n", name, n);
137
56
for(i=0;i<n;i++) {
140
tprintf(" %5d.0", tab[i]);
58
tprintf(s->avctx, "%4d: ", i);
59
tprintf(s->avctx, " %5d.0", tab[i]);
61
tprintf(s->avctx, "\n");
146
static void dump_floats(const char *name, int prec, const float *tab, int n)
65
static void dump_floats(WMADecodeContext *s, const char *name, int prec, const float *tab, int n)
150
tprintf("%s[%d]:\n", name, n);
69
tprintf(s->avctx, "%s[%d]:\n", name, n);
151
70
for(i=0;i<n;i++) {
154
tprintf(" %8.*f", prec, tab[i]);
72
tprintf(s->avctx, "%4d: ", i);
73
tprintf(s->avctx, " %8.*f", prec, tab[i]);
75
tprintf(s->avctx, "\n");
78
tprintf(s->avctx, "\n");
163
/* XXX: use same run/length optimization as mpeg decoders */
164
static void init_coef_vlc(VLC *vlc,
165
uint16_t **prun_table, uint16_t **plevel_table,
166
const CoefVLCTable *vlc_table)
168
int n = vlc_table->n;
169
const uint8_t *table_bits = vlc_table->huffbits;
170
const uint32_t *table_codes = vlc_table->huffcodes;
171
const uint16_t *levels_table = vlc_table->levels;
172
uint16_t *run_table, *level_table;
176
init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
178
run_table = av_malloc(n * sizeof(uint16_t));
179
level_table = av_malloc(n * sizeof(uint16_t));
187
level_table[i] = level;
192
*prun_table = run_table;
193
*plevel_table = level_table;
196
82
static int wma_decode_init(AVCodecContext * avctx)
198
WMADecodeContext *s = avctx->priv_data;
84
WMACodecContext *s = avctx->priv_data;
199
85
int i, flags1, flags2;
201
86
uint8_t *extradata;
202
float bps1, high_freq, bps;
206
s->sample_rate = avctx->sample_rate;
207
s->nb_channels = avctx->channels;
208
s->bit_rate = avctx->bit_rate;
209
s->block_align = avctx->block_align;
211
if (avctx->codec->id == CODEC_ID_WMAV1) {
217
90
/* extract flag infos */
220
93
extradata = avctx->extradata;
221
if (s->version == 1 && avctx->extradata_size >= 4) {
94
if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
222
95
flags1 = extradata[0] | (extradata[1] << 8);
223
96
flags2 = extradata[2] | (extradata[3] << 8);
224
} else if (s->version == 2 && avctx->extradata_size >= 6) {
225
flags1 = extradata[0] | (extradata[1] << 8) |
97
} else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
98
flags1 = extradata[0] | (extradata[1] << 8) |
226
99
(extradata[2] << 16) | (extradata[3] << 24);
227
100
flags2 = extradata[4] | (extradata[5] << 8);
102
// for(i=0; i<avctx->extradata_size; i++)
103
// av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);
229
105
s->use_exp_vlc = flags2 & 0x0001;
230
106
s->use_bit_reservoir = flags2 & 0x0002;
231
107
s->use_variable_block_len = flags2 & 0x0004;
233
/* compute MDCT block size */
234
if (s->sample_rate <= 16000) {
235
s->frame_len_bits = 9;
236
} else if (s->sample_rate <= 22050 ||
237
(s->sample_rate <= 32000 && s->version == 1)) {
238
s->frame_len_bits = 10;
240
s->frame_len_bits = 11;
242
s->frame_len = 1 << s->frame_len_bits;
243
if (s->use_variable_block_len) {
245
nb = ((flags2 >> 3) & 3) + 1;
246
if ((s->bit_rate / s->nb_channels) >= 32000)
248
nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
251
s->nb_block_sizes = nb + 1;
253
s->nb_block_sizes = 1;
256
/* init rate dependant parameters */
257
s->use_noise_coding = 1;
258
high_freq = s->sample_rate * 0.5;
260
/* if version 2, then the rates are normalized */
261
sample_rate1 = s->sample_rate;
262
if (s->version == 2) {
263
if (sample_rate1 >= 44100)
264
sample_rate1 = 44100;
265
else if (sample_rate1 >= 22050)
266
sample_rate1 = 22050;
267
else if (sample_rate1 >= 16000)
268
sample_rate1 = 16000;
269
else if (sample_rate1 >= 11025)
270
sample_rate1 = 11025;
271
else if (sample_rate1 >= 8000)
275
bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
276
s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0)) + 2;
278
/* compute high frequency value and choose if noise coding should
281
if (s->nb_channels == 2)
283
if (sample_rate1 == 44100) {
285
s->use_noise_coding = 0;
287
high_freq = high_freq * 0.4;
288
} else if (sample_rate1 == 22050) {
290
s->use_noise_coding = 0;
291
else if (bps1 >= 0.72)
292
high_freq = high_freq * 0.7;
294
high_freq = high_freq * 0.6;
295
} else if (sample_rate1 == 16000) {
297
high_freq = high_freq * 0.5;
299
high_freq = high_freq * 0.3;
300
} else if (sample_rate1 == 11025) {
301
high_freq = high_freq * 0.7;
302
} else if (sample_rate1 == 8000) {
304
high_freq = high_freq * 0.5;
305
} else if (bps > 0.75) {
306
s->use_noise_coding = 0;
308
high_freq = high_freq * 0.65;
312
high_freq = high_freq * 0.75;
313
} else if (bps >= 0.6) {
314
high_freq = high_freq * 0.6;
316
high_freq = high_freq * 0.5;
319
dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
320
dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
321
s->version, s->nb_channels, s->sample_rate, s->bit_rate,
323
dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
324
bps, bps1, high_freq, s->byte_offset_bits);
325
dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
326
s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
328
/* compute the scale factor band sizes for each MDCT block size */
330
int a, b, pos, lpos, k, block_len, i, j, n;
331
const uint8_t *table;
333
if (s->version == 1) {
338
for(k = 0; k < s->nb_block_sizes; k++) {
339
block_len = s->frame_len >> k;
341
if (s->version == 1) {
344
a = wma_critical_freqs[i];
346
pos = ((block_len * 2 * a) + (b >> 1)) / b;
349
s->exponent_bands[0][i] = pos - lpos;
350
if (pos >= block_len) {
356
s->exponent_sizes[0] = i;
358
/* hardcoded tables */
360
a = s->frame_len_bits - BLOCK_MIN_BITS - k;
362
if (s->sample_rate >= 44100)
363
table = exponent_band_44100[a];
364
else if (s->sample_rate >= 32000)
365
table = exponent_band_32000[a];
366
else if (s->sample_rate >= 22050)
367
table = exponent_band_22050[a];
372
s->exponent_bands[k][i] = table[i];
373
s->exponent_sizes[k] = n;
378
a = wma_critical_freqs[i];
380
pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
385
s->exponent_bands[k][j++] = pos - lpos;
386
if (pos >= block_len)
390
s->exponent_sizes[k] = j;
394
/* max number of coefs */
395
s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
396
/* high freq computation */
397
s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
398
s->sample_rate + 0.5);
399
n = s->exponent_sizes[k];
405
pos += s->exponent_bands[k][i];
407
if (start < s->high_band_start[k])
408
start = s->high_band_start[k];
409
if (end > s->coefs_end[k])
410
end = s->coefs_end[k];
412
s->exponent_high_bands[k][j++] = end - start;
414
s->exponent_high_sizes[k] = j;
416
tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
419
s->high_band_start[k],
420
s->exponent_high_sizes[k]);
421
for(j=0;j<s->exponent_high_sizes[k];j++)
422
tprintf(" %d", s->exponent_high_bands[k][j]);
431
for(i = 0; i < s->nb_block_sizes; i++) {
432
tprintf("%5d: n=%2d:",
434
s->exponent_sizes[i]);
435
for(j=0;j<s->exponent_sizes[i];j++)
436
tprintf(" %d", s->exponent_bands[i][j]);
109
ff_wma_init(avctx, flags2);
443
112
for(i = 0; i < s->nb_block_sizes; i++)
444
113
ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
446
/* init MDCT windows : simple sinus window */
447
for(i = 0; i < s->nb_block_sizes; i++) {
450
n = 1 << (s->frame_len_bits - i);
451
window = av_malloc(sizeof(float) * n);
452
alpha = M_PI / (2.0 * n);
454
window[n - j - 1] = sin((j + 0.5) * alpha);
456
s->windows[i] = window;
459
s->reset_block_lengths = 1;
461
115
if (s->use_noise_coding) {
463
/* init the noise generator */
465
s->noise_mult = 0.02;
467
s->noise_mult = 0.04;
470
for(i=0;i<NOISE_TAB_SIZE;i++)
471
s->noise_table[i] = 1.0 * s->noise_mult;
477
norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
478
for(i=0;i<NOISE_TAB_SIZE;i++) {
479
seed = seed * 314159 + 1;
480
s->noise_table[i] = (float)((int)seed) * norm;
484
init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits),
485
hgain_huffbits, 1, 1,
486
hgain_huffcodes, 2, 2);
116
init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
117
ff_wma_hgain_huffbits, 1, 1,
118
ff_wma_hgain_huffcodes, 2, 2, 0);
489
121
if (s->use_exp_vlc) {
490
init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits),
491
scale_huffbits, 1, 1,
492
scale_huffcodes, 4, 4);
122
init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_wma_scale_huffbits), //FIXME move out of context
123
ff_wma_scale_huffbits, 1, 1,
124
ff_wma_scale_huffcodes, 4, 4, 0);
494
126
wma_lsp_to_curve_init(s, s->frame_len);
497
/* choose the VLC tables for the coefficients */
499
if (s->sample_rate >= 32000) {
502
else if (bps1 < 1.16)
506
init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
507
&coef_vlcs[coef_vlc_table * 2]);
508
init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
509
&coef_vlcs[coef_vlc_table * 2 + 1]);
513
/* interpolate values for a bigger or smaller block. The block must
514
have multiple sizes */
133
* interpolate values for a bigger or smaller block. The block must
134
* have multiple sizes
515
136
static void interpolate_array(float *scale, int old_size, int new_size)
517
138
int i, j, jincr, k;