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 uint16_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 } APEContext;
00160
00161
00162 static inline void vector_add(int16_t * v1, int16_t * v2, int order)
00163 {
00164 while (order--)
00165 *v1++ += *v2++;
00166 }
00167
00168
00169 static inline void vector_sub(int16_t * v1, int16_t * v2, int order)
00170 {
00171 while (order--)
00172 *v1++ -= *v2++;
00173 }
00174
00175
00176 static inline int32_t scalarproduct(int16_t * v1, int16_t * v2, int order)
00177 {
00178 int res = 0;
00179
00180 while (order--)
00181 res += *v1++ * *v2++;
00182
00183 return res;
00184 }
00185
00186 static int ape_decode_init(AVCodecContext * avctx)
00187 {
00188 APEContext *s = avctx->priv_data;
00189 int i;
00190
00191 if (avctx->extradata_size != 6) {
00192 av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
00193 return -1;
00194 }
00195 if (avctx->bits_per_sample != 16) {
00196 av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n");
00197 return -1;
00198 }
00199 if (avctx->channels > 2) {
00200 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
00201 return -1;
00202 }
00203 s->avctx = avctx;
00204 s->channels = avctx->channels;
00205 s->fileversion = AV_RL16(avctx->extradata);
00206 s->compression_level = AV_RL16(avctx->extradata + 2);
00207 s->flags = AV_RL16(avctx->extradata + 4);
00208
00209 av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags);
00210 if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) {
00211 av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level);
00212 return -1;
00213 }
00214 s->fset = s->compression_level / 1000 - 1;
00215 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00216 if (!ape_filter_orders[s->fset][i])
00217 break;
00218 s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4);
00219 }
00220
00221 dsputil_init(&s->dsp, avctx);
00222 return 0;
00223 }
00224
00225 static int ape_decode_close(AVCodecContext * avctx)
00226 {
00227 APEContext *s = avctx->priv_data;
00228 int i;
00229
00230 for (i = 0; i < APE_FILTER_LEVELS; i++)
00231 av_freep(&s->filterbuf[i]);
00232
00233 return 0;
00234 }
00235
00241 #define CODE_BITS 32
00242 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
00243 #define SHIFT_BITS (CODE_BITS - 9)
00244 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
00245 #define BOTTOM_VALUE (TOP_VALUE >> 8)
00246
00248 static inline void range_start_decoding(APEContext * ctx)
00249 {
00250 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
00251 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
00252 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
00253 }
00254
00256 static inline void range_dec_normalize(APEContext * ctx)
00257 {
00258 while (ctx->rc.range <= BOTTOM_VALUE) {
00259 ctx->rc.buffer = (ctx->rc.buffer << 8) | bytestream_get_byte(&ctx->ptr);
00260 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
00261 ctx->rc.range <<= 8;
00262 }
00263 }
00264
00270 static inline int range_decode_culfreq(APEContext * ctx, int tot_f)
00271 {
00272 range_dec_normalize(ctx);
00273 ctx->rc.help = ctx->rc.range / tot_f;
00274 return ctx->rc.low / ctx->rc.help;
00275 }
00276
00281 static inline int range_decode_culshift(APEContext * ctx, int shift)
00282 {
00283 range_dec_normalize(ctx);
00284 ctx->rc.help = ctx->rc.range >> shift;
00285 return ctx->rc.low / ctx->rc.help;
00286 }
00287
00288
00294 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f)
00295 {
00296 ctx->rc.low -= ctx->rc.help * lt_f;
00297 ctx->rc.range = ctx->rc.help * sy_f;
00298 }
00299
00301 static inline int range_decode_bits(APEContext * ctx, int n)
00302 {
00303 int sym = range_decode_culshift(ctx, n);
00304 range_decode_update(ctx, 1, sym);
00305 return sym;
00306 }
00307
00308
00309 #define MODEL_ELEMENTS 64
00310
00314 static const uint32_t counts_3970[65] = {
00315 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
00316 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
00317 65450, 65469, 65480, 65487, 65491, 65493, 65494, 65495,
00318 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503,
00319 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511,
00320 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519,
00321 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527,
00322 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
00323 65536
00324 };
00325
00329 static const uint16_t counts_diff_3970[64] = {
00330 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
00331 1104, 677, 415, 248, 150, 89, 54, 31,
00332 19, 11, 7, 4, 2, 1, 1, 1,
00333 1, 1, 1, 1, 1, 1, 1, 1,
00334 1, 1, 1, 1, 1, 1, 1, 1,
00335 1, 1, 1, 1, 1, 1, 1, 1,
00336 1, 1, 1, 1, 1, 1, 1, 1,
00337 1, 1, 1, 1, 1, 1, 1, 1
00338 };
00339
00343 static const uint32_t counts_3980[65] = {
00344 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
00345 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
00346 65485, 65488, 65490, 65491, 65492, 65493, 65494, 65495,
00347 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503,
00348 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511,
00349 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519,
00350 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527,
00351 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
00352 65536
00353 };
00354
00358 static const uint16_t counts_diff_3980[64] = {
00359 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
00360 261, 119, 65, 31, 19, 10, 6, 3,
00361 3, 2, 1, 1, 1, 1, 1, 1,
00362 1, 1, 1, 1, 1, 1, 1, 1,
00363 1, 1, 1, 1, 1, 1, 1, 1,
00364 1, 1, 1, 1, 1, 1, 1, 1,
00365 1, 1, 1, 1, 1, 1, 1, 1,
00366 1, 1, 1, 1, 1, 1, 1, 1
00367 };
00368
00374 static inline int range_get_symbol(APEContext * ctx,
00375 const uint32_t counts[],
00376 const uint16_t counts_diff[])
00377 {
00378 int symbol, cf;
00379
00380 cf = range_decode_culshift(ctx, 16);
00381
00382
00383 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
00384
00385 range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
00386
00387 return symbol;
00388 }
00390
00391 static inline void update_rice(APERice *rice, int x)
00392 {
00393 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
00394
00395 if (rice->k == 0)
00396 rice->k = 1;
00397 else if (rice->ksum < (1 << (rice->k + 4)))
00398 rice->k--;
00399 else if (rice->ksum >= (1 << (rice->k + 5)))
00400 rice->k++;
00401 }
00402
00403 static inline int ape_decode_value(APEContext * ctx, APERice *rice)
00404 {
00405 int x, overflow;
00406
00407 if (ctx->fileversion < 3980) {
00408 int tmpk;
00409
00410 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
00411
00412 if (overflow == (MODEL_ELEMENTS - 1)) {
00413 tmpk = range_decode_bits(ctx, 5);
00414 overflow = 0;
00415 } else
00416 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
00417
00418 if (tmpk <= 16)
00419 x = range_decode_bits(ctx, tmpk);
00420 else {
00421 x = range_decode_bits(ctx, 16);
00422 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
00423 }
00424 x += overflow << tmpk;
00425 } else {
00426 int base, pivot;
00427
00428 pivot = rice->ksum >> 5;
00429 if (pivot == 0)
00430 pivot = 1;
00431
00432 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
00433
00434 if (overflow == (MODEL_ELEMENTS - 1)) {
00435 overflow = range_decode_bits(ctx, 16) << 16;
00436 overflow |= range_decode_bits(ctx, 16);
00437 }
00438
00439 base = range_decode_culfreq(ctx, pivot);
00440 range_decode_update(ctx, 1, base);
00441
00442 x = base + overflow * pivot;
00443 }
00444
00445 update_rice(rice, x);
00446
00447
00448 if (x & 1)
00449 return (x >> 1) + 1;
00450 else
00451 return -(x >> 1);
00452 }
00453
00454 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo)
00455 {
00456 int32_t *decoded0 = ctx->decoded0;
00457 int32_t *decoded1 = ctx->decoded1;
00458
00459 ctx->blocksdecoded = blockstodecode;
00460
00461 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00462
00463 memset(decoded0, 0, blockstodecode * sizeof(int32_t));
00464 memset(decoded1, 0, blockstodecode * sizeof(int32_t));
00465 } else {
00466 while (blockstodecode--) {
00467 *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
00468 if (stereo)
00469 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
00470 }
00471 }
00472
00473 if (ctx->blocksdecoded == ctx->currentframeblocks)
00474 range_dec_normalize(ctx);
00475 }
00476
00477 static void init_entropy_decoder(APEContext * ctx)
00478 {
00479
00480 ctx->CRC = bytestream_get_be32(&ctx->ptr);
00481
00482
00483 ctx->frameflags = 0;
00484 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
00485 ctx->CRC &= ~0x80000000;
00486
00487 ctx->frameflags = bytestream_get_be32(&ctx->ptr);
00488 }
00489
00490
00491 ctx->blocksdecoded = 0;
00492
00493
00494 ctx->riceX.k = 10;
00495 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
00496 ctx->riceY.k = 10;
00497 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
00498
00499
00500 ctx->ptr++;
00501
00502 range_start_decoding(ctx);
00503 }
00504
00505 static const int32_t initial_coeffs[4] = {
00506 360, 317, -109, 98
00507 };
00508
00509 static void init_predictor_decoder(APEContext * ctx)
00510 {
00511 APEPredictor *p = &ctx->predictor;
00512
00513
00514 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
00515 p->buf = p->historybuffer;
00516
00517
00518 memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
00519 memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
00520 memset(p->coeffsB, 0, sizeof(p->coeffsB));
00521
00522 p->filterA[0] = p->filterA[1] = 0;
00523 p->filterB[0] = p->filterB[1] = 0;
00524 p->lastA[0] = p->lastA[1] = 0;
00525 }
00526
00528 static inline int APESIGN(int32_t x) {
00529 return (x < 0) - (x > 0);
00530 }
00531
00532 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)
00533 {
00534 int32_t predictionA, predictionB;
00535
00536 p->buf[delayA] = p->lastA[filter];
00537 p->buf[adaptA] = APESIGN(p->buf[delayA]);
00538 p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
00539 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
00540
00541 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
00542 p->buf[delayA - 1] * p->coeffsA[filter][1] +
00543 p->buf[delayA - 2] * p->coeffsA[filter][2] +
00544 p->buf[delayA - 3] * p->coeffsA[filter][3];
00545
00546
00547 p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
00548 p->buf[adaptB] = APESIGN(p->buf[delayB]);
00549 p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
00550 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
00551 p->filterB[filter] = p->filterA[filter ^ 1];
00552
00553 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
00554 p->buf[delayB - 1] * p->coeffsB[filter][1] +
00555 p->buf[delayB - 2] * p->coeffsB[filter][2] +
00556 p->buf[delayB - 3] * p->coeffsB[filter][3] +
00557 p->buf[delayB - 4] * p->coeffsB[filter][4];
00558
00559 p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
00560 p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
00561
00562 if (!decoded)
00563 return p->filterA[filter];
00564
00565 if (decoded > 0) {
00566 p->coeffsA[filter][0] -= p->buf[adaptA ];
00567 p->coeffsA[filter][1] -= p->buf[adaptA - 1];
00568 p->coeffsA[filter][2] -= p->buf[adaptA - 2];
00569 p->coeffsA[filter][3] -= p->buf[adaptA - 3];
00570
00571 p->coeffsB[filter][0] -= p->buf[adaptB ];
00572 p->coeffsB[filter][1] -= p->buf[adaptB - 1];
00573 p->coeffsB[filter][2] -= p->buf[adaptB - 2];
00574 p->coeffsB[filter][3] -= p->buf[adaptB - 3];
00575 p->coeffsB[filter][4] -= p->buf[adaptB - 4];
00576 } else {
00577 p->coeffsA[filter][0] += p->buf[adaptA ];
00578 p->coeffsA[filter][1] += p->buf[adaptA - 1];
00579 p->coeffsA[filter][2] += p->buf[adaptA - 2];
00580 p->coeffsA[filter][3] += p->buf[adaptA - 3];
00581
00582 p->coeffsB[filter][0] += p->buf[adaptB ];
00583 p->coeffsB[filter][1] += p->buf[adaptB - 1];
00584 p->coeffsB[filter][2] += p->buf[adaptB - 2];
00585 p->coeffsB[filter][3] += p->buf[adaptB - 3];
00586 p->coeffsB[filter][4] += p->buf[adaptB - 4];
00587 }
00588 return p->filterA[filter];
00589 }
00590
00591 static void predictor_decode_stereo(APEContext * ctx, int count)
00592 {
00593 int32_t predictionA, predictionB;
00594 APEPredictor *p = &ctx->predictor;
00595 int32_t *decoded0 = ctx->decoded0;
00596 int32_t *decoded1 = ctx->decoded1;
00597
00598 while (count--) {
00599
00600 predictionA = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB);
00601 predictionB = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB);
00602 *(decoded0++) = predictionA;
00603 *(decoded1++) = predictionB;
00604
00605
00606 p->buf++;
00607
00608
00609 if (p->buf == p->historybuffer + HISTORY_SIZE) {
00610 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
00611 p->buf = p->historybuffer;
00612 }
00613 }
00614 }
00615
00616 static void predictor_decode_mono(APEContext * ctx, int count)
00617 {
00618 APEPredictor *p = &ctx->predictor;
00619 int32_t *decoded0 = ctx->decoded0;
00620 int32_t predictionA, currentA, A;
00621
00622 currentA = p->lastA[0];
00623
00624 while (count--) {
00625 A = *decoded0;
00626
00627 p->buf[YDELAYA] = currentA;
00628 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
00629
00630 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
00631 p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
00632 p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
00633 p->buf[YDELAYA - 3] * p->coeffsA[0][3];
00634
00635 currentA = A + (predictionA >> 10);
00636
00637 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
00638 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
00639
00640 if (A > 0) {
00641 p->coeffsA[0][0] -= p->buf[YADAPTCOEFFSA ];
00642 p->coeffsA[0][1] -= p->buf[YADAPTCOEFFSA - 1];
00643 p->coeffsA[0][2] -= p->buf[YADAPTCOEFFSA - 2];
00644 p->coeffsA[0][3] -= p->buf[YADAPTCOEFFSA - 3];
00645 } else if (A < 0) {
00646 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ];
00647 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1];
00648 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2];
00649 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3];
00650 }
00651
00652 p->buf++;
00653
00654
00655 if (p->buf == p->historybuffer + HISTORY_SIZE) {
00656 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
00657 p->buf = p->historybuffer;
00658 }
00659
00660 p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
00661 *(decoded0++) = p->filterA[0];
00662 }
00663
00664 p->lastA[0] = currentA;
00665 }
00666
00667 static void do_init_filter(APEFilter *f, int16_t * buf, int order)
00668 {
00669 f->coeffs = buf;
00670 f->historybuffer = buf + order;
00671 f->delay = f->historybuffer + order * 2;
00672 f->adaptcoeffs = f->historybuffer + order;
00673
00674 memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
00675 memset(f->coeffs, 0, order * sizeof(int16_t));
00676 f->avg = 0;
00677 }
00678
00679 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order)
00680 {
00681 do_init_filter(&f[0], buf, order);
00682 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
00683 }
00684
00685 static inline void do_apply_filter(int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
00686 {
00687 int res;
00688 int absres;
00689
00690 while (count--) {
00691
00692 res = (scalarproduct(f->delay - order, f->coeffs, order) + (1 << (fracbits - 1))) >> fracbits;
00693
00694 if (*data < 0)
00695 vector_add(f->coeffs, f->adaptcoeffs - order, order);
00696 else if (*data > 0)
00697 vector_sub(f->coeffs, f->adaptcoeffs - order, order);
00698
00699 res += *data;
00700
00701 *data++ = res;
00702
00703
00704 *f->delay++ = av_clip_int16(res);
00705
00706 if (version < 3980) {
00707
00708 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
00709 f->adaptcoeffs[-4] >>= 1;
00710 f->adaptcoeffs[-8] >>= 1;
00711 } else {
00712
00713
00714
00715 absres = (res < 0 ? -res : res);
00716
00717 if (absres > (f->avg * 3))
00718 *f->adaptcoeffs = ((res >> 25) & 64) - 32;
00719 else if (absres > (f->avg * 4) / 3)
00720 *f->adaptcoeffs = ((res >> 26) & 32) - 16;
00721 else if (absres > 0)
00722 *f->adaptcoeffs = ((res >> 27) & 16) - 8;
00723 else
00724 *f->adaptcoeffs = 0;
00725
00726 f->avg += (absres - f->avg) / 16;
00727
00728 f->adaptcoeffs[-1] >>= 1;
00729 f->adaptcoeffs[-2] >>= 1;
00730 f->adaptcoeffs[-8] >>= 1;
00731 }
00732
00733 f->adaptcoeffs++;
00734
00735
00736 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
00737 memmove(f->historybuffer, f->delay - (order * 2),
00738 (order * 2) * sizeof(int16_t));
00739 f->delay = f->historybuffer + order * 2;
00740 f->adaptcoeffs = f->historybuffer + order;
00741 }
00742 }
00743 }
00744
00745 static void apply_filter(APEContext * ctx, APEFilter *f,
00746 int32_t * data0, int32_t * data1,
00747 int count, int order, int fracbits)
00748 {
00749 do_apply_filter(ctx->fileversion, &f[0], data0, count, order, fracbits);
00750 if (data1)
00751 do_apply_filter(ctx->fileversion, &f[1], data1, count, order, fracbits);
00752 }
00753
00754 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0,
00755 int32_t * decoded1, int count)
00756 {
00757 int i;
00758
00759 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00760 if (!ape_filter_orders[ctx->fset][i])
00761 break;
00762 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
00763 }
00764 }
00765
00766 static void init_frame_decoder(APEContext * ctx)
00767 {
00768 int i;
00769 init_entropy_decoder(ctx);
00770 init_predictor_decoder(ctx);
00771
00772 for (i = 0; i < APE_FILTER_LEVELS; i++) {
00773 if (!ape_filter_orders[ctx->fset][i])
00774 break;
00775 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
00776 }
00777 }
00778
00779 static void ape_unpack_mono(APEContext * ctx, int count)
00780 {
00781 int32_t left;
00782 int32_t *decoded0 = ctx->decoded0;
00783 int32_t *decoded1 = ctx->decoded1;
00784
00785 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00786 entropy_decode(ctx, count, 0);
00787
00788 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
00789 return;
00790 }
00791
00792 entropy_decode(ctx, count, 0);
00793 ape_apply_filters(ctx, decoded0, NULL, count);
00794
00795
00796 predictor_decode_mono(ctx, count);
00797
00798
00799 if (ctx->channels == 2) {
00800 while (count--) {
00801 left = *decoded0;
00802 *(decoded1++) = *(decoded0++) = left;
00803 }
00804 }
00805 }
00806
00807 static void ape_unpack_stereo(APEContext * ctx, int count)
00808 {
00809 int32_t left, right;
00810 int32_t *decoded0 = ctx->decoded0;
00811 int32_t *decoded1 = ctx->decoded1;
00812
00813 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
00814
00815 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
00816 return;
00817 }
00818
00819 entropy_decode(ctx, count, 1);
00820 ape_apply_filters(ctx, decoded0, decoded1, count);
00821
00822
00823 predictor_decode_stereo(ctx, count);
00824
00825
00826 while (count--) {
00827 left = *decoded1 - (*decoded0 / 2);
00828 right = left + *decoded0;
00829
00830 *(decoded0++) = left;
00831 *(decoded1++) = right;
00832 }
00833 }
00834
00835 static int ape_decode_frame(AVCodecContext * avctx,
00836 void *data, int *data_size,
00837 const uint8_t * buf, int buf_size)
00838 {
00839 APEContext *s = avctx->priv_data;
00840 int16_t *samples = data;
00841 int nblocks;
00842 int i, n;
00843 int blockstodecode;
00844 int bytes_used;
00845
00846 if (buf_size == 0 && !s->samples) {
00847 *data_size = 0;
00848 return 0;
00849 }
00850
00851
00852 if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {
00853 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);
00854 return -1;
00855 }
00856
00857 if(!s->samples){
00858 s->data = av_realloc(s->data, (buf_size + 3) & ~3);
00859 s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
00860 s->ptr = s->last_ptr = s->data;
00861 s->data_end = s->data + buf_size;
00862
00863 nblocks = s->samples = bytestream_get_be32(&s->ptr);
00864 n = bytestream_get_be32(&s->ptr);
00865 if(n < 0 || n > 3){
00866 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
00867 s->data = NULL;
00868 return -1;
00869 }
00870 s->ptr += n;
00871
00872 s->currentframeblocks = nblocks;
00873 buf += 4;
00874 if (s->samples <= 0) {
00875 *data_size = 0;
00876 return buf_size;
00877 }
00878
00879 memset(s->decoded0, 0, sizeof(s->decoded0));
00880 memset(s->decoded1, 0, sizeof(s->decoded1));
00881
00882
00883 init_frame_decoder(s);
00884 }
00885
00886 if (!s->data) {
00887 *data_size = 0;
00888 return buf_size;
00889 }
00890
00891 nblocks = s->samples;
00892 blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);
00893
00894 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
00895 ape_unpack_mono(s, blockstodecode);
00896 else
00897 ape_unpack_stereo(s, blockstodecode);
00898
00899 for (i = 0; i < blockstodecode; i++) {
00900 *samples++ = s->decoded0[i];
00901 if(s->channels == 2)
00902 *samples++ = s->decoded1[i];
00903 }
00904
00905 s->samples -= blockstodecode;
00906
00907 *data_size = blockstodecode * 2 * s->channels;
00908 bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;
00909 s->last_ptr = s->ptr;
00910 return bytes_used;
00911 }
00912
00913 AVCodec ape_decoder = {
00914 "ape",
00915 CODEC_TYPE_AUDIO,
00916 CODEC_ID_APE,
00917 sizeof(APEContext),
00918 ape_decode_init,
00919 NULL,
00920 ape_decode_close,
00921 ape_decode_frame,
00922 };