36 #define MAX_CHANNELS 2
37 #define MAX_BYTESPERSAMPLE 3
39 #define APE_FRAMECODE_MONO_SILENCE 1
40 #define APE_FRAMECODE_STEREO_SILENCE 3
41 #define APE_FRAMECODE_PSEUDO_STEREO 4
43 #define HISTORY_SIZE 512
44 #define PREDICTOR_ORDER 8
46 #define PREDICTOR_SIZE 50
48 #define YDELAYA (18 + PREDICTOR_ORDER*4)
49 #define YDELAYB (18 + PREDICTOR_ORDER*3)
50 #define XDELAYA (18 + PREDICTOR_ORDER*2)
51 #define XDELAYB (18 + PREDICTOR_ORDER)
53 #define YADAPTCOEFFSA 18
54 #define XADAPTCOEFFSA 14
55 #define YADAPTCOEFFSB 10
56 #define XADAPTCOEFFSB 5
71 #define APE_FILTER_LEVELS 3
249 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
250 #define SHIFT_BITS (CODE_BITS - 9)
251 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
252 #define BOTTOM_VALUE (TOP_VALUE >> 8)
325 #define MODEL_ELEMENTS 64
331 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
332 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
333 65450, 65469, 65480, 65487, 65491, 65493,
340 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
341 1104, 677, 415, 248, 150, 89, 54, 31,
349 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
350 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
351 65485, 65488, 65490, 65491, 65492, 65493,
358 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
359 261, 119, 65, 31, 19, 10, 6, 3,
370 const uint16_t counts[],
371 const uint16_t counts_diff[])
378 symbol= cf - 65535 + 63;
385 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
395 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
396 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
398 if (rice->
ksum < lim)
400 else if (rice->
ksum >= (1 << (rice->
k + 5)))
406 unsigned int x, overflow;
417 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
421 else if (tmpk <= 32) {
428 x += overflow << tmpk;
432 pivot = rice->
ksum >> 5;
443 if (pivot < 0x10000) {
447 int base_hi = pivot, base_lo;
450 while (base_hi & ~0xFFFF) {
459 base = (base_hi << bbits) + base_lo;
462 x = base + overflow * pivot;
479 while (blockstodecode--) {
491 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
496 ctx->
CRC &= ~0x80000000;
541 return (x < 0) - (x > 0);
545 const int decoded,
const int filter,
546 const int delayA,
const int delayB,
547 const int adaptA,
const int adaptB)
549 int32_t predictionA, predictionB, sign;
553 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
564 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
574 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
622 int32_t predictionA, currentA,
A, sign;
624 currentA = p->
lastA[0];
637 currentA = A + (predictionA >> 10);
661 p->
lastA[0] = currentA;
693 res = (res + (1 << (fracbits - 1))) >> fracbits;
698 *f->
delay++ = av_clip_int16(res);
700 if (version < 3980) {
702 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
712 (25 + (absres <= f->
avg*3) + (absres <= f->
avg*4/3));
716 f->
avg += (absres - f->
avg) / 16;
737 int count,
int order,
int fracbits)
814 left = *decoded1 - (*decoded0 / 2);
815 right = left + *decoded0;
817 *(decoded0++) = left;
818 *(decoded1++) = right;
823 int *got_frame_ptr,
AVPacket *avpkt)
846 if (avpkt->
size < 8) {
850 buf_size = avpkt->
size & ~3;
851 if (buf_size != avpkt->
size) {
853 "extra bytes at the end will be skipped.\n");
863 nblocks = bytestream_get_be32(&s->
ptr);
864 offset = bytestream_get_be32(&s->
ptr);
876 if (!nblocks || nblocks > INT_MAX) {
928 for (ch = 0; ch < s->
channels; ch++) {
930 for (i = 0; i < blockstodecode; i++)
931 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
935 for (ch = 0; ch < s->
channels; ch++) {
936 sample16 = (int16_t *)frame->
data[ch];
937 for (i = 0; i < blockstodecode; i++)
938 *sample16++ = s->
decoded[ch][i];
942 for (ch = 0; ch < s->
channels; ch++) {
944 for (i = 0; i < blockstodecode; i++)
945 *sample24++ = s->
decoded[ch][i] << 8;
963 #define OFFSET(x) offsetof(APEContext, x)
964 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
966 {
"max_samples",
"maximum number of samples decoded per call",
OFFSET(blocks_per_loop),
AV_OPT_TYPE_INT, { .i64 = 4608 }, 1, INT_MAX,
PAR,
"max_samples" },
967 {
"all",
"no maximum. decode all samples for each packet at once", 0,
AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX,
PAR,
"max_samples" },