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_v4l2_sink.c - Linux V4L2 Virtual Camera Loopback Sink
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13  
14 #include "avx_v4l2_sink.h"
15 #include "avx_platform.h"
16 #include "avx_simd.h"
17 #include "avx_color.h"
18  
19 #if defined(AVX_OS_LINUX)
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/ioctl.h>
24 #include <linux/videodev2.h>
25 #endif
26  
27 #if defined(AVX_OS_LINUX)
28  
29 struct avx_v4l2_sink_t {
30 int fd;
31 char device_path[128];
32 uint32_t width;
33 uint32_t height;
34 uint32_t pixelformat;
35 size_t frame_bytes;
36 uint8_t *conv_buf;
37 size_t conv_buf_cap;
38 avx_mutex_t lock;
39 };
40  
41 /* Performs 32-bit word byte swap converting broadcast UYVY (U Y0 V Y1) to native V4L2 YUYV (Y0 U Y1 V). */
42 static void uyvy_to_yuyv_convert(const uint8_t *src, uint8_t *dst, size_t pixel_count)
43 {
44 size_t word_count = pixel_count / 2;
45 const uint32_t *src32 = (const uint32_t*)src;
46 uint32_t *dst32 = (uint32_t*)dst;
47  
48 /* Process in 64-bit unrolled chunks */
49 size_t i = 0;
50 for (; i + 4 <= word_count; i += 4) {
51 uint32_t v0 = src32[i + 0];
52 uint32_t v1 = src32[i + 1];
53 uint32_t v2 = src32[i + 2];
54 uint32_t v3 = src32[i + 3];
55  
56 dst32[i + 0] = ((v0 & 0xFF00FF00) >> 8) | ((v0 & 0x00FF00FF) << 8);
57 dst32[i + 1] = ((v1 & 0xFF00FF00) >> 8) | ((v1 & 0x00FF00FF) << 8);
58 dst32[i + 2] = ((v2 & 0xFF00FF00) >> 8) | ((v2 & 0x00FF00FF) << 8);
59 dst32[i + 3] = ((v3 & 0xFF00FF00) >> 8) | ((v3 & 0x00FF00FF) << 8);
60 }
61 for (; i < word_count; ++i) {
62 uint32_t v = src32[i];
63 dst32[i] = ((v & 0xFF00FF00) >> 8) | ((v & 0x00FF00FF) << 8);
64 }
65 }
66  
67 /* Convert I420 (planar Y, U, V) directly to packed YUYV (Y0 U Y1 V) */
68 static void i420_to_yuyv_convert(const uint8_t *y_src, size_t y_stride,
69 const uint8_t *u_src, size_t u_stride,
70 const uint8_t *v_src, size_t v_stride,
71 uint8_t *dst, size_t dst_stride,
72 int width, int height)
73 {
74 if (!y_src || !u_src || !v_src || !dst || width <= 0 || height <= 0) return;
75 if (y_stride == 0) y_stride = (size_t)width;
76 if (u_stride == 0) u_stride = (size_t)(width + 1) / 2;
77 if (v_stride == 0) v_stride = (size_t)(width + 1) / 2;
78 if (dst_stride == 0) dst_stride = (size_t)width * 2;
79  
80 int half_h = (height + 1) / 2;
81 int half_w = (width + 1) / 2;
82  
83 for (int y2 = 0; y2 < half_h; ++y2) {
84 int y0 = y2 * 2;
85 int y1 = y0 + 1;
86 const uint8_t *y_row0 = y_src + (size_t)y0 * y_stride;
87 const uint8_t *y_row1 = (y1 < height) ? (y_src + (size_t)y1 * y_stride) : y_row0;
88 const uint8_t *u_row = u_src + (size_t)y2 * u_stride;
89 const uint8_t *v_row = v_src + (size_t)y2 * v_stride;
90 uint8_t *dst_row0 = dst + (size_t)y0 * dst_stride;
91 uint8_t *dst_row1 = (y1 < height) ? (dst + (size_t)y1 * dst_stride) : NULL;
92  
93 for (int x2 = 0; x2 < half_w; ++x2) {
94 uint8_t u = u_row[x2];
95 uint8_t v = v_row[x2];
96 uint8_t y00 = y_row0[2 * x2 + 0];
97 uint8_t y01 = (2 * x2 + 1 < width) ? y_row0[2 * x2 + 1] : y00;
98  
99 dst_row0[4 * x2 + 0] = y00;
100 dst_row0[4 * x2 + 1] = u;
101 dst_row0[4 * x2 + 2] = y01;
102 dst_row0[4 * x2 + 3] = v;
103  
104 if (dst_row1) {
105 uint8_t y10 = y_row1[2 * x2 + 0];
106 uint8_t y11 = (2 * x2 + 1 < width) ? y_row1[2 * x2 + 1] : y10;
107 dst_row1[4 * x2 + 0] = y10;
108 dst_row1[4 * x2 + 1] = u;
109 dst_row1[4 * x2 + 2] = y11;
110 dst_row1[4 * x2 + 3] = v;
111 }
112 }
113 }
114 }
115  
116 /* Convert NV12 (semi-planar Y, UV) directly to packed YUYV (Y0 U Y1 V) */
117 static void nv12_to_yuyv_convert(const uint8_t *y_src, size_t y_stride,
118 const uint8_t *uv_src, size_t uv_stride,
119 uint8_t *dst, size_t dst_stride,
120 int width, int height)
121 {
122 if (!y_src || !uv_src || !dst || width <= 0 || height <= 0) return;
123 if (y_stride == 0) y_stride = (size_t)width;
124 if (uv_stride == 0) uv_stride = (size_t)width;
125 if (dst_stride == 0) dst_stride = (size_t)width * 2;
126  
127 int half_h = (height + 1) / 2;
128 int half_w = (width + 1) / 2;
129  
130 for (int y2 = 0; y2 < half_h; ++y2) {
131 int y0 = y2 * 2;
132 int y1 = y0 + 1;
133 const uint8_t *y_row0 = y_src + (size_t)y0 * y_stride;
134 const uint8_t *y_row1 = (y1 < height) ? (y_src + (size_t)y1 * y_stride) : y_row0;
135 const uint8_t *uv_row = uv_src + (size_t)y2 * uv_stride;
136 uint8_t *dst_row0 = dst + (size_t)y0 * dst_stride;
137 uint8_t *dst_row1 = (y1 < height) ? (dst + (size_t)y1 * dst_stride) : NULL;
138  
139 for (int x2 = 0; x2 < half_w; ++x2) {
140 uint8_t u = uv_row[2 * x2 + 0];
141 uint8_t v = uv_row[2 * x2 + 1];
142 uint8_t y00 = y_row0[2 * x2 + 0];
143 uint8_t y01 = (2 * x2 + 1 < width) ? y_row0[2 * x2 + 1] : y00;
144  
145 dst_row0[4 * x2 + 0] = y00;
146 dst_row0[4 * x2 + 1] = u;
147 dst_row0[4 * x2 + 2] = y01;
148 dst_row0[4 * x2 + 3] = v;
149  
150 if (dst_row1) {
151 uint8_t y10 = y_row1[2 * x2 + 0];
152 uint8_t y11 = (2 * x2 + 1 < width) ? y_row1[2 * x2 + 1] : y10;
153 dst_row1[4 * x2 + 0] = y10;
154 dst_row1[4 * x2 + 1] = u;
155 dst_row1[4 * x2 + 2] = y11;
156 dst_row1[4 * x2 + 3] = v;
157 }
158 }
159 }
160 }
161  
162 /* Probes /dev/video* device nodes via VIDIOC_QUERYCAP to locate active v4l2loopback instances. */
163 bool avx_v4l2_sink_probe_loopback(char *out_path, size_t out_path_len)
164 {
165 if (!out_path || out_path_len == 0) return false;
166  
167 char path[64];
168 for (int i = 0; i < 64; ++i) {
169 snprintf(path, sizeof(path), "/dev/video%d", i);
170 int fd = open(path, O_RDWR | O_NONBLOCK);
171 if (fd < 0) continue;
172  
173 struct v4l2_capability cap;
174 memset(&cap, 0, sizeof(cap));
175 int ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
176 close(fd);
177  
178 if (ret < 0) continue;
179  
180 uint32_t caps = cap.capabilities;
181 if (caps & V4L2_CAP_DEVICE_CAPS) {
182 caps = cap.device_caps;
183 }
184  
185 /* Check if device supports video output (writing frames into it) */
186 if (caps & V4L2_CAP_VIDEO_OUTPUT) {
187 const char *driver = (const char*)cap.driver;
188 const char *card = (const char*)cap.card;
189  
190 if (strcasestr(driver, "loopback") ||
191 strcasestr(card, "loopback") ||
192 strcasestr(card, "OpenAVX") ||
193 strcasestr(card, "Virtual") ||
194 strcasestr(card, "Dummy")) {
195 snprintf(out_path, out_path_len, "%s", path);
196 return true;
197 }
198 }
199 }
200  
201 return false;
202 }
203  
204 /* Sets output resolution, pixel format, and field order on V4L2 loopback sink via VIDIOC_S_FMT. */
205 int avx_v4l2_sink_set_format(avx_v4l2_sink_t *sink, uint32_t width, uint32_t height, uint32_t pixelformat)
206 {
207 if (!sink || sink->fd < 0) return -1;
208 if (width == 0 || height == 0) return -1;
209  
210 if (pixelformat == 0) {
211 pixelformat = V4L2_PIX_FMT_YUYV;
212 }
213  
214 struct v4l2_format fmt;
215 memset(&fmt, 0, sizeof(fmt));
216 fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
217 fmt.fmt.pix.width = width;
218 fmt.fmt.pix.height = height;
219 fmt.fmt.pix.pixelformat = pixelformat;
220 fmt.fmt.pix.field = V4L2_FIELD_NONE;
221 fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
222  
223 /* Calculate frame size based on pixelformat */
224 if (pixelformat == V4L2_PIX_FMT_YUYV || pixelformat == V4L2_PIX_FMT_UYVY) {
225 fmt.fmt.pix.bytesperline = width * 2;
226 fmt.fmt.pix.sizeimage = (size_t)width * height * 2;
227 } else if (pixelformat == V4L2_PIX_FMT_BGR24 || pixelformat == V4L2_PIX_FMT_RGB24) {
228 fmt.fmt.pix.bytesperline = width * 3;
229 fmt.fmt.pix.sizeimage = (size_t)width * height * 3;
230 } else {
231 fmt.fmt.pix.bytesperline = width * 2;
232 fmt.fmt.pix.sizeimage = (size_t)width * height * 2;
233 }
234  
235 if (ioctl(sink->fd, VIDIOC_S_FMT, &fmt) < 0) {
236 return -1;
237 }
238  
239 sink->width = fmt.fmt.pix.width;
240 sink->height = fmt.fmt.pix.height;
241 sink->pixelformat = fmt.fmt.pix.pixelformat;
242 sink->frame_bytes = fmt.fmt.pix.sizeimage;
243  
244 /* Ensure conversion buffer is appropriately sized */
245 size_t need_cap = sink->frame_bytes > (width * height * 4) ? sink->frame_bytes : (width * height * 4);
246 if (sink->conv_buf_cap < need_cap) {
247 uint8_t *nb = (uint8_t*)realloc(sink->conv_buf, need_cap);
248 if (!nb) return -1;
249 sink->conv_buf = nb;
250 sink->conv_buf_cap = need_cap;
251 }
252  
253 return 0;
254 }
255  
256 avx_v4l2_sink_t* avx_v4l2_sink_create(const avx_v4l2_sink_config_t *cfg)
257 {
258 char dev_path[128];
259 memset(dev_path, 0, sizeof(dev_path));
260  
261 if (cfg && cfg->device_path && cfg->device_path[0] != '\0') {
262 snprintf(dev_path, sizeof(dev_path), "%s", cfg->device_path);
263 } else {
264 if (!avx_v4l2_sink_probe_loopback(dev_path, sizeof(dev_path))) {
265 return NULL; /* No available loopback device discovered */
266 }
267 }
268  
269 int fd = open(dev_path, O_RDWR);
270 if (fd < 0) {
271 return NULL;
272 }
273  
274 struct v4l2_capability cap;
275 memset(&cap, 0, sizeof(cap));
276 if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
277 close(fd);
278 return NULL;
279 }
280  
281 uint32_t caps = cap.capabilities;
282 if (caps & V4L2_CAP_DEVICE_CAPS) {
283 caps = cap.device_caps;
284 }
285 if (!(caps & V4L2_CAP_VIDEO_OUTPUT)) {
286 close(fd);
287 return NULL; /* Not an output-capable V4L2 device */
288 }
289  
290 avx_v4l2_sink_t *sink = (avx_v4l2_sink_t*)calloc(1, sizeof(avx_v4l2_sink_t));
291 if (!sink) {
292 close(fd);
293 return NULL;
294 }
295  
296 sink->fd = fd;
297 snprintf(sink->device_path, sizeof(sink->device_path), "%s", dev_path);
298 avx_mutex_init(&sink->lock);
299  
300 uint32_t w = (cfg && cfg->width > 0) ? cfg->width : 1920;
301 uint32_t h = (cfg && cfg->height > 0) ? cfg->height : 1080;
302 uint32_t pixfmt = (cfg && cfg->pixelformat > 0) ? cfg->pixelformat : V4L2_PIX_FMT_YUYV;
303  
304 if (avx_v4l2_sink_set_format(sink, w, h, pixfmt) != 0) {
305 avx_v4l2_sink_destroy(sink);
306 return NULL;
307 }
308  
309 /* Set target output FPS via VIDIOC_S_PARM if provided */
310 if (cfg && cfg->fps_num > 0) {
311 struct v4l2_streamparm parm;
312 memset(&parm, 0, sizeof(parm));
313 parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
314 parm.parm.output.timeperframe.numerator = (cfg->fps_den > 0) ? cfg->fps_den : 1;
315 parm.parm.output.timeperframe.denominator = cfg->fps_num;
316 (void)ioctl(sink->fd, VIDIOC_S_PARM, &parm);
317 }
318  
319 return sink;
320 }
321  
322 int avx_v4l2_sink_write_raw(avx_v4l2_sink_t *sink, const void *data, size_t size)
323 {
324 if (!sink || sink->fd < 0 || !data || size == 0) return -1;
325  
326 avx_mutex_lock(&sink->lock);
327 ssize_t written = write(sink->fd, data, size);
328 avx_mutex_unlock(&sink->lock);
329  
330 if (written < 0) {
331 return -1;
332 }
333 return 0;
334 }
335  
336 /* Converts video frame (UYVY/RGBA/NV12/I420) to loopback device format and writes raw buffer. */
337 int avx_v4l2_sink_write_frame(avx_v4l2_sink_t *sink, const oavx_video_frame_t *frame)
338 {
339 if (!sink || !frame || !frame->planes[0]) return -1;
340  
341 avx_mutex_lock(&sink->lock);
342  
343 /* Check if frame dimension changed and re-negotiate format if needed */
344 if (frame->width != sink->width || frame->height != sink->height) {
345 if (avx_v4l2_sink_set_format(sink, frame->width, frame->height, sink->pixelformat) != 0) {
346 avx_mutex_unlock(&sink->lock);
347 return -1;
348 }
349 }
350  
351 const void *send_data = frame->planes[0];
352 size_t send_size = sink->frame_bytes;
353  
354 /* Handle format conversion to sink->pixelformat */
355 if (sink->pixelformat == V4L2_PIX_FMT_YUYV) {
356 if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY) {
357 /* Convert UYVY -> YUYV */
358 size_t pixels = (size_t)frame->width * frame->height;
359 size_t yuyv_bytes = pixels * 2;
360 if (sink->conv_buf_cap >= yuyv_bytes) {
361 uyvy_to_yuyv_convert(frame->planes[0], sink->conv_buf, pixels);
362 send_data = sink->conv_buf;
363 send_size = yuyv_bytes;
364 }
365 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_I420) {
366 /* Convert Planar I420 -> Packed YUYV */
367 size_t pixels = (size_t)frame->width * frame->height;
368 size_t yuyv_bytes = pixels * 2;
369 if (sink->conv_buf_cap >= yuyv_bytes) {
370 i420_to_yuyv_convert(
371 frame->planes[0], frame->strides[0],
372 frame->planes[1], frame->strides[1],
373 frame->planes[2], frame->strides[2],
374 sink->conv_buf, (size_t)frame->width * 2,
375 (int)frame->width, (int)frame->height
376 );
377 send_data = sink->conv_buf;
378 send_size = yuyv_bytes;
379 }
380 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_YV12) {
381 /* Convert Planar YV12 (V before U) -> Packed YUYV */
382 size_t pixels = (size_t)frame->width * frame->height;
383 size_t yuyv_bytes = pixels * 2;
384 if (sink->conv_buf_cap >= yuyv_bytes) {
385 i420_to_yuyv_convert(
386 frame->planes[0], frame->strides[0],
387 frame->planes[2], frame->strides[2],
388 frame->planes[1], frame->strides[1],
389 sink->conv_buf, (size_t)frame->width * 2,
390 (int)frame->width, (int)frame->height
391 );
392 send_data = sink->conv_buf;
393 send_size = yuyv_bytes;
394 }
395 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_NV12) {
396 /* Convert Semi-planar NV12 -> Packed YUYV */
397 size_t pixels = (size_t)frame->width * frame->height;
398 size_t yuyv_bytes = pixels * 2;
399 if (sink->conv_buf_cap >= yuyv_bytes) {
400 nv12_to_yuyv_convert(
401 frame->planes[0], frame->strides[0],
402 frame->planes[1], frame->strides[1],
403 sink->conv_buf, (size_t)frame->width * 2,
404 (int)frame->width, (int)frame->height
405 );
406 send_data = sink->conv_buf;
407 send_size = yuyv_bytes;
408 }
409 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA || frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBX) {
410 /* Convert RGBA -> UYVY, then UYVY -> YUYV */
411 size_t pixels = (size_t)frame->width * frame->height;
412 size_t yuyv_bytes = pixels * 2;
413 if (sink->conv_buf_cap >= yuyv_bytes * 2) {
414 uint8_t *tmp_uyvy = sink->conv_buf + yuyv_bytes;
415 avx_color_rgba_to_uyvy(frame->planes[0], tmp_uyvy, (int)frame->width, (int)frame->height);
416 uyvy_to_yuyv_convert(tmp_uyvy, sink->conv_buf, pixels);
417 send_data = sink->conv_buf;
418 send_size = yuyv_bytes;
419 }
420 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA || frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRX) {
421 /* Convert BGRA -> UYVY, then UYVY -> YUYV */
422 size_t pixels = (size_t)frame->width * frame->height;
423 size_t yuyv_bytes = pixels * 2;
424 if (sink->conv_buf_cap >= yuyv_bytes * 2) {
425 uint8_t *tmp_uyvy = sink->conv_buf + yuyv_bytes;
426 avx_color_bgra_to_uyvy(frame->planes[0], tmp_uyvy, (int)frame->width, (int)frame->height);
427 uyvy_to_yuyv_convert(tmp_uyvy, sink->conv_buf, pixels);
428 send_data = sink->conv_buf;
429 send_size = yuyv_bytes;
430 }
431 }
432 } else if (sink->pixelformat == V4L2_PIX_FMT_UYVY) {
433 if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY) {
434 /* Already UYVY: zero-copy passthrough */
435 send_data = frame->planes[0];
436 send_size = (size_t)frame->width * frame->height * 2;
437 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_I420) {
438 /* Convert Planar I420 -> Packed UYVY */
439 size_t uyvy_bytes = (size_t)frame->width * frame->height * 2;
440 if (sink->conv_buf_cap >= uyvy_bytes) {
441 avx_color_i420_to_uyvy(
442 frame->planes[0], frame->strides[0],
443 frame->planes[1], frame->strides[1],
444 frame->planes[2], frame->strides[2],
445 sink->conv_buf, (size_t)frame->width * 2,
446 (int)frame->width, (int)frame->height
447 );
448 send_data = sink->conv_buf;
449 send_size = uyvy_bytes;
450 }
451 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_YV12) {
452 /* Convert Planar YV12 -> Packed UYVY */
453 size_t uyvy_bytes = (size_t)frame->width * frame->height * 2;
454 if (sink->conv_buf_cap >= uyvy_bytes) {
455 avx_color_i420_to_uyvy(
456 frame->planes[0], frame->strides[0],
457 frame->planes[2], frame->strides[2],
458 frame->planes[1], frame->strides[1],
459 sink->conv_buf, (size_t)frame->width * 2,
460 (int)frame->width, (int)frame->height
461 );
462 send_data = sink->conv_buf;
463 send_size = uyvy_bytes;
464 }
465 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_NV12) {
466 /* Convert Semi-planar NV12 -> Packed UYVY */
467 size_t uyvy_bytes = (size_t)frame->width * frame->height * 2;
468 if (sink->conv_buf_cap >= uyvy_bytes) {
469 avx_color_nv12_to_uyvy(
470 frame->planes[0], frame->strides[0],
471 frame->planes[1], frame->strides[1],
472 sink->conv_buf, (size_t)frame->width * 2,
473 (int)frame->width, (int)frame->height
474 );
475 send_data = sink->conv_buf;
476 send_size = uyvy_bytes;
477 }
478 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA || frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBX) {
479 size_t uyvy_bytes = (size_t)frame->width * frame->height * 2;
480 if (sink->conv_buf_cap >= uyvy_bytes) {
481 avx_color_rgba_to_uyvy(frame->planes[0], sink->conv_buf, (int)frame->width, (int)frame->height);
482 send_data = sink->conv_buf;
483 send_size = uyvy_bytes;
484 }
485 } else if (frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA || frame->fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRX) {
486 size_t uyvy_bytes = (size_t)frame->width * frame->height * 2;
487 if (sink->conv_buf_cap >= uyvy_bytes) {
488 avx_color_bgra_to_uyvy(frame->planes[0], sink->conv_buf, (int)frame->width, (int)frame->height);
489 send_data = sink->conv_buf;
490 send_size = uyvy_bytes;
491 }
492 }
493 }
494  
495 ssize_t written = write(sink->fd, send_data, send_size);
496 avx_mutex_unlock(&sink->lock);
497  
498 return (written >= 0) ? 0 : -1;
499 }
500  
501 const char* avx_v4l2_sink_get_device_path(const avx_v4l2_sink_t *sink)
502 {
503 return sink ? sink->device_path : "";
504 }
505  
506 void avx_v4l2_sink_destroy(avx_v4l2_sink_t *sink)
507 {
508 if (!sink) return;
509 avx_mutex_lock(&sink->lock);
510 if (sink->fd >= 0) {
511 close(sink->fd);
512 sink->fd = -1;
513 }
514 if (sink->conv_buf) {
515 free(sink->conv_buf);
516 sink->conv_buf = NULL;
517 sink->conv_buf_cap = 0;
518 }
519 avx_mutex_unlock(&sink->lock);
520 avx_mutex_destroy(&sink->lock);
521 free(sink);
522 }
523  
524 #else /* !AVX_OS_LINUX */
525  
526 /* Portable non-Linux fallback stubs */
527 /* Probes /dev/video* device nodes via VIDIOC_QUERYCAP to locate active v4l2loopback instances. */
528 bool avx_v4l2_sink_probe_loopback(char *out_path, size_t out_path_len)
529 {
530 (void)out_path; (void)out_path_len;
531 return false;
532 }
533  
534 avx_v4l2_sink_t* avx_v4l2_sink_create(const avx_v4l2_sink_config_t *cfg)
535 {
536 (void)cfg;
537 return NULL;
538 }
539  
540 /* Sets output resolution, pixel format, and field order on V4L2 loopback sink via VIDIOC_S_FMT. */
541 int avx_v4l2_sink_set_format(avx_v4l2_sink_t *sink, uint32_t width, uint32_t height, uint32_t pixelformat)
542 {
543 (void)sink; (void)width; (void)height; (void)pixelformat;
544 return -1;
545 }
546  
547 int avx_v4l2_sink_write_raw(avx_v4l2_sink_t *sink, const void *data, size_t size)
548 {
549 (void)sink; (void)data; (void)size;
550 return -1;
551 }
552  
553 /* Converts video frame (UYVY/RGBA/NV12/I420) to loopback device format and writes raw buffer. */
554 int avx_v4l2_sink_write_frame(avx_v4l2_sink_t *sink, const oavx_video_frame_t *frame)
555 {
556 (void)sink; (void)frame;
557 return -1;
558 }
559  
560 const char* avx_v4l2_sink_get_device_path(const avx_v4l2_sink_t *sink)
561 {
562 (void)sink;
563 return "";
564 }
565  
566 void avx_v4l2_sink_destroy(avx_v4l2_sink_t *sink)
567 {
568 (void)sink;
569 }
570  
571 #endif /* AVX_OS_LINUX */