2
* AAC coefficients encoder
3
* Copyright (C) 2008-2009 Konstantin Shishkov
5
* This file is part of FFmpeg.
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.
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.
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
24
* AAC coefficients encoder
27
/***********************************
29
* speedup quantizer selection
30
* add sane pulse detection
31
***********************************/
39
/** bits needed to code codebook run value for long windows */
40
static 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
47
/** bits needed to code codebook run value for short windows */
48
static 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
52
static const uint8_t *run_value_bits[2] = {
53
run_value_bits_long, run_value_bits_short
58
* Quantize one coefficient.
59
* @return absolute value of the quantized coefficient
60
* @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
62
static av_always_inline int quant(float coef, const float Q)
65
return sqrtf(a * sqrtf(a)) + 0.4054;
68
static void quantize_bands(int (*out)[2], const float *in, const float *scaled,
69
int size, float Q34, int is_signed, int maxval)
73
for (i = 0; i < size; i++) {
75
out[i][0] = (int)FFMIN(qc, (double)maxval);
76
out[i][1] = (int)FFMIN(qc + 0.4054, (double)maxval);
77
if (is_signed && in[i] < 0.0f) {
78
out[i][0] = -out[i][0];
79
out[i][1] = -out[i][1];
84
static void abs_pow34_v(float *out, const float *in, const int size)
86
#ifndef USE_REALLY_FULL_SEARCH
88
for (i = 0; i < size; i++) {
89
float a = fabsf(in[i]);
90
out[i] = sqrtf(a * sqrtf(a));
92
#endif /* USE_REALLY_FULL_SEARCH */
95
static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
96
static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
99
* Calculate rate distortion cost for quantizing with given codebook
101
* @return quantization distortion
103
static float quantize_and_encode_band_cost(struct AACEncContext *s,
104
PutBitContext *pb, const float *in,
105
const float *scaled, int size, int scale_idx,
106
int cb, const float lambda, const float uplim,
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;
114
const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
116
#ifndef USE_REALLY_FULL_SEARCH
117
const float Q34 = sqrtf(Q * sqrtf(Q));
118
const int range = aac_cb_range[cb];
119
const int maxval = aac_cb_maxval[cb];
121
#endif /* USE_REALLY_FULL_SEARCH */
124
for (i = 0; i < size; i++)
128
return cost * lambda;
130
#ifndef USE_REALLY_FULL_SEARCH
132
for (i = 1; i < dim; i++)
133
offs[i] = offs[i-1]*range;
135
abs_pow34_v(s->scoefs, in, size);
138
quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
139
#endif /* USE_REALLY_FULL_SEARCH */
140
for (i = 0; i < size; i += dim) {
145
#ifndef USE_REALLY_FULL_SEARCH
146
int (*quants)[2] = &s->qcoefs[i];
148
for (j = 0; j < dim; j++)
149
mincost += in[i+j]*in[i+j];
150
minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
151
minbits = ff_aac_spectral_bits[cb-1][minidx];
152
mincost = mincost * lambda + minbits;
153
for (j = 0; j < (1<<dim); j++) {
156
int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
158
for (k = 0; k < dim; k++) {
159
if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
166
for (k = 0; k < dim; k++)
167
curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
168
curbits = ff_aac_spectral_bits[cb-1][curidx];
169
vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
172
vec = ff_aac_codebook_vectors[cb-1];
173
for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
175
int curbits = ff_aac_spectral_bits[cb-1][j];
177
#endif /* USE_REALLY_FULL_SEARCH */
178
if (IS_CODEBOOK_UNSIGNED(cb)) {
179
for (k = 0; k < dim; k++) {
180
float t = fabsf(in[i+k]);
182
if (vec[k] == 64.0f) { //FIXME: slow
183
//do not code with escape sequence small values
188
if (t >= CLIPPED_ESCAPE) {
189
di = t - CLIPPED_ESCAPE;
192
int c = av_clip(quant(t, Q), 0, 8191);
193
di = t - c*cbrtf(c)*IQ;
194
curbits += av_log2(c)*2 - 4 + 1;
204
for (k = 0; k < dim; k++) {
205
float di = in[i+k] - vec[k]*IQ;
209
rd = rd * lambda + curbits;
221
put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
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)
225
put_bits(pb, 1, in[i+j] < 0.0f);
227
for (j = 0; j < 2; j++) {
228
if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
229
int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
230
int len = av_log2(coef);
232
put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
233
put_bits(pb, len, coef & ((1 << len) - 1));
244
static 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,
249
return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
250
cb, lambda, uplim, bits);
253
static 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)
257
quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
262
* structure used in optimal codebook search
264
typedef struct BandCodingPath {
265
int prev_idx; ///< pointer to the previous path point
266
float cost; ///< path cost
271
* Encode band info for single window group bands.
273
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
274
int win, int group_len, const float lambda)
276
BandCodingPath path[120][12];
277
int w, swb, cb, start, start2, size;
279
const int max_sfb = sce->ics.max_sfb;
280
const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
281
const int run_esc = (1 << run_bits) - 1;
282
int idx, ppos, count;
283
int stackrun[120], stackcb[120], stack_len;
284
float next_minrd = INFINITY;
287
abs_pow34_v(s->scoefs, sce->coeffs, 1024);
289
for (cb = 0; cb < 12; cb++) {
290
path[0][cb].cost = 0.0f;
291
path[0][cb].prev_idx = -1;
294
for (swb = 0; swb < max_sfb; swb++) {
296
size = sce->ics.swb_sizes[swb];
297
if (sce->zeroes[win*16 + swb]) {
298
for (cb = 0; cb < 12; cb++) {
299
path[swb+1][cb].prev_idx = cb;
300
path[swb+1][cb].cost = path[swb][cb].cost;
301
path[swb+1][cb].run = path[swb][cb].run + 1;
304
float minrd = next_minrd;
305
int mincb = next_mincb;
306
next_minrd = INFINITY;
308
for (cb = 0; cb < 12; cb++) {
309
float cost_stay_here, cost_get_here;
311
for (w = 0; w < group_len; w++) {
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);
318
cost_stay_here = path[swb][cb].cost + rd;
319
cost_get_here = minrd + rd + run_bits + 4;
320
if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
321
!= run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
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;
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;
332
if (path[swb+1][cb].cost < next_minrd) {
333
next_minrd = path[swb+1][cb].cost;
338
start += sce->ics.swb_sizes[swb];
341
//convert resulting path from backward-linked list
344
for (cb = 1; cb < 12; cb++)
345
if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
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;
356
//perform actual band info encoding
358
for (i = stack_len - 1; i >= 0; i--) {
359
put_bits(&s->pb, 4, stackcb[i]);
361
memset(sce->zeroes + win*16 + start, !stackcb[i], count);
362
//XXX: memset when band_type is also uint8_t
363
for (j = 0; j < count; j++) {
364
sce->band_type[win*16 + start] = stackcb[i];
367
while (count >= run_esc) {
368
put_bits(&s->pb, run_bits, run_esc);
371
put_bits(&s->pb, run_bits, count);
375
typedef struct TrellisPath {
382
#define TRELLIS_STAGES 121
383
#define TRELLIS_STATES 256
385
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
386
SingleChannelElement *sce,
389
int q, w, w2, g, start = 0;
392
TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
393
int bandaddr[TRELLIS_STAGES];
397
for (i = 0; i < TRELLIS_STATES; i++) {
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;
403
for (j = 1; j < TRELLIS_STAGES; j++) {
404
for (i = 0; i < TRELLIS_STATES; i++) {
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;
412
abs_pow34_v(s->scoefs, sce->coeffs, 1024);
413
for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
415
for (g = 0; g < sce->ics.num_swb; g++) {
416
const float *coefs = sce->coeffs + start;
420
bandaddr[idx] = w * 16 + g;
423
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
424
FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
425
if (band->energy <= band->threshold || band->threshold == 0.0f) {
426
sce->zeroes[(w+w2)*16+g] = 1;
429
sce->zeroes[(w+w2)*16+g] = 0;
431
for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
432
float t = fabsf(coefs[w2*128+i]);
434
qmin = FFMIN(qmin, t);
435
qmax = FFMAX(qmax, t);
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);
445
for (q = minscale; q < maxscale; q++) {
446
float dists[12], dist;
447
memset(dists, 0, sizeof(dists));
448
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
449
FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
451
for (cb = 0; cb <= ESC_BT; cb++)
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);
456
for (i = 1; i <= ESC_BT; i++)
457
dist = FFMIN(dist, dists[i]);
458
minrd = FFMIN(minrd, dist);
460
for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
463
if (isinf(paths[idx - 1][i].cost))
465
cost = paths[idx - 1][i].cost + dist
466
+ ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
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;
478
for (q = 0; q < TRELLIS_STATES; q++) {
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);
486
for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
489
if (isinf(paths[idx - 1][i].cost))
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;
503
sce->zeroes[w*16+g] = !nz;
504
start += sce->ics.swb_sizes[g];
509
mincost = paths[idx][0].cost;
511
for (i = 1; i < TRELLIS_STATES; i++) {
512
if (paths[idx][i].cost < mincost) {
513
mincost = paths[idx][i].cost;
518
sce->sf_idx[bandaddr[idx]] = minq;
519
minq = paths[idx][minq].prev;
522
//set the same quantizers inside window groups
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++)
526
sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
530
* two-loop quantizers search taken from ISO 13818-7 Appendix C
532
static void search_for_quantizers_twoloop(AVCodecContext *avctx,
534
SingleChannelElement *sce,
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;
543
float minthr = INFINITY;
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
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++) {
552
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
553
FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
554
uplim += band->threshold;
555
if (band->energy <= band->threshold || band->threshold == 0.0f) {
556
sce->zeroes[(w+w2)*16+g] = 1;
561
uplims[w*16+g] = uplim *512;
562
sce->zeroes[w*16+g] = !nz;
564
minthr = FFMIN(minthr, uplim);
565
allz = FFMAX(allz, nz);
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]) {
571
sce->sf_idx[w*16+g] = SCALE_ONE_POS;
574
sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
580
abs_pow34_v(s->scoefs, sce->coeffs, 1024);
581
//perform two-loop search
582
//outer loop - improve quality
585
minscaler = sce->sf_idx[0];
586
//inner loop - quantize spectrum to fit into given number of bits
587
qstep = its ? 1 : 32;
592
for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
594
for (g = 0; g < sce->ics.num_swb; g++) {
595
const float *coefs = sce->coeffs + start;
596
const float *scaled = s->scoefs + start;
599
float mindist = INFINITY;
602
if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
603
start += sce->ics.swb_sizes[g];
606
minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
607
for (cb = 0; cb <= ESC_BT; cb++) {
610
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
612
dist += quantize_band_cost(s, coefs + w2*128,
614
sce->ics.swb_sizes[g],
622
if (dist < mindist) {
627
dists[w*16+g] = (mindist - minbits) / lambda;
630
bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
633
start += sce->ics.swb_sizes[g];
634
prev = sce->sf_idx[w*16+g];
637
if (tbits > destbits) {
638
for (i = 0; i < 128; i++)
639
if (sce->sf_idx[i] < 218 - qstep)
640
sce->sf_idx[i] += qstep;
642
for (i = 0; i < 128; i++)
643
if (sce->sf_idx[i] > 60 - qstep)
644
sce->sf_idx[i] -= qstep;
647
if (!qstep && tbits > destbits*1.02)
649
if (sce->sf_idx[0] >= 217)
654
minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
655
for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
657
for (g = 0; g < sce->ics.num_swb; g++) {
658
int prevsc = sce->sf_idx[w*16+g];
659
if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
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);
663
if (sce->sf_idx[w*16+g] != prevsc)
668
} while (fflag && its < 10);
671
static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
672
SingleChannelElement *sce,
675
int start = 0, i, w, w2, g;
676
float uplim[128], maxq[128];
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;
681
if (sce->ics.num_windows == 1) {
683
for (i = 0; i < 1024; i++) {
684
if (i - start >= sce->ics.swb_sizes[curband]) {
685
start += sce->ics.swb_sizes[curband];
688
if (sce->coeffs[i]) {
689
avg_energy += sce->coeffs[i] * sce->coeffs[i];
695
for (w = 0; w < 8; w++) {
696
const float *coeffs = sce->coeffs + w*128;
698
for (i = 0; i < 128; i++) {
699
if (i - start >= sce->ics.swb_sizes[curband]) {
700
start += sce->ics.swb_sizes[curband];
704
avg_energy += coeffs[i] * coeffs[i];
705
last = FFMAX(last, i);
706
lastband = FFMAX(lastband, curband);
713
if (avg_energy == 0.0f) {
714
for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
715
sce->sf_idx[i] = SCALE_ONE_POS;
718
for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
720
for (g = 0; g < sce->ics.num_swb; g++) {
721
float *coefs = sce->coeffs + start;
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;
729
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
730
memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
733
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
734
for (i = 0; i < size; i++) {
735
float t = coefs[w2*128+i]*coefs[w2*128+i];
736
maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
738
if (sce->ics.num_windows == 1 && maxval < t) {
744
if (sce->ics.num_windows == 1) {
745
start2 = FFMAX(peakpos - 2, start2);
746
end2 = FFMIN(peakpos + 3, end2);
752
thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
753
t = 1.0 - (1.0 * start2 / last);
754
uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
757
memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
758
abs_pow34_v(s->scoefs, sce->coeffs, 1024);
759
for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
761
for (g = 0; g < sce->ics.num_swb; g++) {
762
const float *coefs = sce->coeffs + start;
763
const float *scaled = s->scoefs + start;
764
const int size = sce->ics.swb_sizes[g];
765
int scf, prev_scf, step;
766
int min_scf = -1, max_scf = 256;
768
if (maxq[w*16+g] < 21.544) {
769
sce->zeroes[w*16+g] = 1;
773
sce->zeroes[w*16+g] = 0;
774
scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
780
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
782
dist += quantize_band_cost(s, coefs + w2*128,
784
sce->ics.swb_sizes[g],
792
dist *= 1.0f / 512.0f / lambda;
793
quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
794
if (quant_max >= 8191) { // too much, return to the previous quantizer
795
sce->sf_idx[w*16+g] = prev_scf;
799
curdiff = fabsf(dist - uplim[w*16+g]);
803
step = log2(curdiff);
804
if (dist > uplim[w*16+g])
807
scf = av_clip_uint8(scf);
808
step = scf - prev_scf;
809
if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
810
sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
821
minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
822
for (i = 1; i < 128; i++) {
824
sce->sf_idx[i] = sce->sf_idx[i-1];
826
minq = FFMIN(minq, sce->sf_idx[i]);
830
minq = FFMIN(minq, SCALE_MAX_POS);
831
maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
832
for (i = 126; i >= 0; i--) {
834
sce->sf_idx[i] = sce->sf_idx[i+1];
835
sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
839
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
840
SingleChannelElement *sce,
843
int start = 0, i, w, w2, g;
846
memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
847
for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
849
for (g = 0; g < sce->ics.num_swb; g++) {
850
for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
851
FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
852
if (band->energy <= band->threshold) {
853
sce->sf_idx[(w+w2)*16+g] = 218;
854
sce->zeroes[(w+w2)*16+g] = 1;
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;
859
minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
863
for (i = 0; i < 128; i++) {
864
sce->sf_idx[i] = 140;
865
//av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
867
//set the same quantizers inside window groups
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++)
871
sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
874
static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
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];
882
if (!cpe->common_window)
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]) {
887
float dist1 = 0.0f, dist2 = 0.0f;
888
for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
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];
891
float minthr = FFMIN(band0->threshold, band1->threshold);
892
float maxthr = FFMAX(band0->threshold, band1->threshold);
893
for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
894
M[i] = (sce0->coeffs[start+w2*128+i]
895
+ sce1->coeffs[start+w2*128+i]) * 0.5;
896
S[i] = sce0->coeffs[start+w2*128+i]
897
- sce1->coeffs[start+w2*128+i];
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,
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,
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,
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,
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);
928
cpe->ms_mask[w*16+g] = dist2 < dist1;
930
start += sce0->ics.swb_sizes[g];
935
AACCoefficientsEncoder ff_aac_coders[] = {
937
search_for_quantizers_faac,
938
encode_window_bands_info,
939
quantize_and_encode_band,
943
search_for_quantizers_anmr,
944
encode_window_bands_info,
945
quantize_and_encode_band,
949
search_for_quantizers_twoloop,
950
encode_window_bands_info,
951
quantize_and_encode_band,
955
search_for_quantizers_fast,
956
encode_window_bands_info,
957
quantize_and_encode_band,