History

avcodec/wavarc: fix bugs in arithmetic coding mode

Paul B Mahol committed 3 years ago

Commit 9c2206a1 parent fb06a441 file · +48 −26Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/wavarc.c+48−26
@@ -412,13 +412,13 @@ static int ac_init(AVCodecContext *avctx, · 411 unmodified lines
412412 return 0;
413413 }
414414
415−static unsigned ac_get_prob(WavArcContext *s)
415+static uint16_t ac_get_prob(WavArcContext *s)
416416 {
417− return ((s->freq_range - 1) + ((unsigned)s->ac_value - (unsigned)s->ac_low) *
418− (unsigned)s->freq_range) / (((unsigned)s->ac_high - (unsigned)s->ac_low) + 1) & 0xffffu;
417+ return ((s->freq_range - 1) + (s->ac_value - s->ac_low) * s->freq_range) /
418+ ((s->ac_high - s->ac_low) + 1U);
419419 }
420420
421−static unsigned ac_map_symbol(WavArcContext *s, unsigned prob)
421+static uint8_t ac_map_symbol(WavArcContext *s, uint16_t prob)
422422 {
423423 int idx = 255;
424424
@@ -435,33 +435,43 @@ static int ac_normalize(AVCodecContext *avctx, WavArcContext *s, GetBitContext * · 10 unmodified lines
435435 {
436436 int range;
437437
438− range = (unsigned)s->ac_high - (unsigned)s->ac_low + 1;
439− s->ac_high = (int16_t)((range * (unsigned)s->range_high) / (unsigned)s->freq_range) + -1 + s->ac_low;
440− s->ac_low += (int16_t)((range * (unsigned)s->range_low) / (unsigned)s->freq_range);
438+ if (s->ac_high < s->ac_low)
439+ goto fail;
440+
441+ range = (s->ac_high - s->ac_low) + 1;
442+ s->ac_high = (range * s->range_high) / s->freq_range + s->ac_low - 1;
443+ s->ac_low += (range * s->range_low) / s->freq_range;
444+
445+ if (s->ac_high < s->ac_low)
446+ goto fail;
441447
442448 for (;;) {
443449 if ((s->ac_high & 0x8000) != (s->ac_low & 0x8000)) {
444− if ((s->ac_low & 0x4000) == 0 || (s->ac_high & 0x4000) != 0)
450+ if (((s->ac_low & 0x4000) == 0) || ((s->ac_high & 0x4000) != 0))
445451 return 0;
446452 s->ac_value ^= 0x4000;
447453 s->ac_low &= 0x3fff;
448454 s->ac_high |= 0x4000;
449455 }
450456
451− s->ac_low = s->ac_low << 1;
452− s->ac_high = s->ac_high * 2 + 1;
453− if (s->ac_high < s->ac_low) {
454− av_log(avctx, AV_LOG_ERROR, "invalid state\n");
455− return AVERROR_INVALIDDATA;
456− }
457+ s->ac_low = s->ac_low * 2;
458+ s->ac_high = s->ac_high * 2 | 1;
459+ if (s->ac_high < s->ac_low)
460+ goto fail;
457461
458462 if (get_bits_left(gb) <= 0) {
459463 av_log(avctx, AV_LOG_ERROR, "overread in arithmetic coder\n");
460− return AVERROR_INVALIDDATA;
464+ goto fail;
461465 }
462466
463467 s->ac_value = s->ac_value * 2 + get_bits1(gb);
468+ if (s->ac_low > s->ac_value || s->ac_high < s->ac_value)
469+ goto fail;
464470 }
471+
472+fail:
473+ av_log(avctx, AV_LOG_ERROR, "invalid state\n");
474+ return AVERROR_INVALIDDATA;
465475 }
466476
467477 static void ac_init_model(WavArcContext *s)
@@ -484,11 +494,11 @@ static int ac_read_model(AVCodecContext *avctx, · 16 unmodified lines
484494 end = get_bits(gb, 8);
485495
486496 for (;;) {
487− while (start <= end)
497+ while (start <= end) {
498+ if (get_bits_left(gb) < 8)
499+ return AVERROR_INVALIDDATA;
488500 s->model[start++] = get_bits(gb, 8);
489−
490− if (get_bits_left(gb) < 8)
491− return AVERROR_INVALIDDATA;
501+ }
492502
493503 if (get_bits_left(gb) < 8)
494504 return AVERROR_INVALIDDATA;
@@ -568,9 +578,9 @@ static int decode_5elp(AVCodecContext *avctx, · 73 unmodified lines
568578 }
569579
570580 for (int n = 0; n < s->nb_samples; n++) {
571− unsigned prob = ac_get_prob(s);
572− prob = ac_map_symbol(s, prob);
573− ac_out[n] = prob - 0x80;
581+ uint16_t prob = ac_get_prob(s);
582+ int ac = ac_map_symbol(s, prob);
583+ ac_out[n] = ac - 0x80;
574584 if ((ret = ac_normalize(avctx, s, &ac_gb)) < 0)
575585 return ret;
576586 }
@@ -620,11 +630,16 @@ static int decode_5elp(AVCodecContext *avctx, · 43 unmodified lines
620630 case 20:
621631 case 7:
622632 for (int n = 0; n < s->nb_samples; n++)
623− samples[n + 70] = ac_out[n] + samples[n + 69] * 3 - samples[n + 68] * 3 + samples[n + 67];
633+ samples[n + 70] += ac_out[n] + samples[n + 69] * 3 - samples[n + 68] * 3 + samples[n + 67];
624634 finished = 1;
625635 break;
626636 case 19:
627637 case 6:
638+ for (int n = 0; n < 70; n++) {
639+ ac_pred[n] = samples[n];
640+ samples[n] = 0;
641+ }
642+
628643 for (int n = 0; n < s->nb_samples; n++) {
629644 int sum = 15;
630645
@@ -633,24 +648,31 @@ static int decode_5elp(AVCodecContext *avctx, · 2 unmodified lines
633648
634649 samples[n + 70] += ac_out[n] + (sum >> 4);
635650 }
651+
652+ for (int n = 0; n < 70; n++)
653+ samples[n] = ac_pred[n];
654+
655+ for (int n = 0; n < s->nb_samples; n++)
656+ samples[n + 70] += ac_out[n] + samples[n + 69] * 3 - samples[n + 68] * 3 + samples[n + 67];
657+
636658 finished = 1;
637659 break;
638660 case 18:
639661 case 5:
640662 for (int n = 0; n < s->nb_samples; n++)
641− samples[n + 70] = ac_out[n] + samples[n + 69] * 2 - samples[n + 68];
663+ samples[n + 70] += ac_out[n] + samples[n + 69] * 2 - samples[n + 68];
642664 finished = 1;
643665 break;
644666 case 17:
645667 case 4:
646668 for (int n = 0; n < s->nb_samples; n++)
647− samples[n + 70] = ac_out[n];
669+ samples[n + 70] += ac_out[n];
648670 finished = 1;
649671 break;
650672 case 16:
651673 case 3:
652674 for (int n = 0; n < s->nb_samples; n++)
653− samples[n + 70] = ac_out[n] + samples[n + 69];
675+ samples[n + 70] += ac_out[n] + samples[n + 69];
654676 finished = 1;
655677 break;
656678 case 15:

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