15fe4307327a0434a74ad690d45c8159db3840e2
[libav.git] / libavcodec / aaccoder.c
1 /*
2 * AAC coefficients encoder
3 * Copyright (C) 2008-2009 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * AAC coefficients encoder
25 */
26
27 /***********************************
28 * TODOs:
29 * speedup quantizer selection
30 * add sane pulse detection
31 ***********************************/
32
33 #include <float.h>
34 #include "avcodec.h"
35 #include "put_bits.h"
36 #include "aac.h"
37 #include "aacenc.h"
38 #include "aactab.h"
39 #include "libavutil/libm.h"
40
41 /** bits needed to code codebook run value for long windows */
42 static const uint8_t run_value_bits_long[64] = {
43 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
44 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
45 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
46 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
47 };
48
49 /** bits needed to code codebook run value for short windows */
50 static const uint8_t run_value_bits_short[16] = {
51 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
52 };
53
54 static const uint8_t *run_value_bits[2] = {
55 run_value_bits_long, run_value_bits_short
56 };
57
58
59 /**
60 * Quantize one coefficient.
61 * @return absolute value of the quantized coefficient
62 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
63 */
64 static av_always_inline int quant(float coef, const float Q)
65 {
66 float a = coef * Q;
67 return sqrtf(a * sqrtf(a)) + 0.4054;
68 }
69
70 static void quantize_bands(int *out, const float *in, const float *scaled,
71 int size, float Q34, int is_signed, int maxval)
72 {
73 int i;
74 double qc;
75 for (i = 0; i < size; i++) {
76 qc = scaled[i] * Q34;
77 out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
78 if (is_signed && in[i] < 0.0f) {
79 out[i] = -out[i];
80 }
81 }
82 }
83
84 static void abs_pow34_v(float *out, const float *in, const int size)
85 {
86 #ifndef USE_REALLY_FULL_SEARCH
87 int i;
88 for (i = 0; i < size; i++) {
89 float a = fabsf(in[i]);
90 out[i] = sqrtf(a * sqrtf(a));
91 }
92 #endif /* USE_REALLY_FULL_SEARCH */
93 }
94
95 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
96 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
97
98 /**
99 * Calculate rate distortion cost for quantizing with given codebook
100 *
101 * @return quantization distortion
102 */
103 static av_always_inline float quantize_and_encode_band_cost_template(
104 struct AACEncContext *s,
105 PutBitContext *pb, const float *in,
106 const float *scaled, int size, int scale_idx,
107 int cb, const float lambda, const float uplim,
108 int *bits, int BT_ZERO, int BT_UNSIGNED,
109 int BT_PAIR, int BT_ESC)
110 {
111 const float IQ = ff_aac_pow2sf_tab[POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
112 const float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
113 const float CLIPPED_ESCAPE = 165140.0f*IQ;
114 int i, j;
115 float cost = 0;
116 const int dim = BT_PAIR ? 2 : 4;
117 int resbits = 0;
118 const float Q34 = sqrtf(Q * sqrtf(Q));
119 const int range = aac_cb_range[cb];
120 const int maxval = aac_cb_maxval[cb];
121 int off;
122
123 if (BT_ZERO) {
124 for (i = 0; i < size; i++)
125 cost += in[i]*in[i];
126 if (bits)
127 *bits = 0;
128 return cost * lambda;
129 }
130 if (!scaled) {
131 abs_pow34_v(s->scoefs, in, size);
132 scaled = s->scoefs;
133 }
134 quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
135 if (BT_UNSIGNED) {
136 off = 0;
137 } else {
138 off = maxval;
139 }
140 for (i = 0; i < size; i += dim) {
141 const float *vec;
142 int *quants = s->qcoefs + i;
143 int curidx = 0;
144 int curbits;
145 float rd = 0.0f;
146 for (j = 0; j < dim; j++) {
147 curidx *= range;
148 curidx += quants[j] + off;
149 }
150 curbits = ff_aac_spectral_bits[cb-1][curidx];
151 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
152 if (BT_UNSIGNED) {
153 for (j = 0; j < dim; j++) {
154 float t = fabsf(in[i+j]);
155 float di;
156 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
157 if (t >= CLIPPED_ESCAPE) {
158 di = t - CLIPPED_ESCAPE;
159 curbits += 21;
160 } else {
161 int c = av_clip(quant(t, Q), 0, 8191);
162 di = t - c*cbrtf(c)*IQ;
163 curbits += av_log2(c)*2 - 4 + 1;
164 }
165 } else {
166 di = t - vec[j]*IQ;
167 }
168 if (vec[j] != 0.0f)
169 curbits++;
170 rd += di*di;
171 }
172 } else {
173 for (j = 0; j < dim; j++) {
174 float di = in[i+j] - vec[j]*IQ;
175 rd += di*di;
176 }
177 }
178 cost += rd * lambda + curbits;
179 resbits += curbits;
180 if (cost >= uplim)
181 return uplim;
182 if (pb) {
183 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
184 if (BT_UNSIGNED)
185 for (j = 0; j < dim; j++)
186 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
187 put_bits(pb, 1, in[i+j] < 0.0f);
188 if (BT_ESC) {
189 for (j = 0; j < 2; j++) {
190 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
191 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
192 int len = av_log2(coef);
193
194 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
195 put_bits(pb, len, coef & ((1 << len) - 1));
196 }
197 }
198 }
199 }
200 }
201
202 if (bits)
203 *bits = resbits;
204 return cost;
205 }
206
207 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
208 static float quantize_and_encode_band_cost_ ## NAME( \
209 struct AACEncContext *s, \
210 PutBitContext *pb, const float *in, \
211 const float *scaled, int size, int scale_idx, \
212 int cb, const float lambda, const float uplim, \
213 int *bits) { \
214 return quantize_and_encode_band_cost_template( \
215 s, pb, in, scaled, size, scale_idx, \
216 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
217 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \
218 }
219
220 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0)
221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0)
222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0)
223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0)
224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0)
225 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1)
226
227 static float (*const quantize_and_encode_band_cost_arr[])(
228 struct AACEncContext *s,
229 PutBitContext *pb, const float *in,
230 const float *scaled, int size, int scale_idx,
231 int cb, const float lambda, const float uplim,
232 int *bits) = {
233 quantize_and_encode_band_cost_ZERO,
234 quantize_and_encode_band_cost_SQUAD,
235 quantize_and_encode_band_cost_SQUAD,
236 quantize_and_encode_band_cost_UQUAD,
237 quantize_and_encode_band_cost_UQUAD,
238 quantize_and_encode_band_cost_SPAIR,
239 quantize_and_encode_band_cost_SPAIR,
240 quantize_and_encode_band_cost_UPAIR,
241 quantize_and_encode_band_cost_UPAIR,
242 quantize_and_encode_band_cost_UPAIR,
243 quantize_and_encode_band_cost_UPAIR,
244 quantize_and_encode_band_cost_ESC,
245 };
246
247 #define quantize_and_encode_band_cost( \
248 s, pb, in, scaled, size, scale_idx, cb, \
249 lambda, uplim, bits) \
250 quantize_and_encode_band_cost_arr[cb]( \
251 s, pb, in, scaled, size, scale_idx, cb, \
252 lambda, uplim, bits)
253
254 static float quantize_band_cost(struct AACEncContext *s, const float *in,
255 const float *scaled, int size, int scale_idx,
256 int cb, const float lambda, const float uplim,
257 int *bits)
258 {
259 return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
260 cb, lambda, uplim, bits);
261 }
262
263 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
264 const float *in, int size, int scale_idx,
265 int cb, const float lambda)
266 {
267 quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
268 INFINITY, NULL);
269 }
270
271 static float find_max_val(int group_len, int swb_size, const float *scaled) {
272 float maxval = 0.0f;
273 int w2, i;
274 for (w2 = 0; w2 < group_len; w2++) {
275 for (i = 0; i < swb_size; i++) {
276 maxval = FFMAX(maxval, scaled[w2*128+i]);
277 }
278 }
279 return maxval;
280 }
281
282 static int find_min_book(float maxval, int sf) {
283 float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
284 float Q34 = sqrtf(Q * sqrtf(Q));
285 int qmaxval, cb;
286 qmaxval = maxval * Q34 + 0.4054f;
287 if (qmaxval == 0) cb = 0;
288 else if (qmaxval == 1) cb = 1;
289 else if (qmaxval == 2) cb = 3;
290 else if (qmaxval <= 4) cb = 5;
291 else if (qmaxval <= 7) cb = 7;
292 else if (qmaxval <= 12) cb = 9;
293 else cb = 11;
294 return cb;
295 }
296
297 /**
298 * structure used in optimal codebook search
299 */
300 typedef struct BandCodingPath {
301 int prev_idx; ///< pointer to the previous path point
302 float cost; ///< path cost
303 int run;
304 } BandCodingPath;
305
306 /**
307 * Encode band info for single window group bands.
308 */
309 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
310 int win, int group_len, const float lambda)
311 {
312 BandCodingPath path[120][12];
313 int w, swb, cb, start, start2, size;
314 int i, j;
315 const int max_sfb = sce->ics.max_sfb;
316 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
317 const int run_esc = (1 << run_bits) - 1;
318 int idx, ppos, count;
319 int stackrun[120], stackcb[120], stack_len;
320 float next_minrd = INFINITY;
321 int next_mincb = 0;
322
323 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
324 start = win*128;
325 for (cb = 0; cb < 12; cb++) {
326 path[0][cb].cost = 0.0f;
327 path[0][cb].prev_idx = -1;
328 path[0][cb].run = 0;
329 }
330 for (swb = 0; swb < max_sfb; swb++) {
331 start2 = start;
332 size = sce->ics.swb_sizes[swb];
333 if (sce->zeroes[win*16 + swb]) {
334 for (cb = 0; cb < 12; cb++) {
335 path[swb+1][cb].prev_idx = cb;
336 path[swb+1][cb].cost = path[swb][cb].cost;
337 path[swb+1][cb].run = path[swb][cb].run + 1;
338 }
339 } else {
340 float minrd = next_minrd;
341 int mincb = next_mincb;
342 next_minrd = INFINITY;
343 next_mincb = 0;
344 for (cb = 0; cb < 12; cb++) {
345 float cost_stay_here, cost_get_here;
346 float rd = 0.0f;
347 for (w = 0; w < group_len; w++) {
348 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
349 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
350 s->scoefs + start + w*128, size,
351 sce->sf_idx[(win+w)*16+swb], cb,
352 lambda / band->threshold, INFINITY, NULL);
353 }
354 cost_stay_here = path[swb][cb].cost + rd;
355 cost_get_here = minrd + rd + run_bits + 4;
356 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
357 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
358 cost_stay_here += run_bits;
359 if (cost_get_here < cost_stay_here) {
360 path[swb+1][cb].prev_idx = mincb;
361 path[swb+1][cb].cost = cost_get_here;
362 path[swb+1][cb].run = 1;
363 } else {
364 path[swb+1][cb].prev_idx = cb;
365 path[swb+1][cb].cost = cost_stay_here;
366 path[swb+1][cb].run = path[swb][cb].run + 1;
367 }
368 if (path[swb+1][cb].cost < next_minrd) {
369 next_minrd = path[swb+1][cb].cost;
370 next_mincb = cb;
371 }
372 }
373 }
374 start += sce->ics.swb_sizes[swb];
375 }
376
377 //convert resulting path from backward-linked list
378 stack_len = 0;
379 idx = 0;
380 for (cb = 1; cb < 12; cb++)
381 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
382 idx = cb;
383 ppos = max_sfb;
384 while (ppos > 0) {
385 cb = idx;
386 stackrun[stack_len] = path[ppos][cb].run;
387 stackcb [stack_len] = cb;
388 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
389 ppos -= path[ppos][cb].run;
390 stack_len++;
391 }
392 //perform actual band info encoding
393 start = 0;
394 for (i = stack_len - 1; i >= 0; i--) {
395 put_bits(&s->pb, 4, stackcb[i]);
396 count = stackrun[i];
397 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
398 //XXX: memset when band_type is also uint8_t
399 for (j = 0; j < count; j++) {
400 sce->band_type[win*16 + start] = stackcb[i];
401 start++;
402 }
403 while (count >= run_esc) {
404 put_bits(&s->pb, run_bits, run_esc);
405 count -= run_esc;
406 }
407 put_bits(&s->pb, run_bits, count);
408 }
409 }
410
411 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
412 int win, int group_len, const float lambda)
413 {
414 BandCodingPath path[120][12];
415 int w, swb, cb, start, start2, size;
416 int i, j;
417 const int max_sfb = sce->ics.max_sfb;
418 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
419 const int run_esc = (1 << run_bits) - 1;
420 int idx, ppos, count;
421 int stackrun[120], stackcb[120], stack_len;
422 float next_minrd = INFINITY;
423 int next_mincb = 0;
424
425 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
426 start = win*128;
427 for (cb = 0; cb < 12; cb++) {
428 path[0][cb].cost = run_bits+4;
429 path[0][cb].prev_idx = -1;
430 path[0][cb].run = 0;
431 }
432 for (swb = 0; swb < max_sfb; swb++) {
433 start2 = start;
434 size = sce->ics.swb_sizes[swb];
435 if (sce->zeroes[win*16 + swb]) {
436 for (cb = 0; cb < 12; cb++) {
437 path[swb+1][cb].prev_idx = cb;
438 path[swb+1][cb].cost = path[swb][cb].cost;
439 path[swb+1][cb].run = path[swb][cb].run + 1;
440 }
441 } else {
442 float minrd = next_minrd;
443 int mincb = next_mincb;
444 int startcb = sce->band_type[win*16+swb];
445 next_minrd = INFINITY;
446 next_mincb = 0;
447 for (cb = 0; cb < startcb; cb++) {
448 path[swb+1][cb].cost = 61450;
449 path[swb+1][cb].prev_idx = -1;
450 path[swb+1][cb].run = 0;
451 }
452 for (cb = startcb; cb < 12; cb++) {
453 float cost_stay_here, cost_get_here;
454 float rd = 0.0f;
455 for (w = 0; w < group_len; w++) {
456 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
457 s->scoefs + start + w*128, size,
458 sce->sf_idx[(win+w)*16+swb], cb,
459 0, INFINITY, NULL);
460 }
461 cost_stay_here = path[swb][cb].cost + rd;
462 cost_get_here = minrd + rd + run_bits + 4;
463 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
464 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
465 cost_stay_here += run_bits;
466 if (cost_get_here < cost_stay_here) {
467 path[swb+1][cb].prev_idx = mincb;
468 path[swb+1][cb].cost = cost_get_here;
469 path[swb+1][cb].run = 1;
470 } else {
471 path[swb+1][cb].prev_idx = cb;
472 path[swb+1][cb].cost = cost_stay_here;
473 path[swb+1][cb].run = path[swb][cb].run + 1;
474 }
475 if (path[swb+1][cb].cost < next_minrd) {
476 next_minrd = path[swb+1][cb].cost;
477 next_mincb = cb;
478 }
479 }
480 }
481 start += sce->ics.swb_sizes[swb];
482 }
483
484 //convert resulting path from backward-linked list
485 stack_len = 0;
486 idx = 0;
487 for (cb = 1; cb < 12; cb++)
488 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
489 idx = cb;
490 ppos = max_sfb;
491 while (ppos > 0) {
492 assert(idx >= 0);
493 cb = idx;
494 stackrun[stack_len] = path[ppos][cb].run;
495 stackcb [stack_len] = cb;
496 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
497 ppos -= path[ppos][cb].run;
498 stack_len++;
499 }
500 //perform actual band info encoding
501 start = 0;
502 for (i = stack_len - 1; i >= 0; i--) {
503 put_bits(&s->pb, 4, stackcb[i]);
504 count = stackrun[i];
505 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
506 //XXX: memset when band_type is also uint8_t
507 for (j = 0; j < count; j++) {
508 sce->band_type[win*16 + start] = stackcb[i];
509 start++;
510 }
511 while (count >= run_esc) {
512 put_bits(&s->pb, run_bits, run_esc);
513 count -= run_esc;
514 }
515 put_bits(&s->pb, run_bits, count);
516 }
517 }
518
519 /** Return the minimum scalefactor where the quantized coef does not clip. */
520 static av_always_inline uint8_t coef2minsf(float coef) {
521 return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
522 }
523
524 /** Return the maximum scalefactor where the quantized coef is not zero. */
525 static av_always_inline uint8_t coef2maxsf(float coef) {
526 return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
527 }
528
529 typedef struct TrellisPath {
530 float cost;
531 int prev;
532 } TrellisPath;
533
534 #define TRELLIS_STAGES 121
535 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
536
537 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
538 SingleChannelElement *sce,
539 const float lambda)
540 {
541 int q, w, w2, g, start = 0;
542 int i, j;
543 int idx;
544 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
545 int bandaddr[TRELLIS_STAGES];
546 int minq;
547 float mincost;
548 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
549 int q0, q1, qcnt = 0;
550
551 for (i = 0; i < 1024; i++) {
552 float t = fabsf(sce->coeffs[i]);
553 if (t > 0.0f) {
554 q0f = FFMIN(q0f, t);
555 q1f = FFMAX(q1f, t);
556 qnrgf += t*t;
557 qcnt++;
558 }
559 }
560
561 if (!qcnt) {
562 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
563 memset(sce->zeroes, 1, sizeof(sce->zeroes));
564 return;
565 }
566
567 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
568 q0 = coef2minsf(q0f);
569 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
570 q1 = coef2maxsf(q1f);
571 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
572 if (q1 - q0 > 60) {
573 int q0low = q0;
574 int q1high = q1;
575 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
576 int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
577 q1 = qnrg + 30;
578 q0 = qnrg - 30;
579 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
580 if (q0 < q0low) {
581 q1 += q0low - q0;
582 q0 = q0low;
583 } else if (q1 > q1high) {
584 q0 -= q1 - q1high;
585 q1 = q1high;
586 }
587 }
588 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
589
590 for (i = 0; i < TRELLIS_STATES; i++) {
591 paths[0][i].cost = 0.0f;
592 paths[0][i].prev = -1;
593 }
594 for (j = 1; j < TRELLIS_STAGES; j++) {
595 for (i = 0; i < TRELLIS_STATES; i++) {
596 paths[j][i].cost = INFINITY;
597 paths[j][i].prev = -2;
598 }
599 }
600 idx = 1;
601 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
602 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
603 start = w*128;
604 for (g = 0; g < sce->ics.num_swb; g++) {
605 const float *coefs = sce->coeffs + start;
606 float qmin, qmax;
607 int nz = 0;
608
609 bandaddr[idx] = w * 16 + g;
610 qmin = INT_MAX;
611 qmax = 0.0f;
612 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
613 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
614 if (band->energy <= band->threshold || band->threshold == 0.0f) {
615 sce->zeroes[(w+w2)*16+g] = 1;
616 continue;
617 }
618 sce->zeroes[(w+w2)*16+g] = 0;
619 nz = 1;
620 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
621 float t = fabsf(coefs[w2*128+i]);
622 if (t > 0.0f)
623 qmin = FFMIN(qmin, t);
624 qmax = FFMAX(qmax, t);
625 }
626 }
627 if (nz) {
628 int minscale, maxscale;
629 float minrd = INFINITY;
630 float maxval;
631 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
632 minscale = coef2minsf(qmin);
633 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
634 maxscale = coef2maxsf(qmax);
635 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
636 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
637 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
638 for (q = minscale; q < maxscale; q++) {
639 float dist = 0;
640 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
641 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
642 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
643 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
644 q + q0, cb, lambda / band->threshold, INFINITY, NULL);
645 }
646 minrd = FFMIN(minrd, dist);
647
648 for (i = 0; i < q1 - q0; i++) {
649 float cost;
650 cost = paths[idx - 1][i].cost + dist
651 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
652 if (cost < paths[idx][q].cost) {
653 paths[idx][q].cost = cost;
654 paths[idx][q].prev = i;
655 }
656 }
657 }
658 } else {
659 for (q = 0; q < q1 - q0; q++) {
660 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
661 paths[idx][q].prev = q;
662 }
663 }
664 sce->zeroes[w*16+g] = !nz;
665 start += sce->ics.swb_sizes[g];
666 idx++;
667 }
668 }
669 idx--;
670 mincost = paths[idx][0].cost;
671 minq = 0;
672 for (i = 1; i < TRELLIS_STATES; i++) {
673 if (paths[idx][i].cost < mincost) {
674 mincost = paths[idx][i].cost;
675 minq = i;
676 }
677 }
678 while (idx) {
679 sce->sf_idx[bandaddr[idx]] = minq + q0;
680 minq = paths[idx][minq].prev;
681 idx--;
682 }
683 //set the same quantizers inside window groups
684 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
685 for (g = 0; g < sce->ics.num_swb; g++)
686 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
687 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
688 }
689
690 /**
691 * two-loop quantizers search taken from ISO 13818-7 Appendix C
692 */
693 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
694 AACEncContext *s,
695 SingleChannelElement *sce,
696 const float lambda)
697 {
698 int start = 0, i, w, w2, g;
699 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
700 float dists[128], uplims[128];
701 float maxvals[128];
702 int fflag, minscaler;
703 int its = 0;
704 int allz = 0;
705 float minthr = INFINITY;
706
707 //XXX: some heuristic to determine initial quantizers will reduce search time
708 memset(dists, 0, sizeof(dists));
709 //determine zero bands and upper limits
710 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
711 for (g = 0; g < sce->ics.num_swb; g++) {
712 int nz = 0;
713 float uplim = 0.0f;
714 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
715 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
716 uplim += band->threshold;
717 if (band->energy <= band->threshold || band->threshold == 0.0f) {
718 sce->zeroes[(w+w2)*16+g] = 1;
719 continue;
720 }
721 nz = 1;
722 }
723 uplims[w*16+g] = uplim *512;
724 sce->zeroes[w*16+g] = !nz;
725 if (nz)
726 minthr = FFMIN(minthr, uplim);
727 allz |= nz;
728 }
729 }
730 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
731 for (g = 0; g < sce->ics.num_swb; g++) {
732 if (sce->zeroes[w*16+g]) {
733 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
734 continue;
735 }
736 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
737 }
738 }
739
740 if (!allz)
741 return;
742 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
743
744 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
745 start = w*128;
746 for (g = 0; g < sce->ics.num_swb; g++) {
747 const float *scaled = s->scoefs + start;
748 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
749 start += sce->ics.swb_sizes[g];
750 }
751 }
752
753 //perform two-loop search
754 //outer loop - improve quality
755 do {
756 int tbits, qstep;
757 minscaler = sce->sf_idx[0];
758 //inner loop - quantize spectrum to fit into given number of bits
759 qstep = its ? 1 : 32;
760 do {
761 int prev = -1;
762 tbits = 0;
763 fflag = 0;
764 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
765 start = w*128;
766 for (g = 0; g < sce->ics.num_swb; g++) {
767 const float *coefs = sce->coeffs + start;
768 const float *scaled = s->scoefs + start;
769 int bits = 0;
770 int cb;
771 float dist = 0.0f;
772
773 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
774 start += sce->ics.swb_sizes[g];
775 continue;
776 }
777 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
778 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
779 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
780 int b;
781 dist += quantize_band_cost(s, coefs + w2*128,
782 scaled + w2*128,
783 sce->ics.swb_sizes[g],
784 sce->sf_idx[w*16+g],
785 cb,
786 1.0f,
787 INFINITY,
788 &b);
789 bits += b;
790 }
791 dists[w*16+g] = dist - bits;
792 if (prev != -1) {
793 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
794 }
795 tbits += bits;
796 start += sce->ics.swb_sizes[g];
797 prev = sce->sf_idx[w*16+g];
798 }
799 }
800 if (tbits > destbits) {
801 for (i = 0; i < 128; i++)
802 if (sce->sf_idx[i] < 218 - qstep)
803 sce->sf_idx[i] += qstep;
804 } else {
805 for (i = 0; i < 128; i++)
806 if (sce->sf_idx[i] > 60 - qstep)
807 sce->sf_idx[i] -= qstep;
808 }
809 qstep >>= 1;
810 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
811 qstep = 1;
812 } while (qstep);
813
814 fflag = 0;
815 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
816 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
817 for (g = 0; g < sce->ics.num_swb; g++) {
818 int prevsc = sce->sf_idx[w*16+g];
819 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
820 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
821 sce->sf_idx[w*16+g]--;
822 else //Try to make sure there is some energy in every band
823 sce->sf_idx[w*16+g]-=2;
824 }
825 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
826 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
827 if (sce->sf_idx[w*16+g] != prevsc)
828 fflag = 1;
829 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
830 }
831 }
832 its++;
833 } while (fflag && its < 10);
834 }
835
836 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
837 SingleChannelElement *sce,
838 const float lambda)
839 {
840 int start = 0, i, w, w2, g;
841 float uplim[128], maxq[128];
842 int minq, maxsf;
843 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
844 int last = 0, lastband = 0, curband = 0;
845 float avg_energy = 0.0;
846 if (sce->ics.num_windows == 1) {
847 start = 0;
848 for (i = 0; i < 1024; i++) {
849 if (i - start >= sce->ics.swb_sizes[curband]) {
850 start += sce->ics.swb_sizes[curband];
851 curband++;
852 }
853 if (sce->coeffs[i]) {
854 avg_energy += sce->coeffs[i] * sce->coeffs[i];
855 last = i;
856 lastband = curband;
857 }
858 }
859 } else {
860 for (w = 0; w < 8; w++) {
861 const float *coeffs = sce->coeffs + w*128;
862 start = 0;
863 for (i = 0; i < 128; i++) {
864 if (i - start >= sce->ics.swb_sizes[curband]) {
865 start += sce->ics.swb_sizes[curband];
866 curband++;
867 }
868 if (coeffs[i]) {
869 avg_energy += coeffs[i] * coeffs[i];
870 last = FFMAX(last, i);
871 lastband = FFMAX(lastband, curband);
872 }
873 }
874 }
875 }
876 last++;
877 avg_energy /= last;
878 if (avg_energy == 0.0f) {
879 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
880 sce->sf_idx[i] = SCALE_ONE_POS;
881 return;
882 }
883 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
884 start = w*128;
885 for (g = 0; g < sce->ics.num_swb; g++) {
886 float *coefs = sce->coeffs + start;
887 const int size = sce->ics.swb_sizes[g];
888 int start2 = start, end2 = start + size, peakpos = start;
889 float maxval = -1, thr = 0.0f, t;
890 maxq[w*16+g] = 0.0f;
891 if (g > lastband) {
892 maxq[w*16+g] = 0.0f;
893 start += size;
894 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
895 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
896 continue;
897 }
898 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
899 for (i = 0; i < size; i++) {
900 float t = coefs[w2*128+i]*coefs[w2*128+i];
901 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
902 thr += t;
903 if (sce->ics.num_windows == 1 && maxval < t) {
904 maxval = t;
905 peakpos = start+i;
906 }
907 }
908 }
909 if (sce->ics.num_windows == 1) {
910 start2 = FFMAX(peakpos - 2, start2);
911 end2 = FFMIN(peakpos + 3, end2);
912 } else {
913 start2 -= start;
914 end2 -= start;
915 }
916 start += size;
917 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
918 t = 1.0 - (1.0 * start2 / last);
919 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
920 }
921 }
922 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
923 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
924 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
925 start = w*128;
926 for (g = 0; g < sce->ics.num_swb; g++) {
927 const float *coefs = sce->coeffs + start;
928 const float *scaled = s->scoefs + start;
929 const int size = sce->ics.swb_sizes[g];
930 int scf, prev_scf, step;
931 int min_scf = -1, max_scf = 256;
932 float curdiff;
933 if (maxq[w*16+g] < 21.544) {
934 sce->zeroes[w*16+g] = 1;
935 start += size;
936 continue;
937 }
938 sce->zeroes[w*16+g] = 0;
939 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
940 step = 16;
941 for (;;) {
942 float dist = 0.0f;
943 int quant_max;
944
945 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
946 int b;
947 dist += quantize_band_cost(s, coefs + w2*128,
948 scaled + w2*128,
949 sce->ics.swb_sizes[g],
950 scf,
951 ESC_BT,
952 lambda,
953 INFINITY,
954 &b);
955 dist -= b;
956 }
957 dist *= 1.0f / 512.0f / lambda;
958 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
959 if (quant_max >= 8191) { // too much, return to the previous quantizer
960 sce->sf_idx[w*16+g] = prev_scf;
961 break;
962 }
963 prev_scf = scf;
964 curdiff = fabsf(dist - uplim[w*16+g]);
965 if (curdiff <= 1.0f)
966 step = 0;
967 else
968 step = log2f(curdiff);
969 if (dist > uplim[w*16+g])
970 step = -step;
971 scf += step;
972 scf = av_clip_uint8(scf);
973 step = scf - prev_scf;
974 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
975 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
976 break;
977 }
978 if (step > 0)
979 min_scf = prev_scf;
980 else
981 max_scf = prev_scf;
982 }
983 start += size;
984 }
985 }
986 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
987 for (i = 1; i < 128; i++) {
988 if (!sce->sf_idx[i])
989 sce->sf_idx[i] = sce->sf_idx[i-1];
990 else
991 minq = FFMIN(minq, sce->sf_idx[i]);
992 }
993 if (minq == INT_MAX)
994 minq = 0;
995 minq = FFMIN(minq, SCALE_MAX_POS);
996 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
997 for (i = 126; i >= 0; i--) {
998 if (!sce->sf_idx[i])
999 sce->sf_idx[i] = sce->sf_idx[i+1];
1000 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
1001 }
1002 }
1003
1004 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
1005 SingleChannelElement *sce,
1006 const float lambda)
1007 {
1008 int start = 0, i, w, w2, g;
1009 int minq = 255;
1010
1011 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
1012 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1013 start = w*128;
1014 for (g = 0; g < sce->ics.num_swb; g++) {
1015 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
1016 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
1017 if (band->energy <= band->threshold) {
1018 sce->sf_idx[(w+w2)*16+g] = 218;
1019 sce->zeroes[(w+w2)*16+g] = 1;
1020 } else {
1021 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
1022 sce->zeroes[(w+w2)*16+g] = 0;
1023 }
1024 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
1025 }
1026 }
1027 }
1028 for (i = 0; i < 128; i++) {
1029 sce->sf_idx[i] = 140;
1030 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
1031 }
1032 //set the same quantizers inside window groups
1033 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
1034 for (g = 0; g < sce->ics.num_swb; g++)
1035 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
1036 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
1037 }
1038
1039 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
1040 const float lambda)
1041 {
1042 int start = 0, i, w, w2, g;
1043 float M[128], S[128];
1044 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
1045 SingleChannelElement *sce0 = &cpe->ch[0];
1046 SingleChannelElement *sce1 = &cpe->ch[1];
1047 if (!cpe->common_window)
1048 return;
1049 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
1050 for (g = 0; g < sce0->ics.num_swb; g++) {
1051 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
1052 float dist1 = 0.0f, dist2 = 0.0f;
1053 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1054 FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
1055 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
1056 float minthr = FFMIN(band0->threshold, band1->threshold);
1057 float maxthr = FFMAX(band0->threshold, band1->threshold);
1058 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1059 M[i] = (sce0->coeffs[start+w2*128+i]
1060 + sce1->coeffs[start+w2*128+i]) * 0.5;
1061 S[i] = M[i]
1062 - sce1->coeffs[start+w2*128+i];
1063 }
1064 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1065 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1066 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
1067 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
1068 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
1069 L34,
1070 sce0->ics.swb_sizes[g],
1071 sce0->sf_idx[(w+w2)*16+g],
1072 sce0->band_type[(w+w2)*16+g],
1073 lambda / band0->threshold, INFINITY, NULL);
1074 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1075 R34,
1076 sce1->ics.swb_sizes[g],
1077 sce1->sf_idx[(w+w2)*16+g],
1078 sce1->band_type[(w+w2)*16+g],
1079 lambda / band1->threshold, INFINITY, NULL);
1080 dist2 += quantize_band_cost(s, M,
1081 M34,
1082 sce0->ics.swb_sizes[g],
1083 sce0->sf_idx[(w+w2)*16+g],
1084 sce0->band_type[(w+w2)*16+g],
1085 lambda / maxthr, INFINITY, NULL);
1086 dist2 += quantize_band_cost(s, S,
1087 S34,
1088 sce1->ics.swb_sizes[g],
1089 sce1->sf_idx[(w+w2)*16+g],
1090 sce1->band_type[(w+w2)*16+g],
1091 lambda / minthr, INFINITY, NULL);
1092 }
1093 cpe->ms_mask[w*16+g] = dist2 < dist1;
1094 }
1095 start += sce0->ics.swb_sizes[g];
1096 }
1097 }
1098 }
1099
1100 AACCoefficientsEncoder ff_aac_coders[] = {
1101 {
1102 search_for_quantizers_faac,
1103 encode_window_bands_info,
1104 quantize_and_encode_band,
1105 search_for_ms,
1106 },
1107 {
1108 search_for_quantizers_anmr,
1109 encode_window_bands_info,
1110 quantize_and_encode_band,
1111 search_for_ms,
1112 },
1113 {
1114 search_for_quantizers_twoloop,
1115 codebook_trellis_rate,
1116 quantize_and_encode_band,
1117 search_for_ms,
1118 },
1119 {
1120 search_for_quantizers_fast,
1121 encode_window_bands_info,
1122 quantize_and_encode_band,
1123 search_for_ms,
1124 },
1125 };