Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/wmalosslessdec.c
Line
Count
Source
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 <inttypes.h>
26
27
#include "libavutil/attributes.h"
28
#include "libavutil/avassert.h"
29
#include "libavutil/mem.h"
30
#include "libavutil/mem_internal.h"
31
32
#include "avcodec.h"
33
#include "codec_internal.h"
34
#include "decode.h"
35
#include "get_bits.h"
36
#include "put_bits.h"
37
#include "lossless_audiodsp.h"
38
#include "wma_common.h"
39
40
/** current decoder limitations */
41
1.51k
#define WMALL_MAX_CHANNELS      8                       ///< max number of handled channels
42
245k
#define MAX_SUBFRAMES          32                       ///< max number of subframes per channel
43
#define MAX_BANDS              29                       ///< max number of scale factor bands
44
1.47k
#define MAX_FRAMESIZE       32768                       ///< maximum compressed frame size
45
56.1k
#define MAX_ORDER             256
46
47
#define WMALL_BLOCK_MIN_BITS    6                       ///< log2 of min block size
48
#define WMALL_BLOCK_MAX_BITS   14                       ///< log2 of max block size
49
#define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)    ///< maximum block size
50
#define WMALL_BLOCK_SIZES    (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1) ///< possible block sizes
51
52
49.5k
#define WMALL_COEFF_PAD_SIZE   16                       ///< pad coef buffers with 0 for use with SIMD
53
54
/**
55
 * @brief frame-specific decoder context for a single channel
56
 */
57
typedef struct WmallChannelCtx {
58
    int16_t     prev_block_len;                         ///< length of the previous block
59
    uint8_t     transmit_coefs;
60
    uint8_t     num_subframes;
61
    uint16_t    subframe_len[MAX_SUBFRAMES];            ///< subframe length in samples
62
    uint16_t    subframe_offsets[MAX_SUBFRAMES];        ///< subframe positions in the current frame
63
    uint8_t     cur_subframe;                           ///< current subframe number
64
    uint16_t    decoded_samples;                        ///< number of already processed samples
65
    int         quant_step;                             ///< quantization step for the current subframe
66
    int         transient_counter;                      ///< number of transient samples from the beginning of the transient zone
67
} WmallChannelCtx;
68
69
/**
70
 * @brief main decoder context
71
 */
72
typedef struct WmallDecodeCtx {
73
    /* generic decoder variables */
74
    AVCodecContext  *avctx;
75
    AVFrame         *frame;
76
    LLAudDSPContext dsp;                           ///< accelerated DSP functions
77
    uint8_t         *frame_data;                    ///< compressed frame data
78
    int             max_frame_size;                 ///< max bitstream size
79
    PutBitContext   pb;                             ///< context for filling the frame_data buffer
80
81
    /* frame size dependent frame information (set during initialization) */
82
    uint32_t        decode_flags;                   ///< used compression features
83
    int             len_prefix;                     ///< frame is prefixed with its length
84
    int             dynamic_range_compression;      ///< frame contains DRC data
85
    uint8_t         bits_per_sample;                ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
86
    uint16_t        samples_per_frame;              ///< number of samples to output
87
    uint16_t        log2_frame_size;
88
    int8_t          num_channels;                   ///< number of channels in the stream (same as AVCodecContext.num_channels)
89
    int8_t          lfe_channel;                    ///< lfe channel index
90
    uint8_t         max_num_subframes;
91
    uint8_t         subframe_len_bits;              ///< number of bits used for the subframe length
92
    uint8_t         max_subframe_len_bit;           ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
93
    uint16_t        min_samples_per_subframe;
94
95
    /* packet decode state */
96
    GetBitContext   pgb;                            ///< bitstream reader context for the packet
97
    int             next_packet_start;              ///< start offset of the next WMA packet in the demuxer packet
98
    uint8_t         packet_offset;                  ///< offset to the frame in the packet
99
    uint8_t         packet_sequence_number;         ///< current packet number
100
    int             num_saved_bits;                 ///< saved number of bits
101
    int             frame_offset;                   ///< frame offset in the bit reservoir
102
    int             subframe_offset;                ///< subframe offset in the bit reservoir
103
    uint8_t         packet_loss;                    ///< set in case of bitstream error
104
    uint8_t         packet_done;                    ///< set when a packet is fully decoded
105
106
    /* frame decode state */
107
    uint32_t        frame_num;                      ///< current frame number (not used for decoding)
108
    GetBitContext   gb;                             ///< bitstream reader context
109
    int             buf_bit_size;                   ///< buffer size in bits
110
    int16_t         *samples_16[WMALL_MAX_CHANNELS]; ///< current sample buffer pointer (16-bit)
111
    int32_t         *samples_32[WMALL_MAX_CHANNELS]; ///< current sample buffer pointer (24-bit)
112
    uint8_t         drc_gain;                       ///< gain for the DRC tool
113
    int8_t          skip_frame;                     ///< skip output step
114
    int8_t          parsed_all_subframes;           ///< all subframes decoded?
115
116
    /* subframe/block decode state */
117
    int16_t         subframe_len;                   ///< current subframe length
118
    int8_t          channels_for_cur_subframe;      ///< number of channels that contain the subframe
119
    int8_t          channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
120
121
    WmallChannelCtx channel[WMALL_MAX_CHANNELS];    ///< per channel data
122
123
    // WMA Lossless-specific
124
125
    uint8_t do_arith_coding;
126
    uint8_t do_ac_filter;
127
    uint8_t do_inter_ch_decorr;
128
    uint8_t do_mclms;
129
    uint8_t do_lpc;
130
131
    int8_t  acfilter_order;
132
    int8_t  acfilter_scaling;
133
    int16_t acfilter_coeffs[16];
134
    int     acfilter_prevvalues[WMALL_MAX_CHANNELS][16];
135
136
    int8_t  mclms_order;
137
    int8_t  mclms_scaling;
138
    int16_t mclms_coeffs[WMALL_MAX_CHANNELS * WMALL_MAX_CHANNELS * 32];
139
    int16_t mclms_coeffs_cur[WMALL_MAX_CHANNELS * WMALL_MAX_CHANNELS];
140
    int32_t mclms_prevvalues[WMALL_MAX_CHANNELS * 2 * 32];
141
    int32_t mclms_updates[WMALL_MAX_CHANNELS * 2 * 32];
142
    int     mclms_recent;
143
144
    int     movave_scaling;
145
    int     quant_stepsize;
146
147
    struct {
148
        int order;
149
        int scaling;
150
        int coefsend;
151
        int bitsend;
152
        DECLARE_ALIGNED(16, int16_t, coefs)[MAX_ORDER + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
153
        DECLARE_ALIGNED(16, int32_t, lms_prevvalues)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
154
        DECLARE_ALIGNED(16, int16_t, lms_updates)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
155
        int recent;
156
    } cdlms[WMALL_MAX_CHANNELS][9];
157
158
    int cdlms_ttl[WMALL_MAX_CHANNELS];
159
160
    int bV3RTM;
161
162
    int is_channel_coded[WMALL_MAX_CHANNELS];
163
    int update_speed[WMALL_MAX_CHANNELS];
164
165
    int transient[WMALL_MAX_CHANNELS];
166
    int transient_pos[WMALL_MAX_CHANNELS];
167
    int seekable_tile;
168
169
    unsigned ave_sum[WMALL_MAX_CHANNELS];
170
171
    int channel_residues[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE];
172
173
    int lpc_coefs[WMALL_MAX_CHANNELS][40];
174
    int lpc_order;
175
    int lpc_scaling;
176
    int lpc_intbits;
177
} WmallDecodeCtx;
178
179
/** Get sign of integer (1 for positive, -1 for negative and 0 for zero) */
180
229M
#define WMASIGN(x) (((x) > 0) - ((x) < 0))
181
182
static av_cold int decode_init(AVCodecContext *avctx)
183
1.68k
{
184
1.68k
    WmallDecodeCtx *s  = avctx->priv_data;
185
1.68k
    uint8_t *edata_ptr = avctx->extradata;
186
1.68k
    unsigned int channel_mask;
187
1.68k
    int i, log2_max_num_subframes;
188
189
1.68k
    if (avctx->block_align <= 0 || avctx->block_align > (1<<21)) {
190
108
        av_log(avctx, AV_LOG_ERROR, "block_align is not set or invalid\n");
191
108
        return AVERROR(EINVAL);
192
108
    }
193
194
1.57k
    if (avctx->extradata_size >= 18) {
195
1.53k
        s->decode_flags    = AV_RL16(edata_ptr + 14);
196
1.53k
        channel_mask       = AV_RL32(edata_ptr +  2);
197
1.53k
        s->bits_per_sample = AV_RL16(edata_ptr);
198
1.53k
        if (s->bits_per_sample == 16)
199
804
            avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
200
727
        else if (s->bits_per_sample == 24) {
201
713
            avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
202
713
            avctx->bits_per_raw_sample = 24;
203
713
        } else {
204
14
            av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %"PRIu8"\n",
205
14
                   s->bits_per_sample);
206
14
            return AVERROR_INVALIDDATA;
207
14
        }
208
        /* dump the extradata */
209
1.14M
        for (i = 0; i < avctx->extradata_size; i++)
210
1.14M
            ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
211
1.51k
        ff_dlog(avctx, "\n");
212
213
1.51k
    } else {
214
41
        avpriv_request_sample(avctx, "Unsupported extradata size");
215
41
        return AVERROR_PATCHWELCOME;
216
41
    }
217
218
1.51k
    if (channel_mask) {
219
1.30k
        av_channel_layout_uninit(&avctx->ch_layout);
220
1.30k
        av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
221
1.30k
    }
222
1.51k
    av_assert0(avctx->ch_layout.nb_channels >= 0);
223
1.51k
    if (avctx->ch_layout.nb_channels > WMALL_MAX_CHANNELS) {
224
40
        avpriv_request_sample(avctx,
225
40
                            "More than " AV_STRINGIFY(WMALL_MAX_CHANNELS) " channels");
226
40
        return AVERROR_PATCHWELCOME;
227
40
    }
228
229
1.47k
    s->num_channels = avctx->ch_layout.nb_channels;
230
231
    /* extract lfe channel position */
232
1.47k
    s->lfe_channel = -1;
233
234
1.47k
    if (channel_mask & 8) {
235
157
        unsigned int mask;
236
785
        for (mask = 1; mask < 16; mask <<= 1)
237
628
            if (channel_mask & mask)
238
292
                ++s->lfe_channel;
239
157
    }
240
241
1.47k
    s->max_frame_size = MAX_FRAMESIZE * avctx->ch_layout.nb_channels;
242
1.47k
    s->frame_data = av_mallocz(s->max_frame_size + AV_INPUT_BUFFER_PADDING_SIZE);
243
1.47k
    if (!s->frame_data)
244
0
        return AVERROR(ENOMEM);
245
246
1.47k
    s->avctx = avctx;
247
1.47k
    ff_llauddsp_init(&s->dsp);
248
1.47k
    init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
249
250
    /* generic init */
251
1.47k
    s->log2_frame_size = av_log2(avctx->block_align) + 4;
252
253
    /* frame info */
254
1.47k
    s->skip_frame  = 1; /* skip first frame */
255
1.47k
    s->packet_loss = 1;
256
1.47k
    s->len_prefix  = s->decode_flags & 0x40;
257
258
    /* get frame len */
259
1.47k
    s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
260
1.47k
                                                          3, s->decode_flags);
