/proc/self/cwd/external/faac/libfaad/sbr.c
Line | Count | Source |
1 | | /* |
2 | | * FAAD - Freeware Advanced Audio Decoder |
3 | | * Copyright (C) 2026 Nils Schimmelmann |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | */ |
15 | | |
16 | | #include "faad_internal.h" |
17 | | #include "sbr_tables.h" |
18 | | #include "fft.h" |
19 | | |
20 | | #ifndef FAAD_DISABLE_SBR |
21 | 3.21k | #define SBR_NOISE_FLOOR_OFFSET 6 |
22 | | |
23 | | /* Limiter gains (§4.6.18.7.5), amplitude domain: -3 dB, 0 dB, +3 dB, none. */ |
24 | | static const float sbr_lim_gain[4] = { 0.70794578f, 1.0f, 1.41253754f, 1e10f }; |
25 | | |
26 | | /* Gain smoothing window (§4.6.18.7.6). */ |
27 | | static const float sbr_h_smooth[5] = { |
28 | | 0.33333333333333f, 0.30150283239582f, 0.21816949906249f, |
29 | | 0.11516383427084f, 0.03183050093751f |
30 | | }; |
31 | | |
32 | | /* Twiddles for the FFT-based QMF banks */ |
33 | | |
34 | | static float ana_pre_c[64], ana_pre_s[64]; /* exp(+j*pi*n/64) */ |
35 | | static float ana_post_c[32], ana_post_s[32]; /* exp(-j*pi*(k+1/2)/128) */ |
36 | | static float syn_rot_c[32], syn_rot_s[32]; /* exp(-j*pi*(n+1/8)/64) */ |
37 | | static float syn_a_c[32], syn_a_s[32]; /* the same, over 128 */ |
38 | | static float syn_b_c[32], syn_b_s[32]; /* the same times exp(+j*2*pi*k/64), over 128 */ |
39 | | |
40 | | /* The shared table holds the first 384 prototype taps; the rest mirror them, |
41 | | * with a sign flip at the taps 384 and 512. */ |
42 | | static float qmf_win[640]; |
43 | | void init_qmf_twiddles(void) |
44 | 2 | { |
45 | 2 | fft_init(); |
46 | 1.28k | for (int n = 0; n < 640; n++) |
47 | 1.28k | qmf_win[n] = n < 384 ? qmf_c[n] : (n == 384 || n == 512 ? -qmf_c[640 - n] : qmf_c[640 - n]); |
48 | 130 | for (int n = 0; n < 64; n++) { |
49 | 128 | ana_pre_c[n] = (float)cos(M_PI * n / 64.0); |
50 | 128 | ana_pre_s[n] = (float)sin(M_PI * n / 64.0); |
51 | 128 | } |
52 | 66 | for (int k = 0; k < 32; k++) { |
53 | 64 | ana_post_c[k] = (float)cos(-0.5 * M_PI * (k + 0.5) / 64.0); |
54 | 64 | ana_post_s[k] = (float)sin(-0.5 * M_PI * (k + 0.5) / 64.0); |
55 | 64 | } |
56 | 66 | for (int n = 0; n < 32; n++) { |
57 | 64 | double a = -M_PI * (n + 0.125) / 64.0, b = a + 2.0 * M_PI * n / 64.0; |
58 | 64 | syn_rot_c[n] = (float)cos(a); |
59 | 64 | syn_rot_s[n] = (float)sin(a); |
60 | 64 | syn_a_c[n] = (float)(cos(a) / 128.0); |
61 | 64 | syn_a_s[n] = (float)(sin(a) / 128.0); |
62 | 64 | syn_b_c[n] = (float)(cos(b) / 128.0); |
63 | 64 | syn_b_s[n] = (float)(sin(b) / 128.0); |
64 | 64 | } |
65 | 2 | } |
66 | | |
67 | | /* QMF banks (§4.6.18.4, §4.6.18.8) */ |
68 | | |
69 | | /* Push one 32-sample block into the analysis delay line and window it: |
70 | | * u(n) = sum_j x(n + 64j) c(2(n + 64j)), newest sample first. The line is |
71 | | * a ring of ten 32-sample blocks. Resolve tap pointers before accumulation. */ |
72 | | static void qmf_analysis_window(SBRChannel *ch, const float *in, float u[64]) |
73 | 346k | { |
74 | 346k | int pos = ch->qmf_x_pos - 32; |
75 | 346k | if (pos < 0) pos += 320; |
76 | 346k | ch->qmf_x_pos = pos; |
77 | 346k | float *x = ch->qmf_x + ch->qmf_x_pos; |
78 | 11.4M | for (int n = 0; n < 32; n++) x[n] = in[31 - n]; |
79 | 1.03M | for (int half = 0; half < 2; half++) { |
80 | 692k | const float *run[5]; |
81 | 4.15M | for (int j = 0; j < 5; j++) { |
82 | 3.46M | int offset = pos + half * 32 + 64 * j; |
83 | 3.46M | if (offset >= 320) offset -= 320; |
84 | 3.46M | run[j] = ch->qmf_x + offset; |
85 | 3.46M | } |
86 | | /* Same tap order and zero start as a loop over j, without re-deriving pointers per output. */ |
87 | 692k | const float *cb = qmf_win + 64 * half; |
88 | 692k | const float *r0 = run[0], *r1 = run[1], *r2 = run[2], *r3 = run[3], *r4 = run[4]; |
89 | 22.8M | for (int n = 0; n < 32; n++) { |
90 | 22.1M | float acc = 0.0f; |
91 | 22.1M | acc += r0[n] * cb[2 * n]; |
92 | 22.1M | acc += r1[n] * cb[2 * n + 128]; |
93 | 22.1M | acc += r2[n] * cb[2 * n + 256]; |
94 | 22.1M | acc += r3[n] * cb[2 * n + 384]; |
95 | 22.1M | acc += r4[n] * cb[2 * n + 512]; |
96 | 22.1M | u[half * 32 + n] = acc; |
97 | 22.1M | } |
98 | 692k | } |
99 | 346k | } |
100 | | |
101 | | /* 32-band analysis of two consecutive slots (64 new samples). The |
102 | | * decimated bank is the lower half of the 64-band one, so its phase origin |
103 | | * sits a quarter sample in: X(k) = 2 sum_n u(n) exp(j*pi/64*(k+1/2)(2n-1/2)), |
104 | | * the conjugate of a 64-point DFT of u(n)*exp(-j*pi*n/64), rotated; the |
105 | | * factor 2 restores the passband gain the decimated prototype c(2n) |
106 | | * halves. u is real, so that DFT is conjugate-symmetric about bin 63/2 and |
107 | | * one transform of u0 + j*u1 carries both slots. */ |
108 | | static void qmf_analysis_pair(SBRChannel *ch, const float *in, float out0[32][2], float out1[32][2]) |
109 | 173k | { |
110 | 173k | float u0[64], u1[64]; |
111 | 173k | qmf_analysis_window(ch, in, u0); |
112 | 173k | qmf_analysis_window(ch, in + 32, u1); |
113 | | |
114 | 173k | float z[128], w[128]; |
115 | 11.2M | for (int n = 0; n < 64; n++) { |
116 | 11.0M | z[n] = u0[n] * ana_pre_c[n] + u1[n] * ana_pre_s[n]; |
117 | 11.0M | z[64 + n] = u1[n] * ana_pre_c[n] - u0[n] * ana_pre_s[n]; |
118 | 11.0M | } |
119 | 173k | fft(z, w, 6); |
120 | 173k | const float *re = w, *im = w + 64; |
121 | 5.71M | for (int k = 0; k < 32; k++) { |
122 | | /* 2 conj(W0(k)) and 2 conj(W1(k)) from W(k) and W(63 - k) */ |
123 | 5.53M | float ar = re[k] + re[63 - k], ai = im[63 - k] - im[k]; |
124 | 5.53M | float br = im[k] + im[63 - k], bi = re[k] - re[63 - k]; |
125 | 5.53M | out0[k][0] = ar * ana_post_c[k] - ai * ana_post_s[k]; |
126 | 5.53M | out0[k][1] = ar * ana_post_s[k] + ai * ana_post_c[k]; |
127 | 5.53M | out1[k][0] = br * ana_post_c[k] - bi * ana_post_s[k]; |
128 | 5.53M | out1[k][1] = br * ana_post_s[k] + bi * ana_post_c[k]; |
129 | 5.53M | } |
130 | 173k | } |
131 | | |
132 | | /* One pass with the accumulator in a register, so each output is stored once. restrict sits on |
133 | | * parameters because compilers honour it through inlining, keeping the loop vectorisable. */ |
134 | | static inline void synth_window10(float * restrict out, |
135 | | const float * restrict r0, const float * restrict r1, const float * restrict r2, |
136 | | const float * restrict r3, const float * restrict r4, const float * restrict r5, |
137 | | const float * restrict r6, const float * restrict r7, const float * restrict r8, |
138 | | const float * restrict r9, const float * restrict w) |
139 | 355k | { |
140 | 23.1M | for (int n = 0; n < 64; n++) { |
141 | 22.7M | float a = r0[n] * w[n]; |
142 | 22.7M | a += r1[n] * w[64 + n]; |
143 | 22.7M | a += r2[n] * w[128 + n]; |
144 | 22.7M | a += r3[n] * w[192 + n]; |
145 | 22.7M | a += r4[n] * w[256 + n]; |
146 | 22.7M | a += r5[n] * w[320 + n]; |
147 | 22.7M | a += r6[n] * w[384 + n]; |
148 | 22.7M | a += r7[n] * w[448 + n]; |
149 | 22.7M | a += r8[n] * w[512 + n]; |
150 | 22.7M | a += r9[n] * w[576 + n]; |
151 | 22.7M | out[n] = a; |
152 | 22.7M | } |
153 | 355k | } |
154 | | |
155 | | /* 64-band synthesis of one slot: 64 output samples. |
156 | | * v(n) = 1/64 sum_k Re{X(k) exp(j*pi/64*(k+1/2)(n-127.5))}, n = 0..127, |
157 | | * which is C(n-128) - S(n-128) for C the DCT-IV of Re X and S the DST-IV |
158 | | * of Im X; their symmetries give all 128 samples from the 64 of each. The |
159 | | * DST-IV is a DCT-IV of the reversed input with alternate signs, and each |
160 | | * DCT-IV of 64 is a 32-point complex FFT between two rotations, so both |
161 | | * FFTs ride one 64-point transform: the first on the even inputs, the |
162 | | * second on the odd, separated by one butterfly. The delay line is a ring |
163 | | * of ten 128-sample blocks; the newest block starts at qmf_v_pos. */ |
164 | | static void qmf_synthesis_slot(SBRChannel *ch, float X[64][2], float *out) |
165 | 355k | { |
166 | 355k | float z[128], w[128]; |
167 | 11.7M | for (int n = 0; n < 32; n++) { |
168 | 11.3M | float c = syn_rot_c[n], s = syn_rot_s[n]; |
169 | 11.3M | float ar = X[2 * n][0], ai = X[63 - 2 * n][0]; |
170 | 11.3M | float br = X[63 - 2 * n][1], bi = X[2 * n][1]; |
171 | 11.3M | z[2 * n] = ar * c - ai * s; |
172 | 11.3M | z[64 + 2 * n] = ar * s + ai * c; |
173 | 11.3M | z[2 * n + 1] = br * c - bi * s; |
174 | 11.3M | z[64 + 2 * n + 1] = br * s + bi * c; |
175 | 11.3M | } |
176 | 355k | fft(z, w, 6); |
177 | | |
178 | 355k | int vpos = ch->qmf_v_pos - 128; |
179 | 355k | if (vpos < 0) vpos += 1280; |
180 | 355k | ch->qmf_v_pos = vpos; |
181 | 355k | float *v = ch->qmf_v + ch->qmf_v_pos; |
182 | 355k | const float *wr = w, *wi = w + 64; |
183 | 11.7M | for (int k = 0; k < 32; k++) { |
184 | 11.3M | float pr = wr[k] + wr[k + 32], pi = wi[k] + wi[k + 32]; |
185 | 11.3M | float mr = wr[k] - wr[k + 32], mi = wi[k] - wi[k + 32]; |
186 | 11.3M | float cr = pr * syn_a_c[k] - pi * syn_a_s[k], ci = pr * syn_a_s[k] + pi * syn_a_c[k]; |
187 | 11.3M | float dr = mr * syn_b_c[k] - mi * syn_b_s[k], di = mr * syn_b_s[k] + mi * syn_b_c[k]; |
188 | 11.3M | v[2 * k] = dr - cr; |
189 | 11.3M | v[127 - 2 * k] = cr + dr; |
190 | 11.3M | v[63 - 2 * k] = ci + di; |
191 | 11.3M | v[64 + 2 * k] = di - ci; |
192 | 11.3M | } |
193 | | |
194 | | /* Ten 64-tap runs, each inside one 128-sample block, so no run wraps. */ |
195 | 355k | const float *run[10]; |
196 | 2.13M | for (int i = 0; i < 5; i++) { |
197 | 1.77M | int pos0 = ch->qmf_v_pos + 256 * i; |
198 | 1.77M | pos0 -= (pos0 >= 1280) ? 1280 : 0; |
199 | 1.77M | int pos1 = pos0 + 192; |
200 | 1.77M | pos1 -= (pos1 >= 1280) ? 1280 : 0; |
201 | 1.77M | run[2 * i] = ch->qmf_v + pos0; |
202 | 1.77M | run[2 * i + 1] = ch->qmf_v + pos1; |
203 | 1.77M | } |
204 | 355k | synth_window10(out, run[0], run[1], run[2], run[3], run[4], run[5], run[6], run[7], run[8], run[9], qmf_win); |
205 | 355k | } |
206 | | |
207 | | |
208 | | /* Frequency band tables (§4.6.18.3.2) and patches (§4.6.18.6.3) */ |
209 | | |
210 | 730k | static int cmp_u8(const void *a, const void *b) { return (int)*(const uint8_t *)a - (int)*(const uint8_t *)b; } |
211 | | |
212 | 437k | static int sbr_round(double v) { return (int)floor(v + 0.5); } |
213 | | |
214 | | static int sbr_start_band(uint32_t sr, int start_freq) |
215 | 20.4k | { |
216 | 20.4k | int row = (sr <= 16000) ? 0 : (sr <= 22050) ? 1 : (sr <= 24000) ? 2 : (sr <= 32000) ? 3 : (sr <= 64000) ? 4 : 5; |
217 | 20.4k | int temp = (sr < 32000) ? 3000 : (sr < 64000) ? 4000 : 5000; |
218 | 20.4k | int start_min = (int)(((temp << 7) + (sr >> 1)) / sr); |
219 | 20.4k | int k0 = start_min + sbr_offset[row][start_freq & 15]; |
220 | 20.4k | return k0 < 1 ? 1 : k0 > 63 ? 63 : k0; |
221 | 20.4k | } |
222 | | |
223 | | static int sbr_stop_band(uint32_t sr, int k0, int stop_freq) |
224 | 20.4k | { |
225 | 20.4k | if (stop_freq == 15) return k0 * 3 < 64 ? k0 * 3 : 64; |
226 | 18.8k | if (stop_freq == 14) return k0 * 2 < 64 ? k0 * 2 : 64; |
227 | 18.5k | int temp = (sr < 32000) ? 3000 : (sr < 64000) ? 4000 : 5000; |
228 | 18.5k | int stop_min = (int)(((temp << 8) + (sr >> 1)) / sr); |
229 | 18.5k | uint8_t dk[13]; |
230 | 18.5k | int prev = stop_min; |
231 | 260k | for (int i = 0; i < 13; i++) { |
232 | 241k | int cur = sbr_round(stop_min * pow(64.0 / stop_min, (i + 1) / 13.0)); |
233 | 241k | dk[i] = (uint8_t)(cur - prev); |
234 | 241k | prev = cur; |
235 | 241k | } |
236 | 18.5k | qsort(dk, 13, 1, cmp_u8); |
237 | 18.5k | int k2 = stop_min; |
238 | 59.5k | for (int i = 0; i < stop_freq; i++) k2 += dk[i]; |
239 | 18.5k | return k2 < 64 ? k2 : 64; |
240 | 18.8k | } |
241 | | |
242 | | /* Master table for bs_freq_scale > 0: log-spaced bands, two regions above |
243 | | * a 2.2449 ratio, the upper one warped by alter_scale. */ |
244 | | static int sbr_master_log(SBRElement *el, int k0, int k2) |
245 | 11.7k | { |
246 | 11.7k | int bands = 14 - 2 * el->freq_scale; /* 12, 10, 8 */ |
247 | 11.7k | double warp = el->alter_scale ? 1.3 : 1.0; |
248 | 11.7k | int two_regions = ((double)k2 / k0 > 2.2449); |
249 | 11.7k | int k1 = two_regions ? 2 * k0 : k2; |
250 | | |
251 | 11.7k | int nb0 = 2 * sbr_round(bands * log2((double)k1 / k0) / 2.0); |
252 | 11.7k | if (nb0 < 1 || nb0 > SBR_MAX_BANDS) return -1; |
253 | 11.6k | uint8_t dk0[SBR_MAX_BANDS]; |
254 | 86.9k | for (int i = 0; i < nb0; i++) |
255 | 75.2k | dk0[i] = (uint8_t)(sbr_round(k0 * pow((double)k1 / k0, (i + 1.0) / nb0)) - sbr_round(k0 * pow((double)k1 / k0, (double)i / nb0))); |
256 | 11.6k | qsort(dk0, nb0, 1, cmp_u8); |
257 | 11.6k | if (dk0[0] == 0) return -1; |
258 | 10.8k | el->f_master[0] = (uint8_t)k0; |
259 | 78.3k | for (int i = 0; i < nb0; i++) el->f_master[i + 1] = (uint8_t)(el->f_master[i] + dk0[i]); |
260 | 10.8k | int n = nb0; |
261 | | |
262 | 10.8k | if (two_regions) { |
263 | 2.08k | int nb1 = 2 * sbr_round(bands * log2((double)k2 / k1) / (2.0 * warp)); |
264 | 2.08k | if (nb1 < 1 || n + nb1 > SBR_MAX_BANDS) return -1; |
265 | 2.08k | uint8_t dk1[SBR_MAX_BANDS]; |
266 | 10.7k | for (int i = 0; i < nb1; i++) |
267 | 8.69k | dk1[i] = (uint8_t)(sbr_round(k1 * pow((double)k2 / k1, (i + 1.0) / nb1)) - sbr_round(k1 * pow((double)k2 / k1, (double)i / nb1))); |
268 | 2.08k | qsort(dk1, nb1, 1, cmp_u8); |
269 | 2.08k | if (dk1[0] < dk0[nb0 - 1]) { |
270 | 136 | int change = dk0[nb0 - 1] - dk1[0]; |
271 | 136 | int half = (dk1[nb1 - 1] - dk1[0]) / 2; |
272 | 136 | if (change > half) change = half; |
273 | 136 | dk1[0] = (uint8_t)(dk1[0] + change); |
274 | 136 | dk1[nb1 - 1] = (uint8_t)(dk1[nb1 - 1] - change); |
275 | 136 | } |
276 | 2.08k | qsort(dk1, nb1, 1, cmp_u8); |
277 | 2.08k | if (dk1[0] == 0) return -1; |
278 | 10.7k | for (int i = 0; i < nb1; i++) el->f_master[n + i + 1] = (uint8_t)(el->f_master[n + i] + dk1[i]); |
279 | 2.08k | n += nb1; |
280 | 2.08k | } |
281 | 10.8k | return n; |
282 | 10.8k | } |
283 | | |
284 | | /* Master table for bs_freq_scale == 0: uniform spacing, the remainder folded |
285 | | * into the outermost bands. */ |
286 | | static int sbr_master_linear(SBRElement *el, int k0, int k2) |
287 | 4.80k | { |
288 | 4.80k | int dk = el->alter_scale ? 2 : 1; |
289 | 4.80k | int nb = el->alter_scale ? 2 * sbr_round((k2 - k0) / 4.0) : 2 * ((k2 - k0) / 2); |
290 | 4.80k | if (nb < 1 || nb > SBR_MAX_BANDS) return -1; |
291 | 4.76k | int diff = k2 - (k0 + nb * dk); |
292 | 4.76k | uint8_t vdk[SBR_MAX_BANDS]; |
293 | 60.4k | for (int i = 0; i < nb; i++) vdk[i] = (uint8_t)dk; |
294 | 4.76k | int k = diff < 0 ? 0 : nb - 1, incr = diff < 0 ? 1 : -1; |
295 | 6.17k | while (diff != 0) { |
296 | 1.41k | vdk[k] = (uint8_t)(vdk[k] - incr); |
297 | 1.41k | k += incr; |
298 | 1.41k | diff += incr; |
299 | 1.41k | } |
300 | 4.76k | el->f_master[0] = (uint8_t)k0; |
301 | 60.4k | for (int i = 0; i < nb; i++) el->f_master[i + 1] = (uint8_t)(el->f_master[i] + vdk[i]); |
302 | 4.76k | return nb; |
303 | 4.80k | } |
304 | | |
305 | | static bool sbr_build_patches(SBRElement *el, uint32_t sr) |
306 | 15.2k | { |
307 | 15.2k | int k0 = el->k0, kx = el->kx, M = el->M; |
308 | 15.2k | int msb = k0, usb = kx, np = 0; |
309 | 15.2k | int goal_sb = (int)((2048000 + (sr >> 1)) / sr); |
310 | 15.2k | int k; |
311 | 15.2k | if (goal_sb < kx + M) { |
312 | 26.6k | for (k = 0; k < el->n_master && el->f_master[k] < goal_sb; k++) ; |
313 | 11.8k | } else { |
314 | 11.8k | k = el->n_master; |
315 | 11.8k | } |
316 | 15.2k | int sb; |
317 | | /* Passes that add no patch only reset msb, so a malformed table can |
318 | | * revisit the same state forever; a valid one needs few passes. */ |
319 | 28.2k | for (int pass = 0;; pass++) { |
320 | 28.2k | if (pass >= 4 * SBR_MAX_PATCHES) return false; |
321 | 28.0k | int j = k + 1, odd; |
322 | 78.6k | do { |
323 | 78.6k | j--; |
324 | 78.6k | sb = el->f_master[j]; |
325 | 78.6k | odd = (sb - 2 + k0) & 1; |
326 | 78.6k | } while (sb > k0 - 1 + msb - odd && j > 0); |
327 | | |
328 | 28.0k | if (np >= SBR_MAX_PATCHES) return false; |
329 | 27.8k | int num = sb - usb > 0 ? sb - usb : 0; |
330 | 27.8k | el->patch_num[np] = (uint8_t)num; |
331 | 27.8k | el->patch_start[np] = (uint8_t)(k0 - odd - num); |
332 | 27.8k | if (num > 0) { |
333 | 22.5k | usb = sb; |
334 | 22.5k | msb = sb; |
335 | 22.5k | np++; |
336 | 22.5k | } else { |
337 | 5.35k | msb = kx; |
338 | 5.35k | } |
339 | 27.8k | if (el->f_master[k] - sb < 3) k = el->n_master; |
340 | 27.8k | if (sb == kx + M) break; |
341 | 27.8k | } |
342 | | |
343 | 14.9k | if (np > 1 && el->patch_num[np - 1] < 3) np--; |
344 | 14.9k | el->num_patches = (uint8_t)np; |
345 | 14.9k | return np > 0; |
346 | 15.2k | } |
347 | | |
348 | | static bool sbr_build_limiter(SBRElement *el) |
349 | 14.9k | { |
350 | 14.9k | if (el->limiter_bands == 0) { |
351 | 396 | el->f_lim[0] = el->f_low[0]; |
352 | 396 | el->f_lim[1] = el->f_low[el->n_low]; |
353 | 396 | el->n_lim = 1; |
354 | 396 | return true; |
355 | 396 | } |
356 | 14.5k | static const double lim_bands[3] = { 1.2, 2.0, 3.0 }; |
357 | 14.5k | double lb = lim_bands[el->limiter_bands - 1]; |
358 | | |
359 | 14.5k | uint8_t borders[SBR_MAX_PATCHES + 1]; |
360 | 14.5k | int nb = 0; |
361 | 14.5k | borders[nb++] = el->kx; |
362 | 34.4k | for (int i = 0; i < el->num_patches; i++) borders[nb] = (uint8_t)(borders[nb - 1] + el->patch_num[i]), nb++; |
363 | | |
364 | 14.5k | uint8_t lim[SBR_MAX_LIM + 1]; |
365 | 14.5k | int n = 0; |
366 | 83.1k | for (int i = 0; i <= el->n_low; i++) lim[n++] = el->f_low[i]; |
367 | 48.9k | for (int i = 0; i < nb; i++) lim[n++] = borders[i]; |
368 | 14.5k | qsort(lim, n, 1, cmp_u8); |
369 | | |
370 | 14.5k | int k = 1; |
371 | 103k | while (k < n) { |
372 | 88.4k | double oct = log2((double)lim[k] / lim[k - 1]); |
373 | 88.4k | if (oct * lb < 0.49) { |
374 | | /* Too narrow: drop the border that is not a patch boundary; |
375 | | * two adjacent patch boundaries both stay. */ |
376 | 67.5k | int rm; |
377 | 67.5k | if (lim[k] == lim[k - 1]) rm = k; |
378 | 35.6k | else { |
379 | 35.6k | bool kb = false, kb1 = false; |
380 | 135k | for (int i = 0; i < nb; i++) { kb |= (borders[i] == lim[k]); kb1 |= (borders[i] == lim[k - 1]); } |
381 | 35.6k | rm = !kb ? k : !kb1 ? k - 1 : -1; |
382 | 35.6k | } |
383 | 67.5k | if (rm < 0) { k++; continue; } |
384 | 61.3k | memmove(lim + rm, lim + rm + 1, (size_t)(n - rm - 1)); |
385 | 61.3k | n--; |
386 | 61.3k | } else { |
387 | 20.8k | k++; |
388 | 20.8k | } |
389 | 88.4k | } |
390 | 14.5k | el->n_lim = (uint8_t)(n - 1); |
391 | 14.5k | memcpy(el->f_lim, lim, (size_t)n); |
392 | 14.5k | return el->n_lim >= 1; |
393 | 14.9k | } |
394 | | |
395 | | static bool sbr_build_tables(SBRElement *el, uint32_t sr) |
396 | 20.4k | { |
397 | | /* the band tables divide by sr; an explicit ASC rate can be anything */ |
398 | 20.4k | if (sr < 8000 || sr > 192000) return false; |
399 | 20.4k | int k0 = sbr_start_band(sr, el->start_freq); |
400 | 20.4k | int k2 = sbr_stop_band(sr, k0, el->stop_freq); |
401 | 20.4k | int max_span = (sr <= 32000) ? 48 : (sr <= 44100) ? 35 : 32; |
402 | 20.4k | if (k2 <= k0 || k2 - k0 > max_span) return false; |
403 | 16.5k | el->k0 = (uint8_t)k0; |
404 | 16.5k | el->k2 = (uint8_t)k2; |
405 | | |
406 | 16.5k | int n = el->freq_scale ? sbr_master_log(el, k0, k2) : sbr_master_linear(el, k0, k2); |
407 | 16.5k | if (n < 1 || el->xover_band >= n) return false; |
408 | 15.4k | el->n_master = (uint8_t)n; |
409 | | |
410 | 15.4k | el->n_high = (uint8_t)(n - el->xover_band); |
411 | 146k | for (int i = 0; i <= el->n_high; i++) el->f_high[i] = el->f_master[el->xover_band + i]; |
412 | 15.4k | el->kx = el->f_high[0]; |
413 | 15.4k | el->M = (uint8_t)(el->f_high[el->n_high] - el->kx); |
414 | 15.4k | if (el->kx > 32 || el->kx + el->M > 64) return false; |
415 | | |
416 | 15.2k | el->n_low = (uint8_t)((el->n_high + 1) / 2); |
417 | 15.2k | int odd = el->n_high & 1; |
418 | 15.2k | el->f_low[0] = el->f_high[0]; |
419 | 73.8k | for (int i = 1; i <= el->n_low; i++) el->f_low[i] = el->f_high[2 * i - odd]; |
420 | | |
421 | 15.2k | int nq = el->noise_bands ? sbr_round(el->noise_bands * log2((double)k2 / el->kx)) : 1; |
422 | 15.2k | if (nq < 1) nq = 1; |
423 | 15.2k | if (nq > SBR_MAX_NQ) nq = SBR_MAX_NQ; |
424 | 15.2k | el->n_q = (uint8_t)nq; |
425 | 15.2k | el->f_noise[0] = el->f_low[0]; |
426 | 15.2k | int idx = 0; |
427 | 37.0k | for (int k = 1; k <= nq; k++) { |
428 | 21.7k | idx += (el->n_low - idx) / (nq + 1 - k); |
429 | 21.7k | el->f_noise[k] = el->f_low[idx]; |
430 | 21.7k | } |
431 | | |
432 | 15.2k | if (!sbr_build_patches(el, sr)) return false; |
433 | 14.9k | return sbr_build_limiter(el); |
434 | 15.2k | } |
435 | | |
436 | | static void sbr_reset_synthesis(SBRChannel *ch) |
437 | 6.55k | { |
438 | 6.55k | memset(ch->x_low_tail, 0, sizeof(ch->x_low_tail)); |
439 | 6.55k | memset(ch->y_tail, 0, sizeof(ch->y_tail)); |
440 | 6.55k | memset(ch->bw_array, 0, sizeof(ch->bw_array)); |
441 | 6.55k | memset(ch->invf_mode_prev, 0, sizeof(ch->invf_mode_prev)); |
442 | 6.55k | memset(ch->s_index_prev, 0, sizeof(ch->s_index_prev)); |
443 | 6.55k | memset(ch->g_hist, 0, sizeof(ch->g_hist)); |
444 | 6.55k | memset(ch->q_hist, 0, sizeof(ch->q_hist)); |
445 | 6.55k | ch->l_A_prev = -1; |
446 | 6.55k | ch->L_E_prev = 0; |
447 | 6.55k | ch->t_E_end_prev = 0; |
448 | 6.55k | ch->index_noise = 0; |
449 | 6.55k | ch->index_sine = 0; |
450 | 6.55k | ch->have_frame = false; |
451 | 6.55k | ch->primed = false; |
452 | 6.55k | ch->kx_prev = 0; |
453 | 6.55k | ch->M_prev = 0; |
454 | 6.55k | } |
455 | | |
456 | | /* Bitstream: header, grid, envelopes (§4.6.18.3) */ |
457 | | |
458 | | /* Canonical codewords of each length form a contiguous range. */ |
459 | | int sbr_huff_decode(BitReader *bs, const SBRHuffBook *book) |
460 | 452k | { |
461 | 452k | uint32_t code = 0, first_code = 0; |
462 | 748k | for (int len = 1; len <= HB_MAX_LEN; len++) { |
463 | 748k | code = (code << 1) | bits_get_1(bs); |
464 | 748k | int first = book->first[len], end = book->first[len + 1]; |
465 | 748k | if (!book->tab) { |
466 | 458k | uint32_t index = code - first_code; |
467 | 458k | if (index < (uint32_t)(end - first)) |
468 | 274k | return book->order[first + index] - book->offset; |
469 | 184k | first_code = (first_code + (uint32_t)(end - first)) << 1; |
470 | 289k | } else { |
471 | 333k | for (int i = first; i < end; i++) { |
472 | 269k | uint32_t c = book->tab[book->order[i]].code; |
473 | 269k | if (c == code) return book->order[i] - book->offset; |
474 | 91.5k | if (c > code) break; |
475 | 91.5k | } |
476 | 289k | } |
477 | 748k | } |
478 | 0 | return 0; |
479 | 452k | } |
480 | | |
481 | | static const uint8_t sbr_ceil_log2[] = { 0, 1, 2, 2, 3, 3 }; |
482 | | |
483 | | static bool sbr_read_grid(BitReader *bs, SBRChannel *ch, int num_time_slots) |
484 | 41.4k | { |
485 | 41.4k | ch->frame_class = (uint8_t)bits_get(bs, 2); |
486 | 41.4k | int L_E; |
487 | 41.4k | switch (ch->frame_class) { |
488 | 8.46k | case 0: { /* FIXFIX */ |
489 | 8.46k | int v = (int)(bits_get(bs, 2) & 3); |
490 | 8.46k | L_E = 1 << v; |
491 | 8.46k | if (L_E > 4) return false; |
492 | 8.13k | uint8_t fr = (uint8_t)bits_get(bs, 1); |
493 | 8.13k | ch->t_E[0] = 0; |
494 | | /* The spacing is rounded and the last border is the frame end, so 15 slots |
495 | | * give 0 8 15 and 0 4 8 12 15; truncating would put them at 7 and 3 7 11. */ |
496 | 8.13k | int spacing = (num_time_slots + (L_E >> 1)) / L_E; |
497 | 13.2k | for (int i = 1; i < L_E; i++) ch->t_E[i] = (uint8_t)(i * spacing); |
498 | 8.13k | ch->t_E[L_E] = (uint8_t)num_time_slots; |
499 | 21.3k | for (int i = 0; i < L_E; i++) ch->freq_res[i] = fr; |
500 | 8.13k | ch->bs_pointer = 0; |
501 | 8.13k | break; |
502 | 8.46k | } |
503 | 3.44k | case 1: { /* FIXVAR */ |
504 | 3.44k | int end = num_time_slots + (int)(bits_get(bs, 2) & 3); |
505 | 3.44k | L_E = (int)(bits_get(bs, 2) & 3) + 1; |
506 | 3.44k | if (L_E > 4) return false; |
507 | 3.44k | ch->t_E[0] = 0; |
508 | 3.44k | ch->t_E[L_E] = (uint8_t)end; |
509 | 8.30k | for (int i = 0; i < L_E - 1; i++) { |
510 | 4.85k | int rel = 2 * (int)(bits_get(bs, 2) & 3) + 2; |
511 | 4.85k | ch->t_E[L_E - 1 - i] = (uint8_t)(ch->t_E[L_E - i] - rel); |
512 | 4.85k | } |
513 | 3.44k | ch->bs_pointer = (uint8_t)bits_get(bs, sbr_ceil_log2[L_E]); |
514 | 11.7k | for (int i = 0; i < L_E; i++) ch->freq_res[L_E - 1 - i] = (uint8_t)bits_get(bs, 1); |
515 | 3.44k | break; |
516 | 3.44k | } |
517 | 16.8k | case 2: { /* VARFIX */ |
518 | 16.8k | int start = (int)(bits_get(bs, 2) & 3); |
519 | 16.8k | L_E = (int)(bits_get(bs, 2) & 3) + 1; |
520 | 16.8k | if (L_E > 4) return false; |
521 | 16.8k | ch->t_E[0] = (uint8_t)start; |
522 | 30.0k | for (int i = 0; i < L_E - 1; i++) { |
523 | 13.2k | int rel = 2 * (int)(bits_get(bs, 2) & 3) + 2; |
524 | 13.2k | ch->t_E[i + 1] = (uint8_t)(ch->t_E[i] + rel); |
525 | 13.2k | } |
526 | 16.8k | ch->t_E[L_E] = (uint8_t)num_time_slots; |
527 | 16.8k | ch->bs_pointer = (uint8_t)bits_get(bs, sbr_ceil_log2[L_E]); |
528 | 46.9k | for (int i = 0; i < L_E; i++) ch->freq_res[i] = (uint8_t)bits_get(bs, 1); |
529 | 16.8k | break; |
530 | 16.8k | } |
531 | 12.7k | default: { /* VARVAR */ |
532 | 12.7k | int start = (int)(bits_get(bs, 2) & 3); |
533 | 12.7k | int end = num_time_slots + (int)(bits_get(bs, 2) & 3); |
534 | 12.7k | int rel0 = (int)(bits_get(bs, 2) & 3); |
535 | 12.7k | int rel1 = (int)(bits_get(bs, 2) & 3); |
536 | 12.7k | L_E = rel0 + rel1 + 1; |
537 | 12.7k | if (L_E > SBR_MAX_ENV) return false; |
538 | 6.32k | ch->t_E[0] = (uint8_t)start; |
539 | 14.3k | for (int i = 0; i < rel0; i++) { |
540 | 8.04k | int rel = 2 * (int)(bits_get(bs, 2) & 3) + 2; |
541 | 8.04k | ch->t_E[i + 1] = (uint8_t)(ch->t_E[i] + rel); |
542 | 8.04k | } |
543 | 6.32k | ch->t_E[L_E] = (uint8_t)end; |
544 | 12.6k | for (int i = 0; i < rel1; i++) { |
545 | 6.30k | int rel = 2 * (int)(bits_get(bs, 2) & 3) + 2; |
546 | 6.30k | ch->t_E[L_E - 1 - i] = (uint8_t)(ch->t_E[L_E - i] - rel); |
547 | 6.30k | } |
548 | 6.32k | ch->bs_pointer = (uint8_t)bits_get(bs, sbr_ceil_log2[L_E]); |
549 | 26.9k | for (int i = 0; i < L_E; i++) ch->freq_res[i] = (uint8_t)bits_get(bs, 1); |
550 | 6.32k | break; |
551 | 12.7k | } |
552 | 41.4k | } |
553 | 34.7k | ch->L_E = (uint8_t)L_E; |
554 | 104k | for (int i = 0; i < L_E; i++) |
555 | 71.1k | if (ch->t_E[i + 1] <= ch->t_E[i] || ch->t_E[i + 1] > num_time_slots + 3) return false; |
556 | 33.2k | if (ch->bs_pointer > L_E) return false; |
557 | | |
558 | | /* Transient envelope l_A and the noise floor borders (§4.6.18.3.3). */ |
559 | 29.7k | int p = ch->bs_pointer, mid; |
560 | 29.7k | switch (ch->frame_class) { |
561 | 8.13k | case 0: ch->l_A = -1; mid = L_E / 2; break; |
562 | 15.7k | case 2: ch->l_A = (int8_t)(p ? p - 1 : -1); mid = (p == 0) ? 1 : (p == 1) ? L_E - 1 : p - 1; break; |
563 | 5.87k | default: ch->l_A = (int8_t)(p ? L_E + 1 - p : -1); mid = (p > 1) ? L_E + 1 - p : L_E - 1; break; |
564 | 29.7k | } |
565 | 29.7k | if (mid < 0) mid = 0; |
566 | 29.7k | ch->L_Q = (uint8_t)(L_E > 1 ? 2 : 1); |
567 | 29.7k | ch->t_Q[0] = ch->t_E[0]; |
568 | 29.7k | if (ch->L_Q == 1) { |
569 | 16.0k | ch->t_Q[1] = ch->t_E[L_E]; |
570 | 16.0k | } else { |
571 | 13.7k | ch->t_Q[1] = ch->t_E[mid]; |
572 | 13.7k | ch->t_Q[2] = ch->t_E[L_E]; |
573 | 13.7k | } |
574 | 29.7k | return true; |
575 | 29.7k | } |
576 | | |
577 | | static bool sbr_read_envelope(BitReader *bs, const SBRElement *el, SBRChannel *ch, bool balance) |
578 | 42.9k | { |
579 | | /* 3.0 dB resolution: 5-bit start (6 for the balance channel); 1.5 dB: 7 (6). */ |
580 | 42.9k | int start_bits = ch->amp_res ? (balance ? 5 : 6) : (balance ? 6 : 7); |
581 | 42.9k | const SBRHuffBook *t_book = &sbr_books[ch->amp_res ? (balance ? HB_T_ENV_BAL_30 : HB_T_ENV_30) |
582 | 42.9k | : (balance ? HB_T_ENV_BAL_15 : HB_T_ENV_15)]; |
583 | 42.9k | const SBRHuffBook *f_book = &sbr_books[ch->amp_res ? (balance ? HB_F_ENV_BAL_30 : HB_F_ENV_30) |
584 | 42.9k | : (balance ? HB_F_ENV_BAL_15 : HB_F_ENV_15)]; |
585 | | |
586 | | /* The balance channel is coded at half resolution: its values count double. */ |
587 | 42.9k | int scale = balance ? 2 : 1; |
588 | 124k | for (int l = 0; l < ch->L_E; l++) { |
589 | 81.7k | int nb = ch->freq_res[l] ? el->n_high : el->n_low; |
590 | 81.7k | if (ch->df_env[l] == 0) { |
591 | 53.9k | ch->E[l][0] = (int16_t)(scale * (int)bits_get(bs, start_bits)); |
592 | 184k | for (int k = 1; k < nb; k++) { |
593 | 130k | int d = scale * sbr_huff_decode(bs, f_book); |
594 | 130k | ch->E[l][k] = (int16_t)(ch->E[l][k - 1] + d); |
595 | 130k | } |
596 | 53.9k | } else { |
597 | | /* Time deltas against the previous envelope, mapped across a |
598 | | * resolution change (§4.6.18.6.2). */ |
599 | 27.8k | const int16_t *prev = (l == 0) ? ch->E_prev : ch->E[l - 1]; |
600 | 27.8k | int r_prev = (l == 0) ? ch->freq_res_prev : ch->freq_res[l - 1]; |
601 | 132k | for (int k = 0; k < nb; k++) { |
602 | 104k | int d = scale * sbr_huff_decode(bs, t_book); |
603 | 104k | int ref; |
604 | 104k | if (r_prev == ch->freq_res[l]) { |
605 | 67.0k | ref = prev[k]; |
606 | 67.0k | } else if (ch->freq_res[l]) { |
607 | 4.09k | int i = 0; |
608 | 20.2k | while (i + 1 < el->n_low && el->f_high[k] >= el->f_low[i + 1]) i++; |
609 | 4.09k | ref = prev[i]; |
610 | 33.7k | } else { |
611 | 33.7k | int i = 0; |
612 | 159k | while (i < el->n_high && el->f_high[i] != el->f_low[k]) i++; |
613 | 33.7k | ref = prev[i < el->n_high ? i : el->n_high - 1]; |
614 | 33.7k | } |
615 | 104k | ch->E[l][k] = (int16_t)(ref + d); |
616 | 104k | } |
617 | 27.8k | } |
618 | 371k | for (int k = 0; k < nb; k++) { |
619 | 289k | if (ch->E[l][k] < -128 || ch->E[l][k] > 256) return false; |
620 | 289k | } |
621 | 81.7k | } |
622 | 42.8k | return true; |
623 | 42.9k | } |
624 | | |
625 | | static bool sbr_read_noise(BitReader *bs, const SBRElement *el, SBRChannel *ch, bool balance) |
626 | 42.8k | { |
627 | 42.8k | const SBRHuffBook *t_book = &sbr_books[balance ? HB_T_NOISE_BAL_30 : HB_T_NOISE_30]; |
628 | 42.8k | const SBRHuffBook *f_book = &sbr_books[balance ? HB_F_ENV_BAL_30 : HB_F_ENV_30]; |
629 | | |
630 | 42.8k | int scale = balance ? 2 : 1; |
631 | 103k | for (int l = 0; l < ch->L_Q; l++) { |
632 | 61.9k | if (ch->df_noise[l] == 0) { |
633 | 31.4k | ch->Q[l][0] = (int16_t)(scale * (int)bits_get(bs, 5)); |
634 | 36.2k | for (int k = 1; k < el->n_q; k++) { |
635 | 4.79k | int d = scale * sbr_huff_decode(bs, f_book); |
636 | 4.79k | ch->Q[l][k] = (int16_t)(ch->Q[l][k - 1] + d); |
637 | 4.79k | } |
638 | 31.4k | } else { |
639 | 30.5k | const int16_t *prev = (l == 0) ? ch->Q_prev : ch->Q[l - 1]; |
640 | 64.4k | for (int k = 0; k < el->n_q; k++) { |
641 | 33.9k | int d = scale * sbr_huff_decode(bs, t_book); |
642 | 33.9k | ch->Q[l][k] = (int16_t)(prev[k] + d); |
643 | 33.9k | } |
644 | 30.5k | } |
645 | 130k | for (int k = 0; k < el->n_q; k++) { |
646 | 70.0k | if (ch->Q[l][k] < -60 || ch->Q[l][k] > 60) return false; |
647 | 70.0k | } |
648 | 61.9k | } |
649 | 41.4k | return true; |
650 | 42.8k | } |
651 | | |
652 | | static faad_status sbr_read_extension(struct faad_decoder *dec, BitReader *bs, uint32_t ch0, uint32_t syntax_id, bool crc) |
653 | 46.0k | { |
654 | 46.0k | int nch = (syntax_id == ID_CPE) ? 2 : 1; |
655 | 46.0k | if (ch0 + nch > MAX_CHANNELS) return FAAD_ERR_INVALID_ARGUMENT; |
656 | 46.0k | if (faad_ensure_sbr(dec) != FAAD_OK) return FAAD_ERR_NO_MEMORY; |
657 | 46.0k | SBRElement *el = &dec->sbr_el[ch0]; |
658 | 46.0k | bool reset_synthesis = false; |
659 | 46.0k | if (crc) bits_skip(bs, 10); /* bs_sbr_crc_bits, not verified */ |
660 | 46.0k | uint32_t sbr_sr = dec->asc.sbr_sample_rate > 0 ? dec->asc.sbr_sample_rate : 2 * dec->core_sample_rate; |
661 | | |
662 | 46.0k | if (bits_get(bs, 1)) { /* bs_header_flag */ |
663 | 25.4k | SBRElement h = *el; |
664 | 25.4k | h.amp_res = bits_get(bs, 1); |
665 | 25.4k | h.start_freq = (uint8_t)bits_get(bs, 4); |
666 | 25.4k | h.stop_freq = (uint8_t)bits_get(bs, 4); |
667 | 25.4k | h.xover_band = (uint8_t)bits_get(bs, 3); |
668 | 25.4k | bits_skip(bs, 2); |
669 | 25.4k | bool extra1 = bits_get(bs, 1); |
670 | 25.4k | bool extra2 = bits_get(bs, 1); |
671 | 25.4k | if (extra1) { |
672 | 18.1k | h.freq_scale = (uint8_t)bits_get(bs, 2); |
673 | 18.1k | h.alter_scale = (uint8_t)bits_get(bs, 1); |
674 | 18.1k | h.noise_bands = (uint8_t)bits_get(bs, 2); |
675 | 18.1k | } else { |
676 | 7.33k | h.freq_scale = 2; h.alter_scale = 1; h.noise_bands = 2; |
677 | 7.33k | } |
678 | 25.4k | if (extra2) { |
679 | 2.72k | h.limiter_bands = (uint8_t)bits_get(bs, 2); |
680 | 2.72k | h.limiter_gains = (uint8_t)bits_get(bs, 2); |
681 | 2.72k | h.interpol_freq = (uint8_t)bits_get(bs, 1); |
682 | 2.72k | h.smoothing_mode = (uint8_t)bits_get(bs, 1); |
683 | 22.7k | } else { |
684 | 22.7k | h.limiter_bands = 2; h.limiter_gains = 2; h.interpol_freq = 1; h.smoothing_mode = 1; |
685 | 22.7k | } |
686 | | #ifdef FAAD_STATS |
687 | | g_faadStats.sbrHeaderCount++; |
688 | | #endif |
689 | | /* A change to anything the tables depend on resets the element. */ |
690 | 25.4k | bool reset = !el->header_present || h.start_freq != el->start_freq || h.stop_freq != el->stop_freq || |
691 | 10.4k | h.xover_band != el->xover_band || h.freq_scale != el->freq_scale || |
692 | 6.48k | h.alter_scale != el->alter_scale || h.noise_bands != el->noise_bands; |
693 | 25.4k | *el = h; |
694 | 25.4k | el->header_present = true; |
695 | 25.4k | if (reset) { |
696 | 20.4k | if (!sbr_build_tables(el, sbr_sr)) { |
697 | 5.49k | el->header_present = false; |
698 | 5.49k | return FAAD_ERR_DECODE_FAILED; |
699 | 5.49k | } |
700 | 14.9k | reset_synthesis = true; |
701 | 14.9k | } |
702 | | #ifdef FAAD_STATS |
703 | | { |
704 | | FILE *df = faad_dump_file(); |
705 | | if (df) { |
706 | | fprintf(df, "H %u %d %d %u %u %u %u %u %u %u %u %u %u %d %u %u %u %u %u %d %d\n", |
707 | | g_faadStats.totalFrames, nch, (int)el->amp_res, el->start_freq, el->stop_freq, |
708 | | el->xover_band, el->freq_scale, el->alter_scale, el->noise_bands, |
709 | | el->limiter_bands, el->limiter_gains, el->interpol_freq, el->smoothing_mode, |
710 | | (int)reset, el->kx, el->M, el->n_low, el->n_high, el->n_q, |
711 | | (int)extra1, (int)extra2); |
712 | | } |
713 | | } |
714 | | #endif |
715 | 25.4k | } |
716 | 40.5k | if (!el->header_present) { |
717 | | /* A stream joined between SBR headers has no tables yet, but its output |
718 | | * format must not change when the first header arrives: play the core at |
719 | | * the SBR rate until then. */ |
720 | 1.71k | dec->sbr_present = true; |
721 | 1.71k | return FAAD_OK; |
722 | 1.71k | } |
723 | | |
724 | 38.8k | SBRChannel *chs[2] = { &dec->sbr[ch0], &dec->sbr[ch0 + (nch > 1 ? 1 : 0)] }; |
725 | | /* A channel continues only from this element's previous fill. New tables |
726 | | * start it over, and so does a change of element layout: a channel that |
727 | | * fill did not include holds another element's deltas and history, and |
728 | | * a single channel and a pair share neither. */ |
729 | 38.8k | if (el->nch != nch) reset_synthesis = true; |
730 | 95.6k | for (int c = 0; c < nch; c++) { |
731 | 56.7k | SBRChannel *ch = chs[c]; |
732 | 56.7k | if (!reset_synthesis && ch->el_ch == ch0) continue; |
733 | 21.1k | memset(ch->E_prev, 0, sizeof(ch->E_prev)); |
734 | 21.1k | memset(ch->Q_prev, 0, sizeof(ch->Q_prev)); |
735 | 21.1k | ch->freq_res_prev = 1; |
736 | 21.1k | } |
737 | 38.8k | el->nch = (uint8_t)nch; |
738 | | /* sbr_single_channel_element / sbr_channel_pair_element (§4.6.18.3.4) */ |
739 | 38.8k | bool data_extra = bits_get(bs, 1); |
740 | 38.8k | if (data_extra) bits_skip(bs, 4 * (uint32_t)nch); /* bs_reserved, ahead of bs_coupling */ |
741 | 38.8k | el->coupling = (nch == 2) ? bits_get(bs, 1) : false; |
742 | | |
743 | 38.8k | int num_time_slots = FRAME_IS_960(dec) ? 15 : 16; |
744 | 38.8k | if (!sbr_read_grid(bs, chs[0], num_time_slots)) return FAAD_ERR_DECODE_FAILED; |
745 | 27.3k | if (nch == 2) { |
746 | 16.1k | if (el->coupling) { |
747 | 13.4k | SBRChannel *a = chs[0], *b = chs[1]; |
748 | 13.4k | b->frame_class = a->frame_class; b->L_E = a->L_E; b->L_Q = a->L_Q; |
749 | 13.4k | b->bs_pointer = a->bs_pointer; b->l_A = a->l_A; |
750 | 13.4k | memcpy(b->t_E, a->t_E, sizeof(b->t_E)); |
751 | 13.4k | memcpy(b->t_Q, a->t_Q, sizeof(b->t_Q)); |
752 | 13.4k | memcpy(b->freq_res, a->freq_res, sizeof(b->freq_res)); |
753 | 13.4k | } else if (!sbr_read_grid(bs, chs[1], num_time_slots)) { |
754 | 280 | return FAAD_ERR_DECODE_FAILED; |
755 | 280 | } |
756 | 16.1k | } |
757 | 70.0k | for (int c = 0; c < nch; c++) { |
758 | 42.9k | SBRChannel *ch = chs[c]; |
759 | | /* a single FIXFIX envelope is always coded at 1.5 dB */ |
760 | 42.9k | ch->amp_res = (ch->frame_class == 0 && ch->L_E == 1) ? false : el->amp_res; |
761 | 42.9k | } |
762 | 70.0k | for (int c = 0; c < nch; c++) { |
763 | 42.9k | SBRChannel *ch = chs[c]; |
764 | 124k | for (int l = 0; l < ch->L_E && l < SBR_MAX_ENV; l++) ch->df_env[l] = (uint8_t)bits_get(bs, 1); |
765 | 106k | for (int l = 0; l < ch->L_Q && l < 2; l++) ch->df_noise[l] = (uint8_t)bits_get(bs, 1); |
766 | 42.9k | } |
767 | 57.9k | for (int k = 0; k < el->n_q && k < SBR_MAX_NQ; k++) chs[0]->invf_mode[k] = (uint8_t)bits_get(bs, 2); |
768 | 27.1k | if (nch == 2) { |
769 | 15.8k | if (el->coupling) memcpy(chs[1]->invf_mode, chs[0]->invf_mode, sizeof(chs[0]->invf_mode)); |
770 | 5.37k | else for (int k = 0; k < el->n_q && k < SBR_MAX_NQ; k++) chs[1]->invf_mode[k] = (uint8_t)bits_get(bs, 2); |
771 | 15.8k | } |
772 | 27.1k | if (el->coupling) { |
773 | 13.4k | if (!sbr_read_envelope(bs, el, chs[0], false)) return FAAD_ERR_DECODE_FAILED; |
774 | 13.4k | if (!sbr_read_noise(bs, el, chs[0], false)) return FAAD_ERR_DECODE_FAILED; |
775 | 13.4k | if (!sbr_read_envelope(bs, el, chs[1], true)) return FAAD_ERR_DECODE_FAILED; |
776 | 13.4k | if (!sbr_read_noise(bs, el, chs[1], true)) return FAAD_ERR_DECODE_FAILED; |
777 | 13.6k | } else { |
778 | 29.6k | for (int c = 0; c < nch; c++) { |
779 | 16.0k | if (!sbr_read_envelope(bs, el, chs[c], false)) return FAAD_ERR_DECODE_FAILED; |
780 | 16.0k | } |
781 | 29.6k | for (int c = 0; c < nch; c++) { |
782 | 16.0k | if (!sbr_read_noise(bs, el, chs[c], false)) return FAAD_ERR_DECODE_FAILED; |
783 | 16.0k | } |
784 | 13.6k | } |
785 | 65.5k | for (int c = 0; c < nch; c++) { |
786 | 39.9k | SBRChannel *ch = chs[c]; |
787 | 39.9k | ch->add_harmonic_flag = bits_get(bs, 1); |
788 | 39.9k | memset(ch->add_harmonic, 0, sizeof(ch->add_harmonic)); |
789 | 39.9k | if (ch->add_harmonic_flag) |
790 | 42.2k | for (int k = 0; k < el->n_high; k++) ch->add_harmonic[k] = (uint8_t)bits_get(bs, 1); |
791 | 39.9k | } |
792 | | #ifdef FAAD_STATS |
793 | | g_faadStats.sbrEnvelopeSum += chs[0]->L_E; |
794 | | #endif |
795 | | |
796 | 25.6k | if (bits_get(bs, 1)) { /* bs_extended_data */ |
797 | 9.95k | uint32_t cnt = bits_get(bs, 4); |
798 | 9.95k | if (cnt == 15) cnt += bits_get(bs, 8); |
799 | 9.95k | uint32_t end = bits_get_consumed(bs) + cnt * 8; |
800 | 9.95k | while (bits_get_consumed(bs) + 7 < end) { |
801 | 7.67k | uint32_t id = bits_get(bs, 2); |
802 | 7.67k | #ifndef FAAD_DISABLE_PS |
803 | 7.67k | if (id == PS_EXTENSION_DATA) { |
804 | 5.76k | uint32_t here = bits_get_consumed(bs); |
805 | 5.76k | if (faad_ensure_ps(dec) != FAAD_OK) return FAAD_ERR_NO_MEMORY; |
806 | 5.76k | ps_read_data(dec, bs, end > here ? end - here : 0); |
807 | | #ifdef FAAD_STATS |
808 | | g_faadStats.framePsBits += bits_get_consumed(bs) - here; |
809 | | #endif |
810 | 5.76k | break; |
811 | 5.76k | } |
812 | 1.90k | #endif |
813 | 1.90k | (void)id; |
814 | 1.90k | break; |
815 | 7.67k | } |
816 | 9.95k | uint32_t consumed = bits_get_consumed(bs); |
817 | 9.95k | if (consumed < end) bits_skip(bs, end - consumed); |
818 | 9.95k | } |
819 | | |
820 | 25.6k | if (bs->overread) return FAAD_ERR_DECODE_FAILED; |
821 | | /* Synthesis history is not read by the parser. Commit its reset only |
822 | | * after a valid fill, keeping it out of the rollback snapshot. */ |
823 | 38.6k | for (int c = 0; c < nch; c++) { |
824 | 24.2k | if (reset_synthesis || chs[c]->el_ch != ch0) sbr_reset_synthesis(chs[c]); |
825 | 24.2k | chs[c]->have_frame = true; |
826 | 24.2k | chs[c]->el_ch = (uint8_t)ch0; |
827 | 24.2k | } |
828 | 14.3k | dec->sbr_present = true; |
829 | 14.3k | return FAAD_OK; |
830 | 25.6k | } |
831 | | |
832 | | /* Only parsing state needs a transaction; synthesis history is unchanged |
833 | | * until the fill succeeds. Failed fills must not replace valid history. */ |
834 | | faad_status sbr_decode_extension(struct faad_decoder *dec, BitReader *bs, uint32_t ch0, uint32_t syntax_id, bool crc) |
835 | 46.0k | { |
836 | 46.0k | uint32_t nch = syntax_id == ID_CPE ? 2 : 1; |
837 | 46.0k | if (ch0 + nch > MAX_CHANNELS) return FAAD_ERR_INVALID_ARGUMENT; |
838 | 46.0k | if (faad_ensure_sbr(dec) != FAAD_OK) return FAAD_ERR_NO_MEMORY; |
839 | 46.0k | SBRElement saved_el = dec->sbr_el[ch0]; |
840 | 46.0k | uint8_t saved_ch[2][offsetof(SBRChannel, bw_array)]; |
841 | 111k | for (uint32_t c = 0; c < nch; c++) memcpy(saved_ch[c], &dec->sbr[ch0 + c], sizeof(saved_ch[c])); |
842 | 46.0k | #ifndef FAAD_DISABLE_PS |
843 | 46.0k | uint8_t saved_ps[offsetof(PSState, in_buf)] = {0}; |
844 | 46.0k | bool ps_present = dec->ps_present; |
845 | 46.0k | if (dec->ps) memcpy(saved_ps, dec->ps, sizeof(saved_ps)); |
846 | 46.0k | #endif |
847 | 46.0k | faad_status st = sbr_read_extension(dec, bs, ch0, syntax_id, crc); |
848 | 46.0k | if (bs->overread && st == FAAD_OK) st = FAAD_ERR_DECODE_FAILED; |
849 | 46.0k | if (st != FAAD_OK) { |
850 | 30.0k | dec->sbr_el[ch0] = saved_el; |
851 | 68.7k | for (uint32_t c = 0; c < nch; c++) memcpy(&dec->sbr[ch0 + c], saved_ch[c], sizeof(saved_ch[c])); |
852 | 30.0k | #ifndef FAAD_DISABLE_PS |
853 | 30.0k | if (dec->ps) memcpy(dec->ps, saved_ps, sizeof(saved_ps)); |
854 | 30.0k | dec->ps_present = ps_present; |
855 | 30.0k | #endif |
856 | 30.0k | } |
857 | 46.0k | return st; |
858 | 46.0k | } |
859 | | |
860 | | /* HF generation (§4.6.18.6) */ |
861 | | |
862 | | /* Chirp factors from the inverse filtering modes (§4.6.18.6.1). */ |
863 | | static void sbr_chirp(const SBRElement *el, SBRChannel *ch) |
864 | 1.75k | { |
865 | 1.75k | static const float new_bw_tab[4] = { 0.0f, 0.75f, 0.9f, 0.98f }; |
866 | 4.81k | for (int k = 0; k < el->n_q; k++) { |
867 | | /* a switch between OFF and LOW in either direction lands on 0.6 */ |
868 | 3.06k | float nb = (ch->invf_mode[k] + ch->invf_mode_prev[k] == 1) ? 0.6f : new_bw_tab[ch->invf_mode[k] & 3]; |
869 | 3.06k | float prev = ch->bw_array[k]; |
870 | 3.06k | float t = (nb < prev) ? 0.75f * nb + 0.25f * prev : 0.90625f * nb + 0.09375f * prev; |
871 | 3.06k | if (t < 0.015625f) t = 0.0f; |
872 | 3.06k | if (t >= 0.99609375f) t = 0.99609375f; |
873 | 3.06k | ch->bw_array[k] = t; |
874 | 3.06k | ch->invf_mode_prev[k] = ch->invf_mode[k]; |
875 | 3.06k | } |
876 | 1.75k | } |
877 | | |
878 | | /* Second-order covariance LPC of one low band over the whole buffer. */ |
879 | | static void sbr_lpc(float x[SBR_BUF_SLOTS][2], float a0[2], float a1[2]) |
880 | 12.2k | { |
881 | 12.2k | float p01r = 0, p01i = 0, p02r = 0, p02i = 0, p11 = 0, p12r = 0, p12i = 0, p22 = 0; |
882 | 475k | for (int n = SBR_T_HFADJ; n < SBR_BUF_SLOTS; n++) { |
883 | 463k | float x0r = x[n][0], x0i = x[n][1]; |
884 | 463k | float x1r = x[n - 1][0], x1i = x[n - 1][1]; |
885 | 463k | float x2r = x[n - 2][0], x2i = x[n - 2][1]; |
886 | | /* phi(i,j) = sum x(n-i) conj(x(n-j)) */ |
887 | 463k | p01r += x0r * x1r + x0i * x1i; p01i += x0i * x1r - x0r * x1i; |
888 | 463k | p02r += x0r * x2r + x0i * x2i; p02i += x0i * x2r - x0r * x2i; |
889 | 463k | p12r += x1r * x2r + x1i * x2i; p12i += x1i * x2r - x1r * x2i; |
890 | 463k | p11 += x1r * x1r + x1i * x1i; |
891 | 463k | p22 += x2r * x2r + x2i * x2i; |
892 | 463k | } |
893 | 12.2k | float d = p22 * p11 - (p12r * p12r + p12i * p12i) * (1.0f / 1.000001f); |
894 | 12.2k | float a1r = 0, a1i = 0, a0r = 0, a0i = 0; |
895 | 12.2k | if (d != 0.0f) { |
896 | | /* alpha1 = (phi01 phi12 - phi02 phi11) / d */ |
897 | 645 | float inv = 1.0f / d; |
898 | 645 | a1r = (p01r * p12r - p01i * p12i - p02r * p11) * inv; |
899 | 645 | a1i = (p01r * p12i + p01i * p12r - p02i * p11) * inv; |
900 | 645 | } |
901 | 12.2k | if (p11 != 0.0f) { |
902 | | /* alpha0 = -(phi01 + alpha1 conj(phi12)) / phi11 */ |
903 | 825 | float inv = 1.0f / p11; |
904 | 825 | a0r = -(p01r + a1r * p12r + a1i * p12i) * inv; |
905 | 825 | a0i = -(p01i + a1i * p12r - a1r * p12i) * inv; |
906 | 825 | } |
907 | 12.2k | if (a0r * a0r + a0i * a0i >= 16.0f || a1r * a1r + a1i * a1i >= 16.0f) { |
908 | 291 | a0r = a0i = a1r = a1i = 0.0f; |
909 | 291 | } |
910 | 12.2k | a0[0] = a0r; a0[1] = a0i; |
911 | 12.2k | a1[0] = a1r; a1[1] = a1i; |
912 | 12.2k | } |
913 | | |
914 | | static void sbr_hf_generate(const SBRElement *el, SBRChannel *ch, SBRScratch *sc) |
915 | 1.75k | { |
916 | | /* predictor per source band, computed the first time a patch uses it */ |
917 | 1.75k | float a0[32][2], a1[32][2]; |
918 | 1.75k | bool have_lpc[32] = { false }; |
919 | | |
920 | 1.75k | int start = 2 * ch->t_E[0] + SBR_T_HFADJ; |
921 | 1.75k | int end = 2 * ch->t_E[ch->L_E] + SBR_T_HFADJ; |
922 | 1.75k | int k = el->kx, g = 0; |
923 | 5.23k | for (int i = 0; i < el->num_patches; i++) { |
924 | 25.9k | for (int x = 0; x < el->patch_num[i]; x++, k++) { |
925 | 22.4k | int p = el->patch_start[i] + x; |
926 | 22.4k | if (!have_lpc[p]) { |
927 | 12.2k | sbr_lpc(sc->x_low[p], a0[p], a1[p]); |
928 | 12.2k | have_lpc[p] = true; |
929 | 12.2k | } |
930 | 23.7k | while (g < el->n_q && k >= el->f_noise[g + 1]) g++; |
931 | 22.4k | float bw = ch->bw_array[g], bw2 = bw * bw; |
932 | 22.4k | float c0r = a0[p][0] * bw, c0i = a0[p][1] * bw; |
933 | 22.4k | float c1r = a1[p][0] * bw2, c1i = a1[p][1] * bw2; |
934 | 22.4k | float (*xl)[2] = sc->x_low[p]; |
935 | 22.4k | float (*xh)[2] = sc->y[k]; |
936 | 716k | for (int n = start; n < end; n++) { |
937 | 694k | xh[n][0] = xl[n][0] + c0r * xl[n - 1][0] - c0i * xl[n - 1][1] + c1r * xl[n - 2][0] - c1i * xl[n - 2][1]; |
938 | 694k | xh[n][1] = xl[n][1] + c0r * xl[n - 1][1] + c0i * xl[n - 1][0] + c1r * xl[n - 2][1] + c1i * xl[n - 2][0]; |
939 | 694k | } |
940 | 22.4k | } |
941 | 3.48k | } |
942 | 1.75k | } |
943 | | |
944 | | /* Envelope adjustment (§4.6.18.7) */ |
945 | | |
946 | | /* 2^(n/2) built from the float exponent field; the envelope and noise |
947 | | * scalefactors are integers, so no pow() is needed. */ |
948 | | static inline float sbr_pow2_half(int n) |
949 | 20.7k | { |
950 | 20.7k | union { uint32_t u; float f; } v; |
951 | 20.7k | int e = n >> 1; /* floor(n/2), also for negative n */ |
952 | 20.7k | if (e < -126) return 0.0f; |
953 | 20.5k | if (e > 127) e = 127; |
954 | 20.5k | v.u = (uint32_t)(e + 127) << 23; |
955 | 20.5k | return (n & 1) ? v.f * 1.41421356f : v.f; |
956 | 20.7k | } |
957 | | |
958 | | /* Envelope and noise-floor scalefactors to linear values (§4.6.18.7.1): |
959 | | * E = 64 * 2^(E_q / 2) at 1.5 dB steps or 64 * 2^E_q at 3 dB, and |
960 | | * Q = 2^(6 - Q_q). A coupled pair sends a level and a balance; the level |
961 | | * splits as 2E / (1 + 2^(pan - balance)) and its complement. */ |
962 | | static void sbr_dequant(const SBRElement *el, SBRChannel *c0, SBRChannel *c1, |
963 | | float E0[SBR_MAX_ENV][SBR_MAX_BANDS], float Q0[2][SBR_MAX_NQ], |
964 | | float E1[SBR_MAX_ENV][SBR_MAX_BANDS], float Q1[2][SBR_MAX_NQ]) |
965 | 1.39k | { |
966 | 1.39k | if (!el->coupling || !c1) { |
967 | 1.10k | SBRChannel *chs[2] = { c0, c1 }; |
968 | 1.10k | float (*Es[2])[SBR_MAX_BANDS] = { E0, E1 }; |
969 | 1.10k | float (*Qs[2])[SBR_MAX_NQ] = { Q0, Q1 }; |
970 | 2.29k | for (int c = 0; c < 2 && chs[c]; c++) { |
971 | 1.18k | SBRChannel *ch = chs[c]; |
972 | 1.18k | int step = ch->amp_res ? 2 : 1; /* half-steps per unit */ |
973 | 3.22k | for (int l = 0; l < ch->L_E; l++) { |
974 | 2.04k | int nb = ch->freq_res[l] ? el->n_high : el->n_low; |
975 | 9.23k | for (int k = 0; k < nb; k++) Es[c][l][k] = 64.0f * sbr_pow2_half(step * ch->E[l][k]); |
976 | 2.04k | } |
977 | 2.78k | for (int l = 0; l < ch->L_Q; l++) |
978 | 3.90k | for (int k = 0; k < el->n_q; k++) Qs[c][l][k] = sbr_pow2_half(2 * (SBR_NOISE_FLOOR_OFFSET - ch->Q[l][k])); |
979 | 1.18k | } |
980 | 1.10k | return; |
981 | 1.10k | } |
982 | 284 | int step = c0->amp_res ? 2 : 1; |
983 | 284 | int pan = c0->amp_res ? 12 : 24; |
984 | 927 | for (int l = 0; l < c0->L_E; l++) { |
985 | 643 | int nb = c0->freq_res[l] ? el->n_high : el->n_low; |
986 | 5.39k | for (int k = 0; k < nb; k++) { |
987 | 4.74k | float e = 64.0f * sbr_pow2_half(step * c0->E[l][k]); |
988 | 4.74k | float b = sbr_pow2_half(step * (pan - c1->E[l][k])); |
989 | 4.74k | E0[l][k] = 2.0f * e / (1.0f + b); |
990 | 4.74k | E1[l][k] = 2.0f * e / (1.0f + 1.0f / b); |
991 | 4.74k | } |
992 | 643 | } |
993 | 708 | for (int l = 0; l < c0->L_Q; l++) { |
994 | 1.32k | for (int k = 0; k < el->n_q; k++) { |
995 | 903 | float q = sbr_pow2_half(2 * (SBR_NOISE_FLOOR_OFFSET - c0->Q[l][k])); |
996 | 903 | float b = sbr_pow2_half(2 * (12 - c1->Q[l][k])); |
997 | 903 | Q0[l][k] = 2.0f * q / (1.0f + b); |
998 | 903 | Q1[l][k] = 2.0f * q / (1.0f + 1.0f / b); |
999 | 903 | } |
1000 | 424 | } |
1001 | 284 | } |
1002 | | |
1003 | | static void sbr_hf_adjust(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, |
1004 | | float E[SBR_MAX_ENV][SBR_MAX_BANDS], float Q[2][SBR_MAX_NQ]) |
1005 | 1.75k | { |
1006 | 1.75k | int kx = el->kx, M = el->M; |
1007 | 1.75k | float e_orig[SBR_MAX_BANDS], q_map[SBR_MAX_BANDS], e_curr[SBR_MAX_BANDS]; |
1008 | 1.75k | float gain[SBR_MAX_BANDS], q_m[SBR_MAX_BANDS], s_m[SBR_MAX_BANDS]; |
1009 | 1.75k | uint8_t s_index[SBR_MAX_BANDS], s_mapped[SBR_MAX_BANDS]; |
1010 | | /* Smoothing history is primed with the first envelope after a reset. */ |
1011 | 1.75k | bool prime_hist = !ch->primed; |
1012 | 1.75k | ch->primed = true; |
1013 | | |
1014 | 5.07k | for (int l = 0; l < ch->L_E; l++) { |
1015 | 3.32k | bool hi = ch->freq_res[l]; |
1016 | 3.32k | const uint8_t *tab = hi ? el->f_high : el->f_low; |
1017 | 3.32k | int nb = hi ? el->n_high : el->n_low; |
1018 | 3.32k | int q_env = (ch->L_Q > 1 && ch->t_E[l] >= ch->t_Q[1]) ? 1 : 0; |
1019 | 3.32k | int slot0 = 2 * ch->t_E[l] + SBR_T_HFADJ, slot1 = 2 * ch->t_E[l + 1] + SBR_T_HFADJ; |
1020 | 3.32k | bool transient = (l == ch->l_A) || (l == 0 && ch->l_A_prev == (int)ch->L_E_prev && ch->L_E_prev > 0); |
1021 | | |
1022 | | /* mapping (§4.6.18.7.2) */ |
1023 | 20.0k | for (int i = 0; i < nb; i++) |
1024 | 56.2k | for (int k = tab[i]; k < tab[i + 1]; k++) e_orig[k - kx] = E[l][i]; |
1025 | 8.81k | for (int i = 0; i < el->n_q; i++) |
1026 | 45.0k | for (int k = el->f_noise[i]; k < el->f_noise[i + 1]; k++) q_map[k - kx] = Q[q_env][i]; |
1027 | 3.32k | memset(s_index, 0, (size_t)M); |
1028 | 3.32k | memset(s_mapped, 0, (size_t)M); |
1029 | 3.32k | if (ch->add_harmonic_flag) { |
1030 | 8.09k | for (int i = 0; i < el->n_high; i++) { |
1031 | 7.05k | if (!ch->add_harmonic[i]) continue; |
1032 | 4.05k | int mid = (el->f_high[i] + el->f_high[i + 1]) >> 1; |
1033 | 4.05k | if (l >= ch->l_A || ch->s_index_prev[mid - kx]) s_index[mid - kx] = 1; |
1034 | 4.05k | } |
1035 | 1.04k | } |
1036 | 20.0k | for (int i = 0; i < nb; i++) { |
1037 | 16.6k | int any = 0; |
1038 | 56.2k | for (int k = tab[i]; k < tab[i + 1]; k++) any |= s_index[k - kx]; |
1039 | 56.2k | for (int k = tab[i]; k < tab[i + 1]; k++) s_mapped[k - kx] = (uint8_t)any; |
1040 | 16.6k | } |
1041 | | |
1042 | | /* E_curr estimation (§4.6.18.7.3) */ |
1043 | 3.32k | float inv_slots = 1.0f / (float)(slot1 - slot0); |
1044 | 3.32k | if (el->interpol_freq) { |
1045 | 30.7k | for (int m = 0; m < M; m++) { |
1046 | 28.1k | float (*xh)[2] = sc->y[kx + m]; |
1047 | 28.1k | float acc = 0.0f; |
1048 | 526k | for (int n = slot0; n < slot1; n++) acc += xh[n][0] * xh[n][0] + xh[n][1] * xh[n][1]; |
1049 | 28.1k | e_curr[m] = acc * inv_slots; |
1050 | 28.1k | } |
1051 | 2.61k | } else { |
1052 | 5.34k | for (int i = 0; i < nb; i++) { |
1053 | 4.63k | float acc = 0.0f; |
1054 | 16.0k | for (int k = tab[i]; k < tab[i + 1]; k++) { |
1055 | 11.4k | float (*xh)[2] = sc->y[k]; |
1056 | 219k | for (int n = slot0; n < slot1; n++) acc += xh[n][0] * xh[n][0] + xh[n][1] * xh[n][1]; |
1057 | 11.4k | } |
1058 | 4.63k | acc *= inv_slots / (float)(tab[i + 1] - tab[i]); |
1059 | 16.0k | for (int k = tab[i]; k < tab[i + 1]; k++) e_curr[k - kx] = acc; |
1060 | 4.63k | } |
1061 | 717 | } |
1062 | | |
1063 | | /* gains (§4.6.18.7.5) */ |
1064 | 42.8k | for (int m = 0; m < M; m++) { |
1065 | 39.5k | float eo = e_orig[m], q = q_map[m], ec = e_curr[m]; |
1066 | 39.5k | q_m[m] = sqrtf(eo * q / (1.0f + q)); |
1067 | 39.5k | s_m[m] = s_index[m] ? sqrtf(eo / (1.0f + q)) : 0.0f; |
1068 | 39.5k | if (!s_mapped[m]) |
1069 | 33.5k | gain[m] = sqrtf(eo / ((ch->unit_energy + ec) * (1.0f + (transient ? 0.0f : q)))); |
1070 | 6.01k | else |
1071 | 6.01k | gain[m] = sqrtf(eo * q / ((ch->unit_energy + ec) * (1.0f + q))); |
1072 | 39.5k | } |
1073 | | |
1074 | | /* limiter and boost per limiter band */ |
1075 | 9.94k | for (int i = 0; i < el->n_lim; i++) { |
1076 | 6.61k | int m0 = el->f_lim[i] - kx, m1 = el->f_lim[i + 1] - kx; |
1077 | 6.61k | if (m0 < 0) m0 = 0; |
1078 | 6.61k | if (m1 > M) m1 = M; |
1079 | 6.61k | if (m1 <= m0) continue; |
1080 | 6.61k | float so = 1e-12f, sc_ = 1e-12f; |
1081 | 45.6k | for (int m = m0; m < m1; m++) { so += e_orig[m]; sc_ += e_curr[m]; } |
1082 | 6.61k | float g_max = sqrtf(so / sc_) * sbr_lim_gain[el->limiter_gains]; |
1083 | 6.61k | if (g_max > 1e5f) g_max = 1e5f; |
1084 | 45.6k | for (int m = m0; m < m1; m++) { |
1085 | 39.0k | if (gain[m] > g_max) { |
1086 | 11.5k | q_m[m] *= g_max / gain[m]; |
1087 | 11.5k | gain[m] = g_max; |
1088 | 11.5k | } |
1089 | 39.0k | } |
1090 | 6.61k | float den = 1e-12f; |
1091 | 45.6k | for (int m = m0; m < m1; m++) { |
1092 | 39.0k | den += e_curr[m] * gain[m] * gain[m] + s_m[m] * s_m[m]; |
1093 | 39.0k | if (!transient && s_m[m] == 0.0f) den += q_m[m] * q_m[m]; |
1094 | 39.0k | } |
1095 | 6.61k | float boost = sqrtf(so / den); |
1096 | 6.61k | if (boost > 1.584893192f) boost = 1.584893192f; |
1097 | 45.6k | for (int m = m0; m < m1; m++) { |
1098 | 39.0k | gain[m] *= boost; |
1099 | 39.0k | q_m[m] *= boost; |
1100 | 39.0k | s_m[m] *= boost; |
1101 | 39.0k | } |
1102 | 6.61k | } |
1103 | | |
1104 | | /* smoothing over the four previous envelopes (§4.6.18.7.6) */ |
1105 | | /* the history is a ring of four; hist_pos is the oldest entry */ |
1106 | 3.32k | if (prime_hist) { |
1107 | 6.37k | for (int h = 0; h < 4; h++) { |
1108 | 5.09k | memcpy(ch->g_hist[h], gain, sizeof(float) * (size_t)M); |
1109 | 5.09k | memcpy(ch->q_hist[h], q_m, sizeof(float) * (size_t)M); |
1110 | 5.09k | } |
1111 | 1.27k | prime_hist = false; |
1112 | 1.27k | } |
1113 | 3.32k | float g_filt[SBR_MAX_BANDS], q_filt[SBR_MAX_BANDS]; |
1114 | 3.32k | bool smooth = (el->smoothing_mode == 0) && !transient; |
1115 | 3.32k | if (smooth) { |
1116 | 11.2k | for (int m = 0; m < M; m++) { |
1117 | 10.6k | g_filt[m] = gain[m] * sbr_h_smooth[0]; |
1118 | 10.6k | q_filt[m] = q_m[m] * sbr_h_smooth[0]; |
1119 | 10.6k | } |
1120 | 3.40k | for (int h = 0; h < 4; h++) { |
1121 | | /* h + 1 envelopes back */ |
1122 | 2.72k | const float *gh = ch->g_hist[(ch->hist_pos + 3 - h) & 3]; |
1123 | 2.72k | const float *qh = ch->q_hist[(ch->hist_pos + 3 - h) & 3]; |
1124 | 45.1k | for (int m = 0; m < M; m++) { |
1125 | 42.4k | g_filt[m] += gh[m] * sbr_h_smooth[h + 1]; |
1126 | 42.4k | q_filt[m] += qh[m] * sbr_h_smooth[h + 1]; |
1127 | 42.4k | } |
1128 | 2.72k | } |
1129 | 2.64k | } else { |
1130 | 2.64k | memcpy(g_filt, gain, sizeof(float) * (size_t)M); |
1131 | 2.64k | if (transient) memset(q_filt, 0, sizeof(float) * (size_t)M); |
1132 | 2.15k | else memcpy(q_filt, q_m, sizeof(float) * (size_t)M); |
1133 | 2.64k | } |
1134 | 3.32k | memcpy(ch->g_hist[ch->hist_pos], gain, sizeof(float) * (size_t)M); |
1135 | 3.32k | memcpy(ch->q_hist[ch->hist_pos], q_m, sizeof(float) * (size_t)M); |
1136 | 3.32k | ch->hist_pos = (uint8_t)((ch->hist_pos + 1) & 3); |
1137 | | |
1138 | | /* Y = G X + Q noise, plus the sinusoids. The noise index runs on by |
1139 | | * one per slot and by M across the bands of a slot, and a band with |
1140 | | * a sinusoid takes no noise: its Q is zeroed so one uniform pass |
1141 | | * covers every band, then the few sinusoid bands get their tone. */ |
1142 | 42.8k | for (int m = 0; m < M; m++) if (s_m[m] != 0.0f) q_filt[m] = 0.0f; |
1143 | 3.32k | int noise0 = (ch->index_noise + 1) & 511; |
1144 | 3.32k | int phase0 = ch->index_sine; |
1145 | 3.32k | int nslots = slot1 - slot0; |
1146 | 42.8k | for (int m = 0; m < M; m++) { |
1147 | 39.5k | float (*yb)[2] = sc->y[kx + m] + slot0; |
1148 | 39.5k | float g = g_filt[m], q = q_filt[m]; |
1149 | 39.5k | int idx = (noise0 + m) & 511; |
1150 | 745k | for (int n = 0; n < nslots; n++) { |
1151 | 706k | yb[n][0] = yb[n][0] * g + q * sbr_noise_table[idx][0]; |
1152 | 706k | yb[n][1] = yb[n][1] * g + q * sbr_noise_table[idx][1]; |
1153 | 706k | idx = (idx + M) & 511; |
1154 | 706k | } |
1155 | 39.5k | if (s_m[m] != 0.0f) { |
1156 | | /* exp(j pi/2 idx), the imaginary part alternating in sign |
1157 | | * from band to band */ |
1158 | 4.81k | static const float ph_c[4] = { 1.0f, 0.0f, -1.0f, 0.0f }; |
1159 | 4.81k | static const float ph_s[4] = { 0.0f, 1.0f, 0.0f, -1.0f }; |
1160 | 4.81k | float sr = s_m[m], si = ((kx + m) & 1) ? -s_m[m] : s_m[m]; |
1161 | 61.8k | for (int n = 0; n < nslots; n++) { |
1162 | 57.0k | int ph = (phase0 + n) & 3; |
1163 | 57.0k | yb[n][0] += sr * ph_c[ph]; |
1164 | 57.0k | yb[n][1] += si * ph_s[ph]; |
1165 | 57.0k | } |
1166 | 4.81k | } |
1167 | 39.5k | } |
1168 | 3.32k | ch->index_noise = (uint16_t)((ch->index_noise + nslots * M) & 511); |
1169 | 3.32k | ch->index_sine = (uint8_t)((ch->index_sine + nslots) & 3); |
1170 | 3.32k | if (l == ch->L_E - 1) memcpy(ch->s_index_prev, s_index, (size_t)M); |
1171 | 3.32k | } |
1172 | 1.75k | } |
1173 | | |
1174 | | /* Run the analysis bank on one channel's core PCM, filling the scratch |
1175 | | * X_low buffer (previous tail + 32 new slots). */ |
1176 | | static void sbr_analyze(SBRChannel *ch, SBRScratch *sc, const float *pcm, int sbr_slots) |
1177 | 11.1k | { |
1178 | 367k | for (int k = 0; k < 32; k++) |
1179 | 356k | memcpy(sc->x_low[k], ch->x_low_tail[k], sizeof(ch->x_low_tail[k])); |
1180 | 11.1k | float slot[2][32][2]; |
1181 | 184k | for (int t = 0; t < sbr_slots; t += 2) { |
1182 | 173k | qmf_analysis_pair(ch, pcm + t * 32, slot[0], slot[1]); |
1183 | 5.71M | for (int k = 0; k < 32; k++) { |
1184 | 5.53M | sc->x_low[k][SBR_T_HFGEN + t][0] = slot[0][k][0]; |
1185 | 5.53M | sc->x_low[k][SBR_T_HFGEN + t][1] = slot[0][k][1]; |
1186 | 5.53M | sc->x_low[k][SBR_T_HFGEN + t + 1][0] = slot[1][k][0]; |
1187 | 5.53M | sc->x_low[k][SBR_T_HFGEN + t + 1][1] = slot[1][k][1]; |
1188 | 5.53M | } |
1189 | 173k | } |
1190 | 367k | for (int k = 0; k < 32; k++) |
1191 | 356k | memcpy(ch->x_low_tail[k], &sc->x_low[k][sbr_slots], sizeof(ch->x_low_tail[k])); |
1192 | 11.1k | } |
1193 | | |
1194 | | /* Assemble X from the low band and the adjusted HF, retaining slots for PS |
1195 | | * look-ahead or synthesizing each immediately into PCM, |
1196 | | * honoring the previous frame's band split where its last envelope reaches |
1197 | | * into this frame (§4.6.18.7.6 / 4.6.18.8.1). */ |
1198 | | static inline void sbr_assemble_slot(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, bool have_hf, int i, int i_temp, float (*slot)[2], float (*low_head)[SBR_T_HFGEN][2], int sbr_slots) |
1199 | 346k | { |
1200 | 346k | int n = i + SBR_T_HFADJ; |
1201 | 346k | int kx = have_hf ? ((i < i_temp) ? ch->kx_prev : el->kx) : 32; |
1202 | 346k | int kend = have_hf ? ((i < i_temp) ? ch->kx_prev + ch->M_prev : el->kx + el->M) : 32; |
1203 | 346k | if (i >= sbr_slots) kend = kx; /* look-ahead slots: low band only */ |
1204 | 346k | int low_end = kx < 32 ? kx : 32; |
1205 | 346k | int shared_end = have_hf && n < SBR_T_HFGEN && el->kx < low_end |
1206 | 346k | ? el->kx : low_end; |
1207 | 10.6M | for (int k = 0; k < shared_end; k++) { |
1208 | 10.3M | slot[k][0] = sc->x_low[k][n][0]; |
1209 | 10.3M | slot[k][1] = sc->x_low[k][n][1]; |
1210 | 10.3M | } |
1211 | 346k | for (int k = shared_end; k < low_end; k++) { |
1212 | 0 | slot[k][0] = low_head[k][n][0]; |
1213 | 0 | slot[k][1] = low_head[k][n][1]; |
1214 | 0 | } |
1215 | 346k | int high_start = kx; |
1216 | 346k | if (have_hf && high_start < el->kx) { |
1217 | 0 | int tail_end = el->kx < kend ? el->kx : kend; |
1218 | 0 | for (; high_start < tail_end; high_start++) { |
1219 | 0 | slot[high_start][0] = n < SBR_T_HFGEN ? ch->y_tail[high_start][n - SBR_T_HFADJ][0] : 0.0f; |
1220 | 0 | slot[high_start][1] = n < SBR_T_HFGEN ? ch->y_tail[high_start][n - SBR_T_HFADJ][1] : 0.0f; |
1221 | 0 | } |
1222 | 0 | } |
1223 | 1.05M | for (int k = high_start; k < kend; k++) { |
1224 | 707k | slot[k][0] = sc->y[k][n][0]; |
1225 | 707k | slot[k][1] = sc->y[k][n][1]; |
1226 | 707k | } |
1227 | 11.4M | for (int k = kend; k < 64; k++) slot[k][0] = slot[k][1] = 0.0f; |
1228 | 346k | } |
1229 | | |
1230 | | #ifndef FAAD_DISABLE_PS |
1231 | | static void sbr_assemble_hybrid_input(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, bool have_hf, int i, float (*slot)[2], float (*low_head)[SBR_T_HFGEN][2], int sbr_slots) |
1232 | 912 | { |
1233 | 912 | int n = i + SBR_T_HFADJ; |
1234 | 912 | bool previous = have_hf && i < (int)ch->t_E_end_prev - sbr_slots; |
1235 | 912 | int kx = have_hf ? (previous ? ch->kx_prev : el->kx) : 32; |
1236 | 912 | int kend = have_hf ? (previous ? ch->kx_prev + ch->M_prev : el->kx + el->M) : 32; |
1237 | 912 | if (i >= sbr_slots) kend = kx; |
1238 | 5.47k | for (int k = 0; k < 5; k++) { |
1239 | 4.56k | const float *input = NULL; |
1240 | 4.56k | if (k < kx && k < 32) |
1241 | 3.30k | input = have_hf && k >= el->kx && n < SBR_T_HFGEN |
1242 | 3.30k | ? low_head[k][n] : sc->x_low[k][n]; |
1243 | 1.25k | else if (k >= kx && k < kend) { |
1244 | 1.00k | if (have_hf && k < el->kx) { |
1245 | 0 | if (n < SBR_T_HFGEN) input = ch->y_tail[k][n - SBR_T_HFADJ]; |
1246 | 1.00k | } else input = sc->y[k][n]; |
1247 | 1.00k | } |
1248 | 4.56k | slot[k][0] = input ? input[0] : 0.0f; |
1249 | 4.56k | slot[k][1] = input ? input[1] : 0.0f; |
1250 | 4.56k | } |
1251 | 912 | } |
1252 | | |
1253 | | #endif |
1254 | | |
1255 | | static void sbr_assemble(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, bool have_hf, int nslots, float *pcm, float (*output_slot)[2], float (*low_head)[SBR_T_HFGEN][2], int sbr_slots) |
1256 | 10.8k | { |
1257 | 10.8k | int i_temp = have_hf ? (int)ch->t_E_end_prev - sbr_slots : 0; |
1258 | 10.8k | if (i_temp < 0) i_temp = 0; |
1259 | 347k | for (int i = 0; i < nslots; i++) { |
1260 | 336k | float (*slot)[2] = pcm ? output_slot : output_slot + i * 64; |
1261 | 336k | sbr_assemble_slot(el, ch, sc, have_hf, i, i_temp, slot, low_head, sbr_slots); |
1262 | 336k | if (pcm) qmf_synthesis_slot(ch, slot, pcm + i * 64); |
1263 | 336k | } |
1264 | 10.8k | } |
1265 | | |
1266 | | #ifndef FAAD_DISABLE_PS |
1267 | | static void sbr_prepare_channel(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, const float *pcm, |
1268 | | float E[SBR_MAX_ENV][SBR_MAX_BANDS], float Q[2][SBR_MAX_NQ], bool have_hf, float (*low_head)[SBR_T_HFGEN][2], int sbr_slots) |
1269 | 11.1k | { |
1270 | 11.1k | sbr_analyze(ch, sc, pcm, sbr_slots); |
1271 | | |
1272 | 11.1k | int kx = have_hf ? el->kx : 32; |
1273 | | /* A previous envelope may still use a higher crossover in the leading |
1274 | | * slots. Save those low bands before HF replaces them in shared storage. */ |
1275 | 11.1k | if (have_hf && ch->t_E_end_prev > sbr_slots) |
1276 | 92 | for (int k = kx; k < ch->kx_prev && k < 32; k++) |
1277 | 0 | memcpy(low_head[k], sc->x_low[k], sizeof(low_head[k])); |
1278 | 391k | for (int k = kx; k < SBR_MAX_BANDS; k++) { |
1279 | 380k | memset(sc->y[k], 0, sizeof(sc->y[k])); |
1280 | | /* Synthesis starts at HFADJ; the preceding HF slots are never read. */ |
1281 | 380k | memcpy(sc->y[k] + SBR_T_HFADJ, ch->y_tail[k], sizeof(ch->y_tail[k])); |
1282 | 380k | } |
1283 | | |
1284 | 11.1k | if (have_hf) { |
1285 | 1.75k | sbr_chirp(el, ch); |
1286 | 1.75k | sbr_hf_generate(el, ch, sc); |
1287 | 1.75k | sbr_hf_adjust(el, ch, sc, E, Q); |
1288 | 1.75k | } |
1289 | | |
1290 | 11.1k | } |
1291 | | |
1292 | | static void sbr_finish_channel(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, bool have_hf, int sbr_slots) |
1293 | 11.1k | { |
1294 | 11.1k | int kx = have_hf ? el->kx : 32; |
1295 | 342k | for (int k = 0; k < kx; k++) |
1296 | 331k | memset(ch->y_tail[k], 0, sizeof(ch->y_tail[k])); |
1297 | 391k | for (int k = kx; k < SBR_MAX_BANDS; k++) |
1298 | 380k | memcpy(ch->y_tail[k], &sc->y[k][sbr_slots + SBR_T_HFADJ], sizeof(ch->y_tail[k])); |
1299 | | |
1300 | 11.1k | if (have_hf) { |
1301 | | /* remember what the next frame's leading slots and deltas refer to */ |
1302 | 1.75k | int last = ch->L_E - 1; |
1303 | 1.75k | int nb = ch->freq_res[last] ? el->n_high : el->n_low; |
1304 | 1.75k | memcpy(ch->E_prev, ch->E[last], sizeof(int16_t) * (size_t)nb); |
1305 | 1.75k | ch->freq_res_prev = ch->freq_res[last]; |
1306 | 1.75k | memcpy(ch->Q_prev, ch->Q[ch->L_Q - 1], sizeof(int16_t) * el->n_q); |
1307 | 1.75k | ch->l_A_prev = ch->l_A; |
1308 | 1.75k | ch->L_E_prev = ch->L_E; |
1309 | 1.75k | ch->t_E_end_prev = (uint8_t)(2 * ch->t_E[ch->L_E]); |
1310 | 1.75k | ch->kx_prev = el->kx; |
1311 | 1.75k | ch->M_prev = el->M; |
1312 | 1.75k | } |
1313 | 11.1k | } |
1314 | | |
1315 | | #endif |
1316 | | |
1317 | | static void sbr_process_channel(const SBRElement *el, SBRChannel *ch, SBRScratch *sc, const float *pcm, |
1318 | | float E[SBR_MAX_ENV][SBR_MAX_BANDS], float Q[2][SBR_MAX_NQ], bool have_hf, int nslots, float *out_pcm, float (*output_slot)[2], float (*low_head)[SBR_T_HFGEN][2], int sbr_slots) |
1319 | 10.8k | { |
1320 | | #ifdef FAAD_DISABLE_PS |
1321 | | /* Keep no-PS processing in one function so the compiler can optimize the hot path in context. */ |
1322 | | sbr_analyze(ch, sc, pcm, sbr_slots); |
1323 | | |
1324 | | int kx = have_hf ? el->kx : 32; |
1325 | | /* A previous envelope may still use a higher crossover in the leading |
1326 | | * slots. Save those low bands before HF replaces them in shared storage. */ |
1327 | | if (have_hf && ch->t_E_end_prev > sbr_slots) |
1328 | | for (int k = kx; k < ch->kx_prev && k < 32; k++) |
1329 | | memcpy(low_head[k], sc->x_low[k], sizeof(low_head[k])); |
1330 | | for (int k = kx; k < SBR_MAX_BANDS; k++) { |
1331 | | memset(sc->y[k], 0, sizeof(sc->y[k])); |
1332 | | /* Synthesis starts at HFADJ; the preceding HF slots are never read. */ |
1333 | | memcpy(sc->y[k] + SBR_T_HFADJ, ch->y_tail[k], sizeof(ch->y_tail[k])); |
1334 | | } |
1335 | | |
1336 | | if (have_hf) { |
1337 | | sbr_chirp(el, ch); |
1338 | | sbr_hf_generate(el, ch, sc); |
1339 | | sbr_hf_adjust(el, ch, sc, E, Q); |
1340 | | } |
1341 | | |
1342 | | sbr_assemble(el, ch, sc, have_hf, nslots, out_pcm, output_slot, low_head, sbr_slots); |
1343 | | |
1344 | | for (int k = 0; k < kx; k++) |
1345 | | memset(ch->y_tail[k], 0, sizeof(ch->y_tail[k])); |
1346 | | for (int k = kx; k < SBR_MAX_BANDS; k++) |
1347 | | memcpy(ch->y_tail[k], &sc->y[k][sbr_slots + SBR_T_HFADJ], sizeof(ch->y_tail[k])); |
1348 | | |
1349 | | if (have_hf) { |
1350 | | /* remember what the next frame's leading slots and deltas refer to */ |
1351 | | int last = ch->L_E - 1; |
1352 | | int nb = ch->freq_res[last] ? el->n_high : el->n_low; |
1353 | | memcpy(ch->E_prev, ch->E[last], sizeof(int16_t) * (size_t)nb); |
1354 | | ch->freq_res_prev = ch->freq_res[last]; |
1355 | | memcpy(ch->Q_prev, ch->Q[ch->L_Q - 1], sizeof(int16_t) * el->n_q); |
1356 | | ch->l_A_prev = ch->l_A; |
1357 | | ch->L_E_prev = ch->L_E; |
1358 | | ch->t_E_end_prev = (uint8_t)(2 * ch->t_E[ch->L_E]); |
1359 | | ch->kx_prev = el->kx; |
1360 | | ch->M_prev = el->M; |
1361 | | } |
1362 | | #else |
1363 | 10.8k | sbr_prepare_channel(el, ch, sc, pcm, E, Q, have_hf, low_head, sbr_slots); |
1364 | 10.8k | sbr_assemble(el, ch, sc, have_hf, nslots, out_pcm, output_slot, low_head, sbr_slots); |
1365 | 10.8k | sbr_finish_channel(el, ch, sc, have_hf, sbr_slots); |
1366 | 10.8k | #endif |
1367 | 10.8k | } |
1368 | | |
1369 | | #else /* FAAD_DISABLE_SBR */ |
1370 | | |
1371 | | faad_status sbr_decode_extension(struct faad_decoder *dec, BitReader *bs, uint32_t ch0, uint32_t syntax_id, bool crc) |
1372 | | { |
1373 | | (void)dec; (void)bs; (void)ch0; (void)syntax_id; (void)crc; |
1374 | | return FAAD_OK; |
1375 | | } |
1376 | | #endif /* FAAD_DISABLE_SBR */ |
1377 | | |
1378 | | #if defined(FAAD_STATS) && !defined(FAAD_DISABLE_SBR) |
1379 | | /* One 'F' record for one SBR channel of one frame: dequantized envelope/ |
1380 | | * noise levels are captured here (post sbr_dequant, pre sbr_hf_adjust), so |
1381 | | * they are E_orig/Q before any decoder-side limiter or smoothing gain. */ |
1382 | | static void sbr_dump_frame(FILE *df, unsigned int frame_idx, uint32_t ch, const SBRElement *el, |
1383 | | const SBRChannel *sch, float E[SBR_MAX_ENV][SBR_MAX_BANDS], |
1384 | | float Q[2][SBR_MAX_NQ]) |
1385 | | { |
1386 | | char freq_res_s[2 * SBR_MAX_ENV + 1] = {0}; |
1387 | | for (int l = 0; l < sch->L_E; l++) { |
1388 | | char b[3]; |
1389 | | snprintf(b, sizeof(b), "%s%d", l ? "," : "", sch->freq_res[l]); |
1390 | | strncat(freq_res_s, b, sizeof(freq_res_s) - strlen(freq_res_s) - 1); |
1391 | | } |
1392 | | char invf_s[4 * SBR_MAX_NQ + 1] = {0}; |
1393 | | for (int k = 0; k < el->n_q; k++) { |
1394 | | char b[5]; |
1395 | | snprintf(b, sizeof(b), "%s%d", k ? "," : "", sch->invf_mode[k]); |
1396 | | strncat(invf_s, b, sizeof(invf_s) - strlen(invf_s) - 1); |
1397 | | } |
1398 | | int nharm = 0; |
1399 | | if (sch->add_harmonic_flag) |
1400 | | for (int i = 0; i < el->n_high; i++) if (sch->add_harmonic[i]) nharm++; |
1401 | | |
1402 | | double e_sum = 0.0; int e_n = 0; |
1403 | | for (int l = 0; l < sch->L_E; l++) { |
1404 | | int nb = sch->freq_res[l] ? el->n_high : el->n_low; |
1405 | | for (int k = 0; k < nb; k++) { |
1406 | | float e = E[l][k]; |
1407 | | e_sum += 10.0 * log10((double)(e > 1e-9f ? e : 1e-9f)); |
1408 | | e_n++; |
1409 | | } |
1410 | | } |
1411 | | double q_sum = 0.0; int q_n = 0; |
1412 | | for (int l = 0; l < sch->L_Q; l++) { |
1413 | | for (int k = 0; k < el->n_q; k++) { |
1414 | | float q = Q[l][k]; |
1415 | | q_sum += 10.0 * log10((double)(q > 1e-9f ? q : 1e-9f)); |
1416 | | q_n++; |
1417 | | } |
1418 | | } |
1419 | | fprintf(df, "F %u %u %u %u %u %s %d %s %d %d %d %d %.2f %.2f\n", |
1420 | | frame_idx, ch, sch->frame_class, sch->L_E, sch->L_Q, freq_res_s, (int)sch->amp_res, |
1421 | | invf_s, (int)sch->add_harmonic_flag, nharm, (int)el->coupling, -1, |
1422 | | e_n > 0 ? e_sum / e_n : 0.0, q_n > 0 ? q_sum / q_n : 0.0); |
1423 | | fprintf(df, "G %u %u %u %u %u", frame_idx, ch, sch->frame_class, sch->L_E, sch->bs_pointer); |
1424 | | for (int l = 0; l <= sch->L_E; l++) fprintf(df, " %u", sch->t_E[l]); |
1425 | | fprintf(df, "\n"); |
1426 | | } |
1427 | | #endif |
1428 | | |
1429 | | /* In place: channel c's core frame is read from, and its SBR output |
1430 | | * written to, pcm + c * SBR_OUT_LEN (the PS path writes channel 1 there too). */ |
1431 | 12.0k | #define SBR_OUT_LEN 2048 |
1432 | | void sbr_apply(struct faad_decoder *dec, uint32_t num_ch, float *pcm) |
1433 | 2.63k | { |
1434 | 2.63k | float *pcm_in = pcm, *pcm_out = pcm; |
1435 | 2.63k | #ifndef FAAD_DISABLE_SBR |
1436 | 2.63k | SBRScratch *sc = &dec->sbr_scratch; |
1437 | | /* Channel runs stay 2048 apart whatever the core frame length. */ |
1438 | 2.63k | uint32_t sbr_out_len = SBR_OUT_LEN; |
1439 | 2.63k | int sbr_slots = FRAME_IS_960(dec) ? 30 : 32; |
1440 | | |
1441 | | /* The first 128 spectrum floats hold streaming output; the remaining |
1442 | | * storage preserves leading low bands across a crossover change. */ |
1443 | 2.63k | float (*low_head)[SBR_T_HFGEN][2] = |
1444 | 2.63k | (float (*)[SBR_T_HFGEN][2])(dec->spec[0] + 128); |
1445 | 2.63k | float E0[SBR_MAX_ENV][SBR_MAX_BANDS], E1[SBR_MAX_ENV][SBR_MAX_BANDS]; |
1446 | 2.63k | float Q0[2][SBR_MAX_NQ], Q1[2][SBR_MAX_NQ]; |
1447 | | /* The omitted LFE element needs no SBR output or per-channel history. */ |
1448 | 2.63k | uint32_t lfe_channel = faad_downmix_lfe_channel(dec, num_ch); |
1449 | 2.63k | if (lfe_channel < num_ch) num_ch--; |
1450 | 13.0k | for (uint32_t ch = 0; ch < num_ch; ) { |
1451 | 10.7k | SBRElement *el = &dec->sbr_el[ch]; |
1452 | | /* After a change of element layout, a channel can still hold another |
1453 | | * element's payload: pair and enhance only the channels this element |
1454 | | * filled, so a coupled pair always shares one grid. A coupled |
1455 | | * element whose partner now belongs to another element has no |
1456 | | * payload of its own to apply. Only a fill moves a channel to another |
1457 | | * element, so also require this frame's element at ch to have the size |
1458 | | * it was filled for. */ |
1459 | 10.7k | bool own = dec->sbr[ch].el_ch == ch && dec->elem_nch[ch] == el->nch; |
1460 | 10.7k | bool partner = el->nch == 2 && ch + 1 < num_ch; |
1461 | 10.7k | bool pair = el->header_present && partner && own && dec->sbr[ch + 1].el_ch == ch; |
1462 | 10.7k | int nch = pair ? 2 : 1; |
1463 | 10.7k | bool have_hf = el->header_present && own && dec->sbr[ch].have_frame && |
1464 | 1.39k | (pair ? dec->sbr[ch + 1].have_frame : !partner); |
1465 | | |
1466 | 10.7k | dec->sbr[ch].unit_energy = 1.0f; |
1467 | 10.7k | if (pair) dec->sbr[ch + 1].unit_energy = 1.0f; |
1468 | 10.7k | if (have_hf) sbr_dequant(el, &dec->sbr[ch], pair ? &dec->sbr[ch + 1] : NULL, E0, Q0, E1, Q1); |
1469 | 10.7k | #ifndef FAAD_DISABLE_DRC |
1470 | 10.7k | if (have_hf && dec->drc.applied) { |
1471 | 128 | float (*energy[2])[SBR_MAX_BANDS] = { E0, E1 }; |
1472 | 291 | for (int c = 0; c < nch; c++) { |
1473 | 163 | float gain = dec->drc.sbr_gain[ch + c]; |
1474 | 163 | if (gain == 1.0f) continue; |
1475 | 140 | SBRChannel *channel = &dec->sbr[ch + c]; |
1476 | 140 | channel->unit_energy = gain; |
1477 | 472 | for (int l = 0; l < channel->L_E; l++) { |
1478 | 332 | int nb = channel->freq_res[l] ? el->n_high : el->n_low; |
1479 | 1.21k | for (int k = 0; k < nb; k++) energy[c][l][k] *= gain; |
1480 | 332 | } |
1481 | 140 | } |
1482 | 128 | } |
1483 | 10.7k | #endif |
1484 | | |
1485 | | #ifdef FAAD_STATS |
1486 | | if (have_hf) { |
1487 | | FILE *df = faad_dump_file(); |
1488 | | if (df) { |
1489 | | sbr_dump_frame(df, g_faadStats.totalFrames, ch, el, &dec->sbr[ch], E0, Q0); |
1490 | | if (pair) sbr_dump_frame(df, g_faadStats.totalFrames, ch + 1, el, &dec->sbr[ch + 1], E1, Q1); |
1491 | | } |
1492 | | } |
1493 | | #endif |
1494 | | |
1495 | 10.7k | #ifndef FAAD_DISABLE_PS |
1496 | 10.7k | if (dec->ps_seen && num_ch == 1 && dec->config.downmix_mode != FAAD_DOWNMIX_MONO) { |
1497 | 306 | SBRChannel *sch = &dec->sbr[0]; |
1498 | 306 | sbr_prepare_channel(el, sch, sc, pcm_in, E0, Q0, have_hf, low_head, sbr_slots); |
1499 | 306 | dec->num_channels = 2; |
1500 | 306 | float (*slot)[2] = (float (*)[2])dec->spec[0]; |
1501 | 306 | float L[64][2], R[64][2]; |
1502 | 306 | bool ps_active = dec->ps->start; |
1503 | 306 | if (ps_active) ps_frame_begin(dec, have_hf ? el->kx + el->M : 32); |
1504 | 306 | if (ps_active) { |
1505 | 274 | if (!have_hf || (el->kx >= 5 && |
1506 | 250 | (sch->t_E_end_prev <= sbr_slots || sch->kx_prev >= 5))) { |
1507 | | /* These bands stay below both crossovers, so their entire |
1508 | | * look-ahead lines can be copied without slot assembly. */ |
1509 | 1.50k | for (int k = 0; k < 5; k++) |
1510 | 1.25k | memcpy(dec->ps->in_buf[k] + 6, sc->x_low[k] + SBR_T_HFADJ, |
1511 | 1.25k | sizeof(float[PS_IN_SLOTS][2])); |
1512 | 250 | } else { |
1513 | 936 | for (int i = 0; i < PS_IN_SLOTS; i++) { |
1514 | 912 | sbr_assemble_hybrid_input(el, sch, sc, have_hf, i, slot, low_head, sbr_slots); |
1515 | 912 | ps_frame_input(dec, i, slot); |
1516 | 912 | } |
1517 | 24 | } |
1518 | 274 | } |
1519 | 306 | int i_temp = have_hf ? (int)sch->t_E_end_prev - sbr_slots : 0; |
1520 | 306 | if (i_temp < 0) i_temp = 0; |
1521 | 9.76k | for (int i = 0; i < sbr_slots; i++) { |
1522 | 9.45k | sbr_assemble_slot(el, sch, sc, have_hf, i, i_temp, slot, low_head, sbr_slots); |
1523 | 9.45k | if (ps_active) { |
1524 | 8.47k | ps_slot(dec, i, slot, L, R); |
1525 | 8.47k | qmf_synthesis_slot(&dec->sbr[0], L, pcm_out + i * 64); |
1526 | 8.47k | qmf_synthesis_slot(&dec->sbr[1], R, pcm_out + SBR_OUT_LEN + i * 64); |
1527 | 8.47k | } else { |
1528 | 984 | qmf_synthesis_slot(sch, slot, pcm_out + i * 64); |
1529 | 984 | qmf_synthesis_slot(&dec->sbr[1], slot, pcm_out + SBR_OUT_LEN + i * 64); |
1530 | 984 | } |
1531 | 9.45k | } |
1532 | 306 | sbr_finish_channel(el, sch, sc, have_hf, sbr_slots); |
1533 | 306 | return; |
1534 | 306 | } |
1535 | 10.4k | #endif |
1536 | | /* All core IMDCTs have finished; their spectra are dead until the next |
1537 | | * packet. Concealment history lives in prev_spec, so one QMF output |
1538 | | * slot can reuse the first spectrum without adding stack or heap RAM. */ |
1539 | 10.4k | float (*output_slot)[2] = (float (*)[2])dec->spec[0]; |
1540 | 21.2k | for (int c = 0; c < nch; c++) { |
1541 | 10.8k | SBRChannel *sch = &dec->sbr[ch + c]; |
1542 | 10.8k | uint32_t channel = ch + (uint32_t)c; |
1543 | 10.8k | sbr_process_channel(el, sch, sc, pcm_in + channel * sbr_out_len, c ? E1 : E0, c ? Q1 : Q0, have_hf, sbr_slots, pcm_out + channel * sbr_out_len, output_slot, low_head, sbr_slots); |
1544 | 10.8k | } |
1545 | 10.4k | ch += (uint32_t)nch; |
1546 | 10.4k | } |
1547 | | #else |
1548 | | (void)dec; |
1549 | | (void)pcm_in; (void)pcm_out; |
1550 | | uint32_t core_len = FRAME_IS_960(dec) ? 960 : 1024; |
1551 | | uint32_t sbr_out_len = SBR_OUT_LEN; |
1552 | | /* no SBR decoder: linear 2x upsampling, backwards so it can run in place */ |
1553 | | for (uint32_t ch = 0; ch < num_ch; ch++) { |
1554 | | float *p = pcm + ch * sbr_out_len; |
1555 | | for (int i = (int)core_len - 1; i >= 0; i--) { |
1556 | | float sample = p[i], prev = i ? p[i - 1] : sample; |
1557 | | p[i * 2 + 1] = sample; |
1558 | | p[i * 2] = 0.5f * (prev + sample); |
1559 | | } |
1560 | | } |
1561 | | #endif |
1562 | 2.63k | } |