History

aacenc: Merge quantize_band_cost() with quantize_and_encode_band().

If these two functions aren't matched results may be unexpected. Originally committed as revision 22655 to svn://svn.ffmpeg.org/ffmpeg/trunk

Alex Converse committed 16 years ago

Commit 508f0921 parent d6928501 file · +30 −114Browse files

Showing the changes to libavcodec/aaccoder.c (1 of 1 file). Show all

Unified · Split

libavcodec/aaccoder.c+30−114
@@ -100,7 +100,8 @@ static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16} · 99 unmodified lines
100100 *
101101 * @return quantization distortion
102102 */
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,
104105 const float *scaled, int size, int scale_idx,
105106 int cb, const float lambda, const float uplim,
106107 int *bits)
@@ -130,6 +131,10 @@ static float quantize_band_cost(struct AACEncContext *s, const float *in, · 23 unmodified lines
130131 offs[0] = 1;
131132 for (i = 1; i < dim; i++)
132133 offs[i] = offs[i-1]*range;
134+ if (!scaled) {
135+ abs_pow34_v(s->scoefs, in, size);
136+ scaled = s->scoefs;
137+ }
133138 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
134139 #endif /* USE_REALLY_FULL_SEARCH */
135140 for (i = 0; i < size; i += dim) {
@@ -168,6 +173,7 @@ static float quantize_band_cost(struct AACEncContext *s, const float *in, · 32 unmodified lines
168173 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
169174 float rd = 0.0f;
170175 int curbits = ff_aac_spectral_bits[cb-1][j];
176+ int curidx = j;
171177 #endif /* USE_REALLY_FULL_SEARCH */
172178 if (IS_CODEBOOK_UNSIGNED(cb)) {
173179 for (k = 0; k < dim; k++) {
@@ -203,7 +209,7 @@ static float quantize_band_cost(struct AACEncContext *s, const float *in, · 29 unmodified lines
203209 rd = rd * lambda + curbits;
204210 if (rd < mincost) {
205211 mincost = rd;
206− minidx = j;
212+ minidx = curidx;
207213 minbits = curbits;
208214 }
209215 }
@@ -211,117 +217,7 @@ static float quantize_band_cost(struct AACEncContext *s, const float *in, · 1 unmodified line
211217 resbits += minbits;
212218 if (cost >= uplim)
213219 return uplim;
214− }
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− }
220+ if (pb) {
325221 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
326222 if (IS_CODEBOOK_UNSIGNED(cb))
327223 for (j = 0; j < dim; j++)
@@ -338,8 +234,28 @@ static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, · 10 unmodified lines
338234 }
339235 }
340236 }
237+ }
341238 }
342−//STOP_TIMER("quantize_and_encode")
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);
343259 }
344260
345261 /**

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t