Initialize Tizen 2.3
[framework/multimedia/gst-plugins-base0.10.git] / wearable / sys / xvimage / xvimagesink.h
1 /* GStreamer
2  * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
3  * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * * Modifications by Samsung Electronics Co., Ltd.
21  * 1. Add display related properties
22  * 2. Support samsung extension format to improve performance
23  * 3. Support video texture overlay of OSP layer
24  */
25
26 #ifndef __GST_XVIMAGESINK_H__
27 #define __GST_XVIMAGESINK_H__
28
29 #include <gst/video/gstvideosink.h>
30
31 #ifdef HAVE_XSHM
32 #include <sys/types.h>
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35 #endif /* HAVE_XSHM */
36
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39
40 #ifdef HAVE_XSHM
41 #include <X11/extensions/XShm.h>
42 #endif /* HAVE_XSHM */
43
44 #include <X11/extensions/Xv.h>
45 #include <X11/extensions/Xvlib.h>
46 #ifdef GST_EXT_XV_ENHANCEMENT
47 #include <X11/Xatom.h>
48 #include <stdio.h>
49 #include "xv_types.h"
50 #endif
51
52 #include <string.h>
53 #include <math.h>
54 #include <stdlib.h>
55
56 G_BEGIN_DECLS
57
58 #define GST_TYPE_XVIMAGESINK \
59   (gst_xvimagesink_get_type())
60 #define GST_XVIMAGESINK(obj) \
61   (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_XVIMAGESINK, GstXvImageSink))
62 #define GST_XVIMAGESINK_CLASS(klass) \
63   (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_XVIMAGESINK, GstXvImageSinkClass))
64 #define GST_IS_XVIMAGESINK(obj) \
65   (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_XVIMAGESINK))
66 #define GST_IS_XVIMAGESINK_CLASS(klass) \
67   (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_XVIMAGESINK))
68
69 #ifdef GST_EXT_XV_ENHANCEMENT
70 #define XV_SCREEN_SIZE_WIDTH            4096
71 #define XV_SCREEN_SIZE_HEIGHT           4096
72 #define DISPLAYING_BUFFERS_MAX_NUM      10
73
74 #define MAX_PIXMAP_NUM 10
75 typedef uint (*get_pixmap_callback)(void *user_data);
76 typedef struct _GstXPixmap GstXPixmap;
77 typedef struct _GstXvImageDisplayingBuffer GstXvImageDisplayingBuffer;
78 typedef struct _GstXvImageFlushBuffer GstXvImageFlushBuffer;
79 #endif /* GST_EXT_XV_ENHANCEMENT */
80
81 typedef struct _GstXContext GstXContext;
82 typedef struct _GstXWindow GstXWindow;
83 typedef struct _GstXvImageFormat GstXvImageFormat;
84 typedef struct _GstXvImageBuffer GstXvImageBuffer;
85 typedef struct _GstXvImageBufferClass GstXvImageBufferClass;
86
87 typedef struct _GstXvImageSink GstXvImageSink;
88 typedef struct _GstXvImageSinkClass GstXvImageSinkClass;
89
90 /*
91  * GstXContext:
92  * @disp: the X11 Display of this context
93  * @screen: the default Screen of Display @disp
94  * @screen_num: the Screen number of @screen
95  * @visual: the default Visual of Screen @screen
96  * @root: the root Window of Display @disp
97  * @white: the value of a white pixel on Screen @screen
98  * @black: the value of a black pixel on Screen @screen
99  * @depth: the color depth of Display @disp
100  * @bpp: the number of bits per pixel on Display @disp
101  * @endianness: the endianness of image bytes on Display @disp
102  * @width: the width in pixels of Display @disp
103  * @height: the height in pixels of Display @disp
104  * @widthmm: the width in millimeters of Display @disp
105  * @heightmm: the height in millimeters of Display @disp
106  * @par: the pixel aspect ratio calculated from @width, @widthmm and @height,
107  * @heightmm ratio
108  * @use_xshm: used to known wether of not XShm extension is usable or not even
109  * if the Extension is present
110  * @xv_port_id: the XVideo port ID
111  * @im_format: used to store at least a valid format for XShm calls checks
112  * @formats_list: list of supported image formats on @xv_port_id
113  * @channels_list: list of #GstColorBalanceChannels
114  * @caps: the #GstCaps that Display @disp can accept
115  *
116  * Structure used to store various informations collected/calculated for a
117  * Display.
118  */
119 struct _GstXContext {
120   Display *disp;
121
122   Screen *screen;
123   gint screen_num;
124
125   Visual *visual;
126
127   Window root;
128
129   gulong white, black;
130
131   gint depth;
132   gint bpp;
133   gint endianness;
134
135   gint width, height;
136   gint widthmm, heightmm;
137   GValue *par;                  /* calculated pixel aspect ratio */
138
139   gboolean use_xshm;
140
141   XvPortID xv_port_id;
142   guint nb_adaptors;
143   gchar ** adaptors;
144   gint im_format;
145
146   GList *formats_list;
147   GList *channels_list;
148
149   GstCaps *caps;
150
151   /* Optimisation storage for buffer_alloc return */
152   GstCaps *last_caps;
153   gint last_format;
154   gint last_width;
155   gint last_height;
156 };
157
158 /*
159  * GstXWindow:
160  * @win: the Window ID of this X11 window
161  * @width: the width in pixels of Window @win
162  * @height: the height in pixels of Window @win
163  * @internal: used to remember if Window @win was created internally or passed
164  * through the #GstXOverlay interface
165  * @gc: the Graphical Context of Window @win
166  *
167  * Structure used to store informations about a Window.
168  */
169 struct _GstXWindow {
170   Window win;
171 #ifdef GST_EXT_XV_ENHANCEMENT
172   gint x, y;
173 #endif
174   gint width, height;
175   gboolean internal;
176   GC gc;
177 };
178
179 #ifdef GST_EXT_XV_ENHANCEMENT
180 struct _GstXPixmap {
181         Window pixmap;
182         gint x, y;
183         gint width, height;
184         GC gc;
185 };
186
187 struct _GstXvImageDisplayingBuffer {
188         GstBuffer *buffer;
189         unsigned int dmabuf_fd[XV_BUF_PLANE_NUM];
190         unsigned int gem_name[XV_BUF_PLANE_NUM];
191         unsigned int gem_handle[XV_BUF_PLANE_NUM];
192         void *bo[XV_BUF_PLANE_NUM];
193         void *vaddr[XV_BUF_PLANE_NUM];
194         unsigned int ref_count;
195 };
196
197 struct _GstXvImageFlushBuffer {
198         unsigned int gem_name[XV_BUF_PLANE_NUM];
199         void *bo[XV_BUF_PLANE_NUM];
200 };
201 #endif
202
203 /**
204  * GstXvImageFormat:
205  * @format: the image format
206  * @caps: generated #GstCaps for this image format
207  *
208  * Structure storing image format to #GstCaps association.
209  */
210 struct _GstXvImageFormat {
211   gint format;
212   GstCaps *caps;
213 };
214
215 /**
216  * GstXImageBuffer:
217  * @xvimagesink: a reference to our #GstXvImageSink
218  * @xvimage: the XvImage of this buffer
219  * @width: the width in pixels of XvImage @xvimage
220  * @height: the height in pixels of XvImage @xvimage
221  * @im_format: the image format of XvImage @xvimage
222  * @size: the size in bytes of XvImage @xvimage
223  *
224  * Subclass of #GstBuffer containing additional information about an XvImage.
225  */
226 struct _GstXvImageBuffer {
227   GstBuffer   buffer;
228
229   /* Reference to the xvimagesink we belong to */
230   GstXvImageSink *xvimagesink;
231
232   XvImage *xvimage;
233
234 #ifdef HAVE_XSHM
235   XShmSegmentInfo SHMInfo;
236 #endif /* HAVE_XSHM */
237
238   gint width, height, im_format;
239   size_t size;
240 #ifdef GST_EXT_XV_ENHANCEMENT
241   GstBuffer *current_buffer;
242 #endif /* GST_EXT_XV_ENHANCEMENT */
243 };
244
245 #ifdef GST_EXT_XV_ENHANCEMENT
246 #define MAX_PLANE_NUM          4
247 #endif /* GST_EXT_XV_ENHANCEMENT */
248
249 /**
250  * GstXvImageSink:
251  * @display_name: the name of the Display we want to render to
252  * @xcontext: our instance's #GstXContext
253  * @xwindow: the #GstXWindow we are rendering to
254  * @xvimage: internal #GstXvImage used to store incoming buffers and render when
255  * not using the buffer_alloc optimization mechanism
256  * @cur_image: a reference to the last #GstXvImage that was put to @xwindow. It
257  * is used when Expose events are received to redraw the latest video frame
258  * @event_thread: a thread listening for events on @xwindow and handling them
259  * @running: used to inform @event_thread if it should run/shutdown
260  * @fps_n: the framerate fraction numerator
261  * @fps_d: the framerate fraction denominator
262  * @x_lock: used to protect X calls as we are not using the XLib in threaded
263  * mode
264  * @flow_lock: used to protect data flow routines from external calls such as
265  * events from @event_thread or methods from the #GstXOverlay interface
266  * @par: used to override calculated pixel aspect ratio from @xcontext
267  * @pool_lock: used to protect the buffer pool
268  * @image_pool: a list of #GstXvImageBuffer that could be reused at next buffer
269  * allocation call
270  * @synchronous: used to store if XSynchronous should be used or not (for
271  * debugging purpose only)
272  * @keep_aspect: used to remember if reverse negotiation scaling should respect
273  * aspect ratio
274  * @handle_events: used to know if we should handle select XEvents or not
275  * @brightness: used to store the user settings for color balance brightness
276  * @contrast: used to store the user settings for color balance contrast
277  * @hue: used to store the user settings for color balance hue
278  * @saturation: used to store the user settings for color balance saturation
279  * @cb_changed: used to store if the color balance settings where changed
280  * @video_width: the width of incoming video frames in pixels
281  * @video_height: the height of incoming video frames in pixels
282  *
283  * The #GstXvImageSink data structure.
284  */
285 struct _GstXvImageSink {
286   /* Our element stuff */
287   GstVideoSink videosink;
288
289   char *display_name;
290   guint adaptor_no;
291
292   GstXContext *xcontext;
293   GstXWindow *xwindow;
294   GstXvImageBuffer *xvimage;
295   GstXvImageBuffer *cur_image;
296 #ifdef GST_EXT_XV_ENHANCEMENT
297   GstXvImageBuffer *last_image;
298   unsigned int last_image_vaddr[4];
299 #endif
300
301   GThread *event_thread;
302   gboolean running;
303
304   gint fps_n;
305   gint fps_d;
306
307   GMutex *x_lock;
308   GMutex *flow_lock;
309
310   /* object-set pixel aspect ratio */
311   GValue *par;
312
313   GMutex *pool_lock;
314   gboolean pool_invalid;
315   GSList *image_pool;
316
317   gboolean synchronous;
318   gboolean double_buffer;
319   gboolean keep_aspect;
320   gboolean redraw_border;
321   gboolean handle_events;
322   gboolean handle_expose;
323
324   gint brightness;
325   gint contrast;
326   gint hue;
327   gint saturation;
328   gboolean cb_changed;
329
330   /* size of incoming video, used as the size for XvImage */
331   guint video_width, video_height;
332
333   /* display sizes, used for clipping the image */
334   gint disp_x, disp_y;
335   gint disp_width, disp_height;
336
337   /* port attributes */
338   gboolean autopaint_colorkey;
339   gint colorkey;
340
341   gboolean draw_borders;
342
343   /* port features */
344   gboolean have_autopaint_colorkey;
345   gboolean have_colorkey;
346   gboolean have_double_buffer;
347
348   /* stream metadata */
349   gchar *media_title;
350
351   /* target video rectangle */
352   GstVideoRectangle render_rect;
353   gboolean have_render_rect;
354
355 #ifdef GST_EXT_XV_ENHANCEMENT
356   /* display mode */
357   gboolean is_pixmap;
358   gboolean xid_updated;
359   guint display_mode;
360   guint csc_range;
361   guint display_geometry_method;
362   guint flip;
363   guint rotate_angle;
364   gboolean visible;
365   gfloat zoom;
366   guint zoom_pos_x;
367   guint zoom_pos_y;
368   guint rotation;
369   guint rotate_cnt;
370   guint dst_roi_mode;
371   guint dst_roi_orientation;
372   GstVideoRectangle dst_roi;
373   XImage* xim_transparenter;
374   guint scr_w, scr_h;
375   gboolean stop_video;
376   gboolean is_hided;
377   GstVideoRectangle src_crop;
378
379   /* needed if fourcc is one if S series */
380   guint aligned_width;
381   guint aligned_height;
382   gint drm_fd;
383   void *bufmgr;
384
385   /* for using multiple pixmaps */
386   GstXPixmap *xpixmap[MAX_PIXMAP_NUM];
387   gint current_pixmap_idx;
388   get_pixmap_callback get_pixmap_cb;
389   void* get_pixmap_cb_user_data;
390
391   /* for sync displaying buffers */
392   GstXvImageDisplayingBuffer displaying_buffers[DISPLAYING_BUFFERS_MAX_NUM];
393   GMutex *display_buffer_lock;
394
395   /* buffer count check */
396   guint displayed_buffer_count;
397   guint displaying_buffer_count;
398
399   /* zero copy format */
400   gboolean is_zero_copy_format;
401
402   /* secure contents path */
403   gint secure_path;
404
405   /* DRM level */
406   gint drm_level;
407
408   /* if needed combine planes data */
409   gint need_combine_data;
410
411   /* display request time */
412   struct timeval request_time[DISPLAYING_BUFFERS_MAX_NUM];
413
414   /* last added buffer index */
415   gint last_added_buffer_index;
416
417   /* flush buffer */
418   GstXvImageFlushBuffer *flush_buffer;
419   gboolean enable_flush_buffer;
420
421 #endif /* GST_EXT_XV_ENHANCEMENT */
422 };
423
424 #ifdef GST_EXT_XV_ENHANCEMENT
425 /* max plane count *********************************************************/
426 #define MPLANE_IMGB_MAX_COUNT         (4)
427
428 /* image buffer definition ***************************************************
429
430     +------------------------------------------+ ---
431     |                                          |  ^
432     |     uaddr[], index[]                     |  |
433     |     +---------------------------+ ---    |  |
434     |     |                           |  ^     |  |
435     |     |<-------- width[] -------->|  |     |  |
436     |     |                           |  |     |  |
437     |     |                           |        |
438     |     |                           |height[]|elevation[]
439     |     |                           |        |
440     |     |                           |  |     |  |
441     |     |                           |  |     |  |
442     |     |                           |  v     |  |
443     |     +---------------------------+ ---    |  |
444     |                                          |  v
445     +------------------------------------------+ ---
446
447     |<----------------- stride[] ------------------>|
448 */
449 typedef struct _GstMultiPlaneImageBuffer GstMultiPlaneImageBuffer;
450 struct _GstMultiPlaneImageBuffer
451 {
452     GstBuffer buffer;
453
454     /* width of each image plane */
455     gint      width[MPLANE_IMGB_MAX_COUNT];
456     /* height of each image plane */
457     gint      height[MPLANE_IMGB_MAX_COUNT];
458     /* stride of each image plane */
459     gint      stride[MPLANE_IMGB_MAX_COUNT];
460     /* elevation of each image plane */
461     gint      elevation[MPLANE_IMGB_MAX_COUNT];
462     /* user space address of each image plane */
463     gpointer uaddr[MPLANE_IMGB_MAX_COUNT];
464     /* Index of real address of each image plane, if needs */
465     gpointer index[MPLANE_IMGB_MAX_COUNT];
466     /* left postion, if needs */
467     gint      x;
468     /* top position, if needs */
469     gint      y;
470     /* to align memory */
471     gint      __dummy2;
472     /* arbitrary data */
473     gint      data[16];
474 };
475 #endif /* GST_EXT_XV_ENHANCEMENT */
476
477 struct _GstXvImageSinkClass {
478   GstVideoSinkClass parent_class;
479 };
480
481 GType gst_xvimagesink_get_type(void);
482
483 G_END_DECLS
484
485 #endif /* __GST_XVIMAGESINK_H__ */