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 "get_bits.h"
00027 #include "bytestream.h"
00028 #include "libavutil/audioconvert.h"
00029
00035 #define BLOCKS_PER_LOOP 4608
00036 #define MAX_CHANNELS 2
00037 #define MAX_BYTESPERSAMPLE 3
00038
00039 #define APE_FRAMECODE_MONO_SILENCE 1
00040 #define APE_FRAMECODE_STEREO_SILENCE 3
00041 #define APE_FRAMECODE_PSEUDO_STEREO 4
00042
00043 #define HISTORY_SIZE 512
00044 #define PREDICTOR_ORDER 8
00045
00046 #define PREDICTOR_SIZE 50
00047
00048 #define YDELAYA (18 + PREDICTOR_ORDER*4)
00049 #define YDELAYB (18 + PREDICTOR_ORDER*3)
00050 #define XDELAYA (18 + PREDICTOR_ORDER*2)
00051 #define XDELAYB (18 + PREDICTOR_ORDER)
00052
00053 #define YADAPTCOEFFSA 18
00054 #define XADAPTCOEFFSA 14
00055 #define YADAPTCOEFFSB 10
00056 #define XADAPTCOEFFSB 5
00057
00062 enum APECompressionLevel {
00063 COMPRESSION_LEVEL_FAST = 1000,
00064 COMPRESSION_LEVEL_NORMAL = 2000,
00065 COMPRESSION_LEVEL_HIGH = 3000,
00066 COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
00067 COMPRESSION_LEVEL_INSANE = 5000
00068 };
00071 #define APE_FILTER_LEVELS 3
00072
00074 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
00075 { 0, 0, 0 },
00076 { 16, 0, 0 },
00077 { 64, 0, 0 },
00078 { 32, 256, 0 },
00079 { 16, 256, 1280 }
00080 };
00081
00083 static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
00084 { 0, 0, 0 },
00085 { 11, 0, 0 },
00086 { 11, 0, 0 },
00087 { 10, 13, 0 },
00088 { 11, 13, 15 }
00089 };
00090
00091
00093 typedef struct APEFilter {
00094 int16_t *coeffs;
00095 int16_t *adaptcoeffs;
00096 int16_t *historybuffer;
00097 int16_t *delay;
00098
00099 int avg;
00100 } APEFilter;
00101
00102 typedef struct APERice {
00103 uint32_t k;
00104 uint32_t ksum;
00105 } APERice;
00106
00107 typedef struct APERangecoder {
00108 uint32_t low;
00109 uint32_t range;
00110 uint32_t help;
00111 unsigned int buffer;
00112 } APERangecoder;
00113
00115 typedef struct APEPredictor {
00116 int32_t *buf;
00117
00118 int32_t lastA[2];
00119
00120 int32_t filterA[2];
00121 int32_t filterB[2];
00122
00123 int32_t coeffsA[2][4];
00124 int32_t coeffsB[2][5];
00125 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
00126 } APEPredictor;
00127
00129 typedef struct APEContext {
00130 AVCodecContext *avctx;
00131 DSPContext dsp;
00132 int channels;
00133 int samples;
00134
00135 int fileversion;
00136 int compression_level;
00137 int fset;
00138 int flags;
00139
00140 uint32_t CRC;
00141 int frameflags;
00142 int currentframeblocks;
00143 int blocksdecoded;
00144 APEPredictor predictor;
00145
00146 int32_t decoded0[BLOCKS_PER_LOOP];
00147 int32_t decoded1[BLOCKS_PER_LOOP];
00148
00149 int16_t* filterbuf[APE_FILTER_LEVELS];
00150
00151 APERangecoder rc;
00152 APERice riceX;
00153 APERice riceY;
00154 APEFilter filters[APE_FILTER_LEVELS][2];
00155
00156 uint8_t *data;
00157 uint8_t *data_end;
00158 const uint8_t *ptr;
00159 const uint8_t *last_ptr;
00160
00161 int error;
00162 } APEContext;
00163
00164
00165
00166 static av_cold int ape_decode_close(AVCodecContext * avctx)
00167 {
00168 APEContext *s = avctx->priv_data;
00169 int i;
00170
00171 for (i = 0; i < APE_FILTER_LEVELS; i++)
00172 av_freep(&s->filterbuf[i]);
00173
00174 av_freep(&s->data);
00175 return 0;
00176 }
00177
00178 static av_cold int ape_decode_init(AVCodecContext * avctx)
00179 {
00180 APEContext *s = avctx->priv_data;
00181 int i;
00182
00183 if (avctx->extradata_size != 6) {
00184 av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
00185 return -1;
00186 }
00187 if (avctx->bits_per_coded_sample != 16) {
00188 av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n");
00189 return -1;
00190 }
00191 if (avctx->channels > 2) {
00192 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
00193 return -1;
00194 }
00195 s->avctx = avctx;
00196 s->channels = avctx->channels;
00197 s->fileversion = AV_RL16(avctx->extradata);
00198 s->compression_level = AV_RL16(avctx->extradata + 2);
00199 s->flags = AV_RL16(avctx->extradata + 4);
00200
00201 av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags);
00202 if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) {
00203 av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level);
00204 return -1;
00205 }
00206 s->fset = s->compression_level / 1000 - 1;
00207 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00208 if (!ape_filter_orders[s->fset][i])
00209 break;
00210 FF_ALLOC_OR_GOTO(avctx, s->filterbuf[i],
00211 (ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4,
00212 filter_alloc_fail);
00213 }
00214
00215 dsputil_init(&s->dsp, avctx);
00216 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00217 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
00218 return 0;
00219 filter_alloc_fail:
00220 ape_decode_close(avctx);
00221 return AVERROR(ENOMEM);
00222 }
00223
00229 #define CODE_BITS 32
00230 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
00231 #define SHIFT_BITS (CODE_BITS - 9)
00232 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
00233 #define BOTTOM_VALUE (TOP_VALUE >> 8)
00234
00236 static inline void range_start_decoding(APEContext * ctx)
00237 {
00238 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
00239 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
00240 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
00241 }
00242
00244 static inline void range_dec_normalize(APEContext * ctx)
00245 {
00246 while (ctx->rc.range <= BOTTOM_VALUE) {
00247 ctx->rc.buffer <<= 8;
00248 if(ctx->ptr < ctx->data_end)
00249 ctx->rc.buffer += *ctx->ptr;
00250 ctx->ptr++;
00251 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
00252 ctx->rc.range <<= 8;
00253 }
00254 }
00255
00262 static inline int range_decode_culfreq(APEContext * ctx, int tot_f)
00263 {
00264 range_dec_normalize(ctx);
00265 ctx->rc.help = ctx->rc.range / tot_f;
00266 return ctx->rc.low / ctx->rc.help;
00267 }
00268
00274 static inline int range_decode_culshift(APEContext * ctx, int shift)
00275 {
00276 range_dec_normalize(ctx);
00277 ctx->rc.help = ctx->rc.range >> shift;
00278 return ctx->rc.low / ctx->rc.help;
00279 }
00280
00281
00288 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f)
00289 {
00290 ctx->rc.low -= ctx->rc.help * lt_f;
00291 ctx->rc.range = ctx->rc.help * sy_f;
00292 }
00293
00295 static inline int range_decode_bits(APEContext * ctx, int n)
00296 {
00297 int sym = range_decode_culshift(ctx, n);
00298 range_decode_update(ctx, 1, sym);
00299 return sym;
00300 }
00301
00302
00303 #define MODEL_ELEMENTS 64
00304
00308 static const uint16_t counts_3970[22] = {
00309 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
00310 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
00311 65450, 65469, 65480, 65487, 65491, 65493,
00312 };
00313
00317 static const uint16_t counts_diff_3970[21] = {
00318 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
00319 1104, 677, 415, 248, 150, 89, 54, 31,
00320 19, 11, 7, 4, 2,
00321 };
00322
00326 static const uint16_t counts_3980[22] = {
00327 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
00328 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
00329 65485, 65488, 65490, 65491, 65492, 65493,
00330 };
00331
00335 static const uint16_t counts_diff_3980[21] = {
00336 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
00337 261, 119, 65, 31, 19, 10, 6, 3,
00338 3, 2, 1, 1, 1,
00339 };
00340
00347 static inline int range_get_symbol(APEContext * ctx,
00348 const uint16_t counts[],
00349 const uint16_t counts_diff[])
00350 {
00351 int symbol, cf;
00352
00353 cf = range_decode_culshift(ctx, 16);
00354
00355 if(cf > 65492){
00356 symbol= cf - 65535 + 63;
00357 range_decode_update(ctx, 1, cf);
00358 if(cf > 65535)
00359 ctx->error=1;
00360 return symbol;
00361 }
00362
00363 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
00364
00365 range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
00366
00367 return symbol;
00368 }
00370
00371 static inline void update_rice(APERice *rice, int x)
00372 {
00373 int lim = rice->k ? (1 << (rice->k + 4)) : 0;
00374 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
00375
00376 if (rice->ksum < lim)
00377 rice->k--;
00378 else if (rice->ksum >= (1 << (rice->k + 5)))
00379 rice->k++;
00380 }
00381
00382 static inline int ape_decode_value(APEContext * ctx, APERice *rice)
00383 {
00384 int x, overflow;
00385
00386 if (ctx->fileversion < 3990) {
00387 int tmpk;
00388
00389 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
00390
00391 if (overflow == (MODEL_ELEMENTS - 1)) {
00392 tmpk = range_decode_bits(ctx, 5);
00393 overflow = 0;
00394 } else
00395 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
00396
00397 if (tmpk <= 16)
00398 x = range_decode_bits(ctx, tmpk);
00399 else {
00400 x = range_decode_bits(ctx, 16);
00401 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
00402 }
00403 x += overflow << tmpk;
00404 } else {
00405 int base, pivot;
00406
00407 pivot = rice->ksum >> 5;
00408 if (pivot == 0)
00409 pivot = 1;
00410
00411 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
00412
00413 if (overflow == (MODEL_ELEMENTS - 1)) {
00414 overflow = range_decode_bits(ctx, 16) << 16;
00415 overflow |= range_decode_bits(ctx, 16);
00416 }
00417
00418 if (pivot < 0x10000) {
00419 base = range_decode_culfreq(ctx, pivot);
00420 range_decode_update(ctx, 1, base);
00421 } else {
00422 int base_hi = pivot, base_lo;
00423 int bbits = 0;
00424
00425 while (base_hi & ~0xFFFF) {
00426 base_hi >>= 1;
00427 bbits++;
00428 }
00429 base_hi = range_decode_culfreq(ctx, base_hi + 1);
00430 range_decode_update(ctx, 1, base_hi);
00431 base_lo = range_decode_culfreq(ctx, 1 << bbits);
00432 range_decode_update(ctx, 1, base_lo);
00433
00434 base = (base_hi << bbits) + base_lo;
00435 }
00436
00437 x = base + overflow * pivot;
00438 }
00439
00440 update_rice(rice, x);
00441
00442
00443 if (x & 1)
00444 return (x >> 1) + 1;
00445 else
00446 return -(x >> 1);
00447 }
00448
00449 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo)
00450 {
00451 int32_t *decoded0 = ctx->decoded0;
00452 int32_t *decoded1 = ctx->decoded1;
00453
00454 ctx->blocksdecoded = blockstodecode;
00455
00456 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00457
00458 memset(decoded0, 0, blockstodecode * sizeof(int32_t));
00459 memset(decoded1, 0, blockstodecode * sizeof(int32_t));
00460 } else {
00461 while (blockstodecode--) {
00462 *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
00463 if (stereo)
00464 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
00465 }
00466 }
00467
00468 if (ctx->blocksdecoded == ctx->currentframeblocks)
00469 range_dec_normalize(ctx);
00470 }
00471
00472 static void init_entropy_decoder(APEContext * ctx)
00473 {
00474
00475 ctx->CRC = bytestream_get_be32(&ctx->ptr);
00476
00477
00478 ctx->frameflags = 0;
00479 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
00480 ctx->CRC &= ~0x80000000;
00481
00482 ctx->frameflags = bytestream_get_be32(&ctx->ptr);
00483 }
00484
00485
00486 ctx->blocksdecoded = 0;
00487
00488
00489 ctx->riceX.k = 10;
00490 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
00491 ctx->riceY.k = 10;
00492 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
00493
00494
00495 ctx->ptr++;
00496
00497 range_start_decoding(ctx);
00498 }
00499
00500 static const int32_t initial_coeffs[4] = {
00501 360, 317, -109, 98
00502 };
00503
00504 static void init_predictor_decoder(APEContext * ctx)
00505 {
00506 APEPredictor *p = &ctx->predictor;
00507
00508
00509 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
00510 p->buf = p->historybuffer;
00511
00512
00513 memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
00514 memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
00515 memset(p->coeffsB, 0, sizeof(p->coeffsB));
00516
00517 p->filterA[0] = p->filterA[1] = 0;
00518 p->filterB[0] = p->filterB[1] = 0;
00519 p->lastA[0] = p->lastA[1] = 0;
00520 }
00521
00523 static inline int APESIGN(int32_t x) {
00524 return (x < 0) - (x > 0);
00525 }
00526
00527 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)
00528 {
00529 int32_t predictionA, predictionB, sign;
00530
00531 p->buf[delayA] = p->lastA[filter];
00532 p->buf[adaptA] = APESIGN(p->buf[delayA]);
00533 p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
00534 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
00535
00536 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
00537 p->buf[delayA - 1] * p->coeffsA[filter][1] +
00538 p->buf[delayA - 2] * p->coeffsA[filter][2] +
00539 p->buf[delayA - 3] * p->coeffsA[filter][3];
00540
00541
00542 p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
00543 p->buf[adaptB] = APESIGN(p->buf[delayB]);
00544 p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
00545 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
00546 p->filterB[filter] = p->filterA[filter ^ 1];
00547
00548 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
00549 p->buf[delayB - 1] * p->coeffsB[filter][1] +
00550 p->buf[delayB - 2] * p->coeffsB[filter][2] +
00551 p->buf[delayB - 3] * p->coeffsB[filter][3] +
00552 p->buf[delayB - 4] * p->coeffsB[filter][4];
00553
00554 p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
00555 p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
00556
00557 sign = APESIGN(decoded);
00558 p->coeffsA[filter][0] += p->buf[adaptA ] * sign;
00559 p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
00560 p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
00561 p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
00562 p->coeffsB[filter][0] += p->buf[adaptB ] * sign;
00563 p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
00564 p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
00565 p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
00566 p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
00567
00568 return p->filterA[filter];
00569 }
00570
00571 static void predictor_decode_stereo(APEContext * ctx, int count)
00572 {
00573 APEPredictor *p = &ctx->predictor;
00574 int32_t *decoded0 = ctx->decoded0;
00575 int32_t *decoded1 = ctx->decoded1;
00576
00577 while (count--) {
00578
00579 *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB);
00580 decoded0++;
00581 *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB);
00582 decoded1++;
00583
00584
00585 p->buf++;
00586
00587
00588 if (p->buf == p->historybuffer + HISTORY_SIZE) {
00589 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
00590 p->buf = p->historybuffer;
00591 }
00592 }
00593 }
00594
00595 static void predictor_decode_mono(APEContext * ctx, int count)
00596 {
00597 APEPredictor *p = &ctx->predictor;
00598 int32_t *decoded0 = ctx->decoded0;
00599 int32_t predictionA, currentA, A, sign;
00600
00601 currentA = p->lastA[0];
00602
00603 while (count--) {
00604 A = *decoded0;
00605
00606 p->buf[YDELAYA] = currentA;
00607 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
00608
00609 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
00610 p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
00611 p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
00612 p->buf[YDELAYA - 3] * p->coeffsA[0][3];
00613
00614 currentA = A + (predictionA >> 10);
00615
00616 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
00617 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
00618
00619 sign = APESIGN(A);
00620 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ] * sign;
00621 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
00622 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
00623 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
00624
00625 p->buf++;
00626
00627
00628 if (p->buf == p->historybuffer + HISTORY_SIZE) {
00629 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
00630 p->buf = p->historybuffer;
00631 }
00632
00633 p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
00634 *(decoded0++) = p->filterA[0];
00635 }
00636
00637 p->lastA[0] = currentA;
00638 }
00639
00640 static void do_init_filter(APEFilter *f, int16_t * buf, int order)
00641 {
00642 f->coeffs = buf;
00643 f->historybuffer = buf + order;
00644 f->delay = f->historybuffer + order * 2;
00645 f->adaptcoeffs = f->historybuffer + order;
00646
00647 memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
00648 memset(f->coeffs, 0, order * sizeof(int16_t));
00649 f->avg = 0;
00650 }
00651
00652 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order)
00653 {
00654 do_init_filter(&f[0], buf, order);
00655 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
00656 }
00657
00658 static void do_apply_filter(APEContext * ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
00659 {
00660 int res;
00661 int absres;
00662
00663 while (count--) {
00664
00665 res = ctx->dsp.scalarproduct_and_madd_int16(f->coeffs, f->delay - order, f->adaptcoeffs - order, order, APESIGN(*data));
00666 res = (res + (1 << (fracbits - 1))) >> fracbits;
00667 res += *data;
00668 *data++ = res;
00669
00670
00671 *f->delay++ = av_clip_int16(res);
00672
00673 if (version < 3980) {
00674
00675 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
00676 f->adaptcoeffs[-4] >>= 1;
00677 f->adaptcoeffs[-8] >>= 1;
00678 } else {
00679
00680
00681
00682 absres = FFABS(res);
00683 if (absres)
00684 *f->adaptcoeffs = ((res & (1<<31)) - (1<<30)) >> (25 + (absres <= f->avg*3) + (absres <= f->avg*4/3));
00685 else
00686 *f->adaptcoeffs = 0;
00687
00688 f->avg += (absres - f->avg) / 16;
00689
00690 f->adaptcoeffs[-1] >>= 1;
00691 f->adaptcoeffs[-2] >>= 1;
00692 f->adaptcoeffs[-8] >>= 1;
00693 }
00694
00695 f->adaptcoeffs++;
00696
00697
00698 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
00699 memmove(f->historybuffer, f->delay - (order * 2),
00700 (order * 2) * sizeof(int16_t));
00701 f->delay = f->historybuffer + order * 2;
00702 f->adaptcoeffs = f->historybuffer + order;
00703 }
00704 }
00705 }
00706
00707 static void apply_filter(APEContext * ctx, APEFilter *f,
00708 int32_t * data0, int32_t * data1,
00709 int count, int order, int fracbits)
00710 {
00711 do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
00712 if (data1)
00713 do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
00714 }
00715
00716 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0,
00717 int32_t * decoded1, int count)
00718 {
00719 int i;
00720
00721 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00722 if (!ape_filter_orders[ctx->fset][i])
00723 break;
00724 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
00725 }
00726 }
00727
00728 static void init_frame_decoder(APEContext * ctx)
00729 {
00730 int i;
00731 init_entropy_decoder(ctx);
00732 init_predictor_decoder(ctx);
00733
00734 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00735 if (!ape_filter_orders[ctx->fset][i])
00736 break;
00737 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
00738 }
00739 }
00740
00741 static void ape_unpack_mono(APEContext * ctx, int count)
00742 {
00743 int32_t left;
00744 int32_t *decoded0 = ctx->decoded0;
00745 int32_t *decoded1 = ctx->decoded1;
00746
00747 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00748 entropy_decode(ctx, count, 0);
00749
00750 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
00751 return;
00752 }
00753
00754 entropy_decode(ctx, count, 0);
00755 ape_apply_filters(ctx, decoded0, NULL, count);
00756
00757
00758 predictor_decode_mono(ctx, count);
00759
00760
00761 if (ctx->channels == 2) {
00762 while (count--) {
00763 left = *decoded0;
00764 *(decoded1++) = *(decoded0++) = left;
00765 }
00766 }
00767 }
00768
00769 static void ape_unpack_stereo(APEContext * ctx, int count)
00770 {
00771 int32_t left, right;
00772 int32_t *decoded0 = ctx->decoded0;
00773 int32_t *decoded1 = ctx->decoded1;
00774
00775 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00776
00777 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
00778 return;
00779 }
00780
00781 entropy_decode(ctx, count, 1);
00782 ape_apply_filters(ctx, decoded0, decoded1, count);
00783
00784
00785 predictor_decode_stereo(ctx, count);
00786
00787
00788 while (count--) {
00789 left = *decoded1 - (*decoded0 / 2);
00790 right = left + *decoded0;
00791
00792 *(decoded0++) = left;
00793 *(decoded1++) = right;
00794 }
00795 }
00796
00797 static int ape_decode_frame(AVCodecContext * avctx,
00798 void *data, int *data_size,
00799 AVPacket *avpkt)
00800 {
00801 const uint8_t *buf = avpkt->data;
00802 int buf_size = avpkt->size;
00803 APEContext *s = avctx->priv_data;
00804 int16_t *samples = data;
00805 uint32_t nblocks;
00806 int i, n;
00807 int blockstodecode;
00808 int bytes_used;
00809
00810 if (buf_size == 0 && !s->samples) {
00811 *data_size = 0;
00812 return 0;
00813 }
00814
00815
00816 if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {
00817 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);
00818 return -1;
00819 }
00820
00821 if(!s->samples){
00822 void *tmp_data = av_realloc(s->data, (buf_size + 3) & ~3);
00823 if (!tmp_data)
00824 return AVERROR(ENOMEM);
00825 s->data = tmp_data;
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 = 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 buf += 4;
00840 if (!nblocks || nblocks > INT_MAX) {
00841 av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks);
00842 *data_size = 0;
00843 return AVERROR_INVALIDDATA;
00844 }
00845 s->currentframeblocks = s->samples = nblocks;
00846
00847 memset(s->decoded0, 0, sizeof(s->decoded0));
00848 memset(s->decoded1, 0, sizeof(s->decoded1));
00849
00850
00851 init_frame_decoder(s);
00852 }
00853
00854 if (!s->data) {
00855 *data_size = 0;
00856 return buf_size;
00857 }
00858
00859 nblocks = s->samples;
00860 blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);
00861
00862 s->error=0;
00863
00864 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
00865 ape_unpack_mono(s, blockstodecode);
00866 else
00867 ape_unpack_stereo(s, blockstodecode);
00868 emms_c();
00869
00870 if(s->error || s->ptr > s->data_end){
00871 s->samples=0;
00872 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
00873 return -1;
00874 }
00875
00876 for (i = 0; i < blockstodecode; i++) {
00877 *samples++ = s->decoded0[i];
00878 if(s->channels == 2)
00879 *samples++ = s->decoded1[i];
00880 }
00881
00882 s->samples -= blockstodecode;
00883
00884 *data_size = blockstodecode * 2 * s->channels;
00885 bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;
00886 s->last_ptr = s->ptr;
00887 return bytes_used;
00888 }
00889
00890 static void ape_flush(AVCodecContext *avctx)
00891 {
00892 APEContext *s = avctx->priv_data;
00893 s->samples= 0;
00894 }
00895
00896 AVCodec ff_ape_decoder = {
00897 "ape",
00898 AVMEDIA_TYPE_AUDIO,
00899 CODEC_ID_APE,
00900 sizeof(APEContext),
00901 ape_decode_init,
00902 NULL,
00903 ape_decode_close,
00904 ape_decode_frame,
00905 .capabilities = CODEC_CAP_SUBFRAMES,
00906 .flush = ape_flush,
00907 .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
00908 };