261
1.47k
    av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
262
263
    /* init previous block len */
264
7.96k
    for (i = 0; i < avctx->ch_layout.nb_channels; i++)
265
6.49k
        s->channel[i].prev_block_len = s->samples_per_frame;
266
267
    /* subframe info */
268
1.47k
    log2_max_num_subframes  = (s->decode_flags & 0x38) >> 3;
269
1.47k
    s->max_num_subframes    = 1 << log2_max_num_subframes;
270
1.47k
    s->max_subframe_len_bit = 0;
271
1.47k
    s->subframe_len_bits    = av_log2(log2_max_num_subframes) + 1;
272
273
1.47k
    s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
274
1.47k
    s->dynamic_range_compression = s->decode_flags & 0x80;
275
1.47k
    s->bV3RTM                    = s->decode_flags & 0x100;
276
277
1.47k
    if (s->max_num_subframes > MAX_SUBFRAMES) {
278
1
        av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRIu8"\n",
279
1
               s->max_num_subframes);
280
1
        return AVERROR_INVALIDDATA;
281
1
    }
282
283
1.47k
    s->frame = av_frame_alloc();
284
1.47k
    if (!s->frame)
285
0
        return AVERROR(ENOMEM);
286
287
1.47k
    return 0;
288
1.47k
}
289
290
/**
291
 * @brief Decode the subframe length.
292
 * @param s      context
293
 * @param offset sample offset in the frame
294
 * @return decoded subframe length on success, < 0 in case of an error
295
 */
296
static int decode_subframe_length(WmallDecodeCtx *s, int offset)
297
135k
{
298
135k
    int frame_len_ratio, subframe_len, len;
299
300
    /* no need to read from the bitstream when only one length is possible */
301
135k
    if (offset == s->samples_per_frame - s->min_samples_per_subframe)
302
81.0k
        return s->min_samples_per_subframe;
303
304
54.6k
    len             = av_log2(s->max_num_subframes - 1) + 1;
305
54.6k
    frame_len_ratio = get_bits(&s->gb, len);
306
54.6k
    subframe_len    = s->min_samples_per_subframe * (frame_len_ratio + 1);
307
308
    /* sanity check the length */
309
54.6k
    if (subframe_len < s->min_samples_per_subframe ||
310
54.6k
        subframe_len > s->samples_per_frame) {
311
0
        av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
312
0
               subframe_len);
313
0
        return AVERROR_INVALIDDATA;
314
0
    }
315
54.6k
    return subframe_len;
316
54.6k
}
317
318
/**
319
 * @brief Decode how the data in the frame is split into subframes.
320
 *       Every WMA frame contains the encoded data for a fixed number of
321
 *       samples per channel. The data for every channel might be split
322
 *       into several subframes. This function will reconstruct the list of
323
 *       subframes for every channel.
324
 *
325
 *       If the subframes are not evenly split, the algorithm estimates the
326
 *       channels with the lowest number of total samples.
327
 *       Afterwards, for each of these channels a bit is read from the
328
 *       bitstream that indicates if the channel contains a subframe with the
329
 *       next subframe size that is going to be read from the bitstream or not.
330
 *       If a channel contains such a subframe, the subframe size gets added to
331
 *       the channel's subframe list.
332
 *       The algorithm repeats these steps until the frame is properly divided
333
 *       between the individual channels.
334
 *
335
 * @param s context
336
 * @return 0 on success, < 0 in case of an error
337
 */
338
static int decode_tilehdr(WmallDecodeCtx *s)
339
85.4k
{
340
85.4k
    uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
341
85.4k
    uint8_t  contains_subframe[WMALL_MAX_CHANNELS];   /* flag indicating if a channel contains the current subframe */
342
85.4k
    int channels_for_cur_subframe = s->num_channels;  /* number of channels that contain the current subframe */
343
85.4k
    int fixed_channel_layout = 0;                     /* flag indicating that all channels use the same subfra2me offsets and sizes */
344
85.4k
    int min_channel_len = 0;                          /* smallest sum of samples (channels with this length will be processed first) */
345
85.4k
    int c, tile_aligned;
346
347
    /* reset tiling information */
348
255k
    for (c = 0; c < s->num_channels; c++)
349
169k
        s->channel[c].num_subframes = 0;
350
351
85.4k
    tile_aligned = get_bits1(&s->gb);
352
85.4k
    if (s->max_num_subframes == 1 || tile_aligned)
353
77.5k
        fixed_channel_layout = 1;
354
355
    /* loop until the frame data is split between the subframes */
356
138k
    do {
357
138k
        int subframe_len, in_use = 0;
358
359
        /* check which channels contain the subframe */
360
435k
        for (c = 0; c < s->num_channels; c++) {
361
297k
            if (num_samples[c] == min_channel_len) {
362
268k
                if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
363
230k
                   (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
364
230k
                    contains_subframe[c] = 1;
365
230k
                } else {
366
38.5k
                    contains_subframe[c] = get_bits1(&s->gb);
367
38.5k
                }
368
268k
                in_use |= contains_subframe[c];
369
268k
            } else
370
28.7k
                contains_subframe[c] = 0;
371
297k
        }
372
373
138k
        if (!in_use) {
374
2.43k
            av_log(s->avctx, AV_LOG_ERROR,
375
2.43k
                   "Found empty subframe\n");
376
2.43k
            return AVERROR_INVALIDDATA;
377
2.43k
        }
378
379
        /* get subframe length, subframe_len == 0 is not allowed */
380
135k
        if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
381
0
            return AVERROR_INVALIDDATA;
382
        /* add subframes to the individual channels and find new min_channel_len */
383
135k
        min_channel_len += subframe_len;
384
420k
        for (c = 0; c < s->num_channels; c++) {
385
285k
            WmallChannelCtx *chan = &s->channel[c];
386
387
285k
            if (contains_subframe[c]) {
388
244k
                if (chan->num_subframes >= MAX_SUBFRAMES) {
389
0
                    av_log(s->avctx, AV_LOG_ERROR,
390
0
                           "broken frame: num subframes > 31\n");
391
0
                    return AVERROR_INVALIDDATA;
392
0
                }
393
244k
                chan->subframe_len[chan->num_subframes] = subframe_len;
394
244k
                num_samples[c] += subframe_len;
395
244k
                ++chan->num_subframes;
396
244k
                if (num_samples[c] > s->samples_per_frame) {
397
479
                    av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
398
479
                           "channel len(%"PRIu16") > samples_per_frame(%"PRIu16")\n",
399
479
                           num_samples[c], s->samples_per_frame);
400
479
                    return AVERROR_INVALIDDATA;
401
479
                }
402
244k
            } else if (num_samples[c] <= min_channel_len) {
403
33.7k
                if (num_samples[c] < min_channel_len) {
404
9.03k
                    channels_for_cur_subframe = 0;
405
9.03k
                    min_channel_len = num_samples[c];
406
9.03k
                }
407
33.7k
                ++channels_for_cur_subframe;
408
33.7k
            }
409
285k
        }
410
135k
    } while (min_channel_len < s->samples_per_frame);
