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/intreadwrite.h"
00090 #include "avcodec.h"
00091 #include "internal.h"
00092 #include "get_bits.h"
00093 #include "put_bits.h"
00094 #include "wmaprodata.h"
00095 #include "dsputil.h"
00096 #include "fmtconvert.h"
00097 #include "sinewin.h"
00098 #include "wma.h"
00099
00101 #define WMAPRO_MAX_CHANNELS 8
00102 #define MAX_SUBFRAMES 32
00103 #define MAX_BANDS 29
00104 #define MAX_FRAMESIZE 32768
00105
00106 #define WMAPRO_BLOCK_MIN_BITS 6
00107 #define WMAPRO_BLOCK_MAX_BITS 12
00108 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
00109 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
00110
00111
00112 #define VLCBITS 9
00113 #define SCALEVLCBITS 8
00114 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00115 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00116 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00117 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00118 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00119
00120 static VLC sf_vlc;
00121 static VLC sf_rl_vlc;
00122 static VLC vec4_vlc;
00123 static VLC vec2_vlc;
00124 static VLC vec1_vlc;
00125 static VLC coef_vlc[2];
00126 static float sin64[33];
00127
00131 typedef struct {
00132 int16_t prev_block_len;
00133 uint8_t transmit_coefs;
00134 uint8_t num_subframes;
00135 uint16_t subframe_len[MAX_SUBFRAMES];
00136 uint16_t subframe_offset[MAX_SUBFRAMES];
00137 uint8_t cur_subframe;
00138 uint16_t decoded_samples;
00139 uint8_t grouped;
00140 int quant_step;
00141 int8_t reuse_sf;
00142 int8_t scale_factor_step;
00143 int max_scale_factor;
00144 int saved_scale_factors[2][MAX_BANDS];
00145 int8_t scale_factor_idx;
00146 int* scale_factors;
00147 uint8_t table_idx;
00148 float* coeffs;
00149 uint16_t num_vec_coeffs;
00150 DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2];
00151 } WMAProChannelCtx;
00152
00156 typedef struct {
00157 uint8_t num_channels;
00158 int8_t transform;
00159 int8_t transform_band[MAX_BANDS];
00160 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00161 float* channel_data[WMAPRO_MAX_CHANNELS];
00162 } WMAProChannelGrp;
00163
00167 typedef struct WMAProDecodeCtx {
00168
00169 AVCodecContext* avctx;
00170 AVFrame frame;
00171 DSPContext dsp;
00172 FmtConvertContext fmt_conv;
00173 uint8_t frame_data[MAX_FRAMESIZE +
00174 FF_INPUT_BUFFER_PADDING_SIZE];
00175 PutBitContext pb;
00176 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES];
00177 DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE];
00178 float* windows[WMAPRO_BLOCK_SIZES];
00179
00180
00181 uint32_t decode_flags;
00182 uint8_t len_prefix;
00183 uint8_t dynamic_range_compression;
00184 uint8_t bits_per_sample;
00185 uint16_t samples_per_frame;
00186 uint16_t log2_frame_size;
00187 int8_t num_channels;
00188 int8_t lfe_channel;
00189 uint8_t max_num_subframes;
00190 uint8_t subframe_len_bits;
00191 uint8_t max_subframe_len_bit;
00192 uint16_t min_samples_per_subframe;
00193 int8_t num_sfb[WMAPRO_BLOCK_SIZES];
00194 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];
00195 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS];
00196 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES];
00197
00198
00199 GetBitContext pgb;
00200 int next_packet_start;
00201 uint8_t packet_offset;
00202 uint8_t packet_sequence_number;
00203 int num_saved_bits;
00204 int frame_offset;
00205 int subframe_offset;
00206 uint8_t packet_loss;
00207 uint8_t packet_done;
00208
00209
00210 uint32_t frame_num;
00211 GetBitContext gb;
00212 int buf_bit_size;
00213 uint8_t drc_gain;
00214 int8_t skip_frame;
00215 int8_t parsed_all_subframes;
00216
00217
00218 int16_t subframe_len;
00219 int8_t channels_for_cur_subframe;
00220 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00221 int8_t num_bands;
00222 int8_t transmit_num_vec_coeffs;
00223 int16_t* cur_sfb_offsets;
00224 uint8_t table_idx;
00225 int8_t esc_len;
00226
00227 uint8_t num_chgroups;
00228 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];
00229
00230 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];
00231 } WMAProDecodeCtx;
00232
00233
00238 static void av_cold dump_context(WMAProDecodeCtx *s)
00239 {
00240 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00241 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00242
00243 PRINT("ed sample bit depth", s->bits_per_sample);
00244 PRINT_HEX("ed decode flags", s->decode_flags);
00245 PRINT("samples per frame", s->samples_per_frame);
00246 PRINT("log2 frame size", s->log2_frame_size);
00247 PRINT("max num subframes", s->max_num_subframes);
00248 PRINT("len prefix", s->len_prefix);
00249 PRINT("num channels", s->num_channels);
00250 }
00251
00257 static av_cold int decode_end(AVCodecContext *avctx)
00258 {
00259 WMAProDecodeCtx *s = avctx->priv_data;
00260 int i;
00261
00262 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00263 ff_mdct_end(&s->mdct_ctx[i]);
00264
00265 return 0;
00266 }
00267
00273 static av_cold int decode_init(AVCodecContext *avctx)
00274 {
00275 WMAProDecodeCtx *s = avctx->priv_data;
00276 uint8_t *edata_ptr = avctx->extradata;
00277 unsigned int channel_mask;
00278 int i;
00279 int log2_max_num_subframes;
00280 int num_possible_block_sizes;
00281
00282 s->avctx = avctx;
00283 dsputil_init(&s->dsp, avctx);
00284 ff_fmt_convert_init(&s->fmt_conv, avctx);
00285 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00286
00287 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00288
00289 if (avctx->extradata_size >= 18) {
00290 s->decode_flags = AV_RL16(edata_ptr+14);
00291 channel_mask = AV_RL32(edata_ptr+2);
00292 s->bits_per_sample = AV_RL16(edata_ptr);
00294 for (i = 0; i < avctx->extradata_size; i++)
00295 av_dlog(avctx, "[%x] ", avctx->extradata[i]);
00296 av_dlog(avctx, "\n");
00297
00298 } else {
00299 av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00300 return AVERROR_INVALIDDATA;
00301 }
00302
00304 s->log2_frame_size = av_log2(avctx->block_align) + 4;
00305
00307 s->skip_frame = 1;
00308 s->packet_loss = 1;
00309 s->len_prefix = (s->decode_flags & 0x40);
00310
00312 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00313 3, s->decode_flags);
00314
00316 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
00317 s->max_num_subframes = 1 << log2_max_num_subframes;
00318 if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
00319 s->max_subframe_len_bit = 1;
00320 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00321
00322 num_possible_block_sizes = log2_max_num_subframes + 1;
00323 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
00324 s->dynamic_range_compression = (s->decode_flags & 0x80);
00325
00326 if (s->max_num_subframes > MAX_SUBFRAMES) {
00327 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00328 s->max_num_subframes);
00329 return AVERROR_INVALIDDATA;
00330 }
00331
00332 s->num_channels = avctx->channels;
00333
00334 if (s->num_channels < 0) {
00335 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00336 return AVERROR_INVALIDDATA;
00337 } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00338 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00339 return AVERROR_PATCHWELCOME;
00340 }
00341
00343 for (i = 0; i < s->num_channels; i++)
00344 s->channel[i].prev_block_len = s->samples_per_frame;
00345
00347 s->lfe_channel = -1;
00348
00349 if (channel_mask & 8) {
00350 unsigned int mask;
00351 for (mask = 1; mask < 16; mask <<= 1) {
00352 if (channel_mask & mask)
00353 ++s->lfe_channel;
00354 }
00355 }
00356
00357 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00358 scale_huffbits, 1, 1,
00359 scale_huffcodes, 2, 2, 616);
00360
00361 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00362 scale_rl_huffbits, 1, 1,
00363 scale_rl_huffcodes, 4, 4, 1406);
00364
00365 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00366 coef0_huffbits, 1, 1,
00367 coef0_huffcodes, 4, 4, 2108);
00368
00369 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00370 coef1_huffbits, 1, 1,
00371 coef1_huffcodes, 4, 4, 3912);
00372
00373 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00374 vec4_huffbits, 1, 1,
00375 vec4_huffcodes, 2, 2, 604);
00376
00377 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00378 vec2_huffbits, 1, 1,
00379 vec2_huffcodes, 2, 2, 562);
00380
00381 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00382 vec1_huffbits, 1, 1,
00383 vec1_huffcodes, 2, 2, 562);
00384
00387 for (i = 0; i < num_possible_block_sizes; i++) {
00388 int subframe_len = s->samples_per_frame >> i;
00389 int x;
00390 int band = 1;
00391
00392 s->sfb_offsets[i][0] = 0;
00393
00394 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00395 int offset = (subframe_len * 2 * critical_freq[x])
00396 / s->avctx->sample_rate + 2;
00397 offset &= ~3;
00398 if (offset > s->sfb_offsets[i][band - 1])
00399 s->sfb_offsets[i][band++] = offset;
00400 }
00401 s->sfb_offsets[i][band - 1] = subframe_len;
00402 s->num_sfb[i] = band - 1;
00403 }
00404
00405
00411 for (i = 0; i < num_possible_block_sizes; i++) {
00412 int b;
00413 for (b = 0; b < s->num_sfb[i]; b++) {
00414 int x;
00415 int offset = ((s->sfb_offsets[i][b]
00416 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00417 for (x = 0; x < num_possible_block_sizes; x++) {
00418 int v = 0;
00419 while (s->sfb_offsets[x][v + 1] << x < offset)
00420 ++v;
00421 s->sf_offsets[i][x][b] = v;
00422 }
00423 }
00424 }
00425
00427 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00428 ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00429 1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00430 / (1 << (s->bits_per_sample - 1)));
00431
00433 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00434 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00435 ff_init_ff_sine_windows(win_idx);
00436 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00437 }
00438
00440 for (i = 0; i < num_possible_block_sizes; i++) {
00441 int block_size = s->samples_per_frame >> i;
00442 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00443 / s->avctx->sample_rate;
00444 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00445 }
00446
00448 for (i = 0; i < 33; i++)
00449 sin64[i] = sin(i*M_PI / 64.0);
00450
00451 if (avctx->debug & FF_DEBUG_BITSTREAM)
00452 dump_context(s);
00453
00454 avctx->channel_layout = channel_mask;
00455
00456 avcodec_get_frame_defaults(&s->frame);
00457 avctx->coded_frame = &s->frame;
00458
00459 return 0;
00460 }
00461
00468 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00469 {
00470 int frame_len_shift = 0;
00471 int subframe_len;
00472
00474 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00475 return s->min_samples_per_subframe;
00476
00478 if (s->max_subframe_len_bit) {
00479 if (get_bits1(&s->gb))
00480 frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00481 } else
00482 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00483
00484 subframe_len = s->samples_per_frame >> frame_len_shift;
00485
00487 if (subframe_len < s->min_samples_per_subframe ||
00488 subframe_len > s->samples_per_frame) {
00489 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00490 subframe_len);
00491 return AVERROR_INVALIDDATA;
00492 }
00493 return subframe_len;
00494 }
00495
00516 static int decode_tilehdr(WMAProDecodeCtx *s)
00517 {
00518 uint16_t num_samples[WMAPRO_MAX_CHANNELS];
00519 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS];
00520 int channels_for_cur_subframe = s->num_channels;
00521 int fixed_channel_layout = 0;
00522 int min_channel_len = 0;
00523 int c;
00524
00525
00526
00527
00528
00529
00530
00532 for (c = 0; c < s->num_channels; c++)
00533 s->channel[c].num_subframes = 0;
00534
00535 memset(num_samples, 0, sizeof(num_samples));
00536
00537 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00538 fixed_channel_layout = 1;
00539
00541 do {
00542 int subframe_len;
00543
00545 for (c = 0; c < s->num_channels; c++) {
00546 if (num_samples[c] == min_channel_len) {
00547 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00548 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00549 contains_subframe[c] = 1;
00550 else
00551 contains_subframe[c] = get_bits1(&s->gb);
00552 } else
00553 contains_subframe[c] = 0;
00554 }
00555
00557 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00558 return AVERROR_INVALIDDATA;
00559
00561 min_channel_len += subframe_len;
00562 for (c = 0; c < s->num_channels; c++) {
00563 WMAProChannelCtx* chan = &s->channel[c];
00564
00565 if (contains_subframe[c]) {
00566 if (chan->num_subframes >= MAX_SUBFRAMES) {
00567 av_log(s->avctx, AV_LOG_ERROR,
00568 "broken frame: num subframes > 31\n");
00569 return AVERROR_INVALIDDATA;
00570 }
00571 chan->subframe_len[chan->num_subframes] = subframe_len;
00572 num_samples[c] += subframe_len;
00573 ++chan->num_subframes;
00574 if (num_samples[c] > s->samples_per_frame) {
00575 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00576 "channel len > samples_per_frame\n");
00577 return AVERROR_INVALIDDATA;
00578 }
00579 } else if (num_samples[c] <= min_channel_len) {
00580 if (num_samples[c] < min_channel_len) {
00581 channels_for_cur_subframe = 0;
00582 min_channel_len = num_samples[c];
00583 }
00584 ++channels_for_cur_subframe;
00585 }
00586 }
00587 } while (min_channel_len < s->samples_per_frame);
00588
00589 for (c = 0; c < s->num_channels; c++) {
00590 int i;
00591 int offset = 0;
00592 for (i = 0; i < s->channel[c].num_subframes; i++) {
00593 av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00594 " len %i\n", s->frame_num, c, i,
00595 s->channel[c].subframe_len[i]);
00596 s->channel[c].subframe_offset[i] = offset;
00597 offset += s->channel[c].subframe_len[i];
00598 }
00599 }
00600
00601 return 0;
00602 }
00603
00609 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00610 WMAProChannelGrp *chgroup)
00611 {
00612 int i;
00613 int offset = 0;
00614 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00615 memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00616 s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00617
00618 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00619 rotation_offset[i] = get_bits(&s->gb, 6);
00620
00621 for (i = 0; i < chgroup->num_channels; i++)
00622 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00623 get_bits1(&s->gb) ? 1.0 : -1.0;
00624
00625 for (i = 1; i < chgroup->num_channels; i++) {
00626 int x;
00627 for (x = 0; x < i; x++) {
00628 int y;
00629 for (y = 0; y < i + 1; y++) {
00630 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00631 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00632 int n = rotation_offset[offset + x];
00633 float sinv;
00634 float cosv;
00635
00636 if (n < 32) {
00637 sinv = sin64[n];
00638 cosv = sin64[32 - n];
00639 } else {
00640 sinv = sin64[64 - n];
00641 cosv = -sin64[n - 32];
00642 }
00643
00644 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00645 (v1 * sinv) - (v2 * cosv);
00646 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00647 (v1 * cosv) + (v2 * sinv);
00648 }
00649 }
00650 offset += i;
00651 }
00652 }
00653
00659 static int decode_channel_transform(WMAProDecodeCtx* s)
00660 {
00661 int i;
00662
00663
00664
00665
00666
00668 s->num_chgroups = 0;
00669 if (s->num_channels > 1) {
00670 int remaining_channels = s->channels_for_cur_subframe;
00671
00672 if (get_bits1(&s->gb)) {
00673 av_log_ask_for_sample(s->avctx,
00674 "unsupported channel transform bit\n");
00675 return AVERROR_INVALIDDATA;
00676 }
00677
00678 for (s->num_chgroups = 0; remaining_channels &&
00679 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00680 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00681 float** channel_data = chgroup->channel_data;
00682 chgroup->num_channels = 0;
00683 chgroup->transform = 0;
00684
00686 if (remaining_channels > 2) {
00687 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00688 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00689 if (!s->channel[channel_idx].grouped
00690 && get_bits1(&s->gb)) {
00691 ++chgroup->num_channels;
00692 s->channel[channel_idx].grouped = 1;
00693 *channel_data++ = s->channel[channel_idx].coeffs;
00694 }
00695 }
00696 } else {
00697 chgroup->num_channels = remaining_channels;
00698 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00699 int channel_idx = s->channel_indexes_for_cur_subframe[i];
00700 if (!s->channel[channel_idx].grouped)
00701 *channel_data++ = s->channel[channel_idx].coeffs;
00702 s->channel[channel_idx].grouped = 1;
00703 }
00704 }
00705
00707 if (chgroup->num_channels == 2) {
00708 if (get_bits1(&s->gb)) {
00709 if (get_bits1(&s->gb)) {
00710 av_log_ask_for_sample(s->avctx,
00711 "unsupported channel transform type\n");
00712 }
00713 } else {
00714 chgroup->transform = 1;
00715 if (s->num_channels == 2) {
00716 chgroup->decorrelation_matrix[0] = 1.0;
00717 chgroup->decorrelation_matrix[1] = -1.0;
00718 chgroup->decorrelation_matrix[2] = 1.0;
00719 chgroup->decorrelation_matrix[3] = 1.0;
00720 } else {
00722 chgroup->decorrelation_matrix[0] = 0.70703125;
00723 chgroup->decorrelation_matrix[1] = -0.70703125;
00724 chgroup->decorrelation_matrix[2] = 0.70703125;
00725 chgroup->decorrelation_matrix[3] = 0.70703125;
00726 }
00727 }
00728 } else if (chgroup->num_channels > 2) {
00729 if (get_bits1(&s->gb)) {
00730 chgroup->transform = 1;
00731 if (get_bits1(&s->gb)) {
00732 decode_decorrelation_matrix(s, chgroup);
00733 } else {
00735 if (chgroup->num_channels > 6) {
00736 av_log_ask_for_sample(s->avctx,
00737 "coupled channels > 6\n");
00738 } else {
00739 memcpy(chgroup->decorrelation_matrix,
00740 default_decorrelation[chgroup->num_channels],
00741 chgroup->num_channels * chgroup->num_channels *
00742 sizeof(*chgroup->decorrelation_matrix));
00743 }
00744 }
00745 }
00746 }
00747
00749 if (chgroup->transform) {
00750 if (!get_bits1(&s->gb)) {
00751 int i;
00753 for (i = 0; i < s->num_bands; i++) {
00754 chgroup->transform_band[i] = get_bits1(&s->gb);
00755 }
00756 } else {
00757 memset(chgroup->transform_band, 1, s->num_bands);
00758 }
00759 }
00760 remaining_channels -= chgroup->num_channels;
00761 }
00762 }
00763 return 0;
00764 }
00765
00772 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00773 {
00774
00775
00776
00777 static const uint32_t fval_tab[16] = {
00778 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00779 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00780 0x41000000, 0x41100000, 0x41200000, 0x41300000,
00781 0x41400000, 0x41500000, 0x41600000, 0x41700000,
00782 };
00783 int vlctable;
00784 VLC* vlc;
00785 WMAProChannelCtx* ci = &s->channel[c];
00786 int rl_mode = 0;
00787 int cur_coeff = 0;
00788 int num_zeros = 0;
00789 const uint16_t* run;
00790 const float* level;
00791
00792 av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00793
00794 vlctable = get_bits1(&s->gb);
00795 vlc = &coef_vlc[vlctable];
00796
00797 if (vlctable) {
00798 run = coef1_run;
00799 level = coef1_level;
00800 } else {
00801 run = coef0_run;
00802 level = coef0_level;
00803 }
00804
00807 while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00808 (cur_coeff + 3 < ci->num_vec_coeffs)) {
00809 uint32_t vals[4];
00810 int i;
00811 unsigned int idx;
00812
00813 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00814
00815 if (idx == HUFF_VEC4_SIZE - 1) {
00816 for (i = 0; i < 4; i += 2) {
00817 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00818 if (idx == HUFF_VEC2_SIZE - 1) {
00819 uint32_t v0, v1;
00820 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00821 if (v0 == HUFF_VEC1_SIZE - 1)
00822 v0 += ff_wma_get_large_val(&s->gb);
00823 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00824 if (v1 == HUFF_VEC1_SIZE - 1)
00825 v1 += ff_wma_get_large_val(&s->gb);
00826 vals[i ] = ((av_alias32){ .f32 = v0 }).u32;
00827 vals[i+1] = ((av_alias32){ .f32 = v1 }).u32;
00828 } else {
00829 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
00830 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00831 }
00832 }
00833 } else {
00834 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
00835 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00836 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00837 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
00838 }
00839
00841 for (i = 0; i < 4; i++) {
00842 if (vals[i]) {
00843 uint32_t sign = get_bits1(&s->gb) - 1;
00844 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
00845 num_zeros = 0;
00846 } else {
00847 ci->coeffs[cur_coeff] = 0;
00850 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00851 }
00852 ++cur_coeff;
00853 }
00854 }
00855
00857 if (cur_coeff < s->subframe_len) {
00858 memset(&ci->coeffs[cur_coeff], 0,
00859 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00860 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00861 level, run, 1, ci->coeffs,
00862 cur_coeff, s->subframe_len,
00863 s->subframe_len, s->esc_len, 0))
00864 return AVERROR_INVALIDDATA;
00865 }
00866
00867 return 0;
00868 }
00869
00875 static int decode_scale_factors(WMAProDecodeCtx* s)
00876 {
00877 int i;
00878
00883 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00884 int c = s->channel_indexes_for_cur_subframe[i];
00885 int* sf;
00886 int* sf_end;
00887 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00888 sf_end = s->channel[c].scale_factors + s->num_bands;
00889
00895 if (s->channel[c].reuse_sf) {
00896 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00897 int b;
00898 for (b = 0; b < s->num_bands; b++)
00899 s->channel[c].scale_factors[b] =
00900 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00901 }
00902
00903 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00904
00905 if (!s->channel[c].reuse_sf) {
00906 int val;
00908 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00909 val = 45 / s->channel[c].scale_factor_step;
00910 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00911 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00912 *sf = val;
00913 }
00914 } else {
00915 int i;
00917 for (i = 0; i < s->num_bands; i++) {
00918 int idx;
00919 int skip;
00920 int val;
00921 int sign;
00922
00923 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00924
00925 if (!idx) {
00926 uint32_t code = get_bits(&s->gb, 14);
00927 val = code >> 6;
00928 sign = (code & 1) - 1;
00929 skip = (code & 0x3f) >> 1;
00930 } else if (idx == 1) {
00931 break;
00932 } else {
00933 skip = scale_rl_run[idx];
00934 val = scale_rl_level[idx];
00935 sign = get_bits1(&s->gb)-1;
00936 }
00937
00938 i += skip;
00939 if (i >= s->num_bands) {
00940 av_log(s->avctx, AV_LOG_ERROR,
00941 "invalid scale factor coding\n");
00942 return AVERROR_INVALIDDATA;
00943 }
00944 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00945 }
00946 }
00948 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00949 s->channel[c].table_idx = s->table_idx;
00950 s->channel[c].reuse_sf = 1;
00951 }
00952
00954 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00955 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00956 s->channel[c].max_scale_factor =
00957 FFMAX(s->channel[c].max_scale_factor, *sf);
00958 }
00959
00960 }
00961 return 0;
00962 }
00963
00968 static void inverse_channel_transform(WMAProDecodeCtx *s)
00969 {
00970 int i;
00971
00972 for (i = 0; i < s->num_chgroups; i++) {
00973 if (s->chgroup[i].transform) {
00974 float data[WMAPRO_MAX_CHANNELS];
00975 const int num_channels = s->chgroup[i].num_channels;
00976 float** ch_data = s->chgroup[i].channel_data;
00977 float** ch_end = ch_data + num_channels;
00978 const int8_t* tb = s->chgroup[i].transform_band;
00979 int16_t* sfb;
00980
00982 for (sfb = s->cur_sfb_offsets;
00983 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00984 int y;
00985 if (*tb++ == 1) {
00987 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00988 const float* mat = s->chgroup[i].decorrelation_matrix;
00989 const float* data_end = data + num_channels;
00990 float* data_ptr = data;
00991 float** ch;
00992
00993 for (ch = ch_data; ch < ch_end; ch++)
00994 *data_ptr++ = (*ch)[y];
00995
00996 for (ch = ch_data; ch < ch_end; ch++) {
00997 float sum = 0;
00998 data_ptr = data;
00999 while (data_ptr < data_end)
01000 sum += *data_ptr++ * *mat++;
01001
01002 (*ch)[y] = sum;
01003 }
01004 }
01005 } else if (s->num_channels == 2) {
01006 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01007 s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01008 ch_data[0] + sfb[0],
01009 181.0 / 128, len);
01010 s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01011 ch_data[1] + sfb[0],
01012 181.0 / 128, len);
01013 }
01014 }
01015 }
01016 }
01017 }
01018
01023 static void wmapro_window(WMAProDecodeCtx *s)
01024 {
01025 int i;
01026 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01027 int c = s->channel_indexes_for_cur_subframe[i];
01028 float* window;
01029 int winlen = s->channel[c].prev_block_len;
01030 float* start = s->channel[c].coeffs - (winlen >> 1);
01031
01032 if (s->subframe_len < winlen) {
01033 start += (winlen - s->subframe_len) >> 1;
01034 winlen = s->subframe_len;
01035 }
01036
01037 window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01038
01039 winlen >>= 1;
01040
01041 s->dsp.vector_fmul_window(start, start, start + winlen,
01042 window, winlen);
01043
01044 s->channel[c].prev_block_len = s->subframe_len;
01045 }
01046 }
01047
01053 static int decode_subframe(WMAProDecodeCtx *s)
01054 {
01055 int offset = s->samples_per_frame;
01056 int subframe_len = s->samples_per_frame;
01057 int i;
01058 int total_samples = s->samples_per_frame * s->num_channels;
01059 int transmit_coeffs = 0;
01060 int cur_subwoofer_cutoff;
01061
01062 s->subframe_offset = get_bits_count(&s->gb);
01063
01068 for (i = 0; i < s->num_channels; i++) {
01069 s->channel[i].grouped = 0;
01070 if (offset > s->channel[i].decoded_samples) {
01071 offset = s->channel[i].decoded_samples;
01072 subframe_len =
01073 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01074 }
01075 }
01076
01077 av_dlog(s->avctx,
01078 "processing subframe with offset %i len %i\n", offset, subframe_len);
01079
01081 s->channels_for_cur_subframe = 0;
01082 for (i = 0; i < s->num_channels; i++) {
01083 const int cur_subframe = s->channel[i].cur_subframe;
01085 total_samples -= s->channel[i].decoded_samples;
01086
01088 if (offset == s->channel[i].decoded_samples &&
01089 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01090 total_samples -= s->channel[i].subframe_len[cur_subframe];
01091 s->channel[i].decoded_samples +=
01092 s->channel[i].subframe_len[cur_subframe];
01093 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01094 ++s->channels_for_cur_subframe;
01095 }
01096 }
01097
01100 if (!total_samples)
01101 s->parsed_all_subframes = 1;
01102
01103
01104 av_dlog(s->avctx, "subframe is part of %i channels\n",
01105 s->channels_for_cur_subframe);
01106
01108 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
01109 s->num_bands = s->num_sfb[s->table_idx];
01110 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
01111 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01112
01114 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01115 int c = s->channel_indexes_for_cur_subframe[i];
01116
01117 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01118 + offset];
01119 }
01120
01121 s->subframe_len = subframe_len;
01122 s->esc_len = av_log2(s->subframe_len - 1) + 1;
01123
01125 if (get_bits1(&s->gb)) {
01126 int num_fill_bits;
01127 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01128 int len = get_bits(&s->gb, 4);
01129 num_fill_bits = get_bits(&s->gb, len) + 1;
01130 }
01131
01132 if (num_fill_bits >= 0) {
01133 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01134 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01135 return AVERROR_INVALIDDATA;
01136 }
01137
01138 skip_bits_long(&s->gb, num_fill_bits);
01139 }
01140 }
01141
01143 if (get_bits1(&s->gb)) {
01144 av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01145 return AVERROR_INVALIDDATA;
01146 }
01147
01148
01149 if (decode_channel_transform(s) < 0)
01150 return AVERROR_INVALIDDATA;
01151
01152
01153 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01154 int c = s->channel_indexes_for_cur_subframe[i];
01155 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01156 transmit_coeffs = 1;
01157 }
01158
01159 if (transmit_coeffs) {
01160 int step;
01161 int quant_step = 90 * s->bits_per_sample >> 4;
01162
01164 if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01165 int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01166 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01167 int c = s->channel_indexes_for_cur_subframe[i];
01168 s->channel[c].num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01169 }
01170 } else {
01171 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01172 int c = s->channel_indexes_for_cur_subframe[i];
01173 s->channel[c].num_vec_coeffs = s->subframe_len;
01174 }
01175 }
01177 step = get_sbits(&s->gb, 6);
01178 quant_step += step;
01179 if (step == -32 || step == 31) {
01180 const int sign = (step == 31) - 1;
01181 int quant = 0;
01182 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01183 (step = get_bits(&s->gb, 5)) == 31) {
01184 quant += 31;
01185 }
01186 quant_step += ((quant + step) ^ sign) - sign;
01187 }
01188 if (quant_step < 0) {
01189 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01190 }
01191
01194 if (s->channels_for_cur_subframe == 1) {
01195 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01196 } else {
01197 int modifier_len = get_bits(&s->gb, 3);
01198 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01199 int c = s->channel_indexes_for_cur_subframe[i];
01200 s->channel[c].quant_step = quant_step;
01201 if (get_bits1(&s->gb)) {
01202 if (modifier_len) {
01203 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01204 } else
01205 ++s->channel[c].quant_step;
01206 }
01207 }
01208 }
01209
01211 if (decode_scale_factors(s) < 0)
01212 return AVERROR_INVALIDDATA;
01213 }
01214
01215 av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01216 get_bits_count(&s->gb) - s->subframe_offset);
01217
01219 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01220 int c = s->channel_indexes_for_cur_subframe[i];
01221 if (s->channel[c].transmit_coefs &&
01222 get_bits_count(&s->gb) < s->num_saved_bits) {
01223 decode_coeffs(s, c);
01224 } else
01225 memset(s->channel[c].coeffs, 0,
01226 sizeof(*s->channel[c].coeffs) * subframe_len);
01227 }
01228
01229 av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01230 get_bits_count(&s->gb) - s->subframe_offset);
01231
01232 if (transmit_coeffs) {
01233 FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01235 inverse_channel_transform(s);
01236 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01237 int c = s->channel_indexes_for_cur_subframe[i];
01238 const int* sf = s->channel[c].scale_factors;
01239 int b;
01240
01241 if (c == s->lfe_channel)
01242 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01243 (subframe_len - cur_subwoofer_cutoff));
01244
01246 for (b = 0; b < s->num_bands; b++) {
01247 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01248 const int exp = s->channel[c].quant_step -
01249 (s->channel[c].max_scale_factor - *sf++) *
01250 s->channel[c].scale_factor_step;
01251 const float quant = pow(10.0, exp / 20.0);
01252 int start = s->cur_sfb_offsets[b];
01253 s->dsp.vector_fmul_scalar(s->tmp + start,
01254 s->channel[c].coeffs + start,
01255 quant, end - start);
01256 }
01257
01259 mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01260 }
01261 }
01262
01264 wmapro_window(s);
01265
01267 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01268 int c = s->channel_indexes_for_cur_subframe[i];
01269 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01270 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01271 return AVERROR_INVALIDDATA;
01272 }
01273 ++s->channel[c].cur_subframe;
01274 }
01275
01276 return 0;
01277 }
01278
01285 static int decode_frame(WMAProDecodeCtx *s, int *got_frame_ptr)
01286 {
01287 AVCodecContext *avctx = s->avctx;
01288 GetBitContext* gb = &s->gb;
01289 int more_frames = 0;
01290 int len = 0;
01291 int i, ret;
01292 const float *out_ptr[WMAPRO_MAX_CHANNELS];
01293 float *samples;
01294
01296 if (s->len_prefix)
01297 len = get_bits(gb, s->log2_frame_size);
01298
01299 av_dlog(s->avctx, "decoding frame with length %x\n", len);
01300
01302 if (decode_tilehdr(s)) {
01303 s->packet_loss = 1;
01304 return 0;
01305 }
01306
01308 if (s->num_channels > 1 && get_bits1(gb)) {
01309 if (get_bits1(gb)) {
01310 for (i = 0; i < s->num_channels * s->num_channels; i++)
01311 skip_bits(gb, 4);
01312 }
01313 }
01314
01316 if (s->dynamic_range_compression) {
01317 s->drc_gain = get_bits(gb, 8);
01318 av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01319 }
01320
01323 if (get_bits1(gb)) {
01324 int av_unused skip;
01325
01327 if (get_bits1(gb)) {
01328 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01329 av_dlog(s->avctx, "start skip: %i\n", skip);
01330 }
01331
01333 if (get_bits1(gb)) {
01334 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01335 av_dlog(s->avctx, "end skip: %i\n", skip);
01336 }
01337
01338 }
01339
01340 av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01341 get_bits_count(gb) - s->frame_offset);
01342
01344 s->parsed_all_subframes = 0;
01345 for (i = 0; i < s->num_channels; i++) {
01346 s->channel[i].decoded_samples = 0;
01347 s->channel[i].cur_subframe = 0;
01348 s->channel[i].reuse_sf = 0;
01349 }
01350
01352 while (!s->parsed_all_subframes) {
01353 if (decode_subframe(s) < 0) {
01354 s->packet_loss = 1;
01355 return 0;
01356 }
01357 }
01358
01359
01360 s->frame.nb_samples = s->samples_per_frame;
01361 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01362 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01363 s->packet_loss = 1;
01364 return 0;
01365 }
01366 samples = (float *)s->frame.data[0];
01367
01369 for (i = 0; i < s->num_channels; i++)
01370 out_ptr[i] = s->channel[i].out;
01371 s->fmt_conv.float_interleave(samples, out_ptr, s->samples_per_frame,
01372 s->num_channels);
01373
01374 for (i = 0; i < s->num_channels; i++) {
01376 memcpy(&s->channel[i].out[0],
01377 &s->channel[i].out[s->samples_per_frame],
01378 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01379 }
01380
01381 if (s->skip_frame) {
01382 s->skip_frame = 0;
01383 *got_frame_ptr = 0;
01384 } else {
01385 *got_frame_ptr = 1;
01386 }
01387
01388 if (s->len_prefix) {
01389 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01391 av_log(s->avctx, AV_LOG_ERROR,
01392 "frame[%i] would have to skip %i bits\n", s->frame_num,
01393 len - (get_bits_count(gb) - s->frame_offset) - 1);
01394 s->packet_loss = 1;
01395 return 0;
01396 }
01397
01399 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01400 } else {
01401 while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01402 }
01403 }
01404
01406 more_frames = get_bits1(gb);
01407
01408 ++s->frame_num;
01409 return more_frames;
01410 }
01411
01418 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01419 {
01420 return s->buf_bit_size - get_bits_count(gb);
01421 }
01422
01430 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01431 int append)
01432 {
01433 int buflen;
01434
01439 if (!append) {
01440 s->frame_offset = get_bits_count(gb) & 7;
01441 s->num_saved_bits = s->frame_offset;
01442 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01443 }
01444
01445 buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
01446
01447 if (len <= 0 || buflen > MAX_FRAMESIZE) {
01448 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01449 s->packet_loss = 1;
01450 return;
01451 }
01452
01453 s->num_saved_bits += len;
01454 if (!append) {
01455 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01456 s->num_saved_bits);
01457 } else {
01458 int align = 8 - (get_bits_count(gb) & 7);
01459 align = FFMIN(align, len);
01460 put_bits(&s->pb, align, get_bits(gb, align));
01461 len -= align;
01462 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01463 }
01464 skip_bits_long(gb, len);
01465
01466 {
01467 PutBitContext tmp = s->pb;
01468 flush_put_bits(&tmp);
01469 }
01470
01471 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01472 skip_bits(&s->gb, s->frame_offset);
01473 }
01474
01483 static int decode_packet(AVCodecContext *avctx, void *data,
01484 int *got_frame_ptr, AVPacket* avpkt)
01485 {
01486 WMAProDecodeCtx *s = avctx->priv_data;
01487 GetBitContext* gb = &s->pgb;
01488 const uint8_t* buf = avpkt->data;
01489 int buf_size = avpkt->size;
01490 int num_bits_prev_frame;
01491 int packet_sequence_number;
01492
01493 *got_frame_ptr = 0;
01494
01495 if (s->packet_done || s->packet_loss) {
01496 s->packet_done = 0;
01497
01499 if (buf_size < avctx->block_align)
01500 return 0;
01501
01502 s->next_packet_start = buf_size - avctx->block_align;
01503 buf_size = avctx->block_align;
01504 s->buf_bit_size = buf_size << 3;
01505
01507 init_get_bits(gb, buf, s->buf_bit_size);
01508 packet_sequence_number = get_bits(gb, 4);
01509 skip_bits(gb, 2);
01510
01512 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01513 av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01514 num_bits_prev_frame);
01515
01517 if (!s->packet_loss &&
01518 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01519 s->packet_loss = 1;
01520 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01521 s->packet_sequence_number, packet_sequence_number);
01522 }
01523 s->packet_sequence_number = packet_sequence_number;
01524
01525 if (num_bits_prev_frame > 0) {
01526 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01527 if (num_bits_prev_frame >= remaining_packet_bits) {
01528 num_bits_prev_frame = remaining_packet_bits;
01529 s->packet_done = 1;
01530 }
01531
01534 save_bits(s, gb, num_bits_prev_frame, 1);
01535 av_dlog(avctx, "accumulated %x bits of frame data\n",
01536 s->num_saved_bits - s->frame_offset);
01537
01539 if (!s->packet_loss)
01540 decode_frame(s, got_frame_ptr);
01541 } else if (s->num_saved_bits - s->frame_offset) {
01542 av_dlog(avctx, "ignoring %x previously saved bits\n",
01543 s->num_saved_bits - s->frame_offset);
01544 }
01545
01546 if (s->packet_loss) {
01550 s->num_saved_bits = 0;
01551 s->packet_loss = 0;
01552 }
01553
01554 } else {
01555 int frame_size;
01556 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01557 init_get_bits(gb, avpkt->data, s->buf_bit_size);
01558 skip_bits(gb, s->packet_offset);
01559 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01560 (frame_size = show_bits(gb, s->log2_frame_size)) &&
01561 frame_size <= remaining_bits(s, gb)) {
01562 save_bits(s, gb, frame_size, 0);
01563 s->packet_done = !decode_frame(s, got_frame_ptr);
01564 } else if (!s->len_prefix
01565 && s->num_saved_bits > get_bits_count(&s->gb)) {
01573 s->packet_done = !decode_frame(s, got_frame_ptr);
01574 } else
01575 s->packet_done = 1;
01576 }
01577
01578 if (s->packet_done && !s->packet_loss &&
01579 remaining_bits(s, gb) > 0) {
01582 save_bits(s, gb, remaining_bits(s, gb), 0);
01583 }
01584
01585 s->packet_offset = get_bits_count(gb) & 7;
01586 if (s->packet_loss)
01587 return AVERROR_INVALIDDATA;
01588
01589 if (*got_frame_ptr)
01590 *(AVFrame *)data = s->frame;
01591
01592 return get_bits_count(gb) >> 3;
01593 }
01594
01599 static void flush(AVCodecContext *avctx)
01600 {
01601 WMAProDecodeCtx *s = avctx->priv_data;
01602 int i;
01605 for (i = 0; i < s->num_channels; i++)
01606 memset(s->channel[i].out, 0, s->samples_per_frame *
01607 sizeof(*s->channel[i].out));
01608 s->packet_loss = 1;
01609 }
01610
01611
01615 AVCodec ff_wmapro_decoder = {
01616 .name = "wmapro",
01617 .type = AVMEDIA_TYPE_AUDIO,
01618 .id = CODEC_ID_WMAPRO,
01619 .priv_data_size = sizeof(WMAProDecodeCtx),
01620 .init = decode_init,
01621 .close = decode_end,
01622 .decode = decode_packet,
01623 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01624 .flush= flush,
01625 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01626 };