Merge remote-tracking branch 'gst-plugins-ugly/tizen_gst_1.19.2' into tizen_gst_1...
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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 #include <unistd.h>
39
40 typedef struct _GstV4l2Object GstV4l2Object;
41 typedef struct _GstV4l2ObjectClassHelper GstV4l2ObjectClassHelper;
42
43 #include <gstv4l2bufferpool.h>
44
45 /* size of v4l2 buffer pool in streaming case, obj->info needs to be valid */
46 #define GST_V4L2_MIN_BUFFERS(obj) \
47     ((GST_VIDEO_INFO_INTERLACE_MODE (&obj->info) == \
48       GST_VIDEO_INTERLACE_MODE_ALTERNATE) ? \
49       /* 2x buffers needed with each field in its own buffer */ \
50       4 : 2)
51
52 /* max frame width/height */
53 #define GST_V4L2_MAX_SIZE (1<<15) /* 2^15 == 32768 */
54
55 G_BEGIN_DECLS
56
57 #define GST_TYPE_V4L2_IO_MODE (gst_v4l2_io_mode_get_type ())
58 GType gst_v4l2_io_mode_get_type (void);
59
60 #define GST_V4L2_OBJECT(obj) (GstV4l2Object *)(obj)
61
62 typedef enum {
63   GST_V4L2_IO_AUTO          = 0,
64   GST_V4L2_IO_RW            = 1,
65   GST_V4L2_IO_MMAP          = 2,
66   GST_V4L2_IO_USERPTR       = 3,
67   GST_V4L2_IO_DMABUF        = 4,
68   GST_V4L2_IO_DMABUF_IMPORT = 5
69 } GstV4l2IOMode;
70
71 typedef gboolean  (*GstV4l2GetInOutFunction)  (GstV4l2Object * v4l2object, guint32 * input);
72 typedef gboolean  (*GstV4l2SetInOutFunction)  (GstV4l2Object * v4l2object, guint32 input);
73 typedef gboolean  (*GstV4l2UpdateFpsFunction) (GstV4l2Object * v4l2object);
74
75 /* On Android NDK r18b the ioctl() signature uses 'unsigned' instead of
76  * 'unsigned long' for the 2nd parameter */
77 #ifdef __ANDROID__
78 typedef unsigned ioctl_req_t;
79 #else
80 typedef gulong ioctl_req_t;
81 #endif
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   GstObject * dbg_obj;
128
129   enum v4l2_buf_type type;   /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
130
131   /* the video device */
132   char *videodev;
133
134 #ifdef TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE
135   /* auto scan device */
136   gboolean auto_scan_device;
137 #endif /* TIZEN_FEATURE_V4L2SRC_AUTO_SCAN_DEVICE_NODE */
138 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
139   gboolean tbm_output;
140 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
141
142   /* the video-device's file descriptor */
143   gint video_fd;
144   GstV4l2IOMode mode;
145
146   gboolean active;
147
148   /* the current format */
149   struct v4l2_fmtdesc *fmtdesc;
150   struct v4l2_format format;
151   GstVideoInfo info;
152   GstVideoAlignment align;
153   GstVideoTransferFunction transfer;
154
155   /* Features */
156   gboolean need_video_meta;
157   gboolean has_alpha_component;
158
159   /* only used if the device supports MPLANE
160    * nb planes is meaning of v4l2 planes
161    * the gstreamer equivalent is gst_buffer_n_memory
162    */
163   gint n_v4l2_planes;
164
165   /* We cache the frame duration if known */
166   GstClockTime duration;
167
168   /* if the MPLANE device support both contiguous and non contiguous
169    * it allows to select which one we want. But we prefered_non_contiguous
170    * non contiguous mode.
171    */
172   gboolean prefered_non_contiguous;
173
174   /* This will be set if supported in decide_allocation. It can be used to
175    * calculate the minimum latency. */
176   guint32 min_buffers;
177
178   /* wanted mode */
179   GstV4l2IOMode req_mode;
180
181   /* optional pool */
182   GstBufferPool *pool;
183   /* the sequence of pool to identify (for debugging) */
184   guint pool_seq;
185
186   /* the video device's capabilities */
187   struct v4l2_capability vcap;
188   /* opened device specific capabilities */
189   guint32 device_caps;
190
191   /* lists... */
192   GSList *formats;              /* list of available capture formats */
193   GstCaps *probed_caps;
194
195   GList *colors;
196   GList *norms;
197   GList *channels;
198   GData *controls;
199
200   /* properties */
201   v4l2_std_id tv_norm;
202   gchar *channel;
203   gulong frequency;
204   GstStructure *extra_controls;
205   gboolean keep_aspect;
206   GValue *par;
207
208   /* funcs */
209   GstV4l2GetInOutFunction  get_in_out_func;
210   GstV4l2SetInOutFunction  set_in_out_func;
211   GstV4l2UpdateFpsFunction update_fps_func;
212
213   /* syscalls */
214   gint (*fd_open) (gint fd, gint v4l2_flags);
215   gint (*close) (gint fd);
216   gint (*dup) (gint fd);
217   gint (*ioctl) (gint fd, ioctl_req_t request, ...);
218   gssize (*read) (gint fd, gpointer buffer, gsize n);
219   gpointer (*mmap) (gpointer start, gsize length, gint prot, gint flags,
220       gint fd,  off_t offset);
221   gint (*munmap) (gpointer _start, gsize length);
222
223   /* Quirks */
224   /* Skips interlacing probes */
225   gboolean never_interlaced;
226   /* Allow to skip reading initial format through G_FMT. Some devices
227    * just fails if you don't call S_FMT first. (ex: M2M decoders) */
228   gboolean no_initial_format;
229   /* Avoid any try_fmt probe. This is used by v4l2src to speedup start up time
230    * on slow USB firmwares. When this is set, gst_v4l2_set_format() will modify
231    * the caps to reflect what was negotiated during fixation */
232   gboolean skip_try_fmt_probes;
233 };
234
235 struct _GstV4l2ObjectClassHelper {
236   /* probed devices */
237   GList *devices;
238 };
239
240 GType gst_v4l2_object_get_type (void);
241
242 #define V4L2_STD_OBJECT_PROPS \
243     PROP_DEVICE,              \
244     PROP_DEVICE_NAME,         \
245     PROP_DEVICE_FD,           \
246     PROP_FLAGS,               \
247     PROP_BRIGHTNESS,          \
248     PROP_CONTRAST,            \
249     PROP_SATURATION,          \
250     PROP_HUE,                 \
251     PROP_TV_NORM,             \
252     PROP_IO_MODE,             \
253     PROP_OUTPUT_IO_MODE,      \
254     PROP_CAPTURE_IO_MODE,     \
255     PROP_EXTRA_CONTROLS,      \
256     PROP_PIXEL_ASPECT_RATIO,  \
257     PROP_FORCE_ASPECT_RATIO
258
259 /* create/destroy */
260 GstV4l2Object*  gst_v4l2_object_new       (GstElement * element,
261                                            GstObject * dbg_obj,
262                                            enum v4l2_buf_type  type,
263                                            const char * default_device,
264                                            GstV4l2GetInOutFunction get_in_out_func,
265                                            GstV4l2SetInOutFunction set_in_out_func,
266                                            GstV4l2UpdateFpsFunction update_fps_func);
267
268 void         gst_v4l2_object_destroy   (GstV4l2Object * v4l2object);
269
270 /* properties */
271
272 void         gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
273                                                         const char * default_device);
274
275 void         gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class);
276
277 gboolean     gst_v4l2_object_set_property_helper       (GstV4l2Object * v4l2object,
278                                                         guint prop_id,
279                                                         const GValue * value,
280                                                         GParamSpec * pspec);
281 gboolean     gst_v4l2_object_get_property_helper       (GstV4l2Object *v4l2object,
282                                                         guint prop_id, GValue * value,
283                                                         GParamSpec * pspec);
284 /* open/close */
285 gboolean     gst_v4l2_object_open            (GstV4l2Object * v4l2object, GstV4l2Error * error);
286 gboolean     gst_v4l2_object_open_shared     (GstV4l2Object * v4l2object, GstV4l2Object * other);
287 gboolean     gst_v4l2_object_close           (GstV4l2Object * v4l2object);
288
289 /* probing */
290
291 GstCaps*     gst_v4l2_object_get_all_caps (void);
292
293 GstCaps*     gst_v4l2_object_get_raw_caps (void);
294
295 GstCaps*     gst_v4l2_object_get_codec_caps (void);
296
297 gint         gst_v4l2_object_extrapolate_stride (const GstVideoFormatInfo * finfo,
298                                                   gint plane, gint stride);
299
300 gboolean     gst_v4l2_object_set_format  (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error * error);
301 gboolean     gst_v4l2_object_try_format  (GstV4l2Object * v4l2object, GstCaps * caps, GstV4l2Error * error);
302 gboolean     gst_v4l2_object_try_import  (GstV4l2Object * v4l2object, GstBuffer * buffer);
303
304 gboolean     gst_v4l2_object_caps_equal       (GstV4l2Object * v4l2object, GstCaps * caps);
305 gboolean     gst_v4l2_object_caps_is_subset   (GstV4l2Object * v4l2object, GstCaps * caps);
306 GstCaps *    gst_v4l2_object_get_current_caps (GstV4l2Object * v4l2object);
307
308 gboolean     gst_v4l2_object_unlock      (GstV4l2Object * v4l2object);
309 gboolean     gst_v4l2_object_unlock_stop (GstV4l2Object * v4l2object);
310
311 gboolean     gst_v4l2_object_stop        (GstV4l2Object * v4l2object);
312
313 GstCaps *    gst_v4l2_object_probe_caps  (GstV4l2Object * v4l2object, GstCaps * filter);
314 GstCaps *    gst_v4l2_object_get_caps    (GstV4l2Object * v4l2object, GstCaps * filter);
315
316 gboolean     gst_v4l2_object_acquire_format (GstV4l2Object * v4l2object, GstVideoInfo * info);
317
318 gboolean     gst_v4l2_object_set_crop    (GstV4l2Object * obj);
319
320 gboolean     gst_v4l2_object_decide_allocation (GstV4l2Object * v4l2object, GstQuery * query);
321
322 gboolean     gst_v4l2_object_propose_allocation (GstV4l2Object * obj, GstQuery * query);
323
324 GstStructure * gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc);
325
326 /* TODO Move to proper namespace */
327 /* open/close the device */
328 gboolean     gst_v4l2_open           (GstV4l2Object * v4l2object, GstV4l2Error * error);
329 gboolean     gst_v4l2_dup            (GstV4l2Object * v4l2object, GstV4l2Object * other);
330 gboolean     gst_v4l2_close          (GstV4l2Object * v4l2object);
331
332 /* norm/input/output */
333 gboolean     gst_v4l2_get_norm       (GstV4l2Object * v4l2object, v4l2_std_id * norm);
334 gboolean     gst_v4l2_set_norm       (GstV4l2Object * v4l2object, v4l2_std_id norm);
335 gboolean     gst_v4l2_get_input      (GstV4l2Object * v4l2object, guint32 * input);
336 gboolean     gst_v4l2_set_input      (GstV4l2Object * v4l2object, guint32 input);
337 gboolean     gst_v4l2_query_input    (GstV4l2Object * v4l2object, struct v4l2_input * input);
338 gboolean     gst_v4l2_get_output     (GstV4l2Object * v4l2object, guint32 * output);
339 gboolean     gst_v4l2_set_output     (GstV4l2Object * v4l2object, guint32 output);
340
341 /* dv timings */
342 gboolean     gst_v4l2_set_dv_timings   (GstV4l2Object * v4l2object, struct v4l2_dv_timings *timings);
343 gboolean     gst_v4l2_query_dv_timings (GstV4l2Object * v4l2object, struct v4l2_dv_timings *timings);
344
345 /* frequency control */
346 gboolean     gst_v4l2_get_frequency   (GstV4l2Object * v4l2object, gint tunernum, gulong * frequency);
347 gboolean     gst_v4l2_set_frequency   (GstV4l2Object * v4l2object, gint tunernum, gulong frequency);
348 gboolean     gst_v4l2_signal_strength (GstV4l2Object * v4l2object, gint tunernum, gulong * signal);
349
350 /* attribute control */
351 gboolean     gst_v4l2_get_attribute   (GstV4l2Object * v4l2object, int attribute, int * value);
352 gboolean     gst_v4l2_set_attribute   (GstV4l2Object * v4l2object, int attribute, const int value);
353 gboolean     gst_v4l2_set_string_attribute (GstV4l2Object * v4l2object, int attribute_num, const char *value);
354 gboolean     gst_v4l2_set_controls    (GstV4l2Object * v4l2object, GstStructure * controls);
355
356 /* events */
357 gboolean     gst_v4l2_subscribe_event (GstV4l2Object * v4l2object, guint32 event, guint32 id);
358 gboolean     gst_v4l2_dequeue_event   (GstV4l2Object * v4l2object, struct v4l2_event *event);
359
360 G_END_DECLS
361
362 #endif /* __GST_V4L2_OBJECT_H__ */