2017-09-21 03:55:40 +02:00
|
|
|
/*
|
|
|
|
* V4L mem2mem
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
|
|
|
|
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/videodev2.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/pixfmt.h"
|
|
|
|
#include "v4l2_context.h"
|
|
|
|
#include "v4l2_fmt.h"
|
|
|
|
#include "v4l2_m2m.h"
|
|
|
|
|
|
|
|
static inline int v4l2_splane_video(struct v4l2_capability *cap)
|
|
|
|
{
|
|
|
|
if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT) &&
|
|
|
|
cap->capabilities & V4L2_CAP_STREAMING)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (cap->capabilities & V4L2_CAP_VIDEO_M2M)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int v4l2_mplane_video(struct v4l2_capability *cap)
|
|
|
|
{
|
|
|
|
if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE) &&
|
|
|
|
cap->capabilities & V4L2_CAP_STREAMING)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (cap->capabilities & V4L2_CAP_VIDEO_M2M_MPLANE)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-13 06:11:30 +02:00
|
|
|
static int v4l2_prepare_contexts(V4L2m2mContext *s, int probe)
|
2017-09-21 03:55:40 +02:00
|
|
|
{
|
|
|
|
struct v4l2_capability cap;
|
2020-01-13 06:11:29 +02:00
|
|
|
void *log_ctx = s->avctx;
|
2017-09-21 03:55:40 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
s->capture.done = s->output.done = 0;
|
|
|
|
s->capture.name = "capture";
|
2019-08-22 21:20:40 +02:00
|
|
|
s->output.name = "output";
|
2017-09-21 03:55:40 +02:00
|
|
|
atomic_init(&s->refcount, 0);
|
|
|
|
sem_init(&s->refsync, 0, 0);
|
|
|
|
|
|
|
|
memset(&cap, 0, sizeof(cap));
|
|
|
|
ret = ioctl(s->fd, VIDIOC_QUERYCAP, &cap);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, probe ? AV_LOG_DEBUG : AV_LOG_INFO,
|
2019-08-22 20:33:24 +02:00
|
|
|
"driver '%s' on card '%s' in %s mode\n", cap.driver, cap.card,
|
|
|
|
v4l2_mplane_video(&cap) ? "mplane" :
|
|
|
|
v4l2_splane_video(&cap) ? "splane" : "unknown");
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
if (v4l2_mplane_video(&cap)) {
|
|
|
|
s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
|
|
|
|
s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v4l2_splane_video(&cap)) {
|
|
|
|
s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2020-01-13 06:11:30 +02:00
|
|
|
static int v4l2_probe_driver(V4L2m2mContext *s)
|
2017-09-21 03:55:40 +02:00
|
|
|
{
|
2020-01-13 06:11:29 +02:00
|
|
|
void *log_ctx = s->avctx;
|
2017-09-21 03:55:40 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
|
|
|
|
if (s->fd < 0)
|
|
|
|
return AVERROR(errno);
|
|
|
|
|
2019-08-22 20:33:24 +02:00
|
|
|
ret = v4l2_prepare_contexts(s, 1);
|
2017-09-21 03:55:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
|
2019-08-28 02:21:36 +02:00
|
|
|
ret = ff_v4l2_context_get_format(&s->output, 1);
|
2017-09-21 03:55:40 +02:00
|
|
|
if (ret) {
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_DEBUG, "v4l2 output format not supported\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:21:36 +02:00
|
|
|
ret = ff_v4l2_context_get_format(&s->capture, 1);
|
2017-09-21 03:55:40 +02:00
|
|
|
if (ret) {
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (close(s->fd) < 0) {
|
|
|
|
ret = AVERROR(errno);
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "failure closing %s (%s)\n", s->devname, av_err2str(AVERROR(errno)));
|
2017-09-21 03:55:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->fd = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-13 06:11:30 +02:00
|
|
|
static int v4l2_configure_contexts(V4L2m2mContext *s)
|
2017-09-21 03:55:40 +02:00
|
|
|
{
|
2020-01-13 06:11:29 +02:00
|
|
|
void *log_ctx = s->avctx;
|
2017-09-21 03:55:40 +02:00
|
|
|
int ret;
|
2019-08-22 21:06:24 +02:00
|
|
|
struct v4l2_format ofmt, cfmt;
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
|
|
|
|
if (s->fd < 0)
|
|
|
|
return AVERROR(errno);
|
|
|
|
|
2019-08-22 20:33:24 +02:00
|
|
|
ret = v4l2_prepare_contexts(s, 0);
|
2017-09-21 03:55:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2019-08-22 21:06:24 +02:00
|
|
|
ofmt = s->output.format;
|
|
|
|
cfmt = s->capture.format;
|
|
|
|
av_log(log_ctx, AV_LOG_INFO, "requesting formats: output=%s capture=%s\n",
|
|
|
|
av_fourcc2str(V4L2_TYPE_IS_MULTIPLANAR(ofmt.type) ?
|
|
|
|
ofmt.fmt.pix_mp.pixelformat :
|
|
|
|
ofmt.fmt.pix.pixelformat),
|
|
|
|
av_fourcc2str(V4L2_TYPE_IS_MULTIPLANAR(cfmt.type) ?
|
|
|
|
cfmt.fmt.pix_mp.pixelformat :
|
|
|
|
cfmt.fmt.pix.pixelformat));
|
|
|
|
|
2017-09-21 03:55:40 +02:00
|
|
|
ret = ff_v4l2_context_set_format(&s->output);
|
|
|
|
if (ret) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_v4l2_context_set_format(&s->capture);
|
|
|
|
if (ret) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_v4l2_context_init(&s->output);
|
|
|
|
if (ret) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decoder's buffers need to be updated at a later stage */
|
2020-01-13 06:11:27 +02:00
|
|
|
if (s->avctx && !av_codec_is_decoder(s->avctx->codec)) {
|
2017-09-21 03:55:40 +02:00
|
|
|
ret = ff_v4l2_context_init(&s->capture);
|
|
|
|
if (ret) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (close(s->fd) < 0) {
|
|
|
|
ret = AVERROR(errno);
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "error closing %s (%s)\n",
|
|
|
|
s->devname, av_err2str(AVERROR(errno)));
|
|
|
|
}
|
|
|
|
s->fd = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* V4L2 M2M Interface
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2020-01-13 06:11:30 +02:00
|
|
|
int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *s)
|
2017-09-21 03:55:40 +02:00
|
|
|
{
|
2020-01-13 06:11:29 +02:00
|
|
|
void *log_ctx = s->avctx;
|
2017-09-21 03:55:40 +02:00
|
|
|
int ret;
|
|
|
|
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_DEBUG, "reinit context\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
/* 1. streamoff */
|
|
|
|
ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
|
|
|
|
if (ret)
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "capture VIDIOC_STREAMOFF\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
/* 2. unmap the capture buffers (v4l2 and ffmpeg):
|
|
|
|
* we must wait for all references to be released before being allowed
|
|
|
|
* to queue new buffers.
|
|
|
|
*/
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_DEBUG, "waiting for user to release AVBufferRefs\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
if (atomic_load(&s->refcount))
|
|
|
|
while(sem_wait(&s->refsync) == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
ff_v4l2_context_release(&s->capture);
|
|
|
|
|
|
|
|
/* 3. get the new capture format */
|
2019-08-28 02:21:36 +02:00
|
|
|
ret = ff_v4l2_context_get_format(&s->capture, 0);
|
2017-09-21 03:55:40 +02:00
|
|
|
if (ret) {
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "query the new capture format\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. set the capture format */
|
|
|
|
ret = ff_v4l2_context_set_format(&s->capture);
|
|
|
|
if (ret) {
|
2019-08-23 21:56:33 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "setting capture format\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5. complete reinit */
|
|
|
|
s->draining = 0;
|
|
|
|
s->reinit = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-10 00:56:41 +02:00
|
|
|
static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context)
|
|
|
|
{
|
|
|
|
V4L2m2mContext *s = (V4L2m2mContext*)context;
|
|
|
|
|
|
|
|
ff_v4l2_context_release(&s->capture);
|
|
|
|
sem_destroy(&s->refsync);
|
|
|
|
|
2022-06-28 13:15:12 +02:00
|
|
|
if (s->fd >= 0)
|
|
|
|
close(s->fd);
|
2020-08-23 19:33:37 +02:00
|
|
|
av_frame_unref(s->frame);
|
2020-06-09 23:31:32 +02:00
|
|
|
av_frame_free(&s->frame);
|
2020-08-02 20:31:22 +02:00
|
|
|
av_packet_unref(&s->buf_pkt);
|
2018-01-10 00:56:41 +02:00
|
|
|
|
|
|
|
av_free(s);
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:56:33 +02:00
|
|
|
int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv)
|
2017-09-21 03:55:40 +02:00
|
|
|
{
|
2020-01-13 06:11:30 +02:00
|
|
|
V4L2m2mContext *s = priv->context;
|
2017-09-21 03:55:40 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-08-02 20:31:22 +02:00
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s->fd >= 0) {
|
2020-08-02 20:36:04 +02:00
|
|
|
ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF);
|
|
|
|
if (ret)
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->output.name);
|
2017-09-21 03:55:40 +02:00
|
|
|
|
2020-08-02 20:36:04 +02:00
|
|
|
ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
|
|
|
|
if (ret)
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->capture.name);
|
2020-08-02 20:31:22 +02:00
|
|
|
}
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
ff_v4l2_context_release(&s->output);
|
|
|
|
|
2018-01-10 00:56:41 +02:00
|
|
|
s->self_ref = NULL;
|
|
|
|
av_buffer_unref(&priv->context_ref);
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:56:33 +02:00
|
|
|
int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv)
|
2017-09-21 03:55:40 +02:00
|
|
|
{
|
|
|
|
int ret = AVERROR(EINVAL);
|
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dirp;
|
|
|
|
|
2019-08-23 21:56:33 +02:00
|
|
|
V4L2m2mContext *s = priv->context;
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
dirp = opendir("/dev");
|
|
|
|
if (!dirp)
|
|
|
|
return AVERROR(errno);
|
|
|
|
|
|
|
|
for (entry = readdir(dirp); entry; entry = readdir(dirp)) {
|
|
|
|
|
|
|
|
if (strncmp(entry->d_name, "video", 5))
|
|
|
|
continue;
|
|
|
|
|
2020-03-03 06:20:05 +02:00
|
|
|
snprintf(s->devname, sizeof(s->devname), "/dev/%s", entry->d_name);
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "probing device %s\n", s->devname);
|
2017-09-21 03:55:40 +02:00
|
|
|
ret = v4l2_probe_driver(s);
|
|
|
|
if (!ret)
|
2019-10-27 06:19:47 +02:00
|
|
|
break;
|
2017-09-21 03:55:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
|
|
|
|
if (ret) {
|
2020-01-13 06:11:29 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Could not find a valid device\n");
|
2017-09-21 03:55:40 +02:00
|
|
|
memset(s->devname, 0, sizeof(s->devname));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-03 06:20:05 +02:00
|
|
|
av_log(s->avctx, AV_LOG_INFO, "Using device %s\n", s->devname);
|
2017-09-21 03:55:40 +02:00
|
|
|
|
|
|
|
return v4l2_configure_contexts(s);
|
|
|
|
}
|
2018-01-10 00:56:41 +02:00
|
|
|
|
2019-08-23 21:56:33 +02:00
|
|
|
int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s)
|
2018-01-10 00:56:41 +02:00
|
|
|
{
|
|
|
|
*s = av_mallocz(sizeof(V4L2m2mContext));
|
|
|
|
if (!*s)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
priv->context_ref = av_buffer_create((uint8_t *) *s, sizeof(V4L2m2mContext),
|
|
|
|
&v4l2_m2m_destroy_context, NULL, 0);
|
|
|
|
if (!priv->context_ref) {
|
2018-01-22 00:59:29 +02:00
|
|
|
av_freep(s);
|
2018-01-10 00:56:41 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assign the context */
|
|
|
|
priv->context = *s;
|
2019-08-23 21:56:33 +02:00
|
|
|
(*s)->priv = priv;
|
2018-01-10 00:56:41 +02:00
|
|
|
|
|
|
|
/* populate it */
|
|
|
|
priv->context->capture.num_buffers = priv->num_capture_buffers;
|
|
|
|
priv->context->output.num_buffers = priv->num_output_buffers;
|
|
|
|
priv->context->self_ref = priv->context_ref;
|
2019-09-02 08:34:35 +02:00
|
|
|
priv->context->fd = -1;
|
2018-01-10 00:56:41 +02:00
|
|
|
|
2020-06-09 23:31:32 +02:00
|
|
|
priv->context->frame = av_frame_alloc();
|
|
|
|
if (!priv->context->frame) {
|
|
|
|
av_buffer_unref(&priv->context_ref);
|
|
|
|
*s = NULL; /* freed when unreferencing context_ref */
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2018-01-10 00:56:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|