2 * Windows Media Audio Lossless decoder
3 * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4 * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
5 * Copyright (c) 2011 Andreas Ă–man
6 * Copyright (c) 2011 - 2012 Mashiat Sarker Shakkhar
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/attributes.h"
26 #include "libavutil/avassert.h"
33 #include "wma_common.h"
35 /** current decoder limitations */
36 #define WMALL_MAX_CHANNELS 8 ///< max number of handled channels
37 #define MAX_SUBFRAMES 32 ///< max number of subframes per channel
38 #define MAX_BANDS 29 ///< max number of scale factor bands
39 #define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
42 #define WMALL_BLOCK_MIN_BITS 6 ///< log2 of min block size
43 #define WMALL_BLOCK_MAX_BITS 14 ///< log2 of max block size
44 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS) ///< maximum block size
45 #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1) ///< possible block sizes
49 * @brief frame-specific decoder context for a single channel
52 int16_t prev_block_len; ///< length of the previous block
53 uint8_t transmit_coefs;
54 uint8_t num_subframes;
55 uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
56 uint16_t subframe_offsets[MAX_SUBFRAMES]; ///< subframe positions in the current frame
57 uint8_t cur_subframe; ///< current subframe number
58 uint16_t decoded_samples; ///< number of already processed samples
59 int quant_step; ///< quantization step for the current subframe
60 int transient_counter; ///< number of transient samples from the beginning of the transient zone
64 * @brief main decoder context
66 typedef struct WmallDecodeCtx {
67 /* generic decoder variables */
68 AVCodecContext *avctx;
70 uint8_t frame_data[MAX_FRAMESIZE + FF_INPUT_BUFFER_PADDING_SIZE]; ///< compressed frame data
71 PutBitContext pb; ///< context for filling the frame_data buffer
73 /* frame size dependent frame information (set during initialization) */
74 uint32_t decode_flags; ///< used compression features
75 int len_prefix; ///< frame is prefixed with its length
76 int dynamic_range_compression; ///< frame contains DRC data
77 uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
78 uint16_t samples_per_frame; ///< number of samples to output
79 uint16_t log2_frame_size;
80 int8_t num_channels; ///< number of channels in the stream (same as AVCodecContext.num_channels)
81 int8_t lfe_channel; ///< lfe channel index
82 uint8_t max_num_subframes;
83 uint8_t subframe_len_bits; ///< number of bits used for the subframe length
84 uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
85 uint16_t min_samples_per_subframe;
87 /* packet decode state */
88 GetBitContext pgb; ///< bitstream reader context for the packet
89 int next_packet_start; ///< start offset of the next WMA packet in the demuxer packet
90 uint8_t packet_offset; ///< offset to the frame in the packet
91 uint8_t packet_sequence_number; ///< current packet number
92 int num_saved_bits; ///< saved number of bits
93 int frame_offset; ///< frame offset in the bit reservoir
94 int subframe_offset; ///< subframe offset in the bit reservoir
95 uint8_t packet_loss; ///< set in case of bitstream error
96 uint8_t packet_done; ///< set when a packet is fully decoded
98 /* frame decode state */
99 uint32_t frame_num; ///< current frame number (not used for decoding)
100 GetBitContext gb; ///< bitstream reader context
101 int buf_bit_size; ///< buffer size in bits
102 int16_t *samples_16[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (16-bit)
103 int32_t *samples_32[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (24-bit)
104 uint8_t drc_gain; ///< gain for the DRC tool
105 int8_t skip_frame; ///< skip output step
106 int8_t parsed_all_subframes; ///< all subframes decoded?
108 /* subframe/block decode state */
109 int16_t subframe_len; ///< current subframe length
110 int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
111 int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
113 WmallChannelCtx channel[WMALL_MAX_CHANNELS]; ///< per channel data
115 // WMA Lossless-specific
117 uint8_t do_arith_coding;
118 uint8_t do_ac_filter;
119 uint8_t do_inter_ch_decorr;
123 int8_t acfilter_order;
124 int8_t acfilter_scaling;
125 int64_t acfilter_coeffs[16];
126 int acfilter_prevvalues[WMALL_MAX_CHANNELS][16];
129 int8_t mclms_scaling;
130 int16_t mclms_coeffs[128];
131 int16_t mclms_coeffs_cur[4];
132 int16_t mclms_prevvalues[WMALL_MAX_CHANNELS * 2 * 32];
133 int16_t mclms_updates[WMALL_MAX_CHANNELS * 2 * 32];
144 int16_t coefs[MAX_ORDER];
145 int16_t lms_prevvalues[MAX_ORDER * 2];
146 int16_t lms_updates[MAX_ORDER * 2];
148 } cdlms[WMALL_MAX_CHANNELS][9];
150 int cdlms_ttl[WMALL_MAX_CHANNELS];
154 int is_channel_coded[WMALL_MAX_CHANNELS];
155 int update_speed[WMALL_MAX_CHANNELS];
157 int transient[WMALL_MAX_CHANNELS];
158 int transient_pos[WMALL_MAX_CHANNELS];
161 int ave_sum[WMALL_MAX_CHANNELS];
163 int channel_residues[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
165 int lpc_coefs[WMALL_MAX_CHANNELS][40];
170 int channel_coeffs[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
174 static av_cold int decode_init(AVCodecContext *avctx)
176 WmallDecodeCtx *s = avctx->priv_data;
177 uint8_t *edata_ptr = avctx->extradata;
178 unsigned int channel_mask;
179 int i, log2_max_num_subframes;
182 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
184 if (avctx->extradata_size >= 18) {
185 s->decode_flags = AV_RL16(edata_ptr + 14);
186 channel_mask = AV_RL32(edata_ptr + 2);
187 s->bits_per_sample = AV_RL16(edata_ptr);
188 if (s->bits_per_sample == 16)
189 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
190 else if (s->bits_per_sample == 24) {
191 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
192 avpriv_report_missing_feature(avctx, "Bit-depth higher than 16");
193 return AVERROR_PATCHWELCOME;
195 av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
197 return AVERROR_INVALIDDATA;
199 /* dump the extradata */
200 for (i = 0; i < avctx->extradata_size; i++)
201 av_dlog(avctx, "[%x] ", avctx->extradata[i]);
202 av_dlog(avctx, "\n");
205 avpriv_request_sample(avctx, "Unsupported extradata size");
206 return AVERROR_PATCHWELCOME;
210 s->log2_frame_size = av_log2(avctx->block_align) + 4;
213 s->skip_frame = 1; /* skip first frame */
215 s->len_prefix = s->decode_flags & 0x40;
218 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
220 av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
222 /* init previous block len */
223 for (i = 0; i < avctx->channels; i++)
224 s->channel[i].prev_block_len = s->samples_per_frame;
227 log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
228 s->max_num_subframes = 1 << log2_max_num_subframes;
229 s->max_subframe_len_bit = 0;
230 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
232 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
233 s->dynamic_range_compression = s->decode_flags & 0x80;
234 s->bV3RTM = s->decode_flags & 0x100;
236 if (s->max_num_subframes > MAX_SUBFRAMES) {
237 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
238 s->max_num_subframes);
239 return AVERROR_INVALIDDATA;
242 s->num_channels = avctx->channels;
244 /* extract lfe channel position */
247 if (channel_mask & 8) {
249 for (mask = 1; mask < 16; mask <<= 1)
250 if (channel_mask & mask)
254 if (s->num_channels < 0) {
255 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
257 return AVERROR_INVALIDDATA;
258 } else if (s->num_channels > WMALL_MAX_CHANNELS) {
259 avpriv_request_sample(avctx,
260 "More than %d channels", WMALL_MAX_CHANNELS);
261 return AVERROR_PATCHWELCOME;
264 avcodec_get_frame_defaults(&s->frame);
265 avctx->coded_frame = &s->frame;
266 avctx->channel_layout = channel_mask;
271 * @brief Decode the subframe length.
273 * @param offset sample offset in the frame
274 * @return decoded subframe length on success, < 0 in case of an error
276 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
278 int frame_len_ratio, subframe_len, len;
280 /* no need to read from the bitstream when only one length is possible */
281 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
282 return s->min_samples_per_subframe;
284 len = av_log2(s->max_num_subframes - 1) + 1;
285 frame_len_ratio = get_bits(&s->gb, len);
286 subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
288 /* sanity check the length */
289 if (subframe_len < s->min_samples_per_subframe ||
290 subframe_len > s->samples_per_frame) {
291 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
293 return AVERROR_INVALIDDATA;
299 * @brief Decode how the data in the frame is split into subframes.
300 * Every WMA frame contains the encoded data for a fixed number of
301 * samples per channel. The data for every channel might be split
302 * into several subframes. This function will reconstruct the list of
303 * subframes for every channel.
305 * If the subframes are not evenly split, the algorithm estimates the
306 * channels with the lowest number of total samples.
307 * Afterwards, for each of these channels a bit is read from the
308 * bitstream that indicates if the channel contains a subframe with the
309 * next subframe size that is going to be read from the bitstream or not.
310 * If a channel contains such a subframe, the subframe size gets added to
311 * the channel's subframe list.
312 * The algorithm repeats these steps until the frame is properly divided
313 * between the individual channels.
316 * @return 0 on success, < 0 in case of an error
318 static int decode_tilehdr(WmallDecodeCtx *s)
320 uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
321 uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
322 int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
323 int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
324 int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
327 /* reset tiling information */
328 for (c = 0; c < s->num_channels; c++)
329 s->channel[c].num_subframes = 0;
331 tile_aligned = get_bits1(&s->gb);
332 if (s->max_num_subframes == 1 || tile_aligned)
333 fixed_channel_layout = 1;
335 /* loop until the frame data is split between the subframes */
337 int subframe_len, in_use = 0;
339 /* check which channels contain the subframe */
340 for (c = 0; c < s->num_channels; c++) {
341 if (num_samples[c] == min_channel_len) {
342 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
343 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
344 contains_subframe[c] = in_use = 1;
346 if (get_bits1(&s->gb))
347 contains_subframe[c] = in_use = 1;
350 contains_subframe[c] = 0;
354 av_log(s->avctx, AV_LOG_ERROR,
355 "Found empty subframe\n");
356 return AVERROR_INVALIDDATA;
359 /* get subframe length, subframe_len == 0 is not allowed */
360 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
361 return AVERROR_INVALIDDATA;
362 /* add subframes to the individual channels and find new min_channel_len */
363 min_channel_len += subframe_len;
364 for (c = 0; c < s->num_channels; c++) {
365 WmallChannelCtx *chan = &s->channel[c];
367 if (contains_subframe[c]) {
368 if (chan->num_subframes >= MAX_SUBFRAMES) {
369 av_log(s->avctx, AV_LOG_ERROR,
370 "broken frame: num subframes > 31\n");
371 return AVERROR_INVALIDDATA;
373 chan->subframe_len[chan->num_subframes] = subframe_len;
374 num_samples[c] += subframe_len;
375 ++chan->num_subframes;
376 if (num_samples[c] > s->samples_per_frame) {
377 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
378 "channel len(%d) > samples_per_frame(%d)\n",
379 num_samples[c], s->samples_per_frame);
380 return AVERROR_INVALIDDATA;
382 } else if (num_samples[c] <= min_channel_len) {
383 if (num_samples[c] < min_channel_len) {
384 channels_for_cur_subframe = 0;
385 min_channel_len = num_samples[c];
387 ++channels_for_cur_subframe;
390 } while (min_channel_len < s->samples_per_frame);
392 for (c = 0; c < s->num_channels; c++) {
394 for (i = 0; i < s->channel[c].num_subframes; i++) {
395 s->channel[c].subframe_offsets[i] = offset;
396 offset += s->channel[c].subframe_len[i];
403 static void decode_ac_filter(WmallDecodeCtx *s)
406 s->acfilter_order = get_bits(&s->gb, 4) + 1;
407 s->acfilter_scaling = get_bits(&s->gb, 4);
409 for (i = 0; i < s->acfilter_order; i++)
410 s->acfilter_coeffs[i] = (s->acfilter_scaling ?
411 get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
414 static void decode_mclms(WmallDecodeCtx *s)
416 s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
417 s->mclms_scaling = get_bits(&s->gb, 4);
418 if (get_bits1(&s->gb)) {
419 int i, send_coef_bits;
420 int cbits = av_log2(s->mclms_scaling + 1);
421 if (1 << cbits < s->mclms_scaling + 1)
424 send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
426 for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
427 s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
429 for (i = 0; i < s->num_channels; i++) {
431 for (c = 0; c < i; c++)
432 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
437 static int decode_cdlms(WmallDecodeCtx *s)
440 int cdlms_send_coef = get_bits1(&s->gb);
442 for (c = 0; c < s->num_channels; c++) {
443 s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
444 for (i = 0; i < s->cdlms_ttl[c]; i++) {
445 s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
446 if (s->cdlms[c][i].order > MAX_ORDER) {
447 av_log(s->avctx, AV_LOG_ERROR,
448 "Order[%d][%d] %d > max (%d), not supported\n",
449 c, i, s->cdlms[c][i].order, MAX_ORDER);
450 s->cdlms[0][0].order = 0;
451 return AVERROR_INVALIDDATA;
455 for (i = 0; i < s->cdlms_ttl[c]; i++)
456 s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
458 if (cdlms_send_coef) {
459 for (i = 0; i < s->cdlms_ttl[c]; i++) {
460 int cbits, shift_l, shift_r, j;
461 cbits = av_log2(s->cdlms[c][i].order);
462 if ((1 << cbits) < s->cdlms[c][i].order)
464 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
466 cbits = av_log2(s->cdlms[c][i].scaling + 1);
467 if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
470 s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
471 shift_l = 32 - s->cdlms[c][i].bitsend;
472 shift_r = 32 - s->cdlms[c][i].scaling - 2;
473 for (j = 0; j < s->cdlms[c][i].coefsend; j++)
474 s->cdlms[c][i].coefs[j] =
475 (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
483 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
486 unsigned int ave_mean;
487 s->transient[ch] = get_bits1(&s->gb);
488 if (s->transient[ch]) {
489 s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
490 if (s->transient_pos[ch])
491 s->transient[ch] = 0;
492 s->channel[ch].transient_counter =
493 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
494 } else if (s->channel[ch].transient_counter)
495 s->transient[ch] = 1;
497 if (s->seekable_tile) {
498 ave_mean = get_bits(&s->gb, s->bits_per_sample);
499 s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
502 if (s->seekable_tile) {
503 if (s->do_inter_ch_decorr)
504 s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
506 s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
509 for (; i < tile_size; i++) {
510 int quo = 0, rem, rem_bits, residue;
511 while(get_bits1(&s->gb)) {
513 if (get_bits_left(&s->gb) <= 0)
517 quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
519 ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
523 rem_bits = av_ceil_log2(ave_mean);
524 rem = get_bits_long(&s->gb, rem_bits);
525 residue = (quo << rem_bits) + rem;
528 s->ave_sum[ch] = residue + s->ave_sum[ch] -
529 (s->ave_sum[ch] >> s->movave_scaling);
532 residue = -(residue >> 1) - 1;
534 residue = residue >> 1;
535 s->channel_residues[ch][i] = residue;
542 static void decode_lpc(WmallDecodeCtx *s)
545 s->lpc_order = get_bits(&s->gb, 5) + 1;
546 s->lpc_scaling = get_bits(&s->gb, 4);
547 s->lpc_intbits = get_bits(&s->gb, 3) + 1;
548 cbits = s->lpc_scaling + s->lpc_intbits;
549 for (ch = 0; ch < s->num_channels; ch++)
550 for (i = 0; i < s->lpc_order; i++)
551 s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
554 static void clear_codec_buffers(WmallDecodeCtx *s)
558 memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
559 memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
560 memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
562 memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
563 memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
564 memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
565 memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
567 for (ich = 0; ich < s->num_channels; ich++) {
568 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
569 memset(s->cdlms[ich][ilms].coefs, 0,
570 sizeof(s->cdlms[ich][ilms].coefs));
571 memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
572 sizeof(s->cdlms[ich][ilms].lms_prevvalues));
573 memset(s->cdlms[ich][ilms].lms_updates, 0,
574 sizeof(s->cdlms[ich][ilms].lms_updates));
581 * @brief Reset filter parameters and transient area at new seekable tile.
583 static void reset_codec(WmallDecodeCtx *s)
586 s->mclms_recent = s->mclms_order * s->num_channels;
587 for (ich = 0; ich < s->num_channels; ich++) {
588 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
589 s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
590 /* first sample of a seekable subframe is considered as the starting of
591 a transient area which is samples_per_frame samples long */
592 s->channel[ich].transient_counter = s->samples_per_frame;
593 s->transient[ich] = 1;
594 s->transient_pos[ich] = 0;
598 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
600 int i, j, ich, pred_error;
601 int order = s->mclms_order;
602 int num_channels = s->num_channels;
603 int range = 1 << (s->bits_per_sample - 1);
605 for (ich = 0; ich < num_channels; ich++) {
606 pred_error = s->channel_residues[ich][icoef] - pred[ich];
607 if (pred_error > 0) {
608 for (i = 0; i < order * num_channels; i++)
609 s->mclms_coeffs[i + ich * order * num_channels] +=
610 s->mclms_updates[s->mclms_recent + i];
611 for (j = 0; j < ich; j++) {
612 if (s->channel_residues[j][icoef] > 0)
613 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
614 else if (s->channel_residues[j][icoef] < 0)
615 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
617 } else if (pred_error < 0) {
618 for (i = 0; i < order * num_channels; i++)
619 s->mclms_coeffs[i + ich * order * num_channels] -=
620 s->mclms_updates[s->mclms_recent + i];
621 for (j = 0; j < ich; j++) {
622 if (s->channel_residues[j][icoef] > 0)
623 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
624 else if (s->channel_residues[j][icoef] < 0)
625 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
630 for (ich = num_channels - 1; ich >= 0; ich--) {
632 s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
633 if (s->channel_residues[ich][icoef] > range - 1)
634 s->mclms_prevvalues[s->mclms_recent] = range - 1;
635 else if (s->channel_residues[ich][icoef] < -range)
636 s->mclms_prevvalues[s->mclms_recent] = -range;
638 s->mclms_updates[s->mclms_recent] = 0;
639 if (s->channel_residues[ich][icoef] > 0)
640 s->mclms_updates[s->mclms_recent] = 1;
641 else if (s->channel_residues[ich][icoef] < 0)
642 s->mclms_updates[s->mclms_recent] = -1;
645 if (s->mclms_recent == 0) {
646 memcpy(&s->mclms_prevvalues[order * num_channels],
648 2 * order * num_channels);
649 memcpy(&s->mclms_updates[order * num_channels],
651 2 * order * num_channels);
652 s->mclms_recent = num_channels * order;
656 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
659 int order = s->mclms_order;
660 int num_channels = s->num_channels;
662 for (ich = 0; ich < num_channels; ich++) {
664 if (!s->is_channel_coded[ich])
666 for (i = 0; i < order * num_channels; i++)
667 pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
668 s->mclms_coeffs[i + order * num_channels * ich];
669 for (i = 0; i < ich; i++)
670 pred[ich] += s->channel_residues[i][icoef] *
671 s->mclms_coeffs_cur[i + num_channels * ich];
672 pred[ich] += 1 << s->mclms_scaling - 1;
673 pred[ich] >>= s->mclms_scaling;
674 s->channel_residues[ich][icoef] += pred[ich];
678 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
680 int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
681 for (icoef = 0; icoef < tile_size; icoef++) {
682 mclms_predict(s, icoef, pred);
683 mclms_update(s, icoef, pred);
687 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
690 int recent = s->cdlms[ich][ilms].recent;
692 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
693 pred += s->cdlms[ich][ilms].coefs[icoef] *
694 s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
699 static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
700 int input, int residue)
703 int recent = s->cdlms[ich][ilms].recent;
704 int range = 1 << s->bits_per_sample - 1;
707 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
708 s->cdlms[ich][ilms].coefs[icoef] -=
709 s->cdlms[ich][ilms].lms_updates[icoef + recent];
710 } else if (residue > 0) {
711 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
712 s->cdlms[ich][ilms].coefs[icoef] +=
713 s->cdlms[ich][ilms].lms_updates[icoef + recent];
719 memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
720 s->cdlms[ich][ilms].lms_prevvalues,
721 2 * s->cdlms[ich][ilms].order);
722 memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
723 s->cdlms[ich][ilms].lms_updates,
724 2 * s->cdlms[ich][ilms].order);
725 recent = s->cdlms[ich][ilms].order - 1;
728 s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
730 s->cdlms[ich][ilms].lms_updates[recent] = 0;
732 s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
734 s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
736 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
737 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
738 s->cdlms[ich][ilms].recent = recent;
741 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
743 int ilms, recent, icoef;
744 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
745 recent = s->cdlms[ich][ilms].recent;
746 if (s->update_speed[ich] == 16)
749 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
750 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
752 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
753 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
756 s->update_speed[ich] = 16;
759 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
761 int ilms, recent, icoef;
762 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
763 recent = s->cdlms[ich][ilms].recent;
764 if (s->update_speed[ich] == 8)
767 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
768 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
770 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
771 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
773 s->update_speed[ich] = 8;
776 static void revert_cdlms(WmallDecodeCtx *s, int ch,
777 int coef_begin, int coef_end)
779 int icoef, pred, ilms, num_lms, residue, input;
781 num_lms = s->cdlms_ttl[ch];
782 for (ilms = num_lms - 1; ilms >= 0; ilms--) {
783 for (icoef = coef_begin; icoef < coef_end; icoef++) {
784 pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
785 residue = s->channel_residues[ch][icoef];
786 pred += lms_predict(s, ch, ilms);
787 input = residue + (pred >> s->cdlms[ch][ilms].scaling);
788 lms_update(s, ch, ilms, input, residue);
789 s->channel_residues[ch][icoef] = input;
794 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
796 if (s->num_channels != 2)
798 else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
800 for (icoef = 0; icoef < tile_size; icoef++) {
801 s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
802 s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
807 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
810 int64_t *filter_coeffs = s->acfilter_coeffs;
811 int scaling = s->acfilter_scaling;
812 int order = s->acfilter_order;
814 for (ich = 0; ich < s->num_channels; ich++) {
815 int *prevvalues = s->acfilter_prevvalues[ich];
816 for (i = 0; i < order; i++) {
818 for (j = 0; j < order; j++) {
820 pred += filter_coeffs[j] * prevvalues[j - i];
822 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
825 s->channel_residues[ich][i] += pred;
827 for (i = order; i < tile_size; i++) {
829 for (j = 0; j < order; j++)
830 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
832 s->channel_residues[ich][i] += pred;
834 for (j = 0; j < order; j++)
835 prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
839 static int decode_subframe(WmallDecodeCtx *s)
841 int offset = s->samples_per_frame;
842 int subframe_len = s->samples_per_frame;
843 int total_samples = s->samples_per_frame * s->num_channels;
844 int i, j, rawpcm_tile, padding_zeroes, res;
846 s->subframe_offset = get_bits_count(&s->gb);
848 /* reset channel context and find the next block offset and size
849 == the next block of the channel with the smallest number of
851 for (i = 0; i < s->num_channels; i++) {
852 if (offset > s->channel[i].decoded_samples) {
853 offset = s->channel[i].decoded_samples;
855 s->channel[i].subframe_len[s->channel[i].cur_subframe];
859 /* get a list of all channels that contain the estimated block */
860 s->channels_for_cur_subframe = 0;
861 for (i = 0; i < s->num_channels; i++) {
862 const int cur_subframe = s->channel[i].cur_subframe;
863 /* subtract already processed samples */
864 total_samples -= s->channel[i].decoded_samples;
866 /* and count if there are multiple subframes that match our profile */
867 if (offset == s->channel[i].decoded_samples &&
868 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
869 total_samples -= s->channel[i].subframe_len[cur_subframe];
870 s->channel[i].decoded_samples +=
871 s->channel[i].subframe_len[cur_subframe];
872 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
873 ++s->channels_for_cur_subframe;
877 /* check if the frame will be complete after processing the
880 s->parsed_all_subframes = 1;
883 s->seekable_tile = get_bits1(&s->gb);
884 if (s->seekable_tile) {
885 clear_codec_buffers(s);
887 s->do_arith_coding = get_bits1(&s->gb);
888 if (s->do_arith_coding) {
889 avpriv_request_sample(s->avctx, "Arithmetic coding");
890 return AVERROR_PATCHWELCOME;
892 s->do_ac_filter = get_bits1(&s->gb);
893 s->do_inter_ch_decorr = get_bits1(&s->gb);
894 s->do_mclms = get_bits1(&s->gb);
902 if ((res = decode_cdlms(s)) < 0)
904 s->movave_scaling = get_bits(&s->gb, 3);
905 s->quant_stepsize = get_bits(&s->gb, 8) + 1;
908 } else if (!s->cdlms[0][0].order) {
909 av_log(s->avctx, AV_LOG_DEBUG,
910 "Waiting for seekable tile\n");
911 s->frame.nb_samples = 0;
915 rawpcm_tile = get_bits1(&s->gb);
917 for (i = 0; i < s->num_channels; i++)
918 s->is_channel_coded[i] = 1;
921 for (i = 0; i < s->num_channels; i++)
922 s->is_channel_coded[i] = get_bits1(&s->gb);
926 s->do_lpc = get_bits1(&s->gb);
929 avpriv_request_sample(s->avctx, "Expect wrong output since "
930 "inverse LPC filter");
937 if (get_bits1(&s->gb))
938 padding_zeroes = get_bits(&s->gb, 5);
943 int bits = s->bits_per_sample - padding_zeroes;
945 av_log(s->avctx, AV_LOG_ERROR,
946 "Invalid number of padding bits in raw PCM tile\n");
947 return AVERROR_INVALIDDATA;
949 av_dlog(s->avctx, "RAWPCM %d bits per sample. "
950 "total %d bits, remain=%d\n", bits,
951 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
952 for (i = 0; i < s->num_channels; i++)
953 for (j = 0; j < subframe_len; j++)
954 s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
956 for (i = 0; i < s->num_channels; i++)
957 if (s->is_channel_coded[i]) {
958 decode_channel_residues(s, i, subframe_len);
959 if (s->seekable_tile)
960 use_high_update_speed(s, i);
962 use_normal_update_speed(s, i);
963 revert_cdlms(s, i, 0, subframe_len);
965 memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
969 revert_mclms(s, subframe_len);
970 if (s->do_inter_ch_decorr)
971 revert_inter_ch_decorr(s, subframe_len);
973 revert_acfilter(s, subframe_len);
976 if (s->quant_stepsize != 1)
977 for (i = 0; i < s->num_channels; i++)
978 for (j = 0; j < subframe_len; j++)
979 s->channel_residues[i][j] *= s->quant_stepsize;
981 /* Write to proper output buffer depending on bit-depth */
982 for (i = 0; i < s->channels_for_cur_subframe; i++) {
983 int c = s->channel_indexes_for_cur_subframe[i];
984 int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
986 for (j = 0; j < subframe_len; j++) {
987 if (s->bits_per_sample == 16) {
988 *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
990 *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
995 /* handled one subframe */
996 for (i = 0; i < s->channels_for_cur_subframe; i++) {
997 int c = s->channel_indexes_for_cur_subframe[i];
998 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
999 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1000 return AVERROR_INVALIDDATA;
1002 ++s->channel[c].cur_subframe;
1008 * @brief Decode one WMA frame.
1009 * @param s codec context
1010 * @return 0 if the trailer bit indicates that this is the last frame,
1011 * 1 if there are additional frames
1013 static int decode_frame(WmallDecodeCtx *s)
1015 GetBitContext* gb = &s->gb;
1016 int more_frames = 0, len = 0, i, ret;
1018 s->frame.nb_samples = s->samples_per_frame;
1019 if ((ret = ff_get_buffer(s->avctx, &s->frame, 0)) < 0) {
1020 /* return an error if no frame could be decoded at all */
1024 for (i = 0; i < s->num_channels; i++) {
1025 s->samples_16[i] = (int16_t *)s->frame.extended_data[i];
1026 s->samples_32[i] = (int32_t *)s->frame.extended_data[i];
1029 /* get frame length */
1031 len = get_bits(gb, s->log2_frame_size);
1033 /* decode tile information */
1034 if (decode_tilehdr(s)) {
1040 if (s->dynamic_range_compression)
1041 s->drc_gain = get_bits(gb, 8);
1043 /* no idea what these are for, might be the number of samples
1044 that need to be skipped at the beginning or end of a stream */
1045 if (get_bits1(gb)) {
1048 /* usually true for the first frame */
1049 if (get_bits1(gb)) {
1050 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1051 av_dlog(s->avctx, "start skip: %i\n", skip);
1054 /* sometimes true for the last frame */
1055 if (get_bits1(gb)) {
1056 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1057 av_dlog(s->avctx, "end skip: %i\n", skip);
1062 /* reset subframe states */
1063 s->parsed_all_subframes = 0;
1064 for (i = 0; i < s->num_channels; i++) {
1065 s->channel[i].decoded_samples = 0;
1066 s->channel[i].cur_subframe = 0;
1069 /* decode all subframes */
1070 while (!s->parsed_all_subframes) {
1071 if (decode_subframe(s) < 0) {
1077 av_dlog(s->avctx, "Frame done\n");
1082 if (s->len_prefix) {
1083 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1084 /* FIXME: not sure if this is always an error */
1085 av_log(s->avctx, AV_LOG_ERROR,
1086 "frame[%i] would have to skip %i bits\n", s->frame_num,
1087 len - (get_bits_count(gb) - s->frame_offset) - 1);
1092 /* skip the rest of the frame data */
1093 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1096 /* decode trailer bit */
1097 more_frames = get_bits1(gb);
1103 * @brief Calculate remaining input buffer length.
1104 * @param s codec context
1105 * @param gb bitstream reader context
1106 * @return remaining size in bits
1108 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
1110 return s->buf_bit_size - get_bits_count(gb);
1114 * @brief Fill the bit reservoir with a (partial) frame.
1115 * @param s codec context
1116 * @param gb bitstream reader context
1117 * @param len length of the partial frame
1118 * @param append decides whether to reset the buffer or not
1120 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1126 /* when the frame data does not need to be concatenated, the input buffer
1127 is reset and additional bits from the previous frame are copied
1128 and skipped later so that a fast byte copy is possible */
1131 s->frame_offset = get_bits_count(gb) & 7;
1132 s->num_saved_bits = s->frame_offset;
1133 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1136 buflen = (s->num_saved_bits + len + 8) >> 3;
1138 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1139 avpriv_request_sample(s->avctx, "Too small input buffer");
1144 s->num_saved_bits += len;
1146 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1149 int align = 8 - (get_bits_count(gb) & 7);
1150 align = FFMIN(align, len);
1151 put_bits(&s->pb, align, get_bits(gb, align));
1153 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1155 skip_bits_long(gb, len);
1158 flush_put_bits(&tmp);
1160 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1161 skip_bits(&s->gb, s->frame_offset);
1164 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1167 WmallDecodeCtx *s = avctx->priv_data;
1168 GetBitContext* gb = &s->pgb;
1169 const uint8_t* buf = avpkt->data;
1170 int buf_size = avpkt->size;
1171 int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1173 s->frame.nb_samples = 0;
1175 if (s->packet_done || s->packet_loss) {
1178 /* sanity check for the buffer length */
1179 if (buf_size < avctx->block_align)
1182 s->next_packet_start = buf_size - avctx->block_align;
1183 buf_size = avctx->block_align;
1184 s->buf_bit_size = buf_size << 3;
1186 /* parse packet header */
1187 init_get_bits(gb, buf, s->buf_bit_size);
1188 packet_sequence_number = get_bits(gb, 4);
1189 skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1190 spliced_packet = get_bits1(gb);
1192 avpriv_request_sample(avctx, "Bitstream splicing");
1194 /* get number of bits that need to be added to the previous frame */
1195 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1197 /* check for packet loss */
1198 if (!s->packet_loss &&
1199 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1201 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1202 s->packet_sequence_number, packet_sequence_number);
1204 s->packet_sequence_number = packet_sequence_number;
1206 if (num_bits_prev_frame > 0) {
1207 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1208 if (num_bits_prev_frame >= remaining_packet_bits) {
1209 num_bits_prev_frame = remaining_packet_bits;
1213 /* Append the previous frame data to the remaining data from the
1214 * previous packet to create a full frame. */
1215 save_bits(s, gb, num_bits_prev_frame, 1);
1217 /* decode the cross packet frame if it is valid */
1218 if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1220 } else if (s->num_saved_bits - s->frame_offset) {
1221 av_dlog(avctx, "ignoring %x previously saved bits\n",
1222 s->num_saved_bits - s->frame_offset);
1225 if (s->packet_loss) {
1226 /* Reset number of saved bits so that the decoder does not start
1227 * to decode incomplete frames in the s->len_prefix == 0 case. */
1228 s->num_saved_bits = 0;
1230 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1236 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1237 init_get_bits(gb, avpkt->data, s->buf_bit_size);
1238 skip_bits(gb, s->packet_offset);
1240 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1241 (frame_size = show_bits(gb, s->log2_frame_size)) &&
1242 frame_size <= remaining_bits(s, gb)) {
1243 save_bits(s, gb, frame_size, 0);
1244 s->packet_done = !decode_frame(s);
1245 } else if (!s->len_prefix
1246 && s->num_saved_bits > get_bits_count(&s->gb)) {
1247 /* when the frames do not have a length prefix, we don't know the
1248 * compressed length of the individual frames however, we know what
1249 * part of a new packet belongs to the previous frame therefore we
1250 * save the incoming packet first, then we append the "previous
1251 * frame" data from the next packet so that we get a buffer that
1252 * only contains full frames */
1253 s->packet_done = !decode_frame(s);
1259 if (s->packet_done && !s->packet_loss &&
1260 remaining_bits(s, gb) > 0) {
1261 /* save the rest of the data so that it can be decoded
1262 * with the next packet */
1263 save_bits(s, gb, remaining_bits(s, gb), 0);
1266 *got_frame_ptr = s->frame.nb_samples > 0;
1267 av_frame_move_ref(data, &s->frame);
1269 s->packet_offset = get_bits_count(gb) & 7;
1271 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1274 static void flush(AVCodecContext *avctx)
1276 WmallDecodeCtx *s = avctx->priv_data;
1279 s->num_saved_bits = 0;
1280 s->frame_offset = 0;
1281 s->next_packet_start = 0;
1282 s->cdlms[0][0].order = 0;
1283 s->frame.nb_samples = 0;
1284 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1287 AVCodec ff_wmalossless_decoder = {
1288 .name = "wmalossless",
1289 .type = AVMEDIA_TYPE_AUDIO,
1290 .id = AV_CODEC_ID_WMALOSSLESS,
1291 .priv_data_size = sizeof(WmallDecodeCtx),
1292 .init = decode_init,
1293 .decode = decode_packet,
1295 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1296 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1297 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1299 AV_SAMPLE_FMT_NONE },