1 /*
2 * This file is part of MPlayer.
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "config.h"
24 #include "video_out.h"
25 #define NO_DRAW_FRAME
26 #include "video_out_internal.h"
27 #include "libmpcodecs/vf.h"
28 #include "aspect.h"
29
30
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33
34 #include <errno.h>
35
36 #include "x11_common.h"
37
38 #ifdef HAVE_SHM
39 #include <sys/ipc.h>
40 #include <sys/shm.h>
41 #include <X11/extensions/XShm.h>
42
43 static int Shmem_Flag;
44
45 //static int Quiet_Flag; Here also what is this for. It's used but isn't initialized?
46 static XShmSegmentInfo Shminfo[1];
47 static int gXErrorFlag;
48 static int CompletionType = -1;
49 #endif
50
51 #include "sub/sub.h"
52
53 #include "libswscale/swscale.h"
54 #include "libmpcodecs/vf_scale.h"
55 #define MODE_RGB 0x1
56 #define MODE_BGR 0x2
57
58 #include "mp_msg.h"
59 #include "help_mp.h"
60
61 static const vo_info_t info = {
62 "X11 ( XImage/Shm )",
63 "x11",
64 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
65 ""
66 };
67
68 const LIBVO_EXTERN(x11)
69 /* private prototypes */
70 static vo_draw_alpha_func draw_alpha_func;
71
72 /* local data */
73 static unsigned char *ImageData;
74 //! original unaligned pointer for free
75 static unsigned char *ImageDataOrig;
76
77 /* X11 related variables */
78 static XImage *myximage = NULL;
79 static int depth, bpp;
80 static XWindowAttributes attribs;
81
82 static int int_pause;
83
84 static int Flip_Flag;
85 static int zoomFlag;
86
87
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;
93 static int srcW = -1;
94 static int srcH = -1;
95
96 static int old_vo_dwidth = -1;
97 static int old_vo_dheight = -1;
98
check_events(void)99 static void check_events(void)
100 {
101 int ret = vo_x11_check_events(mDisplay);
102
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, vo_fs);
108 if (ret & VO_EVENT_EXPOSE && int_pause)
109 flip_page();
110 }
111
draw_alpha(int x0,int y0,int w,int h,unsigned char * src,unsigned char * srca,int stride)112 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
113 unsigned char *srca, int stride)
114 {
115 int bpp = pixel_stride(out_format);
116 draw_alpha_func(w, h, src, srca, stride,
117 ImageData + bpp * (y0 * image_width + x0),
118 bpp * image_width);
119 }
120
121 static struct SwsContext *swsContext = NULL;
122 static int dst_width;
123
124 static XVisualInfo vinfo;
125
getMyXImage(void)126 static void getMyXImage(void)
127 {
128 #ifdef HAVE_SHM
129 if (mLocalDisplay && XShmQueryExtension(mDisplay))
130 Shmem_Flag = 1;
131 else
132 {
133 Shmem_Flag = 0;
134 mp_msg(MSGT_VO, MSGL_WARN,
135 "Shared memory not supported\nReverting to normal Xlib\n");
136 }
137 if (Shmem_Flag)
138 CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
139
140 if (Shmem_Flag)
141 {
142 myximage =
143 XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
144 &Shminfo[0], image_width, image_height);
145 if (myximage == NULL)
146 {
147 mp_msg(MSGT_VO, MSGL_WARN,
148 "Shared memory error,disabling ( Ximage error )\n");
149 goto shmemerror;
150 }
151 Shminfo[0].shmid = shmget(IPC_PRIVATE,
152 myximage->bytes_per_line *
153 myximage->height, IPC_CREAT | 0777);
154 if (Shminfo[0].shmid < 0)
155 {
156 XDestroyImage(myximage);
157 mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
158 //perror( strerror( errno ) );
159 mp_msg(MSGT_VO, MSGL_WARN,
160 "Shared memory error,disabling ( seg id error )\n");
161 goto shmemerror;
162 }
163 Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
164
165 if (Shminfo[0].shmaddr == ((char *) -1))
166 {
167 XDestroyImage(myximage);
168 if (Shminfo[0].shmaddr != ((char *) -1))
169 shmdt(Shminfo[0].shmaddr);
170 mp_msg(MSGT_VO, MSGL_WARN,
171 "Shared memory error,disabling ( address error )\n");
172 goto shmemerror;
173 }
174 myximage->data = Shminfo[0].shmaddr;
175 ImageData = (unsigned char *) myximage->data;
176 Shminfo[0].readOnly = False;
177 XShmAttach(mDisplay, &Shminfo[0]);
178
179 XSync(mDisplay, False);
180
181 if (gXErrorFlag)
182 {
183 XDestroyImage(myximage);
184 shmdt(Shminfo[0].shmaddr);
185 mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
186 gXErrorFlag = 0;
187 goto shmemerror;
188 } else
189 shmctl(Shminfo[0].shmid, IPC_RMID, 0);
190
191 {
192 static int firstTime = 1;
193
194 if (firstTime)
195 {
196 mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
197 firstTime = 0;
198 }
199 }
200 } else
201 {
202 shmemerror:
203 Shmem_Flag = 0;
204 #endif
205 myximage = XCreateImage(mDisplay, vinfo.visual, depth, ZPixmap,
206 0, NULL, image_width, image_height, 8, 0);
207 ImageDataOrig = malloc(myximage->bytes_per_line * image_height + 32);
208 myximage->data = ImageDataOrig + 16 - ((long)ImageDataOrig & 15);
209 memset(myximage->data, 0, myximage->bytes_per_line * image_height);
210 ImageData = myximage->data;
211 #ifdef HAVE_SHM
212 }
213 #endif
214 }
215
freeMyXImage(void)216 static void freeMyXImage(void)
217 {
218 #ifdef HAVE_SHM
219 if (Shmem_Flag)
220 {
221 XShmDetach(mDisplay, &Shminfo[0]);
222 XDestroyImage(myximage);
223 shmdt(Shminfo[0].shmaddr);
224 } else
225 #endif
226 {
227 myximage->data = ImageDataOrig;
228 XDestroyImage(myximage);
229 ImageDataOrig = NULL;
230 }
231 myximage = NULL;
232 ImageData = NULL;
233 }
234
235 #if HAVE_BIGENDIAN
236 #define BO_NATIVE MSBFirst
237 #define BO_NONNATIVE LSBFirst
238 #else
239 #define BO_NATIVE LSBFirst
240 #define BO_NONNATIVE MSBFirst
241 #endif
242 const struct fmt2Xfmtentry_s {
243 uint32_t mpfmt;
244 int byte_order;
245 unsigned red_mask;
246 unsigned green_mask;
247 unsigned blue_mask;
248 } fmt2Xfmt[] = {
249 {IMGFMT_RGB8, BO_NATIVE, 0x00000007, 0x00000038, 0x000000C0},
250 {IMGFMT_RGB8, BO_NONNATIVE, 0x00000007, 0x00000038, 0x000000C0},
251 {IMGFMT_BGR8, BO_NATIVE, 0x000000E0, 0x0000001C, 0x00000003},
252 {IMGFMT_BGR8, BO_NONNATIVE, 0x000000E0, 0x0000001C, 0x00000003},
253 {IMGFMT_RGB15, BO_NATIVE, 0x0000001F, 0x000003E0, 0x00007C00},
254 {IMGFMT_BGR15, BO_NATIVE, 0x00007C00, 0x000003E0, 0x0000001F},
255 {IMGFMT_RGB16, BO_NATIVE, 0x0000001F, 0x000007E0, 0x0000F800},
256 {IMGFMT_BGR16, BO_NATIVE, 0x0000F800, 0x000007E0, 0x0000001F},
257 {IMGFMT_RGB24, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
258 {IMGFMT_RGB24, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
259 {IMGFMT_BGR24, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
260 {IMGFMT_BGR24, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
261 {IMGFMT_RGB32, BO_NATIVE, 0x000000FF, 0x0000FF00, 0x00FF0000},
262 {IMGFMT_RGB32, BO_NONNATIVE, 0xFF000000, 0x00FF0000, 0x0000FF00},
263 {IMGFMT_BGR32, BO_NATIVE, 0x00FF0000, 0x0000FF00, 0x000000FF},
264 {IMGFMT_BGR32, BO_NONNATIVE, 0x0000FF00, 0x00FF0000, 0xFF000000},
265 {IMGFMT_ARGB, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
266 {IMGFMT_ARGB, LSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
267 {IMGFMT_ABGR, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
268 {IMGFMT_ABGR, LSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
269 {IMGFMT_RGBA, MSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
270 {IMGFMT_RGBA, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
271 {IMGFMT_BGRA, MSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
272 {IMGFMT_BGRA, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
273 {0, 0, 0, 0, 0}
274 };
275
config(uint32_t width,uint32_t height,uint32_t d_width,uint32_t d_height,uint32_t flags,char * title,uint32_t format)276 static int config(uint32_t width, uint32_t height, uint32_t d_width,
277 uint32_t d_height, uint32_t flags, char *title,
278 uint32_t format)
279 {
280 // int screen;
281
282 // int interval, prefer_blank, allow_exp, nothing;
283 Colormap theCmap;
284 const struct fmt2Xfmtentry_s *fmte = fmt2Xfmt;
285
286 #ifdef CONFIG_XF86VM
287 int vm = flags & VOFLAG_MODESWITCHING;
288 #endif
289 Flip_Flag = flags & VOFLAG_FLIPPING;
290 zoomFlag = flags & VOFLAG_SWSCALE;
291
292 old_vo_dwidth = -1;
293 old_vo_dheight = -1;
294
295 int_pause = 0;
296 if (!title)
297 title = "MPlayer X11 (XImage/Shm) render";
298
299 in_format = format;
300 srcW = width;
301 srcH = height;
302
303 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
304 depth = attribs.depth;
305
306 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
307 {
308 Visual *visual;
309
310 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
311 }
312 if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
313 (WinID > 0
314 && vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
315 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
316
317 /* set image size (which is indeed neither the input nor output size),
318 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
319 */
320 image_width = (width + 7) & (~7);
321 image_height = height;
322
323 {
324 #ifdef CONFIG_XF86VM
325 if (vm)
326 {
327 vo_vm_switch();
328 }
329 #endif
330
331 theCmap = vo_x11_create_colormap(&vinfo);
332
333 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, vo_dwidth, vo_dheight,
334 flags, theCmap, "x11", title);
335 if (WinID > 0)
336 depth = vo_x11_update_geometry();
337
338 #ifdef CONFIG_XF86VM
339 if (vm)
340 {
341 /* Grab the mouse pointer in our window */
342 if (vo_grabpointer)
343 XGrabPointer(mDisplay, vo_window, True, 0,
344 GrabModeAsync, GrabModeAsync,
345 vo_window, None, CurrentTime);
346 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
347 }
348 #endif
349 }
350
351 if (myximage)
352 {
353 freeMyXImage();
354 sws_freeContext(swsContext);
355 }
356 getMyXImage();
357
358 while (fmte->mpfmt) {
359 int depth = IMGFMT_RGB_DEPTH(fmte->mpfmt);
360 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
361 => force depth to 16 so that only the color masks are used for the format check */
362 if (depth == 15)
363 depth = 16;
364
365 if (depth == myximage->bits_per_pixel &&
366 fmte->byte_order == myximage->byte_order &&
367 fmte->red_mask == myximage->red_mask &&
368 fmte->green_mask == myximage->green_mask &&
369 fmte->blue_mask == myximage->blue_mask)
370 break;
371 fmte++;
372 }
373 if (!fmte->mpfmt) {
374 mp_msg(MSGT_VO, MSGL_ERR,
375 "X server image format not supported, please contact the developers\n");
376 return -1;
377 }
378 out_format = fmte->mpfmt;
379 draw_alpha_func = vo_get_draw_alpha(out_format);
380
381 bpp = myximage->bits_per_pixel;
382 out_offset = 0;
383 // We can easily "emulate" non-native RGB32 and BGR32
384 if (out_format == (IMGFMT_BGR32 | 128) || out_format == (IMGFMT_RGB32 | 128)) {
385 out_format &= ~128;
386 #if HAVE_BIGENDIAN
387 out_offset = 1;
388 #else
389 out_offset = -1;
390 #endif
391 }
392
393 /* always allocate swsContext as size could change between frames */
394 swsContext =
395 sws_getContextFromCmdLine(width, height, in_format, width, height,
396 out_format);
397 if (!swsContext)
398 return -1;
399
400 dst_width = width;
401 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
402
403 return 0;
404 }
405
Display_Image(XImage * myximage,uint8_t * ImageData)406 static void Display_Image(XImage * myximage, uint8_t * ImageData)
407 {
408 int x = (vo_dwidth - dst_width) / 2;
409 int y = (vo_dheight - myximage->height) / 2;
410
411 // do not draw if the image needs rescaling
412 if ((old_vo_dwidth != vo_dwidth || old_vo_dheight != vo_dheight) && zoomFlag)
413 return;
414
415 if (WinID == 0) {
416 x = vo_dx;
417 y = vo_dy;
418 }
419 myximage->data += out_offset;
420 #ifdef HAVE_SHM
421 if (Shmem_Flag)
422 {
423 XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
424 0, 0,
425 x, y, dst_width,
426 myximage->height, True);
427 } else
428 #endif
429 {
430 XPutImage(mDisplay, vo_window, vo_gc, myximage,
431 0, 0,
432 x, y, dst_width,
433 myximage->height);
434 }
435 myximage->data -= out_offset;
436 }
437
draw_osd(void)438 static void draw_osd(void)
439 {
440 if (draw_alpha_func)
441 vo_draw_text(image_width, image_height, draw_alpha);
442 }
443
flip_page(void)444 static void flip_page(void)
445 {
446 Display_Image(myximage, ImageData);
447 XSync(mDisplay, False);
448 }
449
draw_slice(uint8_t * src[],int stride[],int w,int h,int x,int y)450 static int draw_slice(uint8_t * src[], int stride[], int w, int h,
451 int x, int y)
452 {
453 uint8_t *dst[MP_MAX_PLANES] = {NULL};
454 int dstStride[MP_MAX_PLANES] = {0};
455
456 if ((old_vo_dwidth != vo_dwidth
457 || old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
458 {
459 int newW = vo_dwidth;
460 int newH = vo_dheight;
461 struct SwsContext *oldContext = swsContext;
462
463 old_vo_dwidth = vo_dwidth;
464 old_vo_dheight = vo_dheight;
465
466 if (vo_fs)
467 aspect(&newW, &newH, A_ZOOM);
468 if (sws_flags == 0)
469 newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
470
471 swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
472 newW, newH, out_format);
473 if (swsContext)
474 {
475 image_width = (newW + 7) & (~7);
476 image_height = newH;
477
478 freeMyXImage();
479 getMyXImage();
480 sws_freeContext(oldContext);
481 } else
482 {
483 swsContext = oldContext;
484 }
485 dst_width = newW;
486 }
487
488 dstStride[0] = image_width * ((bpp + 7) / 8);
489 dst[0] = ImageData;
490 if (Flip_Flag)
491 {
492 dst[0] += dstStride[0] * (image_height - 1);
493 dstStride[0] = -dstStride[0];
494 }
495 sws_scale(swsContext, src, stride, y, h, dst, dstStride);
496 return 0;
497 }
498
get_image(mp_image_t * mpi)499 static uint32_t get_image(mp_image_t * mpi)
500 {
501 if (zoomFlag ||
502 !IMGFMT_IS_BGR(mpi->imgfmt) ||
503 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
504 ((mpi->type != MP_IMGTYPE_STATIC)
505 && (mpi->type != MP_IMGTYPE_TEMP))
506 || (mpi->flags & MP_IMGFLAG_PLANAR)
507 || (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
508 || (mpi->height != image_height))
509 return VO_FALSE;
510
511 if (Flip_Flag)
512 {
513 mpi->stride[0] = -image_width * ((bpp + 7) / 8);
514 mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
515 } else
516 {
517 mpi->stride[0] = image_width * ((bpp + 7) / 8);
518 mpi->planes[0] = ImageData;
519 }
520 mpi->flags |= MP_IMGFLAG_DIRECT;
521
522 return VO_TRUE;
523 }
524
query_format(uint32_t format)525 static int query_format(uint32_t format)
526 {
527 mp_msg(MSGT_VO, MSGL_DBG2,
528 "vo_x11: query_format was called: %x (%s)\n", format,
529 vo_format_name(format));
530 if (IMGFMT_IS_BGR(format))
531 {
532 if (IMGFMT_BGR_DEPTH(format) <= 8)
533 return 0; // TODO 8bpp not yet fully implemented
534 if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
535 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
536 VFCAP_ACCEPT_STRIDE;
537 else
538 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
539 VFCAP_ACCEPT_STRIDE;
540 }
541
542 switch (format)
543 {
544 // case IMGFMT_BGR8:
545 // case IMGFMT_BGR15:
546 // case IMGFMT_BGR16:
547 // case IMGFMT_BGR24:
548 // case IMGFMT_BGR32:
549 // return 0x2;
550 // case IMGFMT_YUY2:
551 case IMGFMT_I420:
552 case IMGFMT_IYUV:
553 case IMGFMT_YV12:
554 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
555 }
556 return 0;
557 }
558
559
uninit(void)560 static void uninit(void)
561 {
562 if (!myximage)
563 return;
564
565 freeMyXImage();
566
567 #ifdef CONFIG_XF86VM
568 vo_vm_close();
569 #endif
570
571 zoomFlag = 0;
572 vo_x11_uninit();
573
574 sws_freeContext(swsContext);
575 }
576
preinit(const char * arg)577 static int preinit(const char *arg)
578 {
579 if (arg)
580 {
581 mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
582 return ENOSYS;
583 }
584
585 if (!vo_init())
586 return -1; // Can't open X11
587 return 0;
588 }
589
control(uint32_t request,void * data)590 static int control(uint32_t request, void *data)
591 {
592 switch (request)
593 {
594 case VOCTRL_PAUSE:
595 return int_pause = 1;
596 case VOCTRL_RESUME:
597 return int_pause = 0;
598 case VOCTRL_QUERY_FORMAT:
599 return query_format(*((uint32_t *) data));
600 case VOCTRL_GET_IMAGE:
601 return get_image(data);
602 case VOCTRL_GUISUPPORT:
603 return VO_TRUE;
604 case VOCTRL_FULLSCREEN:
605 vo_x11_fullscreen();
606 vo_x11_clearwindow(mDisplay, vo_window);
607 return VO_TRUE;
608 case VOCTRL_SET_EQUALIZER:
609 {
610 vf_equalizer_t *eq=data;
611 return vo_x11_set_equalizer(eq->item, eq->value);
612 }
613 case VOCTRL_GET_EQUALIZER:
614 {
615 vf_equalizer_t *eq=data;
616 return vo_x11_get_equalizer(eq->item, &eq->value);
617 }
618 case VOCTRL_ONTOP:
619 vo_x11_ontop();
620 return VO_TRUE;
621 case VOCTRL_UPDATE_SCREENINFO:
622 update_xinerama_info();
623 return VO_TRUE;
624 }
625 return VO_NOTIMPL;
626 }
627