apedec.c File Reference

#include "avcodec.h"
#include "dsputil.h"
#include "bitstream.h"
#include "bytestream.h"

Go to the source code of this file.

Data Structures

struct  APEFilter
struct  APERice
struct  APERangecoder
struct  APEPredictor
struct  APEContext

Defines

#define ALT_BITSTREAM_READER_LE
#define BLOCKS_PER_LOOP   4608
#define MAX_CHANNELS   2
#define MAX_BYTESPERSAMPLE   3
#define APE_FRAMECODE_MONO_SILENCE   1
#define APE_FRAMECODE_STEREO_SILENCE   3
#define APE_FRAMECODE_PSEUDO_STEREO   4
#define HISTORY_SIZE   512
#define PREDICTOR_ORDER   8
#define PREDICTOR_SIZE   50
#define YDELAYA   (18 + PREDICTOR_ORDER*4)
#define YDELAYB   (18 + PREDICTOR_ORDER*3)
#define XDELAYA   (18 + PREDICTOR_ORDER*2)
#define XDELAYB   (18 + PREDICTOR_ORDER)
#define YADAPTCOEFFSA   18
#define XADAPTCOEFFSA   14
#define YADAPTCOEFFSB   10
#define XADAPTCOEFFSB   5
#define APE_FILTER_LEVELS   3
#define CODE_BITS   32
#define TOP_VALUE   ((unsigned int)1 << (CODE_BITS-1))
#define SHIFT_BITS   (CODE_BITS - 9)
#define EXTRA_BITS   ((CODE_BITS-2) % 8 + 1)
#define BOTTOM_VALUE   (TOP_VALUE >> 8)
#define MODEL_ELEMENTS   64

Enumerations

enum  APECompressionLevel {
  COMPRESSION_LEVEL_FAST = 1000, COMPRESSION_LEVEL_NORMAL = 2000, COMPRESSION_LEVEL_HIGH = 3000, COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
  COMPRESSION_LEVEL_INSANE = 5000
}

Functions

