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
23 * @file libavcodec/aaccoder.c
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_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
,
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
;
113 const int dim
= cb
< FIRST_PAIR_BT
? 4 : 2;
115 #ifndef USE_REALLY_FULL_SEARCH
116 const float Q34
= sqrtf(Q
* sqrtf(Q
));
117 const int range
= aac_cb_range
[cb
];
118 const int maxval
= aac_cb_maxval
[cb
];
120 #endif /* USE_REALLY_FULL_SEARCH */
123 for (i
= 0; i
< size
; i
++)
124 cost
+= in
[i
]*in
[i
]*lambda
;
129 #ifndef USE_REALLY_FULL_SEARCH
131 for (i
= 1; i
< dim
; i
++)
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 */
135 for (i
= 0; i
< size
; i
+= dim
) {
140 #ifndef USE_REALLY_FULL_SEARCH
141 int (*quants
)[2] = &s
->qcoefs
[i
];
143 for (j
= 0; j
< dim
; j
++)
144 mincost
+= in
[i
+j
]*in
[i
+j
]*lambda
;
145 minidx
= IS_CODEBOOK_UNSIGNED(cb
) ? 0 : 40;
146 minbits
= ff_aac_spectral_bits
[cb
-1][minidx
];
148 for (j
= 0; j
< (1<<dim
); j
++) {
151 int curidx
= IS_CODEBOOK_UNSIGNED(cb
) ? 0 : 40;
153 for (k
= 0; k
< dim
; k
++) {
154 if ((j
& (1 << k
)) && quants
[k
][0] == quants
[k
][1]) {
161 for (k
= 0; k
< dim
; k
++)
162 curidx
+= quants
[k
][!!(j
& (1 << k
))] * offs
[dim
- 1 - k
];
163 curbits
= ff_aac_spectral_bits
[cb
-1][curidx
];
164 vec
= &ff_aac_codebook_vectors
[cb
-1][curidx
*dim
];
167 vec
= ff_aac_codebook_vectors
[cb
-1];
168 for (j
= 0; j
< ff_aac_spectral_sizes
[cb
-1]; j
++, vec
+= dim
) {
170 int curbits
= ff_aac_spectral_bits
[cb
-1][j
];
171 #endif /* USE_REALLY_FULL_SEARCH */
172 if (IS_CODEBOOK_UNSIGNED(cb
)) {
173 for (k
= 0; k
< dim
; k
++) {
174 float t
= fabsf(in
[i
+k
]);
176 //do not code with escape sequence small values
177 if (vec
[k
] == 64.0f
&& t
< 39.0f
*IQ
) {
181 if (vec
[k
] == 64.0f
) { //FIXME: slow
182 if (t
>= CLIPPED_ESCAPE
) {
183 di
= t
- CLIPPED_ESCAPE
;
186 int c
= av_clip(quant(t
, Q
), 0, 8191);
187 di
= t
- c
*cbrt(c
)*IQ
;
188 curbits
+= av_log2(c
)*2 - 4 + 1;
198 for (k
= 0; k
< dim
; k
++) {
199 float di
= in
[i
+k
] - vec
[k
]*IQ
;
221 static 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
)
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;
230 #ifndef USE_REALLY_FULL_SEARCH
231 const float Q34
= sqrtf(Q
* sqrtf(Q
));
232 const int range
= aac_cb_range
[cb
];
233 const int maxval
= aac_cb_maxval
[cb
];
235 float *scaled
= s
->scoefs
;
236 #endif /* USE_REALLY_FULL_SEARCH */
242 #ifndef USE_REALLY_FULL_SEARCH
244 for (i
= 1; i
< dim
; i
++)
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 */
249 for (i
= 0; i
< size
; i
+= dim
) {
254 #ifndef USE_REALLY_FULL_SEARCH
255 int (*quants
)[2] = &s
->qcoefs
[i
];
257 for (j
= 0; j
< dim
; j
++)
258 mincost
+= in
[i
+j
]*in
[i
+j
]*lambda
;
259 minidx
= IS_CODEBOOK_UNSIGNED(cb
) ? 0 : 40;
260 minbits
= ff_aac_spectral_bits
[cb
-1][minidx
];
262 for (j
= 0; j
< (1<<dim
); j
++) {
265 int curidx
= IS_CODEBOOK_UNSIGNED(cb
) ? 0 : 40;
267 for (k
= 0; k
< dim
; k
++) {
268 if ((j
& (1 << k
)) && quants
[k
][0] == quants
[k
][1]) {
275 for (k
= 0; k
< dim
; k
++)
276 curidx
+= quants
[k
][!!(j
& (1 << k
))] * offs
[dim
- 1 - k
];
277 curbits
= ff_aac_spectral_bits
[cb
-1][curidx
];
278 vec
= &ff_aac_codebook_vectors
[cb
-1][curidx
*dim
];
280 vec
= ff_aac_codebook_vectors
[cb
-1];
282 for (j
= 0; j
< ff_aac_spectral_sizes
[cb
-1]; j
++, vec
+= dim
) {
284 int curbits
= ff_aac_spectral_bits
[cb
-1][j
];
286 #endif /* USE_REALLY_FULL_SEARCH */
287 if (IS_CODEBOOK_UNSIGNED(cb
)) {
288 for (k
= 0; k
< dim
; k
++) {
289 float t
= fabsf(in
[i
+k
]);
291 //do not code with escape sequence small values
292 if (vec
[k
] == 64.0f
&& t
< 39.0f
*IQ
) {
296 if (vec
[k
] == 64.0f
) { //FIXME: slow
297 if (t
>= CLIPPED_ESCAPE
) {
298 di
= t
- CLIPPED_ESCAPE
;
301 int c
= av_clip(quant(t
, Q
), 0, 8191);
302 di
= t
- c
*cbrt(c
)*IQ
;
303 curbits
+= av_log2(c
)*2 - 4 + 1;
313 for (k
= 0; k
< dim
; k
++) {
314 float di
= in
[i
+k
] - vec
[k
]*IQ
;
325 put_bits(pb
, ff_aac_spectral_bits
[cb
-1][minidx
], ff_aac_spectral_codes
[cb
-1][minidx
]);
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
)
329 put_bits(pb
, 1, in
[i
+j
] < 0.0f
);
331 for (j
= 0; j
< 2; j
++) {
332 if (ff_aac_codebook_vectors
[cb
-1][minidx
*2+j
] == 64.0f
) {
333 int coef
= av_clip(quant(fabsf(in
[i
+j
]), Q
), 0, 8191);
334 int len
= av_log2(coef
);
336 put_bits(pb
, len
- 4 + 1, (1 << (len
- 4 + 1)) - 2);
337 put_bits(pb
, len
, coef
& ((1 << len
) - 1));
342 //STOP_TIMER("quantize_and_encode")
346 * structure used in optimal codebook search
348 typedef struct BandCodingPath
{
349 int prev_idx
; ///< pointer to the previous path point
350 float cost
; ///< path cost
355 * Encode band info for single window group bands.
357 static void encode_window_bands_info(AACEncContext
*s
, SingleChannelElement
*sce
,
358 int win
, int group_len
, const float lambda
)
360 BandCodingPath path
[120][12];
361 int w
, swb
, cb
, start
, start2
, size
;
363 const int max_sfb
= sce
->ics
.max_sfb
;
364 const int run_bits
= sce
->ics
.num_windows
== 1 ? 5 : 3;
365 const int run_esc
= (1 << run_bits
) - 1;
366 int idx
, ppos
, count
;
367 int stackrun
[120], stackcb
[120], stack_len
;
368 float next_minrd
= INFINITY
;
371 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
373 for (cb
= 0; cb
< 12; cb
++) {
374 path
[0][cb
].cost
= 0.0f
;
375 path
[0][cb
].prev_idx
= -1;
378 for (swb
= 0; swb
< max_sfb
; swb
++) {
380 size
= sce
->ics
.swb_sizes
[swb
];
381 if (sce
->zeroes
[win
*16 + swb
]) {
382 for (cb
= 0; cb
< 12; cb
++) {
383 path
[swb
+1][cb
].prev_idx
= cb
;
384 path
[swb
+1][cb
].cost
= path
[swb
][cb
].cost
;
385 path
[swb
+1][cb
].run
= path
[swb
][cb
].run
+ 1;
388 float minrd
= next_minrd
;
389 int mincb
= next_mincb
;
390 next_minrd
= INFINITY
;
392 for (cb
= 0; cb
< 12; cb
++) {
393 float cost_stay_here
, cost_get_here
;
395 for (w
= 0; w
< group_len
; w
++) {
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
);
402 cost_stay_here
= path
[swb
][cb
].cost
+ rd
;
403 cost_get_here
= minrd
+ rd
+ run_bits
+ 4;
404 if ( run_value_bits
[sce
->ics
.num_windows
== 8][path
[swb
][cb
].run
]
405 != run_value_bits
[sce
->ics
.num_windows
== 8][path
[swb
][cb
].run
+1])
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;
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;
416 if (path
[swb
+1][cb
].cost
< next_minrd
) {
417 next_minrd
= path
[swb
+1][cb
].cost
;
422 start
+= sce
->ics
.swb_sizes
[swb
];
425 //convert resulting path from backward-linked list
428 for (cb
= 1; cb
< 12; cb
++)
429 if (path
[max_sfb
][cb
].cost
< path
[max_sfb
][idx
].cost
)
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
;
440 //perform actual band info encoding
442 for (i
= stack_len
- 1; i
>= 0; i
--) {
443 put_bits(&s
->pb
, 4, stackcb
[i
]);
445 memset(sce
->zeroes
+ win
*16 + start
, !stackcb
[i
], count
);
446 //XXX: memset when band_type is also uint8_t
447 for (j
= 0; j
< count
; j
++) {
448 sce
->band_type
[win
*16 + start
] = stackcb
[i
];
451 while (count
>= run_esc
) {
452 put_bits(&s
->pb
, run_bits
, run_esc
);
455 put_bits(&s
->pb
, run_bits
, count
);
459 typedef struct TrellisPath
{
466 static void search_for_quantizers_anmr(AVCodecContext
*avctx
, AACEncContext
*s
,
467 SingleChannelElement
*sce
,
470 int q
, w
, w2
, g
, start
= 0;
473 TrellisPath paths
[256*121];
478 for (i
= 0; i
< 256; i
++) {
479 paths
[i
].cost
= 0.0f
;
481 paths
[i
].min_val
= i
;
482 paths
[i
].max_val
= i
;
484 for (i
= 256; i
< 256*121; i
++) {
485 paths
[i
].cost
= INFINITY
;
487 paths
[i
].min_val
= INT_MAX
;
488 paths
[i
].max_val
= 0;
491 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
492 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
494 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
495 const float *coefs
= sce
->coeffs
+ start
;
499 bandaddr
[idx
>> 8] = w
* 16 + g
;
502 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
503 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
504 if (band
->energy
<= band
->threshold
|| band
->threshold
== 0.0f
) {
505 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
508 sce
->zeroes
[(w
+w2
)*16+g
] = 0;
510 for (i
= 0; i
< sce
->ics
.swb_sizes
[g
]; i
++) {
511 float t
= fabsf(coefs
[w2
*128+i
]);
513 qmin
= FFMIN(qmin
, t
);
514 qmax
= FFMAX(qmax
, t
);
518 int minscale
, maxscale
;
519 float minrd
= INFINITY
;
520 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
521 minscale
= av_clip_uint8(log2(qmin
)*4 - 69 + SCALE_ONE_POS
- SCALE_DIV_512
);
522 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
523 maxscale
= av_clip_uint8(log2(qmax
)*4 + 6 + SCALE_ONE_POS
- SCALE_DIV_512
);
524 for (q
= minscale
; q
< maxscale
; q
++) {
525 float dists
[12], dist
;
526 memset(dists
, 0, sizeof(dists
));
527 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
528 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
530 for (cb
= 0; cb
<= ESC_BT
; cb
++)
531 dists
[cb
] += quantize_band_cost(s
, coefs
+ w2
*128, s
->scoefs
+ start
+ w2
*128, sce
->ics
.swb_sizes
[g
],
532 q
, cb
, lambda
/ band
->threshold
, INFINITY
, NULL
);
535 for (i
= 1; i
<= ESC_BT
; i
++)
536 dist
= FFMIN(dist
, dists
[i
]);
537 minrd
= FFMIN(minrd
, dist
);
539 for (i
= FFMAX(q
- SCALE_MAX_DIFF
, 0); i
< FFMIN(q
+ SCALE_MAX_DIFF
, 256); i
++) {
542 if (isinf(paths
[idx
- 256 + i
].cost
))
544 cost
= paths
[idx
- 256 + i
].cost
+ dist
545 + ff_aac_scalefactor_bits
[q
- i
+ SCALE_DIFF_ZERO
];
546 minv
= FFMIN(paths
[idx
- 256 + i
].min_val
, q
);
547 maxv
= FFMAX(paths
[idx
- 256 + i
].max_val
, q
);
548 if (cost
< paths
[idx
+ q
].cost
&& maxv
-minv
< SCALE_MAX_DIFF
) {
549 paths
[idx
+ q
].cost
= cost
;
550 paths
[idx
+ q
].prev
= idx
- 256 + i
;
551 paths
[idx
+ q
].min_val
= minv
;
552 paths
[idx
+ q
].max_val
= maxv
;
557 for (q
= 0; q
< 256; q
++) {
558 if (!isinf(paths
[idx
- 256 + q
].cost
)) {
559 paths
[idx
+ q
].cost
= paths
[idx
- 256 + q
].cost
+ 1;
560 paths
[idx
+ q
].prev
= idx
- 256 + q
;
561 paths
[idx
+ q
].min_val
= FFMIN(paths
[idx
- 256 + q
].min_val
, q
);
562 paths
[idx
+ q
].max_val
= FFMAX(paths
[idx
- 256 + q
].max_val
, q
);
565 for (i
= FFMAX(q
- SCALE_MAX_DIFF
, 0); i
< FFMIN(q
+ SCALE_MAX_DIFF
, 256); i
++) {
568 if (isinf(paths
[idx
- 256 + i
].cost
))
570 cost
= paths
[idx
- 256 + i
].cost
+ ff_aac_scalefactor_bits
[q
- i
+ SCALE_DIFF_ZERO
];
571 minv
= FFMIN(paths
[idx
- 256 + i
].min_val
, q
);
572 maxv
= FFMAX(paths
[idx
- 256 + i
].max_val
, q
);
573 if (cost
< paths
[idx
+ q
].cost
&& maxv
-minv
< SCALE_MAX_DIFF
) {
574 paths
[idx
+ q
].cost
= cost
;
575 paths
[idx
+ q
].prev
= idx
- 256 + i
;
576 paths
[idx
+ q
].min_val
= minv
;
577 paths
[idx
+ q
].max_val
= maxv
;
582 sce
->zeroes
[w
*16+g
] = !nz
;
583 start
+= sce
->ics
.swb_sizes
[g
];
588 mincost
= paths
[idx
].cost
;
590 for (i
= 1; i
< 256; i
++) {
591 if (paths
[idx
+ i
].cost
< mincost
) {
592 mincost
= paths
[idx
+ i
].cost
;
596 while (minq
>= 256) {
597 sce
->sf_idx
[bandaddr
[minq
>>8]] = minq
& 0xFF;
598 minq
= paths
[minq
].prev
;
600 //set the same quantizers inside window groups
601 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
])
602 for (g
= 0; g
< sce
->ics
.num_swb
; g
++)
603 for (w2
= 1; w2
< sce
->ics
.group_len
[w
]; w2
++)
604 sce
->sf_idx
[(w
+w2
)*16+g
] = sce
->sf_idx
[w
*16+g
];
608 * two-loop quantizers search taken from ISO 13818-7 Appendix C
610 static void search_for_quantizers_twoloop(AVCodecContext
*avctx
,
612 SingleChannelElement
*sce
,
615 int start
= 0, i
, w
, w2
, g
;
616 int destbits
= avctx
->bit_rate
* 1024.0 / avctx
->sample_rate
/ avctx
->channels
;
617 float dists
[128], uplims
[128];
618 int fflag
, minscaler
;
621 float minthr
= INFINITY
;
623 //XXX: some heuristic to determine initial quantizers will reduce search time
624 memset(dists
, 0, sizeof(dists
));
625 //determine zero bands and upper limits
626 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
627 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
630 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
631 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
632 uplim
+= band
->threshold
;
633 if (band
->energy
<= band
->threshold
|| band
->threshold
== 0.0f
) {
634 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
639 uplims
[w
*16+g
] = uplim
*512;
640 sce
->zeroes
[w
*16+g
] = !nz
;
642 minthr
= FFMIN(minthr
, uplim
);
643 allz
= FFMAX(allz
, nz
);
646 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
647 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
648 if (sce
->zeroes
[w
*16+g
]) {
649 sce
->sf_idx
[w
*16+g
] = SCALE_ONE_POS
;
652 sce
->sf_idx
[w
*16+g
] = SCALE_ONE_POS
+ FFMIN(log2(uplims
[w
*16+g
]/minthr
)*4,59);
658 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
659 //perform two-loop search
660 //outer loop - improve quality
663 minscaler
= sce
->sf_idx
[0];
664 //inner loop - quantize spectrum to fit into given number of bits
665 qstep
= its
? 1 : 32;
670 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
672 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
673 const float *coefs
= sce
->coeffs
+ start
;
674 const float *scaled
= s
->scoefs
+ start
;
677 float mindist
= INFINITY
;
680 if (sce
->zeroes
[w
*16+g
] || sce
->sf_idx
[w
*16+g
] >= 218) {
681 start
+= sce
->ics
.swb_sizes
[g
];
684 minscaler
= FFMIN(minscaler
, sce
->sf_idx
[w
*16+g
]);
685 for (cb
= 0; cb
<= ESC_BT
; cb
++) {
688 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
690 dist
+= quantize_band_cost(s
, coefs
+ w2
*128,
692 sce
->ics
.swb_sizes
[g
],
700 if (dist
< mindist
) {
705 dists
[w
*16+g
] = (mindist
- minbits
) / lambda
;
708 bits
+= ff_aac_scalefactor_bits
[sce
->sf_idx
[w
*16+g
] - prev
+ SCALE_DIFF_ZERO
];
711 start
+= sce
->ics
.swb_sizes
[g
];
712 prev
= sce
->sf_idx
[w
*16+g
];
715 if (tbits
> destbits
) {
716 for (i
= 0; i
< 128; i
++)
717 if (sce
->sf_idx
[i
] < 218 - qstep
)
718 sce
->sf_idx
[i
] += qstep
;
720 for (i
= 0; i
< 128; i
++)
721 if (sce
->sf_idx
[i
] > 60 - qstep
)
722 sce
->sf_idx
[i
] -= qstep
;
725 if (!qstep
&& tbits
> destbits
*1.02)
727 if (sce
->sf_idx
[0] >= 217)
732 minscaler
= av_clip(minscaler
, 60, 255 - SCALE_MAX_DIFF
);
733 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
735 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
736 int prevsc
= sce
->sf_idx
[w
*16+g
];
737 if (dists
[w
*16+g
] > uplims
[w
*16+g
] && sce
->sf_idx
[w
*16+g
] > 60)
738 sce
->sf_idx
[w
*16+g
]--;
739 sce
->sf_idx
[w
*16+g
] = av_clip(sce
->sf_idx
[w
*16+g
], minscaler
, minscaler
+ SCALE_MAX_DIFF
);
740 sce
->sf_idx
[w
*16+g
] = FFMIN(sce
->sf_idx
[w
*16+g
], 219);
741 if (sce
->sf_idx
[w
*16+g
] != prevsc
)
746 } while (fflag
&& its
< 10);
749 static void search_for_quantizers_faac(AVCodecContext
*avctx
, AACEncContext
*s
,
750 SingleChannelElement
*sce
,
753 int start
= 0, i
, w
, w2
, g
;
754 float uplim
[128], maxq
[128];
756 float distfact
= ((sce
->ics
.num_windows
> 1) ? 85.80 : 147.84) / lambda
;
757 int last
= 0, lastband
= 0, curband
= 0;
758 float avg_energy
= 0.0;
759 if (sce
->ics
.num_windows
== 1) {
761 for (i
= 0; i
< 1024; i
++) {
762 if (i
- start
>= sce
->ics
.swb_sizes
[curband
]) {
763 start
+= sce
->ics
.swb_sizes
[curband
];
766 if (sce
->coeffs
[i
]) {
767 avg_energy
+= sce
->coeffs
[i
] * sce
->coeffs
[i
];
773 for (w
= 0; w
< 8; w
++) {
774 const float *coeffs
= sce
->coeffs
+ w
*128;
776 for (i
= 0; i
< 128; i
++) {
777 if (i
- start
>= sce
->ics
.swb_sizes
[curband
]) {
778 start
+= sce
->ics
.swb_sizes
[curband
];
782 avg_energy
+= coeffs
[i
] * coeffs
[i
];
783 last
= FFMAX(last
, i
);
784 lastband
= FFMAX(lastband
, curband
);
791 if (avg_energy
== 0.0f
) {
792 for (i
= 0; i
< FF_ARRAY_ELEMS(sce
->sf_idx
); i
++)
793 sce
->sf_idx
[i
] = SCALE_ONE_POS
;
796 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
798 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
799 float *coefs
= sce
->coeffs
+ start
;
800 const int size
= sce
->ics
.swb_sizes
[g
];
801 int start2
= start
, end2
= start
+ size
, peakpos
= start
;
802 float maxval
= -1, thr
= 0.0f
, t
;
807 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++)
808 memset(coefs
+ w2
*128, 0, sizeof(coefs
[0])*size
);
811 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
812 for (i
= 0; i
< size
; i
++) {
813 float t
= coefs
[w2
*128+i
]*coefs
[w2
*128+i
];
814 maxq
[w
*16+g
] = FFMAX(maxq
[w
*16+g
], fabsf(coefs
[w2
*128 + i
]));
816 if (sce
->ics
.num_windows
== 1 && maxval
< t
) {
822 if (sce
->ics
.num_windows
== 1) {
823 start2
= FFMAX(peakpos
- 2, start2
);
824 end2
= FFMIN(peakpos
+ 3, end2
);
830 thr
= pow(thr
/ (avg_energy
* (end2
- start2
)), 0.3 + 0.1*(lastband
- g
) / lastband
);
831 t
= 1.0 - (1.0 * start2
/ last
);
832 uplim
[w
*16+g
] = distfact
/ (1.4 * thr
+ t
*t
*t
+ 0.075);
835 memset(sce
->sf_idx
, 0, sizeof(sce
->sf_idx
));
836 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
837 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
839 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
840 const float *coefs
= sce
->coeffs
+ start
;
841 const float *scaled
= s
->scoefs
+ start
;
842 const int size
= sce
->ics
.swb_sizes
[g
];
843 int scf
, prev_scf
, step
;
844 int min_scf
= 0, max_scf
= 255;
846 if (maxq
[w
*16+g
] < 21.544) {
847 sce
->zeroes
[w
*16+g
] = 1;
851 sce
->zeroes
[w
*16+g
] = 0;
852 scf
= prev_scf
= av_clip(SCALE_ONE_POS
- SCALE_DIV_512
- log2(1/maxq
[w
*16+g
])*16/3, 60, 218);
858 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
860 dist
+= quantize_band_cost(s
, coefs
+ w2
*128,
862 sce
->ics
.swb_sizes
[g
],
870 dist
*= 1.0f
/ 512.0f
/ lambda
;
871 quant_max
= quant(maxq
[w
*16+g
], ff_aac_pow2sf_tab
[200 - scf
+ SCALE_ONE_POS
- SCALE_DIV_512
]);
872 if (quant_max
>= 8191) { // too much, return to the previous quantizer
873 sce
->sf_idx
[w
*16+g
] = prev_scf
;
877 curdiff
= fabsf(dist
- uplim
[w
*16+g
]);
881 step
= fabsf(log2(curdiff
));
882 if (dist
> uplim
[w
*16+g
])
884 if (FFABS(step
) <= 1 || (step
> 0 && scf
>= max_scf
) || (step
< 0 && scf
<= min_scf
)) {
885 sce
->sf_idx
[w
*16+g
] = scf
;
897 minq
= sce
->sf_idx
[0] ? sce
->sf_idx
[0] : INT_MAX
;
898 for (i
= 1; i
< 128; i
++) {
900 sce
->sf_idx
[i
] = sce
->sf_idx
[i
-1];
902 minq
= FFMIN(minq
, sce
->sf_idx
[i
]);
906 minq
= FFMIN(minq
, SCALE_MAX_POS
);
907 maxsf
= FFMIN(minq
+ SCALE_MAX_DIFF
, SCALE_MAX_POS
);
908 for (i
= 126; i
>= 0; i
--) {
910 sce
->sf_idx
[i
] = sce
->sf_idx
[i
+1];
911 sce
->sf_idx
[i
] = av_clip(sce
->sf_idx
[i
], minq
, maxsf
);
915 static void search_for_quantizers_fast(AVCodecContext
*avctx
, AACEncContext
*s
,
916 SingleChannelElement
*sce
,
919 int start
= 0, i
, w
, w2
, g
;
922 memset(sce
->sf_idx
, 0, sizeof(sce
->sf_idx
));
923 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
925 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
926 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
927 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
928 if (band
->energy
<= band
->threshold
) {
929 sce
->sf_idx
[(w
+w2
)*16+g
] = 218;
930 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
932 sce
->sf_idx
[(w
+w2
)*16+g
] = av_clip(SCALE_ONE_POS
- SCALE_DIV_512
+ log2(band
->threshold
), 80, 218);
933 sce
->zeroes
[(w
+w2
)*16+g
] = 0;
935 minq
= FFMIN(minq
, sce
->sf_idx
[(w
+w2
)*16+g
]);
939 for (i
= 0; i
< 128; i
++) {
940 sce
->sf_idx
[i
] = 140;
941 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
943 //set the same quantizers inside window groups
944 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
])
945 for (g
= 0; g
< sce
->ics
.num_swb
; g
++)
946 for (w2
= 1; w2
< sce
->ics
.group_len
[w
]; w2
++)
947 sce
->sf_idx
[(w
+w2
)*16+g
] = sce
->sf_idx
[w
*16+g
];
950 static void search_for_ms(AACEncContext
*s
, ChannelElement
*cpe
,
953 int start
= 0, i
, w
, w2
, g
;
954 float M
[128], S
[128];
955 float *L34
= s
->scoefs
, *R34
= s
->scoefs
+ 128, *M34
= s
->scoefs
+ 128*2, *S34
= s
->scoefs
+ 128*3;
956 SingleChannelElement
*sce0
= &cpe
->ch
[0];
957 SingleChannelElement
*sce1
= &cpe
->ch
[1];
958 if (!cpe
->common_window
)
960 for (w
= 0; w
< sce0
->ics
.num_windows
; w
+= sce0
->ics
.group_len
[w
]) {
961 for (g
= 0; g
< sce0
->ics
.num_swb
; g
++) {
962 if (!cpe
->ch
[0].zeroes
[w
*16+g
] && !cpe
->ch
[1].zeroes
[w
*16+g
]) {
963 float dist1
= 0.0f
, dist2
= 0.0f
;
964 for (w2
= 0; w2
< sce0
->ics
.group_len
[w
]; w2
++) {
965 FFPsyBand
*band0
= &s
->psy
.psy_bands
[(s
->cur_channel
+0)*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
966 FFPsyBand
*band1
= &s
->psy
.psy_bands
[(s
->cur_channel
+1)*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
967 float minthr
= FFMIN(band0
->threshold
, band1
->threshold
);
968 float maxthr
= FFMAX(band0
->threshold
, band1
->threshold
);
969 for (i
= 0; i
< sce0
->ics
.swb_sizes
[g
]; i
++) {
970 M
[i
] = (sce0
->coeffs
[start
+w2
*128+i
]
971 + sce1
->coeffs
[start
+w2
*128+i
]) * 0.5;
972 S
[i
] = sce0
->coeffs
[start
+w2
*128+i
]
973 - sce1
->coeffs
[start
+w2
*128+i
];
975 abs_pow34_v(L34
, sce0
->coeffs
+start
+w2
*128, sce0
->ics
.swb_sizes
[g
]);
976 abs_pow34_v(R34
, sce1
->coeffs
+start
+w2
*128, sce0
->ics
.swb_sizes
[g
]);
977 abs_pow34_v(M34
, M
, sce0
->ics
.swb_sizes
[g
]);
978 abs_pow34_v(S34
, S
, sce0
->ics
.swb_sizes
[g
]);
979 dist1
+= quantize_band_cost(s
, sce0
->coeffs
+ start
+ w2
*128,
981 sce0
->ics
.swb_sizes
[g
],
982 sce0
->sf_idx
[(w
+w2
)*16+g
],
983 sce0
->band_type
[(w
+w2
)*16+g
],
984 lambda
/ band0
->threshold
, INFINITY
, NULL
);
985 dist1
+= quantize_band_cost(s
, sce1
->coeffs
+ start
+ w2
*128,
987 sce1
->ics
.swb_sizes
[g
],
988 sce1
->sf_idx
[(w
+w2
)*16+g
],
989 sce1
->band_type
[(w
+w2
)*16+g
],
990 lambda
/ band1
->threshold
, INFINITY
, NULL
);
991 dist2
+= quantize_band_cost(s
, M
,
993 sce0
->ics
.swb_sizes
[g
],
994 sce0
->sf_idx
[(w
+w2
)*16+g
],
995 sce0
->band_type
[(w
+w2
)*16+g
],
996 lambda
/ maxthr
, INFINITY
, NULL
);
997 dist2
+= quantize_band_cost(s
, S
,
999 sce1
->ics
.swb_sizes
[g
],
1000 sce1
->sf_idx
[(w
+w2
)*16+g
],
1001 sce1
->band_type
[(w
+w2
)*16+g
],
1002 lambda
/ minthr
, INFINITY
, NULL
);
1004 cpe
->ms_mask
[w
*16+g
] = dist2
< dist1
;
1006 start
+= sce0
->ics
.swb_sizes
[g
];
1011 AACCoefficientsEncoder ff_aac_coders
[] = {
1013 search_for_quantizers_faac
,
1014 encode_window_bands_info
,
1015 quantize_and_encode_band
,
1019 search_for_quantizers_anmr
,
1020 encode_window_bands_info
,
1021 quantize_and_encode_band
,
1025 search_for_quantizers_twoloop
,
1026 encode_window_bands_info
,
1027 quantize_and_encode_band
,
1031 search_for_quantizers_fast
,
1032 encode_window_bands_info
,
1033 quantize_and_encode_band
,