Raw ↗
+ +
3b489b4 OpenAVX v0.2.1 - Cross-platform MSVC & macOS CoreMediaIO portability hardening
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * src/avx_audio.c - Audio resampling and channel downmixing
7 */
8  
9 #define _POSIX_C_SOURCE 200809L
10 #include "avx_audio.h"
11 #include "avx_simd.h"
12 #include "avx_platform.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17  
18 #ifndef M_PI
19 #define M_PI 3.14159265358979323846
20 #endif
21  
22 #define INV_SQRT2 0.7071067811865475f
23  
24 #if defined(__x86_64__) || defined(_M_X64)
25 #include <immintrin.h>
26 #define AUDIO_HAS_X86_SIMD 1
27 #endif
28  
29 #if defined(__aarch64__) || defined(_M_ARM64) || defined(__ARM_NEON)
30 #include <arm_neon.h>
31 #define AUDIO_HAS_ARM_NEON 1
32 #endif
33  
34 /* ========================================================================= */
35 /* 1. SCALAR DOWNMIXING IMPLEMENTATIONS */
36 /* ========================================================================= */
37  
38 static inline float clamp_f32(float v) {
39 if (v < -1.0f) return -1.0f;
40 if (v > 1.0f) return 1.0f;
41 return v;
42 }
43  
44 /* Downmixes 5.1 surround to stereo following ITU-R BS.775 (C and surrounds scaled by 1/sqrt(2), LFE omitted). */
45 static void scalar_downmix_5_1(
46 const float* const in[6],
47 float* restrict out_l,
48 float* restrict out_r,
49 int n,
50 float scale,
51 bool normalize_peak
52 ) {
53 const float* fl = in[0];
54 const float* fr = in[1];
55 const float* c = in[2];
56 /* in[3] is LFE, omitted in ITU-R BS.775 standard stereo downmix */
57 const float* bl = in[4];
58 const float* br = in[5];
59  
60 float max_peak = 0.0f;
61  
62 for (int i = 0; i < n; ++i) {
63 float l = (fl[i] + INV_SQRT2 * c[i] + INV_SQRT2 * bl[i]) * scale;
64 float r = (fr[i] + INV_SQRT2 * c[i] + INV_SQRT2 * br[i]) * scale;
65  
66 out_l[i] = l;
67 out_r[i] = r;
68  
69 if (normalize_peak) {
70 float al = fabsf(l);
71 float ar = fabsf(r);
72 if (al > max_peak) max_peak = al;
73 if (ar > max_peak) max_peak = ar;
74 }
75 }
76  
77 if (normalize_peak && max_peak > 1.0f) {
78 float inv = 1.0f / max_peak;
79 for (int i = 0; i < n; ++i) {
80 out_l[i] *= inv;
81 out_r[i] *= inv;
82 }
83 } else if (!normalize_peak && scale == 1.0f) {
84 for (int i = 0; i < n; ++i) {
85 out_l[i] = clamp_f32(out_l[i]);
86 out_r[i] = clamp_f32(out_r[i]);
87 }
88 }
89 }
90  
91 /* Dolby Pro Logic Lt/Rt matrix downmix: applies +90 deg phase shift to Ls and -90 deg to Rs. */
92 static void scalar_downmix_5_1_ltrt(
93 const float* const in[6],
94 float* restrict out_l,
95 float* restrict out_r,
96 int n,
97 float scale,
98 bool normalize_peak
99 ) {
100 const float* fl = in[0];
101 const float* fr = in[1];
102 const float* c = in[2];
103 /* in[3] is LFE, omitted in standard Lt/Rt matrix downmix */
104 const float* bl = in[4];
105 const float* br = in[5];
106  
107 float max_peak = 0.0f;
108  
109 for (int i = 0; i < n; ++i) {
110 float fl_val = fl ? fl[i] : 0.0f;
111 float fr_val = fr ? fr[i] : 0.0f;
112 float c_val = c ? c[i] : 0.0f;
113 float bl_val = bl ? bl[i] : 0.0f;
114 float br_val = br ? br[i] : 0.0f;
115  
116 float c_term = INV_SQRT2 * c_val;
117 float surr_term = INV_SQRT2 * (bl_val + br_val);
118  
119 float l = (fl_val + c_term - surr_term) * scale;
120 float r = (fr_val + c_term + surr_term) * scale;
121  
122 out_l[i] = l;
123 out_r[i] = r;
124  
125 if (normalize_peak) {
126 float al = fabsf(l);
127 float ar = fabsf(r);
128 if (al > max_peak) max_peak = al;
129 if (ar > max_peak) max_peak = ar;
130 }
131 }
132  
133 if (normalize_peak && max_peak > 1.0f) {
134 float inv = 1.0f / max_peak;
135 for (int i = 0; i < n; ++i) {
136 out_l[i] *= inv;
137 out_r[i] *= inv;
138 }
139 } else if (!normalize_peak && scale == 1.0f) {
140 for (int i = 0; i < n; ++i) {
141 out_l[i] = clamp_f32(out_l[i]);
142 out_r[i] = clamp_f32(out_r[i]);
143 }
144 }
145 }
146  
147 static void scalar_downmix_7_1(
148 const float* const in[8],
149 float* restrict out_l,
150 float* restrict out_r,
151 int n,
152 float scale,
153 bool normalize_peak
154 ) {
155 const float* fl = in[0];
156 const float* fr = in[1];
157 const float* c = in[2];
158 /* in[3] is LFE */
159 const float* bl = in[4];
160 const float* br = in[5];
161 const float* sl = in[6];
162 const float* sr = in[7];
163  
164 float max_peak = 0.0f;
165  
166 for (int i = 0; i < n; ++i) {
167 float l = (fl[i] + INV_SQRT2 * c[i] + INV_SQRT2 * bl[i] + INV_SQRT2 * sl[i]) * scale;
168 float r = (fr[i] + INV_SQRT2 * c[i] + INV_SQRT2 * br[i] + INV_SQRT2 * sr[i]) * scale;
169  
170 out_l[i] = l;
171 out_r[i] = r;
172  
173 if (normalize_peak) {
174 float al = fabsf(l);
175 float ar = fabsf(r);
176 if (al > max_peak) max_peak = al;
177 if (ar > max_peak) max_peak = ar;
178 }
179 }
180  
181 if (normalize_peak && max_peak > 1.0f) {
182 float inv = 1.0f / max_peak;
183 for (int i = 0; i < n; ++i) {
184 out_l[i] *= inv;
185 out_r[i] *= inv;
186 }
187 } else if (!normalize_peak && scale == 1.0f) {
188 for (int i = 0; i < n; ++i) {
189 out_l[i] = clamp_f32(out_l[i]);
190 out_r[i] = clamp_f32(out_r[i]);
191 }
192 }
193 }
194  
195 static void scalar_downmix_quad(
196 const float* const in[4],
197 float* restrict out_l,
198 float* restrict out_r,
199 int n,
200 float scale,
201 bool normalize_peak
202 ) {
203 const float* fl = in[0];
204 const float* fr = in[1];
205 const float* bl = in[2];
206 const float* br = in[3];
207  
208 float max_peak = 0.0f;
209  
210 for (int i = 0; i < n; ++i) {
211 float l = (fl[i] + INV_SQRT2 * bl[i]) * scale;
212 float r = (fr[i] + INV_SQRT2 * br[i]) * scale;
213  
214 out_l[i] = l;
215 out_r[i] = r;
216  
217 if (normalize_peak) {
218 float al = fabsf(l);
219 float ar = fabsf(r);
220 if (al > max_peak) max_peak = al;
221 if (ar > max_peak) max_peak = ar;
222 }
223 }
224  
225 if (normalize_peak && max_peak > 1.0f) {
226 float inv = 1.0f / max_peak;
227 for (int i = 0; i < n; ++i) {
228 out_l[i] *= inv;
229 out_r[i] *= inv;
230 }
231 } else if (!normalize_peak && scale == 1.0f) {
232 for (int i = 0; i < n; ++i) {
233 out_l[i] = clamp_f32(out_l[i]);
234 out_r[i] = clamp_f32(out_r[i]);
235 }
236 }
237 }
238  
239 /* ========================================================================= */
240 /* 2. AVX2 VECTORIZED DOWNMIXING */
241 /* ========================================================================= */
242  
243 #if AUDIO_HAS_X86_SIMD
244  
245 AVX_TARGET_AVX2
246 static void avx2_downmix_5_1(
247 const float* const in[6],
248 float* restrict out_l,
249 float* restrict out_r,
250 int n,
251 float scale,
252 bool normalize_peak
253 ) {
254 const float* fl = in[0];
255 const float* fr = in[1];
256 const float* c = in[2];
257 const float* bl = in[4];
258 const float* br = in[5];
259  
260 __m256 vc = _mm256_set1_ps(INV_SQRT2);
261 __m256 vscale = _mm256_set1_ps(scale);
262 __m256 v_max_peak = _mm256_setzero_ps();
263 __m256 v_min = _mm256_set1_ps(-1.0f);
264 __m256 v_max = _mm256_set1_ps(1.0f);
265  
266 int vec_len = (n / 8) * 8;
267 for (int i = 0; i < vec_len; i += 8) {
268 __m256 l_fl = _mm256_loadu_ps(fl + i);
269 __m256 r_fr = _mm256_loadu_ps(fr + i);
270 __m256 m_c = _mm256_loadu_ps(c + i);
271 __m256 l_bl = _mm256_loadu_ps(bl + i);
272 __m256 r_br = _mm256_loadu_ps(br + i);
273  
274 __m256 l = _mm256_fmadd_ps(m_c, vc, l_fl);
275 l = _mm256_fmadd_ps(l_bl, vc, l);
276 l = _mm256_mul_ps(l, vscale);
277  
278 __m256 r = _mm256_fmadd_ps(m_c, vc, r_fr);
279 r = _mm256_fmadd_ps(r_br, vc, r);
280 r = _mm256_mul_ps(r, vscale);
281  
282 if (normalize_peak) {
283 __m256 al = _mm256_andnot_ps(_mm256_set1_ps(-0.0f), l);
284 __m256 ar = _mm256_andnot_ps(_mm256_set1_ps(-0.0f), r);
285 v_max_peak = _mm256_max_ps(v_max_peak, al);
286 v_max_peak = _mm256_max_ps(v_max_peak, ar);
287 } else if (scale == 1.0f) {
288 l = _mm256_min_ps(_mm256_max_ps(l, v_min), v_max);
289 r = _mm256_min_ps(_mm256_max_ps(r, v_min), v_max);
290 }
291  
292 _mm256_storeu_ps(out_l + i, l);
293 _mm256_storeu_ps(out_r + i, r);
294 }
295  
296 /* Process remainder scalar */
297 float scalar_peak = 0.0f;
298 for (int i = vec_len; i < n; ++i) {
299 float l = (fl[i] + INV_SQRT2 * c[i] + INV_SQRT2 * bl[i]) * scale;
300 float r = (fr[i] + INV_SQRT2 * c[i] + INV_SQRT2 * br[i]) * scale;
301 out_l[i] = l;
302 out_r[i] = r;
303  
304 if (normalize_peak) {
305 float al = fabsf(l);
306 float ar = fabsf(r);
307 if (al > scalar_peak) scalar_peak = al;
308 if (ar > scalar_peak) scalar_peak = ar;
309 } else if (scale == 1.0f) {
310 out_l[i] = clamp_f32(out_l[i]);
311 out_r[i] = clamp_f32(out_r[i]);
312 }
313 }
314  
315 if (normalize_peak) {
316 float peaks[8];
317 _mm256_storeu_ps(peaks, v_max_peak);
318 float peak = scalar_peak;
319 for (int k = 0; k < 8; ++k) {
320 if (peaks[k] > peak) peak = peaks[k];
321 }
322 if (peak > 1.0f) {
323 float inv = 1.0f / peak;
324 __m256 vinv = _mm256_set1_ps(inv);
325 for (int i = 0; i < vec_len; i += 8) {
326 __m256 l = _mm256_mul_ps(_mm256_loadu_ps(out_l + i), vinv);
327 __m256 r = _mm256_mul_ps(_mm256_loadu_ps(out_r + i), vinv);
328 _mm256_storeu_ps(out_l + i, l);
329 _mm256_storeu_ps(out_r + i, r);
330 }
331 for (int i = vec_len; i < n; ++i) {
332 out_l[i] *= inv;
333 out_r[i] *= inv;
334 }
335 }
336 }
337 }
338  
339 AVX_TARGET_AVX2
340 static void avx2_downmix_5_1_ltrt(
341 const float* const in[6],
342 float* restrict out_l,
343 float* restrict out_r,
344 int n,
345 float scale,
346 bool normalize_peak
347 ) {
348 const float* fl = in[0];
349 const float* fr = in[1];
350 const float* c = in[2];
351 const float* bl = in[4];
352 const float* br = in[5];
353  
354 __m256 v_inv_sqrt2 = _mm256_set1_ps(INV_SQRT2);
355 __m256 v_scale = _mm256_set1_ps(scale);
356 __m256 v_min = _mm256_set1_ps(-1.0f);
357 __m256 v_max = _mm256_set1_ps(1.0f);
358 __m256 v_max_peak = _mm256_setzero_ps();
359  
360 int vec_len = n & ~7;
361 for (int i = 0; i < vec_len; i += 8) {
362 __m256 v_fl = _mm256_loadu_ps(fl + i);
363 __m256 v_fr = _mm256_loadu_ps(fr + i);
364 __m256 v_c = _mm256_loadu_ps(c + i);
365 __m256 v_bl = _mm256_loadu_ps(bl + i);
366 __m256 v_br = _mm256_loadu_ps(br + i);
367  
368 __m256 v_c_term = _mm256_mul_ps(v_c, v_inv_sqrt2);
369 __m256 v_surround = _mm256_mul_ps(_mm256_add_ps(v_bl, v_br), v_inv_sqrt2);
370  
371 __m256 l = _mm256_mul_ps(_mm256_sub_ps(_mm256_add_ps(v_fl, v_c_term), v_surround), v_scale);
372 __m256 r = _mm256_mul_ps(_mm256_add_ps(_mm256_add_ps(v_fr, v_c_term), v_surround), v_scale);
373  
374 if (normalize_peak) {
375 __m256 abs_l = _mm256_andnot_ps(_mm256_set1_ps(-0.0f), l);
376 __m256 abs_r = _mm256_andnot_ps(_mm256_set1_ps(-0.0f), r);
377 v_max_peak = _mm256_max_ps(v_max_peak, _mm256_max_ps(abs_l, abs_r));
378 } else if (scale == 1.0f) {
379 l = _mm256_min_ps(_mm256_max_ps(l, v_min), v_max);
380 r = _mm256_min_ps(_mm256_max_ps(r, v_min), v_max);
381 }
382  
383 _mm256_storeu_ps(out_l + i, l);
384 _mm256_storeu_ps(out_r + i, r);
385 }
386  
387 /* Process remainder scalar */
388 float scalar_peak = 0.0f;
389 for (int i = vec_len; i < n; ++i) {
390 float c_term = INV_SQRT2 * c[i];
391 float surr_term = INV_SQRT2 * (bl[i] + br[i]);
392 float l = (fl[i] + c_term - surr_term) * scale;
393 float r = (fr[i] + c_term + surr_term) * scale;
394 out_l[i] = l;
395 out_r[i] = r;
396  
397 if (normalize_peak) {
398 float al = fabsf(l);
399 float ar = fabsf(r);
400 if (al > scalar_peak) scalar_peak = al;
401 if (ar > scalar_peak) scalar_peak = ar;
402 } else if (scale == 1.0f) {
403 out_l[i] = clamp_f32(out_l[i]);
404 out_r[i] = clamp_f32(out_r[i]);
405 }
406 }
407  
408 if (normalize_peak) {
409 float peaks[8];
410 _mm256_storeu_ps(peaks, v_max_peak);
411 float peak = scalar_peak;
412 for (int k = 0; k < 8; ++k) {
413 if (peaks[k] > peak) peak = peaks[k];
414 }
415 if (peak > 1.0f) {
416 float inv = 1.0f / peak;
417 __m256 vinv = _mm256_set1_ps(inv);
418 for (int i = 0; i < vec_len; i += 8) {
419 __m256 l = _mm256_mul_ps(_mm256_loadu_ps(out_l + i), vinv);
420 __m256 r = _mm256_mul_ps(_mm256_loadu_ps(out_r + i), vinv);
421 _mm256_storeu_ps(out_l + i, l);
422 _mm256_storeu_ps(out_r + i, r);
423 }
424 for (int i = vec_len; i < n; ++i) {
425 out_l[i] *= inv;
426 out_r[i] *= inv;
427 }
428 }
429 }
430 }
431  
432 AVX_TARGET_AVX2
433 static void avx2_sinc_convolve_channels(
434 float** fifo,
435 int curr_offset,
436 const float* weights,
437 float* const* out_channels,
438 int out_idx,
439 int channels
440 ) {
441 for (int c = 0; c < channels; ++c) {
442 const float *a = fifo[c] + curr_offset;
443 const float *b = weights;
444 __m256 va0 = _mm256_loadu_ps(a + 0);
445 __m256 vb0 = _mm256_loadu_ps(b + 0);
446 __m256 acc = _mm256_mul_ps(va0, vb0);
447  
448 __m256 va1 = _mm256_loadu_ps(a + 8);
449 __m256 vb1 = _mm256_loadu_ps(b + 8);
450 acc = _mm256_fmadd_ps(va1, vb1, acc);
451  
452 __m256 va2 = _mm256_loadu_ps(a + 16);
453 __m256 vb2 = _mm256_loadu_ps(b + 16);
454 acc = _mm256_fmadd_ps(va2, vb2, acc);
455  
456 __m256 va3 = _mm256_loadu_ps(a + 24);
457 __m256 vb3 = _mm256_loadu_ps(b + 24);
458 acc = _mm256_fmadd_ps(va3, vb3, acc);
459  
460 __m128 lo = _mm256_castps256_ps128(acc);
461 __m128 hi = _mm256_extractf128_ps(acc, 1);
462 __m128 sum128 = _mm_add_ps(lo, hi);
463 sum128 = _mm_hadd_ps(sum128, sum128);
464 sum128 = _mm_hadd_ps(sum128, sum128);
465 out_channels[c][out_idx] = _mm_cvtss_f32(sum128);
466 }
467 }
468  
469 AVX_TARGET_AVX2
470 static inline void avx2_cubic_interpolate_stereo(
471 float** fifo,
472 int curr_idx,
473 float wm1, float w0, float w1, float w2,
474 float* const* out_channels,
475 int out_idx
476 ) {
477 __m128 vw = _mm_set_ps(w2, w1, w0, wm1);
478 __m128 y_l = _mm_loadu_ps(fifo[0] + curr_idx - 1);
479 __m128 y_r = _mm_loadu_ps(fifo[1] + curr_idx - 1);
480  
481 __m128 prod_l = _mm_mul_ps(y_l, vw);
482 __m128 prod_r = _mm_mul_ps(y_r, vw);
483  
484 __m128 sum_lr = _mm_hadd_ps(prod_l, prod_r);
485 sum_lr = _mm_hadd_ps(sum_lr, sum_lr);
486  
487 out_channels[0][out_idx] = _mm_cvtss_f32(sum_lr);
488 out_channels[1][out_idx] = _mm_cvtss_f32(_mm_shuffle_ps(sum_lr, sum_lr, _MM_SHUFFLE(1, 1, 1, 1)));
489 }
490  
491 #endif /* AUDIO_HAS_X86_SIMD */
492  
493 /* ========================================================================= */
494 /* 3. ARM64 NEON VECTORIZED DOWNMIXING */
495 /* ========================================================================= */
496  
497 #if AUDIO_HAS_ARM_NEON
498  
499 static void neon_downmix_5_1(
500 const float* const in[6],
501 float* restrict out_l,
502 float* restrict out_r,
503 int n,
504 float scale,
505 bool normalize_peak
506 ) {
507 const float* fl = in[0];
508 const float* fr = in[1];
509 const float* c = in[2];
510 const float* bl = in[4];
511 const float* br = in[5];
512  
513 float32x4_t vc = vdupq_n_f32(INV_SQRT2);
514 float32x4_t vscale = vdupq_n_f32(scale);
515 float32x4_t v_max_peak = vdupq_n_f32(0.0f);
516 float32x4_t v_min = vdupq_n_f32(-1.0f);
517 float32x4_t v_max = vdupq_n_f32(1.0f);
518  
519 int vec_len = (n / 4) * 4;
520 for (int i = 0; i < vec_len; i += 4) {
521 float32x4_t l_fl = vld1q_f32(fl + i);
522 float32x4_t r_fr = vld1q_f32(fr + i);
523 float32x4_t m_c = vld1q_f32(c + i);
524 float32x4_t l_bl = vld1q_f32(bl + i);
525 float32x4_t r_br = vld1q_f32(br + i);
526  
527 float32x4_t l = vmlaq_f32(l_fl, m_c, vc);
528 l = vmlaq_f32(l, l_bl, vc);
529 l = vmulq_f32(l, vscale);
530  
531 float32x4_t r = vmlaq_f32(r_fr, m_c, vc);
532 r = vmlaq_f32(r, r_br, vc);
533 r = vmulq_f32(r, vscale);
534  
535 if (normalize_peak) {
536 float32x4_t al = vabsq_f32(l);
537 float32x4_t ar = vabsq_f32(r);
538 v_max_peak = vmaxq_f32(v_max_peak, al);
539 v_max_peak = vmaxq_f32(v_max_peak, ar);
540 } else if (scale == 1.0f) {
541 l = vminq_f32(vmaxq_f32(l, v_min), v_max);
542 r = vminq_f32(vmaxq_f32(r, v_min), v_max);
543 }
544  
545 vst1q_f32(out_l + i, l);
546 vst1q_f32(out_r + i, r);
547 }
548  
549 float scalar_peak = 0.0f;
550 for (int i = vec_len; i < n; ++i) {
551 float l = (fl[i] + INV_SQRT2 * c[i] + INV_SQRT2 * bl[i]) * scale;
552 float r = (fr[i] + INV_SQRT2 * c[i] + INV_SQRT2 * br[i]) * scale;
553 out_l[i] = l;
554 out_r[i] = r;
555  
556 if (normalize_peak) {
557 float al = fabsf(l);
558 float ar = fabsf(r);
559 if (al > scalar_peak) scalar_peak = al;
560 if (ar > scalar_peak) scalar_peak = ar;
561 } else if (scale == 1.0f) {
562 out_l[i] = clamp_f32(out_l[i]);
563 out_r[i] = clamp_f32(out_r[i]);
564 }
565 }
566  
567 if (normalize_peak) {
568 float peaks[4];
569 vst1q_f32(peaks, v_max_peak);
570 float peak = scalar_peak;
571 for (int k = 0; k < 4; ++k) {
572 if (peaks[k] > peak) peak = peaks[k];
573 }
574 if (peak > 1.0f) {
575 float inv = 1.0f / peak;
576 float32x4_t vinv = vdupq_n_f32(inv);
577 for (int i = 0; i < vec_len; i += 4) {
578 float32x4_t l = vmulq_f32(vld1q_f32(out_l + i), vinv);
579 float32x4_t r = vmulq_f32(vld1q_f32(out_r + i), vinv);
580 vst1q_f32(out_l + i, l);
581 vst1q_f32(out_r + i, r);
582 }
583 for (int i = vec_len; i < n; ++i) {
584 out_l[i] *= inv;
585 out_r[i] *= inv;
586 }
587 }
588 }
589 }
590  
591 static void neon_downmix_5_1_ltrt(
592 const float* const in[6],
593 float* restrict out_l,
594 float* restrict out_r,
595 int n,
596 float scale,
597 bool normalize_peak
598 ) {
599 const float* fl = in[0];
600 const float* fr = in[1];
601 const float* c = in[2];
602 const float* bl = in[4];
603 const float* br = in[5];
604  
605 float32x4_t vc = vdupq_n_f32(INV_SQRT2);
606 float32x4_t vscale = vdupq_n_f32(scale);
607 float32x4_t v_max_peak = vdupq_n_f32(0.0f);
608 float32x4_t v_min = vdupq_n_f32(-1.0f);
609 float32x4_t v_max = vdupq_n_f32(1.0f);
610  
611 int vec_len = (n / 4) * 4;
612 for (int i = 0; i < vec_len; i += 4) {
613 float32x4_t l_fl = vld1q_f32(fl + i);
614 float32x4_t r_fr = vld1q_f32(fr + i);
615 float32x4_t m_c = vld1q_f32(c + i);
616 float32x4_t l_bl = vld1q_f32(bl + i);
617 float32x4_t r_br = vld1q_f32(br + i);
618  
619 float32x4_t surr = vaddq_f32(l_bl, r_br);
620 float32x4_t c_term = vmulq_f32(m_c, vc);
621 float32x4_t surr_term = vmulq_f32(surr, vc);
622  
623 float32x4_t l = vsubq_f32(vaddq_f32(l_fl, c_term), surr_term);
624 l = vmulq_f32(l, vscale);
625  
626 float32x4_t r = vaddq_f32(vaddq_f32(r_fr, c_term), surr_term);
627 r = vmulq_f32(r, vscale);
628  
629 if (normalize_peak) {
630 float32x4_t al = vabsq_f32(l);
631 float32x4_t ar = vabsq_f32(r);
632 v_max_peak = vmaxq_f32(v_max_peak, al);
633 v_max_peak = vmaxq_f32(v_max_peak, ar);
634 } else if (scale == 1.0f) {
635 l = vminq_f32(vmaxq_f32(l, v_min), v_max);
636 r = vminq_f32(vmaxq_f32(r, v_min), v_max);
637 }
638  
639 vst1q_f32(out_l + i, l);
640 vst1q_f32(out_r + i, r);
641 }
642  
643 float scalar_peak = 0.0f;
644 for (int i = vec_len; i < n; ++i) {
645 float c_term = INV_SQRT2 * c[i];
646 float surr_term = INV_SQRT2 * (bl[i] + br[i]);
647 float l = (fl[i] + c_term - surr_term) * scale;
648 float r = (fr[i] + c_term + surr_term) * scale;
649 out_l[i] = l;
650 out_r[i] = r;
651  
652 if (normalize_peak) {
653 float al = fabsf(l);
654 float ar = fabsf(r);
655 if (al > scalar_peak) scalar_peak = al;
656 if (ar > scalar_peak) scalar_peak = ar;
657 } else if (scale == 1.0f) {
658 out_l[i] = clamp_f32(out_l[i]);
659 out_r[i] = clamp_f32(out_r[i]);
660 }
661 }
662  
663 if (normalize_peak) {
664 float peaks[4];
665 vst1q_f32(peaks, v_max_peak);
666 float peak = scalar_peak;
667 for (int k = 0; k < 4; ++k) {
668 if (peaks[k] > peak) peak = peaks[k];
669 }
670 if (peak > 1.0f) {
671 float inv = 1.0f / peak;
672 float32x4_t vinv = vdupq_n_f32(inv);
673 for (int i = 0; i < vec_len; i += 4) {
674 float32x4_t l = vmulq_f32(vld1q_f32(out_l + i), vinv);
675 float32x4_t r = vmulq_f32(vld1q_f32(out_r + i), vinv);
676 vst1q_f32(out_l + i, l);
677 vst1q_f32(out_r + i, r);
678 }
679 for (int i = vec_len; i < n; ++i) {
680 out_l[i] *= inv;
681 out_r[i] *= inv;
682 }
683 }
684 }
685 }
686  
687 static void neon_sinc_convolve_channels(
688 float** fifo,
689 int curr_offset,
690 const float* weights,
691 float* const* out_channels,
692 int out_idx,
693 int channels
694 ) {
695 for (int c = 0; c < channels; ++c) {
696 const float *a = fifo[c] + curr_offset;
697 const float *b = weights;
698  
699 float32x4_t acc0 = vmulq_f32(vld1q_f32(a + 0), vld1q_f32(b + 0));
700 float32x4_t acc1 = vmulq_f32(vld1q_f32(a + 4), vld1q_f32(b + 4));
701  
702 acc0 = vmlaq_f32(acc0, vld1q_f32(a + 8), vld1q_f32(b + 8));
703 acc1 = vmlaq_f32(acc1, vld1q_f32(a + 12), vld1q_f32(b + 12));
704  
705 acc0 = vmlaq_f32(acc0, vld1q_f32(a + 16), vld1q_f32(b + 16));
706 acc1 = vmlaq_f32(acc1, vld1q_f32(a + 20), vld1q_f32(b + 20));
707  
708 acc0 = vmlaq_f32(acc0, vld1q_f32(a + 24), vld1q_f32(b + 24));
709 acc1 = vmlaq_f32(acc1, vld1q_f32(a + 28), vld1q_f32(b + 28));
710  
711 float32x4_t sum4 = vaddq_f32(acc0, acc1);
712 out_channels[c][out_idx] = vaddvq_f32(sum4);
713 }
714 }
715  
716 static inline void neon_cubic_interpolate_stereo(
717 float** fifo,
718 int curr_idx,
719 float wm1, float w0, float w1, float w2,
720 float* const* out_channels,
721 int out_idx
722 ) {
723 float32x4_t vw = { wm1, w0, w1, w2 };
724 float32x4_t y_l = vld1q_f32(fifo[0] + curr_idx - 1);
725 float32x4_t y_r = vld1q_f32(fifo[1] + curr_idx - 1);
726  
727 out_channels[0][out_idx] = vaddvq_f32(vmulq_f32(y_l, vw));
728 out_channels[1][out_idx] = vaddvq_f32(vmulq_f32(y_r, vw));
729 }
730  
731 #endif /* AUDIO_HAS_ARM_NEON */
732  
733 /* ========================================================================= */
734 /* 4. PUBLIC DOWNMIXING APIS */
735 /* ========================================================================= */
736  
737 void avx_audio_downmix_5_1_to_stereo(
738 const float* const in_channels[6],
739 float* out_left,
740 float* out_right,
741 int frames,
742 avx_audio_normalize_e normalize_mode
743 ) {
744 if (!in_channels || !out_left || !out_right || frames <= 0) return;
745 if (!in_channels[0] || !in_channels[1] || !in_channels[2] || !in_channels[4] || !in_channels[5]) return;
746  
747 float scale = 1.0f;
748 bool peak_norm = (normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
749 if (normalize_mode == AVX_AUDIO_NORMALIZE_ATTEN_3DB) {
750 /* Total sum of weights for L: 1.0 (FL) + 0.7071 (C) + 0.7071 (BL) = 2.41421356 */
751 scale = 1.0f / (1.0f + 2.0f * INV_SQRT2);
752 }
753  
754 avx_simd_type_e caps = avx_simd_get_caps();
755  
756 #if AUDIO_HAS_X86_SIMD
757 if (caps == AVX_SIMD_AVX2) {
758 avx2_downmix_5_1(in_channels, out_left, out_right, frames, scale, peak_norm);
759 return;
760 }
761 #elif AUDIO_HAS_ARM_NEON
762 if (caps == AVX_SIMD_NEON) {
763 neon_downmix_5_1(in_channels, out_left, out_right, frames, scale, peak_norm);
764 return;
765 }
766 #else
767 (void)caps;
768 #endif
769  
770 scalar_downmix_5_1(in_channels, out_left, out_right, frames, scale, peak_norm);
771 }
772  
773 void avx_audio_downmix_5_1_to_ltrt(
774 const float* const in_channels[6],
775 float* out_left,
776 float* out_right,
777 int frames,
778 avx_audio_normalize_e normalize_mode
779 ) {
780 if (!in_channels || !out_left || !out_right || frames <= 0) return;
781  
782 float scale = 1.0f;
783 bool peak_norm = (normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
784 if (normalize_mode == AVX_AUDIO_NORMALIZE_ATTEN_3DB) {
785 /* Total sum of weights for Lt: 1.0 (FL) + 0.7071 (C) + 0.7071 (BL) + 0.7071 (BR) = 3.12132 */
786 scale = 1.0f / (1.0f + 3.0f * INV_SQRT2);
787 }
788  
789 if (!in_channels[0] || !in_channels[1] || !in_channels[2] ||
790 !in_channels[4] || !in_channels[5]) {
791 scalar_downmix_5_1_ltrt(in_channels, out_left, out_right, frames, scale, peak_norm);
792 return;
793 }
794  
795 avx_simd_type_e caps = avx_simd_get_caps();
796  
797 #if AUDIO_HAS_X86_SIMD
798 if (caps == AVX_SIMD_AVX2) {
799 avx2_downmix_5_1_ltrt(in_channels, out_left, out_right, frames, scale, peak_norm);
800 return;
801 }
802 #elif AUDIO_HAS_ARM_NEON
803 if (caps == AVX_SIMD_NEON) {
804 neon_downmix_5_1_ltrt(in_channels, out_left, out_right, frames, scale, peak_norm);
805 return;
806 }
807 #else
808 (void)caps;
809 #endif
810  
811 scalar_downmix_5_1_ltrt(in_channels, out_left, out_right, frames, scale, peak_norm);
812 }
813  
814 void avx_audio_downmix_7_1_to_stereo(
815 const float* const in_channels[8],
816 float* out_left,
817 float* out_right,
818 int frames,
819 avx_audio_normalize_e normalize_mode
820 ) {
821 if (!in_channels || !out_left || !out_right || frames <= 0) return;
822 if (!in_channels[0] || !in_channels[1] || !in_channels[2] || !in_channels[4] ||
823 !in_channels[5] || !in_channels[6] || !in_channels[7]) return;
824  
825 float scale = 1.0f;
826 bool peak_norm = (normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
827 if (normalize_mode == AVX_AUDIO_NORMALIZE_ATTEN_3DB) {
828 /* Total sum of weights for L: 1.0 + 3 * 0.7071 = 3.12132 */
829 scale = 1.0f / (1.0f + 3.0f * INV_SQRT2);
830 }
831  
832 scalar_downmix_7_1(in_channels, out_left, out_right, frames, scale, peak_norm);
833 }
834  
835 void avx_audio_downmix_quad_to_stereo(
836 const float* const in_channels[4],
837 float* out_left,
838 float* out_right,
839 int frames,
840 avx_audio_normalize_e normalize_mode
841 ) {
842 if (!in_channels || !out_left || !out_right || frames <= 0) return;
843 if (!in_channels[0] || !in_channels[1] || !in_channels[2] || !in_channels[3]) return;
844  
845 float scale = 1.0f;
846 bool peak_norm = (normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
847 if (normalize_mode == AVX_AUDIO_NORMALIZE_ATTEN_3DB) {
848 /* 1.0 + 0.7071 = 1.7071 */
849 scale = 1.0f / (1.0f + INV_SQRT2);
850 }
851  
852 scalar_downmix_quad(in_channels, out_left, out_right, frames, scale, peak_norm);
853 }
854  
855 void avx_audio_downmix_stereo_to_mono(
856 const float* in_left,
857 const float* in_right,
858 float* out_mono,
859 int frames,
860 avx_audio_normalize_e normalize_mode
861 ) {
862 if (!in_left || !in_right || !out_mono || frames <= 0) return;
863  
864 float scale = 0.5f;
865 if (normalize_mode == AVX_AUDIO_NORMALIZE_NONE) {
866 scale = 1.0f;
867 }
868  
869 float max_peak = 0.0f;
870 bool peak_norm = (normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
871  
872 for (int i = 0; i < frames; ++i) {
873 float m = (in_left[i] + in_right[i]) * scale;
874 out_mono[i] = m;
875 if (peak_norm) {
876 float am = fabsf(m);
877 if (am > max_peak) max_peak = am;
878 } else if (scale == 1.0f) {
879 out_mono[i] = clamp_f32(out_mono[i]);
880 }
881 }
882  
883 if (peak_norm && max_peak > 1.0f) {
884 float inv = 1.0f / max_peak;
885 for (int i = 0; i < frames; ++i) {
886 out_mono[i] *= inv;
887 }
888 }
889 }
890  
891 void avx_audio_matrix_mix(
892 const float* const* in_channels,
893 int in_channel_count,
894 float* const* out_channels,
895 int out_channel_count,
896 const float* matrix,
897 int frames,
898 avx_audio_normalize_e normalize_mode
899 ) {
900 if (!in_channels || in_channel_count <= 0 ||
901 !out_channels || out_channel_count <= 0 ||
902 !matrix || frames <= 0) {
903 return;
904 }
905  
906 float max_peak = 0.0f;
907 bool peak_norm = (normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
908  
909 /* Compute coefficient sum normalization per output channel if ATTEN_3DB */
910 float* norm_scales = (float*)malloc(out_channel_count * sizeof(float));
911 if (!norm_scales) return;
912  
913 for (int o = 0; o < out_channel_count; ++o) {
914 if (normalize_mode == AVX_AUDIO_NORMALIZE_ATTEN_3DB) {
915 float sum_w = 0.0f;
916 for (int in = 0; in < in_channel_count; ++in) {
917 sum_w += fabsf(matrix[o * in_channel_count + in]);
918 }
919 norm_scales[o] = (sum_w > 1.0f) ? (1.0f / sum_w) : 1.0f;
920 } else {
921 norm_scales[o] = 1.0f;
922 }
923 }
924  
925 for (int o = 0; o < out_channel_count; ++o) {
926 float* dst = out_channels[o];
927 const float* row = matrix + o * in_channel_count;
928 float scale = norm_scales[o];
929  
930 for (int i = 0; i < frames; ++i) {
931 float acc = 0.0f;
932 for (int in = 0; in < in_channel_count; ++in) {
933 acc += row[in] * in_channels[in][i];
934 }
935 acc *= scale;
936 dst[i] = acc;
937  
938 if (peak_norm) {
939 float a = fabsf(acc);
940 if (a > max_peak) max_peak = a;
941 } else if (scale == 1.0f) {
942 dst[i] = clamp_f32(dst[i]);
943 }
944 }
945 }
946  
947 free(norm_scales);
948  
949 if (peak_norm && max_peak > 1.0f) {
950 float inv = 1.0f / max_peak;
951 for (int o = 0; o < out_channel_count; ++o) {
952 float* dst = out_channels[o];
953 for (int i = 0; i < frames; ++i) {
954 dst[i] *= inv;
955 }
956 }
957 }
958 }
959  
960 bool avx_audio_frame_downmix_to_stereo(
961 const oavx_audio_frame_t* p_src_frame,
962 oavx_audio_frame_t* p_dst_frame,
963 avx_audio_normalize_e normalize_mode
964 ) {
965 if (!p_src_frame || !p_src_frame->planes[0] || p_src_frame->frames == 0 || !p_dst_frame) {
966 return false;
967 }
968  
969 int in_channels = (int)p_src_frame->channels;
970 int frames = (int)p_src_frame->frames;
971 size_t src_stride = p_src_frame->stride_bytes;
972 if (src_stride == 0) {
973 src_stride = (size_t)frames * sizeof(float);
974 }
975 size_t stride_samples = src_stride / sizeof(float);
976  
977 int dst_channels = 2;
978 int dst_stride = frames * (int)sizeof(float);
979 float* dst_data = (float*)malloc((size_t)dst_channels * dst_stride);
980 if (!dst_data) return false;
981  
982 float* out_l = dst_data;
983 float* out_r = dst_data + frames;
984  
985 if (in_channels == 1) {
986 /* Mono -> Duplicate to Stereo */
987 const float* in_m = (const float*)p_src_frame->planes[0];
988 memcpy(out_l, in_m, frames * sizeof(float));
989 memcpy(out_r, in_m, frames * sizeof(float));
990 } else if (in_channels == 2) {
991 /* Already stereo: copy channels */
992 const float* in_l = (const float*)p_src_frame->planes[0];
993 const float* in_r = p_src_frame->planes[1] ? (const float*)p_src_frame->planes[1] :
994 ((const float*)p_src_frame->planes[0] + stride_samples);
995 memcpy(out_l, in_l, frames * sizeof(float));
996 memcpy(out_r, in_r, frames * sizeof(float));
997 } else if (in_channels == 4) {
998 /* Quad 4.0 */
999 const float* in_ch[4];
1000 for (int c = 0; c < 4; ++c) {
1001 in_ch[c] = p_src_frame->planes[c] ? (const float*)p_src_frame->planes[c] :
1002 ((const float*)p_src_frame->planes[0] + c * stride_samples);
1003 }
1004 avx_audio_downmix_quad_to_stereo(in_ch, out_l, out_r, frames, normalize_mode);
1005 } else if (in_channels == 6) {
1006 /* 5.1 Surround */
1007 const float* in_ch[6];
1008 for (int c = 0; c < 6; ++c) {
1009 in_ch[c] = p_src_frame->planes[c] ? (const float*)p_src_frame->planes[c] :
1010 ((const float*)p_src_frame->planes[0] + c * stride_samples);
1011 }
1012 avx_audio_downmix_5_1_to_stereo(in_ch, out_l, out_r, frames, normalize_mode);
1013 } else if (in_channels == 8) {
1014 /* 7.1 Surround */
1015 const float* in_ch[8];
1016 for (int c = 0; c < 8; ++c) {
1017 in_ch[c] = p_src_frame->planes[c] ? (const float*)p_src_frame->planes[c] :
1018 ((const float*)p_src_frame->planes[0] + c * stride_samples);
1019 }
1020 avx_audio_downmix_7_1_to_stereo(in_ch, out_l, out_r, frames, normalize_mode);
1021 } else {
1022 /* Arbitrary layout: mix even channels to L, odd channels to R */
1023 float* out_ch[2] = { out_l, out_r };
1024 float* matrix = (float*)calloc(2 * in_channels, sizeof(float));
1025 if (!matrix) {
1026 free(dst_data);
1027 return false;
1028 }
1029 for (int c = 0; c < in_channels; ++c) {
1030 int out_idx = c % 2;
1031 matrix[out_idx * in_channels + c] = 1.0f;
1032 }
1033 const float** in_ptrs = (const float**)malloc(in_channels * sizeof(float*));
1034 if (!in_ptrs) {
1035 free(matrix);
1036 free(dst_data);
1037 return false;
1038 }
1039 for (int c = 0; c < in_channels; ++c) {
1040 in_ptrs[c] = (c < OAVX_MAX_AUDIO_CHANNELS && p_src_frame->planes[c]) ? (const float*)p_src_frame->planes[c] :
1041 ((const float*)p_src_frame->planes[0] + (size_t)c * stride_samples);
1042 }
1043 avx_audio_matrix_mix(in_ptrs, in_channels, out_ch, 2, matrix, frames, normalize_mode);
1044 free(in_ptrs);
1045 free(matrix);
1046 }
1047  
1048 memset(p_dst_frame, 0, sizeof(*p_dst_frame));
1049 p_dst_frame->channels = (uint16_t)dst_channels;
1050 p_dst_frame->frames = (uint32_t)frames;
1051 p_dst_frame->sample_rate = p_src_frame->sample_rate;
1052 p_dst_frame->format = OAVX_AUDIO_FMT_FLT_PLANAR;
1053 p_dst_frame->pts_ns = p_src_frame->pts_ns;
1054 p_dst_frame->channel_mask = OAVX_AUDIO_MASK_STEREO;
1055 p_dst_frame->stride_bytes = (size_t)dst_stride;
1056 p_dst_frame->planes[0] = dst_data;
1057 p_dst_frame->planes[1] = dst_data + frames;
1058 for (int c = 2; c < OAVX_MAX_AUDIO_CHANNELS; ++c) p_dst_frame->planes[c] = NULL;
1059 p_dst_frame->metadata = p_src_frame->metadata;
1060 p_dst_frame->metadata_len = p_src_frame->metadata_len;
1061 p_dst_frame->user_ctx = NULL;
1062  
1063 return true;
1064 }
1065  
1066 void avx_audio_frame_free_data(oavx_audio_frame_t* p_frame) {
1067 if (p_frame && p_frame->planes[0]) {
1068 free(p_frame->planes[0]);
1069 for (int c = 0; c < OAVX_MAX_AUDIO_CHANNELS; ++c) p_frame->planes[c] = NULL;
1070 }
1071 }
1072  
1073 /* ========================================================================= */
1074 /* 5. BROADCAST RESAMPLING ENGINE */
1075 /* ========================================================================= */
1076  
1077 static uint64_t gcd_u64(uint64_t a, uint64_t b) {
1078 while (b != 0) {
1079 uint64_t t = b;
1080 b = a % b;
1081 a = t;
1082 }
1083 return a;
1084 }
1085  
1086 struct avx_resampler_t {
1087 int in_rate;
1088 int out_rate;
1089 int channels;
1090 avx_audio_resample_mode_e mode;
1091  
1092 uint64_t step_num;
1093 uint64_t step_den;
1094 uint64_t phase_frac;
1095  
1096 int lookbehind;
1097 int lookahead;
1098  
1099 float** fifo;
1100 int fifo_cap;
1101 int fifo_count;
1102 bool primed;
1103 };
1104  
1105 static inline float sinc_func(float x) {
1106 if (fabsf(x) < 1e-6f) return 1.0f;
1107 float px = (float)M_PI * x;
1108 return sinf(px) / px;
1109 }
1110  
1111 static inline float blackman_window(float x, float radius) {
1112 if (fabsf(x) >= radius) return 0.0f;
1113 float norm = (x + radius) / (2.0f * radius);
1114 return 0.42f - 0.5f * cosf(2.0f * (float)M_PI * norm) + 0.08f * cosf(4.0f * (float)M_PI * norm);
1115 }
1116  
1117 static void scalar_sinc_convolve_channels(
1118 float** fifo,
1119 int curr_offset,
1120 const float* weights,
1121 float* const* out_channels,
1122 int out_idx,
1123 int channels
1124 ) {
1125 for (int c = 0; c < channels; ++c) {
1126 const float *a = fifo[c] + curr_offset;
1127 float sum = 0.0f;
1128 for (int j = 0; j < 32; ++j) {
1129 sum += a[j] * weights[j];
1130 }
1131 out_channels[c][out_idx] = sum;
1132 }
1133 }
1134  
1135 /*
1136 * Creates polyphase sinc resampler. Computes reduced rational conversion factor (P/Q)
1137 * and pre-computes Kaiser-windowed sinc filter table for arbitrary sample rate pairs.
1138 */
1139 avx_resampler_t* avx_resampler_create(
1140 int in_rate,
1141 int out_rate,
1142 int channels,
1143 avx_audio_resample_mode_e mode
1144 ) {
1145 if (in_rate <= 0 || out_rate <= 0 || channels <= 0) return NULL;
1146  
1147 avx_resampler_t* r = (avx_resampler_t*)calloc(1, sizeof(*r));
1148 if (!r) return NULL;
1149  
1150 r->in_rate = in_rate;
1151 r->out_rate = out_rate;
1152 r->channels = channels;
1153 r->mode = mode;
1154  
1155 uint64_t g = gcd_u64((uint64_t)in_rate, (uint64_t)out_rate);
1156 r->step_num = (uint64_t)in_rate / g;
1157 r->step_den = (uint64_t)out_rate / g;
1158 r->phase_frac = 0;
1159  
1160 if (mode == AVX_AUDIO_RESAMPLE_LINEAR) {
1161 r->lookbehind = 0;
1162 r->lookahead = 1;
1163 } else if (mode == AVX_AUDIO_RESAMPLE_CUBIC) {
1164 r->lookbehind = 1;
1165 r->lookahead = 2;
1166 } else {
1167 r->lookbehind = 16;
1168 r->lookahead = 16;
1169 }
1170  
1171 r->fifo_cap = 4096;
1172 r->fifo = (float**)calloc((size_t)channels, sizeof(float*));
1173 if (!r->fifo) {
1174 free(r);
1175 return NULL;
1176 }
1177  
1178 for (int c = 0; c < channels; ++c) {
1179 r->fifo[c] = (float*)malloc((size_t)r->fifo_cap * sizeof(float));
1180 if (!r->fifo[c]) {
1181 for (int k = 0; k < c; ++k) free(r->fifo[k]);
1182 free(r->fifo);
1183 free(r);
1184 return NULL;
1185 }
1186 }
1187  
1188 r->fifo_count = 0;
1189 r->primed = false;
1190  
1191 return r;
1192 }
1193  
1194 void avx_resampler_reset(avx_resampler_t* r) {
1195 if (!r) return;
1196 r->phase_frac = 0;
1197 r->fifo_count = 0;
1198 r->primed = false;
1199 }
1200  
1201 void avx_resampler_destroy(avx_resampler_t* r) {
1202 if (!r) return;
1203 for (int c = 0; c < r->channels; ++c) {
1204 free(r->fifo[c]);
1205 }
1206 free(r->fifo);
1207 free(r);
1208 }
1209  
1210 /* Convolves input audio through polyphase filter bank with history ring buffers and SIMD MAC. */
1211 int avx_resampler_process(
1212 avx_resampler_t* r,
1213 const float* const* in_channels,
1214 int in_samples,
1215 float* const* out_channels,
1216 int max_out_samples
1217 ) {
1218 if (!r || !out_channels || max_out_samples <= 0) return 0;
1219  
1220 /* Expand FIFO if needed */
1221 int needed = r->fifo_count + in_samples + (r->primed ? 0 : r->lookbehind);
1222 if (needed > r->fifo_cap) {
1223 int new_cap = r->fifo_cap * 2;
1224 while (new_cap < needed) new_cap *= 2;
1225 for (int c = 0; c < r->channels; ++c) {
1226 float* nb = (float*)realloc(r->fifo[c], (size_t)new_cap * sizeof(float));
1227 if (!nb) return 0;
1228 r->fifo[c] = nb;
1229 }
1230 r->fifo_cap = new_cap;
1231 }
1232  
1233 /* Prime FIFO if first call */
1234 if (!r->primed && in_samples > 0 && in_channels) {
1235 for (int c = 0; c < r->channels; ++c) {
1236 float first_val = in_channels[c][0];
1237 for (int k = 0; k < r->lookbehind; ++k) {
1238 r->fifo[c][k] = first_val;
1239 }
1240 }
1241 r->fifo_count = r->lookbehind;
1242 r->primed = true;
1243 }
1244  
1245 /* Append incoming samples */
1246 if (in_samples > 0 && in_channels) {
1247 for (int c = 0; c < r->channels; ++c) {
1248 memcpy(r->fifo[c] + r->fifo_count, in_channels[c], (size_t)in_samples * sizeof(float));
1249 }
1250 r->fifo_count += in_samples;
1251 }
1252  
1253 int curr_idx = r->lookbehind;
1254 int out_idx = 0;
1255 uint64_t phase = r->phase_frac;
1256 uint64_t step_n = r->step_num;
1257 uint64_t step_d = r->step_den;
1258  
1259 float fc = 1.0f;
1260 if (r->out_rate < r->in_rate) {
1261 fc = (float)r->out_rate / (float)r->in_rate;
1262 }
1263  
1264 avx_simd_type_e caps = avx_simd_get_caps();
1265  
1266 while (curr_idx + r->lookahead < r->fifo_count && out_idx < max_out_samples) {
1267 float mu = (float)phase / (float)step_d;
1268  
1269 if (r->mode == AVX_AUDIO_RESAMPLE_LINEAR) {
1270 for (int c = 0; c < r->channels; ++c) {
1271 float y0 = r->fifo[c][curr_idx];
1272 float y1 = r->fifo[c][curr_idx + 1];
1273 out_channels[c][out_idx] = (1.0f - mu) * y0 + mu * y1;
1274 }
1275 } else if (r->mode == AVX_AUDIO_RESAMPLE_CUBIC) {
1276 float mu2 = mu * mu;
1277 float mu3 = mu2 * mu;
1278 float wm1 = -0.5f * mu3 + 1.0f * mu2 - 0.5f * mu;
1279 float w0 = 1.5f * mu3 - 2.5f * mu2 + 1.0f;
1280 float w1 = -1.5f * mu3 + 2.0f * mu2 + 0.5f * mu;
1281 float w2 = 0.5f * mu3 - 0.5f * mu2;
1282  
1283 #if AUDIO_HAS_X86_SIMD
1284 if (caps == AVX_SIMD_AVX2 && r->channels == 2) {
1285 avx2_cubic_interpolate_stereo(r->fifo, curr_idx, wm1, w0, w1, w2, out_channels, out_idx);
1286 } else
1287 #elif AUDIO_HAS_ARM_NEON
1288 if (caps == AVX_SIMD_NEON && r->channels == 2) {
1289 neon_cubic_interpolate_stereo(r->fifo, curr_idx, wm1, w0, w1, w2, out_channels, out_idx);
1290 } else
1291 #endif
1292 {
1293 for (int c = 0; c < r->channels; ++c) {
1294 float ym1 = r->fifo[c][curr_idx - 1];
1295 float y0 = r->fifo[c][curr_idx];
1296 float y1 = r->fifo[c][curr_idx + 1];
1297 float y2 = r->fifo[c][curr_idx + 2];
1298 out_channels[c][out_idx] = wm1 * ym1 + w0 * y0 + w1 * y1 + w2 * y2;
1299 }
1300 }
1301 } else if (r->mode == AVX_AUDIO_RESAMPLE_SINC) {
1302 int radius = r->lookbehind;
1303 if (radius == 16) {
1304 float weights[32];
1305 float wsum = 0.0f;
1306 for (int j = 0; j < 32; ++j) {
1307 float t = (float)(j - 15) - mu;
1308 float w = sinc_func(fc * t) * blackman_window(t, (float)radius);
1309 weights[j] = w;
1310 wsum += w;
1311 }
1312 float inv_wsum = (fabsf(wsum) > 1e-6f) ? (1.0f / wsum) : 1.0f;
1313 for (int j = 0; j < 32; ++j) {
1314 weights[j] *= inv_wsum;
1315 }
1316  
1317 #if AUDIO_HAS_X86_SIMD
1318 if (caps == AVX_SIMD_AVX2) {
1319 avx2_sinc_convolve_channels(r->fifo, curr_idx - 15, weights, out_channels, out_idx, r->channels);
1320 } else
1321 #elif AUDIO_HAS_ARM_NEON
1322 if (caps == AVX_SIMD_NEON) {
1323 neon_sinc_convolve_channels(r->fifo, curr_idx - 15, weights, out_channels, out_idx, r->channels);
1324 } else
1325 #endif
1326 {
1327 scalar_sinc_convolve_channels(r->fifo, curr_idx - 15, weights, out_channels, out_idx, r->channels);
1328 }
1329 } else {
1330 for (int c = 0; c < r->channels; ++c) {
1331 float sum = 0.0f;
1332 float wsum = 0.0f;
1333 for (int j = -radius + 1; j <= radius; ++j) {
1334 float t = (float)j - mu;
1335 float w = sinc_func(fc * t) * blackman_window(t, (float)radius);
1336 sum += r->fifo[c][curr_idx + j] * w;
1337 wsum += w;
1338 }
1339 out_channels[c][out_idx] = (fabsf(wsum) > 1e-6f) ? (sum / wsum) : sum;
1340 }
1341 }
1342 }
1343  
1344 out_idx++;
1345 phase += step_n;
1346 uint64_t adv = phase / step_d;
1347 phase %= step_d;
1348 curr_idx += (int)adv;
1349 }
1350  
1351 r->phase_frac = phase;
1352  
1353 /* Shift FIFO: retain tail lookbehind samples before curr_idx */
1354 int shift_start = curr_idx - r->lookbehind;
1355 if (shift_start > 0) {
1356 int remaining = r->fifo_count - shift_start;
1357 if (remaining > 0) {
1358 for (int c = 0; c < r->channels; ++c) {
1359 memmove(r->fifo[c], r->fifo[c] + shift_start, (size_t)remaining * sizeof(float));
1360 }
1361 r->fifo_count = remaining;
1362 } else {
1363 r->fifo_count = 0;
1364 }
1365 }
1366  
1367 return out_idx;
1368 }
1369  
1370 int avx_resampler_flush(
1371 avx_resampler_t* r,
1372 float* const* out_channels,
1373 int max_out_samples
1374 ) {
1375 if (!r || !out_channels || max_out_samples <= 0 || r->fifo_count == 0) return 0;
1376  
1377 /* Pad lookahead samples by replicating the last sample in the FIFO */
1378 int pad = r->lookahead;
1379 int needed = r->fifo_count + pad;
1380 if (needed > r->fifo_cap) {
1381 int new_cap = r->fifo_cap * 2;
1382 while (new_cap < needed) new_cap *= 2;
1383 for (int c = 0; c < r->channels; ++c) {
1384 float* nb = (float*)realloc(r->fifo[c], (size_t)new_cap * sizeof(float));
1385 if (!nb) return 0;
1386 r->fifo[c] = nb;
1387 }
1388 r->fifo_cap = new_cap;
1389 }
1390  
1391 for (int c = 0; c < r->channels; ++c) {
1392 float last_val = r->fifo[c][r->fifo_count - 1];
1393 for (int k = 0; k < pad; ++k) {
1394 r->fifo[c][r->fifo_count + k] = last_val;
1395 }
1396 }
1397 r->fifo_count += pad;
1398  
1399 /* Drain the remaining samples */
1400 int n = avx_resampler_process(r, NULL, 0, out_channels, max_out_samples);
1401 return n;
1402 }
1403  
1404 bool avx_audio_frame_resample(
1405 avx_resampler_t* resampler,
1406 const oavx_audio_frame_t* p_src_frame,
1407 oavx_audio_frame_t* p_dst_frame
1408 ) {
1409 if (!resampler || !p_src_frame || !p_src_frame->planes[0] ||
1410 p_src_frame->frames == 0 || !p_dst_frame) {
1411 return false;
1412 }
1413  
1414 int in_channels = (int)p_src_frame->channels;
1415 if (in_channels <= 0 || in_channels != resampler->channels) {
1416 return false;
1417 }
1418  
1419 size_t src_stride = p_src_frame->stride_bytes;
1420 if (src_stride == 0) {
1421 src_stride = (size_t)p_src_frame->frames * sizeof(float);
1422 }
1423 size_t stride_samples = src_stride / sizeof(float);
1424  
1425 /* Estimate upper bound on output samples */
1426 int max_out = (int)(((int64_t)p_src_frame->frames * resampler->out_rate +
1427 resampler->in_rate - 1) / resampler->in_rate) + 128;
1428  
1429 float* dst_data = (float*)malloc((size_t)in_channels * max_out * sizeof(float));
1430 if (!dst_data) return false;
1431  
1432 const float** in_ptrs = (const float**)calloc((size_t)in_channels, sizeof(float*));
1433 float** out_ptrs = (float**)calloc((size_t)in_channels, sizeof(float*));
1434 if (!in_ptrs || !out_ptrs) {
1435 free(dst_data);
1436 if (in_ptrs) free((void*)in_ptrs);
1437 if (out_ptrs) free(out_ptrs);
1438 return false;
1439 }
1440  
1441 for (int c = 0; c < in_channels; ++c) {
1442 in_ptrs[c] = (c < OAVX_MAX_AUDIO_CHANNELS && p_src_frame->planes[c]) ? (const float*)p_src_frame->planes[c] :
1443 ((const float*)p_src_frame->planes[0] + (size_t)c * stride_samples);
1444 out_ptrs[c] = dst_data + (size_t)c * max_out;
1445 }
1446  
1447 int generated = avx_resampler_process(resampler, in_ptrs, (int)p_src_frame->frames, out_ptrs, max_out);
1448  
1449 /* Compact channel stride to exact generated sample count */
1450 int dst_stride = generated * (int)sizeof(float);
1451 if (generated < max_out) {
1452 for (int c = 1; c < in_channels; ++c) {
1453 memmove(dst_data + (size_t)c * generated, out_ptrs[c], (size_t)generated * sizeof(float));
1454 }
1455 }
1456  
1457 free((void*)in_ptrs);
1458 free(out_ptrs);
1459  
1460 memset(p_dst_frame, 0, sizeof(*p_dst_frame));
1461 p_dst_frame->channels = (uint16_t)in_channels;
1462 p_dst_frame->frames = (uint32_t)generated;
1463 p_dst_frame->sample_rate = (uint32_t)resampler->out_rate;
1464 p_dst_frame->format = OAVX_AUDIO_FMT_FLT_PLANAR;
1465 p_dst_frame->pts_ns = p_src_frame->pts_ns;
1466 p_dst_frame->channel_mask = p_src_frame->channel_mask;
1467 p_dst_frame->stride_bytes = (size_t)dst_stride;
1468 p_dst_frame->planes[0] = dst_data;
1469 for (int c = 0; c < in_channels && c < OAVX_MAX_AUDIO_CHANNELS; ++c) {
1470 p_dst_frame->planes[c] = dst_data + (size_t)c * generated;
1471 }
1472 for (int c = in_channels; c < OAVX_MAX_AUDIO_CHANNELS; ++c) {
1473 p_dst_frame->planes[c] = NULL;
1474 }
1475 p_dst_frame->metadata = p_src_frame->metadata;
1476 p_dst_frame->metadata_len = p_src_frame->metadata_len;
1477 p_dst_frame->user_ctx = NULL;
1478  
1479 return true;
1480 }
1481  
1482 int avx_audio_resample_simple(
1483 const float* in_data,
1484 int in_samples,
1485 int in_rate,
1486 float* out_data,
1487 int max_out_samples,
1488 int out_rate,
1489 avx_audio_resample_mode_e mode
1490 ) {
1491 if (!in_data || in_samples <= 0 || in_rate <= 0 ||
1492 !out_data || max_out_samples <= 0 || out_rate <= 0) {
1493 return -1;
1494 }
1495  
1496 avx_resampler_t* r = avx_resampler_create(in_rate, out_rate, 1, mode);
1497 if (!r) return -1;
1498  
1499 const float* in_ch[1] = { in_data };
1500 float* out_ch[1] = { out_data };
1501  
1502 int n = avx_resampler_process(r, in_ch, in_samples, out_ch, max_out_samples);
1503 if (n < max_out_samples) {
1504 float* flushed_ch[1] = { out_data + n };
1505 int fn = avx_resampler_flush(r, flushed_ch, max_out_samples - n);
1506 n += fn;
1507 }
1508  
1509 avx_resampler_destroy(r);
1510 return n;
1511 }
1512  
1513 /* ========================================================================= */
1514 /* 3. ADAPTIVE AUDIO JITTER BUFFER & CLOCK SLEWING ENGINE */
1515 /* ========================================================================= */
1516  
1517 #define AVX_JITTER_MAX_CHANNELS 8
1518 #define AVX_JITTER_CAPACITY_SAMPLES (192000) /* 4 seconds at 48kHz */
1519 #define AVX_JITTER_FADE_SAMPLES 96 /* 2 ms smooth crossfade at 48kHz */
1520  
1521 #if defined(AVX_OS_WINDOWS)
1522 static inline uint64_t get_jitter_time_ns(void) {
1523 LARGE_INTEGER freq, count;
1524 QueryPerformanceFrequency(&freq);
1525 QueryPerformanceCounter(&count);
1526 return (uint64_t)((count.QuadPart * 1000000000ULL) / freq.QuadPart);
1527 }
1528 #else
1529 static inline uint64_t get_jitter_time_ns(void) {
1530 struct timespec ts;
1531 clock_gettime(CLOCK_MONOTONIC, &ts);
1532 return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
1533 }
1534 #endif
1535  
1536 struct avx_audio_jitter_t {
1537 avx_mutex_t lock;
1538 int sample_rate;
1539 int channels;
1540  
1541 /* Elastic Planar Ring Buffer */
1542 float* ring[AVX_JITTER_MAX_CHANNELS];
1543 size_t ring_head;
1544 size_t ring_tail;
1545 size_t ring_count;
1546 size_t capacity;
1547  
1548 /* Adaptive Cushion Targets (in samples) */
1549 size_t target_cushion;
1550 size_t min_cushion;
1551 size_t max_cushion;
1552 size_t base_cushion;
1553  
1554 /* Pre-roll state */
1555 bool primed;
1556  
1557 /* Jitter Estimation */
1558 uint64_t last_push_time_ns;
1559 float current_jitter_ms;
1560  
1561 /* Micro-Slewing / Clock Drift Compensation */
1562 double phase_accum;
1563  
1564 /* Concealment State */
1565 bool in_starvation;
1566 int fade_out_remaining;
1567 int fade_in_remaining;
1568 float last_samples[AVX_JITTER_MAX_CHANNELS];
1569 };
1570  
1571 avx_audio_jitter_t* avx_audio_jitter_create(const avx_audio_jitter_config_t* cfg) {
1572 if (!cfg || cfg->sample_rate <= 0 || cfg->channels <= 0 || cfg->channels > AVX_JITTER_MAX_CHANNELS) {
1573 return NULL;
1574 }
1575  
1576 avx_audio_jitter_t* jb = (avx_audio_jitter_t*)calloc(1, sizeof(avx_audio_jitter_t));
1577 if (!jb) return NULL;
1578  
1579 avx_mutex_init(&jb->lock);
1580 jb->sample_rate = cfg->sample_rate;
1581 jb->channels = cfg->channels;
1582 jb->capacity = AVX_JITTER_CAPACITY_SAMPLES;
1583  
1584 int target_ms = cfg->target_cushion_ms > 0 ? cfg->target_cushion_ms : 15;
1585 int min_ms = cfg->min_cushion_ms > 0 ? cfg->min_cushion_ms : 5;
1586 int max_ms = cfg->max_cushion_ms > 0 ? cfg->max_cushion_ms : 40;
1587  
1588 jb->base_cushion = (size_t)((target_ms * jb->sample_rate) / 1000);
1589 jb->min_cushion = (size_t)((min_ms * jb->sample_rate) / 1000);
1590 jb->max_cushion = (size_t)((max_ms * jb->sample_rate) / 1000);
1591 jb->target_cushion = jb->base_cushion;
1592  
1593 for (int c = 0; c < jb->channels; ++c) {
1594 jb->ring[c] = (float*)calloc(jb->capacity, sizeof(float));
1595 if (!jb->ring[c]) {
1596 avx_audio_jitter_destroy(jb);
1597 return NULL;
1598 }
1599 }
1600  
1601 jb->primed = false;
1602 jb->in_starvation = false;
1603 return jb;
1604 }
1605  
1606 void avx_audio_jitter_destroy(avx_audio_jitter_t* jb) {
1607 if (!jb) return;
1608 for (int c = 0; c < AVX_JITTER_MAX_CHANNELS; ++c) {
1609 if (jb->ring[c]) {
1610 free(jb->ring[c]);
1611 jb->ring[c] = NULL;
1612 }
1613 }
1614 avx_mutex_destroy(&jb->lock);
1615 free(jb);
1616 }
1617  
1618 void avx_audio_jitter_reset(avx_audio_jitter_t* jb) {
1619 if (!jb) return;
1620 avx_mutex_lock(&jb->lock);
1621 jb->ring_head = 0;
1622 jb->ring_tail = 0;
1623 jb->ring_count = 0;
1624 jb->primed = false;
1625 jb->in_starvation = false;
1626 jb->fade_out_remaining = 0;
1627 jb->fade_in_remaining = 0;
1628 jb->last_push_time_ns = 0;
1629 jb->phase_accum = 0.0;
1630 jb->target_cushion = jb->base_cushion;
1631 jb->current_jitter_ms = 0.0f;
1632 for (int c = 0; c < AVX_JITTER_MAX_CHANNELS; ++c) {
1633 jb->last_samples[c] = 0.0f;
1634 }
1635 avx_mutex_unlock(&jb->lock);
1636 }
1637  
1638 void avx_audio_jitter_push(avx_audio_jitter_t* jb, const float* const* channels, int num_samples) {
1639 if (!jb || !channels || num_samples <= 0) return;
1640  
1641 avx_mutex_lock(&jb->lock);
1642  
1643 /* 1. Track inter-arrival jitter via exponential moving average (EMA).
1644 * Only adapt cushion after initial priming, and ignore burst drains (< 1.0ms)
1645 * that occur when multiple frames are drained in a single socket poll loop.
1646 */
1647 uint64_t now_ns = get_jitter_time_ns();
1648 if (jb->primed && jb->last_push_time_ns > 0) {
1649 float dt_ms = (float)(now_ns - jb->last_push_time_ns) / 1000000.0f;
1650 float expected_dt_ms = (float)num_samples * 1000.0f / (float)jb->sample_rate;
1651 if (dt_ms >= 1.0f) {
1652 float diff_ms = fabsf(dt_ms - expected_dt_ms);
1653  
1654 if (jb->current_jitter_ms == 0.0f) {
1655 jb->current_jitter_ms = diff_ms;
1656 } else {
1657 jb->current_jitter_ms = 0.90f * jb->current_jitter_ms + 0.10f * diff_ms;
1658 }
1659  
1660 /* Dynamically adapt target cushion: base + 2.0 * jitter */
1661 size_t adaptive_samples = (size_t)(((float)jb->base_cushion) + (2.0f * jb->current_jitter_ms * (float)jb->sample_rate / 1000.0f));
1662 if (adaptive_samples < jb->min_cushion) adaptive_samples = jb->min_cushion;
1663 if (adaptive_samples > jb->max_cushion) adaptive_samples = jb->max_cushion;
1664 jb->target_cushion = adaptive_samples;
1665 }
1666 }
1667 jb->last_push_time_ns = now_ns;
1668  
1669 /* 2. Write incoming planar samples to ring buffer */
1670 for (int s = 0; s < num_samples; ++s) {
1671 if (jb->ring_count == jb->capacity) {
1672 /* Drop oldest sample on ring overflow */
1673 jb->ring_head = (jb->ring_head + 1) % jb->capacity;
1674 jb->ring_count--;
1675 }
1676  
1677 for (int c = 0; c < jb->channels; ++c) {
1678 jb->ring[c][jb->ring_tail] = channels[c] ? channels[c][s] : 0.0f;
1679 }
1680  
1681 jb->ring_tail = (jb->ring_tail + 1) % jb->capacity;
1682 jb->ring_count++;
1683 }
1684  
1685 avx_mutex_unlock(&jb->lock);
1686 }
1687  
1688 /*
1689 * Pulls audio frames with adaptive clock drift slewing (+-500 PPM) and raised-cosine
1690 * concealment crossfading to prevent audio clicks/pops between unsynchronized clocks.
1691 */
1692 int avx_audio_jitter_pull(avx_audio_jitter_t* jb, float* const* out_channels, int requested_samples) {
1693 if (!jb || !out_channels || requested_samples <= 0) return 0;
1694  
1695 avx_mutex_lock(&jb->lock);
1696  
1697 /* 1. Pre-roll cushion accumulation check */
1698 if (!jb->primed) {
1699 if (jb->ring_count < jb->target_cushion) {
1700 /* Still filling pre-roll cushion: deliver silence to avoid startup click */
1701 for (int c = 0; c < jb->channels; ++c) {
1702 if (out_channels[c]) {
1703 memset(out_channels[c], 0, (size_t)requested_samples * sizeof(float));
1704 }
1705 }
1706 avx_mutex_unlock(&jb->lock);
1707 return requested_samples;
1708 }
1709 jb->primed = true;
1710 }
1711  
1712 /* 2. Drift slewing calculation:
1713 * Compare current ring_count against target_cushion.
1714 * Deadband of 5 ms prevents micro-hunting on clean arrival.
1715 * If buffer > target + deadband: slew by +0.05% (+500 PPM) to gently drain.
1716 * If buffer < target - deadband: slew by -0.05% (-500 PPM) to gently fill.
1717 */
1718 int deadband = (jb->sample_rate * 5) / 1000;
1719 int diff = (int)jb->ring_count - (int)jb->target_cushion;
1720 double slew_step = 1.0;
1721  
1722 if (diff > deadband) {
1723 slew_step = 1.0005; /* Gently speed up drain (imperceptible +0.05% pitch) */
1724 } else if (diff < -deadband) {
1725 slew_step = 0.9995; /* Gently slow down drain (-0.05% pitch) */
1726 }
1727  
1728 /* 3. Pull requested samples with micro-steered interpolation and smooth concealment */
1729 int delivered = 0;
1730 for (int k = 0; k < requested_samples; ++k) {
1731 if (jb->ring_count == 0) {
1732 /* Starvation detected! */
1733 if (!jb->in_starvation) {
1734 jb->in_starvation = true;
1735 jb->fade_out_remaining = AVX_JITTER_FADE_SAMPLES;
1736 jb->fade_in_remaining = 0;
1737 }
1738  
1739 if (jb->fade_out_remaining > 0) {
1740 int f_idx = AVX_JITTER_FADE_SAMPLES - jb->fade_out_remaining;
1741 float w = 0.5f * (1.0f + cosf((float)M_PI * (float)f_idx / (float)AVX_JITTER_FADE_SAMPLES));
1742 for (int c = 0; c < jb->channels; ++c) {
1743 if (out_channels[c]) {
1744 out_channels[c][k] = jb->last_samples[c] * w;
1745 }
1746 }
1747 jb->fade_out_remaining--;
1748 } else {
1749 for (int c = 0; c < jb->channels; ++c) {
1750 if (out_channels[c]) {
1751 out_channels[c][k] = 0.0f;
1752 }
1753 }
1754 }
1755 delivered++;
1756 continue;
1757 }
1758  
1759 /* If recovering from starvation and samples are now available, transition to fade-in */
1760 if (jb->in_starvation) {
1761 jb->in_starvation = false;
1762 jb->fade_out_remaining = 0;
1763 jb->fade_in_remaining = AVX_JITTER_FADE_SAMPLES;
1764 }
1765  
1766 /* Normal reading */
1767 size_t idx0 = jb->ring_head;
1768 size_t idx1 = (jb->ring_head + 1) % jb->capacity;
1769 float frac = (float)(jb->phase_accum);
1770  
1771 for (int c = 0; c < jb->channels; ++c) {
1772 float s0 = jb->ring[c][idx0];
1773 float s1 = (jb->ring_count > 1) ? jb->ring[c][idx1] : s0;
1774 float val = s0 * (1.0f - frac) + s1 * frac;
1775  
1776 /* Smooth cosine fade-in on starvation recovery */
1777 if (jb->fade_in_remaining > 0) {
1778 int f_idx = AVX_JITTER_FADE_SAMPLES - jb->fade_in_remaining;
1779 float w = 0.5f * (1.0f - cosf((float)M_PI * (float)f_idx / (float)AVX_JITTER_FADE_SAMPLES));
1780 val *= w;
1781 }
1782  
1783 if (out_channels[c]) {
1784 out_channels[c][k] = val;
1785 }
1786 jb->last_samples[c] = s0;
1787 }
1788  
1789 if (jb->fade_in_remaining > 0) {
1790 jb->fade_in_remaining--;
1791 }
1792  
1793 /* Step phase accumulator by slew rate */
1794 jb->phase_accum += slew_step;
1795 while (jb->phase_accum >= 1.0) {
1796 jb->phase_accum -= 1.0;
1797 if (jb->ring_count > 0) {
1798 jb->ring_head = (jb->ring_head + 1) % jb->capacity;
1799 jb->ring_count--;
1800 }
1801 }
1802 delivered++;
1803 }
1804  
1805 avx_mutex_unlock(&jb->lock);
1806 return delivered;
1807 }
1808  
1809 int avx_audio_jitter_get_depth_samples(const avx_audio_jitter_t* jb) {
1810 if (!jb) return 0;
1811 return (int)jb->ring_count;
1812 }
1813  
1814 float avx_audio_jitter_get_depth_ms(const avx_audio_jitter_t* jb) {
1815 if (!jb || jb->sample_rate <= 0) return 0.0f;
1816 return (float)jb->ring_count * 1000.0f / (float)jb->sample_rate;
1817 }
1818  
1819 /* ========================================================================= */
1820 /* 4. CHANNEL LAYOUT & SEMANTIC METADATA HELPERS */
1821 /* ========================================================================= */
1822  
1823 oavx_audio_channel_mask_t avx_audio_layout_to_mask(avx_audio_layout_e layout) {
1824 switch (layout) {
1825 case AVX_AUDIO_LAYOUT_MONO: return OAVX_AUDIO_MASK_MONO;
1826 case AVX_AUDIO_LAYOUT_STEREO: return OAVX_AUDIO_MASK_STEREO;
1827 case AVX_AUDIO_LAYOUT_2_1: return OAVX_AUDIO_MASK_2_1;
1828 case AVX_AUDIO_LAYOUT_QUAD: return OAVX_AUDIO_MASK_QUAD;
1829 case AVX_AUDIO_LAYOUT_5_1: return OAVX_AUDIO_MASK_5_1;
1830 case AVX_AUDIO_LAYOUT_7_1: return OAVX_AUDIO_MASK_7_1;
1831 case AVX_AUDIO_LAYOUT_7_1_4: return OAVX_AUDIO_MASK_7_1_4;
1832 case AVX_AUDIO_LAYOUT_BROADCAST_16CH: {
1833 uint64_t m = 0;
1834 for (int i = 1; i <= 16; ++i) m |= OAVX_AUDIO_CH_DISCRETE_TRACK(i);
1835 return m;
1836 }
1837 case AVX_AUDIO_LAYOUT_BROADCAST_32CH: {
1838 uint64_t m = 0;
1839 for (int i = 1; i <= 32; ++i) m |= OAVX_AUDIO_CH_DISCRETE_TRACK(i);
1840 return m;
1841 }
1842 case AVX_AUDIO_LAYOUT_DISCRETE:
1843 default:
1844 return OAVX_AUDIO_MASK_DISCRETE;
1845 }
1846 }
1847  
1848 avx_audio_layout_e avx_audio_layout_from_channels(uint16_t channels) {
1849 switch (channels) {
1850 case 1: return AVX_AUDIO_LAYOUT_MONO;
1851 case 2: return AVX_AUDIO_LAYOUT_STEREO;
1852 case 3: return AVX_AUDIO_LAYOUT_2_1;
1853 case 4: return AVX_AUDIO_LAYOUT_QUAD;
1854 case 6: return AVX_AUDIO_LAYOUT_5_1;
1855 case 8: return AVX_AUDIO_LAYOUT_7_1;
1856 case 12: return AVX_AUDIO_LAYOUT_7_1_4;
1857 case 16: return AVX_AUDIO_LAYOUT_BROADCAST_16CH;
1858 case 32: return AVX_AUDIO_LAYOUT_BROADCAST_32CH;
1859 default: return (channels > 0) ? AVX_AUDIO_LAYOUT_DISCRETE : AVX_AUDIO_LAYOUT_UNKNOWN;
1860 }
1861 }
1862  
1863 avx_audio_layout_e avx_audio_layout_from_mask(oavx_audio_channel_mask_t mask, uint16_t channels) {
1864 if (mask == OAVX_AUDIO_MASK_MONO) return AVX_AUDIO_LAYOUT_MONO;
1865 if (mask == OAVX_AUDIO_MASK_STEREO) return AVX_AUDIO_LAYOUT_STEREO;
1866 if (mask == OAVX_AUDIO_MASK_2_1) return AVX_AUDIO_LAYOUT_2_1;
1867 if (mask == OAVX_AUDIO_MASK_QUAD) return AVX_AUDIO_LAYOUT_QUAD;
1868 if (mask == OAVX_AUDIO_MASK_5_1) return AVX_AUDIO_LAYOUT_5_1;
1869 if (mask == OAVX_AUDIO_MASK_7_1) return AVX_AUDIO_LAYOUT_7_1;
1870 if (mask == OAVX_AUDIO_MASK_7_1_4) return AVX_AUDIO_LAYOUT_7_1_4;
1871 return avx_audio_layout_from_channels(channels);
1872 }
1873  
1874 const char* avx_audio_layout_name(avx_audio_layout_e layout) {
1875 switch (layout) {
1876 case AVX_AUDIO_LAYOUT_MONO: return "Mono (1.0)";
1877 case AVX_AUDIO_LAYOUT_STEREO: return "Stereo (2.0)";
1878 case AVX_AUDIO_LAYOUT_2_1: return "2.1";
1879 case AVX_AUDIO_LAYOUT_QUAD: return "Quad (4.0)";
1880 case AVX_AUDIO_LAYOUT_5_1: return "5.1 Surround";
1881 case AVX_AUDIO_LAYOUT_7_1: return "7.1 Surround";
1882 case AVX_AUDIO_LAYOUT_7_1_4: return "7.1.4 Immersive";
1883 case AVX_AUDIO_LAYOUT_BROADCAST_16CH: return "Broadcast 16-Channel SDI";
1884 case AVX_AUDIO_LAYOUT_BROADCAST_32CH: return "Broadcast 32-Channel Pro-Audio";
1885 case AVX_AUDIO_LAYOUT_DISCRETE: return "Discrete Multi-Track";
1886 default: return "Unknown Layout";
1887 }
1888 }
1889  
1890 uint16_t avx_audio_layout_channels(avx_audio_layout_e layout) {
1891 switch (layout) {
1892 case AVX_AUDIO_LAYOUT_MONO: return 1;
1893 case AVX_AUDIO_LAYOUT_STEREO: return 2;
1894 case AVX_AUDIO_LAYOUT_2_1: return 3;
1895 case AVX_AUDIO_LAYOUT_QUAD: return 4;
1896 case AVX_AUDIO_LAYOUT_5_1: return 6;
1897 case AVX_AUDIO_LAYOUT_7_1: return 8;
1898 case AVX_AUDIO_LAYOUT_7_1_4: return 12;
1899 case AVX_AUDIO_LAYOUT_BROADCAST_16CH: return 16;
1900 case AVX_AUDIO_LAYOUT_BROADCAST_32CH: return 32;
1901 default: return 0;
1902 }
1903 }
1904  
1905 const char* avx_audio_channel_name(uint16_t channel_idx, avx_audio_layout_e layout) {
1906 if (layout == AVX_AUDIO_LAYOUT_5_1) {
1907 static const char* const names[6] = { "Front Left", "Front Right", "Center", "LFE / Sub", "Surround Left", "Surround Right" };
1908 if (channel_idx < 6) return names[channel_idx];
1909 } else if (layout == AVX_AUDIO_LAYOUT_7_1) {
1910 static const char* const names[8] = { "Front Left", "Front Right", "Center", "LFE / Sub", "Back Left", "Back Right", "Side Left", "Side Right" };
1911 if (channel_idx < 8) return names[channel_idx];
1912 } else if (layout == AVX_AUDIO_LAYOUT_STEREO) {
1913 if (channel_idx == 0) return "Left";
1914 if (channel_idx == 1) return "Right";
1915 } else if (layout == AVX_AUDIO_LAYOUT_MONO) {
1916 if (channel_idx == 0) return "Center / Mono";
1917 } else if (layout == AVX_AUDIO_LAYOUT_QUAD) {
1918 static const char* const names[4] = { "Front Left", "Front Right", "Back Left", "Back Right" };
1919 if (channel_idx < 4) return names[channel_idx];
1920 } else if (layout == AVX_AUDIO_LAYOUT_2_1) {
1921 static const char* const names[3] = { "Left", "Right", "LFE / Sub" };
1922 if (channel_idx < 3) return names[channel_idx];
1923 } else if (layout == AVX_AUDIO_LAYOUT_7_1_4) {
1924 static const char* const names[12] = {
1925 "Front Left", "Front Right", "Center", "LFE / Sub",
1926 "Back Left", "Back Right", "Side Left", "Side Right",
1927 "Top Front Left", "Top Front Right", "Top Back Left", "Top Back Right"
1928 };
1929 if (channel_idx < 12) return names[channel_idx];
1930 }
1931 static char buf[32];
1932 snprintf(buf, sizeof(buf), "Track %u", (unsigned)(channel_idx + 1));
1933 return buf;
1934 }
1935  
1936 /* ========================================================================= */
1937 /* 7. AUDIO ROUTING & UPMIXING TOOLS */
1938 /* ========================================================================= */
1939  
1940 /* Arbitrary N-channel input to M-channel output matrix router with per-crosspoint gains. */
1941 void avx_audio_route_channels(
1942 const float* const* in_planes, int in_channels,
1943 float* const* out_planes, int out_channels,
1944 const int8_t* channel_map,
1945 const float* channel_gains,
1946 int frames
1947 ) {
1948 if (!out_planes || out_channels <= 0 || frames <= 0) return;
1949  
1950 if (!in_planes || in_channels <= 0) {
1951 for (int o = 0; o < out_channels; ++o) {
1952 if (out_planes[o]) {
1953 memset(out_planes[o], 0, (size_t)frames * sizeof(float));
1954 }
1955 }
1956 return;
1957 }
1958  
1959 /* Check for aliasing / buffer sharing between output and input */
1960 bool has_aliasing = false;
1961 for (int o = 0; o < out_channels; ++o) {
1962 if (!out_planes[o]) continue;
1963 for (int in = 0; in < in_channels; ++in) {
1964 if (out_planes[o] == in_planes[in]) {
1965 has_aliasing = true;
1966 break;
1967 }
1968 }
1969 if (has_aliasing) break;
1970 }
1971  
1972 float* temp_data = NULL;
1973 float** temp_out = NULL;
1974 float* const* effective_out = out_planes;
1975  
1976 if (has_aliasing) {
1977 temp_data = (float*)malloc((size_t)out_channels * (size_t)frames * sizeof(float));
1978 temp_out = (float**)malloc((size_t)out_channels * sizeof(float*));
1979 if (temp_data && temp_out) {
1980 for (int o = 0; o < out_channels; ++o) {
1981 temp_out[o] = temp_data + (size_t)o * frames;
1982 }
1983 effective_out = temp_out;
1984 }
1985 }
1986  
1987 for (int o = 0; o < out_channels; ++o) {
1988 float* dst = effective_out[o];
1989 if (!dst && !has_aliasing) continue;
1990 if (has_aliasing && !out_planes[o]) continue;
1991  
1992 int8_t src_idx = channel_map ? channel_map[o] : (int8_t)o;
1993 float gain = channel_gains ? channel_gains[o] : 1.0f;
1994  
1995 if (src_idx < 0 || src_idx >= in_channels || !in_planes[src_idx] || gain == 0.0f) {
1996 memset(dst, 0, (size_t)frames * sizeof(float));
1997 } else {
1998 const float* src = in_planes[src_idx];
1999 if (gain == 1.0f) {
2000 if (src != dst) {
2001 memcpy(dst, src, (size_t)frames * sizeof(float));
2002 }
2003 } else {
2004 for (int i = 0; i < frames; ++i) {
2005 dst[i] = src[i] * gain;
2006 }
2007 }
2008 }
2009 }
2010  
2011 if (has_aliasing && temp_data && temp_out) {
2012 for (int o = 0; o < out_channels; ++o) {
2013 if (out_planes[o]) {
2014 memcpy(out_planes[o], temp_out[o], (size_t)frames * sizeof(float));
2015 }
2016 }
2017 free(temp_out);
2018 free(temp_data);
2019 }
2020 }
2021  
2022 bool avx_audio_frame_route_channels(
2023 const oavx_audio_frame_t* src_frame,
2024 oavx_audio_frame_t* dst_frame,
2025 const int8_t* channel_map,
2026 const float* channel_gains,
2027 uint16_t out_channels
2028 ) {
2029 if (!src_frame || !src_frame->planes[0] || src_frame->channels == 0 ||
2030 src_frame->frames == 0 || !dst_frame) {
2031 return false;
2032 }
2033 if (out_channels == 0 || out_channels > OAVX_MAX_AUDIO_CHANNELS) {
2034 return false;
2035 }
2036  
2037 size_t frames = src_frame->frames;
2038 float* buf = (float*)calloc((size_t)out_channels * frames, sizeof(float));
2039 if (!buf) return false;
2040  
2041 memset(dst_frame, 0, sizeof(*dst_frame));
2042 dst_frame->channels = out_channels;
2043 dst_frame->frames = (uint32_t)frames;
2044 dst_frame->sample_rate = src_frame->sample_rate;
2045 dst_frame->format = src_frame->format;
2046 dst_frame->pts_ns = src_frame->pts_ns;
2047 dst_frame->stride_bytes = sizeof(float);
2048 dst_frame->channel_mask = avx_audio_layout_to_mask(avx_audio_layout_from_channels(out_channels));
2049  
2050 for (uint16_t c = 0; c < out_channels; ++c) {
2051 dst_frame->planes[c] = buf + (size_t)c * frames;
2052 }
2053  
2054 const float* in_ptrs[OAVX_MAX_AUDIO_CHANNELS];
2055 for (uint16_t c = 0; c < src_frame->channels && c < OAVX_MAX_AUDIO_CHANNELS; ++c) {
2056 in_ptrs[c] = (const float*)src_frame->planes[c];
2057 }
2058  
2059 avx_audio_route_channels(
2060 in_ptrs, src_frame->channels,
2061 (float* const*)dst_frame->planes, out_channels,
2062 channel_map, channel_gains, (int)frames
2063 );
2064  
2065 return true;
2066 }
2067  
2068 static void compute_butterworth_lfe(int sample_rate, float* b0, float* b1, float* b2, float* a1, float* a2) {
2069 if (sample_rate <= 0) sample_rate = 48000;
2070 const float fc = 120.0f; /* 120 Hz standard LFE crossover */
2071 float omega = 2.0f * (float)M_PI * fc / (float)sample_rate;
2072 if (omega > (float)M_PI * 0.45f) {
2073 omega = (float)M_PI * 0.45f;
2074 }
2075 float cos_w = cosf(omega);
2076 float sin_w = sinf(omega);
2077 float alpha = sin_w * INV_SQRT2;
2078  
2079 float a0_raw = 1.0f + alpha;
2080 float b0_raw = (1.0f - cos_w) * 0.5f;
2081 float b1_raw = 1.0f - cos_w;
2082 float b2_raw = (1.0f - cos_w) * 0.5f;
2083 float a1_raw = -2.0f * cos_w;
2084 float a2_raw = 1.0f - alpha;
2085  
2086 float inv_a0 = 1.0f / a0_raw;
2087 *b0 = b0_raw * inv_a0;
2088 *b1 = b1_raw * inv_a0;
2089 *b2 = b2_raw * inv_a0;
2090 *a1 = a1_raw * inv_a0;
2091 *a2 = a2_raw * inv_a0;
2092 }
2093  
2094 struct avx_audio_upmixer_t {
2095 int sample_rate;
2096 avx_audio_upmix_cfg_t cfg;
2097  
2098 int delay_samples;
2099 int delay_cap;
2100 int delay_pos;
2101 float* delay_buf_l;
2102 float* delay_buf_r;
2103  
2104 float b0, b1, b2;
2105 float a1, a2;
2106 float lfe_s1;
2107 float lfe_s2;
2108 };
2109  
2110 void avx_audio_upmix_cfg_default(avx_audio_upmix_cfg_t* cfg) {
2111 if (!cfg) return;
2112 cfg->center_level = INV_SQRT2; /* 0.70710678f */
2113 cfg->surround_level = INV_SQRT2; /* 0.70710678f */
2114 cfg->lfe_level = 0.5000f; /* 0.5000f */
2115 cfg->ambient_delay_ms = 15; /* 15 ms standard Haas decorrelation */
2116 cfg->normalize_mode = AVX_AUDIO_NORMALIZE_NONE;
2117 }
2118  
2119 avx_audio_upmixer_t* avx_audio_upmixer_create(int sample_rate, const avx_audio_upmix_cfg_t* cfg) {
2120 if (sample_rate <= 0) sample_rate = 48000;
2121 avx_audio_upmixer_t* u = (avx_audio_upmixer_t*)calloc(1, sizeof(avx_audio_upmixer_t));
2122 if (!u) return NULL;
2123  
2124 u->sample_rate = sample_rate;
2125 if (cfg) {
2126 u->cfg = *cfg;
2127 } else {
2128 avx_audio_upmix_cfg_default(&u->cfg);
2129 }
2130  
2131 if (u->cfg.ambient_delay_ms < 0) u->cfg.ambient_delay_ms = 0;
2132 if (u->cfg.ambient_delay_ms > 100) u->cfg.ambient_delay_ms = 100;
2133  
2134 u->delay_samples = (int)(((int64_t)u->cfg.ambient_delay_ms * sample_rate) / 1000);
2135 u->delay_cap = (u->delay_samples > 0) ? (u->delay_samples + 32) : 64;
2136 u->delay_buf_l = (float*)calloc(u->delay_cap, sizeof(float));
2137 u->delay_buf_r = (float*)calloc(u->delay_cap, sizeof(float));
2138 if (!u->delay_buf_l || !u->delay_buf_r) {
2139 free(u->delay_buf_l);
2140 free(u->delay_buf_r);
2141 free(u);
2142 return NULL;
2143 }
2144  
2145 compute_butterworth_lfe(sample_rate, &u->b0, &u->b1, &u->b2, &u->a1, &u->a2);
2146 return u;
2147 }
2148  
2149 void avx_audio_upmixer_destroy(avx_audio_upmixer_t* u) {
2150 if (!u) return;
2151 free(u->delay_buf_l);
2152 free(u->delay_buf_r);
2153 free(u);
2154 }
2155  
2156 void avx_audio_upmixer_reset(avx_audio_upmixer_t* u) {
2157 if (!u) return;
2158 if (u->delay_buf_l) memset(u->delay_buf_l, 0, (size_t)u->delay_cap * sizeof(float));
2159 if (u->delay_buf_r) memset(u->delay_buf_r, 0, (size_t)u->delay_cap * sizeof(float));
2160 u->delay_pos = 0;
2161 u->lfe_s1 = 0.0f;
2162 u->lfe_s2 = 0.0f;
2163 }
2164  
2165 void avx_audio_upmixer_process(
2166 avx_audio_upmixer_t* u,
2167 const float* in_left,
2168 const float* in_right,
2169 float* const out_channels[6],
2170 int frames
2171 ) {
2172 if (!u || !out_channels || frames <= 0) return;
2173 float* out_fl = out_channels[0];
2174 float* out_fr = out_channels[1];
2175 float* out_c = out_channels[2];
2176 float* out_lfe = out_channels[3];
2177 float* out_bl = out_channels[4];
2178 float* out_br = out_channels[5];
2179  
2180 float max_peak = 0.0f;
2181 bool peak_norm = (u->cfg.normalize_mode == AVX_AUDIO_NORMALIZE_PEAK);
2182 float c_lvl = u->cfg.center_level;
2183 float s_lvl = u->cfg.surround_level;
2184 float lfe_lvl = u->cfg.lfe_level;
2185 int delay_s = u->delay_samples;
2186 int cap = u->delay_cap;
2187  
2188 for (int i = 0; i < frames; ++i) {
2189 float l = in_left ? in_left[i] : 0.0f;
2190 float r = in_right ? in_right[i] : 0.0f;
2191  
2192 /* Center / Dialog */
2193 float mono = 0.5f * (l + r);
2194 float c = mono * c_lvl;
2195  
2196 /* Fronts (dialog bleed cancellation) */
2197 float fl = l - 0.5f * c;
2198 float fr = r - 0.5f * c;
2199  
2200 /* Ambient Surrounds */
2201 float diff = 0.5f * (l - r);
2202 float bl_raw = diff * s_lvl;
2203 float br_raw = -diff * s_lvl;
2204  
2205 float bl = bl_raw;
2206 float br = br_raw;
2207 if (delay_s > 0 && cap > 0) {
2208 int read_idx = (u->delay_pos - delay_s + cap) % cap;
2209 bl = u->delay_buf_l[read_idx];
2210 br = u->delay_buf_r[read_idx];
2211 u->delay_buf_l[u->delay_pos] = bl_raw;
2212 u->delay_buf_r[u->delay_pos] = br_raw;
2213 u->delay_pos = (u->delay_pos + 1) % cap;
2214 }
2215  
2216 /* LFE 2nd-order Butterworth low-pass */
2217 float lfe_in = mono;
2218 float lfe_filtered = u->b0 * lfe_in + u->lfe_s1;
2219 u->lfe_s1 = u->b1 * lfe_in - u->a1 * lfe_filtered + u->lfe_s2;
2220 u->lfe_s2 = u->b2 * lfe_in - u->a2 * lfe_filtered;
2221 float lfe = lfe_filtered * lfe_lvl;
2222  
2223 if (out_fl) out_fl[i] = fl;
2224 if (out_fr) out_fr[i] = fr;
2225 if (out_c) out_c[i] = c;
2226 if (out_lfe) out_lfe[i] = lfe;
2227 if (out_bl) out_bl[i] = bl;
2228 if (out_br) out_br[i] = br;
2229  
2230 if (peak_norm) {
2231 float a;
2232 if (out_fl && (a = fabsf(fl)) > max_peak) max_peak = a;
2233 if (out_fr && (a = fabsf(fr)) > max_peak) max_peak = a;
2234 if (out_c && (a = fabsf(c)) > max_peak) max_peak = a;
2235 if (out_lfe && (a = fabsf(lfe)) > max_peak) max_peak = a;
2236 if (out_bl && (a = fabsf(bl)) > max_peak) max_peak = a;
2237 if (out_br && (a = fabsf(br)) > max_peak) max_peak = a;
2238 } else if (u->cfg.normalize_mode == AVX_AUDIO_NORMALIZE_NONE) {
2239 if (out_fl) out_fl[i] = clamp_f32(fl);
2240 if (out_fr) out_fr[i] = clamp_f32(fr);
2241 if (out_c) out_c[i] = clamp_f32(c);
2242 if (out_lfe) out_lfe[i] = clamp_f32(lfe);
2243 if (out_bl) out_bl[i] = clamp_f32(bl);
2244 if (out_br) out_br[i] = clamp_f32(br);
2245 }
2246 }
2247  
2248 if (peak_norm && max_peak > 1.0f) {
2249 float inv = 1.0f / max_peak;
2250 for (int ch = 0; ch < 6; ++ch) {
2251 if (!out_channels[ch]) continue;
2252 float* dst = out_channels[ch];
2253 for (int i = 0; i < frames; ++i) {
2254 dst[i] *= inv;
2255 }
2256 }
2257 }
2258 }
2259  
2260 void avx_audio_upmix_stereo_to_5_1(
2261 const float* in_left,
2262 const float* in_right,
2263 float* const out_channels[6],
2264 int frames,
2265 int sample_rate,
2266 const avx_audio_upmix_cfg_t* cfg
2267 ) {
2268 if (!out_channels || frames <= 0) return;
2269 avx_audio_upmixer_t* u = avx_audio_upmixer_create(sample_rate, cfg);
2270 if (!u) return;
2271 avx_audio_upmixer_process(u, in_left, in_right, out_channels, frames);
2272 avx_audio_upmixer_destroy(u);
2273 }