static av_cold int ape_decode_init (AVCodecContext *avctx)
static av_cold int ape_decode_close (AVCodecContext *avctx)
static void range_start_decoding (APEContext *ctx)
static void range_dec_normalize (APEContext *ctx)
static int range_decode_culfreq (APEContext *ctx, int tot_f)
static int range_decode_culshift (APEContext *ctx, int shift)
static void range_decode_update (APEContext *ctx, int sy_f, int lt_f)
static int range_decode_bits (APEContext *ctx, int n)
static int range_get_symbol (APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
static void update_rice (APERice *rice, int x)
static int ape_decode_value (APEContext *ctx, APERice *rice)
static void entropy_decode (APEContext *ctx, int blockstodecode, int stereo)
static void init_entropy_decoder (APEContext *ctx)
static void init_predictor_decoder (APEContext *ctx)
static int APESIGN (int32_t x)
static int predictor_update_filter (APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
static void predictor_decode_stereo (APEContext *ctx, int count)
static void predictor_decode_mono (APEContext *ctx, int count)
static void do_init_filter (APEFilter *f, int16_t *buf, int order)
static void init_filter (APEContext *ctx, APEFilter *f, int16_t *buf, int order)
static void do_apply_filter (APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
static void apply_filter (APEContext *ctx, APEFilter *f, int32_t *data0, int32_t *data1, int count, int order, int fracbits)
static void ape_apply_filters (APEContext *ctx, int32_t *decoded0, int32_t *decoded1, int count)
static void init_frame_decoder (APEContext *ctx)
static void ape_unpack_mono (APEContext *ctx, int count)
static void ape_unpack_stereo (APEContext *ctx, int count)
static int ape_decode_frame (AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)

Variables

static const uint16_t ape_filter_orders [5][APE_FILTER_LEVELS]
static const uint8_t ape_filter_fracbits [5][APE_FILTER_LEVELS]
static const uint16_t counts_3970 [22]
static const uint16_t counts_diff_3970 [21]
static const uint16_t counts_3980 [22]
static const uint16_t counts_diff_3980 [21]
static const int32_t initial_coeffs [4]
AVCodec ape_decoder


Detailed Description

Monkey's Audio lossless audio decoder

Definition in file apedec.c.


Define Documentation

#define ALT_BITSTREAM_READER_LE

Definition at line 23 of file apedec.c.

#define APE_FILTER_LEVELS   3

Definition at line 70 of file apedec.c.

Referenced by ape_apply_filters(), ape_decode_close(), ape_decode_init(), and init_frame_decoder().

#define APE_FRAMECODE_MONO_SILENCE   1

Definition at line 38 of file apedec.c.

#define APE_FRAMECODE_PSEUDO_STEREO   4

Definition at line 40 of file apedec.c.

Referenced by ape_decode_frame().

#define APE_FRAMECODE_STEREO_SILENCE   3

Definition at line 39 of file apedec.c.

Referenced by ape_unpack_mono(), ape_unpack_stereo(), and entropy_decode().

#define BLOCKS_PER_LOOP   4608

Definition at line 34 of file apedec.c.

Referenced by ape_decode_frame().

#define HISTORY_SIZE   512

Definition at line 42 of file apedec.c.

Referenced by ape_decode_init(), do_apply_filter(), init_filter(), predictor_decode_mono(), and predictor_decode_stereo().

#define MAX_BYTESPERSAMPLE   3

Definition at line 36 of file apedec.c.

#define MAX_CHANNELS   2

Definition at line 35 of file apedec.c.

#define PREDICTOR_ORDER   8

Definition at line 43 of file apedec.c.

#define PREDICTOR_SIZE   50

Total size of all predictor histories

Definition at line 45 of file apedec.c.

Referenced by init_predictor_decoder(), predictor_decode_mono(), and predictor_decode_stereo().

#define XADAPTCOEFFSA   14

Definition at line 53 of file apedec.c.

Referenced by predictor_decode_stereo().

#define XADAPTCOEFFSB   5

Definition at line 55 of file apedec.c.

Referenced by predictor_decode_stereo().

#define XDELAYA   (18 + PREDICTOR_ORDER*2)

Definition at line 49 of file apedec.c.

Referenced by predictor_decode_stereo().

#define XDELAYB   (18 + PREDICTOR_ORDER)

Definition at line 50 of file apedec.c.

Referenced by predictor_decode_stereo().

#define YADAPTCOEFFSA   18

Definition at line 52 of file apedec.c.

Referenced by predictor_decode_mono(), and predictor_decode_stereo().

#define YADAPTCOEFFSB   10

Definition at line 54 of file apedec.c.

Referenced by predictor_decode_stereo().

#define YDELAYA   (18 + PREDICTOR_ORDER*4)

Definition at line 47 of file apedec.c.

Referenced by predictor_decode_mono(), and predictor_decode_stereo().

#define YDELAYB   (18 + PREDICTOR_ORDER*3)

Definition at line 48 of file apedec.c.

Referenced by predictor_decode_stereo().


Enumeration Type Documentation

enum APECompressionLevel

Possible compression levels

Enumerator:
COMPRESSION_LEVEL_FAST 
COMPRESSION_LEVEL_NORMAL 
COMPRESSION_LEVEL_HIGH 
COMPRESSION_LEVEL_EXTRA_HIGH 
COMPRESSION_LEVEL_INSANE 

Definition at line 61 of file apedec.c.

00061                          {
00062     COMPRESSION_LEVEL_FAST       = 1000,
00063     COMPRESSION_LEVEL_NORMAL     = 2000,
00064     COMPRESSION_LEVEL_HIGH       = 3000,
00065     COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
00066     COMPRESSION_LEVEL_INSANE     = 5000
00067 };


Function Documentation

static void ape_apply_filters ( APEContext ctx,
int32_t *  decoded0,
int32_t *  decoded1,
int  count 
) [static]

Definition at line 721 of file apedec.c.

References ape_filter_fracbits, APE_FILTER_LEVELS, ape_filter_orders, apply_filter(), APEContext::filters, and APEContext::fset.

Referenced by ape_unpack_mono(), and ape_unpack_stereo().

00723 {
00724     int i;
00725 
00726     for (i = 0; i < APE_FILTER_LEVELS; i++) {
00727         if (!ape_filter_orders[ctx->fset][i])
00728             break;
00729         apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
00730     }
00731 }

static av_cold int ape_decode_close ( AVCodecContext avctx  )  [static]

Definition at line 204 of file apedec.c.

References APE_FILTER_LEVELS, av_freep(), APEContext::filterbuf, and AVCodecContext::priv_data.

00205 {
00206     APEContext *s = avctx->priv_data;
00207     int i;
00208 
00209     for (i = 0; i < APE_FILTER_LEVELS; i++)
00210         av_freep(&s->filterbuf[i]);
00211 
00212     return 0;
00213 }

static int ape_decode_frame ( AVCodecContext avctx,
void *  data,
int *  data_size,
const uint8_t *  buf,
int  buf_size 
) [static]

Definition at line 802 of file apedec.c.

References APE_FRAMECODE_PSEUDO_STEREO, ape_unpack_mono(), ape_unpack_stereo(), av_log(), AV_LOG_ERROR, av_realloc(), BLOCKS_PER_LOOP, DSPContext::bswap_buf, APEContext::channels, AVCodecContext::channels, APEContext::currentframeblocks, APEContext::data, APEContext::data_end, APEContext::decoded0, APEContext::decoded1, APEContext::dsp, APEContext::error, FFMIN, APEContext::frameflags, init_frame_decoder(), APEContext::last_ptr, NULL, AVCodecContext::priv_data, APEContext::ptr, APEContext::samples, and samples.

00805 {
00806     APEContext *s = avctx->priv_data;
00807     int16_t *samples = data;
00808     int nblocks;
00809     int i, n;
00810     int blockstodecode;
00811     int bytes_used;
00812 
00813     if (buf_size == 0 && !s->samples) {
00814         *data_size = 0;
00815         return 0;
00816     }
00817 
00818     /* should not happen but who knows */
00819     if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {
00820         av_log (avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\n", *data_size, s->samples * 2 * avctx->channels);
00821         return -1;
00822     }
00823 
00824     if(!s->samples){
00825         s->data = av_realloc(s->data, (buf_size + 3) & ~3);
00826         s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
00827         s->ptr = s->last_ptr = s->data;
00828         s->data_end = s->data + buf_size;
00829 
00830         nblocks = s->samples = bytestream_get_be32(&s->ptr);
00831         n =  bytestream_get_be32(&s->ptr);
00832         if(n < 0 || n > 3){
00833             av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
00834             s->data = NULL;
00835             return -1;
00836         }
00837         s->ptr += n;
00838 
00839         s->currentframeblocks = nblocks;
00840         buf += 4;
00841         if (s->samples <= 0) {
00842             *data_size = 0;
00843             return buf_size;
00844         }
00845 
00846         memset(s->decoded0,  0, sizeof(s->decoded0));
00847         memset(s->decoded1,  0, sizeof(s->decoded1));
00848 
00849         /* Initialize the frame decoder */
00850         init_frame_decoder(s);
00851     }
00852 
00853     if (!s->data) {
00854         *data_size = 0;
00855         return buf_size;
00856     }
00857 
00858     nblocks = s->samples;
00859     blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);
00860 
00861     s->error=0;
00862 
00863     if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
00864         ape_unpack_mono(s, blockstodecode);
00865     else
00866         ape_unpack_stereo(s, blockstodecode);
00867 
00868     if(s->error || s->ptr > s->data_end){
00869         s->samples=0;
00870         av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
00871         return -1;
00872     }
00873 
00874     for (i = 0; i < blockstodecode; i++) {
00875         *samples++ = s->decoded0[i];
00876         if(s->channels == 2)
00877             *samples++ = s->decoded1[i];
00878     }
00879 
00880     s->samples -= blockstodecode;
00881 
00882     *data_size = blockstodecode * 2 * s->channels;
00883     bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;
00884     s->last_ptr = s->ptr;
00885     return bytes_used;
00886 }

static av_cold int ape_decode_init ( AVCodecContext avctx  )  [static]

Definition at line 165 of file apedec.c.

References APE_FILTER_LEVELS, ape_filter_orders, av_log(), AV_LOG_DEBUG, AV_LOG_ERROR, av_malloc(), AV_RL16, APEContext::avctx, AVCodecContext::bits_per_sample, APEContext::channels, AVCodecContext::channels, APEContext::compression_level, COMPRESSION_LEVEL_INSANE, APEContext::dsp, dsputil_init(), AVCodecContext::extradata, AVCodecContext::extradata_size, APEContext::fileversion, APEContext::filterbuf, APEContext::flags, APEContext::fset, HISTORY_SIZE, and AVCodecContext::priv_data.

00166 {
00167     APEContext *s = avctx->priv_data;
00168     int i;
00169 
00170     if (avctx->extradata_size != 6) {
00171         av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
00172         return -1;
00173     }
00174     if (avctx->bits_per_sample != 16) {
00175         av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n");
00176         return -1;
00177     }
00178     if (avctx->channels > 2) {
00179         av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
00180         return -1;
00181     }
00182     s->avctx             = avctx;
00183     s->channels          = avctx->channels;
00184     s->fileversion       = AV_RL16(avctx->extradata);
00185     s->compression_level = AV_RL16(avctx->extradata + 2);
00186     s->flags             = AV_RL16(avctx->extradata + 4);
00187 
00188     av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags);
00189     if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) {
00190         av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level);
00191         return -1;
00192     }
00193     s->fset = s->compression_level / 1000 - 1;
00194     for (i = 0; i < APE_FILTER_LEVELS; i++) {
00195         if (!ape_filter_orders[s->fset][i])
00196             break;
00197         s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4);
00198     }
00199 
00200     dsputil_init(&s->dsp, avctx);
00201     return 0;
00202 }

