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