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