Ticket #2686: aac-improvements-wip.patch
| File aac-improvements-wip.patch, 25.1 KB (added by , 13 years ago) |
|---|
-
libavcodec/aaccoder.c
diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c index e0285bb..5418ad1 100644
a b static const uint8_t *run_value_bits[2] = { 57 57 run_value_bits_long, run_value_bits_short 58 58 }; 59 59 60 61 60 /** 62 61 * Quantize one coefficient. 63 62 * @return absolute value of the quantized coefficient … … static float find_max_val(int group_len, int swb_size, const float *scaled) { 282 281 return maxval; 283 282 } 284 283 284 static float find_max_absval(int group_len, int swb_size, const float *scaled) { 285 float maxval = 0.0f; 286 int w2, i; 287 for (w2 = 0; w2 < group_len; w2++) { 288 for (i = 0; i < swb_size; i++) { 289 maxval = FFMAX(maxval, fabs(scaled[w2*128+i])); 290 } 291 } 292 return maxval; 293 } 294 285 295 static int find_min_book(float maxval, int sf) { 286 296 float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512]; 287 297 float Q34 = sqrtf(Q * sqrtf(Q)); … … static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, 701 711 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g]; 702 712 } 703 713 714 #define sclip(x) av_clip(x,60,218) 715 704 716 /** 705 717 * two-loop quantizers search taken from ISO 13818-7 Appendix C 706 718 */ 707 719 static void search_for_quantizers_twoloop(AVCodecContext *avctx, 708 720 AACEncContext *s, 709 721 SingleChannelElement *sce, 710 constfloat lambda)722 float lambda) 711 723 { 712 724 int start = 0, i, w, w2, g; 713 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f); 714 float dists[128] = { 0 }, uplims[128]; 725 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate 726 / ((avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels) 727 * (lambda / 120.f); 728 int toomanybits, toofewbits; 729 float dists[128] = { 0 }, uplims[128], energies[128]; 715 730 float maxvals[128]; 716 int fflag, minscaler; 731 732 /* rdlambda controls the maximum tolerated distortion. Twoloop 733 * will keep iterating until it fails to lower it or it reaches 734 * ulimit * rdlambda. Keeping it low increases quality on difficult 735 * signals, but lower it too much, and bits will be taken from weak 736 * signals, creating "holes". A balance is necesary. 737 */ 738 float rdlambda = av_clipf(2 * 120.f / lambda, 0.0625f, 16.0f); 739 float rdmax = (avctx->flags & CODEC_FLAG_QSCALE) ? 4.0f : 1.0f; 740 741 /* sfoffs controls an offset of optmium allocation that will be 742 * applied based on lambda. Keep it real and modest, the loop 743 * will take care of the rest, this just accelerates convergence 744 */ 745 float sfoffs = av_clipf((120.0f - lambda) * 0.1f, -5, 10); 746 747 int fflag, minscaler, nminscaler, minrdsf; 717 748 int its = 0; 718 749 int allz = 0; 719 float minthr = INFINITY, maxthr = 0.0f; 720 float iminthr, ilogmaxthr; 750 int tbits; 751 752 /* zeroscale controls a multiplier of the threshold, if band energy 753 * is below this, a zero is forced. Keep it lower than 1, unless 754 * low lambda is used, because energy < threshold doesn't mean there's 755 * no audible signal outright, it's just energy. Also make it rise 756 * slower than rdlambda, as rdscale has due compensation with 757 * noisy band depriorization below, whereas zeroing logic is rather dumb 758 */ 759 float zeroscale; 760 if (lambda > 120.f) 761 zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f); 762 else if (lambda < 80.f) 763 zeroscale = av_clipf(powf(80.f / lambda, 0.25f), 1.0f, 2.0f); 764 else 765 zeroscale = 1.f; 766 767 768 if (s->psy.bitres.alloc >= 0) { 769 // Psy granted us extra bits to use, from the reservoire 770 destbits = s->psy.bitres.alloc * (lambda / 120.f); 771 } 772 773 if (avctx->flags & CODEC_FLAG_QSCALE) { 774 // When using a constant Q-scale, be lenient on bit over/underflow 775 toomanybits = destbits * 2; 776 toofewbits = destbits / 2; 777 } else { 778 // When using ABR, be strict 779 toomanybits = destbits + destbits/16; 780 toofewbits = destbits - destbits/16; 781 } 721 782 722 783 // for values above this the decoder might end up in an endless loop 723 784 // due to always having more bits than what can be encoded. 724 785 destbits = FFMIN(destbits, 5800); 786 toomanybits = FFMIN(toomanybits, 5800); 787 toofewbits = FFMIN(toofewbits, 5800); 725 788 //XXX: some heuristic to determine initial quantizers will reduce search time 726 789 //determine zero bands and upper limits 727 790 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 728 791 for (g = 0; g < sce->ics.num_swb; g++) { 729 792 int nz = 0; 730 float uplim = 0.0f; 793 float uplim = INFINITY; 794 float energy = 0.0f; 731 795 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { 732 796 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; 733 uplim += band->threshold; 734 if (band->energy <= band->threshold || band->threshold == 0.0f) { 797 if (band->energy <= band->threshold * zeroscale || band->threshold == 0.0f) { 735 798 sce->zeroes[(w+w2)*16+g] = 1; 736 799 continue; 737 800 } 801 if (uplim > band->threshold) { 802 uplim = band->threshold; 803 energy = band->energy; 804 } 738 805 nz = 1; 739 806 } 740 uplims[w*16+g] = uplim *512; 807 if (!nz) 808 uplim = 0.0f; 809 uplims[w*16+g] = uplim; 810 energies[w*16+g] = energy; 741 811 sce->zeroes[w*16+g] = !nz; 742 if (nz) {743 minthr = FFMIN(minthr, uplim);744 maxthr = FFMAX(maxthr, uplim);745 }746 812 allz |= nz; 747 813 } 748 814 } 749 iminthr = 1.0f / minthr; 750 ilogmaxthr = 12.0f / log2f(FFMAX(2.0f,maxthr*512*iminthr)); 815 816 /* Compute initial scalers */ 817 minscaler = 65535; 751 818 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 752 819 for (g = 0; g < sce->ics.num_swb; g++) { 753 820 if (sce->zeroes[w*16+g]) { 754 821 sce->sf_idx[w*16+g] = SCALE_ONE_POS; 755 822 continue; 756 823 } 757 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]*iminthr)*ilogmaxthr+59-12-1,59); 824 /* log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2). 825 * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion, 826 * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus 827 * more robust. 828 */ 829 sce->sf_idx[w*16+g] = SCALE_ONE_POS + 1.75*log2f(FFMAX(0.125f,uplims[w*16+g])) + sfoffs; 830 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); 758 831 } 759 832 } 760 833 834 /* Clip */ 835 minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512); 836 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 837 for (g = 0; g < sce->ics.num_swb; g++) 838 if (!sce->zeroes[w*16+g]) 839 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1); 840 761 841 if (!allz) 762 842 return; 763 843 abs_pow34_v(s->scoefs, sce->coeffs, 1024); … … static void search_for_quantizers_twoloop(AVCodecContext *avctx, 771 851 } 772 852 } 773 853 854 /* Scale uplims to match rate distortion to quality 855 * and apply noisy band depriorization. 856 * Maxval-energy ratio gives us an idea of how noisy the band is. 857 * If maxval^2 ~ energy, then that band is mostly noise, and we can relax 858 * rate distortion requirements. 859 */ 860 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 861 start = w*128; 862 for (g = 0; g < sce->ics.num_swb; g++) { 863 float max = find_max_absval(sce->ics.group_len[w], sce->ics.swb_sizes[g], sce->coeffs + start); 864 if (max > 0) { 865 float energy2uplim = energies[w*16+g] / (max*max*sce->ics.swb_sizes[g]); 866 energy2uplim = FFMAX(0.0625f, FFMIN(1.0f,energy2uplim)); 867 uplims[w*16+g] *= av_clipf(rdlambda * rdlambda * energy2uplim, 0.0625f, rdmax); 868 start += sce->ics.swb_sizes[g]; 869 } 870 } 871 } 872 774 873 //perform two-loop search 775 874 //outer loop - improve quality 776 875 do { 777 int tbits, qstep; 778 minscaler = sce->sf_idx[0]; 876 int qstep; 779 877 //inner loop - quantize spectrum to fit into given number of bits 780 878 qstep = its ? 1 : 32; 781 879 do { … … static void search_for_quantizers_twoloop(AVCodecContext *avctx, 795 893 start += sce->ics.swb_sizes[g]; 796 894 continue; 797 895 } 798 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);799 896 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); 800 897 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { 801 898 int b; … … static void search_for_quantizers_twoloop(AVCodecContext *avctx, 818 915 prev = sce->sf_idx[w*16+g]; 819 916 } 820 917 } 821 if (tbits > destbits) {822 for (i = 0; i < 128; i++) 823 if (sce->sf_idx[i] < 218 - qstep)824 sce->sf_idx[i] += qstep;825 } else if (tbits < destbits) {826 for (i = 0; i < 128; i++) 827 if (sce->sf_idx[i] > 60 + qstep)828 sce->sf_idx[i] -= qstep;918 if (tbits > toomanybits) { 919 for (i = 0; i < 128; i++) 920 if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) 921 sce->sf_idx[i] = FFMIN(SCALE_MAX_POS, sce->sf_idx[i] + qstep); 922 } else if (tbits < toofewbits) { 923 for (i = 0; i < 128; i++) 924 if (sce->sf_idx[i] > SCALE_ONE_POS) 925 sce->sf_idx[i] = FFMAX(SCALE_ONE_POS, sce->sf_idx[i] - qstep); 829 926 } 830 927 qstep >>= 1; 831 if (!qstep && tbits > destbits*1.02&& sce->sf_idx[0] < 217)928 if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217) 832 929 qstep = 1; 833 930 } while (qstep); 834 931 932 minscaler = SCALE_MAX_POS; 933 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 934 for (g = 0; g < sce->ics.num_swb; g++) 935 if (!sce->zeroes[w*16+g]) 936 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); 937 835 938 fflag = 0; 836 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF); 939 minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512); 940 minrdsf = (avctx->flags & CODEC_FLAG_QSCALE) ? 60 : minscaler; 837 941 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 838 942 for (g = 0; g < sce->ics.num_swb; g++) { 839 943 int prevsc = sce->sf_idx[w*16+g]; 840 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {944 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > minrdsf) { 841 945 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1)) 842 946 sce->sf_idx[w*16+g]--; 843 947 else //Try to make sure there is some energy in every band 844 948 sce->sf_idx[w*16+g]-=2; 845 949 } 846 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], min scaler, minscaler + SCALE_MAX_DIFF);847 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);950 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minrdsf, minscaler + SCALE_MAX_DIFF); 951 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], SCALE_MAX_POS - SCALE_DIV_512); 848 952 if (sce->sf_idx[w*16+g] != prevsc) 849 953 fflag = 1; 954 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]); 850 955 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); 851 956 } 852 957 } 958 if (nminscaler < minscaler) { 959 // Drecreased some scalers below minscaler. Must re-clamp. 960 minscaler = nminscaler; 961 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 962 for (g = 0; g < sce->ics.num_swb; g++) 963 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF); 964 } 853 965 its++; 854 } while (fflag && its < 10); 966 } while (fflag && its < 20); 967 968 /* Fill implicit zeroes */ 969 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 970 for (g = 0; g < sce->ics.num_swb; g++) 971 if (sce->band_type[w*16+g] == 0) 972 sce->zeroes[w*16+g] = 1; 855 973 } 856 974 857 975 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, … … static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, 1028 1146 { 1029 1147 int w, w2, g; 1030 1148 float lowlambda = av_clipf(120.f / lambda, 0.85f, 1.f); 1031 float rlambda = av_clipf(120.f / lambda, 0.75f, 2.f);1149 float rlambda = av_clipf(120.f / lambda, 0.75f, 10.f); 1032 1150 const int minq = av_clip(2 * log2f(120.f / lambda) + 150, 100, 218 - SCALE_MAX_DIFF); 1033 1151 const int maxq = minq + SCALE_MAX_DIFF - 1; 1034 1152 … … static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, 1041 1159 sce->sf_idx[(w+w2)*16+g] = maxq; 1042 1160 sce->zeroes[(w+w2)*16+g] = 1; 1043 1161 } else { 1044 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold * rlambda), minq, maxq);1162 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + 1.414*log2f(band->threshold * rlambda), minq, maxq); 1045 1163 sce->zeroes[(w+w2)*16+g] = 0; 1046 1164 } 1047 1165 } 1048 1166 } 1049 1167 } 1050 1168 //set the same quantizers inside window groups 1051 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 1052 for (g = 0; g < sce->ics.num_swb; g++) 1053 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) 1054 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g]; 1169 if (sce->ics.num_windows > 1) { 1170 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 1171 for (g = 0; g < sce->ics.num_swb; g++) { 1172 if (sce->ics.group_len[w] > 1) { 1173 int avg_sf_idx = 0; 1174 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) 1175 avg_sf_idx += sce->sf_idx[w*16+g]; 1176 avg_sf_idx /= sce->ics.group_len[w]; 1177 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) 1178 sce->sf_idx[(w+w2)*16+g] = avg_sf_idx; 1179 } 1180 } 1181 } 1182 } 1055 1183 } 1056 1184 1057 1185 static void search_for_ms(AACEncContext *s, ChannelElement *cpe, … … static void search_for_ms(AACEncContext *s, ChannelElement *cpe, 1065 1193 if (!cpe->common_window) 1066 1194 return; 1067 1195 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) { 1196 int min_sf_idx_mid[8]; 1197 int min_sf_idx_side[8]; 1198 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { 1199 min_sf_idx_mid[w2] = SCALE_MAX_POS; 1200 min_sf_idx_side[w2] = SCALE_MAX_POS; 1201 for (g = 0; g < sce0->ics.num_swb; g++) { 1202 if (!sce0->zeroes[w*16+g]) 1203 min_sf_idx_mid[w2] = FFMIN(min_sf_idx_mid[w2], sce0->sf_idx[(w+w2)*16+g]); 1204 if (!sce1->zeroes[w*16+g]) 1205 min_sf_idx_side[w2] = FFMIN(min_sf_idx_side[w2], sce1->sf_idx[(w+w2)*16+g]); 1206 } 1207 } 1208 1068 1209 for (g = 0; g < sce0->ics.num_swb; g++) { 1069 1210 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) { 1070 1211 float dist1 = 0.0f, dist2 = 0.0f; 1212 int B0 = 0, B1 = 0; 1213 int wmidcb[8], wsidcb[8]; 1071 1214 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { 1072 1215 FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; 1073 1216 FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; 1074 1217 float minthr = FFMIN(band0->threshold, band1->threshold); 1075 float avgthr = 0.5f*(band0->threshold + band1->threshold); 1218 int minidx = FFMIN(sce0->sf_idx[(w+w2)*16+g], sce1->sf_idx[(w+w2)*16+g]); 1219 int mididx = av_clip(minidx, min_sf_idx_mid[w2], min_sf_idx_mid[w2] + SCALE_MAX_DIFF); 1220 int sididx = av_clip(minidx-1, min_sf_idx_side[w2], min_sf_idx_side[w2] + SCALE_MAX_DIFF); 1221 float Mmax = 0.0f, Smax = 0.0f; 1222 int midcb, sidcb; 1223 int b1,b2,b3,b4; 1076 1224 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { 1077 1225 M[i] = (sce0->coeffs[start+w2*128+i] 1078 1226 + sce1->coeffs[start+w2*128+i]) * 0.5; … … static void search_for_ms(AACEncContext *s, ChannelElement *cpe, 1083 1231 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]); 1084 1232 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]); 1085 1233 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]); 1234 for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) { 1235 Mmax = FFMAX(Mmax, M34[i]); 1236 Smax = FFMAX(Smax, S34[i]); 1237 } 1238 wmidcb[w2] = midcb = find_min_book(Mmax, mididx); 1239 wsidcb[w2] = sidcb = find_min_book(Smax, sididx); 1086 1240 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128, 1087 1241 L34, 1088 1242 sce0->ics.swb_sizes[g], 1089 1243 sce0->sf_idx[(w+w2)*16+g], 1090 1244 sce0->band_type[(w+w2)*16+g], 1091 lambda / band0->threshold, INFINITY, NULL);1245 lambda / band0->threshold, INFINITY, &b1); 1092 1246 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128, 1093 1247 R34, 1094 1248 sce1->ics.swb_sizes[g], 1095 1249 sce1->sf_idx[(w+w2)*16+g], 1096 1250 sce1->band_type[(w+w2)*16+g], 1097 lambda / band1->threshold, INFINITY, NULL);1251 lambda / band1->threshold, INFINITY, &b2); 1098 1252 dist2 += quantize_band_cost(s, M, 1099 1253 M34, 1100 1254 sce0->ics.swb_sizes[g], 1101 sce0->sf_idx[(w+w2)*16+g],1102 sce0->band_type[(w+w2)*16+g],1103 lambda / avgthr, INFINITY, NULL);1255 mididx, 1256 midcb, 1257 lambda / minthr, INFINITY, &b3); 1104 1258 dist2 += quantize_band_cost(s, S, 1105 1259 S34, 1106 1260 sce1->ics.swb_sizes[g], 1107 sce1->sf_idx[(w+w2)*16+g], 1108 sce1->band_type[(w+w2)*16+g], 1109 lambda / minthr, INFINITY, NULL); 1261 sididx, 1262 sidcb, 1263 lambda / minthr, INFINITY, &b4); 1264 B0 += b1+b2; 1265 B1 += b3+b4; 1266 dist1 -= B0; 1267 dist2 -= B1; 1268 } 1269 cpe->ms_mask[w*16+g] = dist2 < dist1 && B1 < B0; 1270 if (cpe->ms_mask[w*16+g]) { 1271 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { 1272 int minidx = FFMIN(sce0->sf_idx[(w+w2)*16+g], sce1->sf_idx[(w+w2)*16+g]); 1273 int mididx = av_clip(minidx, min_sf_idx_mid[w2], min_sf_idx_mid[w2] + SCALE_MAX_DIFF); 1274 int sididx = av_clip(minidx-1, min_sf_idx_side[w2], min_sf_idx_side[w2] + SCALE_MAX_DIFF); 1275 sce0->sf_idx[(w+w2)*16+g] = mididx; 1276 sce0->band_type[(w+w2)*16+g] = wmidcb[w2]; 1277 sce1->sf_idx[(w+w2)*16+g] = sididx; 1278 sce1->band_type[(w+w2)*16+g] = wsidcb[w2]; 1279 } 1110 1280 } 1111 cpe->ms_mask[w*16+g] = dist2 < dist1;1112 1281 } 1113 1282 start += sce0->ics.swb_sizes[g]; 1114 1283 } -
libavcodec/aacenc.c
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c index aa93c90..8f89536 100644
a b static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 591 591 put_bits(&s->pb, 4, chan_el_counter[tag]++); 592 592 for (ch = 0; ch < chans; ch++) 593 593 coeffs[ch] = cpe->ch[ch].coeffs; 594 s->psy.bitres.alloc = -1; 594 595 s->psy.model->analyze(&s->psy, start_ch, coeffs, wi); 595 596 for (ch = 0; ch < chans; ch++) { 596 s->cur_channel = start_ch * 2+ ch;597 s->cur_channel = start_ch + ch; 597 598 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda); 598 599 } 599 600 cpe->common_window = 0; … … static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 609 610 } 610 611 } 611 612 } 612 s->cur_channel = start_ch * 2;613 s->cur_channel = start_ch; 613 614 if (s->options.stereo_mode && cpe->common_window) { 614 615 if (s->options.stereo_mode > 0) { 615 616 IndividualChannelStream *ics = &cpe->ch[0].ics; … … static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 621 622 } 622 623 } 623 624 adjust_frame_information(cpe, chans); 624 if (cpe->ms_mode) {625 /* Re-evaluate psy model and quantization selection based on626 MS-transformed channels */627 s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);628 for (ch = 0; ch < chans; ch++) {629 s->cur_channel = start_ch * 2 + ch;630 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);631 }632 }633 625 if (chans == 2) { 634 626 put_bits(&s->pb, 1, cpe->common_window); 635 627 if (cpe->common_window) { -
libavcodec/aacpsy.c
diff --git a/libavcodec/aacpsy.c b/libavcodec/aacpsy.c index 51c669a..98af667 100644
a b static av_cold int psy_3gpp_init(FFPsyContext *ctx) { 297 297 float bark; 298 298 int i, j, g, start; 299 299 float prev, minscale, minath, minsnr, pe_min; 300 const int chan_bitrate = ctx->avctx->bit_rate / ctx->avctx->channels;300 const int chan_bitrate = ctx->avctx->bit_rate / ((ctx->avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : ctx->avctx->channels); 301 301 const int bandwidth = ctx->avctx->cutoff ? ctx->avctx->cutoff : AAC_CUTOFF(ctx->avctx); 302 302 const float num_bark = calc_bark((float)bandwidth); 303 303 … … static av_unused FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx, 389 389 int channel, int prev_type) 390 390 { 391 391 int i, j; 392 int br = ctx->avctx->bit_rate / ctx->avctx->channels;392 int br = ((AacPsyContext*)ctx->model_priv_data)->chan_bitrate; 393 393 int attack_ratio = br <= 16000 ? 18 : 10; 394 394 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data; 395 395 AacPsyChannel *pch = &pctx->ch[channel]; … … static void psy_3gpp_analyze_channel(FFPsyContext *ctx, int channel, 679 679 desired_pe *= av_clipf(pctx->pe.previous / PSY_3GPP_BITS_TO_PE(ctx->bitres.bits), 680 680 0.85f, 1.15f); 681 681 pctx->pe.previous = PSY_3GPP_BITS_TO_PE(desired_bits); 682 ctx->bitres.alloc = desired_bits; 682 683 683 684 if (desired_pe < pe) { 684 685 /* 5.6.1.3.4 "First Estimation of the reduction value" */ -
libavcodec/psymodel.h
diff --git a/libavcodec/psymodel.h b/libavcodec/psymodel.h index d1a126a..c446ceb 100644
a b typedef struct FFPsyContext { 88 88 struct { 89 89 int size; ///< size of the bitresevoir in bits 90 90 int bits; ///< number of bits used in the bitresevoir 91 int alloc; ///< number of bits allocated by the psy, or -1 if no allocation was done 91 92 } bitres; 92 93 93 94 void* model_priv_data; ///< psychoacoustic model implementation private data
