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