1 /*
2 * V4L2 mem2mem decoders
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>
32
36
38 {
42 struct v4l2_selection selection = { 0 };
44
45 /* 1. start the output process */
51 }
52 }
53
55 return 0;
56
57 /* 2. get the capture format */
59 ret = ioctl(
s->fd, VIDIOC_G_FMT, &capture->
format);
63 }
64
65 /* 2.1 update the AVCodecContext */
68
69 /* 3. set the crop parameters */
70 selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
73 ret = ioctl(
s->fd, VIDIOC_S_SELECTION, &selection);
75 ret = ioctl(
s->fd, VIDIOC_G_SELECTION, &selection);
78 } else {
79 av_log(avctx,
AV_LOG_DEBUG,
"crop output %dx%d\n", selection.r.width, selection.r.height);
80 /* update the size of the resulting frame */
81 capture->
height = selection.r.height;
82 capture->
width = selection.r.width;
83 }
84 }
85
86 /* 4. init the capture context now that we have the capture format */
92 }
93 }
94
95 /* 5. start the capture process */
100 }
101
102 return 0;
103 }
104
106 {
107 struct v4l2_event_subscription sub;
110
111 /**
112 * requirements
113 */
114 memset(&sub, 0, sizeof(sub));
115 sub.type = V4L2_EVENT_SOURCE_CHANGE;
116 ret = ioctl(
s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
120 "the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n"
121 "you must provide codec_height and codec_width on input\n");
123 }
124 }
125
126 memset(&sub, 0, sizeof(sub));
127 sub.type = V4L2_EVENT_EOS;
128 ret = ioctl(
s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
131 "the v4l2 driver does not support end of stream VIDIOC_SUBSCRIBE_EVENT\n");
132
133 return 0;
134 }
135
137 {
142
143 if (!
s->buf_pkt.size) {
150 }
151 }
152
154 goto dequeue;
155
159
160 /* if EAGAIN don't unref packet and try to enqueue in the next iteration */
163
167 /* can't recover */
171 }
172 }
173
174 dequeue:
179 }
180
182 {
187
191
192 capture = &
s->capture;
194
195 /* if these dimensions are invalid (ie, 0 or too small) an event will be raised
196 * by the v4l2 driver; this event will trigger a full pipeline reconfig and
197 * the proper values will be retrieved from the kernel driver.
198 */
201
204
207
213 }
214
216 }
217
219 {
221 }
222
223 #define OFFSET(x) offsetof(V4L2m2mPriv, x)
224 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
225
228 { "num_capture_buffers", "Number of buffers in the capture context",
231 };
232
233 #define M2MDEC_CLASS(NAME) \
234 static const AVClass v4l2_m2m_ ## NAME ## _dec_class = { \
235 .class_name = #NAME "_v4l2m2m_decoder", \
236 .item_name = av_default_item_name, \
237 .option = options, \
238 .version = LIBAVUTIL_VERSION_INT, \
239 };
240
241 #define M2MDEC(NAME, LONGNAME, CODEC, bsf_name) \
242 M2MDEC_CLASS(NAME) \
243 const FFCodec ff_ ## NAME ## _v4l2m2m_decoder = { \
244 .p.name = #NAME "_v4l2m2m" , \
245 CODEC_LONG_NAME("V4L2 mem2mem " LONGNAME " decoder wrapper"), \
246 .p.type = AVMEDIA_TYPE_VIDEO, \
247 .p.id = CODEC , \
248 .priv_data_size = sizeof(V4L2m2mPriv), \
249 .p.priv_class = &v4l2_m2m_ ## NAME ## _dec_class, \
250 .init = v4l2_decode_init, \
251 FF_CODEC_RECEIVE_FRAME_CB(v4l2_receive_frame), \
252 .close = v4l2_decode_close, \
253 .bsfs = bsf_name, \
254 .p.capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
255 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
256 FF_CODEC_CAP_INIT_CLEANUP, \
257 .p.wrapper_name = "v4l2m2m", \
258 }
259