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 ***********************************/
33 #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
37 #include "libavutil/mathematics.h"
44 #include "aacenctab.h"
45 #include "aacenc_utils.h"
46 #include "aacenc_quantization.h"
48 #include "aacenc_is.h"
49 #include "aacenc_tns.h"
50 #include "aacenc_ltp.h"
51 #include "aacenc_pred.h"
53 #include "libavcodec/aaccoder_twoloop.h"
55 /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread
56 * beyond which no PNS is used (since the SFBs contain tone rather than noise) */
57 #define NOISE_SPREAD_THRESHOLD 0.9f
59 /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to
60 * replace low energy non zero bands */
61 #define NOISE_LAMBDA_REPLACE 1.948f
63 #include "libavcodec/aaccoder_trellis.h"
65 typedef float (*quantize_and_encode_band_func
)(struct AACEncContext
*s
, PutBitContext
*pb
,
66 const float *in
, float *quant
, const float *scaled
,
67 int size
, int scale_idx
, int cb
,
68 const float lambda
, const float uplim
,
69 int *bits
, float *energy
);
72 * Calculate rate distortion cost for quantizing with given codebook
74 * @return quantization distortion
76 static av_always_inline
float quantize_and_encode_band_cost_template(
77 struct AACEncContext
*s
,
78 PutBitContext
*pb
, const float *in
, float *out
,
79 const float *scaled
, int size
, int scale_idx
,
80 int cb
, const float lambda
, const float uplim
,
81 int *bits
, float *energy
, int BT_ZERO
, int BT_UNSIGNED
,
82 int BT_PAIR
, int BT_ESC
, int BT_NOISE
, int BT_STEREO
,
85 const int q_idx
= POW_SF2_ZERO
- scale_idx
+ SCALE_ONE_POS
- SCALE_DIV_512
;
86 const float Q
= ff_aac_pow2sf_tab
[q_idx
];
87 const float Q34
= ff_aac_pow34sf_tab
[q_idx
];
88 const float IQ
= ff_aac_pow2sf_tab
[POW_SF2_ZERO
+ scale_idx
- SCALE_ONE_POS
+ SCALE_DIV_512
];
89 const float CLIPPED_ESCAPE
= 165140.0f
*IQ
;
92 const int dim
= BT_PAIR
? 2 : 4;
96 if (BT_ZERO
|| BT_NOISE
|| BT_STEREO
) {
97 for (int i
= 0; i
< size
; i
++)
104 for (int i
= 0; i
< size
; i
+= dim
)
105 for (int j
= 0; j
< dim
; j
++)
108 return cost
* lambda
;
111 s
->aacdsp
.abs_pow34(s
->scoefs
, in
, size
);
114 s
->aacdsp
.quant_bands(s
->qcoefs
, in
, scaled
, size
, !BT_UNSIGNED
, aac_cb_maxval
[cb
], Q34
, ROUNDING
);
118 off
= aac_cb_maxval
[cb
];
120 for (int i
= 0; i
< size
; i
+= dim
) {
122 int *quants
= s
->qcoefs
+ i
;
125 float quantized
, rd
= 0.0f
;
126 for (int j
= 0; j
< dim
; j
++) {
127 curidx
*= aac_cb_range
[cb
];
128 curidx
+= quants
[j
] + off
;
130 curbits
= ff_aac_spectral_bits
[cb
-1][curidx
];
131 vec
= &ff_aac_codebook_vectors
[cb
-1][curidx
*dim
];
133 for (int j
= 0; j
< dim
; j
++) {
134 float t
= fabsf(in
[i
+j
]);
136 if (BT_ESC
&& vec
[j
] == 64.0f
) { //FIXME: slow
137 if (t
>= CLIPPED_ESCAPE
) {
138 quantized
= CLIPPED_ESCAPE
;
141 int c
= av_clip_uintp2(quant(t
, Q
, ROUNDING
), 13);
142 quantized
= c
*cbrtf(c
)*IQ
;
143 curbits
+= av_log2(c
)*2 - 4 + 1;
146 quantized
= vec
[j
]*IQ
;
150 out
[i
+j
] = in
[i
+j
] >= 0 ? quantized
: -quantized
;
153 qenergy
+= quantized
*quantized
;
157 for (int j
= 0; j
< dim
; j
++) {
158 quantized
= vec
[j
]*IQ
;
159 qenergy
+= quantized
*quantized
;
161 out
[i
+j
] = quantized
;
162 rd
+= (in
[i
+j
] - quantized
)*(in
[i
+j
] - quantized
);
165 cost
+= rd
* lambda
+ curbits
;
170 put_bits(pb
, ff_aac_spectral_bits
[cb
-1][curidx
], ff_aac_spectral_codes
[cb
-1][curidx
]);
172 for (int j
= 0; j
< dim
; j
++)
173 if (ff_aac_codebook_vectors
[cb
-1][curidx
*dim
+j
] != 0.0f
)
174 put_bits(pb
, 1, in
[i
+j
] < 0.0f
);
176 for (int j
= 0; j
< 2; j
++) {
177 if (ff_aac_codebook_vectors
[cb
-1][curidx
*2+j
] == 64.0f
) {
178 int coef
= av_clip_uintp2(quant(fabsf(in
[i
+j
]), Q
, ROUNDING
), 13);
179 int len
= av_log2(coef
);
181 put_bits(pb
, len
- 4 + 1, (1 << (len
- 4 + 1)) - 2);
182 put_sbits(pb
, len
, coef
);
196 static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext
*s
, PutBitContext
*pb
,
197 const float *in
, float *quant
, const float *scaled
,
198 int size
, int scale_idx
, int cb
,
199 const float lambda
, const float uplim
,
200 int *bits
, float *energy
) {
205 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
206 static float quantize_and_encode_band_cost_ ## NAME( \
207 struct AACEncContext *s, \
208 PutBitContext *pb, const float *in, float *quant, \
209 const float *scaled, int size, int scale_idx, \
210 int cb, const float lambda, const float uplim, \
211 int *bits, float *energy) { \
212 return quantize_and_encode_band_cost_template( \
213 s, pb, in, quant, scaled, size, scale_idx, \
214 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
215 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
219 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO
, 1, 0, 0, 0, 0, 0, ROUND_STANDARD
)
220 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD
, 0, 0, 0, 0, 0, 0, ROUND_STANDARD
)
221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD
, 0, 1, 0, 0, 0, 0, ROUND_STANDARD
)
222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR
, 0, 0, 1, 0, 0, 0, ROUND_STANDARD
)
223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR
, 0, 1, 1, 0, 0, 0, ROUND_STANDARD
)
224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC
, 0, 1, 1, 1, 0, 0, ROUND_STANDARD
)
225 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ
, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO
)
226 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE
, 0, 0, 0, 0, 1, 0, ROUND_STANDARD
)
227 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO
,0, 0, 0, 0, 0, 1, ROUND_STANDARD
)
229 static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr
[] =
231 quantize_and_encode_band_cost_ZERO
,
232 quantize_and_encode_band_cost_SQUAD
,
233 quantize_and_encode_band_cost_SQUAD
,
234 quantize_and_encode_band_cost_UQUAD
,
235 quantize_and_encode_band_cost_UQUAD
,
236 quantize_and_encode_band_cost_SPAIR
,
237 quantize_and_encode_band_cost_SPAIR
,
238 quantize_and_encode_band_cost_UPAIR
,
239 quantize_and_encode_band_cost_UPAIR
,
240 quantize_and_encode_band_cost_UPAIR
,
241 quantize_and_encode_band_cost_UPAIR
,
242 quantize_and_encode_band_cost_ESC
,
243 quantize_and_encode_band_cost_NONE
, /* CB 12 doesn't exist */
244 quantize_and_encode_band_cost_NOISE
,
245 quantize_and_encode_band_cost_STEREO
,
246 quantize_and_encode_band_cost_STEREO
,
249 static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr
[] =
251 quantize_and_encode_band_cost_ZERO
,
252 quantize_and_encode_band_cost_SQUAD
,
253 quantize_and_encode_band_cost_SQUAD
,
254 quantize_and_encode_band_cost_UQUAD
,
255 quantize_and_encode_band_cost_UQUAD
,
256 quantize_and_encode_band_cost_SPAIR
,
257 quantize_and_encode_band_cost_SPAIR
,
258 quantize_and_encode_band_cost_UPAIR
,
259 quantize_and_encode_band_cost_UPAIR
,
260 quantize_and_encode_band_cost_UPAIR
,
261 quantize_and_encode_band_cost_UPAIR
,
262 quantize_and_encode_band_cost_ESC_RTZ
,
263 quantize_and_encode_band_cost_NONE
, /* CB 12 doesn't exist */
264 quantize_and_encode_band_cost_NOISE
,
265 quantize_and_encode_band_cost_STEREO
,
266 quantize_and_encode_band_cost_STEREO
,
269 float ff_quantize_and_encode_band_cost(struct AACEncContext
*s
, PutBitContext
*pb
,
270 const float *in
, float *quant
, const float *scaled
,
271 int size
, int scale_idx
, int cb
,
272 const float lambda
, const float uplim
,
273 int *bits
, float *energy
)
275 return quantize_and_encode_band_cost_arr
[cb
](s
, pb
, in
, quant
, scaled
, size
,
276 scale_idx
, cb
, lambda
, uplim
,
280 static inline void quantize_and_encode_band(struct AACEncContext
*s
, PutBitContext
*pb
,
281 const float *in
, float *out
, int size
, int scale_idx
,
282 int cb
, const float lambda
, int rtz
)
284 (rtz
? quantize_and_encode_band_cost_rtz_arr
: quantize_and_encode_band_cost_arr
)[cb
](s
, pb
, in
, out
, NULL
, size
, scale_idx
, cb
,
285 lambda
, INFINITY
, NULL
, NULL
);
289 * structure used in optimal codebook search
291 typedef struct BandCodingPath
{
292 int prev_idx
; ///< pointer to the previous path point
293 float cost
; ///< path cost
298 * Encode band info for single window group bands.
300 static void encode_window_bands_info(AACEncContext
*s
, SingleChannelElement
*sce
,
301 int win
, int group_len
, const float lambda
)
303 BandCodingPath path
[120][CB_TOT_ALL
];
304 int w
, swb
, cb
, start
, size
;
306 const int max_sfb
= sce
->ics
.max_sfb
;
307 const int run_bits
= sce
->ics
.num_windows
== 1 ? 5 : 3;
308 const int run_esc
= (1 << run_bits
) - 1;
309 int idx
, ppos
, count
;
310 int stackrun
[120], stackcb
[120], stack_len
;
311 float next_minrd
= INFINITY
;
314 s
->aacdsp
.abs_pow34(s
->scoefs
, sce
->coeffs
, 1024);
316 for (cb
= 0; cb
< CB_TOT_ALL
; cb
++) {
317 path
[0][cb
].cost
= 0.0f
;
318 path
[0][cb
].prev_idx
= -1;
321 for (swb
= 0; swb
< max_sfb
; swb
++) {
322 size
= sce
->ics
.swb_sizes
[swb
];
323 if (sce
->zeroes
[win
*16 + swb
]) {
324 for (cb
= 0; cb
< CB_TOT_ALL
; cb
++) {
325 path
[swb
+1][cb
].prev_idx
= cb
;
326 path
[swb
+1][cb
].cost
= path
[swb
][cb
].cost
;
327 path
[swb
+1][cb
].run
= path
[swb
][cb
].run
+ 1;
330 float minrd
= next_minrd
;
331 int mincb
= next_mincb
;
332 next_minrd
= INFINITY
;
334 for (cb
= 0; cb
< CB_TOT_ALL
; cb
++) {
335 float cost_stay_here
, cost_get_here
;
337 if (cb
>= 12 && sce
->band_type
[win
*16+swb
] < aac_cb_out_map
[cb
] ||
338 cb
< aac_cb_in_map
[sce
->band_type
[win
*16+swb
]] && sce
->band_type
[win
*16+swb
] > aac_cb_out_map
[cb
]) {
339 path
[swb
+1][cb
].prev_idx
= -1;
340 path
[swb
+1][cb
].cost
= INFINITY
;
341 path
[swb
+1][cb
].run
= path
[swb
][cb
].run
+ 1;
344 for (w
= 0; w
< group_len
; w
++) {
345 FFPsyBand
*band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(win
+w
)*16+swb
];
346 rd
+= quantize_band_cost(s
, &sce
->coeffs
[start
+ w
*128],
347 &s
->scoefs
[start
+ w
*128], size
,
348 sce
->sf_idx
[(win
+w
)*16+swb
], aac_cb_out_map
[cb
],
349 lambda
/ band
->threshold
, INFINITY
, NULL
, NULL
);
351 cost_stay_here
= path
[swb
][cb
].cost
+ rd
;
352 cost_get_here
= minrd
+ rd
+ run_bits
+ 4;
353 if ( run_value_bits
[sce
->ics
.num_windows
== 8][path
[swb
][cb
].run
]
354 != run_value_bits
[sce
->ics
.num_windows
== 8][path
[swb
][cb
].run
+1])
355 cost_stay_here
+= run_bits
;
356 if (cost_get_here
< cost_stay_here
) {
357 path
[swb
+1][cb
].prev_idx
= mincb
;
358 path
[swb
+1][cb
].cost
= cost_get_here
;
359 path
[swb
+1][cb
].run
= 1;
361 path
[swb
+1][cb
].prev_idx
= cb
;
362 path
[swb
+1][cb
].cost
= cost_stay_here
;
363 path
[swb
+1][cb
].run
= path
[swb
][cb
].run
+ 1;
365 if (path
[swb
+1][cb
].cost
< next_minrd
) {
366 next_minrd
= path
[swb
+1][cb
].cost
;
371 start
+= sce
->ics
.swb_sizes
[swb
];
374 //convert resulting path from backward-linked list
377 for (cb
= 1; cb
< CB_TOT_ALL
; cb
++)
378 if (path
[max_sfb
][cb
].cost
< path
[max_sfb
][idx
].cost
)
382 av_assert1(idx
>= 0);
384 stackrun
[stack_len
] = path
[ppos
][cb
].run
;
385 stackcb
[stack_len
] = cb
;
386 idx
= path
[ppos
-path
[ppos
][cb
].run
+1][cb
].prev_idx
;
387 ppos
-= path
[ppos
][cb
].run
;
390 //perform actual band info encoding
392 for (i
= stack_len
- 1; i
>= 0; i
--) {
393 cb
= aac_cb_out_map
[stackcb
[i
]];
394 put_bits(&s
->pb
, 4, cb
);
396 memset(sce
->zeroes
+ win
*16 + start
, !cb
, count
);
397 //XXX: memset when band_type is also uint8_t
398 for (j
= 0; j
< count
; j
++) {
399 sce
->band_type
[win
*16 + start
] = cb
;
402 while (count
>= run_esc
) {
403 put_bits(&s
->pb
, run_bits
, run_esc
);
406 put_bits(&s
->pb
, run_bits
, count
);
411 typedef struct TrellisPath
{
416 #define TRELLIS_STAGES 121
417 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
419 static void set_special_band_scalefactors(AACEncContext
*s
, SingleChannelElement
*sce
)
422 int prevscaler_n
= -255, prevscaler_i
= 0;
425 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
426 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
427 if (sce
->zeroes
[w
*16+g
])
429 if (sce
->band_type
[w
*16+g
] == INTENSITY_BT
|| sce
->band_type
[w
*16+g
] == INTENSITY_BT2
) {
430 sce
->sf_idx
[w
*16+g
] = av_clip(roundf(log2f(sce
->is_ener
[w
*16+g
])*2), -155, 100);
432 } else if (sce
->band_type
[w
*16+g
] == NOISE_BT
) {
433 sce
->sf_idx
[w
*16+g
] = av_clip(3+ceilf(log2f(sce
->pns_ener
[w
*16+g
])*2), -100, 155);
434 if (prevscaler_n
== -255)
435 prevscaler_n
= sce
->sf_idx
[w
*16+g
];
444 /* Clip the scalefactor indices */
445 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
446 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
447 if (sce
->zeroes
[w
*16+g
])
449 if (sce
->band_type
[w
*16+g
] == INTENSITY_BT
|| sce
->band_type
[w
*16+g
] == INTENSITY_BT2
) {
450 sce
->sf_idx
[w
*16+g
] = prevscaler_i
= av_clip(sce
->sf_idx
[w
*16+g
], prevscaler_i
- SCALE_MAX_DIFF
, prevscaler_i
+ SCALE_MAX_DIFF
);
451 } else if (sce
->band_type
[w
*16+g
] == NOISE_BT
) {
452 sce
->sf_idx
[w
*16+g
] = prevscaler_n
= av_clip(sce
->sf_idx
[w
*16+g
], prevscaler_n
- SCALE_MAX_DIFF
, prevscaler_n
+ SCALE_MAX_DIFF
);
458 static void search_for_quantizers_anmr(AVCodecContext
*avctx
, AACEncContext
*s
,
459 SingleChannelElement
*sce
,
462 int q
, w
, w2
, g
, start
= 0;
465 TrellisPath paths
[TRELLIS_STAGES
][TRELLIS_STATES
];
466 int bandaddr
[TRELLIS_STAGES
];
469 float q0f
= FLT_MAX
, q1f
= 0.0f
, qnrgf
= 0.0f
;
470 int q0
, q1
, qcnt
= 0;
472 for (i
= 0; i
< 1024; i
++) {
473 float t
= fabsf(sce
->coeffs
[i
]);
483 memset(sce
->sf_idx
, 0, sizeof(sce
->sf_idx
));
484 memset(sce
->zeroes
, 1, sizeof(sce
->zeroes
));
488 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
489 q0
= av_clip(coef2minsf(q0f
), 0, SCALE_MAX_POS
-1);
490 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
491 q1
= av_clip(coef2maxsf(q1f
), 1, SCALE_MAX_POS
);
495 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
496 int qnrg
= av_clip_uint8(log2f(sqrtf(qnrgf
/qcnt
))*4 - 31 + SCALE_ONE_POS
- SCALE_DIV_512
);
502 } else if (q1
> q1high
) {
507 // q0 == q1 isn't really a legal situation
509 // the following is indirect but guarantees q1 != q0 && q1 near q0
510 q1
= av_clip(q0
+1, 1, SCALE_MAX_POS
);
511 q0
= av_clip(q1
-1, 0, SCALE_MAX_POS
- 1);
514 for (i
= 0; i
< TRELLIS_STATES
; i
++) {
515 paths
[0][i
].cost
= 0.0f
;
516 paths
[0][i
].prev
= -1;
518 for (j
= 1; j
< TRELLIS_STAGES
; j
++) {
519 for (i
= 0; i
< TRELLIS_STATES
; i
++) {
520 paths
[j
][i
].cost
= INFINITY
;
521 paths
[j
][i
].prev
= -2;
525 s
->aacdsp
.abs_pow34(s
->scoefs
, sce
->coeffs
, 1024);
526 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
528 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
529 const float *coefs
= &sce
->coeffs
[start
];
533 bandaddr
[idx
] = w
* 16 + g
;
536 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
537 FFPsyBand
*band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(w
+w2
)*16+g
];
538 if (band
->energy
<= band
->threshold
|| band
->threshold
== 0.0f
) {
539 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
542 sce
->zeroes
[(w
+w2
)*16+g
] = 0;
544 for (i
= 0; i
< sce
->ics
.swb_sizes
[g
]; i
++) {
545 float t
= fabsf(coefs
[w2
*128+i
]);
547 qmin
= FFMIN(qmin
, t
);
548 qmax
= FFMAX(qmax
, t
);
552 int minscale
, maxscale
;
553 float minrd
= INFINITY
;
555 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
556 minscale
= coef2minsf(qmin
);
557 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
558 maxscale
= coef2maxsf(qmax
);
559 minscale
= av_clip(minscale
- q0
, 0, TRELLIS_STATES
- 1);
560 maxscale
= av_clip(maxscale
- q0
, 0, TRELLIS_STATES
);
561 if (minscale
== maxscale
) {
562 maxscale
= av_clip(minscale
+1, 1, TRELLIS_STATES
);
563 minscale
= av_clip(maxscale
-1, 0, TRELLIS_STATES
- 1);
565 maxval
= find_max_val(sce
->ics
.group_len
[w
], sce
->ics
.swb_sizes
[g
], s
->scoefs
+start
);
566 for (q
= minscale
; q
< maxscale
; q
++) {
568 int cb
= find_min_book(maxval
, sce
->sf_idx
[w
*16+g
]);
569 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
570 FFPsyBand
*band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(w
+w2
)*16+g
];
571 dist
+= quantize_band_cost(s
, coefs
+ w2
*128, s
->scoefs
+ start
+ w2
*128, sce
->ics
.swb_sizes
[g
],
572 q
+ q0
, cb
, lambda
/ band
->threshold
, INFINITY
, NULL
, NULL
);
574 minrd
= FFMIN(minrd
, dist
);
576 for (i
= 0; i
< q1
- q0
; i
++) {
578 cost
= paths
[idx
- 1][i
].cost
+ dist
579 + ff_aac_scalefactor_bits
[q
- i
+ SCALE_DIFF_ZERO
];
580 if (cost
< paths
[idx
][q
].cost
) {
581 paths
[idx
][q
].cost
= cost
;
582 paths
[idx
][q
].prev
= i
;
587 for (q
= 0; q
< q1
- q0
; q
++) {
588 paths
[idx
][q
].cost
= paths
[idx
- 1][q
].cost
+ 1;
589 paths
[idx
][q
].prev
= q
;
592 sce
->zeroes
[w
*16+g
] = !nz
;
593 start
+= sce
->ics
.swb_sizes
[g
];
598 mincost
= paths
[idx
][0].cost
;
600 for (i
= 1; i
< TRELLIS_STATES
; i
++) {
601 if (paths
[idx
][i
].cost
< mincost
) {
602 mincost
= paths
[idx
][i
].cost
;
607 sce
->sf_idx
[bandaddr
[idx
]] = minq
+ q0
;
608 minq
= FFMAX(paths
[idx
][minq
].prev
, 0);
611 //set the same quantizers inside window groups
612 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
])
613 for (g
= 0; g
< sce
->ics
.num_swb
; g
++)
614 for (w2
= 1; w2
< sce
->ics
.group_len
[w
]; w2
++)
615 sce
->sf_idx
[(w
+w2
)*16+g
] = sce
->sf_idx
[w
*16+g
];
618 static void search_for_quantizers_fast(AVCodecContext
*avctx
, AACEncContext
*s
,
619 SingleChannelElement
*sce
,
622 int start
= 0, i
, w
, w2
, g
;
623 int destbits
= avctx
->bit_rate
* 1024.0 / avctx
->sample_rate
/ avctx
->ch_layout
.nb_channels
* (lambda
/ 120.f
);
624 float dists
[128] = { 0 }, uplims
[128] = { 0 };
626 int fflag
, minscaler
;
629 float minthr
= INFINITY
;
631 // for values above this the decoder might end up in an endless loop
632 // due to always having more bits than what can be encoded.
633 destbits
= FFMIN(destbits
, 5800);
634 //some heuristic to determine initial quantizers will reduce search time
635 //determine zero bands and upper limits
636 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
638 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
641 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
642 FFPsyBand
*band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(w
+w2
)*16+g
];
643 uplim
+= band
->threshold
;
644 if (band
->energy
<= band
->threshold
|| band
->threshold
== 0.0f
) {
645 sce
->zeroes
[(w
+w2
)*16+g
] = 1;
650 uplims
[w
*16+g
] = uplim
*512;
651 sce
->band_type
[w
*16+g
] = 0;
652 sce
->zeroes
[w
*16+g
] = !nz
;
654 minthr
= FFMIN(minthr
, uplim
);
656 start
+= sce
->ics
.swb_sizes
[g
];
659 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
660 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
661 if (sce
->zeroes
[w
*16+g
]) {
662 sce
->sf_idx
[w
*16+g
] = SCALE_ONE_POS
;
665 sce
->sf_idx
[w
*16+g
] = SCALE_ONE_POS
+ FFMIN(log2f(uplims
[w
*16+g
]/minthr
)*4,59);
671 s
->aacdsp
.abs_pow34(s
->scoefs
, sce
->coeffs
, 1024);
672 ff_quantize_band_cost_cache_init(s
);
674 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
676 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
677 const float *scaled
= s
->scoefs
+ start
;
678 maxvals
[w
*16+g
] = find_max_val(sce
->ics
.group_len
[w
], sce
->ics
.swb_sizes
[g
], scaled
);
679 start
+= sce
->ics
.swb_sizes
[g
];
683 //perform two-loop search
684 //outer loop - improve quality
687 minscaler
= sce
->sf_idx
[0];
688 //inner loop - quantize spectrum to fit into given number of bits
689 qstep
= its
? 1 : 32;
693 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
695 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
696 const float *coefs
= sce
->coeffs
+ start
;
697 const float *scaled
= s
->scoefs
+ start
;
702 if (sce
->zeroes
[w
*16+g
] || sce
->sf_idx
[w
*16+g
] >= 218) {
703 start
+= sce
->ics
.swb_sizes
[g
];
706 minscaler
= FFMIN(minscaler
, sce
->sf_idx
[w
*16+g
]);
707 cb
= find_min_book(maxvals
[w
*16+g
], sce
->sf_idx
[w
*16+g
]);
708 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
710 dist
+= quantize_band_cost_cached(s
, w
+ w2
, g
,
713 sce
->ics
.swb_sizes
[g
],
719 dists
[w
*16+g
] = dist
- bits
;
721 bits
+= ff_aac_scalefactor_bits
[sce
->sf_idx
[w
*16+g
] - prev
+ SCALE_DIFF_ZERO
];
724 start
+= sce
->ics
.swb_sizes
[g
];
725 prev
= sce
->sf_idx
[w
*16+g
];
728 if (tbits
> destbits
) {
729 for (i
= 0; i
< 128; i
++)
730 if (sce
->sf_idx
[i
] < 218 - qstep
)
731 sce
->sf_idx
[i
] += qstep
;
733 for (i
= 0; i
< 128; i
++)
734 if (sce
->sf_idx
[i
] > 60 - qstep
)
735 sce
->sf_idx
[i
] -= qstep
;
738 if (!qstep
&& tbits
> destbits
*1.02 && sce
->sf_idx
[0] < 217)
743 minscaler
= av_clip(minscaler
, 60, 255 - SCALE_MAX_DIFF
);
745 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
746 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
747 int prevsc
= sce
->sf_idx
[w
*16+g
];
748 if (dists
[w
*16+g
] > uplims
[w
*16+g
] && sce
->sf_idx
[w
*16+g
] > 60) {
749 if (find_min_book(maxvals
[w
*16+g
], sce
->sf_idx
[w
*16+g
]-1))
750 sce
->sf_idx
[w
*16+g
]--;
751 else //Try to make sure there is some energy in every band
752 sce
->sf_idx
[w
*16+g
]-=2;
754 sce
->sf_idx
[w
*16+g
] = av_clip(sce
->sf_idx
[w
*16+g
], minscaler
, minscaler
+ SCALE_MAX_DIFF
);
755 sce
->sf_idx
[w
*16+g
] = FFMIN(sce
->sf_idx
[w
*16+g
], 219);
756 if (sce
->sf_idx
[w
*16+g
] != prevsc
)
758 sce
->band_type
[w
*16+g
] = find_min_book(maxvals
[w
*16+g
], sce
->sf_idx
[w
*16+g
]);
762 } while (fflag
&& its
< 10);
765 static void search_for_pns(AACEncContext
*s
, AVCodecContext
*avctx
, SingleChannelElement
*sce
)
769 int wlen
= 1024 / sce
->ics
.num_windows
;
770 int bandwidth
, cutoff
;
771 float *PNS
= &s
->scoefs
[0*128], *PNS34
= &s
->scoefs
[1*128];
772 float *NOR34
= &s
->scoefs
[3*128];
773 uint8_t nextband
[128];
774 const float lambda
= s
->lambda
;
775 const float freq_mult
= avctx
->sample_rate
*0.5f
/wlen
;
776 const float thr_mult
= NOISE_LAMBDA_REPLACE
*(100.0f
/lambda
);
777 const float spread_threshold
= FFMIN(0.75f
, NOISE_SPREAD_THRESHOLD
*FFMAX(0.5f
, lambda
/100.f
));
778 const float dist_bias
= av_clipf(4.f
* 120 / lambda
, 0.25f
, 4.0f
);
779 const float pns_transient_energy_r
= FFMIN(0.7f
, lambda
/ 140.f
);
781 int refbits
= avctx
->bit_rate
* 1024.0 / avctx
->sample_rate
782 / ((avctx
->flags
& AV_CODEC_FLAG_QSCALE
) ? 2.0f
: avctx
->ch_layout
.nb_channels
)
785 /** Keep this in sync with twoloop's cutoff selection */
786 float rate_bandwidth_multiplier
= 1.5f
;
787 int prev
= -1000, prev_sf
= -1;
788 int frame_bit_rate
= (avctx
->flags
& AV_CODEC_FLAG_QSCALE
)
789 ? (refbits
* rate_bandwidth_multiplier
* avctx
->sample_rate
/ 1024)
790 : (avctx
->bit_rate
/ avctx
->ch_layout
.nb_channels
);
792 frame_bit_rate
*= 1.15f
;
794 if (avctx
->cutoff
> 0) {
795 bandwidth
= avctx
->cutoff
;
797 bandwidth
= FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate
, 1, avctx
->sample_rate
));
800 cutoff
= bandwidth
* 2 * wlen
/ avctx
->sample_rate
;
802 memcpy(sce
->band_alt
, sce
->band_type
, sizeof(sce
->band_type
));
803 ff_init_nextband_map(sce
, nextband
);
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
++) {
808 float dist1
= 0.0f
, dist2
= 0.0f
, noise_amp
;
809 float pns_energy
= 0.0f
, pns_tgt_energy
, energy_ratio
, dist_thresh
;
810 float sfb_energy
= 0.0f
, threshold
= 0.0f
, spread
= 2.0f
;
811 float min_energy
= -1.0f
, max_energy
= 0.0f
;
812 const int start
= wstart
+sce
->ics
.swb_offset
[g
];
813 const float freq
= (start
-wstart
)*freq_mult
;
814 const float freq_boost
= FFMAX(0.88f
*freq
/NOISE_LOW_LIMIT
, 1.0f
);
815 if (freq
< NOISE_LOW_LIMIT
|| (start
-wstart
) >= cutoff
) {
816 if (!sce
->zeroes
[w
*16+g
])
817 prev_sf
= sce
->sf_idx
[w
*16+g
];
820 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
821 band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(w
+w2
)*16+g
];
822 sfb_energy
+= band
->energy
;
823 spread
= FFMIN(spread
, band
->spread
);
824 threshold
+= band
->threshold
;
826 min_energy
= max_energy
= band
->energy
;
828 min_energy
= FFMIN(min_energy
, band
->energy
);
829 max_energy
= FFMAX(max_energy
, band
->energy
);
833 /* Ramps down at ~8000Hz and loosens the dist threshold */
834 dist_thresh
= av_clipf(2.5f
*NOISE_LOW_LIMIT
/freq
, 0.5f
, 2.5f
) * dist_bias
;
836 /* PNS is acceptable when all of these are true:
837 * 1. high spread energy (noise-like band)
838 * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
839 * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
841 * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important)
843 if ((!sce
->zeroes
[w
*16+g
] && !ff_sfdelta_can_remove_band(sce
, nextband
, prev_sf
, w
*16+g
)) ||
844 ((sce
->zeroes
[w
*16+g
] || !sce
->band_alt
[w
*16+g
]) && sfb_energy
< threshold
*sqrtf(1.0f
/freq_boost
)) || spread
< spread_threshold
||
845 (!sce
->zeroes
[w
*16+g
] && sce
->band_alt
[w
*16+g
] && sfb_energy
> threshold
*thr_mult
*freq_boost
) ||
846 min_energy
< pns_transient_energy_r
* max_energy
) {
847 sce
->pns_ener
[w
*16+g
] = sfb_energy
;
848 if (!sce
->zeroes
[w
*16+g
])
849 prev_sf
= sce
->sf_idx
[w
*16+g
];
853 pns_tgt_energy
= sfb_energy
*FFMIN(1.0f
, spread
*spread
);
854 noise_sfi
= av_clip(roundf(log2f(pns_tgt_energy
)*2), -100, 155); /* Quantize */
855 noise_amp
= -ff_aac_pow2sf_tab
[noise_sfi
+ POW_SF2_ZERO
]; /* Dequantize */
857 int noise_sfdiff
= noise_sfi
- prev
+ SCALE_DIFF_ZERO
;
858 if (noise_sfdiff
< 0 || noise_sfdiff
> 2*SCALE_MAX_DIFF
) {
859 if (!sce
->zeroes
[w
*16+g
])
860 prev_sf
= sce
->sf_idx
[w
*16+g
];
864 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
865 float band_energy
, scale
, pns_senergy
;
866 const int start_c
= (w
+w2
)*128+sce
->ics
.swb_offset
[g
];
867 band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(w
+w2
)*16+g
];
868 for (i
= 0; i
< sce
->ics
.swb_sizes
[g
]; i
++) {
869 s
->random_state
= lcg_random(s
->random_state
);
870 PNS
[i
] = s
->random_state
;
872 band_energy
= s
->fdsp
->scalarproduct_float(PNS
, PNS
, sce
->ics
.swb_sizes
[g
]);
873 scale
= noise_amp
/sqrtf(band_energy
);
874 s
->fdsp
->vector_fmul_scalar(PNS
, PNS
, scale
, sce
->ics
.swb_sizes
[g
]);
875 pns_senergy
= s
->fdsp
->scalarproduct_float(PNS
, PNS
, sce
->ics
.swb_sizes
[g
]);
876 pns_energy
+= pns_senergy
;
877 s
->aacdsp
.abs_pow34(NOR34
, &sce
->coeffs
[start_c
], sce
->ics
.swb_sizes
[g
]);
878 s
->aacdsp
.abs_pow34(PNS34
, PNS
, sce
->ics
.swb_sizes
[g
]);
879 dist1
+= quantize_band_cost(s
, &sce
->coeffs
[start_c
],
881 sce
->ics
.swb_sizes
[g
],
882 sce
->sf_idx
[(w
+w2
)*16+g
],
883 sce
->band_alt
[(w
+w2
)*16+g
],
884 lambda
/band
->threshold
, INFINITY
, NULL
, NULL
);
885 /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */
886 dist2
+= band
->energy
/(band
->spread
*band
->spread
)*lambda
*dist_thresh
/band
->threshold
;
888 if (g
&& sce
->band_type
[w
*16+g
-1] == NOISE_BT
) {
893 energy_ratio
= pns_tgt_energy
/pns_energy
; /* Compensates for quantization error */
894 sce
->pns_ener
[w
*16+g
] = energy_ratio
*pns_tgt_energy
;
895 if (sce
->zeroes
[w
*16+g
] || !sce
->band_alt
[w
*16+g
] || (energy_ratio
> 0.85f
&& energy_ratio
< 1.25f
&& dist2
< dist1
)) {
896 sce
->band_type
[w
*16+g
] = NOISE_BT
;
897 sce
->zeroes
[w
*16+g
] = 0;
900 if (!sce
->zeroes
[w
*16+g
])
901 prev_sf
= sce
->sf_idx
[w
*16+g
];
907 static void mark_pns(AACEncContext
*s
, AVCodecContext
*avctx
, SingleChannelElement
*sce
)
911 int wlen
= 1024 / sce
->ics
.num_windows
;
912 int bandwidth
, cutoff
;
913 const float lambda
= s
->lambda
;
914 const float freq_mult
= avctx
->sample_rate
*0.5f
/wlen
;
915 const float spread_threshold
= FFMIN(0.75f
, NOISE_SPREAD_THRESHOLD
*FFMAX(0.5f
, lambda
/100.f
));
916 const float pns_transient_energy_r
= FFMIN(0.7f
, lambda
/ 140.f
);
918 int refbits
= avctx
->bit_rate
* 1024.0 / avctx
->sample_rate
919 / ((avctx
->flags
& AV_CODEC_FLAG_QSCALE
) ? 2.0f
: avctx
->ch_layout
.nb_channels
)
922 /** Keep this in sync with twoloop's cutoff selection */
923 float rate_bandwidth_multiplier
= 1.5f
;
924 int frame_bit_rate
= (avctx
->flags
& AV_CODEC_FLAG_QSCALE
)
925 ? (refbits
* rate_bandwidth_multiplier
* avctx
->sample_rate
/ 1024)
926 : (avctx
->bit_rate
/ avctx
->ch_layout
.nb_channels
);
928 frame_bit_rate
*= 1.15f
;
930 if (avctx
->cutoff
> 0) {
931 bandwidth
= avctx
->cutoff
;
933 bandwidth
= FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate
, 1, avctx
->sample_rate
));
936 cutoff
= bandwidth
* 2 * wlen
/ avctx
->sample_rate
;
938 memcpy(sce
->band_alt
, sce
->band_type
, sizeof(sce
->band_type
));
939 for (w
= 0; w
< sce
->ics
.num_windows
; w
+= sce
->ics
.group_len
[w
]) {
940 for (g
= 0; g
< sce
->ics
.num_swb
; g
++) {
941 float sfb_energy
= 0.0f
, threshold
= 0.0f
, spread
= 2.0f
;
942 float min_energy
= -1.0f
, max_energy
= 0.0f
;
943 const int start
= sce
->ics
.swb_offset
[g
];
944 const float freq
= start
*freq_mult
;
945 const float freq_boost
= FFMAX(0.88f
*freq
/NOISE_LOW_LIMIT
, 1.0f
);
946 if (freq
< NOISE_LOW_LIMIT
|| start
>= cutoff
) {
947 sce
->can_pns
[w
*16+g
] = 0;
950 for (w2
= 0; w2
< sce
->ics
.group_len
[w
]; w2
++) {
951 band
= &s
->psy
.ch
[s
->cur_channel
].psy_bands
[(w
+w2
)*16+g
];
952 sfb_energy
+= band
->energy
;
953 spread
= FFMIN(spread
, band
->spread
);
954 threshold
+= band
->threshold
;
956 min_energy
= max_energy
= band
->energy
;
958 min_energy
= FFMIN(min_energy
, band
->energy
);
959 max_energy
= FFMAX(max_energy
, band
->energy
);
963 /* PNS is acceptable when all of these are true:
964 * 1. high spread energy (noise-like band)
965 * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
966 * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
968 sce
->pns_ener
[w
*16+g
] = sfb_energy
;
969 if (sfb_energy
< threshold
*sqrtf(1.5f
/freq_boost
) || spread
< spread_threshold
|| min_energy
< pns_transient_energy_r
* max_energy
) {
970 sce
->can_pns
[w
*16+g
] = 0;
972 sce
->can_pns
[w
*16+g
] = 1;
978 static void search_for_ms(AACEncContext
*s
, ChannelElement
*cpe
)
980 int start
= 0, i
, w
, w2
, g
, sid_sf_boost
, prev_mid
, prev_side
;
981 uint8_t nextband0
[128], nextband1
[128];
982 float *M
= s
->scoefs
+ 128*0, *S
= s
->scoefs
+ 128*1;
983 float *L34
= s
->scoefs
+ 128*2, *R34
= s
->scoefs
+ 128*3;
984 float *M34
= s
->scoefs
+ 128*4, *S34
= s
->scoefs
+ 128*5;
985 const float lambda
= s
->lambda
;
986 const float mslambda
= FFMIN(1.0f
, lambda
/ 120.f
);
987 SingleChannelElement
*sce0
= &cpe
->ch
[0];
988 SingleChannelElement
*sce1
= &cpe
->ch
[1];
989 if (!cpe
->common_window
)
992 /** Scout out next nonzero bands */
993 ff_init_nextband_map(sce0
, nextband0
);
994 ff_init_nextband_map(sce1
, nextband1
);
996 prev_mid
= sce0
->sf_idx
[0];
997 prev_side
= sce1
->sf_idx
[0];
998 for (w
= 0; w
< sce0
->ics
.num_windows
; w
+= sce0
->ics
.group_len
[w
]) {
1000 for (g
= 0; g
< sce0
->ics
.num_swb
; g
++) {
1001 float bmax
= bval2bmax(g
* 17.0f
/ sce0
->ics
.num_swb
) / 0.0045f
;
1002 if (!cpe
->is_mask
[w
*16+g
])
1003 cpe
->ms_mask
[w
*16+g
] = 0;
1004 if (!sce0
->zeroes
[w
*16+g
] && !sce1
->zeroes
[w
*16+g
] && !cpe
->is_mask
[w
*16+g
]) {
1005 float Mmax
= 0.0f
, Smax
= 0.0f
;
1007 /* Must compute mid/side SF and book for the whole window group */
1008 for (w2
= 0; w2
< sce0
->ics
.group_len
[w
]; w2
++) {
1009 for (i
= 0; i
< sce0
->ics
.swb_sizes
[g
]; i
++) {
1010 M
[i
] = (sce0
->coeffs
[start
+(w
+w2
)*128+i
]
1011 + sce1
->coeffs
[start
+(w
+w2
)*128+i
]) * 0.5;
1013 - sce1
->coeffs
[start
+(w
+w2
)*128+i
];
1015 s
->aacdsp
.abs_pow34(M34
, M
, sce0
->ics
.swb_sizes
[g
]);
1016 s
->aacdsp
.abs_pow34(S34
, S
, sce0
->ics
.swb_sizes
[g
]);
1017 for (i
= 0; i
< sce0
->ics
.swb_sizes
[g
]; i
++ ) {
1018 Mmax
= FFMAX(Mmax
, M34
[i
]);
1019 Smax
= FFMAX(Smax
, S34
[i
]);
1023 for (sid_sf_boost
= 0; sid_sf_boost
< 4; sid_sf_boost
++) {
1024 float dist1
= 0.0f
, dist2
= 0.0f
;
1030 minidx
= FFMIN(sce0
->sf_idx
[w
*16+g
], sce1
->sf_idx
[w
*16+g
]);
1031 mididx
= av_clip(minidx
, 0, SCALE_MAX_POS
- SCALE_DIV_512
);
1032 sididx
= av_clip(minidx
- sid_sf_boost
* 3, 0, SCALE_MAX_POS
- SCALE_DIV_512
);
1033 if (sce0
->band_type
[w
*16+g
] != NOISE_BT
&& sce1
->band_type
[w
*16+g
] != NOISE_BT
1034 && ( !ff_sfdelta_can_replace(sce0
, nextband0
, prev_mid
, mididx
, w
*16+g
)
1035 || !ff_sfdelta_can_replace(sce1
, nextband1
, prev_side
, sididx
, w
*16+g
))) {
1036 /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
1040 midcb
= find_min_book(Mmax
, mididx
);
1041 sidcb
= find_min_book(Smax
, sididx
);
1043 /* No CB can be zero */
1044 midcb
= FFMAX(1,midcb
);
1045 sidcb
= FFMAX(1,sidcb
);
1047 for (w2
= 0; w2
< sce0
->ics
.group_len
[w
]; w2
++) {
1048 FFPsyBand
*band0
= &s
->psy
.ch
[s
->cur_channel
+0].psy_bands
[(w
+w2
)*16+g
];
1049 FFPsyBand
*band1
= &s
->psy
.ch
[s
->cur_channel
+1].psy_bands
[(w
+w2
)*16+g
];
1050 float minthr
= FFMIN(band0
->threshold
, band1
->threshold
);
1052 for (i
= 0; i
< sce0
->ics
.swb_sizes
[g
]; i
++) {
1053 M
[i
] = (sce0
->coeffs
[start
+(w
+w2
)*128+i
]
1054 + sce1
->coeffs
[start
+(w
+w2
)*128+i
]) * 0.5;
1056 - sce1
->coeffs
[start
+(w
+w2
)*128+i
];
1059 s
->aacdsp
.abs_pow34(L34
, sce0
->coeffs
+start
+(w
+w2
)*128, sce0
->ics
.swb_sizes
[g
]);
1060 s
->aacdsp
.abs_pow34(R34
, sce1
->coeffs
+start
+(w
+w2
)*128, sce0
->ics
.swb_sizes
[g
]);
1061 s
->aacdsp
.abs_pow34(M34
, M
, sce0
->ics
.swb_sizes
[g
]);
1062 s
->aacdsp
.abs_pow34(S34
, S
, sce0
->ics
.swb_sizes
[g
]);
1063 dist1
+= quantize_band_cost(s
, &sce0
->coeffs
[start
+ (w
+w2
)*128],
1065 sce0
->ics
.swb_sizes
[g
],
1066 sce0
->sf_idx
[w
*16+g
],
1067 sce0
->band_type
[w
*16+g
],
1068 lambda
/ (band0
->threshold
+ FLT_MIN
), INFINITY
, &b1
, NULL
);
1069 dist1
+= quantize_band_cost(s
, &sce1
->coeffs
[start
+ (w
+w2
)*128],
1071 sce1
->ics
.swb_sizes
[g
],
1072 sce1
->sf_idx
[w
*16+g
],
1073 sce1
->band_type
[w
*16+g
],
1074 lambda
/ (band1
->threshold
+ FLT_MIN
), INFINITY
, &b2
, NULL
);
1075 dist2
+= quantize_band_cost(s
, M
,
1077 sce0
->ics
.swb_sizes
[g
],
1080 lambda
/ (minthr
+ FLT_MIN
), INFINITY
, &b3
, NULL
);
1081 dist2
+= quantize_band_cost(s
, S
,
1083 sce1
->ics
.swb_sizes
[g
],
1086 mslambda
/ (minthr
* bmax
+ FLT_MIN
), INFINITY
, &b4
, NULL
);
1092 cpe
->ms_mask
[w
*16+g
] = dist2
<= dist1
&& B1
< B0
;
1093 if (cpe
->ms_mask
[w
*16+g
]) {
1094 if (sce0
->band_type
[w
*16+g
] != NOISE_BT
&& sce1
->band_type
[w
*16+g
] != NOISE_BT
) {
1095 sce0
->sf_idx
[w
*16+g
] = mididx
;
1096 sce1
->sf_idx
[w
*16+g
] = sididx
;
1097 sce0
->band_type
[w
*16+g
] = midcb
;
1098 sce1
->band_type
[w
*16+g
] = sidcb
;
1099 } else if ((sce0
->band_type
[w
*16+g
] != NOISE_BT
) ^ (sce1
->band_type
[w
*16+g
] != NOISE_BT
)) {
1100 /* ms_mask unneeded, and it confuses some decoders */
1101 cpe
->ms_mask
[w
*16+g
] = 0;
1104 } else if (B1
> B0
) {
1105 /* More boost won't fix this */
1110 if (!sce0
->zeroes
[w
*16+g
] && sce0
->band_type
[w
*16+g
] < RESERVED_BT
)
1111 prev_mid
= sce0
->sf_idx
[w
*16+g
];
1112 if (!sce1
->zeroes
[w
*16+g
] && !cpe
->is_mask
[w
*16+g
] && sce1
->band_type
[w
*16+g
] < RESERVED_BT
)
1113 prev_side
= sce1
->sf_idx
[w
*16+g
];
1114 start
+= sce0
->ics
.swb_sizes
[g
];
1119 const AACCoefficientsEncoder ff_aac_coders
[AAC_CODER_NB
] = {
1120 [AAC_CODER_ANMR
] = {
1121 search_for_quantizers_anmr
,
1122 encode_window_bands_info
,
1123 quantize_and_encode_band
,
1124 ff_aac_encode_tns_info
,
1125 ff_aac_encode_ltp_info
,
1126 ff_aac_encode_main_pred
,
1127 ff_aac_adjust_common_pred
,
1128 ff_aac_adjust_common_ltp
,
1129 ff_aac_apply_main_pred
,
1132 ff_aac_ltp_insert_new_frame
,
1133 set_special_band_scalefactors
,
1136 ff_aac_search_for_tns
,
1137 ff_aac_search_for_ltp
,
1139 ff_aac_search_for_is
,
1140 ff_aac_search_for_pred
,
1142 [AAC_CODER_TWOLOOP
] = {
1143 search_for_quantizers_twoloop
,
1144 codebook_trellis_rate
,
1145 quantize_and_encode_band
,
1146 ff_aac_encode_tns_info
,
1147 ff_aac_encode_ltp_info
,
1148 ff_aac_encode_main_pred
,
1149 ff_aac_adjust_common_pred
,
1150 ff_aac_adjust_common_ltp
,
1151 ff_aac_apply_main_pred
,
1154 ff_aac_ltp_insert_new_frame
,
1155 set_special_band_scalefactors
,
1158 ff_aac_search_for_tns
,
1159 ff_aac_search_for_ltp
,
1161 ff_aac_search_for_is
,
1162 ff_aac_search_for_pred
,
1164 [AAC_CODER_FAST
] = {
1165 search_for_quantizers_fast
,
1166 codebook_trellis_rate
,
1167 quantize_and_encode_band
,
1168 ff_aac_encode_tns_info
,
1169 ff_aac_encode_ltp_info
,
1170 ff_aac_encode_main_pred
,
1171 ff_aac_adjust_common_pred
,
1172 ff_aac_adjust_common_ltp
,
1173 ff_aac_apply_main_pred
,
1176 ff_aac_ltp_insert_new_frame
,
1177 set_special_band_scalefactors
,
1180 ff_aac_search_for_tns
,
1181 ff_aac_search_for_ltp
,
1183 ff_aac_search_for_is
,
1184 ff_aac_search_for_pred
,