]> git.sesse.net Git - ffmpeg/blob - libavcodec/apedec.c
16l trocadero: don't forget to free frame data buffer in APE decoder
[ffmpeg] / libavcodec / apedec.c
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 #define ALT_BITSTREAM_READER_LE
24 #include "avcodec.h"
25 #include "dsputil.h"
26 #include "get_bits.h"
27 #include "bytestream.h"
28
29 /**
30  * @file libavcodec/apedec.c
31  * Monkey's Audio lossless audio decoder
32  */
33
34 #define BLOCKS_PER_LOOP     4608
35 #define MAX_CHANNELS        2
36 #define MAX_BYTESPERSAMPLE  3
37
38 #define APE_FRAMECODE_MONO_SILENCE    1
39 #define APE_FRAMECODE_STEREO_SILENCE  3
40 #define APE_FRAMECODE_PSEUDO_STEREO   4
41
42 #define HISTORY_SIZE 512
43 #define PREDICTOR_ORDER 8
44 /** Total size of all predictor histories */
45 #define PREDICTOR_SIZE 50
46
47 #define YDELAYA (18 + PREDICTOR_ORDER*4)
48 #define YDELAYB (18 + PREDICTOR_ORDER*3)
49 #define XDELAYA (18 + PREDICTOR_ORDER*2)
50 #define XDELAYB (18 + PREDICTOR_ORDER)
51
52 #define YADAPTCOEFFSA 18
53 #define XADAPTCOEFFSA 14
54 #define YADAPTCOEFFSB 10
55 #define XADAPTCOEFFSB 5
56
57 /**
58  * Possible compression levels
59  * @{
60  */
61 enum APECompressionLevel {
62     COMPRESSION_LEVEL_FAST       = 1000,
63     COMPRESSION_LEVEL_NORMAL     = 2000,
64     COMPRESSION_LEVEL_HIGH       = 3000,
65     COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
66     COMPRESSION_LEVEL_INSANE     = 5000
67 };
68 /** @} */
69
70 #define APE_FILTER_LEVELS 3
71
72 /** Filter orders depending on compression level */
73 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
74     {  0,   0,    0 },
75     { 16,   0,    0 },
76     { 64,   0,    0 },
77     { 32, 256,    0 },
78     { 16, 256, 1280 }
79 };
80
81 /** Filter fraction bits depending on compression level */
82 static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
83     {  0,  0,  0 },
84     { 11,  0,  0 },
85     { 11,  0,  0 },
86     { 10, 13,  0 },
87     { 11, 13, 15 }
88 };
89
90
91 /** Filters applied to the decoded data */
92 typedef struct APEFilter {
93     int16_t *coeffs;        ///< actual coefficients used in filtering
94     int16_t *adaptcoeffs;   ///< adaptive filter coefficients used for correcting of actual filter coefficients
95     int16_t *historybuffer; ///< filter memory
96     int16_t *delay;         ///< filtered values
97
98     int avg;
99 } APEFilter;
100
101 typedef struct APERice {
102     uint32_t k;
103     uint32_t ksum;
104 } APERice;
105
106 typedef struct APERangecoder {
107     uint32_t low;           ///< low end of interval
108     uint32_t range;         ///< length of interval
109     uint32_t help;          ///< bytes_to_follow resp. intermediate value
110     unsigned int buffer;    ///< buffer for input/output
111 } APERangecoder;
112
113 /** Filter histories */
114 typedef struct APEPredictor {
115     int32_t *buf;
116
117     int32_t lastA[2];
118
119     int32_t filterA[2];
120     int32_t filterB[2];
121
122     int32_t coeffsA[2][4];  ///< adaption coefficients
123     int32_t coeffsB[2][5];  ///< adaption coefficients
124     int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
125 } APEPredictor;
126
127 /** Decoder context */
128 typedef struct APEContext {
129     AVCodecContext *avctx;
130     DSPContext dsp;
131     int channels;
132     int samples;                             ///< samples left to decode in current frame
133
134     int fileversion;                         ///< codec version, very important in decoding process
135     int compression_level;                   ///< compression levels
136     int fset;                                ///< which filter set to use (calculated from compression level)
137     int flags;                               ///< global decoder flags
138
139     uint32_t CRC;                            ///< frame CRC
140     int frameflags;                          ///< frame flags
141     int currentframeblocks;                  ///< samples (per channel) in current frame
142     int blocksdecoded;                       ///< count of decoded samples in current frame
143     APEPredictor predictor;                  ///< predictor used for final reconstruction
144
145     int32_t decoded0[BLOCKS_PER_LOOP];       ///< decoded data for the first channel
146     int32_t decoded1[BLOCKS_PER_LOOP];       ///< decoded data for the second channel
147
148     int16_t* filterbuf[APE_FILTER_LEVELS];   ///< filter memory
149
150     APERangecoder rc;                        ///< rangecoder used to decode actual values
151     APERice riceX;                           ///< rice code parameters for the second channel
152     APERice riceY;                           ///< rice code parameters for the first channel
153     APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
154
155     uint8_t *data;                           ///< current frame data
156     uint8_t *data_end;                       ///< frame data end
157     const uint8_t *ptr;                      ///< current position in frame data
158     const uint8_t *last_ptr;                 ///< position where last 4608-sample block ended
159
160     int error;
161 } APEContext;
162
163 // TODO: dsputilize
164
165 static av_cold int ape_decode_init(AVCodecContext * avctx)
166 {
167     APEContext *s = avctx->priv_data;
168     int i;
169
170     if (avctx->extradata_size != 6) {
171         av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
172         return -1;
173     }
174     if (avctx->bits_per_coded_sample != 16) {
175         av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n");
176         return -1;
177     }
178     if (avctx->channels > 2) {
179         av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
180         return -1;
181     }
182     s->avctx             = avctx;
183     s->channels          = avctx->channels;
184     s->fileversion       = AV_RL16(avctx->extradata);
185     s->compression_level = AV_RL16(avctx->extradata + 2);
186     s->flags             = AV_RL16(avctx->extradata + 4);
187
188     av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags);
189     if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) {
190         av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level);
191         return -1;
192     }
193     s->fset = s->compression_level / 1000 - 1;
194     for (i = 0; i < APE_FILTER_LEVELS; i++) {
195         if (!ape_filter_orders[s->fset][i])
196             break;
197         s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4);
198     }
199
200     dsputil_init(&s->dsp, avctx);
201     avctx->sample_fmt = SAMPLE_FMT_S16;
202     avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
203     return 0;
204 }
205
206 static av_cold int ape_decode_close(AVCodecContext * avctx)
207 {
208     APEContext *s = avctx->priv_data;
209     int i;
210
211     for (i = 0; i < APE_FILTER_LEVELS; i++)
212         av_freep(&s->filterbuf[i]);
213
214     av_freep(&s->data);
215     return 0;
216 }
217
218 /**
219  * @defgroup rangecoder APE range decoder
220  * @{
221  */
222
223 #define CODE_BITS    32
224 #define TOP_VALUE    ((unsigned int)1 << (CODE_BITS-1))
225 #define SHIFT_BITS   (CODE_BITS - 9)
226 #define EXTRA_BITS   ((CODE_BITS-2) % 8 + 1)
227 #define BOTTOM_VALUE (TOP_VALUE >> 8)
228
229 /** Start the decoder */
230 static inline void range_start_decoding(APEContext * ctx)
231 {
232     ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
233     ctx->rc.low    = ctx->rc.buffer >> (8 - EXTRA_BITS);
234     ctx->rc.range  = (uint32_t) 1 << EXTRA_BITS;
235 }
236
237 /** Perform normalization */
238 static inline void range_dec_normalize(APEContext * ctx)
239 {
240     while (ctx->rc.range <= BOTTOM_VALUE) {
241         ctx->rc.buffer <<= 8;
242         if(ctx->ptr < ctx->data_end)
243             ctx->rc.buffer += *ctx->ptr;
244         ctx->ptr++;
245         ctx->rc.low    = (ctx->rc.low << 8)    | ((ctx->rc.buffer >> 1) & 0xFF);
246         ctx->rc.range  <<= 8;
247     }
248 }
249
250 /**
251  * Calculate culmulative frequency for next symbol. Does NO update!
252  * @param ctx decoder context
253  * @param tot_f is the total frequency or (code_value)1<<shift
254  * @return the culmulative frequency
255  */
256 static inline int range_decode_culfreq(APEContext * ctx, int tot_f)
257 {
258     range_dec_normalize(ctx);
259     ctx->rc.help = ctx->rc.range / tot_f;
260     return ctx->rc.low / ctx->rc.help;
261 }
262
263 /**
264  * Decode value with given size in bits
265  * @param ctx decoder context
266  * @param shift number of bits to decode
267  */
268 static inline int range_decode_culshift(APEContext * ctx, int shift)
269 {
270     range_dec_normalize(ctx);
271     ctx->rc.help = ctx->rc.range >> shift;
272     return ctx->rc.low / ctx->rc.help;
273 }
274
275
276 /**
277  * Update decoding state
278  * @param ctx decoder context
279  * @param sy_f the interval length (frequency of the symbol)
280  * @param lt_f the lower end (frequency sum of < symbols)
281  */
282 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f)
283 {
284     ctx->rc.low  -= ctx->rc.help * lt_f;
285     ctx->rc.range = ctx->rc.help * sy_f;
286 }
287
288 /** Decode n bits (n <= 16) without modelling */
289 static inline int range_decode_bits(APEContext * ctx, int n)
290 {
291     int sym = range_decode_culshift(ctx, n);
292     range_decode_update(ctx, 1, sym);
293     return sym;
294 }
295
296
297 #define MODEL_ELEMENTS 64
298
299 /**
300  * Fixed probabilities for symbols in Monkey Audio version 3.97
301  */
302 static const uint16_t counts_3970[22] = {
303         0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
304     62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
305     65450, 65469, 65480, 65487, 65491, 65493,
306 };
307
308 /**
309  * Probability ranges for symbols in Monkey Audio version 3.97
310  */
311 static const uint16_t counts_diff_3970[21] = {
312     14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
313     1104, 677, 415, 248, 150, 89, 54, 31,
314     19, 11, 7, 4, 2,
315 };
316
317 /**
318  * Fixed probabilities for symbols in Monkey Audio version 3.98
319  */
320 static const uint16_t counts_3980[22] = {
321         0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
322     64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
323     65485, 65488, 65490, 65491, 65492, 65493,
324 };
325
326 /**
327  * Probability ranges for symbols in Monkey Audio version 3.98
328  */
329 static const uint16_t counts_diff_3980[21] = {
330     19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
331     261, 119, 65, 31, 19, 10, 6, 3,
332     3, 2, 1, 1, 1,
333 };
334
335 /**
336  * Decode symbol
337  * @param ctx decoder context
338  * @param counts probability range start position
339  * @param counts_diff probability range widths
340  */
341 static inline int range_get_symbol(APEContext * ctx,
342                                    const uint16_t counts[],
343                                    const uint16_t counts_diff[])
344 {
345     int symbol, cf;
346
347     cf = range_decode_culshift(ctx, 16);
348
349     if(cf > 65492){
350         symbol= cf - 65535 + 63;
351         range_decode_update(ctx, 1, cf);
352         if(cf > 65535)
353             ctx->error=1;
354         return symbol;
355     }
356     /* figure out the symbol inefficiently; a binary search would be much better */
357     for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
358
359     range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
360
361     return symbol;
362 }
363 /** @} */ // group rangecoder
364
365 static inline void update_rice(APERice *rice, int x)
366 {
367     int lim = rice->k ? (1 << (rice->k + 4)) : 0;
368     rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
369
370     if (rice->ksum < lim)
371         rice->k--;
372     else if (rice->ksum >= (1 << (rice->k + 5)))
373         rice->k++;
374 }
375
376 static inline int ape_decode_value(APEContext * ctx, APERice *rice)
377 {
378     int x, overflow;
379
380     if (ctx->fileversion < 3990) {
381         int tmpk;
382
383         overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
384
385         if (overflow == (MODEL_ELEMENTS - 1)) {
386             tmpk = range_decode_bits(ctx, 5);
387             overflow = 0;
388         } else
389             tmpk = (rice->k < 1) ? 0 : rice->k - 1;
390
391         if (tmpk <= 16)
392             x = range_decode_bits(ctx, tmpk);
393         else {
394             x = range_decode_bits(ctx, 16);
395             x |= (range_decode_bits(ctx, tmpk - 16) << 16);
396         }
397         x += overflow << tmpk;
398     } else {
399         int base, pivot;
400
401         pivot = rice->ksum >> 5;
402         if (pivot == 0)
403             pivot = 1;
404
405         overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
406
407         if (overflow == (MODEL_ELEMENTS - 1)) {
408             overflow  = range_decode_bits(ctx, 16) << 16;
409             overflow |= range_decode_bits(ctx, 16);
410         }
411
412         if (pivot < 0x10000) {
413             base = range_decode_culfreq(ctx, pivot);
414             range_decode_update(ctx, 1, base);
415         } else {
416             int base_hi = pivot, base_lo;
417             int bbits = 0;
418
419             while (base_hi & ~0xFFFF) {
420                 base_hi >>= 1;
421                 bbits++;
422             }
423             base_hi = range_decode_culfreq(ctx, base_hi + 1);
424             range_decode_update(ctx, 1, base_hi);
425             base_lo = range_decode_culfreq(ctx, 1 << bbits);
426             range_decode_update(ctx, 1, base_lo);
427
428             base = (base_hi << bbits) + base_lo;
429         }
430
431         x = base + overflow * pivot;
432     }
433
434     update_rice(rice, x);
435
436     /* Convert to signed */
437     if (x & 1)
438         return (x >> 1) + 1;
439     else
440         return -(x >> 1);
441 }
442
443 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo)
444 {
445     int32_t *decoded0 = ctx->decoded0;
446     int32_t *decoded1 = ctx->decoded1;
447
448     ctx->blocksdecoded = blockstodecode;
449
450     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
451         /* We are pure silence, just memset the output buffer. */
452         memset(decoded0, 0, blockstodecode * sizeof(int32_t));
453         memset(decoded1, 0, blockstodecode * sizeof(int32_t));
454     } else {
455         while (blockstodecode--) {
456             *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
457             if (stereo)
458                 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
459         }
460     }
461
462     if (ctx->blocksdecoded == ctx->currentframeblocks)
463         range_dec_normalize(ctx);   /* normalize to use up all bytes */
464 }
465
466 static void init_entropy_decoder(APEContext * ctx)
467 {
468     /* Read the CRC */
469     ctx->CRC = bytestream_get_be32(&ctx->ptr);
470
471     /* Read the frame flags if they exist */
472     ctx->frameflags = 0;
473     if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
474         ctx->CRC &= ~0x80000000;
475
476         ctx->frameflags = bytestream_get_be32(&ctx->ptr);
477     }
478
479     /* Keep a count of the blocks decoded in this frame */
480     ctx->blocksdecoded = 0;
481
482     /* Initialize the rice structs */
483     ctx->riceX.k = 10;
484     ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
485     ctx->riceY.k = 10;
486     ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
487
488     /* The first 8 bits of input are ignored. */
489     ctx->ptr++;
490
491     range_start_decoding(ctx);
492 }
493
494 static const int32_t initial_coeffs[4] = {
495     360, 317, -109, 98
496 };
497
498 static void init_predictor_decoder(APEContext * ctx)
499 {
500     APEPredictor *p = &ctx->predictor;
501
502     /* Zero the history buffers */
503     memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
504     p->buf = p->historybuffer;
505
506     /* Initialize and zero the coefficients */
507     memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
508     memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
509     memset(p->coeffsB, 0, sizeof(p->coeffsB));
510
511     p->filterA[0] = p->filterA[1] = 0;
512     p->filterB[0] = p->filterB[1] = 0;
513     p->lastA[0]   = p->lastA[1]   = 0;
514 }
515
516 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
517 static inline int APESIGN(int32_t x) {
518     return (x < 0) - (x > 0);
519 }
520
521 static av_always_inline int predictor_update_filter(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
522 {
523     int32_t predictionA, predictionB, sign;
524
525     p->buf[delayA]     = p->lastA[filter];
526     p->buf[adaptA]     = APESIGN(p->buf[delayA]);
527     p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
528     p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
529
530     predictionA = p->buf[delayA    ] * p->coeffsA[filter][0] +
531                   p->buf[delayA - 1] * p->coeffsA[filter][1] +
532                   p->buf[delayA - 2] * p->coeffsA[filter][2] +
533                   p->buf[delayA - 3] * p->coeffsA[filter][3];
534
535     /*  Apply a scaled first-order filter compression */
536     p->buf[delayB]     = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
537     p->buf[adaptB]     = APESIGN(p->buf[delayB]);
538     p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
539     p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
540     p->filterB[filter] = p->filterA[filter ^ 1];
541
542     predictionB = p->buf[delayB    ] * p->coeffsB[filter][0] +
543                   p->buf[delayB - 1] * p->coeffsB[filter][1] +
544                   p->buf[delayB - 2] * p->coeffsB[filter][2] +
545                   p->buf[delayB - 3] * p->coeffsB[filter][3] +
546                   p->buf[delayB - 4] * p->coeffsB[filter][4];
547
548     p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
549     p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
550
551     sign = APESIGN(decoded);
552     p->coeffsA[filter][0] += p->buf[adaptA    ] * sign;
553     p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
554     p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
555     p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
556     p->coeffsB[filter][0] += p->buf[adaptB    ] * sign;
557     p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
558     p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
559     p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
560     p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
561
562     return p->filterA[filter];
563 }
564
565 static void predictor_decode_stereo(APEContext * ctx, int count)
566 {
567     APEPredictor *p = &ctx->predictor;
568     int32_t *decoded0 = ctx->decoded0;
569     int32_t *decoded1 = ctx->decoded1;
570
571     while (count--) {
572         /* Predictor Y */
573         *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB);
574         decoded0++;
575         *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB);
576         decoded1++;
577
578         /* Combined */
579         p->buf++;
580
581         /* Have we filled the history buffer? */
582         if (p->buf == p->historybuffer + HISTORY_SIZE) {
583             memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
584             p->buf = p->historybuffer;
585         }
586     }
587 }
588
589 static void predictor_decode_mono(APEContext * ctx, int count)
590 {
591     APEPredictor *p = &ctx->predictor;
592     int32_t *decoded0 = ctx->decoded0;
593     int32_t predictionA, currentA, A, sign;
594
595     currentA = p->lastA[0];
596
597     while (count--) {
598         A = *decoded0;
599
600         p->buf[YDELAYA] = currentA;
601         p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
602
603         predictionA = p->buf[YDELAYA    ] * p->coeffsA[0][0] +
604                       p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
605                       p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
606                       p->buf[YDELAYA - 3] * p->coeffsA[0][3];
607
608         currentA = A + (predictionA >> 10);
609
610         p->buf[YADAPTCOEFFSA]     = APESIGN(p->buf[YDELAYA    ]);
611         p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
612
613         sign = APESIGN(A);
614         p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA    ] * sign;
615         p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
616         p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
617         p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
618
619         p->buf++;
620
621         /* Have we filled the history buffer? */
622         if (p->buf == p->historybuffer + HISTORY_SIZE) {
623             memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
624             p->buf = p->historybuffer;
625         }
626
627         p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
628         *(decoded0++) = p->filterA[0];
629     }
630
631     p->lastA[0] = currentA;
632 }
633
634 static void do_init_filter(APEFilter *f, int16_t * buf, int order)
635 {
636     f->coeffs = buf;
637     f->historybuffer = buf + order;
638     f->delay       = f->historybuffer + order * 2;
639     f->adaptcoeffs = f->historybuffer + order;
640
641     memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
642     memset(f->coeffs, 0, order * sizeof(int16_t));
643     f->avg = 0;
644 }
645
646 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order)
647 {
648     do_init_filter(&f[0], buf, order);
649     do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
650 }
651
652 static void do_apply_filter(APEContext * ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
653 {
654     int res;
655     int absres;
656
657     while (count--) {
658         /* round fixedpoint scalar product */
659         res = ctx->dsp.scalarproduct_and_madd_int16(f->coeffs, f->delay - order, f->adaptcoeffs - order, order, APESIGN(*data));
660         res = (res + (1 << (fracbits - 1))) >> fracbits;
661         res += *data;
662         *data++ = res;
663
664         /* Update the output history */
665         *f->delay++ = av_clip_int16(res);
666
667         if (version < 3980) {
668             /* Version ??? to < 3.98 files (untested) */
669             f->adaptcoeffs[0]  = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
670             f->adaptcoeffs[-4] >>= 1;
671             f->adaptcoeffs[-8] >>= 1;
672         } else {
673             /* Version 3.98 and later files */
674
675             /* Update the adaption coefficients */
676             absres = FFABS(res);
677             if (absres)
678                 *f->adaptcoeffs = ((res & (1<<31)) - (1<<30)) >> (25 + (absres <= f->avg*3) + (absres <= f->avg*4/3));
679             else
680                 *f->adaptcoeffs = 0;
681
682             f->avg += (absres - f->avg) / 16;
683
684             f->adaptcoeffs[-1] >>= 1;
685             f->adaptcoeffs[-2] >>= 1;
686             f->adaptcoeffs[-8] >>= 1;
687         }
688
689         f->adaptcoeffs++;
690
691         /* Have we filled the history buffer? */
692         if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
693             memmove(f->historybuffer, f->delay - (order * 2),
694                     (order * 2) * sizeof(int16_t));
695             f->delay = f->historybuffer + order * 2;
696             f->adaptcoeffs = f->historybuffer + order;
697         }
698     }
699 }
700
701 static void apply_filter(APEContext * ctx, APEFilter *f,
702                          int32_t * data0, int32_t * data1,
703                          int count, int order, int fracbits)
704 {
705     do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
706     if (data1)
707         do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
708 }
709
710 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0,
711                               int32_t * decoded1, int count)
712 {
713     int i;
714
715     for (i = 0; i < APE_FILTER_LEVELS; i++) {
716         if (!ape_filter_orders[ctx->fset][i])
717             break;
718         apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
719     }
720 }
721
722 static void init_frame_decoder(APEContext * ctx)
723 {
724     int i;
725     init_entropy_decoder(ctx);
726     init_predictor_decoder(ctx);
727
728     for (i = 0; i < APE_FILTER_LEVELS; i++) {
729         if (!ape_filter_orders[ctx->fset][i])
730             break;
731         init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
732     }
733 }
734
735 static void ape_unpack_mono(APEContext * ctx, int count)
736 {
737     int32_t left;
738     int32_t *decoded0 = ctx->decoded0;
739     int32_t *decoded1 = ctx->decoded1;
740
741     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
742         entropy_decode(ctx, count, 0);
743         /* We are pure silence, so we're done. */
744         av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
745         return;
746     }
747
748     entropy_decode(ctx, count, 0);
749     ape_apply_filters(ctx, decoded0, NULL, count);
750
751     /* Now apply the predictor decoding */
752     predictor_decode_mono(ctx, count);
753
754     /* Pseudo-stereo - just copy left channel to right channel */
755     if (ctx->channels == 2) {
756         while (count--) {
757             left = *decoded0;
758             *(decoded1++) = *(decoded0++) = left;
759         }
760     }
761 }
762
763 static void ape_unpack_stereo(APEContext * ctx, int count)
764 {
765     int32_t left, right;
766     int32_t *decoded0 = ctx->decoded0;
767     int32_t *decoded1 = ctx->decoded1;
768
769     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
770         /* We are pure silence, so we're done. */
771         av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
772         return;
773     }
774
775     entropy_decode(ctx, count, 1);
776     ape_apply_filters(ctx, decoded0, decoded1, count);
777
778     /* Now apply the predictor decoding */
779     predictor_decode_stereo(ctx, count);
780
781     /* Decorrelate and scale to output depth */
782     while (count--) {
783         left = *decoded1 - (*decoded0 / 2);
784         right = left + *decoded0;
785
786         *(decoded0++) = left;
787         *(decoded1++) = right;
788     }
789 }
790
791 static int ape_decode_frame(AVCodecContext * avctx,
792                             void *data, int *data_size,
793                             AVPacket *avpkt)
794 {
795     const uint8_t *buf = avpkt->data;
796     int buf_size = avpkt->size;
797     APEContext *s = avctx->priv_data;
798     int16_t *samples = data;
799     int nblocks;
800     int i, n;
801     int blockstodecode;
802     int bytes_used;
803
804     if (buf_size == 0 && !s->samples) {
805         *data_size = 0;
806         return 0;
807     }
808
809     /* should not happen but who knows */
810     if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {
811         av_log (avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\n", *data_size, s->samples * 2 * avctx->channels);
812         return -1;
813     }
814
815     if(!s->samples){
816         s->data = av_realloc(s->data, (buf_size + 3) & ~3);
817         s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
818         s->ptr = s->last_ptr = s->data;
819         s->data_end = s->data + buf_size;
820
821         nblocks = s->samples = bytestream_get_be32(&s->ptr);
822         n =  bytestream_get_be32(&s->ptr);
823         if(n < 0 || n > 3){
824             av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
825             s->data = NULL;
826             return -1;
827         }
828         s->ptr += n;
829
830         s->currentframeblocks = nblocks;
831         buf += 4;
832         if (s->samples <= 0) {
833             *data_size = 0;
834             return buf_size;
835         }
836
837         memset(s->decoded0,  0, sizeof(s->decoded0));
838         memset(s->decoded1,  0, sizeof(s->decoded1));
839
840         /* Initialize the frame decoder */
841         init_frame_decoder(s);
842     }
843
844     if (!s->data) {
845         *data_size = 0;
846         return buf_size;
847     }
848
849     nblocks = s->samples;
850     blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);
851
852     s->error=0;
853
854     if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
855         ape_unpack_mono(s, blockstodecode);
856     else
857         ape_unpack_stereo(s, blockstodecode);
858     emms_c();
859
860     if(s->error || s->ptr > s->data_end){
861         s->samples=0;
862         av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
863         return -1;
864     }
865
866     for (i = 0; i < blockstodecode; i++) {
867         *samples++ = s->decoded0[i];
868         if(s->channels == 2)
869             *samples++ = s->decoded1[i];
870     }
871
872     s->samples -= blockstodecode;
873
874     *data_size = blockstodecode * 2 * s->channels;
875     bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;
876     s->last_ptr = s->ptr;
877     return bytes_used;
878 }
879
880 AVCodec ape_decoder = {
881     "ape",
882     CODEC_TYPE_AUDIO,
883     CODEC_ID_APE,
884     sizeof(APEContext),
885     ape_decode_init,
886     NULL,
887     ape_decode_close,
888     ape_decode_frame,
889     .capabilities = CODEC_CAP_SUBFRAMES,
890     .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
891 };