00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00089 #include "libavutil/intfloat.h"
00090 #include "libavutil/intreadwrite.h"
00091 #include "avcodec.h"
00092 #include "internal.h"
00093 #include "get_bits.h"
00094 #include "put_bits.h"
00095 #include "wmaprodata.h"
00096 #include "dsputil.h"
00097 #include "fmtconvert.h"
00098 #include "sinewin.h"
00099 #include "wma.h"
00100
00102 #define WMAPRO_MAX_CHANNELS 8
00103 #define MAX_SUBFRAMES 32
00104 #define MAX_BANDS 29
00105 #define MAX_FRAMESIZE 32768
00106
00107 #define WMAPRO_BLOCK_MIN_BITS 6
00108 #define WMAPRO_BLOCK_MAX_BITS 12
00109 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
00110 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
00111
00112
00113 #define VLCBITS 9
00114 #define SCALEVLCBITS 8
00115 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00116 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00117 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00118 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00119 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00120
00121 static VLC sf_vlc;
00122 static VLC sf_rl_vlc;
00123 static VLC vec4_vlc;
00124 static VLC vec2_vlc;
00125 static VLC vec1_vlc;
00126 static VLC coef_vlc[2];
00127 static float sin64[33];
00128
00132 typedef struct {
00133 int16_t prev_block_len;
00134 uint8_t transmit_coefs;
00135 uint8_t num_subframes;
00136 uint16_t subframe_len[MAX_SUBFRAMES];
00137 uint16_t subframe_offset[MAX_SUBFRAMES];
00138 uint8_t cur_subframe;
00139 uint16_t decoded_samples;
00140 uint8_t grouped;
00141 int quant_step;
00142 int8_t reuse_sf;
00143 int8_t scale_factor_step;
00144 int max_scale_factor;
00145 int saved_scale_factors[2][MAX_BANDS];
00146 int8_t scale_factor_idx;
00147 int* scale_factors;
00148 uint8_t table_idx;
00149 float* coeffs;
00150 uint16_t num_vec_coeffs;
00151 DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2];
00152 } WMAProChannelCtx;
00153
00157 typedef struct {
00158 uint8_t num_channels;
00159 int8_t transform;
00160 int8_t transform_band[MAX_BANDS];
00161 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00162 float* channel_data[WMAPRO_MAX_CHANNELS];
00163 } WMAProChannelGrp;
00164
00168 typedef struct WMAProDecodeCtx {
00169
00170 AVCodecContext* avctx;
00171 AVFrame frame;
00172 DSPContext dsp;
00173 FmtConvertContext fmt_conv;
00174 uint8_t frame_data[MAX_FRAMESIZE +
00175 FF_INPUT_BUFFER_PADDING_SIZE];
00176 PutBitContext pb;
00177 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES];
00178 DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE];
00179 float* windows[WMAPRO_BLOCK_SIZES];
00180
00181
00182 uint32_t decode_flags;
00183 uint8_t len_prefix;
00184 uint8_t dynamic_range_compression;
00185 uint8_t bits_per_sample;
00186 uint16_t samples_per_frame;
00187 uint16_t log2_frame_size;
00188 int8_t num_channels;
00189 int8_t lfe_channel;
00190 uint8_t max_num_subframes;
00191 uint8_t subframe_len_bits;
00192 uint8_t max_subframe_len_bit;
00193 uint16_t min_samples_per_subframe;
00194 int8_t num_sfb[WMAPRO_BLOCK_SIZES];
00195 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];
00196 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS];
00197 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES];
00198
00199
00200 GetBitContext pgb;
00201 int next_packet_start;
00202 uint8_t packet_offset;
00203 uint8_t packet_sequence_number;
00204 int num_saved_bits;
00205 int frame_offset;
00206 int subframe_offset;
00207 uint8_t packet_loss;
00208 uint8_t packet_done;
00209
00210
00211 uint32_t frame_num;
00212 GetBitContext gb;
00213 int buf_bit_size;
00214 uint8_t drc_gain;
00215 int8_t skip_frame;
00216 int8_t parsed_all_subframes;
00217
00218
00219 int16_t subframe_len;
00220 int8_t channels_for_cur_subframe;
00221 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00222 int8_t num_bands;
00223 int8_t transmit_num_vec_coeffs;
00224 int16_t* cur_sfb_offsets;
00225 uint8_t table_idx;
00226 int8_t esc_len;
00227
00228 uint8_t num_chgroups;
00229 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];
00230
00231 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];
00232 } WMAProDecodeCtx;
00233
00234
00239 static void av_cold dump_context(WMAProDecodeCtx *s)
00240 {
00241 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00242 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00243
00244 PRINT("ed sample bit depth", s->bits_per_sample);
00245 PRINT_HEX("ed decode flags", s->decode_flags);
00246 PRINT("samples per frame", s->samples_per_frame);
00247 PRINT("log2 frame size", s->log2_frame_size);
00248 PRINT("max num subframes", s->max_num_subframes);
00249 PRINT("len prefix", s->len_prefix);
00250 PRINT("num channels", s->num_channels);
00251 }
00252
00258 static av_cold int decode_end(AVCodecContext *avctx)
00259 {
00260 WMAProDecodeCtx *s = avctx->priv_data;
00261 int i;
00262
00263 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00264 ff_mdct_end(&s->mdct_ctx[i]);
00265
00266 return 0;
00267 }
00268
00274 static av_cold int decode_init(AVCodecContext *avctx)
00275 {
00276 WMAProDecodeCtx *s = avctx->priv_data;
00277 uint8_t *edata_ptr = avctx->extradata;
00278 unsigned int channel_mask;
00279 int i;
00280 int log2_max_num_subframes;
00281 int num_possible_block_sizes;
00282
00283 s->avctx = avctx;
00284 dsputil_init(&s->dsp, avctx);
00285 ff_fmt_convert_init(&s->fmt_conv, avctx);
00286 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00287
00288 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00289
00290 if (avctx->extradata_size >= 18) {
00291 s->decode_flags = AV_RL16(edata_ptr+14);
00292 channel_mask = AV_RL32(edata_ptr+2);
00293 s->bits_per_sample = AV_RL16(edata_ptr);
00295 for (i = 0; i < avctx->extradata_size; i++)
00296 av_dlog(avctx, "[%x] ", avctx->extradata[i]);
00297 av_dlog(avctx, "\n");
00298
00299 } else {
00300 av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00301 return AVERROR_INVALIDDATA;
00302 }
00303
00305 s->log2_frame_size = av_log2(avctx->block_align) + 4;
00306
00308 s->skip_frame = 1;
00309 s->packet_loss = 1;
00310 s->len_prefix = (s->decode_flags & 0x40);
00311
00313 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00314 3, s->decode_flags);
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 || s->max_num_subframes == 4)
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 if (s->avctx->sample_rate <= 0) {
00334 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
00335 return AVERROR_INVALIDDATA;
00336 }
00337
00338 s->num_channels = avctx->channels;
00339
00340 if (s->num_channels < 0) {
00341 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00342 return AVERROR_INVALIDDATA;
00343 } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00344 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00345 return AVERROR_PATCHWELCOME;
00346 }
00347
00349 for (i = 0; i < s->num_channels; i++)
00350 s->channel[i].prev_block_len = s->samples_per_frame;
00351
00353 s->lfe_channel = -1;
00354
00355 if (channel_mask & 8) {
00356 unsigned int mask;
00357 for (mask = 1; mask < 16; mask <<= 1) {
00358 if (channel_mask & mask)
00359 ++s->lfe_channel;
00360 }
00361 }
00362
00363 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00364 scale_huffbits, 1, 1,
00365 scale_huffcodes, 2, 2, 616);
00366
00367 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00368 scale_rl_huffbits, 1, 1,
00369 scale_rl_huffcodes, 4, 4, 1406);
00370
00371 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00372 coef0_huffbits, 1, 1,
00373 coef0_huffcodes, 4, 4, 2108);
00374
00375 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00376 coef1_huffbits, 1, 1,
00377 coef1_huffcodes, 4, 4, 3912);
00378
00379 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00380 vec4_huffbits, 1, 1,
00381 vec4_huffcodes, 2, 2, 604);
00382
00383 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00384 vec2_huffbits, 1, 1,
00385 vec2_huffcodes, 2, 2, 562);
00386
00387 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00388 vec1_huffbits, 1, 1,
00389 vec1_huffcodes, 2, 2, 562);
00390
00393 for (i = 0; i < num_possible_block_sizes; i++) {
00394 int subframe_len = s->samples_per_frame >> i;
00395 int x;
00396 int band = 1;
00397
00398 s->sfb_offsets[i][0] = 0;
00399
00400 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00401 int offset = (subframe_len * 2 * critical_freq[x])
00402 / s->avctx->sample_rate + 2;
00403 offset &= ~3;
00404 if (offset > s->sfb_offsets[i][band - 1])
00405 s->sfb_offsets[i][band++] = offset;
00406 }
00407 s->sfb_offsets[i][band - 1] = subframe_len;
00408 s->num_sfb[i] = band - 1;
00409 }
00410
00411
00417 for (i = 0; i < num_possible_block_sizes; i++) {
00418 int b;
00419 for (b = 0; b < s->num_sfb[i]; b++) {
00420 int x;
00421 int offset = ((s->sfb_offsets[i][b]
00422 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00423 for (x = 0; x < num_possible_block_sizes; x++) {
00424 int v = 0;
00425 while (s->sfb_offsets[x][v + 1] << x < offset)
00426 ++v;
00427 s->sf_offsets[i][x][b] = v;
00428 }
00429 }
00430 }
00431
00433 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00434 ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00435 1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00436 / (1 << (s->bits_per_sample - 1)));
00437
00439 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00440 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00441 ff_init_ff_sine_windows(win_idx);
00442 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00443 }
00444
00446 for (i = 0; i < num_possible_block_sizes; i++) {
00447 int block_size = s->samples_per_frame >> i;
00448 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00449 / s->avctx->sample_rate;
00450 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00451 }
00452
00454 for (i = 0; i < 33; i++)
00455 sin64[i] = sin(i*M_PI / 64.0);
00456
00457 if (avctx->debug & FF_DEBUG_BITSTREAM)
00458 dump_context(s);
00459
00460 avctx->channel_layout = channel_mask;
00461
00462 avcodec_get_frame_defaults(&s->frame);
00463 avctx->coded_frame = &s->frame;
00464
00465 return 0;
00466 }
00467
00474 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00475 {
00476 int frame_len_shift = 0;
00477 int subframe_len;
00478
00480 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00481 return s->min_samples_per_subframe;
00482
00484 if (s->max_subframe_len_bit) {
00485 if (get_bits1(&s->gb))
00486 frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00487 } else
00488 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00489
00490 subframe_len = s->samples_per_frame >> frame_len_shift;
00491
00493 if (subframe_len < s->min_samples_per_subframe ||
00494 subframe_len > s->samples_per_frame) {
00495 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00496 subframe_len);
00497 return AVERROR_INVALIDDATA;
00498 }
00499 return subframe_len;
00500 }
00501
00522 static int decode_tilehdr(WMAProDecodeCtx *s)
00523 {
00524 uint16_t num_samples[WMAPRO_MAX_CHANNELS];
00525 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS];
00526 int channels_for_cur_subframe = s->num_channels;
00527 int fixed_channel_layout = 0;
00528 int min_channel_len = 0;
00529 int c;
00530
00531
00532
00533
00534
00535
00536
00538 for (c = 0; c < s->num_channels; c++)
00539 s->channel[c].num_subframes = 0;
00540
00541 memset(num_samples, 0, sizeof(num_samples));
00542
00543 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00544 fixed_channel_layout = 1;
00545
00547 do {
00548 int subframe_len;
00549
00551 for (c = 0; c < s->num_channels; c++) {
00552 if (num_samples[c] == min_channel_len) {
00553 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00554 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00555 contains_subframe[c] = 1;
00556 else
00557 contains_subframe[c] = get_bits1(&s->gb);
00558 } else
00559 contains_subframe[c] = 0;
00560 }
00561
00563 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00564 return AVERROR_INVALIDDATA;
00565
00567 min_channel_len += subframe_len;
00568 for (c = 0; c < s->num_channels; c++) {
00569 WMAProChannelCtx* chan = &s->channel[c];
00570
00571 if (contains_subframe[c]) {
00572 if (chan->num_subframes >= MAX_SUBFRAMES) {
00573 av_log(s->avctx, AV_LOG_ERROR,
00574 "broken frame: num subframes > 31\n");
00575 return AVERROR_INVALIDDATA;
00576 }
00577 chan->subframe_len[chan->num_subframes] = subframe_len;
00578 num_samples[c] += subframe_len;
00579 ++chan->num_subframes;
00580 if (num_samples[c] > s->samples_per_frame) {
00581 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00582 "channel len > samples_per_frame\n");
00583 return AVERROR_INVALIDDATA;
00584 }
00585 } else if (num_samples[c] <= min_channel_len) {
00586 if (num_samples[c] < min_channel_len) {
00587 channels_for_cur_subframe = 0;
00588 min_channel_len = num_samples[c];
00589 }
00590 ++channels_for_cur_subframe;
00591 }
00592 }
00593 } while (min_channel_len < s->samples_per_frame);
00594
00595 for (c = 0; c < s->num_channels; c++) {
00596 int i;
00597 int offset = 0;
00598 for (i = 0; i < s->channel[c].num_subframes; i++) {
00599 av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00600 " len %i\n", s->frame_num, c, i,
00601 s->channel[c].subframe_len[i]);
00602 s->channel[c].subframe_offset[i] = offset;
00603 offset += s->channel[c].subframe_len[i];
00604 }
00605 }
00606
00607 return 0;
00608 }
00609
00615 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00616 WMAProChannelGrp *chgroup)
00617 {
00618 int i;
00619 int offset = 0;
00620 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00621 memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00622 s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00623
00624 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00625 rotation_offset[i] = get_bits(&s->gb, 6);
00626
00627 for (i = 0; i < chgroup->num_channels; i++)
00628 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00629 get_bits1(&s->gb) ? 1.0 : -1.0;
00630
00631 for (i = 1; i < chgroup->num_channels; i++) {
00632 int x;
00633 for (x = 0; x < i; x++) {
00634 int y;
00635 for (y = 0; y < i + 1; y++) {
00636 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00637 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00638 int n = rotation_offset[offset + x];
00639 float sinv;
00640 float cosv;
00641
00642 if (n < 32) {
00643 sinv = sin64[n];
00644 cosv = sin64[32 - n];
00645 } else {
00646 sinv = sin64[64 - n];
00647 cosv = -sin64[n - 32];
00648 }
00649
00650 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00651 (v1 * sinv) - (v2 * cosv);
00652 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00653 (v1 * cosv) + (v2 * sinv);
00654 }
00655 }
00656 offset += i;
00657 }
00658 }
00659
00665 static int decode_channel_transform(WMAProDecodeCtx* s)
00666 {
00667 int i;
00668
00669
00670
00671
00672
00674 s->num_chgroups = 0;
00675 if (s->num_channels > 1) {
00676 int remaining_channels = s->channels_for_cur_subframe;
00677
00678 if (get_bits1(&s->gb)) {
00679 av_log_ask_for_sample(s->avctx,
00680 "unsupported channel transform bit\n");
00681 return AVERROR_INVALIDDATA;
00682 }
00683
00684 for (s->num_chgroups = 0; remaining_channels &&
00685 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00686 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00687 float** channel_data = chgroup->channel_data;
00688 chgroup->num_channels = 0;
00689 chgroup->transform = 0;
00690
00692 if (remaining_channels > 2) {
00693 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00694 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00695 if (!s->channel[channel_idx].grouped
00696 && get_bits1(&s->gb)) {
00697 ++chgroup->num_channels;
00698 s->channel[channel_idx].grouped = 1;
00699 *channel_data++ = s->channel[channel_idx].coeffs;
00700 }
00701 }
00702 } else {
00703 chgroup->num_channels = remaining_channels;
00704 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00705 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00706 if (!s->channel[channel_idx].grouped)
00707 *channel_data++ = s->channel[channel_idx].coeffs;
00708 s->channel[channel_idx].grouped = 1;
00709 }
00710 }
00711
00713 if (chgroup->num_channels == 2) {
00714 if (get_bits1(&s->gb)) {
00715 if (get_bits1(&s->gb)) {
00716 av_log_ask_for_sample(s->avctx,
00717 "unsupported channel transform type\n");
00718 }
00719 } else {
00720 chgroup->transform = 1;
00721 if (s->num_channels == 2) {
00722 chgroup->decorrelation_matrix[0] = 1.0;
00723 chgroup->decorrelation_matrix[1] = -1.0;
00724 chgroup->decorrelation_matrix[2] = 1.0;
00725 chgroup->decorrelation_matrix[3] = 1.0;
00726 } else {
00728 chgroup->decorrelation_matrix[0] = 0.70703125;
00729 chgroup->decorrelation_matrix[1] = -0.70703125;
00730 chgroup->decorrelation_matrix[2] = 0.70703125;
00731 chgroup->decorrelation_matrix[3] = 0.70703125;
00732 }
00733 }
00734 } else if (chgroup->num_channels > 2) {
00735 if (get_bits1(&s->gb)) {
00736 chgroup->transform = 1;
00737 if (get_bits1(&s->gb)) {
00738 decode_decorrelation_matrix(s, chgroup);
00739 } else {
00741 if (chgroup->num_channels > 6) {
00742 av_log_ask_for_sample(s->avctx,
00743 "coupled channels > 6\n");
00744 } else {
00745 memcpy(chgroup->decorrelation_matrix,
00746 default_decorrelation[chgroup->num_channels],
00747 chgroup->num_channels * chgroup->num_channels *
00748 sizeof(*chgroup->decorrelation_matrix));
00749 }
00750 }
00751 }
00752 }
00753
00755 if (chgroup->transform) {
00756 if (!get_bits1(&s->gb)) {
00757 int i;
00759 for (i = 0; i < s->num_bands; i++) {
00760 chgroup->transform_band[i] = get_bits1(&s->gb);
00761 }
00762 } else {
00763 memset(chgroup->transform_band, 1, s->num_bands);
00764 }
00765 }
00766 remaining_channels -= chgroup->num_channels;
00767 }
00768 }
00769 return 0;
00770 }
00771
00778 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00779 {
00780
00781
00782
00783 static const uint32_t fval_tab[16] = {
00784 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00785 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00786 0x41000000, 0x41100000, 0x41200000, 0x41300000,
00787 0x41400000, 0x41500000, 0x41600000, 0x41700000,
00788 };
00789 int vlctable;
00790 VLC* vlc;
00791 WMAProChannelCtx* ci = &s->channel[c];
00792 int rl_mode = 0;
00793 int cur_coeff = 0;
00794 int num_zeros = 0;
00795 const uint16_t* run;
00796 const float* level;
00797
00798 av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00799
00800 vlctable = get_bits1(&s->gb);
00801 vlc = &coef_vlc[vlctable];
00802
00803 if (vlctable) {
00804 run = coef1_run;
00805 level = coef1_level;
00806 } else {
00807 run = coef0_run;
00808 level = coef0_level;
00809 }
00810
00813 while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00814 (cur_coeff + 3 < ci->num_vec_coeffs)) {
00815 uint32_t vals[4];
00816 int i;
00817 unsigned int idx;
00818
00819 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00820
00821 if (idx == HUFF_VEC4_SIZE - 1) {
00822 for (i = 0; i < 4; i += 2) {
00823 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00824 if (idx == HUFF_VEC2_SIZE - 1) {
00825 uint32_t v0, v1;
00826 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00827 if (v0 == HUFF_VEC1_SIZE - 1)
00828 v0 += ff_wma_get_large_val(&s->gb);
00829 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00830 if (v1 == HUFF_VEC1_SIZE - 1)
00831 v1 += ff_wma_get_large_val(&s->gb);
00832 vals[i ] = av_float2int(v0);
00833 vals[i+1] = av_float2int(v1);
00834 } else {
00835 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
00836 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00837 }
00838 }
00839 } else {
00840 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
00841 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00842 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00843 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
00844 }
00845
00847 for (i = 0; i < 4; i++) {
00848 if (vals[i]) {
00849 uint32_t sign = get_bits1(&s->gb) - 1;
00850 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
00851 num_zeros = 0;
00852 } else {
00853 ci->coeffs[cur_coeff] = 0;
00856 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00857 }
00858 ++cur_coeff;
00859 }
00860 }
00861
00863 if (cur_coeff < s->subframe_len) {
00864 memset(&ci->coeffs[cur_coeff], 0,
00865 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00866 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00867 level, run, 1, ci->coeffs,
00868 cur_coeff, s->subframe_len,
00869 s->subframe_len, s->esc_len, 0))
00870 return AVERROR_INVALIDDATA;
00871 }
00872
00873 return 0;
00874 }
00875
00881 static int decode_scale_factors(WMAProDecodeCtx* s)
00882 {
00883 int i;
00884
00889 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00890 int c = s->channel_indexes_for_cur_subframe[i];
00891 int* sf;
00892 int* sf_end;
00893 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00894 sf_end = s->channel[c].scale_factors + s->num_bands;
00895
00901 if (s->channel[c].reuse_sf) {
00902 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00903 int b;
00904 for (b = 0; b < s->num_bands; b++)
00905 s->channel[c].scale_factors[b] =
00906 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00907 }
00908
00909 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00910
00911 if (!s->channel[c].reuse_sf) {
00912 int val;
00914 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00915 val = 45 / s->channel[c].scale_factor_step;
00916 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00917 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00918 *sf = val;
00919 }
00920 } else {
00921 int i;
00923 for (i = 0; i < s->num_bands; i++) {
00924 int idx;
00925 int skip;
00926 int val;
00927 int sign;
00928
00929 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00930
00931 if (!idx) {
00932 uint32_t code = get_bits(&s->gb, 14);
00933 val = code >> 6;
00934 sign = (code & 1) - 1;
00935 skip = (code & 0x3f) >> 1;
00936 } else if (idx == 1) {
00937 break;
00938 } else {
00939 skip = scale_rl_run[idx];
00940 val = scale_rl_level[idx];
00941 sign = get_bits1(&s->gb)-1;
00942 }
00943
00944 i += skip;
00945 if (i >= s->num_bands) {
00946 av_log(s->avctx, AV_LOG_ERROR,
00947 "invalid scale factor coding\n");
00948 return AVERROR_INVALIDDATA;
00949 }
00950 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00951 }
00952 }
00954 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00955 s->channel[c].table_idx = s->table_idx;
00956 s->channel[c].reuse_sf = 1;
00957 }
00958
00960 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00961 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00962 s->channel[c].max_scale_factor =
00963 FFMAX(s->channel[c].max_scale_factor, *sf);
00964 }
00965
00966 }
00967 return 0;
00968 }
00969
00974 static void inverse_channel_transform(WMAProDecodeCtx *s)
00975 {
00976 int i;
00977
00978 for (i = 0; i < s->num_chgroups; i++) {
00979 if (s->chgroup[i].transform) {
00980 float data[WMAPRO_MAX_CHANNELS];
00981 const int num_channels = s->chgroup[i].num_channels;
00982 float** ch_data = s->chgroup[i].channel_data;
00983 float** ch_end = ch_data + num_channels;
00984 const int8_t* tb = s->chgroup[i].transform_band;
00985 int16_t* sfb;
00986
00988 for (sfb = s->cur_sfb_offsets;
00989 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00990 int y;
00991 if (*tb++ == 1) {
00993 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00994 const float* mat = s->chgroup[i].decorrelation_matrix;
00995 const float* data_end = data + num_channels;
00996 float* data_ptr = data;
00997 float** ch;
00998
00999 for (ch = ch_data; ch < ch_end; ch++)
01000 *data_ptr++ = (*ch)[y];
01001
01002 for (ch = ch_data; ch < ch_end; ch++) {
01003 float sum = 0;
01004 data_ptr = data;
01005 while (data_ptr < data_end)
01006 sum += *data_ptr++ * *mat++;
01007
01008 (*ch)[y] = sum;
01009 }
01010 }
01011 } else if (s->num_channels == 2) {
01012 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01013 s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01014 ch_data[0] + sfb[0],
01015 181.0 / 128, len);
01016 s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01017 ch_data[1] + sfb[0],
01018 181.0 / 128, len);
01019 }
01020 }
01021 }
01022 }
01023 }
01024
01029 static void wmapro_window(WMAProDecodeCtx *s)
01030 {
01031 int i;
01032 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01033 int c = s->channel_indexes_for_cur_subframe[i];
01034 float* window;
01035 int winlen = s->channel[c].prev_block_len;
01036 float* start = s->channel[c].coeffs - (winlen >> 1);
01037
01038 if (s->subframe_len < winlen) {
01039 start += (winlen - s->subframe_len) >> 1;
01040 winlen = s->subframe_len;
01041 }
01042
01043 window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01044
01045 winlen >>= 1;
01046
01047 s->dsp.vector_fmul_window(start, start, start + winlen,
01048 window, winlen);
01049
01050 s->channel[c].prev_block_len = s->subframe_len;
01051 }
01052 }
01053
01059 static int decode_subframe(WMAProDecodeCtx *s)
01060 {
01061 int offset = s->samples_per_frame;
01062 int subframe_len = s->samples_per_frame;
01063 int i;
01064 int total_samples = s->samples_per_frame * s->num_channels;
01065 int transmit_coeffs = 0;
01066 int cur_subwoofer_cutoff;
01067
01068 s->subframe_offset = get_bits_count(&s->gb);
01069
01074 for (i = 0; i < s->num_channels; i++) {
01075 s->channel[i].grouped = 0;
01076 if (offset > s->channel[i].decoded_samples) {
01077 offset = s->channel[i].decoded_samples;
01078 subframe_len =
01079 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01080 }
01081 }
01082
01083 av_dlog(s->avctx,
01084 "processing subframe with offset %i len %i\n", offset, subframe_len);
01085
01087 s->channels_for_cur_subframe = 0;
01088 for (i = 0; i < s->num_channels; i++) {
01089 const int cur_subframe = s->channel[i].cur_subframe;
01091 total_samples -= s->channel[i].decoded_samples;
01092
01094 if (offset == s->channel[i].decoded_samples &&
01095 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01096 total_samples -= s->channel[i].subframe_len[cur_subframe];
01097 s->channel[i].decoded_samples +=
01098 s->channel[i].subframe_len[cur_subframe];
01099 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01100 ++s->channels_for_cur_subframe;
01101 }
01102 }
01103
01106 if (!total_samples)
01107 s->parsed_all_subframes = 1;
01108
01109
01110 av_dlog(s->avctx, "subframe is part of %i channels\n",
01111 s->channels_for_cur_subframe);
01112
01114 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
01115 s->num_bands = s->num_sfb[s->table_idx];
01116 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
01117 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01118
01120 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01121 int c = s->channel_indexes_for_cur_subframe[i];
01122
01123 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01124 + offset];
01125 }
01126
01127 s->subframe_len = subframe_len;
01128 s->esc_len = av_log2(s->subframe_len - 1) + 1;
01129
01131 if (get_bits1(&s->gb)) {
01132 int num_fill_bits;
01133 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01134 int len = get_bits(&s->gb, 4);
01135 num_fill_bits = get_bits(&s->gb, len) + 1;
01136 }
01137
01138 if (num_fill_bits >= 0) {
01139 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01140 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01141 return AVERROR_INVALIDDATA;
01142 }
01143
01144 skip_bits_long(&s->gb, num_fill_bits);
01145 }
01146 }
01147
01149 if (get_bits1(&s->gb)) {
01150 av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01151 return AVERROR_INVALIDDATA;
01152 }
01153
01154
01155 if (decode_channel_transform(s) < 0)
01156 return AVERROR_INVALIDDATA;
01157
01158
01159 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01160 int c = s->channel_indexes_for_cur_subframe[i];
01161 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01162 transmit_coeffs = 1;
01163 }
01164
01165 if (transmit_coeffs) {
01166 int step;
01167 int quant_step = 90 * s->bits_per_sample >> 4;
01168
01170 if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01171 int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01172 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01173 int c = s->channel_indexes_for_cur_subframe[i];
01174 int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01175 if (num_vec_coeffs > WMAPRO_BLOCK_MAX_SIZE) {
01176 av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
01177 return AVERROR_INVALIDDATA;
01178 }
01179 s->channel[c].num_vec_coeffs = num_vec_coeffs;
01180 }
01181 } else {
01182 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01183 int c = s->channel_indexes_for_cur_subframe[i];
01184 s->channel[c].num_vec_coeffs = s->subframe_len;
01185 }
01186 }
01188 step = get_sbits(&s->gb, 6);
01189 quant_step += step;
01190 if (step == -32 || step == 31) {
01191 const int sign = (step == 31) - 1;
01192 int quant = 0;
01193 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01194 (step = get_bits(&s->gb, 5)) == 31) {
01195 quant += 31;
01196 }
01197 quant_step += ((quant + step) ^ sign) - sign;
01198 }
01199 if (quant_step < 0) {
01200 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01201 }
01202
01205 if (s->channels_for_cur_subframe == 1) {
01206 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01207 } else {
01208 int modifier_len = get_bits(&s->gb, 3);
01209 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01210 int c = s->channel_indexes_for_cur_subframe[i];
01211 s->channel[c].quant_step = quant_step;
01212 if (get_bits1(&s->gb)) {
01213 if (modifier_len) {
01214 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01215 } else
01216 ++s->channel[c].quant_step;
01217 }
01218 }
01219 }
01220
01222 if (decode_scale_factors(s) < 0)
01223 return AVERROR_INVALIDDATA;
01224 }
01225
01226 av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01227 get_bits_count(&s->gb) - s->subframe_offset);
01228
01230 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01231 int c = s->channel_indexes_for_cur_subframe[i];
01232 if (s->channel[c].transmit_coefs &&
01233 get_bits_count(&s->gb) < s->num_saved_bits) {
01234 decode_coeffs(s, c);
01235 } else
01236 memset(s->channel[c].coeffs, 0,
01237 sizeof(*s->channel[c].coeffs) * subframe_len);
01238 }
01239
01240 av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01241 get_bits_count(&s->gb) - s->subframe_offset);
01242
01243 if (transmit_coeffs) {
01244 FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01246 inverse_channel_transform(s);
01247 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01248 int c = s->channel_indexes_for_cur_subframe[i];
01249 const int* sf = s->channel[c].scale_factors;
01250 int b;
01251
01252 if (c == s->lfe_channel)
01253 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01254 (subframe_len - cur_subwoofer_cutoff));
01255
01257 for (b = 0; b < s->num_bands; b++) {
01258 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01259 const int exp = s->channel[c].quant_step -
01260 (s->channel[c].max_scale_factor - *sf++) *
01261 s->channel[c].scale_factor_step;
01262 const float quant = pow(10.0, exp / 20.0);
01263 int start = s->cur_sfb_offsets[b];
01264 s->dsp.vector_fmul_scalar(s->tmp + start,
01265 s->channel[c].coeffs + start,
01266 quant, end - start);
01267 }
01268
01270 mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01271 }
01272 }
01273
01275 wmapro_window(s);
01276
01278 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01279 int c = s->channel_indexes_for_cur_subframe[i];
01280 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01281 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01282 return AVERROR_INVALIDDATA;
01283 }
01284 ++s->channel[c].cur_subframe;
01285 }
01286
01287 return 0;
01288 }
01289
01296 static int decode_frame(WMAProDecodeCtx *s, int *got_frame_ptr)
01297 {
01298 AVCodecContext *avctx = s->avctx;
01299 GetBitContext* gb = &s->gb;
01300 int more_frames = 0;
01301 int len = 0;
01302 int i, ret;
01303 const float *out_ptr[WMAPRO_MAX_CHANNELS];
01304 float *samples;
01305
01307 if (s->len_prefix)
01308 len = get_bits(gb, s->log2_frame_size);
01309
01310 av_dlog(s->avctx, "decoding frame with length %x\n", len);
01311
01313 if (decode_tilehdr(s)) {
01314 s->packet_loss = 1;
01315 return 0;
01316 }
01317
01319 if (s->num_channels > 1 && get_bits1(gb)) {
01320 if (get_bits1(gb)) {
01321 for (i = 0; i < s->num_channels * s->num_channels; i++)
01322 skip_bits(gb, 4);
01323 }
01324 }
01325
01327 if (s->dynamic_range_compression) {
01328 s->drc_gain = get_bits(gb, 8);
01329 av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01330 }
01331
01334 if (get_bits1(gb)) {
01335 int av_unused skip;
01336
01338 if (get_bits1(gb)) {
01339 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01340 av_dlog(s->avctx, "start skip: %i\n", skip);
01341 }
01342
01344 if (get_bits1(gb)) {
01345 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01346 av_dlog(s->avctx, "end skip: %i\n", skip);
01347 }
01348
01349 }
01350
01351 av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01352 get_bits_count(gb) - s->frame_offset);
01353
01355 s->parsed_all_subframes = 0;
01356 for (i = 0; i < s->num_channels; i++) {
01357 s->channel[i].decoded_samples = 0;
01358 s->channel[i].cur_subframe = 0;
01359 s->channel[i].reuse_sf = 0;
01360 }
01361
01363 while (!s->parsed_all_subframes) {
01364 if (decode_subframe(s) < 0) {
01365 s->packet_loss = 1;
01366 return 0;
01367 }
01368 }
01369
01370
01371 s->frame.nb_samples = s->samples_per_frame;
01372 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01373 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01374 s->packet_loss = 1;
01375 return 0;
01376 }
01377 samples = (float *)s->frame.data[0];
01378
01380 for (i = 0; i < s->num_channels; i++)
01381 out_ptr[i] = s->channel[i].out;
01382 s->fmt_conv.float_interleave(samples, out_ptr, s->samples_per_frame,
01383 s->num_channels);
01384
01385 for (i = 0; i < s->num_channels; i++) {
01387 memcpy(&s->channel[i].out[0],
01388 &s->channel[i].out[s->samples_per_frame],
01389 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01390 }
01391
01392 if (s->skip_frame) {
01393 s->skip_frame = 0;
01394 *got_frame_ptr = 0;
01395 } else {
01396 *got_frame_ptr = 1;
01397 }
01398
01399 if (s->len_prefix) {
01400 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01402 av_log(s->avctx, AV_LOG_ERROR,
01403 "frame[%i] would have to skip %i bits\n", s->frame_num,
01404 len - (get_bits_count(gb) - s->frame_offset) - 1);
01405 s->packet_loss = 1;
01406 return 0;
01407 }
01408
01410 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01411 } else {
01412 while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01413 }
01414 }
01415
01417 more_frames = get_bits1(gb);
01418
01419 ++s->frame_num;
01420 return more_frames;
01421 }
01422
01429 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01430 {
01431 return s->buf_bit_size - get_bits_count(gb);
01432 }
01433
01441 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01442 int append)
01443 {
01444 int buflen;
01445
01450 if (!append) {
01451 s->frame_offset = get_bits_count(gb) & 7;
01452 s->num_saved_bits = s->frame_offset;
01453 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01454 }
01455
01456 buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
01457
01458 if (len <= 0 || buflen > MAX_FRAMESIZE) {
01459 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01460 s->packet_loss = 1;
01461 return;
01462 }
01463
01464 s->num_saved_bits += len;
01465 if (!append) {
01466 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01467 s->num_saved_bits);
01468 } else {
01469 int align = 8 - (get_bits_count(gb) & 7);
01470 align = FFMIN(align, len);
01471 put_bits(&s->pb, align, get_bits(gb, align));
01472 len -= align;
01473 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01474 }
01475 skip_bits_long(gb, len);
01476
01477 {
01478 PutBitContext tmp = s->pb;
01479 flush_put_bits(&tmp);
01480 }
01481
01482 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01483 skip_bits(&s->gb, s->frame_offset);
01484 }
01485
01494 static int decode_packet(AVCodecContext *avctx, void *data,
01495 int *got_frame_ptr, AVPacket* avpkt)
01496 {
01497 WMAProDecodeCtx *s = avctx->priv_data;
01498 GetBitContext* gb = &s->pgb;
01499 const uint8_t* buf = avpkt->data;
01500 int buf_size = avpkt->size;
01501 int num_bits_prev_frame;
01502 int packet_sequence_number;
01503
01504 *got_frame_ptr = 0;
01505
01506 if (s->packet_done || s->packet_loss) {
01507 s->packet_done = 0;
01508
01510 if (buf_size < avctx->block_align)
01511 return 0;
01512
01513 s->next_packet_start = buf_size - avctx->block_align;
01514 buf_size = avctx->block_align;
01515 s->buf_bit_size = buf_size << 3;
01516
01518 init_get_bits(gb, buf, s->buf_bit_size);
01519 packet_sequence_number = get_bits(gb, 4);
01520 skip_bits(gb, 2);
01521
01523 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01524 av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01525 num_bits_prev_frame);
01526
01528 if (!s->packet_loss &&
01529 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01530 s->packet_loss = 1;
01531 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01532 s->packet_sequence_number, packet_sequence_number);
01533 }
01534 s->packet_sequence_number = packet_sequence_number;
01535
01536 if (num_bits_prev_frame > 0) {
01537 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01538 if (num_bits_prev_frame >= remaining_packet_bits) {
01539 num_bits_prev_frame = remaining_packet_bits;
01540 s->packet_done = 1;
01541 }
01542
01545 save_bits(s, gb, num_bits_prev_frame, 1);
01546 av_dlog(avctx, "accumulated %x bits of frame data\n",
01547 s->num_saved_bits - s->frame_offset);
01548
01550 if (!s->packet_loss)
01551 decode_frame(s, got_frame_ptr);
01552 } else if (s->num_saved_bits - s->frame_offset) {
01553 av_dlog(avctx, "ignoring %x previously saved bits\n",
01554 s->num_saved_bits - s->frame_offset);
01555 }
01556
01557 if (s->packet_loss) {
01561 s->num_saved_bits = 0;
01562 s->packet_loss = 0;
01563 }
01564
01565 } else {
01566 int frame_size;
01567 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01568 init_get_bits(gb, avpkt->data, s->buf_bit_size);
01569 skip_bits(gb, s->packet_offset);
01570 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01571 (frame_size = show_bits(gb, s->log2_frame_size)) &&
01572 frame_size <= remaining_bits(s, gb)) {
01573 save_bits(s, gb, frame_size, 0);
01574 s->packet_done = !decode_frame(s, got_frame_ptr);
01575 } else if (!s->len_prefix
01576 && s->num_saved_bits > get_bits_count(&s->gb)) {
01584 s->packet_done = !decode_frame(s, got_frame_ptr);
01585 } else
01586 s->packet_done = 1;
01587 }
01588
01589 if (s->packet_done && !s->packet_loss &&
01590 remaining_bits(s, gb) > 0) {
01593 save_bits(s, gb, remaining_bits(s, gb), 0);
01594 }
01595
01596 s->packet_offset = get_bits_count(gb) & 7;
01597 if (s->packet_loss)
01598 return AVERROR_INVALIDDATA;
01599
01600 if (*got_frame_ptr)
01601 *(AVFrame *)data = s->frame;
01602
01603 return get_bits_count(gb) >> 3;
01604 }
01605
01610 static void flush(AVCodecContext *avctx)
01611 {
01612 WMAProDecodeCtx *s = avctx->priv_data;
01613 int i;
01616 for (i = 0; i < s->num_channels; i++)
01617 memset(s->channel[i].out, 0, s->samples_per_frame *
01618 sizeof(*s->channel[i].out));
01619 s->packet_loss = 1;
01620 }
01621
01622
01626 AVCodec ff_wmapro_decoder = {
01627 .name = "wmapro",
01628 .type = AVMEDIA_TYPE_AUDIO,
01629 .id = CODEC_ID_WMAPRO,
01630 .priv_data_size = sizeof(WMAProDecodeCtx),
01631 .init = decode_init,
01632 .close = decode_end,
01633 .decode = decode_packet,
01634 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01635 .flush= flush,
01636 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01637 };