Go to the documentation of this file.
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
210 s->decoded_size =
s->data_size = 0;
241 "%d bits per coded sample",
s->bps);
251 s->compression_level,
s->flags);
253 !
s->compression_level ||
256 s->compression_level);
259 s->fset =
s->compression_level / 1000 - 1;
268 if (
s->fileversion < 3860) {
271 }
else if (
s->fileversion < 3900) {
274 }
else if (
s->fileversion < 3930) {
277 }
else if (
s->fileversion < 3990) {
285 if (
s->fileversion < 3930) {
288 }
else if (
s->fileversion < 3950) {
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)
320 ctx->rc.buffer = bytestream_get_byte(&
ctx->ptr);
329 ctx->rc.buffer <<= 8;
330 if(
ctx->ptr <
ctx->data_end) {
331 ctx->rc.buffer += *
ctx->ptr;
336 ctx->rc.low = (
ctx->rc.low << 8) | ((
ctx->rc.buffer >> 1) & 0xFF);
350 ctx->rc.help =
ctx->rc.range / tot_f;
351 return ctx->rc.low /
ctx->rc.help;
363 return ctx->rc.low /
ctx->rc.help;
375 ctx->rc.low -=
ctx->rc.help * lt_f;
376 ctx->rc.range =
ctx->rc.help * sy_f;
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)) && rice->
k < 24)
486 if (
ctx->fileversion > 3880) {
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;
522 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
530 }
else if (tmpk <= 31) {
542 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
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;
585 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
592 unsigned ksummax, ksummin;
595 for (
i = 0;
i <
FFMIN(blockstodecode, 5);
i++) {
602 for (;
i <
FFMIN(blockstodecode, 64);
i++) {
609 ksummax = 1 << rice->
k + 7;
610 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
611 for (;
i < blockstodecode;
i++) {
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++)
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--) {
723 if (
ctx->fileversion >= 3900) {
724 if (
ctx->data_end -
ctx->ptr < 6)
726 ctx->CRC = bytestream_get_be32(&
ctx->ptr);
733 if ((
ctx->fileversion > 3820) && (
ctx->CRC & 0x80000000)) {
734 ctx->CRC &= ~0x80000000;
736 if (
ctx->data_end -
ctx->ptr < 6)
738 ctx->frameflags = bytestream_get_be32(&
ctx->ptr);
743 ctx->riceX.ksum = (1 <<
ctx->riceX.k) * 16;
745 ctx->riceY.ksum = (1 <<
ctx->riceY.k) * 16;
747 if (
ctx->fileversion >= 3900) {
782 if (
ctx->fileversion < 3930) {
799 if (
ctx->fileversion < 3930) {
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] - (unsigned)p->
buf[delayA - 1]) * 2;
863 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - (unsigned)p->
buf[delayA - 1]) * 8);
864 d3 = p->
buf[delayB] * 2
U - 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++)
904 for (j = 0; j < order; j++) {
905 dotprod += delay[j] * (unsigned)coeffs[j];
906 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
909 for (j = 0; j < order - 1; j++)
910 delay[j] = delay[j + 1];
920 uint32_t coeffs[8] = { 0 };
925 for (j = 7; j >= 0; j--) {
926 dotprod += delay[j] * coeffs[j];
927 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
929 for (j = 7; j > 0; j--)
930 delay[j] = delay[j - 1];
932 buffer[
i] -= (unsigned)(dotprod >> 9);
948 int order = 128,
shift2 = 11;
950 if (
ctx->fileversion >= 3830) {
963 int X = *decoded0,
Y = *decoded1;
1001 int order = 128,
shift2 = 11;
1003 if (
ctx->fileversion >= 3830) {
1041 uint32_t d0, d1, d2, d3;
1044 d0 = p->
buf[delayA ];
1045 d1 = p->
buf[delayA ] - (unsigned)p->
buf[delayA - 1];
1046 d2 = p->
buf[delayA - 1] - (
unsigned)p->
buf[delayA - 2];
1047 d3 = p->
buf[delayA - 2] - (unsigned)p->
buf[delayA - 3];
1076 int Y = *decoded1, X = *decoded0;
1118 const int delayA,
const int delayB,
1119 const int adaptA,
const int adaptB)
1121 int32_t predictionA, predictionB, sign;
1125 p->
buf[delayA - 1] = p->
buf[delayA] - (unsigned)p->
buf[delayA - 1];
1136 p->
buf[delayB - 1] = p->
buf[delayB] - (unsigned)p->
buf[delayB - 1];
1196 int32_t predictionA, currentA,
A, sign;
1200 currentA = p->
lastA[0];
1213 currentA =
A + (
unsigned)(predictionA >> 10);
1233 p->
filterA[0] = currentA + (unsigned)((
int)(p->
filterA[0] * 31
U) >> 5);
1234 *(decoded0++) = p->
filterA[0];
1237 p->
lastA[0] = currentA;
1243 f->historybuffer =
buf + order;
1244 f->delay =
f->historybuffer + order * 2;
1245 f->adaptcoeffs =
f->historybuffer + order;
1247 memset(
f->historybuffer, 0, (order * 2) *
sizeof(*
f->historybuffer));
1248 memset(
f->coeffs, 0, order *
sizeof(*
f->coeffs));
1266 res =
ctx->adsp.scalarproduct_and_madd_int16(
f->coeffs,
1268 f->adaptcoeffs - order,
1270 res = (
int)(res + (1
U << (fracbits - 1))) >> fracbits;
1271 res += (unsigned)*
data;
1275 *
f->delay++ = av_clip_int16(res);
1279 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1280 f->adaptcoeffs[-4] >>= 1;
1281 f->adaptcoeffs[-8] >>= 1;
1286 absres = res < 0 ? -(unsigned)res : res;
1289 (8 << ((absres >
f->avg * 3LL) + (absres > (
f->avg +
f->avg / 3))));
1299 *
f->adaptcoeffs = 0;
1301 f->avg += (
int)(absres - (
unsigned)
f->avg) / 16;
1303 f->adaptcoeffs[-1] >>= 1;
1304 f->adaptcoeffs[-2] >>= 1;
1305 f->adaptcoeffs[-8] >>= 1;
1312 memmove(
f->historybuffer,
f->delay - (order * 2),
1313 (order * 2) *
sizeof(*
f->historybuffer));
1314 f->delay =
f->historybuffer + order * 2;
1315 f->adaptcoeffs =
f->historybuffer + order;
1322 int count,
int order,
int fracbits)
1373 if (
ctx->channels == 2) {
1374 memcpy(
ctx->decoded[1],
ctx->decoded[0],
count *
sizeof(*
ctx->decoded[1]));
1380 unsigned left, right;
1397 left = *decoded1 - (unsigned)(*decoded0 / 2);
1398 right =
left + *decoded0;
1400 *(decoded0++) =
left;
1401 *(decoded1++) = right;
1406 int *got_frame_ptr,
AVPacket *avpkt)
1416 uint64_t decoded_buffer_size;
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");
1439 if (
s->fileversion < 3950)
1444 s->bdsp.bswap_buf((uint32_t *)
s->data, (
const uint32_t *)
buf,
1446 memset(
s->data + (buf_size & ~3), 0, buf_size & 3);
1448 s->data_end =
s->data + buf_size;
1450 nblocks = bytestream_get_be32(&
s->ptr);
1451 offset = bytestream_get_be32(&
s->ptr);
1452 if (
s->fileversion >= 3900) {
1459 if (
s->data_end -
s->ptr <
offset) {
1467 if (
s->fileversion > 3800)
1473 if (!nblocks || nblocks > INT_MAX / 2 /
sizeof(*
s->decoded_buffer) - 8) {
1484 s->samples = nblocks;
1492 blockstodecode =
FFMIN(
s->blocks_per_loop,
s->samples);
1495 if (
s->fileversion < 3930)
1496 blockstodecode =
s->samples;
1499 decoded_buffer_size = 2LL *
FFALIGN(blockstodecode, 8) *
sizeof(*
s->decoded_buffer);
1502 if (!
s->decoded_buffer)
1504 memset(
s->decoded_buffer, 0, decoded_buffer_size);
1505 s->decoded[0] =
s->decoded_buffer;
1506 s->decoded[1] =
s->decoded_buffer +
FFALIGN(blockstodecode, 8);
1509 frame->nb_samples = blockstodecode;
1529 for (
ch = 0;
ch <
s->channels;
ch++) {
1531 for (
i = 0;
i < blockstodecode;
i++)
1532 *sample8++ = (
s->decoded[
ch][
i] + 0x80U) & 0xff;
1536 for (
ch = 0;
ch <
s->channels;
ch++) {
1537 sample16 = (int16_t *)
frame->data[
ch];
1538 for (
i = 0;
i < blockstodecode;
i++)
1539 *sample16++ =
s->decoded[
ch][
i];
1543 for (
ch = 0;
ch <
s->channels;
ch++) {
1545 for (
i = 0;
i < blockstodecode;
i++)
1546 *sample24++ =
s->decoded[
ch][
i] * 256
U;
1551 s->samples -= blockstodecode;
1555 return !
s->samples ? avpkt->
size : 0;
1564 #define OFFSET(x) offsetof(APEContext, x)
1565 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1568 {
"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" },
APERice riceX
rice code parameters for the second channel
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
static int init_frame_decoder(APEContext *ctx)
uint8_t * data
current frame data
static void range_start_decoding(APEContext *ctx)
Start the decoder.
static void apply_filter(APEContext *ctx, APEFilter *f, int32_t *data0, int32_t *data1, int count, int order, int fracbits)
#define PREDICTOR_SIZE
Total size of all predictor histories.
static av_cold int init(AVCodecContext *avctx)
static int get_bits_left(GetBitContext *gb)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
uint64_t channel_layout
Audio channel layout.
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
int32_t coeffsB[2][5]
adaption coefficients
static enum AVSampleFormat sample_fmts[]
#define APE_FILTER_LEVELS
uint32_t low
low end of interval
static av_always_inline int predictor_update_filter(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
#define AV_CH_LAYOUT_MONO
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
static int init_entropy_decoder(APEContext *ctx)
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
This structure describes decoded (raw) audio or video data.
static const AVClass ape_decoder_class
static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
static void predictor_decode_mono_3930(APEContext *ctx, int count)
APEFilter filters[APE_FILTER_LEVELS][2]
filters used for reconstruction
static void long_filter_ehigh_3830(int32_t *buffer, int length)
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
static void update_rice(APERice *rice, unsigned int x)
static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
#define AV_LOG_VERBOSE
Detailed information.
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
static void ape_flush(AVCodecContext *avctx)
void(* predictor_decode_mono)(struct APEContext *ctx, int count)
static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS]
Filter fraction bits depending on compression level.
int compression_level
compression levels
void(* entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode)
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
av_cold void ff_llauddsp_init(LLAudDSPContext *c)
static void predictor_decode_stereo_3930(APEContext *ctx, int count)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a it should return
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void predictor_decode_mono_3800(APEContext *ctx, int count)
static av_cold int ape_decode_init(AVCodecContext *avctx)
static void ape_unpack_mono(APEContext *ctx, int count)
int fileversion
codec version, very important in decoding process
static int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, APERice *rice)
static av_always_inline int filter_3800(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int start, const int shift)
static void predictor_decode_stereo_3800(APEContext *ctx, int count)
#define AV_CH_LAYOUT_STEREO
static const AVOption options[]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
APERangecoder rc
rangecoder used to decode actual values
int samples
samples left to decode in current frame
const uint8_t * ptr
current position in frame data
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int16_t * historybuffer
filter memory
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
#define APE_FRAMECODE_STEREO_SILENCE
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
unsigned int buffer
buffer for input/output
static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
int fset
which filter set to use (calculated from compression level)
static int ape_decode_value_3900(APEContext *ctx, APERice *rice)
int32_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
#define LIBAVUTIL_VERSION_INT
int frameflags
frame flags
Describe the class of an AVClass context structure.
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
static void flush(AVCodecContext *avctx)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
static const uint16_t counts_3970[22]
Fixed probabilities for symbols in Monkey Audio version 3.97.
const char * av_default_item_name(void *ptr)
Return the context name.
static av_always_inline int predictor_update_3930(APEPredictor *p, const int decoded, const int filter, const int delayA)
static void init_predictor_decoder(APEContext *ctx)
int32_t coeffsA[2][4]
adaption coefficients
@ COMPRESSION_LEVEL_EXTRA_HIGH
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate cumulative frequency for next symbol.
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
#define APE_FRAMECODE_PSEUDO_STEREO
void(* entropy_decode_mono)(struct APEContext *ctx, int blockstodecode)
enum AVSampleFormat sample_fmt
audio sample format
int16_t * coeffs
actual coefficients used in filtering
int16_t * delay
filtered values
uint32_t range
length of interval
static const int32_t initial_coeffs_a_3800[3]
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
void(* predictor_decode_stereo)(struct APEContext *ctx, int count)
static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
static int ape_decode_value_3990(APEContext *ctx, APERice *rice)
static const int shift2[6]
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, int32_t *out, APERice *rice, int blockstodecode)
static int range_get_symbol(APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
Decode symbol.
int channels
number of audio channels
static void ape_unpack_stereo(APEContext *ctx, int count)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define i(width, name, range_min, range_max)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
AVSampleFormat
Audio sample formats.
static void range_dec_normalize(APEContext *ctx)
Perform normalization.
static const int32_t initial_coeffs_fast_3320[1]
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
@ COMPRESSION_LEVEL_INSANE
static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
const char * name
Name of the codec implementation.
uint32_t help
bytes_to_follow resp. intermediate value
APECompressionLevel
Possible compression levels.
static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
static av_cold int ape_decode_close(AVCodecContext *avctx)
static void predictor_decode_stereo_3950(APEContext *ctx, int count)
static void predictor_decode_mono_3950(APEContext *ctx, int count)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static int get_rice_ook(GetBitContext *gb, int k)
int data_size
frame data allocated size
APEPredictor predictor
predictor used for final reconstruction
Filters applied to the decoded data.
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
main external API structure.
static const uint16_t counts_3980[22]
Fixed probabilities for symbols in Monkey Audio version 3.98.
@ COMPRESSION_LEVEL_NORMAL
static const uint16_t counts_diff_3970[21]
Probability ranges for symbols in Monkey Audio version 3.97.
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
static const int32_t initial_coeffs_b_3800[2]
int32_t * decoded[MAX_CHANNELS]
decoded data for each channel
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
static int APESIGN(int32_t x)
Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)
APERice riceY
rice code parameters for the first channel
uint8_t * data_end
frame data end
static int shift(int a, int b)
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
static const int32_t initial_coeffs_3930[4]
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
Undefined Behavior In the C some operations are like signed integer overflow
static int ape_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
#define avpriv_request_sample(...)
int blocks_per_loop
maximum number of samples to decode for each call
static av_always_inline int filter_fast_3320(APEPredictor *p, const int decoded, const int filter, const int delayA)
int flags
global decoder flags
This structure stores compressed data.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, int32_t *decoded1, int count)
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)