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_video_v4l2.c - Linux V4L2 Stateful M2M Hardware Decoder Backend
7 */
8  
9 #include "avx_video_codec.h"
10 #include "avx_internal.h"
11  
12 #if defined(AVX_OS_LINUX)
13  
14 #include <linux/videodev2.h>
15 #include <sys/ioctl.h>
16 #include <sys/mman.h>
17 #include <fcntl.h>
18 #include <poll.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24  
25 #define AVX_V4L2_MAX_DEVICES 64
26 #define AVX_V4L2_OUT_BUFS 4
27 #define AVX_V4L2_CAP_BUFS 6
28 #define AVX_V4L2_OUT_BUF_MIN (2U * 1024U * 1024U)
29  
30 typedef struct {
31 void *ptr;
32 size_t len;
33 bool in_use;
34 } avx_v4l2_buf_t;
35  
36 typedef struct {
37 int fd;
38 uint32_t src_fourcc;
39 uint32_t v4l2_in_fmt;
40 uint32_t v4l2_cap_fmt;
41 uint32_t width;
42 uint32_t height;
43 uint32_t bytesperline[2];
44 uint32_t plane_sizes[2];
45 uint32_t cap_num_planes;
46  
47 avx_v4l2_buf_t out_bufs[AVX_V4L2_OUT_BUFS];
48 uint32_t out_count;
49  
50 struct {
51 void *planes[2];
52 size_t lens[2];
53 } cap_bufs[AVX_V4L2_CAP_BUFS];
54 uint32_t cap_count;
55  
56 bool streaming_out;
57 bool streaming_cap;
58  
59 uint8_t *frame_buf;
60 size_t frame_buf_size;
61 } avx_v4l2_dec_ctx_t;
62  
63 static avx_mutex_t s_v4l2_probe_lock = AVX_MUTEX_INITIALIZER;
64 static bool s_v4l2_probed = false;
65 static char s_v4l2_dev_h264[64];
66 static char s_v4l2_dev_hevc[64];
67 static char s_v4l2_dev_av1[64];
68  
69 static int xioctl(int fd, unsigned long req, void *arg) {
70 int rc;
71 do {
72 rc = ioctl(fd, req, arg);
73 } while (rc == -1 && errno == EINTR);
74 return rc;
75 }
76  
77 static bool v4l2_enum_has_fmt(int fd, uint32_t buf_type, uint32_t target_pixfmt, bool require_compressed) {
78 struct v4l2_fmtdesc fmtdesc;
79 memset(&fmtdesc, 0, sizeof(fmtdesc));
80 fmtdesc.type = buf_type;
81 for (fmtdesc.index = 0; ; fmtdesc.index++) {
82 if (xioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) != 0) break;
83 if (fmtdesc.pixelformat == target_pixfmt) {
84 if (require_compressed && !(fmtdesc.flags & V4L2_FMT_FLAG_COMPRESSED)) {
85 return false;
86 }
87 return true;
88 }
89 }
90 return false;
91 }
92  
93 /* Probes /dev/video* devices via VIDIOC_QUERYCAP to discover V4L2 M2M hardware video decoders. */
94 static void v4l2_probe_devices_once(void) {
95 avx_mutex_lock(&s_v4l2_probe_lock);
96 if (s_v4l2_probed) {
97 avx_mutex_unlock(&s_v4l2_probe_lock);
98 return;
99 }
100  
101 s_v4l2_dev_h264[0] = '\0';
102 s_v4l2_dev_hevc[0] = '\0';
103 s_v4l2_dev_av1[0] = '\0';
104  
105 for (int i = 0; i < AVX_V4L2_MAX_DEVICES; ++i) {
106 char path[64];
107 snprintf(path, sizeof(path), "/dev/video%d", i);
108 int fd = open(path, O_RDWR | O_NONBLOCK);
109 if (fd < 0) continue;
110  
111 struct v4l2_capability cap;
112 memset(&cap, 0, sizeof(cap));
113 if (xioctl(fd, VIDIOC_QUERYCAP, &cap) != 0) {
114 close(fd);
115 continue;
116 }
117  
118 uint32_t caps = (cap.capabilities & V4L2_CAP_DEVICE_CAPS) ? cap.device_caps : cap.capabilities;
119 if (!(caps & V4L2_CAP_VIDEO_M2M_MPLANE) || !(caps & V4L2_CAP_STREAMING)) {
120 close(fd);
121 continue;
122 }
123  
124 bool cap_has_yuv = v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, V4L2_PIX_FMT_NV12, false) ||
125 v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, V4L2_PIX_FMT_NV12M, false) ||
126 v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, V4L2_PIX_FMT_YUV420, false) ||
127 v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, V4L2_PIX_FMT_YUV420M, false);
128 if (!cap_has_yuv) {
129 close(fd);
130 continue;
131 }
132  
133 #ifdef V4L2_PIX_FMT_H264
134 if (s_v4l2_dev_h264[0] == '\0' &&
135 v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, V4L2_PIX_FMT_H264, true)) {
136 snprintf(s_v4l2_dev_h264, sizeof(s_v4l2_dev_h264), "%s", path);
137 }
138 #endif
139 #ifdef V4L2_PIX_FMT_HEVC
140 if (s_v4l2_dev_hevc[0] == '\0' &&
141 v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, V4L2_PIX_FMT_HEVC, true)) {
142 snprintf(s_v4l2_dev_hevc, sizeof(s_v4l2_dev_hevc), "%s", path);
143 }
144 #endif
145 #ifdef V4L2_PIX_FMT_AV1
146 if (s_v4l2_dev_av1[0] == '\0' &&
147 v4l2_enum_has_fmt(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, V4L2_PIX_FMT_AV1, true)) {
148 snprintf(s_v4l2_dev_av1, sizeof(s_v4l2_dev_av1), "%s", path);
149 }
150 #endif
151 close(fd);
152 }
153  
154 s_v4l2_probed = true;
155 avx_mutex_unlock(&s_v4l2_probe_lock);
156 }
157  
158 static const char *v4l2_select_device_and_fmt(uint32_t fourcc, uint32_t *out_pixfmt) {
159 v4l2_probe_devices_once();
160  
161 if (fourcc == OAVX_FOURCC_VIDEO_TYPE_H264 || fourcc == 0x34363268) {
162 #ifdef V4L2_PIX_FMT_H264
163 if (s_v4l2_dev_h264[0] != '\0') {
164 if (out_pixfmt) *out_pixfmt = V4L2_PIX_FMT_H264;
165 return s_v4l2_dev_h264;
166 }
167 #endif
168 } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC || fourcc == 0x43564568) {
169 #ifdef V4L2_PIX_FMT_HEVC
170 if (s_v4l2_dev_hevc[0] != '\0') {
171 if (out_pixfmt) *out_pixfmt = V4L2_PIX_FMT_HEVC;
172 return s_v4l2_dev_hevc;
173 }
174 #endif
175 } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_AV01) {
176 #ifdef V4L2_PIX_FMT_AV1
177 if (s_v4l2_dev_av1[0] != '\0') {
178 if (out_pixfmt) *out_pixfmt = V4L2_PIX_FMT_AV1;
179 return s_v4l2_dev_av1;
180 }
181 #endif
182 }
183 return NULL;
184 }
185  
186 static void v4l2_unmap_capture_bufs(avx_v4l2_dec_ctx_t *ctx) {
187 for (uint32_t i = 0; i < ctx->cap_count; ++i) {
188 for (uint32_t p = 0; p < 2; ++p) {
189 if (ctx->cap_bufs[i].planes[p] && ctx->cap_bufs[i].planes[p] != MAP_FAILED) {
190 munmap(ctx->cap_bufs[i].planes[p], ctx->cap_bufs[i].lens[p]);
191 }
192 ctx->cap_bufs[i].planes[p] = NULL;
193 ctx->cap_bufs[i].lens[p] = 0;
194 }
195 }
196 ctx->cap_count = 0;
197 }
198  
199 /* Negotiates V4L2 capture queue format (NV12/YUV420M) and memory-maps driver buffers. */
200 static int v4l2_setup_capture_queue(avx_v4l2_dec_ctx_t *ctx) {
201 struct v4l2_format cap_fmt;
202 memset(&cap_fmt, 0, sizeof(cap_fmt));
203 cap_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
204  
205 if (xioctl(ctx->fd, VIDIOC_G_FMT, &cap_fmt) != 0) {
206 cap_fmt.fmt.pix_mp.width = ctx->width;
207 cap_fmt.fmt.pix_mp.height = ctx->height;
208 }
209  
210 uint32_t candidates[4] = {
211 V4L2_PIX_FMT_NV12,
212 V4L2_PIX_FMT_NV12M,
213 V4L2_PIX_FMT_YUV420,
214 V4L2_PIX_FMT_YUV420M
215 };
216 bool fmt_ok = false;
217 for (int i = 0; i < 4; ++i) {
218 struct v4l2_format try_fmt = cap_fmt;
219 try_fmt.fmt.pix_mp.pixelformat = candidates[i];
220 try_fmt.fmt.pix_mp.field = V4L2_FIELD_NONE;
221 if (xioctl(ctx->fd, VIDIOC_S_FMT, &try_fmt) == 0 &&
222 try_fmt.fmt.pix_mp.pixelformat == candidates[i]) {
223 cap_fmt = try_fmt;
224 fmt_ok = true;
225 break;
226 }
227 }
228 if (!fmt_ok && xioctl(ctx->fd, VIDIOC_G_FMT, &cap_fmt) != 0) {
229 return -1;
230 }
231  
232 ctx->v4l2_cap_fmt = cap_fmt.fmt.pix_mp.pixelformat;
233 if (cap_fmt.fmt.pix_mp.width > 0) ctx->width = cap_fmt.fmt.pix_mp.width;
234 if (cap_fmt.fmt.pix_mp.height > 0) ctx->height = cap_fmt.fmt.pix_mp.height;
235 ctx->cap_num_planes = cap_fmt.fmt.pix_mp.num_planes ? cap_fmt.fmt.pix_mp.num_planes : 1;
236 if (ctx->cap_num_planes > 2) ctx->cap_num_planes = 2;
237 for (uint32_t p = 0; p < ctx->cap_num_planes; ++p) {
238 ctx->bytesperline[p] = cap_fmt.fmt.pix_mp.plane_fmt[p].bytesperline;
239 ctx->plane_sizes[p] = cap_fmt.fmt.pix_mp.plane_fmt[p].sizeimage;
240 }
241  
242 struct v4l2_requestbuffers req;
243 memset(&req, 0, sizeof(req));
244 req.count = AVX_V4L2_CAP_BUFS;
245 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
246 req.memory = V4L2_MEMORY_MMAP;
247 if (xioctl(ctx->fd, VIDIOC_REQBUFS, &req) != 0 || req.count == 0) {
248 return -1;
249 }
250 if (req.count > AVX_V4L2_CAP_BUFS) req.count = AVX_V4L2_CAP_BUFS;
251 ctx->cap_count = req.count;
252  
253 for (uint32_t i = 0; i < ctx->cap_count; ++i) {
254 struct v4l2_plane planes[VIDEO_MAX_PLANES];
255 struct v4l2_buffer buf;
256 memset(planes, 0, sizeof(planes));
257 memset(&buf, 0, sizeof(buf));
258 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
259 buf.memory = V4L2_MEMORY_MMAP;
260 buf.index = i;
261 buf.length = ctx->cap_num_planes;
262 buf.m.planes = planes;
263  
264 if (xioctl(ctx->fd, VIDIOC_QUERYBUF, &buf) != 0) return -1;
265  
266 for (uint32_t p = 0; p < ctx->cap_num_planes; ++p) {
267 void *addr = mmap(NULL, planes[p].length, PROT_READ | PROT_WRITE, MAP_SHARED,
268 ctx->fd, planes[p].m.mem_offset);
269 if (addr == MAP_FAILED) return -1;
270 ctx->cap_bufs[i].planes[p] = addr;
271 ctx->cap_bufs[i].lens[p] = planes[p].length;
272 }
273 }
274  
275 for (uint32_t i = 0; i < ctx->cap_count; ++i) {
276 struct v4l2_plane planes[VIDEO_MAX_PLANES];
277 struct v4l2_buffer buf;
278 memset(planes, 0, sizeof(planes));
279 memset(&buf, 0, sizeof(buf));
280 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
281 buf.memory = V4L2_MEMORY_MMAP;
282 buf.index = i;
283 buf.length = ctx->cap_num_planes;
284 buf.m.planes = planes;
285 if (xioctl(ctx->fd, VIDIOC_QBUF, &buf) != 0) return -1;
286 }
287  
288 uint32_t cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
289 if (xioctl(ctx->fd, VIDIOC_STREAMON, &cap_type) != 0) return -1;
290 ctx->streaming_cap = true;
291 return 0;
292 }
293  
294 static bool v4l2_dec_supports_fourcc(uint32_t src_fourcc) {
295 return v4l2_select_device_and_fmt(src_fourcc, NULL) != NULL;
296 }
297  
298 static void v4l2_dec_destroy(void *backend_ctx) {
299 avx_v4l2_dec_ctx_t *ctx = (avx_v4l2_dec_ctx_t *)backend_ctx;
300 if (!ctx) return;
301  
302 if (ctx->fd >= 0) {
303 if (ctx->streaming_out) {
304 uint32_t t = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
305 xioctl(ctx->fd, VIDIOC_STREAMOFF, &t);
306 ctx->streaming_out = false;
307 }
308 if (ctx->streaming_cap) {
309 uint32_t t = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
310 xioctl(ctx->fd, VIDIOC_STREAMOFF, &t);
311 ctx->streaming_cap = false;
312 }
313 for (uint32_t i = 0; i < ctx->out_count; ++i) {
314 if (ctx->out_bufs[i].ptr && ctx->out_bufs[i].ptr != MAP_FAILED) {
315 munmap(ctx->out_bufs[i].ptr, ctx->out_bufs[i].len);
316 }
317 }
318 v4l2_unmap_capture_bufs(ctx);
319 close(ctx->fd);
320 ctx->fd = -1;
321 }
322  
323 free(ctx->frame_buf);
324 free(ctx);
325 }
326  
327 static int v4l2_dec_create(const avx_video_dec_cfg_t *cfg, void **backend_ctx) {
328 if (!cfg || !backend_ctx) return -1;
329  
330 uint32_t in_pixfmt = 0;
331 const char *dev_path = v4l2_select_device_and_fmt(cfg->fourcc, &in_pixfmt);
332 if (!dev_path) return -1;
333  
334 int fd = open(dev_path, O_RDWR | O_NONBLOCK);
335 if (fd < 0) return -1;
336  
337 avx_v4l2_dec_ctx_t *ctx = (avx_v4l2_dec_ctx_t *)calloc(1, sizeof(*ctx));
338 if (!ctx) {
339 close(fd);
340 return -1;
341 }
342 ctx->fd = fd;
343 ctx->src_fourcc = cfg->fourcc;
344 ctx->v4l2_in_fmt = in_pixfmt;
345 ctx->width = cfg->width ? cfg->width : 1920;
346 ctx->height = cfg->height ? cfg->height : 1080;
347  
348 struct v4l2_event_subscription sub;
349 memset(&sub, 0, sizeof(sub));
350 sub.type = V4L2_EVENT_SOURCE_CHANGE;
351 xioctl(ctx->fd, VIDIOC_SUBSCRIBE_EVENT, &sub); /* Best-effort; not all drivers require */
352  
353 struct v4l2_format out_fmt;
354 memset(&out_fmt, 0, sizeof(out_fmt));
355 out_fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
356 out_fmt.fmt.pix_mp.width = ctx->width;
357 out_fmt.fmt.pix_mp.height = ctx->height;
358 out_fmt.fmt.pix_mp.pixelformat = in_pixfmt;
359 out_fmt.fmt.pix_mp.num_planes = 1;
360 out_fmt.fmt.pix_mp.plane_fmt[0].sizeimage = AVX_V4L2_OUT_BUF_MIN;
361 if (xioctl(ctx->fd, VIDIOC_S_FMT, &out_fmt) != 0) {
362 v4l2_dec_destroy(ctx);
363 return -1;
364 }
365  
366 struct v4l2_requestbuffers req;
367 memset(&req, 0, sizeof(req));
368 req.count = AVX_V4L2_OUT_BUFS;
369 req.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
370 req.memory = V4L2_MEMORY_MMAP;
371 if (xioctl(ctx->fd, VIDIOC_REQBUFS, &req) != 0 || req.count == 0) {
372 v4l2_dec_destroy(ctx);
373 return -1;
374 }
375 if (req.count > AVX_V4L2_OUT_BUFS) req.count = AVX_V4L2_OUT_BUFS;
376 ctx->out_count = req.count;
377  
378 for (uint32_t i = 0; i < ctx->out_count; ++i) {
379 struct v4l2_plane planes[VIDEO_MAX_PLANES];
380 struct v4l2_buffer buf;
381 memset(planes, 0, sizeof(planes));
382 memset(&buf, 0, sizeof(buf));
383 buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
384 buf.memory = V4L2_MEMORY_MMAP;
385 buf.index = i;
386 buf.length = 1;
387 buf.m.planes = planes;
388  
389 if (xioctl(ctx->fd, VIDIOC_QUERYBUF, &buf) != 0) {
390 v4l2_dec_destroy(ctx);
391 return -1;
392 }
393 void *addr = mmap(NULL, planes[0].length, PROT_READ | PROT_WRITE, MAP_SHARED,
394 ctx->fd, planes[0].m.mem_offset);
395 if (addr == MAP_FAILED) {
396 v4l2_dec_destroy(ctx);
397 return -1;
398 }
399 ctx->out_bufs[i].ptr = addr;
400 ctx->out_bufs[i].len = planes[0].length;
401 ctx->out_bufs[i].in_use = false;
402 }
403  
404 uint32_t out_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
405 if (xioctl(ctx->fd, VIDIOC_STREAMON, &out_type) != 0) {
406 v4l2_dec_destroy(ctx);
407 return -1;
408 }
409 ctx->streaming_out = true;
410  
411 if (v4l2_setup_capture_queue(ctx) != 0) {
412 v4l2_dec_destroy(ctx);
413 return -1;
414 }
415  
416 *backend_ctx = ctx;
417 return 0;
418 }
419  
420 static void v4l2_reclaim_output_bufs(avx_v4l2_dec_ctx_t *ctx) {
421 for (;;) {
422 struct v4l2_plane planes[VIDEO_MAX_PLANES];
423 struct v4l2_buffer buf;
424 memset(planes, 0, sizeof(planes));
425 memset(&buf, 0, sizeof(buf));
426 buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
427 buf.memory = V4L2_MEMORY_MMAP;
428 buf.length = 1;
429 buf.m.planes = planes;
430 if (xioctl(ctx->fd, VIDIOC_DQBUF, &buf) != 0) break;
431 if (buf.index < ctx->out_count) {
432 ctx->out_bufs[buf.index].in_use = false;
433 }
434 }
435 }
436  
437 /* Handles V4L2_EVENT_SOURCE_CHANGE events, draining in-flight frames and reallocating capture buffers. */
438 static int v4l2_handle_source_change(avx_v4l2_dec_ctx_t *ctx) {
439 struct v4l2_event ev;
440 memset(&ev, 0, sizeof(ev));
441 while (xioctl(ctx->fd, VIDIOC_DQEVENT, &ev) == 0) {
442 if (ev.type == V4L2_EVENT_SOURCE_CHANGE &&
443 (ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION)) {
444 if (ctx->streaming_cap) {
445 uint32_t t = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
446 xioctl(ctx->fd, VIDIOC_STREAMOFF, &t);
447 ctx->streaming_cap = false;
448 }
449 v4l2_unmap_capture_bufs(ctx);
450  
451 struct v4l2_requestbuffers rel;
452 memset(&rel, 0, sizeof(rel));
453 rel.count = 0;
454 rel.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
455 rel.memory = V4L2_MEMORY_MMAP;
456 xioctl(ctx->fd, VIDIOC_REQBUFS, &rel);
457  
458 if (v4l2_setup_capture_queue(ctx) != 0) return -1;
459 }
460 }
461 return 0;
462 }
463  
464 /* Queues compressed NAL packets to V4L2 output queue and drains decoded pictures from capture queue. */
465 static int v4l2_dec_decode(void *backend_ctx,
466 const uint8_t *pkt_data, size_t pkt_size,
467 oavx_video_frame_t *out_frame)
468 {
469 avx_v4l2_dec_ctx_t *ctx = (avx_v4l2_dec_ctx_t *)backend_ctx;
470 if (!ctx || ctx->fd < 0 || !pkt_data || pkt_size == 0 || !out_frame) {
471 return AVX_VIDEO_CODEC_ERROR;
472 }
473  
474 if (v4l2_handle_source_change(ctx) != 0) {
475 return AVX_VIDEO_CODEC_ERROR;
476 }
477  
478 v4l2_reclaim_output_bufs(ctx);
479  
480 int free_idx = -1;
481 for (uint32_t i = 0; i < ctx->out_count; ++i) {
482 if (!ctx->out_bufs[i].in_use) {
483 free_idx = (int)i;
484 break;
485 }
486 }
487 if (free_idx < 0) {
488 struct pollfd pfd = { .fd = ctx->fd, .events = POLLOUT, .revents = 0 };
489 poll(&pfd, 1, 5);
490 v4l2_reclaim_output_bufs(ctx);
491 for (uint32_t i = 0; i < ctx->out_count; ++i) {
492 if (!ctx->out_bufs[i].in_use) {
493 free_idx = (int)i;
494 break;
495 }
496 }
497 }
498 if (free_idx < 0 || pkt_size > ctx->out_bufs[free_idx].len) {
499 return AVX_VIDEO_CODEC_ERROR;
500 }
501  
502 memcpy(ctx->out_bufs[free_idx].ptr, pkt_data, pkt_size);
503  
504 struct v4l2_plane out_planes[VIDEO_MAX_PLANES];
505 struct v4l2_buffer out_buf;
506 memset(out_planes, 0, sizeof(out_planes));
507 memset(&out_buf, 0, sizeof(out_buf));
508 out_planes[0].bytesused = (uint32_t)pkt_size;
509 out_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
510 out_buf.memory = V4L2_MEMORY_MMAP;
511 out_buf.index = (uint32_t)free_idx;
512 out_buf.length = 1;
513 out_buf.m.planes = out_planes;
514  
515 if (xioctl(ctx->fd, VIDIOC_QBUF, &out_buf) != 0) {
516 return AVX_VIDEO_CODEC_ERROR;
517 }
518 ctx->out_bufs[free_idx].in_use = true;
519  
520 struct pollfd cap_pfd = { .fd = ctx->fd, .events = POLLIN | POLLPRI, .revents = 0 };
521 poll(&cap_pfd, 1, 2);
522  
523 if (cap_pfd.revents & POLLPRI) {
524 if (v4l2_handle_source_change(ctx) != 0) {
525 return AVX_VIDEO_CODEC_ERROR;
526 }
527 }
528  
529 struct v4l2_plane cap_planes[VIDEO_MAX_PLANES];
530 struct v4l2_buffer cap_buf;
531 memset(cap_planes, 0, sizeof(cap_planes));
532 memset(&cap_buf, 0, sizeof(cap_buf));
533 cap_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
534 cap_buf.memory = V4L2_MEMORY_MMAP;
535 cap_buf.length = ctx->cap_num_planes;
536 cap_buf.m.planes = cap_planes;
537  
538 if (xioctl(ctx->fd, VIDIOC_DQBUF, &cap_buf) != 0) {
539 if (errno == EAGAIN) {
540 return AVX_VIDEO_CODEC_NEED_MORE_DATA;
541 }
542 if (errno == EPIPE) {
543 if (v4l2_handle_source_change(ctx) == 0) {
544 return AVX_VIDEO_CODEC_NEED_MORE_DATA;
545 }
546 }
547 return AVX_VIDEO_CODEC_ERROR;
548 }
549  
550 if ((cap_buf.flags & V4L2_BUF_FLAG_LAST) && cap_planes[0].bytesused == 0) {
551 xioctl(ctx->fd, VIDIOC_QBUF, &cap_buf);
552 if (v4l2_handle_source_change(ctx) != 0) {
553 return AVX_VIDEO_CODEC_ERROR;
554 }
555 return AVX_VIDEO_CODEC_NEED_MORE_DATA;
556 }
557  
558 uint32_t idx = cap_buf.index;
559 uint32_t w = ctx->width;
560 uint32_t h = ctx->height;
561 uint64_t need_u64 = (uint64_t)w * (uint64_t)h * 2ULL;
562 if (w == 0 || h == 0 || w > AVX_MAX_VIDEO_WIDTH || h > AVX_MAX_VIDEO_HEIGHT ||
563 need_u64 > AVX_MAX_DECOMPRESSED_FRAME_SIZE || idx >= ctx->cap_count) {
564 xioctl(ctx->fd, VIDIOC_QBUF, &cap_buf);
565 return AVX_VIDEO_CODEC_ERROR;
566 }
567  
568 uint32_t cw = (w + 1U) / 2U;
569 uint32_t ch = (h + 1U) / 2U;
570 bool is_nv12 = (ctx->v4l2_cap_fmt == V4L2_PIX_FMT_NV12 || ctx->v4l2_cap_fmt == V4L2_PIX_FMT_NV12M);
571 size_t y_stride = ctx->bytesperline[0] ? ctx->bytesperline[0] : (size_t)w;
572 size_t need = is_nv12
573 ? ((size_t)w * (size_t)h + (size_t)w * (size_t)ch)
574 : ((size_t)w * (size_t)h + 2ULL * (size_t)cw * (size_t)ch);
575 if (ctx->frame_buf_size < need) {
576 uint8_t *nb = (uint8_t *)realloc(ctx->frame_buf, need);
577 if (!nb) {
578 xioctl(ctx->fd, VIDIOC_QBUF, &cap_buf);
579 return AVX_VIDEO_CODEC_ERROR;
580 }
581 ctx->frame_buf = nb;
582 ctx->frame_buf_size = need;
583 }
584  
585 const uint8_t *src_p0 = (const uint8_t *)ctx->cap_bufs[idx].planes[0];
586 size_t src_p0_len = ctx->cap_bufs[idx].lens[0];
587  
588 if (ctx->v4l2_cap_fmt == V4L2_PIX_FMT_NV12 || ctx->v4l2_cap_fmt == V4L2_PIX_FMT_NV12M) {
589 uint32_t ch = (h + 1) / 2;
590 for (uint32_t y = 0; y < h; ++y) {
591 if ((size_t)y * y_stride + w <= src_p0_len) {
592 memcpy(ctx->frame_buf + (size_t)y * w, src_p0 + (size_t)y * y_stride, w);
593 }
594 }
595 const uint8_t *src_uv = (ctx->cap_num_planes > 1 && ctx->cap_bufs[idx].planes[1])
596 ? (const uint8_t *)ctx->cap_bufs[idx].planes[1]
597 : (src_p0 + (size_t)h * y_stride);
598 size_t uv_stride = (ctx->cap_num_planes > 1 && ctx->bytesperline[1]) ? ctx->bytesperline[1] : y_stride;
599 size_t uv_max_len = (ctx->cap_num_planes > 1) ? ctx->cap_bufs[idx].lens[1] : src_p0_len;
600 size_t uv_off = (ctx->cap_num_planes > 1) ? 0 : (size_t)h * y_stride;
601 uint8_t *dst_uv = ctx->frame_buf + (size_t)w * h;
602 for (uint32_t y = 0; y < ch; ++y) {
603 if (uv_off + (size_t)y * uv_stride + w <= uv_max_len) {
604 memcpy(dst_uv + (size_t)y * w, src_uv + (size_t)y * uv_stride, w);
605 }
606 }
607 out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_NV12;
608 out_frame->width = w;
609 out_frame->height = h;
610 out_frame->planes[0] = ctx->frame_buf;
611 out_frame->strides[0] = w;
612 out_frame->planes[1] = dst_uv;
613 out_frame->strides[1] = w;
614 out_frame->planes[2] = NULL;
615 out_frame->strides[2] = 0;
616 out_frame->planes[3] = NULL;
617 out_frame->strides[3] = 0;
618 } else {
619 /* Fallback I420 (single-plane contiguous Y + U + V) */
620 uint32_t cw = (w + 1) / 2;
621 uint32_t ch = (h + 1) / 2;
622 for (uint32_t y = 0; y < h; ++y) {
623 if ((size_t)y * y_stride + w <= src_p0_len) {
624 memcpy(ctx->frame_buf + (size_t)y * w, src_p0 + (size_t)y * y_stride, w);
625 }
626 }
627 const uint8_t *src_u = src_p0 + (size_t)h * y_stride;
628 size_t c_stride = y_stride / 2;
629 uint8_t *dst_u = ctx->frame_buf + (size_t)w * h;
630 uint8_t *dst_v = dst_u + (size_t)cw * ch;
631 for (uint32_t y = 0; y < ch; ++y) {
632 if ((size_t)h * y_stride + (size_t)y * c_stride + cw <= src_p0_len) {
633 memcpy(dst_u + (size_t)y * cw, src_u + (size_t)y * c_stride, cw);
634 }
635 }
636 const uint8_t *src_v = src_u + (size_t)ch * c_stride;
637 for (uint32_t y = 0; y < ch; ++y) {
638 if ((size_t)h * y_stride + (size_t)ch * c_stride + (size_t)y * c_stride + cw <= src_p0_len) {
639 memcpy(dst_v + (size_t)y * cw, src_v + (size_t)y * c_stride, cw);
640 }
641 }
642 out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_I420;
643 out_frame->width = w;
644 out_frame->height = h;
645 out_frame->planes[0] = ctx->frame_buf;
646 out_frame->strides[0] = w;
647 out_frame->planes[1] = dst_u;
648 out_frame->strides[1] = cw;
649 out_frame->planes[2] = dst_v;
650 out_frame->strides[2] = cw;
651 out_frame->planes[3] = NULL;
652 out_frame->strides[3] = 0;
653 }
654  
655 xioctl(ctx->fd, VIDIOC_QBUF, &cap_buf);
656 return AVX_VIDEO_CODEC_OK;
657 }
658  
659 static void v4l2_dec_reset(void *backend_ctx) {
660 avx_v4l2_dec_ctx_t *ctx = (avx_v4l2_dec_ctx_t *)backend_ctx;
661 if (!ctx || ctx->fd < 0) return;
662  
663 if (ctx->streaming_out) {
664 uint32_t t = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
665 xioctl(ctx->fd, VIDIOC_STREAMOFF, &t);
666 }
667 if (ctx->streaming_cap) {
668 uint32_t t = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
669 xioctl(ctx->fd, VIDIOC_STREAMOFF, &t);
670 }
671 for (uint32_t i = 0; i < ctx->out_count; ++i) {
672 ctx->out_bufs[i].in_use = false;
673 }
674 for (uint32_t i = 0; i < ctx->cap_count; ++i) {
675 struct v4l2_plane planes[VIDEO_MAX_PLANES];
676 struct v4l2_buffer buf;
677 memset(planes, 0, sizeof(planes));
678 memset(&buf, 0, sizeof(buf));
679 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
680 buf.memory = V4L2_MEMORY_MMAP;
681 buf.index = i;
682 buf.length = ctx->cap_num_planes;
683 buf.m.planes = planes;
684 xioctl(ctx->fd, VIDIOC_QBUF, &buf);
685 }
686 if (ctx->streaming_out) {
687 uint32_t t = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
688 xioctl(ctx->fd, VIDIOC_STREAMON, &t);
689 }
690 if (ctx->streaming_cap) {
691 uint32_t t = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
692 xioctl(ctx->fd, VIDIOC_STREAMON, &t);
693 }
694 }
695  
696 static const avx_video_decoder_ops_t s_v4l2_decoder_ops = {
697 .name = "v4l2-stateful",
698 .priority = 75,
699 .is_hardware = true,
700 .supports_fourcc = v4l2_dec_supports_fourcc,
701 .create = v4l2_dec_create,
702 .decode = v4l2_dec_decode,
703 .reset = v4l2_dec_reset,
704 .destroy = v4l2_dec_destroy
705 };
706  
707 const avx_video_decoder_ops_t *avx_video_v4l2_get_ops(void) {
708 return &s_v4l2_decoder_ops;
709 }
710  
711 #else
712  
713 const avx_video_decoder_ops_t *avx_video_v4l2_get_ops(void) {
714 return NULL;
715 }
716  
717 #endif /* AVX_OS_LINUX */