ffmpeg / libavcodec / wmadec.c @ f08d171c
History  View  Annotate  Download (31.4 KB)
1 
/*


2 
* WMA compatible decoder

3 
* Copyright (c) 2002 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 021101301 USA

20 
*/

21  
22 
/**

23 
* @file

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.

28 
*

29 
* To use this decoder, a calling application must supply the extra data

30 
* bytes provided with the WMA data. These are the extra, codecspecific

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.

34 
*/

35  
36 
#include "avcodec.h" 
37 
#include "wma.h" 
38  
39 
#undef NDEBUG

40 
#include <assert.h> 
41  
42 
#define EXPVLCBITS 8 
43 
#define EXPMAX ((19+EXPVLCBITS1)/EXPVLCBITS) 
44  
45 
#define HGAINVLCBITS 9 
46 
#define HGAINMAX ((13+HGAINVLCBITS1)/HGAINVLCBITS) 
47  
48 
static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len); 
49  
50 
#ifdef TRACE

51 
static void dump_shorts(WMACodecContext *s, const char *name, const short *tab, int n) 
52 
{ 
53 
int i;

54  
55 
tprintf(s>avctx, "%s[%d]:\n", name, n);

56 
for(i=0;i<n;i++) { 
57 
if ((i & 7) == 0) 
58 
tprintf(s>avctx, "%4d: ", i);

59 
tprintf(s>avctx, " %5d.0", tab[i]);

60 
if ((i & 7) == 7) 
61 
tprintf(s>avctx, "\n");

62 
} 
63 
} 
64  
65 
static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n) 
66 
{ 
67 
int i;

68  
69 
tprintf(s>avctx, "%s[%d]:\n", name, n);

70 
for(i=0;i<n;i++) { 
71 
if ((i & 7) == 0) 
72 
tprintf(s>avctx, "%4d: ", i);

73 
tprintf(s>avctx, " %8.*f", prec, tab[i]);

74 
if ((i & 7) == 7) 
75 
tprintf(s>avctx, "\n");

76 
} 
77 
if ((i & 7) != 0) 
78 
tprintf(s>avctx, "\n");

79 
} 
80 
#endif

81  
82 
static int wma_decode_init(AVCodecContext * avctx) 
83 
{ 
84 
WMACodecContext *s = avctx>priv_data; 
85 
int i, flags2;

86 
uint8_t *extradata; 
87  
88 
s>avctx = avctx; 
89  
90 
/* extract flag infos */

91 
flags2 = 0;

92 
extradata = avctx>extradata; 
93 
if (avctx>codec>id == CODEC_ID_WMAV1 && avctx>extradata_size >= 4) { 
94 
flags2 = AV_RL16(extradata+2);

95 
} else if (avctx>codec>id == CODEC_ID_WMAV2 && avctx>extradata_size >= 6) { 
96 
flags2 = AV_RL16(extradata+4);

97 
} 
98 
// for(i=0; i<avctx>extradata_size; i++)

99 
// av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);

100  
101 
s>use_exp_vlc = flags2 & 0x0001;

102 
s>use_bit_reservoir = flags2 & 0x0002;

103 
s>use_variable_block_len = flags2 & 0x0004;

104  
105 
if(avctx>codec>id == CODEC_ID_WMAV2 && avctx>extradata_size >= 8){ 
106 
if(AV_RL16(extradata+4)==0xd && s>use_variable_block_len){ 
107 
av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");

108 
s>use_variable_block_len= 0; // this fixes issue1503 
109 
} 
110 
} 
111  
112 
if(ff_wma_init(avctx, flags2)<0) 
113 
return 1; 
114  
115 
/* init MDCT */

116 
for(i = 0; i < s>nb_block_sizes; i++) 
117 
ff_mdct_init(&s>mdct_ctx[i], s>frame_len_bits  i + 1, 1, 1.0); 
118  
119 
if (s>use_noise_coding) {

120 
init_vlc(&s>hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),

121 
ff_wma_hgain_huffbits, 1, 1, 
122 
ff_wma_hgain_huffcodes, 2, 2, 0); 
123 
} 
124  
125 
if (s>use_exp_vlc) {

126 
init_vlc(&s>exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context 
127 
ff_aac_scalefactor_bits, 1, 1, 
128 
ff_aac_scalefactor_code, 4, 4, 0); 
129 
} else {

130 
wma_lsp_to_curve_init(s, s>frame_len); 
131 
} 
132  
133 
avctx>sample_fmt = AV_SAMPLE_FMT_S16; 
134 
return 0; 
135 
} 
136  
137 
/**

138 
* compute x^0.25 with an exponent and mantissa table. We use linear

139 
* interpolation to reduce the mantissa table size at a small speed

140 
* expense (linear interpolation approximately doubles the number of

141 
* bits of precision).

142 
*/

