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
)
64 return pow(coef
* Q
, 0.75) + 0.4054;
67 static void quantize_bands(int (*out
)[2], const float *in
, const float *scaled
,
68 int size
, float Q34
, int is_signed
, int maxval
)
72 for (i
= 0; i
< size
; i
++) {
74 out
[i
][0] = (int)FFMIN((int)qc
, maxval
);
75 out
[i
][1] = (int)FFMIN((int)(qc
+ 0.4054), maxval
);
76 if (is_signed
&& in
[i
] < 0.0f
) {
77 out
[i
][0] = -out
[i
][0];
78 out
[i
][1] = -out
[i
][1];
83 static void abs_pow34_v(float *out
, const float *in
, const int size
)
85 #ifndef USE_REALLY_FULL_SEARCH
87 for (i
= 0; i
< size
; i
++)
88 out
[i
] = pow(fabsf(in
[i
]), 0.75);
89 #endif /* USE_REALLY_FULL_SEARCH */
92 static av_always_inline
int quant2(float coef
, const float Q
)
94 return pow(coef
* Q
, 0.75);
97 static const uint8_t aac_cb_range
[12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
98 static const uint8_t aac_cb_maxval
[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
101 * Calculate rate distortion cost for quantizing with given codebook
103 * @return quantization distortion
105 static float quantize_band_cost(struct AACEncContext
*s
, const float *in
,
106 const float *scaled
, int size
, int scale_idx
,
107 int cb
, const float lambda
, const float uplim
,
110 const float IQ
= ff_aac_pow2sf_tab
[200 + scale_idx
- SCALE_ONE_POS
+ SCALE_DIV_512
];
111 const float Q
= ff_aac_pow2sf_tab
[200 - scale_idx
+ SCALE_ONE_POS
- SCALE_DIV_512
];
112 const float CLIPPED_ESCAPE
= 165140.0f
*IQ
;
115 const int dim
= cb
< FIRST_PAIR_BT ?
4 : 2;
117 #ifndef USE_REALLY_FULL_SEARCH
118 const float Q34
= pow(Q
, 0.75);
119 const int range
= aac_cb_range
[cb
];
120 const int maxval
= aac_cb_maxval
[cb
];
122 #endif /* USE_REALLY_FULL_SEARCH */
125 for (i
= 0; i
< size
; i
++)
126 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
= pow(Q
, 0.75);
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 int codebook
; ///< codebook for coding band run
351 float cost
; ///< path cost
356 * Encode band info for single window group bands.
358 static void encode_window_bands_info(AACEncContext
*s
, SingleChannelElement
*sce
,
359 int win
, int group_len
, const float lambda
)
361 BandCodingPath path
[120][12];
362 int w
, swb
, cb
, start
, start2
, size
;
364 const int max_sfb
= sce
->ics
.max_sfb
;
365 const int run_bits
= sce
->ics
.num_windows
== 1 ?
5 : 3;
366 const int run_esc
= (1 << run_bits
) - 1;
367 int idx
, ppos
, count
;
368 int stackrun
[120], stackcb
[120], stack_len
;
369 float next_minrd
= INFINITY
;
372 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
374 for (cb
= 0; cb
< 12; cb
++) {
375 path
[0][cb
].cost
= 0.0f
;
376 path
[0][cb
].prev_idx
= -1;
379 for (swb
= 0; swb
< max_sfb
; swb
++) {
381 size
= sce
->ics
.swb_sizes
[swb
];
382 if (sce
->zeroes
[win
*16 + swb
]) {
383 for (cb
= 0; cb
< 12; cb
++) {
384 path
[swb
+1][cb
].prev_idx
= cb
;
385 path
[swb
+1][cb
].cost
= path
[swb
][cb
].cost
;
386 path
[swb
+1][cb
].run
= path
[swb
][cb
].run
+ 1;
389 float minrd
= next_minrd
;
390 int mincb
= next_mincb
;
391 next_minrd
= INFINITY
;
393 for (cb
= 0; cb
< 12; cb
++) {
394 float cost_stay_here
, cost_get_here
;
396 for (w
= 0; w
< group_len
; w
++) {
397 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(win
+w
)*16+swb
];
398 rd
+= quantize_band_cost(s
, sce
->coeffs
+ start
+ w
*128,
399 s
->scoefs
+ start
+ w
*128, size
,
400 sce
->sf_idx
[(win
+w
)*16+swb
], cb
,
401 lambda
/ band
->threshold
, INFINITY
, NULL
);
403 cost_stay_here
= path
[swb
][cb
].cost
+ rd
;
404 cost_get_here
= minrd
+ rd
+ run_bits
+ 4;
405 if ( run_value_bits
[sce
->ics
.num_windows
== 8][path
[swb
][cb
].run
]
406 != run_value_bits
[sce
->ics
.num_windows
== 8][path
[swb
][cb
].run
+1])
407 cost_stay_here
+= run_bits
;
408 if (cost_get_here
< cost_stay_here
) {
409 path
[swb
+1][cb
].prev_idx
= mincb
;
410 path
[swb
+1][cb
].cost
= cost_get_here
;
411 path
[swb
+1][cb
].run
= 1;
413 path
[swb
+1][cb
].prev_idx
= cb
;
414 path
[swb
+1][cb
].cost
= cost_stay_here
;
415 path
[swb
+1][cb
].run
= path
[swb
][cb
].run
+ 1;
417 if (path
[swb
+1][cb
].cost
< next_minrd
) {
418 next_minrd
= path
[swb
+1][cb
].cost
;
423 start
+= sce
->ics
.swb_sizes
[swb
];
426 //convert resulting path from backward-linked list
429 for (cb
= 1; cb
< 12; cb
++)
430 if (path
[max_sfb
][cb
].cost
< path
[max_sfb
][idx
].cost
)
435 stackrun
[stack_len
] = path
[ppos
][cb
].run
;
436 stackcb
[stack_len
] = cb
;
437 idx
= path
[ppos
-path
[ppos
][cb
].run
+1][cb
].prev_idx
;
438 ppos
-= path
[ppos
][cb
].run
;
441 //perform actual band info encoding
443 for (i
= stack_len
- 1; i
>= 0; i
--) {
444 put_bits(&s
->pb
, 4, stackcb
[i
]);
446 memset(sce
->zeroes
+ win
*16 + start
, !stackcb
[i
], count
);
447 //XXX: memset when band_type is also uint8_t
448 for (j
= 0; j
< count
; j
++) {
449 sce
->band_type
[win
*16 + start
] = stackcb
[i
];
452 while (count
>= run_esc
) {
453 put_bits(&s
->pb
, run_bits
, run_esc
);
456 put_bits(&s
->pb
, run_bits
, count
);
460 static void encode_window_bands_info_fixed(AACEncContext
*s
,
461 SingleChannelElement
*sce
,
462 int win
, int group_len
,
465 encode_window_bands_info(s
, sce
, win
, group_len
, 1.0f
);
469 typedef struct TrellisPath
{
476 static void search_for_quantizers_anmr(AVCodecContext
*avctx
, AACEncContext
*s
,
477 SingleChannelElement
*sce
,
480 int q
, w
, w2
, g
, start
= 0;
483 TrellisPath paths
[256*121];
488 for (i
= 0; i
< 256; i
++) {
489 paths
[i
].cost
= 0.0f
;
491 paths
[i
].min_val
= i
;
492 paths
[i
].max_val
= i
;
494 for (i
= 256; i
< 256*121; i
++) {
495 paths
[i
].cost
= INFINITY
;
497 paths
[i
].min_val
= INT_MAX
;
498 paths
[i
].max_val
= 0;
501 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
502 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
504 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
505 const float *coefs
= sce
->coeffs
+ start
;
509 bandaddr
[idx
>> 8] = w
* 16 + g
;
512 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
513 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
514 if (band
->energy
<= band
->threshold
|| band
->threshold
== 0.0f
) {
515 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
518 sce
->zeroes
[(w
+w2
)*16+g
] = 0;
520 for (i
= 0; i
< sce
->ics
.swb_sizes
[g
]; i
++) {
521 float t
= fabsf(coefs
[w2
*128+i
]);
523 qmin
= fminf(qmin
, t
);
524 qmax
= fmaxf(qmax
, t
);
528 int minscale
, maxscale
;
529 float minrd
= INFINITY
;
530 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
531 minscale
= av_clip_uint8(log2(qmin
)*4 - 69 + SCALE_ONE_POS
- SCALE_DIV_512
);
532 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
533 maxscale
= av_clip_uint8(log2(qmax
)*4 + 6 + SCALE_ONE_POS
- SCALE_DIV_512
);
534 for (q
= minscale
; q
< maxscale
; q
++) {
535 float dists
[12], dist
;
536 memset(dists
, 0, sizeof(dists
));
537 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
538 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
540 for (cb
= 0; cb
<= ESC_BT
; cb
++)
541 dists
[cb
] += quantize_band_cost(s
, coefs
+ w2
*128, s
->scoefs
+ start
+ w2
*128, sce
->ics
.swb_sizes
[g
],
542 q
, cb
, lambda
/ band
->threshold
, INFINITY
, NULL
);
545 for (i
= 1; i
<= ESC_BT
; i
++)
546 dist
= fminf(dist
, dists
[i
]);
547 minrd
= fminf(minrd
, dist
);
549 for (i
= FFMAX(q
- SCALE_MAX_DIFF
, 0); i
< FFMIN(q
+ SCALE_MAX_DIFF
, 256); i
++) {
552 if (isinf(paths
[idx
- 256 + i
].cost
))
554 cost
= paths
[idx
- 256 + i
].cost
+ dist
555 + ff_aac_scalefactor_bits
[q
- i
+ SCALE_DIFF_ZERO
];
556 minv
= FFMIN(paths
[idx
- 256 + i
].min_val
, q
);
557 maxv
= FFMAX(paths
[idx
- 256 + i
].max_val
, q
);
558 if (cost
< paths
[idx
+ q
].cost
&& maxv
-minv
< SCALE_MAX_DIFF
) {
559 paths
[idx
+ q
].cost
= cost
;
560 paths
[idx
+ q
].prev
= idx
- 256 + i
;
561 paths
[idx
+ q
].min_val
= minv
;
562 paths
[idx
+ q
].max_val
= maxv
;
567 for (q
= 0; q
< 256; q
++) {
568 if (!isinf(paths
[idx
- 256 + q
].cost
)) {
569 paths
[idx
+ q
].cost
= paths
[idx
- 256 + q
].cost
+ 1;
570 paths
[idx
+ q
].prev
= idx
- 256 + q
;
571 paths
[idx
+ q
].min_val
= FFMIN(paths
[idx
- 256 + q
].min_val
, q
);
572 paths
[idx
+ q
].max_val
= FFMAX(paths
[idx
- 256 + q
].max_val
, q
);
575 for (i
= FFMAX(q
- SCALE_MAX_DIFF
, 0); i
< FFMIN(q
+ SCALE_MAX_DIFF
, 256); i
++) {
578 if (isinf(paths
[idx
- 256 + i
].cost
))
580 cost
= paths
[idx
- 256 + i
].cost
+ ff_aac_scalefactor_bits
[q
- i
+ SCALE_DIFF_ZERO
];
581 minv
= FFMIN(paths
[idx
- 256 + i
].min_val
, q
);
582 maxv
= FFMAX(paths
[idx
- 256 + i
].max_val
, q
);
583 if (cost
< paths
[idx
+ q
].cost
&& maxv
-minv
< SCALE_MAX_DIFF
) {
584 paths
[idx
+ q
].cost
= cost
;
585 paths
[idx
+ q
].prev
= idx
- 256 + i
;
586 paths
[idx
+ q
].min_val
= minv
;
587 paths
[idx
+ q
].max_val
= maxv
;
592 sce
->zeroes
[w
*16+g
] = !nz
;
593 start
+= sce
->ics
.swb_sizes
[g
];
598 mincost
= paths
[idx
].cost
;
600 for (i
= 1; i
< 256; i
++) {
601 if (paths
[idx
+ i
].cost
< mincost
) {
602 mincost
= paths
[idx
+ i
].cost
;
606 while (minq
>= 256) {
607 sce
->sf_idx
[bandaddr
[minq
>>8]] = minq
& 0xFF;
608 minq
= paths
[minq
].prev
;
610 //set the same quantizers inside window groups
611 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
])
612 for (g
= 0; g
< sce
->ics
.num_swb
; g
++)
613 for (w2
= 1; w2
< sce
->ics
.group_len
[w
]; w2
++)
614 sce
->sf_idx
[(w
+w2
)*16+g
] = sce
->sf_idx
[w
*16+g
];
618 * two-loop quantizers search taken from ISO 13818-7 Appendix C
620 static void search_for_quantizers_twoloop(AVCodecContext
*avctx
,
622 SingleChannelElement
*sce
,
625 int start
= 0, i
, w
, w2
, g
;
626 int destbits
= avctx
->bit_rate
* 1024.0 / avctx
->sample_rate
/ avctx
->channels
;
627 float dists
[128], uplims
[128];
628 int fflag
, minscaler
;
631 float minthr
= INFINITY
;
633 //XXX: some heuristic to determine initial quantizers will reduce search time
634 memset(dists
, 0, sizeof(dists
));
635 //determine zero bands and upper limits
636 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
637 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
640 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
641 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
642 uplim
+= band
->threshold
;
643 if (band
->energy
<= band
->threshold
|| band
->threshold
== 0.0f
) {
644 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
649 uplims
[w
*16+g
] = uplim
*512;
650 sce
->zeroes
[w
*16+g
] = !nz
;
652 minthr
= fminf(minthr
, uplim
);
653 allz
= FFMAX(allz
, nz
);
656 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 if (sce
->zeroes
[w
*16+g
]) {
659 sce
->sf_idx
[w
*16+g
] = SCALE_ONE_POS
;
662 sce
->sf_idx
[w
*16+g
] = SCALE_ONE_POS
+ fminf(log2(uplims
[w
*16+g
]/minthr
)*4,59);
668 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
669 //perform two-loop search
670 //outer loop - improve quality
673 minscaler
= sce
->sf_idx
[0];
674 //inner loop - quantize spectrum to fit into given number of bits
675 qstep
= its ?
1 : 32;
680 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
682 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
683 const float *coefs
= sce
->coeffs
+ start
;
684 const float *scaled
= s
->scoefs
+ start
;
687 float mindist
= INFINITY
;
690 if (sce
->zeroes
[w
*16+g
] || sce
->sf_idx
[w
*16+g
] >= 218)
692 minscaler
= FFMIN(minscaler
, sce
->sf_idx
[w
*16+g
]);
693 for (cb
= 0; cb
<= ESC_BT
; cb
++) {
696 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
698 dist
+= quantize_band_cost(s
, coefs
+ w2
*128,
700 sce
->ics
.swb_sizes
[g
],
708 if (dist
< mindist
) {
713 dists
[w
*16+g
] = mindist
- minbits
;
716 bits
+= ff_aac_scalefactor_bits
[sce
->sf_idx
[w
*16+g
] - prev
+ SCALE_DIFF_ZERO
];
719 start
+= sce
->ics
.swb_sizes
[g
];
720 prev
= sce
->sf_idx
[w
*16+g
];
723 if (tbits
> destbits
) {
724 for (i
= 0; i
< 128; i
++)
725 if (sce
->sf_idx
[i
] < 218 - qstep
)
726 sce
->sf_idx
[i
] += qstep
;
728 for (i
= 0; i
< 128; i
++)
729 if (sce
->sf_idx
[i
] > 60 - qstep
)
730 sce
->sf_idx
[i
] -= qstep
;
733 if (!qstep
&& tbits
> destbits
*1.02)
735 if (sce
->sf_idx
[0] >= 217)
740 minscaler
= av_clip(minscaler
, 60, 255 - SCALE_MAX_DIFF
);
741 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
743 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
744 int prevsc
= sce
->sf_idx
[w
*16+g
];
745 if (dists
[w
*16+g
] > uplims
[w
*16+g
] && sce
->sf_idx
[w
*16+g
] > 60)
746 sce
->sf_idx
[w
*16+g
]--;
747 sce
->sf_idx
[w
*16+g
] = av_clip(sce
->sf_idx
[w
*16+g
], minscaler
, minscaler
+ SCALE_MAX_DIFF
);
748 sce
->sf_idx
[w
*16+g
] = FFMIN(sce
->sf_idx
[w
*16+g
], 219);
749 if (sce
->sf_idx
[w
*16+g
] != prevsc
)
754 } while (fflag
&& its
< 10);
757 static void search_for_quantizers_faac(AVCodecContext
*avctx
, AACEncContext
*s
,
758 SingleChannelElement
*sce
,
761 int start
= 0, i
, w
, w2
, g
;
762 float uplim
[128], maxq
[128];
764 float distfact
= ((sce
->ics
.num_windows
> 1) ?
85.80 : 147.84) / lambda
;
765 int last
= 0, lastband
= 0, curband
= 0;
766 float avg_energy
= 0.0;
767 if (sce
->ics
.num_windows
== 1) {
769 for (i
= 0; i
< 1024; i
++) {
770 if (i
- start
>= sce
->ics
.swb_sizes
[curband
]) {
771 start
+= sce
->ics
.swb_sizes
[curband
];
774 if (sce
->coeffs
[i
]) {
775 avg_energy
+= sce
->coeffs
[i
] * sce
->coeffs
[i
];
781 for (w
= 0; w
< 8; w
++) {
782 const float *coeffs
= sce
->coeffs
+ w
*128;
784 for (i
= 0; i
< 128; i
++) {
785 if (i
- start
>= sce
->ics
.swb_sizes
[curband
]) {
786 start
+= sce
->ics
.swb_sizes
[curband
];
790 avg_energy
+= coeffs
[i
] * coeffs
[i
];
791 last
= FFMAX(last
, i
);
792 lastband
= FFMAX(lastband
, curband
);
799 if (avg_energy
== 0.0f
) {
800 for (i
= 0; i
< FF_ARRAY_ELEMS(sce
->sf_idx
); i
++)
801 sce
->sf_idx
[i
] = SCALE_ONE_POS
;
804 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
806 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
807 float *coefs
= sce
->coeffs
+ start
;
808 const int size
= sce
->ics
.swb_sizes
[g
];
809 int start2
= start
, end2
= start
+ size
, peakpos
= start
;
810 float maxval
= -1, thr
= 0.0f
, t
;
815 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++)
816 memset(coefs
+ w2
*128, 0, sizeof(coefs
[0])*size
);
819 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
820 for (i
= 0; i
< size
; i
++) {
821 float t
= coefs
[w2
*128+i
]*coefs
[w2
*128+i
];
822 maxq
[w
*16+g
] = fmaxf(maxq
[w
*16+g
], fabsf(coefs
[w2
*128 + i
]));
824 if (sce
->ics
.num_windows
== 1 && maxval
< t
) {
830 if (sce
->ics
.num_windows
== 1) {
831 start2
= FFMAX(peakpos
- 2, start2
);
832 end2
= FFMIN(peakpos
+ 3, end2
);
838 thr
= pow(thr
/ (avg_energy
* (end2
- start2
)), 0.3 + 0.1*(lastband
- g
) / lastband
);
839 t
= 1.0 - (1.0 * start2
/ last
);
840 uplim
[w
*16+g
] = distfact
/ (1.4 * thr
+ t
*t
*t
+ 0.075);
843 memset(sce
->sf_idx
, 0, sizeof(sce
->sf_idx
));
844 abs_pow34_v(s
->scoefs
, sce
->coeffs
, 1024);
845 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
847 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
848 const float *coefs
= sce
->coeffs
+ start
;
849 const float *scaled
= s
->scoefs
+ start
;
850 const int size
= sce
->ics
.swb_sizes
[g
];
851 int scf
, prev_scf
, step
;
852 int min_scf
= 0, max_scf
= 255;
854 if (maxq
[w
*16+g
] < 21.544) {
855 sce
->zeroes
[w
*16+g
] = 1;
859 sce
->zeroes
[w
*16+g
] = 0;
860 scf
= prev_scf
= av_clip(SCALE_ONE_POS
- SCALE_DIV_512
- log2(1/maxq
[w
*16+g
])*16/3, 60, 218);
866 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
868 dist
+= quantize_band_cost(s
, coefs
+ w2
*128,
870 sce
->ics
.swb_sizes
[g
],
879 quant_max
= quant(maxq
[w
*16+g
], ff_aac_pow2sf_tab
[200 - scf
+ SCALE_ONE_POS
- SCALE_DIV_512
]);
880 if (quant_max
>= 8191) { // too much, return to the previous quantizer
881 sce
->sf_idx
[w
*16+g
] = prev_scf
;
885 curdiff
= fabsf(dist
- uplim
[w
*16+g
]);
889 step
= fabsf(log2(curdiff
));
890 if (dist
> uplim
[w
*16+g
])
892 if (FFABS(step
) <= 1 || (step
> 0 && scf
>= max_scf
) || (step
< 0 && scf
<= min_scf
)) {
893 sce
->sf_idx
[w
*16+g
] = scf
;
905 minq
= sce
->sf_idx
[0] ? sce
->sf_idx
[0] : INT_MAX
;
906 for (i
= 1; i
< 128; i
++) {
908 sce
->sf_idx
[i
] = sce
->sf_idx
[i
-1];
910 minq
= FFMIN(minq
, sce
->sf_idx
[i
]);
914 minq
= FFMIN(minq
, SCALE_MAX_POS
);
915 maxsf
= FFMIN(minq
+ SCALE_MAX_DIFF
, SCALE_MAX_POS
);
916 for (i
= 126; i
>= 0; i
--) {
918 sce
->sf_idx
[i
] = sce
->sf_idx
[i
+1];
919 sce
->sf_idx
[i
] = av_clip(sce
->sf_idx
[i
], minq
, maxsf
);
923 static void search_for_quantizers_fast(AVCodecContext
*avctx
, AACEncContext
*s
,
924 SingleChannelElement
*sce
,
927 int start
= 0, i
, w
, w2
, g
;
930 memset(sce
->sf_idx
, 0, sizeof(sce
->sf_idx
));
931 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
933 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
934 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
935 FFPsyBand
*band
= &s
->psy
.psy_bands
[s
->cur_channel
*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
936 if (band
->energy
<= band
->threshold
) {
937 sce
->sf_idx
[(w
+w2
)*16+g
] = 218;
938 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
940 sce
->sf_idx
[(w
+w2
)*16+g
] = av_clip(SCALE_ONE_POS
- SCALE_DIV_512
+ log2(band
->threshold
), 80, 218);
941 sce
->zeroes
[(w
+w2
)*16+g
] = 0;
943 minq
= FFMIN(minq
, sce
->sf_idx
[(w
+w2
)*16+g
]);
947 for (i
= 0; i
< 128; i
++) {
948 sce
->sf_idx
[i
] = 140;
949 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
951 //set the same quantizers inside window groups
952 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
])
953 for (g
= 0; g
< sce
->ics
.num_swb
; g
++)
954 for (w2
= 1; w2
< sce
->ics
.group_len
[w
]; w2
++)
955 sce
->sf_idx
[(w
+w2
)*16+g
] = sce
->sf_idx
[w
*16+g
];
958 static void search_for_ms(AACEncContext
*s
, ChannelElement
*cpe
,
961 int start
= 0, i
, w
, w2
, g
;
962 float M
[128], S
[128];
963 float *L34
= s
->scoefs
, *R34
= s
->scoefs
+ 128, *M34
= s
->scoefs
+ 128*2, *S34
= s
->scoefs
+ 128*3;
964 SingleChannelElement
*sce0
= &cpe
->ch
[0];
965 SingleChannelElement
*sce1
= &cpe
->ch
[1];
966 if (!cpe
->common_window
)
968 for (w
= 0; w
< sce0
->ics
.num_windows
; w
+= sce0
->ics
.group_len
[w
]) {
969 for (g
= 0; g
< sce0
->ics
.num_swb
; g
++) {
970 if (!cpe
->ch
[0].zeroes
[w
*16+g
] && !cpe
->ch
[1].zeroes
[w
*16+g
]) {
971 float dist1
= 0.0f
, dist2
= 0.0f
;
972 for (w2
= 0; w2
< sce0
->ics
.group_len
[w
]; w2
++) {
973 FFPsyBand
*band0
= &s
->psy
.psy_bands
[(s
->cur_channel
+0)*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
974 FFPsyBand
*band1
= &s
->psy
.psy_bands
[(s
->cur_channel
+1)*PSY_MAX_BANDS
+(w
+w2
)*16+g
];
975 float minthr
= fminf(band0
->threshold
, band1
->threshold
);
976 float maxthr
= fmaxf(band0
->threshold
, band1
->threshold
);
977 for (i
= 0; i
< sce0
->ics
.swb_sizes
[g
]; i
++) {
978 M
[i
] = (sce0
->coeffs
[start
+w2
*128+i
]
979 + sce1
->coeffs
[start
+w2
*128+i
]) * 0.5;
980 S
[i
] = sce0
->coeffs
[start
+w2
*128+i
]
981 - sce1
->coeffs
[start
+w2
*128+i
];
983 abs_pow34_v(L34
, sce0
->coeffs
+start
+w2
*128, sce0
->ics
.swb_sizes
[g
]);
984 abs_pow34_v(R34
, sce1
->coeffs
+start
+w2
*128, sce0
->ics
.swb_sizes
[g
]);
985 abs_pow34_v(M34
, M
, sce0
->ics
.swb_sizes
[g
]);
986 abs_pow34_v(S34
, S
, sce0
->ics
.swb_sizes
[g
]);
987 dist1
+= quantize_band_cost(s
, sce0
->coeffs
+ start
+ w2
*128,
989 sce0
->ics
.swb_sizes
[g
],
990 sce0
->sf_idx
[(w
+w2
)*16+g
],
991 sce0
->band_type
[(w
+w2
)*16+g
],
992 lambda
/ band0
->threshold
, INFINITY
, NULL
);
993 dist1
+= quantize_band_cost(s
, sce1
->coeffs
+ start
+ w2
*128,
995 sce1
->ics
.swb_sizes
[g
],
996 sce1
->sf_idx
[(w
+w2
)*16+g
],
997 sce1
->band_type
[(w
+w2
)*16+g
],
998 lambda
/ band1
->threshold
, INFINITY
, NULL
);
999 dist2
+= quantize_band_cost(s
, M
,
1001 sce0
->ics
.swb_sizes
[g
],
1002 sce0
->sf_idx
[(w
+w2
)*16+g
],
1003 sce0
->band_type
[(w
+w2
)*16+g
],
1004 lambda
/ maxthr
, INFINITY
, NULL
);
1005 dist2
+= quantize_band_cost(s
, S
,
1007 sce1
->ics
.swb_sizes
[g
],
1008 sce1
->sf_idx
[(w
+w2
)*16+g
],
1009 sce1
->band_type
[(w
+w2
)*16+g
],
1010 lambda
/ minthr
, INFINITY
, NULL
);
1012 cpe
->ms_mask
[w
*16+g
] = dist2
< dist1
;
1014 start
+= sce0
->ics
.swb_sizes
[g
];
1019 AACCoefficientsEncoder ff_aac_coders
[] = {
1021 search_for_quantizers_faac
,
1022 encode_window_bands_info_fixed
,
1023 quantize_and_encode_band
,
1027 search_for_quantizers_anmr
,
1028 encode_window_bands_info
,
1029 quantize_and_encode_band
,
1033 search_for_quantizers_twoloop
,
1034 encode_window_bands_info
,
1035 quantize_and_encode_band
,
1039 search_for_quantizers_fast
,
1040 encode_window_bands_info
,
1041 quantize_and_encode_band
,