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
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
34 #include "lossless_audiodsp.h"
36 #include "wma_common.h"
38 /** current decoder limitations */
39 #define WMALL_MAX_CHANNELS 8 ///< max number of handled channels
40 #define MAX_SUBFRAMES 32 ///< max number of subframes per channel
41 #define MAX_BANDS 29 ///< max number of scale factor bands
42 #define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
45 #define WMALL_BLOCK_MIN_BITS 6 ///< log2 of min block size
46 #define WMALL_BLOCK_MAX_BITS 14 ///< log2 of max block size
47 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS) ///< maximum block size
48 #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1) ///< possible block sizes
50 #define WMALL_COEFF_PAD_SIZE 16 ///< pad coef buffers with 0 for use with SIMD
53 * @brief frame-specific decoder context for a single channel
55 typedef struct WmallChannelCtx {
56 int16_t prev_block_len; ///< length of the previous block
57 uint8_t transmit_coefs;
58 uint8_t num_subframes;
59 uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
60 uint16_t subframe_offsets[MAX_SUBFRAMES]; ///< subframe positions in the current frame
61 uint8_t cur_subframe; ///< current subframe number
62 uint16_t decoded_samples; ///< number of already processed samples
63 int quant_step; ///< quantization step for the current subframe
64 int transient_counter; ///< number of transient samples from the beginning of the transient zone
68 * @brief main decoder context
70 typedef struct WmallDecodeCtx {
71 /* generic decoder variables */
72 AVCodecContext *avctx;
74 LLAudDSPContext dsp; ///< accelerated DSP functions
75 uint8_t *frame_data; ///< compressed frame data
76 int max_frame_size; ///< max bitstream size
77 PutBitContext pb; ///< context for filling the frame_data buffer
79 /* frame size dependent frame information (set during initialization) */
80 uint32_t decode_flags; ///< used compression features
81 int len_prefix; ///< frame is prefixed with its length
82 int dynamic_range_compression; ///< frame contains DRC data
83 uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
84 uint16_t samples_per_frame; ///< number of samples to output
85 uint16_t log2_frame_size;
86 int8_t num_channels; ///< number of channels in the stream (same as AVCodecContext.num_channels)
87 int8_t lfe_channel; ///< lfe channel index
88 uint8_t max_num_subframes;
89 uint8_t subframe_len_bits; ///< number of bits used for the subframe length
90 uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
91 uint16_t min_samples_per_subframe;
93 /* packet decode state */
94 GetBitContext pgb; ///< bitstream reader context for the packet
95 int next_packet_start; ///< start offset of the next WMA packet in the demuxer packet
96 uint8_t packet_offset; ///< offset to the frame in the packet
97 uint8_t packet_sequence_number; ///< current packet number
98 int num_saved_bits; ///< saved number of bits
99 int frame_offset; ///< frame offset in the bit reservoir
100 int subframe_offset; ///< subframe offset in the bit reservoir
101 uint8_t packet_loss; ///< set in case of bitstream error
102 uint8_t packet_done; ///< set when a packet is fully decoded
104 /* frame decode state */
105 uint32_t frame_num; ///< current frame number (not used for decoding)
106 GetBitContext gb; ///< bitstream reader context
107 int buf_bit_size; ///< buffer size in bits
108 int16_t *samples_16[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (16-bit)
109 int32_t *samples_32[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (24-bit)
110 uint8_t drc_gain; ///< gain for the DRC tool
111 int8_t skip_frame; ///< skip output step
112 int8_t parsed_all_subframes; ///< all subframes decoded?
114 /* subframe/block decode state */
115 int16_t subframe_len; ///< current subframe length
116 int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
117 int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
119 WmallChannelCtx channel[WMALL_MAX_CHANNELS]; ///< per channel data
121 // WMA Lossless-specific
123 uint8_t do_arith_coding;
124 uint8_t do_ac_filter;
125 uint8_t do_inter_ch_decorr;
129 int8_t acfilter_order;
130 int8_t acfilter_scaling;
131 int16_t acfilter_coeffs[16];
132 int acfilter_prevvalues[WMALL_MAX_CHANNELS][16];
135 int8_t mclms_scaling;
136 int16_t mclms_coeffs[WMALL_MAX_CHANNELS * WMALL_MAX_CHANNELS * 32];
137 int16_t mclms_coeffs_cur[WMALL_MAX_CHANNELS * WMALL_MAX_CHANNELS];
138 int32_t mclms_prevvalues[WMALL_MAX_CHANNELS * 2 * 32];
139 int32_t mclms_updates[WMALL_MAX_CHANNELS * 2 * 32];
150 DECLARE_ALIGNED(16, int32_t, coefs)[MAX_ORDER + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
151 DECLARE_ALIGNED(16, int32_t, lms_prevvalues)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
152 DECLARE_ALIGNED(16, int32_t, lms_updates)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
154 } cdlms[WMALL_MAX_CHANNELS][9];
156 int cdlms_ttl[WMALL_MAX_CHANNELS];
160 int is_channel_coded[WMALL_MAX_CHANNELS];
161 int update_speed[WMALL_MAX_CHANNELS];
163 int transient[WMALL_MAX_CHANNELS];
164 int transient_pos[WMALL_MAX_CHANNELS];
167 int ave_sum[WMALL_MAX_CHANNELS];
169 int channel_residues[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
171 int lpc_coefs[WMALL_MAX_CHANNELS][40];
176 int channel_coeffs[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
179 /** Get sign of integer (1 for positive, -1 for negative and 0 for zero) */
180 #define WMASIGN(x) (((x) > 0) - ((x) < 0))
182 static av_cold int decode_init(AVCodecContext *avctx)
184 WmallDecodeCtx *s = avctx->priv_data;
185 uint8_t *edata_ptr = avctx->extradata;
186 unsigned int channel_mask;
187 int i, log2_max_num_subframes;
189 if (!avctx->block_align) {
190 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
191 return AVERROR(EINVAL);
194 s->max_frame_size = MAX_FRAMESIZE * avctx->channels;
195 s->frame_data = av_mallocz(s->max_frame_size + AV_INPUT_BUFFER_PADDING_SIZE);
197 return AVERROR(ENOMEM);
200 ff_llauddsp_init(&s->dsp);
201 init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
203 if (avctx->extradata_size >= 18) {
204 s->decode_flags = AV_RL16(edata_ptr + 14);
205 channel_mask = AV_RL32(edata_ptr + 2);
206 s->bits_per_sample = AV_RL16(edata_ptr);
207 if (s->bits_per_sample == 16)
208 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
209 else if (s->bits_per_sample == 24) {
210 av_log(avctx, AV_LOG_WARNING, "Decoding audio at 24 bit-depth\n");
211 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
212 avctx->bits_per_raw_sample = 24;
214 av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %"PRIu8"\n",
216 return AVERROR_INVALIDDATA;
218 /* dump the extradata */
219 for (i = 0; i < avctx->extradata_size; i++)
220 ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
221 ff_dlog(avctx, "\n");
224 avpriv_request_sample(avctx, "Unsupported extradata size");
225 return AVERROR_PATCHWELCOME;
229 s->log2_frame_size = av_log2(avctx->block_align) + 4;
232 s->skip_frame = 1; /* skip first frame */
234 s->len_prefix = s->decode_flags & 0x40;
237 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
239 av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
241 /* init previous block len */
242 for (i = 0; i < avctx->channels; i++)
243 s->channel[i].prev_block_len = s->samples_per_frame;
246 log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
247 s->max_num_subframes = 1 << log2_max_num_subframes;
248 s->max_subframe_len_bit = 0;
249 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
251 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
252 s->dynamic_range_compression = s->decode_flags & 0x80;
253 s->bV3RTM = s->decode_flags & 0x100;
255 if (s->max_num_subframes > MAX_SUBFRAMES) {
256 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRIu8"\n",
257 s->max_num_subframes);
258 return AVERROR_INVALIDDATA;
261 s->num_channels = avctx->channels;
263 /* extract lfe channel position */
266 if (channel_mask & 8) {
268 for (mask = 1; mask < 16; mask <<= 1)
269 if (channel_mask & mask)
273 if (s->num_channels < 0) {
274 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %"PRId8"\n",
276 return AVERROR_INVALIDDATA;
277 } else if (s->num_channels > WMALL_MAX_CHANNELS) {
278 avpriv_request_sample(avctx,
279 "More than %d channels", WMALL_MAX_CHANNELS);
280 return AVERROR_PATCHWELCOME;
283 s->frame = av_frame_alloc();
285 return AVERROR(ENOMEM);
287 avctx->channel_layout = channel_mask;
292 * @brief Decode the subframe length.
294 * @param offset sample offset in the frame
295 * @return decoded subframe length on success, < 0 in case of an error
297 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
299 int frame_len_ratio, subframe_len, len;
301 /* no need to read from the bitstream when only one length is possible */
302 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
303 return s->min_samples_per_subframe;
305 len = av_log2(s->max_num_subframes - 1) + 1;
306 frame_len_ratio = get_bits(&s->gb, len);
307 subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
309 /* sanity check the length */
310 if (subframe_len < s->min_samples_per_subframe ||
311 subframe_len > s->samples_per_frame) {
312 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
314 return AVERROR_INVALIDDATA;
320 * @brief Decode how the data in the frame is split into subframes.
321 * Every WMA frame contains the encoded data for a fixed number of
322 * samples per channel. The data for every channel might be split
323 * into several subframes. This function will reconstruct the list of
324 * subframes for every channel.
326 * If the subframes are not evenly split, the algorithm estimates the
327 * channels with the lowest number of total samples.
328 * Afterwards, for each of these channels a bit is read from the
329 * bitstream that indicates if the channel contains a subframe with the
330 * next subframe size that is going to be read from the bitstream or not.
331 * If a channel contains such a subframe, the subframe size gets added to
332 * the channel's subframe list.
333 * The algorithm repeats these steps until the frame is properly divided
334 * between the individual channels.
337 * @return 0 on success, < 0 in case of an error
339 static int decode_tilehdr(WmallDecodeCtx *s)
341 uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
342 uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
343 int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
344 int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
345 int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
348 /* reset tiling information */
349 for (c = 0; c < s->num_channels; c++)
350 s->channel[c].num_subframes = 0;
352 tile_aligned = get_bits1(&s->gb);
353 if (s->max_num_subframes == 1 || tile_aligned)
354 fixed_channel_layout = 1;
356 /* loop until the frame data is split between the subframes */
358 int subframe_len, in_use = 0;
360 /* check which channels contain the subframe */
361 for (c = 0; c < s->num_channels; c++) {
362 if (num_samples[c] == min_channel_len) {
363 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
364 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
365 contains_subframe[c] = 1;
367 contains_subframe[c] = get_bits1(&s->gb);
369 in_use |= contains_subframe[c];
371 contains_subframe[c] = 0;
375 av_log(s->avctx, AV_LOG_ERROR,
376 "Found empty subframe\n");
377 return AVERROR_INVALIDDATA;
380 /* get subframe length, subframe_len == 0 is not allowed */
381 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
382 return AVERROR_INVALIDDATA;
383 /* add subframes to the individual channels and find new min_channel_len */
384 min_channel_len += subframe_len;
385 for (c = 0; c < s->num_channels; c++) {
386 WmallChannelCtx *chan = &s->channel[c];
388 if (contains_subframe[c]) {
389 if (chan->num_subframes >= MAX_SUBFRAMES) {
390 av_log(s->avctx, AV_LOG_ERROR,
391 "broken frame: num subframes > 31\n");
392 return AVERROR_INVALIDDATA;
394 chan->subframe_len[chan->num_subframes] = subframe_len;
395 num_samples[c] += subframe_len;
396 ++chan->num_subframes;
397 if (num_samples[c] > s->samples_per_frame) {
398 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
399 "channel len(%"PRIu16") > samples_per_frame(%"PRIu16")\n",
400 num_samples[c], s->samples_per_frame);
401 return AVERROR_INVALIDDATA;
403 } else if (num_samples[c] <= min_channel_len) {
404 if (num_samples[c] < min_channel_len) {
405 channels_for_cur_subframe = 0;
406 min_channel_len = num_samples[c];
408 ++channels_for_cur_subframe;
411 } while (min_channel_len < s->samples_per_frame);
413 for (c = 0; c < s->num_channels; c++) {
415 for (i = 0; i < s->channel[c].num_subframes; i++) {
416 s->channel[c].subframe_offsets[i] = offset;
417 offset += s->channel[c].subframe_len[i];
424 static void decode_ac_filter(WmallDecodeCtx *s)
427 s->acfilter_order = get_bits(&s->gb, 4) + 1;
428 s->acfilter_scaling = get_bits(&s->gb, 4);
430 for (i = 0; i < s->acfilter_order; i++)
431 s->acfilter_coeffs[i] = get_bitsz(&s->gb, s->acfilter_scaling) + 1;
434 static void decode_mclms(WmallDecodeCtx *s)
436 s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
437 s->mclms_scaling = get_bits(&s->gb, 4);
438 if (get_bits1(&s->gb)) {
439 int i, send_coef_bits;
440 int cbits = av_log2(s->mclms_scaling + 1);
441 if (1 << cbits < s->mclms_scaling + 1)
444 send_coef_bits = get_bitsz(&s->gb, cbits) + 2;
446 for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
447 s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
449 for (i = 0; i < s->num_channels; i++) {
451 for (c = 0; c < i; c++)
452 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
457 static int decode_cdlms(WmallDecodeCtx *s)
460 int cdlms_send_coef = get_bits1(&s->gb);
462 for (c = 0; c < s->num_channels; c++) {
463 s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
464 for (i = 0; i < s->cdlms_ttl[c]; i++) {
465 s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
466 if (s->cdlms[c][i].order > MAX_ORDER) {
467 av_log(s->avctx, AV_LOG_ERROR,
468 "Order[%d][%d] %d > max (%d), not supported\n",
469 c, i, s->cdlms[c][i].order, MAX_ORDER);
470 s->cdlms[0][0].order = 0;
471 return AVERROR_INVALIDDATA;
473 if(s->cdlms[c][i].order & 8) {
476 avpriv_request_sample(s->avctx, "CDLMS of order %d",
477 s->cdlms[c][i].order);
482 for (i = 0; i < s->cdlms_ttl[c]; i++)
483 s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
485 if (cdlms_send_coef) {
486 for (i = 0; i < s->cdlms_ttl[c]; i++) {
487 int cbits, shift_l, shift_r, j;
488 cbits = av_log2(s->cdlms[c][i].order);
489 if ((1 << cbits) < s->cdlms[c][i].order)
491 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
493 cbits = av_log2(s->cdlms[c][i].scaling + 1);
494 if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
497 s->cdlms[c][i].bitsend = get_bitsz(&s->gb, cbits) + 2;
498 shift_l = 32 - s->cdlms[c][i].bitsend;
499 shift_r = 32 - s->cdlms[c][i].scaling - 2;
500 for (j = 0; j < s->cdlms[c][i].coefsend; j++)
501 s->cdlms[c][i].coefs[j] =
502 (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
506 for (i = 0; i < s->cdlms_ttl[c]; i++)
507 memset(s->cdlms[c][i].coefs + s->cdlms[c][i].order,
508 0, WMALL_COEFF_PAD_SIZE);
514 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
517 unsigned int ave_mean;
518 s->transient[ch] = get_bits1(&s->gb);
519 if (s->transient[ch]) {
520 s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
521 if (s->transient_pos[ch])
522 s->transient[ch] = 0;
523 s->channel[ch].transient_counter =
524 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
525 } else if (s->channel[ch].transient_counter)
526 s->transient[ch] = 1;
528 if (s->seekable_tile) {
529 ave_mean = get_bits(&s->gb, s->bits_per_sample);
530 s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
533 if (s->seekable_tile) {
534 if (s->do_inter_ch_decorr)
535 s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
537 s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
540 for (; i < tile_size; i++) {
541 int quo = 0, rem, rem_bits, residue;
542 while(get_bits1(&s->gb)) {
544 if (get_bits_left(&s->gb) <= 0)
548 quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
550 ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
554 rem_bits = av_ceil_log2(ave_mean);
555 rem = get_bits_long(&s->gb, rem_bits);
556 residue = (quo << rem_bits) + rem;
559 s->ave_sum[ch] = residue + s->ave_sum[ch] -
560 (s->ave_sum[ch] >> s->movave_scaling);
562 residue = (residue >> 1) ^ -(residue & 1);
563 s->channel_residues[ch][i] = residue;
570 static void decode_lpc(WmallDecodeCtx *s)
573 s->lpc_order = get_bits(&s->gb, 5) + 1;
574 s->lpc_scaling = get_bits(&s->gb, 4);
575 s->lpc_intbits = get_bits(&s->gb, 3) + 1;
576 cbits = s->lpc_scaling + s->lpc_intbits;
577 for (ch = 0; ch < s->num_channels; ch++)
578 for (i = 0; i < s->lpc_order; i++)
579 s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
582 static void clear_codec_buffers(WmallDecodeCtx *s)
586 memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
587 memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
588 memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
590 memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
591 memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
592 memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
593 memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
595 for (ich = 0; ich < s->num_channels; ich++) {
596 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
597 memset(s->cdlms[ich][ilms].coefs, 0,
598 sizeof(s->cdlms[ich][ilms].coefs));
599 memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
600 sizeof(s->cdlms[ich][ilms].lms_prevvalues));
601 memset(s->cdlms[ich][ilms].lms_updates, 0,
602 sizeof(s->cdlms[ich][ilms].lms_updates));
609 * @brief Reset filter parameters and transient area at new seekable tile.
611 static void reset_codec(WmallDecodeCtx *s)
614 s->mclms_recent = s->mclms_order * s->num_channels;
615 for (ich = 0; ich < s->num_channels; ich++) {
616 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
617 s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
618 /* first sample of a seekable subframe is considered as the starting of
619 a transient area which is samples_per_frame samples long */
620 s->channel[ich].transient_counter = s->samples_per_frame;
621 s->transient[ich] = 1;
622 s->transient_pos[ich] = 0;
626 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
628 int i, j, ich, pred_error;
629 int order = s->mclms_order;
630 int num_channels = s->num_channels;
631 int range = 1 << (s->bits_per_sample - 1);
633 for (ich = 0; ich < num_channels; ich++) {
634 pred_error = s->channel_residues[ich][icoef] - pred[ich];
635 if (pred_error > 0) {
636 for (i = 0; i < order * num_channels; i++)
637 s->mclms_coeffs[i + ich * order * num_channels] +=
638 s->mclms_updates[s->mclms_recent + i];
639 for (j = 0; j < ich; j++)
640 s->mclms_coeffs_cur[ich * num_channels + j] += WMASIGN(s->channel_residues[j][icoef]);
641 } else if (pred_error < 0) {
642 for (i = 0; i < order * num_channels; i++)
643 s->mclms_coeffs[i + ich * order * num_channels] -=
644 s->mclms_updates[s->mclms_recent + i];
645 for (j = 0; j < ich; j++)
646 s->mclms_coeffs_cur[ich * num_channels + j] -= WMASIGN(s->channel_residues[j][icoef]);
650 for (ich = num_channels - 1; ich >= 0; ich--) {
652 s->mclms_prevvalues[s->mclms_recent] = av_clip(s->channel_residues[ich][icoef],
654 s->mclms_updates[s->mclms_recent] = WMASIGN(s->channel_residues[ich][icoef]);
657 if (s->mclms_recent == 0) {
658 memcpy(&s->mclms_prevvalues[order * num_channels],
660 sizeof(int32_t) * order * num_channels);
661 memcpy(&s->mclms_updates[order * num_channels],
663 sizeof(int32_t) * order * num_channels);
664 s->mclms_recent = num_channels * order;
668 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
671 int order = s->mclms_order;
672 int num_channels = s->num_channels;
674 for (ich = 0; ich < num_channels; ich++) {
676 if (!s->is_channel_coded[ich])
678 for (i = 0; i < order * num_channels; i++)
679 pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
680 s->mclms_coeffs[i + order * num_channels * ich];
681 for (i = 0; i < ich; i++)
682 pred[ich] += s->channel_residues[i][icoef] *
683 s->mclms_coeffs_cur[i + num_channels * ich];
684 pred[ich] += 1 << s->mclms_scaling - 1;
685 pred[ich] >>= s->mclms_scaling;
686 s->channel_residues[ich][icoef] += pred[ich];
690 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
692 int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
693 for (icoef = 0; icoef < tile_size; icoef++) {
694 mclms_predict(s, icoef, pred);
695 mclms_update(s, icoef, pred);
699 static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input)
701 int recent = s->cdlms[ich][ilms].recent;
702 int range = 1 << s->bits_per_sample - 1;
703 int order = s->cdlms[ich][ilms].order;
708 memcpy(s->cdlms[ich][ilms].lms_prevvalues + order,
709 s->cdlms[ich][ilms].lms_prevvalues, sizeof(*s->cdlms[ich][ilms].lms_prevvalues) * order);
710 memcpy(s->cdlms[ich][ilms].lms_updates + order,
711 s->cdlms[ich][ilms].lms_updates, sizeof(*s->cdlms[ich][ilms].lms_updates) * order);
715 s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
716 s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich];
718 s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2;
719 s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1;
720 s->cdlms[ich][ilms].recent = recent;
721 memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0,
722 sizeof(s->cdlms[ich][ilms].lms_updates) - 4*(recent+order));
725 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
727 int ilms, recent, icoef;
728 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
729 recent = s->cdlms[ich][ilms].recent;
730 if (s->update_speed[ich] == 16)
733 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
734 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
736 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
737 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
740 s->update_speed[ich] = 16;
743 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
745 int ilms, recent, icoef;
746 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
747 recent = s->cdlms[ich][ilms].recent;
748 if (s->update_speed[ich] == 8)
751 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
752 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
754 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
755 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
757 s->update_speed[ich] = 8;
760 static void revert_cdlms(WmallDecodeCtx *s, int ch,
761 int coef_begin, int coef_end)
763 int icoef, pred, ilms, num_lms, residue, input;
765 num_lms = s->cdlms_ttl[ch];
766 for (ilms = num_lms - 1; ilms >= 0; ilms--) {
767 for (icoef = coef_begin; icoef < coef_end; icoef++) {
768 pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
769 residue = s->channel_residues[ch][icoef];
770 pred += s->dsp.scalarproduct_and_madd_int32(s->cdlms[ch][ilms].coefs,
771 s->cdlms[ch][ilms].lms_prevvalues
772 + s->cdlms[ch][ilms].recent,
773 s->cdlms[ch][ilms].lms_updates
774 + s->cdlms[ch][ilms].recent,
775 FFALIGN(s->cdlms[ch][ilms].order,
776 WMALL_COEFF_PAD_SIZE),
778 input = residue + (pred >> s->cdlms[ch][ilms].scaling);
779 lms_update(s, ch, ilms, input);
780 s->channel_residues[ch][icoef] = input;
786 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
788 if (s->num_channels != 2)
790 else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
792 for (icoef = 0; icoef < tile_size; icoef++) {
793 s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
794 s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
799 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
802 int16_t *filter_coeffs = s->acfilter_coeffs;
803 int scaling = s->acfilter_scaling;
804 int order = s->acfilter_order;
806 for (ich = 0; ich < s->num_channels; ich++) {
807 int *prevvalues = s->acfilter_prevvalues[ich];
808 for (i = 0; i < order; i++) {
810 for (j = 0; j < order; j++) {
812 pred += filter_coeffs[j] * prevvalues[j - i];
814 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
817 s->channel_residues[ich][i] += pred;
819 for (i = order; i < tile_size; i++) {
821 for (j = 0; j < order; j++)
822 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
824 s->channel_residues[ich][i] += pred;
826 for (j = 0; j < order; j++)
827 prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
831 static int decode_subframe(WmallDecodeCtx *s)
833 int offset = s->samples_per_frame;
834 int subframe_len = s->samples_per_frame;
835 int total_samples = s->samples_per_frame * s->num_channels;
836 int i, j, rawpcm_tile, padding_zeroes, res;
838 s->subframe_offset = get_bits_count(&s->gb);
840 /* reset channel context and find the next block offset and size
841 == the next block of the channel with the smallest number of
843 for (i = 0; i < s->num_channels; i++) {
844 if (offset > s->channel[i].decoded_samples) {
845 offset = s->channel[i].decoded_samples;
847 s->channel[i].subframe_len[s->channel[i].cur_subframe];
851 /* get a list of all channels that contain the estimated block */
852 s->channels_for_cur_subframe = 0;
853 for (i = 0; i < s->num_channels; i++) {
854 const int cur_subframe = s->channel[i].cur_subframe;
855 /* subtract already processed samples */
856 total_samples -= s->channel[i].decoded_samples;
858 /* and count if there are multiple subframes that match our profile */
859 if (offset == s->channel[i].decoded_samples &&
860 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
861 total_samples -= s->channel[i].subframe_len[cur_subframe];
862 s->channel[i].decoded_samples +=
863 s->channel[i].subframe_len[cur_subframe];
864 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
865 ++s->channels_for_cur_subframe;
869 /* check if the frame will be complete after processing the
872 s->parsed_all_subframes = 1;
875 s->seekable_tile = get_bits1(&s->gb);
876 if (s->seekable_tile) {
877 clear_codec_buffers(s);
879 s->do_arith_coding = get_bits1(&s->gb);
880 if (s->do_arith_coding) {
881 avpriv_request_sample(s->avctx, "Arithmetic coding");
882 return AVERROR_PATCHWELCOME;
884 s->do_ac_filter = get_bits1(&s->gb);
885 s->do_inter_ch_decorr = get_bits1(&s->gb);
886 s->do_mclms = get_bits1(&s->gb);
894 if ((res = decode_cdlms(s)) < 0)
896 s->movave_scaling = get_bits(&s->gb, 3);
897 s->quant_stepsize = get_bits(&s->gb, 8) + 1;
900 } else if (!s->cdlms[0][0].order) {
901 av_log(s->avctx, AV_LOG_DEBUG,
902 "Waiting for seekable tile\n");
903 av_frame_unref(s->frame);
907 rawpcm_tile = get_bits1(&s->gb);
909 for (i = 0; i < s->num_channels; i++)
910 s->is_channel_coded[i] = 1;
913 for (i = 0; i < s->num_channels; i++)
914 s->is_channel_coded[i] = get_bits1(&s->gb);
918 s->do_lpc = get_bits1(&s->gb);
921 avpriv_request_sample(s->avctx, "Expect wrong output since "
922 "inverse LPC filter");
929 if (get_bits1(&s->gb))
930 padding_zeroes = get_bits(&s->gb, 5);
935 int bits = s->bits_per_sample - padding_zeroes;
937 av_log(s->avctx, AV_LOG_ERROR,
938 "Invalid number of padding bits in raw PCM tile\n");
939 return AVERROR_INVALIDDATA;
941 ff_dlog(s->avctx, "RAWPCM %d bits per sample. "
942 "total %d bits, remain=%d\n", bits,
943 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
944 for (i = 0; i < s->num_channels; i++)
945 for (j = 0; j < subframe_len; j++)
946 s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
948 for (i = 0; i < s->num_channels; i++)
949 if (s->is_channel_coded[i]) {
950 decode_channel_residues(s, i, subframe_len);
951 if (s->seekable_tile)
952 use_high_update_speed(s, i);
954 use_normal_update_speed(s, i);
955 revert_cdlms(s, i, 0, subframe_len);
957 memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
961 revert_mclms(s, subframe_len);
962 if (s->do_inter_ch_decorr)
963 revert_inter_ch_decorr(s, subframe_len);
965 revert_acfilter(s, subframe_len);
968 if (s->quant_stepsize != 1)
969 for (i = 0; i < s->num_channels; i++)
970 for (j = 0; j < subframe_len; j++)
971 s->channel_residues[i][j] *= s->quant_stepsize;
973 /* Write to proper output buffer depending on bit-depth */
974 for (i = 0; i < s->channels_for_cur_subframe; i++) {
975 int c = s->channel_indexes_for_cur_subframe[i];
976 int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
978 for (j = 0; j < subframe_len; j++) {
979 if (s->bits_per_sample == 16) {
980 *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
982 *s->samples_32[c]++ = s->channel_residues[c][j] << (padding_zeroes + 8);
987 /* handled one subframe */
988 for (i = 0; i < s->channels_for_cur_subframe; i++) {
989 int c = s->channel_indexes_for_cur_subframe[i];
990 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
991 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
992 return AVERROR_INVALIDDATA;
994 ++s->channel[c].cur_subframe;
1000 * @brief Decode one WMA frame.
1001 * @param s codec context
1002 * @return 0 if the trailer bit indicates that this is the last frame,
1003 * 1 if there are additional frames
1005 static int decode_frame(WmallDecodeCtx *s)
1007 GetBitContext* gb = &s->gb;
1008 int more_frames = 0, len = 0, i, ret;
1010 s->frame->nb_samples = s->samples_per_frame;
1011 if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1012 /* return an error if no frame could be decoded at all */
1014 s->frame->nb_samples = 0;
1017 for (i = 0; i < s->num_channels; i++) {
1018 s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1019 s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1022 /* get frame length */
1024 len = get_bits(gb, s->log2_frame_size);
1026 /* decode tile information */
1027 if ((ret = decode_tilehdr(s))) {
1029 av_frame_unref(s->frame);
1034 if (s->dynamic_range_compression)
1035 s->drc_gain = get_bits(gb, 8);
1037 /* no idea what these are for, might be the number of samples
1038 that need to be skipped at the beginning or end of a stream */
1039 if (get_bits1(gb)) {
1042 /* usually true for the first frame */
1043 if (get_bits1(gb)) {
1044 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1045 ff_dlog(s->avctx, "start skip: %i\n", skip);
1048 /* sometimes true for the last frame */
1049 if (get_bits1(gb)) {
1050 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1051 ff_dlog(s->avctx, "end skip: %i\n", skip);
1052 s->frame->nb_samples -= skip;
1053 if (s->frame->nb_samples <= 0)
1054 return AVERROR_INVALIDDATA;
1059 /* reset subframe states */
1060 s->parsed_all_subframes = 0;
1061 for (i = 0; i < s->num_channels; i++) {
1062 s->channel[i].decoded_samples = 0;
1063 s->channel[i].cur_subframe = 0;
1066 /* decode all subframes */
1067 while (!s->parsed_all_subframes) {
1068 int decoded_samples = s->channel[0].decoded_samples;
1069 if (decode_subframe(s) < 0) {
1071 if (s->frame->nb_samples)
1072 s->frame->nb_samples = decoded_samples;
1077 ff_dlog(s->avctx, "Frame done\n");
1081 if (s->len_prefix) {
1082 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1083 /* FIXME: not sure if this is always an error */
1084 av_log(s->avctx, AV_LOG_ERROR,
1085 "frame[%"PRIu32"] would have to skip %i bits\n",
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, s->max_frame_size);
1136 buflen = (s->num_saved_bits + len + 8) >> 3;
1138 if (len <= 0 || buflen > s->max_frame_size) {
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 (!buf_size && s->num_saved_bits > get_bits_count(&s->gb)) {
1177 if (!decode_frame(s))
1178 s->num_saved_bits = 0;
1179 } else if (s->packet_done || s->packet_loss) {
1185 s->next_packet_start = buf_size - FFMIN(avctx->block_align, buf_size);
1186 buf_size = FFMIN(avctx->block_align, buf_size);
1187 s->buf_bit_size = buf_size << 3;
1189 /* parse packet header */
1190 init_get_bits(gb, buf, s->buf_bit_size);
1191 packet_sequence_number = get_bits(gb, 4);
1192 skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1193 spliced_packet = get_bits1(gb);
1195 avpriv_request_sample(avctx, "Bitstream splicing");
1197 /* get number of bits that need to be added to the previous frame */
1198 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1200 /* check for packet loss */
1201 if (!s->packet_loss &&
1202 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1204 av_log(avctx, AV_LOG_ERROR,
1205 "Packet loss detected! seq %"PRIx8" vs %x\n",
1206 s->packet_sequence_number, packet_sequence_number);
1208 s->packet_sequence_number = packet_sequence_number;
1210 if (num_bits_prev_frame > 0) {
1211 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1212 if (num_bits_prev_frame >= remaining_packet_bits) {
1213 num_bits_prev_frame = remaining_packet_bits;
1217 /* Append the previous frame data to the remaining data from the
1218 * previous packet to create a full frame. */
1219 save_bits(s, gb, num_bits_prev_frame, 1);
1221 /* decode the cross packet frame if it is valid */
1222 if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1224 } else if (s->num_saved_bits - s->frame_offset) {
1225 ff_dlog(avctx, "ignoring %x previously saved bits\n",
1226 s->num_saved_bits - s->frame_offset);
1229 if (s->packet_loss) {
1230 /* Reset number of saved bits so that the decoder does not start
1231 * to decode incomplete frames in the s->len_prefix == 0 case. */
1232 s->num_saved_bits = 0;
1234 init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
1240 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1241 init_get_bits(gb, avpkt->data, s->buf_bit_size);
1242 skip_bits(gb, s->packet_offset);
1244 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1245 (frame_size = show_bits(gb, s->log2_frame_size)) &&
1246 frame_size <= remaining_bits(s, gb)) {
1247 save_bits(s, gb, frame_size, 0);
1248 s->packet_done = !decode_frame(s);
1249 } else if (!s->len_prefix
1250 && s->num_saved_bits > get_bits_count(&s->gb)) {
1251 /* when the frames do not have a length prefix, we don't know the
1252 * compressed length of the individual frames however, we know what
1253 * part of a new packet belongs to the previous frame therefore we
1254 * save the incoming packet first, then we append the "previous
1255 * frame" data from the next packet so that we get a buffer that
1256 * only contains full frames */
1257 s->packet_done = !decode_frame(s);
1263 if (s->packet_done && !s->packet_loss &&
1264 remaining_bits(s, gb) > 0) {
1265 /* save the rest of the data so that it can be decoded
1266 * with the next packet */
1267 save_bits(s, gb, remaining_bits(s, gb), 0);
1270 *got_frame_ptr = s->frame->nb_samples > 0;
1271 av_frame_move_ref(data, s->frame);
1273 s->packet_offset = get_bits_count(gb) & 7;
1275 return (s->packet_loss) ? AVERROR_INVALIDDATA : buf_size ? get_bits_count(gb) >> 3 : 0;
1278 static void flush(AVCodecContext *avctx)
1280 WmallDecodeCtx *s = avctx->priv_data;
1283 s->num_saved_bits = 0;
1284 s->frame_offset = 0;
1285 s->next_packet_start = 0;
1286 s->cdlms[0][0].order = 0;
1287 s->frame->nb_samples = 0;
1288 init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
1291 static av_cold int decode_close(AVCodecContext *avctx)
1293 WmallDecodeCtx *s = avctx->priv_data;
1295 av_frame_free(&s->frame);
1296 av_freep(&s->frame_data);
1301 AVCodec ff_wmalossless_decoder = {
1302 .name = "wmalossless",
1303 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1304 .type = AVMEDIA_TYPE_AUDIO,
1305 .id = AV_CODEC_ID_WMALOSSLESS,
1306 .priv_data_size = sizeof(WmallDecodeCtx),
1307 .init = decode_init,
1308 .close = decode_close,
1309 .decode = decode_packet,
1311 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1312 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1314 AV_SAMPLE_FMT_NONE },