comparison aacsbr.c @ 11437:8b2d9e680000 libavcodec

aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context and some duplicate local variables.
author alexc
date Tue, 09 Mar 2010 10:27:35 +0000
parents d90f6c676063
children 121d700285e7
comparison
equal deleted inserted replaced
11436:d90f6c676063 11437:8b2d9e680000
615 615
616 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr, 616 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
617 GetBitContext *gb, SBRData *ch_data) 617 GetBitContext *gb, SBRData *ch_data)
618 { 618 {
619 int i; 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 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env[1]]; 627 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env[1]];
622 ch_data->bs_num_env[0] = ch_data->bs_num_env[1]; 628 ch_data->bs_num_env[0] = ch_data->bs_num_env[1];
623 ch_data->bs_amp_res = sbr->bs_amp_res_header; 629 ch_data->bs_amp_res = sbr->bs_amp_res_header;
624 630
625 switch (ch_data->bs_frame_class = get_bits(gb, 2)) { 631 switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
626 case FIXFIX: 632 case FIXFIX:
627 ch_data->bs_num_env[1] = 1 << get_bits(gb, 2); 633 ch_data->bs_num_env[1] = 1 << get_bits(gb, 2);
634 num_rel_lead = ch_data->bs_num_env[1] - 1;
628 if (ch_data->bs_num_env[1] == 1) 635 if (ch_data->bs_num_env[1] == 1)
629 ch_data->bs_amp_res = 0; 636 ch_data->bs_amp_res = 0;
630 637
631 if (ch_data->bs_num_env[1] > 4) { 638 if (ch_data->bs_num_env[1] > 4) {
632 av_log(ac->avccontext, AV_LOG_ERROR, 639 av_log(ac->avccontext, AV_LOG_ERROR,
633 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n", 640 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
634 ch_data->bs_num_env[1]); 641 ch_data->bs_num_env[1]);
635 return -1; 642 return -1;
636 } 643 }
637 644
638 ch_data->bs_pointer = 0; 645 bs_pointer = 0;
639 646
640 ch_data->bs_freq_res[1] = get_bits1(gb); 647 ch_data->bs_freq_res[1] = get_bits1(gb);
641 for (i = 1; i < ch_data->bs_num_env[1]; i++) 648 for (i = 1; i < ch_data->bs_num_env[1]; i++)
642 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1]; 649 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
643 break; 650 break;
644 case FIXVAR: 651 case FIXVAR:
645 ch_data->bs_var_bord[1] = get_bits(gb, 2); 652 abs_bord_trail += get_bits(gb, 2);
646 ch_data->bs_num_rel[1] = get_bits(gb, 2); 653 num_rel_trail = get_bits(gb, 2);
647 ch_data->bs_num_env[1] = ch_data->bs_num_rel[1] + 1; 654 num_rel_lead = 0;
648 655 ch_data->bs_num_env[1] = num_rel_trail + 1;
649 for (i = 0; i < ch_data->bs_num_rel[1]; i++) 656
650 ch_data->bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2; 657 for (i = 0; i < num_rel_trail; i++)
651 658 bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2;
652 ch_data->bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]); 659
660 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env[1]]);
653 661
654 for (i = 0; i < ch_data->bs_num_env[1]; i++) 662 for (i = 0; i < ch_data->bs_num_env[1]; i++)
655 ch_data->bs_freq_res[ch_data->bs_num_env[1] - i] = get_bits1(gb); 663 ch_data->bs_freq_res[ch_data->bs_num_env[1] - i] = get_bits1(gb);
656 break; 664 break;
657 case VARFIX: 665 case VARFIX:
658 ch_data->bs_var_bord[0] = get_bits(gb, 2); 666 abs_bord_lead = get_bits(gb, 2);
659 ch_data->bs_num_rel[0] = get_bits(gb, 2); 667 num_rel_lead = get_bits(gb, 2);
660 ch_data->bs_num_env[1] = ch_data->bs_num_rel[0] + 1; 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++) 670 for (i = 0; i < num_rel_lead; i++)
663 ch_data->bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2; 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 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]); 675 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]);
668 break; 676 break;
669 case VARVAR: 677 case VARVAR:
670 ch_data->bs_var_bord[0] = get_bits(gb, 2); 678 abs_bord_lead = get_bits(gb, 2);
671 ch_data->bs_var_bord[1] = get_bits(gb, 2); 679 abs_bord_trail += get_bits(gb, 2);
672 ch_data->bs_num_rel[0] = get_bits(gb, 2); 680 num_rel_lead = get_bits(gb, 2);
673 ch_data->bs_num_rel[1] = get_bits(gb, 2); 681 num_rel_trail = get_bits(gb, 2);
674 ch_data->bs_num_env[1] = ch_data->bs_num_rel[0] + ch_data->bs_num_rel[1] + 1; 682 ch_data->bs_num_env[1] = num_rel_lead + num_rel_trail + 1;
675 683
676 if (ch_data->bs_num_env[1] > 5) { 684 if (ch_data->bs_num_env[1] > 5) {
677 av_log(ac->avccontext, AV_LOG_ERROR, 685 av_log(ac->avccontext, AV_LOG_ERROR,
678 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n", 686 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
679 ch_data->bs_num_env[1]); 687 ch_data->bs_num_env[1]);
680 return -1; 688 return -1;
681 } 689 }
682 690
683 for (i = 0; i < ch_data->bs_num_rel[0]; i++) 691 for (i = 0; i < num_rel_lead; i++)
684 ch_data->bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2; 692 bs_rel_bord[0][i] = 2 * get_bits(gb, 2) + 2;
685 for (i = 0; i < ch_data->bs_num_rel[1]; i++) 693 for (i = 0; i < num_rel_trail; i++)
686 ch_data->bs_rel_bord[1][i] = 2 * get_bits(gb, 2) + 2; 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 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]); 698 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env[1]);
691 break; 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 av_log(ac->avccontext, AV_LOG_ERROR, 703 av_log(ac->avccontext, AV_LOG_ERROR,
696 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n", 704 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
697 ch_data->bs_pointer); 705 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);
715 return -1; 706 return -1;
716 } 707 }
717 708
718 ch_data->t_env_num_env_old = ch_data->t_env[ch_data->bs_num_env[0]]; 709 ch_data->t_env_num_env_old = ch_data->t_env[ch_data->bs_num_env[0]];
719 ch_data->t_env[0] = abs_bord_lead; 710 ch_data->t_env[0] = abs_bord_lead;
720 ch_data->t_env[ch_data->bs_num_env[1]] = abs_bord_trail; 711 ch_data->t_env[ch_data->bs_num_env[1]] = abs_bord_trail;
721 712
722 if (ch_data->bs_frame_class == FIXFIX) { 713 if (ch_data->bs_frame_class == FIXFIX) {
723 int temp = (abs_bord_trail + (ch_data->bs_num_env[1] >> 1)) / 714 int temp = (abs_bord_trail + (ch_data->bs_num_env[1] >> 1)) /
724 ch_data->bs_num_env[1]; 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 ch_data->t_env[i + 1] = ch_data->t_env[i] + temp; 717 ch_data->t_env[i + 1] = ch_data->t_env[i] + temp;
727 } else if (ch_data->bs_frame_class > 1) { // VARFIX or VARVAR 718 } else if (ch_data->bs_frame_class > 1) { // VARFIX or VARVAR
728 for (i = 0; i < n_rel_lead; i++) 719 for (i = 0; i < num_rel_lead; i++)
729 ch_data->t_env[i + 1] = ch_data->t_env[i] + ch_data->bs_rel_bord[0][i]; 720 ch_data->t_env[i + 1] = ch_data->t_env[i] + bs_rel_bord[0][i];
730 } else { // FIXVAR 721 } else { // FIXVAR
731 for (i = 0; i < n_rel_lead; i++) 722 for (i = 0; i < num_rel_lead; i++)
732 ch_data->t_env[i + 1] = abs_bord_lead; 723 ch_data->t_env[i + 1] = abs_bord_lead;
733 } 724 }
734 725
735 if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR 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 ch_data->t_env[i] = ch_data->t_env[i + 1] - 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 } else { // FIXFIX or VARFIX 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 ch_data->t_env[i + 1] = abs_bord_trail; 732 ch_data->t_env[i + 1] = abs_bord_trail;
742 } 733 }
743 734
744 ch_data->bs_num_noise = (ch_data->bs_num_env[1] > 1) + 1; 735 ch_data->bs_num_noise = (ch_data->bs_num_env[1] > 1) + 1;
745 736
747 if (ch_data->bs_num_noise > 1) { // typo in spec bases this on bs_num_env... 738 if (ch_data->bs_num_noise > 1) { // typo in spec bases this on bs_num_env...
748 unsigned int idx; 739 unsigned int idx;
749 if (ch_data->bs_frame_class == FIXFIX) { 740 if (ch_data->bs_frame_class == FIXFIX) {
750 idx = ch_data->bs_num_env[1] >> 1; 741 idx = ch_data->bs_num_env[1] >> 1;
751 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR 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 } else { // VARFIX 744 } else { // VARFIX
754 if (!ch_data->bs_pointer) 745 if (!bs_pointer)
755 idx = 1; 746 idx = 1;
756 else if (ch_data->bs_pointer == 1) 747 else if (bs_pointer == 1)
757 idx = ch_data->bs_num_env[1] - 1; 748 idx = ch_data->bs_num_env[1] - 1;
758 else // bs_pointer > 1 749 else // bs_pointer > 1
759 idx = ch_data->bs_pointer - 1; 750 idx = bs_pointer - 1;
760 } 751 }
761 ch_data->t_q[1] = ch_data->t_env[idx]; 752 ch_data->t_q[1] = ch_data->t_env[idx];
762 ch_data->t_q[2] = ch_data->t_env[ch_data->bs_num_env[1]]; 753 ch_data->t_q[2] = ch_data->t_env[ch_data->bs_num_env[1]];
763 } else 754 } else
764 ch_data->t_q[1] = ch_data->t_env[ch_data->bs_num_env[1]]; 755 ch_data->t_q[1] = ch_data->t_env[ch_data->bs_num_env[1]];
765 756
766 ch_data->e_a[0] = -(ch_data->e_a[1] != ch_data->bs_num_env[0]); // l_APrev 757 ch_data->e_a[0] = -(ch_data->e_a[1] != ch_data->bs_num_env[0]); // l_APrev
767 ch_data->e_a[1] = -1; 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 759 if ((ch_data->bs_frame_class & 1) && 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; 760 ch_data->e_a[1] = ch_data->bs_num_env[1] + 1 - bs_pointer;
770 } else if ((ch_data->bs_frame_class == 2) && (ch_data->bs_pointer > 1)) // VARFIX and bs_pointer > 1 761 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
771 ch_data->e_a[1] = ch_data->bs_pointer - 1; 762 ch_data->e_a[1] = bs_pointer - 1;
772 763
773 return 0; 764 return 0;
774 } 765 }
775 766
776 static void copy_sbr_grid(SBRData *dst, const SBRData *src) { 767 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
781 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env[0]); 772 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env[0]);
782 773
783 //These variables are read from the bitstream and therefore copied 774 //These variables are read from the bitstream and therefore copied
784 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res)); 775 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
785 memcpy(dst->bs_num_env+1, src->bs_num_env+1, sizeof(dst->bs_num_env)- sizeof(*dst->bs_num_env)); 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 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env)); 777 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
790 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q)); 778 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
791 dst->bs_amp_res = src->bs_amp_res; 779 dst->bs_amp_res = src->bs_amp_res;
792 dst->bs_num_noise = src->bs_num_noise; 780 dst->bs_num_noise = src->bs_num_noise;
793 dst->bs_pointer = src->bs_pointer;
794 dst->bs_frame_class = src->bs_frame_class; 781 dst->bs_frame_class = src->bs_frame_class;
795 dst->e_a[1] = src->e_a[1]; 782 dst->e_a[1] = src->e_a[1];
796 } 783 }
797 784
798 /// Read how the envelope and noise floor data is delta coded 785 /// Read how the envelope and noise floor data is delta coded