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 | #include "aacenc_ltp.h" | ||
51 | #include "aacenc_pred.h" | ||
52 | |||
53 | #include "libavcodec/aaccoder_twoloop.h" | ||
54 | |||
55 | /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread | ||
56 | * beyond which no PNS is used (since the SFBs contain tone rather than noise) */ | ||
57 | #define NOISE_SPREAD_THRESHOLD 0.9f | ||
58 | |||
59 | /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to | ||
60 | * replace low energy non zero bands */ | ||
61 | #define NOISE_LAMBDA_REPLACE 1.948f | ||
62 | |||
63 | #include "libavcodec/aaccoder_trellis.h" | ||
64 | |||
65 | typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb, | ||
66 | const float *in, float *quant, const float *scaled, | ||
67 | int size, int scale_idx, int cb, | ||
68 | const float lambda, const float uplim, | ||
69 | int *bits, float *energy); | ||
70 | |||
71 | /** | ||
72 | * Calculate rate distortion cost for quantizing with given codebook | ||
73 | * | ||
74 | * @return quantization distortion | ||
75 | */ | ||
76 | 9209431 | static av_always_inline float quantize_and_encode_band_cost_template( | |
77 | struct AACEncContext *s, | ||
78 | PutBitContext *pb, const float *in, float *out, | ||
79 | const float *scaled, int size, int scale_idx, | ||
80 | int cb, const float lambda, const float uplim, | ||
81 | int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, | ||
82 | int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, | ||
83 | const float ROUNDING) | ||
84 | { | ||
85 | 9209431 | const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; | |
86 | 9209431 | const float Q = ff_aac_pow2sf_tab [q_idx]; | |
87 | 9209431 | const float Q34 = ff_aac_pow34sf_tab[q_idx]; | |
88 | 9209431 | const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | |
89 | 9209431 | const float CLIPPED_ESCAPE = 165140.0f*IQ; | |
90 | 9209431 | float cost = 0; | |
91 | 9209431 | float qenergy = 0; | |
92 |
2/2✓ Branch 0 taken 4543142 times.
✓ Branch 1 taken 4666289 times.
|
9209431 | const int dim = BT_PAIR ? 2 : 4; |
93 | 9209431 | int resbits = 0; | |
94 | int off; | ||
95 | |||
96 |
6/6✓ Branch 0 taken 7587715 times.
✓ Branch 1 taken 1621716 times.
✓ Branch 2 taken 7574415 times.
✓ Branch 3 taken 13300 times.
✓ Branch 4 taken 17350 times.
✓ Branch 5 taken 7557065 times.
|
9209431 | if (BT_ZERO || BT_NOISE || BT_STEREO) { |
97 |
2/2✓ Branch 0 taken 37189524 times.
✓ Branch 1 taken 1652366 times.
|
38841890 | for (int i = 0; i < size; i++) |
98 | 37189524 | cost += in[i]*in[i]; | |
99 |
2/2✓ Branch 0 taken 1628852 times.
✓ Branch 1 taken 23514 times.
|
1652366 | if (bits) |
100 | 1628852 | *bits = 0; | |
101 |
2/2✓ Branch 0 taken 1596318 times.
✓ Branch 1 taken 56048 times.
|
1652366 | if (energy) |
102 | 1596318 | *energy = qenergy; | |
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1652366 times.
|
1652366 | if (out) { |
104 | ✗ | for (int i = 0; i < size; i += dim) | |
105 | ✗ | for (int j = 0; j < dim; j++) | |
106 | ✗ | out[i+j] = 0.0f; | |
107 | } | ||
108 | 1652366 | return cost * lambda; | |
109 | } | ||
110 |
2/2✓ Branch 0 taken 469295 times.
✓ Branch 1 taken 7087770 times.
|
7557065 | if (!scaled) { |
111 | 469295 | s->aacdsp.abs_pow34(s->scoefs, in, size); | |
112 | 469295 | scaled = s->scoefs; | |
113 | } | ||
114 | 7557065 | s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING); | |
115 |
2/2✓ Branch 0 taken 4623855 times.
✓ Branch 1 taken 2933210 times.
|
7557065 | if (BT_UNSIGNED) { |
116 | 4623855 | off = 0; | |
117 | } else { | ||
118 | 2933210 | off = aac_cb_maxval[cb]; | |
119 | } | ||
120 |
2/2✓ Branch 0 taken 59793004 times.
✓ Branch 1 taken 7549665 times.
|
67342669 | for (int i = 0; i < size; i += dim) { |
121 | const float *vec; | ||
122 | 59793004 | int *quants = s->qcoefs + i; | |
123 | 59793004 | int curidx = 0; | |
124 | int curbits; | ||
125 | 59793004 | float quantized, rd = 0.0f; | |
126 |
2/2✓ Branch 0 taken 154525248 times.
✓ Branch 1 taken 59793004 times.
|
214318252 | for (int j = 0; j < dim; j++) { |
127 | 154525248 | curidx *= aac_cb_range[cb]; | |
128 | 154525248 | curidx += quants[j] + off; | |
129 | } | ||
130 | 59793004 | curbits = ff_aac_spectral_bits[cb-1][curidx]; | |
131 | 59793004 | vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | |
132 |
2/2✓ Branch 0 taken 37505267 times.
✓ Branch 1 taken 22287737 times.
|
59793004 | if (BT_UNSIGNED) { |
133 |
2/2✓ Branch 0 taken 89600198 times.
✓ Branch 1 taken 37505267 times.
|
127105465 | for (int j = 0; j < dim; j++) { |
134 | 89600198 | float t = fabsf(in[i+j]); | |
135 | float di; | ||
136 |
4/4✓ Branch 0 taken 15712130 times.
✓ Branch 1 taken 73888068 times.
✓ Branch 2 taken 1618136 times.
✓ Branch 3 taken 14093994 times.
|
89600198 | if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow |
137 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 1618023 times.
|
1618136 | if (t >= CLIPPED_ESCAPE) { |
138 | 113 | quantized = CLIPPED_ESCAPE; | |
139 | 113 | curbits += 21; | |
140 | } else { | ||
141 | 1618023 | int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13); | |
142 | 1618023 | quantized = c*cbrtf(c)*IQ; | |
143 | 1618023 | curbits += av_log2(c)*2 - 4 + 1; | |
144 | } | ||
145 | } else { | ||
146 | 87982062 | quantized = vec[j]*IQ; | |
147 | } | ||
148 | 89600198 | di = t - quantized; | |
149 |
2/2✓ Branch 0 taken 55528 times.
✓ Branch 1 taken 89544670 times.
|
89600198 | if (out) |
150 |
2/2✓ Branch 0 taken 27577 times.
✓ Branch 1 taken 27951 times.
|
55528 | out[i+j] = in[i+j] >= 0 ? quantized : -quantized; |
151 |
2/2✓ Branch 0 taken 51625338 times.
✓ Branch 1 taken 37974860 times.
|
89600198 | if (vec[j] != 0.0f) |
152 | 51625338 | curbits++; | |
153 | 89600198 | qenergy += quantized*quantized; | |
154 | 89600198 | rd += di*di; | |
155 | } | ||
156 | } else { | ||
157 |
2/2✓ Branch 0 taken 64925050 times.
✓ Branch 1 taken 22287737 times.
|
87212787 | for (int j = 0; j < dim; j++) { |
158 | 64925050 | quantized = vec[j]*IQ; | |
159 | 64925050 | qenergy += quantized*quantized; | |
160 |
2/2✓ Branch 0 taken 443504 times.
✓ Branch 1 taken 64481546 times.
|
64925050 | if (out) |
161 | 443504 | out[i+j] = quantized; | |
162 | 64925050 | rd += (in[i+j] - quantized)*(in[i+j] - quantized); | |
163 | } | ||
164 | } | ||
165 | 59793004 | cost += rd * lambda + curbits; | |
166 | 59793004 | resbits += curbits; | |
167 |
2/2✓ Branch 0 taken 7400 times.
✓ Branch 1 taken 59785604 times.
|
59793004 | if (cost >= uplim) |
168 | 7400 | return uplim; | |
169 |
2/2✓ Branch 0 taken 3365767 times.
✓ Branch 1 taken 56419837 times.
|
59785604 | if (pb) { |
170 | 3365767 | put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); | |
171 |
2/2✓ Branch 0 taken 1758664 times.
✓ Branch 1 taken 1607103 times.
|
3365767 | if (BT_UNSIGNED) |
172 |
2/2✓ Branch 0 taken 4658984 times.
✓ Branch 1 taken 1758664 times.
|
6417648 | for (int j = 0; j < dim; j++) |
173 |
2/2✓ Branch 0 taken 3229049 times.
✓ Branch 1 taken 1429935 times.
|
4658984 | if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) |
174 | 3229049 | put_bits(pb, 1, in[i+j] < 0.0f); | |
175 |
2/2✓ Branch 0 taken 454666 times.
✓ Branch 1 taken 2911101 times.
|
3365767 | if (BT_ESC) { |
176 |
2/2✓ Branch 0 taken 909332 times.
✓ Branch 1 taken 454666 times.
|
1363998 | for (int j = 0; j < 2; j++) { |
177 |
2/2✓ Branch 0 taken 132912 times.
✓ Branch 1 taken 776420 times.
|
909332 | if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { |
178 | 132912 | int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13); | |
179 | 132912 | int len = av_log2(coef); | |
180 | |||
181 | 132912 | put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); | |
182 | 132912 | put_sbits(pb, len, coef); | |
183 | } | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | |||
189 |
2/2✓ Branch 0 taken 6981773 times.
✓ Branch 1 taken 567892 times.
|
7549665 | if (bits) |
190 | 6981773 | *bits = resbits; | |
191 |
2/2✓ Branch 0 taken 3338013 times.
✓ Branch 1 taken 4211652 times.
|
7549665 | if (energy) |
192 | 3338013 | *energy = qenergy; | |
193 | 7549665 | return cost; | |
194 | } | ||
195 | |||
196 | ✗ | static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, | |
197 | const float *in, float *quant, const float *scaled, | ||
198 | int size, int scale_idx, int cb, | ||
199 | const float lambda, const float uplim, | ||
200 | int *bits, float *energy) { | ||
201 | ✗ | av_assert0(0); | |
202 | return 0.0f; | ||
203 | } | ||
204 | |||
205 | #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \ | ||
206 | static float quantize_and_encode_band_cost_ ## NAME( \ | ||
207 | struct AACEncContext *s, \ | ||
208 | PutBitContext *pb, const float *in, float *quant, \ | ||
209 | const float *scaled, int size, int scale_idx, \ | ||
210 | int cb, const float lambda, const float uplim, \ | ||
211 | int *bits, float *energy) { \ | ||
212 | return quantize_and_encode_band_cost_template( \ | ||
213 | s, pb, in, quant, scaled, size, scale_idx, \ | ||
214 | BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \ | ||
215 | BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \ | ||
216 | ROUNDING); \ | ||
217 | } | ||
218 | |||
219 | 1621716 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
220 | 1699236 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
221 | 1314687 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD) | |
222 | 1233974 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD) | |
223 | 2296227 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD) | |
224 | 1007828 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD) | |
225 | 5113 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO) | |
226 | 13300 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD) | |
227 | 17350 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD) | |
228 | |||
229 | static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[] = | ||
230 | { | ||
231 | quantize_and_encode_band_cost_ZERO, | ||
232 | quantize_and_encode_band_cost_SQUAD, | ||
233 | quantize_and_encode_band_cost_SQUAD, | ||
234 | quantize_and_encode_band_cost_UQUAD, | ||
235 | quantize_and_encode_band_cost_UQUAD, | ||
236 | quantize_and_encode_band_cost_SPAIR, | ||
237 | quantize_and_encode_band_cost_SPAIR, | ||
238 | quantize_and_encode_band_cost_UPAIR, | ||
239 | quantize_and_encode_band_cost_UPAIR, | ||
240 | quantize_and_encode_band_cost_UPAIR, | ||
241 | quantize_and_encode_band_cost_UPAIR, | ||
242 | quantize_and_encode_band_cost_ESC, | ||
243 | quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | ||
244 | quantize_and_encode_band_cost_NOISE, | ||
245 | quantize_and_encode_band_cost_STEREO, | ||
246 | quantize_and_encode_band_cost_STEREO, | ||
247 | }; | ||
248 | |||
249 | static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[] = | ||
250 | { | ||
251 | quantize_and_encode_band_cost_ZERO, | ||
252 | quantize_and_encode_band_cost_SQUAD, | ||
253 | quantize_and_encode_band_cost_SQUAD, | ||
254 | quantize_and_encode_band_cost_UQUAD, | ||
255 | quantize_and_encode_band_cost_UQUAD, | ||
256 | quantize_and_encode_band_cost_SPAIR, | ||
257 | quantize_and_encode_band_cost_SPAIR, | ||
258 | quantize_and_encode_band_cost_UPAIR, | ||
259 | quantize_and_encode_band_cost_UPAIR, | ||
260 | quantize_and_encode_band_cost_UPAIR, | ||
261 | quantize_and_encode_band_cost_UPAIR, | ||
262 | quantize_and_encode_band_cost_ESC_RTZ, | ||
263 | quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | ||
264 | quantize_and_encode_band_cost_NOISE, | ||
265 | quantize_and_encode_band_cost_STEREO, | ||
266 | quantize_and_encode_band_cost_STEREO, | ||
267 | }; | ||
268 | |||
269 | 8725051 | float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb, | |
270 | const float *in, float *quant, const float *scaled, | ||
271 | int size, int scale_idx, int cb, | ||
272 | const float lambda, const float uplim, | ||
273 | int *bits, float *energy) | ||
274 | { | ||
275 | 8725051 | return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size, | |
276 | scale_idx, cb, lambda, uplim, | ||
277 | bits, energy); | ||
278 | } | ||
279 | |||
280 | 484380 | static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, | |
281 | const float *in, float *out, int size, int scale_idx, | ||
282 | int cb, const float lambda, int rtz) | ||
283 | { | ||
284 |
2/2✓ Branch 0 taken 16224 times.
✓ Branch 1 taken 468156 times.
|
484380 | (rtz ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb](s, pb, in, out, NULL, size, scale_idx, cb, |
285 | lambda, INFINITY, NULL, NULL); | ||
286 | 484380 | } | |
287 | |||
288 | /** | ||
289 | * structure used in optimal codebook search | ||
290 | */ | ||
291 | typedef struct BandCodingPath { | ||
292 | int prev_idx; ///< pointer to the previous path point | ||
293 | float cost; ///< path cost | ||
294 | int run; | ||
295 | } BandCodingPath; | ||
296 | |||
297 | /** | ||
298 | * Encode band info for single window group bands. | ||
299 | */ | ||
300 | ✗ | static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce, | |
301 | int win, int group_len, const float lambda) | ||
302 | { | ||
303 | BandCodingPath path[120][CB_TOT_ALL]; | ||
304 | int w, swb, cb, start, size; | ||
305 | int i, j; | ||
306 | ✗ | const int max_sfb = sce->ics.max_sfb; | |
307 | ✗ | const int run_bits = sce->ics.num_windows == 1 ? 5 : 3; | |
308 | ✗ | const int run_esc = (1 << run_bits) - 1; | |
309 | int idx, ppos, count; | ||
310 | int stackrun[120], stackcb[120], stack_len; | ||
311 | ✗ | float next_minrd = INFINITY; | |
312 | ✗ | int next_mincb = 0; | |
313 | |||
314 | ✗ | s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024); | |
315 | ✗ | start = win*128; | |
316 | ✗ | for (cb = 0; cb < CB_TOT_ALL; cb++) { | |
317 | ✗ | path[0][cb].cost = 0.0f; | |
318 | ✗ | path[0][cb].prev_idx = -1; | |
319 | ✗ | path[0][cb].run = 0; | |
320 | } | ||
321 | ✗ | for (swb = 0; swb < max_sfb; swb++) { | |
322 | ✗ | size = sce->ics.swb_sizes[swb]; | |
323 | ✗ | if (sce->zeroes[win*16 + swb]) { | |
324 | ✗ | for (cb = 0; cb < CB_TOT_ALL; cb++) { | |
325 | ✗ | path[swb+1][cb].prev_idx = cb; | |
326 | ✗ | path[swb+1][cb].cost = path[swb][cb].cost; | |
327 | ✗ | path[swb+1][cb].run = path[swb][cb].run + 1; | |
328 | } | ||
329 | } else { | ||
330 | ✗ | float minrd = next_minrd; | |
331 | ✗ | int mincb = next_mincb; | |
332 | ✗ | next_minrd = INFINITY; | |
333 | ✗ | next_mincb = 0; | |
334 | ✗ | for (cb = 0; cb < CB_TOT_ALL; cb++) { | |
335 | float cost_stay_here, cost_get_here; | ||
336 | ✗ | float rd = 0.0f; | |
337 | ✗ | if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] || | |
338 | ✗ | cb < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) { | |
339 | ✗ | path[swb+1][cb].prev_idx = -1; | |
340 | ✗ | path[swb+1][cb].cost = INFINITY; | |
341 | ✗ | path[swb+1][cb].run = path[swb][cb].run + 1; | |
342 | ✗ | continue; | |
343 | } | ||
344 | ✗ | for (w = 0; w < group_len; w++) { | |
345 | ✗ | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb]; | |
346 | ✗ | rd += quantize_band_cost(s, &sce->coeffs[start + w*128], | |
347 | ✗ | &s->scoefs[start + w*128], size, | |
348 | ✗ | sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb], | |
349 | ✗ | lambda / band->threshold, INFINITY, NULL, NULL); | |
350 | } | ||
351 | ✗ | cost_stay_here = path[swb][cb].cost + rd; | |
352 | ✗ | cost_get_here = minrd + rd + run_bits + 4; | |
353 | ✗ | if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run] | |
354 | ✗ | != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1]) | |
355 | ✗ | cost_stay_here += run_bits; | |
356 | ✗ | if (cost_get_here < cost_stay_here) { | |
357 | ✗ | path[swb+1][cb].prev_idx = mincb; | |
358 | ✗ | path[swb+1][cb].cost = cost_get_here; | |
359 | ✗ | path[swb+1][cb].run = 1; | |
360 | } else { | ||
361 | ✗ | path[swb+1][cb].prev_idx = cb; | |
362 | ✗ | path[swb+1][cb].cost = cost_stay_here; | |
363 | ✗ | path[swb+1][cb].run = path[swb][cb].run + 1; | |
364 | } | ||
365 | ✗ | if (path[swb+1][cb].cost < next_minrd) { | |
366 | ✗ | next_minrd = path[swb+1][cb].cost; | |
367 | ✗ | next_mincb = cb; | |
368 | } | ||
369 | } | ||
370 | } | ||
371 | ✗ | start += sce->ics.swb_sizes[swb]; | |
372 | } | ||
373 | |||
374 | //convert resulting path from backward-linked list | ||
375 | ✗ | stack_len = 0; | |
376 | ✗ | idx = 0; | |
377 | ✗ | for (cb = 1; cb < CB_TOT_ALL; cb++) | |
378 | ✗ | if (path[max_sfb][cb].cost < path[max_sfb][idx].cost) | |
379 | ✗ | idx = cb; | |
380 | ✗ | ppos = max_sfb; | |
381 | ✗ | while (ppos > 0) { | |
382 | av_assert1(idx >= 0); | ||
383 | ✗ | cb = idx; | |
384 | ✗ | stackrun[stack_len] = path[ppos][cb].run; | |
385 | ✗ | stackcb [stack_len] = cb; | |
386 | ✗ | idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx; | |
387 | ✗ | ppos -= path[ppos][cb].run; | |
388 | ✗ | stack_len++; | |
389 | } | ||
390 | //perform actual band info encoding | ||
391 | ✗ | start = 0; | |
392 | ✗ | for (i = stack_len - 1; i >= 0; i--) { | |
393 | ✗ | cb = aac_cb_out_map[stackcb[i]]; | |
394 | ✗ | put_bits(&s->pb, 4, cb); | |
395 | ✗ | count = stackrun[i]; | |
396 | ✗ | memset(sce->zeroes + win*16 + start, !cb, count); | |
397 | //XXX: memset when band_type is also uint8_t | ||
398 | ✗ | for (j = 0; j < count; j++) { | |
399 | ✗ | sce->band_type[win*16 + start] = cb; | |
400 | ✗ | start++; | |
401 | } | ||
402 | ✗ | while (count >= run_esc) { | |
403 | ✗ | put_bits(&s->pb, run_bits, run_esc); | |
404 | ✗ | count -= run_esc; | |
405 | } | ||
406 | ✗ | put_bits(&s->pb, run_bits, count); | |
407 | } | ||
408 | ✗ | } | |
409 | |||
410 | |||
411 | typedef struct TrellisPath { | ||
412 | float cost; | ||
413 | int prev; | ||
414 | } TrellisPath; | ||
415 | |||
416 | #define TRELLIS_STAGES 121 | ||
417 | #define TRELLIS_STATES (SCALE_MAX_DIFF+1) | ||
418 | |||
419 | 10231 | static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce) | |
420 | { | ||
421 | int w, g; | ||
422 | 10231 | int prevscaler_n = -255, prevscaler_i = 0; | |
423 | 10231 | int bands = 0; | |
424 | |||
425 |
2/2✓ Branch 0 taken 10883 times.
✓ Branch 1 taken 10231 times.
|
21114 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
426 |
2/2✓ Branch 0 taken 498280 times.
✓ Branch 1 taken 10883 times.
|
509163 | for (g = 0; g < sce->ics.num_swb; g++) { |
427 |
2/2✓ Branch 0 taken 13935 times.
✓ Branch 1 taken 484345 times.
|
498280 | if (sce->zeroes[w*16+g]) |
428 | 13935 | continue; | |
429 |
4/4✓ Branch 0 taken 478019 times.
✓ Branch 1 taken 6326 times.
✓ Branch 2 taken 2238 times.
✓ Branch 3 taken 475781 times.
|
484345 | if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) { |
430 | 8564 | sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100); | |
431 | 8564 | bands++; | |
432 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 469406 times.
|
475781 | } else if (sce->band_type[w*16+g] == NOISE_BT) { |
433 | 6375 | sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155); | |
434 |
2/2✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 5253 times.
|
6375 | if (prevscaler_n == -255) |
435 | 1122 | prevscaler_n = sce->sf_idx[w*16+g]; | |
436 | 6375 | bands++; | |
437 | } | ||
438 | } | ||
439 | } | ||
440 | |||
441 |
2/2✓ Branch 0 taken 8646 times.
✓ Branch 1 taken 1585 times.
|
10231 | if (!bands) |
442 | 8646 | return; | |
443 | |||
444 | /* Clip the scalefactor indices */ | ||
445 |
2/2✓ Branch 0 taken 1652 times.
✓ Branch 1 taken 1585 times.
|
3237 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
446 |
2/2✓ Branch 0 taken 77798 times.
✓ Branch 1 taken 1652 times.
|
79450 | for (g = 0; g < sce->ics.num_swb; g++) { |
447 |
2/2✓ Branch 0 taken 5080 times.
✓ Branch 1 taken 72718 times.
|
77798 | if (sce->zeroes[w*16+g]) |
448 | 5080 | continue; | |
449 |
4/4✓ Branch 0 taken 66392 times.
✓ Branch 1 taken 6326 times.
✓ Branch 2 taken 2238 times.
✓ Branch 3 taken 64154 times.
|
72718 | if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) { |
450 | 8564 | sce->sf_idx[w*16+g] = prevscaler_i = av_clip(sce->sf_idx[w*16+g], prevscaler_i - SCALE_MAX_DIFF, prevscaler_i + SCALE_MAX_DIFF); | |
451 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 57779 times.
|
64154 | } else if (sce->band_type[w*16+g] == NOISE_BT) { |
452 | 6375 | 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); | |
453 | } | ||
454 | } | ||
455 | } | ||
456 | } | ||
457 | |||
458 | ✗ | static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, | |
459 | SingleChannelElement *sce, | ||
460 | const float lambda) | ||
461 | { | ||
462 | ✗ | int q, w, w2, g, start = 0; | |
463 | int i, j; | ||
464 | int idx; | ||
465 | TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES]; | ||
466 | int bandaddr[TRELLIS_STAGES]; | ||
467 | int minq; | ||
468 | float mincost; | ||
469 | ✗ | float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f; | |
470 | ✗ | int q0, q1, qcnt = 0; | |
471 | |||
472 | ✗ | for (i = 0; i < 1024; i++) { | |
473 | ✗ | float t = fabsf(sce->coeffs[i]); | |
474 | ✗ | if (t > 0.0f) { | |
475 | ✗ | q0f = FFMIN(q0f, t); | |
476 | ✗ | q1f = FFMAX(q1f, t); | |
477 | ✗ | qnrgf += t*t; | |
478 | ✗ | qcnt++; | |
479 | } | ||
480 | } | ||
481 | |||
482 | ✗ | if (!qcnt) { | |
483 | ✗ | memset(sce->sf_idx, 0, sizeof(sce->sf_idx)); | |
484 | ✗ | memset(sce->zeroes, 1, sizeof(sce->zeroes)); | |
485 | ✗ | return; | |
486 | } | ||
487 | |||
488 | //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped | ||
489 | ✗ | q0 = av_clip(coef2minsf(q0f), 0, SCALE_MAX_POS-1); | |
490 | //maximum scalefactor index is when maximum coefficient after quantizing is still not zero | ||
491 | ✗ | q1 = av_clip(coef2maxsf(q1f), 1, SCALE_MAX_POS); | |
492 | ✗ | if (q1 - q0 > 60) { | |
493 | ✗ | int q0low = q0; | |
494 | ✗ | int q1high = q1; | |
495 | //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped | ||
496 | ✗ | int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512); | |
497 | ✗ | q1 = qnrg + 30; | |
498 | ✗ | q0 = qnrg - 30; | |
499 | ✗ | if (q0 < q0low) { | |
500 | ✗ | q1 += q0low - q0; | |
501 | ✗ | q0 = q0low; | |
502 | ✗ | } else if (q1 > q1high) { | |
503 | ✗ | q0 -= q1 - q1high; | |
504 | ✗ | q1 = q1high; | |
505 | } | ||
506 | } | ||
507 | // q0 == q1 isn't really a legal situation | ||
508 | ✗ | if (q0 == q1) { | |
509 | // the following is indirect but guarantees q1 != q0 && q1 near q0 | ||
510 | ✗ | q1 = av_clip(q0+1, 1, SCALE_MAX_POS); | |
511 | ✗ | q0 = av_clip(q1-1, 0, SCALE_MAX_POS - 1); | |
512 | } | ||
513 | |||
514 | ✗ | for (i = 0; i < TRELLIS_STATES; i++) { | |
515 | ✗ | paths[0][i].cost = 0.0f; | |
516 | ✗ | paths[0][i].prev = -1; | |
517 | } | ||
518 | ✗ | for (j = 1; j < TRELLIS_STAGES; j++) { | |
519 | ✗ | for (i = 0; i < TRELLIS_STATES; i++) { | |
520 | ✗ | paths[j][i].cost = INFINITY; | |
521 | ✗ | paths[j][i].prev = -2; | |
522 | } | ||
523 | } | ||
524 | ✗ | idx = 1; | |
525 | ✗ | s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024); | |
526 | ✗ | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { | |
527 | ✗ | start = w*128; | |
528 | ✗ | for (g = 0; g < sce->ics.num_swb; g++) { | |
529 | ✗ | const float *coefs = &sce->coeffs[start]; | |
530 | float qmin, qmax; | ||
531 | ✗ | int nz = 0; | |
532 | |||
533 | ✗ | bandaddr[idx] = w * 16 + g; | |
534 | ✗ | qmin = INT_MAX; | |
535 | ✗ | qmax = 0.0f; | |
536 | ✗ | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { | |
537 | ✗ | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
538 | ✗ | if (band->energy <= band->threshold || band->threshold == 0.0f) { | |
539 | ✗ | sce->zeroes[(w+w2)*16+g] = 1; | |
540 | ✗ | continue; | |
541 | } | ||
542 | ✗ | sce->zeroes[(w+w2)*16+g] = 0; | |
543 | ✗ | nz = 1; | |
544 | ✗ | for (i = 0; i < sce->ics.swb_sizes[g]; i++) { | |
545 | ✗ | float t = fabsf(coefs[w2*128+i]); | |
546 | ✗ | if (t > 0.0f) | |
547 | ✗ | qmin = FFMIN(qmin, t); | |
548 | ✗ | qmax = FFMAX(qmax, t); | |
549 | } | ||
550 | } | ||
551 | ✗ | if (nz) { | |
552 | int minscale, maxscale; | ||
553 | ✗ | float minrd = INFINITY; | |
554 | float maxval; | ||
555 | //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped | ||
556 | ✗ | minscale = coef2minsf(qmin); | |
557 | //maximum scalefactor index is when maximum coefficient after quantizing is still not zero | ||
558 | ✗ | maxscale = coef2maxsf(qmax); | |
559 | ✗ | minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1); | |
560 | ✗ | maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES); | |
561 | ✗ | if (minscale == maxscale) { | |
562 | ✗ | maxscale = av_clip(minscale+1, 1, TRELLIS_STATES); | |
563 | ✗ | minscale = av_clip(maxscale-1, 0, TRELLIS_STATES - 1); | |
564 | } | ||
565 | ✗ | maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start); | |
566 | ✗ | for (q = minscale; q < maxscale; q++) { | |
567 | ✗ | float dist = 0; | |
568 | ✗ | int cb = find_min_book(maxval, sce->sf_idx[w*16+g]); | |
569 | ✗ | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { | |
570 | ✗ | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
571 | ✗ | dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g], | |
572 | ✗ | q + q0, cb, lambda / band->threshold, INFINITY, NULL, NULL); | |
573 | } | ||
574 | ✗ | minrd = FFMIN(minrd, dist); | |
575 | |||
576 | ✗ | for (i = 0; i < q1 - q0; i++) { | |
577 | float cost; | ||
578 | ✗ | cost = paths[idx - 1][i].cost + dist | |
579 | ✗ | + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO]; | |
580 | ✗ | if (cost < paths[idx][q].cost) { | |
581 | ✗ | paths[idx][q].cost = cost; | |
582 | ✗ | paths[idx][q].prev = i; | |
583 | } | ||
584 | } | ||
585 | } | ||
586 | } else { | ||
587 | ✗ | for (q = 0; q < q1 - q0; q++) { | |
588 | ✗ | paths[idx][q].cost = paths[idx - 1][q].cost + 1; | |
589 | ✗ | paths[idx][q].prev = q; | |
590 | } | ||
591 | } | ||
592 | ✗ | sce->zeroes[w*16+g] = !nz; | |
593 | ✗ | start += sce->ics.swb_sizes[g]; | |
594 | ✗ | idx++; | |
595 | } | ||
596 | } | ||
597 | ✗ | idx--; | |
598 | ✗ | mincost = paths[idx][0].cost; | |
599 | ✗ | minq = 0; | |
600 | ✗ | for (i = 1; i < TRELLIS_STATES; i++) { | |
601 | ✗ | if (paths[idx][i].cost < mincost) { | |
602 | ✗ | mincost = paths[idx][i].cost; | |
603 | ✗ | minq = i; | |
604 | } | ||
605 | } | ||
606 | ✗ | while (idx) { | |
607 | ✗ | sce->sf_idx[bandaddr[idx]] = minq + q0; | |
608 | ✗ | minq = FFMAX(paths[idx][minq].prev, 0); | |
609 | ✗ | idx--; | |
610 | } | ||
611 | //set the same quantizers inside window groups | ||
612 | ✗ | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) | |
613 | ✗ | for (g = 0; g < sce->ics.num_swb; g++) | |
614 | ✗ | for (w2 = 1; w2 < sce->ics.group_len[w]; w2++) | |
615 | ✗ | sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g]; | |
616 | } | ||
617 | |||
618 | 9492 | static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, | |
619 | SingleChannelElement *sce, | ||
620 | const float lambda) | ||
621 | { | ||
622 | 9492 | int start = 0, i, w, w2, g; | |
623 | 9492 | int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f); | |
624 | 9492 | float dists[128] = { 0 }, uplims[128] = { 0 }; | |
625 | float maxvals[128]; | ||
626 | int fflag, minscaler; | ||
627 | 9492 | int its = 0; | |
628 | 9492 | int allz = 0; | |
629 | 9492 | float minthr = INFINITY; | |
630 | |||
631 | // for values above this the decoder might end up in an endless loop | ||
632 | // due to always having more bits than what can be encoded. | ||
633 | 9492 | destbits = FFMIN(destbits, 5800); | |
634 | //some heuristic to determine initial quantizers will reduce search time | ||
635 | //determine zero bands and upper limits | ||
636 |
2/2✓ Branch 0 taken 10110 times.
✓ Branch 1 taken 9492 times.
|
19602 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
637 | 10110 | start = 0; | |
638 |
2/2✓ Branch 0 taken 462013 times.
✓ Branch 1 taken 10110 times.
|
472123 | for (g = 0; g < sce->ics.num_swb; g++) { |
639 | 462013 | int nz = 0; | |
640 | 462013 | float uplim = 0.0f; | |
641 |
2/2✓ Branch 0 taken 476979 times.
✓ Branch 1 taken 462013 times.
|
938992 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
642 | 476979 | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
643 | 476979 | uplim += band->threshold; | |
644 |
3/4✓ Branch 0 taken 462539 times.
✓ Branch 1 taken 14440 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 462539 times.
|
476979 | if (band->energy <= band->threshold || band->threshold == 0.0f) { |
645 | 14440 | sce->zeroes[(w+w2)*16+g] = 1; | |
646 | 14440 | continue; | |
647 | } | ||
648 | 462539 | nz = 1; | |
649 | } | ||
650 | 462013 | uplims[w*16+g] = uplim *512; | |
651 | 462013 | sce->band_type[w*16+g] = 0; | |
652 | 462013 | sce->zeroes[w*16+g] = !nz; | |
653 |
2/2✓ Branch 0 taken 454126 times.
✓ Branch 1 taken 7887 times.
|
462013 | if (nz) |
654 |
2/2✓ Branch 0 taken 103575 times.
✓ Branch 1 taken 350551 times.
|
454126 | minthr = FFMIN(minthr, uplim); |
655 | 462013 | allz |= nz; | |
656 | 462013 | start += sce->ics.swb_sizes[g]; | |
657 | } | ||
658 | } | ||
659 |
2/2✓ Branch 0 taken 10110 times.
✓ Branch 1 taken 9492 times.
|
19602 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
660 |
2/2✓ Branch 0 taken 462013 times.
✓ Branch 1 taken 10110 times.
|
472123 | for (g = 0; g < sce->ics.num_swb; g++) { |
661 |
2/2✓ Branch 0 taken 7887 times.
✓ Branch 1 taken 454126 times.
|
462013 | if (sce->zeroes[w*16+g]) { |
662 | 7887 | sce->sf_idx[w*16+g] = SCALE_ONE_POS; | |
663 | 7887 | continue; | |
664 | } | ||
665 |
2/2✓ Branch 0 taken 207229 times.
✓ Branch 1 taken 246897 times.
|
454126 | sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59); |
666 | } | ||
667 | } | ||
668 | |||
669 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 9396 times.
|
9492 | if (!allz) |
670 | 96 | return; | |
671 | 9396 | s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024); | |
672 | 9396 | ff_quantize_band_cost_cache_init(s); | |
673 | |||
674 |
2/2✓ Branch 0 taken 9834 times.
✓ Branch 1 taken 9396 times.
|
19230 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
675 | 9834 | start = w*128; | |
676 |
2/2✓ Branch 0 taken 457939 times.
✓ Branch 1 taken 9834 times.
|
467773 | for (g = 0; g < sce->ics.num_swb; g++) { |
677 | 457939 | const float *scaled = s->scoefs + start; | |
678 | 457939 | maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled); | |
679 | 457939 | start += sce->ics.swb_sizes[g]; | |
680 | } | ||
681 | } | ||
682 | |||
683 | //perform two-loop search | ||
684 | //outer loop - improve quality | ||
685 | do { | ||
686 | int tbits, qstep; | ||
687 | 37496 | minscaler = sce->sf_idx[0]; | |
688 | //inner loop - quantize spectrum to fit into given number of bits | ||
689 |
2/2✓ Branch 0 taken 28100 times.
✓ Branch 1 taken 9396 times.
|
37496 | qstep = its ? 1 : 32; |
690 | do { | ||
691 | 89205 | int prev = -1; | |
692 | 89205 | tbits = 0; | |
693 |
2/2✓ Branch 0 taken 96651 times.
✓ Branch 1 taken 89205 times.
|
185856 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
694 | 96651 | start = w*128; | |
695 |
2/2✓ Branch 0 taken 4334819 times.
✓ Branch 1 taken 96651 times.
|
4431470 | for (g = 0; g < sce->ics.num_swb; g++) { |
696 | 4334819 | const float *coefs = sce->coeffs + start; | |
697 | 4334819 | const float *scaled = s->scoefs + start; | |
698 | 4334819 | int bits = 0; | |
699 | int cb; | ||
700 | 4334819 | float dist = 0.0f; | |
701 | |||
702 |
3/4✓ Branch 0 taken 4278616 times.
✓ Branch 1 taken 56203 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4278616 times.
|
4334819 | if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) { |
703 | 56203 | start += sce->ics.swb_sizes[g]; | |
704 | 56203 | continue; | |
705 | } | ||
706 | 4278616 | minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); | |
707 | 4278616 | cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
708 |
2/2✓ Branch 0 taken 4425588 times.
✓ Branch 1 taken 4278616 times.
|
8704204 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
709 | int b; | ||
710 | 8851176 | dist += quantize_band_cost_cached(s, w + w2, g, | |
711 | 4425588 | coefs + w2*128, | |
712 | 4425588 | scaled + w2*128, | |
713 | 4425588 | sce->ics.swb_sizes[g], | |
714 | 4425588 | sce->sf_idx[w*16+g], | |
715 | cb, 1.0f, INFINITY, | ||
716 | &b, NULL, 0); | ||
717 | 4425588 | bits += b; | |
718 | } | ||
719 | 4278616 | dists[w*16+g] = dist - bits; | |
720 |
2/2✓ Branch 0 taken 4189411 times.
✓ Branch 1 taken 89205 times.
|
4278616 | if (prev != -1) { |
721 | 4189411 | bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO]; | |
722 | } | ||
723 | 4278616 | tbits += bits; | |
724 | 4278616 | start += sce->ics.swb_sizes[g]; | |
725 | 4278616 | prev = sce->sf_idx[w*16+g]; | |
726 | } | ||
727 | } | ||
728 |
2/2✓ Branch 0 taken 18656 times.
✓ Branch 1 taken 70549 times.
|
89205 | if (tbits > destbits) { |
729 |
2/2✓ Branch 0 taken 2387968 times.
✓ Branch 1 taken 18656 times.
|
2406624 | for (i = 0; i < 128; i++) |
730 |
1/2✓ Branch 0 taken 2387968 times.
✗ Branch 1 not taken.
|
2387968 | if (sce->sf_idx[i] < 218 - qstep) |
731 | 2387968 | sce->sf_idx[i] += qstep; | |
732 | } else { | ||
733 |
2/2✓ Branch 0 taken 9030272 times.
✓ Branch 1 taken 70549 times.
|
9100821 | for (i = 0; i < 128; i++) |
734 |
2/2✓ Branch 0 taken 3687483 times.
✓ Branch 1 taken 5342789 times.
|
9030272 | if (sce->sf_idx[i] > 60 - qstep) |
735 | 3687483 | sce->sf_idx[i] -= qstep; | |
736 | } | ||
737 | 89205 | qstep >>= 1; | |
738 |
5/6✓ Branch 0 taken 42225 times.
✓ Branch 1 taken 46980 times.
✓ Branch 2 taken 4729 times.
✓ Branch 3 taken 37496 times.
✓ Branch 4 taken 4729 times.
✗ Branch 5 not taken.
|
89205 | if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217) |
739 | 4729 | qstep = 1; | |
740 |
2/2✓ Branch 0 taken 51709 times.
✓ Branch 1 taken 37496 times.
|
89205 | } while (qstep); |
741 | |||
742 | 37496 | fflag = 0; | |
743 | 37496 | minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF); | |
744 | |||
745 |
2/2✓ Branch 0 taken 40355 times.
✓ Branch 1 taken 37496 times.
|
77851 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
746 |
2/2✓ Branch 0 taken 1809420 times.
✓ Branch 1 taken 40355 times.
|
1849775 | for (g = 0; g < sce->ics.num_swb; g++) { |
747 | 1809420 | int prevsc = sce->sf_idx[w*16+g]; | |
748 |
3/4✓ Branch 0 taken 15594 times.
✓ Branch 1 taken 1793826 times.
✓ Branch 2 taken 15594 times.
✗ Branch 3 not taken.
|
1809420 | if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) { |
749 |
2/2✓ Branch 1 taken 14882 times.
✓ Branch 2 taken 712 times.
|
15594 | if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1)) |
750 | 14882 | sce->sf_idx[w*16+g]--; | |
751 | else //Try to make sure there is some energy in every band | ||
752 | 712 | sce->sf_idx[w*16+g]-=2; | |
753 | } | ||
754 | 1809420 | sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF); | |
755 | 1809420 | sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219); | |
756 |
2/2✓ Branch 0 taken 513325 times.
✓ Branch 1 taken 1296095 times.
|
1809420 | if (sce->sf_idx[w*16+g] != prevsc) |
757 | 513325 | fflag = 1; | |
758 | 1809420 | sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
759 | } | ||
760 | } | ||
761 | 37496 | its++; | |
762 |
4/4✓ Branch 0 taken 31105 times.
✓ Branch 1 taken 6391 times.
✓ Branch 2 taken 28100 times.
✓ Branch 3 taken 3005 times.
|
37496 | } while (fflag && its < 10); |
763 | } | ||
764 | |||
765 | 2003 | static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce) | |
766 | { | ||
767 | FFPsyBand *band; | ||
768 | int w, g, w2, i; | ||
769 | 2003 | int wlen = 1024 / sce->ics.num_windows; | |
770 | int bandwidth, cutoff; | ||
771 | 2003 | float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128]; | |
772 | 2003 | float *NOR34 = &s->scoefs[3*128]; | |
773 | uint8_t nextband[128]; | ||
774 | 2003 | const float lambda = s->lambda; | |
775 | 2003 | const float freq_mult = avctx->sample_rate*0.5f/wlen; | |
776 | 2003 | const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda); | |
777 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2003 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2003 | const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f)); |
778 | 2003 | const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f); | |
779 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f); |
780 | |||
781 | 4006 | int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate | |
782 |
1/2✓ Branch 0 taken 2003 times.
✗ Branch 1 not taken.
|
2003 | / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels) |
783 | 2003 | * (lambda / 120.f); | |
784 | |||
785 | /** Keep this in sync with twoloop's cutoff selection */ | ||
786 | 2003 | float rate_bandwidth_multiplier = 1.5f; | |
787 | 2003 | int prev = -1000, prev_sf = -1; | |
788 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE) |
789 | ✗ | ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024) | |
790 | 2003 | : (avctx->bit_rate / avctx->ch_layout.nb_channels); | |
791 | |||
792 | 2003 | frame_bit_rate *= 1.15f; | |
793 | |||
794 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 1171 times.
|
2003 | if (avctx->cutoff > 0) { |
795 | 832 | bandwidth = avctx->cutoff; | |
796 | } else { | ||
797 |
5/10✓ Branch 0 taken 1171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1171 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1171 times.
✓ Branch 6 taken 1171 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1171 times.
|
1171 | bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate)); |
798 | } | ||
799 | |||
800 | 2003 | cutoff = bandwidth * 2 * wlen / avctx->sample_rate; | |
801 | |||
802 | 2003 | memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type)); | |
803 | 2003 | ff_init_nextband_map(sce, nextband); | |
804 |
2/2✓ Branch 0 taken 2131 times.
✓ Branch 1 taken 2003 times.
|
4134 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
805 | 2131 | int wstart = w*128; | |
806 |
2/2✓ Branch 0 taken 94947 times.
✓ Branch 1 taken 2131 times.
|
97078 | for (g = 0; g < sce->ics.num_swb; g++) { |
807 | int noise_sfi; | ||
808 | 94947 | float dist1 = 0.0f, dist2 = 0.0f, noise_amp; | |
809 | 94947 | float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh; | |
810 | 94947 | float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f; | |
811 | 94947 | float min_energy = -1.0f, max_energy = 0.0f; | |
812 | 94947 | const int start = wstart+sce->ics.swb_offset[g]; | |
813 | 94947 | const float freq = (start-wstart)*freq_mult; | |
814 |
2/2✓ Branch 0 taken 46953 times.
✓ Branch 1 taken 47994 times.
|
94947 | const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f); |
815 |
4/4✓ Branch 0 taken 48836 times.
✓ Branch 1 taken 46111 times.
✓ Branch 2 taken 6599 times.
✓ Branch 3 taken 42237 times.
|
94947 | if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) { |
816 |
2/2✓ Branch 0 taken 43241 times.
✓ Branch 1 taken 9469 times.
|
52710 | if (!sce->zeroes[w*16+g]) |
817 | 43241 | prev_sf = sce->sf_idx[w*16+g]; | |
818 | 52710 | continue; | |
819 | } | ||
820 |
2/2✓ Branch 0 taken 43753 times.
✓ Branch 1 taken 42237 times.
|
85990 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
821 | 43753 | band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
822 | 43753 | sfb_energy += band->energy; | |
823 |
2/2✓ Branch 0 taken 42567 times.
✓ Branch 1 taken 1186 times.
|
43753 | spread = FFMIN(spread, band->spread); |
824 | 43753 | threshold += band->threshold; | |
825 |
2/2✓ Branch 0 taken 42237 times.
✓ Branch 1 taken 1516 times.
|
43753 | if (!w2) { |
826 | 42237 | min_energy = max_energy = band->energy; | |
827 | } else { | ||
828 |
2/2✓ Branch 0 taken 526 times.
✓ Branch 1 taken 990 times.
|
1516 | min_energy = FFMIN(min_energy, band->energy); |
829 |
2/2✓ Branch 0 taken 676 times.
✓ Branch 1 taken 840 times.
|
1516 | max_energy = FFMAX(max_energy, band->energy); |
830 | } | ||
831 | } | ||
832 | |||
833 | /* Ramps down at ~8000Hz and loosens the dist threshold */ | ||
834 | 42237 | dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias; | |
835 | |||
836 | /* PNS is acceptable when all of these are true: | ||
837 | * 1. high spread energy (noise-like band) | ||
838 | * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed) | ||
839 | * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS) | ||
840 | * | ||
841 | * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important) | ||
842 | */ | ||
843 |
4/4✓ Branch 0 taken 39218 times.
✓ Branch 1 taken 3019 times.
✓ Branch 3 taken 39192 times.
✓ Branch 4 taken 26 times.
|
42237 | if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) || |
844 |
8/8✓ Branch 0 taken 39192 times.
✓ Branch 1 taken 3019 times.
✓ Branch 2 taken 4276 times.
✓ Branch 3 taken 34916 times.
✓ Branch 4 taken 6471 times.
✓ Branch 5 taken 824 times.
✓ Branch 6 taken 40906 times.
✓ Branch 7 taken 481 times.
|
42211 | ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold || |
845 |
6/6✓ Branch 0 taken 38737 times.
✓ Branch 1 taken 2169 times.
✓ Branch 2 taken 34462 times.
✓ Branch 3 taken 4275 times.
✓ Branch 4 taken 21257 times.
✓ Branch 5 taken 13205 times.
|
40906 | (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) || |
846 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 27631 times.
|
27701 | min_energy < pns_transient_energy_r * max_energy ) { |
847 | 14606 | sce->pns_ener[w*16+g] = sfb_energy; | |
848 |
2/2✓ Branch 0 taken 13756 times.
✓ Branch 1 taken 850 times.
|
14606 | if (!sce->zeroes[w*16+g]) |
849 | 13756 | prev_sf = sce->sf_idx[w*16+g]; | |
850 | 14606 | continue; | |
851 | } | ||
852 | |||
853 |
2/2✓ Branch 0 taken 26697 times.
✓ Branch 1 taken 934 times.
|
27631 | pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread); |
854 | 27631 | noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */ | |
855 | 27631 | noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */ | |
856 |
2/2✓ Branch 0 taken 14932 times.
✓ Branch 1 taken 12699 times.
|
27631 | if (prev != -1000) { |
857 | 14932 | int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO; | |
858 |
2/4✓ Branch 0 taken 14932 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14932 times.
|
14932 | if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) { |
859 | ✗ | if (!sce->zeroes[w*16+g]) | |
860 | ✗ | prev_sf = sce->sf_idx[w*16+g]; | |
861 | ✗ | continue; | |
862 | } | ||
863 | } | ||
864 |
2/2✓ Branch 0 taken 27666 times.
✓ Branch 1 taken 27631 times.
|
55297 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
865 | float band_energy, scale, pns_senergy; | ||
866 | 27666 | const int start_c = (w+w2)*128+sce->ics.swb_offset[g]; | |
867 | 27666 | band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
868 |
2/2✓ Branch 0 taken 904188 times.
✓ Branch 1 taken 27666 times.
|
931854 | for (i = 0; i < sce->ics.swb_sizes[g]; i++) { |
869 | 904188 | s->random_state = lcg_random(s->random_state); | |
870 | 904188 | PNS[i] = s->random_state; | |
871 | } | ||
872 | 27666 | band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]); | |
873 | 27666 | scale = noise_amp/sqrtf(band_energy); | |
874 | 27666 | s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]); | |
875 | 27666 | pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]); | |
876 | 27666 | pns_energy += pns_senergy; | |
877 | 27666 | s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]); | |
878 | 27666 | s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]); | |
879 | 55332 | dist1 += quantize_band_cost(s, &sce->coeffs[start_c], | |
880 | NOR34, | ||
881 | 27666 | sce->ics.swb_sizes[g], | |
882 | 27666 | sce->sf_idx[(w+w2)*16+g], | |
883 | 27666 | sce->band_alt[(w+w2)*16+g], | |
884 | 27666 | lambda/band->threshold, INFINITY, NULL, NULL); | |
885 | /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */ | ||
886 | 27666 | dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold; | |
887 | } | ||
888 |
3/4✓ Branch 0 taken 27631 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5590 times.
✓ Branch 3 taken 22041 times.
|
27631 | if (g && sce->band_type[w*16+g-1] == NOISE_BT) { |
889 | 5590 | dist2 += 5; | |
890 | } else { | ||
891 | 22041 | dist2 += 9; | |
892 | } | ||
893 | 27631 | energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */ | |
894 | 27631 | sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy; | |
895 |
8/10✓ Branch 0 taken 25462 times.
✓ Branch 1 taken 2169 times.
✓ Branch 2 taken 21256 times.
✓ Branch 3 taken 4206 times.
✓ Branch 4 taken 20609 times.
✓ Branch 5 taken 647 times.
✓ Branch 6 taken 20609 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 20609 times.
|
27631 | if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) { |
896 | 6375 | sce->band_type[w*16+g] = NOISE_BT; | |
897 | 6375 | sce->zeroes[w*16+g] = 0; | |
898 | 6375 | prev = noise_sfi; | |
899 | } else { | ||
900 |
1/2✓ Branch 0 taken 21256 times.
✗ Branch 1 not taken.
|
21256 | if (!sce->zeroes[w*16+g]) |
901 | 21256 | prev_sf = sce->sf_idx[w*16+g]; | |
902 | } | ||
903 | } | ||
904 | } | ||
905 | 2003 | } | |
906 | |||
907 | 2003 | static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce) | |
908 | { | ||
909 | FFPsyBand *band; | ||
910 | int w, g, w2; | ||
911 | 2003 | int wlen = 1024 / sce->ics.num_windows; | |
912 | int bandwidth, cutoff; | ||
913 | 2003 | const float lambda = s->lambda; | |
914 | 2003 | const float freq_mult = avctx->sample_rate*0.5f/wlen; | |
915 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2003 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2003 | const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f)); |
916 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f); |
917 | |||
918 | 4006 | int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate | |
919 |
1/2✓ Branch 0 taken 2003 times.
✗ Branch 1 not taken.
|
2003 | / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels) |
920 | 2003 | * (lambda / 120.f); | |
921 | |||
922 | /** Keep this in sync with twoloop's cutoff selection */ | ||
923 | 2003 | float rate_bandwidth_multiplier = 1.5f; | |
924 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE) |
925 | ✗ | ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024) | |
926 | 2003 | : (avctx->bit_rate / avctx->ch_layout.nb_channels); | |
927 | |||
928 | 2003 | frame_bit_rate *= 1.15f; | |
929 | |||
930 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 1171 times.
|
2003 | if (avctx->cutoff > 0) { |
931 | 832 | bandwidth = avctx->cutoff; | |
932 | } else { | ||
933 |
5/10✓ Branch 0 taken 1171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1171 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1171 times.
✓ Branch 6 taken 1171 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1171 times.
|
1171 | bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate)); |
934 | } | ||
935 | |||
936 | 2003 | cutoff = bandwidth * 2 * wlen / avctx->sample_rate; | |
937 | |||
938 | 2003 | memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type)); | |
939 |
2/2✓ Branch 0 taken 2131 times.
✓ Branch 1 taken 2003 times.
|
4134 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
940 |
2/2✓ Branch 0 taken 94947 times.
✓ Branch 1 taken 2131 times.
|
97078 | for (g = 0; g < sce->ics.num_swb; g++) { |
941 | 94947 | float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f; | |
942 | 94947 | float min_energy = -1.0f, max_energy = 0.0f; | |
943 | 94947 | const int start = sce->ics.swb_offset[g]; | |
944 | 94947 | const float freq = start*freq_mult; | |
945 |
2/2✓ Branch 0 taken 46953 times.
✓ Branch 1 taken 47994 times.
|
94947 | const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f); |
946 |
4/4✓ Branch 0 taken 48836 times.
✓ Branch 1 taken 46111 times.
✓ Branch 2 taken 6599 times.
✓ Branch 3 taken 42237 times.
|
94947 | if (freq < NOISE_LOW_LIMIT || start >= cutoff) { |
947 | 52710 | sce->can_pns[w*16+g] = 0; | |
948 | 52710 | continue; | |
949 | } | ||
950 |
2/2✓ Branch 0 taken 43753 times.
✓ Branch 1 taken 42237 times.
|
85990 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
951 | 43753 | band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
952 | 43753 | sfb_energy += band->energy; | |
953 |
2/2✓ Branch 0 taken 42567 times.
✓ Branch 1 taken 1186 times.
|
43753 | spread = FFMIN(spread, band->spread); |
954 | 43753 | threshold += band->threshold; | |
955 |
2/2✓ Branch 0 taken 42237 times.
✓ Branch 1 taken 1516 times.
|
43753 | if (!w2) { |
956 | 42237 | min_energy = max_energy = band->energy; | |
957 | } else { | ||
958 |
2/2✓ Branch 0 taken 526 times.
✓ Branch 1 taken 990 times.
|
1516 | min_energy = FFMIN(min_energy, band->energy); |
959 |
2/2✓ Branch 0 taken 676 times.
✓ Branch 1 taken 840 times.
|
1516 | max_energy = FFMAX(max_energy, band->energy); |
960 | } | ||
961 | } | ||
962 | |||
963 | /* PNS is acceptable when all of these are true: | ||
964 | * 1. high spread energy (noise-like band) | ||
965 | * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed) | ||
966 | * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS) | ||
967 | */ | ||
968 | 42237 | sce->pns_ener[w*16+g] = sfb_energy; | |
969 |
6/6✓ Branch 0 taken 41403 times.
✓ Branch 1 taken 834 times.
✓ Branch 2 taken 40932 times.
✓ Branch 3 taken 471 times.
✓ Branch 4 taken 411 times.
✓ Branch 5 taken 40521 times.
|
42237 | if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) { |
970 | 1716 | sce->can_pns[w*16+g] = 0; | |
971 | } else { | ||
972 | 40521 | sce->can_pns[w*16+g] = 1; | |
973 | } | ||
974 | } | ||
975 | } | ||
976 | 2003 | } | |
977 | |||
978 | 630 | static void search_for_ms(AACEncContext *s, ChannelElement *cpe) | |
979 | { | ||
980 | 630 | int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side; | |
981 | uint8_t nextband0[128], nextband1[128]; | ||
982 | 630 | float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1; | |
983 | 630 | float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3; | |
984 | 630 | float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5; | |
985 | 630 | const float lambda = s->lambda; | |
986 |
2/2✓ Branch 0 taken 95 times.
✓ Branch 1 taken 535 times.
|
630 | const float mslambda = FFMIN(1.0f, lambda / 120.f); |
987 | 630 | SingleChannelElement *sce0 = &cpe->ch[0]; | |
988 | 630 | SingleChannelElement *sce1 = &cpe->ch[1]; | |
989 |
2/2✓ Branch 0 taken 521 times.
✓ Branch 1 taken 109 times.
|
630 | if (!cpe->common_window) |
990 | 521 | return; | |
991 | |||
992 | /** Scout out next nonzero bands */ | ||
993 | 109 | ff_init_nextband_map(sce0, nextband0); | |
994 | 109 | ff_init_nextband_map(sce1, nextband1); | |
995 | |||
996 | 109 | prev_mid = sce0->sf_idx[0]; | |
997 | 109 | prev_side = sce1->sf_idx[0]; | |
998 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 109 times.
|
232 | for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) { |
999 | 123 | start = 0; | |
1000 |
2/2✓ Branch 0 taken 5362 times.
✓ Branch 1 taken 123 times.
|
5485 | for (g = 0; g < sce0->ics.num_swb; g++) { |
1001 | 5362 | float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f; | |
1002 |
2/2✓ Branch 0 taken 4904 times.
✓ Branch 1 taken 458 times.
|
5362 | if (!cpe->is_mask[w*16+g]) |
1003 | 4904 | cpe->ms_mask[w*16+g] = 0; | |
1004 |
5/6✓ Branch 0 taken 3641 times.
✓ Branch 1 taken 1721 times.
✓ Branch 2 taken 3641 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3183 times.
✓ Branch 5 taken 458 times.
|
5362 | if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) { |
1005 | 3183 | float Mmax = 0.0f, Smax = 0.0f; | |
1006 | |||
1007 | /* Must compute mid/side SF and book for the whole window group */ | ||
1008 |
2/2✓ Branch 0 taken 3395 times.
✓ Branch 1 taken 3183 times.
|
6578 | for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { |
1009 |
2/2✓ Branch 0 taken 66844 times.
✓ Branch 1 taken 3395 times.
|
70239 | for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { |
1010 | 66844 | M[i] = (sce0->coeffs[start+(w+w2)*128+i] | |
1011 | 66844 | + sce1->coeffs[start+(w+w2)*128+i]) * 0.5; | |
1012 | 66844 | S[i] = M[i] | |
1013 | 66844 | - sce1->coeffs[start+(w+w2)*128+i]; | |
1014 | } | ||
1015 | 3395 | s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]); | |
1016 | 3395 | s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]); | |
1017 |
2/2✓ Branch 0 taken 66844 times.
✓ Branch 1 taken 3395 times.
|
70239 | for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) { |
1018 |
2/2✓ Branch 0 taken 50326 times.
✓ Branch 1 taken 16518 times.
|
66844 | Mmax = FFMAX(Mmax, M34[i]); |
1019 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66844 times.
|
66844 | Smax = FFMAX(Smax, S34[i]); |
1020 | } | ||
1021 | } | ||
1022 | |||
1023 |
2/2✓ Branch 0 taken 3294 times.
✓ Branch 1 taken 37 times.
|
3331 | for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) { |
1024 | 3294 | float dist1 = 0.0f, dist2 = 0.0f; | |
1025 | 3294 | int B0 = 0, B1 = 0; | |
1026 | int minidx; | ||
1027 | int mididx, sididx; | ||
1028 | int midcb, sidcb; | ||
1029 | |||
1030 | 3294 | minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]); | |
1031 | 3294 | mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512); | |
1032 | 3294 | sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512); | |
1033 |
4/4✓ Branch 0 taken 3063 times.
✓ Branch 1 taken 231 times.
✓ Branch 2 taken 3035 times.
✓ Branch 3 taken 28 times.
|
3294 | if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT |
1034 |
1/2✓ Branch 1 taken 3035 times.
✗ Branch 2 not taken.
|
3035 | && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g) |
1035 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3035 times.
|
3035 | || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) { |
1036 | /* scalefactor range violation, bad stuff, will decrease quality unacceptably */ | ||
1037 | ✗ | continue; | |
1038 | } | ||
1039 | |||
1040 | 3294 | midcb = find_min_book(Mmax, mididx); | |
1041 | 3294 | sidcb = find_min_book(Smax, sididx); | |
1042 | |||
1043 | /* No CB can be zero */ | ||
1044 | 3294 | midcb = FFMAX(1,midcb); | |
1045 | 3294 | sidcb = FFMAX(1,sidcb); | |
1046 | |||
1047 |
2/2✓ Branch 0 taken 3506 times.
✓ Branch 1 taken 3294 times.
|
6800 | for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { |
1048 | 3506 | FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
1049 | 3506 | FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
1050 |
2/2✓ Branch 0 taken 1433 times.
✓ Branch 1 taken 2073 times.
|
3506 | float minthr = FFMIN(band0->threshold, band1->threshold); |
1051 | int b1,b2,b3,b4; | ||
1052 |
2/2✓ Branch 0 taken 67732 times.
✓ Branch 1 taken 3506 times.
|
71238 | for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { |
1053 | 67732 | M[i] = (sce0->coeffs[start+(w+w2)*128+i] | |
1054 | 67732 | + sce1->coeffs[start+(w+w2)*128+i]) * 0.5; | |
1055 | 67732 | S[i] = M[i] | |
1056 | 67732 | - sce1->coeffs[start+(w+w2)*128+i]; | |
1057 | } | ||
1058 | |||
1059 | 3506 | s->aacdsp.abs_pow34(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]); | |
1060 | 3506 | s->aacdsp.abs_pow34(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]); | |
1061 | 3506 | s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]); | |
1062 | 3506 | s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]); | |
1063 | 7012 | dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128], | |
1064 | L34, | ||
1065 | 3506 | sce0->ics.swb_sizes[g], | |
1066 | 3506 | sce0->sf_idx[w*16+g], | |
1067 | 3506 | sce0->band_type[w*16+g], | |
1068 | 3506 | lambda / (band0->threshold + FLT_MIN), INFINITY, &b1, NULL); | |
1069 | 7012 | dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128], | |
1070 | R34, | ||
1071 | 3506 | sce1->ics.swb_sizes[g], | |
1072 | 3506 | sce1->sf_idx[w*16+g], | |
1073 | 3506 | sce1->band_type[w*16+g], | |
1074 | 3506 | lambda / (band1->threshold + FLT_MIN), INFINITY, &b2, NULL); | |
1075 | 7012 | dist2 += quantize_band_cost(s, M, | |
1076 | M34, | ||
1077 | 3506 | sce0->ics.swb_sizes[g], | |
1078 | mididx, | ||
1079 | midcb, | ||
1080 | 3506 | lambda / (minthr + FLT_MIN), INFINITY, &b3, NULL); | |
1081 | 7012 | dist2 += quantize_band_cost(s, S, | |
1082 | S34, | ||
1083 | 3506 | sce1->ics.swb_sizes[g], | |
1084 | sididx, | ||
1085 | sidcb, | ||
1086 | 3506 | mslambda / (minthr * bmax + FLT_MIN), INFINITY, &b4, NULL); | |
1087 | 3506 | B0 += b1+b2; | |
1088 | 3506 | B1 += b3+b4; | |
1089 | 3506 | dist1 -= b1+b2; | |
1090 | 3506 | dist2 -= b3+b4; | |
1091 | } | ||
1092 |
3/4✓ Branch 0 taken 3294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2887 times.
✓ Branch 3 taken 407 times.
|
3294 | cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0; |
1093 |
2/2✓ Branch 0 taken 2887 times.
✓ Branch 1 taken 407 times.
|
3294 | if (cpe->ms_mask[w*16+g]) { |
1094 |
2/4✓ Branch 0 taken 2887 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2887 times.
✗ Branch 3 not taken.
|
2887 | if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) { |
1095 | 2887 | sce0->sf_idx[w*16+g] = mididx; | |
1096 | 2887 | sce1->sf_idx[w*16+g] = sididx; | |
1097 | 2887 | sce0->band_type[w*16+g] = midcb; | |
1098 | 2887 | sce1->band_type[w*16+g] = sidcb; | |
1099 | ✗ | } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) { | |
1100 | /* ms_mask unneeded, and it confuses some decoders */ | ||
1101 | ✗ | cpe->ms_mask[w*16+g] = 0; | |
1102 | } | ||
1103 | 2887 | break; | |
1104 |
2/2✓ Branch 0 taken 259 times.
✓ Branch 1 taken 148 times.
|
407 | } else if (B1 > B0) { |
1105 | /* More boost won't fix this */ | ||
1106 | 259 | break; | |
1107 | } | ||
1108 | } | ||
1109 | } | ||
1110 |
4/4✓ Branch 0 taken 3641 times.
✓ Branch 1 taken 1721 times.
✓ Branch 2 taken 3410 times.
✓ Branch 3 taken 231 times.
|
5362 | if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT) |
1111 | 3410 | prev_mid = sce0->sf_idx[w*16+g]; | |
1112 |
6/6✓ Branch 0 taken 3641 times.
✓ Branch 1 taken 1721 times.
✓ Branch 2 taken 3183 times.
✓ Branch 3 taken 458 times.
✓ Branch 4 taken 2950 times.
✓ Branch 5 taken 233 times.
|
5362 | if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT) |
1113 | 2950 | prev_side = sce1->sf_idx[w*16+g]; | |
1114 | 5362 | start += sce0->ics.swb_sizes[g]; | |
1115 | } | ||
1116 | } | ||
1117 | } | ||
1118 | |||
1119 | const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = { | ||
1120 | [AAC_CODER_ANMR] = { | ||
1121 | search_for_quantizers_anmr, | ||
1122 | encode_window_bands_info, | ||
1123 | quantize_and_encode_band, | ||
1124 | ff_aac_encode_tns_info, | ||
1125 | ff_aac_encode_ltp_info, | ||
1126 | ff_aac_encode_main_pred, | ||
1127 | ff_aac_adjust_common_pred, | ||
1128 | ff_aac_adjust_common_ltp, | ||
1129 | ff_aac_apply_main_pred, | ||
1130 | ff_aac_apply_tns, | ||
1131 | ff_aac_update_ltp, | ||
1132 | ff_aac_ltp_insert_new_frame, | ||
1133 | set_special_band_scalefactors, | ||
1134 | search_for_pns, | ||
1135 | mark_pns, | ||
1136 | ff_aac_search_for_tns, | ||
1137 | ff_aac_search_for_ltp, | ||
1138 | search_for_ms, | ||
1139 | ff_aac_search_for_is, | ||
1140 | ff_aac_search_for_pred, | ||
1141 | }, | ||
1142 | [AAC_CODER_TWOLOOP] = { | ||
1143 | search_for_quantizers_twoloop, | ||
1144 | codebook_trellis_rate, | ||
1145 | quantize_and_encode_band, | ||
1146 | ff_aac_encode_tns_info, | ||
1147 | ff_aac_encode_ltp_info, | ||
1148 | ff_aac_encode_main_pred, | ||
1149 | ff_aac_adjust_common_pred, | ||
1150 | ff_aac_adjust_common_ltp, | ||
1151 | ff_aac_apply_main_pred, | ||
1152 | ff_aac_apply_tns, | ||
1153 | ff_aac_update_ltp, | ||
1154 | ff_aac_ltp_insert_new_frame, | ||
1155 | set_special_band_scalefactors, | ||
1156 | search_for_pns, | ||
1157 | mark_pns, | ||
1158 | ff_aac_search_for_tns, | ||
1159 | ff_aac_search_for_ltp, | ||
1160 | search_for_ms, | ||
1161 | ff_aac_search_for_is, | ||
1162 | ff_aac_search_for_pred, | ||
1163 | }, | ||
1164 | [AAC_CODER_FAST] = { | ||
1165 | search_for_quantizers_fast, | ||
1166 | codebook_trellis_rate, | ||
1167 | quantize_and_encode_band, | ||
1168 | ff_aac_encode_tns_info, | ||
1169 | ff_aac_encode_ltp_info, | ||
1170 | ff_aac_encode_main_pred, | ||
1171 | ff_aac_adjust_common_pred, | ||
1172 | ff_aac_adjust_common_ltp, | ||
1173 | ff_aac_apply_main_pred, | ||
1174 | ff_aac_apply_tns, | ||
1175 | ff_aac_update_ltp, | ||
1176 | ff_aac_ltp_insert_new_frame, | ||
1177 | set_special_band_scalefactors, | ||
1178 | search_for_pns, | ||
1179 | mark_pns, | ||
1180 | ff_aac_search_for_tns, | ||
1181 | ff_aac_search_for_ltp, | ||
1182 | search_for_ms, | ||
1183 | ff_aac_search_for_is, | ||
1184 | ff_aac_search_for_pred, | ||
1185 | }, | ||
1186 | }; | ||
1187 |