v4l2: move vb_queue probing from allocator to v4l2object
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2object.h
1 /* GStreamer
2  *
3  * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *
6  * gstv4l2object.h: base class for V4L2 elements
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifndef __GST_V4L2_OBJECT_H__
25 #define __GST_V4L2_OBJECT_H__
26
27 #include "ext/videodev2.h"
28
29 #include <gst/gst.h>
30 #include <gst/base/gstpushsrc.h>
31
32 #include <gst/video/video.h>
33
34 typedef struct _GstV4l2Object GstV4l2Object;
35 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
36 typedef struct _GstV4l2Xv GstV4l2Xv;
37
38 #include <gstv4l2bufferpool.h>
39
40 /* size of v4l2 buffer pool in streaming case */
41 #define GST_V4L2_MIN_BUFFERS 2
42
43 /* max frame width/height */
44 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
45
46 G_BEGIN_DECLS
47
48 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
49 GType gst_v4l2_io_mode_get_type (void);
50
51 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
52
53 typedef enum {
54   GST_V4L2_IO_AUTO          = 0,
55   GST_V4L2_IO_RW            = 1,
56   GST_V4L2_IO_MMAP          = 2,
57   GST_V4L2_IO_USERPTR       = 3,
58   GST_V4L2_IO_DMABUF        = 4,
59   GST_V4L2_IO_DMABUF_IMPORT = 5
60 } GstV4l2IOMode;
61
62 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
63 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
64 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
65
66 #define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
67 #define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
68 #define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
69 #define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
70 #define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
71
72 /* simple check whether the device is open */
73 #define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)
74
75 /* check whether the device is 'active' */
76 #define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
77 #define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
78 #define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)
79
80 #define GST_V4L2_OBJECT_CAN_REQUEST(obj,path) \
81         (GST_OBJECT_FLAG_IS_SET (obj, GST_V4L2_OBJECT_FLAG_ ## path ## _REQBUFS))
82 #define GST_V4L2_OBJECT_CAN_ALLOCATE(obj,path) \
83         (GST_OBJECT_FLAG_IS_SET (obj, GST_V4L2_OBJECT_FLAG_ ## path ## _CREATE_BUFS))
84
85 enum _GstV4l2ObjectFlags
86 {
87   GST_V4L2_OBJECT_FLAG_MMAP_REQBUFS        = (GST_OBJECT_FLAG_LAST << 0),
88   GST_V4L2_OBJECT_FLAG_MMAP_CREATE_BUFS    = (GST_OBJECT_FLAG_LAST << 1),
89   GST_V4L2_OBJECT_FLAG_USERPTR_REQBUFS     = (GST_OBJECT_FLAG_LAST << 2),
90   GST_V4L2_OBJECT_FLAG_USERPTR_CREATE_BUFS = (GST_OBJECT_FLAG_LAST << 3),
91   GST_V4L2_OBJECT_FLAG_DMABUF_REQBUFS      = (GST_OBJECT_FLAG_LAST << 4),
92   GST_V4L2_OBJECT_FLAG_DMABUF_CREATE_BUFS  = (GST_OBJECT_FLAG_LAST << 5),
93 };
94
95 struct _GstV4l2Object {
96   GstElement * element;
97
98   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
99
100   /* the video device */
101   char *videodev;
102
103   /* the video-device's file descriptor */
104   gint video_fd;
105   GstV4l2IOMode mode;
106
107   gboolean active;
108   gboolean streaming;
109
110   /* the current format */
111   struct v4l2_fmtdesc *fmtdesc;
112   struct v4l2_format format;
113   GstVideoInfo info;
114   GstVideoAlignment align;
115
116   /* Features */
117   gboolean need_video_meta;
118   gboolean has_alpha_component;
119
120   /* only used if the device supports MPLANE
121    * nb planes is meaning of v4l2 planes
122    * the gstreamer equivalent is gst_buffer_n_memory
123    */
124   gint n_v4l2_planes;
125
126   /* We cache the frame duration if known */
127   GstClockTime duration;
128
129   /* if the MPLANE device support both contiguous and non contiguous
130    * it allows to select which one we want. But we prefered_non_contiguous
131    * non contiguous mode.
132    */
133   gboolean prefered_non_contiguous;
134
135   /* This will be set if supported in decide_allocation. It can be used to
136    * calculate the minimum latency. */
137   guint32 min_buffers;
138
139   /* This will be set if supported in propose allocation. */
140   guint32 min_buffers_for_output;
141
142   /* wanted mode */
143   GstV4l2IOMode req_mode;
144
145   /* optional pool */
146   GstBufferPool *pool;
147
148   /* the video device's capabilities */
149   struct v4l2_capability vcap;
150
151   /* the video device's window properties */
152   struct v4l2_window vwin;
153
154   /* some more info about the current input's capabilities */
155   struct v4l2_input vinput;
156
157   /* lists... */
158   GSList *formats;              /* list of available capture formats */
159   GstCaps *probed_caps;
160
161   GList *colors;
162   GList *norms;
163   GList *channels;
164   GData *controls;
165
166   /* properties */
167   v4l2_std_id tv_norm;
168   gchar *channel;
169   gulong frequency;
170   GstStructure *extra_controls;
171   gboolean keep_aspect;
172   GValue *par;
173
174   /* X-overlay */
175   GstV4l2Xv *xv;
176   gulong xwindow_id;
177
178   /* funcs */
179   GstV4l2GetInOutFunction  get_in_out_func;
180   GstV4l2SetInOutFunction  set_in_out_func;
181   GstV4l2UpdateFpsFunction update_fps_func;
182
183   /* Quirks */
184   /* Skips interlacing probes */
185   gboolean never_interlaced;
186   /* Allow to skip reading initial format through G_FMT. Some devices
187    * just fails if you don't call S_FMT first. (ex: M2M decoders) */
188   gboolean no_initial_format;
189 };
190
191 struct _GstV4l2ObjectClassHelper {
192   /* probed devices */
193   GList *devices;
194 };
195
196 GType gst_v4l2_object_get_type (void);
197
198 #define V4L2_STD_OBJECT_PROPS \
199     PROP_DEVICE,              \
200     PROP_DEVICE_NAME,         \
201     PROP_DEVICE_FD,           \
202     PROP_FLAGS,               \
203     PROP_BRIGHTNESS,          \
204     PROP_CONTRAST,            \
205     PROP_SATURATION,          \
206     PROP_HUE,                 \
207     PROP_TV_NORM,             \
208     PROP_IO_MODE,             \
209     PROP_OUTPUT_IO_MODE,      \
210     PROP_CAPTURE_IO_MODE,     \
211     PROP_EXTRA_CONTROLS,      \
212     PROP_PIXEL_ASPECT_RATIO,  \
213     PROP_FORCE_ASPECT_RATIO
214
215 /* create/destroy */
216 GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
217                                            enum v4l2_buf_type  type,
218                                            const char * default_device,
219                                            GstV4l2GetInOutFunction get_in_out_func,
220                                            GstV4l2SetInOutFunction set_in_out_func,
221                                            GstV4l2UpdateFpsFunction update_fps_func);
222
223 void            gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
224
225 /* properties */
226
227 void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
228                                                         const char * default_device);
229
230 void         gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class);
231
232 gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
233                                                         guint prop_id,
234                                                         const GValue * value,
235                                                         GParamSpec * pspec);
236 gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
237                                                         guint prop_id, GValue * value,
238                                                         GParamSpec * pspec);
239 /* open/close */
240 gboolean     gst_v4l2_object_open            (GstV4l2Object *v4l2object);
241 gboolean     gst_v4l2_object_open_shared     (GstV4l2Object *v4l2object, GstV4l2Object *other);
242 gboolean     gst_v4l2_object_close           (GstV4l2Object *v4l2object);
243
244 /* probing */
245 #if 0
246 const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);
247
248 void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
249                                              const GParamSpec * pspec,
250                                              GList ** klass_devices);
251 gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
252                                              const GParamSpec * pspec,
253                                              GList ** klass_devices);
254 GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
255                                              const GParamSpec * pspec,
256                                              GList ** klass_devices);
257 #endif
258
259 GstCaps*      gst_v4l2_object_get_all_caps (void);
260
261 GstCaps*      gst_v4l2_object_get_raw_caps (void);
262
263 GstCaps*      gst_v4l2_object_get_codec_caps (void);
264
265 gboolean      gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps);
266
267 gboolean      gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);
268
269 gboolean      gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
270 gboolean      gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
271
272 gboolean      gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
273
274 GstCaps *     gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object,
275                                            GstCaps * filter);
276
277 gboolean      gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object,
278                                               GstVideoInfo * info);
279
280 gboolean      gst_v4l2_object_set_crop    (GstV4l2Object * obj);
281
282 gboolean      gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object,
283                                                  GstQuery * query);
284
285 gboolean      gst_v4l2_object_propose_allocation (GstV4l2Object * obj,
286                                                   GstQuery * query);
287
288 GstStructure * gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
289
290
291 #define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
292                                                                                             \
293 static void                                                                                 \
294 interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
295                                                 guint prop_id,                              \
296                                                 const GParamSpec * pspec)                   \
297 {                                                                                           \
298   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
299   gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
300                                  &this_class->v4l2_class_devices);                          \
301 }                                                                                           \
302                                                                                             \
303 static gboolean                                                                             \
304 interface_as_function ## _probe_needs_probe (GstPropertyProbe * probe,                      \
305                                              guint prop_id,                                 \
306                                              const GParamSpec * pspec)                      \
307 {                                                                                           \
308   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
309   return gst_v4l2_probe_needs_probe (probe, prop_id, pspec,                                 \
310                                      &this_class->v4l2_class_devices);                      \
311 }                                                                                           \
312                                                                                             \
313 static GValueArray *                                                                        \
314 interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
315                                             guint prop_id,                                  \
316                                             const GParamSpec * pspec)                       \
317 {                                                                                           \
318   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
319   return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
320                                     &this_class->v4l2_class_devices);                       \
321 }                                                                                           \
322                                                                                             \
323 static void                                                                                 \
324 interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
325 {                                                                                           \
326   iface->get_properties = gst_v4l2_probe_get_properties;                                    \
327   iface->probe_property = interface_as_function ## _probe_probe_property;                   \
328   iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
329   iface->get_values = interface_as_function ## _probe_get_values;                           \
330 }
331
332 G_END_DECLS
333
334 #endif /* __GST_V4L2_OBJECT_H__ */