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