00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00036 #include "avcodec.h"
00037 #include "wma.h"
00038
00039 #undef NDEBUG
00040 #include <assert.h>
00041
00042 #define EXPVLCBITS 8
00043 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
00044
00045 #define HGAINVLCBITS 9
00046 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
00047
00048 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
00049
00050 #ifdef TRACE
00051 static void dump_shorts(WMACodecContext *s, const char *name, const short *tab, int n)
00052 {
00053 int i;
00054
00055 tprintf(s->avctx, "%s[%d]:\n", name, n);
00056 for(i=0;i<n;i++) {
00057 if ((i & 7) == 0)
00058 tprintf(s->avctx, "%4d: ", i);
00059 tprintf(s->avctx, " %5d.0", tab[i]);
00060 if ((i & 7) == 7)
00061 tprintf(s->avctx, "\n");
00062 }
00063 }
00064
00065 static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
00066 {
00067 int i;
00068
00069 tprintf(s->avctx, "%s[%d]:\n", name, n);
00070 for(i=0;i<n;i++) {
00071 if ((i & 7) == 0)
00072 tprintf(s->avctx, "%4d: ", i);
00073 tprintf(s->avctx, " %8.*f", prec, tab[i]);
00074 if ((i & 7) == 7)
00075 tprintf(s->avctx, "\n");
00076 }
00077 if ((i & 7) != 0)
00078 tprintf(s->avctx, "\n");
00079 }
00080 #endif
00081
00082 static int wma_decode_init(AVCodecContext * avctx)
00083 {
00084 WMACodecContext *s = avctx->priv_data;
00085 int i, flags2;
00086 uint8_t *extradata;
00087
00088 s->avctx = avctx;
00089
00090
00091 flags2 = 0;
00092 extradata = avctx->extradata;
00093 if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
00094 flags2 = AV_RL16(extradata+2);
00095 } else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
00096 flags2 = AV_RL16(extradata+4);
00097 }
00098
00099
00100
00101 s->use_exp_vlc = flags2 & 0x0001;
00102 s->use_bit_reservoir = flags2 & 0x0002;
00103 s->use_variable_block_len = flags2 & 0x0004;
00104
00105 if(avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 8){
00106 if(AV_RL16(extradata+4)==0xd && s->use_variable_block_len){
00107 av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
00108 s->use_variable_block_len= 0;
00109 }
00110 }
00111
00112 if(avctx->channels > MAX_CHANNELS){
00113 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels (%d)\n", avctx->channels);
00114 return -1;
00115 }
00116
00117 if(ff_wma_init(avctx, flags2)<0)
00118 return -1;
00119
00120
00121 for(i = 0; i < s->nb_block_sizes; i++)
00122 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0);
00123
00124 if (s->use_noise_coding) {
00125 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
00126 ff_wma_hgain_huffbits, 1, 1,
00127 ff_wma_hgain_huffcodes, 2, 2, 0);
00128 }
00129
00130 if (s->use_exp_vlc) {
00131 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits),
00132 ff_aac_scalefactor_bits, 1, 1,
00133 ff_aac_scalefactor_code, 4, 4, 0);
00134 } else {
00135 wma_lsp_to_curve_init(s, s->frame_len);
00136 }
00137
00138 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00139
00140 avcodec_get_frame_defaults(&s->frame);
00141 avctx->coded_frame = &s->frame;
00142
00143 return 0;
00144 }
00145
00152 static inline float pow_m1_4(WMACodecContext *s, float x)
00153 {
00154 union {
00155 float f;
00156 unsigned int v;
00157 } u, t;
00158 unsigned int e, m;
00159 float a, b;
00160
00161 u.f = x;
00162 e = u.v >> 23;
00163 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
00164
00165 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
00166 a = s->lsp_pow_m_table1[m];
00167 b = s->lsp_pow_m_table2[m];
00168 return s->lsp_pow_e_table[e] * (a + b * t.f);
00169 }
00170
00171 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
00172 {
00173 float wdel, a, b;
00174 int i, e, m;
00175
00176 wdel = M_PI / frame_len;
00177 for(i=0;i<frame_len;i++)
00178 s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
00179
00180
00181 for(i=0;i<256;i++) {
00182 e = i - 126;
00183 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
00184 }
00185
00186
00187
00188 b = 1.0;
00189 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
00190 m = (1 << LSP_POW_BITS) + i;
00191 a = (float)m * (0.5 / (1 << LSP_POW_BITS));
00192 a = pow(a, -0.25);
00193 s->lsp_pow_m_table1[i] = 2 * a - b;
00194 s->lsp_pow_m_table2[i] = b - a;
00195 b = a;
00196 }
00197 }
00198
00203 static void wma_lsp_to_curve(WMACodecContext *s,
00204 float *out, float *val_max_ptr,
00205 int n, float *lsp)
00206 {
00207 int i, j;
00208 float p, q, w, v, val_max;
00209
00210 val_max = 0;
00211 for(i=0;i<n;i++) {
00212 p = 0.5f;
00213 q = 0.5f;
00214 w = s->lsp_cos_table[i];
00215 for(j=1;j<NB_LSP_COEFS;j+=2){
00216 q *= w - lsp[j - 1];
00217 p *= w - lsp[j];
00218 }
00219 p *= p * (2.0f - w);
00220 q *= q * (2.0f + w);
00221 v = p + q;
00222 v = pow_m1_4(s, v);
00223 if (v > val_max)
00224 val_max = v;
00225 out[i] = v;
00226 }
00227 *val_max_ptr = val_max;
00228 }
00229
00233 static void decode_exp_lsp(WMACodecContext *s, int ch)
00234 {
00235 float lsp_coefs[NB_LSP_COEFS];
00236 int val, i;
00237
00238 for(i = 0; i < NB_LSP_COEFS; i++) {
00239 if (i == 0 || i >= 8)
00240 val = get_bits(&s->gb, 3);
00241 else
00242 val = get_bits(&s->gb, 4);
00243 lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
00244 }
00245
00246 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
00247 s->block_len, lsp_coefs);
00248 }
00249
00251 static const float pow_tab[] = {
00252 1.7782794100389e-04, 2.0535250264571e-04,
00253 2.3713737056617e-04, 2.7384196342644e-04,
00254 3.1622776601684e-04, 3.6517412725484e-04,
00255 4.2169650342858e-04, 4.8696752516586e-04,
00256 5.6234132519035e-04, 6.4938163157621e-04,
00257 7.4989420933246e-04, 8.6596432336006e-04,
00258 1.0000000000000e-03, 1.1547819846895e-03,
00259 1.3335214321633e-03, 1.5399265260595e-03,
00260 1.7782794100389e-03, 2.0535250264571e-03,
00261 2.3713737056617e-03, 2.7384196342644e-03,
00262 3.1622776601684e-03, 3.6517412725484e-03,
00263 4.2169650342858e-03, 4.8696752516586e-03,
00264 5.6234132519035e-03, 6.4938163157621e-03,
00265 7.4989420933246e-03, 8.6596432336006e-03,
00266 1.0000000000000e-02, 1.1547819846895e-02,
00267 1.3335214321633e-02, 1.5399265260595e-02,
00268 1.7782794100389e-02, 2.0535250264571e-02,
00269 2.3713737056617e-02, 2.7384196342644e-02,
00270 3.1622776601684e-02, 3.6517412725484e-02,
00271 4.2169650342858e-02, 4.8696752516586e-02,
00272 5.6234132519035e-02, 6.4938163157621e-02,
00273 7.4989420933246e-02, 8.6596432336007e-02,
00274 1.0000000000000e-01, 1.1547819846895e-01,
00275 1.3335214321633e-01, 1.5399265260595e-01,
00276 1.7782794100389e-01, 2.0535250264571e-01,
00277 2.3713737056617e-01, 2.7384196342644e-01,
00278 3.1622776601684e-01, 3.6517412725484e-01,
00279 4.2169650342858e-01, 4.8696752516586e-01,
00280 5.6234132519035e-01, 6.4938163157621e-01,
00281 7.4989420933246e-01, 8.6596432336007e-01,
00282 1.0000000000000e+00, 1.1547819846895e+00,
00283 1.3335214321633e+00, 1.5399265260595e+00,
00284 1.7782794100389e+00, 2.0535250264571e+00,
00285 2.3713737056617e+00, 2.7384196342644e+00,
00286 3.1622776601684e+00, 3.6517412725484e+00,
00287 4.2169650342858e+00, 4.8696752516586e+00,
00288 5.6234132519035e+00, 6.4938163157621e+00,
00289 7.4989420933246e+00, 8.6596432336007e+00,
00290 1.0000000000000e+01, 1.1547819846895e+01,
00291 1.3335214321633e+01, 1.5399265260595e+01,
00292 1.7782794100389e+01, 2.0535250264571e+01,
00293 2.3713737056617e+01, 2.7384196342644e+01,
00294 3.1622776601684e+01, 3.6517412725484e+01,
00295 4.2169650342858e+01, 4.8696752516586e+01,
00296 5.6234132519035e+01, 6.4938163157621e+01,
00297 7.4989420933246e+01, 8.6596432336007e+01,
00298 1.0000000000000e+02, 1.1547819846895e+02,
00299 1.3335214321633e+02, 1.5399265260595e+02,
00300 1.7782794100389e+02, 2.0535250264571e+02,
00301 2.3713737056617e+02, 2.7384196342644e+02,
00302 3.1622776601684e+02, 3.6517412725484e+02,
00303 4.2169650342858e+02, 4.8696752516586e+02,
00304 5.6234132519035e+02, 6.4938163157621e+02,
00305 7.4989420933246e+02, 8.6596432336007e+02,
00306 1.0000000000000e+03, 1.1547819846895e+03,
00307 1.3335214321633e+03, 1.5399265260595e+03,
00308 1.7782794100389e+03, 2.0535250264571e+03,
00309 2.3713737056617e+03, 2.7384196342644e+03,
00310 3.1622776601684e+03, 3.6517412725484e+03,
00311 4.2169650342858e+03, 4.8696752516586e+03,
00312 5.6234132519035e+03, 6.4938163157621e+03,
00313 7.4989420933246e+03, 8.6596432336007e+03,
00314 1.0000000000000e+04, 1.1547819846895e+04,
00315 1.3335214321633e+04, 1.5399265260595e+04,
00316 1.7782794100389e+04, 2.0535250264571e+04,
00317 2.3713737056617e+04, 2.7384196342644e+04,
00318 3.1622776601684e+04, 3.6517412725484e+04,
00319 4.2169650342858e+04, 4.8696752516586e+04,
00320 5.6234132519035e+04, 6.4938163157621e+04,
00321 7.4989420933246e+04, 8.6596432336007e+04,
00322 1.0000000000000e+05, 1.1547819846895e+05,
00323 1.3335214321633e+05, 1.5399265260595e+05,
00324 1.7782794100389e+05, 2.0535250264571e+05,
00325 2.3713737056617e+05, 2.7384196342644e+05,
00326 3.1622776601684e+05, 3.6517412725484e+05,
00327 4.2169650342858e+05, 4.8696752516586e+05,
00328 5.6234132519035e+05, 6.4938163157621e+05,
00329 7.4989420933246e+05, 8.6596432336007e+05,
00330 };
00331
00335 static int decode_exp_vlc(WMACodecContext *s, int ch)
00336 {
00337 int last_exp, n, code;
00338 const uint16_t *ptr;
00339 float v, max_scale;
00340 uint32_t *q, *q_end, iv;
00341 const float *ptab = pow_tab + 60;
00342 const uint32_t *iptab = (const uint32_t*)ptab;
00343
00344 ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
00345 q = (uint32_t *)s->exponents[ch];
00346 q_end = q + s->block_len;
00347 max_scale = 0;
00348 if (s->version == 1) {
00349 last_exp = get_bits(&s->gb, 5) + 10;
00350 v = ptab[last_exp];
00351 iv = iptab[last_exp];
00352 max_scale = v;
00353 n = *ptr++;
00354 switch (n & 3) do {
00355 case 0: *q++ = iv;
00356 case 3: *q++ = iv;
00357 case 2: *q++ = iv;
00358 case 1: *q++ = iv;
00359 } while ((n -= 4) > 0);
00360 }else
00361 last_exp = 36;
00362
00363 while (q < q_end) {
00364 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
00365 if (code < 0){
00366 av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
00367 return -1;
00368 }
00369
00370 last_exp += code - 60;
00371 if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
00372 av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
00373 last_exp);
00374 return -1;
00375 }
00376 v = ptab[last_exp];
00377 iv = iptab[last_exp];
00378 if (v > max_scale)
00379 max_scale = v;
00380 n = *ptr++;
00381 switch (n & 3) do {
00382 case 0: *q++ = iv;
00383 case 3: *q++ = iv;
00384 case 2: *q++ = iv;
00385 case 1: *q++ = iv;
00386 } while ((n -= 4) > 0);
00387 }
00388 s->max_exponent[ch] = max_scale;
00389 return 0;
00390 }
00391
00392
00399 static void wma_window(WMACodecContext *s, float *out)
00400 {
00401 float *in = s->output;
00402 int block_len, bsize, n;
00403
00404
00405 if (s->block_len_bits <= s->prev_block_len_bits) {
00406 block_len = s->block_len;
00407 bsize = s->frame_len_bits - s->block_len_bits;
00408
00409 s->dsp.vector_fmul_add(out, in, s->windows[bsize],
00410 out, block_len);
00411
00412 } else {
00413 block_len = 1 << s->prev_block_len_bits;
00414 n = (s->block_len - block_len) / 2;
00415 bsize = s->frame_len_bits - s->prev_block_len_bits;
00416
00417 s->dsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
00418 out+n, block_len);
00419
00420 memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
00421 }
00422
00423 out += s->block_len;
00424 in += s->block_len;
00425
00426
00427 if (s->block_len_bits <= s->next_block_len_bits) {
00428 block_len = s->block_len;
00429 bsize = s->frame_len_bits - s->block_len_bits;
00430
00431 s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
00432
00433 } else {
00434 block_len = 1 << s->next_block_len_bits;
00435 n = (s->block_len - block_len) / 2;
00436 bsize = s->frame_len_bits - s->next_block_len_bits;
00437
00438 memcpy(out, in, n*sizeof(float));
00439
00440 s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
00441
00442 memset(out+n+block_len, 0, n*sizeof(float));
00443 }
00444 }
00445
00446
00451 static int wma_decode_block(WMACodecContext *s)
00452 {
00453 int n, v, a, ch, bsize;
00454 int coef_nb_bits, total_gain;
00455 int nb_coefs[MAX_CHANNELS];
00456 float mdct_norm;
00457 FFTContext *mdct;
00458
00459 #ifdef TRACE
00460 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
00461 #endif
00462
00463
00464 if (s->use_variable_block_len) {
00465 n = av_log2(s->nb_block_sizes - 1) + 1;
00466
00467 if (s->reset_block_lengths) {
00468 s->reset_block_lengths = 0;
00469 v = get_bits(&s->gb, n);
00470 if (v >= s->nb_block_sizes){
00471 av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
00472 return -1;
00473 }
00474 s->prev_block_len_bits = s->frame_len_bits - v;
00475 v = get_bits(&s->gb, n);
00476 if (v >= s->nb_block_sizes){
00477 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
00478 return -1;
00479 }
00480 s->block_len_bits = s->frame_len_bits - v;
00481 } else {
00482
00483 s->prev_block_len_bits = s->block_len_bits;
00484 s->block_len_bits = s->next_block_len_bits;
00485 }
00486 v = get_bits(&s->gb, n);
00487 if (v >= s->nb_block_sizes){
00488 av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
00489 return -1;
00490 }
00491 s->next_block_len_bits = s->frame_len_bits - v;
00492 } else {
00493
00494 s->next_block_len_bits = s->frame_len_bits;
00495 s->prev_block_len_bits = s->frame_len_bits;
00496 s->block_len_bits = s->frame_len_bits;
00497 }
00498
00499 if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){
00500 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n");
00501 return -1;
00502 }
00503
00504
00505 s->block_len = 1 << s->block_len_bits;
00506 if ((s->block_pos + s->block_len) > s->frame_len){
00507 av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
00508 return -1;
00509 }
00510
00511 if (s->nb_channels == 2) {
00512 s->ms_stereo = get_bits1(&s->gb);
00513 }
00514 v = 0;
00515 for(ch = 0; ch < s->nb_channels; ch++) {
00516 a = get_bits1(&s->gb);
00517 s->channel_coded[ch] = a;
00518 v |= a;
00519 }
00520
00521 bsize = s->frame_len_bits - s->block_len_bits;
00522
00523
00524
00525 if (!v)
00526 goto next;
00527
00528
00529
00530 total_gain = 1;
00531 for(;;) {
00532 a = get_bits(&s->gb, 7);
00533 total_gain += a;
00534 if (a != 127)
00535 break;
00536 }
00537
00538 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
00539
00540
00541 n = s->coefs_end[bsize] - s->coefs_start;
00542 for(ch = 0; ch < s->nb_channels; ch++)
00543 nb_coefs[ch] = n;
00544
00545
00546 if (s->use_noise_coding) {
00547
00548 for(ch = 0; ch < s->nb_channels; ch++) {
00549 if (s->channel_coded[ch]) {
00550 int i, n, a;
00551 n = s->exponent_high_sizes[bsize];
00552 for(i=0;i<n;i++) {
00553 a = get_bits1(&s->gb);
00554 s->high_band_coded[ch][i] = a;
00555
00556 if (a)
00557 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
00558 }
00559 }
00560 }
00561 for(ch = 0; ch < s->nb_channels; ch++) {
00562 if (s->channel_coded[ch]) {
00563 int i, n, val, code;
00564
00565 n = s->exponent_high_sizes[bsize];
00566 val = (int)0x80000000;
00567 for(i=0;i<n;i++) {
00568 if (s->high_band_coded[ch][i]) {
00569 if (val == (int)0x80000000) {
00570 val = get_bits(&s->gb, 7) - 19;
00571 } else {
00572 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
00573 if (code < 0){
00574 av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
00575 return -1;
00576 }
00577 val += code - 18;
00578 }
00579 s->high_band_values[ch][i] = val;
00580 }
00581 }
00582 }
00583 }
00584 }
00585
00586
00587 if ((s->block_len_bits == s->frame_len_bits) ||
00588 get_bits1(&s->gb)) {
00589 for(ch = 0; ch < s->nb_channels; ch++) {
00590 if (s->channel_coded[ch]) {
00591 if (s->use_exp_vlc) {
00592 if (decode_exp_vlc(s, ch) < 0)
00593 return -1;
00594 } else {
00595 decode_exp_lsp(s, ch);
00596 }
00597 s->exponents_bsize[ch] = bsize;
00598 }
00599 }
00600 }
00601
00602
00603 for(ch = 0; ch < s->nb_channels; ch++) {
00604 if (s->channel_coded[ch]) {
00605 int tindex;
00606 WMACoef* ptr = &s->coefs1[ch][0];
00607
00608
00609
00610 tindex = (ch == 1 && s->ms_stereo);
00611 memset(ptr, 0, s->block_len * sizeof(WMACoef));
00612 ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
00613 s->level_table[tindex], s->run_table[tindex],
00614 0, ptr, 0, nb_coefs[ch],
00615 s->block_len, s->frame_len_bits, coef_nb_bits);
00616 }
00617 if (s->version == 1 && s->nb_channels >= 2) {
00618 align_get_bits(&s->gb);
00619 }
00620 }
00621
00622
00623 {
00624 int n4 = s->block_len / 2;
00625 mdct_norm = 1.0 / (float)n4;
00626 if (s->version == 1) {
00627 mdct_norm *= sqrt(n4);
00628 }
00629 }
00630
00631
00632 for(ch = 0; ch < s->nb_channels; ch++) {
00633 if (s->channel_coded[ch]) {
00634 WMACoef *coefs1;
00635 float *coefs, *exponents, mult, mult1, noise;
00636 int i, j, n, n1, last_high_band, esize;
00637 float exp_power[HIGH_BAND_MAX_SIZE];
00638
00639 coefs1 = s->coefs1[ch];
00640 exponents = s->exponents[ch];
00641 esize = s->exponents_bsize[ch];
00642 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
00643 mult *= mdct_norm;
00644 coefs = s->coefs[ch];
00645 if (s->use_noise_coding) {
00646 mult1 = mult;
00647
00648 for(i = 0;i < s->coefs_start; i++) {
00649 *coefs++ = s->noise_table[s->noise_index] *
00650 exponents[i<<bsize>>esize] * mult1;
00651 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
00652 }
00653
00654 n1 = s->exponent_high_sizes[bsize];
00655
00656
00657 exponents = s->exponents[ch] +
00658 (s->high_band_start[bsize]<<bsize>>esize);
00659 last_high_band = 0;
00660 for(j=0;j<n1;j++) {
00661 n = s->exponent_high_bands[s->frame_len_bits -
00662 s->block_len_bits][j];
00663 if (s->high_band_coded[ch][j]) {
00664 float e2, v;
00665 e2 = 0;
00666 for(i = 0;i < n; i++) {
00667 v = exponents[i<<bsize>>esize];
00668 e2 += v * v;
00669 }
00670 exp_power[j] = e2 / n;
00671 last_high_band = j;
00672 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
00673 }
00674 exponents += n<<bsize>>esize;
00675 }
00676
00677
00678 exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
00679 for(j=-1;j<n1;j++) {
00680 if (j < 0) {
00681 n = s->high_band_start[bsize] -
00682 s->coefs_start;
00683 } else {
00684 n = s->exponent_high_bands[s->frame_len_bits -
00685 s->block_len_bits][j];
00686 }
00687 if (j >= 0 && s->high_band_coded[ch][j]) {
00688
00689 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
00690
00691 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
00692 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
00693 mult1 *= mdct_norm;
00694 for(i = 0;i < n; i++) {
00695 noise = s->noise_table[s->noise_index];
00696 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
00697 *coefs++ = noise *
00698 exponents[i<<bsize>>esize] * mult1;
00699 }
00700 exponents += n<<bsize>>esize;
00701 } else {
00702
00703 for(i = 0;i < n; i++) {
00704 noise = s->noise_table[s->noise_index];
00705 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
00706 *coefs++ = ((*coefs1++) + noise) *
00707 exponents[i<<bsize>>esize] * mult;
00708 }
00709 exponents += n<<bsize>>esize;
00710 }
00711 }
00712
00713
00714 n = s->block_len - s->coefs_end[bsize];
00715 mult1 = mult * exponents[((-1<<bsize))>>esize];
00716 for(i = 0; i < n; i++) {
00717 *coefs++ = s->noise_table[s->noise_index] * mult1;
00718 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
00719 }
00720 } else {
00721
00722 for(i = 0;i < s->coefs_start; i++)
00723 *coefs++ = 0.0;
00724 n = nb_coefs[ch];
00725 for(i = 0;i < n; i++) {
00726 *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
00727 }
00728 n = s->block_len - s->coefs_end[bsize];
00729 for(i = 0;i < n; i++)
00730 *coefs++ = 0.0;
00731 }
00732 }
00733 }
00734
00735 #ifdef TRACE
00736 for(ch = 0; ch < s->nb_channels; ch++) {
00737 if (s->channel_coded[ch]) {
00738 dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
00739 dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
00740 }
00741 }
00742 #endif
00743
00744 if (s->ms_stereo && s->channel_coded[1]) {
00745
00746
00747
00748 if (!s->channel_coded[0]) {
00749 tprintf(s->avctx, "rare ms-stereo case happened\n");
00750 memset(s->coefs[0], 0, sizeof(float) * s->block_len);
00751 s->channel_coded[0] = 1;
00752 }
00753
00754 s->dsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
00755 }
00756
00757 next:
00758 mdct = &s->mdct_ctx[bsize];
00759
00760 for(ch = 0; ch < s->nb_channels; ch++) {
00761 int n4, index;
00762
00763 n4 = s->block_len / 2;
00764 if(s->channel_coded[ch]){
00765 mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
00766 }else if(!(s->ms_stereo && ch==1))
00767 memset(s->output, 0, sizeof(s->output));
00768
00769
00770 index = (s->frame_len / 2) + s->block_pos - n4;
00771 wma_window(s, &s->frame_out[ch][index]);
00772 }
00773
00774
00775 s->block_num++;
00776 s->block_pos += s->block_len;
00777 if (s->block_pos >= s->frame_len)
00778 return 1;
00779 else
00780 return 0;
00781 }
00782
00783
00784 static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
00785 {
00786 int ret, n, ch, incr;
00787 const float *output[MAX_CHANNELS];
00788
00789 #ifdef TRACE
00790 tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
00791 #endif
00792
00793
00794 s->block_num = 0;
00795 s->block_pos = 0;
00796 for(;;) {
00797 ret = wma_decode_block(s);
00798 if (ret < 0)
00799 return -1;
00800 if (ret)
00801 break;
00802 }
00803
00804
00805 n = s->frame_len;
00806 incr = s->nb_channels;
00807 for (ch = 0; ch < MAX_CHANNELS; ch++)
00808 output[ch] = s->frame_out[ch];
00809 s->fmt_conv.float_to_int16_interleave(samples, output, n, incr);
00810 for (ch = 0; ch < incr; ch++) {
00811
00812 memmove(&s->frame_out[ch][0], &s->frame_out[ch][n], n * sizeof(float));
00813 }
00814
00815 #ifdef TRACE
00816 dump_shorts(s, "samples", samples, n * s->nb_channels);
00817 #endif
00818 return 0;
00819 }
00820
00821 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
00822 int *got_frame_ptr, AVPacket *avpkt)
00823 {
00824 const uint8_t *buf = avpkt->data;
00825 int buf_size = avpkt->size;
00826 WMACodecContext *s = avctx->priv_data;
00827 int nb_frames, bit_offset, i, pos, len, ret;
00828 uint8_t *q;
00829 int16_t *samples;
00830
00831 tprintf(avctx, "***decode_superframe:\n");
00832
00833 if(buf_size==0){
00834 s->last_superframe_len = 0;
00835 return 0;
00836 }
00837 if (buf_size < s->block_align) {
00838 av_log(avctx, AV_LOG_ERROR,
00839 "Input packet size too small (%d < %d)\n",
00840 buf_size, s->block_align);
00841 return AVERROR_INVALIDDATA;
00842 }
00843 if(s->block_align)
00844 buf_size = s->block_align;
00845
00846 init_get_bits(&s->gb, buf, buf_size*8);
00847
00848 if (s->use_bit_reservoir) {
00849
00850 skip_bits(&s->gb, 4);
00851 nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
00852 } else {
00853 nb_frames = 1;
00854 }
00855
00856
00857 s->frame.nb_samples = nb_frames * s->frame_len;
00858 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00859 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00860 return ret;
00861 }
00862 samples = (int16_t *)s->frame.data[0];
00863
00864 if (s->use_bit_reservoir) {
00865 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
00866 if (bit_offset > get_bits_left(&s->gb)) {
00867 av_log(avctx, AV_LOG_ERROR,
00868 "Invalid last frame bit offset %d > buf size %d (%d)\n",
00869 bit_offset, get_bits_left(&s->gb), buf_size);
00870 goto fail;
00871 }
00872
00873 if (s->last_superframe_len > 0) {
00874
00875
00876 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
00877 MAX_CODED_SUPERFRAME_SIZE)
00878 goto fail;
00879 q = s->last_superframe + s->last_superframe_len;
00880 len = bit_offset;
00881 while (len > 7) {
00882 *q++ = (get_bits)(&s->gb, 8);
00883 len -= 8;
00884 }
00885 if (len > 0) {
00886 *q++ = (get_bits)(&s->gb, len) << (8 - len);
00887 }
00888 memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00889
00890
00891 init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
00892
00893 if (s->last_bitoffset > 0)
00894 skip_bits(&s->gb, s->last_bitoffset);
00895
00896
00897 if (wma_decode_frame(s, samples) < 0)
00898 goto fail;
00899 samples += s->nb_channels * s->frame_len;
00900 nb_frames--;
00901 }
00902
00903
00904 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
00905 if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
00906 return AVERROR_INVALIDDATA;
00907 init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
00908 len = pos & 7;
00909 if (len > 0)
00910 skip_bits(&s->gb, len);
00911
00912 s->reset_block_lengths = 1;
00913 for(i=0;i<nb_frames;i++) {
00914 if (wma_decode_frame(s, samples) < 0)
00915 goto fail;
00916 samples += s->nb_channels * s->frame_len;
00917 }
00918
00919
00920 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
00921 s->last_bitoffset = pos & 7;
00922 pos >>= 3;
00923 len = buf_size - pos;
00924 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
00925 av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
00926 goto fail;
00927 }
00928 s->last_superframe_len = len;
00929 memcpy(s->last_superframe, buf + pos, len);
00930 } else {
00931
00932 if (wma_decode_frame(s, samples) < 0)
00933 goto fail;
00934 samples += s->nb_channels * s->frame_len;
00935 }
00936
00937
00938
00939 *got_frame_ptr = 1;
00940 *(AVFrame *)data = s->frame;
00941
00942 return buf_size;
00943 fail:
00944
00945 s->last_superframe_len = 0;
00946 return -1;
00947 }
00948
00949 static av_cold void flush(AVCodecContext *avctx)
00950 {
00951 WMACodecContext *s = avctx->priv_data;
00952
00953 s->last_bitoffset=
00954 s->last_superframe_len= 0;
00955 }
00956
00957 AVCodec ff_wmav1_decoder = {
00958 .name = "wmav1",
00959 .type = AVMEDIA_TYPE_AUDIO,
00960 .id = CODEC_ID_WMAV1,
00961 .priv_data_size = sizeof(WMACodecContext),
00962 .init = wma_decode_init,
00963 .close = ff_wma_end,
00964 .decode = wma_decode_superframe,
00965 .flush = flush,
00966 .capabilities = CODEC_CAP_DR1,
00967 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
00968 };
00969
00970 AVCodec ff_wmav2_decoder = {
00971 .name = "wmav2",
00972 .type = AVMEDIA_TYPE_AUDIO,
00973 .id = CODEC_ID_WMAV2,
00974 .priv_data_size = sizeof(WMACodecContext),
00975 .init = wma_decode_init,
00976 .close = ff_wma_end,
00977 .decode = wma_decode_superframe,
00978 .flush = flush,
00979 .capabilities = CODEC_CAP_DR1,
00980 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
00981 };