FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/v4l2_buffers.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 0 384 0.0%
Functions: 0 24 0.0%
Branches: 0 330 0.0%

Line Branch Exec Source
1 /*
2 * V4L2 buffer helper functions.
3 *
4 * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
5 * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <linux/videodev2.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <poll.h>
31 #include "libavcodec/avcodec.h"
32 #include "libavutil/attributes.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/refstruct.h"
36 #include "v4l2_context.h"
37 #include "v4l2_buffers.h"
38 #include "v4l2_m2m.h"
39
40 #ifndef USEC_PER_SEC
41 #define USEC_PER_SEC 1000000
42 #endif
43
44 static AVRational v4l2_timebase = { 1, USEC_PER_SEC };
45
46 ✗ static inline V4L2m2mContext *buf_to_m2mctx(V4L2Buffer *buf)
47 {
48 ✗ return V4L2_TYPE_IS_OUTPUT(buf->context->type) ?
49 ✗ container_of(buf->context, V4L2m2mContext, output) :
50 ✗ container_of(buf->context, V4L2m2mContext, capture);
51 }
52
53 ✗ static inline AVCodecContext *logger(V4L2Buffer *buf)
54 {
55 ✗ return buf_to_m2mctx(buf)->avctx;
56 }
57
58 ✗ static inline AVRational v4l2_get_timebase(V4L2Buffer *avbuf)
59 {
60 ✗ V4L2m2mContext *s = buf_to_m2mctx(avbuf);
61
62 ✗ if (s->avctx->pkt_timebase.num)
63 ✗ return s->avctx->pkt_timebase;
64 ✗ return s->avctx->time_base;
65 }
66
67 ✗ static inline void v4l2_set_pts(V4L2Buffer *out, int64_t pts)
68 {
69 int64_t v4l2_pts;
70
71 ✗ if (pts == AV_NOPTS_VALUE)
72 ✗ pts = 0;
73
74 /* convert pts to v4l2 timebase */
75 ✗ v4l2_pts = av_rescale_q(pts, v4l2_get_timebase(out), v4l2_timebase);
76 ✗ out->buf.timestamp.tv_usec = v4l2_pts % USEC_PER_SEC;
77 ✗ out->buf.timestamp.tv_sec = v4l2_pts / USEC_PER_SEC;
78 ✗ }
79
80 ✗ static inline int64_t v4l2_get_pts(V4L2Buffer *avbuf)
81 {
82 int64_t v4l2_pts;
83
84 /* convert pts back to encoder timebase */
85 ✗ v4l2_pts = (int64_t)avbuf->buf.timestamp.tv_sec * USEC_PER_SEC +
86 ✗ avbuf->buf.timestamp.tv_usec;
87
88 ✗ return av_rescale_q(v4l2_pts, v4l2_timebase, v4l2_get_timebase(avbuf));
89 }
90
91 ✗ static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf)
92 {
93 enum v4l2_ycbcr_encoding ycbcr;
94 enum v4l2_colorspace cs;
95
96 ✗ cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
97 ✗ buf->context->format.fmt.pix_mp.colorspace :
98 ✗ buf->context->format.fmt.pix.colorspace;
99
100 ✗ ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
101 ✗ buf->context->format.fmt.pix_mp.ycbcr_enc:
102 ✗ buf->context->format.fmt.pix.ycbcr_enc;
103
104 ✗ switch(ycbcr) {
105 ✗ case V4L2_YCBCR_ENC_XV709:
106 ✗ case V4L2_YCBCR_ENC_709: return AVCOL_PRI_BT709;
107 ✗ case V4L2_YCBCR_ENC_XV601:
108 ✗ case V4L2_YCBCR_ENC_601:return AVCOL_PRI_BT470M;
109 ✗ default:
110 ✗ break;
111 }
112
113 ✗ switch(cs) {
114 ✗ case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_PRI_BT470BG;
115 ✗ case V4L2_COLORSPACE_SMPTE170M: return AVCOL_PRI_SMPTE170M;
116 ✗ case V4L2_COLORSPACE_SMPTE240M: return AVCOL_PRI_SMPTE240M;
117 ✗ case V4L2_COLORSPACE_BT2020: return AVCOL_PRI_BT2020;
118 ✗ default:
119 ✗ break;
120 }
121
122 ✗ return AVCOL_PRI_UNSPECIFIED;
123 }
124
125 ✗ static enum AVColorRange v4l2_get_color_range(V4L2Buffer *buf)
126 {
127 enum v4l2_quantization qt;
128
129 ✗ qt = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
130 ✗ buf->context->format.fmt.pix_mp.quantization :
131 ✗ buf->context->format.fmt.pix.quantization;
132
133 ✗ switch (qt) {
134 ✗ case V4L2_QUANTIZATION_LIM_RANGE: return AVCOL_RANGE_MPEG;
135 ✗ case V4L2_QUANTIZATION_FULL_RANGE: return AVCOL_RANGE_JPEG;
136 ✗ default:
137 ✗ break;
138 }
139
140 ✗ return AVCOL_RANGE_UNSPECIFIED;
141 }
142
143 ✗ static enum AVColorSpace v4l2_get_color_space(V4L2Buffer *buf)
144 {
145 enum v4l2_ycbcr_encoding ycbcr;
146 enum v4l2_colorspace cs;
147
148 ✗ cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
149 ✗ buf->context->format.fmt.pix_mp.colorspace :
150 ✗ buf->context->format.fmt.pix.colorspace;
151
152 ✗ ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
153 ✗ buf->context->format.fmt.pix_mp.ycbcr_enc:
154 ✗ buf->context->format.fmt.pix.ycbcr_enc;
155
156 ✗ switch(cs) {
157 ✗ case V4L2_COLORSPACE_SRGB: return AVCOL_SPC_RGB;
158 ✗ case V4L2_COLORSPACE_REC709: return AVCOL_SPC_BT709;
159 ✗ case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_SPC_FCC;
160 ✗ case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_SPC_BT470BG;
161 ✗ case V4L2_COLORSPACE_SMPTE170M: return AVCOL_SPC_SMPTE170M;
162 ✗ case V4L2_COLORSPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
163 ✗ case V4L2_COLORSPACE_BT2020:
164 ✗ if (ycbcr == V4L2_YCBCR_ENC_BT2020_CONST_LUM)
165 ✗ return AVCOL_SPC_BT2020_CL;
166 else
167 ✗ return AVCOL_SPC_BT2020_NCL;
168 ✗ default:
169 ✗ break;
170 }
171
172 ✗ return AVCOL_SPC_UNSPECIFIED;
173 }
174
175 ✗ static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf)
176 {
177 enum v4l2_ycbcr_encoding ycbcr;
178 enum v4l2_xfer_func xfer;
179 enum v4l2_colorspace cs;
180
181 ✗ cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
182 ✗ buf->context->format.fmt.pix_mp.colorspace :
183 ✗ buf->context->format.fmt.pix.colorspace;
184
185 ✗ ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
186 ✗ buf->context->format.fmt.pix_mp.ycbcr_enc:
187 ✗ buf->context->format.fmt.pix.ycbcr_enc;
188
189 ✗ xfer = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
190 ✗ buf->context->format.fmt.pix_mp.xfer_func:
191 ✗ buf->context->format.fmt.pix.xfer_func;
192
193 ✗ switch (xfer) {
194 ✗ case V4L2_XFER_FUNC_709: return AVCOL_TRC_BT709;
195 ✗ case V4L2_XFER_FUNC_SRGB: return AVCOL_TRC_IEC61966_2_1;
196 ✗ case V4L2_XFER_FUNC_SMPTE240M: return AVCOL_TRC_SMPTE240M;
197 ✗ case V4L2_XFER_FUNC_NONE: return AVCOL_TRC_LINEAR;
198 ✗ case V4L2_XFER_FUNC_SMPTE2084: return AVCOL_TRC_SMPTE2084;
199 ✗ default:
200 ✗ break;
201 }
202
203 ✗ switch (cs) {
204 ✗ case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_TRC_GAMMA22;
205 ✗ case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_TRC_GAMMA28;
206 ✗ case V4L2_COLORSPACE_SMPTE170M: return AVCOL_TRC_SMPTE170M;
207 ✗ case V4L2_COLORSPACE_SMPTE240M: return AVCOL_TRC_SMPTE240M;
208 ✗ default:
209 ✗ break;
210 }
211
212 ✗ switch (ycbcr) {
213 ✗ case V4L2_YCBCR_ENC_XV709:
214 ✗ case V4L2_YCBCR_ENC_XV601: return AVCOL_TRC_BT1361_ECG;
215 ✗ default:
216 ✗ break;
217 }
218
219 ✗ return AVCOL_TRC_UNSPECIFIED;
220 }
221
222 ✗ static void v4l2_get_interlacing(AVFrame *frame, V4L2Buffer *buf)
223 {
224 ✗ enum v4l2_field field = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
225 ✗ buf->context->format.fmt.pix_mp.field :
226 ✗ buf->context->format.fmt.pix.field;
227
228 ✗ switch (field) {
229 ✗ case V4L2_FIELD_INTERLACED:
230 case V4L2_FIELD_INTERLACED_TB:
231 ✗ frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
232 av_fallthrough;
233 ✗ case V4L2_FIELD_INTERLACED_BT:
234 ✗ frame->flags |= AV_FRAME_FLAG_INTERLACED;
235 ✗ break;
236 }
237 ✗ }
238
239 ✗ static void v4l2_free_buffer(void *opaque, uint8_t *unused)
240 {
241 ✗ V4L2Buffer* avbuf = opaque;
242 ✗ V4L2m2mContext *s = buf_to_m2mctx(avbuf);
243
244 ✗ if (atomic_fetch_sub(&avbuf->context_refcount, 1) == 1) {
245 ✗ atomic_fetch_sub_explicit(&s->refcount, 1, memory_order_acq_rel);
246
247 ✗ if (s->reinit) {
248 ✗ if (!atomic_load(&s->refcount))
249 ✗ sem_post(&s->refsync);
250 } else {
251 ✗ if (s->draining && V4L2_TYPE_IS_OUTPUT(avbuf->context->type)) {
252 /* no need to queue more buffers to the driver */
253 ✗ avbuf->status = V4L2BUF_AVAILABLE;
254 }
255 ✗ else if (avbuf->context->streamon)
256 ✗ ff_v4l2_buffer_enqueue(avbuf);
257 }
258
259 ✗ av_refstruct_unref(&avbuf->context_ref);
260 }
261 ✗ }
262
263 ✗ static int v4l2_buf_increase_ref(V4L2Buffer *in)
264 {
265 ✗ V4L2m2mContext *s = buf_to_m2mctx(in);
266
267 ✗ if (in->context_ref)
268 ✗ atomic_fetch_add(&in->context_refcount, 1);
269 else {
270 ✗ in->context_ref = av_refstruct_ref(s->self_ref);
271
272 ✗ in->context_refcount = 1;
273 ✗ atomic_fetch_add_explicit(&s->refcount, 1, memory_order_relaxed);
274 }
275
276 ✗ in->status = V4L2BUF_RET_USER;
277
278 ✗ return 0;
279 }
280
281 ✗ static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
282 {
283 int ret;
284
285 ✗ if (plane >= in->num_planes)
286 ✗ return AVERROR(EINVAL);
287
288 /* even though most encoders return 0 in data_offset encoding vp8 does require this value */
289 ✗ *buf = av_buffer_create((char *)in->plane_info[plane].mm_addr + in->planes[plane].data_offset,
290 in->plane_info[plane].length, v4l2_free_buffer, in, 0);
291 ✗ if (!*buf)
292 ✗ return AVERROR(ENOMEM);
293
294 ✗ ret = v4l2_buf_increase_ref(in);
295 ✗ if (ret)
296 ✗ av_buffer_unref(buf);
297
298 ✗ return ret;
299 }
300
301 ✗ static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t* data, int size, int offset)
302 {
303 unsigned int bytesused, length;
304
305 ✗ if (plane >= out->num_planes)
306 ✗ return AVERROR(EINVAL);
307
308 ✗ length = out->plane_info[plane].length;
309 ✗ bytesused = FFMIN(size+offset, length);
310
311 ✗ memcpy((uint8_t*)out->plane_info[plane].mm_addr+offset, data, FFMIN(size, length-offset));
312
313 ✗ if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) {
314 ✗ out->planes[plane].bytesused = bytesused;
315 ✗ out->planes[plane].length = length;
316 } else {
317 ✗ out->buf.bytesused = bytesused;
318 ✗ out->buf.length = length;
319 }
320
321 ✗ return 0;
322 }
323
324 ✗ static int v4l2_buffer_buf_to_swframe(AVFrame *frame, V4L2Buffer *avbuf)
325 {
326 int i, ret;
327
328 ✗ frame->format = avbuf->context->av_pix_fmt;
329
330 ✗ for (i = 0; i < avbuf->num_planes; i++) {
331 ✗ ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]);
332 ✗ if (ret)
333 ✗ return ret;
334
335 ✗ frame->linesize[i] = avbuf->plane_info[i].bytesperline;
336 ✗ frame->data[i] = frame->buf[i]->data;
337 }
338
339 /* fixup special cases */
340 ✗ switch (avbuf->context->av_pix_fmt) {
341 ✗ case AV_PIX_FMT_NV12:
342 case AV_PIX_FMT_NV21:
343 ✗ if (avbuf->num_planes > 1)
344 ✗ break;
345 ✗ frame->linesize[1] = avbuf->plane_info[0].bytesperline;
346 ✗ frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
347 ✗ break;
348
349 ✗ case AV_PIX_FMT_YUV420P:
350 ✗ if (avbuf->num_planes > 1)
351 ✗ break;
352 ✗ frame->linesize[1] = avbuf->plane_info[0].bytesperline >> 1;
353 ✗ frame->linesize[2] = avbuf->plane_info[0].bytesperline >> 1;
354 ✗ frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
355 ✗ frame->data[2] = frame->data[1] + ((avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height) >> 2);
356 ✗ break;
357
358 ✗ default:
359 ✗ break;
360 }
361
362 ✗ return 0;
363 }
364
365 /**
366 * Copy a software frame into one contiguous V4L2 memory plane while keeping
367 * the visible frame geometry separate from the driver-aligned storage layout.
368 */
369 ✗ static int v4l2_buffer_swframe_to_single_buffer(const AVFrame *frame,
370 V4L2Buffer *out,
371 uint32_t pixel_format,
372 unsigned int width,
373 unsigned int height,
374 unsigned int bytesperline,
375 unsigned int sizeimage)
376 {
377 ✗ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
378 uint8_t *dst_data[4];
379 int active_linesizes[4], storage_linesizes[4];
380 ✗ int dst_linesizes[4] = { 0 };
381 ✗ int i, layout_size, planes_nb = 0, ret;
382 ✗ size_t length = out->plane_info[0].length;
383
384 ✗ if (!desc || out->num_planes < 1 ||
385 ✗ frame->width <= 0 || frame->height <= 0 ||
386 ✗ width > INT_MAX || height > INT_MAX || bytesperline > INT_MAX ||
387 ✗ frame->width > width || frame->height > height)
388 ✗ return AVERROR(EINVAL);
389
390 /* Active linesizes describe pixels; storage linesizes include alignment. */
391 ✗ ret = av_image_fill_linesizes(active_linesizes, frame->format,
392 ✗ frame->width);
393 ✗ if (ret < 0)
394 ✗ return ret;
395
396 ✗ ret = av_image_fill_linesizes(storage_linesizes, frame->format, width);
397 ✗ if (ret < 0)
398 ✗ return ret;
399
400 ✗ for (i = 0; i < desc->nb_components; i++)
401 ✗ planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
402
403 ✗ if (!bytesperline)
404 ✗ bytesperline = storage_linesizes[0];
405
406 ✗ if (bytesperline < storage_linesizes[0])
407 ✗ return AVERROR(EINVAL);
408
409 ✗ if (planes_nb == 1) {
410 ✗ dst_linesizes[0] = bytesperline;
411 } else {
412 /*
413 * The supported contiguous multi-plane formats have 8-bit luma, so
414 * bytesperline is also the padded luma width used to derive chroma
415 * strides.
416 */
417 ✗ if (storage_linesizes[0] != width)
418 ✗ return AVERROR(EINVAL);
419
420 ✗ ret = av_image_fill_linesizes(dst_linesizes, frame->format,
421 bytesperline);
422 ✗ if (ret < 0 || dst_linesizes[0] != bytesperline)
423 ✗ return ret < 0 ? ret : AVERROR(EINVAL);
424 }
425
426 /* Storage height determines plane offsets; only visible rows are copied. */
427 ✗ layout_size = av_image_fill_pointers(dst_data, frame->format, height,
428 ✗ out->plane_info[0].mm_addr,
429 dst_linesizes);
430 ✗ if (layout_size < 0)
431 ✗ return layout_size;
432
433 /* Reject the complete layout before any write can exceed driver storage. */
434 ✗ if ((size_t)layout_size > length ||
435 ✗ (sizeimage && (size_t)layout_size > sizeimage))
436 ✗ return AVERROR(EINVAL);
437
438 /* av_image_copy2() requires each active row to fit both line strides. */
439 ✗ for (i = 0; i < planes_nb; i++) {
440 ✗ int64_t src_linesize = frame->linesize[i];
441
442 ✗ if (src_linesize < 0)
443 ✗ src_linesize = -src_linesize;
444
445 ✗ if (!frame->data[i] || !dst_data[i] ||
446 ✗ src_linesize < active_linesizes[i] ||
447 ✗ dst_linesizes[i] < active_linesizes[i])
448 ✗ return AVERROR(EINVAL);
449 }
450
451 /* AVFrame stores Y, U, V while YVU formats place V before U. */
452 ✗ switch (pixel_format) {
453 ✗ case V4L2_PIX_FMT_YVU410:
454 case V4L2_PIX_FMT_YVU420:
455 ✗ FFSWAP(uint8_t *, dst_data[1], dst_data[2]);
456 ✗ break;
457 }
458
459 /* Copy visible pixels only; aligned rows and columns remain padding. */
460 ✗ av_image_copy2(dst_data, dst_linesizes, frame->data, frame->linesize,
461 ✗ frame->format, frame->width, frame->height);
462
463 ✗ if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) {
464 ✗ out->planes[0].bytesused = layout_size;
465 ✗ out->planes[0].length = length;
466 } else {
467 ✗ out->buf.bytesused = layout_size;
468 ✗ out->buf.length = length;
469 }
470
471 ✗ return 0;
472 }
473
474 ✗ static int v4l2_buffer_swframe_to_planes(const AVFrame *frame, V4L2Buffer *out)
475 {
476 ✗ const struct v4l2_pix_format_mplane *pix_mp = &out->context->format.fmt.pix_mp;
477 ✗ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
478 ✗ int height = pix_mp->height;
479 int bytewidths[4];
480 int ret;
481
482 ✗ if (!desc || !V4L2_TYPE_IS_MULTIPLANAR(out->buf.type) ||
483 ✗ out->num_planes > av_pix_fmt_count_planes(frame->format) ||
484 ✗ frame->width > pix_mp->width || frame->height > height)
485 ✗ return AVERROR(EINVAL);
486
487 ✗ ret = av_image_fill_linesizes(bytewidths, frame->format, frame->width);
488 ✗ if (ret < 0)
489 ✗ return ret;
490
491 ✗ for (int i = 0; i < out->num_planes; i++) {
492 ✗ int shift = i == 1 || i == 2 ? desc->log2_chroma_h : 0;
493 ✗ size_t stride = pix_mp->plane_fmt[i].bytesperline;
494 ✗ size_t size = stride * AV_CEIL_RSHIFT(height, shift);
495
496 ✗ if (!frame->data[i] || stride < bytewidths[i] ||
497 ✗ size > out->plane_info[i].length)
498 ✗ return AVERROR(EINVAL);
499
500 ✗ av_image_copy_plane(out->plane_info[i].mm_addr, stride,
501 ✗ frame->data[i], frame->linesize[i], bytewidths[i],
502 ✗ AV_CEIL_RSHIFT(frame->height, shift));
503 ✗ out->planes[i].bytesused = size;
504 ✗ out->planes[i].length = out->plane_info[i].length;
505 }
506
507 ✗ return 0;
508 }
509
510 ✗ static int v4l2_buffer_swframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
511 {
512 ✗ struct v4l2_format fmt = out->context->format;
513 ✗ int pixel_format = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
514 ✗ fmt.fmt.pix_mp.pixelformat : fmt.fmt.pix.pixelformat;
515 ✗ int width = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
516 ✗ fmt.fmt.pix_mp.width : fmt.fmt.pix.width;
517 ✗ int height = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
518 ✗ fmt.fmt.pix_mp.height : fmt.fmt.pix.height;
519 ✗ int bytesperline = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
520 ✗ (fmt.fmt.pix_mp.num_planes ?
521 ✗ fmt.fmt.pix_mp.plane_fmt[0].bytesperline : 0) :
522 ✗ fmt.fmt.pix.bytesperline;
523 ✗ int sizeimage = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
524 ✗ (fmt.fmt.pix_mp.num_planes ?
525 ✗ fmt.fmt.pix_mp.plane_fmt[0].sizeimage : 0) :
526 ✗ fmt.fmt.pix.sizeimage;
527 ✗ int is_planar_format = 0;
528
529 ✗ switch (pixel_format) {
530 ✗ case V4L2_PIX_FMT_YUV420M:
531 case V4L2_PIX_FMT_YVU420M:
532 #ifdef V4L2_PIX_FMT_YUV422M
533 case V4L2_PIX_FMT_YUV422M:
534 #endif
535 #ifdef V4L2_PIX_FMT_YVU422M
536 case V4L2_PIX_FMT_YVU422M:
537 #endif
538 #ifdef V4L2_PIX_FMT_YUV444M
539 case V4L2_PIX_FMT_YUV444M:
540 #endif
541 #ifdef V4L2_PIX_FMT_YVU444M
542 case V4L2_PIX_FMT_YVU444M:
543 #endif
544 case V4L2_PIX_FMT_NV12M:
545 case V4L2_PIX_FMT_NV21M:
546 case V4L2_PIX_FMT_NV12MT_16X16:
547 case V4L2_PIX_FMT_NV12MT:
548 case V4L2_PIX_FMT_NV16M:
549 case V4L2_PIX_FMT_NV61M:
550 ✗ is_planar_format = 1;
551 }
552
553 ✗ if (!is_planar_format)
554 ✗ return v4l2_buffer_swframe_to_single_buffer(frame, out,
555 pixel_format, width,
556 height, bytesperline,
557 sizeimage);
558
559 ✗ return v4l2_buffer_swframe_to_planes(frame, out);
560 }
561
562 /******************************************************************************
563 *
564 * V4L2Buffer interface
565 *
566 ******************************************************************************/
567
568 ✗ int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
569 {
570 ✗ v4l2_set_pts(out, frame->pts);
571
572 ✗ return v4l2_buffer_swframe_to_buf(frame, out);
573 }
574
575 ✗ int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf)
576 {
577 int ret;
578
579 ✗ av_frame_unref(frame);
580
581 /* 1. get references to the actual data */
582 ✗ ret = v4l2_buffer_buf_to_swframe(frame, avbuf);
583 ✗ if (ret)
584 ✗ return ret;
585
586 /* 2. get frame information */
587 ✗ if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME)
588 ✗ frame->flags |= AV_FRAME_FLAG_KEY;
589 ✗ frame->color_primaries = v4l2_get_color_primaries(avbuf);
590 ✗ frame->colorspace = v4l2_get_color_space(avbuf);
591 ✗ frame->color_range = v4l2_get_color_range(avbuf);
592 ✗ frame->color_trc = v4l2_get_color_trc(avbuf);
593 ✗ frame->pts = v4l2_get_pts(avbuf);
594 ✗ frame->pkt_dts = AV_NOPTS_VALUE;
595 ✗ v4l2_get_interlacing(frame, avbuf);
596
597 /* these values are updated also during re-init in v4l2_process_driver_event */
598 ✗ frame->height = avbuf->context->height;
599 ✗ frame->width = avbuf->context->width;
600 ✗ frame->sample_aspect_ratio = avbuf->context->sample_aspect_ratio;
601
602 /* 3. report errors upstream */
603 ✗ if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
604 ✗ av_log(logger(avbuf), AV_LOG_ERROR, "%s: driver decode error\n", avbuf->context->name);
605 ✗ frame->decode_error_flags |= FF_DECODE_ERROR_INVALID_BITSTREAM;
606 }
607
608 ✗ return 0;
609 }
610
611 ✗ int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf)
612 {
613 int ret;
614
615 ✗ av_packet_unref(pkt);
616 ✗ ret = v4l2_buf_to_bufref(avbuf, 0, &pkt->buf);
617 ✗ if (ret)
618 ✗ return ret;
619
620 ✗ pkt->size = V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type) ? avbuf->buf.m.planes[0].bytesused : avbuf->buf.bytesused;
621 ✗ pkt->data = pkt->buf->data;
622
623 ✗ if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME)
624 ✗ pkt->flags |= AV_PKT_FLAG_KEY;
625
626 ✗ if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
627 ✗ av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name);
628 ✗ pkt->flags |= AV_PKT_FLAG_CORRUPT;
629 }
630
631 ✗ pkt->dts = pkt->pts = v4l2_get_pts(avbuf);
632
633 ✗ return 0;
634 }
635
636 ✗ int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
637 {
638 int ret;
639
640 ✗ ret = v4l2_bufref_to_buf(out, 0, pkt->data, pkt->size, 0);
641 ✗ if (ret)
642 ✗ return ret;
643
644 ✗ v4l2_set_pts(out, pkt->pts);
645
646 ✗ if (pkt->flags & AV_PKT_FLAG_KEY)
647 ✗ out->flags = V4L2_BUF_FLAG_KEYFRAME;
648
649 ✗ return 0;
650 }
651
652 ✗ int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
653 {
654 ✗ V4L2Context *ctx = avbuf->context;
655 int ret, i;
656
657 ✗ avbuf->buf.memory = V4L2_MEMORY_MMAP;
658 ✗ avbuf->buf.type = ctx->type;
659 ✗ avbuf->buf.index = index;
660
661 ✗ if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
662 ✗ avbuf->buf.length = VIDEO_MAX_PLANES;
663 ✗ avbuf->buf.m.planes = avbuf->planes;
664 }
665
666 ✗ ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QUERYBUF, &avbuf->buf);
667 ✗ if (ret < 0)
668 ✗ return AVERROR(errno);
669
670 ✗ if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
671 ✗ avbuf->num_planes = 0;
672 /* in MP, the V4L2 API states that buf.length means num_planes */
673 ✗ for (i = 0; i < avbuf->buf.length; i++) {
674 ✗ if (avbuf->buf.m.planes[i].length)
675 ✗ avbuf->num_planes++;
676 }
677 } else
678 ✗ avbuf->num_planes = 1;
679
680 ✗ for (i = 0; i < avbuf->num_planes; i++) {
681
682 ✗ avbuf->plane_info[i].bytesperline = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
683 ✗ ctx->format.fmt.pix_mp.plane_fmt[i].bytesperline :
684 ✗ ctx->format.fmt.pix.bytesperline;
685
686 ✗ if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
687 ✗ avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length;
688 ✗ avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length,
689 PROT_READ | PROT_WRITE, MAP_SHARED,
690 ✗ buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset);
691 } else {
692 ✗ avbuf->plane_info[i].length = avbuf->buf.length;
693 ✗ avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length,
694 PROT_READ | PROT_WRITE, MAP_SHARED,
695 ✗ buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset);
696 }
697
698 ✗ if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
699 ✗ return AVERROR(ENOMEM);
700 }
701
702 ✗ avbuf->status = V4L2BUF_AVAILABLE;
703
704 ✗ if (V4L2_TYPE_IS_OUTPUT(ctx->type))
705 ✗ return 0;
706
707 ✗ if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
708 ✗ avbuf->buf.m.planes = avbuf->planes;
709 ✗ avbuf->buf.length = avbuf->num_planes;
710
711 } else {
712 ✗ avbuf->buf.bytesused = avbuf->planes[0].bytesused;
713 ✗ avbuf->buf.length = avbuf->planes[0].length;
714 }
715
716 ✗ return ff_v4l2_buffer_enqueue(avbuf);
717 }
718
719 ✗ int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf)
720 {
721 int ret;
722
723 ✗ avbuf->buf.flags = avbuf->flags;
724
725 ✗ ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QBUF, &avbuf->buf);
726 ✗ if (ret < 0)
727 ✗ return AVERROR(errno);
728
729 ✗ avbuf->status = V4L2BUF_IN_DRIVER;
730
731 ✗ return 0;
732 }
733