143 
static inline float pow_m1_4(WMACodecContext *s, float x) 
144 
{ 
145 
union {

146 
float f;

147 
unsigned int v; 
148 
} u, t; 
149 
unsigned int e, m; 
150 
float a, b;

151  
152 
u.f = x; 
153 
e = u.v >> 23;

154 
m = (u.v >> (23  LSP_POW_BITS)) & ((1 << LSP_POW_BITS)  1); 
155 
/* build interpolation scale: 1 <= t < 2. */

156 
t.v = ((u.v << LSP_POW_BITS) & ((1 << 23)  1))  (127 << 23); 
157 
a = s>lsp_pow_m_table1[m]; 
158 
b = s>lsp_pow_m_table2[m]; 
159 
return s>lsp_pow_e_table[e] * (a + b * t.f);

160 
} 
161  
162 
static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len) 
163 
{ 
164 
float wdel, a, b;

165 
int i, e, m;

166  
167 
wdel = M_PI / frame_len; 
168 
for(i=0;i<frame_len;i++) 
169 
s>lsp_cos_table[i] = 2.0f * cos(wdel * i); 
170  
171 
/* tables for x^0.25 computation */

172 
for(i=0;i<256;i++) { 
173 
e = i  126;

174 
s>lsp_pow_e_table[i] = pow(2.0, e * 0.25); 
175 
} 
176  
177 
/* NOTE: these two tables are needed to avoid two operations in

178 
pow_m1_4 */

179 
b = 1.0; 
180 
for(i=(1 << LSP_POW_BITS)  1;i>=0;i) { 
181 
m = (1 << LSP_POW_BITS) + i;

182 
a = (float)m * (0.5 / (1 << LSP_POW_BITS)); 
183 
a = pow(a, 0.25); 
184 
s>lsp_pow_m_table1[i] = 2 * a  b;

185 
s>lsp_pow_m_table2[i] = b  a; 
186 
b = a; 
187 
} 
188 
#if 0

189 
for(i=1;i<20;i++) {

190 
float v, r1, r2;

191 
v = 5.0 / i;

192 
r1 = pow_m1_4(s, v);

193 
r2 = pow(v,0.25);

194 
printf("%f^0.25=%f e=%f\n", v, r1, r2  r1);

195 
}

196 
#endif

197 
} 
198  
199 
/**

200 
* NOTE: We use the same code as Vorbis here

201 
* @todo optimize it further with SSE/3Dnow

202 
*/

203 
static void wma_lsp_to_curve(WMACodecContext *s, 
204 
float *out, float *val_max_ptr, 
205 
int n, float *lsp) 
206 
{ 
207 
int i, j;

208 
float p, q, w, v, val_max;

209  
210 
val_max = 0;

211 
for(i=0;i<n;i++) { 
212 
p = 0.5f; 
213 
q = 0.5f; 
214 
w = s>lsp_cos_table[i]; 
215 
for(j=1;j<NB_LSP_COEFS;j+=2){ 
216 
q *= w  lsp[j  1];

217 
p *= w  lsp[j]; 
218 
} 
219 
p *= p * (2.0f  w); 
220 
q *= q * (2.0f + w); 
221 
v = p + q; 
222 
v = pow_m1_4(s, v); 
223 
if (v > val_max)

224 
val_max = v; 
225 
out[i] = v; 
226 
} 
227 
*val_max_ptr = val_max; 
228 
} 
229  
230 
/**

231 
* decode exponents coded with LSP coefficients (same idea as Vorbis)

232 
*/

233 
static void decode_exp_lsp(WMACodecContext *s, int ch) 
234 
{ 
235 
float lsp_coefs[NB_LSP_COEFS];

236 
int val, i;

237  
238 
for(i = 0; i < NB_LSP_COEFS; i++) { 
239 
if (i == 0  i >= 8) 
240 
val = get_bits(&s>gb, 3);

241 
else

242 
val = get_bits(&s>gb, 4);

243 
lsp_coefs[i] = ff_wma_lsp_codebook[i][val]; 
244 
} 
245  
246 
wma_lsp_to_curve(s, s>exponents[ch], &s>max_exponent[ch], 
247 
s>block_len, lsp_coefs); 
248 
} 
249  
250 
/** pow(10, i / 16.0) for i in 60..95 */

