38 #define MAX_CHANNELS 2
39 #define MAX_BYTESPERSAMPLE 3
41 #define APE_FRAMECODE_MONO_SILENCE 1
42 #define APE_FRAMECODE_STEREO_SILENCE 3
43 #define APE_FRAMECODE_PSEUDO_STEREO 4
45 #define HISTORY_SIZE 512
46 #define PREDICTOR_ORDER 8
48 #define PREDICTOR_SIZE 50
50 #define YDELAYA (18 + PREDICTOR_ORDER*4)
51 #define YDELAYB (18 + PREDICTOR_ORDER*3)
52 #define XDELAYA (18 + PREDICTOR_ORDER*2)
53 #define XDELAYB (18 + PREDICTOR_ORDER)
55 #define YADAPTCOEFFSA 18
56 #define XADAPTCOEFFSA 14
57 #define YADAPTCOEFFSB 10
58 #define XADAPTCOEFFSB 5
73 #define APE_FILTER_LEVELS 3
239 "%d bits per coded sample", s->
bps);
309 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
310 #define SHIFT_BITS (CODE_BITS - 9)
311 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
312 #define BOTTOM_VALUE (TOP_VALUE >> 8)
385 #define MODEL_ELEMENTS 64
391 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
392 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
393 65450, 65469, 65480, 65487, 65491, 65493,
400 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
401 1104, 677, 415, 248, 150, 89, 54, 31,
409 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
410 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
411 65485, 65488, 65490, 65491, 65492, 65493,
418 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
419 261, 119, 65, 31, 19, 10, 6, 3,
430 const uint16_t counts[],
431 const uint16_t counts_diff[])
438 symbol= cf - 65535 + 63;
445 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
455 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
456 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
458 if (rice->
ksum < lim)
460 else if (rice->
ksum >= (1 << (rice->
k + 5)))
479 unsigned int x, overflow;
484 while (overflow >= 16) {
493 x = (overflow << rice->
k) +
get_bits(gb, rice->
k);
498 rice->
ksum += x - (rice->
ksum + 8 >> 4);
499 if (rice->
ksum < (rice->
k ? 1 << (rice->
k + 4) : 0))
501 else if (rice->
ksum >= (1 << (rice->
k + 5)) && rice->
k < 24)
513 unsigned int x, overflow;
522 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
530 }
else if (tmpk <= 32) {
537 x += overflow << tmpk;
550 unsigned int x, overflow;
553 pivot = rice->
ksum >> 5;
564 if (pivot < 0x10000) {
568 int base_hi = pivot, base_lo;
571 while (base_hi & ~0xFFFF) {
580 base = (base_hi << bbits) + base_lo;
583 x = base + overflow * pivot;
598 int ksummax, ksummin;
601 for (i = 0; i < 5; i++) {
603 rice->
ksum += out[i];
608 for (; i < 64; i++) {
610 rice->
ksum += out[i];
615 ksummax = 1 << rice->
k + 7;
616 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
617 for (; i < blockstodecode; i++) {
619 rice->
ksum += out[i] - out[i - 64];
620 while (rice->
ksum < ksummin) {
622 ksummin = rice->
k ? ksummin >> 1 : 0;
625 while (rice->
ksum >= ksummax) {
630 ksummin = ksummin ? ksummin << 1 : 128;
634 for (i = 0; i < blockstodecode; i++) {
636 out[i] = (out[i] >> 1) + 1;
638 out[i] = -(out[i] >> 1);
660 while (blockstodecode--)
668 int blocks = blockstodecode;
670 while (blockstodecode--)
680 while (blockstodecode--)
688 int blocks = blockstodecode;
690 while (blockstodecode--)
705 while (blockstodecode--) {
715 while (blockstodecode--)
724 while (blockstodecode--) {
736 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
744 ctx->
CRC &= ~0x80000000;
825 return (x < 0) - (x > 0);
841 predictionA = p->
buf[delayA] * 2 - p->
buf[delayA - 1];
844 if ((decoded ^ predictionA) > 0)
856 const int delayA,
const int delayB,
859 int32_t predictionA, predictionB, sign;
872 d1 = (p->
buf[delayA] - p->
buf[delayA - 1]) << 1;
873 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - p->
buf[delayA - 1]) << 3);
874 d3 = p->
buf[delayB] * 2 - p->
buf[delayB - 1];
905 memset(coeffs, 0, order *
sizeof(*coeffs));
906 for (i = 0; i < order; i++)
907 delay[i] = buffer[i];
908 for (i = order; i <
length; i++) {
911 for (j = 0; j < order; j++) {
912 dotprod += delay[j] * coeffs[j];
913 coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
915 buffer[i] -= dotprod >>
shift;
916 for (j = 0; j < order - 1; j++)
917 delay[j] = delay[j + 1];
918 delay[order - 1] = buffer[i];
928 memset(coeffs, 0,
sizeof(coeffs));
929 memset(delay, 0,
sizeof(delay));
930 for (i = 0; i <
length; i++) {
933 for (j = 7; j >= 0; j--) {
934 dotprod += delay[j] * coeffs[j];
935 coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
937 for (j = 7; j > 0; j--)
938 delay[j] = delay[j - 1];
939 delay[0] = buffer[i];
940 buffer[i] -= dotprod >> 9;
957 int order = 128,
shift2 = 11;
972 int X = *decoded0,
Y = *decoded1;
1011 int order = 128,
shift2 = 11;
1054 d0 = p->
buf[delayA ];
1055 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1056 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1057 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1086 int Y = *decoded1, X = *decoded0;
1128 const int delayA,
const int delayB,
1129 const int adaptA,
const int adaptB)
1131 int32_t predictionA, predictionB, sign;
1135 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1146 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1156 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1206 int32_t predictionA, currentA,
A, sign;
1210 currentA = p->
lastA[0];
1223 currentA = A + (predictionA >> 10);
1244 *(decoded0++) = p->
filterA[0];
1247 p->
lastA[0] = currentA;
1279 res = (res + (1 << (fracbits - 1))) >> fracbits;
1284 *f->
delay++ = av_clip_int16(res);
1286 if (version < 3980) {
1288 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1295 absres =
FFABS(res);
1297 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1298 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1302 f->
avg += (absres - f->
avg) / 16;
1323 int count,
int order,
int fracbits)
1398 left = *decoded1 - (*decoded0 / 2);
1399 right = left + *decoded0;
1401 *(decoded0++) = left;
1402 *(decoded1++) = right;
1407 int *got_frame_ptr,
AVPacket *avpkt)
1423 uint32_t nblocks,
offset;
1430 if (avpkt->
size < 8) {
1434 buf_size = avpkt->
size & ~3;
1435 if (buf_size != avpkt->
size) {
1437 "extra bytes at the end will be skipped.\n");
1445 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1449 nblocks = bytestream_get_be32(&s->
ptr);
1450 offset = bytestream_get_be32(&s->
ptr);
1471 if (!nblocks || nblocks > INT_MAX) {
1525 for (ch = 0; ch < s->
channels; ch++) {
1527 for (i = 0; i < blockstodecode; i++)
1528 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1532 for (ch = 0; ch < s->
channels; ch++) {
1533 sample16 = (int16_t *)frame->
data[ch];
1534 for (i = 0; i < blockstodecode; i++)
1535 *sample16++ = s->
decoded[ch][i];
1539 for (ch = 0; ch < s->
channels; ch++) {
1541 for (i = 0; i < blockstodecode; i++)
1542 *sample24++ = s->
decoded[ch][i] << 8;
1560 #define OFFSET(x) offsetof(APEContext, x)
1561 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1564 {
"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" },