00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define ALT_BITSTREAM_READER_LE
00024 #include "avcodec.h"
00025 #include "dsputil.h"
00026 #include "bitstream.h"
00027 #include "bytestream.h"
00028
00034 #define BLOCKS_PER_LOOP 4608
00035 #define MAX_CHANNELS 2
00036 #define MAX_BYTESPERSAMPLE 3
00037
00038 #define APE_FRAMECODE_MONO_SILENCE 1
00039 #define APE_FRAMECODE_STEREO_SILENCE 3
00040 #define APE_FRAMECODE_PSEUDO_STEREO 4
00041
00042 #define HISTORY_SIZE 512
00043 #define PREDICTOR_ORDER 8
00044
00045 #define PREDICTOR_SIZE 50
00046
00047 #define YDELAYA (18 + PREDICTOR_ORDER*4)
00048 #define YDELAYB (18 + PREDICTOR_ORDER*3)
00049 #define XDELAYA (18 + PREDICTOR_ORDER*2)
00050 #define XDELAYB (18 + PREDICTOR_ORDER)
00051
00052 #define YADAPTCOEFFSA 18
00053 #define XADAPTCOEFFSA 14
00054 #define YADAPTCOEFFSB 10
00055 #define XADAPTCOEFFSB 5
00056
00061 enum APECompressionLevel {
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 };
00070 #define APE_FILTER_LEVELS 3
00071
00073 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
00074 { 0, 0, 0 },
00075 { 16, 0, 0 },
00076 { 64, 0, 0 },
00077 { 32, 256, 0 },
00078 { 16, 256, 1280 }
00079 };
00080
00082 static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
00083 { 0, 0, 0 },
00084 { 11, 0, 0 },
00085 { 11, 0, 0 },
00086 { 10, 13, 0 },
00087 { 11, 13, 15 }
00088 };
00089
00090
00092 typedef struct APEFilter {
00093 int16_t *coeffs;
00094 int16_t *adaptcoeffs;
00095 int16_t *historybuffer;
00096 int16_t *delay;
00097
00098 int avg;
00099 } APEFilter;
00100
00101 typedef struct APERice {
00102 uint32_t k;
00103 uint32_t ksum;
00104 } APERice;
00105
00106 typedef struct APERangecoder {
00107 uint32_t low;
00108 uint32_t range;
00109 uint32_t help;
00110 unsigned int buffer;
00111 } APERangecoder;
00112
00114 typedef struct APEPredictor {
00115 int32_t *buf;
00116
00117 int32_t lastA[2];
00118
00119 int32_t filterA[2];
00120 int32_t filterB[2];
00121
00122 int32_t coeffsA[2][4];
00123 int32_t coeffsB[2][5];
00124 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
00125 } APEPredictor;
00126
00128 typedef struct APEContext {
00129 AVCodecContext *avctx;
00130 DSPContext dsp;
00131 int channels;
00132 int samples;
00133
00134 int fileversion;
00135 int compression_level;
00136 int fset;
00137 int flags;
00138
00139 uint32_t CRC;
00140 int frameflags;
00141 int currentframeblocks;
00142 int blocksdecoded;
00143 APEPredictor predictor;
00144
00145 int32_t decoded0[BLOCKS_PER_LOOP];
00146 int32_t decoded1[BLOCKS_PER_LOOP];
00147
00148 int16_t* filterbuf[APE_FILTER_LEVELS];
00149
00150 APERangecoder rc;
00151 APERice riceX;
00152 APERice riceY;
00153 APEFilter filters[APE_FILTER_LEVELS][2];
00154
00155 uint8_t *data;
00156 uint8_t *data_end;
00157 const uint8_t *ptr;
00158 const uint8_t *last_ptr;
00159
00160 int error;
00161 } APEContext;
00162
00163
00164
00165 static av_cold int ape_decode_init(AVCodecContext * avctx)
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_coded_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 avctx->sample_fmt = SAMPLE_FMT_S16;
00202 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00203 return 0;
00204 }
00205
00206 static av_cold int ape_decode_close(AVCodecContext * avctx)
00207 {
00208 APEContext *s = avctx->priv_data;
00209 int i;
00210
00211 for (i = 0; i < APE_FILTER_LEVELS; i++)
00212 av_freep(&s->filterbuf[i]);
00213
00214 return 0;
00215 }
00216
00222 #define CODE_BITS 32
00223 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
00224 #define SHIFT_BITS (CODE_BITS - 9)
00225 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
00226 #define BOTTOM_VALUE (TOP_VALUE >> 8)
00227
00229 static inline void range_start_decoding(APEContext * ctx)
00230 {
00231 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
00232 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
00233 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
00234 }
00235
00237 static inline void range_dec_normalize(APEContext * ctx)
00238 {
00239 while (ctx->rc.range <= BOTTOM_VALUE) {
00240 ctx->rc.buffer <<= 8;
00241 if(ctx->ptr < ctx->data_end)
00242 ctx->rc.buffer += *ctx->ptr;
00243 ctx->ptr++;
00244 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
00245 ctx->rc.range <<= 8;
00246 }
00247 }
00248
00255 static inline int range_decode_culfreq(APEContext * ctx, int tot_f)
00256 {
00257 range_dec_normalize(ctx);
00258 ctx->rc.help = ctx->rc.range / tot_f;
00259 return ctx->rc.low / ctx->rc.help;
00260 }
00261
00267 static inline int range_decode_culshift(APEContext * ctx, int shift)
00268 {
00269 range_dec_normalize(ctx);
00270 ctx->rc.help = ctx->rc.range >> shift;
00271 return ctx->rc.low / ctx->rc.help;
00272 }
00273
00274
00281 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f)
00282 {
00283 ctx->rc.low -= ctx->rc.help * lt_f;
00284 ctx->rc.range = ctx->rc.help * sy_f;
00285 }
00286
00288 static inline int range_decode_bits(APEContext * ctx, int n)
00289 {
00290 int sym = range_decode_culshift(ctx, n);
00291 range_decode_update(ctx, 1, sym);
00292 return sym;
00293 }
00294
00295
00296 #define MODEL_ELEMENTS 64
00297
00301 static const uint16_t counts_3970[22] = {
00302 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
00303 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
00304 65450, 65469, 65480, 65487, 65491, 65493,
00305 };
00306
00310 static const uint16_t counts_diff_3970[21] = {
00311 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
00312 1104, 677, 415, 248, 150, 89, 54, 31,
00313 19, 11, 7, 4, 2,
00314 };
00315
00319 static const uint16_t counts_3980[22] = {
00320 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
00321 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
00322 65485, 65488, 65490, 65491, 65492, 65493,
00323 };
00324
00328 static const uint16_t counts_diff_3980[21] = {
00329 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
00330 261, 119, 65, 31, 19, 10, 6, 3,
00331 3, 2, 1, 1, 1,
00332 };
00333
00340 static inline int range_get_symbol(APEContext * ctx,
00341 const uint16_t counts[],
00342 const uint16_t counts_diff[])
00343 {
00344 int symbol, cf;
00345
00346 cf = range_decode_culshift(ctx, 16);
00347
00348 if(cf > 65492){
00349 symbol= cf - 65535 + 63;
00350 range_decode_update(ctx, 1, cf);
00351 if(cf > 65535)
00352 ctx->error=1;
00353 return symbol;
00354 }
00355
00356 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
00357
00358 range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
00359
00360 return symbol;
00361 }
00363
00364 static inline void update_rice(APERice *rice, int x)
00365 {
00366 int lim = rice->k ? (1 << (rice->k + 4)) : 0;
00367 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
00368
00369 if (rice->ksum < lim)
00370 rice->k--;
00371 else if (rice->ksum >= (1 << (rice->k + 5)))
00372 rice->k++;
00373 }
00374
00375 static inline int ape_decode_value(APEContext * ctx, APERice *rice)
00376 {
00377 int x, overflow;
00378
00379 if (ctx->fileversion < 3990) {
00380 int tmpk;
00381
00382 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
00383
00384 if (overflow == (MODEL_ELEMENTS - 1)) {
00385 tmpk = range_decode_bits(ctx, 5);
00386 overflow = 0;
00387 } else
00388 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
00389
00390 if (tmpk <= 16)
00391 x = range_decode_bits(ctx, tmpk);
00392 else {
00393 x = range_decode_bits(ctx, 16);
00394 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
00395 }
00396 x += overflow << tmpk;
00397 } else {
00398 int base, pivot;
00399
00400 pivot = rice->ksum >> 5;
00401 if (pivot == 0)
00402 pivot = 1;
00403
00404 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
00405
00406 if (overflow == (MODEL_ELEMENTS - 1)) {
00407 overflow = range_decode_bits(ctx, 16) << 16;
00408 overflow |= range_decode_bits(ctx, 16);
00409 }
00410
00411 base = range_decode_culfreq(ctx, pivot);
00412 range_decode_update(ctx, 1, base);
00413
00414 x = base + overflow * pivot;
00415 }
00416
00417 update_rice(rice, x);
00418
00419
00420 if (x & 1)
00421 return (x >> 1) + 1;
00422 else
00423 return -(x >> 1);
00424 }
00425
00426 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo)
00427 {
00428 int32_t *decoded0 = ctx->decoded0;
00429 int32_t *decoded1 = ctx->decoded1;
00430
00431 ctx->blocksdecoded = blockstodecode;
00432
00433 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00434
00435 memset(decoded0, 0, blockstodecode * sizeof(int32_t));
00436 memset(decoded1, 0, blockstodecode * sizeof(int32_t));
00437 } else {
00438 while (blockstodecode--) {
00439 *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
00440 if (stereo)
00441 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
00442 }
00443 }
00444
00445 if (ctx->blocksdecoded == ctx->currentframeblocks)
00446 range_dec_normalize(ctx);
00447 }
00448
00449 static void init_entropy_decoder(APEContext * ctx)
00450 {
00451
00452 ctx->CRC = bytestream_get_be32(&ctx->ptr);
00453
00454
00455 ctx->frameflags = 0;
00456 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
00457 ctx->CRC &= ~0x80000000;
00458
00459 ctx->frameflags = bytestream_get_be32(&ctx->ptr);
00460 }
00461
00462
00463 ctx->blocksdecoded = 0;
00464
00465
00466 ctx->riceX.k = 10;
00467 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
00468 ctx->riceY.k = 10;
00469 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
00470
00471
00472 ctx->ptr++;
00473
00474 range_start_decoding(ctx);
00475 }
00476
00477 static const int32_t initial_coeffs[4] = {
00478 360, 317, -109, 98
00479 };
00480
00481 static void init_predictor_decoder(APEContext * ctx)
00482 {
00483 APEPredictor *p = &ctx->predictor;
00484
00485
00486 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
00487 p->buf = p->historybuffer;
00488
00489
00490 memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
00491 memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
00492 memset(p->coeffsB, 0, sizeof(p->coeffsB));
00493
00494 p->filterA[0] = p->filterA[1] = 0;
00495 p->filterB[0] = p->filterB[1] = 0;
00496 p->lastA[0] = p->lastA[1] = 0;
00497 }
00498
00500 static inline int APESIGN(int32_t x) {
00501 return (x < 0) - (x > 0);
00502 }
00503
00504 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)
00505 {
00506 int32_t predictionA, predictionB;
00507
00508 p->buf[delayA] = p->lastA[filter];
00509 p->buf[adaptA] = APESIGN(p->buf[delayA]);
00510 p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
00511 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
00512
00513 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
00514 p->buf[delayA - 1] * p->coeffsA[filter][1] +
00515 p->buf[delayA - 2] * p->coeffsA[filter][2] +
00516 p->buf[delayA - 3] * p->coeffsA[filter][3];
00517
00518
00519 p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
00520 p->buf[adaptB] = APESIGN(p->buf[delayB]);
00521 p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
00522 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
00523 p->filterB[filter] = p->filterA[filter ^ 1];
00524
00525 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
00526 p->buf[delayB - 1] * p->coeffsB[filter][1] +
00527 p->buf[delayB - 2] * p->coeffsB[filter][2] +
00528 p->buf[delayB - 3] * p->coeffsB[filter][3] +
00529 p->buf[delayB - 4] * p->coeffsB[filter][4];
00530
00531 p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
00532 p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
00533
00534 if (!decoded)
00535 return p->filterA[filter];
00536
00537 if (decoded > 0) {
00538 p->coeffsA[filter][0] -= p->buf[adaptA ];
00539 p->coeffsA[filter][1] -= p->buf[adaptA - 1];
00540 p->coeffsA[filter][2] -= p->buf[adaptA - 2];
00541 p->coeffsA[filter][3] -= p->buf[adaptA - 3];
00542
00543 p->coeffsB[filter][0] -= p->buf[adaptB ];
00544 p->coeffsB[filter][1] -= p->buf[adaptB - 1];
00545 p->coeffsB[filter][2] -= p->buf[adaptB - 2];
00546 p->coeffsB[filter][3] -= p->buf[adaptB - 3];
00547 p->coeffsB[filter][4] -= p->buf[adaptB - 4];
00548 } else {
00549 p->coeffsA[filter][0] += p->buf[adaptA ];
00550 p->coeffsA[filter][1] += p->buf[adaptA - 1];
00551 p->coeffsA[filter][2] += p->buf[adaptA - 2];
00552 p->coeffsA[filter][3] += p->buf[adaptA - 3];
00553
00554 p->coeffsB[filter][0] += p->buf[adaptB ];
00555 p->coeffsB[filter][1] += p->buf[adaptB - 1];
00556 p->coeffsB[filter][2] += p->buf[adaptB - 2];
00557 p->coeffsB[filter][3] += p->buf[adaptB - 3];
00558 p->coeffsB[filter][4] += p->buf[adaptB - 4];
00559 }
00560 return p->filterA[filter];
00561 }
00562
00563 static void predictor_decode_stereo(APEContext * ctx, int count)
00564 {
00565 int32_t predictionA, predictionB;
00566 APEPredictor *p = &ctx->predictor;
00567 int32_t *decoded0 = ctx->decoded0;
00568 int32_t *decoded1 = ctx->decoded1;
00569
00570 while (count--) {
00571
00572 predictionA = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB);
00573 predictionB = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB);
00574 *(decoded0++) = predictionA;
00575 *(decoded1++) = predictionB;
00576
00577
00578 p->buf++;
00579
00580
00581 if (p->buf == p->historybuffer + HISTORY_SIZE) {
00582 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
00583 p->buf = p->historybuffer;
00584 }
00585 }
00586 }
00587
00588 static void predictor_decode_mono(APEContext * ctx, int count)
00589 {
00590 APEPredictor *p = &ctx->predictor;
00591 int32_t *decoded0 = ctx->decoded0;
00592 int32_t predictionA, currentA, A;
00593
00594 currentA = p->lastA[0];
00595
00596 while (count--) {
00597 A = *decoded0;
00598
00599 p->buf[YDELAYA] = currentA;
00600 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
00601
00602 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
00603 p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
00604 p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
00605 p->buf[YDELAYA - 3] * p->coeffsA[0][3];
00606
00607 currentA = A + (predictionA >> 10);
00608
00609 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
00610 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
00611
00612 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 } else if (A < 0) {
00618 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ];
00619 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1];
00620 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2];
00621 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3];
00622 }
00623
00624 p->buf++;
00625
00626
00627 if (p->buf == p->historybuffer + HISTORY_SIZE) {
00628 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
00629 p->buf = p->historybuffer;
00630 }
00631
00632 p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
00633 *(decoded0++) = p->filterA[0];
00634 }
00635
00636 p->lastA[0] = currentA;
00637 }
00638
00639 static void do_init_filter(APEFilter *f, int16_t * buf, int order)
00640 {
00641 f->coeffs = buf;
00642 f->historybuffer = buf + order;
00643 f->delay = f->historybuffer + order * 2;
00644 f->adaptcoeffs = f->historybuffer + order;
00645
00646 memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
00647 memset(f->coeffs, 0, order * sizeof(int16_t));
00648 f->avg = 0;
00649 }
00650
00651 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order)
00652 {
00653 do_init_filter(&f[0], buf, order);
00654 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
00655 }
00656
00657 static inline void do_apply_filter(APEContext * ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
00658 {
00659 int res;
00660 int absres;
00661
00662 while (count--) {
00663
00664 res = (ctx->dsp.scalarproduct_int16(f->delay - order, f->coeffs, order, 0) + (1 << (fracbits - 1))) >> fracbits;
00665
00666 if (*data < 0)
00667 ctx->dsp.add_int16(f->coeffs, f->adaptcoeffs - order, order);
00668 else if (*data > 0)
00669 ctx->dsp.sub_int16(f->coeffs, f->adaptcoeffs - order, order);
00670
00671 res += *data;
00672
00673 *data++ = res;
00674
00675
00676 *f->delay++ = av_clip_int16(res);
00677
00678 if (version < 3980) {
00679
00680 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
00681 f->adaptcoeffs[-4] >>= 1;
00682 f->adaptcoeffs[-8] >>= 1;
00683 } else {
00684
00685
00686
00687 absres = (res < 0 ? -res : res);
00688
00689 if (absres > (f->avg * 3))
00690 *f->adaptcoeffs = ((res >> 25) & 64) - 32;
00691 else if (absres > (f->avg * 4) / 3)
00692 *f->adaptcoeffs = ((res >> 26) & 32) - 16;
00693 else if (absres > 0)
00694 *f->adaptcoeffs = ((res >> 27) & 16) - 8;
00695 else
00696 *f->adaptcoeffs = 0;
00697
00698 f->avg += (absres - f->avg) / 16;
00699
00700 f->adaptcoeffs[-1] >>= 1;
00701 f->adaptcoeffs[-2] >>= 1;
00702 f->adaptcoeffs[-8] >>= 1;
00703 }
00704
00705 f->adaptcoeffs++;
00706
00707
00708 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
00709 memmove(f->historybuffer, f->delay - (order * 2),
00710 (order * 2) * sizeof(int16_t));
00711 f->delay = f->historybuffer + order * 2;
00712 f->adaptcoeffs = f->historybuffer + order;
00713 }
00714 }
00715 }
00716
00717 static void apply_filter(APEContext * ctx, APEFilter *f,
00718 int32_t * data0, int32_t * data1,
00719 int count, int order, int fracbits)
00720 {
00721 do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
00722 if (data1)
00723 do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
00724 }
00725
00726 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0,
00727 int32_t * decoded1, int count)
00728 {
00729 int i;
00730
00731 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00732 if (!ape_filter_orders[ctx->fset][i])
00733 break;
00734 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
00735 }
00736 }
00737
00738 static void init_frame_decoder(APEContext * ctx)
00739 {
00740 int i;
00741 init_entropy_decoder(ctx);
00742 init_predictor_decoder(ctx);
00743
00744 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00745 if (!ape_filter_orders[ctx->fset][i])
00746 break;
00747 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
00748 }
00749 }
00750
00751 static void ape_unpack_mono(APEContext * ctx, int count)
00752 {
00753 int32_t left;
00754 int32_t *decoded0 = ctx->decoded0;
00755 int32_t *decoded1 = ctx->decoded1;
00756
00757 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00758 entropy_decode(ctx, count, 0);
00759
00760 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
00761 return;
00762 }
00763
00764 entropy_decode(ctx, count, 0);
00765 ape_apply_filters(ctx, decoded0, NULL, count);
00766
00767
00768 predictor_decode_mono(ctx, count);
00769
00770
00771 if (ctx->channels == 2) {
00772 while (count--) {
00773 left = *decoded0;
00774 *(decoded1++) = *(decoded0++) = left;
00775 }
00776 }
00777 }
00778
00779 static void ape_unpack_stereo(APEContext * ctx, int count)
00780 {
00781 int32_t left, right;
00782 int32_t *decoded0 = ctx->decoded0;
00783 int32_t *decoded1 = ctx->decoded1;
00784
00785 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00786
00787 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
00788 return;
00789 }
00790
00791 entropy_decode(ctx, count, 1);
00792 ape_apply_filters(ctx, decoded0, decoded1, count);
00793
00794
00795 predictor_decode_stereo(ctx, count);
00796
00797
00798 while (count--) {
00799 left = *decoded1 - (*decoded0 / 2);
00800 right = left + *decoded0;
00801
00802 *(decoded0++) = left;
00803 *(decoded1++) = right;
00804 }
00805 }
00806
00807 static int ape_decode_frame(AVCodecContext * avctx,
00808 void *data, int *data_size,
00809 const uint8_t * buf, int buf_size)
00810 {
00811 APEContext *s = avctx->priv_data;
00812 int16_t *samples = data;
00813 int nblocks;
00814 int i, n;
00815 int blockstodecode;
00816 int bytes_used;
00817
00818 if (buf_size == 0 && !s->samples) {
00819 *data_size = 0;
00820 return 0;
00821 }
00822
00823
00824 if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {
00825 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);
00826 return -1;
00827 }
00828
00829 if(!s->samples){
00830 s->data = av_realloc(s->data, (buf_size + 3) & ~3);
00831 s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
00832 s->ptr = s->last_ptr = s->data;
00833 s->data_end = s->data + buf_size;
00834
00835 nblocks = s->samples = bytestream_get_be32(&s->ptr);
00836 n = bytestream_get_be32(&s->ptr);
00837 if(n < 0 || n > 3){
00838 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
00839 s->data = NULL;
00840 return -1;
00841 }
00842 s->ptr += n;
00843
00844 s->currentframeblocks = nblocks;
00845 buf += 4;
00846 if (s->samples <= 0) {
00847 *data_size = 0;
00848 return buf_size;
00849 }
00850
00851 memset(s->decoded0, 0, sizeof(s->decoded0));
00852 memset(s->decoded1, 0, sizeof(s->decoded1));
00853
00854
00855 init_frame_decoder(s);
00856 }
00857
00858 if (!s->data) {
00859 *data_size = 0;
00860 return buf_size;
00861 }
00862
00863 nblocks = s->samples;
00864 blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);
00865
00866 s->error=0;
00867
00868 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
00869 ape_unpack_mono(s, blockstodecode);
00870 else
00871 ape_unpack_stereo(s, blockstodecode);
00872
00873 if(s->error || s->ptr > s->data_end){
00874 s->samples=0;
00875 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
00876 return -1;
00877 }
00878
00879 for (i = 0; i < blockstodecode; i++) {
00880 *samples++ = s->decoded0[i];
00881 if(s->channels == 2)
00882 *samples++ = s->decoded1[i];
00883 }
00884
00885 s->samples -= blockstodecode;
00886
00887 *data_size = blockstodecode * 2 * s->channels;
00888 bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;
00889 s->last_ptr = s->ptr;
00890 return bytes_used;
00891 }
00892
00893 AVCodec ape_decoder = {
00894 "ape",
00895 CODEC_TYPE_AUDIO,
00896 CODEC_ID_APE,
00897 sizeof(APEContext),
00898 ape_decode_init,
00899 NULL,
00900 ape_decode_close,
00901 ape_decode_frame,
00902 .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
00903 };