Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
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_utilities.c - Interleaved audio helpers and SDI format conversion
7 */
8  
9 #include "openavx.h"
10 #include "avx_internal.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <math.h>
14  
15 /* Helper to clamp 32-bit integer to 16-bit signed range */
16 static inline int16_t clamp_s16(int val) {
17 if (val < -32768) return -32768;
18 if (val > 32767) return 32767;
19 return (int16_t)val;
20 }
21  
22 /* Helper to clamp float to [-1.0, 1.0] */
23 static inline float clamp_f32(float val) {
24 if (val < -1.0f) return -1.0f;
25 if (val > 1.0f) return 1.0f;
26 return val;
27 }
28  
29 /* Converts planar float audio channels to interleaved signed 16-bit PCM. */
30 void avx_util_audio_to_interleaved_16s(
31 const oavx_audio_frame_t* src,
32 oavx_audio_frame_interleaved_16s_t* dst
33 ) {
34 if (!src || !dst || !src->planes[0] || !dst->data) return;
35  
36 dst->sample_rate = src->sample_rate;
37 dst->channels = src->channels;
38 dst->frames = src->frames;
39 dst->pts_ns = src->pts_ns;
40  
41 uint16_t channels = src->channels;
42 uint32_t samples = src->frames;
43 size_t stride = (src->stride_bytes > 0) ?
44 (src->stride_bytes / sizeof(float)) : (size_t)samples;
45  
46 float gain = 1.0f;
47  
48 for (uint16_t c = 0; c < channels; ++c) {
49 const float* src_ch = src->planes[c] ? (const float*)src->planes[c] : ((const float*)src->planes[0] + (size_t)c * stride);
50 int16_t* dst_ptr = dst->data + c;
51  
52 for (uint32_t s = 0; s < samples; ++s) {
53 float f = src_ch[s] * gain;
54 dst_ptr[(size_t)s * channels] = clamp_s16((int)roundf(f * 32767.0f));
55 }
56 }
57 }
58  
59 void avx_util_audio_from_interleaved_16s(
60 const oavx_audio_frame_interleaved_16s_t* src,
61 oavx_audio_frame_t* dst
62 ) {
63 if (!src || !dst || !src->data || !dst->planes[0]) return;
64  
65 dst->sample_rate = src->sample_rate;
66 dst->channels = src->channels;
67 dst->frames = src->frames;
68 dst->format = OAVX_AUDIO_FMT_FLT_PLANAR;
69 dst->pts_ns = src->pts_ns;
70 dst->stride_bytes = (size_t)src->frames * sizeof(float);
71  
72 uint16_t channels = src->channels;
73 uint32_t samples = src->frames;
74 size_t stride = (size_t)samples;
75  
76 float gain = 1.0f / 32767.0f;
77  
78 for (uint16_t c = 0; c < channels; ++c) {
79 if (!dst->planes[c]) {
80 dst->planes[c] = (float*)dst->planes[0] + (size_t)c * stride;
81 }
82 float* dst_ch = (float*)dst->planes[c];
83 const int16_t* src_ptr = src->data + c;
84  
85 for (uint32_t s = 0; s < samples; ++s) {
86 dst_ch[s] = clamp_f32((float)src_ptr[(size_t)s * channels] * gain);
87 }
88 }
89 }
90  
91 void avx_util_audio_to_interleaved_32f(
92 const oavx_audio_frame_t* src,
93 oavx_audio_frame_interleaved_32f_t* dst
94 ) {
95 if (!src || !dst || !src->planes[0] || !dst->data) return;
96  
97 dst->sample_rate = src->sample_rate;
98 dst->channels = src->channels;
99 dst->frames = src->frames;
100 dst->pts_ns = src->pts_ns;
101  
102 uint16_t channels = src->channels;
103 uint32_t samples = src->frames;
104 size_t stride = (src->stride_bytes > 0) ?
105 (src->stride_bytes / sizeof(float)) : (size_t)samples;
106  
107 for (uint16_t c = 0; c < channels; ++c) {
108 const float* src_ch = src->planes[c] ? (const float*)src->planes[c] : ((const float*)src->planes[0] + (size_t)c * stride);
109 float* dst_ptr = dst->data + c;
110  
111 for (uint32_t s = 0; s < samples; ++s) {
112 dst_ptr[(size_t)s * channels] = src_ch[s];
113 }
114 }
115 }
116  
117 void avx_util_audio_from_interleaved_32f(
118 const oavx_audio_frame_interleaved_32f_t* src,
119 oavx_audio_frame_t* dst
120 ) {
121 if (!src || !dst || !src->data || !dst->planes[0]) return;
122  
123 dst->sample_rate = src->sample_rate;
124 dst->channels = src->channels;
125 dst->frames = src->frames;
126 dst->format = OAVX_AUDIO_FMT_FLT_PLANAR;
127 dst->pts_ns = src->pts_ns;
128 dst->stride_bytes = (size_t)src->frames * sizeof(float);
129  
130 uint16_t channels = src->channels;
131 uint32_t samples = src->frames;
132 size_t stride = (size_t)samples;
133  
134 for (uint16_t c = 0; c < channels; ++c) {
135 if (!dst->planes[c]) {
136 dst->planes[c] = (float*)dst->planes[0] + (size_t)c * stride;
137 }
138 float* dst_ch = (float*)dst->planes[c];
139 const float* src_ptr = src->data + c;
140  
141 for (uint32_t s = 0; s < samples; ++s) {
142 dst_ch[s] = src_ptr[(size_t)s * channels];
143 }
144 }
145 }
146  
147 void avx_util_audio_to_interleaved_32s(
148 const oavx_audio_frame_t* src,
149 oavx_audio_frame_interleaved_32s_t* dst
150 ) {
151 if (!src || !dst || !src->planes[0] || !dst->data) return;
152  
153 dst->sample_rate = src->sample_rate;
154 dst->channels = src->channels;
155 dst->frames = src->frames;
156 dst->pts_ns = src->pts_ns;
157  
158 uint16_t channels = src->channels;
159 uint32_t samples = src->frames;
160 size_t stride = (src->stride_bytes > 0) ?
161 (src->stride_bytes / sizeof(float)) : (size_t)samples;
162  
163 for (uint16_t c = 0; c < channels; ++c) {
164 const float* src_ch = src->planes[c] ? (const float*)src->planes[c] : ((const float*)src->planes[0] + (size_t)c * stride);
165 int32_t* dst_ptr = dst->data + c;
166  
167 for (uint32_t s = 0; s < samples; ++s) {
168 double d = (double)src_ch[s] * 2147483647.0;
169 if (d < -2147483648.0) d = -2147483648.0;
170 if (d > 2147483647.0) d = 2147483647.0;
171 dst_ptr[(size_t)s * channels] = (int32_t)round(d);
172 }
173 }
174 }
175  
176 void avx_util_audio_from_interleaved_32s(
177 const oavx_audio_frame_interleaved_32s_t* src,
178 oavx_audio_frame_t* dst
179 ) {
180 if (!src || !dst || !src->data || !dst->planes[0]) return;
181  
182 dst->sample_rate = src->sample_rate;
183 dst->channels = src->channels;
184 dst->frames = src->frames;
185 dst->format = OAVX_AUDIO_FMT_FLT_PLANAR;
186 dst->pts_ns = src->pts_ns;
187 dst->stride_bytes = (size_t)src->frames * sizeof(float);
188  
189 uint16_t channels = src->channels;
190 uint32_t samples = src->frames;
191 size_t stride = (size_t)samples;
192  
193 for (uint16_t c = 0; c < channels; ++c) {
194 if (!dst->planes[c]) {
195 dst->planes[c] = (float*)dst->planes[0] + (size_t)c * stride;
196 }
197 float* dst_ch = (float*)dst->planes[c];
198 const int32_t* src_ptr = src->data + c;
199  
200 for (uint32_t s = 0; s < samples; ++s) {
201 dst_ch[s] = (float)((double)src_ptr[(size_t)s * channels] / 2147483647.0);
202 }
203 }
204 }
205  
206 /* Packs 16-bit semi-planar P216 (10-bit msb-aligned) into SMPTE 292M v210 128-bit blocks. */
207 void avx_util_P216_to_V210(
208 const oavx_video_frame_t* src_p216,
209 oavx_video_frame_t* dst_v210
210 ) {
211 if (!src_p216 || !dst_v210 || !src_p216->planes[0] || !dst_v210->planes[0]) return;
212  
213 uint32_t width = src_p216->width;
214 uint32_t height = src_p216->height;
215 if (width == 0 || height == 0 || (width % 6) != 0) return;
216  
217 dst_v210->width = width;
218 dst_v210->height = height;
219 dst_v210->fourcc = (oavx_fourcc_video_type_e)0x30313276; /* "v210" */
220 dst_v210->framerate = src_p216->framerate;
221 dst_v210->pts_ns = src_p216->pts_ns;
222 dst_v210->dts_ns = src_p216->dts_ns;
223 dst_v210->strides[0] = ((width + 47) / 48) * 128;
224  
225 size_t src_stride = (src_p216->strides[0] > 0) ?
226 (src_p216->strides[0] / 2) : (size_t)width;
227 const uint16_t* src_y = (const uint16_t*)src_p216->planes[0];
228 const uint16_t* src_uv = src_p216->planes[1] ?
229 (const uint16_t*)src_p216->planes[1] :
230 (const uint16_t*)(src_p216->planes[0] + src_stride * height * 2);
231  
232 uint32_t* dst_words = (uint32_t*)dst_v210->planes[0];
233 size_t dst_stride_words = dst_v210->strides[0] / sizeof(uint32_t);
234  
235 for (uint32_t y = 0; y < height; ++y) {
236 const uint16_t* y_line = src_y + (size_t)y * src_stride;
237 const uint16_t* uv_line = src_uv + (size_t)y * src_stride;
238 uint32_t* dst_line = dst_words + (size_t)y * dst_stride_words;
239  
240 for (uint32_t x = 0; x < width; x += 6) {
241 /* Extract 10-bit values (P216 is 16-bit left-justified, so shift right 6) */
242 uint32_t u0 = (uint32_t)(uv_line[x + 0] >> 6) & 0x3FF;
243 uint32_t y0 = (uint32_t)(y_line[x + 0] >> 6) & 0x3FF;
244 uint32_t v0 = (uint32_t)(uv_line[x + 1] >> 6) & 0x3FF;
245  
246 uint32_t y1 = (uint32_t)(y_line[x + 1] >> 6) & 0x3FF;
247 uint32_t u1 = (uint32_t)(uv_line[x + 2] >> 6) & 0x3FF;
248 uint32_t y2 = (uint32_t)(y_line[x + 2] >> 6) & 0x3FF;
249  
250 uint32_t v1 = (uint32_t)(uv_line[x + 3] >> 6) & 0x3FF;
251 uint32_t y3 = (uint32_t)(y_line[x + 3] >> 6) & 0x3FF;
252 uint32_t u2 = (uint32_t)(uv_line[x + 4] >> 6) & 0x3FF;
253  
254 uint32_t y4 = (uint32_t)(y_line[x + 4] >> 6) & 0x3FF;
255 uint32_t v2 = (uint32_t)(uv_line[x + 5] >> 6) & 0x3FF;
256 uint32_t y5 = (uint32_t)(y_line[x + 5] >> 6) & 0x3FF;
257  
258 /* Pack into SMPTE 292M 128-bit blocks (4 x 32-bit words) */
259 size_t w_idx = (size_t)(x / 6) * 4;
260 dst_line[w_idx + 0] = (u0) | (y0 << 10) | (v0 << 20);
261 dst_line[w_idx + 1] = (y1) | (u1 << 10) | (y2 << 20);
262 dst_line[w_idx + 2] = (v1) | (y3 << 10) | (u2 << 20);
263 dst_line[w_idx + 3] = (y4) | (v2 << 10) | (y5 << 20);
264 }
265 }
266 }
267  
268 /* Unpacks SMPTE 292M v210 10-bit packed words into 16-bit semi-planar P216. */
269 void avx_util_V210_to_P216(
270 const oavx_video_frame_t* src_v210,
271 oavx_video_frame_t* dst_p216
272 ) {
273 if (!src_v210 || !dst_p216 || !src_v210->planes[0] || !dst_p216->planes[0]) return;
274  
275 uint32_t width = src_v210->width;
276 uint32_t height = src_v210->height;
277 if (width == 0 || height == 0 || (width % 6) != 0) return;
278  
279 dst_p216->width = width;
280 dst_p216->height = height;
281 dst_p216->fourcc = OAVX_FOURCC_VIDEO_TYPE_P216;
282 dst_p216->framerate = src_v210->framerate;
283 dst_p216->pts_ns = src_v210->pts_ns;
284 dst_p216->dts_ns = src_v210->dts_ns;
285 dst_p216->strides[0] = width * 2;
286 dst_p216->strides[1] = width * 2;
287  
288 const uint32_t* src_words = (const uint32_t*)src_v210->planes[0];
289 size_t src_stride_words = src_v210->strides[0] / sizeof(uint32_t);
290  
291 uint16_t* dst_y = (uint16_t*)dst_p216->planes[0];
292 uint16_t* dst_uv = dst_p216->planes[1] ?
293 (uint16_t*)dst_p216->planes[1] :
294 (uint16_t*)(dst_p216->planes[0] + (size_t)width * height * 2);
295 size_t dst_stride = (size_t)width;
296  
297 for (uint32_t y = 0; y < height; ++y) {
298 const uint32_t* src_line = src_words + (size_t)y * src_stride_words;
299 uint16_t* y_line = dst_y + (size_t)y * dst_stride;
300 uint16_t* uv_line = dst_uv + (size_t)y * dst_stride;
301  
302 for (uint32_t x = 0; x < width; x += 6) {
303 size_t w_idx = (size_t)(x / 6) * 4;
304 uint32_t w0 = src_line[w_idx + 0];
305 uint32_t w1 = src_line[w_idx + 1];
306 uint32_t w2 = src_line[w_idx + 2];
307 uint32_t w3 = src_line[w_idx + 3];
308  
309 /* Unpack 10-bit values */
310 uint32_t u0 = (w0) & 0x3FF;
311 uint32_t y0 = (w0 >> 10) & 0x3FF;
312 uint32_t v0 = (w0 >> 20) & 0x3FF;
313  
314 uint32_t y1 = (w1) & 0x3FF;
315 uint32_t u1 = (w1 >> 10) & 0x3FF;
316 uint32_t y2 = (w1 >> 20) & 0x3FF;
317  
318 uint32_t v1 = (w2) & 0x3FF;
319 uint32_t y3 = (w2 >> 10) & 0x3FF;
320 uint32_t u2 = (w2 >> 20) & 0x3FF;
321  
322 uint32_t y4 = (w3) & 0x3FF;
323 uint32_t v2 = (w3 >> 10) & 0x3FF;
324 uint32_t y5 = (w3 >> 20) & 0x3FF;
325  
326 /* Store in P216 (16-bit left-justified) */
327 y_line[x + 0] = (uint16_t)(y0 << 6);
328 y_line[x + 1] = (uint16_t)(y1 << 6);
329 y_line[x + 2] = (uint16_t)(y2 << 6);
330 y_line[x + 3] = (uint16_t)(y3 << 6);
331 y_line[x + 4] = (uint16_t)(y4 << 6);
332 y_line[x + 5] = (uint16_t)(y5 << 6);
333  
334 uv_line[x + 0] = (uint16_t)(u0 << 6);
335 uv_line[x + 1] = (uint16_t)(v0 << 6);
336 uv_line[x + 2] = (uint16_t)(u1 << 6);
337 uv_line[x + 3] = (uint16_t)(v1 << 6);
338 uv_line[x + 4] = (uint16_t)(u2 << 6);
339 uv_line[x + 5] = (uint16_t)(v2 << 6);
340 }
341 }
342 }