251 
static const float pow_tab[] = { 
252 
1.7782794100389e04, 2.0535250264571e04, 
253 
2.3713737056617e04, 2.7384196342644e04, 
254 
3.1622776601684e04, 3.6517412725484e04, 
255 
4.2169650342858e04, 4.8696752516586e04, 
256 
5.6234132519035e04, 6.4938163157621e04, 
257 
7.4989420933246e04, 8.6596432336006e04, 
258 
1.0000000000000e03, 1.1547819846895e03, 
259 
1.3335214321633e03, 1.5399265260595e03, 
260 
1.7782794100389e03, 2.0535250264571e03, 
261 
2.3713737056617e03, 2.7384196342644e03, 
262 
3.1622776601684e03, 3.6517412725484e03, 
263 
4.2169650342858e03, 4.8696752516586e03, 
264 
5.6234132519035e03, 6.4938163157621e03, 
265 
7.4989420933246e03, 8.6596432336006e03, 
266 
1.0000000000000e02, 1.1547819846895e02, 
267 
1.3335214321633e02, 1.5399265260595e02, 
268 
1.7782794100389e02, 2.0535250264571e02, 
269 
2.3713737056617e02, 2.7384196342644e02, 
270 
3.1622776601684e02, 3.6517412725484e02, 
271 
4.2169650342858e02, 4.8696752516586e02, 
272 
5.6234132519035e02, 6.4938163157621e02, 
273 
7.4989420933246e02, 8.6596432336007e02, 
274 
1.0000000000000e01, 1.1547819846895e01, 
275 
1.3335214321633e01, 1.5399265260595e01, 
276 
1.7782794100389e01, 2.0535250264571e01, 
277 
2.3713737056617e01, 2.7384196342644e01, 
278 
3.1622776601684e01, 3.6517412725484e01, 
279 
4.2169650342858e01, 4.8696752516586e01, 
280 
5.6234132519035e01, 6.4938163157621e01, 
281 
7.4989420933246e01, 8.6596432336007e01, 
282 
1.0000000000000e+00, 1.1547819846895e+00, 
283 
1.3335214321633e+00, 1.5399265260595e+00, 
284 
1.7782794100389e+00, 2.0535250264571e+00, 
285 
2.3713737056617e+00, 2.7384196342644e+00, 
286 
3.1622776601684e+00, 3.6517412725484e+00, 
287 
4.2169650342858e+00, 4.8696752516586e+00, 
288 
5.6234132519035e+00, 6.4938163157621e+00, 
289 
7.4989420933246e+00, 8.6596432336007e+00, 
290 
1.0000000000000e+01, 1.1547819846895e+01, 
291 
1.3335214321633e+01, 1.5399265260595e+01, 
292 
1.7782794100389e+01, 2.0535250264571e+01, 
293 
2.3713737056617e+01, 2.7384196342644e+01, 
294 
3.1622776601684e+01, 3.6517412725484e+01, 
295 
4.2169650342858e+01, 4.8696752516586e+01, 
296 
5.6234132519035e+01, 6.4938163157621e+01, 
297 
7.4989420933246e+01, 8.6596432336007e+01, 
298 
1.0000000000000e+02, 1.1547819846895e+02, 
299 
1.3335214321633e+02, 1.5399265260595e+02, 
300 
1.7782794100389e+02, 2.0535250264571e+02, 
301 
2.3713737056617e+02, 2.7384196342644e+02, 
302 
3.1622776601684e+02, 3.6517412725484e+02, 
303 
4.2169650342858e+02, 4.8696752516586e+02, 
304 
5.6234132519035e+02, 6.4938163157621e+02, 
305 
7.4989420933246e+02, 8.6596432336007e+02, 
306 
1.0000000000000e+03, 1.1547819846895e+03, 
307 
1.3335214321633e+03, 1.5399265260595e+03, 
308 
1.7782794100389e+03, 2.0535250264571e+03, 
309 
2.3713737056617e+03, 2.7384196342644e+03, 
310 
3.1622776601684e+03, 3.6517412725484e+03, 
311 
4.2169650342858e+03, 4.8696752516586e+03, 
312 
5.6234132519035e+03, 6.4938163157621e+03, 
313 
7.4989420933246e+03, 8.6596432336007e+03, 
314 
1.0000000000000e+04, 1.1547819846895e+04, 
315 
1.3335214321633e+04, 1.5399265260595e+04, 
316 
1.7782794100389e+04, 2.0535250264571e+04, 
317 
2.3713737056617e+04, 2.7384196342644e+04, 
318 
3.1622776601684e+04, 3.6517412725484e+04, 
319 
4.2169650342858e+04, 4.8696752516586e+04, 
320 
5.6234132519035e+04, 6.4938163157621e+04, 
321 
7.4989420933246e+04, 8.6596432336007e+04, 
322 
1.0000000000000e+05, 1.1547819846895e+05, 
323 
1.3335214321633e+05, 1.5399265260595e+05, 
324 
1.7782794100389e+05, 2.0535250264571e+05, 
325 
2.3713737056617e+05, 2.7384196342644e+05, 
326 
3.1622776601684e+05, 3.6517412725484e+05, 
327 
4.2169650342858e+05, 4.8696752516586e+05, 
328 
5.6234132519035e+05, 6.4938163157621e+05, 
329 
7.4989420933246e+05, 8.6596432336007e+05, 
330 
}; 
331  
332 
/**

333 
* decode exponents coded with VLC codes

334 
*/