411
412
239k
    for (c = 0; c < s->num_channels; c++) {
413
157k
        int i, offset = 0;
414
388k
        for (i = 0; i < s->channel[c].num_subframes; i++) {
415
231k
            s->channel[c].subframe_offsets[i] = offset;
416
231k
            offset += s->channel[c].subframe_len[i];
417
231k
        }
418
157k
    }
419
420
82.5k
    return 0;
421
85.4k
}
422
423
static void decode_ac_filter(WmallDecodeCtx *s)
424
9.55k
{
425
9.55k
    int i;
426
9.55k
    s->acfilter_order   = get_bits(&s->gb, 4) + 1;
427
9.55k
    s->acfilter_scaling = get_bits(&s->gb, 4);
428
429
79.7k
    for (i = 0; i < s->acfilter_order; i++)
430
70.2k
        s->acfilter_coeffs[i] = get_bitsz(&s->gb, s->acfilter_scaling) + 1;
431
9.55k
}
432
433
static void decode_mclms(WmallDecodeCtx *s)
434
6.17k
{
435
6.17k
    s->mclms_order   = (get_bits(&s->gb, 4) + 1) * 2;
436
6.17k
    s->mclms_scaling = get_bits(&s->gb, 4);
437
6.17k
    if (get_bits1(&s->gb)) {
438
2.55k
        int i, send_coef_bits;
439
2.55k
        int cbits = av_log2(s->mclms_scaling + 1);
440
2.55k
        if (1 << cbits < s->mclms_scaling + 1)
441
1.72k
            cbits++;
442
443
2.55k
        send_coef_bits = get_bitsz(&s->gb, cbits) + 2;
444
445
946k
        for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
446
944k
            s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
447
448
11.3k
        for (i = 0; i < s->num_channels; i++) {
449
8.82k
            int c;
450
25.3k
            for (c = 0; c < i; c++)
451
16.5k
                s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
452
8.82k
        }
453
2.55k
    }
454
6.17k
}
455
456
static int decode_cdlms(WmallDecodeCtx *s)
457
11.6k
{
458
11.6k
    int c, i;
459
11.6k
    int cdlms_send_coef = get_bits1(&s->gb);
460
461
35.9k
    for (c = 0; c < s->num_channels; c++) {
462
27.0k
        s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
463
77.5k
        for (i = 0; i < s->cdlms_ttl[c]; i++) {
464
53.3k
            s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
465
53.3k
            if (s->cdlms[c][i].order > MAX_ORDER) {
466
2.83k
                av_log(s->avctx, AV_LOG_ERROR,
467
2.83k
                       "Order[%d][%d] %d > max (%d), not supported\n",
468
2.83k
                       c, i, s->cdlms[c][i].order, MAX_ORDER);
469
2.83k
                s->cdlms[0][0].order = 0;
470
2.83k
                return AVERROR_INVALIDDATA;
471
2.83k
            }
472
50.5k
            if(s->cdlms[c][i].order & 8 && s->bits_per_sample == 16) {
473
14.3k
                static int warned;
474
14.3k
                if(!warned)
475
1
                    avpriv_request_sample(s->avctx, "CDLMS of order %d",
476
1
                                          s->cdlms[c][i].order);
477
14.3k
                warned = 1;
478
14.3k
            }
479
50.5k
        }
480
481
73.7k
        for (i = 0; i < s->cdlms_ttl[c]; i++)
482
49.5k
            s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
483
484
24.2k
        if (cdlms_send_coef) {
485
42.6k
            for (i = 0; i < s->cdlms_ttl[c]; i++) {
486
32.9k
                int cbits, shift_l, shift_r, j;
487
32.9k
                cbits = av_log2(s->cdlms[c][i].order);
488
32.9k
                if ((1 << cbits) < s->cdlms[c][i].order)
489
9.44k
                    cbits++;
490
32.9k
                s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
491
492
32.9k
                cbits = av_log2(s->cdlms[c][i].scaling + 1);
493
32.9k
                if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
494
5.14k
                    cbits++;
495
496
32.9k
                s->cdlms[c][i].bitsend = get_bitsz(&s->gb, cbits) + 2;
497
32.9k
                shift_l = 32 - s->cdlms[c][i].bitsend;
498
32.9k
                shift_r = 32 - s->cdlms[c][i].scaling - 2;
499
198k
                for (j = 0; j < s->cdlms[c][i].coefsend; j++)
500
165k
                    s->cdlms[c][i].coefs[j] =
501
165k
                        (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
502
32.9k
            }
503
9.77k
        }
504
505
73.7k
        for (i = 0; i < s->cdlms_ttl[c]; i++)
506
49.5k
            memset(s->cdlms[c][i].coefs + s->cdlms[c][i].order,
507
49.5k
                   0, WMALL_COEFF_PAD_SIZE);
508
24.2k
    }
509
510
8.85k
    return 0;
511
11.6k
}
512
513
static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
514
11.0k
{
515
11.0k
    int i = 0;
516
11.0k
    unsigned int ave_mean;
517
11.0k
    s->transient[ch] = get_bits1(&s->gb);
518
11.0k
    if (s->transient[ch]) {
519
4.88k
        s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
520
4.88k
        if (s->transient_pos[ch])
521
4.48k
            s->transient[ch] = 0;
522
4.88k
        s->channel[ch].transient_counter =
523
4.88k
            FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
524
6.15k
    } else if (s->channel[ch].transient_counter)
525
6.15k
        s->transient[ch] = 1;
526
527
11.0k
    if (s->seekable_tile) {
528
5.79k
        ave_mean = get_bits(&s->gb, s->bits_per_sample);
529
5.79k
        s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
530
5.79k
    }
531
532
11.0k
    if (s->seekable_tile) {
533
5.79k
        if (s->do_inter_ch_decorr)
534
1.62k
            s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
535
4.16k
        else
536
4.16k
            s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
537
5.79k
        i++;
538
5.79k
    }
539
21.0M
    for (; i < tile_size; i++) {
540
21.0M
        int rem, rem_bits;
541
21.0M
        unsigned quo = 0, residue;
542
48.4M
        while(get_bits1(&s->gb)) {
543
27.3M
            quo++;
544
27.3M
            if (get_bits_left(&s->gb) <= 0)
545
4.77k
                return -1;
546
27.3M
        }
547
21.0M
        if (quo >= 32)
548
338k
            quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
549
550
21.0M
        ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
551
21.0M
        if (ave_mean <= 1)
552
18.8M
            residue = quo;
553
2.24M
        else {
554
2.24M
            rem_bits = av_ceil_log2(ave_mean);
555
2.24M
            rem      = get_bits_long(&s->gb, rem_bits);
556
2.24M
            residue  = (quo << rem_bits) + rem;
557
2.24M
        }
558
559
21.0M
        s->ave_sum[ch] = residue + s->ave_sum[ch] -
560
21.0M
                         (s->ave_sum[ch] >> s->movave_scaling);
561
562
21.0M
        residue = (residue >> 1) ^ -(residue & 1);
563
21.0M
        s->channel_residues[ch][i] = residue;
564
21.0M
    }
565
566
6.27k
    return 0;
567
568
11.0k
}
569
570
static void decode_lpc(WmallDecodeCtx *s)
571
1.29k
{
572
1.29k
    int ch, i, cbits;
573
1.29k
    s->lpc_order   = get_bits(&s->gb, 5) + 1;
574
1.29k
    s->lpc_scaling = get_bits(&s->gb, 4);
575
1.29k
    s->lpc_intbits = get_bits(&s->gb, 3) + 1;
576
1.29k
    cbits = s->lpc_scaling + s->lpc_intbits;
577
7.99k
    for (ch = 0; ch < s->num_channels; ch++)
578
173k
        for (i = 0; i < s->lpc_order; i++)
579
166k
            s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
580
1.29k
}
581
582
static void clear_codec_buffers(WmallDecodeCtx *s)
583
13.8k
{
584
13.8k
    int ich, ilms;
585
586
13.8k
    memset(s->acfilter_coeffs,     0, sizeof(s->acfilter_coeffs));
587
13.8k
    memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
588
13.8k
    memset(s->lpc_coefs,           0, sizeof(s->lpc_coefs));
589
590
13.8k
    memset(s->mclms_coeffs,     0, sizeof(s->mclms_coeffs));
591
13.8k
    memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
592
13.8k
    memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
593
13.8k
    memset(s->mclms_updates,    0, sizeof(s->mclms_updates));
594
595
51.6k
    for (ich = 0; ich < s->num_channels; ich++) {
596
112k
        for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
597
75.1k
            memset(s->cdlms[ich][ilms].coefs, 0,
598
75.1k
                   sizeof(s->cdlms[ich][ilms].coefs));
599
75.1k
            memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
600
75.1k
                   sizeof(s->cdlms[ich][ilms].lms_prevvalues));
601
75.1k
            memset(s->cdlms[ich][ilms].lms_updates, 0,
602
75.1k
                   sizeof(s->cdlms[ich][ilms].lms_updates));
603
75.1k
        }
604
37.7k
        s->ave_sum[ich] = 0;
605
37.7k
    }
