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