fb5007daf5f507c23a5068439baeb4c55215ea15
[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 /* Because of some really cool feature in video4linux1, also known as
28  * 'not including sys/types.h and sys/time.h', we had to include it
29  * ourselves. In all their intelligence, these people decided to fix
30  * this in the next version (video4linux2) in such a cool way that it
31  * breaks all compilations of old stuff...
32  * The real problem is actually that linux/time.h doesn't use proper
33  * macro checks before defining types like struct timeval. The proper
34  * fix here is to either fuck the kernel header (which is what we do
35  * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
36  * upstream, which I'll consider doing later on. If you get compiler
37  * errors here, check your linux/time.h && sys/time.h header setup.
38  */
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #ifdef __sun
42 #include <sys/videodev2.h>
43 #elif defined(__FreeBSD__)
44 #include <linux/videodev2.h>
45 #else /* linux */
46 #include <linux/types.h>
47 #define _LINUX_TIME_H
48 #define __user
49 #include <linux/videodev2.h>
50 #endif
51
52 #include <gst/gst.h>
53 #include <gst/base/gstpushsrc.h>
54
55 #include <gst/video/video.h>
56
57 typedef struct _GstV4l2Object GstV4l2Object;
58 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
59 typedef struct _GstV4l2Xv GstV4l2Xv;
60
61 #include <gstv4l2bufferpool.h>
62
63 /* size of v4l2 buffer pool in streaming case */
64 #define GST_V4L2_MAX_BUFFERS 16
65 #define GST_V4L2_MIN_BUFFERS 1
66
67 /* max frame width/height */
68 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
69
70 G_BEGIN_DECLS
71
72 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
73 GType gst_v4l2_io_mode_get_type (void);
74
75 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
76
77 typedef enum {
78   GST_V4L2_IO_AUTO    = 0,
79   GST_V4L2_IO_RW      = 1,
80   GST_V4L2_IO_MMAP    = 2,
81   GST_V4L2_IO_USERPTR = 3,
82   GST_V4L2_IO_DMABUF  = 4
83 } GstV4l2IOMode;
84
85 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
86 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
87 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
88
89 #define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
90 #define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
91 #define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
92 #define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
93 #define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
94
95 /* simple check whether the device is open */
96 #define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)
97
98 /* check whether the device is 'active' */
99 #define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
100 #define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
101 #define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)
102
103 struct _GstV4l2Object {
104   GstElement * element;
105
106   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
107
108   /* the video device */
109   char *videodev;
110
111   /* the video-device's file descriptor */
112   gint video_fd;
113   GstV4l2IOMode mode;
114   GstPoll * poll;
115   gboolean can_poll_device;
116
117   gboolean active;
118   gboolean streaming;
119
120   /* the current format */
121   struct v4l2_fmtdesc *fmtdesc;
122   GstVideoInfo info;
123
124   /* only used if the device supports MPLANE
125    * nb planes is meaning of v4l2 planes
126    * the gstreamer equivalent is gst_buffer_n_memory
127    */
128   gint n_v4l2_planes;
129
130   guint32 bytesperline[GST_VIDEO_MAX_PLANES];
131   guint32 sizeimage;
132   GstClockTime duration;
133
134   /* if the MPLANE device support both contiguous and non contiguous 
135    * it allows to select which one we want. But we prefered_non_contiguous
136    * non contiguous mode.
137    */
138   gboolean prefered_non_contiguous;
139
140   /* wanted mode */
141   GstV4l2IOMode req_mode;
142
143   /* optional pool */
144   GstBufferPool *pool;
145
146   /* the video device's capabilities */
147   struct v4l2_capability vcap;
148
149   /* the video device's window properties */
150   struct v4l2_window vwin;
151
152   /* some more info about the current input's capabilities */
153   struct v4l2_input vinput;
154
155   /* lists... */
156   GSList *formats;              /* list of available capture formats */
157   GstCaps *probed_caps;
158
159   GList *colors;
160   GList *norms;
161   GList *channels;
162   GData *controls;
163
164   /* properties */
165   v4l2_std_id tv_norm;
166   gchar *channel;
167   gulong frequency;
168   GstStructure *extra_controls;
169   gboolean keep_aspect;
170   GValue *par;
171
172   /* X-overlay */
173   GstV4l2Xv *xv;
174   gulong xwindow_id;
175
176   /* funcs */
177   GstV4l2GetInOutFunction  get_in_out_func;
178   GstV4l2SetInOutFunction  set_in_out_func;
179   GstV4l2UpdateFpsFunction update_fps_func;
180
181   /* Quirks */
182   /* Skips interlacing probes */
183   gboolean never_interlaced;
184 };
185
186 struct _GstV4l2ObjectClassHelper {
187   /* probed devices */
188   GList *devices;
189 };
190
191 GType gst_v4l2_object_get_type (void);
192
193 #define V4L2_STD_OBJECT_PROPS \
194     PROP_DEVICE,              \
195     PROP_DEVICE_NAME,         \
196     PROP_DEVICE_FD,           \
197     PROP_FLAGS,               \
198     PROP_BRIGHTNESS,          \
199     PROP_CONTRAST,            \
200     PROP_SATURATION,          \
201     PROP_HUE,                 \
202     PROP_TV_NORM,             \
203     PROP_IO_MODE,             \
204     PROP_EXTRA_CONTROLS,      \
205     PROP_PIXEL_ASPECT_RATIO,  \
206     PROP_FORCE_ASPECT_RATIO
207
208 /* create/destroy */
209 GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
210                                            enum v4l2_buf_type  type,
211                                            const char * default_device,
212                                            GstV4l2GetInOutFunction get_in_out_func,
213                                            GstV4l2SetInOutFunction set_in_out_func,
214                                            GstV4l2UpdateFpsFunction update_fps_func);
215
216 void            gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
217
218 /* properties */
219
220 void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
221                                                         const char * default_device);
222
223 gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
224                                                         guint prop_id,
225                                                         const GValue * value,
226                                                         GParamSpec * pspec);
227 gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
228                                                         guint prop_id, GValue * value,
229                                                         GParamSpec * pspec);
230 /* open/close */
231 gboolean     gst_v4l2_object_open            (GstV4l2Object *v4l2object);
232 gboolean     gst_v4l2_object_open_shared     (GstV4l2Object *v4l2object, GstV4l2Object *other);
233 gboolean     gst_v4l2_object_close           (GstV4l2Object *v4l2object);
234
235 /* probing */
236 #if 0
237 const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);
238
239 void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
240                                              const GParamSpec * pspec,
241                                              GList ** klass_devices);
242 gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
243                                              const GParamSpec * pspec,
244                                              GList ** klass_devices);
245 GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
246                                              const GParamSpec * pspec,
247                                              GList ** klass_devices);
248 #endif
249
250 GstCaps*      gst_v4l2_object_get_all_caps (void);
251
252 GstCaps*      gst_v4l2_object_get_raw_caps (void);
253
254 GstCaps*      gst_v4l2_object_get_codec_caps (void);
255
256 GstStructure* gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
257
258 gboolean      gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps);
259
260 gboolean      gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);
261
262 gboolean      gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
263 gboolean      gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
264
265 gboolean      gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
266
267
268 gboolean      gst_v4l2_object_copy        (GstV4l2Object * v4l2object,
269                                            GstBuffer * dest, GstBuffer * src);
270
271 GstCaps *     gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object,
272                                            GstCaps * filter);
273
274
275 #define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
276                                                                                             \
277 static void                                                                                 \
278 interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
279                                                 guint prop_id,                              \
280                                                 const GParamSpec * pspec)                   \
281 {                                                                                           \
282   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
283   gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
284                                  &this_class->v4l2_class_devices);                          \
285 }                                                                                           \
286                                                                                             \
287 static gboolean                                                                             \
288 interface_as_function ## _probe_needs_probe (GstPropertyProbe * probe,                      \
289                                              guint prop_id,                                 \
290                                              const GParamSpec * pspec)                      \
291 {                                                                                           \
292   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
293   return gst_v4l2_probe_needs_probe (probe, prop_id, pspec,                                 \
294                                      &this_class->v4l2_class_devices);                      \
295 }                                                                                           \
296                                                                                             \
297 static GValueArray *                                                                        \
298 interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
299                                             guint prop_id,                                  \
300                                             const GParamSpec * pspec)                       \
301 {                                                                                           \
302   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
303   return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
304                                     &this_class->v4l2_class_devices);                       \
305 }                                                                                           \
306                                                                                             \
307 static void                                                                                 \
308 interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
309 {                                                                                           \
310   iface->get_properties = gst_v4l2_probe_get_properties;                                    \
311   iface->probe_property = interface_as_function ## _probe_probe_property;                   \
312   iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
313   iface->get_values = interface_as_function ## _probe_get_values;                           \
314 }
315
316 G_END_DECLS
317
318 #endif /* __GST_V4L2_OBJECT_H__ */