36 #define MAX_CHANNELS 2
37 #define MAX_BYTESPERSAMPLE 3
39 #define APE_FRAMECODE_MONO_SILENCE 1
40 #define APE_FRAMECODE_STEREO_SILENCE 3
41 #define APE_FRAMECODE_PSEUDO_STEREO 4
43 #define HISTORY_SIZE 512
44 #define PREDICTOR_ORDER 8
46 #define PREDICTOR_SIZE 50
48 #define YDELAYA (18 + PREDICTOR_ORDER*4)
49 #define YDELAYB (18 + PREDICTOR_ORDER*3)
50 #define XDELAYA (18 + PREDICTOR_ORDER*2)
51 #define XDELAYB (18 + PREDICTOR_ORDER)
53 #define YADAPTCOEFFSA 18
54 #define XADAPTCOEFFSA 14
55 #define YADAPTCOEFFSB 10
56 #define XADAPTCOEFFSB 5
71 #define APE_FILTER_LEVELS 3
253 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
254 #define SHIFT_BITS (CODE_BITS - 9)
255 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
256 #define BOTTOM_VALUE (TOP_VALUE >> 8)
329 #define MODEL_ELEMENTS 64
335 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
336 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
337 65450, 65469, 65480, 65487, 65491, 65493,
344 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
345 1104, 677, 415, 248, 150, 89, 54, 31,
353 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
354 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
355 65485, 65488, 65490, 65491, 65492, 65493,
362 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
363 261, 119, 65, 31, 19, 10, 6, 3,
374 const uint16_t counts[],
375 const uint16_t counts_diff[])
382 symbol= cf - 65535 + 63;
389 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
399 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
400 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
402 if (rice->
ksum < lim)
404 else if (rice->
ksum >= (1 << (rice->
k + 5)))
410 unsigned int x, overflow;
421 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
425 else if (tmpk <= 32) {
432 x += overflow << tmpk;
436 pivot = rice->
ksum >> 5;
447 if (pivot < 0x10000) {
451 int base_hi = pivot, base_lo;
454 while (base_hi & ~0xFFFF) {
463 base = (base_hi << bbits) + base_lo;
466 x = base + overflow * pivot;
483 while (blockstodecode--) {
495 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
500 ctx->
CRC &= ~0x80000000;
545 return (x < 0) - (x > 0);
549 const int decoded,
const int filter,
550 const int delayA,
const int delayB,
551 const int adaptA,
const int adaptB)
553 int32_t predictionA, predictionB, sign;
557 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
568 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
578 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
626 int32_t predictionA, currentA,
A, sign;
628 currentA = p->
lastA[0];
641 currentA = A + (predictionA >> 10);
665 p->
lastA[0] = currentA;
697 res = (res + (1 << (fracbits - 1))) >> fracbits;
702 *f->
delay++ = av_clip_int16(res);
704 if (version < 3980) {
706 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
716 (25 + (absres <= f->
avg*3) + (absres <= f->
avg*4/3));
720 f->
avg += (absres - f->
avg) / 16;
741 int count,
int order,
int fracbits)
818 left = *decoded1 - (*decoded0 / 2);
819 right = left + *decoded0;
821 *(decoded0++) = left;
822 *(decoded1++) = right;
827 int *got_frame_ptr,
AVPacket *avpkt)
843 uint32_t nblocks, offset;
850 if (avpkt->
size < 8) {
854 buf_size = avpkt->
size & ~3;
855 if (buf_size != avpkt->
size) {
857 "extra bytes at the end will be skipped.\n");
867 nblocks = bytestream_get_be32(&s->
ptr);
868 offset = bytestream_get_be32(&s->
ptr);
880 if (!nblocks || nblocks > INT_MAX) {
892 bytes_used = avpkt->
size;
934 for (ch = 0; ch < s->
channels; ch++) {
936 for (i = 0; i < blockstodecode; i++)
937 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
941 for (ch = 0; ch < s->
channels; ch++) {
943 for (i = 0; i < blockstodecode; i++)
944 *sample16++ = s->
decoded[ch][i];
948 for (ch = 0; ch < s->
channels; ch++) {
950 for (i = 0; i < blockstodecode; i++)
951 *sample24++ = s->
decoded[ch][i] << 8;
970 #define OFFSET(x) offsetof(APEContext, x)
971 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
973 {
"max_samples",
"maximum number of samples decoded per call",
OFFSET(blocks_per_loop),
AV_OPT_TYPE_INT, { .i64 = 4608 }, 1, INT_MAX,
PAR,
"max_samples" },
974 {
"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 void entropy_decode(APEContext *ctx, int blockstodecode, int stereo)
int compression_level
compression levels
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
int32_t coeffsB[2][5]
adaption coefficients
This structure describes decoded (raw) audio or video 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)
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 void update_rice(APERice *rice, unsigned int x)
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)
#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 ...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
#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)
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)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
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.
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
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 void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
struct APEPredictor APEPredictor
Filter histories.
audio channel layout utility functions
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)
Get a buffer for a frame.
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate culmulative frequency for next symbol.
#define CODEC_CAP_SUBFRAMES
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)
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.
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Describe the class of an AVClass context structure.
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.
int blocks_per_loop
maximum number of samples to decode for each call
static const int32_t initial_coeffs[4]
uint8_t * data_end
frame data end
common internal api header.
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
#define AVERROR_INVALIDDATA
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)
AVSampleFormat
Audio Sample Formats.
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
#define AVERROR_PATCHWELCOME
static void init_predictor_decoder(APEContext *ctx)
APEPredictor predictor
predictor used for final reconstruction
static const AVClass ape_decoder_class
int channels
number of audio channels
Filters applied to the decoded data.
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
int16_t * historybuffer
filter memory
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)