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;
181 if (!avctx->block_align) {
182 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
183 return AVERROR(EINVAL);
187 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
189 if (avctx->extradata_size >= 18) {
190 s->decode_flags = AV_RL16(edata_ptr + 14);
191 channel_mask = AV_RL32(edata_ptr + 2);
192 s->bits_per_sample = AV_RL16(edata_ptr);
193 if (s->bits_per_sample == 16)
194 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
195 else if (s->bits_per_sample == 24) {
196 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
197 avpriv_report_missing_feature(avctx, "Bit-depth higher than 16");
198 return AVERROR_PATCHWELCOME;
200 av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
202 return AVERROR_INVALIDDATA;
204 /* dump the extradata */
205 for (i = 0; i < avctx->extradata_size; i++)
206 av_dlog(avctx, "[%x] ", avctx->extradata[i]);
207 av_dlog(avctx, "\n");
210 avpriv_request_sample(avctx, "Unsupported extradata size");
211 return AVERROR_PATCHWELCOME;
215 s->log2_frame_size = av_log2(avctx->block_align) + 4;
218 s->skip_frame = 1; /* skip first frame */
220 s->len_prefix = s->decode_flags & 0x40;
223 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
225 av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
227 /* init previous block len */
228 for (i = 0; i < avctx->channels; i++)
229 s->channel[i].prev_block_len = s->samples_per_frame;
232 log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
233 s->max_num_subframes = 1 << log2_max_num_subframes;
234 s->max_subframe_len_bit = 0;
235 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
237 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
238 s->dynamic_range_compression = s->decode_flags & 0x80;
239 s->bV3RTM = s->decode_flags & 0x100;
241 if (s->max_num_subframes > MAX_SUBFRAMES) {
242 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
243 s->max_num_subframes);
244 return AVERROR_INVALIDDATA;
247 s->num_channels = avctx->channels;
249 /* extract lfe channel position */
252 if (channel_mask & 8) {
254 for (mask = 1; mask < 16; mask <<= 1)
255 if (channel_mask & mask)
259 if (s->num_channels < 0) {
260 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
262 return AVERROR_INVALIDDATA;
263 } else if (s->num_channels > WMALL_MAX_CHANNELS) {
264 avpriv_request_sample(avctx,
265 "More than %d channels", WMALL_MAX_CHANNELS);
266 return AVERROR_PATCHWELCOME;
269 s->frame = av_frame_alloc();
271 return AVERROR(ENOMEM);
273 avctx->channel_layout = channel_mask;
278 * @brief Decode the subframe length.
280 * @param offset sample offset in the frame
281 * @return decoded subframe length on success, < 0 in case of an error
283 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
285 int frame_len_ratio, subframe_len, len;
287 /* no need to read from the bitstream when only one length is possible */
288 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
289 return s->min_samples_per_subframe;
291 len = av_log2(s->max_num_subframes - 1) + 1;
292 frame_len_ratio = get_bits(&s->gb, len);
293 subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
295 /* sanity check the length */
296 if (subframe_len < s->min_samples_per_subframe ||
297 subframe_len > s->samples_per_frame) {
298 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
300 return AVERROR_INVALIDDATA;
306 * @brief Decode how the data in the frame is split into subframes.
307 * Every WMA frame contains the encoded data for a fixed number of
308 * samples per channel. The data for every channel might be split
309 * into several subframes. This function will reconstruct the list of
310 * subframes for every channel.
312 * If the subframes are not evenly split, the algorithm estimates the
313 * channels with the lowest number of total samples.
314 * Afterwards, for each of these channels a bit is read from the
315 * bitstream that indicates if the channel contains a subframe with the
316 * next subframe size that is going to be read from the bitstream or not.
317 * If a channel contains such a subframe, the subframe size gets added to
318 * the channel's subframe list.
319 * The algorithm repeats these steps until the frame is properly divided
320 * between the individual channels.
323 * @return 0 on success, < 0 in case of an error
325 static int decode_tilehdr(WmallDecodeCtx *s)
327 uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
328 uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
329 int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
330 int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
331 int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
334 /* reset tiling information */
335 for (c = 0; c < s->num_channels; c++)
336 s->channel[c].num_subframes = 0;
338 tile_aligned = get_bits1(&s->gb);
339 if (s->max_num_subframes == 1 || tile_aligned)
340 fixed_channel_layout = 1;
342 /* loop until the frame data is split between the subframes */
344 int subframe_len, in_use = 0;
346 /* check which channels contain the subframe */
347 for (c = 0; c < s->num_channels; c++) {
348 if (num_samples[c] == min_channel_len) {
349 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
350 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
351 contains_subframe[c] = in_use = 1;
353 if (get_bits1(&s->gb))
354 contains_subframe[c] = in_use = 1;
357 contains_subframe[c] = 0;
361 av_log(s->avctx, AV_LOG_ERROR,
362 "Found empty subframe\n");
363 return AVERROR_INVALIDDATA;
366 /* get subframe length, subframe_len == 0 is not allowed */
367 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
368 return AVERROR_INVALIDDATA;
369 /* add subframes to the individual channels and find new min_channel_len */
370 min_channel_len += subframe_len;
371 for (c = 0; c < s->num_channels; c++) {
372 WmallChannelCtx *chan = &s->channel[c];
374 if (contains_subframe[c]) {
375 if (chan->num_subframes >= MAX_SUBFRAMES) {
376 av_log(s->avctx, AV_LOG_ERROR,
377 "broken frame: num subframes > 31\n");
378 return AVERROR_INVALIDDATA;
380 chan->subframe_len[chan->num_subframes] = subframe_len;
381 num_samples[c] += subframe_len;
382 ++chan->num_subframes;
383 if (num_samples[c] > s->samples_per_frame) {
384 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
385 "channel len(%d) > samples_per_frame(%d)\n",
386 num_samples[c], s->samples_per_frame);
387 return AVERROR_INVALIDDATA;
389 } else if (num_samples[c] <= min_channel_len) {
390 if (num_samples[c] < min_channel_len) {
391 channels_for_cur_subframe = 0;
392 min_channel_len = num_samples[c];
394 ++channels_for_cur_subframe;
397 } while (min_channel_len < s->samples_per_frame);
399 for (c = 0; c < s->num_channels; c++) {
401 for (i = 0; i < s->channel[c].num_subframes; i++) {
402 s->channel[c].subframe_offsets[i] = offset;
403 offset += s->channel[c].subframe_len[i];
410 static void decode_ac_filter(WmallDecodeCtx *s)
413 s->acfilter_order = get_bits(&s->gb, 4) + 1;
414 s->acfilter_scaling = get_bits(&s->gb, 4);
416 for (i = 0; i < s->acfilter_order; i++)
417 s->acfilter_coeffs[i] = (s->acfilter_scaling ?
418 get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
421 static void decode_mclms(WmallDecodeCtx *s)
423 s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
424 s->mclms_scaling = get_bits(&s->gb, 4);
425 if (get_bits1(&s->gb)) {
426 int i, send_coef_bits;
427 int cbits = av_log2(s->mclms_scaling + 1);
428 if (1 << cbits < s->mclms_scaling + 1)
431 send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
433 for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
434 s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
436 for (i = 0; i < s->num_channels; i++) {
438 for (c = 0; c < i; c++)
439 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
444 static int decode_cdlms(WmallDecodeCtx *s)
447 int cdlms_send_coef = get_bits1(&s->gb);
449 for (c = 0; c < s->num_channels; c++) {
450 s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
451 for (i = 0; i < s->cdlms_ttl[c]; i++) {
452 s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
453 if (s->cdlms[c][i].order > MAX_ORDER) {
454 av_log(s->avctx, AV_LOG_ERROR,
455 "Order[%d][%d] %d > max (%d), not supported\n",
456 c, i, s->cdlms[c][i].order, MAX_ORDER);
457 s->cdlms[0][0].order = 0;
458 return AVERROR_INVALIDDATA;
462 for (i = 0; i < s->cdlms_ttl[c]; i++)
463 s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
465 if (cdlms_send_coef) {
466 for (i = 0; i < s->cdlms_ttl[c]; i++) {
467 int cbits, shift_l, shift_r, j;
468 cbits = av_log2(s->cdlms[c][i].order);
469 if ((1 << cbits) < s->cdlms[c][i].order)
471 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
473 cbits = av_log2(s->cdlms[c][i].scaling + 1);
474 if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
477 s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
478 shift_l = 32 - s->cdlms[c][i].bitsend;
479 shift_r = 32 - s->cdlms[c][i].scaling - 2;
480 for (j = 0; j < s->cdlms[c][i].coefsend; j++)
481 s->cdlms[c][i].coefs[j] =
482 (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
490 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
493 unsigned int ave_mean;
494 s->transient[ch] = get_bits1(&s->gb);
495 if (s->transient[ch]) {
496 s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
497 if (s->transient_pos[ch])
498 s->transient[ch] = 0;
499 s->channel[ch].transient_counter =
500 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
501 } else if (s->channel[ch].transient_counter)
502 s->transient[ch] = 1;
504 if (s->seekable_tile) {
505 ave_mean = get_bits(&s->gb, s->bits_per_sample);
506 s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
509 if (s->seekable_tile) {
510 if (s->do_inter_ch_decorr)
511 s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
513 s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
516 for (; i < tile_size; i++) {
517 int quo = 0, rem, rem_bits, residue;
518 while(get_bits1(&s->gb)) {
520 if (get_bits_left(&s->gb) <= 0)
524 quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
526 ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
530 rem_bits = av_ceil_log2(ave_mean);
531 rem = get_bits_long(&s->gb, rem_bits);
532 residue = (quo << rem_bits) + rem;
535 s->ave_sum[ch] = residue + s->ave_sum[ch] -
536 (s->ave_sum[ch] >> s->movave_scaling);
539 residue = -(residue >> 1) - 1;
541 residue = residue >> 1;
542 s->channel_residues[ch][i] = residue;
549 static void decode_lpc(WmallDecodeCtx *s)
552 s->lpc_order = get_bits(&s->gb, 5) + 1;
553 s->lpc_scaling = get_bits(&s->gb, 4);
554 s->lpc_intbits = get_bits(&s->gb, 3) + 1;
555 cbits = s->lpc_scaling + s->lpc_intbits;
556 for (ch = 0; ch < s->num_channels; ch++)
557 for (i = 0; i < s->lpc_order; i++)
558 s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
561 static void clear_codec_buffers(WmallDecodeCtx *s)
565 memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
566 memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
567 memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
569 memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
570 memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
571 memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
572 memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
574 for (ich = 0; ich < s->num_channels; ich++) {
575 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
576 memset(s->cdlms[ich][ilms].coefs, 0,
577 sizeof(s->cdlms[ich][ilms].coefs));
578 memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
579 sizeof(s->cdlms[ich][ilms].lms_prevvalues));
580 memset(s->cdlms[ich][ilms].lms_updates, 0,
581 sizeof(s->cdlms[ich][ilms].lms_updates));
588 * @brief Reset filter parameters and transient area at new seekable tile.
590 static void reset_codec(WmallDecodeCtx *s)
593 s->mclms_recent = s->mclms_order * s->num_channels;
594 for (ich = 0; ich < s->num_channels; ich++) {
595 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
596 s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
597 /* first sample of a seekable subframe is considered as the starting of
598 a transient area which is samples_per_frame samples long */
599 s->channel[ich].transient_counter = s->samples_per_frame;
600 s->transient[ich] = 1;
601 s->transient_pos[ich] = 0;
605 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
607 int i, j, ich, pred_error;
608 int order = s->mclms_order;
609 int num_channels = s->num_channels;
610 int range = 1 << (s->bits_per_sample - 1);
612 for (ich = 0; ich < num_channels; ich++) {
613 pred_error = s->channel_residues[ich][icoef] - pred[ich];
614 if (pred_error > 0) {
615 for (i = 0; i < order * num_channels; i++)
616 s->mclms_coeffs[i + ich * order * num_channels] +=
617 s->mclms_updates[s->mclms_recent + i];
618 for (j = 0; j < ich; j++) {
619 if (s->channel_residues[j][icoef] > 0)
620 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
621 else if (s->channel_residues[j][icoef] < 0)
622 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
624 } else if (pred_error < 0) {
625 for (i = 0; i < order * num_channels; i++)
626 s->mclms_coeffs[i + ich * order * num_channels] -=
627 s->mclms_updates[s->mclms_recent + i];
628 for (j = 0; j < ich; j++) {
629 if (s->channel_residues[j][icoef] > 0)
630 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
631 else if (s->channel_residues[j][icoef] < 0)
632 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
637 for (ich = num_channels - 1; ich >= 0; ich--) {
639 s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
640 if (s->channel_residues[ich][icoef] > range - 1)
641 s->mclms_prevvalues[s->mclms_recent] = range - 1;
642 else if (s->channel_residues[ich][icoef] < -range)
643 s->mclms_prevvalues[s->mclms_recent] = -range;
645 s->mclms_updates[s->mclms_recent] = 0;
646 if (s->channel_residues[ich][icoef] > 0)
647 s->mclms_updates[s->mclms_recent] = 1;
648 else if (s->channel_residues[ich][icoef] < 0)
649 s->mclms_updates[s->mclms_recent] = -1;
652 if (s->mclms_recent == 0) {
653 memcpy(&s->mclms_prevvalues[order * num_channels],
655 2 * order * num_channels);
656 memcpy(&s->mclms_updates[order * num_channels],
658 2 * order * num_channels);
659 s->mclms_recent = num_channels * order;
663 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
666 int order = s->mclms_order;
667 int num_channels = s->num_channels;
669 for (ich = 0; ich < num_channels; ich++) {
671 if (!s->is_channel_coded[ich])
673 for (i = 0; i < order * num_channels; i++)
674 pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
675 s->mclms_coeffs[i + order * num_channels * ich];
676 for (i = 0; i < ich; i++)
677 pred[ich] += s->channel_residues[i][icoef] *
678 s->mclms_coeffs_cur[i + num_channels * ich];
679 pred[ich] += 1 << s->mclms_scaling - 1;
680 pred[ich] >>= s->mclms_scaling;
681 s->channel_residues[ich][icoef] += pred[ich];
685 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
687 int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
688 for (icoef = 0; icoef < tile_size; icoef++) {
689 mclms_predict(s, icoef, pred);
690 mclms_update(s, icoef, pred);
694 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
697 int recent = s->cdlms[ich][ilms].recent;
699 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
700 pred += s->cdlms[ich][ilms].coefs[icoef] *
701 s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
706 static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
707 int input, int residue)
710 int recent = s->cdlms[ich][ilms].recent;
711 int range = 1 << s->bits_per_sample - 1;
714 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
715 s->cdlms[ich][ilms].coefs[icoef] -=
716 s->cdlms[ich][ilms].lms_updates[icoef + recent];
717 } else if (residue > 0) {
718 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
719 s->cdlms[ich][ilms].coefs[icoef] +=
720 s->cdlms[ich][ilms].lms_updates[icoef + recent];
726 memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
727 s->cdlms[ich][ilms].lms_prevvalues,
728 2 * s->cdlms[ich][ilms].order);
729 memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
730 s->cdlms[ich][ilms].lms_updates,
731 2 * s->cdlms[ich][ilms].order);
732 recent = s->cdlms[ich][ilms].order - 1;
735 s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
737 s->cdlms[ich][ilms].lms_updates[recent] = 0;
739 s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
741 s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
743 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
744 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
745 s->cdlms[ich][ilms].recent = recent;
748 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
750 int ilms, recent, icoef;
751 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
752 recent = s->cdlms[ich][ilms].recent;
753 if (s->update_speed[ich] == 16)
756 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
757 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
759 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
760 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
763 s->update_speed[ich] = 16;
766 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
768 int ilms, recent, icoef;
769 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
770 recent = s->cdlms[ich][ilms].recent;
771 if (s->update_speed[ich] == 8)
774 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
775 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
777 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
778 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
780 s->update_speed[ich] = 8;
783 static void revert_cdlms(WmallDecodeCtx *s, int ch,
784 int coef_begin, int coef_end)
786 int icoef, pred, ilms, num_lms, residue, input;
788 num_lms = s->cdlms_ttl[ch];
789 for (ilms = num_lms - 1; ilms >= 0; ilms--) {
790 for (icoef = coef_begin; icoef < coef_end; icoef++) {
791 pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
792 residue = s->channel_residues[ch][icoef];
793 pred += lms_predict(s, ch, ilms);
794 input = residue + (pred >> s->cdlms[ch][ilms].scaling);
795 lms_update(s, ch, ilms, input, residue);
796 s->channel_residues[ch][icoef] = input;
801 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
803 if (s->num_channels != 2)
805 else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
807 for (icoef = 0; icoef < tile_size; icoef++) {
808 s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
809 s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
814 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
817 int64_t *filter_coeffs = s->acfilter_coeffs;
818 int scaling = s->acfilter_scaling;
819 int order = s->acfilter_order;
821 for (ich = 0; ich < s->num_channels; ich++) {
822 int *prevvalues = s->acfilter_prevvalues[ich];
823 for (i = 0; i < order; i++) {
825 for (j = 0; j < order; j++) {
827 pred += filter_coeffs[j] * prevvalues[j - i];
829 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
832 s->channel_residues[ich][i] += pred;
834 for (i = order; i < tile_size; i++) {
836 for (j = 0; j < order; j++)
837 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
839 s->channel_residues[ich][i] += pred;
841 for (j = 0; j < order; j++)
842 prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
846 static int decode_subframe(WmallDecodeCtx *s)
848 int offset = s->samples_per_frame;
849 int subframe_len = s->samples_per_frame;
850 int total_samples = s->samples_per_frame * s->num_channels;
851 int i, j, rawpcm_tile, padding_zeroes, res;
853 s->subframe_offset = get_bits_count(&s->gb);
855 /* reset channel context and find the next block offset and size
856 == the next block of the channel with the smallest number of
858 for (i = 0; i < s->num_channels; i++) {
859 if (offset > s->channel[i].decoded_samples) {
860 offset = s->channel[i].decoded_samples;
862 s->channel[i].subframe_len[s->channel[i].cur_subframe];
866 /* get a list of all channels that contain the estimated block */
867 s->channels_for_cur_subframe = 0;
868 for (i = 0; i < s->num_channels; i++) {
869 const int cur_subframe = s->channel[i].cur_subframe;
870 /* subtract already processed samples */
871 total_samples -= s->channel[i].decoded_samples;
873 /* and count if there are multiple subframes that match our profile */
874 if (offset == s->channel[i].decoded_samples &&
875 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
876 total_samples -= s->channel[i].subframe_len[cur_subframe];
877 s->channel[i].decoded_samples +=
878 s->channel[i].subframe_len[cur_subframe];
879 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
880 ++s->channels_for_cur_subframe;
884 /* check if the frame will be complete after processing the
887 s->parsed_all_subframes = 1;
890 s->seekable_tile = get_bits1(&s->gb);
891 if (s->seekable_tile) {
892 clear_codec_buffers(s);
894 s->do_arith_coding = get_bits1(&s->gb);
895 if (s->do_arith_coding) {
896 avpriv_request_sample(s->avctx, "Arithmetic coding");
897 return AVERROR_PATCHWELCOME;
899 s->do_ac_filter = get_bits1(&s->gb);
900 s->do_inter_ch_decorr = get_bits1(&s->gb);
901 s->do_mclms = get_bits1(&s->gb);
909 if ((res = decode_cdlms(s)) < 0)
911 s->movave_scaling = get_bits(&s->gb, 3);
912 s->quant_stepsize = get_bits(&s->gb, 8) + 1;
915 } else if (!s->cdlms[0][0].order) {
916 av_log(s->avctx, AV_LOG_DEBUG,
917 "Waiting for seekable tile\n");
918 av_frame_unref(s->frame);
922 rawpcm_tile = get_bits1(&s->gb);
924 for (i = 0; i < s->num_channels; i++)
925 s->is_channel_coded[i] = 1;
928 for (i = 0; i < s->num_channels; i++)
929 s->is_channel_coded[i] = get_bits1(&s->gb);
933 s->do_lpc = get_bits1(&s->gb);
936 avpriv_request_sample(s->avctx, "Expect wrong output since "
937 "inverse LPC filter");
944 if (get_bits1(&s->gb))
945 padding_zeroes = get_bits(&s->gb, 5);
950 int bits = s->bits_per_sample - padding_zeroes;
952 av_log(s->avctx, AV_LOG_ERROR,
953 "Invalid number of padding bits in raw PCM tile\n");
954 return AVERROR_INVALIDDATA;
956 av_dlog(s->avctx, "RAWPCM %d bits per sample. "
957 "total %d bits, remain=%d\n", bits,
958 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
959 for (i = 0; i < s->num_channels; i++)
960 for (j = 0; j < subframe_len; j++)
961 s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
963 for (i = 0; i < s->num_channels; i++)
964 if (s->is_channel_coded[i]) {
965 decode_channel_residues(s, i, subframe_len);
966 if (s->seekable_tile)
967 use_high_update_speed(s, i);
969 use_normal_update_speed(s, i);
970 revert_cdlms(s, i, 0, subframe_len);
972 memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
976 revert_mclms(s, subframe_len);
977 if (s->do_inter_ch_decorr)
978 revert_inter_ch_decorr(s, subframe_len);
980 revert_acfilter(s, subframe_len);
983 if (s->quant_stepsize != 1)
984 for (i = 0; i < s->num_channels; i++)
985 for (j = 0; j < subframe_len; j++)
986 s->channel_residues[i][j] *= s->quant_stepsize;
988 /* Write to proper output buffer depending on bit-depth */
989 for (i = 0; i < s->channels_for_cur_subframe; i++) {
990 int c = s->channel_indexes_for_cur_subframe[i];
991 int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
993 for (j = 0; j < subframe_len; j++) {
994 if (s->bits_per_sample == 16) {
995 *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
997 *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
1002 /* handled one subframe */
1003 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1004 int c = s->channel_indexes_for_cur_subframe[i];
1005 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1006 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1007 return AVERROR_INVALIDDATA;
1009 ++s->channel[c].cur_subframe;
1015 * @brief Decode one WMA frame.
1016 * @param s codec context
1017 * @return 0 if the trailer bit indicates that this is the last frame,
1018 * 1 if there are additional frames
1020 static int decode_frame(WmallDecodeCtx *s)
1022 GetBitContext* gb = &s->gb;
1023 int more_frames = 0, len = 0, i, ret;
1025 s->frame->nb_samples = s->samples_per_frame;
1026 if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1027 /* return an error if no frame could be decoded at all */
1031 for (i = 0; i < s->num_channels; i++) {
1032 s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1033 s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1036 /* get frame length */
1038 len = get_bits(gb, s->log2_frame_size);
1040 /* decode tile information */
1041 if (decode_tilehdr(s)) {
1047 if (s->dynamic_range_compression)
1048 s->drc_gain = get_bits(gb, 8);
1050 /* no idea what these are for, might be the number of samples
1051 that need to be skipped at the beginning or end of a stream */
1052 if (get_bits1(gb)) {
1055 /* usually true for the first frame */
1056 if (get_bits1(gb)) {
1057 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1058 av_dlog(s->avctx, "start skip: %i\n", skip);
1061 /* sometimes true for the last frame */
1062 if (get_bits1(gb)) {
1063 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1064 av_dlog(s->avctx, "end skip: %i\n", skip);
1069 /* reset subframe states */
1070 s->parsed_all_subframes = 0;
1071 for (i = 0; i < s->num_channels; i++) {
1072 s->channel[i].decoded_samples = 0;
1073 s->channel[i].cur_subframe = 0;
1076 /* decode all subframes */
1077 while (!s->parsed_all_subframes) {
1078 if (decode_subframe(s) < 0) {
1084 av_dlog(s->avctx, "Frame done\n");
1089 if (s->len_prefix) {
1090 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1091 /* FIXME: not sure if this is always an error */
1092 av_log(s->avctx, AV_LOG_ERROR,
1093 "frame[%i] would have to skip %i bits\n", s->frame_num,
1094 len - (get_bits_count(gb) - s->frame_offset) - 1);
1099 /* skip the rest of the frame data */
1100 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1103 /* decode trailer bit */
1104 more_frames = get_bits1(gb);
1110 * @brief Calculate remaining input buffer length.
1111 * @param s codec context
1112 * @param gb bitstream reader context
1113 * @return remaining size in bits
1115 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
1117 return s->buf_bit_size - get_bits_count(gb);
1121 * @brief Fill the bit reservoir with a (partial) frame.
1122 * @param s codec context
1123 * @param gb bitstream reader context
1124 * @param len length of the partial frame
1125 * @param append decides whether to reset the buffer or not
1127 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1133 /* when the frame data does not need to be concatenated, the input buffer
1134 is reset and additional bits from the previous frame are copied
1135 and skipped later so that a fast byte copy is possible */
1138 s->frame_offset = get_bits_count(gb) & 7;
1139 s->num_saved_bits = s->frame_offset;
1140 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1143 buflen = (s->num_saved_bits + len + 8) >> 3;
1145 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1146 avpriv_request_sample(s->avctx, "Too small input buffer");
1151 s->num_saved_bits += len;
1153 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1156 int align = 8 - (get_bits_count(gb) & 7);
1157 align = FFMIN(align, len);
1158 put_bits(&s->pb, align, get_bits(gb, align));
1160 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1162 skip_bits_long(gb, len);
1165 flush_put_bits(&tmp);
1167 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1168 skip_bits(&s->gb, s->frame_offset);
1171 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1174 WmallDecodeCtx *s = avctx->priv_data;
1175 GetBitContext* gb = &s->pgb;
1176 const uint8_t* buf = avpkt->data;
1177 int buf_size = avpkt->size;
1178 int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1180 s->frame->nb_samples = 0;
1182 if (s->packet_done || s->packet_loss) {
1187 /* sanity check for the buffer length */
1188 if (buf_size < avctx->block_align) {
1189 av_log(avctx, AV_LOG_ERROR, "buf size %d invalid\n", buf_size);
1190 return AVERROR_INVALIDDATA;
1193 s->next_packet_start = buf_size - avctx->block_align;
1194 buf_size = avctx->block_align;
1195 s->buf_bit_size = buf_size << 3;
1197 /* parse packet header */
1198 init_get_bits(gb, buf, s->buf_bit_size);
1199 packet_sequence_number = get_bits(gb, 4);
1200 skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1201 spliced_packet = get_bits1(gb);
1203 avpriv_request_sample(avctx, "Bitstream splicing");
1205 /* get number of bits that need to be added to the previous frame */
1206 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1208 /* check for packet loss */
1209 if (!s->packet_loss &&
1210 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1212 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1213 s->packet_sequence_number, packet_sequence_number);
1215 s->packet_sequence_number = packet_sequence_number;
1217 if (num_bits_prev_frame > 0) {
1218 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1219 if (num_bits_prev_frame >= remaining_packet_bits) {
1220 num_bits_prev_frame = remaining_packet_bits;
1224 /* Append the previous frame data to the remaining data from the
1225 * previous packet to create a full frame. */
1226 save_bits(s, gb, num_bits_prev_frame, 1);
1228 /* decode the cross packet frame if it is valid */
1229 if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1231 } else if (s->num_saved_bits - s->frame_offset) {
1232 av_dlog(avctx, "ignoring %x previously saved bits\n",
1233 s->num_saved_bits - s->frame_offset);
1236 if (s->packet_loss) {
1237 /* Reset number of saved bits so that the decoder does not start
1238 * to decode incomplete frames in the s->len_prefix == 0 case. */
1239 s->num_saved_bits = 0;
1241 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1247 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1248 init_get_bits(gb, avpkt->data, s->buf_bit_size);
1249 skip_bits(gb, s->packet_offset);
1251 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1252 (frame_size = show_bits(gb, s->log2_frame_size)) &&
1253 frame_size <= remaining_bits(s, gb)) {
1254 save_bits(s, gb, frame_size, 0);
1255 s->packet_done = !decode_frame(s);
1256 } else if (!s->len_prefix
1257 && s->num_saved_bits > get_bits_count(&s->gb)) {
1258 /* when the frames do not have a length prefix, we don't know the
1259 * compressed length of the individual frames however, we know what
1260 * part of a new packet belongs to the previous frame therefore we
1261 * save the incoming packet first, then we append the "previous
1262 * frame" data from the next packet so that we get a buffer that
1263 * only contains full frames */
1264 s->packet_done = !decode_frame(s);
1270 if (s->packet_done && !s->packet_loss &&
1271 remaining_bits(s, gb) > 0) {
1272 /* save the rest of the data so that it can be decoded
1273 * with the next packet */
1274 save_bits(s, gb, remaining_bits(s, gb), 0);
1277 *got_frame_ptr = s->frame->nb_samples > 0;
1278 av_frame_move_ref(data, s->frame);
1280 s->packet_offset = get_bits_count(gb) & 7;
1282 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1285 static void flush(AVCodecContext *avctx)
1287 WmallDecodeCtx *s = avctx->priv_data;
1290 s->num_saved_bits = 0;
1291 s->frame_offset = 0;
1292 s->next_packet_start = 0;
1293 s->cdlms[0][0].order = 0;
1294 s->frame->nb_samples = 0;
1295 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1298 static av_cold int decode_close(AVCodecContext *avctx)
1300 WmallDecodeCtx *s = avctx->priv_data;
1302 av_frame_free(&s->frame);
1307 AVCodec ff_wmalossless_decoder = {
1308 .name = "wmalossless",
1309 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1310 .type = AVMEDIA_TYPE_AUDIO,
1311 .id = AV_CODEC_ID_WMALOSSLESS,
1312 .priv_data_size = sizeof(WmallDecodeCtx),
1313 .init = decode_init,
1314 .close = decode_close,
1315 .decode = decode_packet,
1317 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1318 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1320 AV_SAMPLE_FMT_NONE },