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
253 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
254 #define SHIFT_BITS (CODE_BITS - 9)
255 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
256 #define BOTTOM_VALUE (TOP_VALUE >> 8)
329 #define MODEL_ELEMENTS 64
335 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
336 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
337 65450, 65469, 65480, 65487, 65491, 65493,
344 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
345 1104, 677, 415, 248, 150, 89, 54, 31,
353 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
354 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
355 65485, 65488, 65490, 65491, 65492, 65493,
362 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
363 261, 119, 65, 31, 19, 10, 6, 3,
374 const uint16_t counts[],
375 const uint16_t counts_diff[])
382 symbol= cf - 65535 + 63;
389 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
399 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
400 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
402 if (rice->
ksum < lim)
404 else if (rice->
ksum >= (1 << (rice->
k + 5)))
410 unsigned int x, overflow;
421 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
425 else if (tmpk <= 32) {
432 x += overflow << tmpk;
436 pivot = rice->
ksum >> 5;
447 if (pivot < 0x10000) {
451 int base_hi = pivot, base_lo;
454 while (base_hi & ~0xFFFF) {
463 base = (base_hi << bbits) + base_lo;
466 x = base + overflow * pivot;
483 while (blockstodecode--) {
495 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
500 ctx->
CRC &= ~0x80000000;
545 return (x < 0) - (x > 0);
549 const int decoded,
const int filter,
550 const int delayA,
const int delayB,
551 const int adaptA,
const int adaptB)
553 int32_t predictionA, predictionB, sign;
557 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
568 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
578 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
626 int32_t predictionA, currentA,
A, sign;
628 currentA = p->
lastA[0];
641 currentA = A + (predictionA >> 10);
665 p->
lastA[0] = currentA;
697 res = (res + (1 << (fracbits - 1))) >> fracbits;
702 *f->
delay++ = av_clip_int16(res);
704 if (version < 3980) {
706 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
716 (25 + (absres <= f->
avg*3) + (absres <= f->
avg*4/3));
720 f->
avg += (absres - f->
avg) / 16;
741 int count,
int order,
int fracbits)
818 left = *decoded1 - (*decoded0 / 2);
819 right = left + *decoded0;
821 *(decoded0++) = left;
822 *(decoded1++) = right;
827 int *got_frame_ptr,
AVPacket *avpkt)
849 if (avpkt->
size < 8) {
853 buf_size = avpkt->
size & ~3;
854 if (buf_size != avpkt->
size) {
856 "extra bytes at the end will be skipped.\n");
866 nblocks = bytestream_get_be32(&s->
ptr);
867 offset = bytestream_get_be32(&s->
ptr);
879 if (!nblocks || nblocks > INT_MAX) {
931 for (ch = 0; ch < s->
channels; ch++) {
933 for (i = 0; i < blockstodecode; i++)
934 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
938 for (ch = 0; ch < s->
channels; ch++) {
940 for (i = 0; i < blockstodecode; i++)
941 *sample16++ = s->
decoded[ch][i];
945 for (ch = 0; ch < s->
channels; ch++) {
947 for (i = 0; i < blockstodecode; i++)
948 *sample24++ = s->
decoded[ch][i] << 8;
967 #define OFFSET(x) offsetof(APEContext, x)
968 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
970 {
"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" },
971 {
"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" },