00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00089 #include "avcodec.h"
00090 #include "internal.h"
00091 #include "get_bits.h"
00092 #include "put_bits.h"
00093 #include "wmaprodata.h"
00094 #include "dsputil.h"
00095 #include "sinewin.h"
00096 #include "wma.h"
00097
00099 #define WMAPRO_MAX_CHANNELS 8
00100 #define MAX_SUBFRAMES 32
00101 #define MAX_BANDS 29
00102 #define MAX_FRAMESIZE 32768
00103
00104 #define WMAPRO_BLOCK_MIN_BITS 6
00105 #define WMAPRO_BLOCK_MAX_BITS 12
00106 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
00107 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
00108
00109
00110 #define VLCBITS 9
00111 #define SCALEVLCBITS 8
00112 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00113 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00114 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00115 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00116 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00117
00118 static VLC sf_vlc;
00119 static VLC sf_rl_vlc;
00120 static VLC vec4_vlc;
00121 static VLC vec2_vlc;
00122 static VLC vec1_vlc;
00123 static VLC coef_vlc[2];
00124 static float sin64[33];
00125
00129 typedef struct {
00130 int16_t prev_block_len;
00131 uint8_t transmit_coefs;
00132 uint8_t num_subframes;
00133 uint16_t subframe_len[MAX_SUBFRAMES];
00134 uint16_t subframe_offset[MAX_SUBFRAMES];
00135 uint8_t cur_subframe;
00136 uint16_t decoded_samples;
00137 uint8_t grouped;
00138 int quant_step;
00139 int8_t reuse_sf;
00140 int8_t scale_factor_step;
00141 int max_scale_factor;
00142 int saved_scale_factors[2][MAX_BANDS];
00143 int8_t scale_factor_idx;
00144 int* scale_factors;
00145 uint8_t table_idx;
00146 float* coeffs;
00147 uint16_t num_vec_coeffs;
00148 DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2];
00149 } WMAProChannelCtx;
00150
00154 typedef struct {
00155 uint8_t num_channels;
00156 int8_t transform;
00157 int8_t transform_band[MAX_BANDS];
00158 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00159 float* channel_data[WMAPRO_MAX_CHANNELS];
00160 } WMAProChannelGrp;
00161
00165 typedef struct WMAProDecodeCtx {
00166
00167 AVCodecContext* avctx;
00168 DSPContext dsp;
00169 uint8_t frame_data[MAX_FRAMESIZE +
00170 FF_INPUT_BUFFER_PADDING_SIZE];
00171 PutBitContext pb;
00172 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES];
00173 DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE];
00174 float* windows[WMAPRO_BLOCK_SIZES];
00175
00176
00177 uint32_t decode_flags;
00178 uint8_t len_prefix;
00179 uint8_t dynamic_range_compression;
00180 uint8_t bits_per_sample;
00181 uint16_t samples_per_frame;
00182 uint16_t log2_frame_size;
00183 int8_t num_channels;
00184 int8_t lfe_channel;
00185 uint8_t max_num_subframes;
00186 uint8_t subframe_len_bits;
00187 uint8_t max_subframe_len_bit;
00188 uint16_t min_samples_per_subframe;
00189 int8_t num_sfb[WMAPRO_BLOCK_SIZES];
00190 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];
00191 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS];
00192 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES];
00193
00194
00195 GetBitContext pgb;
00196 int next_packet_start;
00197 uint8_t packet_offset;
00198 uint8_t packet_sequence_number;
00199 int num_saved_bits;
00200 int frame_offset;
00201 int subframe_offset;
00202 uint8_t packet_loss;
00203 uint8_t packet_done;
00204
00205
00206 uint32_t frame_num;
00207 GetBitContext gb;
00208 int buf_bit_size;
00209 float* samples;
00210 float* samples_end;
00211 uint8_t drc_gain;
00212 int8_t skip_frame;
00213 int8_t parsed_all_subframes;
00214
00215
00216 int16_t subframe_len;
00217 int8_t channels_for_cur_subframe;
00218 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00219 int8_t num_bands;
00220 int8_t transmit_num_vec_coeffs;
00221 int16_t* cur_sfb_offsets;
00222 uint8_t table_idx;
00223 int8_t esc_len;
00224
00225 uint8_t num_chgroups;
00226 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];
00227
00228 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];
00229 } WMAProDecodeCtx;
00230
00231
00236 static void av_cold dump_context(WMAProDecodeCtx *s)
00237 {
00238 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00239 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00240
00241 PRINT("ed sample bit depth", s->bits_per_sample);
00242 PRINT_HEX("ed decode flags", s->decode_flags);
00243 PRINT("samples per frame", s->samples_per_frame);
00244 PRINT("log2 frame size", s->log2_frame_size);
00245 PRINT("max num subframes", s->max_num_subframes);
00246 PRINT("len prefix", s->len_prefix);
00247 PRINT("num channels", s->num_channels);
00248 }
00249
00255 static av_cold int decode_end(AVCodecContext *avctx)
00256 {
00257 WMAProDecodeCtx *s = avctx->priv_data;
00258 int i;
00259
00260 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00261 ff_mdct_end(&s->mdct_ctx[i]);
00262
00263 return 0;
00264 }
00265
00271 static av_cold int decode_init(AVCodecContext *avctx)
00272 {
00273 WMAProDecodeCtx *s = avctx->priv_data;
00274 uint8_t *edata_ptr = avctx->extradata;
00275 unsigned int channel_mask;
00276 int i;
00277 int log2_max_num_subframes;
00278 int num_possible_block_sizes;
00279
00280 s->avctx = avctx;
00281 dsputil_init(&s->dsp, avctx);
00282 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00283
00284 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00285
00286 if (avctx->extradata_size >= 18) {
00287 s->decode_flags = AV_RL16(edata_ptr+14);
00288 channel_mask = AV_RL32(edata_ptr+2);
00289 s->bits_per_sample = AV_RL16(edata_ptr);
00291 for (i = 0; i < avctx->extradata_size; i++)
00292 av_dlog(avctx, "[%x] ", avctx->extradata[i]);
00293 av_dlog(avctx, "\n");
00294
00295 } else {
00296 av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00297 return AVERROR_INVALIDDATA;
00298 }
00299
00301 s->log2_frame_size = av_log2(avctx->block_align) + 4;
00302
00304 s->skip_frame = 1;
00305 s->packet_loss = 1;
00306 s->len_prefix = (s->decode_flags & 0x40);
00307
00309 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00310 3, s->decode_flags);
00311
00313 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
00314 s->max_num_subframes = 1 << log2_max_num_subframes;
00315 if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
00316 s->max_subframe_len_bit = 1;
00317 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00318
00319 num_possible_block_sizes = log2_max_num_subframes + 1;
00320 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
00321 s->dynamic_range_compression = (s->decode_flags & 0x80);
00322
00323 if (s->max_num_subframes > MAX_SUBFRAMES) {
00324 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00325 s->max_num_subframes);
00326 return AVERROR_INVALIDDATA;
00327 }
00328
00329 s->num_channels = avctx->channels;
00330
00331 if (s->num_channels < 0) {
00332 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00333 return AVERROR_INVALIDDATA;
00334 } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00335 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00336 return AVERROR_PATCHWELCOME;
00337 }
00338
00340 for (i = 0; i < s->num_channels; i++)
00341 s->channel[i].prev_block_len = s->samples_per_frame;
00342
00344 s->lfe_channel = -1;
00345
00346 if (channel_mask & 8) {
00347 unsigned int mask;
00348 for (mask = 1; mask < 16; mask <<= 1) {
00349 if (channel_mask & mask)
00350 ++s->lfe_channel;
00351 }
00352 }
00353
00354 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00355 scale_huffbits, 1, 1,
00356 scale_huffcodes, 2, 2, 616);
00357
00358 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00359 scale_rl_huffbits, 1, 1,
00360 scale_rl_huffcodes, 4, 4, 1406);
00361
00362 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00363 coef0_huffbits, 1, 1,
00364 coef0_huffcodes, 4, 4, 2108);
00365
00366 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00367 coef1_huffbits, 1, 1,
00368 coef1_huffcodes, 4, 4, 3912);
00369
00370 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00371 vec4_huffbits, 1, 1,
00372 vec4_huffcodes, 2, 2, 604);
00373
00374 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00375 vec2_huffbits, 1, 1,
00376 vec2_huffcodes, 2, 2, 562);
00377
00378 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00379 vec1_huffbits, 1, 1,
00380 vec1_huffcodes, 2, 2, 562);
00381
00384 for (i = 0; i < num_possible_block_sizes; i++) {
00385 int subframe_len = s->samples_per_frame >> i;
00386 int x;
00387 int band = 1;
00388
00389 s->sfb_offsets[i][0] = 0;
00390
00391 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00392 int offset = (subframe_len * 2 * critical_freq[x])
00393 / s->avctx->sample_rate + 2;
00394 offset &= ~3;
00395 if (offset > s->sfb_offsets[i][band - 1])
00396 s->sfb_offsets[i][band++] = offset;
00397 }
00398 s->sfb_offsets[i][band - 1] = subframe_len;
00399 s->num_sfb[i] = band - 1;
00400 }
00401
00402
00408 for (i = 0; i < num_possible_block_sizes; i++) {
00409 int b;
00410 for (b = 0; b < s->num_sfb[i]; b++) {
00411 int x;
00412 int offset = ((s->sfb_offsets[i][b]
00413 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00414 for (x = 0; x < num_possible_block_sizes; x++) {
00415 int v = 0;
00416 while (s->sfb_offsets[x][v + 1] << x < offset)
00417 ++v;
00418 s->sf_offsets[i][x][b] = v;
00419 }
00420 }
00421 }
00422
00424 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00425 ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00426 1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00427 / (1 << (s->bits_per_sample - 1)));
00428
00430 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00431 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00432 ff_init_ff_sine_windows(win_idx);
00433 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00434 }
00435
00437 for (i = 0; i < num_possible_block_sizes; i++) {
00438 int block_size = s->samples_per_frame >> i;
00439 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00440 / s->avctx->sample_rate;
00441 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00442 }
00443
00445 for (i = 0; i < 33; i++)
00446 sin64[i] = sin(i*M_PI / 64.0);
00447
00448 if (avctx->debug & FF_DEBUG_BITSTREAM)
00449 dump_context(s);
00450
00451 avctx->channel_layout = channel_mask;
00452 return 0;
00453 }
00454
00461 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00462 {
00463 int frame_len_shift = 0;
00464 int subframe_len;
00465
00467 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00468 return s->min_samples_per_subframe;
00469
00471 if (s->max_subframe_len_bit) {
00472 if (get_bits1(&s->gb))
00473 frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00474 } else
00475 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00476
00477 subframe_len = s->samples_per_frame >> frame_len_shift;
00478
00480 if (subframe_len < s->min_samples_per_subframe ||
00481 subframe_len > s->samples_per_frame) {
00482 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00483 subframe_len);
00484 return AVERROR_INVALIDDATA;
00485 }
00486 return subframe_len;
00487 }
00488
00509 static int decode_tilehdr(WMAProDecodeCtx *s)
00510 {
00511 uint16_t num_samples[WMAPRO_MAX_CHANNELS];
00512 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS];
00513 int channels_for_cur_subframe = s->num_channels;
00514 int fixed_channel_layout = 0;
00515 int min_channel_len = 0;
00516 int c;
00517
00518
00519
00520
00521
00522
00523
00525 for (c = 0; c < s->num_channels; c++)
00526 s->channel[c].num_subframes = 0;
00527
00528 memset(num_samples, 0, sizeof(num_samples));
00529
00530 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00531 fixed_channel_layout = 1;
00532
00534 do {
00535 int subframe_len;
00536
00538 for (c = 0; c < s->num_channels; c++) {
00539 if (num_samples[c] == min_channel_len) {
00540 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00541 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00542 contains_subframe[c] = 1;
00543 else
00544 contains_subframe[c] = get_bits1(&s->gb);
00545 } else
00546 contains_subframe[c] = 0;
00547 }
00548
00550 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00551 return AVERROR_INVALIDDATA;
00552
00554 min_channel_len += subframe_len;
00555 for (c = 0; c < s->num_channels; c++) {
00556 WMAProChannelCtx* chan = &s->channel[c];
00557
00558 if (contains_subframe[c]) {
00559 if (chan->num_subframes >= MAX_SUBFRAMES) {
00560 av_log(s->avctx, AV_LOG_ERROR,
00561 "broken frame: num subframes > 31\n");
00562 return AVERROR_INVALIDDATA;
00563 }
00564 chan->subframe_len[chan->num_subframes] = subframe_len;
00565 num_samples[c] += subframe_len;
00566 ++chan->num_subframes;
00567 if (num_samples[c] > s->samples_per_frame) {
00568 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00569 "channel len > samples_per_frame\n");
00570 return AVERROR_INVALIDDATA;
00571 }
00572 } else if (num_samples[c] <= min_channel_len) {
00573 if (num_samples[c] < min_channel_len) {
00574 channels_for_cur_subframe = 0;
00575 min_channel_len = num_samples[c];
00576 }
00577 ++channels_for_cur_subframe;
00578 }
00579 }
00580 } while (min_channel_len < s->samples_per_frame);
00581
00582 for (c = 0; c < s->num_channels; c++) {
00583 int i;
00584 int offset = 0;
00585 for (i = 0; i < s->channel[c].num_subframes; i++) {
00586 av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00587 " len %i\n", s->frame_num, c, i,
00588 s->channel[c].subframe_len[i]);
00589 s->channel[c].subframe_offset[i] = offset;
00590 offset += s->channel[c].subframe_len[i];
00591 }
00592 }
00593
00594 return 0;
00595 }
00596
00602 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00603 WMAProChannelGrp *chgroup)
00604 {
00605 int i;
00606 int offset = 0;
00607 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00608 memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00609 s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00610
00611 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00612 rotation_offset[i] = get_bits(&s->gb, 6);
00613
00614 for (i = 0; i < chgroup->num_channels; i++)
00615 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00616 get_bits1(&s->gb) ? 1.0 : -1.0;
00617
00618 for (i = 1; i < chgroup->num_channels; i++) {
00619 int x;
00620 for (x = 0; x < i; x++) {
00621 int y;
00622 for (y = 0; y < i + 1; y++) {
00623 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00624 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00625 int n = rotation_offset[offset + x];
00626 float sinv;
00627 float cosv;
00628
00629 if (n < 32) {
00630 sinv = sin64[n];
00631 cosv = sin64[32 - n];
00632 } else {
00633 sinv = sin64[64 - n];
00634 cosv = -sin64[n - 32];
00635 }
00636
00637 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00638 (v1 * sinv) - (v2 * cosv);
00639 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00640 (v1 * cosv) + (v2 * sinv);
00641 }
00642 }
00643 offset += i;
00644 }
00645 }
00646
00652 static int decode_channel_transform(WMAProDecodeCtx* s)
00653 {
00654 int i;
00655
00656
00657
00658
00659
00661 s->num_chgroups = 0;
00662 if (s->num_channels > 1) {
00663 int remaining_channels = s->channels_for_cur_subframe;
00664
00665 if (get_bits1(&s->gb)) {
00666 av_log_ask_for_sample(s->avctx,
00667 "unsupported channel transform bit\n");
00668 return AVERROR_INVALIDDATA;
00669 }
00670
00671 for (s->num_chgroups = 0; remaining_channels &&
00672 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00673 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00674 float** channel_data = chgroup->channel_data;
00675 chgroup->num_channels = 0;
00676 chgroup->transform = 0;
00677
00679 if (remaining_channels > 2) {
00680 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00681 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00682 if (!s->channel[channel_idx].grouped
00683 && get_bits1(&s->gb)) {
00684 ++chgroup->num_channels;
00685 s->channel[channel_idx].grouped = 1;
00686 *channel_data++ = s->channel[channel_idx].coeffs;
00687 }
00688 }
00689 } else {
00690 chgroup->num_channels = remaining_channels;
00691 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00692 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00693 if (!s->channel[channel_idx].grouped)
00694 *channel_data++ = s->channel[channel_idx].coeffs;
00695 s->channel[channel_idx].grouped = 1;
00696 }
00697 }
00698
00700 if (chgroup->num_channels == 2) {
00701 if (get_bits1(&s->gb)) {
00702 if (get_bits1(&s->gb)) {
00703 av_log_ask_for_sample(s->avctx,
00704 "unsupported channel transform type\n");
00705 }
00706 } else {
00707 chgroup->transform = 1;
00708 if (s->num_channels == 2) {
00709 chgroup->decorrelation_matrix[0] = 1.0;
00710 chgroup->decorrelation_matrix[1] = -1.0;
00711 chgroup->decorrelation_matrix[2] = 1.0;
00712 chgroup->decorrelation_matrix[3] = 1.0;
00713 } else {
00715 chgroup->decorrelation_matrix[0] = 0.70703125;
00716 chgroup->decorrelation_matrix[1] = -0.70703125;
00717 chgroup->decorrelation_matrix[2] = 0.70703125;
00718 chgroup->decorrelation_matrix[3] = 0.70703125;
00719 }
00720 }
00721 } else if (chgroup->num_channels > 2) {
00722 if (get_bits1(&s->gb)) {
00723 chgroup->transform = 1;
00724 if (get_bits1(&s->gb)) {
00725 decode_decorrelation_matrix(s, chgroup);
00726 } else {
00728 if (chgroup->num_channels > 6) {
00729 av_log_ask_for_sample(s->avctx,
00730 "coupled channels > 6\n");
00731 } else {
00732 memcpy(chgroup->decorrelation_matrix,
00733 default_decorrelation[chgroup->num_channels],
00734 chgroup->num_channels * chgroup->num_channels *
00735 sizeof(*chgroup->decorrelation_matrix));
00736 }
00737 }
00738 }
00739 }
00740
00742 if (chgroup->transform) {
00743 if (!get_bits1(&s->gb)) {
00744 int i;
00746 for (i = 0; i < s->num_bands; i++) {
00747 chgroup->transform_band[i] = get_bits1(&s->gb);
00748 }
00749 } else {
00750 memset(chgroup->transform_band, 1, s->num_bands);
00751 }
00752 }
00753 remaining_channels -= chgroup->num_channels;
00754 }
00755 }
00756 return 0;
00757 }
00758
00765 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00766 {
00767
00768
00769
00770 static const int fval_tab[16] = {
00771 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00772 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00773 0x41000000, 0x41100000, 0x41200000, 0x41300000,
00774 0x41400000, 0x41500000, 0x41600000, 0x41700000,
00775 };
00776 int vlctable;
00777 VLC* vlc;
00778 WMAProChannelCtx* ci = &s->channel[c];
00779 int rl_mode = 0;
00780 int cur_coeff = 0;
00781 int num_zeros = 0;
00782 const uint16_t* run;
00783 const float* level;
00784
00785 av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00786
00787 vlctable = get_bits1(&s->gb);
00788 vlc = &coef_vlc[vlctable];
00789
00790 if (vlctable) {
00791 run = coef1_run;
00792 level = coef1_level;
00793 } else {
00794 run = coef0_run;
00795 level = coef0_level;
00796 }
00797
00800 while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00801 (cur_coeff + 3 < ci->num_vec_coeffs)) {
00802 int vals[4];
00803 int i;
00804 unsigned int idx;
00805
00806 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00807
00808 if (idx == HUFF_VEC4_SIZE - 1) {
00809 for (i = 0; i < 4; i += 2) {
00810 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00811 if (idx == HUFF_VEC2_SIZE - 1) {
00812 int v0, v1;
00813 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00814 if (v0 == HUFF_VEC1_SIZE - 1)
00815 v0 += ff_wma_get_large_val(&s->gb);
00816 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00817 if (v1 == HUFF_VEC1_SIZE - 1)
00818 v1 += ff_wma_get_large_val(&s->gb);
00819 ((float*)vals)[i ] = v0;
00820 ((float*)vals)[i+1] = v1;
00821 } else {
00822 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
00823 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00824 }
00825 }
00826 } else {
00827 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
00828 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00829 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00830 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
00831 }
00832
00834 for (i = 0; i < 4; i++) {
00835 if (vals[i]) {
00836 int sign = get_bits1(&s->gb) - 1;
00837 *(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31;
00838 num_zeros = 0;
00839 } else {
00840 ci->coeffs[cur_coeff] = 0;
00843 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00844 }
00845 ++cur_coeff;
00846 }
00847 }
00848
00850 if (cur_coeff < s->subframe_len) {
00851 memset(&ci->coeffs[cur_coeff], 0,
00852 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00853 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00854 level, run, 1, ci->coeffs,
00855 cur_coeff, s->subframe_len,
00856 s->subframe_len, s->esc_len, 0))
00857 return AVERROR_INVALIDDATA;
00858 }
00859
00860 return 0;
00861 }
00862
00868 static int decode_scale_factors(WMAProDecodeCtx* s)
00869 {
00870 int i;
00871
00876 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00877 int c = s->channel_indexes_for_cur_subframe[i];
00878 int* sf;
00879 int* sf_end;
00880 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00881 sf_end = s->channel[c].scale_factors + s->num_bands;
00882
00888 if (s->channel[c].reuse_sf) {
00889 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00890 int b;
00891 for (b = 0; b < s->num_bands; b++)
00892 s->channel[c].scale_factors[b] =
00893 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00894 }
00895
00896 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00897
00898 if (!s->channel[c].reuse_sf) {
00899 int val;
00901 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00902 val = 45 / s->channel[c].scale_factor_step;
00903 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00904 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00905 *sf = val;
00906 }
00907 } else {
00908 int i;
00910 for (i = 0; i < s->num_bands; i++) {
00911 int idx;
00912 int skip;
00913 int val;
00914 int sign;
00915
00916 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00917
00918 if (!idx) {
00919 uint32_t code = get_bits(&s->gb, 14);
00920 val = code >> 6;
00921 sign = (code & 1) - 1;
00922 skip = (code & 0x3f) >> 1;
00923 } else if (idx == 1) {
00924 break;
00925 } else {
00926 skip = scale_rl_run[idx];
00927 val = scale_rl_level[idx];
00928 sign = get_bits1(&s->gb)-1;
00929 }
00930
00931 i += skip;
00932 if (i >= s->num_bands) {
00933 av_log(s->avctx, AV_LOG_ERROR,
00934 "invalid scale factor coding\n");
00935 return AVERROR_INVALIDDATA;
00936 }
00937 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00938 }
00939 }
00941 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00942 s->channel[c].table_idx = s->table_idx;
00943 s->channel[c].reuse_sf = 1;
00944 }
00945
00947 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00948 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00949 s->channel[c].max_scale_factor =
00950 FFMAX(s->channel[c].max_scale_factor, *sf);
00951 }
00952
00953 }
00954 return 0;
00955 }
00956
00961 static void inverse_channel_transform(WMAProDecodeCtx *s)
00962 {
00963 int i;
00964
00965 for (i = 0; i < s->num_chgroups; i++) {
00966 if (s->chgroup[i].transform) {
00967 float data[WMAPRO_MAX_CHANNELS];
00968 const int num_channels = s->chgroup[i].num_channels;
00969 float** ch_data = s->chgroup[i].channel_data;
00970 float** ch_end = ch_data + num_channels;
00971 const int8_t* tb = s->chgroup[i].transform_band;
00972 int16_t* sfb;
00973
00975 for (sfb = s->cur_sfb_offsets;
00976 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00977 int y;
00978 if (*tb++ == 1) {
00980 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00981 const float* mat = s->chgroup[i].decorrelation_matrix;
00982 const float* data_end = data + num_channels;
00983 float* data_ptr = data;
00984 float** ch;
00985
00986 for (ch = ch_data; ch < ch_end; ch++)
00987 *data_ptr++ = (*ch)[y];
00988
00989 for (ch = ch_data; ch < ch_end; ch++) {
00990 float sum = 0;
00991 data_ptr = data;
00992 while (data_ptr < data_end)
00993 sum += *data_ptr++ * *mat++;
00994
00995 (*ch)[y] = sum;
00996 }
00997 }
00998 } else if (s->num_channels == 2) {
00999 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01000 s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01001 ch_data[0] + sfb[0],
01002 181.0 / 128, len);
01003 s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01004 ch_data[1] + sfb[0],
01005 181.0 / 128, len);
01006 }
01007 }
01008 }
01009 }
01010 }
01011
01016 static void wmapro_window(WMAProDecodeCtx *s)
01017 {
01018 int i;
01019 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01020 int c = s->channel_indexes_for_cur_subframe[i];
01021 float* window;
01022 int winlen = s->channel[c].prev_block_len;
01023 float* start = s->channel[c].coeffs - (winlen >> 1);
01024
01025 if (s->subframe_len < winlen) {
01026 start += (winlen - s->subframe_len) >> 1;
01027 winlen = s->subframe_len;
01028 }
01029
01030 window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01031
01032 winlen >>= 1;
01033
01034 s->dsp.vector_fmul_window(start, start, start + winlen,
01035 window, winlen);
01036
01037 s->channel[c].prev_block_len = s->subframe_len;
01038 }
01039 }
01040
01046 static int decode_subframe(WMAProDecodeCtx *s)
01047 {
01048 int offset = s->samples_per_frame;
01049 int subframe_len = s->samples_per_frame;
01050 int i;
01051 int total_samples = s->samples_per_frame * s->num_channels;
01052 int transmit_coeffs = 0;
01053 int cur_subwoofer_cutoff;
01054
01055 s->subframe_offset = get_bits_count(&s->gb);
01056
01061 for (i = 0; i < s->num_channels; i++) {
01062 s->channel[i].grouped = 0;
01063 if (offset > s->channel[i].decoded_samples) {
01064 offset = s->channel[i].decoded_samples;
01065 subframe_len =
01066 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01067 }
01068 }
01069
01070 av_dlog(s->avctx,
01071 "processing subframe with offset %i len %i\n", offset, subframe_len);
01072
01074 s->channels_for_cur_subframe = 0;
01075 for (i = 0; i < s->num_channels; i++) {
01076 const int cur_subframe = s->channel[i].cur_subframe;
01078 total_samples -= s->channel[i].decoded_samples;
01079
01081 if (offset == s->channel[i].decoded_samples &&
01082 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01083 total_samples -= s->channel[i].subframe_len[cur_subframe];
01084 s->channel[i].decoded_samples +=
01085 s->channel[i].subframe_len[cur_subframe];
01086 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01087 ++s->channels_for_cur_subframe;
01088 }
01089 }
01090
01093 if (!total_samples)
01094 s->parsed_all_subframes = 1;
01095
01096
01097 av_dlog(s->avctx, "subframe is part of %i channels\n",
01098 s->channels_for_cur_subframe);
01099
01101 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
01102 s->num_bands = s->num_sfb[s->table_idx];
01103 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
01104 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01105
01107 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01108 int c = s->channel_indexes_for_cur_subframe[i];
01109
01110 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01111 + offset];
01112 }
01113
01114 s->subframe_len = subframe_len;
01115 s->esc_len = av_log2(s->subframe_len - 1) + 1;
01116
01118 if (get_bits1(&s->gb)) {
01119 int num_fill_bits;
01120 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01121 int len = get_bits(&s->gb, 4);
01122 num_fill_bits = get_bits(&s->gb, len) + 1;
01123 }
01124
01125 if (num_fill_bits >= 0) {
01126 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01127 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01128 return AVERROR_INVALIDDATA;
01129 }
01130
01131 skip_bits_long(&s->gb, num_fill_bits);
01132 }
01133 }
01134
01136 if (get_bits1(&s->gb)) {
01137 av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01138 return AVERROR_INVALIDDATA;
01139 }
01140
01141
01142 if (decode_channel_transform(s) < 0)
01143 return AVERROR_INVALIDDATA;
01144
01145
01146 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01147 int c = s->channel_indexes_for_cur_subframe[i];
01148 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01149 transmit_coeffs = 1;
01150 }
01151
01152 if (transmit_coeffs) {
01153 int step;
01154 int quant_step = 90 * s->bits_per_sample >> 4;
01155
01157 if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01158 int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01159 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01160 int c = s->channel_indexes_for_cur_subframe[i];
01161 s->channel[c].num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01162 }
01163 } else {
01164 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01165 int c = s->channel_indexes_for_cur_subframe[i];
01166 s->channel[c].num_vec_coeffs = s->subframe_len;
01167 }
01168 }
01170 step = get_sbits(&s->gb, 6);
01171 quant_step += step;
01172 if (step == -32 || step == 31) {
01173 const int sign = (step == 31) - 1;
01174 int quant = 0;
01175 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01176 (step = get_bits(&s->gb, 5)) == 31) {
01177 quant += 31;
01178 }
01179 quant_step += ((quant + step) ^ sign) - sign;
01180 }
01181 if (quant_step < 0) {
01182 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01183 }
01184
01187 if (s->channels_for_cur_subframe == 1) {
01188 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01189 } else {
01190 int modifier_len = get_bits(&s->gb, 3);
01191 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01192 int c = s->channel_indexes_for_cur_subframe[i];
01193 s->channel[c].quant_step = quant_step;
01194 if (get_bits1(&s->gb)) {
01195 if (modifier_len) {
01196 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01197 } else
01198 ++s->channel[c].quant_step;
01199 }
01200 }
01201 }
01202
01204 if (decode_scale_factors(s) < 0)
01205 return AVERROR_INVALIDDATA;
01206 }
01207
01208 av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01209 get_bits_count(&s->gb) - s->subframe_offset);
01210
01212 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01213 int c = s->channel_indexes_for_cur_subframe[i];
01214 if (s->channel[c].transmit_coefs &&
01215 get_bits_count(&s->gb) < s->num_saved_bits) {
01216 decode_coeffs(s, c);
01217 } else
01218 memset(s->channel[c].coeffs, 0,
01219 sizeof(*s->channel[c].coeffs) * subframe_len);
01220 }
01221
01222 av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01223 get_bits_count(&s->gb) - s->subframe_offset);
01224
01225 if (transmit_coeffs) {
01226 FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01228 inverse_channel_transform(s);
01229 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01230 int c = s->channel_indexes_for_cur_subframe[i];
01231 const int* sf = s->channel[c].scale_factors;
01232 int b;
01233
01234 if (c == s->lfe_channel)
01235 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01236 (subframe_len - cur_subwoofer_cutoff));
01237
01239 for (b = 0; b < s->num_bands; b++) {
01240 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01241 const int exp = s->channel[c].quant_step -
01242 (s->channel[c].max_scale_factor - *sf++) *
01243 s->channel[c].scale_factor_step;
01244 const float quant = pow(10.0, exp / 20.0);
01245 int start = s->cur_sfb_offsets[b];
01246 s->dsp.vector_fmul_scalar(s->tmp + start,
01247 s->channel[c].coeffs + start,
01248 quant, end - start);
01249 }
01250
01252 mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01253 }
01254 }
01255
01257 wmapro_window(s);
01258
01260 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01261 int c = s->channel_indexes_for_cur_subframe[i];
01262 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01263 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01264 return AVERROR_INVALIDDATA;
01265 }
01266 ++s->channel[c].cur_subframe;
01267 }
01268
01269 return 0;
01270 }
01271
01278 static int decode_frame(WMAProDecodeCtx *s)
01279 {
01280 GetBitContext* gb = &s->gb;
01281 int more_frames = 0;
01282 int len = 0;
01283 int i;
01284
01286 if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
01288 av_log(s->avctx, AV_LOG_ERROR,
01289 "not enough space for the output samples\n");
01290 s->packet_loss = 1;
01291 return 0;
01292 }
01293
01295 if (s->len_prefix)
01296 len = get_bits(gb, s->log2_frame_size);
01297
01298 av_dlog(s->avctx, "decoding frame with length %x\n", len);
01299
01301 if (decode_tilehdr(s)) {
01302 s->packet_loss = 1;
01303 return 0;
01304 }
01305
01307 if (s->num_channels > 1 && get_bits1(gb)) {
01308 if (get_bits1(gb)) {
01309 for (i = 0; i < s->num_channels * s->num_channels; i++)
01310 skip_bits(gb, 4);
01311 }
01312 }
01313
01315 if (s->dynamic_range_compression) {
01316 s->drc_gain = get_bits(gb, 8);
01317 av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01318 }
01319
01322 if (get_bits1(gb)) {
01323 int av_unused skip;
01324
01326 if (get_bits1(gb)) {
01327 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01328 av_dlog(s->avctx, "start skip: %i\n", skip);
01329 }
01330
01332 if (get_bits1(gb)) {
01333 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01334 av_dlog(s->avctx, "end skip: %i\n", skip);
01335 }
01336
01337 }
01338
01339 av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01340 get_bits_count(gb) - s->frame_offset);
01341
01343 s->parsed_all_subframes = 0;
01344 for (i = 0; i < s->num_channels; i++) {
01345 s->channel[i].decoded_samples = 0;
01346 s->channel[i].cur_subframe = 0;
01347 s->channel[i].reuse_sf = 0;
01348 }
01349
01351 while (!s->parsed_all_subframes) {
01352 if (decode_subframe(s) < 0) {
01353 s->packet_loss = 1;
01354 return 0;
01355 }
01356 }
01357
01359 for (i = 0; i < s->num_channels; i++) {
01360 float* ptr = s->samples + i;
01361 int incr = s->num_channels;
01362 float* iptr = s->channel[i].out;
01363 float* iend = iptr + s->samples_per_frame;
01364
01365
01366 while (iptr < iend) {
01367 *ptr = *iptr++;
01368 ptr += incr;
01369 }
01370
01372 memcpy(&s->channel[i].out[0],
01373 &s->channel[i].out[s->samples_per_frame],
01374 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01375 }
01376
01377 if (s->skip_frame) {
01378 s->skip_frame = 0;
01379 } else
01380 s->samples += s->num_channels * s->samples_per_frame;
01381
01382 if (s->len_prefix) {
01383 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01385 av_log(s->avctx, AV_LOG_ERROR,
01386 "frame[%i] would have to skip %i bits\n", s->frame_num,
01387 len - (get_bits_count(gb) - s->frame_offset) - 1);
01388 s->packet_loss = 1;
01389 return 0;
01390 }
01391
01393 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01394 } else {
01395 while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01396 }
01397 }
01398
01400 more_frames = get_bits1(gb);
01401
01402 ++s->frame_num;
01403 return more_frames;
01404 }
01405
01412 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01413 {
01414 return s->buf_bit_size - get_bits_count(gb);
01415 }
01416
01424 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01425 int append)
01426 {
01427 int buflen;
01428
01433 if (!append) {
01434 s->frame_offset = get_bits_count(gb) & 7;
01435 s->num_saved_bits = s->frame_offset;
01436 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01437 }
01438
01439 buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
01440
01441 if (len <= 0 || buflen > MAX_FRAMESIZE) {
01442 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01443 s->packet_loss = 1;
01444 return;
01445 }
01446
01447 s->num_saved_bits += len;
01448 if (!append) {
01449 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01450 s->num_saved_bits);
01451 } else {
01452 int align = 8 - (get_bits_count(gb) & 7);
01453 align = FFMIN(align, len);
01454 put_bits(&s->pb, align, get_bits(gb, align));
01455 len -= align;
01456 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01457 }
01458 skip_bits_long(gb, len);
01459
01460 {
01461 PutBitContext tmp = s->pb;
01462 flush_put_bits(&tmp);
01463 }
01464
01465 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01466 skip_bits(&s->gb, s->frame_offset);
01467 }
01468
01477 static int decode_packet(AVCodecContext *avctx,
01478 void *data, int *data_size, AVPacket* avpkt)
01479 {
01480 WMAProDecodeCtx *s = avctx->priv_data;
01481 GetBitContext* gb = &s->pgb;
01482 const uint8_t* buf = avpkt->data;
01483 int buf_size = avpkt->size;
01484 int num_bits_prev_frame;
01485 int packet_sequence_number;
01486
01487 s->samples = data;
01488 s->samples_end = (float*)((int8_t*)data + *data_size);
01489 *data_size = 0;
01490
01491 if (s->packet_done || s->packet_loss) {
01492 s->packet_done = 0;
01493
01495 if (buf_size < avctx->block_align)
01496 return 0;
01497
01498 s->next_packet_start = buf_size - avctx->block_align;
01499 buf_size = avctx->block_align;
01500 s->buf_bit_size = buf_size << 3;
01501
01503 init_get_bits(gb, buf, s->buf_bit_size);
01504 packet_sequence_number = get_bits(gb, 4);
01505 skip_bits(gb, 2);
01506
01508 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01509 av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01510 num_bits_prev_frame);
01511
01513 if (!s->packet_loss &&
01514 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01515 s->packet_loss = 1;
01516 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01517 s->packet_sequence_number, packet_sequence_number);
01518 }
01519 s->packet_sequence_number = packet_sequence_number;
01520
01521 if (num_bits_prev_frame > 0) {
01522 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01523 if (num_bits_prev_frame >= remaining_packet_bits) {
01524 num_bits_prev_frame = remaining_packet_bits;
01525 s->packet_done = 1;
01526 }
01527
01530 save_bits(s, gb, num_bits_prev_frame, 1);
01531 av_dlog(avctx, "accumulated %x bits of frame data\n",
01532 s->num_saved_bits - s->frame_offset);
01533
01535 if (!s->packet_loss)
01536 decode_frame(s);
01537 } else if (s->num_saved_bits - s->frame_offset) {
01538 av_dlog(avctx, "ignoring %x previously saved bits\n",
01539 s->num_saved_bits - s->frame_offset);
01540 }
01541
01542 if (s->packet_loss) {
01546 s->num_saved_bits = 0;
01547 s->packet_loss = 0;
01548 }
01549
01550 } else {
01551 int frame_size;
01552 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01553 init_get_bits(gb, avpkt->data, s->buf_bit_size);
01554 skip_bits(gb, s->packet_offset);
01555 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01556 (frame_size = show_bits(gb, s->log2_frame_size)) &&
01557 frame_size <= remaining_bits(s, gb)) {
01558 save_bits(s, gb, frame_size, 0);
01559 s->packet_done = !decode_frame(s);
01560 } else if (!s->len_prefix
01561 && s->num_saved_bits > get_bits_count(&s->gb)) {
01569 s->packet_done = !decode_frame(s);
01570 } else
01571 s->packet_done = 1;
01572 }
01573
01574 if (s->packet_done && !s->packet_loss &&
01575 remaining_bits(s, gb) > 0) {
01578 save_bits(s, gb, remaining_bits(s, gb), 0);
01579 }
01580
01581 *data_size = (int8_t *)s->samples - (int8_t *)data;
01582 s->packet_offset = get_bits_count(gb) & 7;
01583
01584 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
01585 }
01586
01591 static void flush(AVCodecContext *avctx)
01592 {
01593 WMAProDecodeCtx *s = avctx->priv_data;
01594 int i;
01597 for (i = 0; i < s->num_channels; i++)
01598 memset(s->channel[i].out, 0, s->samples_per_frame *
01599 sizeof(*s->channel[i].out));
01600 s->packet_loss = 1;
01601 }
01602
01603
01607 AVCodec ff_wmapro_decoder = {
01608 "wmapro",
01609 AVMEDIA_TYPE_AUDIO,
01610 CODEC_ID_WMAPRO,
01611 sizeof(WMAProDecodeCtx),
01612 decode_init,
01613 NULL,
01614 decode_end,
01615 decode_packet,
01616 .capabilities = CODEC_CAP_SUBFRAMES,
01617 .flush= flush,
01618 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01619 };