606
13.8k
}
607
608
/**
609
 * @brief Reset filter parameters and transient area at new seekable tile.
610
 */
611
static void reset_codec(WmallDecodeCtx *s)
612
8.85k
{
613
8.85k
    int ich, ilms;
614
8.85k
    s->mclms_recent = s->mclms_order * s->num_channels;
615
32.8k
    for (ich = 0; ich < s->num_channels; ich++) {
616
73.1k
        for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
617
49.2k
            s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
618
        /* first sample of a seekable subframe is considered as the starting of
619
            a transient area which is samples_per_frame samples long */
620
23.9k
        s->channel[ich].transient_counter = s->samples_per_frame;
621
23.9k
        s->transient[ich]     = 1;
622
23.9k
        s->transient_pos[ich] = 0;
623
23.9k
    }
624
8.85k
}
625
626
static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
627
19.6M
{
628
19.6M
    int i, j, ich, pred_error;
629
19.6M
    int order        = s->mclms_order;
630
19.6M
    int num_channels = s->num_channels;
631
19.6M
    int range        = 1 << (s->bits_per_sample - 1);
632
633
97.4M
    for (ich = 0; ich < num_channels; ich++) {
634
77.8M
        pred_error = s->channel_residues[ich][icoef] - (unsigned)pred[ich];
635
77.8M
        if (pred_error > 0) {
636
473M
            for (i = 0; i < order * num_channels; i++)
637
468M
                s->mclms_coeffs[i + ich * order * num_channels] +=
638
468M
                    s->mclms_updates[s->mclms_recent + i];
639
18.8M
            for (j = 0; j < ich; j++)
640
14.3M
                s->mclms_coeffs_cur[ich * num_channels + j] += WMASIGN(s->channel_residues[j][icoef]);
641
73.2M
        } else if (pred_error < 0) {
642
608M
            for (i = 0; i < order * num_channels; i++)
643
604M
                s->mclms_coeffs[i + ich * order * num_channels] -=
644
604M
                    s->mclms_updates[s->mclms_recent + i];
645
19.3M
            for (j = 0; j < ich; j++)
646
14.5M
                s->mclms_coeffs_cur[ich * num_channels + j] -= WMASIGN(s->channel_residues[j][icoef]);
647
4.80M
        }
648
77.8M
    }
649
650
97.4M
    for (ich = num_channels - 1; ich >= 0; ich--) {
651
77.8M
        s->mclms_recent--;
652
77.8M
        s->mclms_prevvalues[s->mclms_recent] = av_clip(s->channel_residues[ich][icoef],
653
77.8M
            -range, range - 1);
654
77.8M
        s->mclms_updates[s->mclms_recent] = WMASIGN(s->channel_residues[ich][icoef]);
655
77.8M
    }
656
657
19.6M
    if (s->mclms_recent == 0) {
658
6.08M
        memcpy(&s->mclms_prevvalues[order * num_channels],
659
6.08M
               s->mclms_prevvalues,
660
6.08M
               sizeof(int32_t) * order * num_channels);
661
6.08M
        memcpy(&s->mclms_updates[order * num_channels],
662
6.08M
               s->mclms_updates,
663
6.08M
               sizeof(int32_t) * order * num_channels);
664
6.08M
        s->mclms_recent = num_channels * order;
665
6.08M
    }
666
19.6M
}
667
668
static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
669
19.6M
{
670
19.6M
    int ich, i;
671
19.6M
    int order        = s->mclms_order;
672
19.6M
    int num_channels = s->num_channels;
673
674
97.4M
    for (ich = 0; ich < num_channels; ich++) {
675
77.8M
        pred[ich] = 0;
676
77.8M
        if (!s->is_channel_coded[ich])
677
46.2M
            continue;
678
2.51G
        for (i = 0; i < order * num_channels; i++)
679
2.48G
            pred[ich] += (uint32_t)s->mclms_prevvalues[i + s->mclms_recent] *
680
2.48G
                         s->mclms_coeffs[i + order * num_channels * ich];
681
132M
        for (i = 0; i < ich; i++)
682
100M
            pred[ich] += (uint32_t)s->channel_residues[i][icoef] *
683
100M
                         s->mclms_coeffs_cur[i + num_channels * ich];
684
31.5M
        pred[ich] += (1U << s->mclms_scaling) >> 1;
685
31.5M
        pred[ich] >>= s->mclms_scaling;
686
31.5M
        s->channel_residues[ich][icoef] += (unsigned)pred[ich];
687
31.5M
    }
688
19.6M
}
689
690
static void revert_mclms(WmallDecodeCtx *s, int tile_size)
691
3.34k
{
692
3.34k
    int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
693
19.6M
    for (icoef = 0; icoef < tile_size; icoef++) {
694
19.6M
        mclms_predict(s, icoef, pred);
695
19.6M
        mclms_update(s, icoef, pred);
696
19.6M
    }
697
3.34k
}
698
699
static void use_high_update_speed(WmallDecodeCtx *s, int ich)
700
5.79k
{
701
5.79k
    int ilms, recent, icoef;
702
27.8k
    for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
703
22.0k
        recent = s->cdlms[ich][ilms].recent;
704
22.0k
        if (s->update_speed[ich] == 16)
705
15.1k
            continue;
706
6.90k
        if (s->bV3RTM) {
707
52.4k
            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
708
50.0k
                s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
709
4.49k
        } else {
710
173k
            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
711
168k
                s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
712
4.49k
        }
713
6.90k
    }
714
5.79k
    s->update_speed[ich] = 16;
715
5.79k
}
716
717
static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
718
5.25k
{
719
5.25k
    int ilms, recent, icoef;
720
16.7k
    for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
721
11.5k
        recent = s->cdlms[ich][ilms].recent;
722
11.5k
        if (s->update_speed[ich] == 8)
723
6.57k
            continue;
724
4.94k
        if (s->bV3RTM)
725
35.6k
            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
726
34.8k
                s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
727
4.22k
        else
728
221k
            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
729
217k
                s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
730
4.94k
    }
731
5.25k
    s->update_speed[ich] = 8;
732
5.25k
}
733
734
#define CD_LMS(bits, ROUND) \
735
61.4M
static void lms_update ## bits (WmallDecodeCtx *s, int ich, int ilms, int input) \
736
61.4M
{ \
737
61.4M
    int recent = s->cdlms[ich][ilms].recent; \
738
61.4M
    int range  = 1 << s->bits_per_sample - 1; \
739
61.4M
    int order  = s->cdlms[ich][ilms].order; \
740
61.4M
    int ##bits##_t *prev = (int##bits##_t *)s->cdlms[ich][ilms].lms_prevvalues; \
741
61.4M
 \
742
61.4M
    if (recent) \
743
61.4M
        recent--; \
744
61.4M
    else { \
745
3.83M
        memcpy(prev + order, prev, (bits/8) * order); \
746
3.83M
        memcpy(s->cdlms[ich][ilms].lms_updates + order, \
747
3.83M
               s->cdlms[ich][ilms].lms_updates, \
748
3.83M
               sizeof(*s->cdlms[ich][ilms].lms_updates) * order); \
749
3.83M
        recent = order - 1; \
750
3.83M
    } \
751
61.4M
 \
752
61.4M
    prev[recent] = av_clip(input, -range, range - 1); \
753
61.4M
    s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich]; \
