1/*
2 * Monkey's Audio lossless audio decoder
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 *  based upon libdemac from Dave Chapman.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <inttypes.h>
24
25#include "libavutil/avassert.h"
26#include "libavutil/channel_layout.h"
27#include "libavutil/opt.h"
28#include "lossless_audiodsp.h"
29#include "avcodec.h"
30#include "bswapdsp.h"
31#include "bytestream.h"
32#include "internal.h"
33#include "get_bits.h"
34#include "unary.h"
35
36/**
37 * @file
38 * Monkey's Audio lossless audio decoder
39 */
40
41#define MAX_CHANNELS        2
42#define MAX_BYTESPERSAMPLE  3
43
44#define APE_FRAMECODE_MONO_SILENCE    1
45#define APE_FRAMECODE_STEREO_SILENCE  3
46#define APE_FRAMECODE_PSEUDO_STEREO   4
47
48#define HISTORY_SIZE 512
49#define PREDICTOR_ORDER 8
50/** Total size of all predictor histories */
51#define PREDICTOR_SIZE 50
52
53#define YDELAYA (18 + PREDICTOR_ORDER*4)
54#define YDELAYB (18 + PREDICTOR_ORDER*3)
55#define XDELAYA (18 + PREDICTOR_ORDER*2)
56#define XDELAYB (18 + PREDICTOR_ORDER)
57
58#define YADAPTCOEFFSA 18
59#define XADAPTCOEFFSA 14
60#define YADAPTCOEFFSB 10
61#define XADAPTCOEFFSB 5
62
63/**
64 * Possible compression levels
65 * @{
66 */
67enum APECompressionLevel {
68    COMPRESSION_LEVEL_FAST       = 1000,
69    COMPRESSION_LEVEL_NORMAL     = 2000,
70    COMPRESSION_LEVEL_HIGH       = 3000,
71    COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
72    COMPRESSION_LEVEL_INSANE     = 5000
73};
74/** @} */
75
76#define APE_FILTER_LEVELS 3
77
78/** Filter orders depending on compression level */
79static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
80    {  0,   0,    0 },
81    { 16,   0,    0 },
82    { 64,   0,    0 },
83    { 32, 256,    0 },
84    { 16, 256, 1280 }
85};
86
87/** Filter fraction bits depending on compression level */
88static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
89    {  0,  0,  0 },
90    { 11,  0,  0 },
91    { 11,  0,  0 },
92    { 10, 13,  0 },
93    { 11, 13, 15 }
94};
95
96
97/** Filters applied to the decoded data */
98typedef struct APEFilter {
99    int16_t *coeffs;        ///< actual coefficients used in filtering
100    int16_t *adaptcoeffs;   ///< adaptive filter coefficients used for correcting of actual filter coefficients
101    int16_t *historybuffer; ///< filter memory
102    int16_t *delay;         ///< filtered values
103
104    int avg;
105} APEFilter;
106
107typedef struct APERice {
108    uint32_t k;
109    uint32_t ksum;
110} APERice;
111
112typedef struct APERangecoder {
113    uint32_t low;           ///< low end of interval
114    uint32_t range;         ///< length of interval
115    uint32_t help;          ///< bytes_to_follow resp. intermediate value
116    unsigned int buffer;    ///< buffer for input/output
117} APERangecoder;
118
119/** Filter histories */
120typedef struct APEPredictor {
121    int32_t *buf;
122
123    int32_t lastA[2];
124
125    int32_t filterA[2];
126    int32_t filterB[2];
127
128    int32_t coeffsA[2][4];  ///< adaption coefficients
129    int32_t coeffsB[2][5];  ///< adaption coefficients
130    int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
131
132    unsigned int sample_pos;
133} APEPredictor;
134
135/** Decoder context */
136typedef struct APEContext {
137    AVClass *class;                          ///< class for AVOptions
138    AVCodecContext *avctx;
139    BswapDSPContext bdsp;
140    LLAudDSPContext adsp;
141    int channels;
142    int samples;                             ///< samples left to decode in current frame
143    int bps;
144
145    int fileversion;                         ///< codec version, very important in decoding process
146    int compression_level;                   ///< compression levels
147    int fset;                                ///< which filter set to use (calculated from compression level)
148    int flags;                               ///< global decoder flags
149
150    uint32_t CRC;                            ///< frame CRC
151    int frameflags;                          ///< frame flags
152    APEPredictor predictor;                  ///< predictor used for final reconstruction
153
154    int32_t *decoded_buffer;
155    int decoded_size;
156    int32_t *decoded[MAX_CHANNELS];          ///< decoded data for each channel
157    int blocks_per_loop;                     ///< maximum number of samples to decode for each call
158
159    int16_t* filterbuf[APE_FILTER_LEVELS];   ///< filter memory
160
161    APERangecoder rc;                        ///< rangecoder used to decode actual values
162    APERice riceX;                           ///< rice code parameters for the second channel
163    APERice riceY;                           ///< rice code parameters for the first channel
164    APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
165    GetBitContext gb;
166
167    uint8_t *data;                           ///< current frame data
168    uint8_t *data_end;                       ///< frame data end
169    int data_size;                           ///< frame data allocated size
170    const uint8_t *ptr;                      ///< current position in frame data
171
172    int error;
173
174    void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
175    void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
176    void (*predictor_decode_mono)(struct APEContext *ctx, int count);
177    void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
178} APEContext;
179
180static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
181                              int32_t *decoded1, int count);
182
183static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
184static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
185static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
186static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
187static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
188static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
189static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
190static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
191static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
192
193static void predictor_decode_mono_3800(APEContext *ctx, int count);
194static void predictor_decode_stereo_3800(APEContext *ctx, int count);
195static void predictor_decode_mono_3930(APEContext *ctx, int count);
196static void predictor_decode_stereo_3930(APEContext *ctx, int count);
197static void predictor_decode_mono_3950(APEContext *ctx, int count);
198static void predictor_decode_stereo_3950(APEContext *ctx, int count);
199
200static av_cold int ape_decode_close(AVCodecContext *avctx)
201{
202    APEContext *s = avctx->priv_data;
203    int i;
204
205    for (i = 0; i < APE_FILTER_LEVELS; i++)
206        av_freep(&s->filterbuf[i]);
207
208    av_freep(&s->decoded_buffer);
209    av_freep(&s->data);
210    s->decoded_size = s->data_size = 0;
211
212    return 0;
213}
214
215static av_cold int ape_decode_init(AVCodecContext *avctx)
216{
217    APEContext *s = avctx->priv_data;
218    int i;
219
220    if (avctx->extradata_size != 6) {
221        av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
222        return AVERROR(EINVAL);
223    }
224    if (avctx->channels > 2) {
225        av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
226        return AVERROR(EINVAL);
227    }
228    s->bps = avctx->bits_per_coded_sample;
229    switch (s->bps) {
230    case 8:
231        avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
232        break;
233    case 16:
234        avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
235        break;
236    case 24:
237        avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
238        break;
239    default:
240        avpriv_request_sample(avctx,
241                              "%d bits per coded sample", s->bps);
242        return AVERROR_PATCHWELCOME;
243    }
244    s->avctx             = avctx;
245    s->channels          = avctx->channels;
246    s->fileversion       = AV_RL16(avctx->extradata);
247    s->compression_level = AV_RL16(avctx->extradata + 2);
248    s->flags             = AV_RL16(avctx->extradata + 4);
249
250    av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n",
251           s->compression_level, s->flags);
252    if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
253        !s->compression_level ||
254        (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
255        av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
256               s->compression_level);
257        return AVERROR_INVALIDDATA;
258    }
259    s->fset = s->compression_level / 1000 - 1;
260    for (i = 0; i < APE_FILTER_LEVELS; i++) {
261        if (!ape_filter_orders[s->fset][i])
262            break;
263        FF_ALLOC_OR_GOTO(avctx, s->filterbuf[i],
264                         (ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4,
265                         filter_alloc_fail);
266    }
267
268    if (s->fileversion < 3860) {
269        s->entropy_decode_mono   = entropy_decode_mono_0000;
270        s->entropy_decode_stereo = entropy_decode_stereo_0000;
271    } else if (s->fileversion < 3900) {
272        s->entropy_decode_mono   = entropy_decode_mono_3860;
273        s->entropy_decode_stereo = entropy_decode_stereo_3860;
274    } else if (s->fileversion < 3930) {
275        s->entropy_decode_mono   = entropy_decode_mono_3900;
276        s->entropy_decode_stereo = entropy_decode_stereo_3900;
277    } else if (s->fileversion < 3990) {
278        s->entropy_decode_mono   = entropy_decode_mono_3900;
279        s->entropy_decode_stereo = entropy_decode_stereo_3930;
280    } else {
281        s->entropy_decode_mono   = entropy_decode_mono_3990;
282        s->entropy_decode_stereo = entropy_decode_stereo_3990;
283    }
284
285    if (s->fileversion < 3930) {
286        s->predictor_decode_mono   = predictor_decode_mono_3800;
287        s->predictor_decode_stereo = predictor_decode_stereo_3800;
288    } else if (s->fileversion < 3950) {
289        s->predictor_decode_mono   = predictor_decode_mono_3930;
290        s->predictor_decode_stereo = predictor_decode_stereo_3930;
291    } else {
292        s->predictor_decode_mono   = predictor_decode_mono_3950;
293        s->predictor_decode_stereo = predictor_decode_stereo_3950;
294    }
295
296    ff_bswapdsp_init(&s->bdsp);
297    ff_llauddsp_init(&s->adsp);
298    avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
299
300    return 0;
301filter_alloc_fail:
302    ape_decode_close(avctx);
303    return AVERROR(ENOMEM);
304}
305
306/**
307 * @name APE range decoding functions
308 * @{
309 */
310
311#define CODE_BITS    32
312#define TOP_VALUE    ((unsigned int)1 << (CODE_BITS-1))
313#define SHIFT_BITS   (CODE_BITS - 9)
314#define EXTRA_BITS   ((CODE_BITS-2) % 8 + 1)
315#define BOTTOM_VALUE (TOP_VALUE >> 8)
316
317/** Start the decoder */
318static inline void range_start_decoding(APEContext *ctx)
319{
320    ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
321    ctx->rc.low    = ctx->rc.buffer >> (8 - EXTRA_BITS);
322    ctx->rc.range  = (uint32_t) 1 << EXTRA_BITS;
323}
324
325/** Perform normalization */
326static inline void range_dec_normalize(APEContext *ctx)
327{
328    while (ctx->rc.range <= BOTTOM_VALUE) {
329        ctx->rc.buffer <<= 8;
330        if(ctx->ptr < ctx->data_end) {
331            ctx->rc.buffer += *ctx->ptr;
332            ctx->ptr++;
333        } else {
334            ctx->error = 1;
335        }
336        ctx->rc.low    = (ctx->rc.low << 8)    | ((ctx->rc.buffer >> 1) & 0xFF);
337        ctx->rc.range  <<= 8;
338    }
339}
340
341/**
342 * Calculate culmulative frequency for next symbol. Does NO update!
343 * @param ctx decoder context
344 * @param tot_f is the total frequency or (code_value)1<<shift
345 * @return the culmulative frequency
346 */
347static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
348{
349    range_dec_normalize(ctx);
350    ctx->rc.help = ctx->rc.range / tot_f;
351    return ctx->rc.low / ctx->rc.help;
352}
353
354/**
355 * Decode value with given size in bits
356 * @param ctx decoder context
357 * @param shift number of bits to decode
358 */
359static inline int range_decode_culshift(APEContext *ctx, int shift)
360{
361    range_dec_normalize(ctx);
362    ctx->rc.help = ctx->rc.range >> shift;
363    return ctx->rc.low / ctx->rc.help;
364}
365
366
367/**
368 * Update decoding state
369 * @param ctx decoder context
370 * @param sy_f the interval length (frequency of the symbol)
371 * @param lt_f the lower end (frequency sum of < symbols)
372 */
373static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
374{
375    ctx->rc.low  -= ctx->rc.help * lt_f;
376    ctx->rc.range = ctx->rc.help * sy_f;
377}
378
379/** Decode n bits (n <= 16) without modelling */
380static inline int range_decode_bits(APEContext *ctx, int n)
381{
382    int sym = range_decode_culshift(ctx, n);
383    range_decode_update(ctx, 1, sym);
384    return sym;
385}
386
387
388#define MODEL_ELEMENTS 64
389
390/**
391 * Fixed probabilities for symbols in Monkey Audio version 3.97
392 */
393static const uint16_t counts_3970[22] = {
394        0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
395    62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
396    65450, 65469, 65480, 65487, 65491, 65493,
397};
398
399/**
400 * Probability ranges for symbols in Monkey Audio version 3.97
401 */
402static const uint16_t counts_diff_3970[21] = {
403    14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
404    1104, 677, 415, 248, 150, 89, 54, 31,
405    19, 11, 7, 4, 2,
406};
407
408/**
409 * Fixed probabilities for symbols in Monkey Audio version 3.98
410 */
411static const uint16_t counts_3980[22] = {
412        0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
413    64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
414    65485, 65488, 65490, 65491, 65492, 65493,
415};
416
417/**
418 * Probability ranges for symbols in Monkey Audio version 3.98
419 */
420static const uint16_t counts_diff_3980[21] = {
421    19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
422    261, 119, 65, 31, 19, 10, 6, 3,
423    3, 2, 1, 1, 1,
424};
425
426/**
427 * Decode symbol
428 * @param ctx decoder context
429 * @param counts probability range start position
430 * @param counts_diff probability range widths
431 */
432static inline int range_get_symbol(APEContext *ctx,
433                                   const uint16_t counts[],
434                                   const uint16_t counts_diff[])
435{
436    int symbol, cf;
437
438    cf = range_decode_culshift(ctx, 16);
439
440    if(cf > 65492){
441        symbol= cf - 65535 + 63;
442        range_decode_update(ctx, 1, cf);
443        if(cf > 65535)
444            ctx->error=1;
445        return symbol;
446    }
447    /* figure out the symbol inefficiently; a binary search would be much better */
448    for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
449
450    range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
451
452    return symbol;
453}
454/** @} */ // group rangecoder
455
456static inline void update_rice(APERice *rice, unsigned int x)
457{
458    int lim = rice->k ? (1 << (rice->k + 4)) : 0;
459    rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
460
461    if (rice->ksum < lim)
462        rice->k--;
463    else if (rice->ksum >= (1 << (rice->k + 5)))
464        rice->k++;
465}
466
467static inline int get_rice_ook(GetBitContext *gb, int k)
468{
469    unsigned int x;
470
471    x = get_unary(gb, 1, get_bits_left(gb));
472
473    if (k)
474        x = (x << k) | get_bits(gb, k);
475
476    return x;
477}
478
479static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb,
480                                        APERice *rice)
481{
482    unsigned int x, overflow;
483
484    overflow = get_unary(gb, 1, get_bits_left(gb));
485
486    if (ctx->fileversion > 3880) {
487        while (overflow >= 16) {
488            overflow -= 16;
489            rice->k  += 4;
490        }
491    }
492
493    if (!rice->k)
494        x = overflow;
495    else if(rice->k <= MIN_CACHE_BITS) {
496        x = (overflow << rice->k) + get_bits(gb, rice->k);
497    } else {
498        av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", rice->k);
499        return AVERROR_INVALIDDATA;
500    }
501    rice->ksum += x - (rice->ksum + 8 >> 4);
502    if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
503        rice->k--;
504    else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
505        rice->k++;
506
507    /* Convert to signed */
508    if (x & 1)
509        return (x >> 1) + 1;
510    else
511        return -(x >> 1);
512}
513
514static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
515{
516    unsigned int x, overflow;
517    int tmpk;
518
519    overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
520
521    if (overflow == (MODEL_ELEMENTS - 1)) {
522        tmpk = range_decode_bits(ctx, 5);
523        overflow = 0;
524    } else
525        tmpk = (rice->k < 1) ? 0 : rice->k - 1;
526
527    if (tmpk <= 16 || ctx->fileversion < 3910) {
528        if (tmpk > 23) {
529            av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
530            return AVERROR_INVALIDDATA;
531        }
532        x = range_decode_bits(ctx, tmpk);
533    } else if (tmpk <= 31) {
534        x = range_decode_bits(ctx, 16);
535        x |= (range_decode_bits(ctx, tmpk - 16) << 16);
536    } else {
537        av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
538        return AVERROR_INVALIDDATA;
539    }
540    x += overflow << tmpk;
541
542    update_rice(rice, x);
543
544    /* Convert to signed */
545    if (x & 1)
546        return (x >> 1) + 1;
547    else
548        return -(x >> 1);
549}
550
551static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
552{
553    unsigned int x, overflow;
554    int base, pivot;
555
556    pivot = rice->ksum >> 5;
557    if (pivot == 0)
558        pivot = 1;
559
560    overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
561
562    if (overflow == (MODEL_ELEMENTS - 1)) {
563        overflow  = range_decode_bits(ctx, 16) << 16;
564        overflow |= range_decode_bits(ctx, 16);
565    }
566
567    if (pivot < 0x10000) {
568        base = range_decode_culfreq(ctx, pivot);
569        range_decode_update(ctx, 1, base);
570    } else {
571        int base_hi = pivot, base_lo;
572        int bbits = 0;
573
574        while (base_hi & ~0xFFFF) {
575            base_hi >>= 1;
576            bbits++;
577        }
578        base_hi = range_decode_culfreq(ctx, base_hi + 1);
579        range_decode_update(ctx, 1, base_hi);
580        base_lo = range_decode_culfreq(ctx, 1 << bbits);
581        range_decode_update(ctx, 1, base_lo);
582
583        base = (base_hi << bbits) + base_lo;
584    }
585
586    x = base + overflow * pivot;
587
588    update_rice(rice, x);
589
590    /* Convert to signed */
591    if (x & 1)
592        return (x >> 1) + 1;
593    else
594        return -(x >> 1);
595}
596
597static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
598                              int32_t *out, APERice *rice, int blockstodecode)
599{
600    int i;
601    int ksummax, ksummin;
602
603    rice->ksum = 0;
604    for (i = 0; i < 5; i++) {
605        out[i] = get_rice_ook(&ctx->gb, 10);
606        rice->ksum += out[i];
607    }
608    rice->k = av_log2(rice->ksum / 10) + 1;
609    if (rice->k >= 24)
610        return;
611    for (; i < 64; i++) {
612        out[i] = get_rice_ook(&ctx->gb, rice->k);
613        rice->ksum += out[i];
614        rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1;
615        if (rice->k >= 24)
616            return;
617    }
618    ksummax = 1 << rice->k + 7;
619    ksummin = rice->k ? (1 << rice->k + 6) : 0;
620    for (; i < blockstodecode; i++) {
621        out[i] = get_rice_ook(&ctx->gb, rice->k);
622        rice->ksum += out[i] - out[i - 64];
623        while (rice->ksum < ksummin) {
624            rice->k--;
625            ksummin = rice->k ? ksummin >> 1 : 0;
626            ksummax >>= 1;
627        }
628        while (rice->ksum >= ksummax) {
629            rice->k++;
630            if (rice->k > 24)
631                return;
632            ksummax <<= 1;
633            ksummin = ksummin ? ksummin << 1 : 128;
634        }
635    }
636
637    for (i = 0; i < blockstodecode; i++) {
638        if (out[i] & 1)
639            out[i] = (out[i] >> 1) + 1;
640        else
641            out[i] = -(out[i] >> 1);
642    }
643}
644
645static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
646{
647    decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
648                      blockstodecode);
649}
650
651static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
652{
653    decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
654                      blockstodecode);
655    decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
656                      blockstodecode);
657}
658
659static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
660{
661    int32_t *decoded0 = ctx->decoded[0];
662
663    while (blockstodecode--)
664        *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
665}
666
667static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
668{
669    int32_t *decoded0 = ctx->decoded[0];
670    int32_t *decoded1 = ctx->decoded[1];
671    int blocks = blockstodecode;
672
673    while (blockstodecode--)
674        *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
675    while (blocks--)
676        *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
677}
678
679static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
680{
681    int32_t *decoded0 = ctx->decoded[0];
682
683    while (blockstodecode--)
684        *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
685}
686
687static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
688{
689    int32_t *decoded0 = ctx->decoded[0];
690    int32_t *decoded1 = ctx->decoded[1];
691    int blocks = blockstodecode;
692
693    while (blockstodecode--)
694        *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
695    range_dec_normalize(ctx);
696    // because of some implementation peculiarities we need to backpedal here
697    ctx->ptr -= 1;
698    range_start_decoding(ctx);
699    while (blocks--)
700        *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
701}
702
703static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
704{
705    int32_t *decoded0 = ctx->decoded[0];
706    int32_t *decoded1 = ctx->decoded[1];
707
708    while (blockstodecode--) {
709        *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
710        *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
711    }
712}
713
714static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
715{
716    int32_t *decoded0 = ctx->decoded[0];
717
718    while (blockstodecode--)
719        *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
720}
721
722static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
723{
724    int32_t *decoded0 = ctx->decoded[0];
725    int32_t *decoded1 = ctx->decoded[1];
726
727    while (blockstodecode--) {
728        *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
729        *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
730    }
731}
732
733static int init_entropy_decoder(APEContext *ctx)
734{
735    /* Read the CRC */
736    if (ctx->fileversion >= 3900) {
737        if (ctx->data_end - ctx->ptr < 6)
738            return AVERROR_INVALIDDATA;
739        ctx->CRC = bytestream_get_be32(&ctx->ptr);
740    } else {
741        ctx->CRC = get_bits_long(&ctx->gb, 32);
742    }
743
744    /* Read the frame flags if they exist */
745    ctx->frameflags = 0;
746    if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
747        ctx->CRC &= ~0x80000000;
748
749        if (ctx->data_end - ctx->ptr < 6)
750            return AVERROR_INVALIDDATA;
751        ctx->frameflags = bytestream_get_be32(&ctx->ptr);
752    }
753
754    /* Initialize the rice structs */
755    ctx->riceX.k = 10;
756    ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
757    ctx->riceY.k = 10;
758    ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
759
760    if (ctx->fileversion >= 3900) {
761        /* The first 8 bits of input are ignored. */
762        ctx->ptr++;
763
764        range_start_decoding(ctx);
765    }
766
767    return 0;
768}
769
770static const int32_t initial_coeffs_fast_3320[1] = {
771    375,
772};
773
774static const int32_t initial_coeffs_a_3800[3] = {
775    64, 115, 64,
776};
777
778static const int32_t initial_coeffs_b_3800[2] = {
779    740, 0
780};
781
782static const int32_t initial_coeffs_3930[4] = {
783    360, 317, -109, 98
784};
785
786static void init_predictor_decoder(APEContext *ctx)
787{
788    APEPredictor *p = &ctx->predictor;
789
790    /* Zero the history buffers */
791    memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
792    p->buf = p->historybuffer;
793
794    /* Initialize and zero the coefficients */
795    if (ctx->fileversion < 3930) {
796        if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
797            memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
798                   sizeof(initial_coeffs_fast_3320));
799            memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
800                   sizeof(initial_coeffs_fast_3320));
801        } else {
802            memcpy(p->coeffsA[0], initial_coeffs_a_3800,
803                   sizeof(initial_coeffs_a_3800));
804            memcpy(p->coeffsA[1], initial_coeffs_a_3800,
805                   sizeof(initial_coeffs_a_3800));
806        }
807    } else {
808        memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
809        memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
810    }
811    memset(p->coeffsB, 0, sizeof(p->coeffsB));
812    if (ctx->fileversion < 3930) {
813        memcpy(p->coeffsB[0], initial_coeffs_b_3800,
814               sizeof(initial_coeffs_b_3800));
815        memcpy(p->coeffsB[1], initial_coeffs_b_3800,
816               sizeof(initial_coeffs_b_3800));
817    }
818
819    p->filterA[0] = p->filterA[1] = 0;
820    p->filterB[0] = p->filterB[1] = 0;
821    p->lastA[0]   = p->lastA[1]   = 0;
822
823    p->sample_pos = 0;
824}
825
826/** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
827static inline int APESIGN(int32_t x) {
828    return (x < 0) - (x > 0);
829}
830
831static av_always_inline int filter_fast_3320(APEPredictor *p,
832                                             const int decoded, const int filter,
833                                             const int delayA)
834{
835    int32_t predictionA;
836
837    p->buf[delayA] = p->lastA[filter];
838    if (p->sample_pos < 3) {
839        p->lastA[filter]   = decoded;
840        p->filterA[filter] = decoded;
841        return decoded;
842    }
843
844    predictionA = p->buf[delayA] * 2 - p->buf[delayA - 1];
845    p->lastA[filter] = decoded + (predictionA  * p->coeffsA[filter][0] >> 9);
846
847    if ((decoded ^ predictionA) > 0)
848        p->coeffsA[filter][0]++;
849    else
850        p->coeffsA[filter][0]--;
851
852    p->filterA[filter] += p->lastA[filter];
853
854    return p->filterA[filter];
855}
856
857static av_always_inline int filter_3800(APEPredictor *p,
858                                        const int decoded, const int filter,
859                                        const int delayA,  const int delayB,
860                                        const int start,   const int shift)
861{
862    int32_t predictionA, predictionB, sign;
863    int32_t d0, d1, d2, d3, d4;
864
865    p->buf[delayA] = p->lastA[filter];
866    p->buf[delayB] = p->filterB[filter];
867    if (p->sample_pos < start) {
868        predictionA = decoded + p->filterA[filter];
869        p->lastA[filter]   = decoded;
870        p->filterB[filter] = decoded;
871        p->filterA[filter] = predictionA;
872        return predictionA;
873    }
874    d2 =  p->buf[delayA];
875    d1 = (p->buf[delayA] - p->buf[delayA - 1]) << 1;
876    d0 =  p->buf[delayA] + ((p->buf[delayA - 2] - p->buf[delayA - 1]) << 3);
877    d3 =  p->buf[delayB] * 2 - p->buf[delayB - 1];
878    d4 =  p->buf[delayB];
879
880    predictionA = d0 * p->coeffsA[filter][0] +
881                  d1 * p->coeffsA[filter][1] +
882                  d2 * p->coeffsA[filter][2];
883
884    sign = APESIGN(decoded);
885    p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
886    p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
887    p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
888
889    predictionB = d3 * p->coeffsB[filter][0] -
890                  d4 * p->coeffsB[filter][1];
891    p->lastA[filter] = decoded + (predictionA >> 11);
892    sign = APESIGN(p->lastA[filter]);
893    p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
894    p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
895
896    p->filterB[filter] = p->lastA[filter] + (predictionB >> shift);
897    p->filterA[filter] = p->filterB[filter] + ((p->filterA[filter] * 31) >> 5);
898
899    return p->filterA[filter];
900}
901
902static void long_filter_high_3800(int32_t *buffer, int order, int shift,
903                                  int32_t *coeffs, int32_t *delay, int length)
904{
905    int i, j;
906    int32_t dotprod, sign;
907
908    memset(coeffs, 0, order * sizeof(*coeffs));
909    for (i = 0; i < order; i++)
910        delay[i] = buffer[i];
911    for (i = order; i < length; i++) {
912        dotprod = 0;
913        sign = APESIGN(buffer[i]);
914        for (j = 0; j < order; j++) {
915            dotprod += delay[j] * coeffs[j];
916            coeffs[j] += ((delay[j] >> 31) | 1) * sign;
917        }
918        buffer[i] -= dotprod >> shift;
919        for (j = 0; j < order - 1; j++)
920            delay[j] = delay[j + 1];
921        delay[order - 1] = buffer[i];
922    }
923}
924
925static void long_filter_ehigh_3830(int32_t *buffer, int length)
926{
927    int i, j;
928    int32_t dotprod, sign;
929    int32_t coeffs[8] = { 0 }, delay[8] = { 0 };
930
931    for (i = 0; i < length; i++) {
932        dotprod = 0;
933        sign = APESIGN(buffer[i]);
934        for (j = 7; j >= 0; j--) {
935            dotprod += delay[j] * coeffs[j];
936            coeffs[j] += ((delay[j] >> 31) | 1) * sign;
937        }
938        for (j = 7; j > 0; j--)
939            delay[j] = delay[j - 1];
940        delay[0] = buffer[i];
941        buffer[i] -= dotprod >> 9;
942    }
943}
944
945static void predictor_decode_stereo_3800(APEContext *ctx, int count)
946{
947    APEPredictor *p = &ctx->predictor;
948    int32_t *decoded0 = ctx->decoded[0];
949    int32_t *decoded1 = ctx->decoded[1];
950    int32_t coeffs[256], delay[256];
951    int start = 4, shift = 10;
952
953    if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
954        start = 16;
955        long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
956        long_filter_high_3800(decoded1, 16, 9, coeffs, delay, count);
957    } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
958        int order = 128, shift2 = 11;
959
960        if (ctx->fileversion >= 3830) {
961            order <<= 1;
962            shift++;
963            shift2++;
964            long_filter_ehigh_3830(decoded0 + order, count - order);
965            long_filter_ehigh_3830(decoded1 + order, count - order);
966        }
967        start = order;
968        long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
969        long_filter_high_3800(decoded1, order, shift2, coeffs, delay, count);
970    }
971
972    while (count--) {
973        int X = *decoded0, Y = *decoded1;
974        if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
975            *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
976            decoded0++;
977            *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
978            decoded1++;
979        } else {
980            *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
981                                    start, shift);
982            decoded0++;
983            *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
984                                    start, shift);
985            decoded1++;
986        }
987
988        /* Combined */
989        p->buf++;
990        p->sample_pos++;
991
992        /* Have we filled the history buffer? */
993        if (p->buf == p->historybuffer + HISTORY_SIZE) {
994            memmove(p->historybuffer, p->buf,
995                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
996            p->buf = p->historybuffer;
997        }
998    }
999}
1000
1001static void predictor_decode_mono_3800(APEContext *ctx, int count)
1002{
1003    APEPredictor *p = &ctx->predictor;
1004    int32_t *decoded0 = ctx->decoded[0];
1005    int32_t coeffs[256], delay[256];
1006    int start = 4, shift = 10;
1007
1008    if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
1009        start = 16;
1010        long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
1011    } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1012        int order = 128, shift2 = 11;
1013
1014        if (ctx->fileversion >= 3830) {
1015            order <<= 1;
1016            shift++;
1017            shift2++;
1018            long_filter_ehigh_3830(decoded0 + order, count - order);
1019        }
1020        start = order;
1021        long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
1022    }
1023
1024    while (count--) {
1025        if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1026            *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
1027            decoded0++;
1028        } else {
1029            *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
1030                                    start, shift);
1031            decoded0++;
1032        }
1033
1034        /* Combined */
1035        p->buf++;
1036        p->sample_pos++;
1037
1038        /* Have we filled the history buffer? */
1039        if (p->buf == p->historybuffer + HISTORY_SIZE) {
1040            memmove(p->historybuffer, p->buf,
1041                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
1042            p->buf = p->historybuffer;
1043        }
1044    }
1045}
1046
1047static av_always_inline int predictor_update_3930(APEPredictor *p,
1048                                                  const int decoded, const int filter,
1049                                                  const int delayA)
1050{
1051    int32_t predictionA, sign;
1052    int32_t d0, d1, d2, d3;
1053
1054    p->buf[delayA]     = p->lastA[filter];
1055    d0 = p->buf[delayA    ];
1056    d1 = p->buf[delayA    ] - p->buf[delayA - 1];
1057    d2 = p->buf[delayA - 1] - p->buf[delayA - 2];
1058    d3 = p->buf[delayA - 2] - p->buf[delayA - 3];
1059
1060    predictionA = d0 * p->coeffsA[filter][0] +
1061                  d1 * p->coeffsA[filter][1] +
1062                  d2 * p->coeffsA[filter][2] +
1063                  d3 * p->coeffsA[filter][3];
1064
1065    p->lastA[filter] = decoded + (predictionA >> 9);
1066    p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
1067
1068    sign = APESIGN(decoded);
1069    p->coeffsA[filter][0] += ((d0 < 0) * 2 - 1) * sign;
1070    p->coeffsA[filter][1] += ((d1 < 0) * 2 - 1) * sign;
1071    p->coeffsA[filter][2] += ((d2 < 0) * 2 - 1) * sign;
1072    p->coeffsA[filter][3] += ((d3 < 0) * 2 - 1) * sign;
1073
1074    return p->filterA[filter];
1075}
1076
1077static void predictor_decode_stereo_3930(APEContext *ctx, int count)
1078{
1079    APEPredictor *p = &ctx->predictor;
1080    int32_t *decoded0 = ctx->decoded[0];
1081    int32_t *decoded1 = ctx->decoded[1];
1082
1083    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1084
1085    while (count--) {
1086        /* Predictor Y */
1087        int Y = *decoded1, X = *decoded0;
1088        *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
1089        decoded0++;
1090        *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
1091        decoded1++;
1092
1093        /* Combined */
1094        p->buf++;
1095
1096        /* Have we filled the history buffer? */
1097        if (p->buf == p->historybuffer + HISTORY_SIZE) {
1098            memmove(p->historybuffer, p->buf,
1099                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
1100            p->buf = p->historybuffer;
1101        }
1102    }
1103}
1104
1105static void predictor_decode_mono_3930(APEContext *ctx, int count)
1106{
1107    APEPredictor *p = &ctx->predictor;
1108    int32_t *decoded0 = ctx->decoded[0];
1109
1110    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1111
1112    while (count--) {
1113        *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
1114        decoded0++;
1115
1116        p->buf++;
1117
1118        /* Have we filled the history buffer? */
1119        if (p->buf == p->historybuffer + HISTORY_SIZE) {
1120            memmove(p->historybuffer, p->buf,
1121                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
1122            p->buf = p->historybuffer;
1123        }
1124    }
1125}
1126
1127static av_always_inline int predictor_update_filter(APEPredictor *p,
1128                                                    const int decoded, const int filter,
1129                                                    const int delayA,  const int delayB,
1130                                                    const int adaptA,  const int adaptB)
1131{
1132    int32_t predictionA, predictionB, sign;
1133
1134    p->buf[delayA]     = p->lastA[filter];
1135    p->buf[adaptA]     = APESIGN(p->buf[delayA]);
1136    p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
1137    p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1138
1139    predictionA = p->buf[delayA    ] * p->coeffsA[filter][0] +
1140                  p->buf[delayA - 1] * p->coeffsA[filter][1] +
1141                  p->buf[delayA - 2] * p->coeffsA[filter][2] +
1142                  p->buf[delayA - 3] * p->coeffsA[filter][3];
1143
1144    /*  Apply a scaled first-order filter compression */
1145    p->buf[delayB]     = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
1146    p->buf[adaptB]     = APESIGN(p->buf[delayB]);
1147    p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
1148    p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1149    p->filterB[filter] = p->filterA[filter ^ 1];
1150
1151    predictionB = p->buf[delayB    ] * p->coeffsB[filter][0] +
1152                  p->buf[delayB - 1] * p->coeffsB[filter][1] +
1153                  p->buf[delayB - 2] * p->coeffsB[filter][2] +
1154                  p->buf[delayB - 3] * p->coeffsB[filter][3] +
1155                  p->buf[delayB - 4] * p->coeffsB[filter][4];
1156
1157    p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1158    p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
1159
1160    sign = APESIGN(decoded);
1161    p->coeffsA[filter][0] += p->buf[adaptA    ] * sign;
1162    p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
1163    p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
1164    p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
1165    p->coeffsB[filter][0] += p->buf[adaptB    ] * sign;
1166    p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
1167    p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
1168    p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
1169    p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
1170
1171    return p->filterA[filter];
1172}
1173
1174static void predictor_decode_stereo_3950(APEContext *ctx, int count)
1175{
1176    APEPredictor *p = &ctx->predictor;
1177    int32_t *decoded0 = ctx->decoded[0];
1178    int32_t *decoded1 = ctx->decoded[1];
1179
1180    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1181
1182    while (count--) {
1183        /* Predictor Y */
1184        *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1185                                            YADAPTCOEFFSA, YADAPTCOEFFSB);
1186        decoded0++;
1187        *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1188                                            XADAPTCOEFFSA, XADAPTCOEFFSB);
1189        decoded1++;
1190
1191        /* Combined */
1192        p->buf++;
1193
1194        /* Have we filled the history buffer? */
1195        if (p->buf == p->historybuffer + HISTORY_SIZE) {
1196            memmove(p->historybuffer, p->buf,
1197                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
1198            p->buf = p->historybuffer;
1199        }
1200    }
1201}
1202
1203static void predictor_decode_mono_3950(APEContext *ctx, int count)
1204{
1205    APEPredictor *p = &ctx->predictor;
1206    int32_t *decoded0 = ctx->decoded[0];
1207    int32_t predictionA, currentA, A, sign;
1208
1209    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1210
1211    currentA = p->lastA[0];
1212
1213    while (count--) {
1214        A = *decoded0;
1215
1216        p->buf[YDELAYA] = currentA;
1217        p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
1218
1219        predictionA = p->buf[YDELAYA    ] * p->coeffsA[0][0] +
1220                      p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
1221                      p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
1222                      p->buf[YDELAYA - 3] * p->coeffsA[0][3];
1223
1224        currentA = A + (predictionA >> 10);
1225
1226        p->buf[YADAPTCOEFFSA]     = APESIGN(p->buf[YDELAYA    ]);
1227        p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
1228
1229        sign = APESIGN(A);
1230        p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA    ] * sign;
1231        p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
1232        p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
1233        p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
1234
1235        p->buf++;
1236
1237        /* Have we filled the history buffer? */
1238        if (p->buf == p->historybuffer + HISTORY_SIZE) {
1239            memmove(p->historybuffer, p->buf,
1240                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
1241            p->buf = p->historybuffer;
1242        }
1243
1244        p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
1245        *(decoded0++) = p->filterA[0];
1246    }
1247
1248    p->lastA[0] = currentA;
1249}
1250
1251static void do_init_filter(APEFilter *f, int16_t *buf, int order)
1252{
1253    f->coeffs = buf;
1254    f->historybuffer = buf + order;
1255    f->delay       = f->historybuffer + order * 2;
1256    f->adaptcoeffs = f->historybuffer + order;
1257
1258    memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
1259    memset(f->coeffs, 0, order * sizeof(*f->coeffs));
1260    f->avg = 0;
1261}
1262
1263static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
1264{
1265    do_init_filter(&f[0], buf, order);
1266    do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1267}
1268
1269static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
1270                            int32_t *data, int count, int order, int fracbits)
1271{
1272    int res;
1273    int absres;
1274
1275    while (count--) {
1276        /* round fixedpoint scalar product */
1277        res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs,
1278                                                     f->delay - order,
1279                                                     f->adaptcoeffs - order,
1280                                                     order, APESIGN(*data));
1281        res = (res + (1 << (fracbits - 1))) >> fracbits;
1282        res += *data;
1283        *data++ = res;
1284
1285        /* Update the output history */
1286        *f->delay++ = av_clip_int16(res);
1287
1288        if (version < 3980) {
1289            /* Version ??? to < 3.98 files (untested) */
1290            f->adaptcoeffs[0]  = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1291            f->adaptcoeffs[-4] >>= 1;
1292            f->adaptcoeffs[-8] >>= 1;
1293        } else {
1294            /* Version 3.98 and later files */
1295
1296            /* Update the adaption coefficients */
1297            absres = FFABS(res);
1298            if (absres)
1299                *f->adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1300                                  (25 + (absres <= f->avg*3) + (absres <= f->avg*4/3));
1301            else
1302                *f->adaptcoeffs = 0;
1303
1304            f->avg += (absres - f->avg) / 16;
1305
1306            f->adaptcoeffs[-1] >>= 1;
1307            f->adaptcoeffs[-2] >>= 1;
1308            f->adaptcoeffs[-8] >>= 1;
1309        }
1310
1311        f->adaptcoeffs++;
1312
1313        /* Have we filled the history buffer? */
1314        if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1315            memmove(f->historybuffer, f->delay - (order * 2),
1316                    (order * 2) * sizeof(*f->historybuffer));
1317            f->delay = f->historybuffer + order * 2;
1318            f->adaptcoeffs = f->historybuffer + order;
1319        }
1320    }
1321}
1322
1323static void apply_filter(APEContext *ctx, APEFilter *f,
1324                         int32_t *data0, int32_t *data1,
1325                         int count, int order, int fracbits)
1326{
1327    do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1328    if (data1)
1329        do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1330}
1331
1332static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
1333                              int32_t *decoded1, int count)
1334{
1335    int i;
1336
1337    for (i = 0; i < APE_FILTER_LEVELS; i++) {
1338        if (!ape_filter_orders[ctx->fset][i])
1339            break;
1340        apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
1341                     ape_filter_orders[ctx->fset][i],
1342                     ape_filter_fracbits[ctx->fset][i]);
1343    }
1344}
1345
1346static int init_frame_decoder(APEContext *ctx)
1347{
1348    int i, ret;
1349    if ((ret = init_entropy_decoder(ctx)) < 0)
1350        return ret;
1351    init_predictor_decoder(ctx);
1352
1353    for (i = 0; i < APE_FILTER_LEVELS; i++) {
1354        if (!ape_filter_orders[ctx->fset][i])
1355            break;
1356        init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
1357                    ape_filter_orders[ctx->fset][i]);
1358    }
1359    return 0;
1360}
1361
1362static void ape_unpack_mono(APEContext *ctx, int count)
1363{
1364    if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1365        /* We are pure silence, so we're done. */
1366        av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
1367        return;
1368    }
1369
1370    ctx->entropy_decode_mono(ctx, count);
1371
1372    /* Now apply the predictor decoding */
1373    ctx->predictor_decode_mono(ctx, count);
1374
1375    /* Pseudo-stereo - just copy left channel to right channel */
1376    if (ctx->channels == 2) {
1377        memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1]));
1378    }
1379}
1380
1381static void ape_unpack_stereo(APEContext *ctx, int count)
1382{
1383    int32_t left, right;
1384    int32_t *decoded0 = ctx->decoded[0];
1385    int32_t *decoded1 = ctx->decoded[1];
1386
1387    if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1388        /* We are pure silence, so we're done. */
1389        av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
1390        return;
1391    }
1392
1393    ctx->entropy_decode_stereo(ctx, count);
1394
1395    /* Now apply the predictor decoding */
1396    ctx->predictor_decode_stereo(ctx, count);
1397
1398    /* Decorrelate and scale to output depth */
1399    while (count--) {
1400        left = *decoded1 - (*decoded0 / 2);
1401        right = left + *decoded0;
1402
1403        *(decoded0++) = left;
1404        *(decoded1++) = right;
1405    }
1406}
1407
1408static int ape_decode_frame(AVCodecContext *avctx, void *data,
1409                            int *got_frame_ptr, AVPacket *avpkt)
1410{
1411    AVFrame *frame     = data;
1412    const uint8_t *buf = avpkt->data;
1413    APEContext *s = avctx->priv_data;
1414    uint8_t *sample8;
1415    int16_t *sample16;
1416    int32_t *sample24;
1417    int i, ch, ret;
1418    int blockstodecode;
1419
1420    /* this should never be negative, but bad things will happen if it is, so
1421       check it just to make sure. */
1422    av_assert0(s->samples >= 0);
1423
1424    if(!s->samples){
1425        uint32_t nblocks, offset;
1426        int buf_size;
1427
1428        if (!avpkt->size) {
1429            *got_frame_ptr = 0;
1430            return 0;
1431        }
1432        if (avpkt->size < 8) {
1433            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1434            return AVERROR_INVALIDDATA;
1435        }
1436        buf_size = avpkt->size & ~3;
1437        if (buf_size != avpkt->size) {
1438            av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
1439                   "extra bytes at the end will be skipped.\n");
1440        }
1441        if (s->fileversion < 3950) // previous versions overread two bytes
1442            buf_size += 2;
1443        av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
1444        if (!s->data)
1445            return AVERROR(ENOMEM);
1446        s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
1447                          buf_size >> 2);
1448        memset(s->data + (buf_size & ~3), 0, buf_size & 3);
1449        s->ptr = s->data;
1450        s->data_end = s->data + buf_size;
1451
1452        nblocks = bytestream_get_be32(&s->ptr);
1453        offset  = bytestream_get_be32(&s->ptr);
1454        if (s->fileversion >= 3900) {
1455            if (offset > 3) {
1456                av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
1457                s->data = NULL;
1458                return AVERROR_INVALIDDATA;
1459            }
1460            if (s->data_end - s->ptr < offset) {
1461                av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1462                return AVERROR_INVALIDDATA;
1463            }
1464            s->ptr += offset;
1465        } else {
1466            if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
1467                return ret;
1468            if (s->fileversion > 3800)
1469                skip_bits_long(&s->gb, offset * 8);
1470            else
1471                skip_bits_long(&s->gb, offset);
1472        }
1473
1474        if (!nblocks || nblocks > INT_MAX) {
1475            av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n",
1476                   nblocks);
1477            return AVERROR_INVALIDDATA;
1478        }
1479        s->samples = nblocks;
1480
1481        /* Initialize the frame decoder */
1482        if (init_frame_decoder(s) < 0) {
1483            av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
1484            return AVERROR_INVALIDDATA;
1485        }
1486    }
1487
1488    if (!s->data) {
1489        *got_frame_ptr = 0;
1490        return avpkt->size;
1491    }
1492
1493    blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
1494    // for old files coefficients were not interleaved,
1495    // so we need to decode all of them at once
1496    if (s->fileversion < 3930)
1497        blockstodecode = s->samples;
1498
1499    /* reallocate decoded sample buffer if needed */
1500    av_fast_malloc(&s->decoded_buffer, &s->decoded_size,
1501                   2 * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer));
1502    if (!s->decoded_buffer)
1503        return AVERROR(ENOMEM);
1504    memset(s->decoded_buffer, 0, s->decoded_size);
1505    s->decoded[0] = s->decoded_buffer;
1506    s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1507
1508    /* get output buffer */
1509    frame->nb_samples = blockstodecode;
1510    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1511        return ret;
1512
1513    s->error=0;
1514
1515    if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1516        ape_unpack_mono(s, blockstodecode);
1517    else
1518        ape_unpack_stereo(s, blockstodecode);
1519    emms_c();
1520
1521    if (s->error) {
1522        s->samples=0;
1523        av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
1524        return AVERROR_INVALIDDATA;
1525    }
1526
1527    switch (s->bps) {
1528    case 8:
1529        for (ch = 0; ch < s->channels; ch++) {
1530            sample8 = (uint8_t *)frame->data[ch];
1531            for (i = 0; i < blockstodecode; i++)
1532                *sample8++ = (s->decoded[ch][i] + 0x80) & 0xff;
1533        }
1534        break;
1535    case 16:
1536        for (ch = 0; ch < s->channels; ch++) {
1537            sample16 = (int16_t *)frame->data[ch];
1538            for (i = 0; i < blockstodecode; i++)
1539                *sample16++ = s->decoded[ch][i];
1540        }
1541        break;
1542    case 24:
1543        for (ch = 0; ch < s->channels; ch++) {
1544            sample24 = (int32_t *)frame->data[ch];
1545            for (i = 0; i < blockstodecode; i++)
1546                *sample24++ = s->decoded[ch][i] << 8;
1547        }
1548        break;
1549    }
1550
1551    s->samples -= blockstodecode;
1552
1553    *got_frame_ptr = 1;
1554
1555    return !s->samples ? avpkt->size : 0;
1556}
1557
1558static void ape_flush(AVCodecContext *avctx)
1559{
1560    APEContext *s = avctx->priv_data;
1561    s->samples= 0;
1562}
1563
1564#define OFFSET(x) offsetof(APEContext, x)
1565#define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1566static const AVOption options[] = {
1567    { "max_samples", "maximum number of samples decoded per call",             OFFSET(blocks_per_loop), AV_OPT_TYPE_INT,   { .i64 = 4608 },    1,       INT_MAX, PAR, "max_samples" },
1568    { "all",         "no maximum. decode all samples for each packet at once", 0,                       AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX, PAR, "max_samples" },
1569    { NULL},
1570};
1571
1572static const AVClass ape_decoder_class = {
1573    .class_name = "APE decoder",
1574    .item_name  = av_default_item_name,
1575    .option     = options,
1576    .version    = LIBAVUTIL_VERSION_INT,
1577};
1578
1579AVCodec ff_ape_decoder = {
1580    .name           = "ape",
1581    .long_name      = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
1582    .type           = AVMEDIA_TYPE_AUDIO,
1583    .id             = AV_CODEC_ID_APE,
1584    .priv_data_size = sizeof(APEContext),
1585    .init           = ape_decode_init,
1586    .close          = ape_decode_close,
1587    .decode         = ape_decode_frame,
1588    .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
1589    .flush          = ape_flush,
1590    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
1591                                                      AV_SAMPLE_FMT_S16P,
1592                                                      AV_SAMPLE_FMT_S32P,
1593                                                      AV_SAMPLE_FMT_NONE },
1594    .priv_class     = &ape_decoder_class,
1595};
1596