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_framesync.c - Frame synchronizer and clock drift compensation
7 */
8  
9 #include "openavx.h"
10 #include "avx_internal.h"
11 #include "avx_platform.h"
12 #include "avx_audio.h"
13 #include "avx_speedhq.h"
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17  
18 #define MAX_AUDIO_CHANNELS OAVX_MAX_AUDIO_CHANNELS
19 #define AUDIO_RING_CAPACITY (192000) /* 4 seconds at 48kHz */
20  
21 typedef struct avx_framesync_t {
22 avx_rx_instance_t p_recv;
23 avx_mutex_t drain_lock;
24 avx_mutex_t video_lock;
25 avx_mutex_t audio_lock;
26  
27 /* Cached Video Frame */
28 bool has_video;
29 oavx_video_frame_t cached_video;
30 uint8_t* video_data_buf;
31 size_t video_data_buf_size;
32 char cached_metadata[1024];
33  
34 /* Caller delivery buffer for video */
35 uint8_t* delivery_video_buf;
36 size_t delivery_video_buf_size;
37  
38 /* Cached Audio Jitter Buffer (Planar) */
39 int source_sample_rate;
40 int source_channels;
41 avx_audio_jitter_t* p_jitter_buf;
42  
43 /* Active Resampler (if requested rate != source rate) */
44 avx_resampler_t* p_resampler;
45 int resampler_in_rate;
46 int resampler_out_rate;
47 int resampler_channels;
48  
49 /* Caller delivery buffer for audio */
50 float* delivery_audio_buf;
51 size_t delivery_audio_buf_size;
52 } avx_framesync_t;
53  
54 static inline size_t compute_video_frame_bytes(const oavx_video_frame_t* vf) {
55 if (!vf || vf->width == 0 || vf->height == 0) return 0;
56 uint32_t w = vf->width;
57 uint32_t h = vf->height;
58  
59 switch (vf->fourcc) {
60 case OAVX_FOURCC_VIDEO_TYPE_H264:
61 case OAVX_FOURCC_VIDEO_TYPE_HEVC:
62 case OAVX_FOURCC_VIDEO_TYPE_AV01:
63 return vf->strides[0];
64 case OAVX_FOURCC_VIDEO_TYPE_SHQ0:
65 case OAVX_FOURCC_VIDEO_TYPE_SHQ1:
66 case OAVX_FOURCC_VIDEO_TYPE_SHQ2:
67 case OAVX_FOURCC_VIDEO_TYPE_SHQ3:
68 case OAVX_FOURCC_VIDEO_TYPE_SHQ4:
69 case OAVX_FOURCC_VIDEO_TYPE_SHQ5:
70 case OAVX_FOURCC_VIDEO_TYPE_SHQ7:
71 case OAVX_FOURCC_VIDEO_TYPE_SHQ9:
72 return vf->strides[0] > 0 ? vf->strides[0] : (size_t)w * h * 2;
73 case OAVX_FOURCC_VIDEO_TYPE_UYVA:
74 return (size_t)w * h * 3;
75 case OAVX_FOURCC_VIDEO_TYPE_P216:
76 return (size_t)w * h * 4;
77 case OAVX_FOURCC_VIDEO_TYPE_PA16:
78 return (size_t)w * h * 6;
79 case OAVX_FOURCC_VIDEO_TYPE_NV12:
80 case OAVX_FOURCC_VIDEO_TYPE_I420:
81 case OAVX_FOURCC_VIDEO_TYPE_YV12:
82 return (size_t)w * h + ((size_t)(w + 1) / 2) * ((size_t)(h + 1) / 2) * 2;
83 case OAVX_FOURCC_VIDEO_TYPE_RGBA:
84 case OAVX_FOURCC_VIDEO_TYPE_RGBX:
85 case OAVX_FOURCC_VIDEO_TYPE_BGRA:
86 case OAVX_FOURCC_VIDEO_TYPE_BGRX:
87 return (size_t)w * h * 4;
88 case OAVX_FOURCC_VIDEO_TYPE_UYVY:
89 default:
90 return (size_t)w * h * 2;
91 }
92 }
93  
94 /* Drains incoming video and audio packets non-blocking from network receiver into jitter buffers. */
95 static void drain_receiver_frames(avx_framesync_t* fs) {
96 if (!fs->p_recv) return;
97  
98 avx_mutex_lock(&fs->drain_lock);
99  
100 oavx_video_frame_t vf;
101 oavx_audio_frame_t af;
102 oavx_metadata_frame_t mf;
103  
104 while (true) {
105 oavx_frame_type_t ftype = avx_rx_receive(fs->p_recv, &vf, &af, &mf, 0);
106 if (ftype == OAVX_FRAME_TYPE_NONE || ftype == OAVX_FRAME_TYPE_ERROR) {
107 break;
108 }
109  
110 if (ftype == OAVX_FRAME_TYPE_VIDEO && vf.planes[0]) {
111 size_t payload_bytes = compute_video_frame_bytes(&vf);
112  
113 avx_mutex_lock(&fs->video_lock);
114 if (payload_bytes > 0 && fs->video_data_buf_size < payload_bytes) {
115 uint8_t* nb = (uint8_t*)realloc(fs->video_data_buf, payload_bytes);
116 if (nb) {
117 fs->video_data_buf = nb;
118 fs->video_data_buf_size = payload_bytes;
119 }
120 }
121  
122 if (payload_bytes > 0 && fs->video_data_buf && fs->video_data_buf_size >= payload_bytes) {
123 if (vf.planes[1] && vf.planes[1] != (uint8_t*)vf.planes[0] + (size_t)vf.width * vf.height * 2 &&
124 vf.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVA) {
125 memcpy(fs->video_data_buf, vf.planes[0], (size_t)vf.width * vf.height * 2);
126 memcpy(fs->video_data_buf + (size_t)vf.width * vf.height * 2, vf.planes[1], (size_t)vf.width * vf.height);
127 } else if (vf.planes[1] && vf.planes[1] != (uint8_t*)vf.planes[0] + (size_t)vf.width * vf.height &&
128 vf.fourcc == OAVX_FOURCC_VIDEO_TYPE_NV12) {
129 memcpy(fs->video_data_buf, vf.planes[0], (size_t)vf.width * vf.height);
130 memcpy(fs->video_data_buf + (size_t)vf.width * vf.height, vf.planes[1], (size_t)vf.width * ((vf.height + 1) / 2));
131 } else {
132 memcpy(fs->video_data_buf, vf.planes[0], payload_bytes);
133 }
134 fs->cached_video = vf;
135 avx_setup_raw_video_planes(&fs->cached_video, fs->video_data_buf, payload_bytes);
136  
137 if (vf.metadata && vf.metadata[0] != '\0') {
138 snprintf(fs->cached_metadata, sizeof(fs->cached_metadata), "%s", vf.metadata);
139 fs->cached_video.metadata = fs->cached_metadata;
140 } else {
141 fs->cached_metadata[0] = '\0';
142 fs->cached_video.metadata = NULL;
143 }
144 fs->has_video = true;
145 }
146 avx_mutex_unlock(&fs->video_lock);
147 avx_rx_free_video(fs->p_recv, &vf);
148 } else if (ftype == OAVX_FRAME_TYPE_AUDIO && af.planes[0]) {
149 int chs = (int)af.channels;
150 if (chs > MAX_AUDIO_CHANNELS) chs = MAX_AUDIO_CHANNELS;
151 int samples = (int)af.frames;
152  
153 avx_mutex_lock(&fs->audio_lock);
154 if (!fs->p_jitter_buf || fs->source_sample_rate != (int)af.sample_rate || fs->source_channels != chs) {
155 if (fs->p_jitter_buf) avx_audio_jitter_destroy(fs->p_jitter_buf);
156 avx_audio_jitter_config_t jcfg = {
157 .sample_rate = (int)af.sample_rate,
158 .channels = chs,
159 .target_cushion_ms = 15,
160 .min_cushion_ms = 5,
161 .max_cushion_ms = 35
162 };
163 fs->p_jitter_buf = avx_audio_jitter_create(&jcfg);
164 fs->source_sample_rate = (int)af.sample_rate;
165 fs->source_channels = chs;
166 }
167  
168 size_t stride = (af.stride_bytes > 0) ? (af.stride_bytes / sizeof(float)) : (size_t)samples;
169 const float* in_ch[MAX_AUDIO_CHANNELS];
170 for (int c = 0; c < chs; ++c) {
171 in_ch[c] = af.planes[c] ? (const float*)af.planes[c] : ((const float*)af.planes[0] + c * stride);
172 }
173 for (int c = chs; c < MAX_AUDIO_CHANNELS; ++c) {
174 in_ch[c] = NULL;
175 }
176  
177 if (fs->p_jitter_buf) {
178 avx_audio_jitter_push(fs->p_jitter_buf, in_ch, samples);
179 }
180 avx_mutex_unlock(&fs->audio_lock);
181 avx_rx_free_audio(fs->p_recv, &af);
182 } else if (ftype == OAVX_FRAME_TYPE_METADATA) {
183 avx_rx_free_metadata(fs->p_recv, &mf);
184 }
185 }
186  
187 avx_mutex_unlock(&fs->drain_lock);
188 }
189  
190 OPENAVX_API avx_sync_instance_t avx_sync_create(avx_rx_instance_t p_receiver) {
191 if (!p_receiver) return NULL;
192  
193 avx_framesync_t* fs = (avx_framesync_t*)calloc(1, sizeof(avx_framesync_t));
194 if (!fs) return NULL;
195  
196 fs->p_recv = p_receiver;
197 avx_mutex_init(&fs->drain_lock);
198 avx_mutex_init(&fs->video_lock);
199 avx_mutex_init(&fs->audio_lock);
200 fs->has_video = false;
201 fs->source_sample_rate = 48000;
202 fs->source_channels = 2;
203  
204 avx_audio_jitter_config_t jcfg = {
205 .sample_rate = 48000,
206 .channels = 2,
207 .target_cushion_ms = 15,
208 .min_cushion_ms = 5,
209 .max_cushion_ms = 35
210 };
211 fs->p_jitter_buf = avx_audio_jitter_create(&jcfg);
212 if (!fs->p_jitter_buf) {
213 avx_sync_destroy(fs);
214 return NULL;
215 }
216  
217 return (avx_sync_instance_t)fs;
218 }
219  
220 OPENAVX_API void avx_sync_destroy(avx_sync_instance_t p_instance) {
221 if (!p_instance) return;
222 avx_framesync_t* fs = (avx_framesync_t*)p_instance;
223  
224 avx_mutex_lock(&fs->drain_lock);
225 avx_mutex_lock(&fs->video_lock);
226 avx_mutex_lock(&fs->audio_lock);
227 if (fs->video_data_buf) {
228 free(fs->video_data_buf);
229 fs->video_data_buf = NULL;
230 }
231 if (fs->delivery_video_buf) {
232 free(fs->delivery_video_buf);
233 fs->delivery_video_buf = NULL;
234 }
235 if (fs->p_jitter_buf) {
236 avx_audio_jitter_destroy(fs->p_jitter_buf);
237 fs->p_jitter_buf = NULL;
238 }
239 if (fs->p_resampler) {
240 avx_resampler_destroy(fs->p_resampler);
241 fs->p_resampler = NULL;
242 }
243 if (fs->delivery_audio_buf) {
244 free(fs->delivery_audio_buf);
245 fs->delivery_audio_buf = NULL;
246 }
247 avx_mutex_unlock(&fs->audio_lock);
248 avx_mutex_unlock(&fs->video_lock);
249 avx_mutex_unlock(&fs->drain_lock);
250  
251 avx_mutex_destroy(&fs->audio_lock);
252 avx_mutex_destroy(&fs->video_lock);
253 avx_mutex_destroy(&fs->drain_lock);
254 free(fs);
255 }
256  
257 /*
258 * Delivers video frame on hardware display ticks, gracefully duplicating the previous
259 * frame on network packet drop or transmission jitter to maintain constant output rate.
260 */
261 void avx_sync_capture_video(
262 avx_sync_instance_t p_instance,
263 oavx_video_frame_t* p_video_data,
264 int field_type
265 ) {
266 (void)field_type;
267 if (!p_instance || !p_video_data) return;
268 avx_framesync_t* fs = (avx_framesync_t*)p_instance;
269  
270 drain_receiver_frames(fs);
271  
272 avx_mutex_lock(&fs->video_lock);
273 if (!fs->has_video || !fs->cached_video.planes[0]) {
274 memset(p_video_data, 0, sizeof(*p_video_data));
275 avx_mutex_unlock(&fs->video_lock);
276 return;
277 }
278  
279 size_t payload_bytes = compute_video_frame_bytes(&fs->cached_video);
280 if (payload_bytes > 0 && fs->delivery_video_buf_size < payload_bytes) {
281 uint8_t* nb = (uint8_t*)realloc(fs->delivery_video_buf, payload_bytes);
282 if (nb) {
283 fs->delivery_video_buf = nb;
284 fs->delivery_video_buf_size = payload_bytes;
285 }
286 }
287  
288 if (payload_bytes > 0 && fs->delivery_video_buf && fs->delivery_video_buf_size >= payload_bytes) {
289 memcpy(fs->delivery_video_buf, fs->cached_video.planes[0], payload_bytes);
290 *p_video_data = fs->cached_video;
291 avx_setup_raw_video_planes(p_video_data, fs->delivery_video_buf, payload_bytes);
292 } else {
293 memset(p_video_data, 0, sizeof(*p_video_data));
294 }
295  
296 avx_mutex_unlock(&fs->video_lock);
297 }
298  
299 void avx_sync_free_video(
300 avx_sync_instance_t p_instance,
301 oavx_video_frame_t* p_video_data
302 ) {
303 (void)p_instance;
304 if (p_video_data) {
305 p_video_data->planes[0] = NULL;
306 p_video_data->planes[1] = NULL;
307 p_video_data->planes[2] = NULL;
308 p_video_data->planes[3] = NULL;
309 }
310 }
311  
312 /*
313 * Pulls audio samples synchronized to DAC render clock, performing dynamic resampling
314 * and jitter buffer drift slewing (+-500 PPM) to prevent buffer underrun/overflow.
315 */
316 void avx_sync_capture_audio(
317 avx_sync_instance_t p_instance,
318 oavx_audio_frame_t* p_audio_data,
319 int target_rate,
320 int target_channels,
321 int target_samples
322 ) {
323 if (!p_instance || !p_audio_data || target_rate <= 0 || target_channels <= 0 || target_samples <= 0) {
324 return;
325 }
326 if (target_channels > MAX_AUDIO_CHANNELS) {
327 target_channels = MAX_AUDIO_CHANNELS;
328 }
329 avx_framesync_t* fs = (avx_framesync_t*)p_instance;
330  
331 drain_receiver_frames(fs);
332  
333 avx_mutex_lock(&fs->audio_lock);
334  
335 /* Allocate or grow delivery audio buffer */
336 size_t needed_bytes = (size_t)target_channels * target_samples * sizeof(float);
337 if (fs->delivery_audio_buf_size < needed_bytes) {
338 float* nb = (float*)realloc(fs->delivery_audio_buf, needed_bytes);
339 if (nb) {
340 fs->delivery_audio_buf = nb;
341 fs->delivery_audio_buf_size = needed_bytes;
342 }
343 }
344  
345 if (!fs->delivery_audio_buf) {
346 avx_mutex_unlock(&fs->audio_lock);
347 return;
348 }
349  
350 /* Check if sample rate conversion is required */
351 if (target_rate != fs->source_sample_rate) {
352 /* Setup / update resampler */
353 if (!fs->p_resampler ||
354 fs->resampler_in_rate != fs->source_sample_rate ||
355 fs->resampler_out_rate != target_rate ||
356 fs->resampler_channels != target_channels) {
357 if (fs->p_resampler) {
358 avx_resampler_destroy(fs->p_resampler);
359 }
360 fs->p_resampler = avx_resampler_create(
361 fs->source_sample_rate, target_rate, target_channels, AVX_AUDIO_RESAMPLE_CUBIC
362 );
363 fs->resampler_in_rate = fs->source_sample_rate;
364 fs->resampler_out_rate = target_rate;
365 fs->resampler_channels = target_channels;
366 }
367  
368 /* Calculate how many source samples correspond to target_samples */
369 int needed_source_samples = (int)ceilf((float)target_samples * (float)fs->source_sample_rate / (float)target_rate);
370  
371 /* Prepare input channel pointers */
372 float* in_ch[MAX_AUDIO_CHANNELS];
373 for (int c = 0; c < target_channels; ++c) {
374 in_ch[c] = (float*)malloc((size_t)needed_source_samples * sizeof(float));
375 }
376 for (int c = target_channels; c < MAX_AUDIO_CHANNELS; ++c) {
377 in_ch[c] = NULL;
378 }
379  
380 if (fs->p_jitter_buf) {
381 avx_audio_jitter_pull(fs->p_jitter_buf, in_ch, needed_source_samples);
382 } else {
383 for (int c = 0; c < target_channels; ++c) {
384 memset(in_ch[c], 0, (size_t)needed_source_samples * sizeof(float));
385 }
386 }
387  
388 /* Prepare output channel pointers */
389 float* out_ch[MAX_AUDIO_CHANNELS];
390 for (int c = 0; c < target_channels; ++c) {
391 out_ch[c] = fs->delivery_audio_buf + (size_t)c * target_samples;
392 }
393  
394 int produced = 0;
395 if (fs->p_resampler && needed_source_samples > 0) {
396 produced = avx_resampler_process(
397 fs->p_resampler,
398 (const float* const*)in_ch,
399 needed_source_samples,
400 out_ch,
401 target_samples
402 );
403 }
404  
405 /* Pad remainder with silence if underflow */
406 for (int c = 0; c < target_channels; ++c) {
407 if (produced < target_samples) {
408 memset(out_ch[c] + produced, 0, (size_t)(target_samples - produced) * sizeof(float));
409 }
410 free(in_ch[c]);
411 }
412 } else {
413 /* Direct pull from adaptive jitter buffer (with micro-slewing & concealment) */
414 float* out_ch[MAX_AUDIO_CHANNELS];
415 for (int c = 0; c < target_channels; ++c) {
416 out_ch[c] = fs->delivery_audio_buf + (size_t)c * target_samples;
417 }
418 for (int c = target_channels; c < MAX_AUDIO_CHANNELS; ++c) {
419 out_ch[c] = NULL;
420 }
421  
422 if (fs->p_jitter_buf) {
423 avx_audio_jitter_pull(fs->p_jitter_buf, out_ch, target_samples);
424 } else {
425 for (int c = 0; c < target_channels; ++c) {
426 memset(out_ch[c], 0, (size_t)(target_samples) * sizeof(float));
427 }
428 }
429 }
430  
431 memset(p_audio_data, 0, sizeof(*p_audio_data));
432 p_audio_data->sample_rate = (uint32_t)target_rate;
433 p_audio_data->channels = (uint16_t)target_channels;
434 p_audio_data->frames = (uint32_t)target_samples;
435 p_audio_data->format = OAVX_AUDIO_FMT_FLT_PLANAR;
436 p_audio_data->stride_bytes = (size_t)target_samples * sizeof(float);
437 p_audio_data->planes[0] = fs->delivery_audio_buf;
438 p_audio_data->channel_mask = avx_audio_layout_to_mask(avx_audio_layout_from_channels((uint16_t)target_channels));
439 for (int c = 0; c < target_channels && c < OAVX_MAX_AUDIO_CHANNELS; ++c) {
440 p_audio_data->planes[c] = fs->delivery_audio_buf + (size_t)c * target_samples;
441 }
442 for (int c = target_channels; c < OAVX_MAX_AUDIO_CHANNELS; ++c) {
443 p_audio_data->planes[c] = NULL;
444 }
445 p_audio_data->metadata = NULL;
446 p_audio_data->metadata_len = 0;
447 p_audio_data->pts_ns = 0;
448 p_audio_data->user_ctx = NULL;
449  
450 avx_mutex_unlock(&fs->audio_lock);
451 }
452  
453 void avx_sync_free_audio(
454 avx_sync_instance_t p_instance,
455 oavx_audio_frame_t* p_audio_data
456 ) {
457 (void)p_instance;
458 if (p_audio_data) {
459 p_audio_data->planes[0] = NULL;
460 }
461 }
462  
463 int avx_sync_audio_queue_depth(avx_sync_instance_t p_instance) {
464 if (!p_instance) return 0;
465 avx_framesync_t* fs = (avx_framesync_t*)p_instance;
466  
467 drain_receiver_frames(fs);
468 avx_mutex_lock(&fs->audio_lock);
469 int depth = fs->p_jitter_buf ? avx_audio_jitter_get_depth_samples(fs->p_jitter_buf) : 0;
470 avx_mutex_unlock(&fs->audio_lock);
471  
472 return depth;
473 }