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