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
);
303 static void allocate_xvimage(int foo
)
306 * allocate XvImages. FIXME: no error checking, without
307 * mit-shm this will bomb... trzing to fix ::atmos
310 if (mLocalDisplay
&& XShmQueryExtension(mDisplay
))
315 mp_msg(MSGT_VO
, MSGL_INFO
,
316 MSGTR_LIBVO_XV_SharedMemoryNotSupported
);
321 (XvImage
*) XvShmCreateImage(mDisplay
, xv_port
, xv_format
,
322 NULL
, image_width
, image_height
,
326 shmget(IPC_PRIVATE
, xvimage
[foo
]->data_size
, IPC_CREAT
| 0777);
327 Shminfo
[foo
].shmaddr
= (char *) shmat(Shminfo
[foo
].shmid
, 0, 0);
328 Shminfo
[foo
].readOnly
= False
;
330 xvimage
[foo
]->data
= Shminfo
[foo
].shmaddr
;
331 XShmAttach(mDisplay
, &Shminfo
[foo
]);
332 XSync(mDisplay
, False
);
333 shmctl(Shminfo
[foo
].shmid
, IPC_RMID
, 0);
338 (XvImage
*) XvCreateImage(mDisplay
, xv_port
, xv_format
, NULL
,
339 image_width
, image_height
);
340 xvimage
[foo
]->data
= malloc(xvimage
[foo
]->data_size
);
341 XSync(mDisplay
, False
);
343 memset(xvimage
[foo
]->data
, 128, xvimage
[foo
]->data_size
);
347 static void deallocate_xvimage(int foo
)
352 XShmDetach(mDisplay
, &Shminfo
[foo
]);
353 shmdt(Shminfo
[foo
].shmaddr
);
357 free(xvimage
[foo
]->data
);
361 XSync(mDisplay
, False
);
365 static inline void put_xvimage( XvImage
* xvi
)
370 XvShmPutImage(mDisplay
, xv_port
, vo_window
, vo_gc
,
372 src_rect
.left
, src_rect
.top
, src_rect
.width
, src_rect
.height
,
373 dst_rect
.left
, dst_rect
.top
, dst_rect
.width
, dst_rect
.height
,
378 XvPutImage(mDisplay
, xv_port
, vo_window
, vo_gc
,
380 src_rect
.left
, src_rect
.top
, src_rect
.width
, src_rect
.height
,
381 dst_rect
.left
, dst_rect
.top
, dst_rect
.width
, dst_rect
.height
);
385 static void check_events(void)
387 int e
= vo_x11_check_events(mDisplay
);
389 if (e
& VO_EVENT_EXPOSE
|| e
& VO_EVENT_RESIZE
)
394 if ((e
& VO_EVENT_EXPOSE
|| e
& VO_EVENT_RESIZE
) && int_pause
)
396 /* did we already draw a buffer */
397 if ( visible_buf
!= -1 )
399 /* redraw the last visible buffer */
400 put_xvimage( xvimage
[visible_buf
] );
405 static void draw_osd(void)
407 vo_draw_text(image_width
-
408 image_width
* vo_panscan_x
/ (vo_dwidth
+ vo_panscan_x
),
409 image_height
, draw_alpha_fnc
);
412 static void flip_page(void)
414 put_xvimage( xvimage
[current_buf
] );
416 /* remember the currently visible buffer */
417 visible_buf
= current_buf
;
422 vo_directrendering
? 0 : ((current_buf
+ 1) % num_buffers
);
425 XSync(mDisplay
, False
);
429 static int draw_slice(uint8_t * image
[], int stride
[], int w
, int h
,
434 dst
= xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[0] +
435 xvimage
[current_buf
]->pitches
[0] * y
+ x
;
436 memcpy_pic(dst
, image
[0], w
, h
, xvimage
[current_buf
]->pitches
[0],
444 dst
= xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[1] +
445 xvimage
[current_buf
]->pitches
[1] * y
+ x
;
446 if (image_format
!= IMGFMT_YV12
)
447 memcpy_pic(dst
, image
[1], w
, h
, xvimage
[current_buf
]->pitches
[1],
450 memcpy_pic(dst
, image
[2], w
, h
, xvimage
[current_buf
]->pitches
[1],
453 dst
= xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[2] +
454 xvimage
[current_buf
]->pitches
[2] * y
+ x
;
455 if (image_format
== IMGFMT_YV12
)
456 memcpy_pic(dst
, image
[1], w
, h
, xvimage
[current_buf
]->pitches
[1],
459 memcpy_pic(dst
, image
[2], w
, h
, xvimage
[current_buf
]->pitches
[1],
465 static int draw_frame(uint8_t * src
[])
470 static uint32_t draw_image(mp_image_t
* mpi
)
472 if (mpi
->flags
& MP_IMGFLAG_DIRECT
)
475 current_buf
= (int) (mpi
->priv
); // hack!
478 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
479 return VO_TRUE
; // done
480 if (mpi
->flags
& MP_IMGFLAG_PLANAR
)
482 draw_slice(mpi
->planes
, mpi
->stride
, mpi
->w
, mpi
->h
, 0, 0);
485 if (mpi
->flags
& MP_IMGFLAG_YUV
)
488 memcpy_pic(xvimage
[current_buf
]->data
+
489 xvimage
[current_buf
]->offsets
[0], mpi
->planes
[0],
490 mpi
->w
* (mpi
->bpp
/ 8), mpi
->h
,
491 xvimage
[current_buf
]->pitches
[0], mpi
->stride
[0]);
494 return VO_FALSE
; // not (yet) supported
497 static uint32_t get_image(mp_image_t
* mpi
)
499 int buf
= current_buf
; // we shouldn't change current_buf unless we do DR!
501 if (mpi
->type
== MP_IMGTYPE_STATIC
&& num_buffers
> 1)
502 return VO_FALSE
; // it is not static
503 if (mpi
->imgfmt
!= image_format
)
504 return VO_FALSE
; // needs conversion :(
505 // if(mpi->flags&MP_IMGFLAG_READABLE) return VO_FALSE; // slow video ram
506 if (mpi
->flags
& MP_IMGFLAG_READABLE
&&
507 (mpi
->type
== MP_IMGTYPE_IPB
|| mpi
->type
== MP_IMGTYPE_IP
))
509 // reference (I/P) frame of IP or IPB:
511 return VO_FALSE
; // not enough
513 // for IPB with 2 buffers we can DR only one of the 2 P frames:
514 if (mpi
->type
== MP_IMGTYPE_IPB
&& num_buffers
< 3
517 buf
= current_ip_buf
;
518 if (mpi
->type
== MP_IMGTYPE_IPB
)
519 ++buf
; // preserve space for B
521 if (mpi
->height
> xvimage
[buf
]->height
)
522 return VO_FALSE
; //buffer to small
523 if (mpi
->width
* (mpi
->bpp
/ 8) > xvimage
[buf
]->pitches
[0])
524 return VO_FALSE
; //buffer to small
525 if ((mpi
->flags
& (MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_ACCEPT_WIDTH
))
526 || (mpi
->width
* (mpi
->bpp
/ 8) == xvimage
[buf
]->pitches
[0]))
530 xvimage
[current_buf
]->data
+ xvimage
[current_buf
]->offsets
[0];
531 mpi
->stride
[0] = xvimage
[current_buf
]->pitches
[0];
532 mpi
->width
= mpi
->stride
[0] / (mpi
->bpp
/ 8);
533 if (mpi
->flags
& MP_IMGFLAG_PLANAR
)
535 if (mpi
->flags
& MP_IMGFLAG_SWAPPED
)
539 xvimage
[current_buf
]->data
+
540 xvimage
[current_buf
]->offsets
[1];
542 xvimage
[current_buf
]->data
+
543 xvimage
[current_buf
]->offsets
[2];
544 mpi
->stride
[1] = xvimage
[current_buf
]->pitches
[1];
545 mpi
->stride
[2] = xvimage
[current_buf
]->pitches
[2];
550 xvimage
[current_buf
]->data
+
551 xvimage
[current_buf
]->offsets
[2];
553 xvimage
[current_buf
]->data
+
554 xvimage
[current_buf
]->offsets
[1];
555 mpi
->stride
[1] = xvimage
[current_buf
]->pitches
[2];
556 mpi
->stride
[2] = xvimage
[current_buf
]->pitches
[1];
559 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
560 mpi
->priv
= (void *) current_buf
;
561 // printf("mga: get_image() SUCCESS -> Direct Rendering ENABLED\n");
567 static int query_format(uint32_t format
)
570 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
572 /* check image formats */
573 for (i
= 0; i
< formats
; i
++)
575 if (fo
[i
].id
== format
)
576 return flag
; //xv_format = fo[i].id;
581 static void uninit(void)
585 if (!vo_config_count
)
588 XvFreeAdaptorInfo(ai
);
594 for (i
= 0; i
< num_buffers
; i
++)
595 deallocate_xvimage(i
);
599 mp_input_rm_event_fd(ConnectionNumber(mDisplay
));
603 static int preinit(const char *arg
)
608 strarg_t ck_src_arg
= { 0, NULL
};
609 strarg_t ck_method_arg
= { 0, NULL
};
612 const opt_t subopts
[] =
614 /* name arg type arg var test */
615 { "port", OPT_ARG_INT
, &xv_port
, int_pos
},
616 { "adaptor", OPT_ARG_INT
, &xv_adaptor
, int_non_neg
},
617 { "ck", OPT_ARG_STR
, &ck_src_arg
, xv_test_ck
},
618 { "ck-method", OPT_ARG_STR
, &ck_method_arg
, xv_test_ckm
},
624 /* parse suboptions */
625 if ( subopt_parse( arg
, subopts
) != 0 )
630 /* modify colorkey settings according to the given options */
631 xv_setup_colorkeyhandling( ck_method_arg
.str
, ck_src_arg
.str
);
636 /* check for Xvideo extension */
637 if (Success
!= XvQueryExtension(mDisplay
, &ver
, &rel
, &req
, &ev
, &err
))
639 mp_msg(MSGT_VO
, MSGL_ERR
,
640 MSGTR_LIBVO_XV_XvNotSupportedByX11
);
644 /* check for Xvideo support */
646 XvQueryAdaptors(mDisplay
, DefaultRootWindow(mDisplay
), &adaptors
,
649 mp_msg(MSGT_VO
, MSGL_ERR
, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed
);
658 for (port_found
= 0, i
= 0; !port_found
&& i
< adaptors
; i
++)
660 if ((ai
[i
].type
& XvInputMask
) && (ai
[i
].type
& XvImageMask
))
662 for (xv_p
= ai
[i
].base_id
;
663 xv_p
< ai
[i
].base_id
+ ai
[i
].num_ports
; ++xv_p
)
675 if (XvGrabPort(mDisplay
, xv_port
, CurrentTime
))
679 mp_msg(MSGT_VO
, MSGL_WARN
,
680 MSGTR_LIBVO_XV_InvalidPortParameter
);
685 for (i
= 0; i
< adaptors
&& xv_port
== 0; i
++)
687 /* check if adaptor number has been specified */
688 if (xv_adaptor
!= -1 && xv_adaptor
!= i
)
691 if ((ai
[i
].type
& XvInputMask
) && (ai
[i
].type
& XvImageMask
))
693 for (xv_p
= ai
[i
].base_id
;
694 xv_p
< ai
[i
].base_id
+ ai
[i
].num_ports
; ++xv_p
)
695 if (!XvGrabPort(mDisplay
, xv_p
, CurrentTime
))
698 mp_msg(MSGT_VO
, MSGL_V
,
699 "[VO_XV] Using Xv Adapter #%d (%s)\n",
704 mp_msg(MSGT_VO
, MSGL_WARN
,
705 MSGTR_LIBVO_XV_CouldNotGrabPort
, (int) xv_p
);
713 mp_msg(MSGT_VO
, MSGL_ERR
,
714 MSGTR_LIBVO_XV_CouldNotFindFreePort
);
716 mp_msg(MSGT_VO
, MSGL_ERR
,
717 MSGTR_LIBVO_XV_NoXvideoSupport
);
721 if ( !vo_xv_init_colorkey() )
723 return -1; // bail out, colorkey setup failed
725 vo_xv_enable_vsync();
726 vo_xv_get_max_img_dim( &max_width
, &max_height
);
728 fo
= XvListImageFormats(mDisplay
, xv_port
, (int *) &formats
);
730 mp_input_add_event_fd(ConnectionNumber(mDisplay
), check_events
);
734 static int control(uint32_t request
, void *data
, ...)
739 return int_pause
= 1;
741 return int_pause
= 0;
742 case VOCTRL_QUERY_FORMAT
:
743 return query_format(*((uint32_t *) data
));
744 case VOCTRL_GET_IMAGE
:
745 return get_image(data
);
746 case VOCTRL_DRAW_IMAGE
:
747 return draw_image(data
);
748 case VOCTRL_GUISUPPORT
:
750 case VOCTRL_GET_PANSCAN
:
752 case VOCTRL_FULLSCREEN
:
754 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
755 case VOCTRL_SET_PANSCAN
:
758 case VOCTRL_SET_EQUALIZER
:
764 value
= va_arg(ap
, int);
768 return vo_xv_set_eq(xv_port
, data
, value
);
770 case VOCTRL_GET_EQUALIZER
:
776 value
= va_arg(ap
, int *);
780 return vo_xv_get_eq(xv_port
, data
, value
);
785 case VOCTRL_UPDATE_SCREENINFO
:
786 update_xinerama_info();