2 * V4L2 mem2mem decoders
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>
26 #include "libavutil/pixfmt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/opt.h"
29 #include "libavcodec/avcodec.h"
30 #include "libavcodec/decode.h"
32 #include "v4l2_context.h"
36 static int v4l2_try_start(AVCodecContext *avctx)
38 V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
39 V4L2Context *const capture = &s->capture;
40 V4L2Context *const output = &s->output;
41 struct v4l2_selection selection;
44 /* 1. start the output process */
45 if (!output->streamon) {
46 ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
48 av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON on output context\n");
53 if (capture->streamon)
56 /* 2. get the capture format */
57 capture->format.type = capture->type;
58 ret = ioctl(s->fd, VIDIOC_G_FMT, &capture->format);
60 av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_FMT ioctl\n");
64 /* 2.1 update the AVCodecContext */
65 avctx->pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO);
66 capture->av_pix_fmt = avctx->pix_fmt;
68 /* 3. set the crop parameters */
69 selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
70 selection.r.height = avctx->coded_height;
71 selection.r.width = avctx->coded_width;
72 ret = ioctl(s->fd, VIDIOC_S_SELECTION, &selection);
74 ret = ioctl(s->fd, VIDIOC_G_SELECTION, &selection);
76 av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_SELECTION ioctl\n");
78 av_log(avctx, AV_LOG_DEBUG, "crop output %dx%d\n", selection.r.width, selection.r.height);
79 /* update the size of the resulting frame */
80 capture->height = selection.r.height;
81 capture->width = selection.r.width;
85 /* 4. init the capture context now that we have the capture format */
86 if (!capture->buffers) {
87 ret = ff_v4l2_context_init(capture);
89 av_log(avctx, AV_LOG_DEBUG, "can't request output buffers\n");
94 /* 5. start the capture process */
95 ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
97 av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON, on capture context\n");
104 static int v4l2_prepare_decoder(V4L2m2mContext *s)
106 struct v4l2_event_subscription sub;
107 V4L2Context *output = &s->output;
113 memset(&sub, 0, sizeof(sub));
114 sub.type = V4L2_EVENT_SOURCE_CHANGE;
115 ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
117 if (output->height == 0 || output->width == 0) {
118 av_log(s->avctx, AV_LOG_ERROR,
119 "the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n"
120 "you must provide codec_height and codec_width on input\n");
128 static int v4l2_receive_frame(AVCodecContext *avctx, AVFrame *frame)
130 V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
131 V4L2Context *const capture = &s->capture;
132 V4L2Context *const output = &s->output;
133 AVPacket avpkt = {0};
136 ret = ff_decode_get_packet(avctx, &avpkt);
137 if (ret < 0 && ret != AVERROR_EOF)
143 ret = ff_v4l2_context_enqueue_packet(output, &avpkt);
145 if (ret != AVERROR(ENOMEM))
147 /* no input buffers available, continue dequeing */
151 ret = v4l2_try_start(avctx);
157 return ff_v4l2_context_dequeue_frame(capture, frame);
160 static av_cold int v4l2_decode_init(AVCodecContext *avctx)
162 V4L2Context *capture, *output;
166 ret = ff_v4l2_m2m_create_context(avctx, &s);
170 capture = &s->capture;
173 /* if these dimensions are invalid (ie, 0 or too small) an event will be raised
174 * by the v4l2 driver; this event will trigger a full pipeline reconfig and
175 * the proper values will be retrieved from the kernel driver.
177 output->height = capture->height = avctx->coded_height;
178 output->width = capture->width = avctx->coded_width;
180 output->av_codec_id = avctx->codec_id;
181 output->av_pix_fmt = AV_PIX_FMT_NONE;
183 capture->av_codec_id = AV_CODEC_ID_RAWVIDEO;
184 capture->av_pix_fmt = avctx->pix_fmt;
186 ret = ff_v4l2_m2m_codec_init(avctx);
188 av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n");
192 return v4l2_prepare_decoder(s);
195 #define OFFSET(x) offsetof(V4L2m2mPriv, x)
196 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
198 static const AVOption options[] = {
199 V4L_M2M_DEFAULT_OPTS,
200 { "num_capture_buffers", "Number of buffers in the capture context",
201 OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
205 #define M2MDEC(NAME, LONGNAME, CODEC, bsf_name) \
206 static const AVClass v4l2_m2m_ ## NAME ## _dec_class = {\
207 .class_name = #NAME "_v4l2_m2m_decoder",\
208 .item_name = av_default_item_name,\
210 .version = LIBAVUTIL_VERSION_INT,\
213 AVCodec ff_ ## NAME ## _v4l2m2m_decoder = { \
214 .name = #NAME "_v4l2m2m" ,\
215 .long_name = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " decoder wrapper"),\
216 .type = AVMEDIA_TYPE_VIDEO,\
218 .priv_data_size = sizeof(V4L2m2mPriv),\
219 .priv_class = &v4l2_m2m_ ## NAME ## _dec_class,\
220 .init = v4l2_decode_init,\
221 .receive_frame = v4l2_receive_frame,\
222 .close = ff_v4l2_m2m_codec_end,\
224 .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY, \
225 .wrapper_name = "v4l2m2m", \
228 M2MDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb");
229 M2MDEC(hevc, "HEVC", AV_CODEC_ID_HEVC, "hevc_mp4toannexb");
230 M2MDEC(mpeg1, "MPEG1", AV_CODEC_ID_MPEG1VIDEO, NULL);
231 M2MDEC(mpeg2, "MPEG2", AV_CODEC_ID_MPEG2VIDEO, NULL);
232 M2MDEC(mpeg4, "MPEG4", AV_CODEC_ID_MPEG4, NULL);
233 M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL);
234 M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL);
235 M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL);
236 M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL);