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);
308 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
309 #define SHIFT_BITS (CODE_BITS - 9)
310 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
311 #define BOTTOM_VALUE (TOP_VALUE >> 8)
384 #define MODEL_ELEMENTS 64
390 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
391 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
392 65450, 65469, 65480, 65487, 65491, 65493,
399 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
400 1104, 677, 415, 248, 150, 89, 54, 31,
408 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
409 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
410 65485, 65488, 65490, 65491, 65492, 65493,
417 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
418 261, 119, 65, 31, 19, 10, 6, 3,
429 const uint16_t counts[],
430 const uint16_t counts_diff[])
437 symbol= cf - 65535 + 63;
444 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
454 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
455 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
457 if (rice->
ksum < lim)
459 else if (rice->
ksum >= (1 << (rice->
k + 5)))
478 unsigned int x, overflow;
483 while (overflow >= 16) {
492 x = (overflow << rice->
k) +
get_bits(gb, rice->
k);
494 rice->
ksum += x - (rice->
ksum + 8 >> 4);
495 if (rice->
ksum < (rice->
k ? 1 << (rice->
k + 4) : 0))
497 else if (rice->
ksum >= (1 << (rice->
k + 5)) && rice->
k < 24)
509 unsigned int x, overflow;
518 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
522 else if (tmpk <= 32) {
529 x += overflow << tmpk;
542 unsigned int x, overflow;
545 pivot = rice->
ksum >> 5;
556 if (pivot < 0x10000) {
560 int base_hi = pivot, base_lo;
563 while (base_hi & ~0xFFFF) {
572 base = (base_hi << bbits) + base_lo;
575 x = base + overflow * pivot;
590 int ksummax, ksummin;
593 for (i = 0; i <
FFMIN(blockstodecode, 5); i++) {
595 rice->
ksum += out[i];
598 for (; i <
FFMIN(blockstodecode, 64); i++) {
600 rice->
ksum += out[i];
603 ksummax = 1 << rice->
k + 7;
604 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
605 for (; i < blockstodecode; i++) {
607 rice->
ksum += out[i] - out[i - 64];
608 while (rice->
ksum < ksummin) {
610 ksummin = rice->
k ? ksummin >> 1 : 0;
613 while (rice->
ksum >= ksummax) {
618 ksummin = ksummin ? ksummin << 1 : 128;
622 for (i = 0; i < blockstodecode; i++) {
624 out[i] = (out[i] >> 1) + 1;
626 out[i] = -(out[i] >> 1);
648 while (blockstodecode--)
656 int blocks = blockstodecode;
658 while (blockstodecode--)
668 while (blockstodecode--)
676 int blocks = blockstodecode;
678 while (blockstodecode--)
693 while (blockstodecode--) {
703 while (blockstodecode--)
712 while (blockstodecode--) {
724 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
732 ctx->
CRC &= ~0x80000000;
813 return (x < 0) - (x > 0);
829 predictionA = p->
buf[delayA] * 2 - p->
buf[delayA - 1];
832 if ((decoded ^ predictionA) > 0)
844 const int delayA,
const int delayB,
845 const int start,
const int shift)
847 int32_t predictionA, predictionB, sign;
860 d1 = (p->
buf[delayA] - p->
buf[delayA - 1]) << 1;
861 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - p->
buf[delayA - 1]) << 3);
862 d3 = p->
buf[delayB] * 2 - p->
buf[delayB - 1];
893 memset(coeffs, 0, order *
sizeof(*coeffs));
894 for (i = 0; i < order; i++)
895 delay[i] = buffer[i];
896 for (i = order; i < length; i++) {
899 for (j = 0; j < order; j++) {
900 dotprod += delay[j] * coeffs[j];
901 coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
903 buffer[i] -= dotprod >> shift;
904 for (j = 0; j < order - 1; j++)
905 delay[j] = delay[j + 1];
906 delay[order - 1] = buffer[i];
916 memset(coeffs, 0,
sizeof(coeffs));
917 memset(delay, 0,
sizeof(delay));
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] >> 30) & 2) - 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;
938 int start = 4, shift = 10;
945 int order = 128,
shift2 = 11;
960 int X = *decoded0,
Y = *decoded1;
993 int start = 4, shift = 10;
999 int order = 128,
shift2 = 11;
1042 d0 = p->
buf[delayA ];
1043 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1044 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1045 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1074 int Y = *decoded1, X = *decoded0;
1116 const int delayA,
const int delayB,
1117 const int adaptA,
const int adaptB)
1119 int32_t predictionA, predictionB, sign;
1123 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1134 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1144 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1194 int32_t predictionA, currentA,
A, sign;
1198 currentA = p->
lastA[0];
1211 currentA = A + (predictionA >> 10);
1232 *(decoded0++) = p->
filterA[0];
1235 p->
lastA[0] = currentA;
1257 int32_t *
data,
int count,
int order,
int fracbits)
1267 res = (res + (1 << (fracbits - 1))) >> fracbits;
1272 *f->
delay++ = av_clip_int16(res);
1274 if (version < 3980) {
1276 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1283 absres =
FFABS(res);
1285 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1286 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1290 f->
avg += (absres - f->
avg) / 16;
1311 int count,
int order,
int fracbits)
1386 left = *decoded1 - (*decoded0 / 2);
1387 right = left + *decoded0;
1389 *(decoded0++) = left;
1390 *(decoded1++) = right;
1395 int *got_frame_ptr,
AVPacket *avpkt)
1411 uint32_t nblocks, offset;
1418 if (avpkt->
size < 8) {
1422 buf_size = avpkt->
size & ~3;
1423 if (buf_size != avpkt->
size) {
1425 "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);
1458 if (!nblocks || nblocks > INT_MAX) {
1515 for (ch = 0; ch < s->
channels; ch++) {
1517 for (i = 0; i < blockstodecode; i++)
1518 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1522 for (ch = 0; ch < s->
channels; ch++) {
1523 sample16 = (int16_t *)frame->
data[ch];
1524 for (i = 0; i < blockstodecode; i++)
1525 *sample16++ = s->
decoded[ch][i];
1529 for (ch = 0; ch < s->
channels; ch++) {
1531 for (i = 0; i < blockstodecode; i++)
1532 *sample24++ = s->
decoded[ch][i] << 8;
1550 #define OFFSET(x) offsetof(APEContext, x)
1551 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1554 {
"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" },