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