Ticket #2686: aac-improvements-wip-v3-vbr.patch
| File aac-improvements-wip-v3-vbr.patch, 35.9 KB (added by , 13 years ago) |
|---|
-
libavcodec/aaccoder.c
diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c index 45fbc2d..ba2c2b1 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 const uint8_t *run_value_bits[2] = { 66 65 static av_always_inline int quant(float coef, const float Q) 67 66 { 68 67 float a = coef * Q; 69 return sqrtf(a * sqrtf(a)) + 0.4054 ;68 return sqrtf(a * sqrtf(a)) + 0.4054f; 70 69 } 71 70 72 71 static void quantize_bands(int *out, const float *in, const float *scaled, … … static void quantize_bands(int *out, const float *in, const float *scaled, 76 75 double qc; 77 76 for (i = 0; i < size; i++) { 78 77 qc = scaled[i] * Q34; 79 out[i] = (int)FFMIN(qc + 0.4054 , (double)maxval);78 out[i] = (int)FFMIN(qc + 0.4054f, (double)maxval); 80 79 if (is_signed && in[i] < 0.0f) { 81 80 out[i] = -out[i]; 82 81 } … … 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 refbits = destbits; 729 int toomanybits, toofewbits; 730 float dists[128] = { 0 }, uplims[128], energies[128]; 715 731 float maxvals[128]; 716 int fflag, minscaler; 732 733 /* rdlambda controls the maximum tolerated distortion. Twoloop 734 * will keep iterating until it fails to lower it or it reaches 735 * ulimit * rdlambda. Keeping it low increases quality on difficult 736 * signals, but lower it too much, and bits will be taken from weak 737 * signals, creating "holes". A balance is necesary. 738 * rdmax and rdmin specify the relative deviation from rdlambda 739 * allowed for tonality compensation 740 */ 741 float rdlambda = av_clipf(2 * 120.f / lambda, 0.0625f, 16.0f); 742 float rdmin = 0.0625f; 743 float rdmax = 1.0f; 744 745 /* sfoffs controls an offset of optmium allocation that will be 746 * applied based on lambda. Keep it real and modest, the loop 747 * will take care of the rest, this just accelerates convergence 748 */ 749 float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10); 750 751 int fflag, minscaler, nminscaler, minrdsf; 717 752 int its = 0; 753 int maxits = 20; 718 754 int allz = 0; 719 float minthr = INFINITY; 755 int tbits; 756 int cutoff = avctx->sample_rate/2; 757 758 /* zeroscale controls a multiplier of the threshold, if band energy 759 * is below this, a zero is forced. Keep it lower than 1, unless 760 * low lambda is used, because energy < threshold doesn't mean there's 761 * no audible signal outright, it's just energy. Also make it rise 762 * slower than rdlambda, as rdscale has due compensation with 763 * noisy band depriorization below, whereas zeroing logic is rather dumb 764 */ 765 float zeroscale; 766 if (lambda > 120.f) 767 zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f); 768 else 769 zeroscale = 1.f; 770 771 if (s->psy.bitres.alloc >= 0) { 772 // Psy granted us extra bits to use, from the reservoire 773 // adjust for lambda except what psy already did 774 destbits = s->psy.bitres.alloc 775 * (lambda / (avctx->global_quality ? avctx->global_quality : 120)); 776 } 777 778 if (avctx->flags & CODEC_FLAG_QSCALE) { 779 // When using a constant Q-scale, be lenient on bit under/overflow 780 toomanybits = FFMAX3(refbits * 2, destbits * 8, 1280); 781 toofewbits = destbits / 2; 782 783 // search further 784 maxits = 40; 785 786 // and zero out above cutoff frequency 787 { 788 int i, w; 789 int frame_bit_rate = (destbits * 1.4f * avctx->sample_rate / 1024) * (lambda / 40.0f); 790 791 /* For high lambdas, don't cut off */ 792 int bandwidth = FFMAX(3000, _AAC_CUTOFF(frame_bit_rate, 1, avctx->sample_rate)); 793 int wlen = 1024 / sce->ics.num_windows; 794 cutoff = bandwidth * 2 * wlen / avctx->sample_rate; 795 } 796 797 } else { 798 // When using ABR, be strict 799 toomanybits = destbits + destbits/16; 800 toofewbits = destbits - destbits/16; 801 } 720 802 721 803 // for values above this the decoder might end up in an endless loop 722 804 // due to always having more bits than what can be encoded. 723 805 destbits = FFMIN(destbits, 5800); 806 toomanybits = FFMIN(toomanybits, 5800); 807 toofewbits = FFMIN(toofewbits, 5800); 724 808 //XXX: some heuristic to determine initial quantizers will reduce search time 725 809 //determine zero bands and upper limits 726 810 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 727 for (g = 0; g < sce->ics.num_swb; g++) {811 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) { 728 812 int nz = 0; 729 float uplim = 0.0f; 813 float uplim = INFINITY; 814 float energy = 0.0f; 815 float lpfactor = FFMAX(1.0f, (float)start / cutoff); 816 730 817 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { 731 818 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; 732 uplim += band->threshold;733 if (band->energy <= band->threshold|| band->threshold == 0.0f) {819 float th = band->threshold * lpfactor; 820 if (band->energy <= th * zeroscale || band->threshold == 0.0f) { 734 821 sce->zeroes[(w+w2)*16+g] = 1; 735 822 continue; 736 823 } 737 824 nz = 1; 738 825 } 739 uplims[w*16+g] = uplim *512; 826 if (!nz) { 827 uplim = 0.0f; 828 } else { 829 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { 830 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; 831 float th = band->threshold * lpfactor; 832 if (band->energy <= band->threshold * zeroscale || band->threshold == 0.0f) 833 continue; 834 if (uplim > th) { 835 uplim = th; 836 energy = band->energy; 837 } 838 } 839 } 840 uplims[w*16+g] = uplim; 841 energies[w*16+g] = energy; 740 842 sce->zeroes[w*16+g] = !nz; 741 if (nz)742 minthr = FFMIN(minthr, uplim);743 843 allz |= nz; 744 844 } 745 845 } 846 847 /* Compute initial scalers */ 848 minscaler = 65535; 746 849 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 747 850 for (g = 0; g < sce->ics.num_swb; g++) { 748 851 if (sce->zeroes[w*16+g]) { 749 852 sce->sf_idx[w*16+g] = SCALE_ONE_POS; 750 853 continue; 751 854 } 752 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59); 855 /* log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2). 856 * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion, 857 * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus 858 * more robust. 859 */ 860 sce->sf_idx[w*16+g] = av_clip( 861 SCALE_ONE_POS 862 + 1.75*log2f(FFMAX(0.00125f,uplims[w*16+g]) / sce->ics.swb_sizes[g]) 863 + sfoffs, 864 60, SCALE_MAX_POS); 865 //fprintf(stderr, "%02x ", sce->sf_idx[w*16+g]); 866 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); 753 867 } 868 //fprintf(stderr, "|\n"); 754 869 } 755 870 //fprintf(stderr, "\n"); 871 872 /* Clip */ 873 minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512); 874 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 875 for (g = 0; g < sce->ics.num_swb; g++) 876 if (!sce->zeroes[w*16+g]) 877 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1); 878 756 879 if (!allz) 757 880 return; 758 881 abs_pow34_v(s->scoefs, sce->coeffs, 1024); … … static void search_for_quantizers_twoloop(AVCodecContext *avctx, 766 889 } 767 890 } 768 891 892 /* Scale uplims to match rate distortion to quality 893 * and apply noisy band depriorization and tonal band priorization. 894 * Maxval-energy ratio gives us an idea of how noisy/tonal the band is. 895 * If maxval^2 ~ energy, then that band is mostly noise, and we can relax 896 * rate distortion requirements. 897 */ 898 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 899 start = w*128; 900 for (g = 0; g < sce->ics.num_swb; g++) { 901 float max = find_max_absval(sce->ics.group_len[w], sce->ics.swb_sizes[g], sce->coeffs + start); 902 if (max > 0) { 903 float energy2uplim = energies[w*16+g] / (max*max*sce->ics.swb_sizes[g]); 904 energy2uplim = FFMAX(0.0625f, FFMIN(1.0f,energy2uplim)); 905 uplims[w*16+g] *= av_clipf(rdlambda * rdlambda * energy2uplim, rdmin, rdmax); 906 start += sce->ics.swb_sizes[g]; 907 } 908 } 909 } 910 769 911 //perform two-loop search 770 912 //outer loop - improve quality 771 913 do { 772 int tbits, qstep; 773 minscaler = sce->sf_idx[0]; 914 int qstep; 774 915 //inner loop - quantize spectrum to fit into given number of bits 775 916 qstep = its ? 1 : 32; 776 917 do { … … static void search_for_quantizers_twoloop(AVCodecContext *avctx, 790 931 start += sce->ics.swb_sizes[g]; 791 932 continue; 792 933 } 793 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);794 934 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); 795 935 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { 796 936 int b; … … static void search_for_quantizers_twoloop(AVCodecContext *avctx, 813 953 prev = sce->sf_idx[w*16+g]; 814 954 } 815 955 } 816 if (tbits > destbits) {956 if (tbits > toomanybits) { 817 957 for (i = 0; i < 128; i++) 818 if (sce->sf_idx[i] < 218 - qstep)819 sce->sf_idx[i] += qstep;820 } else {958 if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) 959 sce->sf_idx[i] = FFMIN(SCALE_MAX_POS, sce->sf_idx[i] + qstep); 960 } else if (tbits < toofewbits) { 821 961 for (i = 0; i < 128; i++) 822 if (sce->sf_idx[i] > 60 - qstep)823 sce->sf_idx[i] -= qstep;962 if (sce->sf_idx[i] > SCALE_ONE_POS) 963 sce->sf_idx[i] = FFMAX(SCALE_ONE_POS, sce->sf_idx[i] - qstep); 824 964 } 825 965 qstep >>= 1; 826 if (!qstep && tbits > destbits*1.02&& sce->sf_idx[0] < 217)966 if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217) 827 967 qstep = 1; 828 968 } while (qstep); 829 969 970 minscaler = SCALE_MAX_POS; 971 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 972 for (g = 0; g < sce->ics.num_swb; g++) 973 if (!sce->zeroes[w*16+g]) 974 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); 975 830 976 fflag = 0; 831 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF); 977 minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512); 978 minrdsf = (avctx->flags & CODEC_FLAG_QSCALE) ? 60 : minscaler; 832 979 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 833 980 for (g = 0; g < sce->ics.num_swb; g++) { 834 981 int prevsc = sce->sf_idx[w*16+g]; 835 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) { 982 //float hfprio = 1.0 + its * av_clipf(120.0f / lambda - 1.0f, 0.0f, 8.0f) * g / (sce->ics.num_swb * maxits); 983 if (dists[w*16+g] > (uplims[w*16+g]/* * hfprio*/) && sce->sf_idx[w*16+g] > minrdsf) { 836 984 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1)) 837 985 sce->sf_idx[w*16+g]--; 838 986 else //Try to make sure there is some energy in every band 839 987 sce->sf_idx[w*16+g]-=2; 840 988 } 841 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], min scaler, minscaler + SCALE_MAX_DIFF);842 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);989 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minrdsf, minscaler + SCALE_MAX_DIFF); 990 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], SCALE_MAX_POS - SCALE_DIV_512); 843 991 if (sce->sf_idx[w*16+g] != prevsc) 844 992 fflag = 1; 993 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]); 845 994 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); 846 995 } 847 996 } 997 if (nminscaler < minscaler) { 998 // Drecreased some scalers below minscaler. Must re-clamp. 999 minscaler = nminscaler; 1000 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 1001 for (g = 0; g < sce->ics.num_swb; g++) 1002 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF); 1003 } 848 1004 its++; 849 } while (fflag && its < 10); 1005 } while (fflag && its < maxits); 1006 1007 /* Fill implicit zeroes */ 1008 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 1009 /* No lowly codebooks beyond cutoff zone, to clean up noisy coefs */ 1010 int scutoff = cutoff + cutoff/5; 1011 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) { 1012 int minbt = (start < scutoff) ? 0 : 7; 1013 if (sce->band_type[w*16+g] <= minbt) { 1014 sce->zeroes[w*16+g] = 1; 1015 sce->band_type[w*16+g] = 0; 1016 } 1017 } 1018 } 1019 //fprintf(stderr, "ba:%d br:%d \t\r", s->psy.bitres.alloc, tbits); 850 1020 } 851 1021 852 1022 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, … … static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, 1021 1191 SingleChannelElement *sce, 1022 1192 const float lambda) 1023 1193 { 1024 int i, w, w2, g; 1025 int minq = 255; 1194 int w, w2, g; 1195 float lowlambda = av_clipf(120.f / lambda, 0.85f, 1.f); 1196 float rlambda = av_clipf(120.f / lambda, 0.75f, 10.f); 1197 const int minq = av_clip(2 * log2f(120.f / lambda) + 150, 100, 218 - SCALE_MAX_DIFF); 1198 const int maxq = minq + SCALE_MAX_DIFF - 1; 1026 1199 1027 1200 memset(sce->sf_idx, 0, sizeof(sce->sf_idx)); 1028 1201 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 1029 1202 for (g = 0; g < sce->ics.num_swb; g++) { 1030 1203 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { 1031 1204 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; 1032 if (band->energy <= band->threshold) {1033 sce->sf_idx[(w+w2)*16+g] = 218;1205 if (band->energy <= 0.05 * lowlambda * band->threshold) { 1206 sce->sf_idx[(w+w2)*16+g] = maxq; 1034 1207 sce->zeroes[(w+w2)*16+g] = 1; 1035 1208 } else { 1036 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);1209 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + 1.414*log2f(band->threshold * rlambda), minq, maxq); 1037 1210 sce->zeroes[(w+w2)*16+g] = 0; 1038 1211 } 1039 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);1040 1212 } 1041 1213 } 1042 1214 } 1043 for (i = 0; i < 128; i++) {1044 sce->sf_idx[i] = 140;1045 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);1046 }1047 1215 //set the same quantizers inside window groups 1048 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) 1049 for (g = 0; g < sce->ics.num_swb; g++) 1050 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) 1051 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g]; 1216 if (sce->ics.num_windows > 1) { 1217 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { 1218 for (g = 0; g < sce->ics.num_swb; g++) { 1219 if (sce->ics.group_len[w] > 1) { 1220 int avg_sf_idx = 0; 1221 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) 1222 avg_sf_idx += sce->sf_idx[w*16+g]; 1223 avg_sf_idx /= sce->ics.group_len[w]; 1224 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) 1225 sce->sf_idx[(w+w2)*16+g] = avg_sf_idx; 1226 } 1227 } 1228 } 1229 } 1230 } 1231 1232 static float bval2bmax(float b) 1233 { 1234 /* approximates exp10f(-3.0f*(0.5f + 0.5f * cosf(FFMIN(b,15.5f) / 15.5f))) */ 1235 return 0.001f + 0.0035f * (b*b*b) / (15.5f*15.5f*15.5f); 1052 1236 } 1053 1237 1054 1238 static void search_for_ms(AACEncContext *s, ChannelElement *cpe, … … static void search_for_ms(AACEncContext *s, ChannelElement *cpe, 1059 1243 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3; 1060 1244 SingleChannelElement *sce0 = &cpe->ch[0]; 1061 1245 SingleChannelElement *sce1 = &cpe->ch[1]; 1246 1062 1247 if (!cpe->common_window) 1063 1248 return; 1249 1064 1250 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) { 1251 int min_sf_idx_mid = SCALE_MAX_POS; 1252 int min_sf_idx_side = SCALE_MAX_POS; 1253 for (g = 0; g < sce0->ics.num_swb; g++) { 1254 if (!sce0->zeroes[w*16+g]) 1255 min_sf_idx_mid = FFMIN(min_sf_idx_mid, sce0->sf_idx[w*16+g]); 1256 if (!sce1->zeroes[w*16+g]) 1257 min_sf_idx_side = FFMIN(min_sf_idx_side, sce1->sf_idx[w*16+g]); 1258 } 1259 1065 1260 for (g = 0; g < sce0->ics.num_swb; g++) { 1261 float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f; 1066 1262 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) { 1067 1263 float dist1 = 0.0f, dist2 = 0.0f; 1264 int B0 = 0, B1 = 0; 1265 int minidx; 1266 int mididx, sididx; 1267 float Mmax = 0.0f, Smax = 0.0f; 1268 int midcb, sidcb; 1269 1270 /* Must compute mid/side SF and book for the whole window group */ 1271 minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]); 1272 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { 1273 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { 1274 M[i] = (sce0->coeffs[start+w2*128+i] 1275 + sce1->coeffs[start+w2*128+i]) * 0.5; 1276 S[i] = M[i] 1277 - sce1->coeffs[start+w2*128+i]; 1278 } 1279 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]); 1280 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]); 1281 for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) { 1282 Mmax = FFMAX(Mmax, M34[i]); 1283 Smax = FFMAX(Smax, S34[i]); 1284 } 1285 } 1286 mididx = av_clip(minidx, min_sf_idx_mid, min_sf_idx_mid + SCALE_MAX_DIFF); 1287 sididx = av_clip(minidx, min_sf_idx_side, min_sf_idx_side + SCALE_MAX_DIFF); 1288 midcb = find_min_book(Mmax, mididx); 1289 sidcb = find_min_book(Smax, sididx); 1290 1068 1291 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { 1069 1292 FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; 1070 1293 FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; 1071 1294 float minthr = FFMIN(band0->threshold, band1->threshold); 1072 float maxthr = FFMAX(band0->threshold, band1->threshold);1295 int b1,b2,b3,b4; 1073 1296 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { 1074 1297 M[i] = (sce0->coeffs[start+w2*128+i] 1075 1298 + sce1->coeffs[start+w2*128+i]) * 0.5; … … static void search_for_ms(AACEncContext *s, ChannelElement *cpe, 1085 1308 sce0->ics.swb_sizes[g], 1086 1309 sce0->sf_idx[(w+w2)*16+g], 1087 1310 sce0->band_type[(w+w2)*16+g], 1088 lambda / band0->threshold, INFINITY, NULL);1311 lambda / band0->threshold, INFINITY, &b1); 1089 1312 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128, 1090 1313 R34, 1091 1314 sce1->ics.swb_sizes[g], 1092 1315 sce1->sf_idx[(w+w2)*16+g], 1093 1316 sce1->band_type[(w+w2)*16+g], 1094 lambda / band1->threshold, INFINITY, NULL);1317 lambda / band1->threshold, INFINITY, &b2); 1095 1318 dist2 += quantize_band_cost(s, M, 1096 1319 M34, 1097 1320 sce0->ics.swb_sizes[g], 1098 sce0->sf_idx[(w+w2)*16+g],1099 sce0->band_type[(w+w2)*16+g],1100 lambda / m axthr, INFINITY, NULL);1321 mididx, 1322 midcb, 1323 lambda / minthr, INFINITY, &b3); 1101 1324 dist2 += quantize_band_cost(s, S, 1102 1325 S34, 1103 1326 sce1->ics.swb_sizes[g], 1104 sce1->sf_idx[(w+w2)*16+g], 1105 sce1->band_type[(w+w2)*16+g], 1106 lambda / minthr, INFINITY, NULL); 1327 sididx, 1328 sidcb, 1329 lambda * (lambda / 120.0f) / (minthr * bmax), INFINITY, &b4); 1330 B0 += b1+b2; 1331 B1 += b3+b4; 1332 dist1 -= B0; 1333 dist2 -= B1; 1334 } 1335 cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0; 1336 if (cpe->ms_mask[w*16+g]) { 1337 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { 1338 sce0->sf_idx[(w+w2)*16+g] = mididx; 1339 sce0->band_type[(w+w2)*16+g] = midcb; 1340 sce1->sf_idx[(w+w2)*16+g] = sididx; 1341 sce1->band_type[(w+w2)*16+g] = sidcb; 1342 } 1107 1343 } 1108 cpe->ms_mask[w*16+g] = dist2 < dist1; 1344 } else { 1345 cpe->ms_mask[w*16+g] = 0; 1109 1346 } 1110 1347 start += sce0->ics.swb_sizes[g]; 1111 1348 } -
libavcodec/aacenc.c
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c index 362f02b..0eb72e6 100644
a b static void adjust_frame_information(ChannelElement *cpe, int chans) 317 317 if (cpe->common_window && !ch && cpe->ms_mask[w + g]) { 318 318 for (i = 0; i < ics->swb_sizes[g]; i++) { 319 319 cpe->ch[0].coeffs[start+i] = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) / 2.0; 320 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i];320 cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].coeffs[start+i]; 321 321 } 322 322 } 323 323 start += ics->swb_sizes[g]; … … static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 507 507 AACEncContext *s = avctx->priv_data; 508 508 float **samples = s->planar_samples, *samples2, *la, *overlap; 509 509 ChannelElement *cpe; 510 int i, ch, w, g, chans, tag, start_ch, ret ;510 int i, ch, w, g, chans, tag, start_ch, ret, frame_bits, its; 511 511 int chan_el_counter[4]; 512 512 FFPsyWindowInfo windows[AAC_MAX_CHANNELS]; 513 513 … … static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 572 572 } 573 573 if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels)) < 0) 574 574 return ret; 575 576 frame_bits = 0; 577 its = 0; 575 578 do { 576 int frame_bits;577 579 int target_bits, too_many_bits, too_few_bits; 580 578 581 init_put_bits(&s->pb, avpkt->data, avpkt->size); 579 582 580 583 if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT)) 581 584 put_bitstream_info(s, LIBAVCODEC_IDENT); 582 585 start_ch = 0; 586 target_bits = 0; 583 587 memset(chan_el_counter, 0, sizeof(chan_el_counter)); 584 588 for (i = 0; i < s->chan_map[0]; i++) { 585 589 FFPsyWindowInfo* wi = windows + start_ch; … … static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 591 595 put_bits(&s->pb, 4, chan_el_counter[tag]++); 592 596 for (ch = 0; ch < chans; ch++) 593 597 coeffs[ch] = cpe->ch[ch].coeffs; 598 s->psy.bitres.alloc = -1; 594 599 s->psy.model->analyze(&s->psy, start_ch, coeffs, wi); 600 target_bits += s->psy.bitres.alloc * (120.f / s->lambda); 595 601 for (ch = 0; ch < chans; ch++) { 596 602 s->cur_channel = start_ch + ch; 597 603 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda); … … static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 635 641 start_ch += chans; 636 642 } 637 643 644 if (avctx->flags & CODEC_FLAG_QSCALE) { 645 // When using a constant Q-scale, don't mess with lambda 646 break; 647 } 648 649 // rate control stuff 650 // target either the nominal bitrate, or what psy's bit reservoir says to target 651 // whichever is greatest 638 652 frame_bits = put_bits_count(&s->pb); 639 if (frame_bits <= 6144 * s->channels - 3) { 640 s->psy.bitres.bits = frame_bits / s->channels; 653 target_bits = FFMAX(target_bits, avctx->bit_rate * 1024 / avctx->sample_rate); 654 target_bits = FFMIN(target_bits, 6144 * s->channels - 3); 655 656 // When using ABR, be strict (but only for increasing) 657 too_many_bits = target_bits + target_bits/2; 658 too_few_bits = target_bits - target_bits/8; 659 660 if ( its == 0 /* for steady-state Q-scale tracking */ 661 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits)) 662 || frame_bits >= 6144 * s->channels - 3 ) 663 { 664 float prev_lambda = s->lambda; 665 float ratio = ((float)target_bits) / frame_bits; 666 s->lambda = FFMIN(s->lambda * ratio, 65536.f); 667 668 if (prev_lambda == s->lambda) 669 break; 670 671 // Keep iterating if we must reduce and lambda is in the sky 672 if (ratio > 0.9f || s->lambda <= 300.f) 673 its++; 674 675 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) { 676 /* 677 This path is for steady-state Q-scale tracking 678 When frame bits fall within the stable range, we still need to adjust 679 lambda to maintain it like so in a stable fashion (large jumps in lambda 680 create artifacts and shoulda be avoided) 681 */ 682 break; 683 } 684 } else { 641 685 break; 642 686 } 643 644 s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;645 646 687 } while (1); 647 688 648 689 put_bits(&s->pb, 3, TYPE_END); 649 690 flush_put_bits(&s->pb); 650 691 avctx->frame_bits = put_bits_count(&s->pb); 651 652 // rate control stuff 653 if (!(avctx->flags & CODEC_FLAG_QSCALE)) { 654 float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / avctx->frame_bits; 655 s->lambda *= ratio; 656 s->lambda = FFMIN(s->lambda, 65536.f); 657 } 692 693 if (avctx->flags & CODEC_FLAG_QSCALE) 694 // Lie, we probably used more bits due to RD adjustment, but we must not try to save those bits later 695 s->psy.bitres.bits = s->psy.bitres.alloc ? s->psy.bitres.alloc : (avctx->frame_bits / s->channels); 696 else 697 s->psy.bitres.bits = avctx->frame_bits / s->channels; 658 698 659 699 if (!frame) 660 700 s->last_frame++; -
libavcodec/aacpsy.c
diff --git a/libavcodec/aacpsy.c b/libavcodec/aacpsy.c index d2a782e..82cac6a 100644
a b 24 24 * AAC encoder psychoacoustic model 25 25 */ 26 26 27 #include "libavutil/attributes.h"28 27 #include "libavutil/libm.h" 29 28 30 29 #include "avcodec.h" … … static float lame_calc_attack_threshold(int bitrate) 255 254 /** 256 255 * LAME psy model specific initialization 257 256 */ 258 static av_cold void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx) 259 { 257 static void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx) { 260 258 int i, j; 261 259 262 260 for (i = 0; i < avctx->channels; i++) { … … static av_cold int psy_3gpp_init(FFPsyContext *ctx) { 299 297 float bark; 300 298 int i, j, g, start; 301 299 float prev, minscale, minath, minsnr, pe_min; 302 const int chan_bitrate = ctx->avctx->bit_rate / ctx->avctx->channels;300 int chan_bitrate = ctx->avctx->bit_rate / ((ctx->avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : ctx->avctx->channels); 303 301 const int bandwidth = ctx->avctx->cutoff ? ctx->avctx->cutoff : AAC_CUTOFF(ctx->avctx); 304 302 const float num_bark = calc_bark((float)bandwidth); 305 303 306 304 ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext)); 307 305 pctx = (AacPsyContext*) ctx->model_priv_data; 308 306 307 if (ctx->avctx->flags & CODEC_FLAG_QSCALE) { 308 /* Use the target average bitrate to compute spread parameters */ 309 chan_bitrate = (int)(chan_bitrate / 120.0 * (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120)); 310 } 311 309 312 pctx->chan_bitrate = chan_bitrate; 310 pctx->frame_bits = chan_bitrate * AAC_BLOCK_SIZE_LONG / ctx->avctx->sample_rate;313 pctx->frame_bits = FFMIN(2560, chan_bitrate * AAC_BLOCK_SIZE_LONG / ctx->avctx->sample_rate); 311 314 pctx->pe.min = 8.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f); 312 315 pctx->pe.max = 12.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f); 313 316 ctx->bitres.size = 6144 - pctx->frame_bits; 314 317 ctx->bitres.size -= ctx->bitres.size % 8; 315 318 pctx->fill_level = ctx->bitres.size; 316 319 minath = ath(3410, ATH_ADD); 320 317 321 for (j = 0; j < 2; j++) { 318 322 AacPsyCoeffs *coeffs = pctx->psy_coef[j]; 319 323 const uint8_t *band_sizes = ctx->bands[j]; … … static av_unused FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx, 391 395 int channel, int prev_type) 392 396 { 393 397 int i, j; 394 int br = ctx->avctx->bit_rate / ctx->avctx->channels;398 int br = ((AacPsyContext*)ctx->model_priv_data)->chan_bitrate; 395 399 int attack_ratio = br <= 16000 ? 18 : 10; 396 400 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data; 397 401 AacPsyChannel *pch = &pctx->ch[channel]; … … static void psy_3gpp_analyze_channel(FFPsyContext *ctx, int channel, 628 632 const uint8_t *band_sizes = ctx->bands[wi->num_windows == 8]; 629 633 AacPsyCoeffs *coeffs = pctx->psy_coef[wi->num_windows == 8]; 630 634 const float avoid_hole_thr = wi->num_windows == 8 ? PSY_3GPP_AH_THR_SHORT : PSY_3GPP_AH_THR_LONG; 631 635 632 636 //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation" 633 637 calc_thr_3gpp(wi, num_bands, pch, band_sizes, coefs); 634 638 … … static void psy_3gpp_analyze_channel(FFPsyContext *ctx, int channel, 681 685 desired_pe *= av_clipf(pctx->pe.previous / PSY_3GPP_BITS_TO_PE(ctx->bitres.bits), 682 686 0.85f, 1.15f); 683 687 pctx->pe.previous = PSY_3GPP_BITS_TO_PE(desired_bits); 688 ctx->bitres.alloc = desired_bits; 684 689 685 690 if (desired_pe < pe) { 686 691 /* 5.6.1.3.4 "First Estimation of the reduction value" */ -
libavcodec/psymodel.c
diff --git a/libavcodec/psymodel.c b/libavcodec/psymodel.c index bfc85b3..2b8396d 100644
a b FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel) 75 75 76 76 av_cold void ff_psy_end(FFPsyContext *ctx) 77 77 { 78 if (ctx->model && ctx->model->end)78 if (ctx->model->end) 79 79 ctx->model->end(ctx); 80 80 av_freep(&ctx->bands); 81 81 av_freep(&ctx->num_bands); … … av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *av 106 106 107 107 if (!cutoff_coeff && avctx->codec_id == AV_CODEC_ID_AAC) 108 108 cutoff_coeff = 2.0 * AAC_CUTOFF(avctx) / avctx->sample_rate; 109 109 110 110 if (cutoff_coeff && cutoff_coeff < 0.98) 111 111 ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH, 112 112 FF_FILTER_MODE_LOWPASS, FILT_ORDER, -
libavcodec/psymodel.h
diff --git a/libavcodec/psymodel.h b/libavcodec/psymodel.h index d1a126a..5ebd44f 100644
a b 27 27 /** maximum possible number of bands */ 28 28 #define PSY_MAX_BANDS 128 29 29 /** maximum number of channels */ 30 #define PSY_MAX_CHANS 2 030 #define PSY_MAX_CHANS 24 31 31 32 #define AAC_CUTOFF(s) (s->bit_rate ? FFMIN3(4000 + s->bit_rate/8, 12000 + s->bit_rate/32, s->sample_rate / 2) : (s->sample_rate / 2)) 32 /* cutoff for VBR is purposedly increased, since LP filtering actually 33 * hinders VBR performance rather than the opposite 34 */ 35 #define _AAC_CUTOFF(bit_rate,channels,sample_rate) (bit_rate ? FFMIN3(FFMIN3( \ 36 bit_rate/channels/2, \ 37 3000 + bit_rate/channels/4, \ 38 12000 + bit_rate/channels/16), \ 39 20000, \ 40 sample_rate / 2): (sample_rate / 2)) 41 #define AAC_CUTOFF(s) ( \ 42 (s->flags & CODEC_FLAG_QSCALE) \ 43 ? /*_AAC_CUTOFF(((int)(480000.0f*(s->global_quality ? s->global_quality/120.0f : 1.0f))), 1, s->sample_rate)*/s->sample_rate / 2 \ 44 : _AAC_CUTOFF(s->bit_rate, s->channels, s->sample_rate) \ 45 ) 33 46 34 47 /** 35 48 * single band psychoacoustic information … … typedef struct FFPsyContext { 88 101 struct { 89 102 int size; ///< size of the bitresevoir in bits 90 103 int bits; ///< number of bits used in the bitresevoir 104 int alloc; ///< number of bits allocated by the psy, or -1 if no allocation was done 91 105 } bitres; 92 106 93 107 void* model_priv_data; ///< psychoacoustic model implementation private data
