Mercurial > libavcodec.hg
comparison aaccoder.c @ 11537:bc0012099ba3 libavcodec
aacenc: Merge quantize_band_cost() with quantize_and_encode_band().
If these two functions aren't matched results may be unexpected.
author | alexc |
---|---|
date | Wed, 24 Mar 2010 17:09:21 +0000 |
parents | cebf6e3381e0 |
children | 7dd2a45249a9 |
comparison
equal
deleted
inserted
replaced
11536:22ab9663ca4e | 11537:bc0012099ba3 |
---|---|
98 /** | 98 /** |
99 * Calculate rate distortion cost for quantizing with given codebook | 99 * Calculate rate distortion cost for quantizing with given codebook |
100 * | 100 * |
101 * @return quantization distortion | 101 * @return quantization distortion |
102 */ | 102 */ |
103 static float quantize_band_cost(struct AACEncContext *s, const float *in, | 103 static float quantize_and_encode_band_cost(struct AACEncContext *s, |
104 PutBitContext *pb, const float *in, | |
104 const float *scaled, int size, int scale_idx, | 105 const float *scaled, int size, int scale_idx, |
105 int cb, const float lambda, const float uplim, | 106 int cb, const float lambda, const float uplim, |
106 int *bits) | 107 int *bits) |
107 { | 108 { |
108 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | 109 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; |
128 } | 129 } |
129 #ifndef USE_REALLY_FULL_SEARCH | 130 #ifndef USE_REALLY_FULL_SEARCH |
130 offs[0] = 1; | 131 offs[0] = 1; |
131 for (i = 1; i < dim; i++) | 132 for (i = 1; i < dim; i++) |
132 offs[i] = offs[i-1]*range; | 133 offs[i] = offs[i-1]*range; |
134 if (!scaled) { | |
135 abs_pow34_v(s->scoefs, in, size); | |
136 scaled = s->scoefs; | |
137 } | |
133 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); | 138 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); |
134 #endif /* USE_REALLY_FULL_SEARCH */ | 139 #endif /* USE_REALLY_FULL_SEARCH */ |
135 for (i = 0; i < size; i += dim) { | 140 for (i = 0; i < size; i += dim) { |
136 float mincost; | 141 float mincost; |
137 int minidx = 0; | 142 int minidx = 0; |
166 mincost = INFINITY; | 171 mincost = INFINITY; |
167 vec = ff_aac_codebook_vectors[cb-1]; | 172 vec = ff_aac_codebook_vectors[cb-1]; |
168 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { | 173 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { |
169 float rd = 0.0f; | 174 float rd = 0.0f; |
170 int curbits = ff_aac_spectral_bits[cb-1][j]; | 175 int curbits = ff_aac_spectral_bits[cb-1][j]; |
176 int curidx = j; | |
171 #endif /* USE_REALLY_FULL_SEARCH */ | 177 #endif /* USE_REALLY_FULL_SEARCH */ |
172 if (IS_CODEBOOK_UNSIGNED(cb)) { | 178 if (IS_CODEBOOK_UNSIGNED(cb)) { |
173 for (k = 0; k < dim; k++) { | 179 for (k = 0; k < dim; k++) { |
174 float t = fabsf(in[i+k]); | 180 float t = fabsf(in[i+k]); |
175 float di; | 181 float di; |
201 } | 207 } |
202 } | 208 } |
203 rd = rd * lambda + curbits; | 209 rd = rd * lambda + curbits; |
204 if (rd < mincost) { | 210 if (rd < mincost) { |
205 mincost = rd; | 211 mincost = rd; |
206 minidx = j; | 212 minidx = curidx; |
207 minbits = curbits; | 213 minbits = curbits; |
208 } | 214 } |
209 } | 215 } |
210 cost += mincost; | 216 cost += mincost; |
211 resbits += minbits; | 217 resbits += minbits; |
212 if (cost >= uplim) | 218 if (cost >= uplim) |
213 return uplim; | 219 return uplim; |
214 } | 220 if (pb) { |
215 | |
216 if (bits) | |
217 *bits = resbits; | |
218 return cost; | |
219 } | |
220 | |
221 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, | |
222 const float *in, int size, int scale_idx, | |
223 int cb, const float lambda) | |
224 { | |
225 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | |
226 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; | |
227 const float CLIPPED_ESCAPE = 165140.0f*IQ; | |
228 const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2; | |
229 int i, j, k; | |
230 #ifndef USE_REALLY_FULL_SEARCH | |
231 const float Q34 = sqrtf(Q * sqrtf(Q)); | |
232 const int range = aac_cb_range[cb]; | |
233 const int maxval = aac_cb_maxval[cb]; | |
234 int offs[4]; | |
235 float *scaled = s->scoefs; | |
236 #endif /* USE_REALLY_FULL_SEARCH */ | |
237 | |
238 //START_TIMER | |
239 if (!cb) | |
240 return; | |
241 | |
242 #ifndef USE_REALLY_FULL_SEARCH | |
243 offs[0] = 1; | |
244 for (i = 1; i < dim; i++) | |
245 offs[i] = offs[i-1]*range; | |
246 abs_pow34_v(scaled, in, size); | |
247 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); | |
248 #endif /* USE_REALLY_FULL_SEARCH */ | |
249 for (i = 0; i < size; i += dim) { | |
250 float mincost; | |
251 int minidx = 0; | |
252 int minbits = 0; | |
253 const float *vec; | |
254 #ifndef USE_REALLY_FULL_SEARCH | |
255 int (*quants)[2] = &s->qcoefs[i]; | |
256 mincost = 0.0f; | |
257 for (j = 0; j < dim; j++) | |
258 mincost += in[i+j]*in[i+j]; | |
259 minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; | |
260 minbits = ff_aac_spectral_bits[cb-1][minidx]; | |
261 mincost = mincost * lambda + minbits; | |
262 for (j = 0; j < (1<<dim); j++) { | |
263 float rd = 0.0f; | |
264 int curbits; | |
265 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; | |
266 int same = 0; | |
267 for (k = 0; k < dim; k++) { | |
268 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { | |
269 same = 1; | |
270 break; | |
271 } | |
272 } | |
273 if (same) | |
274 continue; | |
275 for (k = 0; k < dim; k++) | |
276 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; | |
277 curbits = ff_aac_spectral_bits[cb-1][curidx]; | |
278 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | |
279 #else | |
280 vec = ff_aac_codebook_vectors[cb-1]; | |
281 mincost = INFINITY; | |
282 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { | |
283 float rd = 0.0f; | |
284 int curbits = ff_aac_spectral_bits[cb-1][j]; | |
285 int curidx = j; | |
286 #endif /* USE_REALLY_FULL_SEARCH */ | |
287 if (IS_CODEBOOK_UNSIGNED(cb)) { | |
288 for (k = 0; k < dim; k++) { | |
289 float t = fabsf(in[i+k]); | |
290 float di; | |
291 if (vec[k] == 64.0f) { //FIXME: slow | |
292 //do not code with escape sequence small values | |
293 if (t < 39.0f*IQ) { | |
294 rd = INFINITY; | |
295 break; | |
296 } | |
297 if (t >= CLIPPED_ESCAPE) { | |
298 di = t - CLIPPED_ESCAPE; | |
299 curbits += 21; | |
300 } else { | |
301 int c = av_clip(quant(t, Q), 0, 8191); | |
302 di = t - c*cbrtf(c)*IQ; | |
303 curbits += av_log2(c)*2 - 4 + 1; | |
304 } | |
305 } else { | |
306 di = t - vec[k]*IQ; | |
307 } | |
308 if (vec[k] != 0.0f) | |
309 curbits++; | |
310 rd += di*di; | |
311 } | |
312 } else { | |
313 for (k = 0; k < dim; k++) { | |
314 float di = in[i+k] - vec[k]*IQ; | |
315 rd += di*di; | |
316 } | |
317 } | |
318 rd = rd * lambda + curbits; | |
319 if (rd < mincost) { | |
320 mincost = rd; | |
321 minidx = curidx; | |
322 minbits = curbits; | |
323 } | |
324 } | |
325 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]); | 221 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]); |
326 if (IS_CODEBOOK_UNSIGNED(cb)) | 222 if (IS_CODEBOOK_UNSIGNED(cb)) |
327 for (j = 0; j < dim; j++) | 223 for (j = 0; j < dim; j++) |
328 if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f) | 224 if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f) |
329 put_bits(pb, 1, in[i+j] < 0.0f); | 225 put_bits(pb, 1, in[i+j] < 0.0f); |
336 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); | 232 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); |
337 put_bits(pb, len, coef & ((1 << len) - 1)); | 233 put_bits(pb, len, coef & ((1 << len) - 1)); |
338 } | 234 } |
339 } | 235 } |
340 } | 236 } |
341 } | 237 } |
342 //STOP_TIMER("quantize_and_encode") | 238 } |
239 | |
240 if (bits) | |
241 *bits = resbits; | |
242 return cost; | |
243 } | |
244 static float quantize_band_cost(struct AACEncContext *s, const float *in, | |
245 const float *scaled, int size, int scale_idx, | |
246 int cb, const float lambda, const float uplim, | |
247 int *bits) | |
248 { | |
249 return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx, | |
250 cb, lambda, uplim, bits); | |
251 } | |
252 | |
253 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, | |
254 const float *in, int size, int scale_idx, | |
255 int cb, const float lambda) | |
256 { | |
257 quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda, | |
258 INFINITY, NULL); | |
343 } | 259 } |
344 | 260 |
345 /** | 261 /** |
346 * structure used in optimal codebook search | 262 * structure used in optimal codebook search |
347 */ | 263 */ |