754
61.4M
 \
755
61.4M
    s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2; \
756
61.4M
    s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1; \
757
61.4M
    s->cdlms[ich][ilms].recent = recent; \
758
61.4M
    memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0, \
759
61.4M
           sizeof(s->cdlms[ich][ilms].lms_updates) - \
760
61.4M
           sizeof(*s->cdlms[ich][ilms].lms_updates)*(recent+order)); \
761
61.4M
} \
wmalosslessdec.c:lms_update32
Line
Count
Source
735
17.8M
static void lms_update ## bits (WmallDecodeCtx *s, int ich, int ilms, int input) \
736
17.8M
{ \
737
17.8M
    int recent = s->cdlms[ich][ilms].recent; \
738
17.8M
    int range  = 1 << s->bits_per_sample - 1; \
739
17.8M
    int order  = s->cdlms[ich][ilms].order; \
740
17.8M
    int ##bits##_t *prev = (int##bits##_t *)s->cdlms[ich][ilms].lms_prevvalues; \
741
17.8M
 \
742
17.8M
    if (recent) \
743
17.8M
        recent--; \
744
17.8M
    else { \
745
900k
        memcpy(prev + order, prev, (bits/8) * order); \
746
900k
        memcpy(s->cdlms[ich][ilms].lms_updates + order, \
747
900k
               s->cdlms[ich][ilms].lms_updates, \
748
900k
               sizeof(*s->cdlms[ich][ilms].lms_updates) * order); \
749
900k
        recent = order - 1; \
750
900k
    } \
751
17.8M
 \
752
17.8M
    prev[recent] = av_clip(input, -range, range - 1); \
753
17.8M
    s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich]; \
754
17.8M
 \
755
17.8M
    s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2; \
756
17.8M
    s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1; \
757
17.8M
    s->cdlms[ich][ilms].recent = recent; \
758
17.8M
    memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0, \
759
17.8M
           sizeof(s->cdlms[ich][ilms].lms_updates) - \
760
17.8M
           sizeof(*s->cdlms[ich][ilms].lms_updates)*(recent+order)); \
761
17.8M
} \
wmalosslessdec.c:lms_update16
Line
Count
Source
735
43.5M
static void lms_update ## bits (WmallDecodeCtx *s, int ich, int ilms, int input) \
736
43.5M
{ \
737
43.5M
    int recent = s->cdlms[ich][ilms].recent; \
738
43.5M
    int range  = 1 << s->bits_per_sample - 1; \
739
43.5M
    int order  = s->cdlms[ich][ilms].order; \
740
43.5M
    int ##bits##_t *prev = (int##bits##_t *)s->cdlms[ich][ilms].lms_prevvalues; \
741
43.5M
 \
742
43.5M
    if (recent) \
743
43.5M
        recent--; \
744
43.5M
    else { \
745
2.93M
        memcpy(prev + order, prev, (bits/8) * order); \
746
2.93M
        memcpy(s->cdlms[ich][ilms].lms_updates + order, \
747
2.93M
               s->cdlms[ich][ilms].lms_updates, \
748
2.93M
               sizeof(*s->cdlms[ich][ilms].lms_updates) * order); \
749
2.93M
        recent = order - 1; \
750
2.93M
    } \
751
43.5M
 \
752
43.5M
    prev[recent] = av_clip(input, -range, range - 1); \
753
43.5M
    s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich]; \
754
43.5M
 \
755
43.5M
    s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2; \
756
43.5M
    s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1; \
757
43.5M
    s->cdlms[ich][ilms].recent = recent; \
758
43.5M
    memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0, \
759
43.5M
           sizeof(s->cdlms[ich][ilms].lms_updates) - \
760
43.5M
           sizeof(*s->cdlms[ich][ilms].lms_updates)*(recent+order)); \
761
43.5M
} \
762
 \
763
static void revert_cdlms ## bits (WmallDecodeCtx *s, int ch, \
764
11.0k
                                  int coef_begin, int coef_end) \
765
11.0k
{ \
766
11.0k
    int icoef, ilms, num_lms, residue, input; \
767
11.0k
    unsigned pred;\
768
11.0k
 \
769
11.0k
    num_lms = s->cdlms_ttl[ch]; \
770
44.6k
    for (ilms = num_lms - 1; ilms >= 0; ilms--) { \
771
61.4M
        for (icoef = coef_begin; icoef < coef_end; icoef++) { \
772
61.4M
            int##bits##_t *prevvalues = (int##bits##_t *)s->cdlms[ch][ilms].lms_prevvalues; \
773
61.4M
            pred = (1 << s->cdlms[ch][ilms].scaling) >> 1; \
774
61.4M
            residue = s->channel_residues[ch][icoef]; \
775
61.4M
            pred += s->dsp.scalarproduct_and_madd_int## bits (s->cdlms[ch][ilms].coefs, \
776
61.4M
                                                        prevvalues + s->cdlms[ch][ilms].recent, \
777
61.4M
                                                        s->cdlms[ch][ilms].lms_updates + \
778
61.4M
                                                        s->cdlms[ch][ilms].recent, \
779
61.4M
                                                        FFALIGN(s->cdlms[ch][ilms].order, ROUND), \
780
61.4M
                                                        WMASIGN(residue)); \
781
61.4M
            input = residue + (unsigned)((int)pred >> s->cdlms[ch][ilms].scaling); \
782
61.4M
            lms_update ## bits(s, ch, ilms, input); \
783
61.4M
            s->channel_residues[ch][icoef] = input; \
784
61.4M
        } \
785
33.5k
    } \
786
11.0k
}
wmalosslessdec.c:revert_cdlms32
Line
Count
Source
764
4.51k
                                  int coef_begin, int coef_end) \
765
4.51k
{ \
766
4.51k
    int icoef, ilms, num_lms, residue, input; \
767
4.51k
    unsigned pred;\
768
4.51k
 \
769
4.51k
    num_lms = s->cdlms_ttl[ch]; \
770
27.1k
    for (ilms = num_lms - 1; ilms >= 0; ilms--) { \
771
17.9M
        for (icoef = coef_begin; icoef < coef_end; icoef++) { \
772
17.8M
            int##bits##_t *prevvalues = (int##bits##_t *)s->cdlms[ch][ilms].lms_prevvalues; \
773
17.8M
            pred = (1 << s->cdlms[ch][ilms].scaling) >> 1; \
774
17.8M
            residue = s->channel_residues[ch][icoef]; \
775
17.8M
            pred += s->dsp.scalarproduct_and_madd_int## bits (s->cdlms[ch][ilms].coefs, \
776
17.8M
                                                        prevvalues + s->cdlms[ch][ilms].recent, \
777
17.8M
                                                        s->cdlms[ch][ilms].lms_updates + \
778
17.8M
                                                        s->cdlms[ch][ilms].recent, \
779
17.8M
                                                        FFALIGN(s->cdlms[ch][ilms].order, ROUND), \
780
17.8M
                                                        WMASIGN(residue)); \
781
17.8M
            input = residue + (unsigned)((int)pred >> s->cdlms[ch][ilms].scaling); \
782
17.8M
            lms_update ## bits(s, ch, ilms, input); \
783
17.8M
            s->channel_residues[ch][icoef] = input; \
784
17.8M
        } \
785
22.6k
    } \
786
4.51k
}
wmalosslessdec.c:revert_cdlms16
Line
Count
Source
764
6.53k
                                  int coef_begin, int coef_end) \
