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