static int ape_decode_value ( APEContext ctx,
APERice rice 
) [inline, static]

Definition at line 370 of file apedec.c.

References counts_3970, counts_3980, counts_diff_3970, counts_diff_3980, APEContext::fileversion, APERice::k, APERice::ksum, MODEL_ELEMENTS, range_decode_bits(), range_decode_culfreq(), range_decode_update(), range_get_symbol(), and update_rice().

Referenced by entropy_decode().

00371 {
00372     int x, overflow;
00373 
00374     if (ctx->fileversion < 3980) {
00375         int tmpk;
00376 
00377         overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
00378 
00379         if (overflow == (MODEL_ELEMENTS - 1)) {
00380             tmpk = range_decode_bits(ctx, 5);
00381             overflow = 0;
00382         } else
00383             tmpk = (rice->k < 1) ? 0 : rice->k - 1;
00384 
00385         if (tmpk <= 16)
00386             x = range_decode_bits(ctx, tmpk);
00387         else {
00388             x = range_decode_bits(ctx, 16);
00389             x |= (range_decode_bits(ctx, tmpk - 16) << 16);
00390         }
00391         x += overflow << tmpk;
00392     } else {
00393         int base, pivot;
00394 
00395         pivot = rice->ksum >> 5;
00396         if (pivot == 0)
00397             pivot = 1;
00398 
00399         overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
00400 
00401         if (overflow == (MODEL_ELEMENTS - 1)) {
00402             overflow  = range_decode_bits(ctx, 16) << 16;
00403             overflow |= range_decode_bits(ctx, 16);
00404         }
00405 
00406         base = range_decode_culfreq(ctx, pivot);
00407         range_decode_update(ctx, 1, base);
00408 
00409         x = base + overflow * pivot;
00410     }
00411 
00412     update_rice(rice, x);
00413 
00414     /* Convert to signed */
00415     if (x & 1)
00416         return (x >> 1) + 1;
00417     else
00418         return -(x >> 1);
00419 }

