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];
606 for (; i < 64; i++) {
608 rice->
ksum += out[i];
611 ksummax = 1 << rice->
k + 7;
612 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
613 for (; i < blockstodecode; i++) {
615 rice->
ksum += out[i] - out[i - 64];
616 while (rice->
ksum < ksummin) {
618 ksummin = rice->
k ? ksummin >> 1 : 0;
621 while (rice->
ksum >= ksummax) {
626 ksummin = ksummin ? ksummin << 1 : 128;
630 for (i = 0; i < blockstodecode; i++) {
632 out[i] = (out[i] >> 1) + 1;
634 out[i] = -(out[i] >> 1);
656 while (blockstodecode--)
664 int blocks = blockstodecode;
666 while (blockstodecode--)
676 while (blockstodecode--)
684 int blocks = blockstodecode;
686 while (blockstodecode--)
701 while (blockstodecode--) {
711 while (blockstodecode--)
720 while (blockstodecode--) {
732 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
740 ctx->
CRC &= ~0x80000000;
821 return (x < 0) - (x > 0);
837 predictionA = p->
buf[delayA] * 2 - p->
buf[delayA - 1];
840 if ((decoded ^ predictionA) > 0)
852 const int delayA,
const int delayB,
855 int32_t predictionA, predictionB, sign;
868 d1 = (p->
buf[delayA] - p->
buf[delayA - 1]) << 1;
869 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - p->
buf[delayA - 1]) << 3);
870 d3 = p->
buf[delayB] * 2 - p->
buf[delayB - 1];
901 memset(coeffs, 0, order *
sizeof(*coeffs));
902 for (i = 0; i < order; i++)
903 delay[i] = buffer[i];
904 for (i = order; i <
length; i++) {
907 for (j = 0; j < order; j++) {
908 dotprod += delay[j] * coeffs[j];
909 coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
911 buffer[i] -= dotprod >>
shift;
912 for (j = 0; j < order - 1; j++)
913 delay[j] = delay[j + 1];
914 delay[order - 1] = buffer[i];
924 memset(coeffs, 0,
sizeof(coeffs));
925 memset(delay, 0,
sizeof(delay));
926 for (i = 0; i <
length; i++) {
929 for (j = 7; j >= 0; j--) {
930 dotprod += delay[j] * coeffs[j];
931 coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
933 for (j = 7; j > 0; j--)
934 delay[j] = delay[j - 1];
935 delay[0] = buffer[i];
936 buffer[i] -= dotprod >> 9;
953 int order = 128,
shift2 = 11;
968 int X = *decoded0,
Y = *decoded1;
1007 int order = 128,
shift2 = 11;
1050 d0 = p->
buf[delayA ];
1051 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1052 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1053 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1082 int Y = *decoded1, X = *decoded0;
1124 const int delayA,
const int delayB,
1125 const int adaptA,
const int adaptB)
1127 int32_t predictionA, predictionB, sign;
1131 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1142 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1152 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1202 int32_t predictionA, currentA,
A, sign;
1206 currentA = p->
lastA[0];
1219 currentA = A + (predictionA >> 10);
1240 *(decoded0++) = p->
filterA[0];
1243 p->
lastA[0] = currentA;
1275 res = (res + (1 << (fracbits - 1))) >> fracbits;
1280 *f->
delay++ = av_clip_int16(res);
1282 if (version < 3980) {
1284 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1291 absres =
FFABS(res);
1293 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1294 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1298 f->
avg += (absres - f->
avg) / 16;
1319 int count,
int order,
int fracbits)
1394 left = *decoded1 - (*decoded0 / 2);
1395 right = left + *decoded0;
1397 *(decoded0++) = left;
1398 *(decoded1++) = right;
1403 int *got_frame_ptr,
AVPacket *avpkt)
1419 uint32_t nblocks,
offset;
1426 if (avpkt->
size < 8) {
1430 buf_size = avpkt->
size & ~3;
1431 if (buf_size != avpkt->
size) {
1433 "extra bytes at the end will be skipped.\n");
1441 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1445 nblocks = bytestream_get_be32(&s->
ptr);
1446 offset = bytestream_get_be32(&s->
ptr);
1466 if (!nblocks || nblocks > INT_MAX) {
1520 for (ch = 0; ch < s->
channels; ch++) {
1522 for (i = 0; i < blockstodecode; i++)
1523 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1527 for (ch = 0; ch < s->
channels; ch++) {
1528 sample16 = (int16_t *)frame->
data[ch];
1529 for (i = 0; i < blockstodecode; i++)
1530 *sample16++ = s->
decoded[ch][i];
1534 for (ch = 0; ch < s->
channels; ch++) {
1536 for (i = 0; i < blockstodecode; i++)
1537 *sample24++ = s->
decoded[ch][i] << 8;
1555 #define OFFSET(x) offsetof(APEContext, x)
1556 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1559 {
"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" },