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