765
6.53k
{ \
766
6.53k
    int icoef, ilms, num_lms, residue, input; \
767
6.53k
    unsigned pred;\
768
6.53k
 \
769
6.53k
    num_lms = s->cdlms_ttl[ch]; \
770
17.4k
    for (ilms = num_lms - 1; ilms >= 0; ilms--) { \
771
43.5M
        for (icoef = coef_begin; icoef < coef_end; icoef++) { \
772
43.5M
            int##bits##_t *prevvalues = (int##bits##_t *)s->cdlms[ch][ilms].lms_prevvalues; \
773
43.5M
            pred = (1 << s->cdlms[ch][ilms].scaling) >> 1; \
774
43.5M
            residue = s->channel_residues[ch][icoef]; \
775
43.5M
            pred += s->dsp.scalarproduct_and_madd_int## bits (s->cdlms[ch][ilms].coefs, \
776
43.5M
                                                        prevvalues + s->cdlms[ch][ilms].recent, \
777
43.5M
                                                        s->cdlms[ch][ilms].lms_updates + \
778
43.5M
                                                        s->cdlms[ch][ilms].recent, \
779
43.5M
                                                        FFALIGN(s->cdlms[ch][ilms].order, ROUND), \
780
43.5M
                                                        WMASIGN(residue)); \
781
43.5M
            input = residue + (unsigned)((int)pred >> s->cdlms[ch][ilms].scaling); \
782
43.5M
            lms_update ## bits(s, ch, ilms, input); \
783
43.5M
            s->channel_residues[ch][icoef] = input; \
784
43.5M
        } \
785
10.9k
    } \
786
6.53k
}
787
788
CD_LMS(16, WMALL_COEFF_PAD_SIZE)
789
CD_LMS(32, 8)
790
791
static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
792
59.0k
{
793
59.0k
    if (s->num_channels != 2)
794
57.4k
        return;
795
1.55k
    else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
796
1.22k
        int icoef;
797
8.63M
        for (icoef = 0; icoef < tile_size; icoef++) {
798
8.63M
            s->channel_residues[0][icoef] -= (unsigned)(s->channel_residues[1][icoef] >> 1);
799
8.63M
            s->channel_residues[1][icoef] += (unsigned) s->channel_residues[0][icoef];
800
8.63M
        }
801
1.22k
    }
802
59.0k
}
803
804
static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
805
66.0k
{
806
66.0k
    int ich, pred, i, j;
807
66.0k
    int16_t *filter_coeffs = s->acfilter_coeffs;
808
66.0k
    int scaling            = s->acfilter_scaling;
809
66.0k
    int order              = s->acfilter_order;
810
811
139k
    for (ich = 0; ich < s->num_channels; ich++) {
812
73.1k
        int *prevvalues = s->acfilter_prevvalues[ich];
813
656k
        for (i = 0; i < order; i++) {
814
583k
            pred = 0;
815
5.53M
            for (j = 0; j < order; j++) {
816
4.95M
                if (i <= j)
817
2.76M
                    pred += (uint32_t)filter_coeffs[j] * prevvalues[j - i];
818
2.18M
                else
819
2.18M
                    pred += (uint32_t)s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
820
4.95M
            }
821
583k
            pred >>= scaling;
822
583k
            s->channel_residues[ich][i] += (unsigned)pred;
823
583k
        }
824
570M
        for (i = order; i < tile_size; i++) {
825
570M
            pred = 0;
826
5.24G
            for (j = 0; j < order; j++)
827
4.67G
                pred += (uint32_t)s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
828
570M
            pred >>= scaling;
829
570M
            s->channel_residues[ich][i] += (unsigned)pred;
830
570M
        }
831
656k
        for (j = order - 1; j >= 0; j--)
832
583k
            if (tile_size <= j) {
833
1.69k
                prevvalues[j] = prevvalues[j - tile_size];
834
1.69k
            }else
835
581k
                prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
836
73.1k
    }
837
66.0k
}
838
839
static int decode_subframe(WmallDecodeCtx *s)
840
90.3k
{
841
90.3k
    int offset        = s->samples_per_frame;
842
90.3k
    int subframe_len  = s->samples_per_frame;
843
90.3k
    int total_samples = s->samples_per_frame * s->num_channels;
844
90.3k
    int i, j, rawpcm_tile, padding_zeroes, res;
845
846
90.3k
    s->subframe_offset = get_bits_count(&s->gb);
847
848
    /* reset channel context and find the next block offset and size
849
        == the next block of the channel with the smallest number of
850
        decoded samples */
851
251k
    for (i = 0; i < s->num_channels; i++) {
852
161k
        if (offset > s->channel[i].decoded_samples) {
853
90.7k
            offset = s->channel[i].decoded_samples;
854
90.7k
            subframe_len =
855
90.7k
                s->channel[i].subframe_len[s->channel[i].cur_subframe];
856
90.7k
        }
857
161k
    }
858
859
    /* get a list of all channels that contain the estimated block */
860
90.3k
    s->channels_for_cur_subframe = 0;
861
251k
    for (i = 0; i < s->num_channels; i++) {
862
161k
        const int cur_subframe = s->channel[i].cur_subframe;
863
        /* subtract already processed samples */
864
161k
        total_samples -= s->channel[i].decoded_samples;
865
866
        /* and count if there are multiple subframes that match our profile */
867
161k
        if (offset == s->channel[i].decoded_samples &&
868
160k
            subframe_len == s->channel[i].subframe_len[cur_subframe]) {
869
159k
            total_samples -= s->channel[i].subframe_len[cur_subframe];
870
159k
            s->channel[i].decoded_samples +=
871
159k
                s->channel[i].subframe_len[cur_subframe];
872
159k
            s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
873
159k
            ++s->channels_for_cur_subframe;
874
159k
        }
875
161k
    }
876
877
    /* check if the frame will be complete after processing the
878
        estimated block */
879
90.3k
    if (!total_samples)
880
70.6k
        s->parsed_all_subframes = 1;
881
882
883
90.3k
    s->seekable_tile = get_bits1(&s->gb);
884
90.3k
    if (s->seekable_tile) {
885
13.8k
        clear_codec_buffers(s);
886
887
13.8k
        s->do_arith_coding    = get_bits1(&s->gb);
888
13.8k
        if (s->do_arith_coding) {
889
2.18k
            avpriv_request_sample(s->avctx, "Arithmetic coding");
890
2.18k
            return AVERROR_PATCHWELCOME;
891
2.18k
        }
892
11.6k
        s->do_ac_filter       = get_bits1(&s->gb);
893
11.6k
        s->do_inter_ch_decorr = get_bits1(&s->gb);
894
11.6k
        s->do_mclms           = get_bits1(&s->gb);
895
896
11.6k
        if (s->do_ac_filter)
897
9.55k
            decode_ac_filter(s);
898
899
11.6k
        if (s->do_mclms)
900
6.17k
            decode_mclms(s);
901
902
11.6k
        if ((res = decode_cdlms(s)) < 0)
903
2.83k
            return res;
904
8.85k
        s->movave_scaling = get_bits(&s->gb, 3);
905
8.85k
        s->quant_stepsize = get_bits(&s->gb, 8) + 1;
906
907
8.85k
        reset_codec(s);
908
8.85k
    }
909
910
85.3k
    rawpcm_tile = get_bits1(&s->gb);
911
912
85.3k
    if (!rawpcm_tile && !s->cdlms[0][0].order) {
913
6.61k
        av_log(s->avctx, AV_LOG_DEBUG,
914
6.61k
               "Waiting for seekable tile\n");
915
6.61k
        av_frame_unref(s->frame);
916
6.61k
        return -1;
917
6.61k
    }
918
919
920
198k
    for (i = 0; i < s->num_channels; i++)
921
120k
        s->is_channel_coded[i] = 1;
922
923
78.6k
    if (!rawpcm_tile) {
924
184k
        for (i = 0; i < s->num_channels; i++)
925
109k
            s->is_channel_coded[i] = get_bits1(&s->gb);
926
927
75.6k
        if (s->bV3RTM) {
928
            // LPC
929
59.2k
            s->do_lpc = get_bits1(&s->gb);
930
59.2k
            if (s->do_lpc) {
931
1.29k
                decode_lpc(s);
932
1.29k
                avpriv_request_sample(s->avctx, "Expect wrong output since "
933
1.29k
                                      "inverse LPC filter");
934
1.29k
            }
935
59.2k
        } else
936
16.4k
            s->do_lpc = 0;
937
75.6k
    }
938
939
78.6k
    if (get_bits_left(&s->gb) < 1)
940
6.95k
        return AVERROR_INVALIDDATA;
941
942
71.7k
    if (get_bits1(&s->gb))
943
2.25k
        padding_zeroes = get_bits(&s->gb, 5);
944
69.4k
    else
945
69.4k
        padding_zeroes = 0;
946
947
71.7k
    if (rawpcm_tile) {
948
2.73k
        int bits = s->bits_per_sample - padding_zeroes;
949
2.73k
        if (bits <= 0) {
950
341
            av_log(s->avctx, AV_LOG_ERROR,
951
341
                   "Invalid number of padding bits in raw PCM tile\n");
952
341
            return AVERROR_INVALIDDATA;
953
341
        }
954
2.39k
        ff_dlog(s->avctx, "RAWPCM %d bits per sample. "
955
2.39k
                "total %d bits, remain=%d\n", bits,
956
2.39k
                bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
957
11.4k
        for (i = 0; i < s->num_channels; i++)
958
56.6M
            for (j = 0; j < subframe_len; j++)
959
56.6M
                s->channel_residues[i][j] = get_sbits_long(&s->gb, bits);
960
68.9k
    } else {
961
68.9k
        if (s->bits_per_sample < padding_zeroes)
962
336
            return AVERROR_INVALIDDATA;
963
152k
        for (i = 0; i < s->num_channels; i++) {
964
83.4k
            if (s->is_channel_coded[i]) {
965
11.0k
                decode_channel_residues(s, i, subframe_len);
966
11.0k
                if (s->seekable_tile)
967
5.79k
                    use_high_update_speed(s, i);
968
5.25k
                else
969
5.25k
                    use_normal_update_speed(s, i);
970
11.0k
                if (s->bits_per_sample > 16)
971
4.51k
                    revert_cdlms32(s, i, 0, subframe_len);
972
6.53k
                else
973
6.53k
                    revert_cdlms16(s, i, 0, subframe_len);
974
72.3k
            } else {
975
72.3k
                memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
976
72.3k
            }
977
83.4k
        }
978
979
68.6k
        if (s->do_mclms)
980
3.34k
            revert_mclms(s, subframe_len);
981
68.6k
        if (s->do_inter_ch_decorr)
982
59.0k
            revert_inter_ch_decorr(s, subframe_len);
983
68.6k
        if (s->do_ac_filter)
984
66.0k
            revert_acfilter(s, subframe_len);
985
986
        /* Dequantize */
987
68.6k
        if (s->quant_stepsize != 1)
988
129k
            for (i = 0; i < s->num_channels; i++)
989
579M
                for (j = 0; j < subframe_len; j++)
990
579M
                    s->channel_residues[i][j] *= (unsigned)s->quant_stepsize;
991
68.6k
    }
992
993
    /* Write to proper output buffer depending on bit-depth */
994
162k
    for (i = 0; i < s->channels_for_cur_subframe; i++) {
995
91.8k
        int c = s->channel_indexes_for_cur_subframe[i];
996
91.8k
        int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
997
998
676M
        for (j = 0; j < subframe_len; j++) {
999
676M
            if (s->bits_per_sample == 16) {
1000
116M
                *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] * (1 << padding_zeroes);
1001
560M
            } else {
1002
560M
                *s->samples_32[c]++ = s->channel_residues[c][j] * (256U << padding_zeroes);
1003
560M
            }
1004
676M
        }
1005
91.8k
    }
1006
1007
    /* handled one subframe */
1008
162k
    for (i = 0; i < s->channels_for_cur_subframe; i++) {
1009
91.8k
        int c = s->channel_indexes_for_cur_subframe[i];
1010
91.8k
        if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1011
0
            av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1012
0
            return AVERROR_INVALIDDATA;
1013
0
        }
1014
91.8k
        ++s->channel[c].cur_subframe;
1015
91.8k
    }
