]> git.sesse.net Git - ffmpeg/blob - libavcodec/v4l2_m2m_enc.c
avcodec/v4l2_m2m_enc: Check encoder pix_fmt matches pix_fmt on device
[ffmpeg] / libavcodec / v4l2_m2m_enc.c
1 /*
2  * V4L2 mem2mem encoders
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 <search.h>
27 #include "libavcodec/avcodec.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/pixfmt.h"
30 #include "libavutil/opt.h"
31 #include "v4l2_context.h"
32 #include "v4l2_m2m.h"
33 #include "v4l2_fmt.h"
34
35 #define MPEG_CID(x) V4L2_CID_MPEG_VIDEO_##x
36 #define MPEG_VIDEO(x) V4L2_MPEG_VIDEO_##x
37
38 static inline void v4l2_set_timeperframe(V4L2m2mContext *s, unsigned int num, unsigned int den)
39 {
40     struct v4l2_streamparm parm = { 0 };
41
42     parm.type = V4L2_TYPE_IS_MULTIPLANAR(s->output.type) ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT;
43     parm.parm.output.timeperframe.denominator = den;
44     parm.parm.output.timeperframe.numerator = num;
45
46     if (ioctl(s->fd, VIDIOC_S_PARM, &parm) < 0)
47         av_log(s->avctx, AV_LOG_WARNING, "Failed to set timeperframe");
48 }
49
50 static inline void v4l2_set_ext_ctrl(V4L2m2mContext *s, unsigned int id, signed int value, const char *name)
51 {
52     struct v4l2_ext_controls ctrls = { { 0 } };
53     struct v4l2_ext_control ctrl = { 0 };
54
55     /* set ctrls */
56     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
57     ctrls.controls = &ctrl;
58     ctrls.count = 1;
59
60     /* set ctrl*/
61     ctrl.value = value;
62     ctrl.id = id;
63
64     if (ioctl(s->fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
65         av_log(s->avctx, AV_LOG_WARNING, "Failed to set %s: %s\n", name, strerror(errno));
66     else
67         av_log(s->avctx, AV_LOG_DEBUG, "Encoder: %s = %d\n", name, value);
68 }
69
70 static inline int v4l2_get_ext_ctrl(V4L2m2mContext *s, unsigned int id, signed int *value, const char *name)
71 {
72     struct v4l2_ext_controls ctrls = { { 0 } };
73     struct v4l2_ext_control ctrl = { 0 };
74     int ret;
75
76     /* set ctrls */
77     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
78     ctrls.controls = &ctrl;
79     ctrls.count = 1;
80
81     /* set ctrl*/
82     ctrl.id = id ;
83
84     ret = ioctl(s->fd, VIDIOC_G_EXT_CTRLS, &ctrls);
85     if (ret < 0) {
86         av_log(s->avctx, AV_LOG_WARNING, "Failed to set %s\n", name);
87         return ret;
88     }
89
90     *value = ctrl.value;
91
92     return 0;
93 }
94
95 static inline unsigned int v4l2_h264_profile_from_ff(int p)
96 {
97     static const struct h264_profile  {
98         unsigned int ffmpeg_val;
99         unsigned int v4l2_val;
100     } profile[] = {
101         { FF_PROFILE_H264_CONSTRAINED_BASELINE, MPEG_VIDEO(H264_PROFILE_CONSTRAINED_BASELINE) },
102         { FF_PROFILE_H264_HIGH_444_PREDICTIVE, MPEG_VIDEO(H264_PROFILE_HIGH_444_PREDICTIVE) },
103         { FF_PROFILE_H264_HIGH_422_INTRA, MPEG_VIDEO(H264_PROFILE_HIGH_422_INTRA) },
104         { FF_PROFILE_H264_HIGH_444_INTRA, MPEG_VIDEO(H264_PROFILE_HIGH_444_INTRA) },
105         { FF_PROFILE_H264_HIGH_10_INTRA, MPEG_VIDEO(H264_PROFILE_HIGH_10_INTRA) },
106         { FF_PROFILE_H264_HIGH_422, MPEG_VIDEO(H264_PROFILE_HIGH_422) },
107         { FF_PROFILE_H264_BASELINE, MPEG_VIDEO(H264_PROFILE_BASELINE) },
108         { FF_PROFILE_H264_EXTENDED, MPEG_VIDEO(H264_PROFILE_EXTENDED) },
109         { FF_PROFILE_H264_HIGH_10, MPEG_VIDEO(H264_PROFILE_HIGH_10) },
110         { FF_PROFILE_H264_MAIN, MPEG_VIDEO(H264_PROFILE_MAIN) },
111         { FF_PROFILE_H264_HIGH, MPEG_VIDEO(H264_PROFILE_HIGH) },
112     };
113     int i;
114
115     for (i = 0; i < FF_ARRAY_ELEMS(profile); i++) {
116         if (profile[i].ffmpeg_val == p)
117             return profile[i].v4l2_val;
118     }
119     return AVERROR(ENOENT);
120 }
121
122 static inline int v4l2_mpeg4_profile_from_ff(int p)
123 {
124     static const struct mpeg4_profile {
125         unsigned int ffmpeg_val;
126         unsigned int v4l2_val;
127     } profile[] = {
128         { FF_PROFILE_MPEG4_ADVANCED_CODING, MPEG_VIDEO(MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY) },
129         { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, MPEG_VIDEO(MPEG4_PROFILE_ADVANCED_SIMPLE) },
130         { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, MPEG_VIDEO(MPEG4_PROFILE_SIMPLE_SCALABLE) },
131         { FF_PROFILE_MPEG4_SIMPLE, MPEG_VIDEO(MPEG4_PROFILE_SIMPLE) },
132         { FF_PROFILE_MPEG4_CORE, MPEG_VIDEO(MPEG4_PROFILE_CORE) },
133     };
134     int i;
135
136     for (i = 0; i < FF_ARRAY_ELEMS(profile); i++) {
137         if (profile[i].ffmpeg_val == p)
138             return profile[i].v4l2_val;
139     }
140     return AVERROR(ENOENT);
141 }
142
143 static int v4l2_check_b_frame_support(V4L2m2mContext *s)
144 {
145     if (s->avctx->max_b_frames)
146         av_log(s->avctx, AV_LOG_WARNING, "Encoder does not support b-frames yet\n");
147
148     v4l2_set_ext_ctrl(s, MPEG_CID(B_FRAMES), 0, "number of B-frames");
149     v4l2_get_ext_ctrl(s, MPEG_CID(B_FRAMES), &s->avctx->max_b_frames, "number of B-frames");
150     if (s->avctx->max_b_frames == 0)
151         return 0;
152
153     avpriv_report_missing_feature(s->avctx, "DTS/PTS calculation for V4L2 encoding");
154
155     return AVERROR_PATCHWELCOME;
156 }
157
158 static int v4l2_prepare_encoder(V4L2m2mContext *s)
159 {
160     AVCodecContext *avctx = s->avctx;
161     int qmin_cid, qmax_cid, qmin, qmax;
162     int ret, val;
163
164     /**
165      * requirements
166      */
167     ret = v4l2_check_b_frame_support(s);
168     if (ret)
169         return ret;
170
171     /**
172      * settingss
173      */
174     if (avctx->framerate.num || avctx->framerate.den)
175         v4l2_set_timeperframe(s, avctx->framerate.num, avctx->framerate.den);
176
177     /* set ext ctrls */
178     v4l2_set_ext_ctrl(s, MPEG_CID(HEADER_MODE), MPEG_VIDEO(HEADER_MODE_SEPARATE), "header mode");
179     v4l2_set_ext_ctrl(s, MPEG_CID(BITRATE) , avctx->bit_rate, "bit rate");
180     v4l2_set_ext_ctrl(s, MPEG_CID(GOP_SIZE), avctx->gop_size,"gop size");
181
182     av_log(avctx, AV_LOG_DEBUG,
183         "Encoder Context: id (%d), profile (%d), frame rate(%d/%d), number b-frames (%d), "
184         "gop size (%d), bit rate (%"PRId64"), qmin (%d), qmax (%d)\n",
185         avctx->codec_id, avctx->profile, avctx->framerate.num, avctx->framerate.den,
186         avctx->max_b_frames, avctx->gop_size, avctx->bit_rate, avctx->qmin, avctx->qmax);
187
188     switch (avctx->codec_id) {
189     case AV_CODEC_ID_H264:
190         val = v4l2_h264_profile_from_ff(avctx->profile);
191         if (val < 0)
192             av_log(avctx, AV_LOG_WARNING, "h264 profile not found\n");
193         else
194             v4l2_set_ext_ctrl(s, MPEG_CID(H264_PROFILE), val, "h264 profile");
195         qmin_cid = MPEG_CID(H264_MIN_QP);
196         qmax_cid = MPEG_CID(H264_MAX_QP);
197         qmin = 0;
198         qmax = 51;
199         break;
200     case AV_CODEC_ID_MPEG4:
201         val = v4l2_mpeg4_profile_from_ff(avctx->profile);
202         if (val < 0)
203             av_log(avctx, AV_LOG_WARNING, "mpeg4 profile not found\n");
204         else
205             v4l2_set_ext_ctrl(s, MPEG_CID(MPEG4_PROFILE), val, "mpeg4 profile");
206         qmin_cid = MPEG_CID(MPEG4_MIN_QP);
207         qmax_cid = MPEG_CID(MPEG4_MAX_QP);
208         if (avctx->flags & AV_CODEC_FLAG_QPEL)
209             v4l2_set_ext_ctrl(s, MPEG_CID(MPEG4_QPEL), 1, "qpel");
210         qmin = 1;
211         qmax = 31;
212         break;
213     case AV_CODEC_ID_H263:
214         qmin_cid = MPEG_CID(H263_MIN_QP);
215         qmax_cid = MPEG_CID(H263_MAX_QP);
216         qmin = 1;
217         qmax = 31;
218         break;
219     case AV_CODEC_ID_VP8:
220         qmin_cid = MPEG_CID(VPX_MIN_QP);
221         qmax_cid = MPEG_CID(VPX_MAX_QP);
222         qmin = 0;
223         qmax = 127;
224         break;
225     case AV_CODEC_ID_VP9:
226         qmin_cid = MPEG_CID(VPX_MIN_QP);
227         qmax_cid = MPEG_CID(VPX_MAX_QP);
228         qmin = 0;
229         qmax = 255;
230         break;
231     default:
232         return 0;
233     }
234
235     if (qmin != avctx->qmin || qmax != avctx->qmax)
236         av_log(avctx, AV_LOG_WARNING, "Encoder adjusted: qmin (%d), qmax (%d)\n", qmin, qmax);
237
238     v4l2_set_ext_ctrl(s, qmin_cid, qmin, "minimum video quantizer scale");
239     v4l2_set_ext_ctrl(s, qmax_cid, qmax, "maximum video quantizer scale");
240
241     return 0;
242 }
243
244 static int v4l2_send_frame(AVCodecContext *avctx, const AVFrame *frame)
245 {
246     V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
247     V4L2Context *const output = &s->output;
248
249 #ifdef V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
250     if (frame && frame->pict_type == AV_PICTURE_TYPE_I)
251         v4l2_set_ext_ctrl(s, MPEG_CID(FORCE_KEY_FRAME), 0, "force key frame");
252 #endif
253
254     return ff_v4l2_context_enqueue_frame(output, frame);
255 }
256
257 static int v4l2_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
258 {
259     V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
260     V4L2Context *const capture = &s->capture;
261     V4L2Context *const output = &s->output;
262     int ret;
263
264     if (s->draining)
265         goto dequeue;
266
267     if (!output->streamon) {
268         ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
269         if (ret) {
270             av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMON failed on output context\n");
271             return ret;
272         }
273     }
274
275     if (!capture->streamon) {
276         ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
277         if (ret) {
278             av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMON failed on capture context\n");
279             return ret;
280         }
281     }
282
283 dequeue:
284     return ff_v4l2_context_dequeue_packet(capture, avpkt);
285 }
286
287 static av_cold int v4l2_encode_init(AVCodecContext *avctx)
288 {
289     V4L2Context *capture, *output;
290     V4L2m2mContext *s;
291     V4L2m2mPriv *priv = avctx->priv_data;
292     enum AVPixelFormat pix_fmt_output;
293     uint32_t v4l2_fmt_output;
294     int ret;
295
296     ret = ff_v4l2_m2m_create_context(priv, &s);
297     if (ret < 0)
298         return ret;
299
300     capture = &s->capture;
301     output  = &s->output;
302
303     /* common settings output/capture */
304     output->height = capture->height = avctx->height;
305     output->width = capture->width = avctx->width;
306
307     /* output context */
308     output->av_codec_id = AV_CODEC_ID_RAWVIDEO;
309     output->av_pix_fmt = avctx->pix_fmt;
310
311     /* capture context */
312     capture->av_codec_id = avctx->codec_id;
313     capture->av_pix_fmt = AV_PIX_FMT_NONE;
314
315     ret = ff_v4l2_m2m_codec_init(priv);
316     if (ret) {
317         av_log(avctx, AV_LOG_ERROR, "can't configure encoder\n");
318         return ret;
319     }
320     s->avctx = avctx;
321
322     if (V4L2_TYPE_IS_MULTIPLANAR(output->type))
323         v4l2_fmt_output = output->format.fmt.pix_mp.pixelformat;
324     else
325         v4l2_fmt_output = output->format.fmt.pix.pixelformat;
326
327     pix_fmt_output = ff_v4l2_format_v4l2_to_avfmt(v4l2_fmt_output, AV_CODEC_ID_RAWVIDEO);
328     if (pix_fmt_output != avctx->pix_fmt) {
329         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt_output);
330         av_log(avctx, AV_LOG_ERROR, "Encoder requires %s pixel format.\n", desc->name);
331         return AVERROR(EINVAL);
332     }
333
334     return v4l2_prepare_encoder(s);
335 }
336
337 static av_cold int v4l2_encode_close(AVCodecContext *avctx)
338 {
339     return ff_v4l2_m2m_codec_end(avctx->priv_data);
340 }
341
342 #define OFFSET(x) offsetof(V4L2m2mPriv, x)
343 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
344
345 static const AVOption options[] = {
346     V4L_M2M_DEFAULT_OPTS,
347     { "num_capture_buffers", "Number of buffers in the capture context",
348         OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 4 }, 4, INT_MAX, FLAGS },
349     { NULL },
350 };
351
352 #define M2MENC_CLASS(NAME) \
353     static const AVClass v4l2_m2m_ ## NAME ## _enc_class = { \
354         .class_name = #NAME "_v4l2m2m_encoder", \
355         .item_name  = av_default_item_name, \
356         .option     = options, \
357         .version    = LIBAVUTIL_VERSION_INT, \
358     };
359
360 #define M2MENC(NAME, LONGNAME, CODEC) \
361     M2MENC_CLASS(NAME) \
362     AVCodec ff_ ## NAME ## _v4l2m2m_encoder = { \
363         .name           = #NAME "_v4l2m2m" , \
364         .long_name      = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " encoder wrapper"), \
365         .type           = AVMEDIA_TYPE_VIDEO, \
366         .id             = CODEC , \
367         .priv_data_size = sizeof(V4L2m2mPriv), \
368         .priv_class     = &v4l2_m2m_ ## NAME ##_enc_class, \
369         .init           = v4l2_encode_init, \
370         .send_frame     = v4l2_send_frame, \
371         .receive_packet = v4l2_receive_packet, \
372         .close          = v4l2_encode_close, \
373         .capabilities   = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY, \
374         .wrapper_name   = "v4l2m2m", \
375     };
376
377 M2MENC(mpeg4,"MPEG4", AV_CODEC_ID_MPEG4);
378 M2MENC(h263, "H.263", AV_CODEC_ID_H263);
379 M2MENC(h264, "H.264", AV_CODEC_ID_H264);
380 M2MENC(hevc, "HEVC",  AV_CODEC_ID_HEVC);
381 M2MENC(vp8,  "VP8",   AV_CODEC_ID_VP8);