335 
static int decode_exp_vlc(WMACodecContext *s, int ch) 
336 
{ 
337 
int last_exp, n, code;

338 
const uint16_t *ptr;

339 
float v, max_scale;

340 
uint32_t *q, *q_end, iv; 
341 
const float *ptab = pow_tab + 60; 
342 
const uint32_t *iptab = (const uint32_t*)ptab; 
343  
344 
ptr = s>exponent_bands[s>frame_len_bits  s>block_len_bits]; 
345 
q = (uint32_t *)s>exponents[ch]; 
346 
q_end = q + s>block_len; 
347 
max_scale = 0;

348 
if (s>version == 1) { 
349 
last_exp = get_bits(&s>gb, 5) + 10; 
350 
v = ptab[last_exp]; 
351 
iv = iptab[last_exp]; 
352 
max_scale = v; 
353 
n = *ptr++; 
354 
switch (n & 3) do { 
355 
case 0: *q++ = iv; 
356 
case 3: *q++ = iv; 
357 
case 2: *q++ = iv; 
358 
case 1: *q++ = iv; 
359 
} while ((n = 4) > 0); 
360 
}else

361 
last_exp = 36;

362  
363 
while (q < q_end) {

364 
code = get_vlc2(&s>gb, s>exp_vlc.table, EXPVLCBITS, EXPMAX); 
365 
if (code < 0){ 
366 
av_log(s>avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");

367 
return 1; 
368 
} 
369 
/* NOTE: this offset is the same as MPEG4 AAC ! */

370 
last_exp += code  60;

371 
if ((unsigned)last_exp + 60 > FF_ARRAY_ELEMS(pow_tab)) { 
372 
av_log(s>avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",

373 
last_exp); 
374 
return 1; 
375 
} 
376 
v = ptab[last_exp]; 
377 
iv = iptab[last_exp]; 
378 
if (v > max_scale)

379 
max_scale = v; 
380 
n = *ptr++; 
381 
switch (n & 3) do { 
382 
case 0: *q++ = iv; 
383 
case 3: *q++ = iv; 
384 
case 2: *q++ = iv; 
385 
case 1: *q++ = iv; 
386 
} while ((n = 4) > 0); 
387 
} 
388 
s>max_exponent[ch] = max_scale; 
389 
return 0; 
390 
} 
391  
392  
393 
/**

394 
* Apply MDCT window and add into output.

395 
*

396 
* We ensure that when the windows overlap their squared sum

397 
* is always 1 (MDCT reconstruction rule).

398 
*/

399 
static void wma_window(WMACodecContext *s, float *out) 
400 
{ 
401 
float *in = s>output;

402 
int block_len, bsize, n;

403  
404 
/* left part */

405 
if (s>block_len_bits <= s>prev_block_len_bits) {

406 
block_len = s>block_len; 
407 
bsize = s>frame_len_bits  s>block_len_bits; 
408  
409 
s>dsp.vector_fmul_add(out, in, s>windows[bsize], 
410 
out, block_len); 
411  
412 
} else {

413 
block_len = 1 << s>prev_block_len_bits;

414 
n = (s>block_len  block_len) / 2;

415 
bsize = s>frame_len_bits  s>prev_block_len_bits; 
416  
417 
s>dsp.vector_fmul_add(out+n, in+n, s>windows[bsize], 
418 
out+n, block_len); 
419  
420 
memcpy(out+n+block_len, in+n+block_len, n*sizeof(float)); 
421 
} 
422  
423 
out += s>block_len; 
424 
in += s>block_len; 
425  
426 
/* right part */

427 
if (s>block_len_bits <= s>next_block_len_bits) {

428 
block_len = s>block_len; 
429 
bsize = s>frame_len_bits  s>block_len_bits; 
430  
431 
s>dsp.vector_fmul_reverse(out, in, s>windows[bsize], block_len); 
432  
433 
} else {

434 
block_len = 1 << s>next_block_len_bits;

435 
n = (s>block_len  block_len) / 2;

436 
bsize = s>frame_len_bits  s>next_block_len_bits; 
437  
438 
memcpy(out, in, n*sizeof(float)); 
439  
440 
s>dsp.vector_fmul_reverse(out+n, in+n, s>windows[bsize], block_len); 
441  
442 
memset(out+n+block_len, 0, n*sizeof(float)); 
443 
} 
444 
} 
445  
446  
447 
/**

448 
* @return 0 if OK. 1 if last block of frame. return 1 if

449 
* unrecorrable error.

450 
*/

451 
static int wma_decode_block(WMACodecContext *s) 
452 
{ 
453 
int n, v, a, ch, bsize;

454 
int coef_nb_bits, total_gain;

455 
int nb_coefs[MAX_CHANNELS];

456 
float mdct_norm;

457 
FFTContext *mdct; 
458  
459 
#ifdef TRACE

460 
tprintf(s>avctx, "***decode_block: %d:%d\n", s>frame_count  1, s>block_num); 
461 
#endif

462  
463 
/* compute current block length */

464 
if (s>use_variable_block_len) {

465 
n = av_log2(s>nb_block_sizes  1) + 1; 
466  
467 
if (s>reset_block_lengths) {

468 
s>reset_block_lengths = 0;

469 
v = get_bits(&s>gb, n); 
470 
if (v >= s>nb_block_sizes){

471 
av_log(s>avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s>frame_len_bits  v);

472 
return 1; 
473 
} 
474 
s>prev_block_len_bits = s>frame_len_bits  v; 
475 
v = get_bits(&s>gb, n); 
476 
if (v >= s>nb_block_sizes){

477 
av_log(s>avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s>frame_len_bits  v);

478 
return 1; 
479 
} 
480 
s>block_len_bits = s>frame_len_bits  v; 
481 
} else {

482 
/* update block lengths */

483 
s>prev_block_len_bits = s>block_len_bits; 
484 
s>block_len_bits = s>next_block_len_bits; 
485 
} 
486 
v = get_bits(&s>gb, n); 
487 
if (v >= s>nb_block_sizes){

488 
av_log(s>avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s>frame_len_bits  v);

489 
return 1; 
490 
} 
491 
s>next_block_len_bits = s>frame_len_bits  v; 
492 
} else {

493 
/* fixed block len */

494 
s>next_block_len_bits = s>frame_len_bits; 
495 
s>prev_block_len_bits = s>frame_len_bits; 
496 
s>block_len_bits = s>frame_len_bits; 
497 
} 
498  
499 
if (s>frame_len_bits  s>block_len_bits >= s>nb_block_sizes){

500 
av_log(s>avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n");

501 
return 1; 
502 
} 
503  
504 
/* now check if the block length is coherent with the frame length */

505 
s>block_len = 1 << s>block_len_bits;

506 
if ((s>block_pos + s>block_len) > s>frame_len){

507 
av_log(s>avctx, AV_LOG_ERROR, "frame_len overflow\n");

508 
return 1; 
509 
} 
510  
511 
if (s>nb_channels == 2) { 
512 
s>ms_stereo = get_bits1(&s>gb); 
513 
} 
514 
v = 0;

515 
for(ch = 0; ch < s>nb_channels; ch++) { 
516 
a = get_bits1(&s>gb); 
517 
s>channel_coded[ch] = a; 
518 
v = a; 
519 
} 
520  
521 
bsize = s>frame_len_bits  s>block_len_bits; 
522  
523 
/* if no channel coded, no need to go further */

524 
/* XXX: fix potential framing problems */

525 
if (!v)

526 
goto next;

527  
528 
/* read total gain and extract corresponding number of bits for

529 
coef escape coding */

530 
total_gain = 1;

531 
for(;;) {

532 
a = get_bits(&s>gb, 7);

533 
total_gain += a; 
534 
if (a != 127) 
535 
break;

536 
} 
537  
538 
coef_nb_bits= ff_wma_total_gain_to_bits(total_gain); 
539  
540 
/* compute number of coefficients */

541 
n = s>coefs_end[bsize]  s>coefs_start; 
542 
for(ch = 0; ch < s>nb_channels; ch++) 
543 
nb_coefs[ch] = n; 
544  
545 
/* complex coding */

546 
if (s>use_noise_coding) {

547  
548 
for(ch = 0; ch < s>nb_channels; ch++) { 
549 
if (s>channel_coded[ch]) {

550 
int i, n, a;

551 
n = s>exponent_high_sizes[bsize]; 
552 
for(i=0;i<n;i++) { 
553 
a = get_bits1(&s>gb); 
554 
s>high_band_coded[ch][i] = a; 
555 
/* if noise coding, the coefficients are not transmitted */

556 
if (a)

557 
nb_coefs[ch] = s>exponent_high_bands[bsize][i]; 
558 
} 
559 
} 
560 
} 
561 
for(ch = 0; ch < s>nb_channels; ch++) { 
562 
if (s>channel_coded[ch]) {

563 
int i, n, val, code;

564  
565 
n = s>exponent_high_sizes[bsize]; 
566 
val = (int)0x80000000; 
567 
for(i=0;i<n;i++) { 
568 
if (s>high_band_coded[ch][i]) {

569 
if (val == (int)0x80000000) { 
570 
val = get_bits(&s>gb, 7)  19; 
571 
} else {

572 
code = get_vlc2(&s>gb, s>hgain_vlc.table, HGAINVLCBITS, HGAINMAX); 
573 
if (code < 0){ 
574 
av_log(s>avctx, AV_LOG_ERROR, "hgain vlc invalid\n");

575 
return 1; 
576 
} 
577 
val += code  18;

578 
} 
579 
s>high_band_values[ch][i] = val; 
580 
} 
581 
} 
582 
} 
583 
} 
584 
} 
585  
586 
/* exponents can be reused in short blocks. */

587 
if ((s>block_len_bits == s>frame_len_bits) 

588 
get_bits1(&s>gb)) { 
589 
for(ch = 0; ch < s>nb_channels; ch++) { 
590 
if (s>channel_coded[ch]) {

591 
if (s>use_exp_vlc) {

592 
if (decode_exp_vlc(s, ch) < 0) 
593 
return 1; 
594 
} else {

595 
decode_exp_lsp(s, ch); 
596 
} 
597 
s>exponents_bsize[ch] = bsize; 
598 
} 
599 
} 
600 
} 
601  
602 
/* parse spectral coefficients : just RLE encoding */

603 
for(ch = 0; ch < s>nb_channels; ch++) { 
604 
if (s>channel_coded[ch]) {

605 
int tindex;

606 
WMACoef* ptr = &s>coefs1[ch][0];

607  
608 
/* special VLC tables are used for ms stereo because

609 
there is potentially less energy there */

610 
tindex = (ch == 1 && s>ms_stereo);

611 
memset(ptr, 0, s>block_len * sizeof(WMACoef)); 
612 
ff_wma_run_level_decode(s>avctx, &s>gb, &s>coef_vlc[tindex], 
613 
s>level_table[tindex], s>run_table[tindex], 
614 
0, ptr, 0, nb_coefs[ch], 
615 
s>block_len, s>frame_len_bits, coef_nb_bits); 
616 
} 
617 
if (s>version == 1 && s>nb_channels >= 2) { 
618 
align_get_bits(&s>gb); 
619 
} 
620 
} 
621  
622 
/* normalize */

623 
{ 
624 
int n4 = s>block_len / 2; 
625 
mdct_norm = 1.0 / (float)n4; 
626 
if (s>version == 1) { 
627 
mdct_norm *= sqrt(n4); 
628 
} 
629 
} 
630  
631 
/* finally compute the MDCT coefficients */

632 
for(ch = 0; ch < s>nb_channels; ch++) { 
633 
if (s>channel_coded[ch]) {

634 
WMACoef *coefs1; 
635 
float *coefs, *exponents, mult, mult1, noise;

636 
int i, j, n, n1, last_high_band, esize;

637 
float exp_power[HIGH_BAND_MAX_SIZE];

638  
639 
coefs1 = s>coefs1[ch]; 
640 
exponents = s>exponents[ch]; 
641 
esize = s>exponents_bsize[ch]; 
642 
mult = pow(10, total_gain * 0.05) / s>max_exponent[ch]; 
643 
mult *= mdct_norm; 
644 
coefs = s>coefs[ch]; 
645 
if (s>use_noise_coding) {

646 
mult1 = mult; 
647 
/* very low freqs : noise */

648 
for(i = 0;i < s>coefs_start; i++) { 
649 
*coefs++ = s>noise_table[s>noise_index] * 
650 
exponents[i<<bsize>>esize] * mult1; 
651 
s>noise_index = (s>noise_index + 1) & (NOISE_TAB_SIZE  1); 
652 
} 
653  
654 
n1 = s>exponent_high_sizes[bsize]; 
655  
656 
/* compute power of high bands */

657 
exponents = s>exponents[ch] + 
658 
(s>high_band_start[bsize]<<bsize>>esize); 
659 
last_high_band = 0; /* avoid warning */ 
660 
for(j=0;j<n1;j++) { 
661 
n = s>exponent_high_bands[s>frame_len_bits  
662 
s>block_len_bits][j]; 
663 
if (s>high_band_coded[ch][j]) {

664 
float e2, v;

665 
e2 = 0;

666 
for(i = 0;i < n; i++) { 
667 
v = exponents[i<<bsize>>esize]; 
668 
e2 += v * v; 
669 
} 
670 
exp_power[j] = e2 / n; 
671 
last_high_band = j; 
672 
tprintf(s>avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);

673 
} 
674 
exponents += n<<bsize>>esize; 
675 
} 
676  
677 
/* main freqs and high freqs */

678 
exponents = s>exponents[ch] + (s>coefs_start<<bsize>>esize); 
679 
for(j=1;j<n1;j++) { 
680 
if (j < 0) { 
681 
n = s>high_band_start[bsize]  
682 
s>coefs_start; 
683 
} else {

684 
n = s>exponent_high_bands[s>frame_len_bits  
685 
s>block_len_bits][j]; 
686 
} 
687 
if (j >= 0 && s>high_band_coded[ch][j]) { 
688 
/* use noise with specified power */

689 
mult1 = sqrt(exp_power[j] / exp_power[last_high_band]); 
690 
/* XXX: use a table */

691 
mult1 = mult1 * pow(10, s>high_band_values[ch][j] * 0.05); 
692 
mult1 = mult1 / (s>max_exponent[ch] * s>noise_mult); 
693 
mult1 *= mdct_norm; 
694 
for(i = 0;i < n; i++) { 
695 
noise = s>noise_table[s>noise_index]; 
696 
s>noise_index = (s>noise_index + 1) & (NOISE_TAB_SIZE  1); 
697 
*coefs++ = noise * 
698 
exponents[i<<bsize>>esize] * mult1; 
699 
} 
700 
exponents += n<<bsize>>esize; 
701 
} else {

702 
/* coded values + small noise */

703 
for(i = 0;i < n; i++) { 
704 
noise = s>noise_table[s>noise_index]; 
705 
s>noise_index = (s>noise_index + 1) & (NOISE_TAB_SIZE  1); 
706 
*coefs++ = ((*coefs1++) + noise) * 
707 
exponents[i<<bsize>>esize] * mult; 
708 
} 
709 
exponents += n<<bsize>>esize; 
710 
} 
711 
} 
712  
713 
/* very high freqs : noise */

714 
n = s>block_len  s>coefs_end[bsize]; 
715 
mult1 = mult * exponents[((1<<bsize))>>esize];

716 
for(i = 0; i < n; i++) { 
717 
*coefs++ = s>noise_table[s>noise_index] * mult1; 
718 
s>noise_index = (s>noise_index + 1) & (NOISE_TAB_SIZE  1); 
719 
} 
720 
} else {

721 
/* XXX: optimize more */

722 
for(i = 0;i < s>coefs_start; i++) 
723 
*coefs++ = 0.0; 
724 
n = nb_coefs[ch]; 
725 
for(i = 0;i < n; i++) { 
726 
*coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult; 
727 
} 
728 
n = s>block_len  s>coefs_end[bsize]; 
729 
for(i = 0;i < n; i++) 
730 
*coefs++ = 0.0; 
731 
} 
732 
} 
733 
} 
734  
735 
#ifdef TRACE

736 
for(ch = 0; ch < s>nb_channels; ch++) { 
737 
if (s>channel_coded[ch]) {

738 
dump_floats(s, "exponents", 3, s>exponents[ch], s>block_len); 
739 
dump_floats(s, "coefs", 1, s>coefs[ch], s>block_len); 
740 
} 
741 
} 
742 
#endif

743  
744 
if (s>ms_stereo && s>channel_coded[1]) { 
745 
/* nominal case for ms stereo: we do it before mdct */

746 
/* no need to optimize this case because it should almost

747 
never happen */

748 
if (!s>channel_coded[0]) { 
749 
tprintf(s>avctx, "rare msstereo case happened\n");

750 
memset(s>coefs[0], 0, sizeof(float) * s>block_len); 
751 
s>channel_coded[0] = 1; 
752 
} 
753  
754 
s>dsp.butterflies_float(s>coefs[0], s>coefs[1], s>block_len); 
755 
} 
756  
757 
next:

758 
mdct = &s>mdct_ctx[bsize]; 
759  
760 
for(ch = 0; ch < s>nb_channels; ch++) { 
761 
int n4, index;

762  
763 
n4 = s>block_len / 2;

764 
if(s>channel_coded[ch]){

765 
mdct>imdct_calc(mdct, s>output, s>coefs[ch]); 
766 
}else if(!(s>ms_stereo && ch==1)) 
767 
memset(s>output, 0, sizeof(s>output)); 
768  
769 
/* multiply by the window and add in the frame */

770 
index = (s>frame_len / 2) + s>block_pos  n4;

771 
wma_window(s, &s>frame_out[ch][index]); 
772 
} 
773  
774 
/* update block number */

775 
s>block_num++; 
776 
s>block_pos += s>block_len; 
777 
if (s>block_pos >= s>frame_len)

778 
return 1; 
779 
else

780 
return 0; 
781 
} 
782  
783 
/* decode a frame of frame_len samples */

784 
static int wma_decode_frame(WMACodecContext *s, int16_t *samples) 
785 
{ 
786 
int ret, n, ch, incr;

787 
const float *output[MAX_CHANNELS]; 
788  
789 
#ifdef TRACE

790 
tprintf(s>avctx, "***decode_frame: %d size=%d\n", s>frame_count++, s>frame_len);

791 
#endif

792  
793 
/* read each block */

794 
s>block_num = 0;

795 
s>block_pos = 0;

796 
for(;;) {

797 
ret = wma_decode_block(s); 
798 
if (ret < 0) 
799 
return 1; 
800 
if (ret)

801 
break;

802 
} 
803  
804 
/* convert frame to integer */

805 
n = s>frame_len; 
806 
incr = s>nb_channels; 
807 
for (ch = 0; ch < MAX_CHANNELS; ch++) 
808 
output[ch] = s>frame_out[ch]; 
809 
s>fmt_conv.float_to_int16_interleave(samples, output, n, incr); 
810 
for (ch = 0; ch < incr; ch++) { 
811 
/* prepare for next block */

812 
memmove(&s>frame_out[ch][0], &s>frame_out[ch][n], n * sizeof(float)); 
813 
} 
814  
815 
#ifdef TRACE

816 
dump_shorts(s, "samples", samples, n * s>nb_channels);

817 
#endif

818 
return 0; 
819 
} 
820  
821 
static int wma_decode_superframe(AVCodecContext *avctx, 
822 
void *data, int *data_size, 
823 
AVPacket *avpkt) 
824 
{ 
825 
const uint8_t *buf = avpkt>data;

826 
int buf_size = avpkt>size;

827 
WMACodecContext *s = avctx>priv_data; 
828 
int nb_frames, bit_offset, i, pos, len;

829 
uint8_t *q; 
830 
int16_t *samples; 
831  
832 
tprintf(avctx, "***decode_superframe:\n");

833  
834 
if(buf_size==0){ 
835 
s>last_superframe_len = 0;

836 
return 0; 
837 
} 
838 
if (buf_size < s>block_align)

839 
return 0; 
840 
buf_size = s>block_align; 
841  
842 
samples = data; 
843  
844 
init_get_bits(&s>gb, buf, buf_size*8);

845  
846 
if (s>use_bit_reservoir) {

847 
/* read super frame header */

848 
skip_bits(&s>gb, 4); /* super frame index */ 
849 
nb_frames = get_bits(&s>gb, 4)  1; 
850  
851 
if((nb_frames+1) * s>nb_channels * s>frame_len * sizeof(int16_t) > *data_size){ 
852 
av_log(s>avctx, AV_LOG_ERROR, "Insufficient output space\n");

853 
goto fail;

854 
} 
855  
856 
bit_offset = get_bits(&s>gb, s>byte_offset_bits + 3);

857  
858 
if (s>last_superframe_len > 0) { 
859 
// printf("skip=%d\n", s>last_bitoffset);

860 
/* add bit_offset bits to last frame */

861 
if ((s>last_superframe_len + ((bit_offset + 7) >> 3)) > 
862 
MAX_CODED_SUPERFRAME_SIZE) 
863 
goto fail;

864 
q = s>last_superframe + s>last_superframe_len; 
865 
len = bit_offset; 
866 
while (len > 7) { 
867 
*q++ = (get_bits)(&s>gb, 8);

868 
len = 8;

869 
} 
870 
if (len > 0) { 
871 
*q++ = (get_bits)(&s>gb, len) << (8  len);

872 
} 
873  
874 
/* XXX: bit_offset bits into last frame */

875 
init_get_bits(&s>gb, s>last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);

876 
/* skip unused bits */

877 
if (s>last_bitoffset > 0) 
878 
skip_bits(&s>gb, s>last_bitoffset); 
879 
/* this frame is stored in the last superframe and in the

880 
current one */

881 
if (wma_decode_frame(s, samples) < 0) 
882 
goto fail;

883 
samples += s>nb_channels * s>frame_len; 
884 
} 
885  
886 
/* read each frame starting from bit_offset */

887 
pos = bit_offset + 4 + 4 + s>byte_offset_bits + 3; 
888 
init_get_bits(&s>gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE  (pos >> 3))*8); 
889 
len = pos & 7;

890 
if (len > 0) 
891 
skip_bits(&s>gb, len); 
892  
893 
s>reset_block_lengths = 1;

894 
for(i=0;i<nb_frames;i++) { 
895 
if (wma_decode_frame(s, samples) < 0) 
896 
goto fail;

897 
samples += s>nb_channels * s>frame_len; 
898 
} 
899  
900 
/* we copy the end of the frame in the last frame buffer */

901 
pos = get_bits_count(&s>gb) + ((bit_offset + 4 + 4 + s>byte_offset_bits + 3) & ~7); 
902 
s>last_bitoffset = pos & 7;

903 
pos >>= 3;

904 
len = buf_size  pos; 
905 
if (len > MAX_CODED_SUPERFRAME_SIZE  len < 0) { 
906 
av_log(s>avctx, AV_LOG_ERROR, "len %d invalid\n", len);

907 
goto fail;

908 
} 
909 
s>last_superframe_len = len; 
910 
memcpy(s>last_superframe, buf + pos, len); 
911 
} else {

912 
if(s>nb_channels * s>frame_len * sizeof(int16_t) > *data_size){ 
913 
av_log(s>avctx, AV_LOG_ERROR, "Insufficient output space\n");

914 
goto fail;

915 
} 
916 
/* single frame decode */

917 
if (wma_decode_frame(s, samples) < 0) 
918 
goto fail;

919 
samples += s>nb_channels * s>frame_len; 
920 
} 
921  
922 
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d outbytes:%d eaten:%d\n", s>frame_len_bits, s>block_len_bits, s>frame_len, s>block_len, (int8_t *)samples  (int8_t *)data, s>block_align);

923  
924 
*data_size = (int8_t *)samples  (int8_t *)data; 
925 
return s>block_align;

926 
fail:

927 
/* when error, we reset the bit reservoir */

928 
s>last_superframe_len = 0;

929 
return 1; 
930 
} 
931  
932 
static av_cold void flush(AVCodecContext *avctx) 
933 
{ 
934 
WMACodecContext *s = avctx>priv_data; 
935  
936 
s>last_bitoffset= 
937 
s>last_superframe_len= 0;

938 
} 
939  
940 
AVCodec ff_wmav1_decoder = 
941 
{ 
942 
"wmav1",

943 
AVMEDIA_TYPE_AUDIO, 
944 
CODEC_ID_WMAV1, 
945 
sizeof(WMACodecContext),

946 
wma_decode_init, 
947 
NULL,

948 
ff_wma_end, 
949 
wma_decode_superframe, 
950 
.flush=flush, 
951 
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),

952 
}; 
953  
954 
AVCodec ff_wmav2_decoder = 
955 
{ 
956 
"wmav2",

957 
AVMEDIA_TYPE_AUDIO, 
958 
CODEC_ID_WMAV2, 
959 
sizeof(WMACodecContext),

960 
wma_decode_init, 
961 
NULL,

962 
ff_wma_end, 
963 
wma_decode_superframe, 
964 
.flush=flush, 
965 
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),

966 
}; 