History

ape: make version-dependent decoding functions called via pointers

This will help in supporting old versions, e.g. version 3.93 uses the same range coder but different predictor and version 3.82 uses different range coder and predictor. Also this should not make decoding newer versions slower by introducing additional checks on versions.

Kostya Shishkov committed 13 years ago

Commit b164d661 parent 81cf53e1 file · +123 −59Browse files

Unified · Split

libavcodec/apedec.c+123−59
@@ -161,8 +161,24 @@ typedef struct APEContext { · 160 unmodified lines
161 const uint8_t *ptr; ///< current position in frame data161 const uint8_t *ptr; ///< current position in frame data
162 162
163 int error;163 int error;
164
165 void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
166 void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
167 void (*predictor_decode_mono)(struct APEContext *ctx, int count);
168 void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
164} APEContext;169} APEContext;
165 170
171static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
172 int32_t *decoded1, int count);
173
174static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
175static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
176static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
177static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
178
179static void predictor_decode_mono_3950(APEContext *ctx, int count);
180static void predictor_decode_stereo_3950(APEContext *ctx, int count);
181
166// TODO: dsputilize182// TODO: dsputilize
167 183
168static av_cold int ape_decode_close(AVCodecContext *avctx)184static av_cold int ape_decode_close(AVCodecContext *avctx)
@@ -231,6 +247,17 @@ static av_cold int ape_decode_init(AVCodecContext *avctx) · 62 unmodified lines
231 filter_alloc_fail);247 filter_alloc_fail);
232 }248 }
233 249
250 if (s->fileversion < 3990) {
251 s->entropy_decode_mono = entropy_decode_mono_3900;
252 s->entropy_decode_stereo = entropy_decode_stereo_3900;
253 } else {
254 s->entropy_decode_mono = entropy_decode_mono_3990;
255 s->entropy_decode_stereo = entropy_decode_stereo_3990;
256 }
257
258 s->predictor_decode_mono = predictor_decode_mono_3950;
259 s->predictor_decode_stereo = predictor_decode_stereo_3950;
260
234 ff_dsputil_init(&s->dsp, avctx);261 ff_dsputil_init(&s->dsp, avctx);
235 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;262 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
236 263
@@ -401,67 +428,76 @@ static inline void update_rice(APERice *rice, unsigned int x) · 164 unmodified lines
401 rice->k++;428 rice->k++;
402}429}
403 430
404static inline int ape_decode_value(APEContext *ctx, APERice *rice)431static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
405{432{
406 unsigned int x, overflow;433 unsigned int x, overflow;
434 int tmpk;
407 435
408 if (ctx->fileversion < 3990) {436 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
409 int tmpk;
410 437
411 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);438 if (overflow == (MODEL_ELEMENTS - 1)) {
439 tmpk = range_decode_bits(ctx, 5);
440 overflow = 0;
441 } else
442 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
412 443
413 if (overflow == (MODEL_ELEMENTS - 1)) {444 if (tmpk <= 16)
414 tmpk = range_decode_bits(ctx, 5);445 x = range_decode_bits(ctx, tmpk);
415 overflow = 0;446 else if (tmpk <= 32) {
416 } else447 x = range_decode_bits(ctx, 16);
417 tmpk = (rice->k < 1) ? 0 : rice->k - 1;448 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
418
419 if (tmpk <= 16)
420 x = range_decode_bits(ctx, tmpk);
421 else if (tmpk <= 32) {
422 x = range_decode_bits(ctx, 16);
423 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
424 } else {
425 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
426 return AVERROR_INVALIDDATA;
427 }
428 x += overflow << tmpk;
429 } else {449 } else {
430 int base, pivot;450 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
451 return AVERROR_INVALIDDATA;
452 }
453 x += overflow << tmpk;
454
455 update_rice(rice, x);
456
457 /* Convert to signed */
458 if (x & 1)
459 return (x >> 1) + 1;
460 else
461 return -(x >> 1);
462}
431 463
432 pivot = rice->ksum >> 5;464static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
433 if (pivot == 0)465{
434 pivot = 1;466 unsigned int x, overflow;
467 int base, pivot;
435 468
436 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);469 pivot = rice->ksum >> 5;
470 if (pivot == 0)
471 pivot = 1;
437 472
438 if (overflow == (MODEL_ELEMENTS - 1)) {473 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
439 overflow = range_decode_bits(ctx, 16) << 16;
440 overflow |= range_decode_bits(ctx, 16);
441 }
442 474
443 if (pivot < 0x10000) {475 if (overflow == (MODEL_ELEMENTS - 1)) {
444 base = range_decode_culfreq(ctx, pivot);476 overflow = range_decode_bits(ctx, 16) << 16;
445 range_decode_update(ctx, 1, base);477 overflow |= range_decode_bits(ctx, 16);
446 } else {478 }
447 int base_hi = pivot, base_lo;479
448 int bbits = 0;480 if (pivot < 0x10000) {
449 481 base = range_decode_culfreq(ctx, pivot);
450 while (base_hi & ~0xFFFF) {482 range_decode_update(ctx, 1, base);
451 base_hi >>= 1;483 } else {
452 bbits++;484 int base_hi = pivot, base_lo;
453 }485 int bbits = 0;
454 base_hi = range_decode_culfreq(ctx, base_hi + 1);486
455 range_decode_update(ctx, 1, base_hi);487 while (base_hi & ~0xFFFF) {
456 base_lo = range_decode_culfreq(ctx, 1 << bbits);488 base_hi >>= 1;
457 range_decode_update(ctx, 1, base_lo);489 bbits++;
458
459 base = (base_hi << bbits) + base_lo;
460 }490 }
491 base_hi = range_decode_culfreq(ctx, base_hi + 1);
492 range_decode_update(ctx, 1, base_hi);
493 base_lo = range_decode_culfreq(ctx, 1 << bbits);
494 range_decode_update(ctx, 1, base_lo);
461 495
462 x = base + overflow * pivot;496 base = (base_hi << bbits) + base_lo;
463 }497 }
464 498
499 x = base + overflow * pivot;
500
465 update_rice(rice, x);501 update_rice(rice, x);
466 502
467 /* Convert to signed */503 /* Convert to signed */
@@ -471,15 +507,41 @@ static inline int ape_decode_value(APEContext *ctx, APERice *rice) · 3 unmodified lines
471 return -(x >> 1);507 return -(x >> 1);
472}508}
473 509
474static void entropy_decode(APEContext *ctx, int blockstodecode, int stereo)510static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
511{
512 int32_t *decoded0 = ctx->decoded[0];
513
514 while (blockstodecode--)
515 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
516}
517
518static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
475{519{
476 int32_t *decoded0 = ctx->decoded[0];520 int32_t *decoded0 = ctx->decoded[0];
477 int32_t *decoded1 = ctx->decoded[1];521 int32_t *decoded1 = ctx->decoded[1];
478 522
479 while (blockstodecode--) {523 while (blockstodecode--) {
480 *decoded0++ = ape_decode_value(ctx, &ctx->riceY);524 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
481 if (stereo)525 *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
482 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);526 }
527}
528
529static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
530{
531 int32_t *decoded0 = ctx->decoded[0];
532
533 while (blockstodecode--)
534 *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
535}
536
537static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
538{
539 int32_t *decoded0 = ctx->decoded[0];
540 int32_t *decoded1 = ctx->decoded[1];
541
542 while (blockstodecode--) {
543 *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
544 *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
483 }545 }
484}546}
485 547
@@ -588,12 +650,14 @@ static av_always_inline int predictor_update_filter(APEPredictor *p, · 102 unmodified lines
588 return p->filterA[filter];650 return p->filterA[filter];
589}651}
590 652
591static void predictor_decode_stereo(APEContext *ctx, int count)653static void predictor_decode_stereo_3950(APEContext *ctx, int count)
592{654{
593 APEPredictor *p = &ctx->predictor;655 APEPredictor *p = &ctx->predictor;
594 int32_t *decoded0 = ctx->decoded[0];656 int32_t *decoded0 = ctx->decoded[0];
595 int32_t *decoded1 = ctx->decoded[1];657 int32_t *decoded1 = ctx->decoded[1];
596 658
659 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
660
597 while (count--) {661 while (count--) {
598 /* Predictor Y */662 /* Predictor Y */
599 *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,663 *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
@@ -615,12 +679,14 @@ static void predictor_decode_stereo(APEContext *ctx, int count) · 15 unmodified lines
615 }679 }
616}680}
617 681
618static void predictor_decode_mono(APEContext *ctx, int count)682static void predictor_decode_mono_3950(APEContext *ctx, int count)
619{683{
620 APEPredictor *p = &ctx->predictor;684 APEPredictor *p = &ctx->predictor;
621 int32_t *decoded0 = ctx->decoded[0];685 int32_t *decoded0 = ctx->decoded[0];
622 int32_t predictionA, currentA, A, sign;686 int32_t predictionA, currentA, A, sign;
623 687
688 ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
689
624 currentA = p->lastA[0];690 currentA = p->lastA[0];
625 691
626 while (count--) {692 while (count--) {
@@ -779,11 +845,10 @@ static void ape_unpack_mono(APEContext *ctx, int count) · 152 unmodified lines
779 return;845 return;
780 }846 }
781 847
782 entropy_decode(ctx, count, 0);848 ctx->entropy_decode_mono(ctx, count);
783 ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
784 849
785 /* Now apply the predictor decoding */850 /* Now apply the predictor decoding */
786 predictor_decode_mono(ctx, count);851 ctx->predictor_decode_mono(ctx, count);
787 852
788 /* Pseudo-stereo - just copy left channel to right channel */853 /* Pseudo-stereo - just copy left channel to right channel */
789 if (ctx->channels == 2) {854 if (ctx->channels == 2) {
@@ -803,11 +868,10 @@ static void ape_unpack_stereo(APEContext *ctx, int count) · 13 unmodified lines
803 return;868 return;
804 }869 }
805 870
806 entropy_decode(ctx, count, 1);871 ctx->entropy_decode_stereo(ctx, count);
807 ape_apply_filters(ctx, decoded0, decoded1, count);
808 872
809 /* Now apply the predictor decoding */873 /* Now apply the predictor decoding */
810 predictor_decode_stereo(ctx, count);874 ctx->predictor_decode_stereo(ctx, count);
811 875
812 /* Decorrelate and scale to output depth */876 /* Decorrelate and scale to output depth */
813 while (count--) {877 while (count--) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t