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 Libav.
10 * Libav 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 * Libav 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 Libav; 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[2][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];
154 int is_channel_coded[2];
158 int transient_pos[2];
163 int channel_residues[2][WMALL_BLOCK_MAX_SIZE];
165 int lpc_coefs[2][40];
170 int channel_coeffs[2][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 s->frame = av_frame_alloc();
266 return AVERROR(ENOMEM);
268 avctx->channel_layout = channel_mask;
273 * @brief Decode the subframe length.
275 * @param offset sample offset in the frame
276 * @return decoded subframe length on success, < 0 in case of an error
278 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
280 int frame_len_ratio, subframe_len, len;
282 /* no need to read from the bitstream when only one length is possible */
283 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
284 return s->min_samples_per_subframe;
286 len = av_log2(s->max_num_subframes - 1) + 1;
287 frame_len_ratio = get_bits(&s->gb, len);
288 subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
290 /* sanity check the length */
291 if (subframe_len < s->min_samples_per_subframe ||
292 subframe_len > s->samples_per_frame) {
293 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
295 return AVERROR_INVALIDDATA;
301 * @brief Decode how the data in the frame is split into subframes.
302 * Every WMA frame contains the encoded data for a fixed number of
303 * samples per channel. The data for every channel might be split
304 * into several subframes. This function will reconstruct the list of
305 * subframes for every channel.
307 * If the subframes are not evenly split, the algorithm estimates the
308 * channels with the lowest number of total samples.
309 * Afterwards, for each of these channels a bit is read from the
310 * bitstream that indicates if the channel contains a subframe with the
311 * next subframe size that is going to be read from the bitstream or not.
312 * If a channel contains such a subframe, the subframe size gets added to
313 * the channel's subframe list.
314 * The algorithm repeats these steps until the frame is properly divided
315 * between the individual channels.
318 * @return 0 on success, < 0 in case of an error
320 static int decode_tilehdr(WmallDecodeCtx *s)
322 uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
323 uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
324 int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
325 int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
326 int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
329 /* reset tiling information */
330 for (c = 0; c < s->num_channels; c++)
331 s->channel[c].num_subframes = 0;
333 tile_aligned = get_bits1(&s->gb);
334 if (s->max_num_subframes == 1 || tile_aligned)
335 fixed_channel_layout = 1;
337 /* loop until the frame data is split between the subframes */
339 int subframe_len, in_use = 0;
341 /* check which channels contain the subframe */
342 for (c = 0; c < s->num_channels; c++) {
343 if (num_samples[c] == min_channel_len) {
344 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
345 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
346 contains_subframe[c] = in_use = 1;
348 if (get_bits1(&s->gb))
349 contains_subframe[c] = in_use = 1;
352 contains_subframe[c] = 0;
356 av_log(s->avctx, AV_LOG_ERROR,
357 "Found empty subframe\n");
358 return AVERROR_INVALIDDATA;
361 /* get subframe length, subframe_len == 0 is not allowed */
362 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
363 return AVERROR_INVALIDDATA;
364 /* add subframes to the individual channels and find new min_channel_len */
365 min_channel_len += subframe_len;
366 for (c = 0; c < s->num_channels; c++) {
367 WmallChannelCtx *chan = &s->channel[c];
369 if (contains_subframe[c]) {
370 if (chan->num_subframes >= MAX_SUBFRAMES) {
371 av_log(s->avctx, AV_LOG_ERROR,
372 "broken frame: num subframes > 31\n");
373 return AVERROR_INVALIDDATA;
375 chan->subframe_len[chan->num_subframes] = subframe_len;
376 num_samples[c] += subframe_len;
377 ++chan->num_subframes;
378 if (num_samples[c] > s->samples_per_frame) {
379 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
380 "channel len(%d) > samples_per_frame(%d)\n",
381 num_samples[c], s->samples_per_frame);
382 return AVERROR_INVALIDDATA;
384 } else if (num_samples[c] <= min_channel_len) {
385 if (num_samples[c] < min_channel_len) {
386 channels_for_cur_subframe = 0;
387 min_channel_len = num_samples[c];
389 ++channels_for_cur_subframe;
392 } while (min_channel_len < s->samples_per_frame);
394 for (c = 0; c < s->num_channels; c++) {
396 for (i = 0; i < s->channel[c].num_subframes; i++) {
397 s->channel[c].subframe_offsets[i] = offset;
398 offset += s->channel[c].subframe_len[i];
405 static void decode_ac_filter(WmallDecodeCtx *s)
408 s->acfilter_order = get_bits(&s->gb, 4) + 1;
409 s->acfilter_scaling = get_bits(&s->gb, 4);
411 for (i = 0; i < s->acfilter_order; i++)
412 s->acfilter_coeffs[i] = (s->acfilter_scaling ?
413 get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
416 static void decode_mclms(WmallDecodeCtx *s)
418 s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
419 s->mclms_scaling = get_bits(&s->gb, 4);
420 if (get_bits1(&s->gb)) {
421 int i, send_coef_bits;
422 int cbits = av_log2(s->mclms_scaling + 1);
423 if (1 << cbits < s->mclms_scaling + 1)
426 send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
428 for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
429 s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
431 for (i = 0; i < s->num_channels; i++) {
433 for (c = 0; c < i; c++)
434 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
439 static int decode_cdlms(WmallDecodeCtx *s)
442 int cdlms_send_coef = get_bits1(&s->gb);
444 for (c = 0; c < s->num_channels; c++) {
445 s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
446 for (i = 0; i < s->cdlms_ttl[c]; i++) {
447 s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
448 if (s->cdlms[c][i].order > MAX_ORDER) {
449 av_log(s->avctx, AV_LOG_ERROR,
450 "Order[%d][%d] %d > max (%d), not supported\n",
451 c, i, s->cdlms[c][i].order, MAX_ORDER);
452 s->cdlms[0][0].order = 0;
453 return AVERROR_INVALIDDATA;
457 for (i = 0; i < s->cdlms_ttl[c]; i++)
458 s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
460 if (cdlms_send_coef) {
461 for (i = 0; i < s->cdlms_ttl[c]; i++) {
462 int cbits, shift_l, shift_r, j;
463 cbits = av_log2(s->cdlms[c][i].order);
464 if ((1 << cbits) < s->cdlms[c][i].order)
466 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
468 cbits = av_log2(s->cdlms[c][i].scaling + 1);
469 if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
472 s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
473 shift_l = 32 - s->cdlms[c][i].bitsend;
474 shift_r = 32 - s->cdlms[c][i].scaling - 2;
475 for (j = 0; j < s->cdlms[c][i].coefsend; j++)
476 s->cdlms[c][i].coefs[j] =
477 (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
485 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
488 unsigned int ave_mean;
489 s->transient[ch] = get_bits1(&s->gb);
490 if (s->transient[ch]) {
491 s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
492 if (s->transient_pos[ch])
493 s->transient[ch] = 0;
494 s->channel[ch].transient_counter =
495 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
496 } else if (s->channel[ch].transient_counter)
497 s->transient[ch] = 1;
499 if (s->seekable_tile) {
500 ave_mean = get_bits(&s->gb, s->bits_per_sample);
501 s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
504 if (s->seekable_tile) {
505 if (s->do_inter_ch_decorr)
506 s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
508 s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
511 for (; i < tile_size; i++) {
512 int quo = 0, rem, rem_bits, residue;
513 while(get_bits1(&s->gb)) {
515 if (get_bits_left(&s->gb) <= 0)
519 quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
521 ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
525 rem_bits = av_ceil_log2(ave_mean);
526 rem = rem_bits ? get_bits_long(&s->gb, rem_bits) : 0;
527 residue = (quo << rem_bits) + rem;
530 s->ave_sum[ch] = residue + s->ave_sum[ch] -
531 (s->ave_sum[ch] >> s->movave_scaling);
534 residue = -(residue >> 1) - 1;
536 residue = residue >> 1;
537 s->channel_residues[ch][i] = residue;
544 static void decode_lpc(WmallDecodeCtx *s)
547 s->lpc_order = get_bits(&s->gb, 5) + 1;
548 s->lpc_scaling = get_bits(&s->gb, 4);
549 s->lpc_intbits = get_bits(&s->gb, 3) + 1;
550 cbits = s->lpc_scaling + s->lpc_intbits;
551 for (ch = 0; ch < s->num_channels; ch++)
552 for (i = 0; i < s->lpc_order; i++)
553 s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
556 static void clear_codec_buffers(WmallDecodeCtx *s)
560 memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
561 memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
562 memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
564 memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
565 memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
566 memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
567 memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
569 for (ich = 0; ich < s->num_channels; ich++) {
570 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
571 memset(s->cdlms[ich][ilms].coefs, 0,
572 sizeof(s->cdlms[ich][ilms].coefs));
573 memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
574 sizeof(s->cdlms[ich][ilms].lms_prevvalues));
575 memset(s->cdlms[ich][ilms].lms_updates, 0,
576 sizeof(s->cdlms[ich][ilms].lms_updates));
583 * @brief Reset filter parameters and transient area at new seekable tile.
585 static void reset_codec(WmallDecodeCtx *s)
588 s->mclms_recent = s->mclms_order * s->num_channels;
589 for (ich = 0; ich < s->num_channels; ich++) {
590 for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
591 s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
592 /* first sample of a seekable subframe is considered as the starting of
593 a transient area which is samples_per_frame samples long */
594 s->channel[ich].transient_counter = s->samples_per_frame;
595 s->transient[ich] = 1;
596 s->transient_pos[ich] = 0;
600 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
602 int i, j, ich, pred_error;
603 int order = s->mclms_order;
604 int num_channels = s->num_channels;
605 int range = 1 << (s->bits_per_sample - 1);
607 for (ich = 0; ich < num_channels; ich++) {
608 pred_error = s->channel_residues[ich][icoef] - pred[ich];
609 if (pred_error > 0) {
610 for (i = 0; i < order * num_channels; i++)
611 s->mclms_coeffs[i + ich * order * num_channels] +=
612 s->mclms_updates[s->mclms_recent + i];
613 for (j = 0; j < ich; j++) {
614 if (s->channel_residues[j][icoef] > 0)
615 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
616 else if (s->channel_residues[j][icoef] < 0)
617 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
619 } else if (pred_error < 0) {
620 for (i = 0; i < order * num_channels; i++)
621 s->mclms_coeffs[i + ich * order * num_channels] -=
622 s->mclms_updates[s->mclms_recent + i];
623 for (j = 0; j < ich; j++) {
624 if (s->channel_residues[j][icoef] > 0)
625 s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
626 else if (s->channel_residues[j][icoef] < 0)
627 s->mclms_coeffs_cur[ich * num_channels + j] += 1;
632 for (ich = num_channels - 1; ich >= 0; ich--) {
634 s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
635 if (s->channel_residues[ich][icoef] > range - 1)
636 s->mclms_prevvalues[s->mclms_recent] = range - 1;
637 else if (s->channel_residues[ich][icoef] < -range)
638 s->mclms_prevvalues[s->mclms_recent] = -range;
640 s->mclms_updates[s->mclms_recent] = 0;
641 if (s->channel_residues[ich][icoef] > 0)
642 s->mclms_updates[s->mclms_recent] = 1;
643 else if (s->channel_residues[ich][icoef] < 0)
644 s->mclms_updates[s->mclms_recent] = -1;
647 if (s->mclms_recent == 0) {
648 memcpy(&s->mclms_prevvalues[order * num_channels],
650 2 * order * num_channels);
651 memcpy(&s->mclms_updates[order * num_channels],
653 2 * order * num_channels);
654 s->mclms_recent = num_channels * order;
658 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
661 int order = s->mclms_order;
662 int num_channels = s->num_channels;
664 for (ich = 0; ich < num_channels; ich++) {
666 if (!s->is_channel_coded[ich])
668 for (i = 0; i < order * num_channels; i++)
669 pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
670 s->mclms_coeffs[i + order * num_channels * ich];
671 for (i = 0; i < ich; i++)
672 pred[ich] += s->channel_residues[i][icoef] *
673 s->mclms_coeffs_cur[i + num_channels * ich];
674 pred[ich] += 1 << s->mclms_scaling - 1;
675 pred[ich] >>= s->mclms_scaling;
676 s->channel_residues[ich][icoef] += pred[ich];
680 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
682 int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
683 for (icoef = 0; icoef < tile_size; icoef++) {
684 mclms_predict(s, icoef, pred);
685 mclms_update(s, icoef, pred);
689 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
692 int recent = s->cdlms[ich][ilms].recent;
694 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
695 pred += s->cdlms[ich][ilms].coefs[icoef] *
696 s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
701 static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
702 int input, int residue)
705 int recent = s->cdlms[ich][ilms].recent;
706 int range = 1 << s->bits_per_sample - 1;
709 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
710 s->cdlms[ich][ilms].coefs[icoef] -=
711 s->cdlms[ich][ilms].lms_updates[icoef + recent];
712 } else if (residue > 0) {
713 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
714 s->cdlms[ich][ilms].coefs[icoef] +=
715 s->cdlms[ich][ilms].lms_updates[icoef + recent];
721 memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
722 s->cdlms[ich][ilms].lms_prevvalues,
723 2 * s->cdlms[ich][ilms].order);
724 memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
725 s->cdlms[ich][ilms].lms_updates,
726 2 * s->cdlms[ich][ilms].order);
727 recent = s->cdlms[ich][ilms].order - 1;
730 s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
732 s->cdlms[ich][ilms].lms_updates[recent] = 0;
734 s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
736 s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
738 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
739 s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
740 s->cdlms[ich][ilms].recent = recent;
743 static void use_high_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] == 16)
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;
758 s->update_speed[ich] = 16;
761 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
763 int ilms, recent, icoef;
764 for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
765 recent = s->cdlms[ich][ilms].recent;
766 if (s->update_speed[ich] == 8)
769 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
770 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
772 for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
773 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
775 s->update_speed[ich] = 8;
778 static void revert_cdlms(WmallDecodeCtx *s, int ch,
779 int coef_begin, int coef_end)
781 int icoef, pred, ilms, num_lms, residue, input;
783 num_lms = s->cdlms_ttl[ch];
784 for (ilms = num_lms - 1; ilms >= 0; ilms--) {
785 for (icoef = coef_begin; icoef < coef_end; icoef++) {
786 pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
787 residue = s->channel_residues[ch][icoef];
788 pred += lms_predict(s, ch, ilms);
789 input = residue + (pred >> s->cdlms[ch][ilms].scaling);
790 lms_update(s, ch, ilms, input, residue);
791 s->channel_residues[ch][icoef] = input;
796 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
798 if (s->num_channels != 2)
800 else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
802 for (icoef = 0; icoef < tile_size; icoef++) {
803 s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
804 s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
809 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
812 int64_t *filter_coeffs = s->acfilter_coeffs;
813 int scaling = s->acfilter_scaling;
814 int order = s->acfilter_order;
816 for (ich = 0; ich < s->num_channels; ich++) {
817 int *prevvalues = s->acfilter_prevvalues[ich];
818 for (i = 0; i < order; i++) {
820 for (j = 0; j < order; j++) {
822 pred += filter_coeffs[j] * prevvalues[j - i];
824 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
827 s->channel_residues[ich][i] += pred;
829 for (i = order; i < tile_size; i++) {
831 for (j = 0; j < order; j++)
832 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
834 s->channel_residues[ich][i] += pred;
836 for (j = 0; j < order; j++)
837 prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
841 static int decode_subframe(WmallDecodeCtx *s)
843 int offset = s->samples_per_frame;
844 int subframe_len = s->samples_per_frame;
845 int total_samples = s->samples_per_frame * s->num_channels;
846 int i, j, rawpcm_tile, padding_zeroes, res;
848 s->subframe_offset = get_bits_count(&s->gb);
850 /* reset channel context and find the next block offset and size
851 == the next block of the channel with the smallest number of
853 for (i = 0; i < s->num_channels; i++) {
854 if (offset > s->channel[i].decoded_samples) {
855 offset = s->channel[i].decoded_samples;
857 s->channel[i].subframe_len[s->channel[i].cur_subframe];
861 /* get a list of all channels that contain the estimated block */
862 s->channels_for_cur_subframe = 0;
863 for (i = 0; i < s->num_channels; i++) {
864 const int cur_subframe = s->channel[i].cur_subframe;
865 /* subtract already processed samples */
866 total_samples -= s->channel[i].decoded_samples;
868 /* and count if there are multiple subframes that match our profile */
869 if (offset == s->channel[i].decoded_samples &&
870 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
871 total_samples -= s->channel[i].subframe_len[cur_subframe];
872 s->channel[i].decoded_samples +=
873 s->channel[i].subframe_len[cur_subframe];
874 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
875 ++s->channels_for_cur_subframe;
879 /* check if the frame will be complete after processing the
882 s->parsed_all_subframes = 1;
885 s->seekable_tile = get_bits1(&s->gb);
886 if (s->seekable_tile) {
887 clear_codec_buffers(s);
889 s->do_arith_coding = get_bits1(&s->gb);
890 if (s->do_arith_coding) {
891 avpriv_request_sample(s->avctx, "Arithmetic coding");
892 return AVERROR_PATCHWELCOME;
894 s->do_ac_filter = get_bits1(&s->gb);
895 s->do_inter_ch_decorr = get_bits1(&s->gb);
896 s->do_mclms = get_bits1(&s->gb);
904 if ((res = decode_cdlms(s)) < 0)
906 s->movave_scaling = get_bits(&s->gb, 3);
907 s->quant_stepsize = get_bits(&s->gb, 8) + 1;
910 } else if (!s->cdlms[0][0].order) {
911 av_log(s->avctx, AV_LOG_DEBUG,
912 "Waiting for seekable tile\n");
913 av_frame_unref(s->frame);
917 rawpcm_tile = get_bits1(&s->gb);
919 for (i = 0; i < s->num_channels; i++)
920 s->is_channel_coded[i] = 1;
923 for (i = 0; i < s->num_channels; i++)
924 s->is_channel_coded[i] = get_bits1(&s->gb);
928 s->do_lpc = get_bits1(&s->gb);
931 avpriv_request_sample(s->avctx, "Expect wrong output since "
932 "inverse LPC filter");
939 if (get_bits1(&s->gb))
940 padding_zeroes = get_bits(&s->gb, 5);
945 int bits = s->bits_per_sample - padding_zeroes;
947 av_log(s->avctx, AV_LOG_ERROR,
948 "Invalid number of padding bits in raw PCM tile\n");
949 return AVERROR_INVALIDDATA;
951 av_dlog(s->avctx, "RAWPCM %d bits per sample. "
952 "total %d bits, remain=%d\n", bits,
953 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
954 for (i = 0; i < s->num_channels; i++)
955 for (j = 0; j < subframe_len; j++)
956 s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
958 for (i = 0; i < s->num_channels; i++)
959 if (s->is_channel_coded[i]) {
960 decode_channel_residues(s, i, subframe_len);
961 if (s->seekable_tile)
962 use_high_update_speed(s, i);
964 use_normal_update_speed(s, i);
965 revert_cdlms(s, i, 0, subframe_len);
967 memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
971 revert_mclms(s, subframe_len);
972 if (s->do_inter_ch_decorr)
973 revert_inter_ch_decorr(s, subframe_len);
975 revert_acfilter(s, subframe_len);
978 if (s->quant_stepsize != 1)
979 for (i = 0; i < s->num_channels; i++)
980 for (j = 0; j < subframe_len; j++)
981 s->channel_residues[i][j] *= s->quant_stepsize;
983 /* Write to proper output buffer depending on bit-depth */
984 for (i = 0; i < s->channels_for_cur_subframe; i++) {
985 int c = s->channel_indexes_for_cur_subframe[i];
986 int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
988 for (j = 0; j < subframe_len; j++) {
989 if (s->bits_per_sample == 16) {
990 *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
992 *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
997 /* handled one subframe */
998 for (i = 0; i < s->channels_for_cur_subframe; i++) {
999 int c = s->channel_indexes_for_cur_subframe[i];
1000 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1001 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1002 return AVERROR_INVALIDDATA;
1004 ++s->channel[c].cur_subframe;
1010 * @brief Decode one WMA frame.
1011 * @param s codec context
1012 * @return 0 if the trailer bit indicates that this is the last frame,
1013 * 1 if there are additional frames
1015 static int decode_frame(WmallDecodeCtx *s)
1017 GetBitContext* gb = &s->gb;
1018 int more_frames = 0, len = 0, i, ret;
1020 s->frame->nb_samples = s->samples_per_frame;
1021 if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1022 /* return an error if no frame could be decoded at all */
1023 av_log(s->avctx, AV_LOG_ERROR,
1024 "not enough space for the output samples\n");
1028 for (i = 0; i < s->num_channels; i++) {
1029 s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1030 s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1033 /* get frame length */
1035 len = get_bits(gb, s->log2_frame_size);
1037 /* decode tile information */
1038 if (decode_tilehdr(s)) {
1044 if (s->dynamic_range_compression)
1045 s->drc_gain = get_bits(gb, 8);
1047 /* no idea what these are for, might be the number of samples
1048 that need to be skipped at the beginning or end of a stream */
1049 if (get_bits1(gb)) {
1052 /* usually true for the first frame */
1053 if (get_bits1(gb)) {
1054 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1055 av_dlog(s->avctx, "start skip: %i\n", skip);
1058 /* sometimes true for the last frame */
1059 if (get_bits1(gb)) {
1060 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1061 av_dlog(s->avctx, "end skip: %i\n", skip);
1066 /* reset subframe states */
1067 s->parsed_all_subframes = 0;
1068 for (i = 0; i < s->num_channels; i++) {
1069 s->channel[i].decoded_samples = 0;
1070 s->channel[i].cur_subframe = 0;
1073 /* decode all subframes */
1074 while (!s->parsed_all_subframes) {
1075 if (decode_subframe(s) < 0) {
1081 av_dlog(s->avctx, "Frame done\n");
1086 if (s->len_prefix) {
1087 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1088 /* FIXME: not sure if this is always an error */
1089 av_log(s->avctx, AV_LOG_ERROR,
1090 "frame[%i] would have to skip %i bits\n", s->frame_num,
1091 len - (get_bits_count(gb) - s->frame_offset) - 1);
1096 /* skip the rest of the frame data */
1097 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1100 /* decode trailer bit */
1101 more_frames = get_bits1(gb);
1107 * @brief Calculate remaining input buffer length.
1108 * @param s codec context
1109 * @param gb bitstream reader context
1110 * @return remaining size in bits
1112 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
1114 return s->buf_bit_size - get_bits_count(gb);
1118 * @brief Fill the bit reservoir with a (partial) frame.
1119 * @param s codec context
1120 * @param gb bitstream reader context
1121 * @param len length of the partial frame
1122 * @param append decides whether to reset the buffer or not
1124 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1130 /* when the frame data does not need to be concatenated, the input buffer
1131 is reset and additional bits from the previous frame are copied
1132 and skipped later so that a fast byte copy is possible */
1135 s->frame_offset = get_bits_count(gb) & 7;
1136 s->num_saved_bits = s->frame_offset;
1137 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1140 buflen = (s->num_saved_bits + len + 8) >> 3;
1142 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1143 avpriv_request_sample(s->avctx, "Too small input buffer");
1148 s->num_saved_bits += len;
1150 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1153 int align = 8 - (get_bits_count(gb) & 7);
1154 align = FFMIN(align, len);
1155 put_bits(&s->pb, align, get_bits(gb, align));
1157 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1159 skip_bits_long(gb, len);
1162 flush_put_bits(&tmp);
1164 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1165 skip_bits(&s->gb, s->frame_offset);
1168 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1171 WmallDecodeCtx *s = avctx->priv_data;
1172 GetBitContext* gb = &s->pgb;
1173 const uint8_t* buf = avpkt->data;
1174 int buf_size = avpkt->size;
1175 int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1177 s->frame->nb_samples = 0;
1179 if (s->packet_done || s->packet_loss) {
1182 /* sanity check for the buffer length */
1183 if (buf_size < avctx->block_align)
1186 s->next_packet_start = buf_size - avctx->block_align;
1187 buf_size = avctx->block_align;
1188 s->buf_bit_size = buf_size << 3;
1190 /* parse packet header */
1191 init_get_bits(gb, buf, s->buf_bit_size);
1192 packet_sequence_number = get_bits(gb, 4);
1193 skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1194 spliced_packet = get_bits1(gb);
1196 avpriv_request_sample(avctx, "Bitstream splicing");
1198 /* get number of bits that need to be added to the previous frame */
1199 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1201 /* check for packet loss */
1202 if (!s->packet_loss &&
1203 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1205 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x 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 av_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, MAX_FRAMESIZE);
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 : get_bits_count(gb) >> 3;
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, MAX_FRAMESIZE);
1291 static av_cold int decode_close(AVCodecContext *avctx)
1293 WmallDecodeCtx *s = avctx->priv_data;
1295 av_frame_free(&s->frame);
1300 AVCodec ff_wmalossless_decoder = {
1301 .name = "wmalossless",
1302 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1303 .type = AVMEDIA_TYPE_AUDIO,
1304 .id = AV_CODEC_ID_WMALOSSLESS,
1305 .priv_data_size = sizeof(WmallDecodeCtx),
1306 .init = decode_init,
1307 .close = decode_close,
1308 .decode = decode_packet,
1310 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1311 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1313 AV_SAMPLE_FMT_NONE },