Revision 79350786 libavcodec/aacsbr.c

View differences:

libavcodec/aacsbr.c
617 617
                         GetBitContext *gb, SBRData *ch_data)
618 618
{
619 619
    int i;
620
    unsigned bs_pointer;
621
    int abs_bord_lead = 0;
622
    // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
623
    int abs_bord_trail = 16;
624
    int num_rel_lead, num_rel_trail;
625
    uint8_t bs_rel_bord[2][3];
620 626

  
621 627
    ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env[1]];
622 628
    ch_data->bs_num_env[0] = ch_data->bs_num_env[1];
......
625 631
    switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
626 632
    case FIXFIX:
627 633
        ch_data->bs_num_env[1] = 1 << get_bits(gb, 2);
634
        num_rel_lead           = ch_data->bs_num_env[1] - 1;
628 635
        if (ch_data->bs_num_env[1] == 1)
629 636
            ch_data->bs_amp_res = 0;
630 637

  
......
635 642
            return -1;
636 643
        }
637 644

  
638
        ch_data->bs_pointer = 0;
645
        bs_pointer = 0;
639 646

  
640 647
        ch_data->bs_freq_res[1] = get_bits1(gb);
641 648
        for (i = 1; i < ch_data->bs_num_env[1]; i++)
642 649
            ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
643 650
        break;
644 651
    case FIXVAR:
645
        ch_data->bs_var_bord[1] = get_bits(gb, 2);
646
        ch_data->bs_num_rel[1]  = get_bits(gb, 2);
647
        ch_data->bs_num_env[1]  = ch_data->bs_num_rel[1] + 1;
652
        abs_bord_trail         += get_bits(gb, 2);
653
        num_rel_trail           = get_bits(gb, 2);
654
        num_rel_lead            = 0;
655
        ch_data->bs_num_env[1]  = num_rel_trail + 1;
648 656

  
649
        for (i = 0; i < ch_data->bs_num_rel[1]; i++)
650
            ch_data->bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2;
657
        for (i = 0; i < num_rel_trail; i++)
658
            bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2;
651 659

  
652
        ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
660
        bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
653 661

  
654 662
        for (i = 0; i < ch_data->bs_num_env[1]; i++)
655 663
            ch_data->bs_freq_res[ch_data->bs_num_env[1] - i] = get_bits1(gb);
656 664
        break;
657 665
    case VARFIX:
658
        ch_data->bs_var_bord[0] = get_bits(gb, 2);
659
        ch_data->bs_num_rel[0]  = get_bits(gb, 2);
660
        ch_data->bs_num_env[1]  = ch_data->bs_num_rel[0] + 1;
666
        abs_bord_lead           = get_bits(gb, 2);
667
        num_rel_lead            = get_bits(gb, 2);
668
        ch_data->bs_num_env[1]  = num_rel_lead + 1;
661 669

  
662
        for (i = 0; i < ch_data->bs_num_rel[0]; i++)
663
            ch_data->bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2;
670
        for (i = 0; i < num_rel_lead; i++)
671
            bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2;
664 672

  
665
        ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
673
        bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
666 674

  
667 675
        get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]);
668 676
        break;
669 677
    case VARVAR:
670
        ch_data->bs_var_bord[0] = get_bits(gb, 2);
671
        ch_data->bs_var_bord[1] = get_bits(gb, 2);
672
        ch_data->bs_num_rel[0]  = get_bits(gb, 2);
673
        ch_data->bs_num_rel[1]  = get_bits(gb, 2);
674
        ch_data->bs_num_env[1]  = ch_data->bs_num_rel[0] + ch_data->bs_num_rel[1] + 1;
678
        abs_bord_lead           = get_bits(gb, 2);
679
        abs_bord_trail         += get_bits(gb, 2);
680
        num_rel_lead            = get_bits(gb, 2);
681
        num_rel_trail           = get_bits(gb, 2);
682
        ch_data->bs_num_env[1]  = num_rel_lead + num_rel_trail + 1;
675 683

  
676 684
        if (ch_data->bs_num_env[1] > 5) {
677 685
            av_log(ac->avccontext, AV_LOG_ERROR,
......
680 688
            return -1;
681 689
        }
682 690

  
683
        for (i = 0; i < ch_data->bs_num_rel[0]; i++)
684
            ch_data->bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2;
685
        for (i = 0; i < ch_data->bs_num_rel[1]; i++)
686
            ch_data->bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2;
691
        for (i = 0; i < num_rel_lead; i++)
692
            bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2;
693
        for (i = 0; i < num_rel_trail; i++)
694
            bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2;
687 695

  
688
        ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
696
        bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
689 697

  
690 698
        get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]);
691 699
        break;
692 700
    }
693 701

  
694
    if (ch_data->bs_pointer > ch_data->bs_num_env[1] + 1) {
702
    if (bs_pointer > ch_data->bs_num_env[1] + 1) {
695 703
        av_log(ac->avccontext, AV_LOG_ERROR,
696 704
               "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
697
               ch_data->bs_pointer);
698
        return -1;
699
    }
700

  
701
    int abs_bord_lead  =  ch_data->bs_frame_class >= 2 ? ch_data->bs_var_bord[0] : 0;
702
    // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
703
    int abs_bord_trail = (ch_data->bs_frame_class & 1 ? ch_data->bs_var_bord[1] : 0) + 16;
704
    int n_rel_lead;
705

  
706
    if (ch_data->bs_frame_class == FIXFIX) {
707
        n_rel_lead = ch_data->bs_num_env[1] - 1;
708
    } else if (ch_data->bs_frame_class == FIXVAR) {
709
        n_rel_lead = 0;
710
    } else if (ch_data->bs_frame_class < 4) { // VARFIX or VARVAR
711
        n_rel_lead = ch_data->bs_num_rel[0];
712
    } else {
713
        av_log(ac->avccontext, AV_LOG_ERROR,
714
               "Invalid bs_frame_class for SBR: %d\n", ch_data->bs_frame_class);
705
               bs_pointer);
715 706
        return -1;
716 707
    }
