FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
v4l2_m2m_dec.c
Go to the documentation of this file.
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 "libavcodec/decode.h"
31 
32 #include "v4l2_context.h"
33 #include "v4l2_m2m.h"
34 #include "v4l2_fmt.h"
35 
36 static int v4l2_try_start(AVCodecContext *avctx)
37 {
38  V4L2m2mContext *s = avctx->priv_data;
39  V4L2Context *const capture = &s->capture;
40  V4L2Context *const output = &s->output;
41  struct v4l2_selection selection;
42  int ret;
43 
44  /* 1. start the output process */
45  if (!output->streamon) {
46  ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
47  if (ret < 0) {
48  av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON on output context\n");
49  return ret;
50  }
51  }
52 
53  if (capture->streamon)
54  return 0;
55 
56  /* 2. get the capture format */
57  capture->format.type = capture->type;
58  ret = ioctl(s->fd, VIDIOC_G_FMT, &capture->format);
59  if (ret) {
60  av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_FMT ioctl\n");
61  return ret;
62  }
63 
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;
67 
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);
73  if (!ret) {
74  ret = ioctl(s->fd, VIDIOC_G_SELECTION, &selection);
75  if (ret) {
76  av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_SELECTION ioctl\n");
77  } else {
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;
82  }
83  }
84 
85  /* 4. init the capture context now that we have the capture format */
86  if (!capture->buffers) {
87  ret = ff_v4l2_context_init(capture);
88  if (ret) {
89  av_log(avctx, AV_LOG_DEBUG, "can't request output buffers\n");
90  return ret;
91  }
92  }
93 
94  /* 5. start the capture process */
95  ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
96  if (ret) {
97  av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON, on capture context\n");
98  return ret;
99  }
100 
101  return 0;
102 }
103 
105 {
106  struct v4l2_event_subscription sub;
107  V4L2Context *output = &s->output;
108  int ret;
109 
110  /**
111  * requirements
112  */
113  memset(&sub, 0, sizeof(sub));
114  sub.type = V4L2_EVENT_SOURCE_CHANGE;
115  ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
116  if ( ret < 0) {
117  if (output->height == 0 || output->width == 0) {
119  "the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n"
120  "you must provide codec_height and codec_width on input\n");
121  return ret;
122  }
123  }
124 
125  return 0;
126 }
127 
129 {
130  V4L2m2mContext *s = avctx->priv_data;
131  V4L2Context *const capture = &s->capture;
132  V4L2Context *const output = &s->output;
133  AVPacket avpkt = {0};
134  int ret;
135 
136  ret = ff_decode_get_packet(avctx, &avpkt);
137  if (ret < 0 && ret != AVERROR_EOF)
138  return ret;
139 
140  if (s->draining)
141  goto dequeue;
142 
143  ret = ff_v4l2_context_enqueue_packet(output, &avpkt);
144  if (ret < 0) {
145  if (ret != AVERROR(ENOMEM))
146  return ret;
147  /* no input buffers available, continue dequeing */
148  }
149 
150  if (avpkt.size) {
151  ret = v4l2_try_start(avctx);
152  if (ret)
153  return 0;
154  }
155 
156 dequeue:
157  return ff_v4l2_context_dequeue_frame(capture, frame);
158 }
159 
161 {
162  V4L2m2mContext *s = avctx->priv_data;
163  V4L2Context *capture = &s->capture;
164  V4L2Context *output = &s->output;
165  int ret;
166 
167  /* if these dimensions are invalid (ie, 0 or too small) an event will be raised
168  * by the v4l2 driver; this event will trigger a full pipeline reconfig and
169  * the proper values will be retrieved from the kernel driver.
170  */
171  output->height = capture->height = avctx->coded_height;
172  output->width = capture->width = avctx->coded_width;
173 
174  output->av_codec_id = avctx->codec_id;
175  output->av_pix_fmt = AV_PIX_FMT_NONE;
176 
178  capture->av_pix_fmt = avctx->pix_fmt;
179 
180  ret = ff_v4l2_m2m_codec_init(avctx);
181  if (ret) {
182  av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n");
183  return ret;
184  }
185 
186  return v4l2_prepare_decoder(s);
187 }
188 
189 #define OFFSET(x) offsetof(V4L2m2mContext, x)
190 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
191 
192 static const AVOption options[] = {
194  { "num_capture_buffers", "Number of buffers in the capture context",
195  OFFSET(capture.num_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
196  { NULL},
197 };
198 
199 #define M2MDEC(NAME, LONGNAME, CODEC, bsf_name) \
200 static const AVClass v4l2_m2m_ ## NAME ## _dec_class = {\
201  .class_name = #NAME "_v4l2_m2m_decoder",\
202  .item_name = av_default_item_name,\
203  .option = options,\
204  .version = LIBAVUTIL_VERSION_INT,\
205 };\
206 \
207 AVCodec ff_ ## NAME ## _v4l2m2m_decoder = { \
208  .name = #NAME "_v4l2m2m" ,\
209  .long_name = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " decoder wrapper"),\
210  .type = AVMEDIA_TYPE_VIDEO,\
211  .id = CODEC ,\
212  .priv_data_size = sizeof(V4L2m2mContext),\
213  .priv_class = &v4l2_m2m_ ## NAME ## _dec_class,\
214  .init = v4l2_decode_init,\
215  .receive_frame = v4l2_receive_frame,\
216  .close = ff_v4l2_m2m_codec_end,\
217  .bsfs = bsf_name, \
218 };
219 
220 M2MDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb");
221 M2MDEC(hevc, "HEVC", AV_CODEC_ID_HEVC, "hevc_mp4toannexb");
222 M2MDEC(mpeg1, "MPEG1", AV_CODEC_ID_MPEG1VIDEO, NULL);
223 M2MDEC(mpeg2, "MPEG2", AV_CODEC_ID_MPEG2VIDEO, NULL);
224 M2MDEC(mpeg4, "MPEG4", AV_CODEC_ID_MPEG4, NULL);
225 M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL);
226 M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL);
227 M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL);
228 M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL);
enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
Definition: v4l2_fmt.c:132
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static const AVOption options[]
Definition: v4l2_m2m_dec.c:192
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
AVCodecContext * avctx
Definition: v4l2_m2m.h:57
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1963
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int ff_v4l2_context_init(V4L2Context *ctx)
Initializes a V4L2Context.
Definition: v4l2_context.c:642
int size
Definition: avcodec.h:1680
#define FLAGS
Definition: v4l2_m2m_dec.c:190
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
int ff_v4l2_context_dequeue_frame(V4L2Context *ctx, AVFrame *frame)
Dequeues a buffer from a V4L2Context to an AVFrame.
Definition: v4l2_context.c:551
int width
Width and height of the frames it produces (in case of a capture context, e.g.
Definition: v4l2_context.h:71
int ff_v4l2_m2m_codec_init(AVCodecContext *avctx)
Probes the video nodes looking for the required codec capabilities.
Definition: v4l2_m2m.c:344
enum AVCodecID av_codec_id
AVCodecID corresponding to this buffer context.
Definition: v4l2_context.h:59
#define av_cold
Definition: attributes.h:82
AVOptions.
#define OFFSET(x)
Definition: v4l2_m2m_dec.c:189
static int v4l2_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: v4l2_m2m_dec.c:128
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition: decode.c:293
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int ff_v4l2_context_set_status(V4L2Context *ctx, int cmd)
Sets the status of a V4L2Context.
Definition: v4l2_context.c:487
enum AVPixelFormat av_pix_fmt
AVPixelFormat corresponding to this buffer context.
Definition: v4l2_context.h:53
V4L2Buffer * buffers
Indexed array of V4L2Buffers.
Definition: v4l2_context.h:76
int streamon
Whether the stream has been started (VIDIOC_STREAMON has been sent).
Definition: v4l2_context.h:86
int ff_v4l2_context_enqueue_packet(V4L2Context *ctx, const AVPacket *pkt)
Enqueues a buffer to a V4L2Context from an AVPacket.
Definition: v4l2_context.c:526
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:219
struct v4l2_format format
Format returned by the driver after initializing the buffer context.
Definition: v4l2_context.h:65
static int v4l2_try_start(AVCodecContext *avctx)
Definition: v4l2_m2m_dec.c:36
V4L2Context capture
Definition: v4l2_m2m.h:50
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1778
main external API structure.
Definition: avcodec.h:1761
int coded_height
Definition: avcodec.h:1963
#define M2MDEC(NAME, LONGNAME, CODEC, bsf_name)
Definition: v4l2_m2m_dec.c:199
static int v4l2_prepare_decoder(V4L2m2mContext *s)
Definition: v4l2_m2m_dec.c:104
static av_cold int v4l2_decode_init(AVCodecContext *avctx)
Definition: v4l2_m2m_dec.c:160
V4L2Context output
Definition: v4l2_m2m.h:51
void * priv_data
Definition: avcodec.h:1803
pixel format definitions
#define V4L_M2M_DEFAULT_OPTS
Definition: v4l2_m2m.h:39
This structure stores compressed data.
Definition: avcodec.h:1656
enum v4l2_buf_type type
Type of this buffer context.
Definition: v4l2_context.h:47