00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00090 #include "avcodec.h"
00091 #include "internal.h"
00092 #include "get_bits.h"
00093 #include "put_bits.h"
00094 #include "dsputil.h"
00095 #include "wma.h"
00096
00098 #define WMALL_MAX_CHANNELS 8
00099 #define MAX_SUBFRAMES 32
00100 #define MAX_BANDS 29
00101 #define MAX_FRAMESIZE 32768
00102
00103 #define WMALL_BLOCK_MIN_BITS 6
00104 #define WMALL_BLOCK_MAX_BITS 12
00105 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)
00106 #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1)
00107
00108
00109 #define VLCBITS 9
00110 #define SCALEVLCBITS 8
00111 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00112 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00113 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00114 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00115 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00116
00117 static float sin64[33];
00118
00122 typedef struct {
00123 int16_t prev_block_len;
00124 uint8_t transmit_coefs;
00125 uint8_t num_subframes;
00126 uint16_t subframe_len[MAX_SUBFRAMES];
00127 uint16_t subframe_offset[MAX_SUBFRAMES];
00128 uint8_t cur_subframe;
00129 uint16_t decoded_samples;
00130 uint8_t grouped;
00131 int quant_step;
00132 int8_t reuse_sf;
00133 int8_t scale_factor_step;
00134 int max_scale_factor;
00135 int saved_scale_factors[2][MAX_BANDS];
00136 int8_t scale_factor_idx;
00137 int* scale_factors;
00138 uint8_t table_idx;
00139 float* coeffs;
00140 uint16_t num_vec_coeffs;
00141 DECLARE_ALIGNED(16, float, out)[WMALL_BLOCK_MAX_SIZE + WMALL_BLOCK_MAX_SIZE / 2];
00142 int transient_counter;
00143 } WmallChannelCtx;
00144
00148 typedef struct {
00149 uint8_t num_channels;
00150 int8_t transform;
00151 int8_t transform_band[MAX_BANDS];
00152 float decorrelation_matrix[WMALL_MAX_CHANNELS*WMALL_MAX_CHANNELS];
00153 float* channel_data[WMALL_MAX_CHANNELS];
00154 } WmallChannelGrp;
00155
00159 typedef struct WmallDecodeCtx {
00160
00161 AVCodecContext* avctx;
00162 DSPContext dsp;
00163 uint8_t frame_data[MAX_FRAMESIZE +
00164 FF_INPUT_BUFFER_PADDING_SIZE];
00165 PutBitContext pb;
00166 FFTContext mdct_ctx[WMALL_BLOCK_SIZES];
00167 DECLARE_ALIGNED(16, float, tmp)[WMALL_BLOCK_MAX_SIZE];
00168 float* windows[WMALL_BLOCK_SIZES];
00169
00170
00171 uint32_t decode_flags;
00172 uint8_t len_prefix;
00173 uint8_t dynamic_range_compression;
00174 uint8_t bits_per_sample;
00175 uint16_t samples_per_frame;
00176 uint16_t log2_frame_size;
00177 int8_t num_channels;
00178 int8_t lfe_channel;
00179 uint8_t max_num_subframes;
00180 uint8_t subframe_len_bits;
00181 uint8_t max_subframe_len_bit;
00182 uint16_t min_samples_per_subframe;
00183 int8_t num_sfb[WMALL_BLOCK_SIZES];
00184 int16_t sfb_offsets[WMALL_BLOCK_SIZES][MAX_BANDS];
00185 int8_t sf_offsets[WMALL_BLOCK_SIZES][WMALL_BLOCK_SIZES][MAX_BANDS];
00186 int16_t subwoofer_cutoffs[WMALL_BLOCK_SIZES];
00187
00188
00189 GetBitContext pgb;
00190 int next_packet_start;
00191 uint8_t packet_offset;
00192 uint8_t packet_sequence_number;
00193 int num_saved_bits;
00194 int frame_offset;
00195 int subframe_offset;
00196 uint8_t packet_loss;
00197 uint8_t packet_done;
00198
00199
00200 uint32_t frame_num;
00201 GetBitContext gb;
00202 int buf_bit_size;
00203 float* samples;
00204 float* samples_end;
00205 uint8_t drc_gain;
00206 int8_t skip_frame;
00207 int8_t parsed_all_subframes;
00208
00209
00210 int16_t subframe_len;
00211 int8_t channels_for_cur_subframe;
00212 int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
00213 int8_t num_bands;
00214 int8_t transmit_num_vec_coeffs;
00215 int16_t* cur_sfb_offsets;
00216 uint8_t table_idx;
00217 int8_t esc_len;
00218
00219 uint8_t num_chgroups;
00220 WmallChannelGrp chgroup[WMALL_MAX_CHANNELS];
00221
00222 WmallChannelCtx channel[WMALL_MAX_CHANNELS];
00223
00224
00225
00226 uint8_t do_arith_coding;
00227 uint8_t do_ac_filter;
00228 uint8_t do_inter_ch_decorr;
00229 uint8_t do_mclms;
00230 uint8_t do_lpc;
00231
00232 int8_t acfilter_order;
00233 int8_t acfilter_scaling;
00234 int acfilter_coeffs[16];
00235
00236 int8_t mclms_order;
00237 int8_t mclms_scaling;
00238 int16_t mclms_coeffs[128];
00239 int16_t mclms_coeffs_cur[4];
00240 int16_t mclms_prevvalues[64];
00241 int16_t mclms_updates[64];
00242 int mclms_recent;
00243
00244 int movave_scaling;
00245 int quant_stepsize;
00246
00247 struct {
00248 int order;
00249 int scaling;
00250 int coefsend;
00251 int bitsend;
00252 int16_t coefs[256];
00253 int16_t lms_prevvalues[512];
00254 int16_t lms_updates[512];
00255 int recent;
00256 } cdlms[2][9];
00257
00258
00259
00260
00261 int cdlms_ttl[2];
00262
00263 int bV3RTM;
00264
00265 int is_channel_coded[2];
00266 int update_speed[2];
00267
00268 int transient[2];
00269 int transient_pos[2];
00270 int seekable_tile;
00271
00272 int ave_sum[2];
00273
00274 int channel_residues[2][2048];
00275
00276
00277 int lpc_coefs[2][40];
00278 int lpc_order;
00279 int lpc_scaling;
00280 int lpc_intbits;
00281
00282 int16_t channel_coeffs[2][2048];
00283
00284 } WmallDecodeCtx;
00285
00286
00287 #undef dprintf
00288 #define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
00289
00290
00291 static int num_logged_tiles = 0;
00292 static int num_logged_subframes = 0;
00293 static int num_lms_update_call = 0;
00294
00299 static void av_cold dump_context(WmallDecodeCtx *s)
00300 {
00301 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00302 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00303
00304 PRINT("ed sample bit depth", s->bits_per_sample);
00305 PRINT_HEX("ed decode flags", s->decode_flags);
00306 PRINT("samples per frame", s->samples_per_frame);
00307 PRINT("log2 frame size", s->log2_frame_size);
00308 PRINT("max num subframes", s->max_num_subframes);
00309 PRINT("len prefix", s->len_prefix);
00310 PRINT("num channels", s->num_channels);
00311 }
00312
00313 static void dump_int_buffer(uint8_t *buffer, int size, int length, int delimiter)
00314 {
00315 int i;
00316
00317 for (i=0 ; i<length ; i++) {
00318 if (!(i%delimiter))
00319 av_log(0, 0, "\n[%d] ", i);
00320 av_log(0, 0, "%d, ", *(int16_t *)(buffer + i * size));
00321 }
00322 av_log(0, 0, "\n");
00323
00324 }
00325
00331 static av_cold int decode_end(AVCodecContext *avctx)
00332 {
00333 WmallDecodeCtx *s = avctx->priv_data;
00334 int i;
00335
00336 for (i = 0; i < WMALL_BLOCK_SIZES; i++)
00337 ff_mdct_end(&s->mdct_ctx[i]);
00338
00339 return 0;
00340 }
00341
00347 static av_cold int decode_init(AVCodecContext *avctx)
00348 {
00349 WmallDecodeCtx *s = avctx->priv_data;
00350 uint8_t *edata_ptr = avctx->extradata;
00351 unsigned int channel_mask;
00352 int i;
00353 int log2_max_num_subframes;
00354 int num_possible_block_sizes;
00355
00356 s->avctx = avctx;
00357 dsputil_init(&s->dsp, avctx);
00358 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00359
00360 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00361
00362 if (avctx->extradata_size >= 18) {
00363 s->decode_flags = AV_RL16(edata_ptr+14);
00364 channel_mask = AV_RL32(edata_ptr+2);
00365 s->bits_per_sample = AV_RL16(edata_ptr);
00367 for (i = 0; i < avctx->extradata_size; i++)
00368 dprintf(avctx, "[%x] ", avctx->extradata[i]);
00369 dprintf(avctx, "\n");
00370
00371 } else {
00372 av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00373 return AVERROR_INVALIDDATA;
00374 }
00375
00377 s->log2_frame_size = av_log2(avctx->block_align) + 4;
00378
00380 s->skip_frame = 1;
00381 s->packet_loss = 1;
00382 s->len_prefix = (s->decode_flags & 0x40);
00383
00385 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00386 3, s->decode_flags);
00387
00389 for (i = 0; i < avctx->channels; i++)
00390 s->channel[i].prev_block_len = s->samples_per_frame;
00391
00393 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
00394 s->max_num_subframes = 1 << log2_max_num_subframes;
00395 s->max_subframe_len_bit = 0;
00396 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00397
00398 num_possible_block_sizes = log2_max_num_subframes + 1;
00399 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
00400 s->dynamic_range_compression = (s->decode_flags & 0x80);
00401
00402 s->bV3RTM = s->decode_flags & 0x100;
00403
00404 if (s->max_num_subframes > MAX_SUBFRAMES) {
00405 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00406 s->max_num_subframes);
00407 return AVERROR_INVALIDDATA;
00408 }
00409
00410 s->num_channels = avctx->channels;
00411
00413 s->lfe_channel = -1;
00414
00415 if (channel_mask & 8) {
00416 unsigned int mask;
00417 for (mask = 1; mask < 16; mask <<= 1) {
00418 if (channel_mask & mask)
00419 ++s->lfe_channel;
00420 }
00421 }
00422
00423 if (s->num_channels < 0) {
00424 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00425 return AVERROR_INVALIDDATA;
00426 } else if (s->num_channels > WMALL_MAX_CHANNELS) {
00427 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00428 return AVERROR_PATCHWELCOME;
00429 }
00430
00431 avctx->channel_layout = channel_mask;
00432 return 0;
00433 }
00434
00441 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
00442 {
00443 int frame_len_ratio;
00444 int subframe_len, len;
00445
00447 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00448 return s->min_samples_per_subframe;
00449
00450 len = av_log2(s->max_num_subframes - 1) + 1;
00451 frame_len_ratio = get_bits(&s->gb, len);
00452
00453 subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
00454
00456 if (subframe_len < s->min_samples_per_subframe ||
00457 subframe_len > s->samples_per_frame) {
00458 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00459 subframe_len);
00460 return AVERROR_INVALIDDATA;
00461 }
00462 return subframe_len;
00463 }
00464
00485 static int decode_tilehdr(WmallDecodeCtx *s)
00486 {
00487 uint16_t num_samples[WMALL_MAX_CHANNELS];
00488 uint8_t contains_subframe[WMALL_MAX_CHANNELS];
00489 int channels_for_cur_subframe = s->num_channels;
00490 int fixed_channel_layout = 0;
00491 int min_channel_len = 0;
00492 int c;
00493
00494
00495
00496
00497
00498
00499
00501 for (c = 0; c < s->num_channels; c++)
00502 s->channel[c].num_subframes = 0;
00503
00504 memset(num_samples, 0, sizeof(num_samples));
00505
00506 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00507 fixed_channel_layout = 1;
00508
00510 do {
00511 int subframe_len;
00512
00514 for (c = 0; c < s->num_channels; c++) {
00515 if (num_samples[c] == min_channel_len) {
00516 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00517 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
00518 contains_subframe[c] = 1;
00519 }
00520 else {
00521 contains_subframe[c] = get_bits1(&s->gb);
00522 }
00523 } else
00524 contains_subframe[c] = 0;
00525 }
00526
00528 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00529 return AVERROR_INVALIDDATA;
00531 min_channel_len += subframe_len;
00532 for (c = 0; c < s->num_channels; c++) {
00533 WmallChannelCtx* chan = &s->channel[c];
00534
00535 if (contains_subframe[c]) {
00536 if (chan->num_subframes >= MAX_SUBFRAMES) {
00537 av_log(s->avctx, AV_LOG_ERROR,
00538 "broken frame: num subframes > 31\n");
00539 return AVERROR_INVALIDDATA;
00540 }
00541 chan->subframe_len[chan->num_subframes] = subframe_len;
00542 num_samples[c] += subframe_len;
00543 ++chan->num_subframes;
00544 if (num_samples[c] > s->samples_per_frame) {
00545 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00546 "channel len(%d) > samples_per_frame(%d)\n",
00547 num_samples[c], s->samples_per_frame);
00548 return AVERROR_INVALIDDATA;
00549 }
00550 } else if (num_samples[c] <= min_channel_len) {
00551 if (num_samples[c] < min_channel_len) {
00552 channels_for_cur_subframe = 0;
00553 min_channel_len = num_samples[c];
00554 }
00555 ++channels_for_cur_subframe;
00556 }
00557 }
00558 } while (min_channel_len < s->samples_per_frame);
00559
00560 for (c = 0; c < s->num_channels; c++) {
00561 int i;
00562 int offset = 0;
00563 for (i = 0; i < s->channel[c].num_subframes; i++) {
00564 s->channel[c].subframe_offset[i] = offset;
00565 offset += s->channel[c].subframe_len[i];
00566 }
00567 }
00568
00569 return 0;
00570 }
00571
00572
00573 static int my_log2(unsigned int i)
00574 {
00575 unsigned int iLog2 = 0;
00576 while ((i >> iLog2) > 1)
00577 iLog2++;
00578 return iLog2;
00579 }
00580
00581
00585 static void decode_ac_filter(WmallDecodeCtx *s)
00586 {
00587 int i;
00588 s->acfilter_order = get_bits(&s->gb, 4) + 1;
00589 s->acfilter_scaling = get_bits(&s->gb, 4);
00590
00591 for(i = 0; i < s->acfilter_order; i++) {
00592 s->acfilter_coeffs[i] = get_bits(&s->gb, s->acfilter_scaling) + 1;
00593 }
00594 }
00595
00596
00600 static void decode_mclms(WmallDecodeCtx *s)
00601 {
00602 s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
00603 s->mclms_scaling = get_bits(&s->gb, 4);
00604 if(get_bits1(&s->gb)) {
00605
00606 int i;
00607 int send_coef_bits;
00608 int cbits = av_log2(s->mclms_scaling + 1);
00609 assert(cbits == my_log2(s->mclms_scaling + 1));
00610 if(1 << cbits < s->mclms_scaling + 1)
00611 cbits++;
00612
00613 send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
00614
00615 for(i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++) {
00616 s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
00617 }
00618
00619 for(i = 0; i < s->num_channels; i++) {
00620 int c;
00621 for(c = 0; c < i; c++) {
00622 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
00623 }
00624 }
00625 }
00626 }
00627
00628
00632 static void decode_cdlms(WmallDecodeCtx *s)
00633 {
00634 int c, i;
00635 int cdlms_send_coef = get_bits1(&s->gb);
00636
00637 for(c = 0; c < s->num_channels; c++) {
00638 s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
00639 for(i = 0; i < s->cdlms_ttl[c]; i++) {
00640 s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
00641 }
00642
00643 for(i = 0; i < s->cdlms_ttl[c]; i++) {
00644 s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
00645 }
00646
00647 if(cdlms_send_coef) {
00648 for(i = 0; i < s->cdlms_ttl[c]; i++) {
00649 int cbits, shift_l, shift_r, j;
00650 cbits = av_log2(s->cdlms[c][i].order);
00651 if(1 << cbits < s->cdlms[c][i].order)
00652 cbits++;
00653 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
00654
00655 cbits = av_log2(s->cdlms[c][i].scaling + 1);
00656 if(1 << cbits < s->cdlms[c][i].scaling + 1)
00657 cbits++;
00658
00659 s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
00660 shift_l = 32 - s->cdlms[c][i].bitsend;
00661 shift_r = 32 - 2 - s->cdlms[c][i].scaling;
00662 for(j = 0; j < s->cdlms[c][i].coefsend; j++) {
00663 s->cdlms[c][i].coefs[j] =
00664 (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
00665 }
00666 }
00667 }
00668 }
00669 }
00670
00674 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
00675 {
00676 int i = 0;
00677 unsigned int ave_mean;
00678 s->transient[ch] = get_bits1(&s->gb);
00679 if(s->transient[ch]) {
00680 s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
00681 if (s->transient_pos[ch])
00682 s->transient[ch] = 0;
00683 s->channel[ch].transient_counter =
00684 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
00685 } else if (s->channel[ch].transient_counter)
00686 s->transient[ch] = 1;
00687
00688 if(s->seekable_tile) {
00689 ave_mean = get_bits(&s->gb, s->bits_per_sample);
00690 s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
00691
00692 }
00693
00694 if(s->seekable_tile) {
00695 if(s->do_inter_ch_decorr)
00696 s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
00697 else
00698 s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
00699 i++;
00700 }
00701
00702 for(; i < tile_size; i++) {
00703 int quo = 0, rem, rem_bits, residue;
00704 while(get_bits1(&s->gb))
00705 quo++;
00706 if(quo >= 32)
00707 quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
00708
00709 ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
00710 rem_bits = av_ceil_log2(ave_mean);
00711 rem = rem_bits ? get_bits(&s->gb, rem_bits) : 0;
00712 residue = (quo << rem_bits) + rem;
00713
00714 s->ave_sum[ch] = residue + s->ave_sum[ch] - (s->ave_sum[ch] >> s->movave_scaling);
00715
00716 if(residue & 1)
00717 residue = -(residue >> 1) - 1;
00718 else
00719 residue = residue >> 1;
00720 s->channel_residues[ch][i] = residue;
00721 }
00722 dump_int_buffer(s->channel_residues[ch], 4, tile_size, 16);
00723
00724 return 0;
00725
00726 }
00727
00728
00732 static void
00733 decode_lpc(WmallDecodeCtx *s)
00734 {
00735 int ch, i, cbits;
00736 s->lpc_order = get_bits(&s->gb, 5) + 1;
00737 s->lpc_scaling = get_bits(&s->gb, 4);
00738 s->lpc_intbits = get_bits(&s->gb, 3) + 1;
00739 cbits = s->lpc_scaling + s->lpc_intbits;
00740 for(ch = 0; ch < s->num_channels; ch++) {
00741 for(i = 0; i < s->lpc_order; i++) {
00742 s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
00743 }
00744 }
00745 }
00746
00747
00748 static void clear_codec_buffers(WmallDecodeCtx *s)
00749 {
00750 int ich, ilms;
00751
00752 memset(s->acfilter_coeffs, 0, 16 * sizeof(int));
00753 memset(s->lpc_coefs , 0, 40 * 2 * sizeof(int));
00754
00755 memset(s->mclms_coeffs , 0, 128 * sizeof(int16_t));
00756 memset(s->mclms_coeffs_cur, 0, 4 * sizeof(int16_t));
00757 memset(s->mclms_prevvalues, 0, 64 * sizeof(int));
00758 memset(s->mclms_updates , 0, 64 * sizeof(int16_t));
00759
00760 for (ich = 0; ich < s->num_channels; ich++) {
00761 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
00762 memset(s->cdlms[ich][ilms].coefs , 0, 256 * sizeof(int16_t));
00763 memset(s->cdlms[ich][ilms].lms_prevvalues, 0, 512 * sizeof(int16_t));
00764 memset(s->cdlms[ich][ilms].lms_updates , 0, 512 * sizeof(int16_t));
00765 }
00766 s->ave_sum[ich] = 0;
00767 }
00768 }
00769
00773 static void reset_codec(WmallDecodeCtx *s)
00774 {
00775 int ich, ilms;
00776 s->mclms_recent = s->mclms_order * s->num_channels;
00777 for (ich = 0; ich < s->num_channels; ich++) {
00778 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
00779 s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
00780
00781
00782 s->channel[ich].transient_counter = s->samples_per_frame;
00783 s->transient[ich] = 1;
00784 s->transient_pos[ich] = 0;
00785 }
00786 }
00787
00788
00789
00790 static void mclms_update(WmallDecodeCtx *s, int icoef)
00791 {
00792 int i, j, ich;
00793 int16_t pred_error;
00794 int order = s->mclms_order;
00795 int num_channels = s->num_channels;
00796 int16_t range = 1 << (s->bits_per_sample - 1);
00797 int bps = s->bits_per_sample > 16 ? 4 : 2;
00798
00799 for (ich = 0; ich < num_channels; ich++) {
00800 pred_error = s->channel_coeffs[ich][icoef] -
00801 s->channel_residues[ich][icoef];
00802 if (pred_error > 0) {
00803 for (i = 0; i < order * num_channels; i++)
00804 s->mclms_coeffs[i + ich * order * num_channels] +=
00805 s->mclms_updates[s->mclms_recent + i];
00806 for (j = 0; j < i; j++) {
00807 if (s->channel_coeffs[ich][icoef] > 0)
00808 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
00809 else if (s->channel_coeffs[ich][icoef] < 0)
00810 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
00811 }
00812 } else if (pred_error < 0) {
00813 for (i = 0; i < order * num_channels; i++)
00814 s->mclms_coeffs[i + ich * order * num_channels] -=
00815 s->mclms_updates[s->mclms_recent + i];
00816 for (j = 0; j < i; j++) {
00817 if (s->channel_coeffs[ich][icoef] > 0)
00818 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
00819 else if (s->channel_coeffs[ich][icoef] < 0)
00820 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
00821 }
00822 }
00823 }
00824
00825 for (ich = num_channels - 1; ich >= 0; ich--) {
00826 s->mclms_recent--;
00827 if (s->channel_coeffs[ich][icoef] > range - 1)
00828 s->mclms_prevvalues[s->mclms_recent] = range - 1;
00829 else if (s->channel_coeffs[ich][icoef] <= -range)
00830 s->mclms_prevvalues[s->mclms_recent] = -range;
00831
00832 s->mclms_updates[s->mclms_recent] =
00833 av_clip(-1, s->channel_coeffs[ich][icoef], 1);
00834 }
00835
00836 if (s->mclms_recent == 0) {
00837 memcpy(&s->mclms_prevvalues[order * num_channels],
00838 s->mclms_prevvalues,
00839 bps * order * num_channels);
00840 memcpy(&s->mclms_updates[order * num_channels],
00841 s->mclms_updates,
00842 bps * order * num_channels);
00843 s->mclms_recent = num_channels * order;
00844 }
00845 }
00846 static void mclms_predict(WmallDecodeCtx *s, int icoef)
00847 {
00848 int ich, i;
00849 int16_t pred;
00850 int order = s->mclms_order;
00851 int num_channels = s->num_channels;
00852
00853 for (ich = 0; ich < num_channels; ich++) {
00854 if (!s->is_channel_coded[ich])
00855 continue;
00856 pred = 0;
00857 for (i = 0; i < order * num_channels; i++)
00858 pred += s->mclms_prevvalues[i] *
00859 s->mclms_coeffs[i + order * num_channels * ich];
00860 for (i = 0; i < ich; i++)
00861 pred += s->channel_coeffs[ich][icoef] *
00862 s->mclms_coeffs_cur[i + order * num_channels * ich];
00863 s->channel_coeffs[ich][icoef] =
00864 s->channel_residues[ich][icoef] + pred;
00865 }
00866 }
00867
00868 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
00869 {
00870 int icoef;
00871 for (icoef = 0; icoef < tile_size; icoef++) {
00872 mclms_predict(s, icoef);
00873 mclms_update(s, icoef);
00874 }
00875 }
00876
00877 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
00878 {
00879 int pred = 0;
00880 int icoef;
00881 int recent = s->cdlms[ich][ilms].recent;
00882
00883 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00884 pred += s->cdlms[ich][ilms].coefs[icoef] *
00885 s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
00886
00887
00888
00889
00890
00891
00892 return pred;
00893 }
00894
00895 static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input, int residue)
00896 {
00897 int icoef;
00898 int recent = s->cdlms[ich][ilms].recent;
00899 int range = 1 << s->bits_per_sample - 1;
00900 int bps = s->bits_per_sample > 16 ? 4 : 2;
00901
00902 if (residue < 0) {
00903 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00904 s->cdlms[ich][ilms].coefs[icoef] -=
00905 s->cdlms[ich][ilms].lms_updates[icoef + recent];
00906 } else if (residue > 0) {
00907 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00908 s->cdlms[ich][ilms].coefs[icoef] +=
00909 s->cdlms[ich][ilms].lms_updates[icoef + recent];
00910
00911 }
00912
00913 if (recent)
00914 recent--;
00915 else {
00916
00917
00918 memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
00919 s->cdlms[ich][ilms].lms_prevvalues,
00920 bps * s->cdlms[ich][ilms].order);
00921 memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
00922 s->cdlms[ich][ilms].lms_updates,
00923 bps * s->cdlms[ich][ilms].order);
00924 recent = s->cdlms[ich][ilms].order - 1;
00925 }
00926
00927 s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
00928 if (!input)
00929 s->cdlms[ich][ilms].lms_updates[recent] = 0;
00930 else if (input < 0)
00931 s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
00932 else
00933 s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
00944 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
00945 s->cdlms[ich][ilms].recent = recent;
00946 }
00947
00948 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
00949 {
00950 int ilms, recent, icoef;
00951 s->update_speed[ich] = 16;
00952 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
00953 recent = s->cdlms[ich][ilms].recent;
00954 if (s->bV3RTM) {
00955 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00956 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
00957 } else {
00958 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00959 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
00960 }
00961 }
00962 }
00963
00964 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
00965 {
00966 int ilms, recent, icoef;
00967 s->update_speed[ich] = 8;
00968 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
00969 recent = s->cdlms[ich][ilms].recent;
00970 if (s->bV3RTM) {
00971 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00972 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
00973 } else {
00974 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00975 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
00976 }
00977 }
00978 }
00979
00980 static void revert_cdlms(WmallDecodeCtx *s, int ch, int coef_begin, int coef_end)
00981 {
00982 int icoef;
00983 int pred;
00984 int ilms, num_lms;
00985 int residue, input;
00986
00987 num_lms = s->cdlms_ttl[ch];
00988 for (ilms = num_lms - 1; ilms >= 0; ilms--) {
00989
00990 for (icoef = coef_begin; icoef < coef_end; icoef++) {
00991 pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
00992 residue = s->channel_residues[ch][icoef];
00993 pred += lms_predict(s, ch, ilms);
00994 input = residue + (pred >> s->cdlms[ch][ilms].scaling);
00995 lms_update(s, ch, ilms, input, residue);
00996 s->channel_residues[ch][icoef] = input;
00997 }
00998 }
00999 }
01000
01001
01002
01008 static int decode_subframe(WmallDecodeCtx *s)
01009 {
01010 int offset = s->samples_per_frame;
01011 int subframe_len = s->samples_per_frame;
01012 int i;
01013 int total_samples = s->samples_per_frame * s->num_channels;
01014 int rawpcm_tile;
01015 int padding_zeroes;
01016
01017 s->subframe_offset = get_bits_count(&s->gb);
01018
01023 for (i = 0; i < s->num_channels; i++) {
01024 s->channel[i].grouped = 0;
01025 if (offset > s->channel[i].decoded_samples) {
01026 offset = s->channel[i].decoded_samples;
01027 subframe_len =
01028 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01029 }
01030 }
01031
01033 s->channels_for_cur_subframe = 0;
01034 for (i = 0; i < s->num_channels; i++) {
01035 const int cur_subframe = s->channel[i].cur_subframe;
01037 total_samples -= s->channel[i].decoded_samples;
01038
01040 if (offset == s->channel[i].decoded_samples &&
01041 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01042 total_samples -= s->channel[i].subframe_len[cur_subframe];
01043 s->channel[i].decoded_samples +=
01044 s->channel[i].subframe_len[cur_subframe];
01045 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01046 ++s->channels_for_cur_subframe;
01047 }
01048 }
01049
01052 if (!total_samples)
01053 s->parsed_all_subframes = 1;
01054
01055
01056 s->seekable_tile = get_bits1(&s->gb);
01057 if(s->seekable_tile) {
01058 clear_codec_buffers(s);
01059
01060 s->do_arith_coding = get_bits1(&s->gb);
01061 if(s->do_arith_coding) {
01062 dprintf(s->avctx, "do_arith_coding == 1");
01063 abort();
01064 }
01065 s->do_ac_filter = get_bits1(&s->gb);
01066 s->do_inter_ch_decorr = get_bits1(&s->gb);
01067 s->do_mclms = get_bits1(&s->gb);
01068
01069 if(s->do_ac_filter)
01070 decode_ac_filter(s);
01071
01072 if(s->do_mclms)
01073 decode_mclms(s);
01074
01075 decode_cdlms(s);
01076 s->movave_scaling = get_bits(&s->gb, 3);
01077 s->quant_stepsize = get_bits(&s->gb, 8) + 1;
01078
01079 reset_codec(s);
01080 }
01081
01082 rawpcm_tile = get_bits1(&s->gb);
01083
01084 for(i = 0; i < s->num_channels; i++) {
01085 s->is_channel_coded[i] = 1;
01086 }
01087
01088 if(!rawpcm_tile) {
01089
01090 for(i = 0; i < s->num_channels; i++) {
01091 s->is_channel_coded[i] = get_bits1(&s->gb);
01092 }
01093
01094 if(s->bV3RTM) {
01095
01096 s->do_lpc = get_bits1(&s->gb);
01097 if(s->do_lpc) {
01098 decode_lpc(s);
01099 }
01100 } else {
01101 s->do_lpc = 0;
01102 }
01103 }
01104
01105
01106 if(get_bits1(&s->gb)) {
01107 padding_zeroes = get_bits(&s->gb, 5);
01108 } else {
01109 padding_zeroes = 0;
01110 }
01111
01112 if(rawpcm_tile) {
01113
01114 int bits = s->bits_per_sample - padding_zeroes;
01115 int j;
01116 dprintf(s->avctx, "RAWPCM %d bits per sample. total %d bits, remain=%d\n", bits,
01117 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
01118 for(i = 0; i < s->num_channels; i++) {
01119 for(j = 0; j < subframe_len; j++) {
01120 s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
01121
01122 }
01123 }
01124 } else {
01125 for(i = 0; i < s->num_channels; i++)
01126 if(s->is_channel_coded[i]) {
01127 decode_channel_residues(s, i, subframe_len);
01128 if (s->seekable_tile)
01129 use_high_update_speed(s, i);
01130 else
01131 use_normal_update_speed(s, i);
01132 revert_cdlms(s, i, 0, subframe_len);
01133 }
01134 }
01135
01138 for (i = 0; i < s->channels_for_cur_subframe; i++) {
01139 int c = s->channel_indexes_for_cur_subframe[i];
01140 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01141 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01142 return AVERROR_INVALIDDATA;
01143 }
01144 ++s->channel[c].cur_subframe;
01145 }
01146 num_logged_subframes++;
01147 return 0;
01148 }
01149
01156 static int decode_frame(WmallDecodeCtx *s)
01157 {
01158 GetBitContext* gb = &s->gb;
01159 int more_frames = 0;
01160 int len = 0;
01161 int i;
01162
01164 if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
01166 av_log(s->avctx, AV_LOG_ERROR,
01167 "not enough space for the output samples\n");
01168 s->packet_loss = 1;
01169 return 0;
01170 }
01171
01173 if (s->len_prefix)
01174 len = get_bits(gb, s->log2_frame_size);
01175
01177 if (decode_tilehdr(s)) {
01178 s->packet_loss = 1;
01179 return 0;
01180 }
01181
01183 if (s->dynamic_range_compression) {
01184 s->drc_gain = get_bits(gb, 8);
01185 }
01186
01189 if (get_bits1(gb)) {
01190 int skip;
01191
01193 if (get_bits1(gb)) {
01194 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01195 dprintf(s->avctx, "start skip: %i\n", skip);
01196 }
01197
01199 if (get_bits1(gb)) {
01200 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01201 dprintf(s->avctx, "end skip: %i\n", skip);
01202 }
01203
01204 }
01205
01207 s->parsed_all_subframes = 0;
01208 for (i = 0; i < s->num_channels; i++) {
01209 s->channel[i].decoded_samples = 0;
01210 s->channel[i].cur_subframe = 0;
01211 s->channel[i].reuse_sf = 0;
01212 }
01213
01215 while (!s->parsed_all_subframes) {
01216 if (decode_subframe(s) < 0) {
01217 s->packet_loss = 1;
01218 return 0;
01219 }
01220 }
01221
01222 dprintf(s->avctx, "Frame done\n");
01223
01224 if (s->skip_frame) {
01225 s->skip_frame = 0;
01226 } else
01227 s->samples += s->num_channels * s->samples_per_frame;
01228
01229 if (s->len_prefix) {
01230 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01232 av_log(s->avctx, AV_LOG_ERROR,
01233 "frame[%i] would have to skip %i bits\n", s->frame_num,
01234 len - (get_bits_count(gb) - s->frame_offset) - 1);
01235 s->packet_loss = 1;
01236 return 0;
01237 }
01238
01240 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01241 } else {
01242
01243
01244
01245
01246
01247 }
01248
01250 more_frames = get_bits1(gb);
01251 ++s->frame_num;
01252 return more_frames;
01253 }
01254
01261 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
01262 {
01263 return s->buf_bit_size - get_bits_count(gb);
01264 }
01265
01273 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
01274 int append)
01275 {
01276 int buflen;
01277
01282 if (!append) {
01283 s->frame_offset = get_bits_count(gb) & 7;
01284 s->num_saved_bits = s->frame_offset;
01285 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01286 }
01287
01288 buflen = (s->num_saved_bits + len + 8) >> 3;
01289
01290 if (len <= 0 || buflen > MAX_FRAMESIZE) {
01291 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01292 s->packet_loss = 1;
01293 return;
01294 }
01295
01296 s->num_saved_bits += len;
01297 if (!append) {
01298 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01299 s->num_saved_bits);
01300 } else {
01301 int align = 8 - (get_bits_count(gb) & 7);
01302 align = FFMIN(align, len);
01303 put_bits(&s->pb, align, get_bits(gb, align));
01304 len -= align;
01305 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01306 }
01307 skip_bits_long(gb, len);
01308
01309 {
01310 PutBitContext tmp = s->pb;
01311 flush_put_bits(&tmp);
01312 }
01313
01314 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01315 skip_bits(&s->gb, s->frame_offset);
01316 }
01317
01326 static int decode_packet(AVCodecContext *avctx,
01327 void *data, int *data_size, AVPacket* avpkt)
01328 {
01329 WmallDecodeCtx *s = avctx->priv_data;
01330 GetBitContext* gb = &s->pgb;
01331 const uint8_t* buf = avpkt->data;
01332 int buf_size = avpkt->size;
01333 int num_bits_prev_frame;
01334 int packet_sequence_number;
01335
01336 s->samples = data;
01337 s->samples_end = (float*)((int8_t*)data + *data_size);
01338 *data_size = 0;
01339
01340 if (s->packet_done || s->packet_loss) {
01341 s->packet_done = 0;
01342
01344 if (buf_size < avctx->block_align)
01345 return 0;
01346
01347 s->next_packet_start = buf_size - avctx->block_align;
01348 buf_size = avctx->block_align;
01349 s->buf_bit_size = buf_size << 3;
01350
01352 init_get_bits(gb, buf, s->buf_bit_size);
01353 packet_sequence_number = get_bits(gb, 4);
01354 int seekable_frame_in_packet = get_bits1(gb);
01355 int spliced_packet = get_bits1(gb);
01356
01358 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01359
01361 if (!s->packet_loss &&
01362 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01363 s->packet_loss = 1;
01364 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01365 s->packet_sequence_number, packet_sequence_number);
01366 }
01367 s->packet_sequence_number = packet_sequence_number;
01368
01369 if (num_bits_prev_frame > 0) {
01370 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01371 if (num_bits_prev_frame >= remaining_packet_bits) {
01372 num_bits_prev_frame = remaining_packet_bits;
01373 s->packet_done = 1;
01374 }
01375
01378 save_bits(s, gb, num_bits_prev_frame, 1);
01379
01381 if (!s->packet_loss)
01382 decode_frame(s);
01383 } else if (s->num_saved_bits - s->frame_offset) {
01384 dprintf(avctx, "ignoring %x previously saved bits\n",
01385 s->num_saved_bits - s->frame_offset);
01386 }
01387
01388 if (s->packet_loss) {
01392 s->num_saved_bits = 0;
01393 s->packet_loss = 0;
01394 }
01395
01396 } else {
01397 int frame_size;
01398
01399 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01400 init_get_bits(gb, avpkt->data, s->buf_bit_size);
01401 skip_bits(gb, s->packet_offset);
01402
01403 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01404 (frame_size = show_bits(gb, s->log2_frame_size)) &&
01405 frame_size <= remaining_bits(s, gb)) {
01406 save_bits(s, gb, frame_size, 0);
01407 s->packet_done = !decode_frame(s);
01408 } else if (!s->len_prefix
01409 && s->num_saved_bits > get_bits_count(&s->gb)) {
01417 s->packet_done = !decode_frame(s);
01418 } else {
01419 s->packet_done = 1;
01420 }
01421 }
01422
01423 if (s->packet_done && !s->packet_loss &&
01424 remaining_bits(s, gb) > 0) {
01427 save_bits(s, gb, remaining_bits(s, gb), 0);
01428 }
01429
01430 *data_size = 0;
01431 s->packet_offset = get_bits_count(gb) & 7;
01432
01433 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
01434 }
01435
01440 static void flush(AVCodecContext *avctx)
01441 {
01442 WmallDecodeCtx *s = avctx->priv_data;
01443 int i;
01446 for (i = 0; i < s->num_channels; i++)
01447 memset(s->channel[i].out, 0, s->samples_per_frame *
01448 sizeof(*s->channel[i].out));
01449 s->packet_loss = 1;
01450 }
01451
01452
01456 AVCodec ff_wmalossless_decoder = {
01457 "wmalossless",
01458 AVMEDIA_TYPE_AUDIO,
01459 CODEC_ID_WMALOSSLESS,
01460 sizeof(WmallDecodeCtx),
01461 decode_init,
01462 NULL,
01463 decode_end,
01464 decode_packet,
01465 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_EXPERIMENTAL,
01466 .flush= flush,
01467 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Lossless"),
01468 };