static void ape_unpack_mono ( APEContext ctx,
int  count 
) [static]

Definition at line 746 of file apedec.c.

References ape_apply_filters(), APE_FRAMECODE_STEREO_SILENCE, av_log(), AV_LOG_DEBUG, APEContext::channels, APEContext::decoded0, APEContext::decoded1, entropy_decode(), APEContext::frameflags, NULL, and predictor_decode_mono().

Referenced by ape_decode_frame().

00747 {
00748     int32_t left;
00749     int32_t *decoded0 = ctx->decoded0;
00750     int32_t *decoded1 = ctx->decoded1;
00751 
00752     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00753         entropy_decode(ctx, count, 0);
00754         /* We are pure silence, so we're done. */
00755         av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
00756         return;
00757     }
00758 
00759     entropy_decode(ctx, count, 0);
00760     ape_apply_filters(ctx, decoded0, NULL, count);
00761 
00762     /* Now apply the predictor decoding */
00763     predictor_decode_mono(ctx, count);
00764 
00765     /* Pseudo-stereo - just copy left channel to right channel */
00766     if (ctx->channels == 2) {
00767         while (count--) {
00768             left = *decoded0;
00769             *(decoded1++) = *(decoded0++) = left;
00770         }
00771     }
00772 }

static void ape_unpack_stereo ( APEContext ctx,
int  count 
) [static]

