42 #define MAX_CHANNELS 2 43 #define MAX_BYTESPERSAMPLE 3 45 #define APE_FRAMECODE_MONO_SILENCE 1 46 #define APE_FRAMECODE_STEREO_SILENCE 3 47 #define APE_FRAMECODE_PSEUDO_STEREO 4 49 #define HISTORY_SIZE 512 50 #define PREDICTOR_ORDER 8 52 #define PREDICTOR_SIZE 50 54 #define YDELAYA (18 + PREDICTOR_ORDER*4) 55 #define YDELAYB (18 + PREDICTOR_ORDER*3) 56 #define XDELAYA (18 + PREDICTOR_ORDER*2) 57 #define XDELAYB (18 + PREDICTOR_ORDER) 59 #define YADAPTCOEFFSA 18 60 #define XADAPTCOEFFSA 14 61 #define YADAPTCOEFFSB 10 62 #define XADAPTCOEFFSB 5 77 #define APE_FILTER_LEVELS 3 129 uint32_t coeffsA[2][4];
130 uint32_t coeffsB[2][5];
243 "%d bits per coded sample", s->
bps);
314 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1)) 315 #define SHIFT_BITS (CODE_BITS - 9) 316 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1) 317 #define BOTTOM_VALUE (TOP_VALUE >> 8) 390 #define MODEL_ELEMENTS 64 396 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
397 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
398 65450, 65469, 65480, 65487, 65491, 65493,
405 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
406 1104, 677, 415, 248, 150, 89, 54, 31,
414 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
415 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
416 65485, 65488, 65490, 65491, 65492, 65493,
423 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
424 261, 119, 65, 31, 19, 10, 6, 3,
435 const uint16_t counts[],
436 const uint16_t counts_diff[])
443 symbol= cf - 65535 + 63;
450 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
460 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
461 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
463 if (rice->
ksum < lim)
465 else if (rice->
ksum >= (1 << (rice->
k + 5)) && rice->
k < 24)
484 unsigned int x, overflow;
489 while (overflow >= 16) {
498 x = (overflow << rice->
k) +
get_bits(gb, rice->
k);
504 rice->
ksum += x - (rice->
ksum + 8 >> 4);
505 if (rice->
ksum < (rice->
k ? 1 << (rice->
k + 4) : 0))
507 else if (rice->
ksum >= (1 << (rice->
k + 5)) && rice->
k < 24)
511 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
516 unsigned int x, overflow;
525 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
533 }
else if (tmpk <= 31) {
540 x += overflow << tmpk;
545 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
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;
588 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
600 unsigned ksummax, ksummin;
603 for (i = 0; i <
FFMIN(blockstodecode, 5); i++) {
605 rice->
ksum += out[
i];
608 if (blockstodecode <= 5)
614 for (; i <
FFMIN(blockstodecode, 64); i++) {
616 rice->
ksum += out[
i];
622 if (blockstodecode <= 64)
626 ksummax = 1 << rice->
k + 7;
627 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
628 for (; i < blockstodecode; i++) {
634 rice->
ksum += out[
i] - (unsigned)out[i - 64];
635 while (rice->
ksum < ksummin) {
637 ksummin = rice->
k ? ksummin >> 1 : 0;
640 while (rice->
ksum >= ksummax) {
645 ksummin = ksummin ? ksummin << 1 : 128;
650 for (i = 0; i < blockstodecode; i++)
651 out[i] = ((out[i] >> 1) ^ ((out[
i] & 1) - 1)) + 1;
672 while (blockstodecode--)
680 int blocks = blockstodecode;
682 while (blockstodecode--)
692 while (blockstodecode--)
700 int blocks = blockstodecode;
702 while (blockstodecode--)
717 while (blockstodecode--) {
727 while (blockstodecode--)
736 while (blockstodecode--) {
748 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
757 ctx->
CRC &= ~0x80000000;
838 return (x < 0) - (x > 0);
854 predictionA = p->
buf[delayA] * 2
U - p->
buf[delayA - 1];
857 if ((decoded ^ predictionA) > 0)
869 const int delayA,
const int delayB,
870 const int start,
const int shift)
872 int32_t predictionA, predictionB, sign;
885 d1 = (p->
buf[delayA] - (unsigned)p->
buf[delayA - 1]) * 2;
886 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - (unsigned)p->
buf[delayA - 1]) * 8);
887 d3 = p->
buf[delayB] * 2
U - p->
buf[delayB - 1];
921 memset(coeffs, 0, order *
sizeof(*coeffs));
922 for (i = 0; i < order; i++)
923 delay[i] = buffer[i];
924 for (i = order; i < length; i++) {
927 for (j = 0; j < order; j++) {
928 dotprod += delay[j] * (unsigned)coeffs[j];
929 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
931 buffer[
i] -= (unsigned)(dotprod >> shift);
932 for (j = 0; j < order - 1; j++)
933 delay[j] = delay[j + 1];
934 delay[order - 1] = buffer[
i];
943 uint32_t
coeffs[8] = { 0 };
945 for (i = 0; i < length; i++) {
948 for (j = 7; j >= 0; j--) {
949 dotprod += delay[j] * coeffs[j];
950 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
952 for (j = 7; j > 0; j--)
953 delay[j] = delay[j - 1];
954 delay[0] = buffer[
i];
955 buffer[
i] -= (unsigned)(dotprod >> 9);
964 int start = 4,
shift = 10;
971 int order = 128,
shift2 = 11;
986 int X = *decoded0,
Y = *decoded1;
1018 int start = 4,
shift = 10;
1024 int order = 128,
shift2 = 11;
1064 uint32_t d0, d1, d2, d3;
1067 d0 = p->
buf[delayA ];
1068 d1 = p->
buf[delayA ] - (unsigned)p->
buf[delayA - 1];
1069 d2 = p->
buf[delayA - 1] - (
unsigned)p->
buf[delayA - 2];
1070 d3 = p->
buf[delayA - 2] - (unsigned)p->
buf[delayA - 3];
1072 predictionA = d0 * p->
coeffsA[filter][0] +
1077 p->
lastA[filter] = decoded + (predictionA >> 9);
1099 int Y = *decoded1,
X = *decoded0;
1141 const int delayA,
const int delayB,
1142 const int adaptA,
const int adaptB)
1144 int32_t predictionA, predictionB, sign;
1148 p->
buf[delayA - 1] = p->
buf[delayA] - (unsigned)p->
buf[delayA - 1];
1159 p->
buf[delayB - 1] = p->
buf[delayB] - (unsigned)p->
buf[delayB - 1];
1169 p->
lastA[
filter] = decoded + ((
int)((
unsigned)predictionA + (predictionB >> 1)) >> 10);
1219 int32_t predictionA, currentA,
A, sign;
1223 currentA = p->
lastA[0];
1236 currentA = A + (
unsigned)(predictionA >> 10);
1256 p->
filterA[0] = currentA + (unsigned)((
int)(p->
filterA[0] * 31
U) >> 5);
1257 *(decoded0++) = p->
filterA[0];
1260 p->
lastA[0] = currentA;
1282 int32_t *
data,
int count,
int order,
int fracbits)
1293 res = (
int)(res + (1
U << (fracbits - 1))) >> fracbits;
1294 res += (unsigned)*data;
1298 *f->
delay++ = av_clip_int16(res);
1300 if (version < 3980) {
1302 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1309 absres = res < 0 ? -(unsigned)res : res;
1312 (8 << ((absres > f->
avg * 3LL) + (absres > (f->
avg + f->
avg / 3))));
1324 f->
avg += (
int)(absres - (
unsigned)f->
avg) / 16;
1345 int count,
int order,
int fracbits)
1405 unsigned left, right;
1424 left = *decoded1 - (unsigned)(*decoded0 / 2);
1425 right = left + *decoded0;
1427 *(decoded0++) = left;
1428 *(decoded1++) = right;
1433 int *got_frame_ptr,
AVPacket *avpkt)
1443 uint64_t decoded_buffer_size;
1450 uint32_t nblocks,
offset;
1457 if (avpkt->
size < 8) {
1461 buf_size = avpkt->
size & ~3;
1462 if (buf_size != avpkt->
size) {
1464 "extra bytes at the end will be skipped.\n");
1473 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1477 nblocks = bytestream_get_be32(&s->
ptr);
1478 offset = bytestream_get_be32(&s->
ptr);
1500 if (!nblocks || nblocks > INT_MAX / 2 /
sizeof(*s->
decoded_buffer) - 8) {
1559 for (ch = 0; ch < s->
channels; ch++) {
1561 for (i = 0; i < blockstodecode; i++)
1562 *sample8++ = (s->
decoded[ch][i] + 0x80U) & 0xff;
1566 for (ch = 0; ch < s->
channels; ch++) {
1567 sample16 = (int16_t *)frame->
data[ch];
1568 for (i = 0; i < blockstodecode; i++)
1569 *sample16++ = s->
decoded[ch][i];
1573 for (ch = 0; ch < s->
channels; ch++) {
1575 for (i = 0; i < blockstodecode; i++)
1576 *sample24++ = s->
decoded[ch][i] * 256
U;
1589 int bytes = s->
bps >> 3;
1591 for (i = 0; i < blockstodecode; i++) {
1592 for (ch = 0; ch < s->
channels; ch++) {
1596 for(
int j = 0; j<
stride; j++)
1597 smp[j] = smp_native[stride-j-1];
1601 crc =
av_crc(crc_tab, crc, smp+offset, bytes);
1607 "frames may have been affected as well.\n");
1626 #define OFFSET(x) offsetof(APEContext, x) 1627 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM) 1630 {
"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" },
static int init_frame_decoder(APEContext *ctx)
static const int32_t initial_coeffs_3930[4]
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, int32_t *out, APERice *rice, int blockstodecode)
int compression_level
compression levels
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
static void range_start_decoding(APEContext *ctx)
Start the decoder.
static void flush(AVCodecContext *avctx)
static void apply_filter(APEContext *ctx, APEFilter *f, int32_t *data0, int32_t *data1, int count, int order, int fracbits)
int fileversion
codec version, very important in decoding process
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
void(* entropy_decode_mono)(struct APEContext *ctx, int blockstodecode)
static av_cold int init(AVCodecContext *avctx)
#define avpriv_request_sample(...)
void(* entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode)
static int APESIGN(int32_t x)
Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)
static void update_rice(APERice *rice, unsigned int x)
static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
const char * av_default_item_name(void *ptr)
Return the context name.
static av_cold int ape_decode_init(AVCodecContext *avctx)
unsigned int buffer
buffer for input/output
static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
static int init_entropy_decoder(APEContext *ctx)
static void ape_flush(AVCodecContext *avctx)
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...
static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
static int get_k(int ksum)
static av_always_inline int predictor_update_3930(APEPredictor *p, const int decoded, const int filter, const int delayA)
#define AV_CH_LAYOUT_STEREO
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
static void predictor_decode_mono_3800(APEContext *ctx, int count)
uint32_t CRC_state
accumulated CRC
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int ape_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
enum AVSampleFormat sample_fmt
audio sample format
int16_t * delay
filtered values
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
static av_cold int end(AVCodecContext *avctx)
static const int32_t initial_coeffs_a_3800[3]
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
static void ape_unpack_mono(APEContext *ctx, int count)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
APERangecoder rc
rangecoder used to decode actual values
Public header for CRC hash function implementation.
static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS]
Filter fraction bits depending on compression level.
static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, int32_t *decoded1, int count)
bitstream reader API header.
#define AV_LOG_VERBOSE
Detailed information.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static const uint16_t counts_3970[22]
Fixed probabilities for symbols in Monkey Audio version 3.97.
static void range_dec_normalize(APEContext *ctx)
Perform normalization.
static int get_bits_left(GetBitContext *gb)
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
#define i(width, name, range_min, range_max)
void(* predictor_decode_mono)(struct APEContext *ctx, int count)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold int ape_decode_close(AVCodecContext *avctx)
static int ape_decode_value_3900(APEContext *ctx, APERice *rice)
int32_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
#define APE_FILTER_LEVELS
static const uint8_t offset[127][2]
uint64_t channel_layout
Audio channel layout.
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
audio channel layout utility functions
static void predictor_decode_mono_3930(APEContext *ctx, int count)
uint8_t * data
current frame data
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
static int get_rice_ook(GetBitContext *gb, int k)
static av_always_inline int filter_fast_3320(APEPredictor *p, const int decoded, const int filter, const int delayA)
static void ape_unpack_stereo(APEContext *ctx, int count)
const uint8_t * ptr
current position in frame data
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate cumulative frequency for next symbol.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AV_EF_EXPLODE
abort decoding on minor error detection
int32_t(* scalarproduct_and_madd_int16)(int16_t *v1, const int16_t *v2, const int16_t *v3, int len, int mul)
Calculate scalar product of v1 and v2, and v1[i] += v3[i] * mul.
static void predictor_decode_stereo_3930(APEContext *ctx, int count)
av_cold void ff_llauddsp_init(LLAudDSPContext *c)
uint32_t help
bytes_to_follow resp. intermediate value
uint32_t coeffsA[2][4]
adaption coefficients
static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
#define APE_FRAMECODE_PSEUDO_STEREO
uint32_t range
length of interval
int samples
samples left to decode in current frame
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int fset
which filter set to use (calculated from compression level)
static int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, APERice *rice)
APERice riceX
rice code parameters for the second channel
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
static void predictor_decode_stereo_3950(APEContext *ctx, int count)
typedef void(RENAME(mix_any_func_type))
static void predictor_decode_stereo_3800(APEContext *ctx, int count)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define APE_FRAMECODE_STEREO_SILENCE
static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
int frameflags
frame flags
main external API structure.
static av_always_inline int filter_3800(APEPredictor *p, const unsigned decoded, const int filter, const int delayA, const int delayB, const int start, const int shift)
static int ape_decode_value_3990(APEContext *ctx, APERice *rice)
uint32_t CRC
signalled frame CRC
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static const uint16_t counts_3980[22]
Fixed probabilities for symbols in Monkey Audio version 3.98.
static int range_get_symbol(APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
Decode symbol.
Describe the class of an AVClass context structure.
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
uint32_t low
low end of interval
int flags
global decoder flags
APECompressionLevel
Possible compression levels.
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
void(* predictor_decode_stereo)(struct APEContext *ctx, int count)
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static const int32_t initial_coeffs_fast_3320[1]
static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define PREDICTOR_SIZE
Total size of all predictor histories.
static const uint16_t counts_diff_3970[21]
Probability ranges for symbols in Monkey Audio version 3.97.
int blocks_per_loop
maximum number of samples to decode for each call
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint8_t * data_end
frame data end
common internal api header.
APERice riceY
rice code parameters for the first channel
static const int shift2[6]
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
APEFilter filters[APE_FILTER_LEVELS][2]
filters used for reconstruction
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)
int16_t * coeffs
actual coefficients used in filtering
static void init_predictor_decoder(APEContext *ctx)
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
static const int32_t initial_coeffs_b_3800[2]
APEPredictor predictor
predictor used for final reconstruction
static const AVClass ape_decoder_class
int channels
number of audio channels
static void long_filter_ehigh_3830(int32_t *buffer, int length)
static void predictor_decode_mono_3950(APEContext *ctx, int count)
Filters applied to the decoded data.
static const struct PPFilter filters[]
uint32_t coeffsB[2][5]
adaption coefficients
static enum AVSampleFormat sample_fmts[]
int32_t * decoded[MAX_CHANNELS]
decoded data for each channel
int data_size
frame data allocated size
static const AVOption options[]
#define AV_CH_LAYOUT_MONO
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
int16_t * historybuffer
filter memory
static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)