41 #define MAX_CHANNELS 2
42 #define MAX_BYTESPERSAMPLE 3
44 #define APE_FRAMECODE_MONO_SILENCE 1
45 #define APE_FRAMECODE_STEREO_SILENCE 3
46 #define APE_FRAMECODE_PSEUDO_STEREO 4
48 #define HISTORY_SIZE 512
49 #define PREDICTOR_ORDER 8
51 #define PREDICTOR_SIZE 50
53 #define YDELAYA (18 + PREDICTOR_ORDER*4)
54 #define YDELAYB (18 + PREDICTOR_ORDER*3)
55 #define XDELAYA (18 + PREDICTOR_ORDER*2)
56 #define XDELAYB (18 + PREDICTOR_ORDER)
58 #define YADAPTCOEFFSA 18
59 #define XADAPTCOEFFSA 14
60 #define YADAPTCOEFFSB 10
61 #define XADAPTCOEFFSB 5
76 #define APE_FILTER_LEVELS 3
241 "%d bits per coded sample", s->
bps);
312 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
313 #define SHIFT_BITS (CODE_BITS - 9)
314 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
315 #define BOTTOM_VALUE (TOP_VALUE >> 8)
388 #define MODEL_ELEMENTS 64
394 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
395 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
396 65450, 65469, 65480, 65487, 65491, 65493,
403 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
404 1104, 677, 415, 248, 150, 89, 54, 31,
412 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
413 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
414 65485, 65488, 65490, 65491, 65492, 65493,
421 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
422 261, 119, 65, 31, 19, 10, 6, 3,
433 const uint16_t counts[],
434 const uint16_t counts_diff[])
441 symbol= cf - 65535 + 63;
448 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
458 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
459 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
461 if (rice->
ksum < lim)
463 else if (rice->
ksum >= (1 << (rice->
k + 5)))
482 unsigned int x, overflow;
487 while (overflow >= 16) {
496 x = (overflow << rice->
k) +
get_bits(gb, rice->
k);
501 rice->
ksum += x - (rice->
ksum + 8 >> 4);
502 if (rice->
ksum < (rice->
k ? 1 << (rice->
k + 4) : 0))
504 else if (rice->
ksum >= (1 << (rice->
k + 5)) && rice->
k < 24)
516 unsigned int x, overflow;
525 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
533 }
else if (tmpk <= 31) {
540 x += overflow << tmpk;
553 unsigned int x, overflow;
556 pivot = rice->
ksum >> 5;
567 if (pivot < 0x10000) {
571 int base_hi = pivot, base_lo;
574 while (base_hi & ~0xFFFF) {
583 base = (base_hi << bbits) + base_lo;
586 x = base + overflow * pivot;
601 int ksummax, ksummin;
604 for (i = 0; i < 5; i++) {
606 rice->
ksum += out[i];
611 for (; i < 64; i++) {
613 rice->
ksum += out[i];
618 ksummax = 1 << rice->
k + 7;
619 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
620 for (; i < blockstodecode; i++) {
622 rice->
ksum += out[i] - out[i - 64];
623 while (rice->
ksum < ksummin) {
625 ksummin = rice->
k ? ksummin >> 1 : 0;
628 while (rice->
ksum >= ksummax) {
633 ksummin = ksummin ? ksummin << 1 : 128;
637 for (i = 0; i < blockstodecode; i++) {
639 out[i] = (out[i] >> 1) + 1;
641 out[i] = -(out[i] >> 1);
663 while (blockstodecode--)
671 int blocks = blockstodecode;
673 while (blockstodecode--)
683 while (blockstodecode--)
691 int blocks = blockstodecode;
693 while (blockstodecode--)
708 while (blockstodecode--) {
718 while (blockstodecode--)
727 while (blockstodecode--) {
739 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
747 ctx->
CRC &= ~0x80000000;
828 return (x < 0) - (x > 0);
844 predictionA = p->
buf[delayA] * 2 - p->
buf[delayA - 1];
847 if ((decoded ^ predictionA) > 0)
859 const int delayA,
const int delayB,
862 int32_t predictionA, predictionB, sign;
875 d1 = (p->
buf[delayA] - p->
buf[delayA - 1]) << 1;
876 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - p->
buf[delayA - 1]) << 3);
877 d3 = p->
buf[delayB] * 2 - p->
buf[delayB - 1];
908 memset(coeffs, 0, order *
sizeof(*coeffs));
909 for (i = 0; i < order; i++)
910 delay[i] = buffer[i];
911 for (i = order; i <
length; i++) {
914 for (j = 0; j < order; j++) {
915 dotprod += delay[j] * coeffs[j];
916 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
918 buffer[i] -= dotprod >>
shift;
919 for (j = 0; j < order - 1; j++)
920 delay[j] = delay[j + 1];
921 delay[order - 1] = buffer[i];
929 int32_t coeffs[8] = { 0 }, delay[8] = { 0 };
931 for (i = 0; i <
length; i++) {
934 for (j = 7; j >= 0; j--) {
935 dotprod += delay[j] * coeffs[j];
936 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
938 for (j = 7; j > 0; j--)
939 delay[j] = delay[j - 1];
940 delay[0] = buffer[i];
941 buffer[i] -= dotprod >> 9;
950 int32_t coeffs[256], delay[256];
958 int order = 128,
shift2 = 11;
973 int X = *decoded0,
Y = *decoded1;
1005 int32_t coeffs[256], delay[256];
1012 int order = 128,
shift2 = 11;
1055 d0 = p->
buf[delayA ];
1056 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1057 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1058 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1087 int Y = *decoded1, X = *decoded0;
1129 const int delayA,
const int delayB,
1130 const int adaptA,
const int adaptB)
1132 int32_t predictionA, predictionB, sign;
1136 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1147 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1157 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1207 int32_t predictionA, currentA,
A, sign;
1211 currentA = p->
lastA[0];
1224 currentA = A + (predictionA >> 10);
1245 *(decoded0++) = p->
filterA[0];
1248 p->
lastA[0] = currentA;
1281 res = (res + (1 << (fracbits - 1))) >> fracbits;
1286 *f->
delay++ = av_clip_int16(res);
1288 if (version < 3980) {
1290 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1297 absres =
FFABS(res);
1299 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1300 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1304 f->
avg += (absres - f->
avg) / 16;
1325 int count,
int order,
int fracbits)
1400 left = *decoded1 - (*decoded0 / 2);
1401 right = left + *decoded0;
1403 *(decoded0++) = left;
1404 *(decoded1++) = right;
1409 int *got_frame_ptr,
AVPacket *avpkt)
1425 uint32_t nblocks,
offset;
1432 if (avpkt->
size < 8) {
1436 buf_size = avpkt->
size & ~3;
1437 if (buf_size != avpkt->
size) {
1439 "extra bytes at the end will be skipped.\n");
1448 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1452 nblocks = bytestream_get_be32(&s->
ptr);
1453 offset = bytestream_get_be32(&s->
ptr);
1474 if (!nblocks || nblocks > INT_MAX) {
1529 for (ch = 0; ch < s->
channels; ch++) {
1531 for (i = 0; i < blockstodecode; i++)
1532 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1536 for (ch = 0; ch < s->
channels; ch++) {
1537 sample16 = (int16_t *)frame->
data[ch];
1538 for (i = 0; i < blockstodecode; i++)
1539 *sample16++ = s->
decoded[ch][i];
1543 for (ch = 0; ch < s->
channels; ch++) {
1545 for (i = 0; i < blockstodecode; i++)
1546 *sample24++ = s->
decoded[ch][i] << 8;
1564 #define OFFSET(x) offsetof(APEContext, x)
1565 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1568 {
"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" },