Definition at line 774 of file apedec.c.

References ape_apply_filters(), APE_FRAMECODE_STEREO_SILENCE, av_log(), AV_LOG_DEBUG, APEContext::avctx, APEContext::decoded0, APEContext::decoded1, entropy_decode(), APEContext::frameflags, and predictor_decode_stereo().

Referenced by ape_decode_frame().

00775 {
00776     int32_t left, right;
00777     int32_t *decoded0 = ctx->decoded0;
00778     int32_t *decoded1 = ctx->decoded1;
00779 
00780     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00781         /* We are pure silence, so we're done. */
00782         av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
00783         return;
00784     }
00785 
00786     entropy_decode(ctx, count, 1);
00787     ape_apply_filters(ctx, decoded0, decoded1, count);
00788 
00789     /* Now apply the predictor decoding */
00790     predictor_decode_stereo(ctx, count);
00791 
00792     /* Decorrelate and scale to output depth */
00793     while (count--) {
00794         left = *decoded1 - (*decoded0 / 2);
00795         right = left + *decoded0;
00796 
00797         *(decoded0++) = left;
00798         *(decoded1++) = right;
00799     }
00800 }

static int APESIGN ( int32_t  x  )  [inline, static]

Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)

Definition at line 495 of file apedec.c.

Referenced by predictor_decode_mono(), and predictor_update_filter().

00495                                      {
00496     return (x < 0) - (x > 0);
00497 }

static void apply_filter ( APEContext ctx,
APEFilter f,
int32_t *  data0,
int32_t *  data1,
int  count,
int  order,
int  fracbits 
) [static]

Definition at line 712 of file apedec.c.

References do_apply_filter(), and APEContext::fileversion.

Referenced by ape_apply_filters().

00715 {
00716     do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
00717     if (data1)
00718         do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
00719 }

static void do_apply_filter ( APEContext ctx,
int  version,
APEFilter f,
int32_t *  data,
int  count,
int  order,
int  fracbits 
) [inline, static]

Definition at line 652 of file apedec.c.

References APEFilter::adaptcoeffs, DSPContext::add_int16, av_clip_int16(), APEFilter::avg, APEFilter::coeffs, APEFilter::delay, APEContext::dsp, HISTORY_SIZE, APEFilter::historybuffer, DSPContext::scalarproduct_int16, and DSPContext::sub_int16.

Referenced by apply_filter().

