/src/ffmpeg/libavcodec/v4l2_m2m_dec.c
Line | Count | Source (jump to first uncovered line) |
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> |
26 | | #include "libavutil/pixfmt.h" |
27 | | #include "libavutil/pixdesc.h" |
28 | | #include "libavutil/opt.h" |
29 | | #include "libavcodec/avcodec.h" |
30 | | #include "codec_internal.h" |
31 | | #include "libavcodec/decode.h" |
32 | | |
33 | | #include "v4l2_context.h" |
34 | | #include "v4l2_m2m.h" |
35 | | #include "v4l2_fmt.h" |
36 | | |
37 | | static int v4l2_try_start(AVCodecContext *avctx) |
38 | 0 | { |
39 | 0 | V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context; |
40 | 0 | V4L2Context *const capture = &s->capture; |
41 | 0 | V4L2Context *const output = &s->output; |
42 | 0 | struct v4l2_selection selection = { 0 }; |
43 | 0 | int ret; |
44 | | |
45 | | /* 1. start the output process */ |
46 | 0 | if (!output->streamon) { |
47 | 0 | ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON); |
48 | 0 | if (ret < 0) { |
49 | 0 | av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON on output context\n"); |
50 | 0 | return ret; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | 0 | if (capture->streamon) |
55 | 0 | return 0; |
56 | | |
57 | | /* 2. get the capture format */ |
58 | 0 | capture->format.type = capture->type; |
59 | 0 | ret = ioctl(s->fd, VIDIOC_G_FMT, &capture->format); |
60 | 0 | if (ret) { |
61 | 0 | av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_FMT ioctl\n"); |
62 | 0 | return ret; |
63 | 0 | } |
64 | | |
65 | | /* 2.1 update the AVCodecContext */ |
66 | 0 | avctx->pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO); |
67 | 0 | capture->av_pix_fmt = avctx->pix_fmt; |
68 | | |
69 | | /* 3. set the crop parameters */ |
70 | 0 | selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
71 | 0 | selection.r.height = avctx->coded_height; |
72 | 0 | selection.r.width = avctx->coded_width; |
73 | 0 | ret = ioctl(s->fd, VIDIOC_S_SELECTION, &selection); |
74 | 0 | if (!ret) { |
75 | 0 | ret = ioctl(s->fd, VIDIOC_G_SELECTION, &selection); |
76 | 0 | if (ret) { |
77 | 0 | av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_SELECTION ioctl\n"); |
78 | 0 | } else { |
79 | 0 | 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 | 0 | capture->height = selection.r.height; |
82 | 0 | capture->width = selection.r.width; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | /* 4. init the capture context now that we have the capture format */ |
87 | 0 | if (!capture->buffers) { |
88 | 0 | ret = ff_v4l2_context_init(capture); |
89 | 0 | if (ret) { |
90 | 0 | av_log(avctx, AV_LOG_ERROR, "can't request capture buffers\n"); |
91 | 0 | return AVERROR(ENOMEM); |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | /* 5. start the capture process */ |
96 | 0 | ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON); |
97 | 0 | if (ret) { |
98 | 0 | av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON, on capture context\n"); |
99 | 0 | return ret; |
100 | 0 | } |
101 | | |
102 | 0 | return 0; |
103 | 0 | } |
104 | | |
105 | | static int v4l2_prepare_decoder(V4L2m2mContext *s) |
106 | 0 | { |
107 | 0 | struct v4l2_event_subscription sub; |
108 | 0 | V4L2Context *output = &s->output; |
109 | 0 | int ret; |
110 | | |
111 | | /** |
112 | | * requirements |
113 | | */ |
114 | 0 | memset(&sub, 0, sizeof(sub)); |
115 | 0 | sub.type = V4L2_EVENT_SOURCE_CHANGE; |
116 | 0 | ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub); |
117 | 0 | if ( ret < 0) { |
118 | 0 | if (output->height == 0 || output->width == 0) { |
119 | 0 | av_log(s->avctx, AV_LOG_ERROR, |
120 | 0 | "the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n" |
121 | 0 | "you must provide codec_height and codec_width on input\n"); |
122 | 0 | return ret; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | memset(&sub, 0, sizeof(sub)); |
127 | 0 | sub.type = V4L2_EVENT_EOS; |
128 | 0 | ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub); |
129 | 0 | if (ret < 0) |
130 | 0 | av_log(s->avctx, AV_LOG_WARNING, |
131 | 0 | "the v4l2 driver does not support end of stream VIDIOC_SUBSCRIBE_EVENT\n"); |
132 | |
|
133 | 0 | return 0; |
134 | 0 | } |
135 | | |
136 | | static int v4l2_receive_frame(AVCodecContext *avctx, AVFrame *frame) |
137 | 0 | { |
138 | 0 | V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context; |
139 | 0 | V4L2Context *const capture = &s->capture; |
140 | 0 | V4L2Context *const output = &s->output; |
141 | 0 | int ret; |
142 | |
|
143 | 0 | if (!s->buf_pkt.size) { |
144 | 0 | ret = ff_decode_get_packet(avctx, &s->buf_pkt); |
145 | 0 | if (ret < 0) { |
146 | 0 | if (ret == AVERROR(EAGAIN)) |
147 | 0 | return ff_v4l2_context_dequeue_frame(capture, frame, 0); |
148 | 0 | else if (ret != AVERROR_EOF) |
149 | 0 | return ret; |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | 0 | if (s->draining) |
154 | 0 | goto dequeue; |
155 | | |
156 | 0 | ret = ff_v4l2_context_enqueue_packet(output, &s->buf_pkt); |
157 | 0 | if (ret < 0 && ret != AVERROR(EAGAIN)) |
158 | 0 | goto fail; |
159 | | |
160 | | /* if EAGAIN don't unref packet and try to enqueue in the next iteration */ |
161 | 0 | if (ret != AVERROR(EAGAIN)) |
162 | 0 | av_packet_unref(&s->buf_pkt); |
163 | |
|
164 | 0 | if (!s->draining) { |
165 | 0 | ret = v4l2_try_start(avctx); |
166 | 0 | if (ret) { |
167 | | /* cant recover */ |
168 | 0 | if (ret != AVERROR(ENOMEM)) |
169 | 0 | ret = 0; |
170 | 0 | goto fail; |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | 0 | dequeue: |
175 | 0 | return ff_v4l2_context_dequeue_frame(capture, frame, -1); |
176 | 0 | fail: |
177 | 0 | av_packet_unref(&s->buf_pkt); |
178 | 0 | return ret; |
179 | 0 | } |
180 | | |
181 | | static av_cold int v4l2_decode_init(AVCodecContext *avctx) |
182 | 842 | { |
183 | 842 | V4L2Context *capture, *output; |
184 | 842 | V4L2m2mContext *s; |
185 | 842 | V4L2m2mPriv *priv = avctx->priv_data; |
186 | 842 | int ret; |
187 | | |
188 | 842 | ret = ff_v4l2_m2m_create_context(priv, &s); |
189 | 842 | if (ret < 0) |
190 | 0 | return ret; |
191 | | |
192 | 842 | capture = &s->capture; |
193 | 842 | output = &s->output; |
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 | | */ |
199 | 842 | output->height = capture->height = avctx->coded_height; |
200 | 842 | output->width = capture->width = avctx->coded_width; |
201 | | |
202 | 842 | output->av_codec_id = avctx->codec_id; |
203 | 842 | output->av_pix_fmt = AV_PIX_FMT_NONE; |
204 | | |
205 | 842 | capture->av_codec_id = AV_CODEC_ID_RAWVIDEO; |
206 | 842 | capture->av_pix_fmt = avctx->pix_fmt; |
207 | | |
208 | 842 | s->avctx = avctx; |
209 | 842 | ret = ff_v4l2_m2m_codec_init(priv); |
210 | 842 | if (ret) { |
211 | 842 | av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n"); |
212 | 842 | return ret; |
213 | 842 | } |
214 | | |
215 | 0 | return v4l2_prepare_decoder(s); |
216 | 842 | } |
217 | | |
218 | | static av_cold int v4l2_decode_close(AVCodecContext *avctx) |
219 | 842 | { |
220 | 842 | return ff_v4l2_m2m_codec_end(avctx->priv_data); |
221 | 842 | } |
222 | | |
223 | | #define OFFSET(x) offsetof(V4L2m2mPriv, x) |
224 | | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM |
225 | | |
226 | | static const AVOption options[] = { |
227 | | V4L_M2M_DEFAULT_OPTS, |
228 | | { "num_capture_buffers", "Number of buffers in the capture context", |
229 | | OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 2, INT_MAX, FLAGS }, |
230 | | { NULL}, |
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 | | |
260 | | M2MDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb"); |
261 | | M2MDEC(hevc, "HEVC", AV_CODEC_ID_HEVC, "hevc_mp4toannexb"); |
262 | | M2MDEC(mpeg1, "MPEG1", AV_CODEC_ID_MPEG1VIDEO, NULL); |
263 | | M2MDEC(mpeg2, "MPEG2", AV_CODEC_ID_MPEG2VIDEO, NULL); |
264 | | M2MDEC(mpeg4, "MPEG4", AV_CODEC_ID_MPEG4, NULL); |
265 | | M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL); |
266 | | M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL); |
267 | | M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL); |
268 | | M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL); |