FFmpeg
Loading...
Searching...
No Matches
apedec.c
Go to the documentation of this file.
1/*
2 * Monkey's Audio lossless audio decoder
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 * based upon libdemac from Dave Chapman.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <inttypes.h>
24
26#include "libavutil/avassert.h"
28#include "libavutil/crc.h"
29#include "libavutil/mem.h"
30#include "libavutil/opt.h"
31#include "lossless_audiodsp.h"
32#include "avcodec.h"
33#include "bswapdsp.h"
34#include "bytestream.h"
35#include "codec_internal.h"
36#include "decode.h"
37#include "get_bits.h"
38#include "unary.h"
39
40/**
41 * @file
42 * Monkey's Audio lossless audio decoder
43 */
44
45#define MAX_CHANNELS 2
46#define MAX_BYTESPERSAMPLE 3
47
48#define APE_FRAMECODE_MONO_SILENCE 1
49#define APE_FRAMECODE_STEREO_SILENCE 3
50#define APE_FRAMECODE_PSEUDO_STEREO 4
51
52#define HISTORY_SIZE 512
53#define PREDICTOR_ORDER 8
54/** Total size of all predictor histories */
55#define PREDICTOR_SIZE 50
56
57#define YDELAYA (18 + PREDICTOR_ORDER*4)
58#define YDELAYB (18 + PREDICTOR_ORDER*3)
59#define XDELAYA (18 + PREDICTOR_ORDER*2)
60#define XDELAYB (18 + PREDICTOR_ORDER)
61
62#define YADAPTCOEFFSA 18
63#define XADAPTCOEFFSA 14
64#define YADAPTCOEFFSB 10
65#define XADAPTCOEFFSB 5
66
67/**
68 * Possible compression levels
69 * @{
70 */
78/** @} */
79
80#define APE_FILTER_LEVELS 3
81
82/** Filter orders depending on compression level */
83static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
84 { 0, 0, 0 },
85 { 16, 0, 0 },
86 { 64, 0, 0 },
87 { 32, 256, 0 },
88 { 16, 256, 1280 }
89};
90
91/** Filter fraction bits depending on compression level */
92static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
93 { 0, 0, 0 },
94 { 11, 0, 0 },
95 { 11, 0, 0 },
96 { 10, 13, 0 },
97 { 11, 13, 15 }
98};
99
100
101/** Filters applied to the decoded data */
102typedef struct APEFilter {
103 int16_t *coeffs; ///< actual coefficients used in filtering
104 int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients
105 int16_t *historybuffer; ///< filter memory
106 int16_t *delay; ///< filtered values
107
108 uint32_t avg;
109} APEFilter;
110
111typedef struct APERice {
112 uint32_t k;
113 uint32_t ksum;
114} APERice;
115
116typedef struct APERangecoder {
117 uint32_t low; ///< low end of interval
118 uint32_t range; ///< length of interval
119 uint32_t help; ///< bytes_to_follow resp. intermediate value
120 unsigned int buffer; ///< buffer for input/output
122
123/** Filter histories */
124typedef struct APEPredictor {
126
128
131
132 uint32_t coeffsA[2][4]; ///< adaption coefficients
133 uint32_t coeffsB[2][5]; ///< adaption coefficients
135
136 unsigned int sample_pos;
138
139typedef struct APEPredictor64 {
141
143
146
147 uint64_t coeffsA[2][4]; ///< adaption coefficients
148 uint64_t coeffsB[2][5]; ///< adaption coefficients
151
152/** Decoder context */
153typedef struct APEContext {
154 AVClass *class; ///< class for AVOptions
159 int samples; ///< samples left to decode in current frame
160 int bps;
161
162 int fileversion; ///< codec version, very important in decoding process
163 int compression_level; ///< compression levels
164 int fset; ///< which filter set to use (calculated from compression level)
165 int flags; ///< global decoder flags
166
167 uint32_t CRC; ///< signalled frame CRC
168 uint32_t CRC_state; ///< accumulated CRC
169 int frameflags; ///< frame flags
170 APEPredictor predictor; ///< predictor used for final reconstruction
171 APEPredictor64 predictor64; ///< 64bit predictor used for final reconstruction
172
175 int32_t *decoded[MAX_CHANNELS]; ///< decoded data for each channel
178 int32_t *interim[MAX_CHANNELS]; ///< decoded data for each channel
179 int blocks_per_loop; ///< maximum number of samples to decode for each call
180
181 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
182
183 APERangecoder rc; ///< rangecoder used to decode actual values
184 APERice riceX; ///< rice code parameters for the second channel
185 APERice riceY; ///< rice code parameters for the first channel
186 APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
188
189 uint8_t *data; ///< current frame data
190 uint8_t *data_end; ///< frame data end
191 int data_size; ///< frame data allocated size
192 const uint8_t *ptr; ///< current position in frame data
193
194 int error;
196
197 void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
198 void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
199 void (*predictor_decode_mono)(struct APEContext *ctx, int count);
200 void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
201} APEContext;
202
203static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
204 int32_t *decoded1, int count);
205
206static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
207static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
208static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
209static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
210static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
211static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
212static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
213static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
214static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
215
216static void predictor_decode_mono_3800(APEContext *ctx, int count);
217static void predictor_decode_stereo_3800(APEContext *ctx, int count);
218static void predictor_decode_mono_3930(APEContext *ctx, int count);
219static void predictor_decode_stereo_3930(APEContext *ctx, int count);
220static void predictor_decode_mono_3950(APEContext *ctx, int count);
221static void predictor_decode_stereo_3950(APEContext *ctx, int count);
222
224{
226 int i;
227
228 for (i = 0; i < APE_FILTER_LEVELS; i++)
229 av_freep(&s->filterbuf[i]);
230
231 av_freep(&s->decoded_buffer);
232 av_freep(&s->interim_buffer);
233 av_freep(&s->data);
234 s->decoded_size = s->data_size = 0;
235
236 return 0;
237}
238
240{
243 int i;
244
245 if (avctx->extradata_size != 6) {
246 av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
247 return AVERROR(EINVAL);
248 }
249 if (channels > 2) {
250 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
251 return AVERROR(EINVAL);
252 }
255 switch (s->bps) {
256 case 8:
258 s->interim_mode = 0;
259 break;
260 case 16:
262 s->interim_mode = 0;
263 break;
264 case 24:
266 s->interim_mode = -1;
267 break;
268 default:
270 "%d bits per coded sample", s->bps);
272 }
273 s->avctx = avctx;
274 s->channels = channels;
275 s->fileversion = AV_RL16(avctx->extradata);
276 s->compression_level = AV_RL16(avctx->extradata + 2);
277 s->flags = AV_RL16(avctx->extradata + 4);
278
279 av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n",
280 s->compression_level, s->flags);
281 if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
282 !s->compression_level ||
283 (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
284 av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
285 s->compression_level);
286 return AVERROR_INVALIDDATA;
287 }
288 s->fset = s->compression_level / 1000 - 1;
289 for (i = 0; i < APE_FILTER_LEVELS; i++) {
290 if (!ape_filter_orders[s->fset][i])
291 break;
292 if (!(s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4)))
293 return AVERROR(ENOMEM);
294 }
295
296 if (s->fileversion < 3860) {
297 s->entropy_decode_mono = entropy_decode_mono_0000;
298 s->entropy_decode_stereo = entropy_decode_stereo_0000;
299 } else if (s->fileversion < 3900) {
300 s->entropy_decode_mono = entropy_decode_mono_3860;
301 s->entropy_decode_stereo = entropy_decode_stereo_3860;
302 } else if (s->fileversion < 3930) {
303 s->entropy_decode_mono = entropy_decode_mono_3900;
304 s->entropy_decode_stereo = entropy_decode_stereo_3900;
305 } else if (s->fileversion < 3990) {
306 s->entropy_decode_mono = entropy_decode_mono_3900;
307 s->entropy_decode_stereo = entropy_decode_stereo_3930;
308 } else {
309 s->entropy_decode_mono = entropy_decode_mono_3990;
310 s->entropy_decode_stereo = entropy_decode_stereo_3990;
311 }
312
313 if (s->fileversion < 3930) {
314 s->predictor_decode_mono = predictor_decode_mono_3800;
315 s->predictor_decode_stereo = predictor_decode_stereo_3800;
316 } else if (s->fileversion < 3950) {
317 s->predictor_decode_mono = predictor_decode_mono_3930;
318 s->predictor_decode_stereo = predictor_decode_stereo_3930;
319 } else {
320 s->predictor_decode_mono = predictor_decode_mono_3950;
321 s->predictor_decode_stereo = predictor_decode_stereo_3950;
322 }
323
324 ff_bswapdsp_init(&s->bdsp);
325 ff_llauddsp_init(&s->adsp);
329
330 return 0;
331}
332
333/**
334 * @name APE range decoding functions
335 * @{
336 */
337
338#define CODE_BITS 32
339#define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
340#define SHIFT_BITS (CODE_BITS - 9)
341#define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
342#define BOTTOM_VALUE (TOP_VALUE >> 8)
343
344/** Start the decoder */
346{
347 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
348 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
349 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
350}
351
352/** Perform normalization */
354{
355 while (ctx->rc.range <= BOTTOM_VALUE) {
356 ctx->rc.buffer <<= 8;
357 if(ctx->ptr < ctx->data_end) {
358 ctx->rc.buffer += *ctx->ptr;
359 ctx->ptr++;
360 } else {
361 ctx->error = 1;
362 }
363 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
364 ctx->rc.range <<= 8;
365 }
366}
367
368/**
369 * Calculate cumulative frequency for next symbol. Does NO update!
370 * @param ctx decoder context
371 * @param tot_f is the total frequency or (code_value)1<<shift
372 * @return the cumulative frequency
373 */
374static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
375{
377 ctx->rc.help = ctx->rc.range / tot_f;
378 return ctx->rc.low / ctx->rc.help;
379}
380
381/**
382 * Decode value with given size in bits
383 * @param ctx decoder context
384 * @param shift number of bits to decode
385 */
387{
389 ctx->rc.help = ctx->rc.range >> shift;
390 return ctx->rc.low / ctx->rc.help;
391}
392
393
394/**
395 * Update decoding state
396 * @param ctx decoder context
397 * @param sy_f the interval length (frequency of the symbol)
398 * @param lt_f the lower end (frequency sum of < symbols)
399 */
400static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
401{
402 ctx->rc.low -= ctx->rc.help * lt_f;
403 ctx->rc.range = ctx->rc.help * sy_f;
404}
405
406/** Decode n bits (n <= 16) without modelling */
407static inline int range_decode_bits(APEContext *ctx, int n)
408{
409 int sym = range_decode_culshift(ctx, n);
410 range_decode_update(ctx, 1, sym);
411 return sym;
412}
413
414
415#define MODEL_ELEMENTS 64
416
417/**
418 * Fixed probabilities for symbols in Monkey Audio version 3.97
419 */
420static const uint16_t counts_3970[22] = {
421 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
422 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
423 65450, 65469, 65480, 65487, 65491, 65493,
424};
425
426/**
427 * Probability ranges for symbols in Monkey Audio version 3.97
428 */
429static const uint16_t counts_diff_3970[21] = {
430 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
431 1104, 677, 415, 248, 150, 89, 54, 31,
432 19, 11, 7, 4, 2,
433};
434
435/**
436 * Fixed probabilities for symbols in Monkey Audio version 3.98
437 */
438static const uint16_t counts_3980[22] = {
439 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
440 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
441 65485, 65488, 65490, 65491, 65492, 65493,
442};
443
444/**
445 * Probability ranges for symbols in Monkey Audio version 3.98
446 */
447static const uint16_t counts_diff_3980[21] = {
448 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
449 261, 119, 65, 31, 19, 10, 6, 3,
450 3, 2, 1, 1, 1,
451};
452
453/**
454 * Decode symbol
455 * @param ctx decoder context
456 * @param counts probability range start position
457 * @param counts_diff probability range widths
458 */
459static inline int range_get_symbol(APEContext *ctx,
460 const uint16_t counts[],
461 const uint16_t counts_diff[])
462{
463 int symbol, cf;
464
465 cf = range_decode_culshift(ctx, 16);
466
467 if(cf > 65492){
468 symbol= cf - 65535 + 63;
469 range_decode_update(ctx, 1, cf);
470 if(cf > 65535)
471 ctx->error=1;
472 return symbol;
473 }
474 /* figure out the symbol inefficiently; a binary search would be much better */
475 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
476
477 range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
478
479 return symbol;
480}
481/** @} */ // group rangecoder
482
483static inline void update_rice(APERice *rice, unsigned int x)
484{
485 int lim = rice->k ? (1 << (rice->k + 4)) : 0;
486 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
487
488 if (rice->ksum < lim)
489 rice->k--;
490 else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
491 rice->k++;
492}
493
494static inline int get_rice_ook(GetBitContext *gb, int k)
495{
496 unsigned int x;
497
498 x = get_unary(gb, 1, get_bits_left(gb));
499
500 if (k)
501 x = (x << k) | get_bits(gb, k);
502
503 return x;
504}
505
507 APERice *rice)
508{
509 unsigned int x, overflow;
510
511 overflow = get_unary(gb, 1, get_bits_left(gb));
512
513 if (ctx->fileversion > 3880) {
514 while (overflow >= 16) {
515 overflow -= 16;
516 rice->k += 4;
517 }
518 }
519
520 if (!rice->k)
521 x = overflow;
522 else if(rice->k <= MIN_CACHE_BITS) {
523 x = (overflow << rice->k) + get_bits(gb, rice->k);
524 } else {
525 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %"PRIu32"\n", rice->k);
526 ctx->error = 1;
527 return AVERROR_INVALIDDATA;
528 }
529 rice->ksum += x - (rice->ksum + 8 >> 4);
530 if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
531 rice->k--;
532 else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
533 rice->k++;
534
535 /* Convert to signed */
536 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
537}
538
539static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
540{
541 unsigned int x, overflow;
542 int tmpk;
543
545
546 if (overflow == (MODEL_ELEMENTS - 1)) {
547 tmpk = range_decode_bits(ctx, 5);
548 overflow = 0;
549 } else
550 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
551
552 if (tmpk <= 16 || ctx->fileversion < 3910) {
553 if (tmpk > 23) {
554 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
555 return AVERROR_INVALIDDATA;
556 }
557 x = range_decode_bits(ctx, tmpk);
558 } else if (tmpk <= 31) {
559 x = range_decode_bits(ctx, 16);
560 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
561 } else {
562 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
563 return AVERROR_INVALIDDATA;
564 }
565 x += overflow << tmpk;
566
567 update_rice(rice, x);
568
569 /* Convert to signed */
570 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
571}
572
573static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
574{
575 unsigned int x, overflow, pivot;
576 int base;
577
578 pivot = FFMAX(rice->ksum >> 5, 1);
579
581
582 if (overflow == (MODEL_ELEMENTS - 1)) {
583 overflow = (unsigned)range_decode_bits(ctx, 16) << 16;
584 overflow |= range_decode_bits(ctx, 16);
585 }
586
587 if (pivot < 0x10000) {
588 base = range_decode_culfreq(ctx, pivot);
590 } else {
591 int base_hi = pivot, base_lo;
592 int bbits = 0;
593
594 while (base_hi & ~0xFFFF) {
595 base_hi >>= 1;
596 bbits++;
597 }
598 base_hi = range_decode_culfreq(ctx, base_hi + 1);
599 range_decode_update(ctx, 1, base_hi);
600 base_lo = range_decode_culfreq(ctx, 1 << bbits);
601 range_decode_update(ctx, 1, base_lo);
602
603 base = (base_hi << bbits) + base_lo;
604 }
605
606 x = base + overflow * pivot;
607
608 update_rice(rice, x);
609
610 /* Convert to signed */
611 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
612}
613
614static int get_k(int ksum)
615{
616 return av_log2(ksum) + !!ksum;
617}
618
620 int32_t *out, APERice *rice, int blockstodecode)
621{
622 int i;
623 unsigned ksummax, ksummin;
624
625 rice->ksum = 0;
626 for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
627 out[i] = get_rice_ook(&ctx->gb, 10);
628 rice->ksum += out[i];
629 }
630
631 if (blockstodecode <= 5)
632 goto end;
633
634 rice->k = get_k(rice->ksum / 10);
635 if (rice->k >= 24)
636 return;
637 for (; i < FFMIN(blockstodecode, 64); i++) {
638 out[i] = get_rice_ook(&ctx->gb, rice->k);
639 rice->ksum += out[i];
640 rice->k = get_k(rice->ksum / ((i + 1) * 2));
641 if (rice->k >= 24)
642 return;
643 }
644
645 if (blockstodecode <= 64)
646 goto end;
647
648 rice->k = get_k(rice->ksum >> 7);
649 ksummax = 1 << rice->k + 7;
650 ksummin = rice->k ? (1 << rice->k + 6) : 0;
651 for (; i < blockstodecode; i++) {
652 if (get_bits_left(&ctx->gb) < 1) {
653 ctx->error = 1;
654 return;
655 }
656 out[i] = get_rice_ook(&ctx->gb, rice->k);
657 rice->ksum += out[i] - (unsigned)out[i - 64];
658 while (rice->ksum < ksummin) {
659 rice->k--;
660 ksummin = rice->k ? ksummin >> 1 : 0;
661 ksummax >>= 1;
662 }
663 while (rice->ksum >= ksummax) {
664 rice->k++;
665 if (rice->k > 24)
666 return;
667 ksummax <<= 1;
668 ksummin = ksummin ? ksummin << 1 : 128;
669 }
670 }
671
672end:
673 for (i = 0; i < blockstodecode; i++)
674 out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
675}
676
677static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
678{
679 decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
680 blockstodecode);
681}
682
683static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
684{
685 decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
686 blockstodecode);
687 decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
688 blockstodecode);
689}
690
691static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
692{
693 int32_t *decoded0 = ctx->decoded[0];
694
695 while (blockstodecode--)
696 *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
697}
698
699static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
700{
701 int32_t *decoded0 = ctx->decoded[0];
702 int32_t *decoded1 = ctx->decoded[1];
703 int blocks = blockstodecode;
704
705 while (blockstodecode--)
706 *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
707 while (blocks--)
708 *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
709}
710
711static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
712{
713 int32_t *decoded0 = ctx->decoded[0];
714
715 while (blockstodecode--)
716 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
717}
718
719static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
720{
721 int32_t *decoded0 = ctx->decoded[0];
722 int32_t *decoded1 = ctx->decoded[1];
723 int blocks = blockstodecode;
724
725 while (blockstodecode--)
726 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
728 // because of some implementation peculiarities we need to backpedal here
729 ctx->ptr -= 1;
731 while (blocks--)
732 *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
733}
734
735static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
736{
737 int32_t *decoded0 = ctx->decoded[0];
738 int32_t *decoded1 = ctx->decoded[1];
739
740 while (blockstodecode--) {
741 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
742 *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
743 }
744}
745
746static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
747{
748 int32_t *decoded0 = ctx->decoded[0];
749
750 while (blockstodecode--)
751 *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
752}
753
754static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
755{
756 int32_t *decoded0 = ctx->decoded[0];
757 int32_t *decoded1 = ctx->decoded[1];
758
759 while (blockstodecode--) {
760 *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
761 *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
762 }
763}
764
766{
767 /* Read the CRC */
768 if (ctx->fileversion >= 3900) {
769 if (ctx->data_end - ctx->ptr < 6)
770 return AVERROR_INVALIDDATA;
771 ctx->CRC = bytestream_get_be32(&ctx->ptr);
772 } else {
773 ctx->CRC = get_bits_long(&ctx->gb, 32);
774 }
775
776 /* Read the frame flags if they exist */
777 ctx->frameflags = 0;
778 ctx->CRC_state = UINT32_MAX;
779 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
780 ctx->CRC &= ~0x80000000;
781
782 if (ctx->data_end - ctx->ptr < 6)
783 return AVERROR_INVALIDDATA;
784 ctx->frameflags = bytestream_get_be32(&ctx->ptr);
785 }
786
787 /* Initialize the rice structs */
788 ctx->riceX.k = 10;
789 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
790 ctx->riceY.k = 10;
791 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
792
793 if (ctx->fileversion >= 3900) {
794 /* The first 8 bits of input are ignored. */
795 ctx->ptr++;
796
798 }
799
800 return 0;
801}
802
804 375,
805};
806
808 64, 115, 64,
809};
810
812 740, 0
813};
814
815static const int32_t initial_coeffs_3930[4] = {
816 360, 317, -109, 98
817};
818
820 360, 317, -109, 98
821};
822
824{
825 APEPredictor *p = &ctx->predictor;
826 APEPredictor64 *p64 = &ctx->predictor64;
827
828 /* Zero the history buffers */
829 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
830 memset(p64->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p64->historybuffer));
831 p->buf = p->historybuffer;
832 p64->buf = p64->historybuffer;
833
834 /* Initialize and zero the coefficients */
835 if (ctx->fileversion < 3930) {
836 if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
837 memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
839 memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
841 } else {
842 memcpy(p->coeffsA[0], initial_coeffs_a_3800,
843 sizeof(initial_coeffs_a_3800));
844 memcpy(p->coeffsA[1], initial_coeffs_a_3800,
845 sizeof(initial_coeffs_a_3800));
846 }
847 } else {
848 memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
849 memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
852 }
853 memset(p->coeffsB, 0, sizeof(p->coeffsB));
854 memset(p64->coeffsB, 0, sizeof(p64->coeffsB));
855 if (ctx->fileversion < 3930) {
856 memcpy(p->coeffsB[0], initial_coeffs_b_3800,
857 sizeof(initial_coeffs_b_3800));
858 memcpy(p->coeffsB[1], initial_coeffs_b_3800,
859 sizeof(initial_coeffs_b_3800));
860 }
861
862 p->filterA[0] = p->filterA[1] = 0;
863 p->filterB[0] = p->filterB[1] = 0;
864 p->lastA[0] = p->lastA[1] = 0;
865
866 p64->filterA[0] = p64->filterA[1] = 0;
867 p64->filterB[0] = p64->filterB[1] = 0;
868 p64->lastA[0] = p64->lastA[1] = 0;
869
870 p->sample_pos = 0;
871}
872
873/** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
874static inline int APESIGN(int32_t x) {
875 return (x < 0) - (x > 0);
876}
877
879 const int decoded, const int filter,
880 const int delayA)
881{
882 int32_t predictionA;
883
884 p->buf[delayA] = p->lastA[filter];
885 if (p->sample_pos < 3) {
886 p->lastA[filter] = decoded;
887 p->filterA[filter] = decoded;
888 return decoded;
889 }
890
891 predictionA = p->buf[delayA] * 2U - p->buf[delayA - 1];
892 p->lastA[filter] = decoded + (unsigned)((int32_t)(predictionA * p->coeffsA[filter][0]) >> 9);
893
894 if ((decoded ^ predictionA) > 0)
895 p->coeffsA[filter][0]++;
896 else
897 p->coeffsA[filter][0]--;
898
899 p->filterA[filter] += (unsigned)p->lastA[filter];
900
901 return p->filterA[filter];
902}
903
905 const unsigned decoded, const int filter,
906 const int delayA, const int delayB,
907 const int start, const int shift)
908{
909 int32_t predictionA, predictionB, sign;
910 int32_t d0, d1, d2, d3, d4;
911
912 p->buf[delayA] = p->lastA[filter];
913 p->buf[delayB] = p->filterB[filter];
914 if (p->sample_pos < start) {
915 predictionA = decoded + p->filterA[filter];
916 p->lastA[filter] = decoded;
917 p->filterB[filter] = decoded;
918 p->filterA[filter] = predictionA;
919 return predictionA;
920 }
921 d2 = p->buf[delayA];
922 d1 = (p->buf[delayA] - (unsigned)p->buf[delayA - 1]) * 2;
923 d0 = p->buf[delayA] + ((p->buf[delayA - 2] - (unsigned)p->buf[delayA - 1]) * 8);
924 d3 = p->buf[delayB] * 2U - p->buf[delayB - 1];
925 d4 = p->buf[delayB];
926
927 predictionA = d0 * p->coeffsA[filter][0] +
928 d1 * p->coeffsA[filter][1] +
929 d2 * p->coeffsA[filter][2];
930
931 sign = APESIGN(decoded);
932 p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
933 p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
934 p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
935
936 predictionB = d3 * p->coeffsB[filter][0] -
937 d4 * p->coeffsB[filter][1];
938 p->lastA[filter] = decoded + (predictionA >> 11);
939 sign = APESIGN(p->lastA[filter]);
940 p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
941 p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
942
943 p->filterB[filter] = p->lastA[filter] + (unsigned)(predictionB >> shift);
944 p->filterA[filter] = p->filterB[filter] + (unsigned)((int)(p->filterA[filter] * 31U) >> 5);
945
946 return p->filterA[filter];
947}
948
949static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
950{
951 int i, j;
952 int32_t dotprod, sign;
953 int32_t coeffs[256], delay[256+256], *delayp = delay;
954
955 if (order >= length)
956 return;
957
958 memset(coeffs, 0, order * sizeof(*coeffs));
959 for (i = 0; i < order; i++)
960 delay[i] = buffer[i];
961 for (i = order; i < length; i++) {
962 dotprod = 0;
963 sign = APESIGN(buffer[i]);
964 if (sign == 1) {
965 for (j = 0; j < order; j++) {
966 dotprod += delayp[j] * (unsigned)coeffs[j];
967 coeffs[j] += (delayp[j] >> 31) | 1;
968 }
969 } else if (sign == -1) {
970 for (j = 0; j < order; j++) {
971 dotprod += delayp[j] * (unsigned)coeffs[j];
972 coeffs[j] -= (delayp[j] >> 31) | 1;
973 }
974 } else {
975 for (j = 0; j < order; j++) {
976 dotprod += delayp[j] * (unsigned)coeffs[j];
977 }
978 }
979 buffer[i] -= (unsigned)(dotprod >> shift);
980 delayp ++;
981 delayp[order - 1] = buffer[i];
982 if (delayp - delay == 256) {
983 memcpy(delay, delayp, sizeof(*delay)*256);
984 delayp = delay;
985 }
986 }
987}
988
989static void long_filter_ehigh_3830(int32_t *buffer, int length)
990{
991 int i, j;
992 int32_t dotprod, sign;
993 int32_t delay[8] = { 0 };
994 uint32_t coeffs[8] = { 0 };
995
996 for (i = 0; i < length; i++) {
997 dotprod = 0;
998 sign = APESIGN(buffer[i]);
999 for (j = 7; j >= 0; j--) {
1000 dotprod += delay[j] * coeffs[j];
1001 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
1002 }
1003 for (j = 7; j > 0; j--)
1004 delay[j] = delay[j - 1];
1005 delay[0] = buffer[i];
1006 buffer[i] -= (unsigned)(dotprod >> 9);
1007 }
1008}
1009
1011{
1012 APEPredictor *p = &ctx->predictor;
1013 int32_t *decoded0 = ctx->decoded[0];
1014 int32_t *decoded1 = ctx->decoded[1];
1015 int start = 4, shift = 10;
1016
1017 if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
1018 start = 16;
1019 long_filter_high_3800(decoded0, 16, 9, count);
1020 long_filter_high_3800(decoded1, 16, 9, count);
1021 } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1022 int order = 128, shift2 = 11;
1023
1024 if (ctx->fileversion >= 3830) {
1025 order <<= 1;
1026 shift++;
1027 shift2++;
1028 long_filter_ehigh_3830(decoded0 + order, count - order);
1029 long_filter_ehigh_3830(decoded1 + order, count - order);
1030 }
1031 start = order;
1032 long_filter_high_3800(decoded0, order, shift2, count);
1033 long_filter_high_3800(decoded1, order, shift2, count);
1034 }
1035
1036 while (count--) {
1037 int X = *decoded0, Y = *decoded1;
1038 if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1039 *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
1040 decoded0++;
1041 *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
1042 decoded1++;
1043 } else {
1044 *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
1045 start, shift);
1046 decoded0++;
1047 *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
1048 start, shift);
1049 decoded1++;
1050 }
1051
1052 /* Combined */
1053 p->buf++;
1054 p->sample_pos++;
1055
1056 /* Have we filled the history buffer? */
1057 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1058 memmove(p->historybuffer, p->buf,
1059 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1060 p->buf = p->historybuffer;
1061 }
1062 }
1063}
1064
1066{
1067 APEPredictor *p = &ctx->predictor;
1068 int32_t *decoded0 = ctx->decoded[0];
1069 int start = 4, shift = 10;
1070
1071 if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
1072 start = 16;
1073 long_filter_high_3800(decoded0, 16, 9, count);
1074 } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1075 int order = 128, shift2 = 11;
1076
1077 if (ctx->fileversion >= 3830) {
1078 order <<= 1;
1079 shift++;
1080 shift2++;
1081 long_filter_ehigh_3830(decoded0 + order, count - order);
1082 }
1083 start = order;
1084 long_filter_high_3800(decoded0, order, shift2, count);
1085 }
1086
1087 while (count--) {
1088 if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1089 *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
1090 decoded0++;
1091 } else {
1092 *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
1093 start, shift);
1094 decoded0++;
1095 }
1096
1097 /* Combined */
1098 p->buf++;
1099 p->sample_pos++;
1100
1101 /* Have we filled the history buffer? */
1102 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1103 memmove(p->historybuffer, p->buf,
1104 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1105 p->buf = p->historybuffer;
1106 }
1107 }
1108}
1109
1111 const int decoded, const int filter,
1112 const int delayA)
1113{
1114 int32_t predictionA, sign;
1115 uint32_t d0, d1, d2, d3;
1116
1117 p->buf[delayA] = p->lastA[filter];
1118 d0 = p->buf[delayA ];
1119 d1 = p->buf[delayA ] - (unsigned)p->buf[delayA - 1];
1120 d2 = p->buf[delayA - 1] - (unsigned)p->buf[delayA - 2];
1121 d3 = p->buf[delayA - 2] - (unsigned)p->buf[delayA - 3];
1122
1123 predictionA = d0 * p->coeffsA[filter][0] +
1124 d1 * p->coeffsA[filter][1] +
1125 d2 * p->coeffsA[filter][2] +
1126 d3 * p->coeffsA[filter][3];
1127
1128 p->lastA[filter] = decoded + (predictionA >> 9);
1129 p->filterA[filter] = p->lastA[filter] + ((int)(p->filterA[filter] * 31U) >> 5);
1130
1131 sign = APESIGN(decoded);
1132 p->coeffsA[filter][0] += (((int32_t)d0 < 0) * 2 - 1) * sign;
1133 p->coeffsA[filter][1] += (((int32_t)d1 < 0) * 2 - 1) * sign;
1134 p->coeffsA[filter][2] += (((int32_t)d2 < 0) * 2 - 1) * sign;
1135 p->coeffsA[filter][3] += (((int32_t)d3 < 0) * 2 - 1) * sign;
1136
1137 return p->filterA[filter];
1138}
1139
1141{
1142 APEPredictor *p = &ctx->predictor;
1143 int32_t *decoded0 = ctx->decoded[0];
1144 int32_t *decoded1 = ctx->decoded[1];
1145
1146 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1147
1148 while (count--) {
1149 /* Predictor Y */
1150 int Y = *decoded1, X = *decoded0;
1151 *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
1152 decoded0++;
1153 *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
1154 decoded1++;
1155
1156 /* Combined */
1157 p->buf++;
1158
1159 /* Have we filled the history buffer? */
1160 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1161 memmove(p->historybuffer, p->buf,
1162 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1163 p->buf = p->historybuffer;
1164 }
1165 }
1166}
1167
1169{
1170 APEPredictor *p = &ctx->predictor;
1171 int32_t *decoded0 = ctx->decoded[0];
1172
1173 ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1174
1175 while (count--) {
1176 *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
1177 decoded0++;
1178
1179 p->buf++;
1180
1181 /* Have we filled the history buffer? */
1182 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1183 memmove(p->historybuffer, p->buf,
1184 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1185 p->buf = p->historybuffer;
1186 }
1187 }
1188}
1189
1191 const int decoded, const int filter,
1192 const int delayA, const int delayB,
1193 const int adaptA, const int adaptB,
1194 int interim_mode)
1195{
1196 int64_t predictionA, predictionB;
1197 int32_t sign;
1198
1199 p->buf[delayA] = p->lastA[filter];
1200 p->buf[adaptA] = APESIGN(p->buf[delayA]);
1201 p->buf[delayA - 1] = p->buf[delayA] - (uint64_t)p->buf[delayA - 1];
1202 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1203
1204 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
1205 p->buf[delayA - 1] * p->coeffsA[filter][1] +
1206 p->buf[delayA - 2] * p->coeffsA[filter][2] +
1207 p->buf[delayA - 3] * p->coeffsA[filter][3];
1208
1209 /* Apply a scaled first-order filter compression */
1210 p->buf[delayB] = p->filterA[filter ^ 1] - ((int64_t)(p->filterB[filter] * 31ULL) >> 5);
1211 p->buf[adaptB] = APESIGN(p->buf[delayB]);
1212 p->buf[delayB - 1] = p->buf[delayB] - (uint64_t)p->buf[delayB - 1];
1213 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1214 p->filterB[filter] = p->filterA[filter ^ 1];
1215
1216 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
1217 p->buf[delayB - 1] * p->coeffsB[filter][1] +
1218 p->buf[delayB - 2] * p->coeffsB[filter][2] +
1219 p->buf[delayB - 3] * p->coeffsB[filter][3] +
1220 p->buf[delayB - 4] * p->coeffsB[filter][4];
1221
1222 if (interim_mode < 1) {
1223 predictionA = (int32_t)predictionA;
1224 predictionB = (int32_t)predictionB;
1225 p->lastA[filter] = (int32_t)(decoded + (unsigned)((int32_t)(predictionA + (predictionB >> 1)) >> 10));
1226 } else {
1227 p->lastA[filter] = decoded + ((int64_t)((uint64_t)predictionA + (predictionB >> 1)) >> 10);
1228 }
1229 p->filterA[filter] = p->lastA[filter] + ((int64_t)(p->filterA[filter] * 31ULL) >> 5);
1230
1231 sign = APESIGN(decoded);
1232 p->coeffsA[filter][0] += p->buf[adaptA ] * sign;
1233 p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
1234 p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
1235 p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
1236 p->coeffsB[filter][0] += p->buf[adaptB ] * sign;
1237 p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
1238 p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
1239 p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
1240 p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
1241
1242 return p->filterA[filter];
1243}
1244
1246{
1247 APEPredictor64 *p_default = &ctx->predictor64;
1248 APEPredictor64 p_interim;
1249 int lcount = count;
1250 int num_passes = 1;
1251
1252 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1253 if (ctx->interim_mode == -1) {
1254 p_interim = *p_default;
1255 num_passes ++;
1256 memcpy(ctx->interim[0], ctx->decoded[0], sizeof(*ctx->interim[0])*count);
1257 memcpy(ctx->interim[1], ctx->decoded[1], sizeof(*ctx->interim[1])*count);
1258 }
1259
1260 for (int pass = 0; pass < num_passes; pass++) {
1261 int32_t *decoded0, *decoded1;
1262 int interim_mode = ctx->interim_mode > 0 || pass;
1263 APEPredictor64 *p;
1264
1265 if (pass) {
1266 p = &p_interim;
1267 decoded0 = ctx->interim[0];
1268 decoded1 = ctx->interim[1];
1269 } else {
1270 p = p_default;
1271 decoded0 = ctx->decoded[0];
1272 decoded1 = ctx->decoded[1];
1273 }
1274 p->buf = p->historybuffer;
1275
1276 count = lcount;
1277 while (count--) {
1278 /* Predictor Y */
1279 int32_t a0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1281 interim_mode);
1282 int32_t a1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1284 interim_mode);
1285 *decoded0++ = a0;
1286 *decoded1++ = a1;
1287 if (num_passes > 1) {
1288 int32_t left = a1 - (unsigned)(a0 / 2);
1289 int32_t right = left + (unsigned)a0;
1290
1291 if (FFMIN(FFNABS(left), FFNABS(right)) < -(1<<23)) {
1292 ctx->interim_mode = !interim_mode;
1293 av_log(ctx->avctx, AV_LOG_VERBOSE, "Interim mode: %d\n", ctx->interim_mode);
1294 break;
1295 }
1296 }
1297
1298 /* Combined */
1299 p->buf++;
1300
1301 /* Have we filled the history buffer? */
1302 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1303 memmove(p->historybuffer, p->buf,
1304 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1305 p->buf = p->historybuffer;
1306 }
1307 }
1308 }
1309 if (num_passes > 1 && ctx->interim_mode > 0) {
1310 memcpy(ctx->decoded[0], ctx->interim[0], sizeof(*ctx->interim[0])*lcount);
1311 memcpy(ctx->decoded[1], ctx->interim[1], sizeof(*ctx->interim[1])*lcount);
1312 *p_default = p_interim;
1313 p_default->buf = p_default->historybuffer;
1314 }
1315}
1316
1318{
1319 APEPredictor64 *p = &ctx->predictor64;
1320 int32_t *decoded0 = ctx->decoded[0];
1321 int32_t predictionA, currentA, A, sign;
1322
1323 ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1324
1325 currentA = p->lastA[0];
1326
1327 while (count--) {
1328 A = *decoded0;
1329
1330 p->buf[YDELAYA] = currentA;
1331 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - (uint64_t)p->buf[YDELAYA - 1];
1332
1333 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
1334 p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
1335 p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
1336 p->buf[YDELAYA - 3] * p->coeffsA[0][3];
1337
1338 currentA = A + (uint64_t)(predictionA >> 10);
1339
1340 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
1341 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
1342
1343 sign = APESIGN(A);
1344 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ] * sign;
1345 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
1346 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
1347 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
1348
1349 p->buf++;
1350
1351 /* Have we filled the history buffer? */
1352 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1353 memmove(p->historybuffer, p->buf,
1354 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1355 p->buf = p->historybuffer;
1356 }
1357
1358 p->filterA[0] = currentA + (uint64_t)((int64_t)(p->filterA[0] * 31U) >> 5);
1359 *(decoded0++) = p->filterA[0];
1360 }
1361
1362 p->lastA[0] = currentA;
1363}
1364
1365static void do_init_filter(APEFilter *f, int16_t *buf, int order)
1366{
1367 f->coeffs = buf;
1368 f->historybuffer = buf + order;
1369 f->delay = f->historybuffer + order * 2;
1370 f->adaptcoeffs = f->historybuffer + order;
1371
1372 memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
1373 memset(f->coeffs, 0, order * sizeof(*f->coeffs));
1374 f->avg = 0;
1375}
1376
1377static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
1378{
1379 do_init_filter(&f[0], buf, order);
1380 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1381}
1382
1384 int32_t *data, int count, int order, int fracbits)
1385{
1386 int res;
1387 unsigned absres;
1388
1389 while (count--) {
1390 /* round fixedpoint scalar product */
1391 res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs,
1392 f->delay - order,
1393 f->adaptcoeffs - order,
1394 order, APESIGN(*data));
1395 res = (int64_t)(res + (1LL << (fracbits - 1))) >> fracbits;
1396 res += (unsigned)*data;
1397 *data++ = res;
1398
1399 /* Update the output history */
1400 *f->delay++ = av_clip_int16(res);
1401
1402 if (version < 3980) {
1403 /* Version ??? to < 3.98 files (untested) */
1404 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1405 f->adaptcoeffs[-4] >>= 1;
1406 f->adaptcoeffs[-8] >>= 1;
1407 } else {
1408 /* Version 3.98 and later files */
1409
1410 /* Update the adaption coefficients */
1411 absres = FFABSU(res);
1412 if (absres)
1413 *f->adaptcoeffs = APESIGN(res) *
1414 (8 << ((absres > f->avg * 3LL) + (absres > (f->avg + f->avg / 3))));
1415 /* equivalent to the following code
1416 if (absres <= f->avg * 4 / 3)
1417 *f->adaptcoeffs = APESIGN(res) * 8;
1418 else if (absres <= f->avg * 3)
1419 *f->adaptcoeffs = APESIGN(res) * 16;
1420 else
1421 *f->adaptcoeffs = APESIGN(res) * 32;
1422 */
1423 else
1424 *f->adaptcoeffs = 0;
1425
1426 f->avg += (int)(absres - (unsigned)f->avg) / 16;
1427
1428 f->adaptcoeffs[-1] >>= 1;
1429 f->adaptcoeffs[-2] >>= 1;
1430 f->adaptcoeffs[-8] >>= 1;
1431 }
1432
1433 f->adaptcoeffs++;
1434
1435 /* Have we filled the history buffer? */
1436 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1437 memmove(f->historybuffer, f->delay - (order * 2),
1438 (order * 2) * sizeof(*f->historybuffer));
1439 f->delay = f->historybuffer + order * 2;
1440 f->adaptcoeffs = f->historybuffer + order;
1441 }
1442 }
1443}
1444
1446 int32_t *data0, int32_t *data1,
1447 int count, int order, int fracbits)
1448{
1449 do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1450 if (data1)
1451 do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1452}
1453
1454static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
1455 int32_t *decoded1, int count)
1456{
1457 int i;
1458
1459 for (i = 0; i < APE_FILTER_LEVELS; i++) {
1460 if (!ape_filter_orders[ctx->fset][i])
1461 break;
1462 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
1463 ape_filter_orders[ctx->fset][i],
1464 ape_filter_fracbits[ctx->fset][i]);
1465 }
1466}
1467
1469{
1470 int i, ret;
1471 if ((ret = init_entropy_decoder(ctx)) < 0)
1472 return ret;
1474
1475 for (i = 0; i < APE_FILTER_LEVELS; i++) {
1476 if (!ape_filter_orders[ctx->fset][i])
1477 break;
1478 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
1479 ape_filter_orders[ctx->fset][i]);
1480 }
1481 return 0;
1482}
1483
1484static void ape_unpack_mono(APEContext *ctx, int count)
1485{
1486 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1487 /* We are pure silence, so we're done. */
1488 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
1489 return;
1490 }
1491
1492 ctx->entropy_decode_mono(ctx, count);
1493 if (ctx->error)
1494 return;
1495
1496 /* Now apply the predictor decoding */
1497 ctx->predictor_decode_mono(ctx, count);
1498
1499 /* Pseudo-stereo - just copy left channel to right channel */
1500 if (ctx->channels == 2) {
1501 memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1]));
1502 }
1503}
1504
1505static void ape_unpack_stereo(APEContext *ctx, int count)
1506{
1507 unsigned left, right;
1508 int32_t *decoded0 = ctx->decoded[0];
1509 int32_t *decoded1 = ctx->decoded[1];
1510
1512 /* We are pure silence, so we're done. */
1513 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
1514 return;
1515 }
1516
1517 ctx->entropy_decode_stereo(ctx, count);
1518 if (ctx->error)
1519 return;
1520
1521 /* Now apply the predictor decoding */
1522 ctx->predictor_decode_stereo(ctx, count);
1523
1524 /* Decorrelate and scale to output depth */
1525 while (count--) {
1526 left = *decoded1 - (unsigned)(*decoded0 / 2);
1527 right = left + *decoded0;
1528
1529 *(decoded0++) = left;
1530 *(decoded1++) = right;
1531 }
1532}
1533
1535 int *got_frame_ptr, AVPacket *avpkt)
1536{
1537 const uint8_t *buf = avpkt->data;
1539 uint8_t *sample8;
1540 int16_t *sample16;
1541 int32_t *sample24;
1542 int i, ch, ret;
1543 int blockstodecode;
1544 uint64_t decoded_buffer_size;
1545
1546 /* this should never be negative, but bad things will happen if it is, so
1547 check it just to make sure. */
1548 av_assert0(s->samples >= 0);
1549
1550 if(!s->samples){
1551 uint32_t nblocks, offset;
1552 int buf_size;
1553
1554 if (!avpkt->size) {
1555 *got_frame_ptr = 0;
1556 return 0;
1557 }
1558 if (avpkt->size < 8) {
1559 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1560 return AVERROR_INVALIDDATA;
1561 }
1562 buf_size = avpkt->size & ~3;
1563 if (buf_size != avpkt->size) {
1564 av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
1565 "extra bytes at the end will be skipped.\n");
1566 }
1567 if (s->fileversion < 3950) // previous versions overread two bytes
1568 buf_size += 2;
1569 av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
1570 if (!s->data)
1571 return AVERROR(ENOMEM);
1572 s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
1573 buf_size >> 2);
1574 memset(s->data + (buf_size & ~3), 0, buf_size & 3);
1575 s->ptr = s->data;
1576 s->data_end = s->data + buf_size;
1577
1578 nblocks = bytestream_get_be32(&s->ptr);
1579 offset = bytestream_get_be32(&s->ptr);
1580 if (s->fileversion >= 3900) {
1581 if (offset > 3) {
1582 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
1583 av_freep(&s->data);
1584 s->data_size = 0;
1585 return AVERROR_INVALIDDATA;
1586 }
1587 if (s->data_end - s->ptr < offset) {
1588 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1589 return AVERROR_INVALIDDATA;
1590 }
1591 s->ptr += offset;
1592 } else {
1593 if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
1594 return ret;
1595 if (s->fileversion > 3800)
1596 skip_bits_long(&s->gb, offset * 8);
1597 else
1598 skip_bits_long(&s->gb, offset);
1599 }
1600
1601 if (!nblocks || nblocks > INT_MAX / 2 / sizeof(*s->decoded_buffer) - 8) {
1602 av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n",
1603 nblocks);
1604 return AVERROR_INVALIDDATA;
1605 }
1606
1607 /* Initialize the frame decoder */
1608 if (init_frame_decoder(s) < 0) {
1609 av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
1610 return AVERROR_INVALIDDATA;
1611 }
1612 s->samples = nblocks;
1613 }
1614
1615 if (!s->data) {
1616 *got_frame_ptr = 0;
1617 return avpkt->size;
1618 }
1619
1620 blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
1621 // for old files coefficients were not interleaved,
1622 // so we need to decode all of them at once
1623 if (s->fileversion < 3930)
1624 blockstodecode = s->samples;
1625
1626 /* reallocate decoded sample buffer if needed */
1627 decoded_buffer_size = 2LL * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer);
1628 av_assert0(decoded_buffer_size <= INT_MAX);
1629
1630 /* get output buffer */
1631 frame->nb_samples = blockstodecode;
1632 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1633 s->samples=0;
1634 return ret;
1635 }
1636
1637 av_fast_malloc(&s->decoded_buffer, &s->decoded_size, decoded_buffer_size);
1638 if (!s->decoded_buffer)
1639 return AVERROR(ENOMEM);
1640 memset(s->decoded_buffer, 0, decoded_buffer_size);
1641 s->decoded[0] = s->decoded_buffer;
1642 s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1643
1644 if (s->interim_mode < 0) {
1645 av_fast_malloc(&s->interim_buffer, &s->interim_size, decoded_buffer_size);
1646 if (!s->interim_buffer)
1647 return AVERROR(ENOMEM);
1648 memset(s->interim_buffer, 0, decoded_buffer_size);
1649 s->interim[0] = s->interim_buffer;
1650 s->interim[1] = s->interim_buffer + FFALIGN(blockstodecode, 8);
1651 } else {
1652 av_freep(&s->interim_buffer);
1653 s->interim_size = 0;
1654 memset(s->interim, 0, sizeof(s->interim));
1655 }
1656
1657 s->error=0;
1658
1659 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1660 ape_unpack_mono(s, blockstodecode);
1661 else
1662 ape_unpack_stereo(s, blockstodecode);
1663
1664 if (s->error) {
1665 s->samples=0;
1666 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
1667 return AVERROR_INVALIDDATA;
1668 }
1669
1670 switch (s->bps) {
1671 case 8:
1672 for (ch = 0; ch < s->channels; ch++) {
1673 sample8 = (uint8_t *)frame->data[ch];
1674 for (i = 0; i < blockstodecode; i++)
1675 *sample8++ = (s->decoded[ch][i] + 0x80U) & 0xff;
1676 }
1677 break;
1678 case 16:
1679 for (ch = 0; ch < s->channels; ch++) {
1680 sample16 = (int16_t *)frame->data[ch];
1681 for (i = 0; i < blockstodecode; i++)
1682 *sample16++ = s->decoded[ch][i];
1683 }
1684 break;
1685 case 24:
1686 for (ch = 0; ch < s->channels; ch++) {
1687 sample24 = (int32_t *)frame->data[ch];
1688 for (i = 0; i < blockstodecode; i++)
1689 *sample24++ = s->decoded[ch][i] * 256U;
1690 }
1691 break;
1692 }
1693
1694 s->samples -= blockstodecode;
1695
1697 s->fileversion >= 3900) {
1698 uint32_t crc = s->CRC_state;
1699 const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1700 int stride = s->bps == 24 ? 4 : (s->bps>>3);
1701 int offset = s->bps == 24;
1702 int bytes = s->bps >> 3;
1703
1704 for (i = 0; i < blockstodecode; i++) {
1705 for (ch = 0; ch < s->channels; ch++) {
1706#if HAVE_BIGENDIAN
1707 uint8_t *smp_native = frame->data[ch] + i*stride;
1708 uint8_t smp[4];
1709 for(int j = 0; j<stride; j++)
1710 smp[j] = smp_native[stride-j-1];
1711#else
1712 uint8_t *smp = frame->data[ch] + i*stride;
1713#endif
1714 crc = av_crc(crc_tab, crc, smp+offset, bytes);
1715 }
1716 }
1717
1718 if (!s->samples && (~crc >> 1) ^ s->CRC) {
1719 av_log(avctx, AV_LOG_ERROR, "CRC mismatch! Previously decoded "
1720 "frames may have been affected as well.\n");
1722 return AVERROR_INVALIDDATA;
1723 }
1724
1725 s->CRC_state = crc;
1726 }
1727
1728 *got_frame_ptr = 1;
1729
1730 return !s->samples ? avpkt->size : 0;
1731}
1732
1734{
1736 s->samples= 0;
1737}
1738
1739#define OFFSET(x) offsetof(APEContext, x)
1740#define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1741static const AVOption options[] = {
1742 { "max_samples", "maximum number of samples decoded per call", OFFSET(blocks_per_loop), AV_OPT_TYPE_INT, { .i64 = 4608 }, 1, INT_MAX, PAR, .unit = "max_samples" },
1743 { "all", "no maximum. decode all samples for each packet at once", 0, AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX, PAR, .unit = "max_samples" },
1744 { NULL},
1745};
1746
1748 .class_name = "APE decoder",
1749 .item_name = av_default_item_name,
1750 .option = options,
1751 .version = LIBAVUTIL_VERSION_INT,
1752};
1753
1755 .p.name = "ape",
1756 CODEC_LONG_NAME("Monkey's Audio"),
1757 .p.type = AVMEDIA_TYPE_AUDIO,
1758 .p.id = AV_CODEC_ID_APE,
1759 .priv_data_size = sizeof(APEContext),
1763 .p.capabilities = AV_CODEC_CAP_DELAY |
1765 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1766 .flush = ape_flush,
1767 .p.priv_class = &ape_decoder_class,
1768};
#define MAX_CHANNELS
Definition aac.h:33
const FFCodec ff_ape_decoder
Definition apedec.c:1754
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
Definition apedec.c:699
static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
Definition apedec.c:735
static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS]
Filter fraction bits depending on compression level.
Definition apedec.c:92
static void range_dec_normalize(APEContext *ctx)
Perform normalization.
Definition apedec.c:353
static av_cold int ape_decode_close(AVCodecContext *avctx)
Definition apedec.c:223
static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
Definition apedec.c:691
#define XDELAYA
Definition apedec.c:59
#define XADAPTCOEFFSA
Definition apedec.c:63
static int APESIGN(int32_t x)
Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)
Definition apedec.c:874
static const int32_t initial_coeffs_fast_3320[1]
Definition apedec.c:803
#define YADAPTCOEFFSB
Definition apedec.c:64
#define BOTTOM_VALUE
Definition apedec.c:342
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
Definition apedec.c:711
static av_always_inline int filter_fast_3320(APEPredictor *p, const int decoded, const int filter, const int delayA)
Definition apedec.c:878
#define PAR
Definition apedec.c:1740
static int ape_decode_value_3900(APEContext *ctx, APERice *rice)
Definition apedec.c:539
static int ape_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition apedec.c:1534
static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
Definition apedec.c:1383
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
Definition apedec.c:83
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
Definition apedec.c:400
static void predictor_decode_stereo_3950(APEContext *ctx, int count)
Definition apedec.c:1245
#define HISTORY_SIZE
Definition apedec.c:52
#define MODEL_ELEMENTS
Definition apedec.c:415
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
Definition apedec.c:1365
static int get_k(int ksum)
Definition apedec.c:614
#define YDELAYB
Definition apedec.c:58
#define APE_FRAMECODE_STEREO_SILENCE
Definition apedec.c:49
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate cumulative frequency for next symbol.
Definition apedec.c:374
static void update_rice(APERice *rice, unsigned int x)
Definition apedec.c:483
APECompressionLevel
Possible compression levels.
Definition apedec.c:71
@ COMPRESSION_LEVEL_EXTRA_HIGH
Definition apedec.c:75
@ COMPRESSION_LEVEL_INSANE
Definition apedec.c:76
@ COMPRESSION_LEVEL_FAST
Definition apedec.c:72
@ COMPRESSION_LEVEL_HIGH
Definition apedec.c:74
@ COMPRESSION_LEVEL_NORMAL
Definition apedec.c:73
static int ape_decode_value_3990(APEContext *ctx, APERice *rice)
Definition apedec.c:573
static av_always_inline int filter_3800(APEPredictor *p, const unsigned decoded, const int filter, const int delayA, const int delayB, const int start, const int shift)
Definition apedec.c:904
static const int32_t initial_coeffs_b_3800[2]
Definition apedec.c:811
static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, int32_t *decoded1, int count)
Definition apedec.c:1454
static void ape_unpack_mono(APEContext *ctx, int count)
Definition apedec.c:1484
static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
Definition apedec.c:949
static void predictor_decode_mono_3930(APEContext *ctx, int count)
Definition apedec.c:1168
static int init_entropy_decoder(APEContext *ctx)
Definition apedec.c:765
#define PREDICTOR_SIZE
Total size of all predictor histories.
Definition apedec.c:55
#define XADAPTCOEFFSB
Definition apedec.c:65
static const int64_t initial_coeffs_3930_64bit[4]
Definition apedec.c:819
#define EXTRA_BITS
Definition apedec.c:341
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
Definition apedec.c:386
static const int32_t initial_coeffs_a_3800[3]
Definition apedec.c:807
static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
Definition apedec.c:1377
#define YDELAYA
Definition apedec.c:57
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
Definition apedec.c:683
static void long_filter_ehigh_3830(int32_t *buffer, int length)
Definition apedec.c:989
static av_cold int ape_decode_init(AVCodecContext *avctx)
Definition apedec.c:239
static int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, APERice *rice)
Definition apedec.c:506
static const int32_t initial_coeffs_3930[4]
Definition apedec.c:815
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
Definition apedec.c:677
static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
Definition apedec.c:719
static void predictor_decode_stereo_3800(APEContext *ctx, int count)
Definition apedec.c:1010
static int range_get_symbol(APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
Decode symbol.
Definition apedec.c:459
#define YADAPTCOEFFSA
Definition apedec.c:62
#define APE_FILTER_LEVELS
Definition apedec.c:80
static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
Definition apedec.c:746
static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
Definition apedec.c:754
static void predictor_decode_stereo_3930(APEContext *ctx, int count)
Definition apedec.c:1140
static av_always_inline int predictor_update_3930(APEPredictor *p, const int decoded, const int filter, const int delayA)
Definition apedec.c:1110
static int get_rice_ook(GetBitContext *gb, int k)
Definition apedec.c:494
static av_always_inline int predictor_update_filter(APEPredictor64 *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB, int interim_mode)
Definition apedec.c:1190
static const AVClass ape_decoder_class
Definition apedec.c:1747
static void predictor_decode_mono_3800(APEContext *ctx, int count)
Definition apedec.c:1065
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, int32_t *out, APERice *rice, int blockstodecode)
Definition apedec.c:619
static av_cold void ape_flush(AVCodecContext *avctx)
Definition apedec.c:1733
static const uint16_t counts_3970[22]
Fixed probabilities for symbols in Monkey Audio version 3.97.
Definition apedec.c:420
#define OFFSET(x)
Definition apedec.c:1739
static void ape_unpack_stereo(APEContext *ctx, int count)
Definition apedec.c:1505
#define XDELAYB
Definition apedec.c:60
static void range_start_decoding(APEContext *ctx)
Start the decoder.
Definition apedec.c:345
static const uint16_t counts_diff_3970[21]
Probability ranges for symbols in Monkey Audio version 3.97.
Definition apedec.c:429
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
Definition apedec.c:447
static const uint16_t counts_3980[22]
Fixed probabilities for symbols in Monkey Audio version 3.98.
Definition apedec.c:438
static void init_predictor_decoder(APEContext *ctx)
Definition apedec.c:823
static void apply_filter(APEContext *ctx, APEFilter *f, int32_t *data0, int32_t *data1, int count, int order, int fracbits)
Definition apedec.c:1445
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
Definition apedec.c:407
static int init_frame_decoder(APEContext *ctx)
Definition apedec.c:1468
static void predictor_decode_mono_3950(APEContext *ctx, int count)
Definition apedec.c:1317
#define APE_FRAMECODE_PSEUDO_STEREO
Definition apedec.c:50
static FILE * out
static AVFormatContext * ctx
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define A(x)
Definition vpx_arith.h:28
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define Y
Definition boxblur.h:37
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FFNABS(a)
Negative Absolute value.
Definition common.h:83
#define FFABSU(a)
Unsigned Absolute value.
Definition common.h:91
#define av_clip_int16
Definition common.h:115
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition defs.h:48
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define X
Definition f_ebur128.c:157
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
#define MIN_CACHE_BITS
Definition get_bits.h:167
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_APE
Definition codec_id.h:485
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition utils.c:53
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_32_IEEE_LE
Definition crc.h:53
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:555
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition samplefmt.h:63
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_log2
Definition intmath.h:84
#define AV_RL16(p)
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition bswapdsp.c:37
static const uint8_t shift2[6]
Definition dxa.c:50
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
version
Definition libkvazaar.c:313
av_cold void ff_llauddsp_init(LLAudDSPContext *c)
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
Decoder context.
Definition apedec.c:153
void(* entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode)
Definition apedec.c:198
int32_t * decoded[MAX_CHANNELS]
decoded data for each channel
Definition apedec.c:175
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
Definition apedec.c:181
int samples
samples left to decode in current frame
Definition apedec.c:159
const uint8_t * ptr
current position in frame data
Definition apedec.c:192
void(* predictor_decode_mono)(struct APEContext *ctx, int count)
Definition apedec.c:199
int32_t * interim[MAX_CHANNELS]
decoded data for each channel
Definition apedec.c:178
int flags
global decoder flags
Definition apedec.c:165
int data_size
frame data allocated size
Definition apedec.c:191
APEPredictor64 predictor64
64bit predictor used for final reconstruction
Definition apedec.c:171
void(* predictor_decode_stereo)(struct APEContext *ctx, int count)
Definition apedec.c:200
int channels
Definition apedec.c:158
APERice riceY
rice code parameters for the first channel
Definition apedec.c:185
int interim_size
Definition apedec.c:177
uint8_t * data_end
frame data end
Definition apedec.c:190
int fileversion
codec version, very important in decoding process
Definition apedec.c:162
APEFilter filters[APE_FILTER_LEVELS][2]
filters used for reconstruction
Definition apedec.c:186
uint32_t CRC_state
accumulated CRC
Definition apedec.c:168
APEPredictor predictor
predictor used for final reconstruction
Definition apedec.c:170
uint32_t CRC
signalled frame CRC
Definition apedec.c:167
int32_t * interim_buffer
Definition apedec.c:176
APERangecoder rc
rangecoder used to decode actual values
Definition apedec.c:183
APERice riceX
rice code parameters for the second channel
Definition apedec.c:184
AVCodecContext * avctx
Definition apedec.c:155
int interim_mode
Definition apedec.c:195
int compression_level
compression levels
Definition apedec.c:163
int blocks_per_loop
maximum number of samples to decode for each call
Definition apedec.c:179
GetBitContext gb
Definition apedec.c:187
int bps
Definition apedec.c:160
BswapDSPContext bdsp
Definition apedec.c:156
int error
Definition apedec.c:194
int decoded_size
Definition apedec.c:174
void(* entropy_decode_mono)(struct APEContext *ctx, int blockstodecode)
Definition apedec.c:197
uint8_t * data
current frame data
Definition apedec.c:189
int32_t * decoded_buffer
Definition apedec.c:173
LLAudDSPContext adsp
Definition apedec.c:157
int fset
which filter set to use (calculated from compression level)
Definition apedec.c:164
int frameflags
frame flags
Definition apedec.c:169
Filters applied to the decoded data.
Definition apedec.c:102
uint32_t avg
Definition apedec.c:108
int16_t * historybuffer
filter memory
Definition apedec.c:105
int16_t * delay
filtered values
Definition apedec.c:106
int16_t * coeffs
actual coefficients used in filtering
Definition apedec.c:103
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
Definition apedec.c:104
int64_t lastA[2]
Definition apedec.c:142
int64_t filterB[2]
Definition apedec.c:145
uint64_t coeffsB[2][5]
adaption coefficients
Definition apedec.c:148
uint64_t coeffsA[2][4]
adaption coefficients
Definition apedec.c:147
int64_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition apedec.c:149
int64_t * buf
Definition apedec.c:140
int64_t filterA[2]
Definition apedec.c:144
Filter histories.
Definition apedec.c:124
unsigned int sample_pos
Definition apedec.c:136
int32_t filterA[2]
Definition apedec.c:129
int32_t * buf
Definition apedec.c:125
int32_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition apedec.c:134
int32_t filterB[2]
Definition apedec.c:130
uint32_t coeffsA[2][4]
adaption coefficients
Definition apedec.c:132
uint32_t coeffsB[2][5]
adaption coefficients
Definition apedec.c:133
int32_t lastA[2]
Definition apedec.c:127
uint32_t low
low end of interval
Definition apedec.c:117
unsigned int buffer
buffer for input/output
Definition apedec.c:120
uint32_t range
length of interval
Definition apedec.c:118
uint32_t help
bytes_to_follow resp. intermediate value
Definition apedec.c:119
uint32_t k
Definition apedec.c:112
uint32_t ksum
Definition apedec.c:113
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1416
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
#define stride
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static char buffer[20]
Definition seek.c:32
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition unary.h:46
static double a0(void *priv, double x, double y)
Definition vf_xfade.c:2028
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029
uint8_t base
Definition vp3data.h:128