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