2b13be1cb79ce43a42fed18787b345c2db52ea0e
[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 struct _GstV4l2Object {
81   GstElement * element;
82
83   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
84
85   /* the video device */
86   char *videodev;
87
88   /* the video-device's file descriptor */
89   gint video_fd;
90   GstV4l2IOMode mode;
91   GstPoll * poll;
92   gboolean can_poll_device;
93
94   gboolean active;
95   gboolean streaming;
96
97   /* the current format */
98   struct v4l2_fmtdesc *fmtdesc;
99   struct v4l2_format format;
100   GstVideoInfo info;
101   GstVideoAlignment align;
102
103   /* Features */
104   gboolean need_video_meta;
105   gboolean need_crop_meta;
106   gboolean has_alpha_component;
107
108   /* only used if the device supports MPLANE
109    * nb planes is meaning of v4l2 planes
110    * the gstreamer equivalent is gst_buffer_n_memory
111    */
112   gint n_v4l2_planes;
113
114   guint32 bytesperline[GST_VIDEO_MAX_PLANES];
115   guint32 sizeimage;
116   GstClockTime duration;
117
118   /* if the MPLANE device support both contiguous and non contiguous 
119    * it allows to select which one we want. But we prefered_non_contiguous
120    * non contiguous mode.
121    */
122   gboolean prefered_non_contiguous;
123
124   /* This will be set if supported in decide_allocation. It can be used to
125    * calculate the minimum latency of a m2m decoder. */
126   guint32 min_buffers_for_capture;
127
128   /* wanted mode */
129   GstV4l2IOMode req_mode;
130
131   /* optional pool */
132   GstBufferPool *pool;
133
134   /* the video device's capabilities */
135   struct v4l2_capability vcap;
136
137   /* the video device's window properties */
138   struct v4l2_window vwin;
139
140   /* some more info about the current input's capabilities */
141   struct v4l2_input vinput;
142
143   /* lists... */
144   GSList *formats;              /* list of available capture formats */
145   GstCaps *probed_caps;
146
147   GList *colors;
148   GList *norms;
149   GList *channels;
150   GData *controls;
151
152   /* properties */
153   v4l2_std_id tv_norm;
154   gchar *channel;
155   gulong frequency;
156   GstStructure *extra_controls;
157   gboolean keep_aspect;
158   GValue *par;
159
160   /* X-overlay */
161   GstV4l2Xv *xv;
162   gulong xwindow_id;
163
164   /* funcs */
165   GstV4l2GetInOutFunction  get_in_out_func;
166   GstV4l2SetInOutFunction  set_in_out_func;
167   GstV4l2UpdateFpsFunction update_fps_func;
168
169   /* Quirks */
170   /* Skips interlacing probes */
171   gboolean never_interlaced;
172   /* Allow to skip reading initial format through G_FMT. Some devices
173    * just fails if you don't call S_FMT first. (ex: M2M decoders) */
174   gboolean no_initial_format;
175 };
176
177 struct _GstV4l2ObjectClassHelper {
178   /* probed devices */
179   GList *devices;
180 };
181
182 GType gst_v4l2_object_get_type (void);
183
184 #define V4L2_STD_OBJECT_PROPS \
185     PROP_DEVICE,              \
186     PROP_DEVICE_NAME,         \
187     PROP_DEVICE_FD,           \
188     PROP_FLAGS,               \
189     PROP_BRIGHTNESS,          \
190     PROP_CONTRAST,            \
191     PROP_SATURATION,          \
192     PROP_HUE,                 \
193     PROP_TV_NORM,             \
194     PROP_IO_MODE,             \
195     PROP_EXTRA_CONTROLS,      \
196     PROP_PIXEL_ASPECT_RATIO,  \
197     PROP_FORCE_ASPECT_RATIO
198
199 /* create/destroy */
200 GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
201                                            enum v4l2_buf_type  type,
202                                            const char * default_device,
203                                            GstV4l2GetInOutFunction get_in_out_func,
204                                            GstV4l2SetInOutFunction set_in_out_func,
205                                            GstV4l2UpdateFpsFunction update_fps_func);
206
207 void            gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
208
209 /* properties */
210
211 void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
212                                                         const char * default_device);
213
214 gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
215                                                         guint prop_id,
216                                                         const GValue * value,
217                                                         GParamSpec * pspec);
218 gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
219                                                         guint prop_id, GValue * value,
220                                                         GParamSpec * pspec);
221 /* open/close */
222 gboolean     gst_v4l2_object_open            (GstV4l2Object *v4l2object);
223 gboolean     gst_v4l2_object_open_shared     (GstV4l2Object *v4l2object, GstV4l2Object *other);
224 gboolean     gst_v4l2_object_close           (GstV4l2Object *v4l2object);
225
226 /* probing */
227 #if 0
228 const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);
229
230 void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
231                                              const GParamSpec * pspec,
232                                              GList ** klass_devices);
233 gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
234                                              const GParamSpec * pspec,
235                                              GList ** klass_devices);
236 GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
237                                              const GParamSpec * pspec,
238                                              GList ** klass_devices);
239 #endif
240
241 GstCaps*      gst_v4l2_object_get_all_caps (void);
242
243 GstCaps*      gst_v4l2_object_get_raw_caps (void);
244
245 GstCaps*      gst_v4l2_object_get_codec_caps (void);
246
247 gboolean      gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps);
248
249 gboolean      gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);
250
251 gboolean      gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
252 gboolean      gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
253
254 gboolean      gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
255
256 GstCaps *     gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object,
257                                            GstCaps * filter);
258
259 gboolean      gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object,
260                                               GstVideoInfo * info);
261
262 gboolean      gst_v4l2_object_set_crop    (GstV4l2Object * obj);
263  
264 gboolean      gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object,
265                                                  GstQuery * query);
266
267 gboolean      gst_v4l2_object_propose_allocation (GstV4l2Object * obj,
268                                                   GstQuery * query);
269
270 GstStructure * gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
271
272
273 #define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
274                                                                                             \
275 static void                                                                                 \
276 interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
277                                                 guint prop_id,                              \
278                                                 const GParamSpec * pspec)                   \
279 {                                                                                           \
280   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
281   gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
282                                  &this_class->v4l2_class_devices);                          \
283 }                                                                                           \
284                                                                                             \
285 static gboolean                                                                             \
286 interface_as_function ## _probe_needs_probe (GstPropertyProbe * probe,                      \
287                                              guint prop_id,                                 \
288                                              const GParamSpec * pspec)                      \
289 {                                                                                           \
290   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
291   return gst_v4l2_probe_needs_probe (probe, prop_id, pspec,                                 \
292                                      &this_class->v4l2_class_devices);                      \
293 }                                                                                           \
294                                                                                             \
295 static GValueArray *                                                                        \
296 interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
297                                             guint prop_id,                                  \
298                                             const GParamSpec * pspec)                       \
299 {                                                                                           \
300   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
301   return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
302                                     &this_class->v4l2_class_devices);                       \
303 }                                                                                           \
304                                                                                             \
305 static void                                                                                 \
306 interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
307 {                                                                                           \
308   iface->get_properties = gst_v4l2_probe_get_properties;                                    \
309   iface->probe_property = interface_as_function ## _probe_probe_property;                   \
310   iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
311   iface->get_values = interface_as_function ## _probe_get_values;                           \
312 }
313
314 G_END_DECLS
315
316 #endif /* __GST_V4L2_OBJECT_H__ */