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