]> git.sesse.net Git - ffmpeg/blob - libavcodec/v4l2_context.h
avfilter/vf_identity: remove unnecessary check
[ffmpeg] / libavcodec / v4l2_context.h
1 /*
2  * V4L2 context helper functions.
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 #ifndef AVCODEC_V4L2_CONTEXT_H
25 #define AVCODEC_V4L2_CONTEXT_H
26
27 #include <stdatomic.h>
28 #include <linux/videodev2.h>
29
30 #include "libavcodec/avcodec.h"
31 #include "libavutil/pixfmt.h"
32 #include "libavutil/frame.h"
33 #include "libavutil/buffer.h"
34 #include "v4l2_buffers.h"
35
36 typedef struct V4L2Context {
37     /**
38      * context name.
39      */
40     const char* name;
41
42     /**
43      * Type of this buffer context.
44      * See V4L2_BUF_TYPE_VIDEO_* in videodev2.h
45      * Readonly after init.
46      */
47     enum v4l2_buf_type type;
48
49     /**
50      * AVPixelFormat corresponding to this buffer context.
51      * AV_PIX_FMT_NONE means this is an encoded stream.
52      */
53     enum AVPixelFormat av_pix_fmt;
54
55     /**
56      * AVCodecID corresponding to this buffer context.
57      * AV_CODEC_ID_RAWVIDEO means this is a raw stream and av_pix_fmt must be set to a valid value.
58      */
59     enum AVCodecID av_codec_id;
60
61     /**
62      * Format returned by the driver after initializing the buffer context.
63      * Readonly after init.
64      */
65     struct v4l2_format format;
66
67     /**
68      * Width and height of the frames it produces (in case of a capture context, e.g. when decoding)
69      * or accepts (in case of an output context, e.g. when encoding).
70      */
71     int width, height;
72     AVRational sample_aspect_ratio;
73
74     /**
75      * Indexed array of V4L2Buffers
76      */
77     V4L2Buffer *buffers;
78
79     /**
80      * Readonly after init.
81      */
82     int num_buffers;
83
84     /**
85      * Whether the stream has been started (VIDIOC_STREAMON has been sent).
86      */
87     int streamon;
88
89     /**
90      *  Either no more buffers available or an unrecoverable error was notified
91      *  by the V4L2 kernel driver: once set the context has to be exited.
92      */
93     int done;
94
95 } V4L2Context;
96
97 /**
98  * Initializes a V4L2Context.
99  *
100  * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
101  * @return 0 in case of success, a negative value representing the error otherwise.
102  */
103 int ff_v4l2_context_init(V4L2Context* ctx);
104
105 /**
106  * Sets the V4L2Context format in the v4l2 driver.
107  *
108  * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
109  * @return 0 in case of success, a negative value representing the error otherwise.
110  */
111 int ff_v4l2_context_set_format(V4L2Context* ctx);
112
113 /**
114  * Queries the driver for a valid v4l2 format and copies it to the context.
115  *
116  * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
117  * @param[in] probe Probe only and ignore changes to the format.
118  * @return 0 in case of success, a negative value representing the error otherwise.
119  */
120 int ff_v4l2_context_get_format(V4L2Context* ctx, int probe);
121
122 /**
123  * Releases a V4L2Context.
124  *
125  * @param[in] ctx A pointer to a V4L2Context.
126  *               The caller is reponsible for freeing it.
127  *               It must not be used after calling this function.
128  */
129 void ff_v4l2_context_release(V4L2Context* ctx);
130
131 /**
132  * Sets the status of a V4L2Context.
133  *
134  * @param[in] ctx A pointer to a V4L2Context.
135  * @param[in] cmd The status to set (VIDIOC_STREAMON or VIDIOC_STREAMOFF).
136  *                Warning: If VIDIOC_STREAMOFF is sent to a buffer context that still has some frames buffered,
137  *                those frames will be dropped.
138  * @return 0 in case of success, a negative value representing the error otherwise.
139  */
140 int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd);
141
142 /**
143  * Dequeues a buffer from a V4L2Context to an AVPacket.
144  *
145  * The pkt must be non NULL.
146  * @param[in] ctx The V4L2Context to dequeue from.
147  * @param[inout] pkt The AVPacket to dequeue to.
148  * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error.
149  */
150 int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt);
151
152 /**
153  * Dequeues a buffer from a V4L2Context to an AVFrame.
154  *
155  * The frame must be non NULL.
156  * @param[in] ctx The V4L2Context to dequeue from.
157  * @param[inout] f The AVFrame to dequeue to.
158  * @param[in] timeout The timeout for dequeue (-1 to block, 0 to return immediately, or milliseconds)
159  * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error.
160  */
161 int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* f, int timeout);
162
163 /**
164  * Enqueues a buffer to a V4L2Context from an AVPacket
165  *
166  * The packet must be non NULL.
167  * When the size of the pkt is null, the buffer is not queued but a V4L2_DEC_CMD_STOP command is sent instead to the driver.
168  *
169  * @param[in] ctx The V4L2Context to enqueue to.
170  * @param[in] pkt A pointer to an AVPacket.
171  * @return 0 in case of success, a negative error otherwise.
172  */
173 int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt);
174
175 /**
176  * Enqueues a buffer to a V4L2Context from an AVFrame
177  *
178  * The frame must be non NULL.
179  *
180  * @param[in] ctx The V4L2Context to enqueue to.
181  * @param[in] f A pointer to an AVFrame to enqueue.
182  * @return 0 in case of success, a negative error otherwise.
183  */
184 int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* f);
185
186 #endif // AVCODEC_V4L2_CONTEXT_H