FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacpsy.c
Date: 2026-10-08 05:59:33
Exec Total Coverage
Lines: 555 635 87.4%
Functions: 19 21 90.5%
Branches: 445 539 82.6%

Line Branch Exec Source
1 /*
2 * AAC encoder psychoacoustic model
3 * Copyright (C) 2008 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 encoder psychoacoustic model
25 */
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/ffmath.h"
29 #include "libavutil/mem.h"
30
31 #include "avcodec.h"
32 #include "aac.h"
33 #include "psymodel.h"
34
35 /***********************************
36 * TODOs:
37 * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
38 * control quality for quality-based output
39 **********************************/
40
41 /**
42 * constants for 3GPP AAC psychoacoustic model
43 * @{
44 */
45 #define PSY_3GPP_THR_SPREAD_HI 1.5f // spreading factor for low-to-hi threshold spreading (15 dB/Bark)
46 #define PSY_3GPP_THR_SPREAD_LOW 3.0f // spreading factor for hi-to-low threshold spreading (30 dB/Bark)
47 /* spreading factor for low-to-hi energy spreading, long block, > 22kbps/channel (20dB/Bark) */
48 #define PSY_3GPP_EN_SPREAD_HI_L1 2.0f
49 /* spreading factor for low-to-hi energy spreading, long block, <= 22kbps/channel (15dB/Bark) */
50 #define PSY_3GPP_EN_SPREAD_HI_L2 1.5f
51 /* spreading factor for low-to-hi energy spreading, short block (15 dB/Bark) */
52 #define PSY_3GPP_EN_SPREAD_HI_S 1.5f
53 /* spreading factor for hi-to-low energy spreading, long block (30dB/Bark) */
54 #define PSY_3GPP_EN_SPREAD_LOW_L 3.0f
55 /* spreading factor for hi-to-low energy spreading, short block (20dB/Bark) */
56 #define PSY_3GPP_EN_SPREAD_LOW_S 2.0f
57
58 #define PSY_3GPP_RPEMIN 0.01f
59 #define PSY_3GPP_RPELEV 2.0f
60
61 #define PSY_3GPP_C1 3.0f /* log2(8) */
62 #define PSY_3GPP_C2 1.3219281f /* log2(2.5) */
63 #define PSY_3GPP_C3 0.55935729f /* 1 - C2 / C1 */
64
65 #define PSY_SNR_1DB 7.9432821e-1f /* -1dB */
66 #define PSY_SNR_25DB 3.1622776e-3f /* -25dB */
67
68 #define PSY_3GPP_SAVE_SLOPE_L -0.46666667f
69 #define PSY_3GPP_SAVE_SLOPE_S -0.36363637f
70 #define PSY_3GPP_SAVE_ADD_L -0.84285712f
71 #define PSY_3GPP_SAVE_ADD_S -0.75f
72 #define PSY_3GPP_SPEND_SLOPE_L 0.66666669f
73 #define PSY_3GPP_SPEND_SLOPE_S 0.81818181f
74 #define PSY_3GPP_SPEND_ADD_L -0.35f
75 #define PSY_3GPP_SPEND_ADD_S -0.26111111f
76 #define PSY_3GPP_CLIP_LO_L 0.2f
77 #define PSY_3GPP_CLIP_LO_S 0.2f
78 #define PSY_3GPP_CLIP_HI_L 0.95f
79 #define PSY_3GPP_CLIP_HI_S 0.75f
80
81 /* Floor on how close a coded band's mask may come to the band's own energy.
82 * The two rate-control families want different shapes, and measurably so:
83 * in VBR/ABR the mask IS the rate authority, so a mask that has risen to meet
84 * its band's energy tells the solver the band is free to destroy and it buys
85 * no bits for it - a broadband floor is right there. In CBR the budget is
86 * fixed and the mask only ranks bands against each other, so the same
87 * broadband floor just moves bits around and costs ~2% Zim on random content;
88 * restricted to the top end, where the mask degenerates to within 2 dB of band
89 * energy on every sample measured, it is a clear win. */
90 #define PSY_THRFL_QUALITY 10.0f /* VBR/ABR depth, dB, all bands */
91 #define PSY_THRFL_CBR 6.0f /* CBR depth, dB */
92 #define PSY_THRFL_CBR_KNEE 8000.0f /* CBR: only above this frequency */
93
94 /* Strength of the 3GPP bit-demand curve: its deviation from unity is
95 * amplified, since the trellis coder wants far stronger per-frame budget
96 * modulation than the reference encoder's gentle curve provides.
97 * The amplified factor goes negative in high-PE frames on a starved
98 * reservoir (15-30% of CBR frames). The desired PE is then <= 0 and the
99 * reduction raises every threshold as far as the min-SNR and hole rules
100 * allow, so those frames are shaped for constant SNR instead of by the
101 * mask. That regime is load-bearing: flooring the demand at frame_bits/8
102 * costs 3-5% Zimtohrli at 64 kbps stereo. */
103 #define PSY_3GPP_DEMAND_SCALE 2.5f
104
105 #define PSY_3GPP_AH_THR_LONG 0.5f
106 #define PSY_3GPP_AH_THR_SHORT 0.63f
107
108 #define PSY_PE_FORGET_SLOPE 511
109
110 enum {
111 PSY_3GPP_AH_NONE,
112 PSY_3GPP_AH_INACTIVE,
113 PSY_3GPP_AH_ACTIVE
114 };
115
116 #define PSY_3GPP_BITS_TO_PE(bits) ((bits) * 1.18f)
117 #define PSY_3GPP_PE_TO_BITS(bits) ((bits) / 1.18f)
118
119 /* LAME psy model constants */
120 #define PSY_LAME_FIR_LEN 21 ///< LAME psy model FIR order
121 #define AAC_BLOCK_SIZE_LONG 1024 ///< long block size
122 #define AAC_BLOCK_SIZE_SHORT 128 ///< short block size
123 #define AAC_NUM_BLOCKS_SHORT 8 ///< number of blocks in a short sequence
124 #define PSY_LAME_NUM_SUBBLOCKS 2 ///< Number of sub-blocks in each short block
125
126 /* Pre-echo-aware attack detection: the LAME ratio test misses gentler attacks after a quiet
127 * gap, which then stay long and pre-echo. For an isolated onset (long for PSY_LAME_PE_GAP
128 * frames) whose pre-onset is below PSY_LAME_PE_QUIET of the frame peak, scale the threshold by
129 * PSY_LAME_PE_RED so it switches short; dense-transient content never qualifies. */
130 #define PSY_LAME_PE_GAP 12 ///< min consecutive long frames before the relaxation applies
131 #define PSY_LAME_PE_QUIET 0.4f ///< pre-onset must be below this fraction of the frame peak
132 #define PSY_LAME_PE_RED 0.45f ///< attack-threshold multiplier for a qualifying isolated onset
133
134 /* The novelty check must see at least one full period of a pulse train to
135 * recognize its pulses as repeats; 30 sub-blocks reaches down to ~23Hz. */
136 #define PSY_LAME_HIST 32 ///< HP sub-block peak history depth
137 #define PSY_LAME_NOV_BACK 30 ///< novelty look-back in sub-blocks
138
139 /* HF-novelty veto: an attack candidate stays long when its first-difference
140 * envelope is both relatively and absolutely unremarkable, and pre-echo is
141 * masked by what precedes it. */
142 #define PSY_LAME_HFN_REL 4.0f ///< max derivative rise over the recent envelope
143 #define PSY_LAME_HFN_ABS 2000.0f ///< max absolute derivative peak
144 #define PSY_LAME_HFN_PRE 8.0f ///< max candidate rise over the ~5ms pre-attack minimum
145
146 /* Gap-onset detection against a decaying program-level peak-hold */
147 #define PSY_LAME_GAP_DEPTH 6.0f ///< a gap is this far below the peak-hold
148 #define PSY_LAME_GAP_BACK 24 ///< gap look-back in sub-blocks (<= PSY_LAME_HIST)
149 #define PSY_LAME_GAP_LEVEL 0.4f ///< candidate must reach this fraction of the peak-hold
150 #define PSY_LAME_GAP_FLOOR 4000.0f ///< and this absolute peak
151 #define PSY_LAME_GAP_TOWER 4.0f ///< HP tower: rise over the whole look-back
152
153 /* Level-homogeneous short-window grouping */
154 #define PSY_LAME_GRP_RATIO 2.5f ///< max adjacent-window level ratio inside a group
155 #define PSY_LAME_GRP_MAX 4 ///< max windows per group
156
157 /**
158 * @}
159 */
160
161 /**
162 * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
163 */
164 typedef struct AacPsyBand{
165 float energy; ///< band energy
166 float thr; ///< energy threshold
167 float thr_quiet; ///< threshold in quiet
168 float nz_lines; ///< number of non-zero spectral lines
169 float active_lines; ///< number of active spectral lines
170 float pe; ///< perceptual entropy
171 float pe_const; ///< constant part of the PE calculation
172 float norm_fac; ///< normalization factor for linearization
173 int avoid_holes; ///< hole avoidance flag
174 }AacPsyBand;
175
176 /**
177 * single/pair channel context for psychoacoustic model
178 */
179 typedef struct AacPsyChannel{
180 AacPsyBand band[128]; ///< bands information
181 AacPsyBand prev_band[128]; ///< bands information from the previous frame
182
183 float win_energy; ///< sliding average of channel energy
184 float iir_state[2]; ///< hi-pass IIR filter state
185 uint8_t next_grouping; ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
186 enum WindowSequence next_window_seq; ///< window sequence to be used in the next frame
187 /* LAME psy model specific members */
188 float attack_threshold; ///< attack threshold for this channel
189 float prev_energy_subshort[AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
190 float next_win_level[AAC_NUM_BLOCKS_SHORT]; ///< lookahead short-window peak levels (grouping homogeneity)
191 float dif_env_hist[PSY_LAME_HIST]; ///< rolling first-difference sub-block peak envelope (HF novelty)
192 float hp_env_hist[PSY_LAME_HIST]; ///< rolling HP sub-block peak envelope
193 float raw_env_hist[PSY_LAME_HIST]; ///< rolling broadband sub-block peak envelope
194 float gap_wall; ///< decaying broadband peak-hold (gap-onset reference)
195 float gap_wall_hp; ///< decaying HP peak-hold (gap-onset reference)
196 int prev_attack; ///< attack value for the last short block in the previous sequence
197 int next_attack0_zero; ///< whether attack[0] of the next frame is zero
198 int frames_since_short; ///< consecutive long frames (pre-echo-aware isolated-onset gate)
199 float prev_frame_energy; ///< previous frame's full-band lookahead energy (attack veto)
200 int64_t win_count; ///< window() calls so far (frame counter for pair sync)
201 int64_t last_att; ///< win_count value of this channel's last own attack
202
203 /* rate-loop re-analysis rewind state, see psy_3gpp_analyze() */
204 int64_t rc_frame_num; ///< frame this channel last saved rewind state for
205 AacPsyBand rc_prev_band[128]; ///< prev_band as it was entering the frame
206 }AacPsyChannel;
207
208 /**
209 * psychoacoustic model frame type-dependent coefficients
210 */
211 typedef struct AacPsyCoeffs{
212 float ath; ///< absolute threshold of hearing per bands
213 float barks; ///< Bark value for each spectral band in long frame
214 float spread_low[2]; ///< spreading factor for low-to-high threshold spreading in long frame
215 float spread_hi [2]; ///< spreading factor for high-to-low threshold spreading in long frame
216 float min_snr; ///< minimal SNR
217 }AacPsyCoeffs;
218
219 /**
220 * 3GPP TS26.403-inspired psychoacoustic model specific data
221 */
222 typedef struct AacPsyContext{
223 int chan_bitrate; ///< bitrate per channel
224 int frame_bits; ///< average bits per frame
225 int fill_level; ///< bit reservoir fill level
226 struct {
227 float min; ///< minimum allowed PE for bit factor calculation
228 float max; ///< maximum allowed PE for bit factor calculation
229 float previous; ///< allowed PE of the previous frame
230 float correction; ///< PE correction factor
231 } pe;
232 AacPsyCoeffs psy_coef[2][64];
233 AacPsyChannel *ch;
234 float global_quality; ///< normalized global quality taken from avctx
235
236 /* rate-loop re-analysis rewind state, see psy_3gpp_analyze() */
237 int64_t rc_frame_num; ///< frame the rewind state was saved for
238 int rc_first_ch; ///< first channel analyzed in that frame
239 int rc_fill_level;
240 float rc_pe_min, rc_pe_max, rc_pe_previous;
241 }AacPsyContext;
242
243 /**
244 * LAME psy model preset struct
245 */
246 typedef struct PsyLamePreset {
247 int quality; ///< Quality to map the rest of the values to.
248 /* This is overloaded to be both kbps per channel in ABR mode, and
249 * requested quality in constant quality mode.
250 */
251 float st_lrm; ///< short threshold for L, R, and M channels
252 } PsyLamePreset;
253
254 /**
255 * LAME psy model preset table for ABR
256 */
257 static const PsyLamePreset psy_abr_map[] = {
258 /* TODO: Tuning. These were taken from LAME. */
259 /* kbps/ch st_lrm */
260 { 8, 7.60},
261 { 16, 7.60},
262 { 24, 7.60},
263 { 32, 7.60},
264 { 40, 7.60},
265 { 48, 7.60},
266 { 56, 7.60},
267 { 64, 7.40},
268 { 80, 7.00},
269 { 96, 6.60},
270 {112, 6.20},
271 {128, 6.20},
272 {160, 6.20}
273 };
274
275 /**
276 * LAME psy model preset table for constant quality
277 */
278 static const PsyLamePreset psy_vbr_map[] = {
279 /* vbr_q st_lrm */
280 { 0, 4.20},
281 { 1, 4.20},
282 { 2, 4.20},
283 { 3, 4.20},
284 { 4, 4.20},
285 { 5, 4.20},
286 { 6, 4.20},
287 { 7, 4.20},
288 { 8, 4.20},
289 { 9, 4.20},
290 {10, 4.20}
291 };
292
293 /**
294 * LAME psy model FIR coefficient table
295 */
296 static const float psy_fir_coeffs[] = {
297 -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
298 -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
299 -5.52212e-17 * 2, -0.313819 * 2
300 };
301
302 /**
303 * Calculate the ABR attack threshold from the above LAME psymodel table.
304 */
305 143 static float lame_calc_attack_threshold(int bitrate)
306 {
307 /* Assume max bitrate to start with */
308 143 int lower_range = 12, upper_range = 12;
309 143 int lower_range_kbps = psy_abr_map[12].quality;
310 143 int upper_range_kbps = psy_abr_map[12].quality;
311 int i;
312
313 /* Determine which bitrates the value specified falls between.
314 * If the loop ends without breaking our above assumption of 320kbps was correct.
315 */
316
2/2
✓ Branch 0 taken 1046 times.
✓ Branch 1 taken 4 times.
1050 for (i = 1; i < 13; i++) {
317
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 907 times.
1046 if (FFMAX(bitrate, psy_abr_map[i].quality) != bitrate) {
318 139 upper_range = i;
319 139 upper_range_kbps = psy_abr_map[i ].quality;
320 139 lower_range = i - 1;
321 139 lower_range_kbps = psy_abr_map[i - 1].quality;
322 139 break; /* Upper range found */
323 }
324 }
325
326 /* Determine which range the value specified is closer to */
327
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 72 times.
143 if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps))
328 71 return psy_abr_map[lower_range].st_lrm;
329 72 return psy_abr_map[upper_range].st_lrm;
330 }
331
332 /**
333 * LAME psy model specific initialization
334 */
335 27 static av_cold void lame_window_init(FFPsyContext *fctx, AacPsyContext *ctx, AVCodecContext *avctx)
336 {
337 int i, j;
338
339
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 27 times.
174 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
340 147 AacPsyChannel *pch = &ctx->ch[i];
341
342
4/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 143 times.
147 if ((avctx->flags & AV_CODEC_FLAG_QSCALE) || fctx->unbounded_pe)
343 /* quality-target coders (VBR and ABR) switch windows for quality,
344 * not rate: use the quality attack map regardless of bit_rate */
345 4 pch->attack_threshold = psy_vbr_map[av_clip(avctx->global_quality / FF_QP2LAMBDA, 0, 10)].st_lrm;
346 else
347 143 pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->ch_layout.nb_channels / 1000);
348
349
2/2
✓ Branch 0 taken 2352 times.
✓ Branch 1 taken 147 times.
2499 for (j = 0; j < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; j++)
350 2352 pch->prev_energy_subshort[j] = 10.0f;
351
2/2
✓ Branch 0 taken 4704 times.
✓ Branch 1 taken 147 times.
4851 for (j = 0; j < PSY_LAME_HIST; j++)
352 4704 pch->hp_env_hist[j] = pch->raw_env_hist[j] = pch->dif_env_hist[j] = 10.0f;
353 147 pch->gap_wall = pch->gap_wall_hp = 0.0f;
354 }
355 27 }
356
357 /**
358 * Calculate Bark value for given line.
359 */
360 1728 static av_cold float calc_bark(float f)
361 {
362 1728 return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
363 }
364
365 #define ATH_ADD 4
366 /**
367 * Calculate ATH value for given frequency.
368 * Borrowed from Lame.
369 */
370 35221 static av_cold float ath(float f, float add)
371 {
372 35221 f /= 1000.0f;
373 35221 return 3.64 * pow(f, -0.8)
374 35221 - 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
375 35221 + 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
376 35221 + (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
377 }
378
379 27 static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
380 AacPsyContext *pctx;
381 float bark;
382 int i, j, g, start;
383 float prev, minscale, minath, minsnr, pe_min;
384
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
27 int chan_bitrate = ctx->avctx->bit_rate / ((ctx->avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : ctx->avctx->ch_layout.nb_channels);
385
386
1/8
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
27 const int bandwidth = ctx->cutoff ? ctx->cutoff : AAC_CUTOFF(ctx->avctx);
387 27 const float num_bark = calc_bark((float)bandwidth);
388
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (bandwidth <= 0)
390 ✗ return AVERROR(EINVAL);
391
392 27 ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!ctx->model_priv_data)
394 ✗ return AVERROR(ENOMEM);
395 27 pctx = ctx->model_priv_data;
396
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 pctx->global_quality = (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120) * 0.01f;
397
398
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 if (ctx->avctx->flags & AV_CODEC_FLAG_QSCALE) {
399 /* Use the target average bitrate to compute spread parameters */
400
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 chan_bitrate = (int)(chan_bitrate / 120.0 * (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120));
401 }
402
403 27 pctx->chan_bitrate = chan_bitrate;
404 27 pctx->frame_bits = FFMIN(2560, chan_bitrate * AAC_BLOCK_SIZE_LONG / ctx->avctx->sample_rate);
405 27 pctx->pe.min = 8.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
406 27 pctx->pe.max = 12.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
407 27 ctx->bitres.size = 6144 - pctx->frame_bits;
408 27 ctx->bitres.size -= ctx->bitres.size % 8;
409 27 pctx->fill_level = ctx->bitres.size;
410 27 minath = ath(3410 - 0.733 * ATH_ADD, ATH_ADD);
411
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 27 times.
81 for (j = 0; j < 2; j++) {
412 54 AacPsyCoeffs *coeffs = pctx->psy_coef[j];
413 54 const uint8_t *band_sizes = ctx->bands[j];
414
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
415
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 float avg_chan_bits = chan_bitrate * (j ? 128.0f : 1024.0f) / ctx->avctx->sample_rate;
416 /* reference encoder uses 2.4% here instead of 60% like the spec says */
417 54 float bark_pe = 0.024f * PSY_3GPP_BITS_TO_PE(avg_chan_bits) / num_bark;
418
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 float en_spread_low = j ? PSY_3GPP_EN_SPREAD_LOW_S : PSY_3GPP_EN_SPREAD_LOW_L;
419 /* High energy spreading for long blocks <= 22kbps/channel and short blocks are the same. */
420
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
54 float en_spread_hi = (j || (chan_bitrate <= 22.0f)) ? PSY_3GPP_EN_SPREAD_HI_S : PSY_3GPP_EN_SPREAD_HI_L1;
421
422 54 i = 0;
423 54 prev = 0.0;
424
2/2
✓ Branch 0 taken 1701 times.
✓ Branch 1 taken 54 times.
1755 for (g = 0; g < ctx->num_bands[j]; g++) {
425 1701 i += band_sizes[g];
426 1701 bark = calc_bark((i-1) * line_to_frequency);
427 1701 coeffs[g].barks = (bark + prev) / 2.0;
428 1701 prev = bark;
429 }
430
2/2
✓ Branch 0 taken 1647 times.
✓ Branch 1 taken 54 times.
1701 for (g = 0; g < ctx->num_bands[j] - 1; g++) {
431 1647 AacPsyCoeffs *coeff = &coeffs[g];
432 1647 float bark_width = coeffs[g+1].barks - coeffs->barks;
433 1647 coeff->spread_low[0] = ff_exp10(-bark_width * PSY_3GPP_THR_SPREAD_LOW);
434 1647 coeff->spread_hi [0] = ff_exp10(-bark_width * PSY_3GPP_THR_SPREAD_HI);
435 1647 coeff->spread_low[1] = ff_exp10(-bark_width * en_spread_low);
436 1647 coeff->spread_hi [1] = ff_exp10(-bark_width * en_spread_hi);
437 1647 pe_min = bark_pe * bark_width;
438 1647 minsnr = exp2(pe_min / band_sizes[g]) - 1.5f;
439 1647 coeff->min_snr = av_clipf(1.0f / minsnr, PSY_SNR_25DB, PSY_SNR_1DB);
440 }
441 54 start = 0;
442
2/2
✓ Branch 0 taken 1701 times.
✓ Branch 1 taken 54 times.
1755 for (g = 0; g < ctx->num_bands[j]; g++) {
443 1701 minscale = ath(start * line_to_frequency, ATH_ADD);
444
2/2
✓ Branch 0 taken 29403 times.
✓ Branch 1 taken 1701 times.
31104 for (i = 1; i < band_sizes[g]; i++)
445
2/2
✓ Branch 1 taken 4090 times.
✓ Branch 2 taken 25313 times.
29403 minscale = FFMIN(minscale, ath((start + i) * line_to_frequency, ATH_ADD));
446 1701 coeffs[g].ath = minscale - minath;
447 1701 start += band_sizes[g];
448 }
449 }
450
451 27 pctx->ch = av_calloc(ctx->avctx->ch_layout.nb_channels, sizeof(*pctx->ch));
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!pctx->ch) {
453 ✗ av_freep(&ctx->model_priv_data);
454 ✗ return AVERROR(ENOMEM);
455 }
456
457 27 pctx->rc_frame_num = -1;
458
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 27 times.
174 for (i = 0; i < ctx->avctx->ch_layout.nb_channels; i++)
459 147 pctx->ch[i].rc_frame_num = -1;
460
461 27 lame_window_init(ctx, pctx, ctx->avctx);
462
463 27 return 0;
464 }
465
466 /**
467 * IIR filter used in block switching decision
468 */
469 ✗ static float iir_filter(int in, float state[2])
470 {
471 float ret;
472
473 ✗ ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
474 ✗ state[0] = in;
475 ✗ state[1] = ret;
476 ✗ return ret;
477 }
478
479 /**
480 * window grouping information stored as bits (0 - new group, 1 - group continues)
481 */
482 static const uint8_t window_grouping[9] = {
483 0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
484 };
485
486 /**
487 * Tell encoder which window types to use.
488 * @see 3GPP TS26.403 5.4.1 "Blockswitching"
489 */
490 ✗ av_unused static FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
491 const int16_t *audio,
492 const int16_t *la,
493 int channel, int prev_type)
494 {
495 int i, j;
496 ✗ int br = ((AacPsyContext*)ctx->model_priv_data)->chan_bitrate;
497 ✗ int attack_ratio = br <= 16000 ? 18 : 10;
498 ✗ AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
499 ✗ AacPsyChannel *pch = &pctx->ch[channel];
500 ✗ uint8_t grouping = 0;
501 ✗ int next_type = pch->next_window_seq;
502 ✗ FFPsyWindowInfo wi = { { 0 } };
503
504 ✗ if (la) {
505 float s[8], v;
506 ✗ int switch_to_eight = 0;
507 ✗ float sum = 0.0, sum2 = 0.0;
508 ✗ int attack_n = 0;
509 ✗ int stay_short = 0;
510 ✗ for (i = 0; i < 8; i++) {
511 ✗ for (j = 0; j < 128; j++) {
512 ✗ v = iir_filter(la[i*128+j], pch->iir_state);
513 ✗ sum += v*v;
514 }
515 ✗ s[i] = sum;
516 ✗ sum2 += sum;
517 }
518 ✗ for (i = 0; i < 8; i++) {
519 ✗ if (s[i] > pch->win_energy * attack_ratio) {
520 ✗ attack_n = i + 1;
521 ✗ switch_to_eight = 1;
522 ✗ break;
523 }
524 }
525 ✗ pch->win_energy = pch->win_energy*7/8 + sum2/64;
526
527 ✗ wi.window_type[1] = prev_type;
528 ✗ switch (prev_type) {
529 ✗ case ONLY_LONG_SEQUENCE:
530 ✗ wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
531 ✗ next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
532 ✗ break;
533 ✗ case LONG_START_SEQUENCE:
534 ✗ wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
535 ✗ grouping = pch->next_grouping;
536 ✗ next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
537 ✗ break;
538 ✗ case LONG_STOP_SEQUENCE:
539 ✗ wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
540 ✗ next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
541 ✗ break;
542 ✗ case EIGHT_SHORT_SEQUENCE:
543 ✗ stay_short = next_type == EIGHT_SHORT_SEQUENCE || switch_to_eight;
544 ✗ wi.window_type[0] = stay_short ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
545 ✗ grouping = next_type == EIGHT_SHORT_SEQUENCE ? pch->next_grouping : 0;
546 ✗ next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
547 ✗ break;
548 }
549
550 ✗ pch->next_grouping = window_grouping[attack_n];
551 ✗ pch->next_window_seq = next_type;
552 } else {
553 ✗ for (i = 0; i < 3; i++)
554 ✗ wi.window_type[i] = prev_type;
555 ✗ grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
556 }
557
558 ✗ wi.window_shape = 1;
559 ✗ if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
560 ✗ wi.num_windows = 1;
561 ✗ wi.grouping[0] = 1;
562 } else {
563 ✗ int lastgrp = 0;
564 ✗ wi.num_windows = 8;
565 ✗ for (i = 0; i < 8; i++) {
566 ✗ if (!((grouping >> i) & 1))
567 ✗ lastgrp = i;
568 ✗ wi.grouping[lastgrp]++;
569 }
570 }
571
572 ✗ return wi;
573 }
574
575 /* 5.6.1.2 "Calculation of Bit Demand" */
576 9730 static int calc_bit_demand(AacPsyContext *ctx, float pe, int bits, int size,
577 int short_window)
578 {
579
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 9317 times.
9730 const float bitsave_slope = short_window ? PSY_3GPP_SAVE_SLOPE_S : PSY_3GPP_SAVE_SLOPE_L;
580
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 9317 times.
9730 const float bitsave_add = short_window ? PSY_3GPP_SAVE_ADD_S : PSY_3GPP_SAVE_ADD_L;
581
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 9317 times.
9730 const float bitspend_slope = short_window ? PSY_3GPP_SPEND_SLOPE_S : PSY_3GPP_SPEND_SLOPE_L;
582
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 9317 times.
9730 const float bitspend_add = short_window ? PSY_3GPP_SPEND_ADD_S : PSY_3GPP_SPEND_ADD_L;
583 9730 const float clip_low = short_window ? PSY_3GPP_CLIP_LO_S : PSY_3GPP_CLIP_LO_L;
584
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 9317 times.
9730 const float clip_high = short_window ? PSY_3GPP_CLIP_HI_S : PSY_3GPP_CLIP_HI_L;
585 float clipped_pe, bit_save, bit_spend, bit_factor, fill_level, forgetful_min_pe;
586
587 9730 ctx->fill_level += ctx->frame_bits - bits;
588 9730 ctx->fill_level = av_clip(ctx->fill_level, 0, size);
589 9730 fill_level = av_clipf((float)ctx->fill_level / size, clip_low, clip_high);
590 9730 clipped_pe = av_clipf(pe, ctx->pe.min, ctx->pe.max);
591 9730 bit_save = (fill_level + bitsave_add) * bitsave_slope;
592 assert(bit_save <= 0.3f && bit_save >= -0.05000001f);
593 9730 bit_spend = (fill_level + bitspend_add) * bitspend_slope;
594 assert(bit_spend <= 0.5f && bit_spend >= -0.1f);
595 /* The bit factor graph in the spec is obviously incorrect.
596 * bit_spend + ((bit_spend - bit_spend))...
597 * The reference encoder subtracts everything from 1, but also seems incorrect.
598 * 1 - bit_save + ((bit_spend + bit_save))...
599 * Hopefully below is correct.
600 */
601 9730 bit_factor = 1.0f - bit_save + ((bit_spend - bit_save) / (ctx->pe.max - ctx->pe.min)) * (clipped_pe - ctx->pe.min);
602 9730 bit_factor = 1.0f + (bit_factor - 1.0f) * PSY_3GPP_DEMAND_SCALE;
603 /* NOTE: The reference encoder attempts to center pe max/min around the current pe.
604 * Here we do that by slowly forgetting pe.min when pe stays in a range that makes
605 * it unlikely (ie: above the mean)
606 */
607
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 9678 times.
9730 ctx->pe.max = FFMAX(pe, ctx->pe.max);
608 19460 forgetful_min_pe = ((ctx->pe.min * PSY_PE_FORGET_SLOPE)
609
2/2
✓ Branch 0 taken 3091 times.
✓ Branch 1 taken 6639 times.
9730 + FFMAX(ctx->pe.min, pe * (pe / ctx->pe.max))) / (PSY_PE_FORGET_SLOPE + 1);
610
2/2
✓ Branch 0 taken 9706 times.
✓ Branch 1 taken 24 times.
9730 ctx->pe.min = FFMIN(pe, forgetful_min_pe);
611
612 /* NOTE: allocate a minimum of 1/8th average frame bits, to avoid
613 * reservoir starvation from producing zero-bit frames
614 */
615
4/6
✓ Branch 0 taken 9730 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 9723 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
9730 return FFMIN(
616 ctx->frame_bits * bit_factor,
617 FFMAX(ctx->frame_bits + size - bits, ctx->frame_bits / 8));
618 }
619
620 1883070 static float calc_pe_3gpp(AacPsyBand *band)
621 {
622 float pe, a;
623
624 1883070 band->pe = 0.0f;
625 1883070 band->pe_const = 0.0f;
626 1883070 band->active_lines = 0.0f;
627
2/2
✓ Branch 0 taken 1795122 times.
✓ Branch 1 taken 87948 times.
1883070 if (band->energy > band->thr) {
628 1795122 a = log2f(band->energy);
629 1795122 pe = a - log2f(band->thr);
630 1795122 band->active_lines = band->nz_lines;
631
2/2
✓ Branch 0 taken 799420 times.
✓ Branch 1 taken 995702 times.
1795122 if (pe < PSY_3GPP_C1) {
632 799420 pe = pe * PSY_3GPP_C3 + PSY_3GPP_C2;
633 799420 a = a * PSY_3GPP_C3 + PSY_3GPP_C2;
634 799420 band->active_lines *= PSY_3GPP_C3;
635 }
636 1795122 band->pe = pe * band->nz_lines;
637 1795122 band->pe_const = a * band->nz_lines;
638 }
639
640 1883070 return band->pe;
641 }
642
643 28706 static float calc_reduction_3gpp(float a, float desired_pe, float pe,
644 float active_lines)
645 {
646 float thr_avg, reduction;
647
648
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28704 times.
28706 if(active_lines == 0.0)
649 2 return 0;
650
651 28704 thr_avg = exp2f((a - pe) / (4.0f * active_lines));
652 28704 reduction = exp2f((a - desired_pe) / (4.0f * active_lines)) - thr_avg;
653
654
2/2
✓ Branch 0 taken 23449 times.
✓ Branch 1 taken 5255 times.
28704 return FFMAX(reduction, 0.0f);
655 }
656
657 1292669 static float calc_reduced_thr_3gpp(AacPsyBand *band, float min_snr,
658 float reduction)
659 {
660 1292669 float thr = band->thr;
661
662
2/2
✓ Branch 0 taken 1230984 times.
✓ Branch 1 taken 61685 times.
1292669 if (band->energy > thr) {
663 1230984 thr = sqrtf(thr);
664 1230984 thr = sqrtf(thr) + reduction;
665 1230984 thr *= thr;
666 1230984 thr *= thr;
667
668 /* This deviates from the 3GPP spec to match the reference encoder.
669 * It performs min(thr_reduced, max(thr, energy/min_snr)) only for bands
670 * that have hole avoidance on (active or inactive). It always reduces the
671 * threshold of bands with hole avoidance off.
672 */
673
4/4
✓ Branch 0 taken 528319 times.
✓ Branch 1 taken 702665 times.
✓ Branch 2 taken 528249 times.
✓ Branch 3 taken 70 times.
1230984 if (thr > band->energy * min_snr && band->avoid_holes != PSY_3GPP_AH_NONE) {
674
2/2
✓ Branch 0 taken 5261 times.
✓ Branch 1 taken 522988 times.
528249 thr = FFMAX(band->thr, band->energy * min_snr);
675 528249 band->avoid_holes = PSY_3GPP_AH_ACTIVE;
676 }
677 }
678
679 1292669 return thr;
680 }
681
682 11374 static void calc_thr_3gpp(const FFPsyWindowInfo *wi, const int num_bands, AacPsyChannel *pch,
683 const uint8_t *band_sizes, const float *coefs, const int cutoff)
684 {
685 int i, w, g;
686 11374 int start = 0, wstart = 0;
687
2/2
✓ Branch 0 taken 15049 times.
✓ Branch 1 taken 11374 times.
26423 for (w = 0; w < wi->num_windows*16; w += 16) {
688 15049 wstart = 0;
689
2/2
✓ Branch 0 taken 590401 times.
✓ Branch 1 taken 15049 times.
605450 for (g = 0; g < num_bands; g++) {
690 590401 AacPsyBand *band = &pch->band[w+g];
691
692 590401 float form_factor = 0.0f;
693 float Temp;
694 590401 band->energy = 0.0f;
695
2/2
✓ Branch 0 taken 567609 times.
✓ Branch 1 taken 22792 times.
590401 if (wstart < cutoff) {
696
2/2
✓ Branch 0 taken 10777536 times.
✓ Branch 1 taken 567609 times.
11345145 for (i = 0; i < band_sizes[g]; i++) {
697 10777536 band->energy += coefs[start+i] * coefs[start+i];
698 10777536 form_factor += sqrtf(fabs(coefs[start+i]));
699 }
700 }
701
2/2
✓ Branch 0 taken 566577 times.
✓ Branch 1 taken 23824 times.
590401 Temp = band->energy > 0 ? sqrtf((float)band_sizes[g] / band->energy) : 0;
702 590401 band->thr = band->energy * 0.001258925f;
703 590401 band->nz_lines = form_factor * sqrtf(Temp);
704
705 590401 start += band_sizes[g];
706 590401 wstart += band_sizes[g];
707 }
708 }
709 11374 }
710
711 8738 static void psy_hp_filter(const float *firbuf, float *hpfsmpl, const float *psy_fir_coeffs)
712 {
713 int i, j;
714
2/2
✓ Branch 0 taken 8947712 times.
✓ Branch 1 taken 8738 times.
8956450 for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
715 float sum1, sum2;
716 8947712 sum1 = firbuf[i + (PSY_LAME_FIR_LEN - 1) / 2];
717 8947712 sum2 = 0.0;
718
2/2
✓ Branch 0 taken 44738560 times.
✓ Branch 1 taken 8947712 times.
53686272 for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
719 44738560 sum1 += psy_fir_coeffs[j] * (firbuf[i + j] + firbuf[i + PSY_LAME_FIR_LEN - j]);
720 44738560 sum2 += psy_fir_coeffs[j + 1] * (firbuf[i + j + 1] + firbuf[i + PSY_LAME_FIR_LEN - j - 1]);
721 }
722 /* NOTE: The LAME psymodel expects it's input in the range -32768 to 32768.
723 * Tuning this for normalized floats would be difficult. */
724 8947712 hpfsmpl[i] = (sum1 + sum2) * 32768.0f;
725 }
726 8738 }
727
728 /**
729 * Calculate band thresholds as suggested in 3GPP TS26.403
730 */
731 11374 static void psy_3gpp_analyze_channel(FFPsyContext *ctx, int channel,
732 const float *coefs, const FFPsyWindowInfo *wi)
733 {
734 11374 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
735 11374 AacPsyChannel *pch = &pctx->ch[channel];
736 int i, w, g;
737 11374 float desired_bits, desired_pe, delta_pe, reduction= NAN, spread_en[128] = {0};
738 11374 float a = 0.0f, active_lines = 0.0f, norm_fac = 0.0f;
739
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11374 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11374 float pe = pctx->chan_bitrate > 32000 ? 0.0f : FFMAX(50.0f, 100.0f - pctx->chan_bitrate * 100.0f / 32000.0f);
740
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 10849 times.
11374 const int num_bands = ctx->num_bands[wi->num_windows == 8];
741
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 10849 times.
11374 const uint8_t *band_sizes = ctx->bands[wi->num_windows == 8];
742 11374 uint8_t s2l[16] = {0};
743
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 10849 times.
11899 int start_after_long = wi->num_windows == 8 &&
744
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 91 times.
525 wi->window_type[1] == LONG_START_SEQUENCE;
745 { /* short->long grid band map for cross-transition pre-echo control */
746
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 10940 times.
11374 if (start_after_long) {
747 434 const uint8_t *ls = ctx->bands[0];
748 434 const int ln = ctx->num_bands[0];
749 434 const uint8_t *ss = ctx->bands[1];
750 434 int lacc = 0, sacc = 0, gl = 0;
751
3/4
✓ Branch 0 taken 6076 times.
✓ Branch 1 taken 434 times.
✓ Branch 2 taken 6076 times.
✗ Branch 3 not taken.
6510 for (int gs = 0; gs < num_bands && gs < 16; gs++) {
752 6076 int center8 = (sacc + ss[gs] / 2) * 8;
753
4/4
✓ Branch 0 taken 26474 times.
✓ Branch 1 taken 434 times.
✓ Branch 2 taken 20832 times.
✓ Branch 3 taken 5642 times.
26908 while (gl < ln - 1 && lacc + ls[gl] <= center8) { lacc += ls[gl]; gl++; }
754 6076 s2l[gs] = gl;
755 6076 sacc += ss[gs];
756 }
757 }
758 }
759 11374 AacPsyCoeffs *coeffs = pctx->psy_coef[wi->num_windows == 8];
760
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 10849 times.
11374 const float avoid_hole_thr = wi->num_windows == 8 ? PSY_3GPP_AH_THR_SHORT : PSY_3GPP_AH_THR_LONG;
761
1/8
✓ Branch 0 taken 11374 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
11374 const int bandwidth = ctx->cutoff ? ctx->cutoff : AAC_CUTOFF(ctx->avctx);
762 11374 const int cutoff = bandwidth * 2048 / wi->num_windows / ctx->avctx->sample_rate;
763
764 //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
765 11374 calc_thr_3gpp(wi, num_bands, pch, band_sizes, coefs, cutoff);
766
767 //modify thresholds and energies - spread, threshold in quiet, pre-echo control
768
2/2
✓ Branch 0 taken 15049 times.
✓ Branch 1 taken 11374 times.
26423 for (w = 0; w < wi->num_windows*16; w += 16) {
769 15049 AacPsyBand *bands = &pch->band[w];
770
771 /* 5.4.2.3 "Spreading" & 5.4.3 "Spread Energy Calculation" */
772 15049 spread_en[0] = bands[0].energy;
773
2/2
✓ Branch 0 taken 575352 times.
✓ Branch 1 taken 15049 times.
590401 for (g = 1; g < num_bands; g++) {
774
2/2
✓ Branch 0 taken 550955 times.
✓ Branch 1 taken 24397 times.
575352 bands[g].thr = FFMAX(bands[g].thr, bands[g-1].thr * coeffs[g].spread_hi[0]);
775
2/2
✓ Branch 0 taken 551445 times.
✓ Branch 1 taken 23907 times.
575352 spread_en[w+g] = FFMAX(bands[g].energy, spread_en[w+g-1] * coeffs[g].spread_hi[1]);
776 }
777
2/2
✓ Branch 0 taken 575352 times.
✓ Branch 1 taken 15049 times.
590401 for (g = num_bands - 2; g >= 0; g--) {
778
2/2
✓ Branch 0 taken 559701 times.
✓ Branch 1 taken 15651 times.
575352 bands[g].thr = FFMAX(bands[g].thr, bands[g+1].thr * coeffs[g].spread_low[0]);
779
2/2
✓ Branch 0 taken 553217 times.
✓ Branch 1 taken 22135 times.
575352 spread_en[w+g] = FFMAX(spread_en[w+g], spread_en[w+g+1] * coeffs[g].spread_low[1]);
780 }
781 //5.4.2.4 "Threshold in quiet"
782
2/2
✓ Branch 0 taken 590401 times.
✓ Branch 1 taken 15049 times.
605450 for (g = 0; g < num_bands; g++) {
783 590401 AacPsyBand *band = &bands[g];
784
785
2/2
✓ Branch 0 taken 442499 times.
✓ Branch 1 taken 147902 times.
590401 band->thr_quiet = band->thr = FFMAX(band->thr, coeffs[g].ath);
786 //5.4.2.5 "Pre-echo control"
787
6/6
✓ Branch 0 taken 568057 times.
✓ Branch 1 taken 22344 times.
✓ Branch 2 taken 516607 times.
✓ Branch 3 taken 51450 times.
✓ Branch 4 taken 510531 times.
✓ Branch 5 taken 6076 times.
590401 if (!(wi->window_type[0] == LONG_STOP_SEQUENCE || (!w && wi->window_type[1] == LONG_START_SEQUENCE)))
788
6/6
✓ Branch 0 taken 95526 times.
✓ Branch 1 taken 466455 times.
✓ Branch 2 taken 25608 times.
✓ Branch 3 taken 536373 times.
✓ Branch 4 taken 69918 times.
✓ Branch 5 taken 466455 times.
561981 band->thr = FFMAX(PSY_3GPP_RPEMIN*band->thr, FFMIN(band->thr,
789 PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
790
3/4
✓ Branch 0 taken 28420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6076 times.
✓ Branch 3 taken 22344 times.
28420 else if (!w && start_after_long)
791 /* w0 after a START frame: grid-mapped, scaled continuity
792 * clamp instead of the spec's skip (cannot bind on noise
793 * content - see memory - but correct for tonal) */
794
6/6
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 5570 times.
✓ Branch 2 taken 358 times.
✓ Branch 3 taken 5718 times.
✓ Branch 4 taken 148 times.
✓ Branch 5 taken 5570 times.
6076 band->thr = FFMAX(PSY_3GPP_RPEMIN*band->thr, FFMIN(band->thr,
795 PSY_3GPP_RPELEV*pch->prev_band[s2l[FFMIN(g,15)]].thr / 8.0f));
796
797 /* 5.6.1.3.1 "Preparatory steps of the perceptual entropy calculation" */
798 590401 pe += calc_pe_3gpp(band);
799 590401 a += band->pe_const;
800 590401 active_lines += band->active_lines;
801
802 /* 5.6.1.3.3 "Selection of the bands for avoidance of holes" */
803
3/4
✓ Branch 0 taken 589037 times.
✓ Branch 1 taken 1364 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 589037 times.
590401 if (spread_en[w+g] * avoid_hole_thr > band->energy || coeffs[g].min_snr > 1.0f)
804 1364 band->avoid_holes = PSY_3GPP_AH_NONE;
805 else
806 589037 band->avoid_holes = PSY_3GPP_AH_INACTIVE;
807 }
808 }
809
810 /* 5.6.1.3.2 "Calculation of the desired perceptual entropy" */
811 11374 ctx->ch[channel].entropy = pe;
812
2/2
✓ Branch 0 taken 1644 times.
✓ Branch 1 taken 9730 times.
11374 if (ctx->unbounded_pe) {
813 /* quality-target coder: run the PE reduction at a FIXED reference
814 * quality so thresholds keep their perceptual shaping but stay
815 * independent of the user's -q:a (the coder's own noise-to-mask
816 * target is the sole quality authority) */
817 1644 desired_pe = pe * 120.0f / (2 * 2.5f * 120.0f);
818
1/2
✓ Branch 0 taken 1644 times.
✗ Branch 1 not taken.
1644 desired_bits = FFMIN(2560, PSY_3GPP_PE_TO_BITS(desired_pe));
819 1644 desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits);
820
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1640 times.
1644 pctx->pe.max = FFMAX(pe, pctx->pe.max);
821
1/2
✓ Branch 0 taken 1644 times.
✗ Branch 1 not taken.
1644 pctx->pe.min = FFMIN(pe, pctx->pe.min);
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9730 times.
9730 } else if (ctx->avctx->flags & AV_CODEC_FLAG_QSCALE) {
823 /* (2.5 * 120) achieves almost transparent rate, and we want to give
824 * ample room downwards, so we make that equivalent to QSCALE=2.4
825 */
826 ✗ desired_pe = pe * (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120) / (2 * 2.5f * 120.0f);
827 ✗ desired_bits = FFMIN(2560, PSY_3GPP_PE_TO_BITS(desired_pe));
828 ✗ desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits); // reflect clipping
829
830 /* PE slope smoothing */
831 ✗ if (ctx->bitres.bits > 0) {
832 ✗ desired_bits = FFMIN(2560, PSY_3GPP_PE_TO_BITS(desired_pe));
833 ✗ desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits); // reflect clipping
834 }
835
836 ✗ pctx->pe.max = FFMAX(pe, pctx->pe.max);
837 ✗ pctx->pe.min = FFMIN(pe, pctx->pe.min);
838 } else {
839 9730 desired_bits = calc_bit_demand(pctx, pe, ctx->bitres.bits, ctx->bitres.size, wi->num_windows == 8);
840 9730 desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits);
841
842 /* NOTE: PE correction is kept simple. During initial testing it had very
843 * little effect on the final bitrate. Probably a good idea to come
844 * back and do more testing later.
845 */
846
2/2
✓ Branch 0 taken 9557 times.
✓ Branch 1 taken 173 times.
9730 if (ctx->bitres.bits > 0) {
847 /* symmetric in the log domain: a negative previous demand (see
848 * PSY_3GPP_DEMAND_SCALE) sits on the lower bound, and the
849 * asymmetric 0.85 there costs ~2% Zimtohrli at 64 kbps */
850 9557 desired_pe *= av_clipf(pctx->pe.previous / PSY_3GPP_BITS_TO_PE(ctx->bitres.bits),
851 1.0f / 1.15f, 1.15f);
852 }
853 }
854 11374 pctx->pe.previous = PSY_3GPP_BITS_TO_PE(desired_bits);
855 11374 ctx->bitres.alloc = desired_bits;
856
857
2/2
✓ Branch 0 taken 11350 times.
✓ Branch 1 taken 24 times.
11374 if (desired_pe < pe) {
858 /* 5.6.1.3.4 "First Estimation of the reduction value" */
859
2/2
✓ Branch 0 taken 15025 times.
✓ Branch 1 taken 11350 times.
26375 for (w = 0; w < wi->num_windows*16; w += 16) {
860 15025 reduction = calc_reduction_3gpp(a, desired_pe, pe, active_lines);
861 15025 pe = 0.0f;
862 15025 a = 0.0f;
863 15025 active_lines = 0.0f;
864
2/2
✓ Branch 0 taken 589225 times.
✓ Branch 1 taken 15025 times.
604250 for (g = 0; g < num_bands; g++) {
865 589225 AacPsyBand *band = &pch->band[w+g];
866
867 589225 band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
868 /* recalculate PE */
869 589225 pe += calc_pe_3gpp(band);
870 589225 a += band->pe_const;
871 589225 active_lines += band->active_lines;
872 }
873 }
874
875 /* 5.6.1.3.5 "Second Estimation of the reduction value" */
876
2/2
✓ Branch 0 taken 13681 times.
✓ Branch 1 taken 2331 times.
16012 for (i = 0; i < 2; i++) {
877 13681 float pe_no_ah = 0.0f, desired_pe_no_ah;
878 13681 active_lines = a = 0.0f;
879
2/2
✓ Branch 0 taken 17356 times.
✓ Branch 1 taken 13681 times.
31037 for (w = 0; w < wi->num_windows*16; w += 16) {
880
2/2
✓ Branch 0 taken 703444 times.
✓ Branch 1 taken 17356 times.
720800 for (g = 0; g < num_bands; g++) {
881 703444 AacPsyBand *band = &pch->band[w+g];
882
883
2/2
✓ Branch 0 taken 500064 times.
✓ Branch 1 taken 203380 times.
703444 if (band->avoid_holes != PSY_3GPP_AH_ACTIVE) {
884 500064 pe_no_ah += band->pe;
885 500064 a += band->pe_const;
886 500064 active_lines += band->active_lines;
887 }
888 }
889 }
890
2/2
✓ Branch 0 taken 7441 times.
✓ Branch 1 taken 6240 times.
13681 desired_pe_no_ah = FFMAX(desired_pe - (pe - pe_no_ah), 0.0f);
891
1/2
✓ Branch 0 taken 13681 times.
✗ Branch 1 not taken.
13681 if (active_lines > 0.0f)
892 13681 reduction = calc_reduction_3gpp(a, desired_pe_no_ah, pe_no_ah, active_lines);
893
894 13681 pe = 0.0f;
895
2/2
✓ Branch 0 taken 17356 times.
✓ Branch 1 taken 13681 times.
31037 for (w = 0; w < wi->num_windows*16; w += 16) {
896
2/2
✓ Branch 0 taken 703444 times.
✓ Branch 1 taken 17356 times.
720800 for (g = 0; g < num_bands; g++) {
897 703444 AacPsyBand *band = &pch->band[w+g];
898
899
1/2
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
703444 if (active_lines > 0.0f)
900 703444 band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
901 703444 pe += calc_pe_3gpp(band);
902
1/2
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
703444 if (band->thr > 0.0f)
903 703444 band->norm_fac = band->active_lines / band->thr;
904 else
905 ✗ band->norm_fac = 0.0f;
906 703444 norm_fac += band->norm_fac;
907 }
908 }
909 13681 delta_pe = desired_pe - pe;
910
2/2
✓ Branch 0 taken 9019 times.
✓ Branch 1 taken 4662 times.
13681 if (fabs(delta_pe) > 0.05f * desired_pe)
911 9019 break;
912 }
913
914
2/2
✓ Branch 0 taken 3756 times.
✓ Branch 1 taken 7594 times.
11350 if (pe < 1.15f * desired_pe) {
915 /* 6.6.1.3.6 "Final threshold modification by linearization" */
916
1/2
✓ Branch 0 taken 3756 times.
✗ Branch 1 not taken.
3756 norm_fac = norm_fac ? 1.0f / norm_fac : 0;
917
2/2
✓ Branch 0 taken 3756 times.
✓ Branch 1 taken 3756 times.
7512 for (w = 0; w < wi->num_windows*16; w += 16) {
918
2/2
✓ Branch 0 taken 184044 times.
✓ Branch 1 taken 3756 times.
187800 for (g = 0; g < num_bands; g++) {
919 184044 AacPsyBand *band = &pch->band[w+g];
920
921
2/2
✓ Branch 0 taken 167980 times.
✓ Branch 1 taken 16064 times.
184044 if (band->active_lines > 0.5f) {
922 167980 float delta_sfb_pe = band->norm_fac * norm_fac * delta_pe;
923 167980 float thr = band->thr;
924
925 167980 thr *= exp2f(delta_sfb_pe / band->active_lines);
926
4/4
✓ Branch 0 taken 10250 times.
✓ Branch 1 taken 157730 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 10124 times.
167980 if (thr > coeffs[g].min_snr * band->energy && band->avoid_holes == PSY_3GPP_AH_INACTIVE)
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 thr = FFMAX(band->thr, coeffs[g].min_snr * band->energy);
928 167980 band->thr = thr;
929 }
930 }
931 }
932 } else {
933 /* 5.6.1.3.7 "Further perceptual entropy reduction" */
934 7594 g = num_bands;
935
4/4
✓ Branch 0 taken 360932 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 353362 times.
✓ Branch 3 taken 7570 times.
360956 while (pe > desired_pe && g--) {
936
2/2
✓ Branch 0 taken 404812 times.
✓ Branch 1 taken 353362 times.
758174 for (w = 0; w < wi->num_windows*16; w+= 16) {
937 404812 AacPsyBand *band = &pch->band[w+g];
938
4/4
✓ Branch 0 taken 403469 times.
✓ Branch 1 taken 1343 times.
✓ Branch 2 taken 868 times.
✓ Branch 3 taken 402601 times.
404812 if (band->avoid_holes != PSY_3GPP_AH_NONE && coeffs[g].min_snr < PSY_SNR_1DB) {
939 868 coeffs[g].min_snr = PSY_SNR_1DB;
940 868 band->thr = band->energy * PSY_SNR_1DB;
941 868 pe += band->active_lines * 1.5f - band->pe;
942 }
943 }
944 }
945 /* TODO: allow more holes (unused without mid/side) */
946 }
947 }
948
949 /* Signal-relative mask ceiling. 5.6.1.3.3 exempts bands that are quiet
950 * relative to the spread energy from the min-SNR floor ("holes allowed
951 * here"), so on spectrally lopsided programme their mask is free to rise
952 * until it meets their own energy - the model then calls a band that
953 * carries real texture inaudible, and every consumer of the mask agrees:
954 * the allocator buys it nothing and the quality-target solver sees a mask
955 * it cannot fail. A band we still choose to code must never be allowed
956 * noise within PSY_THRFL_* dB of its own energy, whatever the hole logic said. */
957 {
958
3/4
✓ Branch 0 taken 9730 times.
✓ Branch 1 taken 1644 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9730 times.
11374 int qmode = ctx->unbounded_pe || (ctx->avctx->flags & AV_CODEC_FLAG_QSCALE);
959
2/2
✓ Branch 0 taken 1644 times.
✓ Branch 1 taken 9730 times.
11374 float lim = qmode ? PSY_THRFL_QUALITY : PSY_THRFL_CBR;
960
2/2
✓ Branch 0 taken 1644 times.
✓ Branch 1 taken 9730 times.
11374 float knee = qmode ? 0.0f : PSY_THRFL_CBR_KNEE;
961 11374 float lo = ff_exp10f(-lim / 10.0f);
962 22748 float l2f = ctx->avctx->sample_rate / 2.0f /
963
2/2
✓ Branch 0 taken 10849 times.
✓ Branch 1 taken 525 times.
11374 (wi->num_windows == 1 ? 1024.0f : 128.0f);
964
2/2
✓ Branch 0 taken 15049 times.
✓ Branch 1 taken 11374 times.
26423 for (w = 0; w < wi->num_windows*16; w += 16) {
965 15049 int start = 0;
966
2/2
✓ Branch 0 taken 590401 times.
✓ Branch 1 taken 15049 times.
605450 for (g = 0; g < num_bands; g++) {
967 590401 AacPsyBand *band = &pch->band[w+g];
968
2/2
✓ Branch 0 taken 272260 times.
✓ Branch 1 taken 318141 times.
590401 if (start * l2f >= knee)
969
2/2
✓ Branch 0 taken 213323 times.
✓ Branch 1 taken 58937 times.
272260 band->thr = FFMIN(band->thr, band->energy * lo);
970 590401 start += band_sizes[g];
971 }
972 }
973 }
974
975
2/2
✓ Branch 0 taken 15049 times.
✓ Branch 1 taken 11374 times.
26423 for (w = 0; w < wi->num_windows*16; w += 16) {
976
2/2
✓ Branch 0 taken 590401 times.
✓ Branch 1 taken 15049 times.
605450 for (g = 0; g < num_bands; g++) {
977 590401 AacPsyBand *band = &pch->band[w+g];
978 590401 FFPsyBand *psy_band = &ctx->ch[channel].psy_bands[w+g];
979
980 590401 psy_band->threshold = band->thr;
981 590401 psy_band->energy = band->energy;
982 590401 psy_band->spread = band->active_lines * 2.0f / band_sizes[g];
983 590401 psy_band->bits = PSY_3GPP_PE_TO_BITS(band->pe);
984 }
985 }
986
987 11374 memcpy(pch->prev_band, pch->band, sizeof(pch->band));
988 11374 }
989
990 6122 static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
991 const float **coeffs, const FFPsyWindowInfo *wi)
992 {
993 int ch;
994 6122 FFPsyChannelGroup *group = ff_psy_find_group(ctx, channel);
995 6122 AacPsyContext *pctx = ctx->model_priv_data;
996
997 /* The encoder's rate-control loop may re-run the analysis for the same
998 * frame; carried state (bit reservoir, PE history, previous-frame
999 * thresholds) must advance exactly once per frame, so save it on the
1000 * frame's first run and rewind on re-runs. */
1001
2/2
✓ Branch 0 taken 4332 times.
✓ Branch 1 taken 1790 times.
6122 if (ctx->avctx->frame_num != pctx->rc_frame_num) {
1002 4332 pctx->rc_frame_num = ctx->avctx->frame_num;
1003 4332 pctx->rc_first_ch = channel;
1004 4332 pctx->rc_fill_level = pctx->fill_level;
1005 4332 pctx->rc_pe_min = pctx->pe.min;
1006 4332 pctx->rc_pe_max = pctx->pe.max;
1007 4332 pctx->rc_pe_previous = pctx->pe.previous;
1008
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 717 times.
1790 } else if (channel == pctx->rc_first_ch) {
1009 1073 pctx->fill_level = pctx->rc_fill_level;
1010 1073 pctx->pe.min = pctx->rc_pe_min;
1011 1073 pctx->pe.max = pctx->rc_pe_max;
1012 1073 pctx->pe.previous = pctx->rc_pe_previous;
1013 }
1014
1015
2/2
✓ Branch 0 taken 11374 times.
✓ Branch 1 taken 6122 times.
17496 for (ch = 0; ch < group->num_ch; ch++) {
1016 11374 AacPsyChannel *pch = &pctx->ch[channel + ch];
1017
2/2
✓ Branch 0 taken 9148 times.
✓ Branch 1 taken 2226 times.
11374 if (ctx->avctx->frame_num != pch->rc_frame_num) {
1018 9148 pch->rc_frame_num = ctx->avctx->frame_num;
1019 9148 memcpy(pch->rc_prev_band, pch->prev_band, sizeof(pch->prev_band));
1020 } else {
1021 2226 memcpy(pch->prev_band, pch->rc_prev_band, sizeof(pch->prev_band));
1022 }
1023 11374 psy_3gpp_analyze_channel(ctx, channel + ch, coeffs[ch], &wi[ch]);
1024 }
1025 6122 }
1026
1027 27 static av_cold void psy_3gpp_end(FFPsyContext *apc)
1028 {
1029 27 AacPsyContext *pctx = (AacPsyContext*) apc->model_priv_data;
1030
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (pctx)
1031 27 av_freep(&pctx->ch);
1032 27 av_freep(&apc->model_priv_data);
1033 27 }
1034
1035 9010 static void lame_apply_block_type(AacPsyChannel *ctx, FFPsyWindowInfo *wi, int uselongblock)
1036 {
1037 9010 int blocktype = ONLY_LONG_SEQUENCE;
1038
2/2
✓ Branch 0 taken 8501 times.
✓ Branch 1 taken 509 times.
9010 if (uselongblock) {
1039
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 8071 times.
8501 if (ctx->next_window_seq == EIGHT_SHORT_SEQUENCE)
1040 430 blocktype = LONG_STOP_SEQUENCE;
1041 } else {
1042 509 blocktype = EIGHT_SHORT_SEQUENCE;
1043
2/2
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 85 times.
509 if (ctx->next_window_seq == ONLY_LONG_SEQUENCE)
1044 424 ctx->next_window_seq = LONG_START_SEQUENCE;
1045
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 503 times.
509 if (ctx->next_window_seq == LONG_STOP_SEQUENCE)
1046 6 ctx->next_window_seq = EIGHT_SHORT_SEQUENCE;
1047 }
1048
1049 9010 wi->window_type[0] = ctx->next_window_seq;
1050 9010 ctx->next_window_seq = blocktype;
1051 9010 }
1052
1053 /* Attack detection half of the LAME window decision: everything up to (and
1054 * excluding) the block-type state machine. Fills attacks[] and returns the raw
1055 * uselongblock; mutates only the detection history. Split out so a channel
1056 * pair can be detected first and DECIDED together (synced block switching). */
1057 9010 static int psy_lame_detect(AacPsyContext *pctx, AacPsyChannel *pch,
1058 const float *la, int channel, int prev_type,
1059 int attacks[AAC_NUM_BLOCKS_SHORT + 1])
1060 {
1061 9010 int uselongblock = 1;
1062 int i;
1063
1064
2/2
✓ Branch 0 taken 8738 times.
✓ Branch 1 taken 272 times.
9010 if (la) {
1065 float hpfsmpl[AAC_BLOCK_SIZE_LONG];
1066 8738 const float *pf = hpfsmpl;
1067 float attack_intensity[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
1068 float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
1069 8738 float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
1070 8738 const float *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN);
1071 8738 int att_sum = 0;
1072
1073 /* LAME comment: apply high pass filter of fs/4 */
1074 8738 psy_hp_filter(firbuf, hpfsmpl, psy_fir_coeffs);
1075
1076 /* Calculate the energies of each sub-shortblock */
1077
2/2
✓ Branch 0 taken 17476 times.
✓ Branch 1 taken 8738 times.
26214 for (i = 0; i < PSY_LAME_NUM_SUBBLOCKS; i++) {
1078 17476 energy_subshort[i] = pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS)];
1079 assert(pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS - 2)] > 0);
1080 17476 attack_intensity[i] = energy_subshort[i] / pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS - 2)];
1081 17476 energy_short[0] += energy_subshort[i];
1082 }
1083
1084
2/2
✓ Branch 0 taken 139808 times.
✓ Branch 1 taken 8738 times.
148546 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
1085 139808 const float *const pfe = pf + AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
1086 139808 float p = 1.0f;
1087
2/2
✓ Branch 0 taken 8947712 times.
✓ Branch 1 taken 139808 times.
9087520 for (; pf < pfe; pf++)
1088
2/2
✓ Branch 0 taken 8167034 times.
✓ Branch 1 taken 780678 times.
8947712 p = FFMAX(p, fabsf(*pf));
1089 139808 pch->prev_energy_subshort[i] = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS] = p;
1090 139808 energy_short[1 + i / PSY_LAME_NUM_SUBBLOCKS] += p;
1091
1092 /* NOTE: The indexes below are [i + 3 - 2] in the LAME source. Compare each sub-block to sub-block - 2 */
1093
2/2
✓ Branch 0 taken 69736 times.
✓ Branch 1 taken 70072 times.
139808 if (p > energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS - 2])
1094 69736 p = p / energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS - 2];
1095
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 70008 times.
70072 else if (energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS - 2] > p * 10.0f)
1096 64 p = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS - 2] / (p * 10.0f);
1097 else
1098 70008 p = 0.0;
1099
1100 139808 attack_intensity[i + PSY_LAME_NUM_SUBBLOCKS] = p;
1101 }
1102
2/2
✓ Branch 0 taken 69904 times.
✓ Branch 1 taken 8738 times.
78642 for (i = 0; i < AAC_NUM_BLOCKS_SHORT; i++)
1103 69904 pch->next_win_level[i] = energy_short[1 + i];
1104
1105 { /* pre-echo-aware threshold relaxation + periodicity/novelty check
1106 * (a pulse train repeats its peak; a real onset towers) */
1107 8738 float frame_peak = 1.0f;
1108 float env[PSY_LAME_HIST + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
1109 float denv[PSY_LAME_HIST + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
1110 8738 const float nov_gate = 1.25f;
1111 /* first-difference peak per sub-block: an attack that shorts can
1112 * help has HF novelty; a bass pluck under a long window does not */
1113 8738 memcpy(denv, pch->dif_env_hist, sizeof(pch->dif_env_hist));
1114 {
1115 8738 const int sub = AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
1116
2/2
✓ Branch 0 taken 139808 times.
✓ Branch 1 taken 8738 times.
148546 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
1117 139808 float p = 0.0f;
1118
2/2
✓ Branch 0 taken 8807904 times.
✓ Branch 1 taken 139808 times.
8947712 for (int j2 = i*sub + 1; j2 < (i+1)*sub; j2++)
1119
2/2
✓ Branch 0 taken 7942962 times.
✓ Branch 1 taken 864942 times.
8807904 p = FFMAX(p, fabsf(la[j2] - la[j2-1]));
1120
2/2
✓ Branch 0 taken 136590 times.
✓ Branch 1 taken 3218 times.
139808 denv[PSY_LAME_HIST + i] = FFMAX(p * 32768.0f, 1.0f);
1121 }
1122 }
1123 8738 memcpy(pch->dif_env_hist, denv + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS,
1124 sizeof(pch->dif_env_hist));
1125 8738 memcpy(env,pch->hp_env_hist, sizeof(pch->hp_env_hist));
1126 8738 memcpy(env + PSY_LAME_HIST, energy_subshort + PSY_LAME_NUM_SUBBLOCKS,
1127 AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS * sizeof(*env));
1128
2/2
✓ Branch 0 taken 139808 times.
✓ Branch 1 taken 8738 times.
148546 for (i = PSY_LAME_NUM_SUBBLOCKS; i < (AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS; i++)
1129
2/2
✓ Branch 0 taken 106250 times.
✓ Branch 1 taken 33558 times.
139808 frame_peak = FFMAX(frame_peak, energy_subshort[i]);
1130
2/2
✓ Branch 0 taken 157284 times.
✓ Branch 1 taken 8738 times.
166022 for (i = 0; i < (AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS; i++)
1131
2/2
✓ Branch 0 taken 157019 times.
✓ Branch 1 taken 265 times.
157284 if (!attacks[i / PSY_LAME_NUM_SUBBLOCKS]) {
1132 157019 float thr = pch->attack_threshold;
1133
2/2
✓ Branch 0 taken 139576 times.
✓ Branch 1 taken 17443 times.
157019 if (i >= PSY_LAME_NUM_SUBBLOCKS &&
1134
2/2
✓ Branch 0 taken 70959 times.
✓ Branch 1 taken 68617 times.
139576 pch->frames_since_short >= PSY_LAME_PE_GAP &&
1135
2/2
✓ Branch 0 taken 6290 times.
✓ Branch 1 taken 64669 times.
70959 energy_subshort[i - PSY_LAME_NUM_SUBBLOCKS] < PSY_LAME_PE_QUIET * frame_peak)
1136 6290 thr *= PSY_LAME_PE_RED;
1137
2/2
✓ Branch 0 taken 803 times.
✓ Branch 1 taken 156216 times.
157019 if (attack_intensity[i] > thr) {
1138 /* An attack must tower over the recent HP envelope:
1139 * within ~12ms always (pitch-rate trains), within
1140 * ~44ms only from steady long-window state (slow
1141 * pulse trains, where an isolated short excursion
1142 * is an audible click). */
1143
3/4
✓ Branch 0 taken 803 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 759 times.
✓ Branch 3 taken 44 times.
803 if (nov_gate > 0.0f && i >= PSY_LAME_NUM_SUBBLOCKS) {
1144 759 const int pos = PSY_LAME_HIST + i - PSY_LAME_NUM_SUBBLOCKS;
1145 759 float nearmax = 1.0f, deepmax = 1.0f;
1146
2/2
✓ Branch 0 taken 4554 times.
✓ Branch 1 taken 759 times.
5313 for (int k = 3; k <= 8; k++)
1147
2/2
✓ Branch 0 taken 2292 times.
✓ Branch 1 taken 2262 times.
4554 nearmax = FFMAX(nearmax, env[pos - k]);
1148
2/2
✓ Branch 0 taken 21252 times.
✓ Branch 1 taken 759 times.
22011 for (int k = 3; k <= PSY_LAME_NOV_BACK; k++)
1149
2/2
✓ Branch 0 taken 15429 times.
✓ Branch 1 taken 5823 times.
21252 deepmax = FFMAX(deepmax, env[pos - k]);
1150
2/2
✓ Branch 0 taken 462 times.
✓ Branch 1 taken 297 times.
759 if (energy_subshort[i] < nov_gate * nearmax ||
1151
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 389 times.
462 (energy_subshort[i] < nov_gate * deepmax &&
1152
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 7 times.
73 pch->frames_since_short >= PSY_LAME_PE_GAP))
1153 363 continue; /* periodic, not an onset */
1154 }
1155
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 44 times.
440 if (i >= PSY_LAME_NUM_SUBBLOCKS) {
1156 /* no HF novelty: pre-echo is masked by the
1157 * sustained LF itself, and the short excursion
1158 * (HF mute, then the stop frame's noisy HF
1159 * hand-back) is the audible event. Both bars
1160 * must agree: absolutely small AND relatively
1161 * unremarkable - a quiet transient rising out
1162 * of silence has a tiny derivative but maximal
1163 * novelty, and its pre-echo lands on silence */
1164 396 const int pos = PSY_LAME_HIST + i - PSY_LAME_NUM_SUBBLOCKS;
1165 396 float dmax = 1.0f, premin = 1e30f;
1166
2/2
✓ Branch 0 taken 11880 times.
✓ Branch 1 taken 396 times.
12276 for (int k = 1; k <= PSY_LAME_NOV_BACK; k++)
1167
2/2
✓ Branch 0 taken 7316 times.
✓ Branch 1 taken 4564 times.
11880 dmax = FFMAX(dmax, denv[pos - k]);
1168 /* pre-echo audibility: the veto is only safe
1169 * when the surroundings mask the smear - a deep
1170 * dip right before a LOUD attack (stop-gap
1171 * slams) means the long window's pre-echo lands
1172 * on quiet. Quiet candidates keep the veto:
1173 * their smear is at the noise floor, and shorts
1174 * would only fragment the passage */
1175
2/2
✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 396 times.
1980 for (int k = 1; k <= 4; k++)
1176
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 778 times.
1584 premin = FFMIN(premin, env[pos - k]);
1177
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 130 times.
396 if (denv[pos] < PSY_LAME_HFN_REL * dmax &&
1178
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 161 times.
266 denv[pos] < PSY_LAME_HFN_ABS &&
1179
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 13 times.
105 premin * PSY_LAME_HFN_PRE > energy_subshort[i])
1180 92 continue;
1181 }
1182 348 attacks[i / PSY_LAME_NUM_SUBBLOCKS] = (i % PSY_LAME_NUM_SUBBLOCKS) + 1;
1183 }
1184 }
1185 }
1186
1187 /* should have energy change between short blocks, in order to avoid periodic signals */
1188 /* Good samples to show the effect are Trumpet test songs */
1189 /* GB: tuned (1) to avoid too many short blocks for test sample TRUMPET */
1190 /* RH: tuned (2) to let enough short blocks through for test sample FSOL and SNAPS */
1191
2/2
✓ Branch 0 taken 69904 times.
✓ Branch 1 taken 8738 times.
78642 for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++) {
1192 69904 const float u = energy_short[i - 1];
1193 69904 const float v = energy_short[i];
1194
2/2
✓ Branch 0 taken 35310 times.
✓ Branch 1 taken 34594 times.
69904 const float m = FFMAX(u, v);
1195
2/2
✓ Branch 0 taken 67524 times.
✓ Branch 1 taken 2380 times.
69904 if (m < 40000) { /* (2) */
1196
4/4
✓ Branch 0 taken 67101 times.
✓ Branch 1 taken 423 times.
✓ Branch 2 taken 66334 times.
✓ Branch 3 taken 767 times.
67524 if (u < 2.3f * v && v < 2.3f * u) { /* (1) */
1197
4/4
✓ Branch 0 taken 8128 times.
✓ Branch 1 taken 58206 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 8122 times.
66334 if (i == 1 && attacks[0] < attacks[i])
1198 6 attacks[0] = 0;
1199 66334 attacks[i] = 0;
1200 }
1201 }
1202 69904 att_sum += attacks[i];
1203 }
1204
1205 { /* Gap-onset detection on the broadband envelope: a slam that ends
1206 * a quiet gap (stop-start riffing, kick after a break) can be
1207 * invisible to the HP path - no content above fs/4, or a rise too
1208 * gradual for the 2-sub-block ratio - yet pre-echo into the gap
1209 * is maximally audible (no forward masking there). Fire when the
1210 * candidate towers over a recent dip. Pulse trains cannot fire:
1211 * their inter-pulse floor never dips far enough below the pulse. */
1212 float renv[PSY_LAME_HIST + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
1213 /* scan both envelopes: broadband (kick+chug slams with LF
1214 * dominance) and HP (events whose gap only exists above the
1215 * sustained bass) */
1216 float henv[PSY_LAME_HIST + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
1217 8738 float *walls[2] = { &pch->gap_wall, &pch->gap_wall_hp };
1218 8738 const float *envs[2] = { renv, henv };
1219 8738 memcpy(renv, pch->raw_env_hist, sizeof(pch->raw_env_hist));
1220
2/2
✓ Branch 0 taken 139808 times.
✓ Branch 1 taken 8738 times.
148546 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
1221 139808 float p = 0.0f;
1222
2/2
✓ Branch 0 taken 8947712 times.
✓ Branch 1 taken 139808 times.
9087520 for (int j2 = 0; j2 < 64; j2++)
1223
2/2
✓ Branch 0 taken 7648434 times.
✓ Branch 1 taken 1299278 times.
8947712 p = FFMAX(p, fabsf(la[i*64 + j2]));
1224
2/2
✓ Branch 0 taken 136608 times.
✓ Branch 1 taken 3200 times.
139808 renv[PSY_LAME_HIST + i] = FFMAX(p * 32768.0f, 1.0f);
1225 }
1226 8738 memcpy(henv, pch->hp_env_hist, sizeof(pch->hp_env_hist));
1227 8738 memcpy(henv + PSY_LAME_HIST, energy_subshort + PSY_LAME_NUM_SUBBLOCKS,
1228 AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS * sizeof(*henv));
1229
2/2
✓ Branch 0 taken 17476 times.
✓ Branch 1 taken 8738 times.
26214 for (int e = 0; e < 2; e++) {
1230 17476 const float *ev = envs[e];
1231 17476 float wall = *walls[e];
1232
2/2
✓ Branch 0 taken 279616 times.
✓ Branch 1 taken 17476 times.
297092 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
1233 279616 const int b = (i + PSY_LAME_NUM_SUBBLOCKS) / PSY_LAME_NUM_SUBBLOCKS;
1234 279616 const int pos = PSY_LAME_HIST + i;
1235 279616 const float cand = ev[pos];
1236 279616 const float quiet = wall / PSY_LAME_GAP_DEPTH;
1237 279616 int run = 0, k0 = 0;
1238 /* the gap must end adjacent to the candidate (<= 5
1239 * rising sub-blocks) and hold >= 4 sub-blocks (~6ms).
1240 * A pulse train's inter-pulse floor never drops this
1241 * far below its own running peak, so it cannot fire. */
1242
2/2
✓ Branch 0 taken 1476413 times.
✓ Branch 1 taken 226697 times.
1703110 for (int k = 3; k <= 8; k++)
1243
2/2
✓ Branch 0 taken 52919 times.
✓ Branch 1 taken 1423494 times.
1476413 if (ev[pos - k] < quiet) {
1244 52919 k0 = k;
1245 52919 break;
1246 }
1247
2/2
✓ Branch 0 taken 52919 times.
✓ Branch 1 taken 226697 times.
279616 if (k0)
1248
4/4
✓ Branch 0 taken 418212 times.
✓ Branch 1 taken 10839 times.
✓ Branch 2 taken 376132 times.
✓ Branch 3 taken 42080 times.
429051 for (int k = k0; k <= PSY_LAME_GAP_BACK && ev[pos - k] < quiet; k++)
1249 376132 run++;
1250
4/4
✓ Branch 0 taken 139808 times.
✓ Branch 1 taken 139808 times.
✓ Branch 2 taken 124220 times.
✓ Branch 3 taken 15588 times.
279616 if (e == 1 && !(run >= 4)) {
1251 /* HP tower: an onset rising far above everything
1252 * in the look-back, even without a silent gap
1253 * (cymbal-less slams leave the bass sustaining) */
1254 124220 float dmax = 1.0f;
1255
2/2
✓ Branch 0 taken 2732840 times.
✓ Branch 1 taken 124220 times.
2857060 for (int k = 3; k <= PSY_LAME_GAP_BACK; k++)
1256
2/2
✓ Branch 0 taken 2194011 times.
✓ Branch 1 taken 538829 times.
2732840 dmax = FFMAX(dmax, ev[pos - k]);
1257
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 124016 times.
124220 if (cand > PSY_LAME_GAP_TOWER * dmax)
1258 204 run = 4; /* qualify via the same fire path */
1259 }
1260
8/8
✓ Branch 0 taken 277333 times.
✓ Branch 1 taken 2283 times.
✓ Branch 2 taken 19584 times.
✓ Branch 3 taken 257749 times.
✓ Branch 4 taken 1030 times.
✓ Branch 5 taken 18554 times.
✓ Branch 6 taken 489 times.
✓ Branch 7 taken 541 times.
279616 if (!attacks[b] && run >= 4 && cand > PSY_LAME_GAP_LEVEL * wall &&
1261 cand > PSY_LAME_GAP_FLOOR) {
1262 489 attacks[b] = (i + PSY_LAME_NUM_SUBBLOCKS) % PSY_LAME_NUM_SUBBLOCKS + 1;
1263 489 att_sum += attacks[b];
1264 }
1265
2/2
✓ Branch 0 taken 224014 times.
✓ Branch 1 taken 55602 times.
279616 wall = FFMAX(wall * 0.996f, cand);
1266 }
1267 17476 *walls[e] = wall;
1268 }
1269 8738 memcpy(pch->raw_env_hist, renv + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS,
1270 sizeof(pch->raw_env_hist));
1271 }
1272
1273 /* roll the HP sub-block peak history */
1274 8738 memmove(pch->hp_env_hist,
1275 8738 pch->hp_env_hist + AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS,
1276 (PSY_LAME_HIST - AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS) *
1277 sizeof(*pch->hp_env_hist));
1278 8738 memcpy(pch->hp_env_hist + PSY_LAME_HIST - AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS,
1279 energy_subshort + PSY_LAME_NUM_SUBBLOCKS,
1280 AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS * sizeof(*pch->hp_env_hist));
1281
1282
2/2
✓ Branch 0 taken 8546 times.
✓ Branch 1 taken 192 times.
8738 if (pch->next_attack0_zero)
1283 8546 attacks[0] = 0;
1284 8738 pch->next_attack0_zero = !attacks[AAC_NUM_BLOCKS_SHORT];
1285
1286
2/2
✓ Branch 0 taken 8704 times.
✓ Branch 1 taken 34 times.
8738 if (attacks[0] <= pch->prev_attack)
1287 8704 attacks[0] = 0;
1288
1289 8738 att_sum += attacks[0];
1290
1291 /* If the previous attack happened in the last sub-block of the previous sequence,
1292 * or if there's a new attack, use short window */
1293
4/4
✓ Branch 0 taken 8721 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 344 times.
✓ Branch 3 taken 8377 times.
8738 if (pch->prev_attack == PSY_LAME_NUM_SUBBLOCKS || att_sum) {
1294 361 uselongblock = 0;
1295
1296
2/2
✓ Branch 0 taken 2888 times.
✓ Branch 1 taken 361 times.
3249 for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++)
1297
4/4
✓ Branch 0 taken 735 times.
✓ Branch 1 taken 2153 times.
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 479 times.
2888 if (attacks[i] && attacks[i-1])
1298 256 attacks[i] = 0;
1299 }
1300
1301 } else {
1302 /* We have no lookahead info, so just use same type as the previous sequence. */
1303 272 uselongblock = !(prev_type == EIGHT_SHORT_SEQUENCE);
1304 }
1305 9010 return uselongblock;
1306 }
1307
1308 /* Decision half: the block-type state machine and window/grouping fill,
1309 * given the (possibly pair-synced) final uselongblock. */
1310 9010 static FFPsyWindowInfo psy_lame_apply(AacPsyContext *pctx, AacPsyChannel *pch,
1311 int uselongblock,
1312 const int attacks[AAC_NUM_BLOCKS_SHORT + 1],
1313 int prev_type, int have_la)
1314 {
1315 9010 int grouping = 0;
1316 int i;
1317 9010 FFPsyWindowInfo wi = { { 0 } };
1318
1319
2/2
✓ Branch 0 taken 8738 times.
✓ Branch 1 taken 272 times.
9010 if (have_la)
1320
2/2
✓ Branch 0 taken 8229 times.
✓ Branch 1 taken 509 times.
8738 pch->frames_since_short = uselongblock ? pch->frames_since_short + 1 : 0;
1321
1322 9010 lame_apply_block_type(pch, &wi, uselongblock);
1323
1324 9010 wi.window_type[1] = prev_type;
1325
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 515 times.
9010 if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
1326
1327 8495 wi.num_windows = 1;
1328 8495 wi.grouping[0] = 1;
1329
2/2
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 8071 times.
8495 if (wi.window_type[0] == LONG_START_SEQUENCE)
1330 424 wi.window_shape = 0;
1331 else
1332 8071 wi.window_shape = 1;
1333
1334 } else {
1335 515 int lastgrp = 0;
1336
1337 515 wi.num_windows = 8;
1338 515 wi.window_shape = 0;
1339
2/2
✓ Branch 0 taken 4120 times.
✓ Branch 1 taken 515 times.
4635 for (i = 0; i < 8; i++) {
1340
2/2
✓ Branch 0 taken 1220 times.
✓ Branch 1 taken 2900 times.
4120 if (!((pch->next_grouping >> i) & 1))
1341 1220 lastgrp = i;
1342 4120 wi.grouping[lastgrp]++;
1343 }
1344 }
1345
1346 /* Determine grouping, based on the location of the first attack, and save for
1347 * the next frame.
1348 * FIXME: Move this to analysis.
1349 * TODO: Tune groupings depending on attack location
1350 * TODO: Handle more than one attack in a group
1351 */
1352
2/2
✓ Branch 0 taken 78600 times.
✓ Branch 1 taken 8502 times.
87102 for (i = 0; i < 9; i++) {
1353
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 78092 times.
78600 if (attacks[i]) {
1354 508 grouping = i;
1355 508 break;
1356 }
1357 }
1358 9010 pch->next_grouping = window_grouping[grouping];
1359
1360 {
1361 /* energy-homogeneous grouping: fixed attack-position patterns force
1362 * disparate windows to share scalefactors; regroup on level jumps */
1363 9010 uint8_t bits = 0;
1364 9010 int glen = 1;
1365
2/2
✓ Branch 0 taken 63070 times.
✓ Branch 1 taken 9010 times.
72080 for (i = 1; i < AAC_NUM_BLOCKS_SHORT; i++) {
1366 63070 float a = pch->next_win_level[i], b = pch->next_win_level[i-1];
1367
7/8
✓ Branch 0 taken 30590 times.
✓ Branch 1 taken 32480 times.
✓ Branch 2 taken 30590 times.
✓ Branch 3 taken 32480 times.
✓ Branch 4 taken 63070 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30590 times.
✓ Branch 7 taken 32480 times.
63070 float hi = FFMAX(a, b), lo = FFMAX(FFMIN(a, b), 1.0f);
1368
4/4
✓ Branch 0 taken 62404 times.
✓ Branch 1 taken 666 times.
✓ Branch 2 taken 53474 times.
✓ Branch 3 taken 8930 times.
63070 if (hi <= lo * PSY_LAME_GRP_RATIO && glen < PSY_LAME_GRP_MAX) {
1369 53474 bits |= 1 << i;
1370 53474 glen++;
1371 } else {
1372 9596 glen = 1;
1373 }
1374 }
1375 9010 pch->next_grouping = bits;
1376 }
1377
1378 9010 pch->prev_attack = attacks[AAC_NUM_BLOCKS_SHORT - 1];
1379
1380 9010 return wi;
1381 }
1382
1383 932 static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx, const float *audio,
1384 const float *la, int channel, int prev_type)
1385 {
1386 932 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
1387 932 AacPsyChannel *pch = &pctx->ch[channel];
1388 932 int attacks[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
1389 932 int uselongblock = psy_lame_detect(pctx, pch, la, channel, prev_type, attacks);
1390
1391 932 return psy_lame_apply(pctx, pch, uselongblock, attacks, prev_type, !!la);
1392 }
1393
1394 /* Pair-synced block switching: either channel's attack switches both. */
1395 4039 static void psy_lame_window_pair(FFPsyContext *ctx,
1396 const float *audio0, const float *la0,
1397 const float *audio1, const float *la1,
1398 int channel0, int channel1,
1399 int prev_type0, int prev_type1,
1400 FFPsyWindowInfo wi[2])
1401 {
1402 4039 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
1403 4039 AacPsyChannel *pch0 = &pctx->ch[channel0];
1404 4039 AacPsyChannel *pch1 = &pctx->ch[channel1];
1405 4039 int att0[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
1406 4039 int att1[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
1407 int merged[AAC_NUM_BLOCKS_SHORT + 1];
1408 4039 int u0 = psy_lame_detect(pctx, pch0, la0, channel0, prev_type0, att0);
1409 4039 int u1 = psy_lame_detect(pctx, pch1, la1, channel1, prev_type1, att1);
1410
4/4
✓ Branch 0 taken 3847 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 3793 times.
✓ Branch 3 taken 54 times.
4039 int u = u0 && u1;
1411
1412
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4036 times.
4039 if (ctx->pair_decoupled[(channel0 >> 1) & 15]) {
1413 /* Joint tools are dead on this pair (encoder-fed state): each channel
1414 * windows for ITS transients - divergence costs nothing there, while
1415 * union-syncing forces the steady channel short at every event in
1416 * the other. Correlated content keeps the sync. */
1417 3 wi[0] = psy_lame_apply(pctx, pch0, u0, att0, prev_type0, !!la0);
1418 3 wi[1] = psy_lame_apply(pctx, pch1, u1, att1, prev_type1, !!la1);
1419 3 return;
1420 }
1421
1422 /* One merged attack map for both channels: the grouping (and with it
1423 * common_window) must match across the pair, and the group boundary
1424 * should isolate the first attack heard in EITHER channel. */
1425
2/2
✓ Branch 0 taken 36324 times.
✓ Branch 1 taken 4036 times.
40360 for (int i = 0; i < AAC_NUM_BLOCKS_SHORT + 1; i++)
1426
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 36054 times.
36324 merged[i] = att0[i] ? att0[i] : att1[i];
1427
1428 /* grouping must also match across the pair: merge the level maps */
1429
2/2
✓ Branch 0 taken 32288 times.
✓ Branch 1 taken 4036 times.
36324 for (int i = 0; i < AAC_NUM_BLOCKS_SHORT; i++)
1430 32288 pch0->next_win_level[i] = pch1->next_win_level[i] =
1431
2/2
✓ Branch 0 taken 9876 times.
✓ Branch 1 taken 22412 times.
32288 FFMAX(pch0->next_win_level[i], pch1->next_win_level[i]);
1432
1433 4036 wi[0] = psy_lame_apply(pctx, pch0, u, merged, prev_type0, !!la0);
1434 4036 wi[1] = psy_lame_apply(pctx, pch1, u, merged, prev_type1, !!la1);
1435 }
1436
1437 const FFPsyModel ff_aac_psy_model =
1438 {
1439 .name = "3GPP TS 26.403-inspired model",
1440 .init = psy_3gpp_init,
1441 .window = psy_lame_window,
1442 .analyze = psy_3gpp_analyze,
1443 .end = psy_3gpp_end,
1444 .window_pair = psy_lame_window_pair,
1445 };
1446