Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  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
  7.  *
  8.  * This file is part of FFmpeg.
  9.  *
  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.
  14.  *
  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.
  19.  *
  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
  23.  */
  24.  
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/avassert.h"
  27.  
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. #include "get_bits.h"
  31. #include "put_bits.h"
  32. #include "wma.h"
  33. #include "wma_common.h"
  34.  
  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
  40. #define MAX_ORDER             256
  41.  
  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
  46.  
  47.  
  48. /**
  49.  * @brief frame-specific decoder context for a single channel
  50.  */
  51. typedef struct {
  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
  61. } WmallChannelCtx;
  62.  
  63. /**
  64.  * @brief main decoder context
  65.  */
  66. typedef struct WmallDecodeCtx {
  67.     /* generic decoder variables */
  68.     AVCodecContext  *avctx;
  69.     AVFrame         *frame;
  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
  72.  
  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;
  86.  
  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
  97.  
  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?
  107.  
  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];
  112.  
  113.     WmallChannelCtx channel[WMALL_MAX_CHANNELS];    ///< per channel data
  114.  
  115.     // WMA Lossless-specific
  116.  
  117.     uint8_t do_arith_coding;
  118.     uint8_t do_ac_filter;
  119.     uint8_t do_inter_ch_decorr;
  120.     uint8_t do_mclms;
  121.     uint8_t do_lpc;
  122.  
  123.     int8_t  acfilter_order;
  124.     int8_t  acfilter_scaling;
  125.     int64_t acfilter_coeffs[16];
  126.     int     acfilter_prevvalues[WMALL_MAX_CHANNELS][16];
  127.  
  128.     int8_t  mclms_order;
  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];
  134.     int     mclms_recent;
  135.  
  136.     int     movave_scaling;
  137.     int     quant_stepsize;
  138.  
  139.     struct {
  140.         int order;
  141.         int scaling;
  142.         int coefsend;
  143.         int bitsend;
  144.         int16_t coefs[MAX_ORDER];
  145.         int16_t lms_prevvalues[MAX_ORDER * 2];
  146.         int16_t lms_updates[MAX_ORDER * 2];
  147.         int recent;
  148.     } cdlms[WMALL_MAX_CHANNELS][9];
  149.  
  150.     int cdlms_ttl[WMALL_MAX_CHANNELS];
  151.  
  152.     int bV3RTM;
  153.  
  154.     int is_channel_coded[WMALL_MAX_CHANNELS];
  155.     int update_speed[WMALL_MAX_CHANNELS];
  156.  
  157.     int transient[WMALL_MAX_CHANNELS];
  158.     int transient_pos[WMALL_MAX_CHANNELS];
  159.     int seekable_tile;
  160.  
  161.     int ave_sum[WMALL_MAX_CHANNELS];
  162.  
  163.     int channel_residues[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
  164.  
  165.     int lpc_coefs[WMALL_MAX_CHANNELS][40];
  166.     int lpc_order;
  167.     int lpc_scaling;
  168.     int lpc_intbits;
  169.  
  170.     int channel_coeffs[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
  171. } WmallDecodeCtx;
  172.  
  173.  
  174. static av_cold int decode_init(AVCodecContext *avctx)
  175. {
  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;
  180.  
  181.     if (!avctx->block_align) {
  182.         av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
  183.         return AVERROR(EINVAL);
  184.     }
  185.  
  186.     s->avctx = avctx;
  187.     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
  188.  
  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;
  199.         } else {
  200.             av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
  201.                    s->bits_per_sample);
  202.             return AVERROR_INVALIDDATA;
  203.         }
  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");
  208.  
  209.     } else {
  210.         avpriv_request_sample(avctx, "Unsupported extradata size");
  211.         return AVERROR_PATCHWELCOME;
  212.     }
  213.  
  214.     /* generic init */
  215.     s->log2_frame_size = av_log2(avctx->block_align) + 4;
  216.  
  217.     /* frame info */
  218.     s->skip_frame  = 1; /* skip first frame */
  219.     s->packet_loss = 1;
  220.     s->len_prefix  = s->decode_flags & 0x40;
  221.  
  222.     /* get frame len */
  223.     s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
  224.                                                           3, s->decode_flags);
  225.     av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
  226.  
  227.     /* init previous block len */
  228.     for (i = 0; i < avctx->channels; i++)
  229.         s->channel[i].prev_block_len = s->samples_per_frame;
  230.  
  231.     /* subframe info */
  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;
  236.  
  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;
  240.  
  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;
  245.     }
  246.  
  247.     s->num_channels = avctx->channels;
  248.  
  249.     /* extract lfe channel position */
  250.     s->lfe_channel = -1;
  251.  
  252.     if (channel_mask & 8) {
  253.         unsigned int mask;
  254.         for (mask = 1; mask < 16; mask <<= 1)
  255.             if (channel_mask & mask)
  256.                 ++s->lfe_channel;
  257.     }
  258.  
  259.     if (s->num_channels < 0) {
  260.         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
  261.                s->num_channels);
  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;
  267.     }
  268.  
  269.     s->frame = av_frame_alloc();
  270.     if (!s->frame)
  271.         return AVERROR(ENOMEM);
  272.  
  273.     avctx->channel_layout = channel_mask;
  274.     return 0;
  275. }
  276.  
  277. /**
  278.  * @brief Decode the subframe length.
  279.  * @param s      context
  280.  * @param offset sample offset in the frame
  281.  * @return decoded subframe length on success, < 0 in case of an error
  282.  */
  283. static int decode_subframe_length(WmallDecodeCtx *s, int offset)
  284. {
  285.     int frame_len_ratio, subframe_len, len;
  286.  
  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;
  290.  
  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);
  294.  
  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",
  299.                subframe_len);
  300.         return AVERROR_INVALIDDATA;
  301.     }
  302.     return subframe_len;
  303. }
  304.  
  305. /**
  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.
  311.  *
  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.
  321.  *
  322.  * @param s context
  323.  * @return 0 on success, < 0 in case of an error
  324.  */
  325. static int decode_tilehdr(WmallDecodeCtx *s)
  326. {
  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) */
  332.     int c, tile_aligned;
  333.  
  334.     /* reset tiling information */
  335.     for (c = 0; c < s->num_channels; c++)
  336.         s->channel[c].num_subframes = 0;
  337.  
  338.     tile_aligned = get_bits1(&s->gb);
  339.     if (s->max_num_subframes == 1 || tile_aligned)
  340.         fixed_channel_layout = 1;
  341.  
  342.     /* loop until the frame data is split between the subframes */
  343.     do {
  344.         int subframe_len, in_use = 0;
  345.  
  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;
  352.                 } else {
  353.                     if (get_bits1(&s->gb))
  354.                         contains_subframe[c] = in_use = 1;
  355.                 }
  356.             } else
  357.                 contains_subframe[c] = 0;
  358.         }
  359.  
  360.         if (!in_use) {
  361.             av_log(s->avctx, AV_LOG_ERROR,
  362.                    "Found empty subframe\n");
  363.             return AVERROR_INVALIDDATA;
  364.         }
  365.  
  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];
  373.  
  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;
  379.                 }
  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;
  388.                 }
  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];
  393.                 }
  394.                 ++channels_for_cur_subframe;
  395.             }
  396.         }
  397.     } while (min_channel_len < s->samples_per_frame);
  398.  
  399.     for (c = 0; c < s->num_channels; c++) {
  400.         int i, offset = 0;
  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];
  404.         }
  405.     }
  406.  
  407.     return 0;
  408. }
  409.  
  410. static void decode_ac_filter(WmallDecodeCtx *s)
  411. {
  412.     int i;
  413.     s->acfilter_order   = get_bits(&s->gb, 4) + 1;
  414.     s->acfilter_scaling = get_bits(&s->gb, 4);
  415.  
  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;
  419. }
  420.  
  421. static void decode_mclms(WmallDecodeCtx *s)
  422. {
  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)
  429.             cbits++;
  430.  
  431.         send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
  432.  
  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);
  435.  
  436.         for (i = 0; i < s->num_channels; i++) {
  437.             int c;
  438.             for (c = 0; c < i; c++)
  439.                 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
  440.         }
  441.     }
  442. }
  443.  
  444. static int decode_cdlms(WmallDecodeCtx *s)
  445. {
  446.     int c, i;
  447.     int cdlms_send_coef = get_bits1(&s->gb);
  448.  
  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;
  459.             }
  460.         }
  461.  
  462.         for (i = 0; i < s->cdlms_ttl[c]; i++)
  463.             s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
  464.  
  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)
  470.                     cbits++;
  471.                 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
  472.  
  473.                 cbits = av_log2(s->cdlms[c][i].scaling + 1);
  474.                 if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
  475.                     cbits++;
  476.  
  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;
  483.             }
  484.         }
  485.     }
  486.  
  487.     return 0;
  488. }
  489.  
  490. static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
  491. {
  492.     int i = 0;
  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;
  503.  
  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);
  507.     }
  508.  
  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);
  512.         else
  513.             s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
  514.         i++;
  515.     }
  516.     for (; i < tile_size; i++) {
  517.         int quo = 0, rem, rem_bits, residue;
  518.         while(get_bits1(&s->gb)) {
  519.             quo++;
  520.             if (get_bits_left(&s->gb) <= 0)
  521.                 return -1;
  522.         }
  523.         if (quo >= 32)
  524.             quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
  525.  
  526.         ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
  527.         if (ave_mean <= 1)
  528.             residue = quo;
  529.         else {
  530.             rem_bits = av_ceil_log2(ave_mean);
  531.             rem      = get_bits_long(&s->gb, rem_bits);
  532.             residue  = (quo << rem_bits) + rem;
  533.         }
  534.  
  535.         s->ave_sum[ch] = residue + s->ave_sum[ch] -
  536.                          (s->ave_sum[ch] >> s->movave_scaling);
  537.  
  538.         if (residue & 1)
  539.             residue = -(residue >> 1) - 1;
  540.         else
  541.             residue = residue >> 1;
  542.         s->channel_residues[ch][i] = residue;
  543.     }
  544.  
  545.     return 0;
  546.  
  547. }
  548.  
  549. static void decode_lpc(WmallDecodeCtx *s)
  550. {
  551.     int ch, i, cbits;
  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);
  559. }
  560.  
  561. static void clear_codec_buffers(WmallDecodeCtx *s)
  562. {
  563.     int ich, ilms;
  564.  
  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));
  568.  
  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));
  573.  
  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));
  582.         }
  583.         s->ave_sum[ich] = 0;
  584.     }
  585. }
  586.  
  587. /**
  588.  * @brief Reset filter parameters and transient area at new seekable tile.
  589.  */
  590. static void reset_codec(WmallDecodeCtx *s)
  591. {
  592.     int ich, ilms;
  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;
  602.     }
  603. }
  604.  
  605. static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
  606. {
  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);
  611.  
  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;
  623.             }
  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;
  633.             }
  634.         }
  635.     }
  636.  
  637.     for (ich = num_channels - 1; ich >= 0; ich--) {
  638.         s->mclms_recent--;
  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;
  644.  
  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;
  650.     }
  651.  
  652.     if (s->mclms_recent == 0) {
  653.         memcpy(&s->mclms_prevvalues[order * num_channels],
  654.                s->mclms_prevvalues,
  655.                2 * order * num_channels);
  656.         memcpy(&s->mclms_updates[order * num_channels],
  657.                s->mclms_updates,
  658.                2 * order * num_channels);
  659.         s->mclms_recent = num_channels * order;
  660.     }
  661. }
  662.  
  663. static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
  664. {
  665.     int ich, i;
  666.     int order        = s->mclms_order;
  667.     int num_channels = s->num_channels;
  668.  
  669.     for (ich = 0; ich < num_channels; ich++) {
  670.         pred[ich] = 0;
  671.         if (!s->is_channel_coded[ich])
  672.             continue;
  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];
  682.     }
  683. }
  684.  
  685. static void revert_mclms(WmallDecodeCtx *s, int tile_size)
  686. {
  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);
  691.     }
  692. }
  693.  
  694. static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
  695. {
  696.     int pred = 0, icoef;
  697.     int recent = s->cdlms[ich][ilms].recent;
  698.  
  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];
  702.  
  703.     return pred;
  704. }
  705.  
  706. static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
  707.                        int input, int residue)
  708. {
  709.     int icoef;
  710.     int recent = s->cdlms[ich][ilms].recent;
  711.     int range  = 1 << s->bits_per_sample - 1;
  712.  
  713.     if (residue < 0) {
  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];
  721.     }
  722.  
  723.     if (recent)
  724.         recent--;
  725.     else {
  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;
  733.     }
  734.  
  735.     s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
  736.     if (!input)
  737.         s->cdlms[ich][ilms].lms_updates[recent] = 0;
  738.     else if (input < 0)
  739.         s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
  740.     else
  741.         s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
  742.  
  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;
  746. }
  747.  
  748. static void use_high_update_speed(WmallDecodeCtx *s, int ich)
  749. {
  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)
  754.             continue;
  755.         if (s->bV3RTM) {
  756.             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  757.                 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
  758.         } else {
  759.             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  760.                 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
  761.         }
  762.     }
  763.     s->update_speed[ich] = 16;
  764. }
  765.  
  766. static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
  767. {
  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)
  772.             continue;
  773.         if (s->bV3RTM)
  774.             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  775.                 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
  776.         else
  777.             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  778.                 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
  779.     }
  780.     s->update_speed[ich] = 8;
  781. }
  782.  
  783. static void revert_cdlms(WmallDecodeCtx *s, int ch,
  784.                          int coef_begin, int coef_end)
  785. {
  786.     int icoef, pred, ilms, num_lms, residue, input;
  787.  
  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;
  797.         }
  798.     }
  799. }
  800.  
  801. static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
  802. {
  803.     if (s->num_channels != 2)
  804.         return;
  805.     else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
  806.         int icoef;
  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];
  810.         }
  811.     }
  812. }
  813.  
  814. static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
  815. {
  816.     int ich, pred, i, j;
  817.     int64_t *filter_coeffs = s->acfilter_coeffs;
  818.     int scaling            = s->acfilter_scaling;
  819.     int order              = s->acfilter_order;
  820.  
  821.     for (ich = 0; ich < s->num_channels; ich++) {
  822.         int *prevvalues = s->acfilter_prevvalues[ich];
  823.         for (i = 0; i < order; i++) {
  824.             pred = 0;
  825.             for (j = 0; j < order; j++) {
  826.                 if (i <= j)
  827.                     pred += filter_coeffs[j] * prevvalues[j - i];
  828.                 else
  829.                     pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
  830.             }
  831.             pred >>= scaling;
  832.             s->channel_residues[ich][i] += pred;
  833.         }
  834.         for (i = order; i < tile_size; i++) {
  835.             pred = 0;
  836.             for (j = 0; j < order; j++)
  837.                 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
  838.             pred >>= scaling;
  839.             s->channel_residues[ich][i] += pred;
  840.         }
  841.         for (j = 0; j < order; j++)
  842.             prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
  843.     }
  844. }
  845.  
  846. static int decode_subframe(WmallDecodeCtx *s)
  847. {
  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;
  852.  
  853.     s->subframe_offset = get_bits_count(&s->gb);
  854.  
  855.     /* reset channel context and find the next block offset and size
  856.         == the next block of the channel with the smallest number of
  857.         decoded samples */
  858.     for (i = 0; i < s->num_channels; i++) {
  859.         if (offset > s->channel[i].decoded_samples) {
  860.             offset = s->channel[i].decoded_samples;
  861.             subframe_len =
  862.                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
  863.         }
  864.     }
  865.  
  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;
  872.  
  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;
  881.         }
  882.     }
  883.  
  884.     /* check if the frame will be complete after processing the
  885.         estimated block */
  886.     if (!total_samples)
  887.         s->parsed_all_subframes = 1;
  888.  
  889.  
  890.     s->seekable_tile = get_bits1(&s->gb);
  891.     if (s->seekable_tile) {
  892.         clear_codec_buffers(s);
  893.  
  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;
  898.         }
  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);
  902.  
  903.         if (s->do_ac_filter)
  904.             decode_ac_filter(s);
  905.  
  906.         if (s->do_mclms)
  907.             decode_mclms(s);
  908.  
  909.         if ((res = decode_cdlms(s)) < 0)
  910.             return res;
  911.         s->movave_scaling = get_bits(&s->gb, 3);
  912.         s->quant_stepsize = get_bits(&s->gb, 8) + 1;
  913.  
  914.         reset_codec(s);
  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);
  919.         return -1;
  920.     }
  921.  
  922.     rawpcm_tile = get_bits1(&s->gb);
  923.  
  924.     for (i = 0; i < s->num_channels; i++)
  925.         s->is_channel_coded[i] = 1;
  926.  
  927.     if (!rawpcm_tile) {
  928.         for (i = 0; i < s->num_channels; i++)
  929.             s->is_channel_coded[i] = get_bits1(&s->gb);
  930.  
  931.         if (s->bV3RTM) {
  932.             // LPC
  933.             s->do_lpc = get_bits1(&s->gb);
  934.             if (s->do_lpc) {
  935.                 decode_lpc(s);
  936.                 avpriv_request_sample(s->avctx, "Expect wrong output since "
  937.                                       "inverse LPC filter");
  938.             }
  939.         } else
  940.             s->do_lpc = 0;
  941.     }
  942.  
  943.  
  944.     if (get_bits1(&s->gb))
  945.         padding_zeroes = get_bits(&s->gb, 5);
  946.     else
  947.         padding_zeroes = 0;
  948.  
  949.     if (rawpcm_tile) {
  950.         int bits = s->bits_per_sample - padding_zeroes;
  951.         if (bits <= 0) {
  952.             av_log(s->avctx, AV_LOG_ERROR,
  953.                    "Invalid number of padding bits in raw PCM tile\n");
  954.             return AVERROR_INVALIDDATA;
  955.         }
  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);
  962.     } else {
  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);
  968.                 else
  969.                     use_normal_update_speed(s, i);
  970.                 revert_cdlms(s, i, 0, subframe_len);
  971.             } else {
  972.                 memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
  973.             }
  974.     }
  975.     if (s->do_mclms)
  976.         revert_mclms(s, subframe_len);
  977.     if (s->do_inter_ch_decorr)
  978.         revert_inter_ch_decorr(s, subframe_len);
  979.     if (s->do_ac_filter)
  980.         revert_acfilter(s, subframe_len);
  981.  
  982.     /* Dequantize */
  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;
  987.  
  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];
  992.  
  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;
  996.             } else {
  997.                 *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
  998.             }
  999.         }
  1000.     }
  1001.  
  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;
  1008.         }
  1009.         ++s->channel[c].cur_subframe;
  1010.     }
  1011.     return 0;
  1012. }
  1013.  
  1014. /**
  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
  1019.  */
  1020. static int decode_frame(WmallDecodeCtx *s)
  1021. {
  1022.     GetBitContext* gb = &s->gb;
  1023.     int more_frames = 0, len = 0, i, ret;
  1024.  
  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 */
  1028.         s->packet_loss = 1;
  1029.         return ret;
  1030.     }
  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];
  1034.     }
  1035.  
  1036.     /* get frame length */
  1037.     if (s->len_prefix)
  1038.         len = get_bits(gb, s->log2_frame_size);
  1039.  
  1040.     /* decode tile information */
  1041.     if (decode_tilehdr(s)) {
  1042.         s->packet_loss = 1;
  1043.         return 0;
  1044.     }
  1045.  
  1046.     /* read drc info */
  1047.     if (s->dynamic_range_compression)
  1048.         s->drc_gain = get_bits(gb, 8);
  1049.  
  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)) {
  1053.         int av_unused skip;
  1054.  
  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);
  1059.         }
  1060.  
  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);
  1065.         }
  1066.  
  1067.     }
  1068.  
  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;
  1074.     }
  1075.  
  1076.     /* decode all subframes */
  1077.     while (!s->parsed_all_subframes) {
  1078.         if (decode_subframe(s) < 0) {
  1079.             s->packet_loss = 1;
  1080.             return 0;
  1081.         }
  1082.     }
  1083.  
  1084.     av_dlog(s->avctx, "Frame done\n");
  1085.  
  1086.     if (s->skip_frame)
  1087.         s->skip_frame = 0;
  1088.  
  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);
  1095.             s->packet_loss = 1;
  1096.             return 0;
  1097.         }
  1098.  
  1099.         /* skip the rest of the frame data */
  1100.         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
  1101.     }
  1102.  
  1103.     /* decode trailer bit */
  1104.     more_frames = get_bits1(gb);
  1105.     ++s->frame_num;
  1106.     return more_frames;
  1107. }
  1108.  
  1109. /**
  1110.  * @brief Calculate remaining input buffer length.
  1111.  * @param s  codec context
  1112.  * @param gb bitstream reader context
  1113.  * @return remaining size in bits
  1114.  */
  1115. static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
  1116. {
  1117.     return s->buf_bit_size - get_bits_count(gb);
  1118. }
  1119.  
  1120. /**
  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
  1126.  */
  1127. static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
  1128.                       int append)
  1129. {
  1130.     int buflen;
  1131.     PutBitContext tmp;
  1132.  
  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 */
  1136.  
  1137.     if (!append) {
  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);
  1141.     }
  1142.  
  1143.     buflen = (s->num_saved_bits + len + 8) >> 3;
  1144.  
  1145.     if (len <= 0 || buflen > MAX_FRAMESIZE) {
  1146.         avpriv_request_sample(s->avctx, "Too small input buffer");
  1147.         s->packet_loss = 1;
  1148.         return;
  1149.     }
  1150.  
  1151.     s->num_saved_bits += len;
  1152.     if (!append) {
  1153.         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
  1154.                          s->num_saved_bits);
  1155.     } else {
  1156.         int align = 8 - (get_bits_count(gb) & 7);
  1157.         align = FFMIN(align, len);
  1158.         put_bits(&s->pb, align, get_bits(gb, align));
  1159.         len -= align;
  1160.         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
  1161.     }
  1162.     skip_bits_long(gb, len);
  1163.  
  1164.     tmp = s->pb;
  1165.     flush_put_bits(&tmp);
  1166.  
  1167.     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
  1168.     skip_bits(&s->gb, s->frame_offset);
  1169. }
  1170.  
  1171. static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
  1172.                          AVPacket* avpkt)
  1173. {
  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;
  1179.  
  1180.     s->frame->nb_samples = 0;
  1181.  
  1182.     if (s->packet_done || s->packet_loss) {
  1183.         s->packet_done = 0;
  1184.  
  1185.         if (!buf_size)
  1186.             return 0;
  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;
  1191.         }
  1192.  
  1193.         s->next_packet_start = buf_size - avctx->block_align;
  1194.         buf_size             = avctx->block_align;
  1195.         s->buf_bit_size      = buf_size << 3;
  1196.  
  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);
  1202.         if (spliced_packet)
  1203.             avpriv_request_sample(avctx, "Bitstream splicing");
  1204.  
  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);
  1207.  
  1208.         /* check for packet loss */
  1209.         if (!s->packet_loss &&
  1210.             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
  1211.             s->packet_loss = 1;
  1212.             av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
  1213.                    s->packet_sequence_number, packet_sequence_number);
  1214.         }
  1215.         s->packet_sequence_number = packet_sequence_number;
  1216.  
  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;
  1221.                 s->packet_done = 1;
  1222.             }
  1223.  
  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);
  1227.  
  1228.             /* decode the cross packet frame if it is valid */
  1229.             if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
  1230.                 decode_frame(s);
  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);
  1234.         }
  1235.  
  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;
  1240.             s->packet_loss    = 0;
  1241.             init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
  1242.         }
  1243.  
  1244.     } else {
  1245.         int frame_size;
  1246.  
  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);
  1250.  
  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);
  1265.         } else {
  1266.             s->packet_done = 1;
  1267.         }
  1268.     }
  1269.  
  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);
  1275.     }
  1276.  
  1277.     *got_frame_ptr   = s->frame->nb_samples > 0;
  1278.     av_frame_move_ref(data, s->frame);
  1279.  
  1280.     s->packet_offset = get_bits_count(gb) & 7;
  1281.  
  1282.     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
  1283. }
  1284.  
  1285. static void flush(AVCodecContext *avctx)
  1286. {
  1287.     WmallDecodeCtx *s    = avctx->priv_data;
  1288.     s->packet_loss       = 1;
  1289.     s->packet_done       = 0;
  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);
  1296. }
  1297.  
  1298. static av_cold int decode_close(AVCodecContext *avctx)
  1299. {
  1300.     WmallDecodeCtx *s = avctx->priv_data;
  1301.  
  1302.     av_frame_free(&s->frame);
  1303.  
  1304.     return 0;
  1305. }
  1306.  
  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,
  1316.     .flush          = flush,
  1317.     .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  1318.     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  1319.                                                       AV_SAMPLE_FMT_S32P,
  1320.                                                       AV_SAMPLE_FMT_NONE },
  1321. };
  1322.