Update Edgard Lima's email
[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@gmail.com>
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 #include "v4l2-utils.h"
29
30 #include <gst/gst.h>
31 #include <gst/base/gstpushsrc.h>
32
33 #include <gst/video/video.h>
34
35 typedef struct _GstV4l2Object GstV4l2Object;
36 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
37 typedef struct _GstV4l2Xv GstV4l2Xv;
38
39 #include <gstv4l2bufferpool.h>
40
41 /* size of v4l2 buffer pool in streaming case */
42 #define GST_V4L2_MIN_BUFFERS 2
43
44 /* max frame width/height */
45 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
46
47 G_BEGIN_DECLS
48
49 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
50 GType gst_v4l2_io_mode_get_type (void);
51
52 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
53
54 typedef enum {
55   GST_V4L2_IO_AUTO          = 0,
56   GST_V4L2_IO_RW            = 1,
57   GST_V4L2_IO_MMAP          = 2,
58   GST_V4L2_IO_USERPTR       = 3,
59   GST_V4L2_IO_DMABUF        = 4,
60   GST_V4L2_IO_DMABUF_IMPORT = 5
61 } GstV4l2IOMode;
62
63 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, gint * input);
64 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, gint input);
65 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
66
67 #define GST_V4L2_WIDTH(o)        (GST_VIDEO_INFO_WIDTH (&(o)->info))
68 #define GST_V4L2_HEIGHT(o)       (GST_VIDEO_INFO_HEIGHT (&(o)->info))
69 #define GST_V4L2_PIXELFORMAT(o)  ((o)->fmtdesc->pixelformat)
70 #define GST_V4L2_FPS_N(o)        (GST_VIDEO_INFO_FPS_N (&(o)->info))
71 #define GST_V4L2_FPS_D(o)        (GST_VIDEO_INFO_FPS_D (&(o)->info))
72
73 /* simple check whether the device is open */
74 #define GST_V4L2_IS_OPEN(o)      ((o)->video_fd > 0)
75
76 /* check whether the device is 'active' */
77 #define GST_V4L2_IS_ACTIVE(o)    ((o)->active)
78 #define GST_V4L2_SET_ACTIVE(o)   ((o)->active = TRUE)
79 #define GST_V4L2_SET_INACTIVE(o) ((o)->active = FALSE)
80
81 struct _GstV4l2Object {
82   GstElement * element;
83
84   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
85
86   /* the video device */
87   char *videodev;
88
89   /* the video-device's file descriptor */
90   gint video_fd;
91   GstV4l2IOMode mode;
92
93   gboolean active;
94   gboolean streaming;
95
96   /* the current format */
97   struct v4l2_fmtdesc *fmtdesc;
98   struct v4l2_format format;
99   GstVideoInfo info;
100   GstVideoAlignment align;
101
102   /* Features */
103   gboolean need_video_meta;
104   gboolean has_alpha_component;
105
106   /* only used if the device supports MPLANE
107    * nb planes is meaning of v4l2 planes
108    * the gstreamer equivalent is gst_buffer_n_memory
109    */
110   gint n_v4l2_planes;
111
112   /* We cache the frame duration if known */
113   GstClockTime duration;
114
115   /* if the MPLANE device support both contiguous and non contiguous
116    * it allows to select which one we want. But we prefered_non_contiguous
117    * non contiguous mode.
118    */
119   gboolean prefered_non_contiguous;
120
121   /* This will be set if supported in decide_allocation. It can be used to
122    * calculate the minimum latency. */
123   guint32 min_buffers;
124
125   /* This will be set if supported in propose allocation. */
126   guint32 min_buffers_for_output;
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   /* opened device specific capabilities */
137   guint32 device_caps;
138
139   /* the video device's window properties */
140   struct v4l2_window vwin;
141
142   /* some more info about the current input's capabilities */
143   struct v4l2_input vinput;
144
145   /* lists... */
146   GSList *formats;              /* list of available capture formats */
147   GstCaps *probed_caps;
148
149   GList *colors;
150   GList *norms;
151   GList *channels;
152   GData *controls;
153
154   /* properties */
155   v4l2_std_id tv_norm;
156   gchar *channel;
157   gulong frequency;
158   GstStructure *extra_controls;
159   gboolean keep_aspect;
160   GValue *par;
161
162   /* X-overlay */
163   GstV4l2Xv *xv;
164   gulong xwindow_id;
165
166   /* funcs */
167   GstV4l2GetInOutFunction  get_in_out_func;
168   GstV4l2SetInOutFunction  set_in_out_func;
169   GstV4l2UpdateFpsFunction update_fps_func;
170
171   /* Quirks */
172   /* Skips interlacing probes */
173   gboolean never_interlaced;
174   /* Allow to skip reading initial format through G_FMT. Some devices
175    * just fails if you don't call S_FMT first. (ex: M2M decoders) */
176   gboolean no_initial_format;
177 };
178
179 struct _GstV4l2ObjectClassHelper {
180   /* probed devices */
181   GList *devices;
182 };
183
184 GType gst_v4l2_object_get_type (void);
185
186 #define V4L2_STD_OBJECT_PROPS \
187     PROP_DEVICE,              \
188     PROP_DEVICE_NAME,         \
189     PROP_DEVICE_FD,           \
190     PROP_FLAGS,               \
191     PROP_BRIGHTNESS,          \
192     PROP_CONTRAST,            \
193     PROP_SATURATION,          \
194     PROP_HUE,                 \
195     PROP_TV_NORM,             \
196     PROP_IO_MODE,             \
197     PROP_OUTPUT_IO_MODE,      \
198     PROP_CAPTURE_IO_MODE,     \
199     PROP_EXTRA_CONTROLS,      \
200     PROP_PIXEL_ASPECT_RATIO,  \
201     PROP_FORCE_ASPECT_RATIO
202
203 /* create/destroy */
204 GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
205                                            enum v4l2_buf_type  type,
206                                            const char * default_device,
207                                            GstV4l2GetInOutFunction get_in_out_func,
208                                            GstV4l2SetInOutFunction set_in_out_func,
209                                            GstV4l2UpdateFpsFunction update_fps_func);
210
211 void            gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
212
213 /* properties */
214
215 void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
216                                                         const char * default_device);
217
218 void         gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class);
219
220 gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
221                                                         guint prop_id,
222                                                         const GValue * value,
223                                                         GParamSpec * pspec);
224 gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
225                                                         guint prop_id, GValue * value,
226                                                         GParamSpec * pspec);
227 /* open/close */
228 gboolean     gst_v4l2_object_open            (GstV4l2Object *v4l2object);
229 gboolean     gst_v4l2_object_open_shared     (GstV4l2Object *v4l2object, GstV4l2Object *other);
230 gboolean     gst_v4l2_object_close           (GstV4l2Object *v4l2object);
231
232 /* probing */
233 #if 0
234 const GList* gst_v4l2_probe_get_properties  (GstPropertyProbe * probe);
235
236 void         gst_v4l2_probe_probe_property  (GstPropertyProbe * probe, guint prop_id,
237                                              const GParamSpec * pspec,
238                                              GList ** klass_devices);
239 gboolean     gst_v4l2_probe_needs_probe     (GstPropertyProbe * probe, guint prop_id,
240                                              const GParamSpec * pspec,
241                                              GList ** klass_devices);
242 GValueArray* gst_v4l2_probe_get_values      (GstPropertyProbe * probe, guint prop_id,
243                                              const GParamSpec * pspec,
244                                              GList ** klass_devices);
245 #endif
246
247 GstCaps*      gst_v4l2_object_get_all_caps (void);
248
249 GstCaps*      gst_v4l2_object_get_raw_caps (void);
250
251 GstCaps*      gst_v4l2_object_get_codec_caps (void);
252
253 gint          gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
254                                                   gint plane, gint stride);
255
256 gboolean      gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error *error);
257 gboolean      gst_v4l2_object_try_format  (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error *error);
258
259 gboolean      gst_v4l2_object_caps_equal  (GstV4l2Object * v4l2object, GstCaps * caps);
260
261 gboolean      gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
262 gboolean      gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
263
264 gboolean      gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
265
266 GstCaps *     gst_v4l2_object_probe_caps  (GstV4l2Object * v4l2object,
267                                            GstCaps * filter);
268 GstCaps *     gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object,
269                                            GstCaps * filter);
270
271 gboolean      gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object,
272                                               GstVideoInfo * info);
273
274 gboolean      gst_v4l2_object_set_crop    (GstV4l2Object * obj);
275
276 gboolean      gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object,
277                                                  GstQuery * query);
278
279 gboolean      gst_v4l2_object_propose_allocation (GstV4l2Object * obj,
280                                                   GstQuery * query);
281
282 GstStructure * gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
283
284
285 #define GST_IMPLEMENT_V4L2_PROBE_METHODS(Type_Class, interface_as_function)                 \
286                                                                                             \
287 static void                                                                                 \
288 interface_as_function ## _probe_probe_property (GstPropertyProbe * probe,                   \
289                                                 guint prop_id,                              \
290                                                 const GParamSpec * pspec)                   \
291 {                                                                                           \
292   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
293   gst_v4l2_probe_probe_property (probe, prop_id, pspec,                                     \
294                                  &this_class->v4l2_class_devices);                          \
295 }                                                                                           \
296                                                                                             \
297 static gboolean                                                                             \
298 interface_as_function ## _probe_needs_probe (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_needs_probe (probe, prop_id, pspec,                                 \
304                                      &this_class->v4l2_class_devices);                      \
305 }                                                                                           \
306                                                                                             \
307 static GValueArray *                                                                        \
308 interface_as_function ## _probe_get_values (GstPropertyProbe * probe,                       \
309                                             guint prop_id,                                  \
310                                             const GParamSpec * pspec)                       \
311 {                                                                                           \
312   Type_Class *this_class = (Type_Class*) G_OBJECT_GET_CLASS (probe);                        \
313   return gst_v4l2_probe_get_values (probe, prop_id, pspec,                                  \
314                                     &this_class->v4l2_class_devices);                       \
315 }                                                                                           \
316                                                                                             \
317 static void                                                                                 \
318 interface_as_function ## _property_probe_interface_init (GstPropertyProbeInterface * iface) \
319 {                                                                                           \
320   iface->get_properties = gst_v4l2_probe_get_properties;                                    \
321   iface->probe_property = interface_as_function ## _probe_probe_property;                   \
322   iface->needs_probe = interface_as_function ## _probe_needs_probe;                         \
323   iface->get_values = interface_as_function ## _probe_get_values;                           \
324 }
325
326 G_END_DECLS
327
328 #endif /* __GST_V4L2_OBJECT_H__ */