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)
508 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
513 unsigned int x, overflow;
522 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
530 }
else if (tmpk <= 31) {
537 x += overflow << tmpk;
542 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
547 unsigned int x, overflow;
550 pivot = rice->
ksum >> 5;
561 if (pivot < 0x10000) {
565 int base_hi = pivot, base_lo;
568 while (base_hi & ~0xFFFF) {
577 base = (base_hi << bbits) + base_lo;
580 x = base + overflow * pivot;
585 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
592 int ksummax, ksummin;
595 for (i = 0; i < 5; i++) {
597 rice->
ksum += out[i];
602 for (; i < 64; i++) {
604 rice->
ksum += out[i];
609 ksummax = 1 << rice->
k + 7;
610 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
611 for (; i < blockstodecode; i++) {
613 rice->
ksum += out[i] - out[i - 64];
614 while (rice->
ksum < ksummin) {
616 ksummin = rice->
k ? ksummin >> 1 : 0;
619 while (rice->
ksum >= ksummax) {
624 ksummin = ksummin ? ksummin << 1 : 128;
628 for (i = 0; i < blockstodecode; i++)
629 out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
650 while (blockstodecode--)
658 int blocks = blockstodecode;
660 while (blockstodecode--)
670 while (blockstodecode--)
678 int blocks = blockstodecode;
680 while (blockstodecode--)
695 while (blockstodecode--) {
705 while (blockstodecode--)
714 while (blockstodecode--) {
726 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
734 ctx->
CRC &= ~0x80000000;
815 return (x < 0) - (x > 0);
831 predictionA = p->
buf[delayA] * 2 - p->
buf[delayA - 1];
834 if ((decoded ^ predictionA) > 0)
846 const int delayA,
const int delayB,
849 int32_t predictionA, predictionB, sign;
862 d1 = (p->
buf[delayA] - p->
buf[delayA - 1]) << 1;
863 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - p->
buf[delayA - 1]) << 3);
864 d3 = p->
buf[delayB] * 2 - p->
buf[delayB - 1];
893 int32_t coeffs[256], delay[256];
895 memset(coeffs, 0, order *
sizeof(*coeffs));
896 for (i = 0; i < order; i++)
897 delay[i] = buffer[i];
898 for (i = order; i <
length; i++) {
901 for (j = 0; j < order; j++) {
902 dotprod += delay[j] * coeffs[j];
903 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
905 buffer[i] -= dotprod >>
shift;
906 for (j = 0; j < order - 1; j++)
907 delay[j] = delay[j + 1];
908 delay[order - 1] = buffer[i];
916 int32_t coeffs[8] = { 0 }, delay[8] = { 0 };
918 for (i = 0; i <
length; i++) {
921 for (j = 7; j >= 0; j--) {
922 dotprod += delay[j] * coeffs[j];
923 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
925 for (j = 7; j > 0; j--)
926 delay[j] = delay[j - 1];
927 delay[0] = buffer[i];
928 buffer[i] -= dotprod >> 9;
944 int order = 128,
shift2 = 11;
959 int X = *decoded0,
Y = *decoded1;
997 int order = 128,
shift2 = 11;
1040 d0 = p->
buf[delayA ];
1041 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1042 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1043 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1072 int Y = *decoded1, X = *decoded0;
1114 const int delayA,
const int delayB,
1115 const int adaptA,
const int adaptB)
1117 int32_t predictionA, predictionB, sign;
1121 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1132 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1142 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1192 int32_t predictionA, currentA,
A, sign;
1196 currentA = p->
lastA[0];
1209 currentA = A + (predictionA >> 10);
1230 *(decoded0++) = p->
filterA[0];
1233 p->
lastA[0] = currentA;
1266 res = (res + (1 << (fracbits - 1))) >> fracbits;
1271 *f->
delay++ = av_clip_int16(res);
1273 if (version < 3980) {
1275 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1282 absres =
FFABS(res);
1284 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1285 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1289 f->
avg += (absres - f->
avg) / 16;
1310 int count,
int order,
int fracbits)
1385 left = *decoded1 - (*decoded0 / 2);
1386 right = left + *decoded0;
1388 *(decoded0++) = left;
1389 *(decoded1++) = right;
1394 int *got_frame_ptr,
AVPacket *avpkt)
1410 uint32_t nblocks,
offset;
1417 if (avpkt->
size < 8) {
1421 buf_size = avpkt->
size & ~3;
1422 if (buf_size != avpkt->
size) {
1424 "extra bytes at the end will be skipped.\n");
1433 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1437 nblocks = bytestream_get_be32(&s->
ptr);
1438 offset = bytestream_get_be32(&s->
ptr);
1459 if (!nblocks || nblocks > INT_MAX) {
1514 for (ch = 0; ch < s->
channels; ch++) {
1516 for (i = 0; i < blockstodecode; i++)
1517 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1521 for (ch = 0; ch < s->
channels; ch++) {
1522 sample16 = (int16_t *)frame->
data[ch];
1523 for (i = 0; i < blockstodecode; i++)
1524 *sample16++ = s->
decoded[ch][i];
1528 for (ch = 0; ch < s->
channels; ch++) {
1530 for (i = 0; i < blockstodecode; i++)
1531 *sample24++ = s->
decoded[ch][i] << 8;
1549 #define OFFSET(x) offsetof(APEContext, x)
1550 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1553 {
"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" },