4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 // Number of buffers _FOR_DOUBLEBUFFERING_MODE_
22 // Use option -double to enable double buffering! (default: single buffer)
44 #include "video_out.h"
45 #include "video_out_internal.h"
49 #include <X11/Xutil.h>
52 #include "x11_common.h"
54 #include "fastmemcpy.h"
58 #include "subopt-helper.h"
60 #include "input/input.h"
63 #include "gui/interface.h"
66 #include "libavutil/common.h"
68 static const vo_info_t info
= {
71 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
75 const LIBVO_EXTERN(xv
)
79 #include <X11/extensions/XShm.h>
81 static XShmSegmentInfo Shminfo
[NUM_BUFFERS
];
82 static int Shmem_Flag
;
85 // Note: depends on the inclusion of X11/extensions/XShm.h
86 #include <X11/extensions/Xv.h>
87 #include <X11/extensions/Xvlib.h>
89 // FIXME: dynamically allocate this stuff
90 static void allocate_xvimage(int);
91 static unsigned int ver
, rel
, req
, ev
, err
;
92 static unsigned int formats
, adaptors
, xv_format
;
93 static XvAdaptorInfo
*ai
= NULL
;
94 static XvImageFormatValues
*fo
=NULL
;
96 static int current_buf
= 0;
97 static int current_ip_buf
= 0;
98 static int num_buffers
= 1; // default
99 static int visible_buf
= -1; // -1 means: no buffer was drawn yet
100 static XvImage
*xvimage
[NUM_BUFFERS
];
103 static uint32_t image_width
;
104 static uint32_t image_height
;
105 static uint32_t image_format
;
107 static int int_pause
;
109 static struct vo_rect src_rect
;
110 static struct vo_rect dst_rect
;
111 static uint32_t max_width
= 0, max_height
= 0; // zero means: not set
113 static void (*draw_alpha_fnc
) (int x0
, int y0
, int w
, int h
,
114 unsigned char *src
, unsigned char *srca
,
117 static void draw_alpha_yv12(int x0
, int y0
, int w
, int h
,
118 unsigned char *src
, unsigned char *srca
,
121 x0
+= image_width
* (vo_panscan_x
>> 1) / (vo_dwidth
+ vo_panscan_x
);
122 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
123 xvimage
[current_buf
]->data
+
124 xvimage
[current_buf
]->offsets
[0] +
125 xvimage
[current_buf
]->pitches
[0] * y0
+ x0
,
126 xvimage
[current_buf
]->pitches
[0]);
129 static void draw_alpha_yuy2(int x0
, int y0
, int w
, int h
,
130 unsigned char *src
, unsigned char *srca
,
133 x0
+= image_width
* (vo_panscan_x
>> 1) / (vo_dwidth
+ vo_panscan_x
);
134 vo_draw_alpha_yuy2(w
, h
, src
, srca
, stride
,
135 xvimage
[current_buf
]->data
+
136 xvimage
[current_buf
]->offsets
[0] +
137 xvimage
[current_buf
]->pitches
[0] * y0
+ 2 * x0
,
138 xvimage
[current_buf
]->pitches
[0]);
141 static void draw_alpha_uyvy(int x0
, int y0
, int w
, int h
,
142 unsigned char *src
, unsigned char *srca
,
145 x0
+= image_width
* (vo_panscan_x
>> 1) / (vo_dwidth
+ vo_panscan_x
);
146 vo_draw_alpha_yuy2(w
, h
, src
, srca
, stride
,
147 xvimage
[current_buf
]->data
+
148 xvimage
[current_buf
]->offsets
[0] +
149 xvimage
[current_buf
]->pitches
[0] * y0
+ 2 * x0
+ 1,
150 xvimage
[current_buf
]->pitches
[0]);
153 static void draw_alpha_null(int x0
, int y0
, int w
, int h
,
154 unsigned char *src
, unsigned char *srca
,
160 static void deallocate_xvimage(int foo
);
162 static void resize(void)
164 calc_src_dst_rects(image_width
, image_height
, &src_rect
, &dst_rect
, NULL
, NULL
);
165 vo_x11_clearwindow_part(mDisplay
, vo_window
, dst_rect
.width
, dst_rect
.height
, 1);
166 vo_xv_draw_colorkey(dst_rect
.left
, dst_rect
.top
, dst_rect
.width
, dst_rect
.height
);
170 * connect to server, create and map window,
171 * allocate colors and (shared) memory
173 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
174 uint32_t d_height
, uint32_t flags
, char *title
,
178 XSetWindowAttributes xswa
;
179 XWindowAttributes attribs
;
180 unsigned long xswamask
;
184 int vm
= flags
& VOFLAG_MODESWITCHING
;
187 image_height
= height
;
189 image_format
= format
;
191 if ((max_width
!= 0 && max_height
!= 0) &&
192 (image_width
> max_width
|| image_height
> max_height
))
194 mp_msg( MSGT_VO
, MSGL_ERR
, MSGTR_VO_XV_ImagedimTooHigh
,
195 image_width
, image_height
, max_width
, max_height
);
203 vo_doublebuffering
? (vo_directrendering
? NUM_BUFFERS
: 2) : 1;
205 /* check image formats */
210 for (i
= 0; i
< formats
; i
++)
212 mp_msg(MSGT_VO
, MSGL_V
,
213 "Xvideo image format: 0x%x (%4.4s) %s\n", fo
[i
].id
,
215 (fo
[i
].format
== XvPacked
) ? "packed" : "planar");
216 if (fo
[i
].id
== format
)
217 xv_format
= fo
[i
].id
;
225 guiGetEvent(guiSetShVideo
, 0); // the GUI will set up / resize the window
235 XGetWindowAttributes(mDisplay
, DefaultRootWindow(mDisplay
),
237 depth
= attribs
.depth
;
238 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32)
240 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
242 xswa
.background_pixel
= 0;
243 if (xv_ck_info
.method
== CK_METHOD_BACKGROUND
)
245 xswa
.background_pixel
= xv_colorkey
;
247 xswa
.border_pixel
= 0;
248 xswamask
= CWBackPixel
| CWBorderPixel
;
250 vo_x11_create_vo_window(&vinfo
, vo_dx
, vo_dy
, vo_dwidth
, vo_dheight
,
251 flags
, CopyFromParent
, "xv", title
);
252 XChangeWindowAttributes(mDisplay
, vo_window
, xswamask
, &xswa
);
257 /* Grab the mouse pointer in our window */
259 XGrabPointer(mDisplay
, vo_window
, True
, 0,
260 GrabModeAsync
, GrabModeAsync
,
261 vo_window
, None
, CurrentTime
);
262 XSetInputFocus(mDisplay
, vo_window
, RevertToNone
, CurrentTime
);
267 mp_msg(MSGT_VO
, MSGL_V
, "using Xvideo port %d for hw scaling\n",
275 draw_alpha_fnc
= draw_alpha_yv12
;
279 draw_alpha_fnc
= draw_alpha_yuy2
;
282 draw_alpha_fnc
= draw_alpha_uyvy
;
285 draw_alpha_fnc
= draw_alpha_null
;
289 for (current_buf
= 0; current_buf
< num_buffers
; ++current_buf
)
290 deallocate_xvimage(current_buf
);
292 for (current_buf
= 0; current_buf
< num_buffers
; ++current_buf
)
293 allocate_xvimage(current_buf
);
298 if ((flags
& VOFLAG_FULLSCREEN
) && WinID
<= 0) vo_fs
= 1;
305 static void allocate_xvimage(int foo
)
308 * allocate XvImages. FIXME: no error checking, without
309 * mit-shm this will bomb... trzing to fix ::atmos
312 if (mLocalDisplay
&& XShmQueryExtension(mDisplay
))
317 mp_msg(MSGT_VO
, MSGL_INFO
,
318 MSGTR_LIBVO_XV_SharedMemoryNotSupported
);
323 (XvImage
*) XvShmCreateImage(mDisplay
, xv_port
, xv_format
,
324 NULL
, image_width
, image_height
,
328 shmget(IPC_PRIVATE
, xvimage
[foo
]->data_size
, IPC_CREAT
| 0777);
329 Shminfo
[foo
].shmaddr
= (char *) shmat(Shminfo
[foo
].shmid
, 0, 0);
330 Shminfo
[foo
].readOnly
= False
;
332 xvimage
[foo
]->data
= Shminfo
[foo
].shmaddr
;
333 XShmAttach(mDisplay
, &Shminfo
[foo
]);
334 XSync(mDisplay
, False
);
335 shmctl(Shminfo
[foo
].shmid
, IPC_RMID
, 0);
340 (XvImage
*) XvCreateImage(mDisplay
, xv_port
, xv_format
, NULL
,
341 image_width
, image_height
);
342 xvimage
[foo
]->data
= malloc(xvimage
[foo
]->data_size
);
343 XSync(mDisplay
, False
);
345 memset(xvimage
[foo
]->data
, 128, xvimage
[foo
]->data_size
);
349 static void deallocate_xvimage(int foo
)
354 XShmDetach(mDisplay
, &Shminfo
[foo
]);
355 shmdt(Shminfo
[foo
].shmaddr
);
359 free(xvimage
[foo
]->data
);
363 XSync(mDisplay
, False
);
367 static inline void put_xvimage( XvImage
* xvi
)
372 XvShmPutImage(mDisplay
, xv_port
, vo_window
, vo_gc
,
374 src_rect
.left
, src_rect
.top
, src_rect
.width
, src_rect
.height
,
375 dst_rect
.left
, dst_rect
.top
, dst_rect
.width
, dst_rect
.height
,
380 XvPutImage(mDisplay
, xv_port
, vo_window
, vo_gc
,
382 src_rect
.left
, src_rect
.top
, src_rect
.width
, src_rect
.height
,
383 dst_rect
.left
, dst_rect
.top
, dst_rect
.width
, dst_rect
.height
);
387 static void check_events(void)
389 int e
= vo_x11_check_events(mDisplay
);
391 if (e
& VO_EVENT_EXPOSE
|| e
& VO_EVENT_RESIZE
)
396 if ((e
& VO_EVENT_EXPOSE
|| e
& VO_EVENT_RESIZE
) && int_pause
)
398 /* did we already draw a buffer */
399 if ( visible_buf
!= -1 )
401 /* redraw the last visible buffer */
402 put_xvimage( xvimage
[visible_buf
] );
407 static void draw_osd(void)
409 vo_draw_text(image_width
-
410 image_width
* vo_panscan_x
/ (vo_dwidth
+ vo_panscan_x
),
411 image_height
, draw_alpha_fnc
);
414 static void flip_page(void)
416 put_xvimage( xvimage
[current_buf
] );
418 /* remember the currently visible buffer */
419 visible_buf
= current_buf
;
424 vo_directrendering
? 0 : ((current_buf
+ 1) % num_buffers
);
427 XSync(mDisplay
, False
);
431 static int draw_slice(uint8_t * image
[], int stride
[], int w
, int h
,
436 dst
= xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[0] +
437 xvimage
[current_buf
]->pitches
[0] * y
+ x
;
438 memcpy_pic(dst
, image
[0], w
, h
, xvimage
[current_buf
]->pitches
[0],
446 dst
= xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[1] +
447 xvimage
[current_buf
]->pitches
[1] * y
+ x
;
448 if (image_format
!= IMGFMT_YV12
)
449 memcpy_pic(dst
, image
[1], w
, h
, xvimage
[current_buf
]->pitches
[1],
452 memcpy_pic(dst
, image
[2], w
, h
, xvimage
[current_buf
]->pitches
[1],
455 dst
= xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[2] +
456 xvimage
[current_buf
]->pitches
[2] * y
+ x
;
457 if (image_format
== IMGFMT_YV12
)
458 memcpy_pic(dst
, image
[1], w
, h
, xvimage
[current_buf
]->pitches
[1],
461 memcpy_pic(dst
, image
[2], w
, h
, xvimage
[current_buf
]->pitches
[1],
467 static int draw_frame(uint8_t * src
[])
472 static uint32_t draw_image(mp_image_t
* mpi
)
474 if (mpi
->flags
& MP_IMGFLAG_DIRECT
)
477 current_buf
= (int) (mpi
->priv
); // hack!
480 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
481 return VO_TRUE
; // done
482 if (mpi
->flags
& MP_IMGFLAG_PLANAR
)
484 draw_slice(mpi
->planes
, mpi
->stride
, mpi
->w
, mpi
->h
, 0, 0);
487 if (mpi
->flags
& MP_IMGFLAG_YUV
)
490 memcpy_pic(xvimage
[current_buf
]->data
+
491 xvimage
[current_buf
]->offsets
[0], mpi
->planes
[0],
492 mpi
->w
* (mpi
->bpp
/ 8), mpi
->h
,
493 xvimage
[current_buf
]->pitches
[0], mpi
->stride
[0]);
496 return VO_FALSE
; // not (yet) supported
499 static uint32_t get_image(mp_image_t
* mpi
)
501 int buf
= current_buf
; // we shouldn't change current_buf unless we do DR!
503 if (mpi
->type
== MP_IMGTYPE_STATIC
&& num_buffers
> 1)
504 return VO_FALSE
; // it is not static
505 if (mpi
->imgfmt
!= image_format
)
506 return VO_FALSE
; // needs conversion :(
507 // if(mpi->flags&MP_IMGFLAG_READABLE) return VO_FALSE; // slow video ram
508 if (mpi
->flags
& MP_IMGFLAG_READABLE
&&
509 (mpi
->type
== MP_IMGTYPE_IPB
|| mpi
->type
== MP_IMGTYPE_IP
))
511 // reference (I/P) frame of IP or IPB:
513 return VO_FALSE
; // not enough
515 // for IPB with 2 buffers we can DR only one of the 2 P frames:
516 if (mpi
->type
== MP_IMGTYPE_IPB
&& num_buffers
< 3
519 buf
= current_ip_buf
;
520 if (mpi
->type
== MP_IMGTYPE_IPB
)
521 ++buf
; // preserve space for B
523 if (mpi
->height
> xvimage
[buf
]->height
)
524 return VO_FALSE
; //buffer to small
525 if (mpi
->width
* (mpi
->bpp
/ 8) > xvimage
[buf
]->pitches
[0])
526 return VO_FALSE
; //buffer to small
527 if ((mpi
->flags
& (MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_ACCEPT_WIDTH
))
528 || (mpi
->width
* (mpi
->bpp
/ 8) == xvimage
[buf
]->pitches
[0]))
532 xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[0];
533 mpi
->stride
[0] = xvimage
[current_buf
]->pitches
[0];
534 mpi
->width
= mpi
->stride
[0] / (mpi
->bpp
/ 8);
535 if (mpi
->flags
& MP_IMGFLAG_PLANAR
)
537 if (mpi
->flags
& MP_IMGFLAG_SWAPPED
)
541 xvimage
[current_buf
]->data
+
542 xvimage
[current_buf
]->offsets
[1];
544 xvimage
[current_buf
]->data
+
545 xvimage
[current_buf
]->offsets
[2];
546 mpi
->stride
[1] = xvimage
[current_buf
]->pitches
[1];
547 mpi
->stride
[2] = xvimage
[current_buf
]->pitches
[2];
552 xvimage
[current_buf
]->data
+
553 xvimage
[current_buf
]->offsets
[2];
555 xvimage
[current_buf
]->data
+
556 xvimage
[current_buf
]->offsets
[1];
557 mpi
->stride
[1] = xvimage
[current_buf
]->pitches
[2];
558 mpi
->stride
[2] = xvimage
[current_buf
]->pitches
[1];
561 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
562 mpi
->priv
= (void *) current_buf
;
563 // printf("mga: get_image() SUCCESS -> Direct Rendering ENABLED\n");
569 static int query_format(uint32_t format
)
572 int flag
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_OSD
| VFCAP_ACCEPT_STRIDE
; // FIXME! check for DOWN
574 /* check image formats */
575 for (i
= 0; i
< formats
; i
++)
577 if (fo
[i
].id
== format
)
578 return flag
; //xv_format = fo[i].id;
583 static void uninit(void)
587 if (!vo_config_count
)
590 XvFreeAdaptorInfo(ai
);
596 for (i
= 0; i
< num_buffers
; i
++)
597 deallocate_xvimage(i
);
601 mp_input_rm_event_fd(ConnectionNumber(mDisplay
));
605 static int preinit(const char *arg
)
610 strarg_t ck_src_arg
= { 0, NULL
};
611 strarg_t ck_method_arg
= { 0, NULL
};
616 /* name arg type arg var test */
617 { "port", OPT_ARG_INT
, &xv_port
, (opt_test_f
)int_pos
},
618 { "adaptor", OPT_ARG_INT
, &xv_adaptor
, (opt_test_f
)int_non_neg
},
619 { "ck", OPT_ARG_STR
, &ck_src_arg
, xv_test_ck
},
620 { "ck-method", OPT_ARG_STR
, &ck_method_arg
, xv_test_ckm
},
626 /* parse suboptions */
627 if ( subopt_parse( arg
, subopts
) != 0 )
632 /* modify colorkey settings according to the given options */
633 xv_setup_colorkeyhandling( ck_method_arg
.str
, ck_src_arg
.str
);
638 /* check for Xvideo extension */
639 if (Success
!= XvQueryExtension(mDisplay
, &ver
, &rel
, &req
, &ev
, &err
))
641 mp_msg(MSGT_VO
, MSGL_ERR
,
642 MSGTR_LIBVO_XV_XvNotSupportedByX11
);
646 /* check for Xvideo support */
648 XvQueryAdaptors(mDisplay
, DefaultRootWindow(mDisplay
), &adaptors
,
651 mp_msg(MSGT_VO
, MSGL_ERR
, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed
);
660 for (port_found
= 0, i
= 0; !port_found
&& i
< adaptors
; i
++)
662 if ((ai
[i
].type
& XvInputMask
) && (ai
[i
].type
& XvImageMask
))
664 for (xv_p
= ai
[i
].base_id
;
665 xv_p
< ai
[i
].base_id
+ ai
[i
].num_ports
; ++xv_p
)
677 if (XvGrabPort(mDisplay
, xv_port
, CurrentTime
))
681 mp_msg(MSGT_VO
, MSGL_WARN
,
682 MSGTR_LIBVO_XV_InvalidPortParameter
);
687 for (i
= 0; i
< adaptors
&& xv_port
== 0; i
++)
689 /* check if adaptor number has been specified */
690 if (xv_adaptor
!= -1 && xv_adaptor
!= i
)
693 if ((ai
[i
].type
& XvInputMask
) && (ai
[i
].type
& XvImageMask
))
695 for (xv_p
= ai
[i
].base_id
;
696 xv_p
< ai
[i
].base_id
+ ai
[i
].num_ports
; ++xv_p
)
697 if (!XvGrabPort(mDisplay
, xv_p
, CurrentTime
))
700 mp_msg(MSGT_VO
, MSGL_V
,
701 "[VO_XV] Using Xv Adapter #%d (%s)\n",
706 mp_msg(MSGT_VO
, MSGL_WARN
,
707 MSGTR_LIBVO_XV_CouldNotGrabPort
, (int) xv_p
);
715 mp_msg(MSGT_VO
, MSGL_ERR
,
716 MSGTR_LIBVO_XV_CouldNotFindFreePort
);
718 mp_msg(MSGT_VO
, MSGL_ERR
,
719 MSGTR_LIBVO_XV_NoXvideoSupport
);
723 if ( !vo_xv_init_colorkey() )
725 return -1; // bail out, colorkey setup failed
727 vo_xv_enable_vsync();
728 vo_xv_get_max_img_dim( &max_width
, &max_height
);
730 fo
= XvListImageFormats(mDisplay
, xv_port
, (int *) &formats
);
732 mp_input_add_event_fd(ConnectionNumber(mDisplay
), check_events
);
736 static int control(uint32_t request
, void *data
, ...)
741 return int_pause
= 1;
743 return int_pause
= 0;
744 case VOCTRL_QUERY_FORMAT
:
745 return query_format(*((uint32_t *) data
));
746 case VOCTRL_GET_IMAGE
:
747 return get_image(data
);
748 case VOCTRL_DRAW_IMAGE
:
749 return draw_image(data
);
750 case VOCTRL_GUISUPPORT
:
752 case VOCTRL_GET_PANSCAN
:
753 if (!vo_config_count
|| !vo_fs
)
756 case VOCTRL_FULLSCREEN
:
758 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
759 case VOCTRL_SET_PANSCAN
:
760 if ((vo_fs
&& (vo_panscan
!= vo_panscan_amount
))
761 || (!vo_fs
&& vo_panscan_amount
))
763 int old_y
= vo_panscan_y
;
767 if (old_y
!= vo_panscan_y
)
774 case VOCTRL_SET_EQUALIZER
:
780 value
= va_arg(ap
, int);
784 return vo_xv_set_eq(xv_port
, data
, value
);
786 case VOCTRL_GET_EQUALIZER
:
792 value
= va_arg(ap
, int *);
796 return vo_xv_get_eq(xv_port
, data
, value
);
801 case VOCTRL_UPDATE_SCREENINFO
:
802 update_xinerama_info();