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