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 "wma.h"
00096
00098 #define WMAPRO_MAX_CHANNELS 8
00099 #define MAX_SUBFRAMES 32
00100 #define MAX_BANDS 29
00101 #define MAX_FRAMESIZE 32768
00102
00103 #define WMAPRO_BLOCK_MAX_BITS 12
00104 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
00105 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
00106
00107
00108 #define VLCBITS 9
00109 #define SCALEVLCBITS 8
00110 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00111 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00112 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00113 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00114 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00115
00116 static VLC sf_vlc;
00117 static VLC sf_rl_vlc;
00118 static VLC vec4_vlc;
00119 static VLC vec2_vlc;
00120 static VLC vec1_vlc;
00121 static VLC coef_vlc[2];
00122 static float sin64[33];
00123
00127 typedef struct {
00128 int16_t prev_block_len;
00129 uint8_t transmit_coefs;
00130 uint8_t num_subframes;
00131 uint16_t subframe_len[MAX_SUBFRAMES];
00132 uint16_t subframe_offset[MAX_SUBFRAMES];
00133 uint8_t cur_subframe;
00134 uint16_t decoded_samples;
00135 uint8_t grouped;
00136 int quant_step;
00137 int8_t reuse_sf;
00138 int8_t scale_factor_step;
00139 int max_scale_factor;
00140 int saved_scale_factors[2][MAX_BANDS];
00141 int8_t scale_factor_idx;
00142 int* scale_factors;
00143 uint8_t table_idx;
00144 float* coeffs;
00145 DECLARE_ALIGNED(16, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2];
00146 } WMAProChannelCtx;
00147
00151 typedef struct {
00152 uint8_t num_channels;
00153 int8_t transform;
00154 int8_t transform_band[MAX_BANDS];
00155 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00156 float* channel_data[WMAPRO_MAX_CHANNELS];
00157 } WMAProChannelGrp;
00158
00162 typedef struct WMAProDecodeCtx {
00163
00164 AVCodecContext* avctx;
00165 DSPContext dsp;
00166 uint8_t frame_data[MAX_FRAMESIZE +
00167 FF_INPUT_BUFFER_PADDING_SIZE];
00168 PutBitContext pb;
00169 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES];
00170 DECLARE_ALIGNED(16, float, tmp)[WMAPRO_BLOCK_MAX_SIZE];
00171 float* windows[WMAPRO_BLOCK_SIZES];
00172
00173
00174 uint32_t decode_flags;
00175 uint8_t len_prefix;
00176 uint8_t dynamic_range_compression;
00177 uint8_t bits_per_sample;
00178 uint16_t samples_per_frame;
00179 uint16_t log2_frame_size;
00180 int8_t num_channels;
00181 int8_t lfe_channel;
00182 uint8_t max_num_subframes;
00183 uint8_t subframe_len_bits;
00184 uint8_t max_subframe_len_bit;
00185 uint16_t min_samples_per_subframe;
00186 int8_t num_sfb[WMAPRO_BLOCK_SIZES];
00187 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];
00188 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS];
00189 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES];
00190
00191
00192 GetBitContext pgb;
00193 uint8_t packet_offset;
00194 uint8_t packet_sequence_number;
00195 int num_saved_bits;
00196 int frame_offset;
00197 int subframe_offset;
00198 uint8_t packet_loss;
00199 uint8_t packet_done;
00200
00201
00202 uint32_t frame_num;
00203 GetBitContext gb;
00204 int buf_bit_size;
00205 float* samples;
00206 float* samples_end;
00207 uint8_t drc_gain;
00208 int8_t skip_frame;
00209 int8_t parsed_all_subframes;
00210
00211
00212 int16_t subframe_len;
00213 int8_t channels_for_cur_subframe;
00214 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00215 int8_t num_bands;
00216 int16_t* cur_sfb_offsets;
00217 uint8_t table_idx;
00218 int8_t esc_len;
00219
00220 uint8_t num_chgroups;
00221 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];
00222
00223 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];
00224 } WMAProDecodeCtx;
00225
00226
00231 static void av_cold dump_context(WMAProDecodeCtx *s)
00232 {
00233 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00234 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00235
00236 PRINT("ed sample bit depth", s->bits_per_sample);
00237 PRINT_HEX("ed decode flags", s->decode_flags);
00238 PRINT("samples per frame", s->samples_per_frame);
00239 PRINT("log2 frame size", s->log2_frame_size);
00240 PRINT("max num subframes", s->max_num_subframes);
00241 PRINT("len prefix", s->len_prefix);
00242 PRINT("num channels", s->num_channels);
00243 }
00244
00250 static av_cold int decode_end(AVCodecContext *avctx)
00251 {
00252 WMAProDecodeCtx *s = avctx->priv_data;
00253 int i;
00254
00255 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00256 ff_mdct_end(&s->mdct_ctx[i]);
00257
00258 return 0;
00259 }
00260
00266 static av_cold int decode_init(AVCodecContext *avctx)
00267 {
00268 WMAProDecodeCtx *s = avctx->priv_data;
00269 uint8_t *edata_ptr = avctx->extradata;
00270 unsigned int channel_mask;
00271 int i;
00272 int log2_max_num_subframes;
00273 int num_possible_block_sizes;
00274
00275 s->avctx = avctx;
00276 dsputil_init(&s->dsp, avctx);
00277 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00278
00279 avctx->sample_fmt = SAMPLE_FMT_FLT;
00280
00281 if (avctx->extradata_size >= 18) {
00282 s->decode_flags = AV_RL16(edata_ptr+14);
00283 channel_mask = AV_RL32(edata_ptr+2);
00284 s->bits_per_sample = AV_RL16(edata_ptr);
00286 for (i = 0; i < avctx->extradata_size; i++)
00287 dprintf(avctx, "[%x] ", avctx->extradata[i]);
00288 dprintf(avctx, "\n");
00289
00290 } else {
00291 av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00292 return AVERROR_INVALIDDATA;
00293 }
00294
00296 s->log2_frame_size = av_log2(avctx->block_align) + 4;
00297
00299 s->skip_frame = 1;
00300 s->packet_loss = 1;
00301 s->len_prefix = (s->decode_flags & 0x40);
00302
00303 if (!s->len_prefix) {
00304 av_log_ask_for_sample(avctx, "no length prefix\n");
00305 return AVERROR_INVALIDDATA;
00306 }
00307
00309 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00310 3, s->decode_flags);
00311
00313 for (i = 0; i < avctx->channels; i++)
00314 s->channel[i].prev_block_len = s->samples_per_frame;
00315
00317 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
00318 s->max_num_subframes = 1 << log2_max_num_subframes;
00319 if (s->max_num_subframes == 16)
00320 s->max_subframe_len_bit = 1;
00321 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00322
00323 num_possible_block_sizes = log2_max_num_subframes + 1;
00324 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
00325 s->dynamic_range_compression = (s->decode_flags & 0x80);
00326
00327 if (s->max_num_subframes > MAX_SUBFRAMES) {
00328 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00329 s->max_num_subframes);
00330 return AVERROR_INVALIDDATA;
00331 }
00332
00333 s->num_channels = avctx->channels;
00334
00336 s->lfe_channel = -1;
00337
00338 if (channel_mask & 8) {
00339 unsigned int mask;
00340 for (mask = 1; mask < 16; mask <<= 1) {
00341 if (channel_mask & mask)
00342 ++s->lfe_channel;
00343 }
00344 }
00345
00346 if (s->num_channels < 0) {
00347 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00348 return AVERROR_INVALIDDATA;
00349 } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00350 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00351 return AVERROR_PATCHWELCOME;
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], BLOCK_MIN_BITS+1+i, 1,
00426 1.0 / (1 << (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 dprintf(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 dprintf(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 (!rl_mode && cur_coeff + 3 < s->subframe_len) {
00801 int vals[4];
00802 int i;
00803 unsigned int idx;
00804
00805 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00806
00807 if (idx == HUFF_VEC4_SIZE - 1) {
00808 for (i = 0; i < 4; i += 2) {
00809 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00810 if (idx == HUFF_VEC2_SIZE - 1) {
00811 int v0, v1;
00812 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00813 if (v0 == HUFF_VEC1_SIZE - 1)
00814 v0 += ff_wma_get_large_val(&s->gb);
00815 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00816 if (v1 == HUFF_VEC1_SIZE - 1)
00817 v1 += ff_wma_get_large_val(&s->gb);
00818 ((float*)vals)[i ] = v0;
00819 ((float*)vals)[i+1] = v1;
00820 } else {
00821 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
00822 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00823 }
00824 }
00825 } else {
00826 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
00827 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00828 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00829 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
00830 }
00831
00833 for (i = 0; i < 4; i++) {
00834 if (vals[i]) {
00835 int sign = get_bits1(&s->gb) - 1;
00836 *(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31;
00837 num_zeros = 0;
00838 } else {
00839 ci->coeffs[cur_coeff] = 0;
00842 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00843 }
00844 ++cur_coeff;
00845 }
00846 }
00847
00849 if (rl_mode) {
00850 memset(&ci->coeffs[cur_coeff], 0,
00851 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00852 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00853 level, run, 1, ci->coeffs,
00854 cur_coeff, s->subframe_len,
00855 s->subframe_len, s->esc_len, 0))
00856 return AVERROR_INVALIDDATA;
00857 }
00858
00859 return 0;
00860 }
00861
00867 static int decode_scale_factors(WMAProDecodeCtx* s)
00868 {
00869 int i;
00870
00875 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00876 int c = s->channel_indexes_for_cur_subframe[i];
00877 int* sf;
00878 int* sf_end;
00879 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00880 sf_end = s->channel[c].scale_factors + s->num_bands;
00881
00887 if (s->channel[c].reuse_sf) {
00888 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00889 int b;
00890 for (b = 0; b < s->num_bands; b++)
00891 s->channel[c].scale_factors[b] =
00892 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00893 }
00894
00895 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00896
00897 if (!s->channel[c].reuse_sf) {
00898 int val;
00900 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00901 val = 45 / s->channel[c].scale_factor_step;
00902 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00903 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00904 *sf = val;
00905 }
00906 } else {
00907 int i;
00909 for (i = 0; i < s->num_bands; i++) {
00910 int idx;
00911 int skip;
00912 int val;
00913 int sign;
00914
00915 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00916
00917 if (!idx) {
00918 uint32_t code = get_bits(&s->gb, 14);
00919 val = code >> 6;
00920 sign = (code & 1) - 1;
00921 skip = (code & 0x3f) >> 1;
00922 } else if (idx == 1) {
00923 break;
00924 } else {
00925 skip = scale_rl_run[idx];
00926 val = scale_rl_level[idx];
00927 sign = get_bits1(&s->gb)-1;
00928 }
00929
00930 i += skip;
00931 if (i >= s->num_bands) {
00932 av_log(s->avctx, AV_LOG_ERROR,
00933 "invalid scale factor coding\n");
00934 return AVERROR_INVALIDDATA;
00935 }
00936 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00937 }
00938 }
00940 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00941 s->channel[c].table_idx = s->table_idx;
00942 s->channel[c].reuse_sf = 1;
00943 }
00944
00946 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00947 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00948 s->channel[c].max_scale_factor =
00949 FFMAX(s->channel[c].max_scale_factor, *sf);
00950 }
00951
00952 }
00953 return 0;
00954 }
00955
00960 static void inverse_channel_transform(WMAProDecodeCtx *s)
00961 {
00962 int i;
00963
00964 for (i = 0; i < s->num_chgroups; i++) {
00965 if (s->chgroup[i].transform) {
00966 float data[WMAPRO_MAX_CHANNELS];
00967 const int num_channels = s->chgroup[i].num_channels;
00968 float** ch_data = s->chgroup[i].channel_data;
00969 float** ch_end = ch_data + num_channels;
00970 const int8_t* tb = s->chgroup[i].transform_band;
00971 int16_t* sfb;
00972
00974 for (sfb = s->cur_sfb_offsets;
00975 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00976 int y;
00977 if (*tb++ == 1) {
00979 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00980 const float* mat = s->chgroup[i].decorrelation_matrix;
00981 const float* data_end = data + num_channels;
00982 float* data_ptr = data;
00983 float** ch;
00984
00985 for (ch = ch_data; ch < ch_end; ch++)
00986 *data_ptr++ = (*ch)[y];
00987
00988 for (ch = ch_data; ch < ch_end; ch++) {
00989 float sum = 0;
00990 data_ptr = data;
00991 while (data_ptr < data_end)
00992 sum += *data_ptr++ * *mat++;
00993
00994 (*ch)[y] = sum;
00995 }
00996 }
00997 } else if (s->num_channels == 2) {
00998 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
00999 s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01000 ch_data[0] + sfb[0],
01001 181.0 / 128, len);
01002 s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01003 ch_data[1] + sfb[0],
01004 181.0 / 128, len);
01005 }
01006 }
01007 }
01008 }
01009 }
01010
01015 static void wmapro_window(WMAProDecodeCtx *s)
01016 {
01017 int i;
01018 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01019 int c = s->channel_indexes_for_cur_subframe[i];
01020 float* window;
01021 int winlen = s->channel[c].prev_block_len;
01022 float* start = s->channel[c].coeffs - (winlen >> 1);
01023
01024 if (s->subframe_len < winlen) {
01025 start += (winlen - s->subframe_len) >> 1;
01026 winlen = s->subframe_len;
01027 }
01028
01029 window = s->windows[av_log2(winlen) - BLOCK_MIN_BITS];
01030
01031 winlen >>= 1;
01032
01033 s->dsp.vector_fmul_window(start, start, start + winlen,
01034 window, 0, winlen);
01035
01036 s->channel[c].prev_block_len = s->subframe_len;
01037 }
01038 }
01039
01045 static int decode_subframe(WMAProDecodeCtx *s)
01046 {
01047 int offset = s->samples_per_frame;
01048 int subframe_len = s->samples_per_frame;
01049 int i;
01050 int total_samples = s->samples_per_frame * s->num_channels;
01051 int transmit_coeffs = 0;
01052 int cur_subwoofer_cutoff;
01053
01054 s->subframe_offset = get_bits_count(&s->gb);
01055
01060 for (i = 0; i < s->num_channels; i++) {
01061 s->channel[i].grouped = 0;
01062 if (offset > s->channel[i].decoded_samples) {
01063 offset = s->channel[i].decoded_samples;
01064 subframe_len =
01065 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01066 }
01067 }
01068
01069 dprintf(s->avctx,
01070 "processing subframe with offset %i len %i\n", offset, subframe_len);
01071
01073 s->channels_for_cur_subframe = 0;
01074 for (i = 0; i < s->num_channels; i++) {
01075 const int cur_subframe = s->channel[i].cur_subframe;
01077 total_samples -= s->channel[i].decoded_samples;
01078
01080 if (offset == s->channel[i].decoded_samples &&
01081 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01082 total_samples -= s->channel[i].subframe_len[cur_subframe];
01083 s->channel[i].decoded_samples +=
01084 s->channel[i].subframe_len[cur_subframe];
01085 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01086 ++s->channels_for_cur_subframe;
01087 }
01088 }
01089
01092 if (!total_samples)
01093 s->parsed_all_subframes = 1;
01094
01095
01096 dprintf(s->avctx, "subframe is part of %i channels\n",
01097 s->channels_for_cur_subframe);
01098
01100 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
01101 s->num_bands = s->num_sfb[s->table_idx];
01102 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
01103 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01104
01106 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01107 int c = s->channel_indexes_for_cur_subframe[i];
01108
01109 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01110 + offset];
01111 }
01112
01113 s->subframe_len = subframe_len;
01114 s->esc_len = av_log2(s->subframe_len - 1) + 1;
01115
01117 if (get_bits1(&s->gb)) {
01118 int num_fill_bits;
01119 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01120 int len = get_bits(&s->gb, 4);
01121 num_fill_bits = get_bits(&s->gb, len) + 1;
01122 }
01123
01124 if (num_fill_bits >= 0) {
01125 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01126 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01127 return AVERROR_INVALIDDATA;
01128 }
01129
01130 skip_bits_long(&s->gb, num_fill_bits);
01131 }
01132 }
01133
01135 if (get_bits1(&s->gb)) {
01136 av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01137 return AVERROR_INVALIDDATA;
01138 }
01139
01140
01141 if (decode_channel_transform(s) < 0)
01142 return AVERROR_INVALIDDATA;
01143
01144
01145 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01146 int c = s->channel_indexes_for_cur_subframe[i];
01147 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01148 transmit_coeffs = 1;
01149 }
01150
01151 if (transmit_coeffs) {
01152 int step;
01153 int quant_step = 90 * s->bits_per_sample >> 4;
01154 if ((get_bits1(&s->gb))) {
01156 av_log_ask_for_sample(s->avctx, "unsupported quant step coding\n");
01157 return AVERROR_INVALIDDATA;
01158 }
01160 step = get_sbits(&s->gb, 6);
01161 quant_step += step;
01162 if (step == -32 || step == 31) {
01163 const int sign = (step == 31) - 1;
01164 int quant = 0;
01165 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01166 (step = get_bits(&s->gb, 5)) == 31) {
01167 quant += 31;
01168 }
01169 quant_step += ((quant + step) ^ sign) - sign;
01170 }
01171 if (quant_step < 0) {
01172 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01173 }
01174
01177 if (s->channels_for_cur_subframe == 1) {
01178 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01179 } else {
01180 int modifier_len = get_bits(&s->gb, 3);
01181 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01182 int c = s->channel_indexes_for_cur_subframe[i];
01183 s->channel[c].quant_step = quant_step;
01184 if (get_bits1(&s->gb)) {
01185 if (modifier_len) {
01186 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01187 } else
01188 ++s->channel[c].quant_step;
01189 }
01190 }
01191 }
01192
01194 if (decode_scale_factors(s) < 0)
01195 return AVERROR_INVALIDDATA;
01196 }
01197
01198 dprintf(s->avctx, "BITSTREAM: subframe header length was %i\n",
01199 get_bits_count(&s->gb) - s->subframe_offset);
01200
01202 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01203 int c = s->channel_indexes_for_cur_subframe[i];
01204 if (s->channel[c].transmit_coefs &&
01205 get_bits_count(&s->gb) < s->num_saved_bits) {
01206 decode_coeffs(s, c);
01207 } else
01208 memset(s->channel[c].coeffs, 0,
01209 sizeof(*s->channel[c].coeffs) * subframe_len);
01210 }
01211
01212 dprintf(s->avctx, "BITSTREAM: subframe length was %i\n",
01213 get_bits_count(&s->gb) - s->subframe_offset);
01214
01215 if (transmit_coeffs) {
01217 inverse_channel_transform(s);
01218 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01219 int c = s->channel_indexes_for_cur_subframe[i];
01220 const int* sf = s->channel[c].scale_factors;
01221 int b;
01222
01223 if (c == s->lfe_channel)
01224 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01225 (subframe_len - cur_subwoofer_cutoff));
01226
01228 for (b = 0; b < s->num_bands; b++) {
01229 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01230 const int exp = s->channel[c].quant_step -
01231 (s->channel[c].max_scale_factor - *sf++) *
01232 s->channel[c].scale_factor_step;
01233 const float quant = pow(10.0, exp / 20.0);
01234 int start = s->cur_sfb_offsets[b];
01235 s->dsp.vector_fmul_scalar(s->tmp + start,
01236 s->channel[c].coeffs + start,
01237 quant, end - start);
01238 }
01239
01241 ff_imdct_half(&s->mdct_ctx[av_log2(subframe_len) - BLOCK_MIN_BITS],
01242 s->channel[c].coeffs, s->tmp);
01243 }
01244 }
01245
01247 wmapro_window(s);
01248
01250 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01251 int c = s->channel_indexes_for_cur_subframe[i];
01252 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01253 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01254 return AVERROR_INVALIDDATA;
01255 }
01256 ++s->channel[c].cur_subframe;
01257 }
01258
01259 return 0;
01260 }
01261
01268 static int decode_frame(WMAProDecodeCtx *s)
01269 {
01270 GetBitContext* gb = &s->gb;
01271 int more_frames = 0;
01272 int len = 0;
01273 int i;
01274
01276 if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
01278 av_log(s->avctx, AV_LOG_ERROR,
01279 "not enough space for the output samples\n");
01280 s->packet_loss = 1;
01281 return 0;
01282 }
01283
01285 if (s->len_prefix)
01286 len = get_bits(gb, s->log2_frame_size);
01287
01288 dprintf(s->avctx, "decoding frame with length %x\n", len);
01289
01291 if (decode_tilehdr(s)) {
01292 s->packet_loss = 1;
01293 return 0;
01294 }
01295
01297 if (s->num_channels > 1 && get_bits1(gb)) {
01298 av_log_ask_for_sample(s->avctx, "Unsupported postproc transform found\n");
01299 s->packet_loss = 1;
01300 return 0;
01301 }
01302
01304 if (s->dynamic_range_compression) {
01305 s->drc_gain = get_bits(gb, 8);
01306 dprintf(s->avctx, "drc_gain %i\n", s->drc_gain);
01307 }
01308
01311 if (get_bits1(gb)) {
01312 int skip;
01313
01315 if (get_bits1(gb)) {
01316 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01317 dprintf(s->avctx, "start skip: %i\n", skip);
01318 }
01319
01321 if (get_bits1(gb)) {
01322 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01323 dprintf(s->avctx, "end skip: %i\n", skip);
01324 }
01325
01326 }
01327
01328 dprintf(s->avctx, "BITSTREAM: frame header length was %i\n",
01329 get_bits_count(gb) - s->frame_offset);
01330
01332 s->parsed_all_subframes = 0;
01333 for (i = 0; i < s->num_channels; i++) {
01334 s->channel[i].decoded_samples = 0;
01335 s->channel[i].cur_subframe = 0;
01336 s->channel[i].reuse_sf = 0;
01337 }
01338
01340 while (!s->parsed_all_subframes) {
01341 if (decode_subframe(s) < 0) {
01342 s->packet_loss = 1;
01343 return 0;
01344 }
01345 }
01346
01348 for (i = 0; i < s->num_channels; i++) {
01349 float* ptr = s->samples + i;
01350 int incr = s->num_channels;
01351 float* iptr = s->channel[i].out;
01352 float* iend = iptr + s->samples_per_frame;
01353
01354
01355 while (iptr < iend) {
01356 *ptr = *iptr++;
01357 ptr += incr;
01358 }
01359
01361 memcpy(&s->channel[i].out[0],
01362 &s->channel[i].out[s->samples_per_frame],
01363 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01364 }
01365
01366 if (s->skip_frame) {
01367 s->skip_frame = 0;
01368 } else
01369 s->samples += s->num_channels * s->samples_per_frame;
01370
01371 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01373 av_log(s->avctx, AV_LOG_ERROR, "frame[%i] would have to skip %i bits\n",
01374 s->frame_num, len - (get_bits_count(gb) - s->frame_offset) - 1);
01375 s->packet_loss = 1;
01376 return 0;
01377 }
01378
01380 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01381
01383 more_frames = get_bits1(gb);
01384
01385 ++s->frame_num;
01386 return more_frames;
01387 }
01388
01395 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01396 {
01397 return s->buf_bit_size - get_bits_count(gb);
01398 }
01399
01407 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01408 int append)
01409 {
01410 int buflen;
01411
01416 if (!append) {
01417 s->frame_offset = get_bits_count(gb) & 7;
01418 s->num_saved_bits = s->frame_offset;
01419 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01420 }
01421
01422 buflen = (s->num_saved_bits + len + 8) >> 3;
01423
01424 if (len <= 0 || buflen > MAX_FRAMESIZE) {
01425 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01426 s->packet_loss = 1;
01427 return;
01428 }
01429
01430 s->num_saved_bits += len;
01431 if (!append) {
01432 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01433 s->num_saved_bits);
01434 } else {
01435 int align = 8 - (get_bits_count(gb) & 7);
01436 align = FFMIN(align, len);
01437 put_bits(&s->pb, align, get_bits(gb, align));
01438 len -= align;
01439 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01440 }
01441 skip_bits_long(gb, len);
01442
01443 {
01444 PutBitContext tmp = s->pb;
01445 flush_put_bits(&tmp);
01446 }
01447
01448 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01449 skip_bits(&s->gb, s->frame_offset);
01450 }
01451
01460 static int decode_packet(AVCodecContext *avctx,
01461 void *data, int *data_size, AVPacket* avpkt)
01462 {
01463 WMAProDecodeCtx *s = avctx->priv_data;
01464 GetBitContext* gb = &s->pgb;
01465 const uint8_t* buf = avpkt->data;
01466 int buf_size = avpkt->size;
01467 int num_bits_prev_frame;
01468 int packet_sequence_number;
01469
01470 s->samples = data;
01471 s->samples_end = (float*)((int8_t*)data + *data_size);
01472 *data_size = 0;
01473
01474 if (s->packet_done || s->packet_loss) {
01475 s->packet_done = 0;
01476 s->buf_bit_size = buf_size << 3;
01477
01479 if (buf_size < avctx->block_align)
01480 return 0;
01481
01482 buf_size = avctx->block_align;
01483
01485 init_get_bits(gb, buf, s->buf_bit_size);
01486 packet_sequence_number = get_bits(gb, 4);
01487 skip_bits(gb, 2);
01488
01490 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01491 dprintf(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01492 num_bits_prev_frame);
01493
01495 if (!s->packet_loss &&
01496 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01497 s->packet_loss = 1;
01498 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01499 s->packet_sequence_number, packet_sequence_number);
01500 }
01501 s->packet_sequence_number = packet_sequence_number;
01502
01503 if (num_bits_prev_frame > 0) {
01506 save_bits(s, gb, num_bits_prev_frame, 1);
01507 dprintf(avctx, "accumulated %x bits of frame data\n",
01508 s->num_saved_bits - s->frame_offset);
01509
01511 if (!s->packet_loss)
01512 decode_frame(s);
01513 } else if (s->num_saved_bits - s->frame_offset) {
01514 dprintf(avctx, "ignoring %x previously saved bits\n",
01515 s->num_saved_bits - s->frame_offset);
01516 }
01517
01518 s->packet_loss = 0;
01519
01520 } else {
01521 int frame_size;
01522 s->buf_bit_size = avpkt->size << 3;
01523 init_get_bits(gb, avpkt->data, s->buf_bit_size);
01524 skip_bits(gb, s->packet_offset);
01525 if (remaining_bits(s, gb) > s->log2_frame_size &&
01526 (frame_size = show_bits(gb, s->log2_frame_size)) &&
01527 frame_size <= remaining_bits(s, gb)) {
01528 save_bits(s, gb, frame_size, 0);
01529 s->packet_done = !decode_frame(s);
01530 } else
01531 s->packet_done = 1;
01532 }
01533
01534 if (s->packet_done && !s->packet_loss &&
01535 remaining_bits(s, gb) > 0) {
01538 save_bits(s, gb, remaining_bits(s, gb), 0);
01539 }
01540
01541 *data_size = (int8_t *)s->samples - (int8_t *)data;
01542 s->packet_offset = get_bits_count(gb) & 7;
01543
01544 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
01545 }
01546
01551 static void flush(AVCodecContext *avctx)
01552 {
01553 WMAProDecodeCtx *s = avctx->priv_data;
01554 int i;
01557 for (i = 0; i < s->num_channels; i++)
01558 memset(s->channel[i].out, 0, s->samples_per_frame *
01559 sizeof(*s->channel[i].out));
01560 s->packet_loss = 1;
01561 }
01562
01563
01567 AVCodec wmapro_decoder = {
01568 "wmapro",
01569 AVMEDIA_TYPE_AUDIO,
01570 CODEC_ID_WMAPRO,
01571 sizeof(WMAProDecodeCtx),
01572 decode_init,
01573 NULL,
01574 decode_end,
01575 decode_packet,
01576 .capabilities = CODEC_CAP_SUBFRAMES,
01577 .flush= flush,
01578 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01579 };