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
161161 const uint8_t *ptr; ///< current position in frame data
162162
163163 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);
164169 } APEContext;
165170
171+static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
172+ int32_t *decoded1, int count);
173+
174+static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
175+static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
176+static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
177+static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
178+
179+static void predictor_decode_mono_3950(APEContext *ctx, int count);
180+static void predictor_decode_stereo_3950(APEContext *ctx, int count);
181+
166182 // TODO: dsputilize
167183
168184 static av_cold int ape_decode_close(AVCodecContext *avctx)
@@ -231,6 +247,17 @@ static av_cold int ape_decode_init(AVCodecContext *avctx) · 62 unmodified lines
231247 filter_alloc_fail);
232248 }
233249
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+
234261 ff_dsputil_init(&s->dsp, avctx);
235262 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
236263
@@ -401,67 +428,76 @@ static inline void update_rice(APERice *rice, unsigned int x) · 164 unmodified lines
401428 rice->k++;
402429 }
403430
404−static inline int ape_decode_value(APEContext *ctx, APERice *rice)
431+static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
405432 {
406433 unsigned int x, overflow;
434+ int tmpk;
407435
408− if (ctx->fileversion < 3990) {
409− int tmpk;
436+ overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
410437
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;
412443
413− if (overflow == (MODEL_ELEMENTS - 1)) {
414− tmpk = range_decode_bits(ctx, 5);
415− overflow = 0;
416− } else
417− tmpk = (rice->k < 1) ? 0 : rice->k - 1;
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;
444+ if (tmpk <= 16)
445+ x = range_decode_bits(ctx, tmpk);
446+ else if (tmpk <= 32) {
447+ x = range_decode_bits(ctx, 16);
448+ x |= (range_decode_bits(ctx, tmpk - 16) << 16);
429449 } 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+}
431463
432− pivot = rice->ksum >> 5;
433− if (pivot == 0)
434− pivot = 1;
464+static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
465+{
466+ unsigned int x, overflow;
467+ int base, pivot;
435468
436− overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
469+ pivot = rice->ksum >> 5;
470+ if (pivot == 0)
471+ pivot = 1;
437472
438− if (overflow == (MODEL_ELEMENTS - 1)) {
439− overflow = range_decode_bits(ctx, 16) << 16;
440− overflow |= range_decode_bits(ctx, 16);
441− }
473+ overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
442474
443− if (pivot < 0x10000) {
444− base = range_decode_culfreq(ctx, pivot);
445− range_decode_update(ctx, 1, base);
446− } else {
447− int base_hi = pivot, base_lo;
448− int bbits = 0;
449−
450− while (base_hi & ~0xFFFF) {
451− base_hi >>= 1;
452− bbits++;
453− }
454− base_hi = range_decode_culfreq(ctx, base_hi + 1);
455− range_decode_update(ctx, 1, base_hi);
456− base_lo = range_decode_culfreq(ctx, 1 << bbits);
457− range_decode_update(ctx, 1, base_lo);
458−
459− base = (base_hi << bbits) + base_lo;
475+ if (overflow == (MODEL_ELEMENTS - 1)) {
476+ overflow = range_decode_bits(ctx, 16) << 16;
477+ overflow |= range_decode_bits(ctx, 16);
478+ }
479+
480+ if (pivot < 0x10000) {
481+ base = range_decode_culfreq(ctx, pivot);
482+ range_decode_update(ctx, 1, base);
483+ } else {
484+ int base_hi = pivot, base_lo;
485+ int bbits = 0;
486+
487+ while (base_hi & ~0xFFFF) {
488+ base_hi >>= 1;
489+ bbits++;
460490 }
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);
461495
462− x = base + overflow * pivot;
496+ base = (base_hi << bbits) + base_lo;
463497 }
464498
499+ x = base + overflow * pivot;
500+
465501 update_rice(rice, x);
466502
467503 /* Convert to signed */
@@ -471,15 +507,41 @@ static inline int ape_decode_value(APEContext *ctx, APERice *rice) · 3 unmodified lines
471507 return -(x >> 1);
472508 }
473509
474−static void entropy_decode(APEContext *ctx, int blockstodecode, int stereo)
510+static 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+
518+static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
475519 {
476520 int32_t *decoded0 = ctx->decoded[0];
477521 int32_t *decoded1 = ctx->decoded[1];
478522
479523 while (blockstodecode--) {
480− *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
481− if (stereo)
482− *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
524+ *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
525+ *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
526+ }
527+}
528+
529+static 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+
537+static 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);
483545 }
484546 }
485547
@@ -588,12 +650,14 @@ static av_always_inline int predictor_update_filter(APEPredictor *p, · 102 unmodified lines
588650 return p->filterA[filter];
589651 }
590652
591−static void predictor_decode_stereo(APEContext *ctx, int count)
653+static void predictor_decode_stereo_3950(APEContext *ctx, int count)
592654 {
593655 APEPredictor *p = &ctx->predictor;
594656 int32_t *decoded0 = ctx->decoded[0];
595657 int32_t *decoded1 = ctx->decoded[1];
596658
659+ ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
660+
597661 while (count--) {
598662 /* Predictor Y */
599663 *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
615679 }
616680 }
617681
618−static void predictor_decode_mono(APEContext *ctx, int count)
682+static void predictor_decode_mono_3950(APEContext *ctx, int count)
619683 {
620684 APEPredictor *p = &ctx->predictor;
621685 int32_t *decoded0 = ctx->decoded[0];
622686 int32_t predictionA, currentA, A, sign;
623687
688+ ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
689+
624690 currentA = p->lastA[0];
625691
626692 while (count--) {
@@ -779,11 +845,10 @@ static void ape_unpack_mono(APEContext *ctx, int count) · 152 unmodified lines
779845 return;
780846 }
781847
782− entropy_decode(ctx, count, 0);
783− ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
848+ ctx->entropy_decode_mono(ctx, count);
784849
785850 /* Now apply the predictor decoding */
786− predictor_decode_mono(ctx, count);
851+ ctx->predictor_decode_mono(ctx, count);
787852
788853 /* Pseudo-stereo - just copy left channel to right channel */
789854 if (ctx->channels == 2) {
@@ -803,11 +868,10 @@ static void ape_unpack_stereo(APEContext *ctx, int count) · 13 unmodified lines
803868 return;
804869 }
805870
806− entropy_decode(ctx, count, 1);
807− ape_apply_filters(ctx, decoded0, decoded1, count);
871+ ctx->entropy_decode_stereo(ctx, count);
808872
809873 /* Now apply the predictor decoding */
810− predictor_decode_stereo(ctx, count);
874+ ctx->predictor_decode_stereo(ctx, count);
811875
812876 /* Decorrelate and scale to output depth */
813877 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