Merge svn changes up to r28712
[mplayer/glamo.git] / libvo / vo_xv.c
blobfc6f0b99d9d8b61d05ea06c4760d11166685963f
1 /*
2 * X11 Xv interface
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)
23 #define NUM_BUFFERS 3
26 Buffer allocation:
28 -nodr:
29 1: TEMP
30 2: 2*TEMP
32 -dr:
33 1: TEMP
34 3: 2*STATIC+TEMP
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdint.h>
41 #include <stdbool.h>
43 #include "config.h"
44 #include "options.h"
45 #include "talloc.h"
46 #include "mp_msg.h"
47 #include "help_mp.h"
48 #include "video_out.h"
49 #include "libmpcodecs/vfcap.h"
50 #include "libmpcodecs/mp_image.h"
51 #include "osd.h"
53 #include <X11/Xlib.h>
54 #include <X11/Xutil.h>
55 #include <errno.h>
57 #include "x11_common.h"
59 #include "fastmemcpy.h"
60 #include "sub.h"
61 #include "aspect.h"
63 #include "subopt-helper.h"
65 #include "input/input.h"
66 #include "mp_fifo.h"
68 #ifdef CONFIG_GUI
69 #include "gui/interface.h"
70 #endif
72 #include "libavutil/common.h"
74 static const vo_info_t info = {
75 "X11/Xv",
76 "xv",
77 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
81 #ifdef HAVE_SHM
82 #include <sys/ipc.h>
83 #include <sys/shm.h>
84 #include <X11/extensions/XShm.h>
85 #endif
87 // Note: depends on the inclusion of X11/extensions/XShm.h
88 #include <X11/extensions/Xv.h>
89 #include <X11/extensions/Xvlib.h>
91 struct xvctx {
92 XvAdaptorInfo *ai;
93 XvImageFormatValues *fo;
94 unsigned int formats, adaptors, xv_format;
95 int current_buf;
96 int current_ip_buf;
97 int num_buffers;
98 int total_buffers;
99 int have_visible_image_copy;
100 int have_next_image_copy;
101 int unchanged_visible_image;
102 int unchanged_next_image;
103 int visible_buf;
104 XvImage *xvimage[NUM_BUFFERS + 1];
105 uint32_t image_width;
106 uint32_t image_height;
107 uint32_t image_format;
108 int is_paused;
109 struct vo_rect src_rect;
110 struct vo_rect dst_rect;
111 uint32_t max_width, max_height; // zero means: not set
112 int event_fd_registered; // for uninit called from preinit
113 int mode_switched;
114 int osd_objects_drawn;
115 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
116 unsigned char *src, unsigned char *srca,
117 int stride);
118 #ifdef HAVE_SHM
119 XShmSegmentInfo Shminfo[NUM_BUFFERS];
120 int Shmem_Flag;
121 #endif
124 static void allocate_xvimage(struct vo *, int);
127 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
128 unsigned char *src, unsigned char *srca,
129 int stride)
131 struct vo *vo = p;
132 struct xvctx *ctx = vo->priv;
133 x0 += ctx->image_width * (vo->panscan_x >> 1)
134 / (vo->dwidth + vo->panscan_x);
135 vo_draw_alpha_yv12(w, h, src, srca, stride,
136 ctx->xvimage[ctx->current_buf]->data +
137 ctx->xvimage[ctx->current_buf]->offsets[0] +
138 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
139 ctx->xvimage[ctx->current_buf]->pitches[0]);
140 ctx->osd_objects_drawn++;
143 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
144 unsigned char *src, unsigned char *srca,
145 int stride)
147 struct vo *vo = p;
148 struct xvctx *ctx = vo->priv;
149 x0 += ctx->image_width * (vo->panscan_x >> 1)
150 / (vo->dwidth + vo->panscan_x);
151 vo_draw_alpha_yuy2(w, h, src, srca, stride,
152 ctx->xvimage[ctx->current_buf]->data +
153 ctx->xvimage[ctx->current_buf]->offsets[0] +
154 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
155 ctx->xvimage[ctx->current_buf]->pitches[0]);
156 ctx->osd_objects_drawn++;
159 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
160 unsigned char *src, unsigned char *srca,
161 int stride)
163 struct vo *vo = p;
164 struct xvctx *ctx = vo->priv;
165 x0 += ctx->image_width * (vo->panscan_x >> 1)
166 / (vo->dwidth + vo->panscan_x);
167 vo_draw_alpha_yuy2(w, h, src, srca, stride,
168 ctx->xvimage[ctx->current_buf]->data +
169 ctx->xvimage[ctx->current_buf]->offsets[0] +
170 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
171 ctx->xvimage[ctx->current_buf]->pitches[0]);
172 ctx->osd_objects_drawn++;
175 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
176 unsigned char *src, unsigned char *srca,
177 int stride)
182 static void deallocate_xvimage(struct vo *vo, int foo);
184 static void resize(struct vo *vo)
186 struct xvctx *ctx = vo->priv;
188 calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
189 &ctx->dst_rect, NULL, NULL);
190 struct vo_rect *dst = &ctx->dst_rect;
191 vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height, 1);
192 vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
196 * connect to server, create and map window,
197 * allocate colors and (shared) memory
199 static int config(struct vo *vo, uint32_t width, uint32_t height,
200 uint32_t d_width, uint32_t d_height, uint32_t flags,
201 char *title, uint32_t format)
203 struct MPOpts *opts = vo->opts;
204 struct vo_x11_state *x11 = vo->x11;
205 XVisualInfo vinfo;
206 XSetWindowAttributes xswa;
207 XWindowAttributes attribs;
208 unsigned long xswamask;
209 int depth;
210 struct xvctx *ctx = vo->priv;
211 int i;
213 ctx->image_height = height;
214 ctx->image_width = width;
215 ctx->image_format = format;
217 if ((ctx->max_width != 0 && ctx->max_height != 0)
218 && (ctx->image_width > ctx->max_width
219 || ctx->image_height > ctx->max_height)) {
220 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_VO_XV_ImagedimTooHigh,
221 ctx->image_width, ctx->image_height, ctx->max_width,
222 ctx->max_height);
223 return -1;
226 ctx->is_paused = 0;
227 ctx->visible_buf = -1;
228 ctx->have_visible_image_copy = false;
229 ctx->have_next_image_copy = false;
231 /* check image formats */
232 ctx->xv_format = 0;
233 for (i = 0; i < ctx->formats; i++) {
234 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
235 ctx->fo[i].id, (char *) &ctx->fo[i].id,
236 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
237 if (ctx->fo[i].id == format)
238 ctx->xv_format = ctx->fo[i].id;
240 if (!ctx->xv_format)
241 return -1;
243 #ifdef CONFIG_GUI
244 if (use_gui)
245 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
246 else
247 #endif
249 #ifdef CONFIG_XF86VM
250 int vm = flags & VOFLAG_MODESWITCHING;
251 if (vm) {
252 vo_vm_switch(vo);
253 ctx->mode_switched = 1;
255 #endif
256 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
257 &attribs);
258 depth = attribs.depth;
259 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
260 depth = 24;
261 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
263 xswa.background_pixel = 0;
264 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND)
265 xswa.background_pixel = x11->xv_colorkey;
266 xswa.border_pixel = 0;
267 xswamask = CWBackPixel | CWBorderPixel;
269 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
270 vo->dheight, flags, CopyFromParent, "xv",
271 title);
272 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
274 #ifdef CONFIG_XF86VM
275 if (vm) {
276 /* Grab the mouse pointer in our window */
277 if (vo_grabpointer)
278 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
279 GrabModeAsync, x11->window, None, CurrentTime);
280 XSetInputFocus(x11->display, x11->window, RevertToNone,
281 CurrentTime);
283 #endif
286 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
287 x11->xv_port);
289 switch (ctx->xv_format) {
290 case IMGFMT_YV12:
291 case IMGFMT_I420:
292 case IMGFMT_IYUV:
293 ctx->draw_alpha_fnc = draw_alpha_yv12;
294 break;
295 case IMGFMT_YUY2:
296 case IMGFMT_YVYU:
297 ctx->draw_alpha_fnc = draw_alpha_yuy2;
298 break;
299 case IMGFMT_UYVY:
300 ctx->draw_alpha_fnc = draw_alpha_uyvy;
301 break;
302 default:
303 ctx->draw_alpha_fnc = draw_alpha_null;
306 // In case config has been called before
307 for (i = 0; i < ctx->total_buffers; i++)
308 deallocate_xvimage(vo, i);
310 ctx->num_buffers =
311 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
312 ctx->total_buffers = ctx->num_buffers + 1;
314 for (i = 0; i < ctx->total_buffers; i++)
315 allocate_xvimage(vo, i);
317 ctx->current_buf = 0;
318 ctx->current_ip_buf = 0;
320 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
321 vo_fs = 1;
323 resize(vo);
325 return 0;
328 static void allocate_xvimage(struct vo *vo, int foo)
330 struct xvctx *ctx = vo->priv;
331 struct vo_x11_state *x11 = vo->x11;
333 * allocate XvImages. FIXME: no error checking, without
334 * mit-shm this will bomb... trzing to fix ::atmos
336 #ifdef HAVE_SHM
337 if (x11->display_is_local && XShmQueryExtension(x11->display))
338 ctx->Shmem_Flag = 1;
339 else {
340 ctx->Shmem_Flag = 0;
341 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_LIBVO_XV_SharedMemoryNotSupported);
343 if (ctx->Shmem_Flag) {
344 ctx->xvimage[foo] =
345 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
346 ctx->xv_format, NULL,
347 ctx->image_width, ctx->image_height,
348 &ctx->Shminfo[foo]);
350 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
351 ctx->xvimage[foo]->data_size,
352 IPC_CREAT | 0777);
353 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
355 ctx->Shminfo[foo].readOnly = False;
357 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
358 XShmAttach(x11->display, &ctx->Shminfo[foo]);
359 XSync(x11->display, False);
360 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
361 } else
362 #endif
364 ctx->xvimage[foo] =
365 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
366 ctx->xv_format, NULL, ctx->image_width,
367 ctx->image_height);
368 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
369 XSync(x11->display, False);
371 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
372 return;
375 static void deallocate_xvimage(struct vo *vo, int foo)
377 struct xvctx *ctx = vo->priv;
378 #ifdef HAVE_SHM
379 if (ctx->Shmem_Flag) {
380 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
381 shmdt(ctx->Shminfo[foo].shmaddr);
382 } else
383 #endif
385 free(ctx->xvimage[foo]->data);
387 XFree(ctx->xvimage[foo]);
389 XSync(vo->x11->display, False);
390 return;
393 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
395 struct xvctx *ctx = vo->priv;
396 struct vo_x11_state *x11 = vo->x11;
397 struct vo_rect *src = &ctx->src_rect;
398 struct vo_rect *dst = &ctx->dst_rect;
399 #ifdef HAVE_SHM
400 if (ctx->Shmem_Flag) {
401 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
402 src->left, src->top, src->width, src->height,
403 dst->left, dst->top, dst->width, dst->height,
404 False);
405 } else
406 #endif
408 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
409 src->left, src->top, src->width, src->height,
410 dst->left, dst->top, dst->width, dst->height);
414 // Only copies luma for planar formats as draw_alpha doesn't change others */
415 void copy_backup_image(struct vo *vo, int dest, int src)
417 struct xvctx *ctx = vo->priv;
419 XvImage *vb = ctx->xvimage[dest];
420 XvImage *cp = ctx->xvimage[src];
421 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
422 vb->width, vb->height,
423 vb->pitches[0], cp->pitches[0]);
426 static void check_events(struct vo *vo)
428 struct xvctx *ctx = vo->priv;
429 struct vo_x11_state *x11 = vo->x11;
430 int e = vo_x11_check_events(vo);
432 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
433 resize(vo);
435 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
436 /* did we already draw a buffer */
437 if (ctx->visible_buf != -1) {
438 /* redraw the last visible buffer */
439 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
444 static void draw_osd(struct vo *vo, struct osd_state *osd)
446 struct xvctx *ctx = vo->priv;
448 ctx->osd_objects_drawn = 0;
449 osd_draw_text(osd,
450 ctx->image_width -
451 ctx->image_width * vo->panscan_x / (vo->dwidth +
452 vo->panscan_x),
453 ctx->image_height, ctx->draw_alpha_fnc, vo);
454 if (ctx->osd_objects_drawn)
455 ctx->unchanged_next_image = false;
458 static int redraw_osd(struct vo *vo, struct osd_state *osd)
460 struct xvctx *ctx = vo->priv;
462 if (ctx->have_visible_image_copy)
463 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
464 else if (ctx->unchanged_visible_image) {
465 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
466 ctx->have_visible_image_copy = true;
468 else
469 return false;
470 int temp = ctx->current_buf;
471 ctx->current_buf = ctx->visible_buf;
472 draw_osd(vo, osd);
473 ctx->current_buf = temp;
474 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
475 return true;
478 static void flip_page(struct vo *vo)
480 struct xvctx *ctx = vo->priv;
481 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
483 /* remember the currently visible buffer */
484 ctx->visible_buf = ctx->current_buf;
486 ctx->have_visible_image_copy = ctx->have_next_image_copy;
487 ctx->have_next_image_copy = false;
488 ctx->unchanged_visible_image = ctx->unchanged_next_image;
489 ctx->unchanged_next_image = false;
491 if (ctx->num_buffers > 1) {
492 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
493 ctx->num_buffers);
494 XFlush(vo->x11->display);
495 } else
496 XSync(vo->x11->display, False);
497 return;
500 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
501 int h, int x, int y)
503 struct xvctx *ctx = vo->priv;
504 uint8_t *dst;
505 XvImage *current_image = ctx->xvimage[ctx->current_buf];
507 dst = current_image->data + current_image->offsets[0]
508 + current_image->pitches[0] * y + x;
509 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
511 x /= 2;
512 y /= 2;
513 w /= 2;
514 h /= 2;
516 dst = current_image->data + current_image->offsets[1]
517 + current_image->pitches[1] * y + x;
518 if (ctx->image_format != IMGFMT_YV12)
519 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
520 else
521 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
523 dst = current_image->data + current_image->offsets[2]
524 + current_image->pitches[2] * y + x;
525 if (ctx->image_format == IMGFMT_YV12)
526 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
527 else
528 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
530 return 0;
533 static int draw_frame(struct vo *vo, uint8_t *src[])
535 return VO_ERROR;
538 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
540 struct xvctx *ctx = vo->priv;
542 ctx->have_next_image_copy = false;
544 if (mpi->flags & MP_IMGFLAG_DIRECT)
545 // direct rendering:
546 ctx->current_buf = (int) (mpi->priv); // hack!
547 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
548 ; // done
549 else if (mpi->flags & MP_IMGFLAG_PLANAR)
550 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
551 else if (mpi->flags & MP_IMGFLAG_YUV)
552 // packed YUV:
553 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
554 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
555 mpi->w * (mpi->bpp / 8), mpi->h,
556 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
557 else
558 return false;
560 if (ctx->is_paused) {
561 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
562 ctx->have_next_image_copy = true;
564 ctx->unchanged_next_image = true;
565 return true;
568 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
570 // we shouldn't change current_buf unless we do DR!
571 int buf = ctx->current_buf;
573 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
574 return VO_FALSE; // it is not static
575 if (mpi->imgfmt != ctx->image_format)
576 return VO_FALSE; // needs conversion :(
577 if (mpi->flags & MP_IMGFLAG_READABLE
578 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
579 // reference (I/P) frame of IP or IPB:
580 if (ctx->num_buffers < 2)
581 return VO_FALSE; // not enough
582 ctx->current_ip_buf ^= 1;
583 // for IPB with 2 buffers we can DR only one of the 2 P frames:
584 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
585 && ctx->current_ip_buf)
586 return VO_FALSE;
587 buf = ctx->current_ip_buf;
588 if (mpi->type == MP_IMGTYPE_IPB)
589 ++buf; // preserve space for B
591 if (mpi->height > ctx->xvimage[buf]->height)
592 return VO_FALSE; //buffer to small
593 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
594 return VO_FALSE; //buffer to small
595 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
596 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
597 ctx->current_buf = buf;
598 XvImage *current_image = ctx->xvimage[ctx->current_buf];
599 mpi->planes[0] = current_image->data + current_image->offsets[0];
600 mpi->stride[0] = current_image->pitches[0];
601 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
602 if (mpi->flags & MP_IMGFLAG_PLANAR) {
603 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
604 // I420
605 mpi->planes[1] = current_image->data
606 + current_image->offsets[1];
607 mpi->planes[2] = current_image->data
608 + current_image->offsets[2];
609 mpi->stride[1] = current_image->pitches[1];
610 mpi->stride[2] = current_image->pitches[2];
611 } else {
612 // YV12
613 mpi->planes[1] = current_image->data
614 + current_image->offsets[2];
615 mpi->planes[2] = current_image->data
616 + current_image->offsets[1];
617 mpi->stride[1] = current_image->pitches[2];
618 mpi->stride[2] = current_image->pitches[1];
621 mpi->flags |= MP_IMGFLAG_DIRECT;
622 mpi->priv = (void *) ctx->current_buf;
623 return VO_TRUE;
625 return VO_FALSE;
628 static int query_format(struct xvctx *ctx, uint32_t format)
630 uint32_t i;
631 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
633 /* check image formats */
634 for (i = 0; i < ctx->formats; i++) {
635 if (ctx->fo[i].id == format)
636 return flag; //xv_format = fo[i].id;
638 return 0;
641 static void uninit(struct vo *vo)
643 struct xvctx *ctx = vo->priv;
644 int i;
646 ctx->visible_buf = -1;
647 if (ctx->ai)
648 XvFreeAdaptorInfo(ctx->ai);
649 ctx->ai = NULL;
650 if (ctx->fo) {
651 XFree(ctx->fo);
652 ctx->fo = NULL;
654 for (i = 0; i < ctx->total_buffers; i++)
655 deallocate_xvimage(vo, i);
656 #ifdef CONFIG_XF86VM
657 if (ctx->mode_switched)
658 vo_vm_close(vo);
659 #endif
660 if (ctx->event_fd_registered)
661 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
662 // uninit() shouldn't get called unless initialization went past vo_init()
663 vo_x11_uninit(vo);
666 static int x11_fd_callback(void *ctx, int fd)
668 struct vo *vo = ctx;
669 check_events(vo);
670 return mplayer_get_key(vo->key_fifo, 0);
673 static int preinit(struct vo *vo, const char *arg)
675 XvPortID xv_p;
676 int busy_ports = 0;
677 unsigned int i;
678 strarg_t ck_src_arg = { 0, NULL };
679 strarg_t ck_method_arg = { 0, NULL };
680 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
681 vo->priv = ctx;
682 struct vo_x11_state *x11 = vo->x11;
683 int xv_adaptor = -1;
685 opt_t subopts[] = {
686 /* name arg type arg var test */
687 {"port", OPT_ARG_INT, &x11->xv_port, (opt_test_f) int_pos},
688 {"adaptor", OPT_ARG_INT, &xv_adaptor, (opt_test_f) int_non_neg},
689 {"ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck},
690 {"ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm},
691 {NULL}
694 x11->xv_port = 0;
696 /* parse suboptions */
697 if (subopt_parse(arg, subopts) != 0) {
698 return -1;
701 /* modify colorkey settings according to the given options */
702 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
704 if (!vo_init(vo))
705 return -1;
707 /* check for Xvideo extension */
708 unsigned int ver, rel, req, ev, err;
709 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
710 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvNotSupportedByX11);
711 goto error;
714 /* check for Xvideo support */
715 if (Success !=
716 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
717 &ctx->adaptors, &ctx->ai)) {
718 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed);
719 goto error;
722 /* check adaptors */
723 if (x11->xv_port) {
724 int port_found;
726 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
727 if ((ctx->ai[i].type & XvInputMask)
728 && (ctx->ai[i].type & XvImageMask)) {
729 for (xv_p = ctx->ai[i].base_id;
730 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
731 ++xv_p) {
732 if (xv_p == x11->xv_port) {
733 port_found = 1;
734 break;
739 if (port_found) {
740 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
741 x11->xv_port = 0;
742 } else {
743 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_InvalidPortParameter);
744 x11->xv_port = 0;
748 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
749 /* check if adaptor number has been specified */
750 if (xv_adaptor != -1 && xv_adaptor != i)
751 continue;
753 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
754 for (xv_p = ctx->ai[i].base_id;
755 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
756 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
757 x11->xv_port = xv_p;
758 mp_msg(MSGT_VO, MSGL_V,
759 "[VO_XV] Using Xv Adapter #%d (%s)\n",
760 i, ctx->ai[i].name);
761 break;
762 } else {
763 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_CouldNotGrabPort,
764 (int) xv_p);
765 ++busy_ports;
769 if (!x11->xv_port) {
770 if (busy_ports)
771 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_CouldNotFindFreePort);
772 else
773 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_NoXvideoSupport);
774 goto error;
777 if (!vo_xv_init_colorkey(vo)) {
778 goto error; // bail out, colorkey setup failed
780 vo_xv_enable_vsync(vo);
781 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
783 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
784 (int *) &ctx->formats);
786 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
787 x11_fd_callback, NULL, vo);
788 ctx->event_fd_registered = 1;
789 return 0;
791 error:
792 uninit(vo); // free resources
793 return -1;
796 static int control(struct vo *vo, uint32_t request, void *data)
798 struct xvctx *ctx = vo->priv;
799 struct vo_x11_state *x11 = vo->x11;
800 switch (request) {
801 case VOCTRL_PAUSE:
802 return (ctx->is_paused = 1);
803 case VOCTRL_RESUME:
804 return (ctx->is_paused = 0);
805 case VOCTRL_QUERY_FORMAT:
806 return query_format(ctx, *((uint32_t *) data));
807 case VOCTRL_GET_IMAGE:
808 return get_image(ctx, data);
809 case VOCTRL_DRAW_IMAGE:
810 return draw_image(vo, data);
811 case VOCTRL_GUISUPPORT:
812 return VO_TRUE;
813 case VOCTRL_GET_PANSCAN:
814 if (!vo->config_ok || !vo_fs)
815 return VO_FALSE;
816 return VO_TRUE;
817 case VOCTRL_FULLSCREEN:
818 vo_x11_fullscreen(vo);
819 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
820 case VOCTRL_SET_PANSCAN:
821 if ((vo_fs && (vo_panscan != vo->panscan_amount))
822 || (!vo_fs && vo->panscan_amount)) {
823 int old_y = vo->panscan_y;
825 panscan_calc(vo);
827 if (old_y != vo->panscan_y) {
828 resize(vo);
829 flip_page(vo);
832 return VO_TRUE;
833 case VOCTRL_SET_EQUALIZER:
835 struct voctrl_set_equalizer_args *args = data;
836 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
838 case VOCTRL_GET_EQUALIZER:
840 struct voctrl_get_equalizer_args *args = data;
841 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
843 case VOCTRL_ONTOP:
844 vo_x11_ontop(vo);
845 return VO_TRUE;
846 case VOCTRL_UPDATE_SCREENINFO:
847 update_xinerama_info(vo);
848 return VO_TRUE;
849 case VOCTRL_REDRAW_OSD:
850 return redraw_osd(vo, data);
852 return VO_NOTIMPL;
855 const struct vo_driver video_out_xv = {
856 .is_new = 1,
857 .info = &info,
858 .preinit = preinit,
859 .config = config,
860 .control = control,
861 .draw_frame = draw_frame,
862 .draw_slice = draw_slice,
863 .draw_osd = draw_osd,
864 .flip_page = flip_page,
865 .check_events = check_events,
866 .uninit = uninit