23 #define BITSTREAM_READER_LE
37 #define BLOCKS_PER_LOOP 4608
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
236 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
237 #define SHIFT_BITS (CODE_BITS - 9)
238 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
239 #define BOTTOM_VALUE (TOP_VALUE >> 8)
312 #define MODEL_ELEMENTS 64
318 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
319 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
320 65450, 65469, 65480, 65487, 65491, 65493,
327 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
328 1104, 677, 415, 248, 150, 89, 54, 31,
336 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
337 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
338 65485, 65488, 65490, 65491, 65492, 65493,
345 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
346 261, 119, 65, 31, 19, 10, 6, 3,
357 const uint16_t counts[],
358 const uint16_t counts_diff[])
365 symbol= cf - 65535 + 63;
372 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
382 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
383 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
385 if (rice->
ksum < lim)
387 else if (rice->
ksum >= (1 << (rice->
k + 5)))
404 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
408 else if (tmpk <= 32) {
415 x += overflow << tmpk;
419 pivot = rice->
ksum >> 5;
430 if (pivot < 0x10000) {
434 int base_hi = pivot, base_lo;
437 while (base_hi & ~0xFFFF) {
446 base = (base_hi << bbits) + base_lo;
449 x = base + overflow * pivot;
468 memset(decoded0, 0, blockstodecode *
sizeof(int32_t));
469 memset(decoded1, 0, blockstodecode *
sizeof(int32_t));
471 while (blockstodecode--) {
484 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
489 ctx->
CRC &= ~0x80000000;
534 return (x < 0) - (x > 0);
538 const int decoded,
const int filter,
539 const int delayA,
const int delayB,
540 const int adaptA,
const int adaptB)
542 int32_t predictionA, predictionB, sign;
546 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
557 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
567 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
614 int32_t predictionA, currentA,
A, sign;
616 currentA = p->
lastA[0];
629 currentA = A + (predictionA >> 10);
652 p->
lastA[0] = currentA;
663 memset(f->
coeffs, 0, order *
sizeof(int16_t));
674 int32_t *
data,
int count,
int order,
int fracbits)
684 res = (res + (1 << (fracbits - 1))) >> fracbits;
689 *f->
delay++ = av_clip_int16(res);
691 if (version < 3980) {
693 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
703 (25 + (absres <= f->
avg*3) + (absres <= f->
avg*4/3));
707 f->
avg += (absres - f->
avg) / 16;
719 (order * 2) *
sizeof(int16_t));
727 int32_t *data0, int32_t *data1,
728 int count,
int order,
int fracbits)
736 int32_t *decoded1,
int count)
785 memcpy(decoded1, decoded0, count *
sizeof(*decoded1));
809 left = *decoded1 - (*decoded0 / 2);
810 right = left + *decoded0;
812 *(decoded0++) = left;
813 *(decoded1++) = right;
818 int *got_frame_ptr,
AVPacket *avpkt)
820 const uint8_t *buf = avpkt->
data;
821 int buf_size = avpkt->
size;
832 uint32_t nblocks, offset;
852 nblocks = bytestream_get_be32(&s->
ptr);
853 offset = bytestream_get_be32(&s->
ptr);
865 if (!nblocks || nblocks > INT_MAX) {
911 for (i = 0; i < blockstodecode; i++) {
922 return (s->
samples == 0) ? buf_size : 0;
static int init_frame_decoder(APEContext *ctx)
static void entropy_decode(APEContext *ctx, int blockstodecode, int stereo)
int compression_level
compression levels
int32_t coeffsB[2][5]
adaption coefficients
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
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)
int fileversion
codec version, very important in decoding process
AVFrame * coded_frame
the picture in the bitstream
static int APESIGN(int32_t x)
Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)
static av_cold int ape_decode_init(AVCodecContext *avctx)
unsigned int buffer
buffer for input/output
static int init_entropy_decoder(APEContext *ctx)
static void ape_flush(AVCodecContext *avctx)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
#define AV_CH_LAYOUT_STEREO
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static void update_rice(APERice *rice, int x)
#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 int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
int32_t decoded1[BLOCKS_PER_LOOP]
decoded data for the second channel
enum AVSampleFormat sample_fmt
audio sample format
int16_t * delay
filtered values
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
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
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.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
static int init(AVCodecParserContext *s)
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.
audio conversion routines
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
static av_cold int ape_decode_close(AVCodecContext *avctx)
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
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. ...
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
struct APEContext APEContext
Decoder context.
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
#define APE_FILTER_LEVELS
static void predictor_decode_mono(APEContext *ctx, int count)
uint64_t channel_layout
Audio channel layout.
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
static av_always_inline void emms_c(void)
Empty mmx state.
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
struct APEPredictor APEPredictor
Filter histories.
uint8_t * data
current frame data
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
static void ape_unpack_stereo(APEContext *ctx, int count)
const uint8_t * ptr
current position in frame data
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate culmulative frequency for next symbol.
uint32_t help
bytes_to_follow resp. intermediate value
#define APE_FRAMECODE_PSEUDO_STEREO
uint32_t range
length of interval
int samples
samples left to decode in current frame
int fset
which filter set to use (calculated from compression level)
APERice riceX
rice code parameters for the second channel
static void predictor_decode_stereo(APEContext *ctx, int count)
static int ape_decode_value(APEContext *ctx, APERice *rice)
#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 void close(AVCodecParserContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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.
uint32_t low
low end of interval
int flags
global decoder flags
APECompressionLevel
Possible compression levels.
int32_t coeffsA[2][4]
adaption coefficients
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
struct APEFilter APEFilter
Filters applied to the decoded data.
static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
struct APERangecoder APERangecoder
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.
static const int32_t initial_coeffs[4]
uint8_t * data_end
frame data end
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
APERice riceY
rice code parameters for the first channel
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
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)
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.
int16_t * coeffs
actual coefficients used in filtering
int32_t decoded0[BLOCKS_PER_LOOP]
decoded data for the first channel
static void init_predictor_decoder(APEContext *ctx)
APEPredictor predictor
predictor used for final reconstruction
int channels
number of audio channels
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Filters applied to the decoded data.
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CH_LAYOUT_MONO
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
int nb_samples
number of audio samples (per channel) described by this frame
int16_t * historybuffer
filter memory