00653 {
00654     int res;
00655     int absres;
00656 
00657     while (count--) {
00658         /* round fixedpoint scalar product */
00659         res = (ctx->dsp.scalarproduct_int16(f->delay - order, f->coeffs, order, 0) + (1 << (fracbits - 1))) >> fracbits;
00660 
00661         if (*data < 0)
00662             ctx->dsp.add_int16(f->coeffs, f->adaptcoeffs - order, order);
00663         else if (*data > 0)
00664             ctx->dsp.sub_int16(f->coeffs, f->adaptcoeffs - order, order);
00665 
00666         res += *data;
00667 
00668         *data++ = res;
00669 
00670         /* Update the output history */
00671         *f->delay++ = av_clip_int16(res);
00672 
00673         if (version < 3980) {
00674             /* Version ??? to < 3.98 files (untested) */
00675             f->adaptcoeffs[0]  = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
00676             f->adaptcoeffs[-4] >>= 1;
00677             f->adaptcoeffs[-8] >>= 1;
00678         } else {
00679             /* Version 3.98 and later files */
00680 
00681             /* Update the adaption coefficients */
00682             absres = (res < 0 ? -res : res);
00683 
00684             if (absres > (f->avg * 3))
00685                 *f->adaptcoeffs = ((res >> 25) & 64) - 32;
00686             else if (absres > (f->avg * 4) / 3)
00687                 *f->adaptcoeffs = ((res >> 26) & 32) - 16;
00688             else if (absres > 0)
00689                 *f->adaptcoeffs = ((res >> 27) & 16) - 8;
00690             else
00691                 *f->adaptcoeffs = 0;
00692 
00693             f->avg += (absres - f->avg) / 16;
00694 
00695             f->adaptcoeffs[-1] >>= 1;
00696             f->adaptcoeffs[-2] >>= 1;
00697             f->adaptcoeffs[-8] >>= 1;
00698         }
00699 
00700         f->adaptcoeffs++;
00701 
00702         /* Have we filled the history buffer? */
00703         if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
00704             memmove(f->historybuffer, f->delay - (order * 2),
00705                     (order * 2) * sizeof(int16_t));
00706             f->delay = f->historybuffer + order * 2;
00707             f->adaptcoeffs = f->historybuffer + order;
00708         }
00709     }
00710 }

static void do_init_filter ( APEFilter f,
int16_t *  buf,
int  order 
) [static]

Definition at line 634 of file apedec.c.

References APEFilter::adaptcoeffs, APEFilter::avg, APEFilter::coeffs, APEFilter::delay, and APEFilter::historybuffer.

Referenced by init_filter().

00635 {
00636     f->coeffs = buf;
00637     f->historybuffer = buf + order;
00638     f->delay       = f->historybuffer + order * 2;
00639     f->adaptcoeffs = f->historybuffer + order;
00640 
00641     memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
00642     memset(f->coeffs, 0, order * sizeof(int16_t));
00643     f->avg = 0;
00644 }

static void entropy_decode ( APEContext ctx,
int  blockstodecode,
int  stereo 
) [static]

Definition at line 421 of file apedec.c.

References ape_decode_value(), APE_FRAMECODE_STEREO_SILENCE, APEContext::blocksdecoded, APEContext::currentframeblocks, APEContext::decoded0, APEContext::decoded1, APEContext::frameflags, range_dec_normalize(), APEContext::riceX, and APEContext::riceY.

Referenced by ape_unpack_mono(), and ape_unpack_stereo().

00422 {
00423     int32_t *decoded0 = ctx->decoded0;
00424     int32_t *decoded1 = ctx->decoded1;
00425 
00426     ctx->blocksdecoded = blockstodecode;
00427 
00428     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00429         /* We are pure silence, just memset the output buffer. */
00430         memset(decoded0, 0, blockstodecode * sizeof(int32_t));
00431         memset(decoded1, 0, blockstodecode * sizeof(int32_t));
00432     } else {
00433         while (blockstodecode--) {
00434             *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
00435             if (stereo)
00436                 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
00437         }
00438     }
00439 
00440     if (ctx->blocksdecoded == ctx->currentframeblocks)
00441         range_dec_normalize(ctx);   /* normalize to use up all bytes */
00442 }

static void init_entropy_decoder ( APEContext ctx  )  [static]

Definition at line 444 of file apedec.c.

References APEContext::blocksdecoded, APEContext::CRC, APEContext::fileversion, APEContext::frameflags, APERice::k, APERice::ksum, APEContext::ptr, range_start_decoding(), APEContext::riceX, and APEContext::riceY.

Referenced by init_frame_decoder().

00445 {
00446     /* Read the CRC */
00447     ctx->CRC = bytestream_get_be32(&ctx->ptr);
00448 
00449     /* Read the frame flags if they exist */
00450     ctx->frameflags = 0;
00451     if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
00452         ctx->CRC &= ~0x80000000;
00453 
00454         ctx->frameflags = bytestream_get_be32(&ctx->ptr);
00455     }
00456 
00457     /* Keep a count of the blocks decoded in this frame */
00458     ctx->blocksdecoded = 0;
00459 
00460     /* Initialize the rice structs */
00461     ctx->riceX.k = 10;
00462     ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
00463     ctx->riceY.k = 10;
00464     ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
00465 
00466     /* The first 8 bits of input are ignored. */
00467     ctx->ptr++;
00468 
00469     range_start_decoding(ctx);
00470 }

