rpm: Remove MEncoder from rpm packaging
[mplayer/glamo.git] / libvo / vo_x11.c
blobbdf561cb5302134752c9a009a2681a74b9782043
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #include "video_out.h"
25 #include "video_out_internal.h"
26 #include "aspect.h"
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
32 #include <errno.h>
34 #include "x11_common.h"
36 #ifdef HAVE_SHM
37 #include <sys/ipc.h>
38 #include <sys/shm.h>
39 #include <X11/extensions/XShm.h>
41 static int Shmem_Flag;
43 //static int Quiet_Flag; Here also what is this for. It's used but isn't initialized?
44 static XShmSegmentInfo Shminfo[1];
45 static int gXErrorFlag;
46 static int CompletionType = -1;
47 #endif
49 #include "sub.h"
51 #include "libswscale/swscale.h"
52 #include "libmpcodecs/vf_scale.h"
53 #define MODE_RGB 0x1
54 #define MODE_BGR 0x2
56 #include "mp_msg.h"
58 static const vo_info_t info = {
59 "X11 ( XImage/Shm )",
60 "x11",
61 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
65 const LIBVO_EXTERN(x11)
66 /* private prototypes */
67 static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
68 unsigned char *src, unsigned char *srca,
69 int stride);
71 /* local data */
72 static unsigned char *ImageData;
73 //! original unaligned pointer for free
74 static unsigned char *ImageDataOrig;
76 /* X11 related variables */
77 static XImage *myximage = NULL;
78 static int depth, bpp;
79 static XWindowAttributes attribs;
81 static int int_pause;
83 static int Flip_Flag;
84 static int zoomFlag;
87 static uint32_t image_width;
88 static uint32_t image_height;
89 static uint32_t in_format;
90 static uint32_t out_format = 0;
91 static int out_offset;
92 static int srcW = -1;
93 static int srcH = -1;
95 static int old_vo_dwidth = -1;
96 static int old_vo_dheight = -1;
98 static void check_events(void)
100 int ret = vo_x11_check_events(mDisplay);
102 if (ret & VO_EVENT_RESIZE)
103 vo_x11_clearwindow(mDisplay, vo_window);
104 else if (ret & VO_EVENT_EXPOSE)
105 vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
106 myximage->height, 0);
107 if (ret & VO_EVENT_EXPOSE && int_pause)
108 flip_page();
111 static void draw_alpha_32(int x0, int y0, int w, int h, unsigned char *src,
112 unsigned char *srca, int stride)
114 vo_draw_alpha_rgb32(w, h, src, srca, stride,
115 ImageData + 4 * (y0 * image_width + x0),
116 4 * image_width);
119 static void draw_alpha_24(int x0, int y0, int w, int h, unsigned char *src,
120 unsigned char *srca, int stride)
122 vo_draw_alpha_rgb24(w, h, src, srca, stride,
123 ImageData + 3 * (y0 * image_width + x0),
124 3 * image_width);
127 static void draw_alpha_16(int x0, int y0, int w, int h, unsigned char *src,
128 unsigned char *srca, int stride)
130 vo_draw_alpha_rgb16(w, h, src, srca, stride,
131 ImageData + 2 * (y0 * image_width + x0),
132 2 * image_width);
135 static void draw_alpha_15(int x0, int y0, int w, int h, unsigned char *src,
136 unsigned char *srca, int stride)
138 vo_draw_alpha_rgb15(w, h, src, srca, stride,
139 ImageData + 2 * (y0 * image_width + x0),
140 2 * image_width);
143 static void draw_alpha_null(int x0, int y0, int w, int h,
144 unsigned char *src, unsigned char *srca,
145 int stride)
149 static struct SwsContext *swsContext = NULL;
150 static int dst_width;
151 extern int sws_flags;
153 static XVisualInfo vinfo;
155 static void getMyXImage(void)
157 #ifdef HAVE_SHM
158 if (mLocalDisplay && XShmQueryExtension(mDisplay))
159 Shmem_Flag = 1;
160 else
162 Shmem_Flag = 0;
163 mp_msg(MSGT_VO, MSGL_WARN,
164 "Shared memory not supported\nReverting to normal Xlib\n");
166 if (Shmem_Flag)
167 CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
169 if (Shmem_Flag)
171 myximage =
172 XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
173 &Shminfo[0], image_width, image_height);
174 if (myximage == NULL)
176 mp_msg(MSGT_VO, MSGL_WARN,
177 "Shared memory error,disabling ( Ximage error )\n");
178 goto shmemerror;
180 Shminfo[0].shmid = shmget(IPC_PRIVATE,
181 myximage->bytes_per_line *
182 myximage->height, IPC_CREAT | 0777);
183 if (Shminfo[0].shmid < 0)
185 XDestroyImage(myximage);
186 mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
187 //perror( strerror( errno ) );
188 mp_msg(MSGT_VO, MSGL_WARN,
189 "Shared memory error,disabling ( seg id error )\n");
190 goto shmemerror;
192 Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
194 if (Shminfo[0].shmaddr == ((char *) -1))
196 XDestroyImage(myximage);
197 if (Shminfo[0].shmaddr != ((char *) -1))
198 shmdt(Shminfo[0].shmaddr);
199 mp_msg(MSGT_VO, MSGL_WARN,
200 "Shared memory error,disabling ( address error )\n");
201 goto shmemerror;
203 myximage->data = Shminfo[0].shmaddr;
204 ImageData = (unsigned char *) myximage->data;
205 Shminfo[0].readOnly = False;
206 XShmAttach(mDisplay, &Shminfo[0]);
208 XSync(mDisplay, False);
210 if (gXErrorFlag)
212 XDestroyImage(myximage);
213 shmdt(Shminfo[0].shmaddr);
214 mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
215 gXErrorFlag = 0;
216 goto shmemerror;
217 } else
218 shmctl(Shminfo[0].shmid, IPC_RMID, 0);
221 static int firstTime = 1;
223 if (firstTime)
225 mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
226 firstTime = 0;
229 } else
231 shmemerror:
232 Shmem_Flag = 0;
233 #endif
234 myximage = XCreateImage(mDisplay, vinfo.visual, depth, ZPixmap,
235 0, NULL, image_width, image_height, 8, 0);
236 ImageDataOrig = malloc(myximage->bytes_per_line * image_height + 32);
237 myximage->data = ImageDataOrig + 16 - ((long)ImageDataOrig & 15);
238 memset(myximage->data, 0, myximage->bytes_per_line * image_height);
239 ImageData = myximage->data;
240 #ifdef HAVE_SHM
242 #endif
245 static void freeMyXImage(void)
247 #ifdef HAVE_SHM
248 if (Shmem_Flag)
250 XShmDetach(mDisplay, &Shminfo[0]);
251 XDestroyImage(myximage);
252 shmdt(Shminfo[0].shmaddr);
253 } else
254 #endif
256 myximage->data = ImageDataOrig;
257 XDestroyImage(myximage);
258 ImageDataOrig = NULL;
260 myximage = NULL;
261 ImageData = NULL;
264 #if HAVE_BIGENDIAN
265 #define BO_NATIVE MSBFirst
266 #define BO_NONNATIVE LSBFirst
267 #else
268 #define BO_NATIVE LSBFirst
269 #define BO_NONNATIVE MSBFirst
270 #endif
271 const struct fmt2Xfmtentry_s {
272 uint32_t mpfmt;
273 int byte_order;
274 unsigned red_mask;
275 unsigned green_mask;
276 unsigned blue_mask;
277 } fmt2Xfmt[] = {
278 {IMGFMT_RGB8, BO_NATIVE, 0x00000007, 0x00000038, 0x000000C0},
279 {IMGFMT_RGB8, BO_NONNATIVE, 0x00000007, 0x00000038, 0x000000C0},
280 {IMGFMT_BGR8, BO_NATIVE, 0x000000E0, 0x0000001C, 0x00000003},
281 {IMGFMT_BGR8, BO_NONNATIVE, 0x000000E0, 0x0000001C, 0x00000003},
282 {IMGFMT_RGB15, BO_NATIVE, 0x0000001F, 0x000003E0, 0x00007C00},
283 {IMGFMT_BGR15, BO_NATIVE, 0x00007C00, 0x000003E0, 0x0000001F},
284 {IMGFMT_RGB16, BO_NATIVE, 0x0000001F, 0x000007E0, 0x0000F800},
285 {IMGFMT_BGR16, BO_NATIVE, 0x0000F800, 0x000007E0, 0x0000001F},
286 {IMGFMT_RGB24, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
287 {IMGFMT_RGB24, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
288 {IMGFMT_BGR24, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
289 {IMGFMT_BGR24, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
290 {IMGFMT_RGB32, BO_NATIVE, 0x000000FF, 0x0000FF00, 0x00FF0000},
291 {IMGFMT_RGB32, BO_NONNATIVE, 0xFF000000, 0x00FF0000, 0x0000FF00},
292 {IMGFMT_BGR32, BO_NATIVE, 0x00FF0000, 0x0000FF00, 0x000000FF},
293 {IMGFMT_BGR32, BO_NONNATIVE, 0x0000FF00, 0x00FF0000, 0xFF000000},
294 {IMGFMT_ARGB, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
295 {IMGFMT_ARGB, LSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
296 {IMGFMT_ABGR, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
297 {IMGFMT_ABGR, LSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
298 {IMGFMT_RGBA, MSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
299 {IMGFMT_RGBA, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
300 {IMGFMT_BGRA, MSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
301 {IMGFMT_BGRA, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
302 {0, 0, 0, 0, 0}
305 static int config(uint32_t width, uint32_t height, uint32_t d_width,
306 uint32_t d_height, uint32_t flags, char *title,
307 uint32_t format)
309 // int screen;
311 // int interval, prefer_blank, allow_exp, nothing;
312 unsigned int fg, bg;
313 Colormap theCmap;
314 XSetWindowAttributes xswa;
315 unsigned long xswamask;
316 const struct fmt2Xfmtentry_s *fmte = fmt2Xfmt;
318 #ifdef CONFIG_XF86VM
319 int vm = flags & VOFLAG_MODESWITCHING;
320 #endif
321 Flip_Flag = flags & VOFLAG_FLIPPING;
322 zoomFlag = flags & VOFLAG_SWSCALE;
324 old_vo_dwidth = -1;
325 old_vo_dheight = -1;
327 if (!title)
328 title = "MPlayer X11 (XImage/Shm) render";
330 in_format = format;
331 srcW = width;
332 srcH = height;
334 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
335 depth = attribs.depth;
337 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
339 Visual *visual;
341 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
343 if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
344 (WinID > 0
345 && vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
346 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
348 /* set image size (which is indeed neither the input nor output size),
349 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
351 image_width = (width + 7) & (~7);
352 image_height = height;
355 #ifdef CONFIG_XF86VM
356 if (vm)
358 vo_vm_switch();
360 #endif
361 bg = WhitePixel(mDisplay, mScreen);
362 fg = BlackPixel(mDisplay, mScreen);
364 theCmap = vo_x11_create_colormap(&vinfo);
366 xswa.background_pixel = 0;
367 xswa.border_pixel = 0;
368 xswa.colormap = theCmap;
369 xswamask = CWBackPixel | CWBorderPixel | CWColormap;
371 #ifdef CONFIG_XF86VM
372 if (vm)
374 xswa.override_redirect = True;
375 xswamask |= CWOverrideRedirect;
377 #endif
379 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, vo_dwidth, vo_dheight,
380 flags, theCmap, "x11", title);
381 if (WinID > 0)
382 depth = vo_x11_update_geometry();
384 #ifdef CONFIG_XF86VM
385 if (vm)
387 /* Grab the mouse pointer in our window */
388 if (vo_grabpointer)
389 XGrabPointer(mDisplay, vo_window, True, 0,
390 GrabModeAsync, GrabModeAsync,
391 vo_window, None, CurrentTime);
392 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
394 #endif
397 if (myximage)
399 freeMyXImage();
400 sws_freeContext(swsContext);
402 getMyXImage();
404 while (fmte->mpfmt) {
405 int depth = IMGFMT_RGB_DEPTH(fmte->mpfmt);
406 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
407 => force depth to 16 so that only the color masks are used for the format check */
408 if (depth == 15)
409 depth = 16;
411 if (depth == myximage->bits_per_pixel &&
412 fmte->byte_order == myximage->byte_order &&
413 fmte->red_mask == myximage->red_mask &&
414 fmte->green_mask == myximage->green_mask &&
415 fmte->blue_mask == myximage->blue_mask)
416 break;
417 fmte++;
419 if (!fmte->mpfmt) {
420 mp_msg(MSGT_VO, MSGL_ERR,
421 "X server image format not supported, please contact the developers\n");
422 return -1;
424 out_format = fmte->mpfmt;
425 switch ((bpp = myximage->bits_per_pixel))
427 case 24:
428 draw_alpha_fnc = draw_alpha_24;
429 break;
430 case 32:
431 draw_alpha_fnc = draw_alpha_32;
432 break;
433 case 15:
434 case 16:
435 if (depth == 15)
436 draw_alpha_fnc = draw_alpha_15;
437 else
438 draw_alpha_fnc = draw_alpha_16;
439 break;
440 default:
441 draw_alpha_fnc = draw_alpha_null;
443 out_offset = 0;
444 // for these formats conversion is currently not support and
445 // we can easily "emulate" them.
446 if (out_format & 64 && (IMGFMT_IS_RGB(out_format) || IMGFMT_IS_BGR(out_format))) {
447 out_format &= ~64;
448 #if HAVE_BIGENDIAN
449 out_offset = 1;
450 #else
451 out_offset = -1;
452 #endif
455 /* always allocate swsContext as size could change between frames */
456 swsContext =
457 sws_getContextFromCmdLine(width, height, in_format, width, height,
458 out_format);
459 if (!swsContext)
460 return -1;
462 dst_width = width;
463 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
465 return 0;
468 static void Display_Image(XImage * myximage, uint8_t * ImageData)
470 int x = (vo_dwidth - dst_width) / 2;
471 int y = (vo_dheight - myximage->height) / 2;
473 // do not draw if the image needs rescaling
474 if ((old_vo_dwidth != vo_dwidth || old_vo_dheight != vo_dheight) && zoomFlag)
475 return;
477 if (WinID == 0) {
478 x = vo_dx;
479 y = vo_dy;
481 myximage->data += out_offset;
482 #ifdef HAVE_SHM
483 if (Shmem_Flag)
485 XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
486 0, 0,
487 x, y, dst_width,
488 myximage->height, True);
489 } else
490 #endif
492 XPutImage(mDisplay, vo_window, vo_gc, myximage,
493 0, 0,
494 x, y, dst_width,
495 myximage->height);
497 myximage->data -= out_offset;
500 static void draw_osd(void)
502 vo_draw_text(image_width, image_height, draw_alpha_fnc);
505 static void flip_page(void)
507 Display_Image(myximage, ImageData);
508 XSync(mDisplay, False);
511 static int draw_slice(uint8_t * src[], int stride[], int w, int h,
512 int x, int y)
514 uint8_t *dst[MP_MAX_PLANES] = {NULL};
515 int dstStride[MP_MAX_PLANES] = {0};
517 if ((old_vo_dwidth != vo_dwidth
518 || old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
520 int newW = vo_dwidth;
521 int newH = vo_dheight;
522 struct SwsContext *oldContext = swsContext;
524 old_vo_dwidth = vo_dwidth;
525 old_vo_dheight = vo_dheight;
527 if (vo_fs)
528 aspect(&newW, &newH, A_ZOOM);
529 if (sws_flags == 0)
530 newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
532 swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
533 newW, newH, out_format);
534 if (swsContext)
536 image_width = (newW + 7) & (~7);
537 image_height = newH;
539 freeMyXImage();
540 getMyXImage();
541 sws_freeContext(oldContext);
542 } else
544 swsContext = oldContext;
546 dst_width = newW;
549 dstStride[0] = image_width * ((bpp + 7) / 8);
550 dst[0] = ImageData;
551 if (Flip_Flag)
553 dst[0] += dstStride[0] * (image_height - 1);
554 dstStride[0] = -dstStride[0];
556 sws_scale(swsContext, src, stride, y, h, dst, dstStride);
557 return 0;
560 static int draw_frame(uint8_t * src[])
562 return VO_ERROR;
565 static uint32_t get_image(mp_image_t * mpi)
567 if (zoomFlag ||
568 !IMGFMT_IS_BGR(mpi->imgfmt) ||
569 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
570 ((mpi->type != MP_IMGTYPE_STATIC)
571 && (mpi->type != MP_IMGTYPE_TEMP))
572 || (mpi->flags & MP_IMGFLAG_PLANAR)
573 || (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
574 || (mpi->height != image_height))
575 return VO_FALSE;
577 if (Flip_Flag)
579 mpi->stride[0] = -image_width * ((bpp + 7) / 8);
580 mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
581 } else
583 mpi->stride[0] = image_width * ((bpp + 7) / 8);
584 mpi->planes[0] = ImageData;
586 mpi->flags |= MP_IMGFLAG_DIRECT;
588 return VO_TRUE;
591 static int query_format(uint32_t format)
593 mp_msg(MSGT_VO, MSGL_DBG2,
594 "vo_x11: query_format was called: %x (%s)\n", format,
595 vo_format_name(format));
596 if (IMGFMT_IS_BGR(format))
598 if (IMGFMT_BGR_DEPTH(format) <= 8)
599 return 0; // TODO 8bpp not yet fully implemented
600 if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
601 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
602 VFCAP_ACCEPT_STRIDE;
603 else
604 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
605 VFCAP_ACCEPT_STRIDE;
608 switch (format)
610 // case IMGFMT_BGR8:
611 // case IMGFMT_BGR15:
612 // case IMGFMT_BGR16:
613 // case IMGFMT_BGR24:
614 // case IMGFMT_BGR32:
615 // return 0x2;
616 // case IMGFMT_YUY2:
617 case IMGFMT_I420:
618 case IMGFMT_IYUV:
619 case IMGFMT_YV12:
620 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
622 return 0;
626 static void uninit(void)
628 if (!myximage)
629 return;
631 freeMyXImage();
633 #ifdef CONFIG_XF86VM
634 vo_vm_close();
635 #endif
637 zoomFlag = 0;
638 vo_x11_uninit();
640 sws_freeContext(swsContext);
643 static int preinit(const char *arg)
645 if (arg)
647 mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
648 return ENOSYS;
651 if (!vo_init())
652 return -1; // Can't open X11
653 return 0;
656 static int control(uint32_t request, void *data)
658 switch (request)
660 case VOCTRL_PAUSE:
661 return int_pause = 1;
662 case VOCTRL_RESUME:
663 return int_pause = 0;
664 case VOCTRL_QUERY_FORMAT:
665 return query_format(*((uint32_t *) data));
666 case VOCTRL_GET_IMAGE:
667 return get_image(data);
668 case VOCTRL_FULLSCREEN:
669 vo_x11_fullscreen();
670 vo_x11_clearwindow(mDisplay, vo_window);
671 return VO_TRUE;
672 case VOCTRL_SET_EQUALIZER:
674 struct voctrl_set_equalizer_args *args = data;
675 return vo_x11_set_equalizer(args->name, args->value);
677 case VOCTRL_GET_EQUALIZER:
679 struct voctrl_get_equalizer_args *args = data;
680 return vo_x11_get_equalizer(args->name, args->valueptr);
682 case VOCTRL_ONTOP:
683 vo_x11_ontop();
684 return VO_TRUE;
685 case VOCTRL_UPDATE_SCREENINFO:
686 update_xinerama_info();
687 return VO_TRUE;
689 return VO_NOTIMPL;