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 #include "wma_common.h"
00101
00103 #define WMAPRO_MAX_CHANNELS 8
00104 #define MAX_SUBFRAMES 32
00105 #define MAX_BANDS 29
00106 #define MAX_FRAMESIZE 32768
00107
00108 #define WMAPRO_BLOCK_MIN_BITS 6
00109 #define WMAPRO_BLOCK_MAX_BITS 13
00110 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
00111 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
00112
00113
00114 #define VLCBITS 9
00115 #define SCALEVLCBITS 8
00116 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00117 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00118 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00119 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00120 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00121
00122 static VLC sf_vlc;
00123 static VLC sf_rl_vlc;
00124 static VLC vec4_vlc;
00125 static VLC vec2_vlc;
00126 static VLC vec1_vlc;
00127 static VLC coef_vlc[2];
00128 static float sin64[33];
00129
00133 typedef struct {
00134 int16_t prev_block_len;
00135 uint8_t transmit_coefs;
00136 uint8_t num_subframes;
00137 uint16_t subframe_len[MAX_SUBFRAMES];
00138 uint16_t subframe_offset[MAX_SUBFRAMES];
00139 uint8_t cur_subframe;
00140 uint16_t decoded_samples;
00141 uint8_t grouped;
00142 int quant_step;
00143 int8_t reuse_sf;
00144 int8_t scale_factor_step;
00145 int max_scale_factor;
00146 int saved_scale_factors[2][MAX_BANDS];
00147 int8_t scale_factor_idx;
00148 int* scale_factors;
00149 uint8_t table_idx;
00150 float* coeffs;
00151 uint16_t num_vec_coeffs;
00152 DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2];
00153 } WMAProChannelCtx;
00154
00158 typedef struct {
00159 uint8_t num_channels;
00160 int8_t transform;
00161 int8_t transform_band[MAX_BANDS];
00162 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00163 float* channel_data[WMAPRO_MAX_CHANNELS];
00164 } WMAProChannelGrp;
00165
00169 typedef struct WMAProDecodeCtx {
00170
00171 AVCodecContext* avctx;
00172 AVFrame frame;
00173 DSPContext dsp;
00174 FmtConvertContext fmt_conv;
00175 uint8_t frame_data[MAX_FRAMESIZE +
00176 FF_INPUT_BUFFER_PADDING_SIZE];
00177 PutBitContext pb;
00178 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES];
00179 DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE];
00180 float* windows[WMAPRO_BLOCK_SIZES];
00181
00182
00183 uint32_t decode_flags;
00184 uint8_t len_prefix;
00185 uint8_t dynamic_range_compression;
00186 uint8_t bits_per_sample;
00187 uint16_t samples_per_frame;
00188 uint16_t log2_frame_size;
00189 int8_t num_channels;
00190 int8_t lfe_channel;
00191 uint8_t max_num_subframes;
00192 uint8_t subframe_len_bits;
00193 uint8_t max_subframe_len_bit;
00194 uint16_t min_samples_per_subframe;
00195 int8_t num_sfb[WMAPRO_BLOCK_SIZES];
00196 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];
00197 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS];
00198 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES];
00199
00200
00201 GetBitContext pgb;
00202 int next_packet_start;
00203 uint8_t packet_offset;
00204 uint8_t packet_sequence_number;
00205 int num_saved_bits;
00206 int frame_offset;
00207 int subframe_offset;
00208 uint8_t packet_loss;
00209 uint8_t packet_done;
00210
00211
00212 uint32_t frame_num;
00213 GetBitContext gb;
00214 int buf_bit_size;
00215 uint8_t drc_gain;
00216 int8_t skip_frame;
00217 int8_t parsed_all_subframes;
00218
00219
00220 int16_t subframe_len;
00221 int8_t channels_for_cur_subframe;
00222 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00223 int8_t num_bands;
00224 int8_t transmit_num_vec_coeffs;
00225 int16_t* cur_sfb_offsets;
00226 uint8_t table_idx;
00227 int8_t esc_len;
00228
00229 uint8_t num_chgroups;
00230 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];
00231
00232 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];
00233 } WMAProDecodeCtx;
00234
00235
00240 static av_cold void dump_context(WMAProDecodeCtx *s)
00241 {
00242 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00243 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00244
00245 PRINT("ed sample bit depth", s->bits_per_sample);
00246 PRINT_HEX("ed decode flags", s->decode_flags);
00247 PRINT("samples per frame", s->samples_per_frame);
00248 PRINT("log2 frame size", s->log2_frame_size);
00249 PRINT("max num subframes", s->max_num_subframes);
00250 PRINT("len prefix", s->len_prefix);
00251 PRINT("num channels", s->num_channels);
00252 }
00253
00259 static av_cold int decode_end(AVCodecContext *avctx)
00260 {
00261 WMAProDecodeCtx *s = avctx->priv_data;
00262 int i;
00263
00264 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00265 ff_mdct_end(&s->mdct_ctx[i]);
00266
00267 return 0;
00268 }
00269
00275 static av_cold int decode_init(AVCodecContext *avctx)
00276 {
00277 WMAProDecodeCtx *s = avctx->priv_data;
00278 uint8_t *edata_ptr = avctx->extradata;
00279 unsigned int channel_mask;
00280 int i, bits;
00281 int log2_max_num_subframes;
00282 int num_possible_block_sizes;
00283
00284 s->avctx = avctx;
00285 ff_dsputil_init(&s->dsp, avctx);
00286 ff_fmt_convert_init(&s->fmt_conv, avctx);
00287 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00288
00289 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00290
00291 if (avctx->extradata_size >= 18) {
00292 s->decode_flags = AV_RL16(edata_ptr+14);
00293 channel_mask = AV_RL32(edata_ptr+2);
00294 s->bits_per_sample = AV_RL16(edata_ptr);
00296 for (i = 0; i < avctx->extradata_size; i++)
00297 av_dlog(avctx, "[%x] ", avctx->extradata[i]);
00298 av_dlog(avctx, "\n");
00299
00300 } else {
00301 av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00302 return AVERROR_INVALIDDATA;
00303 }
00304
00306 s->log2_frame_size = av_log2(avctx->block_align) + 4;
00307
00309 s->skip_frame = 1;
00310 s->packet_loss = 1;
00311 s->len_prefix = (s->decode_flags & 0x40);
00312
00314 bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
00315 if (bits > WMAPRO_BLOCK_MAX_BITS) {
00316 av_log_missing_feature(avctx, "14-bits block sizes", 1);
00317 return AVERROR_INVALIDDATA;
00318 }
00319 s->samples_per_frame = 1 << bits;
00320
00322 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
00323 s->max_num_subframes = 1 << log2_max_num_subframes;
00324 if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
00325 s->max_subframe_len_bit = 1;
00326 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00327
00328 num_possible_block_sizes = log2_max_num_subframes + 1;
00329 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
00330 s->dynamic_range_compression = (s->decode_flags & 0x80);
00331
00332 if (s->max_num_subframes > MAX_SUBFRAMES) {
00333 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00334 s->max_num_subframes);
00335 return AVERROR_INVALIDDATA;
00336 }
00337
00338 if (s->min_samples_per_subframe < (1<<WMAPRO_BLOCK_MIN_BITS)) {
00339 av_log(avctx, AV_LOG_ERROR, "min_samples_per_subframe of %d too small\n",
00340 s->min_samples_per_subframe);
00341 return AVERROR_INVALIDDATA;
00342 }
00343
00344 if (s->avctx->sample_rate <= 0) {
00345 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
00346 return AVERROR_INVALIDDATA;
00347 }
00348
00349 s->num_channels = avctx->channels;
00350
00351 if (s->num_channels < 0) {
00352 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00353 return AVERROR_INVALIDDATA;
00354 } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00355 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00356 return AVERROR_PATCHWELCOME;
00357 }
00358
00360 for (i = 0; i < s->num_channels; i++)
00361 s->channel[i].prev_block_len = s->samples_per_frame;
00362
00364 s->lfe_channel = -1;
00365
00366 if (channel_mask & 8) {
00367 unsigned int mask;
00368 for (mask = 1; mask < 16; mask <<= 1) {
00369 if (channel_mask & mask)
00370 ++s->lfe_channel;
00371 }
00372 }
00373
00374 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00375 scale_huffbits, 1, 1,
00376 scale_huffcodes, 2, 2, 616);
00377
00378 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00379 scale_rl_huffbits, 1, 1,
00380 scale_rl_huffcodes, 4, 4, 1406);
00381
00382 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00383 coef0_huffbits, 1, 1,
00384 coef0_huffcodes, 4, 4, 2108);
00385
00386 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00387 coef1_huffbits, 1, 1,
00388 coef1_huffcodes, 4, 4, 3912);
00389
00390 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00391 vec4_huffbits, 1, 1,
00392 vec4_huffcodes, 2, 2, 604);
00393
00394 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00395 vec2_huffbits, 1, 1,
00396 vec2_huffcodes, 2, 2, 562);
00397
00398 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00399 vec1_huffbits, 1, 1,
00400 vec1_huffcodes, 2, 2, 562);
00401
00404 for (i = 0; i < num_possible_block_sizes; i++) {
00405 int subframe_len = s->samples_per_frame >> i;
00406 int x;
00407 int band = 1;
00408
00409 s->sfb_offsets[i][0] = 0;
00410
00411 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00412 int offset = (subframe_len * 2 * critical_freq[x])
00413 / s->avctx->sample_rate + 2;
00414 offset &= ~3;
00415 if (offset > s->sfb_offsets[i][band - 1])
00416 s->sfb_offsets[i][band++] = offset;
00417 }
00418 s->sfb_offsets[i][band - 1] = subframe_len;
00419 s->num_sfb[i] = band - 1;
00420 }
00421
00422
00428 for (i = 0; i < num_possible_block_sizes; i++) {
00429 int b;
00430 for (b = 0; b < s->num_sfb[i]; b++) {
00431 int x;
00432 int offset = ((s->sfb_offsets[i][b]
00433 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00434 for (x = 0; x < num_possible_block_sizes; x++) {
00435 int v = 0;
00436 while (s->sfb_offsets[x][v + 1] << x < offset)
00437 ++v;
00438 s->sf_offsets[i][x][b] = v;
00439 }
00440 }
00441 }
00442
00444 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00445 ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00446 1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00447 / (1 << (s->bits_per_sample - 1)));
00448
00450 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00451 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00452 ff_init_ff_sine_windows(win_idx);
00453 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00454 }
00455
00457 for (i = 0; i < num_possible_block_sizes; i++) {
00458 int block_size = s->samples_per_frame >> i;
00459 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00460 / s->avctx->sample_rate;
00461 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00462 }
00463
00465 for (i = 0; i < 33; i++)
00466 sin64[i] = sin(i*M_PI / 64.0);
00467
00468 if (avctx->debug & FF_DEBUG_BITSTREAM)
00469 dump_context(s);
00470
00471 avctx->channel_layout = channel_mask;
00472
00473 avcodec_get_frame_defaults(&s->frame);
00474 avctx->coded_frame = &s->frame;
00475
00476 return 0;
00477 }
00478
00485 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00486 {
00487 int frame_len_shift = 0;
00488 int subframe_len;
00489
00491 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00492 return s->min_samples_per_subframe;
00493
00495 if (s->max_subframe_len_bit) {
00496 if (get_bits1(&s->gb))
00497 frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00498 } else
00499 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00500
00501 subframe_len = s->samples_per_frame >> frame_len_shift;
00502
00504 if (subframe_len < s->min_samples_per_subframe ||
00505 subframe_len > s->samples_per_frame) {
00506 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00507 subframe_len);
00508 return AVERROR_INVALIDDATA;
00509 }
00510 return subframe_len;
00511 }
00512
00533 static int decode_tilehdr(WMAProDecodeCtx *s)
00534 {
00535 uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };
00536 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS];
00537 int channels_for_cur_subframe = s->num_channels;
00538 int fixed_channel_layout = 0;
00539 int min_channel_len = 0;
00540 int c;
00541
00542
00543
00544
00545
00546
00547
00549 for (c = 0; c < s->num_channels; c++)
00550 s->channel[c].num_subframes = 0;
00551
00552 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00553 fixed_channel_layout = 1;
00554
00556 do {
00557 int subframe_len;
00558
00560 for (c = 0; c < s->num_channels; c++) {
00561 if (num_samples[c] == min_channel_len) {
00562 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00563 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00564 contains_subframe[c] = 1;
00565 else
00566 contains_subframe[c] = get_bits1(&s->gb);
00567 } else
00568 contains_subframe[c] = 0;
00569 }
00570
00572 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00573 return AVERROR_INVALIDDATA;
00574
00576 min_channel_len += subframe_len;
00577 for (c = 0; c < s->num_channels; c++) {
00578 WMAProChannelCtx* chan = &s->channel[c];
00579
00580 if (contains_subframe[c]) {
00581 if (chan->num_subframes >= MAX_SUBFRAMES) {
00582 av_log(s->avctx, AV_LOG_ERROR,
00583 "broken frame: num subframes > 31\n");
00584 return AVERROR_INVALIDDATA;
00585 }
00586 chan->subframe_len[chan->num_subframes] = subframe_len;
00587 num_samples[c] += subframe_len;
00588 ++chan->num_subframes;
00589 if (num_samples[c] > s->samples_per_frame) {
00590 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00591 "channel len > samples_per_frame\n");
00592 return AVERROR_INVALIDDATA;
00593 }
00594 } else if (num_samples[c] <= min_channel_len) {
00595 if (num_samples[c] < min_channel_len) {
00596 channels_for_cur_subframe = 0;
00597 min_channel_len = num_samples[c];
00598 }
00599 ++channels_for_cur_subframe;
00600 }
00601 }
00602 } while (min_channel_len < s->samples_per_frame);
00603
00604 for (c = 0; c < s->num_channels; c++) {
00605 int i;
00606 int offset = 0;
00607 for (i = 0; i < s->channel[c].num_subframes; i++) {
00608 av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00609 " len %i\n", s->frame_num, c, i,
00610 s->channel[c].subframe_len[i]);
00611 s->channel[c].subframe_offset[i] = offset;
00612 offset += s->channel[c].subframe_len[i];
00613 }
00614 }
00615
00616 return 0;
00617 }
00618
00624 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00625 WMAProChannelGrp *chgroup)
00626 {
00627 int i;
00628 int offset = 0;
00629 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00630 memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00631 s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00632
00633 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00634 rotation_offset[i] = get_bits(&s->gb, 6);
00635
00636 for (i = 0; i < chgroup->num_channels; i++)
00637 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00638 get_bits1(&s->gb) ? 1.0 : -1.0;
00639
00640 for (i = 1; i < chgroup->num_channels; i++) {
00641 int x;
00642 for (x = 0; x < i; x++) {
00643 int y;
00644 for (y = 0; y < i + 1; y++) {
00645 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00646 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00647 int n = rotation_offset[offset + x];
00648 float sinv;
00649 float cosv;
00650
00651 if (n < 32) {
00652 sinv = sin64[n];
00653 cosv = sin64[32 - n];
00654 } else {
00655 sinv = sin64[64 - n];
00656 cosv = -sin64[n - 32];
00657 }
00658
00659 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00660 (v1 * sinv) - (v2 * cosv);
00661 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00662 (v1 * cosv) + (v2 * sinv);
00663 }
00664 }
00665 offset += i;
00666 }
00667 }
00668
00674 static int decode_channel_transform(WMAProDecodeCtx* s)
00675 {
00676 int i;
00677
00678
00679
00680
00681
00683 s->num_chgroups = 0;
00684 if (s->num_channels > 1) {
00685 int remaining_channels = s->channels_for_cur_subframe;
00686
00687 if (get_bits1(&s->gb)) {
00688 av_log_ask_for_sample(s->avctx,
00689 "unsupported channel transform bit\n");
00690 return AVERROR_INVALIDDATA;
00691 }
00692
00693 for (s->num_chgroups = 0; remaining_channels &&
00694 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00695 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00696 float** channel_data = chgroup->channel_data;
00697 chgroup->num_channels = 0;
00698 chgroup->transform = 0;
00699
00701 if (remaining_channels > 2) {
00702 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00703 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00704 if (!s->channel[channel_idx].grouped
00705 && get_bits1(&s->gb)) {
00706 ++chgroup->num_channels;
00707 s->channel[channel_idx].grouped = 1;
00708 *channel_data++ = s->channel[channel_idx].coeffs;
00709 }
00710 }
00711 } else {
00712 chgroup->num_channels = remaining_channels;
00713 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00714 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00715 if (!s->channel[channel_idx].grouped)
00716 *channel_data++ = s->channel[channel_idx].coeffs;
00717 s->channel[channel_idx].grouped = 1;
00718 }
00719 }
00720
00722 if (chgroup->num_channels == 2) {
00723 if (get_bits1(&s->gb)) {
00724 if (get_bits1(&s->gb)) {
00725 av_log_ask_for_sample(s->avctx,
00726 "unsupported channel transform type\n");
00727 }
00728 } else {
00729 chgroup->transform = 1;
00730 if (s->num_channels == 2) {
00731 chgroup->decorrelation_matrix[0] = 1.0;
00732 chgroup->decorrelation_matrix[1] = -1.0;
00733 chgroup->decorrelation_matrix[2] = 1.0;
00734 chgroup->decorrelation_matrix[3] = 1.0;
00735 } else {
00737 chgroup->decorrelation_matrix[0] = 0.70703125;
00738 chgroup->decorrelation_matrix[1] = -0.70703125;
00739 chgroup->decorrelation_matrix[2] = 0.70703125;
00740 chgroup->decorrelation_matrix[3] = 0.70703125;
00741 }
00742 }
00743 } else if (chgroup->num_channels > 2) {
00744 if (get_bits1(&s->gb)) {
00745 chgroup->transform = 1;
00746 if (get_bits1(&s->gb)) {
00747 decode_decorrelation_matrix(s, chgroup);
00748 } else {
00750 if (chgroup->num_channels > 6) {
00751 av_log_ask_for_sample(s->avctx,
00752 "coupled channels > 6\n");
00753 } else {
00754 memcpy(chgroup->decorrelation_matrix,
00755 default_decorrelation[chgroup->num_channels],
00756 chgroup->num_channels * chgroup->num_channels *
00757 sizeof(*chgroup->decorrelation_matrix));
00758 }
00759 }
00760 }
00761 }
00762
00764 if (chgroup->transform) {
00765 if (!get_bits1(&s->gb)) {
00766 int i;
00768 for (i = 0; i < s->num_bands; i++) {
00769 chgroup->transform_band[i] = get_bits1(&s->gb);
00770 }
00771 } else {
00772 memset(chgroup->transform_band, 1, s->num_bands);
00773 }
00774 }
00775 remaining_channels -= chgroup->num_channels;
00776 }
00777 }
00778 return 0;
00779 }
00780
00787 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00788 {
00789
00790
00791
00792 static const uint32_t fval_tab[16] = {
00793 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00794 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00795 0x41000000, 0x41100000, 0x41200000, 0x41300000,
00796 0x41400000, 0x41500000, 0x41600000, 0x41700000,
00797 };
00798 int vlctable;
00799 VLC* vlc;
00800 WMAProChannelCtx* ci = &s->channel[c];
00801 int rl_mode = 0;
00802 int cur_coeff = 0;
00803 int num_zeros = 0;
00804 const uint16_t* run;
00805 const float* level;
00806
00807 av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00808
00809 vlctable = get_bits1(&s->gb);
00810 vlc = &coef_vlc[vlctable];
00811
00812 if (vlctable) {
00813 run = coef1_run;
00814 level = coef1_level;
00815 } else {
00816 run = coef0_run;
00817 level = coef0_level;
00818 }
00819
00822 while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00823 (cur_coeff + 3 < ci->num_vec_coeffs)) {
00824 uint32_t vals[4];
00825 int i;
00826 unsigned int idx;
00827
00828 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00829
00830 if (idx == HUFF_VEC4_SIZE - 1) {
00831 for (i = 0; i < 4; i += 2) {
00832 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00833 if (idx == HUFF_VEC2_SIZE - 1) {
00834 uint32_t v0, v1;
00835 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00836 if (v0 == HUFF_VEC1_SIZE - 1)
00837 v0 += ff_wma_get_large_val(&s->gb);
00838 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00839 if (v1 == HUFF_VEC1_SIZE - 1)
00840 v1 += ff_wma_get_large_val(&s->gb);
00841 vals[i ] = av_float2int(v0);
00842 vals[i+1] = av_float2int(v1);
00843 } else {
00844 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
00845 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00846 }
00847 }
00848 } else {
00849 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
00850 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00851 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00852 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
00853 }
00854
00856 for (i = 0; i < 4; i++) {
00857 if (vals[i]) {
00858 uint32_t sign = get_bits1(&s->gb) - 1;
00859 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
00860 num_zeros = 0;
00861 } else {
00862 ci->coeffs[cur_coeff] = 0;
00865 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00866 }
00867 ++cur_coeff;
00868 }
00869 }
00870
00872 if (cur_coeff < s->subframe_len) {
00873 memset(&ci->coeffs[cur_coeff], 0,
00874 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00875 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00876 level, run, 1, ci->coeffs,
00877 cur_coeff, s->subframe_len,
00878 s->subframe_len, s->esc_len, 0))
00879 return AVERROR_INVALIDDATA;
00880 }
00881
00882 return 0;
00883 }
00884
00890 static int decode_scale_factors(WMAProDecodeCtx* s)
00891 {
00892 int i;
00893
00898 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00899 int c = s->channel_indexes_for_cur_subframe[i];
00900 int* sf;
00901 int* sf_end;
00902 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00903 sf_end = s->channel[c].scale_factors + s->num_bands;
00904
00910 if (s->channel[c].reuse_sf) {
00911 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00912 int b;
00913 for (b = 0; b < s->num_bands; b++)
00914 s->channel[c].scale_factors[b] =
00915 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00916 }
00917
00918 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00919
00920 if (!s->channel[c].reuse_sf) {
00921 int val;
00923 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00924 val = 45 / s->channel[c].scale_factor_step;
00925 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00926 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00927 *sf = val;
00928 }
00929 } else {
00930 int i;
00932 for (i = 0; i < s->num_bands; i++) {
00933 int idx;
00934 int skip;
00935 int val;
00936 int sign;
00937
00938 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00939
00940 if (!idx) {
00941 uint32_t code = get_bits(&s->gb, 14);
00942 val = code >> 6;
00943 sign = (code & 1) - 1;
00944 skip = (code & 0x3f) >> 1;
00945 } else if (idx == 1) {
00946 break;
00947 } else {
00948 skip = scale_rl_run[idx];
00949 val = scale_rl_level[idx];
00950 sign = get_bits1(&s->gb)-1;
00951 }
00952
00953 i += skip;
00954 if (i >= s->num_bands) {
00955 av_log(s->avctx, AV_LOG_ERROR,
00956 "invalid scale factor coding\n");
00957 return AVERROR_INVALIDDATA;
00958 }
00959 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00960 }
00961 }
00963 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00964 s->channel[c].table_idx = s->table_idx;
00965 s->channel[c].reuse_sf = 1;
00966 }
00967
00969 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00970 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00971 s->channel[c].max_scale_factor =
00972 FFMAX(s->channel[c].max_scale_factor, *sf);
00973 }
00974
00975 }
00976 return 0;
00977 }
00978
00983 static void inverse_channel_transform(WMAProDecodeCtx *s)
00984 {
00985 int i;
00986
00987 for (i = 0; i < s->num_chgroups; i++) {
00988 if (s->chgroup[i].transform) {
00989 float data[WMAPRO_MAX_CHANNELS];
00990 const int num_channels = s->chgroup[i].num_channels;
00991 float** ch_data = s->chgroup[i].channel_data;
00992 float** ch_end = ch_data + num_channels;
00993 const int8_t* tb = s->chgroup[i].transform_band;
00994 int16_t* sfb;
00995
00997 for (sfb = s->cur_sfb_offsets;
00998 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00999 int y;
01000 if (*tb++ == 1) {
01002 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
01003 const float* mat = s->chgroup[i].decorrelation_matrix;
01004 const float* data_end = data + num_channels;
01005 float* data_ptr = data;
01006 float** ch;
01007
01008 for (ch = ch_data; ch < ch_end; ch++)
01009 *data_ptr++ = (*ch)[y];
01010
01011 for (ch = ch_data; ch < ch_end; ch++) {
01012 float sum = 0;
01013 data_ptr = data;
01014 while (data_ptr < data_end)
01015 sum += *data_ptr++ * *mat++;
01016
01017 (*ch)[y] = sum;
01018 }
01019 }
01020 } else if (s->num_channels == 2) {
01021 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01022 s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01023 ch_data[0] + sfb[0],
01024 181.0 / 128, len);
01025 s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01026 ch_data[1] + sfb[0],
01027 181.0 / 128, len);
01028 }
01029 }
01030 }
01031 }
01032 }
01033
01038 static void wmapro_window(WMAProDecodeCtx *s)
01039 {
01040 int i;
01041 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01042 int c = s->channel_indexes_for_cur_subframe[i];
01043 float* window;
01044 int winlen = s->channel[c].prev_block_len;
01045 float* start = s->channel[c].coeffs - (winlen >> 1);
01046
01047 if (s->subframe_len < winlen) {
01048 start += (winlen - s->subframe_len) >> 1;
01049 winlen = s->subframe_len;
01050 }
01051
01052 window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01053
01054 winlen >>= 1;
01055
01056 s->dsp.vector_fmul_window(start, start, start + winlen,
01057 window, winlen);
01058
01059 s->channel[c].prev_block_len = s->subframe_len;
01060 }
01061 }
01062
01068 static int decode_subframe(WMAProDecodeCtx *s)
01069 {
01070 int offset = s->samples_per_frame;
01071 int subframe_len = s->samples_per_frame;
01072 int i;
01073 int total_samples = s->samples_per_frame * s->num_channels;
01074 int transmit_coeffs = 0;
01075 int cur_subwoofer_cutoff;
01076
01077 s->subframe_offset = get_bits_count(&s->gb);
01078
01083 for (i = 0; i < s->num_channels; i++) {
01084 s->channel[i].grouped = 0;
01085 if (offset > s->channel[i].decoded_samples) {
01086 offset = s->channel[i].decoded_samples;
01087 subframe_len =
01088 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01089 }
01090 }
01091
01092 av_dlog(s->avctx,
01093 "processing subframe with offset %i len %i\n", offset, subframe_len);
01094
01096 s->channels_for_cur_subframe = 0;
01097 for (i = 0; i < s->num_channels; i++) {
01098 const int cur_subframe = s->channel[i].cur_subframe;
01100 total_samples -= s->channel[i].decoded_samples;
01101
01103 if (offset == s->channel[i].decoded_samples &&
01104 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01105 total_samples -= s->channel[i].subframe_len[cur_subframe];
01106 s->channel[i].decoded_samples +=
01107 s->channel[i].subframe_len[cur_subframe];
01108 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01109 ++s->channels_for_cur_subframe;
01110 }
01111 }
01112
01115 if (!total_samples)
01116 s->parsed_all_subframes = 1;
01117
01118
01119 av_dlog(s->avctx, "subframe is part of %i channels\n",
01120 s->channels_for_cur_subframe);
01121
01123 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
01124 s->num_bands = s->num_sfb[s->table_idx];
01125 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
01126 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01127
01129 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01130 int c = s->channel_indexes_for_cur_subframe[i];
01131
01132 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01133 + offset];
01134 }
01135
01136 s->subframe_len = subframe_len;
01137 s->esc_len = av_log2(s->subframe_len - 1) + 1;
01138
01140 if (get_bits1(&s->gb)) {
01141 int num_fill_bits;
01142 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01143 int len = get_bits(&s->gb, 4);
01144 num_fill_bits = (len ? get_bits(&s->gb, len) : 0) + 1;
01145 }
01146
01147 if (num_fill_bits >= 0) {
01148 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01149 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01150 return AVERROR_INVALIDDATA;
01151 }
01152
01153 skip_bits_long(&s->gb, num_fill_bits);
01154 }
01155 }
01156
01158 if (get_bits1(&s->gb)) {
01159 av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01160 return AVERROR_INVALIDDATA;
01161 }
01162
01163
01164 if (decode_channel_transform(s) < 0)
01165 return AVERROR_INVALIDDATA;
01166
01167
01168 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01169 int c = s->channel_indexes_for_cur_subframe[i];
01170 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01171 transmit_coeffs = 1;
01172 }
01173
01174 av_assert0(s->subframe_len <= WMAPRO_BLOCK_MAX_SIZE);
01175 if (transmit_coeffs) {
01176 int step;
01177 int quant_step = 90 * s->bits_per_sample >> 4;
01178
01180 if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01181 int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01182 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01183 int c = s->channel_indexes_for_cur_subframe[i];
01184 int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01185 if (num_vec_coeffs > s->subframe_len) {
01186 av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
01187 return AVERROR_INVALIDDATA;
01188 }
01189 s->channel[c].num_vec_coeffs = num_vec_coeffs;
01190 }
01191 } else {
01192 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01193 int c = s->channel_indexes_for_cur_subframe[i];
01194 s->channel[c].num_vec_coeffs = s->subframe_len;
01195 }
01196 }
01198 step = get_sbits(&s->gb, 6);
01199 quant_step += step;
01200 if (step == -32 || step == 31) {
01201 const int sign = (step == 31) - 1;
01202 int quant = 0;
01203 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01204 (step = get_bits(&s->gb, 5)) == 31) {
01205 quant += 31;
01206 }
01207 quant_step += ((quant + step) ^ sign) - sign;
01208 }
01209 if (quant_step < 0) {
01210 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01211 }
01212
01215 if (s->channels_for_cur_subframe == 1) {
01216 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01217 } else {
01218 int modifier_len = get_bits(&s->gb, 3);
01219 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01220 int c = s->channel_indexes_for_cur_subframe[i];
01221 s->channel[c].quant_step = quant_step;
01222 if (get_bits1(&s->gb)) {
01223 if (modifier_len) {
01224 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01225 } else
01226 ++s->channel[c].quant_step;
01227 }
01228 }
01229 }
01230
01232 if (decode_scale_factors(s) < 0)
01233 return AVERROR_INVALIDDATA;
01234 }
01235
01236 av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01237 get_bits_count(&s->gb) - s->subframe_offset);
01238
01240 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01241 int c = s->channel_indexes_for_cur_subframe[i];
01242 if (s->channel[c].transmit_coefs &&
01243 get_bits_count(&s->gb) < s->num_saved_bits) {
01244 decode_coeffs(s, c);
01245 } else
01246 memset(s->channel[c].coeffs, 0,
01247 sizeof(*s->channel[c].coeffs) * subframe_len);
01248 }
01249
01250 av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01251 get_bits_count(&s->gb) - s->subframe_offset);
01252
01253 if (transmit_coeffs) {
01254 FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01256 inverse_channel_transform(s);
01257 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01258 int c = s->channel_indexes_for_cur_subframe[i];
01259 const int* sf = s->channel[c].scale_factors;
01260 int b;
01261
01262 if (c == s->lfe_channel)
01263 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01264 (subframe_len - cur_subwoofer_cutoff));
01265
01267 for (b = 0; b < s->num_bands; b++) {
01268 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01269 const int exp = s->channel[c].quant_step -
01270 (s->channel[c].max_scale_factor - *sf++) *
01271 s->channel[c].scale_factor_step;
01272 const float quant = pow(10.0, exp / 20.0);
01273 int start = s->cur_sfb_offsets[b];
01274 s->dsp.vector_fmul_scalar(s->tmp + start,
01275 s->channel[c].coeffs + start,
01276 quant, end - start);
01277 }
01278
01280 mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01281 }
01282 }
01283
01285 wmapro_window(s);
01286
01288 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01289 int c = s->channel_indexes_for_cur_subframe[i];
01290 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01291 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01292 return AVERROR_INVALIDDATA;
01293 }
01294 ++s->channel[c].cur_subframe;
01295 }
01296
01297 return 0;
01298 }
01299
01306 static int decode_frame(WMAProDecodeCtx *s, int *got_frame_ptr)
01307 {
01308 AVCodecContext *avctx = s->avctx;
01309 GetBitContext* gb = &s->gb;
01310 int more_frames = 0;
01311 int len = 0;
01312 int i, ret;
01313 const float *out_ptr[WMAPRO_MAX_CHANNELS];
01314 float *samples;
01315
01317 if (s->len_prefix)
01318 len = get_bits(gb, s->log2_frame_size);
01319
01320 av_dlog(s->avctx, "decoding frame with length %x\n", len);
01321
01323 if (decode_tilehdr(s)) {
01324 s->packet_loss = 1;
01325 return 0;
01326 }
01327
01329 if (s->num_channels > 1 && get_bits1(gb)) {
01330 if (get_bits1(gb)) {
01331 for (i = 0; i < s->num_channels * s->num_channels; i++)
01332 skip_bits(gb, 4);
01333 }
01334 }
01335
01337 if (s->dynamic_range_compression) {
01338 s->drc_gain = get_bits(gb, 8);
01339 av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01340 }
01341
01344 if (get_bits1(gb)) {
01345 int av_unused skip;
01346
01348 if (get_bits1(gb)) {
01349 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01350 av_dlog(s->avctx, "start skip: %i\n", skip);
01351 }
01352
01354 if (get_bits1(gb)) {
01355 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01356 av_dlog(s->avctx, "end skip: %i\n", skip);
01357 }
01358
01359 }
01360
01361 av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01362 get_bits_count(gb) - s->frame_offset);
01363
01365 s->parsed_all_subframes = 0;
01366 for (i = 0; i < s->num_channels; i++) {
01367 s->channel[i].decoded_samples = 0;
01368 s->channel[i].cur_subframe = 0;
01369 s->channel[i].reuse_sf = 0;
01370 }
01371
01373 while (!s->parsed_all_subframes) {
01374 if (decode_subframe(s) < 0) {
01375 s->packet_loss = 1;
01376 return 0;
01377 }
01378 }
01379
01380
01381 s->frame.nb_samples = s->samples_per_frame;
01382 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01383 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01384 s->packet_loss = 1;
01385 return 0;
01386 }
01387 samples = (float *)s->frame.data[0];
01388
01390 for (i = 0; i < s->num_channels; i++)
01391 out_ptr[i] = s->channel[i].out;
01392 s->fmt_conv.float_interleave(samples, out_ptr, s->samples_per_frame,
01393 s->num_channels);
01394
01395 for (i = 0; i < s->num_channels; i++) {
01397 memcpy(&s->channel[i].out[0],
01398 &s->channel[i].out[s->samples_per_frame],
01399 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01400 }
01401
01402 if (s->skip_frame) {
01403 s->skip_frame = 0;
01404 *got_frame_ptr = 0;
01405 } else {
01406 *got_frame_ptr = 1;
01407 }
01408
01409 if (s->len_prefix) {
01410 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01412 av_log(s->avctx, AV_LOG_ERROR,
01413 "frame[%i] would have to skip %i bits\n", s->frame_num,
01414 len - (get_bits_count(gb) - s->frame_offset) - 1);
01415 s->packet_loss = 1;
01416 return 0;
01417 }
01418
01420 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01421 } else {
01422 while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01423 }
01424 }
01425
01427 more_frames = get_bits1(gb);
01428
01429 ++s->frame_num;
01430 return more_frames;
01431 }
01432
01439 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01440 {
01441 return s->buf_bit_size - get_bits_count(gb);
01442 }
01443
01451 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01452 int append)
01453 {
01454 int buflen;
01455
01460 if (!append) {
01461 s->frame_offset = get_bits_count(gb) & 7;
01462 s->num_saved_bits = s->frame_offset;
01463 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01464 }
01465
01466 buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
01467
01468 if (len <= 0 || buflen > MAX_FRAMESIZE) {
01469 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01470 s->packet_loss = 1;
01471 return;
01472 }
01473
01474 s->num_saved_bits += len;
01475 if (!append) {
01476 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01477 s->num_saved_bits);
01478 } else {
01479 int align = 8 - (get_bits_count(gb) & 7);
01480 align = FFMIN(align, len);
01481 put_bits(&s->pb, align, get_bits(gb, align));
01482 len -= align;
01483 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01484 }
01485 skip_bits_long(gb, len);
01486
01487 {
01488 PutBitContext tmp = s->pb;
01489 flush_put_bits(&tmp);
01490 }
01491
01492 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01493 skip_bits(&s->gb, s->frame_offset);
01494 }
01495
01503 static int decode_packet(AVCodecContext *avctx, void *data,
01504 int *got_frame_ptr, AVPacket* avpkt)
01505 {
01506 WMAProDecodeCtx *s = avctx->priv_data;
01507 GetBitContext* gb = &s->pgb;
01508 const uint8_t* buf = avpkt->data;
01509 int buf_size = avpkt->size;
01510 int num_bits_prev_frame;
01511 int packet_sequence_number;
01512
01513 *got_frame_ptr = 0;
01514
01515 if (s->packet_done || s->packet_loss) {
01516 s->packet_done = 0;
01517
01519 if (buf_size < avctx->block_align)
01520 return 0;
01521
01522 s->next_packet_start = buf_size - avctx->block_align;
01523 buf_size = avctx->block_align;
01524 s->buf_bit_size = buf_size << 3;
01525
01527 init_get_bits(gb, buf, s->buf_bit_size);
01528 packet_sequence_number = get_bits(gb, 4);
01529 skip_bits(gb, 2);
01530
01532 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01533 av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01534 num_bits_prev_frame);
01535
01537 if (!s->packet_loss &&
01538 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01539 s->packet_loss = 1;
01540 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01541 s->packet_sequence_number, packet_sequence_number);
01542 }
01543 s->packet_sequence_number = packet_sequence_number;
01544
01545 if (num_bits_prev_frame > 0) {
01546 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01547 if (num_bits_prev_frame >= remaining_packet_bits) {
01548 num_bits_prev_frame = remaining_packet_bits;
01549 s->packet_done = 1;
01550 }
01551
01554 save_bits(s, gb, num_bits_prev_frame, 1);
01555 av_dlog(avctx, "accumulated %x bits of frame data\n",
01556 s->num_saved_bits - s->frame_offset);
01557
01559 if (!s->packet_loss)
01560 decode_frame(s, got_frame_ptr);
01561 } else if (s->num_saved_bits - s->frame_offset) {
01562 av_dlog(avctx, "ignoring %x previously saved bits\n",
01563 s->num_saved_bits - s->frame_offset);
01564 }
01565
01566 if (s->packet_loss) {
01570 s->num_saved_bits = 0;
01571 s->packet_loss = 0;
01572 }
01573
01574 } else {
01575 int frame_size;
01576 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01577 init_get_bits(gb, avpkt->data, s->buf_bit_size);
01578 skip_bits(gb, s->packet_offset);
01579 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01580 (frame_size = show_bits(gb, s->log2_frame_size)) &&
01581 frame_size <= remaining_bits(s, gb)) {
01582 save_bits(s, gb, frame_size, 0);
01583 s->packet_done = !decode_frame(s, got_frame_ptr);
01584 } else if (!s->len_prefix
01585 && s->num_saved_bits > get_bits_count(&s->gb)) {
01593 s->packet_done = !decode_frame(s, got_frame_ptr);
01594 } else
01595 s->packet_done = 1;
01596 }
01597
01598 if (s->packet_done && !s->packet_loss &&
01599 remaining_bits(s, gb) > 0) {
01602 save_bits(s, gb, remaining_bits(s, gb), 0);
01603 }
01604
01605 s->packet_offset = get_bits_count(gb) & 7;
01606 if (s->packet_loss)
01607 return AVERROR_INVALIDDATA;
01608
01609 if (*got_frame_ptr)
01610 *(AVFrame *)data = s->frame;
01611
01612 return get_bits_count(gb) >> 3;
01613 }
01614
01619 static void flush(AVCodecContext *avctx)
01620 {
01621 WMAProDecodeCtx *s = avctx->priv_data;
01622 int i;
01625 for (i = 0; i < s->num_channels; i++)
01626 memset(s->channel[i].out, 0, s->samples_per_frame *
01627 sizeof(*s->channel[i].out));
01628 s->packet_loss = 1;
01629 }
01630
01631
01635 AVCodec ff_wmapro_decoder = {
01636 .name = "wmapro",
01637 .type = AVMEDIA_TYPE_AUDIO,
01638 .id = AV_CODEC_ID_WMAPRO,
01639 .priv_data_size = sizeof(WMAProDecodeCtx),
01640 .init = decode_init,
01641 .close = decode_end,
01642 .decode = decode_packet,
01643 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01644 .flush = flush,
01645 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01646 };