static void init_filter ( APEContext ctx,
APEFilter f,
int16_t *  buf,
int  order 
) [static]

Definition at line 646 of file apedec.c.

References do_init_filter(), and HISTORY_SIZE.

Referenced by init_frame_decoder().

00647 {
00648     do_init_filter(&f[0], buf, order);
00649     do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
00650 }

static void init_frame_decoder ( APEContext ctx  )  [static]

Definition at line 733 of file apedec.c.

References APE_FILTER_LEVELS, ape_filter_orders, APEContext::filters, APEContext::fset, init_entropy_decoder(), init_filter(), and init_predictor_decoder().

Referenced by ape_decode_frame().

00734 {
00735     int i;
00736     init_entropy_decoder(ctx);
00737     init_predictor_decoder(ctx);
00738 
00739     for (i = 0; i < APE_FILTER_LEVELS; i++) {
00740         if (!ape_filter_orders[ctx->fset][i])
00741             break;
00742         init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
00743     }
00744 }

static void init_predictor_decoder ( APEContext ctx  )  [static]

Definition at line 476 of file apedec.c.

References APEPredictor::buf, APEPredictor::coeffsA, APEPredictor::coeffsB, APEPredictor::filterA, APEPredictor::filterB, APEPredictor::historybuffer, initial_coeffs, APEPredictor::lastA, APEContext::predictor, and PREDICTOR_SIZE.

Referenced by init_frame_decoder().

00477 {
00478     APEPredictor *p = &ctx->predictor;
00479 
00480     /* Zero the history buffers */
00481     memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
00482     p->buf = p->historybuffer;
00483 
00484     /* Initialize and zero the coefficients */
00485     memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
00486     memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
00487     memset(p->coeffsB, 0, sizeof(p->coeffsB));
00488 
00489     p->filterA[0] = p->filterA[1] = 0;
00490     p->filterB[0] = p->filterB[1] = 0;
00491     p->lastA[0]   = p->lastA[1]   = 0;
00492 }

static void predictor_decode_mono ( APEContext ctx,
int  count 
) [static]

Definition at line 583 of file apedec.c.

References A, APESIGN(), APEPredictor::buf, APEPredictor::coeffsA, APEContext::decoded0, APEPredictor::filterA, HISTORY_SIZE, APEPredictor::historybuffer, APEPredictor::lastA, APEContext::predictor, PREDICTOR_SIZE, YADAPTCOEFFSA, and YDELAYA.

Referenced by ape_unpack_mono().

00584 {
00585     APEPredictor *p = &ctx->predictor;
00586     int32_t *decoded0 = ctx->decoded0;
00587     int32_t predictionA, currentA, A;
00588 
00589     currentA = p->lastA[0];
00590 
00591     while (count--) {
00592         A = *decoded0;
00593 
00594         p->buf[YDELAYA] = currentA;
00595         p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
00596 
00597         predictionA = p->buf[YDELAYA    ] * p->coeffsA[0][0] +
00598                       p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
00599                       p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
00600                       p->buf[YDELAYA - 3] * p->coeffsA[0][3];
00601 
00602         currentA = A + (predictionA >> 10);
00603 
00604         p->buf[YADAPTCOEFFSA]     = APESIGN(p->buf[YDELAYA    ]);
00605         p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
00606 
00607         if (A > 0) {
00608             p->coeffsA[0][0] -= p->buf[YADAPTCOEFFSA    ];
00609             p->coeffsA[0][1] -= p->buf[YADAPTCOEFFSA - 1];
00610             p->coeffsA[0][2] -= p->buf[YADAPTCOEFFSA - 2];
00611             p->coeffsA[0][3] -= p->buf[YADAPTCOEFFSA - 3];
00612         } else if (A < 0) {
00613             p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA    ];
00614             p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1];
00615             p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2];
00616             p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3];
00617         }
00618 
00619         p->buf++;
00620 
00621         /* Have we filled the history buff