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 "libavutil/pixdesc.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/pixfmt.h"
35 #include "v4l2_context.h"
39 static inline int v4l2_splane_video(struct v4l2_capability *cap)
41 if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT) &&
42 cap->capabilities & V4L2_CAP_STREAMING)
45 if (cap->capabilities & V4L2_CAP_VIDEO_M2M)
51 static inline int v4l2_mplane_video(struct v4l2_capability *cap)
53 if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE) &&
54 cap->capabilities & V4L2_CAP_STREAMING)
57 if (cap->capabilities & V4L2_CAP_VIDEO_M2M_MPLANE)
63 static int v4l2_prepare_contexts(V4L2m2mContext* s, int probe)
65 struct v4l2_capability cap;
68 s->capture.done = s->output.done = 0;
69 s->capture.name = "capture";
70 s->output.name = "output";
71 atomic_init(&s->refcount, 0);
72 sem_init(&s->refsync, 0, 0);
74 memset(&cap, 0, sizeof(cap));
75 ret = ioctl(s->fd, VIDIOC_QUERYCAP, &cap);
79 av_log(s->avctx, probe ? AV_LOG_DEBUG : AV_LOG_INFO,
80 "driver '%s' on card '%s' in %s mode\n", cap.driver, cap.card,
81 v4l2_mplane_video(&cap) ? "mplane" :
82 v4l2_splane_video(&cap) ? "splane" : "unknown");
84 if (v4l2_mplane_video(&cap)) {
85 s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
86 s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
90 if (v4l2_splane_video(&cap)) {
91 s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
92 s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
96 return AVERROR(EINVAL);
99 static int v4l2_probe_driver(V4L2m2mContext* s)
103 s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
105 return AVERROR(errno);
107 ret = v4l2_prepare_contexts(s, 1);
111 ret = ff_v4l2_context_get_format(&s->output, 1);
113 av_log(s->avctx, AV_LOG_DEBUG, "v4l2 output format not supported\n");
117 ret = ff_v4l2_context_get_format(&s->capture, 1);
119 av_log(s->avctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n");
124 if (close(s->fd) < 0) {
125 ret = AVERROR(errno);
126 av_log(s->avctx, AV_LOG_ERROR, "failure closing %s (%s)\n", s->devname, av_err2str(AVERROR(errno)));
134 static int v4l2_configure_contexts(V4L2m2mContext* s)
136 void *log_ctx = s->avctx;
138 struct v4l2_format ofmt, cfmt;
140 s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
142 return AVERROR(errno);
144 ret = v4l2_prepare_contexts(s, 0);
148 ofmt = s->output.format;
149 cfmt = s->capture.format;
150 av_log(log_ctx, AV_LOG_INFO, "requesting formats: output=%s capture=%s\n",
151 av_fourcc2str(V4L2_TYPE_IS_MULTIPLANAR(ofmt.type) ?
152 ofmt.fmt.pix_mp.pixelformat :
153 ofmt.fmt.pix.pixelformat),
154 av_fourcc2str(V4L2_TYPE_IS_MULTIPLANAR(cfmt.type) ?
155 cfmt.fmt.pix_mp.pixelformat :
156 cfmt.fmt.pix.pixelformat));
158 ret = ff_v4l2_context_set_format(&s->output);
160 av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n");
164 ret = ff_v4l2_context_set_format(&s->capture);
166 av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n");
170 ret = ff_v4l2_context_init(&s->output);
172 av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n");
176 /* decoder's buffers need to be updated at a later stage */
177 if (!av_codec_is_decoder(s->avctx->codec)) {
178 ret = ff_v4l2_context_init(&s->capture);
180 av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n");
188 if (close(s->fd) < 0) {
189 ret = AVERROR(errno);
190 av_log(log_ctx, AV_LOG_ERROR, "error closing %s (%s)\n",
191 s->devname, av_err2str(AVERROR(errno)));
198 /******************************************************************************
202 ******************************************************************************/
203 int ff_v4l2_m2m_codec_reinit(V4L2m2mContext* s)
207 av_log(s->avctx, AV_LOG_DEBUG, "reinit context\n");
210 ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
212 av_log(s->avctx, AV_LOG_ERROR, "capture VIDIOC_STREAMOFF\n");
214 /* 2. unmap the capture buffers (v4l2 and ffmpeg):
215 * we must wait for all references to be released before being allowed
216 * to queue new buffers.
218 av_log(s->avctx, AV_LOG_DEBUG, "waiting for user to release AVBufferRefs\n");
219 if (atomic_load(&s->refcount))
220 while(sem_wait(&s->refsync) == -1 && errno == EINTR);
222 ff_v4l2_context_release(&s->capture);
224 /* 3. get the new capture format */
225 ret = ff_v4l2_context_get_format(&s->capture, 0);
227 av_log(s->avctx, AV_LOG_ERROR, "query the new capture format\n");
231 /* 4. set the capture format */
232 ret = ff_v4l2_context_set_format(&s->capture);
234 av_log(s->avctx, AV_LOG_ERROR, "setting capture format\n");
238 /* 5. complete reinit */
245 int ff_v4l2_m2m_codec_full_reinit(V4L2m2mContext *s)
247 void *log_ctx = s->avctx;
250 av_log(log_ctx, AV_LOG_DEBUG, "%s full reinit\n", s->devname);
252 /* wait for pending buffer references */
253 if (atomic_load(&s->refcount))
254 while(sem_wait(&s->refsync) == -1 && errno == EINTR);
256 ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF);
258 av_log(log_ctx, AV_LOG_ERROR, "output VIDIOC_STREAMOFF\n");
262 ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
264 av_log(log_ctx, AV_LOG_ERROR, "capture VIDIOC_STREAMOFF\n");
268 /* release and unmmap the buffers */
269 ff_v4l2_context_release(&s->output);
270 ff_v4l2_context_release(&s->capture);
272 /* start again now that we know the stream dimensions */
276 ret = ff_v4l2_context_get_format(&s->output, 0);
278 av_log(log_ctx, AV_LOG_DEBUG, "v4l2 output format not supported\n");
282 ret = ff_v4l2_context_get_format(&s->capture, 0);
284 av_log(log_ctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n");
288 ret = ff_v4l2_context_set_format(&s->output);
290 av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n");
294 ret = ff_v4l2_context_set_format(&s->capture);
296 av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n");
300 ret = ff_v4l2_context_init(&s->output);
302 av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n");
306 /* decoder's buffers need to be updated at a later stage */
307 if (!av_codec_is_decoder(s->avctx->codec)) {
308 ret = ff_v4l2_context_init(&s->capture);
310 av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n");
321 static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context)
323 V4L2m2mContext *s = (V4L2m2mContext*)context;
325 ff_v4l2_context_release(&s->capture);
326 sem_destroy(&s->refsync);
333 int ff_v4l2_m2m_codec_end(AVCodecContext *avctx)
335 V4L2m2mPriv *priv = avctx->priv_data;
336 V4L2m2mContext* s = priv->context;
339 ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF);
341 av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->output.name);
343 ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
345 av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->capture.name);
347 ff_v4l2_context_release(&s->output);
350 av_buffer_unref(&priv->context_ref);
355 int ff_v4l2_m2m_codec_init(AVCodecContext *avctx)
357 int ret = AVERROR(EINVAL);
358 struct dirent *entry;
362 V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
365 dirp = opendir("/dev");
367 return AVERROR(errno);
369 for (entry = readdir(dirp); entry; entry = readdir(dirp)) {
371 if (strncmp(entry->d_name, "video", 5))
374 snprintf(node, sizeof(node), "/dev/%s", entry->d_name);
375 av_log(s->avctx, AV_LOG_DEBUG, "probing device %s\n", node);
376 strncpy(s->devname, node, strlen(node) + 1);
377 ret = v4l2_probe_driver(s);
385 av_log(s->avctx, AV_LOG_ERROR, "Could not find a valid device\n");
386 memset(s->devname, 0, sizeof(s->devname));
391 av_log(s->avctx, AV_LOG_INFO, "Using device %s\n", node);
393 return v4l2_configure_contexts(s);
396 int ff_v4l2_m2m_create_context(AVCodecContext *avctx, V4L2m2mContext **s)
398 V4L2m2mPriv *priv = avctx->priv_data;
400 *s = av_mallocz(sizeof(V4L2m2mContext));
402 return AVERROR(ENOMEM);
404 priv->context_ref = av_buffer_create((uint8_t *) *s, sizeof(V4L2m2mContext),
405 &v4l2_m2m_destroy_context, NULL, 0);
406 if (!priv->context_ref) {
408 return AVERROR(ENOMEM);
411 /* assign the context */
415 priv->context->capture.num_buffers = priv->num_capture_buffers;
416 priv->context->output.num_buffers = priv->num_output_buffers;
417 priv->context->self_ref = priv->context_ref;
418 priv->context->fd = -1;