Mercurial > libavcodec.hg
comparison aaccoder.c @ 11731:73f923159384 libavcodec
aacenc: Use exact values when quantizing, not fuzzy values.
This requires us to code small escapes; we can't avoid it.
author | alexc |
---|---|
date | Fri, 14 May 2010 16:49:40 +0000 |
parents | 5e8702ddbb93 |
children | c40cc26cd23f |
comparison
equal
deleted
inserted
replaced
11730:5e8702ddbb93 | 11731:73f923159384 |
---|---|
63 { | 63 { |
64 float a = coef * Q; | 64 float a = coef * Q; |
65 return sqrtf(a * sqrtf(a)) + 0.4054; | 65 return sqrtf(a * sqrtf(a)) + 0.4054; |
66 } | 66 } |
67 | 67 |
68 static void quantize_bands(int (*out)[2], const float *in, const float *scaled, | 68 static void quantize_bands(int *out, const float *in, const float *scaled, |
69 int size, float Q34, int is_signed, int maxval) | 69 int size, float Q34, int is_signed, int maxval) |
70 { | 70 { |
71 int i; | 71 int i; |
72 double qc; | 72 double qc; |
73 for (i = 0; i < size; i++) { | 73 for (i = 0; i < size; i++) { |
74 qc = scaled[i] * Q34; | 74 qc = scaled[i] * Q34; |
75 out[i][0] = (int)FFMIN(qc, (double)maxval); | 75 out[i] = (int)FFMIN(qc + 0.4054, (double)maxval); |
76 out[i][1] = (int)FFMIN(qc + 0.4054, (double)maxval); | |
77 if (is_signed && in[i] < 0.0f) { | 76 if (is_signed && in[i] < 0.0f) { |
78 out[i][0] = -out[i][0]; | 77 out[i] = -out[i]; |
79 out[i][1] = -out[i][1]; | |
80 } | 78 } |
81 } | 79 } |
82 } | 80 } |
83 | 81 |
84 static void abs_pow34_v(float *out, const float *in, const int size) | 82 static void abs_pow34_v(float *out, const float *in, const int size) |
111 const float CLIPPED_ESCAPE = 165140.0f*IQ; | 109 const float CLIPPED_ESCAPE = 165140.0f*IQ; |
112 int i, j, k; | 110 int i, j, k; |
113 float cost = 0; | 111 float cost = 0; |
114 const int dim = cb < FIRST_PAIR_BT ? 4 : 2; | 112 const int dim = cb < FIRST_PAIR_BT ? 4 : 2; |
115 int resbits = 0; | 113 int resbits = 0; |
116 #ifndef USE_REALLY_FULL_SEARCH | |
117 const float Q34 = sqrtf(Q * sqrtf(Q)); | 114 const float Q34 = sqrtf(Q * sqrtf(Q)); |
118 const int range = aac_cb_range[cb]; | 115 const int range = aac_cb_range[cb]; |
119 const int maxval = aac_cb_maxval[cb]; | 116 const int maxval = aac_cb_maxval[cb]; |
120 int offs[4]; | 117 int off; |
121 #endif /* USE_REALLY_FULL_SEARCH */ | |
122 | 118 |
123 if (!cb) { | 119 if (!cb) { |
124 for (i = 0; i < size; i++) | 120 for (i = 0; i < size; i++) |
125 cost += in[i]*in[i]; | 121 cost += in[i]*in[i]; |
126 if (bits) | 122 if (bits) |
127 *bits = 0; | 123 *bits = 0; |
128 return cost * lambda; | 124 return cost * lambda; |
129 } | 125 } |
130 #ifndef USE_REALLY_FULL_SEARCH | |
131 offs[0] = 1; | |
132 for (i = 1; i < dim; i++) | |
133 offs[i] = offs[i-1]*range; | |
134 if (!scaled) { | 126 if (!scaled) { |
135 abs_pow34_v(s->scoefs, in, size); | 127 abs_pow34_v(s->scoefs, in, size); |
136 scaled = s->scoefs; | 128 scaled = s->scoefs; |
137 } | 129 } |
138 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); | 130 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); |
139 #endif /* USE_REALLY_FULL_SEARCH */ | 131 if (IS_CODEBOOK_UNSIGNED(cb)) { |
132 off = 0; | |
133 } else { | |
134 off = maxval; | |
135 } | |
140 for (i = 0; i < size; i += dim) { | 136 for (i = 0; i < size; i += dim) { |
141 float mincost; | |
142 int minidx = 0; | |
143 int minbits = 0; | |
144 const float *vec; | 137 const float *vec; |
145 #ifndef USE_REALLY_FULL_SEARCH | 138 int *quants = s->qcoefs + i; |
146 int (*quants)[2] = &s->qcoefs[i]; | 139 int curidx = 0; |
147 mincost = 0.0f; | 140 int curbits; |
148 for (j = 0; j < dim; j++) | 141 float rd = 0.0f; |
149 mincost += in[i+j]*in[i+j]; | 142 for (j = 0; j < dim; j++) { |
150 minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; | 143 curidx *= range; |
151 minbits = ff_aac_spectral_bits[cb-1][minidx]; | 144 curidx += quants[j] + off; |
152 mincost = mincost * lambda + minbits; | 145 } |
153 for (j = 0; j < (1<<dim); j++) { | |
154 float rd = 0.0f; | |
155 int curbits; | |
156 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; | |
157 int same = 0; | |
158 for (k = 0; k < dim; k++) { | |
159 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { | |
160 same = 1; | |
161 break; | |
162 } | |
163 } | |
164 if (same) | |
165 continue; | |
166 for (k = 0; k < dim; k++) | |
167 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; | |
168 curbits = ff_aac_spectral_bits[cb-1][curidx]; | 146 curbits = ff_aac_spectral_bits[cb-1][curidx]; |
169 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | 147 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; |
170 #else | |
171 mincost = INFINITY; | |
172 vec = ff_aac_codebook_vectors[cb-1]; | |
173 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { | |
174 float rd = 0.0f; | |
175 int curbits = ff_aac_spectral_bits[cb-1][j]; | |
176 int curidx = j; | |
177 #endif /* USE_REALLY_FULL_SEARCH */ | |
178 if (IS_CODEBOOK_UNSIGNED(cb)) { | 148 if (IS_CODEBOOK_UNSIGNED(cb)) { |
179 for (k = 0; k < dim; k++) { | 149 for (k = 0; k < dim; k++) { |
180 float t = fabsf(in[i+k]); | 150 float t = fabsf(in[i+k]); |
181 float di; | 151 float di; |
182 if (vec[k] == 64.0f) { //FIXME: slow | 152 if (vec[k] == 64.0f) { //FIXME: slow |
183 //do not code with escape sequence small values | |
184 if (t < 39.0f*IQ) { | |
185 rd = INFINITY; | |
186 break; | |
187 } | |
188 if (t >= CLIPPED_ESCAPE) { | 153 if (t >= CLIPPED_ESCAPE) { |
189 di = t - CLIPPED_ESCAPE; | 154 di = t - CLIPPED_ESCAPE; |
190 curbits += 21; | 155 curbits += 21; |
191 } else { | 156 } else { |
192 int c = av_clip(quant(t, Q), 0, 8191); | 157 int c = av_clip(quant(t, Q), 0, 8191); |
204 for (k = 0; k < dim; k++) { | 169 for (k = 0; k < dim; k++) { |
205 float di = in[i+k] - vec[k]*IQ; | 170 float di = in[i+k] - vec[k]*IQ; |
206 rd += di*di; | 171 rd += di*di; |
207 } | 172 } |
208 } | 173 } |
209 rd = rd * lambda + curbits; | 174 cost += rd * lambda + curbits; |
210 if (rd < mincost) { | 175 resbits += curbits; |
211 mincost = rd; | |
212 minidx = curidx; | |
213 minbits = curbits; | |
214 } | |
215 } | |
216 cost += mincost; | |
217 resbits += minbits; | |
218 if (cost >= uplim) | 176 if (cost >= uplim) |
219 return uplim; | 177 return uplim; |
220 if (pb) { | 178 if (pb) { |
221 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]); | 179 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); |
222 if (IS_CODEBOOK_UNSIGNED(cb)) | 180 if (IS_CODEBOOK_UNSIGNED(cb)) |
223 for (j = 0; j < dim; j++) | 181 for (j = 0; j < dim; j++) |
224 if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f) | 182 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) |
225 put_bits(pb, 1, in[i+j] < 0.0f); | 183 put_bits(pb, 1, in[i+j] < 0.0f); |
226 if (cb == ESC_BT) { | 184 if (cb == ESC_BT) { |
227 for (j = 0; j < 2; j++) { | 185 for (j = 0; j < 2; j++) { |
228 if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) { | 186 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { |
229 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191); | 187 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191); |
230 int len = av_log2(coef); | 188 int len = av_log2(coef); |
231 | 189 |
232 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); | 190 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); |
233 put_bits(pb, len, coef & ((1 << len) - 1)); | 191 put_bits(pb, len, coef & ((1 << len) - 1)); |