LCOV - code coverage report
Current view: top level - src/libavcodec - apedec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 556 756 73.5 %
Date: 2017-01-24 04:42:20 Functions: 39 49 79.6 %

          Line data    Source code
       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             :  */
      67             : enum 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 */
      79             : static 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 */
      88             : static 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 */
      98             : typedef 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             : 
     107             : typedef struct APERice {
     108             :     uint32_t k;
     109             :     uint32_t ksum;
     110             : } APERice;
     111             : 
     112             : typedef 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 */
     120             : typedef 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 */
     136             : typedef 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             : 
     180             : static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
     181             :                               int32_t *decoded1, int count);
     182             : 
     183             : static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
     184             : static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
     185             : static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
     186             : static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
     187             : static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
     188             : static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
     189             : static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
     190             : static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
     191             : static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
     192             : 
     193             : static void predictor_decode_mono_3800(APEContext *ctx, int count);
     194             : static void predictor_decode_stereo_3800(APEContext *ctx, int count);
     195             : static void predictor_decode_mono_3930(APEContext *ctx, int count);
     196             : static void predictor_decode_stereo_3930(APEContext *ctx, int count);
     197             : static void predictor_decode_mono_3950(APEContext *ctx, int count);
     198             : static void predictor_decode_stereo_3950(APEContext *ctx, int count);
     199             : 
     200          27 : static av_cold int ape_decode_close(AVCodecContext *avctx)
     201             : {
     202          27 :     APEContext *s = avctx->priv_data;
     203             :     int i;
     204             : 
     205         108 :     for (i = 0; i < APE_FILTER_LEVELS; i++)
     206          81 :         av_freep(&s->filterbuf[i]);
     207             : 
     208          27 :     av_freep(&s->decoded_buffer);
     209          27 :     av_freep(&s->data);
     210          27 :     s->decoded_size = s->data_size = 0;
     211             : 
     212          27 :     return 0;
     213             : }
     214             : 
     215          27 : static av_cold int ape_decode_init(AVCodecContext *avctx)
     216             : {
     217          27 :     APEContext *s = avctx->priv_data;
     218             :     int i;
     219             : 
     220          27 :     if (avctx->extradata_size != 6) {
     221           0 :         av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
     222           0 :         return AVERROR(EINVAL);
     223             :     }
     224          27 :     if (avctx->channels > 2) {
     225           0 :         av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
     226           0 :         return AVERROR(EINVAL);
     227             :     }
     228          27 :     s->bps = avctx->bits_per_coded_sample;
     229          27 :     switch (s->bps) {
     230             :     case 8:
     231           0 :         avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
     232           0 :         break;
     233             :     case 16:
     234          27 :         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
     235          27 :         break;
     236             :     case 24:
     237           0 :         avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
     238           0 :         break;
     239             :     default:
     240           0 :         avpriv_request_sample(avctx,
     241             :                               "%d bits per coded sample", s->bps);
     242           0 :         return AVERROR_PATCHWELCOME;
     243             :     }
     244          27 :     s->avctx             = avctx;
     245          27 :     s->channels          = avctx->channels;
     246          27 :     s->fileversion       = AV_RL16(avctx->extradata);
     247          27 :     s->compression_level = AV_RL16(avctx->extradata + 2);
     248          27 :     s->flags             = AV_RL16(avctx->extradata + 4);
     249             : 
     250          27 :     av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n",
     251             :            s->compression_level, s->flags);
     252          54 :     if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
     253          54 :         !s->compression_level ||
     254          47 :         (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
     255           0 :         av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
     256             :                s->compression_level);
     257           0 :         return AVERROR_INVALIDDATA;
     258             :     }
     259          27 :     s->fset = s->compression_level / 1000 - 1;
     260          66 :     for (i = 0; i < APE_FILTER_LEVELS; i++) {
     261          66 :         if (!ape_filter_orders[s->fset][i])
     262          27 :             break;
     263          39 :         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          27 :     if (s->fileversion < 3860) {
     269           4 :         s->entropy_decode_mono   = entropy_decode_mono_0000;
     270           4 :         s->entropy_decode_stereo = entropy_decode_stereo_0000;
     271          23 :     } else if (s->fileversion < 3900) {
     272           8 :         s->entropy_decode_mono   = entropy_decode_mono_3860;
     273           8 :         s->entropy_decode_stereo = entropy_decode_stereo_3860;
     274          15 :     } else if (s->fileversion < 3930) {
     275           8 :         s->entropy_decode_mono   = entropy_decode_mono_3900;
     276           8 :         s->entropy_decode_stereo = entropy_decode_stereo_3900;
     277           7 :     } else if (s->fileversion < 3990) {
     278           4 :         s->entropy_decode_mono   = entropy_decode_mono_3900;
     279           4 :         s->entropy_decode_stereo = entropy_decode_stereo_3930;
     280             :     } else {
     281           3 :         s->entropy_decode_mono   = entropy_decode_mono_3990;
     282           3 :         s->entropy_decode_stereo = entropy_decode_stereo_3990;
     283             :     }
     284             : 
     285          27 :     if (s->fileversion < 3930) {
     286          20 :         s->predictor_decode_mono   = predictor_decode_mono_3800;
     287          20 :         s->predictor_decode_stereo = predictor_decode_stereo_3800;
     288           7 :     } else if (s->fileversion < 3950) {
     289           4 :         s->predictor_decode_mono   = predictor_decode_mono_3930;
     290           4 :         s->predictor_decode_stereo = predictor_decode_stereo_3930;
     291             :     } else {
     292           3 :         s->predictor_decode_mono   = predictor_decode_mono_3950;
     293           3 :         s->predictor_decode_stereo = predictor_decode_stereo_3950;
     294             :     }
     295             : 
     296          27 :     ff_bswapdsp_init(&s->bdsp);
     297          27 :     ff_llauddsp_init(&s->adsp);
     298          27 :     avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
     299             : 
     300          27 :     return 0;
     301             : filter_alloc_fail:
     302           0 :     ape_decode_close(avctx);
     303           0 :     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 */
     318          26 : static inline void range_start_decoding(APEContext *ctx)
     319             : {
     320          26 :     ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
     321          26 :     ctx->rc.low    = ctx->rc.buffer >> (8 - EXTRA_BITS);
     322          26 :     ctx->rc.range  = (uint32_t) 1 << EXTRA_BITS;
     323          26 : }
     324             : 
     325             : /** Perform normalization */
     326     5289992 : static inline void range_dec_normalize(APEContext *ctx)
     327             : {
     328    13570600 :     while (ctx->rc.range <= BOTTOM_VALUE) {
     329     2990616 :         ctx->rc.buffer <<= 8;
     330     2990616 :         if(ctx->ptr < ctx->data_end) {
     331     2986366 :             ctx->rc.buffer += *ctx->ptr;
     332     2986366 :             ctx->ptr++;
     333             :         } else {
     334        4250 :             ctx->error = 1;
     335             :         }
     336     2990616 :         ctx->rc.low    = (ctx->rc.low << 8)    | ((ctx->rc.buffer >> 1) & 0xFF);
     337     2990616 :         ctx->rc.range  <<= 8;
     338             :     }
     339     5289992 : }
     340             : 
     341             : /**
     342             :  * Calculate cumulative 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 cumulative frequency
     346             :  */
     347      875520 : static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
     348             : {
     349      875520 :     range_dec_normalize(ctx);
     350      875520 :     ctx->rc.help = ctx->rc.range / tot_f;
     351      875520 :     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             :  */
     359     4414464 : static inline int range_decode_culshift(APEContext *ctx, int shift)
     360             : {
     361     4414464 :     range_dec_normalize(ctx);
     362     4414464 :     ctx->rc.help = ctx->rc.range >> shift;
     363     4414464 :     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             :  */
     373     5289984 : static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
     374             : {
     375     5289984 :     ctx->rc.low  -= ctx->rc.help * lt_f;
     376     5289984 :     ctx->rc.range = ctx->rc.help * sy_f;
     377     5289984 : }
     378             : 
     379             : /** Decode n bits (n <= 16) without modelling */
     380     1769472 : static inline int range_decode_bits(APEContext *ctx, int n)
     381             : {
     382     1769472 :     int sym = range_decode_culshift(ctx, n);
     383     1769472 :     range_decode_update(ctx, 1, sym);
     384     1769472 :     return sym;
     385             : }
     386             : 
     387             : 
     388             : #define MODEL_ELEMENTS 64
     389             : 
     390             : /**
     391             :  * Fixed probabilities for symbols in Monkey Audio version 3.97
     392             :  */
     393             : static 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             :  */
     402             : static 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             :  */
     411             : static 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             :  */
     420             : static 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             :  */
     432     2644992 : static 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     2644992 :     cf = range_decode_culshift(ctx, 16);
     439             : 
     440     2644992 :     if(cf > 65492){
     441         183 :         symbol= cf - 65535 + 63;
     442         183 :         range_decode_update(ctx, 1, cf);
     443         183 :         if(cf > 65535)
     444           1 :             ctx->error=1;
     445         183 :         return symbol;
     446             :     }
     447             :     /* figure out the symbol inefficiently; a binary search would be much better */
     448     2644809 :     for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
     449             : 
     450     2644809 :     range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
     451             : 
     452     2644809 :     return symbol;
     453             : }
     454             : /** @} */ // group rangecoder
     455             : 
     456     2644992 : static inline void update_rice(APERice *rice, unsigned int x)
     457             : {
     458     2644992 :     int lim = rice->k ? (1 << (rice->k + 4)) : 0;
     459     2644992 :     rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
     460             : 
     461     2644992 :     if (rice->ksum < lim)
     462       32925 :         rice->k--;
     463     2612067 :     else if (rice->ksum >= (1 << (rice->k + 5)))
     464       32829 :         rice->k++;
     465     2644992 : }
     466             : 
     467      460800 : static inline int get_rice_ook(GetBitContext *gb, int k)
     468             : {
     469             :     unsigned int x;
     470             : 
     471      460800 :     x = get_unary(gb, 1, get_bits_left(gb));
     472             : 
     473      460800 :     if (k)
     474      460800 :         x = (x << k) | get_bits(gb, k);
     475             : 
     476      460800 :     return x;
     477             : }
     478             : 
     479      921600 : static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb,
     480             :                                         APERice *rice)
     481             : {
     482             :     unsigned int x, overflow;
     483             : 
     484      921600 :     overflow = get_unary(gb, 1, get_bits_left(gb));
     485             : 
     486      921600 :     if (ctx->fileversion > 3880) {
     487      921601 :         while (overflow >= 16) {
     488           1 :             overflow -= 16;
     489           1 :             rice->k  += 4;
     490             :         }
     491             :     }
     492             : 
     493      921600 :     if (!rice->k)
     494           0 :         x = overflow;
     495      921600 :     else if(rice->k <= MIN_CACHE_BITS) {
     496      921600 :         x = (overflow << rice->k) + get_bits(gb, rice->k);
     497             :     } else {
     498           0 :         av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", rice->k);
     499           0 :         return AVERROR_INVALIDDATA;
     500             :     }
     501      921600 :     rice->ksum += x - (rice->ksum + 8 >> 4);
     502      921600 :     if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
     503       23449 :         rice->k--;
     504      898151 :     else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
     505       23287 :         rice->k++;
     506             : 
     507             :     /* Convert to signed */
     508      921600 :     return ((x >> 1) ^ ((x & 1) - 1)) + 1;
     509             : }
     510             : 
     511     1769472 : static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
     512             : {
     513             :     unsigned int x, overflow;
     514             :     int tmpk;
     515             : 
     516     1769472 :     overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
     517             : 
     518     1769472 :     if (overflow == (MODEL_ELEMENTS - 1)) {
     519           0 :         tmpk = range_decode_bits(ctx, 5);
     520           0 :         overflow = 0;
     521             :     } else
     522     1769472 :         tmpk = (rice->k < 1) ? 0 : rice->k - 1;
     523             : 
     524     1769472 :     if (tmpk <= 16 || ctx->fileversion < 3910) {
     525     1769472 :         if (tmpk > 23) {
     526           0 :             av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
     527           0 :             return AVERROR_INVALIDDATA;
     528             :         }
     529     1769472 :         x = range_decode_bits(ctx, tmpk);
     530           0 :     } else if (tmpk <= 31) {
     531           0 :         x = range_decode_bits(ctx, 16);
     532           0 :         x |= (range_decode_bits(ctx, tmpk - 16) << 16);
     533             :     } else {
     534           0 :         av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
     535           0 :         return AVERROR_INVALIDDATA;
     536             :     }
     537     1769472 :     x += overflow << tmpk;
     538             : 
     539     1769472 :     update_rice(rice, x);
     540             : 
     541             :     /* Convert to signed */
     542     1769472 :     return ((x >> 1) ^ ((x & 1) - 1)) + 1;
     543             : }
     544             : 
     545      875520 : static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
     546             : {
     547             :     unsigned int x, overflow;
     548             :     int base, pivot;
     549             : 
     550      875520 :     pivot = rice->ksum >> 5;
     551      875520 :     if (pivot == 0)
     552        3207 :         pivot = 1;
     553             : 
     554      875520 :     overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
     555             : 
     556      875520 :     if (overflow == (MODEL_ELEMENTS - 1)) {
     557           0 :         overflow  = range_decode_bits(ctx, 16) << 16;
     558           0 :         overflow |= range_decode_bits(ctx, 16);
     559             :     }
     560             : 
     561      875520 :     if (pivot < 0x10000) {
     562      875520 :         base = range_decode_culfreq(ctx, pivot);
     563      875520 :         range_decode_update(ctx, 1, base);
     564             :     } else {
     565           0 :         int base_hi = pivot, base_lo;
     566           0 :         int bbits = 0;
     567             : 
     568           0 :         while (base_hi & ~0xFFFF) {
     569           0 :             base_hi >>= 1;
     570           0 :             bbits++;
     571             :         }
     572           0 :         base_hi = range_decode_culfreq(ctx, base_hi + 1);
     573           0 :         range_decode_update(ctx, 1, base_hi);
     574           0 :         base_lo = range_decode_culfreq(ctx, 1 << bbits);
     575           0 :         range_decode_update(ctx, 1, base_lo);
     576             : 
     577           0 :         base = (base_hi << bbits) + base_lo;
     578             :     }
     579             : 
     580      875520 :     x = base + overflow * pivot;
     581             : 
     582      875520 :     update_rice(rice, x);
     583             : 
     584             :     /* Convert to signed */
     585      875520 :     return ((x >> 1) ^ ((x & 1) - 1)) + 1;
     586             : }
     587             : 
     588          22 : static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
     589             :                               int32_t *out, APERice *rice, int blockstodecode)
     590             : {
     591             :     int i;
     592             :     int ksummax, ksummin;
     593             : 
     594          22 :     rice->ksum = 0;
     595         132 :     for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
     596         110 :         out[i] = get_rice_ook(&ctx->gb, 10);
     597         110 :         rice->ksum += out[i];
     598             :     }
     599          22 :     rice->k = av_log2(rice->ksum / 10) + 1;
     600          22 :     if (rice->k >= 24)
     601           0 :         return;
     602        1320 :     for (; i < FFMIN(blockstodecode, 64); i++) {
     603        1298 :         out[i] = get_rice_ook(&ctx->gb, rice->k);
     604        1298 :         rice->ksum += out[i];
     605        1298 :         rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1;
     606        1298 :         if (rice->k >= 24)
     607           0 :             return;
     608             :     }
     609          22 :     ksummax = 1 << rice->k + 7;
     610          22 :     ksummin = rice->k ? (1 << rice->k + 6) : 0;
     611      459414 :     for (; i < blockstodecode; i++) {
     612      459392 :         out[i] = get_rice_ook(&ctx->gb, rice->k);
     613      459392 :         rice->ksum += out[i] - out[i - 64];
     614      922581 :         while (rice->ksum < ksummin) {
     615        3797 :             rice->k--;
     616        3797 :             ksummin = rice->k ? ksummin >> 1 : 0;
     617        3797 :             ksummax >>= 1;
     618             :         }
     619      922571 :         while (rice->ksum >= ksummax) {
     620        3787 :             rice->k++;
     621        3787 :             if (rice->k > 24)
     622           0 :                 return;
     623        3787 :             ksummax <<= 1;
     624        3787 :             ksummin = ksummin ? ksummin << 1 : 128;
     625             :         }
     626             :     }
     627             : 
     628      460822 :     for (i = 0; i < blockstodecode; i++)
     629      460800 :         out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
     630             : }
     631             : 
     632           0 : static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
     633             : {
     634           0 :     decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
     635             :                       blockstodecode);
     636           0 : }
     637             : 
     638          11 : static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
     639             : {
     640          11 :     decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
     641             :                       blockstodecode);
     642          11 :     decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
     643             :                       blockstodecode);
     644          11 : }
     645             : 
     646           0 : static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
     647             : {
     648           0 :     int32_t *decoded0 = ctx->decoded[0];
     649             : 
     650           0 :     while (blockstodecode--)
     651           0 :         *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
     652           0 : }
     653             : 
     654          22 : static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
     655             : {
     656          22 :     int32_t *decoded0 = ctx->decoded[0];
     657          22 :     int32_t *decoded1 = ctx->decoded[1];
     658          22 :     int blocks = blockstodecode;
     659             : 
     660      460844 :     while (blockstodecode--)
     661      460800 :         *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
     662      460844 :     while (blocks--)
     663      460800 :         *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
     664          22 : }
     665             : 
     666           0 : static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
     667             : {
     668           0 :     int32_t *decoded0 = ctx->decoded[0];
     669             : 
     670           0 :     while (blockstodecode--)
     671           0 :         *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
     672           0 : }
     673             : 
     674           8 : static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
     675             : {
     676           8 :     int32_t *decoded0 = ctx->decoded[0];
     677           8 :     int32_t *decoded1 = ctx->decoded[1];
     678           8 :     int blocks = blockstodecode;
     679             : 
     680      589840 :     while (blockstodecode--)
     681      589824 :         *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
     682           8 :     range_dec_normalize(ctx);
     683             :     // because of some implementation peculiarities we need to backpedal here
     684           8 :     ctx->ptr -= 1;
     685           8 :     range_start_decoding(ctx);
     686      589840 :     while (blocks--)
     687      589824 :         *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
     688           8 : }
     689             : 
     690          64 : static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
     691             : {
     692          64 :     int32_t *decoded0 = ctx->decoded[0];
     693          64 :     int32_t *decoded1 = ctx->decoded[1];
     694             : 
     695      295040 :     while (blockstodecode--) {
     696      294912 :         *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
     697      294912 :         *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
     698             :     }
     699          64 : }
     700             : 
     701           0 : static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
     702             : {
     703           0 :     int32_t *decoded0 = ctx->decoded[0];
     704             : 
     705           0 :     while (blockstodecode--)
     706           0 :         *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
     707           0 : }
     708             : 
     709          95 : static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
     710             : {
     711          95 :     int32_t *decoded0 = ctx->decoded[0];
     712          95 :     int32_t *decoded1 = ctx->decoded[1];
     713             : 
     714      437950 :     while (blockstodecode--) {
     715      437760 :         *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
     716      437760 :         *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
     717             :     }
     718          95 : }
     719             : 
     720          51 : static int init_entropy_decoder(APEContext *ctx)
     721             : {
     722             :     /* Read the CRC */
     723          51 :     if (ctx->fileversion >= 3900) {
     724          18 :         if (ctx->data_end - ctx->ptr < 6)
     725           0 :             return AVERROR_INVALIDDATA;
     726          18 :         ctx->CRC = bytestream_get_be32(&ctx->ptr);
     727             :     } else {
     728          33 :         ctx->CRC = get_bits_long(&ctx->gb, 32);
     729             :     }
     730             : 
     731             :     /* Read the frame flags if they exist */
     732          51 :     ctx->frameflags = 0;
     733          51 :     if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
     734           0 :         ctx->CRC &= ~0x80000000;
     735             : 
     736           0 :         if (ctx->data_end - ctx->ptr < 6)
     737           0 :             return AVERROR_INVALIDDATA;
     738           0 :         ctx->frameflags = bytestream_get_be32(&ctx->ptr);
     739             :     }
     740             : 
     741             :     /* Initialize the rice structs */
     742          51 :     ctx->riceX.k = 10;
     743          51 :     ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
     744          51 :     ctx->riceY.k = 10;
     745          51 :     ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
     746             : 
     747          51 :     if (ctx->fileversion >= 3900) {
     748             :         /* The first 8 bits of input are ignored. */
     749          18 :         ctx->ptr++;
     750             : 
     751          18 :         range_start_decoding(ctx);
     752             :     }
     753             : 
     754          51 :     return 0;
     755             : }
     756             : 
     757             : static const int32_t initial_coeffs_fast_3320[1] = {
     758             :     375,
     759             : };
     760             : 
     761             : static const int32_t initial_coeffs_a_3800[3] = {
     762             :     64, 115, 64,
     763             : };
     764             : 
     765             : static const int32_t initial_coeffs_b_3800[2] = {
     766             :     740, 0
     767             : };
     768             : 
     769             : static const int32_t initial_coeffs_3930[4] = {
     770             :     360, 317, -109, 98
     771             : };
     772             : 
     773          51 : static void init_predictor_decoder(APEContext *ctx)
     774             : {
     775          51 :     APEPredictor *p = &ctx->predictor;
     776             : 
     777             :     /* Zero the history buffers */
     778          51 :     memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
     779          51 :     p->buf = p->historybuffer;
     780             : 
     781             :     /* Initialize and zero the coefficients */
     782          51 :     if (ctx->fileversion < 3930) {
     783          41 :         if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
     784           0 :             memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
     785             :                    sizeof(initial_coeffs_fast_3320));
     786           0 :             memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
     787             :                    sizeof(initial_coeffs_fast_3320));
     788             :         } else {
     789          41 :             memcpy(p->coeffsA[0], initial_coeffs_a_3800,
     790             :                    sizeof(initial_coeffs_a_3800));
     791          41 :             memcpy(p->coeffsA[1], initial_coeffs_a_3800,
     792             :                    sizeof(initial_coeffs_a_3800));
     793             :         }
     794             :     } else {
     795          10 :         memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
     796          10 :         memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
     797             :     }
     798          51 :     memset(p->coeffsB, 0, sizeof(p->coeffsB));
     799          51 :     if (ctx->fileversion < 3930) {
     800          41 :         memcpy(p->coeffsB[0], initial_coeffs_b_3800,
     801             :                sizeof(initial_coeffs_b_3800));
     802          41 :         memcpy(p->coeffsB[1], initial_coeffs_b_3800,
     803             :                sizeof(initial_coeffs_b_3800));
     804             :     }
     805             : 
     806          51 :     p->filterA[0] = p->filterA[1] = 0;
     807          51 :     p->filterB[0] = p->filterB[1] = 0;
     808          51 :     p->lastA[0]   = p->lastA[1]   = 0;
     809             : 
     810          51 :     p->sample_pos = 0;
     811          51 : }
     812             : 
     813             : /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
     814    15356535 : static inline int APESIGN(int32_t x) {
     815    15356535 :     return (x < 0) - (x > 0);
     816             : }
     817             : 
     818           0 : static av_always_inline int filter_fast_3320(APEPredictor *p,
     819             :                                              const int decoded, const int filter,
     820             :                                              const int delayA)
     821             : {
     822             :     int32_t predictionA;
     823             : 
     824           0 :     p->buf[delayA] = p->lastA[filter];
     825           0 :     if (p->sample_pos < 3) {
     826           0 :         p->lastA[filter]   = decoded;
     827           0 :         p->filterA[filter] = decoded;
     828           0 :         return decoded;
     829             :     }
     830             : 
     831           0 :     predictionA = p->buf[delayA] * 2 - p->buf[delayA - 1];
     832           0 :     p->lastA[filter] = decoded + (predictionA  * p->coeffsA[filter][0] >> 9);
     833             : 
     834           0 :     if ((decoded ^ predictionA) > 0)
     835           0 :         p->coeffsA[filter][0]++;
     836             :     else
     837           0 :         p->coeffsA[filter][0]--;
     838             : 
     839           0 :     p->filterA[filter] += p->lastA[filter];
     840             : 
     841           0 :     return p->filterA[filter];
     842             : }
     843             : 
     844     2562048 : static av_always_inline int filter_3800(APEPredictor *p,
     845             :                                         const int decoded, const int filter,
     846             :                                         const int delayA,  const int delayB,
     847             :                                         const int start,   const int shift)
     848             : {
     849             :     int32_t predictionA, predictionB, sign;
     850             :     int32_t d0, d1, d2, d3, d4;
     851             : 
     852     2562048 :     p->buf[delayA] = p->lastA[filter];
     853     2562048 :     p->buf[delayB] = p->filterB[filter];
     854     2562048 :     if (p->sample_pos < start) {
     855        4856 :         predictionA = decoded + p->filterA[filter];
     856        4856 :         p->lastA[filter]   = decoded;
     857        4856 :         p->filterB[filter] = decoded;
     858        4856 :         p->filterA[filter] = predictionA;
     859        4856 :         return predictionA;
     860             :     }
     861     2557192 :     d2 =  p->buf[delayA];
     862     2557192 :     d1 = (p->buf[delayA] - p->buf[delayA - 1]) << 1;
     863     2557192 :     d0 =  p->buf[delayA] + ((p->buf[delayA - 2] - p->buf[delayA - 1]) << 3);
     864     2557192 :     d3 =  p->buf[delayB] * 2 - p->buf[delayB - 1];
     865     2557192 :     d4 =  p->buf[delayB];
     866             : 
     867     7671576 :     predictionA = d0 * p->coeffsA[filter][0] +
     868     2557192 :                   d1 * p->coeffsA[filter][1] +
     869     2557192 :                   d2 * p->coeffsA[filter][2];
     870             : 
     871     2557192 :     sign = APESIGN(decoded);
     872     2557192 :     p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
     873     2557192 :     p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
     874     2557192 :     p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
     875             : 
     876     5114384 :     predictionB = d3 * p->coeffsB[filter][0] -
     877     2557192 :                   d4 * p->coeffsB[filter][1];
     878     2557192 :     p->lastA[filter] = decoded + (predictionA >> 11);
     879     2557192 :     sign = APESIGN(p->lastA[filter]);
     880     2557192 :     p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
     881     2557192 :     p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
     882             : 
     883     2557192 :     p->filterB[filter] = p->lastA[filter] + (predictionB >> shift);
     884     2557192 :     p->filterA[filter] = p->filterB[filter] + ((p->filterA[filter] * 31) >> 5);
     885             : 
     886     2557192 :     return p->filterA[filter];
     887             : }
     888             : 
     889          20 : static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
     890             : {
     891             :     int i, j;
     892             :     int32_t dotprod, sign;
     893             :     int32_t coeffs[256], delay[256];
     894             : 
     895          20 :     if (order >= length)
     896           0 :         return;
     897             : 
     898          20 :     memset(coeffs, 0, order * sizeof(*coeffs));
     899        4628 :     for (i = 0; i < order; i++)
     900        4608 :         delay[i] = buffer[i];
     901     1469972 :     for (i = order; i < length; i++) {
     902     1469952 :         dotprod = 0;
     903     1469952 :         sign = APESIGN(buffer[i]);
     904   340094464 :         for (j = 0; j < order; j++) {
     905   338624512 :             dotprod += delay[j] * coeffs[j];
     906   338624512 :             coeffs[j] += ((delay[j] >> 31) | 1) * sign;
     907             :         }
     908     1469952 :         buffer[i] -= dotprod >> shift;
     909   338624512 :         for (j = 0; j < order - 1; j++)
     910   337154560 :             delay[j] = delay[j + 1];
     911     1469952 :         delay[order - 1] = buffer[i];
     912             :     }
     913             : }
     914             : 
     915          16 : static void long_filter_ehigh_3830(int32_t *buffer, int length)
     916             : {
     917             :     int i, j;
     918             :     int32_t dotprod, sign;
     919          16 :     int32_t coeffs[8] = { 0 }, delay[8] = { 0 };
     920             : 
     921     1175568 :     for (i = 0; i < length; i++) {
     922     1175552 :         dotprod = 0;
     923     1175552 :         sign = APESIGN(buffer[i]);
     924    10579968 :         for (j = 7; j >= 0; j--) {
     925     9404416 :             dotprod += delay[j] * coeffs[j];
     926     9404416 :             coeffs[j] += ((delay[j] >> 31) | 1) * sign;
     927             :         }
     928     9404416 :         for (j = 7; j > 0; j--)
     929     8228864 :             delay[j] = delay[j - 1];
     930     1175552 :         delay[0] = buffer[i];
     931     1175552 :         buffer[i] -= dotprod >> 9;
     932             :     }
     933          16 : }
     934             : 
     935          41 : static void predictor_decode_stereo_3800(APEContext *ctx, int count)
     936             : {
     937          41 :     APEPredictor *p = &ctx->predictor;
     938          41 :     int32_t *decoded0 = ctx->decoded[0];
     939          41 :     int32_t *decoded1 = ctx->decoded[1];
     940          41 :     int start = 4, shift = 10;
     941             : 
     942          41 :     if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
     943           0 :         start = 16;
     944           0 :         long_filter_high_3800(decoded0, 16, 9, count);
     945           0 :         long_filter_high_3800(decoded1, 16, 9, count);
     946          41 :     } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
     947          10 :         int order = 128, shift2 = 11;
     948             : 
     949          10 :         if (ctx->fileversion >= 3830) {
     950           8 :             order <<= 1;
     951           8 :             shift++;
     952           8 :             shift2++;
     953           8 :             long_filter_ehigh_3830(decoded0 + order, count - order);
     954           8 :             long_filter_ehigh_3830(decoded1 + order, count - order);
     955             :         }
     956          10 :         start = order;
     957          10 :         long_filter_high_3800(decoded0, order, shift2, count);
     958          10 :         long_filter_high_3800(decoded1, order, shift2, count);
     959             :     }
     960             : 
     961     1281106 :     while (count--) {
     962     1281024 :         int X = *decoded0, Y = *decoded1;
     963     1281024 :         if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
     964           0 :             *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
     965           0 :             decoded0++;
     966           0 :             *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
     967           0 :             decoded1++;
     968             :         } else {
     969     1281024 :             *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
     970             :                                     start, shift);
     971     1281024 :             decoded0++;
     972     1281024 :             *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
     973             :                                     start, shift);
     974     1281024 :             decoded1++;
     975             :         }
     976             : 
     977             :         /* Combined */
     978     1281024 :         p->buf++;
     979     1281024 :         p->sample_pos++;
     980             : 
     981             :         /* Have we filled the history buffer? */
     982     1281024 :         if (p->buf == p->historybuffer + HISTORY_SIZE) {
     983        2502 :             memmove(p->historybuffer, p->buf,
     984             :                     PREDICTOR_SIZE * sizeof(*p->historybuffer));
     985        2502 :             p->buf = p->historybuffer;
     986             :         }
     987             :     }
     988          41 : }
     989             : 
     990           0 : static void predictor_decode_mono_3800(APEContext *ctx, int count)
     991             : {
     992           0 :     APEPredictor *p = &ctx->predictor;
     993           0 :     int32_t *decoded0 = ctx->decoded[0];
     994           0 :     int start = 4, shift = 10;
     995             : 
     996           0 :     if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
     997           0 :         start = 16;
     998           0 :         long_filter_high_3800(decoded0, 16, 9, count);
     999           0 :     } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
    1000           0 :         int order = 128, shift2 = 11;
    1001             : 
    1002           0 :         if (ctx->fileversion >= 3830) {
    1003           0 :             order <<= 1;
    1004           0 :             shift++;
    1005           0 :             shift2++;
    1006           0 :             long_filter_ehigh_3830(decoded0 + order, count - order);
    1007             :         }
    1008           0 :         start = order;
    1009           0 :         long_filter_high_3800(decoded0, order, shift2, count);
    1010             :     }
    1011             : 
    1012           0 :     while (count--) {
    1013           0 :         if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
    1014           0 :             *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
    1015           0 :             decoded0++;
    1016             :         } else {
    1017           0 :             *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
    1018             :                                     start, shift);
    1019           0 :             decoded0++;
    1020             :         }
    1021             : 
    1022             :         /* Combined */
    1023           0 :         p->buf++;
    1024           0 :         p->sample_pos++;
    1025             : 
    1026             :         /* Have we filled the history buffer? */
    1027           0 :         if (p->buf == p->historybuffer + HISTORY_SIZE) {
    1028           0 :             memmove(p->historybuffer, p->buf,
    1029             :                     PREDICTOR_SIZE * sizeof(*p->historybuffer));
    1030           0 :             p->buf = p->historybuffer;
    1031             :         }
    1032             :     }
    1033           0 : }
    1034             : 
    1035      589824 : static av_always_inline int predictor_update_3930(APEPredictor *p,
    1036             :                                                   const int decoded, const int filter,
    1037             :                                                   const int delayA)
    1038             : {
    1039             :     int32_t predictionA, sign;
    1040             :     int32_t d0, d1, d2, d3;
    1041             : 
    1042      589824 :     p->buf[delayA]     = p->lastA[filter];
    1043      589824 :     d0 = p->buf[delayA    ];
    1044      589824 :     d1 = p->buf[delayA    ] - p->buf[delayA - 1];
    1045      589824 :     d2 = p->buf[delayA - 1] - p->buf[delayA - 2];
    1046      589824 :     d3 = p->buf[delayA - 2] - p->buf[delayA - 3];
    1047             : 
    1048     1769472 :     predictionA = d0 * p->coeffsA[filter][0] +
    1049     1179648 :                   d1 * p->coeffsA[filter][1] +
    1050      589824 :                   d2 * p->coeffsA[filter][2] +
    1051      589824 :                   d3 * p->coeffsA[filter][3];
    1052             : 
    1053      589824 :     p->lastA[filter] = decoded + (predictionA >> 9);
    1054      589824 :     p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
    1055             : 
    1056      589824 :     sign = APESIGN(decoded);
    1057      589824 :     p->coeffsA[filter][0] += ((d0 < 0) * 2 - 1) * sign;
    1058      589824 :     p->coeffsA[filter][1] += ((d1 < 0) * 2 - 1) * sign;
    1059      589824 :     p->coeffsA[filter][2] += ((d2 < 0) * 2 - 1) * sign;
    1060      589824 :     p->coeffsA[filter][3] += ((d3 < 0) * 2 - 1) * sign;
    1061             : 
    1062      589824 :     return p->filterA[filter];
    1063             : }
    1064             : 
    1065          64 : static void predictor_decode_stereo_3930(APEContext *ctx, int count)
    1066             : {
    1067          64 :     APEPredictor *p = &ctx->predictor;
    1068          64 :     int32_t *decoded0 = ctx->decoded[0];
    1069          64 :     int32_t *decoded1 = ctx->decoded[1];
    1070             : 
    1071          64 :     ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
    1072             : 
    1073      295040 :     while (count--) {
    1074             :         /* Predictor Y */
    1075      294912 :         int Y = *decoded1, X = *decoded0;
    1076      294912 :         *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
    1077      294912 :         decoded0++;
    1078      294912 :         *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
    1079      294912 :         decoded1++;
    1080             : 
    1081             :         /* Combined */
    1082      294912 :         p->buf++;
    1083             : 
    1084             :         /* Have we filled the history buffer? */
    1085      294912 :         if (p->buf == p->historybuffer + HISTORY_SIZE) {
    1086         576 :             memmove(p->historybuffer, p->buf,
    1087             :                     PREDICTOR_SIZE * sizeof(*p->historybuffer));
    1088         576 :             p->buf = p->historybuffer;
    1089             :         }
    1090             :     }
    1091          64 : }
    1092             : 
    1093           0 : static void predictor_decode_mono_3930(APEContext *ctx, int count)
    1094             : {
    1095           0 :     APEPredictor *p = &ctx->predictor;
    1096           0 :     int32_t *decoded0 = ctx->decoded[0];
    1097             : 
    1098           0 :     ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
    1099             : 
    1100           0 :     while (count--) {
    1101           0 :         *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
    1102           0 :         decoded0++;
    1103             : 
    1104           0 :         p->buf++;
    1105             : 
    1106             :         /* Have we filled the history buffer? */
    1107           0 :         if (p->buf == p->historybuffer + HISTORY_SIZE) {
    1108           0 :             memmove(p->historybuffer, p->buf,
    1109             :                     PREDICTOR_SIZE * sizeof(*p->historybuffer));
    1110           0 :             p->buf = p->historybuffer;
    1111             :         }
    1112             :     }
    1113           0 : }
    1114             : 
    1115      875520 : static av_always_inline int predictor_update_filter(APEPredictor *p,
    1116             :                                                     const int decoded, const int filter,
    1117             :                                                     const int delayA,  const int delayB,
    1118             :                                                     const int adaptA,  const int adaptB)
    1119             : {
    1120             :     int32_t predictionA, predictionB, sign;
    1121             : 
    1122      875520 :     p->buf[delayA]     = p->lastA[filter];
    1123      875520 :     p->buf[adaptA]     = APESIGN(p->buf[delayA]);
    1124      875520 :     p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
    1125      875520 :     p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
    1126             : 
    1127     2626560 :     predictionA = p->buf[delayA    ] * p->coeffsA[filter][0] +
    1128     1751040 :                   p->buf[delayA - 1] * p->coeffsA[filter][1] +
    1129      875520 :                   p->buf[delayA - 2] * p->coeffsA[filter][2] +
    1130      875520 :                   p->buf[delayA - 3] * p->coeffsA[filter][3];
    1131             : 
    1132             :     /*  Apply a scaled first-order filter compression */
    1133      875520 :     p->buf[delayB]     = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
    1134      875520 :     p->buf[adaptB]     = APESIGN(p->buf[delayB]);
    1135      875520 :     p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
    1136      875520 :     p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
    1137      875520 :     p->filterB[filter] = p->filterA[filter ^ 1];
    1138             : 
    1139     2626560 :     predictionB = p->buf[delayB    ] * p->coeffsB[filter][0] +
    1140     1751040 :                   p->buf[delayB - 1] * p->coeffsB[filter][1] +
    1141     1751040 :                   p->buf[delayB - 2] * p->coeffsB[filter][2] +
    1142      875520 :                   p->buf[delayB - 3] * p->coeffsB[filter][3] +
    1143      875520 :                   p->buf[delayB - 4] * p->coeffsB[filter][4];
    1144             : 
    1145      875520 :     p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
    1146      875520 :     p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
    1147             : 
    1148      875520 :     sign = APESIGN(decoded);
    1149      875520 :     p->coeffsA[filter][0] += p->buf[adaptA    ] * sign;
    1150      875520 :     p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
    1151      875520 :     p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
    1152      875520 :     p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
    1153      875520 :     p->coeffsB[filter][0] += p->buf[adaptB    ] * sign;
    1154      875520 :     p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
    1155      875520 :     p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
    1156      875520 :     p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
    1157      875520 :     p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
    1158             : 
    1159      875520 :     return p->filterA[filter];
    1160             : }
    1161             : 
    1162          95 : static void predictor_decode_stereo_3950(APEContext *ctx, int count)
    1163             : {
    1164          95 :     APEPredictor *p = &ctx->predictor;
    1165          95 :     int32_t *decoded0 = ctx->decoded[0];
    1166          95 :     int32_t *decoded1 = ctx->decoded[1];
    1167             : 
    1168          95 :     ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
    1169             : 
    1170      437950 :     while (count--) {
    1171             :         /* Predictor Y */
    1172      437760 :         *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
    1173             :                                             YADAPTCOEFFSA, YADAPTCOEFFSB);
    1174      437760 :         decoded0++;
    1175      437760 :         *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
    1176             :                                             XADAPTCOEFFSA, XADAPTCOEFFSB);
    1177      437760 :         decoded1++;
    1178             : 
    1179             :         /* Combined */
    1180      437760 :         p->buf++;
    1181             : 
    1182             :         /* Have we filled the history buffer? */
    1183      437760 :         if (p->buf == p->historybuffer + HISTORY_SIZE) {
    1184         855 :             memmove(p->historybuffer, p->buf,
    1185             :                     PREDICTOR_SIZE * sizeof(*p->historybuffer));
    1186         855 :             p->buf = p->historybuffer;
    1187             :         }
    1188             :     }
    1189          95 : }
    1190             : 
    1191           0 : static void predictor_decode_mono_3950(APEContext *ctx, int count)
    1192             : {
    1193           0 :     APEPredictor *p = &ctx->predictor;
    1194           0 :     int32_t *decoded0 = ctx->decoded[0];
    1195             :     int32_t predictionA, currentA, A, sign;
    1196             : 
    1197           0 :     ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
    1198             : 
    1199           0 :     currentA = p->lastA[0];
    1200             : 
    1201           0 :     while (count--) {
    1202           0 :         A = *decoded0;
    1203             : 
    1204           0 :         p->buf[YDELAYA] = currentA;
    1205           0 :         p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
    1206             : 
    1207           0 :         predictionA = p->buf[YDELAYA    ] * p->coeffsA[0][0] +
    1208           0 :                       p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
    1209           0 :                       p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
    1210           0 :                       p->buf[YDELAYA - 3] * p->coeffsA[0][3];
    1211             : 
    1212           0 :         currentA = A + (predictionA >> 10);
    1213             : 
    1214           0 :         p->buf[YADAPTCOEFFSA]     = APESIGN(p->buf[YDELAYA    ]);
    1215           0 :         p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
    1216             : 
    1217           0 :         sign = APESIGN(A);
    1218           0 :         p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA    ] * sign;
    1219           0 :         p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
    1220           0 :         p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
    1221           0 :         p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
    1222             : 
    1223           0 :         p->buf++;
    1224             : 
    1225             :         /* Have we filled the history buffer? */
    1226           0 :         if (p->buf == p->historybuffer + HISTORY_SIZE) {
    1227           0 :             memmove(p->historybuffer, p->buf,
    1228             :                     PREDICTOR_SIZE * sizeof(*p->historybuffer));
    1229           0 :             p->buf = p->historybuffer;
    1230             :         }
    1231             : 
    1232           0 :         p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
    1233           0 :         *(decoded0++) = p->filterA[0];
    1234             :     }
    1235             : 
    1236           0 :     p->lastA[0] = currentA;
    1237           0 : }
    1238             : 
    1239         126 : static void do_init_filter(APEFilter *f, int16_t *buf, int order)
    1240             : {
    1241         126 :     f->coeffs = buf;
    1242         126 :     f->historybuffer = buf + order;
    1243         126 :     f->delay       = f->historybuffer + order * 2;
    1244         126 :     f->adaptcoeffs = f->historybuffer + order;
    1245             : 
    1246         126 :     memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
    1247         126 :     memset(f->coeffs, 0, order * sizeof(*f->coeffs));
    1248         126 :     f->avg = 0;
    1249         126 : }
    1250             : 
    1251          63 : static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
    1252             : {
    1253          63 :     do_init_filter(&f[0], buf, order);
    1254          63 :     do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
    1255          63 : }
    1256             : 
    1257         382 : static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
    1258             :                             int32_t *data, int count, int order, int fracbits)
    1259             : {
    1260             :     int res;
    1261             :     int absres;
    1262             : 
    1263     1761020 :     while (count--) {
    1264             :         /* round fixedpoint scalar product */
    1265     3520512 :         res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs,
    1266     1760256 :                                                      f->delay - order,
    1267     1760256 :                                                      f->adaptcoeffs - order,
    1268             :                                                      order, APESIGN(*data));
    1269     1760256 :         res = (res + (1 << (fracbits - 1))) >> fracbits;
    1270     1760256 :         res += *data;
    1271     1760256 :         *data++ = res;
    1272             : 
    1273             :         /* Update the output history */
    1274     1760256 :         *f->delay++ = av_clip_int16(res);
    1275             : 
    1276     1760256 :         if (version < 3980) {
    1277             :             /* Version ??? to < 3.98 files (untested) */
    1278      884736 :             f->adaptcoeffs[0]  = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
    1279      884736 :             f->adaptcoeffs[-4] >>= 1;
    1280      884736 :             f->adaptcoeffs[-8] >>= 1;
    1281             :         } else {
    1282             :             /* Version 3.98 and later files */
    1283             : 
    1284             :             /* Update the adaption coefficients */
    1285      875520 :             absres = FFABS(res);
    1286      875520 :             if (absres)
    1287     1737934 :                 *f->adaptcoeffs = APESIGN(res) *
    1288      868967 :                                   (8 << ((absres > f->avg * 3) + (absres > f->avg * 4 / 3)));
    1289             :                 /* equivalent to the following code
    1290             :                     if (absres <= f->avg * 4 / 3)
    1291             :                         *f->adaptcoeffs = APESIGN(res) * 8;
    1292             :                     else if (absres <= f->avg * 3)
    1293             :                         *f->adaptcoeffs = APESIGN(res) * 16;
    1294             :                     else
    1295             :                         *f->adaptcoeffs = APESIGN(res) * 32;
    1296             :                 */
    1297             :             else
    1298        6553 :                 *f->adaptcoeffs = 0;
    1299             : 
    1300      875520 :             f->avg += (absres - f->avg) / 16;
    1301             : 
    1302      875520 :             f->adaptcoeffs[-1] >>= 1;
    1303      875520 :             f->adaptcoeffs[-2] >>= 1;
    1304      875520 :             f->adaptcoeffs[-8] >>= 1;
    1305             :         }
    1306             : 
    1307     1760256 :         f->adaptcoeffs++;
    1308             : 
    1309             :         /* Have we filled the history buffer? */
    1310     1760256 :         if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
    1311        3438 :             memmove(f->historybuffer, f->delay - (order * 2),
    1312             :                     (order * 2) * sizeof(*f->historybuffer));
    1313        3438 :             f->delay = f->historybuffer + order * 2;
    1314        3438 :             f->adaptcoeffs = f->historybuffer + order;
    1315             :         }
    1316             :     }
    1317         382 : }
    1318             : 
    1319         191 : static void apply_filter(APEContext *ctx, APEFilter *f,
    1320             :                          int32_t *data0, int32_t *data1,
    1321             :                          int count, int order, int fracbits)
    1322             : {
    1323         191 :     do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
    1324         191 :     if (data1)
    1325         191 :         do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
    1326         191 : }
    1327             : 
    1328         159 : static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
    1329             :                               int32_t *decoded1, int count)
    1330             : {
    1331             :     int i;
    1332             : 
    1333         350 :     for (i = 0; i < APE_FILTER_LEVELS; i++) {
    1334         350 :         if (!ape_filter_orders[ctx->fset][i])
    1335         159 :             break;
    1336         382 :         apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
    1337         191 :                      ape_filter_orders[ctx->fset][i],
    1338         191 :                      ape_filter_fracbits[ctx->fset][i]);
    1339             :     }
    1340         159 : }
    1341             : 
    1342          51 : static int init_frame_decoder(APEContext *ctx)
    1343             : {
    1344             :     int i, ret;
    1345          51 :     if ((ret = init_entropy_decoder(ctx)) < 0)
    1346           0 :         return ret;
    1347          51 :     init_predictor_decoder(ctx);
    1348             : 
    1349         114 :     for (i = 0; i < APE_FILTER_LEVELS; i++) {
    1350         114 :         if (!ape_filter_orders[ctx->fset][i])
    1351          51 :             break;
    1352          63 :         init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
    1353          63 :                     ape_filter_orders[ctx->fset][i]);
    1354             :     }
    1355          51 :     return 0;
    1356             : }
    1357             : 
    1358           0 : static void ape_unpack_mono(APEContext *ctx, int count)
    1359             : {
    1360           0 :     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
    1361             :         /* We are pure silence, so we're done. */
    1362           0 :         av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
    1363           0 :         return;
    1364             :     }
    1365             : 
    1366           0 :     ctx->entropy_decode_mono(ctx, count);
    1367             : 
    1368             :     /* Now apply the predictor decoding */
    1369           0 :     ctx->predictor_decode_mono(ctx, count);
    1370             : 
    1371             :     /* Pseudo-stereo - just copy left channel to right channel */
    1372           0 :     if (ctx->channels == 2) {
    1373           0 :         memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1]));
    1374             :     }
    1375             : }
    1376             : 
    1377         200 : static void ape_unpack_stereo(APEContext *ctx, int count)
    1378             : {
    1379             :     int32_t left, right;
    1380         200 :     int32_t *decoded0 = ctx->decoded[0];
    1381         200 :     int32_t *decoded1 = ctx->decoded[1];
    1382             : 
    1383         200 :     if ((ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) == APE_FRAMECODE_STEREO_SILENCE) {
    1384             :         /* We are pure silence, so we're done. */
    1385           0 :         av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
    1386           0 :         return;
    1387             :     }
    1388             : 
    1389         200 :     ctx->entropy_decode_stereo(ctx, count);
    1390             : 
    1391             :     /* Now apply the predictor decoding */
    1392         200 :     ctx->predictor_decode_stereo(ctx, count);
    1393             : 
    1394             :     /* Decorrelate and scale to output depth */
    1395     2014096 :     while (count--) {
    1396     2013696 :         left = *decoded1 - (*decoded0 / 2);
    1397     2013696 :         right = left + *decoded0;
    1398             : 
    1399     2013696 :         *(decoded0++) = left;
    1400     2013696 :         *(decoded1++) = right;
    1401             :     }
    1402             : }
    1403             : 
    1404         213 : static int ape_decode_frame(AVCodecContext *avctx, void *data,
    1405             :                             int *got_frame_ptr, AVPacket *avpkt)
    1406             : {
    1407         213 :     AVFrame *frame     = data;
    1408         213 :     const uint8_t *buf = avpkt->data;
    1409         213 :     APEContext *s = avctx->priv_data;
    1410             :     uint8_t *sample8;
    1411             :     int16_t *sample16;
    1412             :     int32_t *sample24;
    1413             :     int i, ch, ret;
    1414             :     int blockstodecode;
    1415             : 
    1416             :     /* this should never be negative, but bad things will happen if it is, so
    1417             :        check it just to make sure. */
    1418         213 :     av_assert0(s->samples >= 0);
    1419             : 
    1420         213 :     if(!s->samples){
    1421             :         uint32_t nblocks, offset;
    1422             :         int buf_size;
    1423             : 
    1424          64 :         if (!avpkt->size) {
    1425          13 :             *got_frame_ptr = 0;
    1426          13 :             return 0;
    1427             :         }
    1428          51 :         if (avpkt->size < 8) {
    1429           0 :             av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
    1430           0 :             return AVERROR_INVALIDDATA;
    1431             :         }
    1432          51 :         buf_size = avpkt->size & ~3;
    1433          51 :         if (buf_size != avpkt->size) {
    1434           0 :             av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
    1435             :                    "extra bytes at the end will be skipped.\n");
    1436             :         }
    1437          51 :         if (s->fileversion < 3950) // previous versions overread two bytes
    1438          45 :             buf_size += 2;
    1439          51 :         av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
    1440          51 :         if (!s->data)
    1441           0 :             return AVERROR(ENOMEM);
    1442          51 :         s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
    1443             :                           buf_size >> 2);
    1444          51 :         memset(s->data + (buf_size & ~3), 0, buf_size & 3);
    1445          51 :         s->ptr = s->data;
    1446          51 :         s->data_end = s->data + buf_size;
    1447             : 
    1448          51 :         nblocks = bytestream_get_be32(&s->ptr);
    1449          51 :         offset  = bytestream_get_be32(&s->ptr);
    1450          51 :         if (s->fileversion >= 3900) {
    1451          18 :             if (offset > 3) {
    1452           0 :                 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
    1453           0 :                 s->data = NULL;
    1454           0 :                 return AVERROR_INVALIDDATA;
    1455             :             }
    1456          18 :             if (s->data_end - s->ptr < offset) {
    1457           0 :                 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
    1458           0 :                 return AVERROR_INVALIDDATA;
    1459             :             }
    1460          18 :             s->ptr += offset;
    1461             :         } else {
    1462          33 :             if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
    1463           0 :                 return ret;
    1464          33 :             if (s->fileversion > 3800)
    1465          22 :                 skip_bits_long(&s->gb, offset * 8);
    1466             :             else
    1467          11 :                 skip_bits_long(&s->gb, offset);
    1468             :         }
    1469             : 
    1470          51 :         if (!nblocks || nblocks > INT_MAX) {
    1471           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n",
    1472             :                    nblocks);
    1473           0 :             return AVERROR_INVALIDDATA;
    1474             :         }
    1475             : 
    1476             :         /* Initialize the frame decoder */
    1477          51 :         if (init_frame_decoder(s) < 0) {
    1478           0 :             av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
    1479           0 :             return AVERROR_INVALIDDATA;
    1480             :         }
    1481          51 :         s->samples = nblocks;
    1482             :     }
    1483             : 
    1484         200 :     if (!s->data) {
    1485           0 :         *got_frame_ptr = 0;
    1486           0 :         return avpkt->size;
    1487             :     }
    1488             : 
    1489         200 :     blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
    1490             :     // for old files coefficients were not interleaved,
    1491             :     // so we need to decode all of them at once
    1492         200 :     if (s->fileversion < 3930)
    1493          41 :         blockstodecode = s->samples;
    1494             : 
    1495             :     /* reallocate decoded sample buffer if needed */
    1496         200 :     av_fast_malloc(&s->decoded_buffer, &s->decoded_size,
    1497         200 :                    2 * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer));
    1498         200 :     if (!s->decoded_buffer)
    1499           0 :         return AVERROR(ENOMEM);
    1500         200 :     memset(s->decoded_buffer, 0, s->decoded_size);
    1501         200 :     s->decoded[0] = s->decoded_buffer;
    1502         200 :     s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
    1503             : 
    1504             :     /* get output buffer */
    1505         200 :     frame->nb_samples = blockstodecode;
    1506         200 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
    1507           0 :         return ret;
    1508             : 
    1509         200 :     s->error=0;
    1510             : 
    1511         200 :     if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
    1512           0 :         ape_unpack_mono(s, blockstodecode);
    1513             :     else
    1514         200 :         ape_unpack_stereo(s, blockstodecode);
    1515         200 :     emms_c();
    1516             : 
    1517         200 :     if (s->error) {
    1518           1 :         s->samples=0;
    1519           1 :         av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
    1520           1 :         return AVERROR_INVALIDDATA;
    1521             :     }
    1522             : 
    1523         199 :     switch (s->bps) {
    1524             :     case 8:
    1525           0 :         for (ch = 0; ch < s->channels; ch++) {
    1526           0 :             sample8 = (uint8_t *)frame->data[ch];
    1527           0 :             for (i = 0; i < blockstodecode; i++)
    1528           0 :                 *sample8++ = (s->decoded[ch][i] + 0x80) & 0xff;
    1529             :         }
    1530           0 :         break;
    1531             :     case 16:
    1532         597 :         for (ch = 0; ch < s->channels; ch++) {
    1533         398 :             sample16 = (int16_t *)frame->data[ch];
    1534     4018574 :             for (i = 0; i < blockstodecode; i++)
    1535     4018176 :                 *sample16++ = s->decoded[ch][i];
    1536             :         }
    1537         199 :         break;
    1538             :     case 24:
    1539           0 :         for (ch = 0; ch < s->channels; ch++) {
    1540           0 :             sample24 = (int32_t *)frame->data[ch];
    1541           0 :             for (i = 0; i < blockstodecode; i++)
    1542           0 :                 *sample24++ = s->decoded[ch][i] << 8;
    1543             :         }
    1544           0 :         break;
    1545             :     }
    1546             : 
    1547         199 :     s->samples -= blockstodecode;
    1548             : 
    1549         199 :     *got_frame_ptr = 1;
    1550             : 
    1551         199 :     return !s->samples ? avpkt->size : 0;
    1552             : }
    1553             : 
    1554           0 : static void ape_flush(AVCodecContext *avctx)
    1555             : {
    1556           0 :     APEContext *s = avctx->priv_data;
    1557           0 :     s->samples= 0;
    1558           0 : }
    1559             : 
    1560             : #define OFFSET(x) offsetof(APEContext, x)
    1561             : #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
    1562             : static const AVOption options[] = {
    1563             :     { "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" },
    1564             :     { "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" },
    1565             :     { NULL},
    1566             : };
    1567             : 
    1568             : static const AVClass ape_decoder_class = {
    1569             :     .class_name = "APE decoder",
    1570             :     .item_name  = av_default_item_name,
    1571             :     .option     = options,
    1572             :     .version    = LIBAVUTIL_VERSION_INT,
    1573             : };
    1574             : 
    1575             : AVCodec ff_ape_decoder = {
    1576             :     .name           = "ape",
    1577             :     .long_name      = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
    1578             :     .type           = AVMEDIA_TYPE_AUDIO,
    1579             :     .id             = AV_CODEC_ID_APE,
    1580             :     .priv_data_size = sizeof(APEContext),
    1581             :     .init           = ape_decode_init,
    1582             :     .close          = ape_decode_close,
    1583             :     .decode         = ape_decode_frame,
    1584             :     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY |
    1585             :                       AV_CODEC_CAP_DR1,
    1586             :     .flush          = ape_flush,
    1587             :     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
    1588             :                                                       AV_SAMPLE_FMT_S16P,
    1589             :                                                       AV_SAMPLE_FMT_S32P,
    1590             :                                                       AV_SAMPLE_FMT_NONE },
    1591             :     .priv_class     = &ape_decoder_class,
    1592             : };

Generated by: LCOV version 1.12