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.
24 #include "video_out.h"
25 #include "video_out_internal.h"
30 #include <X11/Xutil.h>
34 #include "x11_common.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;
51 #include "libswscale/swscale.h"
52 #include "libmpcodecs/vf_scale.h"
59 static const vo_info_t info
= {
62 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
66 const LIBVO_EXTERN(x11
)
67 /* private prototypes */
68 static void (*draw_alpha_fnc
) (int x0
, int y0
, int w
, int h
,
69 unsigned char *src
, unsigned char *srca
,
73 static unsigned char *ImageData
;
74 //! original unaligned pointer for free
75 static unsigned char *ImageDataOrig
;
77 /* X11 related variables */
78 static XImage
*myximage
= NULL
;
79 static int depth
, bpp
;
80 static XWindowAttributes attribs
;
88 static uint32_t image_width
;
89 static uint32_t image_height
;
90 static uint32_t in_format
;
91 static uint32_t out_format
= 0;
92 static int out_offset
;
96 static int old_vo_dwidth
= -1;
97 static int old_vo_dheight
= -1;
99 static void check_events(void)
101 int ret
= vo_x11_check_events(mDisplay
);
103 if (ret
& VO_EVENT_RESIZE
)
104 vo_x11_clearwindow(mDisplay
, vo_window
);
105 else if (ret
& VO_EVENT_EXPOSE
)
106 vo_x11_clearwindow_part(mDisplay
, vo_window
, myximage
->width
,
107 myximage
->height
, 0);
108 if (ret
& VO_EVENT_EXPOSE
&& int_pause
)
112 static void draw_alpha_32(int x0
, int y0
, int w
, int h
, unsigned char *src
,
113 unsigned char *srca
, int stride
)
115 vo_draw_alpha_rgb32(w
, h
, src
, srca
, stride
,
116 ImageData
+ 4 * (y0
* image_width
+ x0
),
120 static void draw_alpha_24(int x0
, int y0
, int w
, int h
, unsigned char *src
,
121 unsigned char *srca
, int stride
)
123 vo_draw_alpha_rgb24(w
, h
, src
, srca
, stride
,
124 ImageData
+ 3 * (y0
* image_width
+ x0
),
128 static void draw_alpha_16(int x0
, int y0
, int w
, int h
, unsigned char *src
,
129 unsigned char *srca
, int stride
)
131 vo_draw_alpha_rgb16(w
, h
, src
, srca
, stride
,
132 ImageData
+ 2 * (y0
* image_width
+ x0
),
136 static void draw_alpha_15(int x0
, int y0
, int w
, int h
, unsigned char *src
,
137 unsigned char *srca
, int stride
)
139 vo_draw_alpha_rgb15(w
, h
, src
, srca
, stride
,
140 ImageData
+ 2 * (y0
* image_width
+ x0
),
144 static void draw_alpha_null(int x0
, int y0
, int w
, int h
,
145 unsigned char *src
, unsigned char *srca
,
150 static struct SwsContext
*swsContext
= NULL
;
151 static int dst_width
;
152 extern int sws_flags
;
154 static XVisualInfo vinfo
;
156 static void getMyXImage(void)
159 if (mLocalDisplay
&& XShmQueryExtension(mDisplay
))
164 mp_msg(MSGT_VO
, MSGL_WARN
,
165 "Shared memory not supported\nReverting to normal Xlib\n");
168 CompletionType
= XShmGetEventBase(mDisplay
) + ShmCompletion
;
173 XShmCreateImage(mDisplay
, vinfo
.visual
, depth
, ZPixmap
, NULL
,
174 &Shminfo
[0], image_width
, image_height
);
175 if (myximage
== NULL
)
177 mp_msg(MSGT_VO
, MSGL_WARN
,
178 "Shared memory error,disabling ( Ximage error )\n");
181 Shminfo
[0].shmid
= shmget(IPC_PRIVATE
,
182 myximage
->bytes_per_line
*
183 myximage
->height
, IPC_CREAT
| 0777);
184 if (Shminfo
[0].shmid
< 0)
186 XDestroyImage(myximage
);
187 mp_msg(MSGT_VO
, MSGL_V
, "%s\n", strerror(errno
));
188 //perror( strerror( errno ) );
189 mp_msg(MSGT_VO
, MSGL_WARN
,
190 "Shared memory error,disabling ( seg id error )\n");
193 Shminfo
[0].shmaddr
= (char *) shmat(Shminfo
[0].shmid
, 0, 0);
195 if (Shminfo
[0].shmaddr
== ((char *) -1))
197 XDestroyImage(myximage
);
198 if (Shminfo
[0].shmaddr
!= ((char *) -1))
199 shmdt(Shminfo
[0].shmaddr
);
200 mp_msg(MSGT_VO
, MSGL_WARN
,
201 "Shared memory error,disabling ( address error )\n");
204 myximage
->data
= Shminfo
[0].shmaddr
;
205 ImageData
= (unsigned char *) myximage
->data
;
206 Shminfo
[0].readOnly
= False
;
207 XShmAttach(mDisplay
, &Shminfo
[0]);
209 XSync(mDisplay
, False
);
213 XDestroyImage(myximage
);
214 shmdt(Shminfo
[0].shmaddr
);
215 mp_msg(MSGT_VO
, MSGL_WARN
, "Shared memory error,disabling.\n");
219 shmctl(Shminfo
[0].shmid
, IPC_RMID
, 0);
222 static int firstTime
= 1;
226 mp_msg(MSGT_VO
, MSGL_V
, "Sharing memory.\n");
235 myximage
= XCreateImage(mDisplay
, vinfo
.visual
, depth
, ZPixmap
,
236 0, NULL
, image_width
, image_height
, 8, 0);
237 ImageDataOrig
= malloc(myximage
->bytes_per_line
* image_height
+ 32);
238 myximage
->data
= ImageDataOrig
+ 16 - ((long)ImageDataOrig
& 15);
239 memset(myximage
->data
, 0, myximage
->bytes_per_line
* image_height
);
240 ImageData
= myximage
->data
;
246 static void freeMyXImage(void)
251 XShmDetach(mDisplay
, &Shminfo
[0]);
252 XDestroyImage(myximage
);
253 shmdt(Shminfo
[0].shmaddr
);
257 myximage
->data
= ImageDataOrig
;
258 XDestroyImage(myximage
);
259 ImageDataOrig
= NULL
;
266 #define BO_NATIVE MSBFirst
267 #define BO_NONNATIVE LSBFirst
269 #define BO_NATIVE LSBFirst
270 #define BO_NONNATIVE MSBFirst
272 const struct fmt2Xfmtentry_s
{
279 {IMGFMT_RGB8
, BO_NATIVE
, 0x00000007, 0x00000038, 0x000000C0},
280 {IMGFMT_RGB8
, BO_NONNATIVE
, 0x00000007, 0x00000038, 0x000000C0},
281 {IMGFMT_BGR8
, BO_NATIVE
, 0x000000E0, 0x0000001C, 0x00000003},
282 {IMGFMT_BGR8
, BO_NONNATIVE
, 0x000000E0, 0x0000001C, 0x00000003},
283 {IMGFMT_RGB15
, BO_NATIVE
, 0x0000001F, 0x000003E0, 0x00007C00},
284 {IMGFMT_BGR15
, BO_NATIVE
, 0x00007C00, 0x000003E0, 0x0000001F},
285 {IMGFMT_RGB16
, BO_NATIVE
, 0x0000001F, 0x000007E0, 0x0000F800},
286 {IMGFMT_BGR16
, BO_NATIVE
, 0x0000F800, 0x000007E0, 0x0000001F},
287 {IMGFMT_RGB24
, MSBFirst
, 0x00FF0000, 0x0000FF00, 0x000000FF},
288 {IMGFMT_RGB24
, LSBFirst
, 0x000000FF, 0x0000FF00, 0x00FF0000},
289 {IMGFMT_BGR24
, MSBFirst
, 0x000000FF, 0x0000FF00, 0x00FF0000},
290 {IMGFMT_BGR24
, LSBFirst
, 0x00FF0000, 0x0000FF00, 0x000000FF},
291 {IMGFMT_RGB32
, BO_NATIVE
, 0x000000FF, 0x0000FF00, 0x00FF0000},
292 {IMGFMT_RGB32
, BO_NONNATIVE
, 0xFF000000, 0x00FF0000, 0x0000FF00},
293 {IMGFMT_BGR32
, BO_NATIVE
, 0x00FF0000, 0x0000FF00, 0x000000FF},
294 {IMGFMT_BGR32
, BO_NONNATIVE
, 0x0000FF00, 0x00FF0000, 0xFF000000},
295 {IMGFMT_ARGB
, MSBFirst
, 0x00FF0000, 0x0000FF00, 0x000000FF},
296 {IMGFMT_ARGB
, LSBFirst
, 0x0000FF00, 0x00FF0000, 0xFF000000},
297 {IMGFMT_ABGR
, MSBFirst
, 0x000000FF, 0x0000FF00, 0x00FF0000},
298 {IMGFMT_ABGR
, LSBFirst
, 0xFF000000, 0x00FF0000, 0x0000FF00},
299 {IMGFMT_RGBA
, MSBFirst
, 0xFF000000, 0x00FF0000, 0x0000FF00},
300 {IMGFMT_RGBA
, LSBFirst
, 0x000000FF, 0x0000FF00, 0x00FF0000},
301 {IMGFMT_BGRA
, MSBFirst
, 0x0000FF00, 0x00FF0000, 0xFF000000},
302 {IMGFMT_BGRA
, LSBFirst
, 0x00FF0000, 0x0000FF00, 0x000000FF},
306 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
307 uint32_t d_height
, uint32_t flags
, char *title
,
312 // int interval, prefer_blank, allow_exp, nothing;
315 XSetWindowAttributes xswa
;
316 unsigned long xswamask
;
317 const struct fmt2Xfmtentry_s
*fmte
= fmt2Xfmt
;
320 int vm
= flags
& VOFLAG_MODESWITCHING
;
322 Flip_Flag
= flags
& VOFLAG_FLIPPING
;
323 zoomFlag
= flags
& VOFLAG_SWSCALE
;
329 title
= "MPlayer X11 (XImage/Shm) render";
335 XGetWindowAttributes(mDisplay
, mRootWin
, &attribs
);
336 depth
= attribs
.depth
;
338 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32)
342 depth
= vo_find_depth_from_visuals(mDisplay
, mScreen
, &visual
);
344 if (!XMatchVisualInfo(mDisplay
, mScreen
, depth
, DirectColor
, &vinfo
) ||
346 && vinfo
.visualid
!= XVisualIDFromVisual(attribs
.visual
)))
347 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
349 /* set image size (which is indeed neither the input nor output size),
350 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
352 image_width
= (width
+ 7) & (~7);
353 image_height
= height
;
362 bg
= WhitePixel(mDisplay
, mScreen
);
363 fg
= BlackPixel(mDisplay
, mScreen
);
365 theCmap
= vo_x11_create_colormap(&vinfo
);
367 xswa
.background_pixel
= 0;
368 xswa
.border_pixel
= 0;
369 xswa
.colormap
= theCmap
;
370 xswamask
= CWBackPixel
| CWBorderPixel
| CWColormap
;
375 xswa
.override_redirect
= True
;
376 xswamask
|= CWOverrideRedirect
;
380 vo_x11_create_vo_window(&vinfo
, vo_dx
, vo_dy
, vo_dwidth
, vo_dheight
,
381 flags
, theCmap
, "x11", title
);
383 depth
= vo_x11_update_geometry();
388 /* Grab the mouse pointer in our window */
390 XGrabPointer(mDisplay
, vo_window
, True
, 0,
391 GrabModeAsync
, GrabModeAsync
,
392 vo_window
, None
, CurrentTime
);
393 XSetInputFocus(mDisplay
, vo_window
, RevertToNone
, CurrentTime
);
401 sws_freeContext(swsContext
);
405 while (fmte
->mpfmt
) {
406 int depth
= IMGFMT_RGB_DEPTH(fmte
->mpfmt
);
407 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
408 => force depth to 16 so that only the color masks are used for the format check */
412 if (depth
== myximage
->bits_per_pixel
&&
413 fmte
->byte_order
== myximage
->byte_order
&&
414 fmte
->red_mask
== myximage
->red_mask
&&
415 fmte
->green_mask
== myximage
->green_mask
&&
416 fmte
->blue_mask
== myximage
->blue_mask
)
421 mp_msg(MSGT_VO
, MSGL_ERR
,
422 "X server image format not supported, please contact the developers\n");
425 out_format
= fmte
->mpfmt
;
426 switch ((bpp
= myximage
->bits_per_pixel
))
429 draw_alpha_fnc
= draw_alpha_24
;
432 draw_alpha_fnc
= draw_alpha_32
;
437 draw_alpha_fnc
= draw_alpha_15
;
439 draw_alpha_fnc
= draw_alpha_16
;
442 draw_alpha_fnc
= draw_alpha_null
;
445 // for these formats conversion is currently not support and
446 // we can easily "emulate" them.
447 if (out_format
& 64 && (IMGFMT_IS_RGB(out_format
) || IMGFMT_IS_BGR(out_format
))) {
456 /* always allocate swsContext as size could change between frames */
458 sws_getContextFromCmdLine(width
, height
, in_format
, width
, height
,
464 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
469 static void Display_Image(XImage
* myximage
, uint8_t * ImageData
)
471 int x
= (vo_dwidth
- dst_width
) / 2;
472 int y
= (vo_dheight
- myximage
->height
) / 2;
474 // do not draw if the image needs rescaling
475 if ((old_vo_dwidth
!= vo_dwidth
|| old_vo_dheight
!= vo_dheight
) && zoomFlag
)
482 myximage
->data
+= out_offset
;
486 XShmPutImage(mDisplay
, vo_window
, vo_gc
, myximage
,
489 myximage
->height
, True
);
493 XPutImage(mDisplay
, vo_window
, vo_gc
, myximage
,
498 myximage
->data
-= out_offset
;
501 static void draw_osd(void)
503 vo_draw_text(image_width
, image_height
, draw_alpha_fnc
);
506 static void flip_page(void)
508 Display_Image(myximage
, ImageData
);
509 XSync(mDisplay
, False
);
512 static int draw_slice(uint8_t * src
[], int stride
[], int w
, int h
,
515 uint8_t *dst
[MP_MAX_PLANES
] = {NULL
};
516 int dstStride
[MP_MAX_PLANES
] = {0};
518 if ((old_vo_dwidth
!= vo_dwidth
519 || old_vo_dheight
!= vo_dheight
) /*&& y==0 */ && zoomFlag
)
521 int newW
= vo_dwidth
;
522 int newH
= vo_dheight
;
523 struct SwsContext
*oldContext
= swsContext
;
525 old_vo_dwidth
= vo_dwidth
;
526 old_vo_dheight
= vo_dheight
;
529 aspect(&newW
, &newH
, A_ZOOM
);
531 newW
&= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
533 swsContext
= sws_getContextFromCmdLine(srcW
, srcH
, in_format
,
534 newW
, newH
, out_format
);
537 image_width
= (newW
+ 7) & (~7);
542 sws_freeContext(oldContext
);
545 swsContext
= oldContext
;
550 dstStride
[0] = image_width
* ((bpp
+ 7) / 8);
554 dst
[0] += dstStride
[0] * (image_height
- 1);
555 dstStride
[0] = -dstStride
[0];
557 sws_scale(swsContext
, src
, stride
, y
, h
, dst
, dstStride
);
561 static int draw_frame(uint8_t * src
[])
566 static uint32_t get_image(mp_image_t
* mpi
)
569 !IMGFMT_IS_BGR(mpi
->imgfmt
) ||
570 (IMGFMT_BGR_DEPTH(mpi
->imgfmt
) != vo_depthonscreen
) ||
571 ((mpi
->type
!= MP_IMGTYPE_STATIC
)
572 && (mpi
->type
!= MP_IMGTYPE_TEMP
))
573 || (mpi
->flags
& MP_IMGFLAG_PLANAR
)
574 || (mpi
->flags
& MP_IMGFLAG_YUV
) || (mpi
->width
!= image_width
)
575 || (mpi
->height
!= image_height
))
580 mpi
->stride
[0] = -image_width
* ((bpp
+ 7) / 8);
581 mpi
->planes
[0] = ImageData
- mpi
->stride
[0] * (image_height
- 1);
584 mpi
->stride
[0] = image_width
* ((bpp
+ 7) / 8);
585 mpi
->planes
[0] = ImageData
;
587 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
592 static int query_format(uint32_t format
)
594 mp_msg(MSGT_VO
, MSGL_DBG2
,
595 "vo_x11: query_format was called: %x (%s)\n", format
,
596 vo_format_name(format
));
597 if (IMGFMT_IS_BGR(format
))
599 if (IMGFMT_BGR_DEPTH(format
) <= 8)
600 return 0; // TODO 8bpp not yet fully implemented
601 if (IMGFMT_BGR_DEPTH(format
) == vo_depthonscreen
)
602 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_OSD
| VFCAP_SWSCALE
| VFCAP_FLIP
|
605 return VFCAP_CSP_SUPPORTED
| VFCAP_OSD
| VFCAP_SWSCALE
| VFCAP_FLIP
|
612 // case IMGFMT_BGR15:
613 // case IMGFMT_BGR16:
614 // case IMGFMT_BGR24:
615 // case IMGFMT_BGR32:
621 return VFCAP_CSP_SUPPORTED
| VFCAP_OSD
| VFCAP_SWSCALE
| VFCAP_ACCEPT_STRIDE
;
627 static void uninit(void)
641 sws_freeContext(swsContext
);
644 static int preinit(const char *arg
)
648 mp_msg(MSGT_VO
, MSGL_ERR
, "vo_x11: Unknown subdevice: %s\n", arg
);
653 return -1; // Can't open X11
657 static int control(uint32_t request
, void *data
)
662 return int_pause
= 1;
664 return int_pause
= 0;
665 case VOCTRL_QUERY_FORMAT
:
666 return query_format(*((uint32_t *) data
));
667 case VOCTRL_GET_IMAGE
:
668 return get_image(data
);
669 case VOCTRL_FULLSCREEN
:
671 vo_x11_clearwindow(mDisplay
, vo_window
);
673 case VOCTRL_SET_EQUALIZER
:
675 struct voctrl_set_equalizer_args
*args
= data
;
676 return vo_x11_set_equalizer(args
->name
, args
->value
);
678 case VOCTRL_GET_EQUALIZER
:
680 struct voctrl_get_equalizer_args
*args
= data
;
681 return vo_x11_get_equalizer(args
->name
, args
->valueptr
);
686 case VOCTRL_UPDATE_SCREENINFO
:
687 update_xinerama_info();