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