History

avcodec/wavarc: fix bugs in arithmetic coding mode

Paul B Mahol committed 3 years ago

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

Unified · Split

libavcodec/wavarc.c+48−26
@@ -412,13 +412,13 @@ static int ac_init(AVCodecContext *avctx, · 411 unmodified lines
412 return 0;412 return 0;
413}413}
414 414
415static unsigned ac_get_prob(WavArcContext *s)415static uint16_t ac_get_prob(WavArcContext *s)
416{416{
417 return ((s->freq_range - 1) + ((unsigned)s->ac_value - (unsigned)s->ac_low) *417 return ((s->freq_range - 1) + (s->ac_value - s->ac_low) * s->freq_range) /
418 (unsigned)s->freq_range) / (((unsigned)s->ac_high - (unsigned)s->ac_low) + 1) & 0xffffu;418 ((s->ac_high - s->ac_low) + 1U);
419}419}
420 420
421static unsigned ac_map_symbol(WavArcContext *s, unsigned prob)421static uint8_t ac_map_symbol(WavArcContext *s, uint16_t prob)
422{422{
423 int idx = 255;423 int idx = 255;
424 424
@@ -435,33 +435,43 @@ static int ac_normalize(AVCodecContext *avctx, WavArcContext *s, GetBitContext * · 10 unmodified lines
435{435{
436 int range;436 int range;
437 437
438 range = (unsigned)s->ac_high - (unsigned)s->ac_low + 1;438 if (s->ac_high < s->ac_low)
439 s->ac_high = (int16_t)((range * (unsigned)s->range_high) / (unsigned)s->freq_range) + -1 + s->ac_low;439 goto fail;
440 s->ac_low += (int16_t)((range * (unsigned)s->range_low) / (unsigned)s->freq_range);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;
441 447
442 for (;;) {448 for (;;) {
443 if ((s->ac_high & 0x8000) != (s->ac_low & 0x8000)) {449 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))
445 return 0;451 return 0;
446 s->ac_value ^= 0x4000;452 s->ac_value ^= 0x4000;
447 s->ac_low &= 0x3fff;453 s->ac_low &= 0x3fff;
448 s->ac_high |= 0x4000;454 s->ac_high |= 0x4000;
449 }455 }
450 456
451 s->ac_low = s->ac_low << 1;457 s->ac_low = s->ac_low * 2;
452 s->ac_high = s->ac_high * 2 + 1;458 s->ac_high = s->ac_high * 2 | 1;
453 if (s->ac_high < s->ac_low) {459 if (s->ac_high < s->ac_low)
454 av_log(avctx, AV_LOG_ERROR, "invalid state\n");460 goto fail;
455 return AVERROR_INVALIDDATA;
456 }
457 461
458 if (get_bits_left(gb) <= 0) {462 if (get_bits_left(gb) <= 0) {
459 av_log(avctx, AV_LOG_ERROR, "overread in arithmetic coder\n");463 av_log(avctx, AV_LOG_ERROR, "overread in arithmetic coder\n");
460 return AVERROR_INVALIDDATA;464 goto fail;
461 }465 }
462 466
463 s->ac_value = s->ac_value * 2 + get_bits1(gb);467 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;
464 }470 }
471
472fail:
473 av_log(avctx, AV_LOG_ERROR, "invalid state\n");
474 return AVERROR_INVALIDDATA;
465}475}
466 476
467static void ac_init_model(WavArcContext *s)477static void ac_init_model(WavArcContext *s)
@@ -484,11 +494,11 @@ static int ac_read_model(AVCodecContext *avctx, · 16 unmodified lines
484 end = get_bits(gb, 8);494 end = get_bits(gb, 8);
485 495
486 for (;;) {496 for (;;) {
487 while (start <= end)497 while (start <= end) {
498 if (get_bits_left(gb) < 8)
499 return AVERROR_INVALIDDATA;
488 s->model[start++] = get_bits(gb, 8);500 s->model[start++] = get_bits(gb, 8);
489 501 }
490 if (get_bits_left(gb) < 8)
491 return AVERROR_INVALIDDATA;
492 502
493 if (get_bits_left(gb) < 8)503 if (get_bits_left(gb) < 8)
494 return AVERROR_INVALIDDATA;504 return AVERROR_INVALIDDATA;
@@ -568,9 +578,9 @@ static int decode_5elp(AVCodecContext *avctx, · 73 unmodified lines
568 }578 }
569 579
570 for (int n = 0; n < s->nb_samples; n++) {580 for (int n = 0; n < s->nb_samples; n++) {
571 unsigned prob = ac_get_prob(s);581 uint16_t prob = ac_get_prob(s);
572 prob = ac_map_symbol(s, prob);582 int ac = ac_map_symbol(s, prob);
573 ac_out[n] = prob - 0x80;583 ac_out[n] = ac - 0x80;
574 if ((ret = ac_normalize(avctx, s, &ac_gb)) < 0)584 if ((ret = ac_normalize(avctx, s, &ac_gb)) < 0)
575 return ret;585 return ret;
576 }586 }
@@ -620,11 +630,16 @@ static int decode_5elp(AVCodecContext *avctx, · 43 unmodified lines
620 case 20:630 case 20:
621 case 7:631 case 7:
622 for (int n = 0; n < s->nb_samples; n++)632 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];
624 finished = 1;634 finished = 1;
625 break;635 break;
626 case 19:636 case 19:
627 case 6:637 case 6:
638 for (int n = 0; n < 70; n++) {
639 ac_pred[n] = samples[n];
640 samples[n] = 0;
641 }
642
628 for (int n = 0; n < s->nb_samples; n++) {643 for (int n = 0; n < s->nb_samples; n++) {
629 int sum = 15;644 int sum = 15;
630 645
@@ -633,24 +648,31 @@ static int decode_5elp(AVCodecContext *avctx, · 2 unmodified lines
633 648
634 samples[n + 70] += ac_out[n] + (sum >> 4);649 samples[n + 70] += ac_out[n] + (sum >> 4);
635 }650 }
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
636 finished = 1;658 finished = 1;
637 break;659 break;
638 case 18:660 case 18:
639 case 5:661 case 5:
640 for (int n = 0; n < s->nb_samples; n++)662 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];
642 finished = 1;664 finished = 1;
643 break;665 break;
644 case 17:666 case 17:
645 case 4:667 case 4:
646 for (int n = 0; n < s->nb_samples; n++)668 for (int n = 0; n < s->nb_samples; n++)
647 samples[n + 70] = ac_out[n];669 samples[n + 70] += ac_out[n];
648 finished = 1;670 finished = 1;
649 break;671 break;
650 case 16:672 case 16:
651 case 3:673 case 3:
652 for (int n = 0; n < s->nb_samples; n++)674 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];
654 finished = 1;676 finished = 1;
655 break;677 break;
656 case 15:678 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