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