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