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