Mercurial > libavcodec.hg
comparison aaccoder.c @ 9937:3e39dbd2d9eb libavcodec
cosmetics: prettyprinting, K&R style, break overly long lines
author | diego |
---|---|
date | Wed, 08 Jul 2009 21:16:06 +0000 |
parents | 7f42ae22c351 |
children | 6c1ac45b3097 |
comparison
equal
deleted
inserted
replaced
9936:7f42ae22c351 | 9937:3e39dbd2d9eb |
---|---|
47 /** bits needed to code codebook run value for short windows */ | 47 /** bits needed to code codebook run value for short windows */ |
48 static const uint8_t run_value_bits_short[16] = { | 48 static const uint8_t run_value_bits_short[16] = { |
49 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9 | 49 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9 |
50 }; | 50 }; |
51 | 51 |
52 static const uint8_t* run_value_bits[2] = { | 52 static const uint8_t *run_value_bits[2] = { |
53 run_value_bits_long, run_value_bits_short | 53 run_value_bits_long, run_value_bits_short |
54 }; | 54 }; |
55 | 55 |
56 | 56 |
57 /** | 57 /** |
62 static av_always_inline int quant(float coef, const float Q) | 62 static av_always_inline int quant(float coef, const float Q) |
63 { | 63 { |
64 return pow(coef * Q, 0.75) + 0.4054; | 64 return pow(coef * Q, 0.75) + 0.4054; |
65 } | 65 } |
66 | 66 |
67 static void quantize_bands(int (*out)[2], const float *in, const float *scaled, int size, float Q34, int is_signed, int maxval) | 67 static void quantize_bands(int (*out)[2], const float *in, const float *scaled, |
68 int size, float Q34, int is_signed, int maxval) | |
68 { | 69 { |
69 int i; | 70 int i; |
70 double qc; | 71 double qc; |
71 for (i = 0; i < size; i++) { | 72 for (i = 0; i < size; i++) { |
72 qc = scaled[i] * Q34; | 73 qc = scaled[i] * Q34; |
73 out[i][0] = (int)FFMIN((int)qc, maxval); | 74 out[i][0] = (int)FFMIN((int)qc, maxval); |
74 out[i][1] = (int)FFMIN((int)(qc + 0.4054), maxval); | 75 out[i][1] = (int)FFMIN((int)(qc + 0.4054), maxval); |
75 if (is_signed && in[i] < 0.0f) { | 76 if (is_signed && in[i] < 0.0f) { |
76 out[i][0] = -out[i][0]; | 77 out[i][0] = -out[i][0]; |
77 out[i][1] = -out[i][1]; | 78 out[i][1] = -out[i][1]; |
78 } | 79 } |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 static void abs_pow34_v(float *out, const float* in, const int size) | 83 static void abs_pow34_v(float *out, const float *in, const int size) |
83 { | 84 { |
84 #ifndef USE_REALLY_FULL_SEARCH | 85 #ifndef USE_REALLY_FULL_SEARCH |
85 int i; | 86 int i; |
86 for (i = 0; i < size; i++) { | 87 for (i = 0; i < size; i++) { |
87 out[i] = pow(fabsf(in[i]), 0.75); | 88 out[i] = pow(fabsf(in[i]), 0.75); |
100 /** | 101 /** |
101 * Calculate rate distortion cost for quantizing with given codebook | 102 * Calculate rate distortion cost for quantizing with given codebook |
102 * | 103 * |
103 * @return quantization distortion | 104 * @return quantization distortion |
104 */ | 105 */ |
105 static float quantize_band_cost(struct AACEncContext *s, const float *in, const float *scaled, int size, int scale_idx, int cb, | 106 static float quantize_band_cost(struct AACEncContext *s, const float *in, |
106 const float lambda, const float uplim, int *bits) | 107 const float *scaled, int size, int scale_idx, |
108 int cb, const float lambda, const float uplim, | |
109 int *bits) | |
107 { | 110 { |
108 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | 111 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; |
109 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; | 112 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; |
110 const float CLIPPED_ESCAPE = 165140.0f*IQ; | 113 const float CLIPPED_ESCAPE = 165140.0f*IQ; |
111 int i, j, k; | 114 int i, j, k; |
112 float cost = 0; | 115 float cost = 0; |
113 const int dim = cb < FIRST_PAIR_BT ? 4 : 2; | 116 const int dim = cb < FIRST_PAIR_BT ? 4 : 2; |
114 int resbits = 0; | 117 int resbits = 0; |
115 #ifndef USE_REALLY_FULL_SEARCH | 118 #ifndef USE_REALLY_FULL_SEARCH |
116 const float Q34 = pow(Q, 0.75); | 119 const float Q34 = pow(Q, 0.75); |
117 const int range = aac_cb_range[cb]; | 120 const int range = aac_cb_range[cb]; |
118 const int maxval = aac_cb_maxval[cb]; | 121 const int maxval = aac_cb_maxval[cb]; |
119 int offs[4]; | 122 int offs[4]; |
120 #endif /* USE_REALLY_FULL_SEARCH */ | 123 #endif /* USE_REALLY_FULL_SEARCH */ |
121 | 124 |
122 if (!cb) { | 125 if (!cb) { |
130 offs[i] = offs[i-1]*range; | 133 offs[i] = offs[i-1]*range; |
131 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); | 134 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); |
132 #endif /* USE_REALLY_FULL_SEARCH */ | 135 #endif /* USE_REALLY_FULL_SEARCH */ |
133 for (i = 0; i < size; i += dim) { | 136 for (i = 0; i < size; i += dim) { |
134 float mincost; | 137 float mincost; |
135 int minidx = 0; | 138 int minidx = 0; |
136 int minbits = 0; | 139 int minbits = 0; |
137 const float *vec; | 140 const float *vec; |
138 #ifndef USE_REALLY_FULL_SEARCH | 141 #ifndef USE_REALLY_FULL_SEARCH |
139 int (*quants)[2] = &s->qcoefs[i]; | 142 int (*quants)[2] = &s->qcoefs[i]; |
140 mincost = 0.0f; | 143 mincost = 0.0f; |
146 mincost += minbits; | 149 mincost += minbits; |
147 for (j = 0; j < (1<<dim); j++) { | 150 for (j = 0; j < (1<<dim); j++) { |
148 float rd = 0.0f; | 151 float rd = 0.0f; |
149 int curbits; | 152 int curbits; |
150 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; | 153 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; |
151 int same = 0; | 154 int same = 0; |
152 for (k = 0; k < dim; k++) { | 155 for (k = 0; k < dim; k++) { |
153 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { | 156 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { |
154 same = 1; | 157 same = 1; |
155 break; | 158 break; |
156 } | 159 } |
157 } | 160 } |
158 if (same) | 161 if (same) |
159 continue; | 162 continue; |
160 for (k = 0; k < dim; k++) | 163 for (k = 0; k < dim; k++) |
161 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; | 164 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; |
162 curbits = ff_aac_spectral_bits[cb-1][curidx]; | 165 curbits = ff_aac_spectral_bits[cb-1][curidx]; |
163 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | 166 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; |
164 #else | 167 #else |
165 mincost = INFINITY; | 168 mincost = INFINITY; |
166 vec = ff_aac_codebook_vectors[cb-1]; | 169 vec = ff_aac_codebook_vectors[cb-1]; |
167 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { | 170 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { |
168 float rd = 0.0f; | 171 float rd = 0.0f; |
175 //do not code with escape sequence small values | 178 //do not code with escape sequence small values |
176 if (vec[k] == 64.0f && t < 39.0f*IQ) { | 179 if (vec[k] == 64.0f && t < 39.0f*IQ) { |
177 rd = INFINITY; | 180 rd = INFINITY; |
178 break; | 181 break; |
179 } | 182 } |
180 if (vec[k] == 64.0f) {//FIXME: slow | 183 if (vec[k] == 64.0f) { //FIXME: slow |
181 if (t >= CLIPPED_ESCAPE) { | 184 if (t >= CLIPPED_ESCAPE) { |
182 di = t - CLIPPED_ESCAPE; | 185 di = t - CLIPPED_ESCAPE; |
183 curbits += 21; | 186 curbits += 21; |
184 } else { | 187 } else { |
185 int c = av_clip(quant(t, Q), 0, 8191); | 188 int c = av_clip(quant(t, Q), 0, 8191); |
200 } | 203 } |
201 } | 204 } |
202 rd += curbits; | 205 rd += curbits; |
203 if (rd < mincost) { | 206 if (rd < mincost) { |
204 mincost = rd; | 207 mincost = rd; |
205 minidx = j; | 208 minidx = j; |
206 minbits = curbits; | 209 minbits = curbits; |
207 } | 210 } |
208 } | 211 } |
209 cost += mincost; | 212 cost += mincost; |
210 resbits += minbits; | 213 resbits += minbits; |
211 if (cost >= uplim) | 214 if (cost >= uplim) |
212 return uplim; | 215 return uplim; |
213 } | 216 } |
214 | 217 |
215 if (bits) | 218 if (bits) |
216 *bits = resbits; | 219 *bits = resbits; |
217 return cost; | 220 return cost; |
218 } | 221 } |
219 | 222 |
220 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, int size, | 223 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, |
221 int scale_idx, int cb, const float lambda) | 224 const float *in, int size, int scale_idx, |
225 int cb, const float lambda) | |
222 { | 226 { |
223 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | 227 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; |
224 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; | 228 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; |
225 const float CLIPPED_ESCAPE = 165140.0f*IQ; | 229 const float CLIPPED_ESCAPE = 165140.0f*IQ; |
226 const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2; | 230 const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2; |
227 int i, j, k; | 231 int i, j, k; |
228 #ifndef USE_REALLY_FULL_SEARCH | 232 #ifndef USE_REALLY_FULL_SEARCH |
229 const float Q34 = pow(Q, 0.75); | 233 const float Q34 = pow(Q, 0.75); |
230 const int range = aac_cb_range[cb]; | 234 const int range = aac_cb_range[cb]; |
231 const int maxval = aac_cb_maxval[cb]; | 235 const int maxval = aac_cb_maxval[cb]; |
232 int offs[4]; | 236 int offs[4]; |
233 float *scaled = s->scoefs; | 237 float *scaled = s->scoefs; |
234 #endif /* USE_REALLY_FULL_SEARCH */ | 238 #endif /* USE_REALLY_FULL_SEARCH */ |
235 | 239 |
244 abs_pow34_v(scaled, in, size); | 248 abs_pow34_v(scaled, in, size); |
245 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); | 249 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); |
246 #endif /* USE_REALLY_FULL_SEARCH */ | 250 #endif /* USE_REALLY_FULL_SEARCH */ |
247 for (i = 0; i < size; i += dim) { | 251 for (i = 0; i < size; i += dim) { |
248 float mincost; | 252 float mincost; |
249 int minidx = 0; | 253 int minidx = 0; |
250 int minbits = 0; | 254 int minbits = 0; |
251 const float *vec; | 255 const float *vec; |
252 #ifndef USE_REALLY_FULL_SEARCH | 256 #ifndef USE_REALLY_FULL_SEARCH |
253 int (*quants)[2] = &s->qcoefs[i]; | 257 int (*quants)[2] = &s->qcoefs[i]; |
254 mincost = 0.0f; | 258 mincost = 0.0f; |
260 mincost += minbits; | 264 mincost += minbits; |
261 for (j = 0; j < (1<<dim); j++) { | 265 for (j = 0; j < (1<<dim); j++) { |
262 float rd = 0.0f; | 266 float rd = 0.0f; |
263 int curbits; | 267 int curbits; |
264 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; | 268 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40; |
265 int same = 0; | 269 int same = 0; |
266 for (k = 0; k < dim; k++) { | 270 for (k = 0; k < dim; k++) { |
267 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { | 271 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) { |
268 same = 1; | 272 same = 1; |
269 break; | 273 break; |
270 } | 274 } |
271 } | 275 } |
272 if (same) | 276 if (same) |
273 continue; | 277 continue; |
274 for (k = 0; k < dim; k++) | 278 for (k = 0; k < dim; k++) |
275 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; | 279 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k]; |
276 curbits = ff_aac_spectral_bits[cb-1][curidx]; | 280 curbits = ff_aac_spectral_bits[cb-1][curidx]; |
277 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | 281 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; |
278 #else | 282 #else |
279 vec = ff_aac_codebook_vectors[cb-1]; | 283 vec = ff_aac_codebook_vectors[cb-1]; |
280 mincost = INFINITY; | 284 mincost = INFINITY; |
281 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { | 285 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) { |
282 float rd = 0.0f; | 286 float rd = 0.0f; |
283 int curbits = ff_aac_spectral_bits[cb-1][j]; | 287 int curbits = ff_aac_spectral_bits[cb-1][j]; |
284 int curidx = j; | 288 int curidx = j; |
285 #endif /* USE_REALLY_FULL_SEARCH */ | 289 #endif /* USE_REALLY_FULL_SEARCH */ |
286 if (IS_CODEBOOK_UNSIGNED(cb)) { | 290 if (IS_CODEBOOK_UNSIGNED(cb)) { |
287 for (k = 0; k < dim; k++) { | 291 for (k = 0; k < dim; k++) { |
288 float t = fabsf(in[i+k]); | 292 float t = fabsf(in[i+k]); |
289 float di; | 293 float di; |
290 //do not code with escape sequence small values | 294 //do not code with escape sequence small values |
291 if (vec[k] == 64.0f && t < 39.0f*IQ) { | 295 if (vec[k] == 64.0f && t < 39.0f*IQ) { |
292 rd = INFINITY; | 296 rd = INFINITY; |
293 break; | 297 break; |
294 } | 298 } |
295 if (vec[k] == 64.0f) {//FIXME: slow | 299 if (vec[k] == 64.0f) { //FIXME: slow |
296 if (t >= CLIPPED_ESCAPE) { | 300 if (t >= CLIPPED_ESCAPE) { |
297 di = t - CLIPPED_ESCAPE; | 301 di = t - CLIPPED_ESCAPE; |
298 curbits += 21; | 302 curbits += 21; |
299 } else { | 303 } else { |
300 int c = av_clip(quant(t, Q), 0, 8191); | 304 int c = av_clip(quant(t, Q), 0, 8191); |
315 } | 319 } |
316 } | 320 } |
317 rd += curbits; | 321 rd += curbits; |
318 if (rd < mincost) { | 322 if (rd < mincost) { |
319 mincost = rd; | 323 mincost = rd; |
320 minidx = curidx; | 324 minidx = curidx; |
321 minbits = curbits; | 325 minbits = curbits; |
322 } | 326 } |
323 } | 327 } |
324 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]); | 328 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]); |
325 if (IS_CODEBOOK_UNSIGNED(cb)) | 329 if (IS_CODEBOOK_UNSIGNED(cb)) |
358 int win, int group_len, const float lambda) | 362 int win, int group_len, const float lambda) |
359 { | 363 { |
360 BandCodingPath path[120][12]; | 364 BandCodingPath path[120][12]; |
361 int w, swb, cb, start, start2, size; | 365 int w, swb, cb, start, start2, size; |
362 int i, j; | 366 int i, j; |
363 const int max_sfb = sce->ics.max_sfb; | 367 const int max_sfb = sce->ics.max_sfb; |
364 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3; | 368 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3; |
365 const int run_esc = (1 << run_bits) - 1; | 369 const int run_esc = (1 << run_bits) - 1; |
366 int idx, ppos, count; | 370 int idx, ppos, count; |
367 int stackrun[120], stackcb[120], stack_len; | 371 int stackrun[120], stackcb[120], stack_len; |
368 float next_minrd = INFINITY; | 372 float next_minrd = INFINITY; |
369 int next_mincb = 0; | 373 int next_mincb = 0; |
370 | 374 |
371 abs_pow34_v(s->scoefs, sce->coeffs, 1024); | 375 abs_pow34_v(s->scoefs, sce->coeffs, 1024); |
372 start = win*128; | 376 start = win*128; |
373 for (cb = 0; cb < 12; cb++) { | 377 for (cb = 0; cb < 12; cb++) { |
374 path[0][cb].cost = 0.0f; | 378 path[0][cb].cost = 0.0f; |
375 path[0][cb].prev_idx = -1; | 379 path[0][cb].prev_idx = -1; |
376 path[0][cb].run = 0; | 380 path[0][cb].run = 0; |
377 } | 381 } |
378 for (swb = 0; swb < max_sfb; swb++) { | 382 for (swb = 0; swb < max_sfb; swb++) { |
379 start2 = start; | 383 start2 = start; |
380 size = sce->ics.swb_sizes[swb]; | 384 size = sce->ics.swb_sizes[swb]; |
381 if (sce->zeroes[win*16 + swb]) { | 385 if (sce->zeroes[win*16 + swb]) { |
382 for (cb = 0; cb < 12; cb++) { | 386 for (cb = 0; cb < 12; cb++) { |
383 path[swb+1][cb].prev_idx = cb; | 387 path[swb+1][cb].prev_idx = cb; |
384 path[swb+1][cb].cost = path[swb][cb].cost; | 388 path[swb+1][cb].cost = path[swb][cb].cost; |
385 path[swb+1][cb].run = path[swb][cb].run + 1; | 389 path[swb+1][cb].run = path[swb][cb].run + 1; |
386 } | 390 } |
387 } else { | 391 } else { |
388 float minrd = next_minrd; | 392 float minrd = next_minrd; |
389 int mincb = next_mincb; | 393 int mincb = next_mincb; |
390 next_minrd = INFINITY; | 394 next_minrd = INFINITY; |
400 lambda / band->threshold, INFINITY, NULL); | 404 lambda / band->threshold, INFINITY, NULL); |
401 } | 405 } |
402 cost_stay_here = path[swb][cb].cost + rd; | 406 cost_stay_here = path[swb][cb].cost + rd; |
403 cost_get_here = minrd + rd + run_bits + 4; | 407 cost_get_here = minrd + rd + run_bits + 4; |
404 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run] | 408 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run] |
405 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1]) | 409 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1]) |
406 cost_stay_here += run_bits; | 410 cost_stay_here += run_bits; |
407 if (cost_get_here < cost_stay_here) { | 411 if (cost_get_here < cost_stay_here) { |
408 path[swb+1][cb].prev_idx = mincb; | 412 path[swb+1][cb].prev_idx = mincb; |
409 path[swb+1][cb].cost = cost_get_here; | 413 path[swb+1][cb].cost = cost_get_here; |
410 path[swb+1][cb].run = 1; | 414 path[swb+1][cb].run = 1; |
422 start += sce->ics.swb_sizes[swb]; | 426 start += sce->ics.swb_sizes[swb]; |
423 } | 427 } |
424 | 428 |
425 //convert resulting path from backward-linked list | 429 //convert resulting path from backward-linked list |
426 stack_len = 0; | 430 stack_len = 0; |
427 idx = 0; | 431 idx = 0; |
428 for (cb = 1; cb < 12; cb++) { | 432 for (cb = 1; cb < 12; cb++) { |
429 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost) | 433 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost) |
430 idx = cb; | 434 idx = cb; |
431 } | 435 } |
432 ppos = max_sfb; | 436 ppos = max_sfb; |
433 while(ppos > 0) { | 437 while (ppos > 0) { |
434 cb = idx; | 438 cb = idx; |
435 stackrun[stack_len] = path[ppos][cb].run; | 439 stackrun[stack_len] = path[ppos][cb].run; |
436 stackcb [stack_len] = cb; | 440 stackcb [stack_len] = cb; |
437 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx; | 441 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx; |
438 ppos -= path[ppos][cb].run; | 442 ppos -= path[ppos][cb].run; |
447 //XXX: memset when band_type is also uint8_t | 451 //XXX: memset when band_type is also uint8_t |
448 for (j = 0; j < count; j++) { | 452 for (j = 0; j < count; j++) { |
449 sce->band_type[win*16 + start] = stackcb[i]; | 453 sce->band_type[win*16 + start] = stackcb[i]; |
450 start++; | 454 start++; |
451 } | 455 } |
452 while(count >= run_esc) { | 456 while (count >= run_esc) { |
453 put_bits(&s->pb, run_bits, run_esc); | 457 put_bits(&s->pb, run_bits, run_esc); |
454 count -= run_esc; | 458 count -= run_esc; |
455 } | 459 } |
456 put_bits(&s->pb, run_bits, count); | 460 put_bits(&s->pb, run_bits, count); |
457 } | 461 } |
458 } | 462 } |
459 | 463 |
460 static void encode_window_bands_info_fixed(AACEncContext *s, SingleChannelElement *sce, | 464 static void encode_window_bands_info_fixed(AACEncContext *s, |
461 int win, int group_len, const float lambda) | 465 SingleChannelElement *sce, |
466 int win, int group_len, | |
467 const float lambda) | |
462 { | 468 { |
463 encode_window_bands_info(s, sce, win, group_len, 1.0f); | 469 encode_window_bands_info(s, sce, win, group_len, 1.0f); |
464 } | 470 } |
465 | 471 |
466 | 472 |
470 int min_val; | 476 int min_val; |
471 int max_val; | 477 int max_val; |
472 } TrellisPath; | 478 } TrellisPath; |
473 | 479 |
474 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, | 480 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, |
475 SingleChannelElement *sce, const float lambda) | 481 SingleChannelElement *sce, |
482 const float lambda) | |
476 { | 483 { |
477 int q, w, w2, g, start = 0; | 484 int q, w, w2, g, start = 0; |
478 int i; | 485 int i; |
479 int idx; | 486 int idx; |
480 TrellisPath paths[256*121]; | 487 TrellisPath paths[256*121]; |
481 int bandaddr[121]; | 488 int bandaddr[121]; |
482 int minq; | 489 int minq; |
483 float mincost; | 490 float mincost; |
484 | 491 |
485 for (i = 0; i < 256; i++) { | 492 for (i = 0; i < 256; i++) { |
486 paths[i].cost = 0.0f; | 493 paths[i].cost = 0.0f; |
487 paths[i].prev = -1; | 494 paths[i].prev = -1; |
488 paths[i].min_val = i; | 495 paths[i].min_val = i; |
489 paths[i].max_val = i; | 496 paths[i].max_val = i; |
490 } | 497 } |
491 for (i = 256; i < 256*121; i++) { | 498 for (i = 256; i < 256*121; i++) { |
492 paths[i].cost = INFINITY; | 499 paths[i].cost = INFINITY; |
493 paths[i].prev = -2; | 500 paths[i].prev = -2; |
494 paths[i].min_val = INT_MAX; | 501 paths[i].min_val = INT_MAX; |
495 paths[i].max_val = 0; | 502 paths[i].max_val = 0; |
496 } | 503 } |
497 idx = 256; | 504 idx = 256; |
498 abs_pow34_v(s->scoefs, sce->coeffs, 1024); | 505 abs_pow34_v(s->scoefs, sce->coeffs, 1024); |
501 for (g = 0; g < sce->ics.num_swb; g++) { | 508 for (g = 0; g < sce->ics.num_swb; g++) { |
502 const float *coefs = sce->coeffs + start; | 509 const float *coefs = sce->coeffs + start; |
503 float qmin, qmax; | 510 float qmin, qmax; |
504 int nz = 0; | 511 int nz = 0; |
505 | 512 |
506 bandaddr[idx >> 8] = w*16+g; | 513 bandaddr[idx >> 8] = w * 16 + g; |
507 qmin = INT_MAX; | 514 qmin = INT_MAX; |
508 qmax = 0.0f; | 515 qmax = 0.0f; |
509 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { | 516 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
510 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g]; | 517 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g]; |
511 if (band->energy <= band->threshold || band->threshold == 0.0f) { | 518 if (band->energy <= band->threshold || band->threshold == 0.0f) { |
551 cost = paths[idx - 256 + i].cost + dist | 558 cost = paths[idx - 256 + i].cost + dist |
552 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO]; | 559 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO]; |
553 minv = FFMIN(paths[idx - 256 + i].min_val, q); | 560 minv = FFMIN(paths[idx - 256 + i].min_val, q); |
554 maxv = FFMAX(paths[idx - 256 + i].max_val, q); | 561 maxv = FFMAX(paths[idx - 256 + i].max_val, q); |
555 if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) { | 562 if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) { |
556 paths[idx + q].cost = cost; | 563 paths[idx + q].cost = cost; |
557 paths[idx + q].prev = idx - 256 + i; | 564 paths[idx + q].prev = idx - 256 + i; |
558 paths[idx + q].min_val = minv; | 565 paths[idx + q].min_val = minv; |
559 paths[idx + q].max_val = maxv; | 566 paths[idx + q].max_val = maxv; |
560 } | 567 } |
561 } | 568 } |
562 } | 569 } |
576 continue; | 583 continue; |
577 cost = paths[idx - 256 + i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO]; | 584 cost = paths[idx - 256 + i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO]; |
578 minv = FFMIN(paths[idx - 256 + i].min_val, q); | 585 minv = FFMIN(paths[idx - 256 + i].min_val, q); |
579 maxv = FFMAX(paths[idx - 256 + i].max_val, q); | 586 maxv = FFMAX(paths[idx - 256 + i].max_val, q); |
580 if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) { | 587 if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) { |
581 paths[idx + q].cost = cost; | 588 paths[idx + q].cost = cost; |
582 paths[idx + q].prev = idx - 256 + i; | 589 paths[idx + q].prev = idx - 256 + i; |
583 paths[idx + q].min_val = minv; | 590 paths[idx + q].min_val = minv; |
584 paths[idx + q].max_val = maxv; | 591 paths[idx + q].max_val = maxv; |
585 } | 592 } |
586 } | 593 } |
587 } | 594 } |
588 } | 595 } |
589 sce->zeroes[w*16+g] = !nz; | 596 sce->zeroes[w*16+g] = !nz; |
590 start += sce->ics.swb_sizes[g]; | 597 start += sce->ics.swb_sizes[g]; |
591 idx += 256; | 598 idx += 256; |
592 } | 599 } |
593 } | 600 } |
594 idx -= 256; | 601 idx -= 256; |
595 mincost = paths[idx].cost; | 602 mincost = paths[idx].cost; |
596 minq = idx; | 603 minq = idx; |
597 for (i = 1; i < 256; i++) { | 604 for (i = 1; i < 256; i++) { |
598 if (paths[idx + i].cost < mincost) { | 605 if (paths[idx + i].cost < mincost) { |
599 mincost = paths[idx + i].cost; | 606 mincost = paths[idx + i].cost; |
600 minq = idx + i; | 607 minq = idx + i; |
601 } | 608 } |
602 } | 609 } |
603 while(minq >= 256) { | 610 while (minq >= 256) { |
604 sce->sf_idx[bandaddr[minq>>8]] = minq & 0xFF; | 611 sce->sf_idx[bandaddr[minq>>8]] = minq & 0xFF; |
605 minq = paths[minq].prev; | 612 minq = paths[minq].prev; |
606 } | 613 } |
607 //set the same quantizers inside window groups | 614 //set the same quantizers inside window groups |
608 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) | 615 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) |
612 } | 619 } |
613 | 620 |
614 /** | 621 /** |
615 * two-loop quantizers search taken from ISO 13818-7 Appendix C | 622 * two-loop quantizers search taken from ISO 13818-7 Appendix C |
616 */ | 623 */ |
617 static void search_for_quantizers_twoloop(AVCodecContext *avctx, AACEncContext *s, | 624 static void search_for_quantizers_twoloop(AVCodecContext *avctx, |
618 SingleChannelElement *sce, const float lambda) | 625 AACEncContext *s, |
626 SingleChannelElement *sce, | |
627 const float lambda) | |
619 { | 628 { |
620 int start = 0, i, w, w2, g; | 629 int start = 0, i, w, w2, g; |
621 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels; | 630 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels; |
622 float dists[128], uplims[128]; | 631 float dists[128], uplims[128]; |
623 int fflag, minscaler; | 632 int fflag, minscaler; |
624 int its = 0; | 633 int its = 0; |
625 int allz = 0; | 634 int allz = 0; |
626 float minthr = INFINITY; | 635 float minthr = INFINITY; |
627 | 636 |
628 //XXX: some heuristic to determine initial quantizers will reduce search time | 637 //XXX: some heuristic to determine initial quantizers will reduce search time |
629 memset(dists, 0, sizeof(dists)); | 638 memset(dists, 0, sizeof(dists)); |
661 if (!allz) | 670 if (!allz) |
662 return; | 671 return; |
663 abs_pow34_v(s->scoefs, sce->coeffs, 1024); | 672 abs_pow34_v(s->scoefs, sce->coeffs, 1024); |
664 //perform two-loop search | 673 //perform two-loop search |
665 //outer loop - improve quality | 674 //outer loop - improve quality |
666 do{ | 675 do { |
667 int tbits, qstep; | 676 int tbits, qstep; |
668 minscaler = sce->sf_idx[0]; | 677 minscaler = sce->sf_idx[0]; |
669 //inner loop - quantize spectrum to fit into given number of bits | 678 //inner loop - quantize spectrum to fit into given number of bits |
670 qstep = its ? 1 : 32; | 679 qstep = its ? 1 : 32; |
671 do{ | 680 do { |
672 int prev = -1; | 681 int prev = -1; |
673 tbits = 0; | 682 tbits = 0; |
674 fflag = 0; | 683 fflag = 0; |
675 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { | 684 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
676 start = w*128; | 685 start = w*128; |
730 } | 739 } |
731 qstep >>= 1; | 740 qstep >>= 1; |
732 if (!qstep && tbits > destbits*1.02) | 741 if (!qstep && tbits > destbits*1.02) |
733 qstep = 1; | 742 qstep = 1; |
734 if (sce->sf_idx[0] >= 217)break; | 743 if (sce->sf_idx[0] >= 217)break; |
735 }while(qstep); | 744 } while (qstep); |
736 | 745 |
737 fflag = 0; | 746 fflag = 0; |
738 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF); | 747 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF); |
739 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { | 748 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
740 start = w*128; | 749 start = w*128; |
747 if (sce->sf_idx[w*16+g] != prevsc) | 756 if (sce->sf_idx[w*16+g] != prevsc) |
748 fflag = 1; | 757 fflag = 1; |
749 } | 758 } |
750 } | 759 } |
751 its++; | 760 its++; |
752 }while(fflag && its < 10); | 761 } while (fflag && its < 10); |
753 } | 762 } |
754 | 763 |
755 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, | 764 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s, |
756 SingleChannelElement *sce, const float lambda) | 765 SingleChannelElement *sce, |
766 const float lambda) | |
757 { | 767 { |
758 int start = 0, i, w, w2, g; | 768 int start = 0, i, w, w2, g; |
759 float uplim[128], maxq[128]; | 769 float uplim[128], maxq[128]; |
760 int minq, maxsf; | 770 int minq, maxsf; |
761 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda; | 771 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda; |
799 return; | 809 return; |
800 } | 810 } |
801 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { | 811 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
802 start = w*128; | 812 start = w*128; |
803 for (g = 0; g < sce->ics.num_swb; g++) { | 813 for (g = 0; g < sce->ics.num_swb; g++) { |
804 float *coefs = sce->coeffs + start; | 814 float *coefs = sce->coeffs + start; |
805 const int size = sce->ics.swb_sizes[g]; | 815 const int size = sce->ics.swb_sizes[g]; |
806 int start2 = start, end2 = start + size, peakpos = start; | 816 int start2 = start, end2 = start + size, peakpos = start; |
807 float maxval = -1, thr = 0.0f, t; | 817 float maxval = -1, thr = 0.0f, t; |
808 maxq[w*16+g] = 0.0f; | 818 maxq[w*16+g] = 0.0f; |
809 if (g > lastband) { | 819 if (g > lastband) { |
817 for (i = 0; i < size; i++) { | 827 for (i = 0; i < size; i++) { |
818 float t = coefs[w2*128+i]*coefs[w2*128+i]; | 828 float t = coefs[w2*128+i]*coefs[w2*128+i]; |
819 maxq[w*16+g] = fmaxf(maxq[w*16+g], fabsf(coefs[w2*128 + i])); | 829 maxq[w*16+g] = fmaxf(maxq[w*16+g], fabsf(coefs[w2*128 + i])); |
820 thr += t; | 830 thr += t; |
821 if (sce->ics.num_windows == 1 && maxval < t) { | 831 if (sce->ics.num_windows == 1 && maxval < t) { |
822 maxval = t; | 832 maxval = t; |
823 peakpos = start+i; | 833 peakpos = start+i; |
824 } | 834 } |
825 } | 835 } |
826 } | 836 } |
827 if (sce->ics.num_windows == 1) { | 837 if (sce->ics.num_windows == 1) { |
831 start2 -= start; | 841 start2 -= start; |
832 end2 -= start; | 842 end2 -= start; |
833 } | 843 } |
834 start += size; | 844 start += size; |
835 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband); | 845 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband); |
836 t = 1.0 - (1.0 * start2 / last); | 846 t = 1.0 - (1.0 * start2 / last); |
837 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075); | 847 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075); |
838 } | 848 } |
839 } | 849 } |
840 memset(sce->sf_idx, 0, sizeof(sce->sf_idx)); | 850 memset(sce->sf_idx, 0, sizeof(sce->sf_idx)); |
841 abs_pow34_v(s->scoefs, sce->coeffs, 1024); | 851 abs_pow34_v(s->scoefs, sce->coeffs, 1024); |
842 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { | 852 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
843 start = w*128; | 853 start = w*128; |
844 for (g = 0; g < sce->ics.num_swb; g++) { | 854 for (g = 0; g < sce->ics.num_swb; g++) { |
845 const float *coefs = sce->coeffs + start; | 855 const float *coefs = sce->coeffs + start; |
846 const float *scaled = s->scoefs + start; | 856 const float *scaled = s->scoefs + start; |
847 const int size = sce->ics.swb_sizes[g]; | 857 const int size = sce->ics.swb_sizes[g]; |
848 int scf, prev_scf, step; | 858 int scf, prev_scf, step; |
849 int min_scf = 0, max_scf = 255; | 859 int min_scf = 0, max_scf = 255; |
850 float curdiff; | 860 float curdiff; |
851 if (maxq[w*16+g] < 21.544) { | 861 if (maxq[w*16+g] < 21.544) { |
852 sce->zeroes[w*16+g] = 1; | 862 sce->zeroes[w*16+g] = 1; |
853 start += size; | 863 start += size; |
854 continue; | 864 continue; |
855 } | 865 } |
856 sce->zeroes[w*16+g] = 0; | 866 sce->zeroes[w*16+g] = 0; |
857 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218); | 867 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218); |
858 step = 16; | 868 step = 16; |
859 for (;;) { | 869 for (;;) { |
860 float dist = 0.0f; | 870 float dist = 0.0f; |
861 int quant_max; | 871 int quant_max; |
862 | 872 |
915 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf); | 925 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf); |
916 } | 926 } |
917 } | 927 } |
918 | 928 |
919 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, | 929 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, |
920 SingleChannelElement *sce, const float lambda) | 930 SingleChannelElement *sce, |
931 const float lambda) | |
921 { | 932 { |
922 int start = 0, i, w, w2, g; | 933 int start = 0, i, w, w2, g; |
923 int minq = 255; | 934 int minq = 255; |
924 | 935 |
925 memset(sce->sf_idx, 0, sizeof(sce->sf_idx)); | 936 memset(sce->sf_idx, 0, sizeof(sce->sf_idx)); |
947 for (g = 0; g < sce->ics.num_swb; g++) | 958 for (g = 0; g < sce->ics.num_swb; g++) |
948 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) | 959 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) |
949 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g]; | 960 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g]; |
950 } | 961 } |
951 | 962 |
952 static void search_for_ms(AACEncContext *s, ChannelElement *cpe, const float lambda) | 963 static void search_for_ms(AACEncContext *s, ChannelElement *cpe, |
964 const float lambda) | |
953 { | 965 { |
954 int start = 0, i, w, w2, g; | 966 int start = 0, i, w, w2, g; |
955 float M[128], S[128]; | 967 float M[128], S[128]; |
956 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3; | 968 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3; |
957 SingleChannelElement *sce0 = &cpe->ch[0]; | 969 SingleChannelElement *sce0 = &cpe->ch[0]; |
967 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g]; | 979 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g]; |
968 float minthr = fminf(band0->threshold, band1->threshold); | 980 float minthr = fminf(band0->threshold, band1->threshold); |
969 float maxthr = fmaxf(band0->threshold, band1->threshold); | 981 float maxthr = fmaxf(band0->threshold, band1->threshold); |
970 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { | 982 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { |
971 M[i] = (sce0->coeffs[start+w2*128+i] | 983 M[i] = (sce0->coeffs[start+w2*128+i] |
972 + sce1->coeffs[start+w2*128+i])*0.5; | 984 + sce1->coeffs[start+w2*128+i]) * 0.5; |
973 S[i] = sce0->coeffs[start+w2*128+i] | 985 S[i] = sce0->coeffs[start+w2*128+i] |
974 - sce1->coeffs[start+w2*128+i]; | 986 - sce1->coeffs[start+w2*128+i]; |
975 } | 987 } |
976 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]); | 988 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]); |
977 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]); | 989 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]); |