FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aaccoder.c
Date: 2026-07-26 12:49:09
Exec Total Coverage
Lines: 338 447 75.6%
Functions: 16 18 88.9%
Branches: 265 366 72.4%

Line Branch Exec Source
1 /*
2 * AAC coefficients encoder
3 * Copyright (C) 2008-2009 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * AAC coefficients encoder
25 */
26
27 /***********************************
28 * TODOs:
29 * speedup quantizer selection
30 * add sane pulse detection
31 ***********************************/
32
33 #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
34
35 #include <float.h>
36
37 #include "libavutil/mathematics.h"
38 #include "mathops.h"
39 #include "avcodec.h"
40 #include "put_bits.h"
41 #include "aac.h"
42 #include "aacenc.h"
43 #include "aactab.h"
44 #include "aacenctab.h"
45 #include "aacenc_utils.h"
46 #include "aacenc_quantization.h"
47
48 #include "aacenc_is.h"
49 #include "aacenc_tns.h"
50
51 #include "libavcodec/aaccoder_twoloop.h"
52
53 /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread
54 * beyond which no PNS is used (since the SFBs contain tone rather than noise) */
55 #define NOISE_SPREAD_THRESHOLD 0.9f
56
57 /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to
58 * replace low energy non zero bands */
59 #define NOISE_LAMBDA_REPLACE 1.948f
60
61 #include "libavcodec/aaccoder_trellis.h"
62 #include "libavcodec/aaccoder_nmr.h"
63
64 typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb,
65 const float *in, float *quant, const float *scaled,
66 int size, int scale_idx, int cb,
67 const float lambda, const float uplim,
68 int *bits, float *energy);
69
70 /**
71 * Calculate rate distortion cost for quantizing with given codebook
72 *
73 * @return quantization distortion
74 */
75 8391634 static av_always_inline float quantize_and_encode_band_cost_template(
76 struct AACEncContext *s,
77 PutBitContext *pb, const float *in, float *out,
78 const float *scaled, int size, int scale_idx,
79 int cb, const float lambda, const float uplim,
80 int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED,
81 int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
82 const float ROUNDING)
83 {
84 8391634 const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
85 8391634 const float Q = ff_aac_pow2sf_tab [q_idx];
86 8391634 const float Q34 = ff_aac_pow34sf_tab[q_idx];
87 8391634 const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
88 8391634 const float CLIPPED_ESCAPE = 165140.0f*IQ;
89 8391634 float cost = 0;
90 8391634 float qenergy = 0;
91
2/2
✓ Branch 0 taken 4408336 times.
✓ Branch 1 taken 3983298 times.
8391634 const int dim = BT_PAIR ? 2 : 4;
92 8391634 int resbits = 0;
93 int off;
94
95
6/6
✓ Branch 0 taken 7193530 times.
✓ Branch 1 taken 1198104 times.
✓ Branch 2 taken 7181506 times.
✓ Branch 3 taken 12024 times.
✓ Branch 4 taken 19958 times.
✓ Branch 5 taken 7161548 times.
8391634 if (BT_ZERO || BT_NOISE || BT_STEREO) {
96
2/2
✓ Branch 0 taken 27973072 times.
✓ Branch 1 taken 1230086 times.
29203158 for (int i = 0; i < size; i++)
97 27973072 cost += in[i]*in[i];
98
2/2
✓ Branch 0 taken 1208351 times.
✓ Branch 1 taken 21735 times.
1230086 if (bits)
99 1208351 *bits = 0;
100
2/2
✓ Branch 0 taken 1179943 times.
✓ Branch 1 taken 50143 times.
1230086 if (energy)
101 1179943 *energy = qenergy;
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1230086 times.
1230086 if (out) {
103 for (int i = 0; i < size; i += dim)
104 for (int j = 0; j < dim; j++)
105 out[i+j] = 0.0f;
106 }
107 1230086 return cost * lambda;
108 }
109
2/2
✓ Branch 0 taken 440658 times.
✓ Branch 1 taken 6720890 times.
7161548 if (!scaled) {
110 440658 s->aacdsp.abs_pow34(s->scoefs, in, size);
111 440658 scaled = s->scoefs;
112 }
113 7161548 s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
114
2/2
✓ Branch 0 taken 4447358 times.
✓ Branch 1 taken 2714190 times.
7161548 if (BT_UNSIGNED) {
115 4447358 off = 0;
116 } else {
117 2714190 off = aac_cb_maxval[cb];
118 }
119
2/2
✓ Branch 0 taken 57685900 times.
✓ Branch 1 taken 7161548 times.
64847448 for (int i = 0; i < size; i += dim) {
120 const float *vec;
121 57685900 int *quants = s->qcoefs + i;
122 57685900 int curidx = 0;
123 int curbits;
124 57685900 float quantized, rd = 0.0f;
125
2/2
✓ Branch 0 taken 147213864 times.
✓ Branch 1 taken 57685900 times.
204899764 for (int j = 0; j < dim; j++) {
126 147213864 curidx *= aac_cb_range[cb];
127 147213864 curidx += quants[j] + off;
128 }
129 57685900 curbits = ff_aac_spectral_bits[cb-1][curidx];
130 57685900 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
131
2/2
✓ Branch 0 taken 36975306 times.
✓ Branch 1 taken 20710594 times.
57685900 if (BT_UNSIGNED) {
132
2/2
✓ Branch 0 taken 87247412 times.
✓ Branch 1 taken 36975306 times.
124222718 for (int j = 0; j < dim; j++) {
133 87247412 float t = fabsf(in[i+j]);
134 float di;
135
4/4
✓ Branch 0 taken 19772492 times.
✓ Branch 1 taken 67474920 times.
✓ Branch 2 taken 5550679 times.
✓ Branch 3 taken 14221813 times.
87247412 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
136
2/2
✓ Branch 0 taken 378214 times.
✓ Branch 1 taken 5172465 times.
5550679 if (t >= CLIPPED_ESCAPE) {
137 378214 quantized = CLIPPED_ESCAPE;
138 378214 curbits += 21;
139 } else {
140 5172465 int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
141 5172465 quantized = c*cbrtf(c)*IQ;
142 5172465 curbits += av_log2(c)*2 - 4 + 1;
143 }
144 } else {
145 81696733 quantized = vec[j]*IQ;
146 }
147 87247412 di = t - quantized;
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87247412 times.
87247412 if (out)
149 out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
150
2/2
✓ Branch 0 taken 51907542 times.
✓ Branch 1 taken 35339870 times.
87247412 if (vec[j] != 0.0f)
151 51907542 curbits++;
152 87247412 qenergy += quantized*quantized;
153 87247412 rd += di*di;
154 }
155 } else {
156
2/2
✓ Branch 0 taken 59966452 times.
✓ Branch 1 taken 20710594 times.
80677046 for (int j = 0; j < dim; j++) {
157 59966452 quantized = vec[j]*IQ;
158 59966452 qenergy += quantized*quantized;
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59966452 times.
59966452 if (out)
160 out[i+j] = quantized;
161 59966452 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
162 }
163 }
164 57685900 cost += rd * lambda + curbits;
165 57685900 resbits += curbits;
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57685900 times.
57685900 if (cost >= uplim)
167 return uplim;
168
2/2
✓ Branch 0 taken 3158437 times.
✓ Branch 1 taken 54527463 times.
57685900 if (pb) {
169 3158437 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
170
2/2
✓ Branch 0 taken 1698190 times.
✓ Branch 1 taken 1460247 times.
3158437 if (BT_UNSIGNED)
171
2/2
✓ Branch 0 taken 4457464 times.
✓ Branch 1 taken 1698190 times.
6155654 for (int j = 0; j < dim; j++)
172
2/2
✓ Branch 0 taken 3083209 times.
✓ Branch 1 taken 1374255 times.
4457464 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
173 3083209 put_bits(pb, 1, in[i+j] < 0.0f);
174
2/2
✓ Branch 0 taken 451002 times.
✓ Branch 1 taken 2707435 times.
3158437 if (BT_ESC) {
175
2/2
✓ Branch 0 taken 902004 times.
✓ Branch 1 taken 451002 times.
1353006 for (int j = 0; j < 2; j++) {
176
2/2
✓ Branch 0 taken 132500 times.
✓ Branch 1 taken 769504 times.
902004 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
177 132500 int coef = av_clip(quant(fabsf(in[i+j]), Q, ROUNDING), 16, (1 << 13) - 1);
178 132500 int len = av_log2(coef);
179
180 132500 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
181 132500 put_sbits(pb, len, coef);
182 }
183 }
184 }
185 }
186 }
187
188
2/2
✓ Branch 0 taken 6652038 times.
✓ Branch 1 taken 509510 times.
7161548 if (bits)
189 6652038 *bits = resbits;
190
2/2
✓ Branch 0 taken 3318014 times.
✓ Branch 1 taken 3843534 times.
7161548 if (energy)
191 3318014 *energy = qenergy;
192 7161548 return cost;
193 }
194
195 static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
196 const float *in, float *quant, const float *scaled,
197 int size, int scale_idx, int cb,
198 const float lambda, const float uplim,
199 int *bits, float *energy) {
200 av_assert0(0);
201 return 0.0f;
202 }
203
204 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
205 static float quantize_and_encode_band_cost_ ## NAME( \
206 struct AACEncContext *s, \
207 PutBitContext *pb, const float *in, float *quant, \
208 const float *scaled, int size, int scale_idx, \
209 int cb, const float lambda, const float uplim, \
210 int *bits, float *energy) { \
211 return quantize_and_encode_band_cost_template( \
212 s, pb, in, quant, scaled, size, scale_idx, \
213 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
214 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
215 ROUNDING); \
216 }
217
218 1198104 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
219 1566711 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
220 1186501 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
221 1147479 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
222 2086297 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
223 1169667 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
224 4893 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
225 12024 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
226 19958 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
227
228 static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[] =
229 {
230 quantize_and_encode_band_cost_ZERO,
231 quantize_and_encode_band_cost_SQUAD,
232 quantize_and_encode_band_cost_SQUAD,
233 quantize_and_encode_band_cost_UQUAD,
234 quantize_and_encode_band_cost_UQUAD,
235 quantize_and_encode_band_cost_SPAIR,
236 quantize_and_encode_band_cost_SPAIR,
237 quantize_and_encode_band_cost_UPAIR,
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_ESC,
242 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
243 quantize_and_encode_band_cost_NOISE,
244 quantize_and_encode_band_cost_STEREO,
245 quantize_and_encode_band_cost_STEREO,
246 };
247
248 static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[] =
249 {
250 quantize_and_encode_band_cost_ZERO,
251 quantize_and_encode_band_cost_SQUAD,
252 quantize_and_encode_band_cost_SQUAD,
253 quantize_and_encode_band_cost_UQUAD,
254 quantize_and_encode_band_cost_UQUAD,
255 quantize_and_encode_band_cost_SPAIR,
256 quantize_and_encode_band_cost_SPAIR,
257 quantize_and_encode_band_cost_UPAIR,
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_ESC_RTZ,
262 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
263 quantize_and_encode_band_cost_NOISE,
264 quantize_and_encode_band_cost_STEREO,
265 quantize_and_encode_band_cost_STEREO,
266 };
267
268 7934985 float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb,
269 const float *in, float *quant, const float *scaled,
270 int size, int scale_idx, int cb,
271 const float lambda, const float uplim,
272 int *bits, float *energy)
273 {
274 7934985 return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size,
275 scale_idx, cb, lambda, uplim,
276 bits, energy);
277 }
278
279 456649 static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
280 const float *in, float *out, int size, int scale_idx,
281 int cb, const float lambda, int rtz)
282 {
283
2/2
✓ Branch 0 taken 15753 times.
✓ Branch 1 taken 440896 times.
456649 (rtz ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb](s, pb, in, out, NULL, size, scale_idx, cb,
284 lambda, INFINITY, NULL, NULL);
285 456649 }
286
287 /**
288 * structure used in optimal codebook search
289 */
290 typedef struct BandCodingPath {
291 int prev_idx; ///< pointer to the previous path point
292 float cost; ///< path cost
293 int run;
294 } BandCodingPath;
295
296 typedef struct TrellisPath {
297 float cost;
298 int prev;
299 } TrellisPath;
300
301 #define TRELLIS_STAGES 121
302 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
303
304 9458 static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
305 {
306 int w, g;
307 9458 int prevscaler_n = -255, prevscaler_i = 0;
308 9458 int bands = 0;
309
310
2/2
✓ Branch 0 taken 10146 times.
✓ Branch 1 taken 9458 times.
19604 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
311
2/2
✓ Branch 0 taken 460867 times.
✓ Branch 1 taken 10146 times.
471013 for (g = 0; g < sce->ics.num_swb; g++) {
312
2/2
✓ Branch 0 taken 10152 times.
✓ Branch 1 taken 450715 times.
460867 if (sce->zeroes[w*16+g])
313 10152 continue;
314
4/4
✓ Branch 0 taken 443396 times.
✓ Branch 1 taken 7319 times.
✓ Branch 2 taken 2129 times.
✓ Branch 3 taken 441267 times.
450715 if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
315 9448 sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
316 9448 bands++;
317
2/2
✓ Branch 0 taken 5957 times.
✓ Branch 1 taken 435310 times.
441267 } else if (sce->band_type[w*16+g] == NOISE_BT) {
318 5957 sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
319
2/2
✓ Branch 0 taken 1025 times.
✓ Branch 1 taken 4932 times.
5957 if (prevscaler_n == -255)
320 1025 prevscaler_n = sce->sf_idx[w*16+g];
321 5957 bands++;
322 }
323 }
324 }
325
326
2/2
✓ Branch 0 taken 7969 times.
✓ Branch 1 taken 1489 times.
9458 if (!bands)
327 7969 return;
328
329 /* Clip the scalefactor indices */
330
2/2
✓ Branch 0 taken 1621 times.
✓ Branch 1 taken 1489 times.
3110 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
331
2/2
✓ Branch 0 taken 73234 times.
✓ Branch 1 taken 1621 times.
74855 for (g = 0; g < sce->ics.num_swb; g++) {
332
2/2
✓ Branch 0 taken 2428 times.
✓ Branch 1 taken 70806 times.
73234 if (sce->zeroes[w*16+g])
333 2428 continue;
334
4/4
✓ Branch 0 taken 63487 times.
✓ Branch 1 taken 7319 times.
✓ Branch 2 taken 2129 times.
✓ Branch 3 taken 61358 times.
70806 if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
335 9448 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);
336
2/2
✓ Branch 0 taken 5957 times.
✓ Branch 1 taken 55401 times.
61358 } else if (sce->band_type[w*16+g] == NOISE_BT) {
337 5957 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);
338 }
339 }
340 }
341 }
342
343 8666 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
344 SingleChannelElement *sce,
345 const float lambda)
346 {
347 8666 int start = 0, i, w, w2, g;
348 8666 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
349 8666 float dists[128] = { 0 }, uplims[128] = { 0 };
350 float maxvals[128];
351 int fflag, minscaler;
352 8666 int its = 0;
353 8666 int allz = 0;
354 8666 float minthr = INFINITY;
355
356 // for values above this the decoder might end up in an endless loop
357 // due to always having more bits than what can be encoded.
358 8666 destbits = FFMIN(destbits, 5800);
359 //some heuristic to determine initial quantizers will reduce search time
360 //determine zero bands and upper limits
361
2/2
✓ Branch 0 taken 9340 times.
✓ Branch 1 taken 8666 times.
18006 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
362 9340 start = 0;
363
2/2
✓ Branch 0 taken 422038 times.
✓ Branch 1 taken 9340 times.
431378 for (g = 0; g < sce->ics.num_swb; g++) {
364 422038 int nz = 0;
365 422038 float uplim = 0.0f;
366
2/2
✓ Branch 0 taken 435730 times.
✓ Branch 1 taken 422038 times.
857768 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
367 435730 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
368 435730 uplim += band->threshold;
369
3/4
✓ Branch 0 taken 431731 times.
✓ Branch 1 taken 3999 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 431731 times.
435730 if (band->energy <= band->threshold || band->threshold == 0.0f) {
370 3999 sce->zeroes[(w+w2)*16+g] = 1;
371 3999 continue;
372 }
373 431731 nz = 1;
374 }
375 422038 uplims[w*16+g] = uplim *512;
376 422038 sce->band_type[w*16+g] = 0;
377 422038 sce->zeroes[w*16+g] = !nz;
378
2/2
✓ Branch 0 taken 418072 times.
✓ Branch 1 taken 3966 times.
422038 if (nz)
379
2/2
✓ Branch 0 taken 91780 times.
✓ Branch 1 taken 326292 times.
418072 minthr = FFMIN(minthr, uplim);
380 422038 allz |= nz;
381 422038 start += sce->ics.swb_sizes[g];
382 }
383 }
384
2/2
✓ Branch 0 taken 9340 times.
✓ Branch 1 taken 8666 times.
18006 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
385
2/2
✓ Branch 0 taken 422038 times.
✓ Branch 1 taken 9340 times.
431378 for (g = 0; g < sce->ics.num_swb; g++) {
386
2/2
✓ Branch 0 taken 3966 times.
✓ Branch 1 taken 418072 times.
422038 if (sce->zeroes[w*16+g]) {
387 3966 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
388 3966 continue;
389 }
390
2/2
✓ Branch 0 taken 182066 times.
✓ Branch 1 taken 236006 times.
418072 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
391 }
392 }
393
394
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8654 times.
8666 if (!allz)
395 12 return;
396 8654 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
397 8654 ff_quantize_band_cost_cache_init(s);
398
399
2/2
✓ Branch 0 taken 9328 times.
✓ Branch 1 taken 8654 times.
17982 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
400 9328 start = w*128;
401
2/2
✓ Branch 0 taken 421450 times.
✓ Branch 1 taken 9328 times.
430778 for (g = 0; g < sce->ics.num_swb; g++) {
402 421450 const float *scaled = s->scoefs + start;
403 421450 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
404 421450 start += sce->ics.swb_sizes[g];
405 }
406 }
407
408 //perform two-loop search
409 //outer loop - improve quality
410 do {
411 int tbits, qstep;
412 37646 minscaler = sce->sf_idx[0];
413 //inner loop - quantize spectrum to fit into given number of bits
414
2/2
✓ Branch 0 taken 28992 times.
✓ Branch 1 taken 8654 times.
37646 qstep = its ? 1 : 32;
415 do {
416 85262 int prev = -1;
417 85262 tbits = 0;
418
2/2
✓ Branch 0 taken 96016 times.
✓ Branch 1 taken 85262 times.
181278 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
419 96016 start = w*128;
420
2/2
✓ Branch 0 taken 4139794 times.
✓ Branch 1 taken 96016 times.
4235810 for (g = 0; g < sce->ics.num_swb; g++) {
421 4139794 const float *coefs = sce->coeffs + start;
422 4139794 const float *scaled = s->scoefs + start;
423 4139794 int bits = 0;
424 int cb;
425 4139794 float dist = 0.0f;
426
427
3/4
✓ Branch 0 taken 4090078 times.
✓ Branch 1 taken 49716 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4090078 times.
4139794 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
428 49716 start += sce->ics.swb_sizes[g];
429 49716 continue;
430 }
431 4090078 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
432 4090078 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
433
2/2
✓ Branch 0 taken 4308698 times.
✓ Branch 1 taken 4090078 times.
8398776 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
434 int b;
435 8617396 dist += quantize_band_cost_cached(s, w + w2, g,
436 4308698 coefs + w2*128,
437 4308698 scaled + w2*128,
438 4308698 sce->ics.swb_sizes[g],
439 4308698 sce->sf_idx[w*16+g],
440 cb, 1.0f, INFINITY,
441 &b, NULL, 0);
442 4308698 bits += b;
443 }
444 4090078 dists[w*16+g] = dist - bits;
445
2/2
✓ Branch 0 taken 4004816 times.
✓ Branch 1 taken 85262 times.
4090078 if (prev != -1) {
446 4004816 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
447 }
448 4090078 tbits += bits;
449 4090078 start += sce->ics.swb_sizes[g];
450 4090078 prev = sce->sf_idx[w*16+g];
451 }
452 }
453
2/2
✓ Branch 0 taken 16277 times.
✓ Branch 1 taken 68985 times.
85262 if (tbits > destbits) {
454
2/2
✓ Branch 0 taken 2083456 times.
✓ Branch 1 taken 16277 times.
2099733 for (i = 0; i < 128; i++)
455
1/2
✓ Branch 0 taken 2083456 times.
✗ Branch 1 not taken.
2083456 if (sce->sf_idx[i] < 218 - qstep)
456 2083456 sce->sf_idx[i] += qstep;
457 } else {
458
2/2
✓ Branch 0 taken 8830080 times.
✓ Branch 1 taken 68985 times.
8899065 for (i = 0; i < 128; i++)
459
2/2
✓ Branch 0 taken 3605669 times.
✓ Branch 1 taken 5224411 times.
8830080 if (sce->sf_idx[i] > 60 - qstep)
460 3605669 sce->sf_idx[i] -= qstep;
461 }
462 85262 qstep >>= 1;
463
5/6
✓ Branch 0 taken 41992 times.
✓ Branch 1 taken 43270 times.
✓ Branch 2 taken 4346 times.
✓ Branch 3 taken 37646 times.
✓ Branch 4 taken 4346 times.
✗ Branch 5 not taken.
85262 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
464 4346 qstep = 1;
465
2/2
✓ Branch 0 taken 47616 times.
✓ Branch 1 taken 37646 times.
85262 } while (qstep);
466
467 37646 fflag = 0;
468 37646 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
469
470
2/2
✓ Branch 0 taken 41787 times.
✓ Branch 1 taken 37646 times.
79433 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
471
2/2
✓ Branch 0 taken 1814368 times.
✓ Branch 1 taken 41787 times.
1856155 for (g = 0; g < sce->ics.num_swb; g++) {
472 1814368 int prevsc = sce->sf_idx[w*16+g];
473
3/4
✓ Branch 0 taken 19509 times.
✓ Branch 1 taken 1794859 times.
✓ Branch 2 taken 19509 times.
✗ Branch 3 not taken.
1814368 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
474
2/2
✓ Branch 1 taken 19235 times.
✓ Branch 2 taken 274 times.
19509 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
475 19235 sce->sf_idx[w*16+g]--;
476 else //Try to make sure there is some energy in every band
477 274 sce->sf_idx[w*16+g]-=2;
478 }
479 1814368 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
480 1814368 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
481
2/2
✓ Branch 0 taken 490398 times.
✓ Branch 1 taken 1323970 times.
1814368 if (sce->sf_idx[w*16+g] != prevsc)
482 490398 fflag = 1;
483 1814368 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
484 }
485 }
486 37646 its++;
487
4/4
✓ Branch 0 taken 32052 times.
✓ Branch 1 taken 5594 times.
✓ Branch 2 taken 28992 times.
✓ Branch 3 taken 3060 times.
37646 } while (fflag && its < 10);
488 }
489
490 1314 static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
491 {
492 FFPsyBand *band;
493 int w, g, w2, i;
494 1314 int wlen = 1024 / sce->ics.num_windows;
495 int bandwidth, cutoff;
496 1314 float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
497 1314 float *NOR34 = &s->scoefs[3*128];
498 uint8_t nextband[128];
499 1314 const float lambda = s->lambda;
500 1314 const float freq_mult = avctx->sample_rate*0.5f/wlen;
501 1314 const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
502
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1314 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1314 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1314 const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
503 1314 const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1314 times.
1314 const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
505
506 1314 int prev = -1000, prev_sf = -1;
507
508 /* PNS candidacy must use the coder's actual coding bandwidth (s->bandwidth,
509 * fixed at init), not a separate heuristic, or it evaluates a different band
510 * range than the coder later codes. */
511 1314 bandwidth = s->bandwidth;
512 1314 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
513
514 1314 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
515 1314 ff_init_nextband_map(sce, nextband);
516
2/2
✓ Branch 0 taken 1428 times.
✓ Branch 1 taken 1314 times.
2742 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
517 1428 int wstart = w*128;
518
2/2
✓ Branch 0 taken 60810 times.
✓ Branch 1 taken 1428 times.
62238 for (g = 0; g < sce->ics.num_swb; g++) {
519 int noise_sfi;
520 60810 float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
521 60810 float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
522 60810 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
523 60810 float min_energy = -1.0f, max_energy = 0.0f;
524 60810 const int start = wstart+sce->ics.swb_offset[g];
525 60810 const float freq = (start-wstart)*freq_mult;
526
2/2
✓ Branch 0 taken 30246 times.
✓ Branch 1 taken 30564 times.
60810 const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
527
4/4
✓ Branch 0 taken 31438 times.
✓ Branch 1 taken 29372 times.
✓ Branch 2 taken 3280 times.
✓ Branch 3 taken 28158 times.
60810 if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) {
528
2/2
✓ Branch 0 taken 29031 times.
✓ Branch 1 taken 3621 times.
32652 if (!sce->zeroes[w*16+g])
529 29031 prev_sf = sce->sf_idx[w*16+g];
530 32652 continue;
531 }
532
2/2
✓ Branch 0 taken 29454 times.
✓ Branch 1 taken 28158 times.
57612 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
533 29454 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
534 29454 sfb_energy += band->energy;
535
2/2
✓ Branch 0 taken 28559 times.
✓ Branch 1 taken 895 times.
29454 spread = FFMIN(spread, band->spread);
536 29454 threshold += band->threshold;
537
2/2
✓ Branch 0 taken 28158 times.
✓ Branch 1 taken 1296 times.
29454 if (!w2) {
538 28158 min_energy = max_energy = band->energy;
539 } else {
540
2/2
✓ Branch 0 taken 609 times.
✓ Branch 1 taken 687 times.
1296 min_energy = FFMIN(min_energy, band->energy);
541
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 530 times.
1296 max_energy = FFMAX(max_energy, band->energy);
542 }
543 }
544
545 /* Ramps down at ~8000Hz and loosens the dist threshold */
546 28158 dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias;
547
548 /* PNS is acceptable when all of these are true:
549 * 1. high spread energy (noise-like band)
550 * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
551 * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
552 *
553 * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important)
554 */
555
3/4
✓ Branch 0 taken 27917 times.
✓ Branch 1 taken 241 times.
✓ Branch 3 taken 27917 times.
✗ Branch 4 not taken.
28158 if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) ||
556
8/8
✓ Branch 0 taken 27917 times.
✓ Branch 1 taken 241 times.
✓ Branch 2 taken 4479 times.
✓ Branch 3 taken 23438 times.
✓ Branch 4 taken 4485 times.
✓ Branch 5 taken 235 times.
✓ Branch 6 taken 27700 times.
✓ Branch 7 taken 223 times.
28158 ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
557
5/6
✓ Branch 0 taken 27700 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23223 times.
✓ Branch 3 taken 4477 times.
✓ Branch 4 taken 14859 times.
✓ Branch 5 taken 8364 times.
27700 (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) ||
558
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 19185 times.
19336 min_energy < pns_transient_energy_r * max_energy ) {
559 8973 sce->pns_ener[w*16+g] = sfb_energy;
560
2/2
✓ Branch 0 taken 8732 times.
✓ Branch 1 taken 241 times.
8973 if (!sce->zeroes[w*16+g])
561 8732 prev_sf = sce->sf_idx[w*16+g];
562 8973 continue;
563 }
564
565
2/2
✓ Branch 0 taken 18601 times.
✓ Branch 1 taken 584 times.
19185 pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread);
566 19185 noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
567 19185 noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */
568
2/2
✓ Branch 0 taken 11907 times.
✓ Branch 1 taken 7278 times.
19185 if (prev != -1000) {
569 11907 int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO;
570
2/4
✓ Branch 0 taken 11907 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11907 times.
11907 if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) {
571 if (!sce->zeroes[w*16+g])
572 prev_sf = sce->sf_idx[w*16+g];
573 continue;
574 }
575 }
576
2/2
✓ Branch 0 taken 19240 times.
✓ Branch 1 taken 19185 times.
38425 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
577 float band_energy, scale, pns_senergy;
578 19240 const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
579 19240 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
580
2/2
✓ Branch 0 taken 638812 times.
✓ Branch 1 taken 19240 times.
658052 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
581 638812 s->random_state = lcg_random(s->random_state);
582 638812 PNS[i] = s->random_state;
583 }
584 19240 band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
585 19240 scale = noise_amp/sqrtf(band_energy);
586 19240 s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
587 19240 pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
588 19240 pns_energy += pns_senergy;
589 19240 s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
590 19240 s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]);
591 38480 dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
592 NOR34,
593 19240 sce->ics.swb_sizes[g],
594 19240 sce->sf_idx[(w+w2)*16+g],
595 19240 sce->band_alt[(w+w2)*16+g],
596 19240 lambda/band->threshold, INFINITY, NULL, NULL);
597 /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */
598 19240 dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold;
599 }
600
3/4
✓ Branch 0 taken 19185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3885 times.
✓ Branch 3 taken 15300 times.
19185 if (g && sce->band_type[w*16+g-1] == NOISE_BT) {
601 3885 dist2 += 5;
602 } else {
603 15300 dist2 += 9;
604 }
605 19185 energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
606 19185 sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
607
8/10
✓ Branch 0 taken 19185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14859 times.
✓ Branch 3 taken 4326 times.
✓ Branch 4 taken 14427 times.
✓ Branch 5 taken 432 times.
✓ Branch 6 taken 14427 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 34 times.
✓ Branch 9 taken 14393 times.
19185 if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
608 4360 sce->band_type[w*16+g] = NOISE_BT;
609 4360 sce->zeroes[w*16+g] = 0;
610 4360 prev = noise_sfi;
611 } else {
612
1/2
✓ Branch 0 taken 14825 times.
✗ Branch 1 not taken.
14825 if (!sce->zeroes[w*16+g])
613 14825 prev_sf = sce->sf_idx[w*16+g];
614 }
615 }
616 }
617 1314 }
618
619 2106 static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
620 {
621 FFPsyBand *band;
622 int w, g, w2;
623 2106 int wlen = 1024 / sce->ics.num_windows;
624 int bandwidth, cutoff;
625 2106 const float lambda = s->lambda;
626 2106 const float freq_mult = avctx->sample_rate*0.5f/wlen;
627
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2106 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2106 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2106 const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2106 times.
2106 const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
629
630 /* PNS candidacy must use the coder's actual coding bandwidth (s->bandwidth,
631 * fixed at init), not a separate heuristic, or it evaluates a different band
632 * range than the coder later codes (NMR relies on this output directly). */
633 2106 bandwidth = s->bandwidth;
634 2106 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
635
636 2106 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
637
2/2
✓ Branch 0 taken 2234 times.
✓ Branch 1 taken 2106 times.
4340 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
638
2/2
✓ Branch 0 taken 99639 times.
✓ Branch 1 taken 2234 times.
101873 for (g = 0; g < sce->ics.num_swb; g++) {
639 99639 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
640 99639 float min_energy = -1.0f, max_energy = 0.0f;
641 99639 const int start = sce->ics.swb_offset[g];
642 99639 const float freq = start*freq_mult;
643
2/2
✓ Branch 0 taken 49286 times.
✓ Branch 1 taken 50353 times.
99639 const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
644
4/4
✓ Branch 0 taken 51265 times.
✓ Branch 1 taken 48374 times.
✓ Branch 2 taken 3552 times.
✓ Branch 3 taken 47713 times.
99639 if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
645 51926 sce->can_pns[w*16+g] = 0;
646 51926 continue;
647 }
648
2/2
✓ Branch 0 taken 49177 times.
✓ Branch 1 taken 47713 times.
96890 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
649 49177 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
650 49177 sfb_energy += band->energy;
651
2/2
✓ Branch 0 taken 48186 times.
✓ Branch 1 taken 991 times.
49177 spread = FFMIN(spread, band->spread);
652 49177 threshold += band->threshold;
653
2/2
✓ Branch 0 taken 47713 times.
✓ Branch 1 taken 1464 times.
49177 if (!w2) {
654 47713 min_energy = max_energy = band->energy;
655 } else {
656
2/2
✓ Branch 0 taken 681 times.
✓ Branch 1 taken 783 times.
1464 min_energy = FFMIN(min_energy, band->energy);
657
2/2
✓ Branch 0 taken 861 times.
✓ Branch 1 taken 603 times.
1464 max_energy = FFMAX(max_energy, band->energy);
658 }
659 }
660
661 /* PNS is acceptable when all of these are true:
662 * 1. high spread energy (noise-like band)
663 * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
664 * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
665 */
666 47713 sce->pns_ener[w*16+g] = sfb_energy;
667 {
668 /* near-mask PNS class (E in [thr/4, 2*thr]): deletion
669 * candidates go to noise, not silence (AAC_PNSHOLE) */
670
2/2
✓ Branch 0 taken 28244 times.
✓ Branch 1 taken 19469 times.
75957 int near = sfb_energy < 2.0f * threshold &&
671
2/2
✓ Branch 0 taken 27722 times.
✓ Branch 1 taken 522 times.
28244 sfb_energy > threshold * 0.25f;
672
2/2
✓ Branch 0 taken 27722 times.
✓ Branch 1 taken 19991 times.
47713 if (near) {
673 /* deletion candidate: noise beats the ~silent rendition */
674
2/2
✓ Branch 0 taken 27492 times.
✓ Branch 1 taken 230 times.
55214 sce->can_pns[w*16+g] = spread >= spread_threshold &&
675
2/2
✓ Branch 0 taken 27490 times.
✓ Branch 1 taken 2 times.
27492 min_energy >= 0.2f * max_energy;
676
6/6
✓ Branch 0 taken 19469 times.
✓ Branch 1 taken 522 times.
✓ Branch 2 taken 18687 times.
✓ Branch 3 taken 782 times.
✓ Branch 4 taken 652 times.
✓ Branch 5 taken 18035 times.
19991 } else if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
677 1956 sce->can_pns[w*16+g] = 0;
678 } else {
679 18035 sce->can_pns[w*16+g] = 1;
680 }
681 }
682 }
683 }
684 2106 }
685
686 static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
687 {
688 int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
689 uint8_t nextband0[128], nextband1[128];
690 float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1;
691 float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3;
692 float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5;
693 const float lambda = s->lambda;
694 const float mslambda = FFMIN(1.0f, lambda / 120.f);
695 SingleChannelElement *sce0 = &cpe->ch[0];
696 SingleChannelElement *sce1 = &cpe->ch[1];
697 if (!cpe->common_window)
698 return;
699
700 /** Scout out next nonzero bands */
701 ff_init_nextband_map(sce0, nextband0);
702 ff_init_nextband_map(sce1, nextband1);
703
704 prev_mid = sce0->sf_idx[0];
705 prev_side = sce1->sf_idx[0];
706 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
707 start = 0;
708 for (g = 0; g < sce0->ics.num_swb; g++) {
709 float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
710 if (!cpe->is_mask[w*16+g])
711 cpe->ms_mask[w*16+g] = 0;
712 if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
713 float Mmax = 0.0f, Smax = 0.0f;
714
715 /* Must compute mid/side SF and book for the whole window group */
716 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
717 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
718 M[i] = (sce0->coeffs[start+(w+w2)*128+i]
719 + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
720 S[i] = M[i]
721 - sce1->coeffs[start+(w+w2)*128+i];
722 }
723 s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
724 s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
725 for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
726 Mmax = FFMAX(Mmax, M34[i]);
727 Smax = FFMAX(Smax, S34[i]);
728 }
729 }
730
731 for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
732 float dist1 = 0.0f, dist2 = 0.0f;
733 int B0 = 0, B1 = 0;
734 int minidx;
735 int mididx, sididx;
736 int midcb, sidcb;
737
738 minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]);
739 mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512);
740 sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512);
741 if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
742 && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
743 || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) {
744 /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
745 continue;
746 }
747
748 midcb = find_min_book(Mmax, mididx);
749 sidcb = find_min_book(Smax, sididx);
750
751 /* No CB can be zero */
752 midcb = FFMAX(1,midcb);
753 sidcb = FFMAX(1,sidcb);
754
755 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
756 FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
757 FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
758 float minthr = FFMIN(band0->threshold, band1->threshold);
759 int b1,b2,b3,b4;
760 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
761 M[i] = (sce0->coeffs[start+(w+w2)*128+i]
762 + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
763 S[i] = M[i]
764 - sce1->coeffs[start+(w+w2)*128+i];
765 }
766
767 s->aacdsp.abs_pow34(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
768 s->aacdsp.abs_pow34(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
769 s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
770 s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
771 dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
772 L34,
773 sce0->ics.swb_sizes[g],
774 sce0->sf_idx[w*16+g],
775 sce0->band_type[w*16+g],
776 lambda / (band0->threshold + FLT_MIN), INFINITY, &b1, NULL);
777 dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
778 R34,
779 sce1->ics.swb_sizes[g],
780 sce1->sf_idx[w*16+g],
781 sce1->band_type[w*16+g],
782 lambda / (band1->threshold + FLT_MIN), INFINITY, &b2, NULL);
783 dist2 += quantize_band_cost(s, M,
784 M34,
785 sce0->ics.swb_sizes[g],
786 mididx,
787 midcb,
788 lambda / (minthr + FLT_MIN), INFINITY, &b3, NULL);
789 dist2 += quantize_band_cost(s, S,
790 S34,
791 sce1->ics.swb_sizes[g],
792 sididx,
793 sidcb,
794 mslambda / (minthr * bmax + FLT_MIN), INFINITY, &b4, NULL);
795 B0 += b1+b2;
796 B1 += b3+b4;
797 dist1 -= b1+b2;
798 dist2 -= b3+b4;
799 }
800 cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
801 if (cpe->ms_mask[w*16+g]) {
802 if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) {
803 sce0->sf_idx[w*16+g] = mididx;
804 sce1->sf_idx[w*16+g] = sididx;
805 sce0->band_type[w*16+g] = midcb;
806 sce1->band_type[w*16+g] = sidcb;
807 } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) {
808 /* ms_mask unneeded, and it confuses some decoders */
809 cpe->ms_mask[w*16+g] = 0;
810 }
811 break;
812 } else if (B1 > B0) {
813 /* More boost won't fix this */
814 break;
815 }
816 }
817 }
818 if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
819 prev_mid = sce0->sf_idx[w*16+g];
820 if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT)
821 prev_side = sce1->sf_idx[w*16+g];
822 start += sce0->ics.swb_sizes[g];
823 }
824 }
825 }
826
827 const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
828 [AAC_CODER_TWOLOOP] = {
829 search_for_quantizers_twoloop,
830 codebook_trellis_rate,
831 quantize_and_encode_band,
832 ff_aac_encode_tns_info,
833 ff_aac_apply_tns,
834 set_special_band_scalefactors,
835 search_for_pns,
836 mark_pns,
837 ff_aac_search_for_tns,
838 search_for_ms,
839 ff_aac_search_for_is,
840 },
841 [AAC_CODER_FAST] = {
842 search_for_quantizers_fast,
843 codebook_trellis_rate,
844 quantize_and_encode_band,
845 ff_aac_encode_tns_info,
846 ff_aac_apply_tns,
847 set_special_band_scalefactors,
848 search_for_pns,
849 mark_pns,
850 ff_aac_search_for_tns,
851 search_for_ms,
852 ff_aac_search_for_is,
853 },
854 [AAC_CODER_NMR] = {
855 search_for_quantizers_nmr,
856 codebook_trellis_rate,
857 quantize_and_encode_band,
858 ff_aac_encode_tns_info,
859 ff_aac_apply_tns,
860 set_special_band_scalefactors,
861 NULL, /* PNS decided in the trellis (search_for_quantizers_nmr) */
862 mark_pns,
863 ff_aac_search_for_tns,
864 NULL,
865 NULL,
866 },
867 };
868