1016
71.0k
    return 0;
1017
71.0k
}
1018
1019
/**
1020
 * @brief Decode one WMA frame.
1021
 * @param s codec context
1022
 * @return 0 if the trailer bit indicates that this is the last frame,
1023
 *         1 if there are additional frames
1024
 */
1025
static int decode_frame(WmallDecodeCtx *s)
1026
85.4k
{
1027
85.4k
    GetBitContext* gb = &s->gb;
1028
85.4k
    int more_frames = 0, len = 0, i, ret;
1029
1030
85.4k
    s->frame->nb_samples = s->samples_per_frame;
1031
85.4k
    if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1032
        /* return an error if no frame could be decoded at all */
1033
0
        s->packet_loss = 1;
1034
0
        s->frame->nb_samples = 0;
1035
0
        return ret;
1036
0
    }
1037
255k
    for (i = 0; i < s->num_channels; i++) {
1038
169k
        s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1039
169k
        s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1040
169k
    }
1041
1042
    /* get frame length */
1043
85.4k
    if (s->len_prefix)
1044
10.6k
        len = get_bits(gb, s->log2_frame_size);
1045
1046
    /* decode tile information */
1047
85.4k
    if ((ret = decode_tilehdr(s))) {
1048
2.91k
        s->packet_loss = 1;
1049
2.91k
        av_frame_unref(s->frame);
1050
2.91k
        return ret;
1051
2.91k
    }
1052
1053
    /* read drc info */
1054
82.5k
    if (s->dynamic_range_compression)
1055
4.93k
        s->drc_gain = get_bits(gb, 8);
1056
1057
    /* no idea what these are for, might be the number of samples
1058
       that need to be skipped at the beginning or end of a stream */
1059
82.5k
    if (get_bits1(gb)) {
1060
3.92k
        av_unused int skip;
1061
1062
        /* usually true for the first frame */
1063
3.92k
        if (get_bits1(gb)) {
1064
2.27k
            skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1065
2.27k
            ff_dlog(s->avctx, "start skip: %i\n", skip);
1066
2.27k
        }
1067
1068
        /* sometimes true for the last frame */
1069
3.92k
        if (get_bits1(gb)) {
1070
2.20k
            skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1071
2.20k
            ff_dlog(s->avctx, "end skip: %i\n", skip);
1072
2.20k
            s->frame->nb_samples -= skip;
1073
2.20k
            if (s->frame->nb_samples <= 0)
1074
1.57k
                return AVERROR_INVALIDDATA;
1075
2.20k
        }
1076
1077
3.92k
    }
1078
1079
    /* reset subframe states */
1080
80.9k
    s->parsed_all_subframes = 0;
1081
230k
    for (i = 0; i < s->num_channels; i++) {
1082
149k
        s->channel[i].decoded_samples = 0;
1083
149k
        s->channel[i].cur_subframe    = 0;
1084
149k
    }
1085
1086
    /* decode all subframes */
1087
151k
    while (!s->parsed_all_subframes) {
1088
90.3k
        int decoded_samples = s->channel[0].decoded_samples;
1089
90.3k
        if (decode_subframe(s) < 0) {
1090
19.2k
            s->packet_loss = 1;
1091
19.2k
            if (s->frame->nb_samples)
1092
12.6k
                s->frame->nb_samples = decoded_samples;
1093
19.2k
            return 0;
1094
19.2k
        }
1095
90.3k
    }
1096
1097
61.6k
    ff_dlog(s->avctx, "Frame done\n");
1098
1099
61.6k
    s->skip_frame = 0;
1100
1101
61.6k
    if (s->len_prefix) {
1102
2.23k
        if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1103
            /* FIXME: not sure if this is always an error */
1104
1.04k
            av_log(s->avctx, AV_LOG_ERROR,
1105
1.04k
                   "frame[%"PRIu32"] would have to skip %i bits\n",
1106
1.04k
                   s->frame_num,
1107
1.04k
                   len - (get_bits_count(gb) - s->frame_offset) - 1);
1108
1.04k
            s->packet_loss = 1;
1109
1.04k
            return 0;
1110
1.04k
        }
1111
1112
        /* skip the rest of the frame data */
1113
1.19k
        skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1114
1.19k
    }
1115
1116
    /* decode trailer bit */
1117
60.6k
    more_frames = get_bits1(gb);
1118
60.6k
    ++s->frame_num;
1119
60.6k
    return more_frames;
1120
61.6k
}
1121
1122
/**
1123
 * @brief Calculate remaining input buffer length.
1124
 * @param s  codec context
1125
 * @param gb bitstream reader context
1126
 * @return remaining size in bits
1127
 */
1128
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
1129
1.00M
{
1130
1.00M
    return s->buf_bit_size - get_bits_count(gb);
1131
1.00M
}
1132
1133
/**
1134
 * @brief Fill the bit reservoir with a (partial) frame.
1135
 * @param s      codec context
1136
 * @param gb     bitstream reader context
1137
 * @param len    length of the partial frame
1138
 * @param append decides whether to reset the buffer or not
1139
 */
1140
static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1141
                      int append)