717 708

  
......
722 713
    if (ch_data->bs_frame_class == FIXFIX) {
723 714
        int temp = (abs_bord_trail + (ch_data->bs_num_env[1] >> 1)) /
724 715
                   ch_data->bs_num_env[1];
725
        for (i = 0; i < n_rel_lead; i++)
716
        for (i = 0; i < num_rel_lead; i++)
726 717
            ch_data->t_env[i + 1] = ch_data->t_env[i] + temp;
727 718
    } else if (ch_data->bs_frame_class > 1) { // VARFIX or VARVAR
728
        for (i = 0; i < n_rel_lead; i++)
729
            ch_data->t_env[i + 1] = ch_data->t_env[i] + ch_data->bs_rel_bord[0][i];
719
        for (i = 0; i < num_rel_lead; i++)
720
            ch_data->t_env[i + 1] = ch_data->t_env[i] + bs_rel_bord[0][i];
730 721
    } else { // FIXVAR
731
        for (i = 0; i < n_rel_lead; i++)
722
        for (i = 0; i < num_rel_lead; i++)
732 723
            ch_data->t_env[i + 1] = abs_bord_lead;
733 724
    }
734 725

  
735 726
    if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
736
        for (i = ch_data->bs_num_env[1] - 1; i > n_rel_lead; i--)
727
        for (i = ch_data->bs_num_env[1] - 1; i > num_rel_lead; i--)
737 728
            ch_data->t_env[i] = ch_data->t_env[i + 1] -
738
                                ch_data->bs_rel_bord[1][ch_data->bs_num_env[1] - 1 - i];
729
                                bs_rel_bord[1][ch_data->bs_num_env[1] - 1 - i];
739 730
    } else { // FIXFIX or VARFIX
740
        for (i = n_rel_lead; i < ch_data->bs_num_env[1]; i++)
731
        for (i = num_rel_lead; i < ch_data->bs_num_env[1]; i++)
741 732
            ch_data->t_env[i + 1] = abs_bord_trail;
742 733
    }
743 734

  
......
749 740
        if (ch_data->bs_frame_class == FIXFIX) {
750 741
            idx = ch_data->bs_num_env[1] >> 1;
751 742
        } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
752
            idx = ch_data->bs_num_env[1] - FFMAX(ch_data->bs_pointer - 1, 1);
743
            idx = ch_data->bs_num_env[1] - FFMAX(bs_pointer - 1, 1);
753 744
        } else { // VARFIX
754
            if (!ch_data->bs_pointer)
745
            if (!bs_pointer)
755 746
                idx = 1;
756
            else if (ch_data->bs_pointer == 1)
747
            else if (bs_pointer == 1)
757 748
                idx = ch_data->bs_num_env[1] - 1;
758 749
            else // bs_pointer > 1
759
                idx = ch_data->bs_pointer - 1;
750
                idx = bs_pointer - 1;
760 751
        }
761 752
        ch_data->t_q[1] = ch_data->t_env[idx];
762 753
        ch_data->t_q[2] = ch_data->t_env[ch_data->bs_num_env[1]];
......
765 756

  
766 757
    ch_data->e_a[0] = -(ch_data->e_a[1] != ch_data->bs_num_env[0]); // l_APrev
767 758
    ch_data->e_a[1] = -1;
768
    if ((ch_data->bs_frame_class & 1) && ch_data->bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
769
        ch_data->e_a[1] = ch_data->bs_num_env[1] + 1 - ch_data->bs_pointer;
770
    } else if ((ch_data->bs_frame_class == 2) && (ch_data->bs_pointer > 1)) // VARFIX and bs_pointer > 1
771
        ch_data->e_a[1] = ch_data->bs_pointer - 1;
759
    if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
760
        ch_data->e_a[1] = ch_data->bs_num_env[1] + 1 - bs_pointer;
761
    } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
762
        ch_data->e_a[1] = bs_pointer - 1;
772 763

  
773 764
    return 0;
774 765
}
......
783 774
    //These variables are read from the bitstream and therefore copied
784 775
    memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
785 776
    memcpy(dst->bs_num_env+1,  src->bs_num_env+1,  sizeof(dst->bs_num_env)- sizeof(*dst->bs_num_env));
786
    memcpy(dst->bs_var_bord,   src->bs_var_bord,   sizeof(dst->bs_var_bord));
787
    memcpy(dst->bs_rel_bord,   src->bs_rel_bord,   sizeof(dst->bs_rel_bord));
788
    memcpy(dst->bs_num_rel,    src->bs_num_rel,    sizeof(dst->bs_rel_bord));
789 777
    memcpy(dst->t_env,         src->t_env,         sizeof(dst->t_env));
790 778
    memcpy(dst->t_q,           src->t_q,           sizeof(dst->t_q));
791 779
    dst->bs_amp_res     = src->bs_amp_res;
792 780
    dst->bs_num_noise   = src->bs_num_noise;
793
    dst->bs_pointer     = src->bs_pointer;
794 781
    dst->bs_frame_class = src->bs_frame_class;
795 782
    dst->e_a[1]         = src->e_a[1];
796 783
}

Also available in: Unified diff