2 * V4L2 buffer helper functions.
4 * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
5 * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
7 * This file is part of FFmpeg.
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.
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.
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
24 #include <linux/videodev2.h>
25 #include <sys/ioctl.h>
30 #include "libavcodec/avcodec.h"
31 #include "libavcodec/internal.h"
32 #include "v4l2_context.h"
33 #include "v4l2_buffers.h"
36 #define USEC_PER_SEC 1000000
38 static inline V4L2m2mContext *buf_to_m2mctx(V4L2Buffer *buf)
40 return V4L2_TYPE_IS_OUTPUT(buf->context->type) ?
41 container_of(buf->context, V4L2m2mContext, output) :
42 container_of(buf->context, V4L2m2mContext, capture);
45 static inline AVCodecContext *logger(V4L2Buffer *buf)
47 return buf_to_m2mctx(buf)->avctx;
50 static inline void v4l2_set_pts(V4L2Buffer *out, int64_t pts)
52 V4L2m2mContext *s = buf_to_m2mctx(out);
53 AVRational v4l2_timebase = { 1, USEC_PER_SEC };
56 if (pts == AV_NOPTS_VALUE)
59 /* convert pts to v4l2 timebase */
60 v4l2_pts = av_rescale_q(pts, s->avctx->time_base, v4l2_timebase);
61 out->buf.timestamp.tv_usec = v4l2_pts % USEC_PER_SEC;
62 out->buf.timestamp.tv_sec = v4l2_pts / USEC_PER_SEC;
65 static inline uint64_t v4l2_get_pts(V4L2Buffer *avbuf)
67 V4L2m2mContext *s = buf_to_m2mctx(avbuf);
68 AVRational v4l2_timebase = { 1, USEC_PER_SEC };
71 /* convert pts back to encoder timebase */
72 v4l2_pts = avbuf->buf.timestamp.tv_sec * USEC_PER_SEC + avbuf->buf.timestamp.tv_usec;
74 return av_rescale_q(v4l2_pts, v4l2_timebase, s->avctx->time_base);
77 static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf)
79 enum v4l2_ycbcr_encoding ycbcr;
80 enum v4l2_colorspace cs;
82 cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
83 buf->context->format.fmt.pix_mp.colorspace :
84 buf->context->format.fmt.pix.colorspace;
86 ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
87 buf->context->format.fmt.pix_mp.ycbcr_enc:
88 buf->context->format.fmt.pix.ycbcr_enc;
91 case V4L2_YCBCR_ENC_XV709:
92 case V4L2_YCBCR_ENC_709: return AVCOL_PRI_BT709;
93 case V4L2_YCBCR_ENC_XV601:
94 case V4L2_YCBCR_ENC_601:return AVCOL_PRI_BT470M;
100 case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_PRI_BT470BG;
101 case V4L2_COLORSPACE_SMPTE170M: return AVCOL_PRI_SMPTE170M;
102 case V4L2_COLORSPACE_SMPTE240M: return AVCOL_PRI_SMPTE240M;
103 case V4L2_COLORSPACE_BT2020: return AVCOL_PRI_BT2020;
108 return AVCOL_PRI_UNSPECIFIED;
111 static enum AVColorRange v4l2_get_color_range(V4L2Buffer *buf)
113 enum v4l2_quantization qt;
115 qt = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
116 buf->context->format.fmt.pix_mp.quantization :
117 buf->context->format.fmt.pix.quantization;
120 case V4L2_QUANTIZATION_LIM_RANGE: return AVCOL_RANGE_MPEG;
121 case V4L2_QUANTIZATION_FULL_RANGE: return AVCOL_RANGE_JPEG;
126 return AVCOL_RANGE_UNSPECIFIED;
129 static enum AVColorSpace v4l2_get_color_space(V4L2Buffer *buf)
131 enum v4l2_ycbcr_encoding ycbcr;
132 enum v4l2_colorspace cs;
134 cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
135 buf->context->format.fmt.pix_mp.colorspace :
136 buf->context->format.fmt.pix.colorspace;
138 ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
139 buf->context->format.fmt.pix_mp.ycbcr_enc:
140 buf->context->format.fmt.pix.ycbcr_enc;
143 case V4L2_COLORSPACE_SRGB: return AVCOL_SPC_RGB;
144 case V4L2_COLORSPACE_REC709: return AVCOL_SPC_BT709;
145 case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_SPC_FCC;
146 case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_SPC_BT470BG;
147 case V4L2_COLORSPACE_SMPTE170M: return AVCOL_SPC_SMPTE170M;
148 case V4L2_COLORSPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
149 case V4L2_COLORSPACE_BT2020:
150 if (ycbcr == V4L2_YCBCR_ENC_BT2020_CONST_LUM)
151 return AVCOL_SPC_BT2020_CL;
153 return AVCOL_SPC_BT2020_NCL;
158 return AVCOL_SPC_UNSPECIFIED;
161 static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf)
163 enum v4l2_ycbcr_encoding ycbcr;
164 enum v4l2_xfer_func xfer;
165 enum v4l2_colorspace cs;
167 cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
168 buf->context->format.fmt.pix_mp.colorspace :
169 buf->context->format.fmt.pix.colorspace;
171 ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
172 buf->context->format.fmt.pix_mp.ycbcr_enc:
173 buf->context->format.fmt.pix.ycbcr_enc;
175 xfer = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
176 buf->context->format.fmt.pix_mp.xfer_func:
177 buf->context->format.fmt.pix.xfer_func;
180 case V4L2_XFER_FUNC_709: return AVCOL_TRC_BT709;
181 case V4L2_XFER_FUNC_SRGB: return AVCOL_TRC_IEC61966_2_1;
187 case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_TRC_GAMMA22;
188 case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_TRC_GAMMA28;
189 case V4L2_COLORSPACE_SMPTE170M: return AVCOL_TRC_SMPTE170M;
190 case V4L2_COLORSPACE_SMPTE240M: return AVCOL_TRC_SMPTE240M;
196 case V4L2_YCBCR_ENC_XV709:
197 case V4L2_YCBCR_ENC_XV601: return AVCOL_TRC_BT1361_ECG;
202 return AVCOL_TRC_UNSPECIFIED;
205 static void v4l2_free_buffer(void *opaque, uint8_t *unused)
207 V4L2Buffer* avbuf = opaque;
208 V4L2m2mContext *s = buf_to_m2mctx(avbuf);
210 if (atomic_fetch_sub(&avbuf->context_refcount, 1) == 1) {
211 atomic_fetch_sub_explicit(&s->refcount, 1, memory_order_acq_rel);
214 if (!atomic_load(&s->refcount))
215 sem_post(&s->refsync);
218 /* no need to queue more buffers to the driver */
219 avbuf->status = V4L2BUF_AVAILABLE;
221 else if (avbuf->context->streamon)
222 ff_v4l2_buffer_enqueue(avbuf);
225 av_buffer_unref(&avbuf->context_ref);
229 static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
231 V4L2m2mContext *s = buf_to_m2mctx(in);
233 if (plane >= in->num_planes)
234 return AVERROR(EINVAL);
236 /* even though most encoders return 0 in data_offset encoding vp8 does require this value */
237 *buf = av_buffer_create((char *)in->plane_info[plane].mm_addr + in->planes[plane].data_offset,
238 in->plane_info[plane].length, v4l2_free_buffer, in, 0);
240 return AVERROR(ENOMEM);
243 atomic_fetch_add(&in->context_refcount, 1);
245 in->context_ref = av_buffer_ref(s->self_ref);
246 if (!in->context_ref) {
247 av_buffer_unref(buf);
248 return AVERROR(ENOMEM);
250 in->context_refcount = 1;
253 in->status = V4L2BUF_RET_USER;
254 atomic_fetch_add_explicit(&s->refcount, 1, memory_order_relaxed);
259 static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t* data, int size, AVBufferRef* bref)
261 unsigned int bytesused, length;
263 if (plane >= out->num_planes)
264 return AVERROR(EINVAL);
266 bytesused = FFMIN(size, out->plane_info[plane].length);
267 length = out->plane_info[plane].length;
269 memcpy(out->plane_info[plane].mm_addr, data, FFMIN(size, out->plane_info[plane].length));
271 if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) {
272 out->planes[plane].bytesused = bytesused;
273 out->planes[plane].length = length;
275 out->buf.bytesused = bytesused;
276 out->buf.length = length;
282 /******************************************************************************
284 * V4L2uffer interface
286 ******************************************************************************/
288 int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer* out)
292 for(i = 0; i < out->num_planes; i++) {
293 ret = v4l2_bufref_to_buf(out, i, frame->buf[i]->data, frame->buf[i]->size, frame->buf[i]);
298 v4l2_set_pts(out, frame->pts);
303 int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf)
305 V4L2m2mContext *s = buf_to_m2mctx(avbuf);
308 av_frame_unref(frame);
310 /* 1. get references to the actual data */
311 for (i = 0; i < avbuf->num_planes; i++) {
312 ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]);
316 frame->linesize[i] = avbuf->plane_info[i].bytesperline;
317 frame->data[i] = frame->buf[i]->data;
320 /* 1.1 fixup special cases */
321 switch (avbuf->context->av_pix_fmt) {
322 case AV_PIX_FMT_NV12:
323 if (avbuf->num_planes > 1)
325 frame->linesize[1] = avbuf->plane_info[0].bytesperline;
326 frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
332 /* 2. get frame information */
333 frame->key_frame = !!(avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME);
334 frame->format = avbuf->context->av_pix_fmt;
335 frame->color_primaries = v4l2_get_color_primaries(avbuf);
336 frame->colorspace = v4l2_get_color_space(avbuf);
337 frame->color_range = v4l2_get_color_range(avbuf);
338 frame->color_trc = v4l2_get_color_trc(avbuf);
339 frame->pts = v4l2_get_pts(avbuf);
341 /* these two values are updated also during re-init in v4l2_process_driver_event */
342 frame->height = s->output.height;
343 frame->width = s->output.width;
345 /* 3. report errors upstream */
346 if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
347 av_log(logger(avbuf), AV_LOG_ERROR, "%s: driver decode error\n", avbuf->context->name);
348 frame->decode_error_flags |= FF_DECODE_ERROR_INVALID_BITSTREAM;
354 int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf)
358 av_packet_unref(pkt);
359 ret = v4l2_buf_to_bufref(avbuf, 0, &pkt->buf);
363 pkt->size = V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type) ? avbuf->buf.m.planes[0].bytesused : avbuf->buf.bytesused;
364 pkt->data = pkt->buf->data;
366 if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME)
367 pkt->flags |= AV_PKT_FLAG_KEY;
369 if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
370 av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name);
371 pkt->flags |= AV_PKT_FLAG_CORRUPT;
374 pkt->dts = pkt->pts = v4l2_get_pts(avbuf);
379 int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
383 ret = v4l2_bufref_to_buf(out, 0, pkt->data, pkt->size, pkt->buf);
387 v4l2_set_pts(out, pkt->pts);
389 if (pkt->flags & AV_PKT_FLAG_KEY)
390 out->flags = V4L2_BUF_FLAG_KEYFRAME;
395 int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
397 V4L2Context *ctx = avbuf->context;
400 avbuf->buf.memory = V4L2_MEMORY_MMAP;
401 avbuf->buf.type = ctx->type;
402 avbuf->buf.index = index;
404 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
405 avbuf->buf.length = VIDEO_MAX_PLANES;
406 avbuf->buf.m.planes = avbuf->planes;
409 ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QUERYBUF, &avbuf->buf);
411 return AVERROR(errno);
413 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
414 avbuf->num_planes = 0;
416 /* in MP, the V4L2 API states that buf.length means num_planes */
417 if (avbuf->num_planes >= avbuf->buf.length)
419 if (avbuf->buf.m.planes[avbuf->num_planes].length)
423 avbuf->num_planes = 1;
425 for (i = 0; i < avbuf->num_planes; i++) {
427 avbuf->plane_info[i].bytesperline = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
428 ctx->format.fmt.pix_mp.plane_fmt[i].bytesperline :
429 ctx->format.fmt.pix.bytesperline;
431 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
432 avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length;
433 avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length,
434 PROT_READ | PROT_WRITE, MAP_SHARED,
435 buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset);
437 avbuf->plane_info[i].length = avbuf->buf.length;
438 avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length,
439 PROT_READ | PROT_WRITE, MAP_SHARED,
440 buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset);
443 if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
444 return AVERROR(ENOMEM);
447 avbuf->status = V4L2BUF_AVAILABLE;
449 if (V4L2_TYPE_IS_OUTPUT(ctx->type))
452 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
453 avbuf->buf.m.planes = avbuf->planes;
454 avbuf->buf.length = avbuf->num_planes;
457 avbuf->buf.bytesused = avbuf->planes[0].bytesused;
458 avbuf->buf.length = avbuf->planes[0].length;
461 return ff_v4l2_buffer_enqueue(avbuf);
464 int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf)
468 avbuf->buf.flags = avbuf->flags;
470 ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QBUF, &avbuf->buf);
472 return AVERROR(errno);
474 avbuf->status = V4L2BUF_IN_DRIVER;