1142
383k
{
1143
383k
    int buflen;
1144
383k
    PutBitContext tmp;
1145
1146
    /* when the frame data does not need to be concatenated, the input buffer
1147
        is reset and additional bits from the previous frame are copied
1148
        and skipped later so that a fast byte copy is possible */
1149
1150
383k
    if (!append) {
1151
100k
        s->frame_offset   = get_bits_count(gb) & 7;
1152
100k
        s->num_saved_bits = s->frame_offset;
1153
100k
        init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
1154
100k
    }
1155
1156
383k
    buflen = (s->num_saved_bits + len + 8) >> 3;
1157
1158
383k
    if (len <= 0 || buflen > s->max_frame_size) {
1159
56.2k
        avpriv_request_sample(s->avctx, "Too small input buffer");
1160
56.2k
        s->packet_loss = 1;
1161
56.2k
        s->num_saved_bits = 0;
1162
56.2k
        return;
1163
56.2k
    }
1164
1165
327k
    s->num_saved_bits += len;
1166
327k
    if (!append) {
1167
100k
        ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1168
100k
                         s->num_saved_bits);
1169
227k
    } else {
1170
227k
        int align = 8 - (get_bits_count(gb) & 7);
1171
227k
        align = FFMIN(align, len);
1172
227k
        put_bits(&s->pb, align, get_bits(gb, align));
1173
227k
        len -= align;
1174
227k
        ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1175
227k
    }
1176
327k
    skip_bits_long(gb, len);
1177
1178
327k
    tmp = s->pb;
1179
327k
    flush_put_bits(&tmp);
1180
1181
327k
    init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1182
327k
    skip_bits(&s->gb, s->frame_offset);
1183
327k
}
1184
1185
static int decode_packet(AVCodecContext *avctx, AVFrame *rframe,
1186
                         int *got_frame_ptr, AVPacket* avpkt)
1187
520k
{
1188
520k
    WmallDecodeCtx *s = avctx->priv_data;
1189
520k
    GetBitContext* gb  = &s->pgb;
1190
520k
    const uint8_t* buf = avpkt->data;
1191
520k
    int buf_size       = avpkt->size;
1192
520k
    int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1193
1194
520k
    s->frame->nb_samples = 0;
1195
1196
520k
    if (!buf_size) {
1197
9.80k
        s->packet_done = 0;
1198
9.80k
        if (s->num_saved_bits <= get_bits_count(&s->gb))
1199
809
            return 0;
1200
8.99k
        if (!decode_frame(s))
1201
835
            s->num_saved_bits = 0;
1202
510k
    } else if (s->packet_done || s->packet_loss) {
1203
360k
        s->packet_done = 0;
1204
1205
360k
        s->next_packet_start = buf_size - FFMIN(avctx->block_align, buf_size);
1206
360k
        buf_size             = FFMIN(avctx->block_align, buf_size);
1207
360k
        s->buf_bit_size      = buf_size << 3;
1208
1209
        /* parse packet header */
1210
360k
        init_get_bits(gb, buf, s->buf_bit_size);
1211
360k
        packet_sequence_number = get_bits(gb, 4);
1212
360k
        skip_bits(gb, 1);   // Skip seekable_frame_in_packet, currently unused
1213
360k
        spliced_packet = get_bits1(gb);
1214
360k
        if (spliced_packet)
1215
229k
            avpriv_request_sample(avctx, "Bitstream splicing");
1216
1217
        /* get number of bits that need to be added to the previous frame */
1218
360k
        num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1219
1220
        /* check for packet loss */
1221
360k
        if (!s->packet_loss &&
1222
161k
            ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1223
135k
            s->packet_loss = 1;
1224
135k
            av_log(avctx, AV_LOG_ERROR,
1225
135k
                   "Packet loss detected! seq %"PRIx8" vs %x\n",
1226
135k
                   s->packet_sequence_number, packet_sequence_number);
1227
135k
        }
1228
360k
        s->packet_sequence_number = packet_sequence_number;
1229
1230
360k
        if (num_bits_prev_frame > 0) {
1231
283k
            int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1232
283k
            if (num_bits_prev_frame >= remaining_packet_bits) {
1233
243k
                num_bits_prev_frame = remaining_packet_bits;
1234
243k
                s->packet_done = 1;
1235
243k
            }
1236
1237
            /* Append the previous frame data to the remaining data from the
1238
             * previous packet to create a full frame. */
1239
283k
            save_bits(s, gb, num_bits_prev_frame, 1);
1240
1241
            /* decode the cross packet frame if it is valid */
1242
283k
            if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1243
16.7k
                decode_frame(s);
1244
283k
        } else if (s->num_saved_bits - s->frame_offset) {
1245
53.1k
            ff_dlog(avctx, "ignoring %x previously saved bits\n",
1246
53.1k
                    s->num_saved_bits - s->frame_offset);
1247
53.1k
        }
1248
1249
360k
        if (s->packet_loss) {
1250
            /* Reset number of saved bits so that the decoder does not start
1251
             * to decode incomplete frames in the s->len_prefix == 0 case. */
1252
347k
            s->num_saved_bits = 0;
1253
347k
            s->packet_loss    = 0;
1254
347k
            init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
1255
347k
        }
1256
1257
360k
    } else {
1258
150k
        int frame_size;
1259
1260
150k
        s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1261
150k
        init_get_bits(gb, avpkt->data, s->buf_bit_size);
1262
150k
        skip_bits(gb, s->packet_offset);
1263
1264
150k
        if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1265
17.3k
            (frame_size = show_bits(gb, s->log2_frame_size)) &&
1266
13.7k
            frame_size <= remaining_bits(s, gb)) {
1267
8.08k
            save_bits(s, gb, frame_size, 0);
1268
1269
8.08k
            if (!s->packet_loss)
1270
8.04k
                s->packet_done = !decode_frame(s);
1271
142k
        } else if (!s->len_prefix
1272
124k
                   && s->num_saved_bits > get_bits_count(&s->gb)) {
1273
            /* when the frames do not have a length prefix, we don't know the
1274
             * compressed length of the individual frames however, we know what
1275
             * part of a new packet belongs to the previous frame therefore we
1276
             * save the incoming packet first, then we append the "previous
1277
             * frame" data from the next packet so that we get a buffer that
1278
             * only contains full frames */
1279
51.6k
            s->packet_done = !decode_frame(s);
1280
90.8k
        } else {
1281
90.8k
            s->packet_done = 1;
1282
90.8k
        }
1283
150k
    }
1284
1285
519k
    if (remaining_bits(s, gb) < 0) {
1286
66.4k
        av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -remaining_bits(s, gb));
1287
66.4k
        s->packet_loss = 1;
1288
66.4k
    }
1289
1290
519k
    if (s->packet_done && !s->packet_loss &&
1291
283k
        remaining_bits(s, gb) > 0) {
1292
        /* save the rest of the data so that it can be decoded
1293
         * with the next packet */
1294
92.2k
        save_bits(s, gb, remaining_bits(s, gb), 0);
1295
92.2k
    }
1296
1297
519k
    *got_frame_ptr   = s->frame->nb_samples > 0;
1298
519k
    av_frame_move_ref(rframe, s->frame);
1299
1300
519k
    s->packet_offset = get_bits_count(gb) & 7;
1301
1302
519k
    return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1303
520k
}
1304
1305
static av_cold void flush(AVCodecContext *avctx)
1306
155k
{
1307
155k
    WmallDecodeCtx *s    = avctx->priv_data;
1308
155k
    s->packet_loss       = 1;
1309
155k
    s->packet_done       = 0;
1310
155k
    s->num_saved_bits    = 0;
1311
155k
    s->frame_offset      = 0;
1312
155k
    s->next_packet_start = 0;
1313
155k
    s->cdlms[0][0].order = 0;
1314
155k
    s->frame->nb_samples = 0;
1315
155k
    init_put_bits(&s->pb, s->frame_data, s->max_frame_size);
1316
155k
}
1317
1318
static av_cold int decode_close(AVCodecContext *avctx)
1319
1.68k
{
1320
1.68k
    WmallDecodeCtx *s = avctx->priv_data;
1321
1322
1.68k
    av_frame_free(&s->frame);
1323
1.68k
    av_freep(&s->frame_data);
1324
1325
1.68k
    return 0;
1326
1.68k
}
1327
1328
const FFCodec ff_wmalossless_decoder = {
1329
    .p.name         = "wmalossless",
1330
    CODEC_LONG_NAME("Windows Media Audio Lossless"),
1331
    .p.type         = AVMEDIA_TYPE_AUDIO,
1332
    .p.id           = AV_CODEC_ID_WMALOSSLESS,
1333
    .priv_data_size = sizeof(WmallDecodeCtx),
1334
    .init           = decode_init,
1335
    .close          = decode_close,
1336
    FF_CODEC_DECODE_CB(decode_packet),
1337
    .flush          = flush,
1338
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1339
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1340
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P),
1341
};