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 <
FFMIN(blockstodecode, 5); i++) {
597 rice->
ksum += out[i];
602 for (; i <
FFMIN(blockstodecode, 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];
898 memset(coeffs, 0, order *
sizeof(*coeffs));
899 for (i = 0; i < order; i++)
900 delay[i] = buffer[i];
901 for (i = order; i <
length; i++) {
904 for (j = 0; j < order; j++) {
905 dotprod += delay[j] * coeffs[j];
906 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
908 buffer[i] -= dotprod >>
shift;
909 for (j = 0; j < order - 1; j++)
910 delay[j] = delay[j + 1];
911 delay[order - 1] = buffer[i];
919 int32_t coeffs[8] = { 0 }, delay[8] = { 0 };
921 for (i = 0; i <
length; i++) {
924 for (j = 7; j >= 0; j--) {
925 dotprod += delay[j] * coeffs[j];
926 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
928 for (j = 7; j > 0; j--)
929 delay[j] = delay[j - 1];
930 delay[0] = buffer[i];
931 buffer[i] -= dotprod >> 9;
947 int order = 128,
shift2 = 11;
962 int X = *decoded0,
Y = *decoded1;
1000 int order = 128,
shift2 = 11;
1043 d0 = p->
buf[delayA ];
1044 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1045 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1046 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1075 int Y = *decoded1, X = *decoded0;
1117 const int delayA,
const int delayB,
1118 const int adaptA,
const int adaptB)
1120 int32_t predictionA, predictionB, sign;
1124 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1135 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1145 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1195 int32_t predictionA, currentA,
A, sign;
1199 currentA = p->
lastA[0];
1212 currentA = A + (predictionA >> 10);
1233 *(decoded0++) = p->
filterA[0];
1236 p->
lastA[0] = currentA;
1269 res = (res + (1 << (fracbits - 1))) >> fracbits;
1274 *f->
delay++ = av_clip_int16(res);
1276 if (version < 3980) {
1278 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1285 absres =
FFABS(res);
1287 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1288 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1292 f->
avg += (absres - f->
avg) / 16;
1313 int count,
int order,
int fracbits)
1388 left = *decoded1 - (*decoded0 / 2);
1389 right = left + *decoded0;
1391 *(decoded0++) = left;
1392 *(decoded1++) = right;
1397 int *got_frame_ptr,
AVPacket *avpkt)
1413 uint32_t nblocks,
offset;
1420 if (avpkt->
size < 8) {
1424 buf_size = avpkt->
size & ~3;
1425 if (buf_size != avpkt->
size) {
1427 "extra bytes at the end will be skipped.\n");
1436 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1440 nblocks = bytestream_get_be32(&s->
ptr);
1441 offset = bytestream_get_be32(&s->
ptr);
1462 if (!nblocks || nblocks > INT_MAX) {
1517 for (ch = 0; ch < s->
channels; ch++) {
1519 for (i = 0; i < blockstodecode; i++)
1520 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1524 for (ch = 0; ch < s->
channels; ch++) {
1525 sample16 = (int16_t *)frame->
data[ch];
1526 for (i = 0; i < blockstodecode; i++)
1527 *sample16++ = s->
decoded[ch][i];
1531 for (ch = 0; ch < s->
channels; ch++) {
1533 for (i = 0; i < blockstodecode; i++)
1534 *sample24++ = s->
decoded[ch][i] << 8;
1552 #define OFFSET(x) offsetof(APEContext, x)
1553 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1556 {
"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" },