Merge svn changes up to r28310
[mplayer/glamo.git] / libvo / vo_xv.c
blob541c6521ca5ba16c2eec5399223553b0a9b7b9cc
1 /* vo_xv.c, X11 Xv interface */
3 // Number of buffers _FOR_DOUBLEBUFFERING_MODE_
4 // Use option -double to enable double buffering! (default: single buffer)
5 #define NUM_BUFFERS 3
7 /*
8 Buffer allocation:
10 -nodr:
11 1: TEMP
12 2: 2*TEMP
14 -dr:
15 1: TEMP
16 3: 2*STATIC+TEMP
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <stdbool.h>
25 #include "config.h"
26 #include "options.h"
27 #include "talloc.h"
28 #include "mp_msg.h"
29 #include "help_mp.h"
30 #include "video_out.h"
31 #include "libmpcodecs/vfcap.h"
32 #include "libmpcodecs/mp_image.h"
33 #include "osd.h"
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #include <errno.h>
39 #include "x11_common.h"
41 #include "fastmemcpy.h"
42 #include "sub.h"
43 #include "aspect.h"
45 #include "subopt-helper.h"
47 #include "input/input.h"
48 #include "mp_fifo.h"
50 #ifdef CONFIG_GUI
51 #include "gui/interface.h"
52 #endif
54 #include "libavutil/common.h"
56 static const vo_info_t info = {
57 "X11/Xv",
58 "xv",
59 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
63 #ifdef HAVE_SHM
64 #include <sys/ipc.h>
65 #include <sys/shm.h>
66 #include <X11/extensions/XShm.h>
67 #endif
69 // Note: depends on the inclusion of X11/extensions/XShm.h
70 #include <X11/extensions/Xv.h>
71 #include <X11/extensions/Xvlib.h>
73 struct xvctx {
74 XvAdaptorInfo *ai;
75 XvImageFormatValues *fo;
76 unsigned int formats, adaptors, xv_format;
77 int current_buf;
78 int current_ip_buf;
79 int num_buffers;
80 int total_buffers;
81 int have_visible_image_copy;
82 int have_next_image_copy;
83 int unchanged_visible_image;
84 int unchanged_next_image;
85 int visible_buf;
86 XvImage *xvimage[NUM_BUFFERS + 1];
87 uint32_t image_width;
88 uint32_t image_height;
89 uint32_t image_format;
90 int is_paused;
91 uint32_t drwX, drwY;
92 uint32_t max_width, max_height; // zero means: not set
93 int event_fd_registered; // for uninit called from preinit
94 int mode_switched;
95 int osd_objects_drawn;
96 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
97 unsigned char *src, unsigned char *srca,
98 int stride);
99 #ifdef HAVE_SHM
100 XShmSegmentInfo Shminfo[NUM_BUFFERS];
101 int Shmem_Flag;
102 #endif
105 static void allocate_xvimage(struct vo *, int);
108 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
109 unsigned char *src, unsigned char *srca,
110 int stride)
112 struct vo *vo = p;
113 struct xvctx *ctx = vo->priv;
114 x0 += ctx->image_width * (vo->panscan_x >> 1)
115 / (vo->dwidth + vo->panscan_x);
116 vo_draw_alpha_yv12(w, h, src, srca, stride,
117 ctx->xvimage[ctx->current_buf]->data +
118 ctx->xvimage[ctx->current_buf]->offsets[0] +
119 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
120 ctx->xvimage[ctx->current_buf]->pitches[0]);
121 ctx->osd_objects_drawn++;
124 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
125 unsigned char *src, unsigned char *srca,
126 int stride)
128 struct vo *vo = p;
129 struct xvctx *ctx = vo->priv;
130 x0 += ctx->image_width * (vo->panscan_x >> 1)
131 / (vo->dwidth + vo->panscan_x);
132 vo_draw_alpha_yuy2(w, h, src, srca, stride,
133 ctx->xvimage[ctx->current_buf]->data +
134 ctx->xvimage[ctx->current_buf]->offsets[0] +
135 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
136 ctx->xvimage[ctx->current_buf]->pitches[0]);
137 ctx->osd_objects_drawn++;
140 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
141 unsigned char *src, unsigned char *srca,
142 int stride)
144 struct vo *vo = p;
145 struct xvctx *ctx = vo->priv;
146 x0 += ctx->image_width * (vo->panscan_x >> 1)
147 / (vo->dwidth + vo->panscan_x);
148 vo_draw_alpha_yuy2(w, h, src, srca, stride,
149 ctx->xvimage[ctx->current_buf]->data +
150 ctx->xvimage[ctx->current_buf]->offsets[0] +
151 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
152 ctx->xvimage[ctx->current_buf]->pitches[0]);
153 ctx->osd_objects_drawn++;
156 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
157 unsigned char *src, unsigned char *srca,
158 int stride)
163 static void deallocate_xvimage(struct vo *vo, int foo);
166 * connect to server, create and map window,
167 * allocate colors and (shared) memory
169 static int config(struct vo *vo, uint32_t width, uint32_t height,
170 uint32_t d_width, uint32_t d_height, uint32_t flags,
171 char *title, uint32_t format)
173 struct MPOpts *opts = vo->opts;
174 struct vo_x11_state *x11 = vo->x11;
175 XVisualInfo vinfo;
176 XSetWindowAttributes xswa;
177 XWindowAttributes attribs;
178 unsigned long xswamask;
179 int depth;
180 struct xvctx *ctx = vo->priv;
181 int i;
183 ctx->image_height = height;
184 ctx->image_width = width;
185 ctx->image_format = format;
187 if ((ctx->max_width != 0 && ctx->max_height != 0)
188 && (ctx->image_width > ctx->max_width
189 || ctx->image_height > ctx->max_height)) {
190 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_VO_XV_ImagedimTooHigh,
191 ctx->image_width, ctx->image_height, ctx->max_width,
192 ctx->max_height);
193 return -1;
196 ctx->is_paused = 0;
197 ctx->visible_buf = -1;
198 ctx->have_visible_image_copy = false;
199 ctx->have_next_image_copy = false;
201 /* check image formats */
202 ctx->xv_format = 0;
203 for (i = 0; i < ctx->formats; i++) {
204 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
205 ctx->fo[i].id, (char *) &ctx->fo[i].id,
206 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
207 if (ctx->fo[i].id == format)
208 ctx->xv_format = ctx->fo[i].id;
210 if (!ctx->xv_format)
211 return -1;
213 #ifdef CONFIG_GUI
214 if (use_gui)
215 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
216 else
217 #endif
219 #ifdef CONFIG_XF86VM
220 int vm = flags & VOFLAG_MODESWITCHING;
221 if (vm) {
222 vo_vm_switch(vo);
223 ctx->mode_switched = 1;
225 #endif
226 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
227 &attribs);
228 depth = attribs.depth;
229 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
230 depth = 24;
231 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
233 xswa.background_pixel = 0;
234 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND)
235 xswa.background_pixel = x11->xv_colorkey;
236 xswa.border_pixel = 0;
237 xswamask = CWBackPixel | CWBorderPixel;
239 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
240 vo->dheight, flags, CopyFromParent, "xv",
241 title);
242 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
244 #ifdef CONFIG_XF86VM
245 if (vm) {
246 /* Grab the mouse pointer in our window */
247 if (vo_grabpointer)
248 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
249 GrabModeAsync, x11->window, None, CurrentTime);
250 XSetInputFocus(x11->display, x11->window, RevertToNone,
251 CurrentTime);
253 #endif
256 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
257 x11->xv_port);
259 switch (ctx->xv_format) {
260 case IMGFMT_YV12:
261 case IMGFMT_I420:
262 case IMGFMT_IYUV:
263 ctx->draw_alpha_fnc = draw_alpha_yv12;
264 break;
265 case IMGFMT_YUY2:
266 case IMGFMT_YVYU:
267 ctx->draw_alpha_fnc = draw_alpha_yuy2;
268 break;
269 case IMGFMT_UYVY:
270 ctx->draw_alpha_fnc = draw_alpha_uyvy;
271 break;
272 default:
273 ctx->draw_alpha_fnc = draw_alpha_null;
276 // In case config has been called before
277 for (i = 0; i < ctx->total_buffers; i++)
278 deallocate_xvimage(vo, i);
280 ctx->num_buffers =
281 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
282 ctx->total_buffers = ctx->num_buffers + 1;
284 for (i = 0; i < ctx->total_buffers; i++)
285 allocate_xvimage(vo, i);
287 ctx->current_buf = 0;
288 ctx->current_ip_buf = 0;
290 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
291 vo_fs = 1;
292 vo_calc_drwXY(vo, &ctx->drwX, &ctx->drwY);
294 panscan_calc(vo);
296 vo_xv_draw_colorkey(vo, ctx->drwX - (vo->panscan_x >> 1),
297 ctx->drwY - (vo->panscan_y >> 1),
298 vo->dwidth + vo->panscan_x - 1,
299 vo->dheight + vo->panscan_y - 1);
301 mp_msg(MSGT_VO, MSGL_V, "[xv] dx: %d dy: %d dw: %d dh: %d\n", ctx->drwX,
302 ctx->drwY, vo->dwidth, vo->dheight);
304 return 0;
307 static void allocate_xvimage(struct vo *vo, int foo)
309 struct xvctx *ctx = vo->priv;
310 struct vo_x11_state *x11 = vo->x11;
312 * allocate XvImages. FIXME: no error checking, without
313 * mit-shm this will bomb... trzing to fix ::atmos
315 #ifdef HAVE_SHM
316 if (x11->display_is_local && XShmQueryExtension(x11->display))
317 ctx->Shmem_Flag = 1;
318 else {
319 ctx->Shmem_Flag = 0;
320 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_LIBVO_XV_SharedMemoryNotSupported);
322 if (ctx->Shmem_Flag) {
323 ctx->xvimage[foo] =
324 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
325 ctx->xv_format, NULL,
326 ctx->image_width, ctx->image_height,
327 &ctx->Shminfo[foo]);
329 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
330 ctx->xvimage[foo]->data_size,
331 IPC_CREAT | 0777);
332 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
334 ctx->Shminfo[foo].readOnly = False;
336 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
337 XShmAttach(x11->display, &ctx->Shminfo[foo]);
338 XSync(x11->display, False);
339 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
340 } else
341 #endif
343 ctx->xvimage[foo] =
344 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
345 ctx->xv_format, NULL, ctx->image_width,
346 ctx->image_height);
347 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
348 XSync(x11->display, False);
350 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
351 return;
354 static void deallocate_xvimage(struct vo *vo, int foo)
356 struct xvctx *ctx = vo->priv;
357 #ifdef HAVE_SHM
358 if (ctx->Shmem_Flag) {
359 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
360 shmdt(ctx->Shminfo[foo].shmaddr);
361 } else
362 #endif
364 free(ctx->xvimage[foo]->data);
366 XFree(ctx->xvimage[foo]);
368 XSync(vo->x11->display, False);
369 return;
372 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
374 struct xvctx *ctx = vo->priv;
375 struct vo_x11_state *x11 = vo->x11;
376 #ifdef HAVE_SHM
377 if (ctx->Shmem_Flag) {
378 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
379 0, 0, ctx->image_width, ctx->image_height,
380 ctx->drwX - (vo->panscan_x >> 1),
381 ctx->drwY - (vo->panscan_y >> 1),
382 vo->dwidth + vo->panscan_x, vo->dheight + vo->panscan_y,
383 False);
384 } else
385 #endif
387 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi, 0,
388 0, ctx->image_width, ctx->image_height,
389 ctx->drwX - (vo->panscan_x >> 1),
390 ctx->drwY - (vo->panscan_y >> 1),
391 vo->dwidth + vo->panscan_x, vo->dheight + vo->panscan_y);
395 // Only copies luma for planar formats as draw_alpha doesn't change others */
396 void copy_backup_image(struct vo *vo, int dest, int src)
398 struct xvctx *ctx = vo->priv;
400 XvImage *vb = ctx->xvimage[dest];
401 XvImage *cp = ctx->xvimage[src];
402 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
403 vb->width, vb->height,
404 vb->pitches[0], cp->pitches[0]);
407 static void check_events(struct vo *vo)
409 struct xvctx *ctx = vo->priv;
410 struct vo_x11_state *x11 = vo->x11;
411 int e = vo_x11_check_events(vo);
413 if (e & VO_EVENT_RESIZE)
414 vo_calc_drwXY(vo, &ctx->drwX, &ctx->drwY);
416 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) {
417 vo_xv_draw_colorkey(vo, ctx->drwX - (vo->panscan_x >> 1),
418 ctx->drwY - (vo->panscan_y >> 1),
419 vo->dwidth + vo->panscan_x - 1,
420 vo->dheight + vo->panscan_y - 1);
423 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
424 /* did we already draw a buffer */
425 if (ctx->visible_buf != -1) {
426 /* redraw the last visible buffer */
427 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
432 static void draw_osd(struct vo *vo, struct osd_state *osd)
434 struct xvctx *ctx = vo->priv;
436 ctx->osd_objects_drawn = 0;
437 osd_draw_text(osd,
438 ctx->image_width -
439 ctx->image_width * vo->panscan_x / (vo->dwidth +
440 vo->panscan_x),
441 ctx->image_height, ctx->draw_alpha_fnc, vo);
442 if (ctx->osd_objects_drawn)
443 ctx->unchanged_next_image = false;
446 static int redraw_osd(struct vo *vo, struct osd_state *osd)
448 struct xvctx *ctx = vo->priv;
450 if (ctx->have_visible_image_copy)
451 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
452 else if (ctx->unchanged_visible_image) {
453 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
454 ctx->have_visible_image_copy = true;
456 else
457 return false;
458 int temp = ctx->current_buf;
459 ctx->current_buf = ctx->visible_buf;
460 draw_osd(vo, osd);
461 ctx->current_buf = temp;
462 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
463 return true;
466 static void flip_page(struct vo *vo)
468 struct xvctx *ctx = vo->priv;
469 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
471 /* remember the currently visible buffer */
472 ctx->visible_buf = ctx->current_buf;
474 ctx->have_visible_image_copy = ctx->have_next_image_copy;
475 ctx->have_next_image_copy = false;
476 ctx->unchanged_visible_image = ctx->unchanged_next_image;
477 ctx->unchanged_next_image = false;
479 if (ctx->num_buffers > 1) {
480 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
481 ctx->num_buffers);
482 XFlush(vo->x11->display);
483 } else
484 XSync(vo->x11->display, False);
485 return;
488 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
489 int h, int x, int y)
491 struct xvctx *ctx = vo->priv;
492 uint8_t *dst;
493 XvImage *current_image = ctx->xvimage[ctx->current_buf];
495 dst = current_image->data + current_image->offsets[0]
496 + current_image->pitches[0] * y + x;
497 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
499 x /= 2;
500 y /= 2;
501 w /= 2;
502 h /= 2;
504 dst = current_image->data + current_image->offsets[1]
505 + current_image->pitches[1] * y + x;
506 if (ctx->image_format != IMGFMT_YV12)
507 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
508 else
509 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
511 dst = current_image->data + current_image->offsets[2]
512 + current_image->pitches[2] * y + x;
513 if (ctx->image_format == IMGFMT_YV12)
514 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
515 else
516 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
518 return 0;
521 static int draw_frame(struct vo *vo, uint8_t *src[])
523 return VO_ERROR;
526 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
528 struct xvctx *ctx = vo->priv;
530 ctx->have_next_image_copy = false;
532 if (mpi->flags & MP_IMGFLAG_DIRECT)
533 // direct rendering:
534 ctx->current_buf = (int) (mpi->priv); // hack!
535 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
536 ; // done
537 else if (mpi->flags & MP_IMGFLAG_PLANAR)
538 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
539 else if (mpi->flags & MP_IMGFLAG_YUV)
540 // packed YUV:
541 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
542 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
543 mpi->w * (mpi->bpp / 8), mpi->h,
544 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
545 else
546 return false;
548 if (ctx->is_paused) {
549 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
550 ctx->have_next_image_copy = true;
552 ctx->unchanged_next_image = true;
553 return true;
556 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
558 // we shouldn't change current_buf unless we do DR!
559 int buf = ctx->current_buf;
561 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
562 return VO_FALSE; // it is not static
563 if (mpi->imgfmt != ctx->image_format)
564 return VO_FALSE; // needs conversion :(
565 if (mpi->flags & MP_IMGFLAG_READABLE
566 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
567 // reference (I/P) frame of IP or IPB:
568 if (ctx->num_buffers < 2)
569 return VO_FALSE; // not enough
570 ctx->current_ip_buf ^= 1;
571 // for IPB with 2 buffers we can DR only one of the 2 P frames:
572 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
573 && ctx->current_ip_buf)
574 return VO_FALSE;
575 buf = ctx->current_ip_buf;
576 if (mpi->type == MP_IMGTYPE_IPB)
577 ++buf; // preserve space for B
579 if (mpi->height > ctx->xvimage[buf]->height)
580 return VO_FALSE; //buffer to small
581 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
582 return VO_FALSE; //buffer to small
583 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
584 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
585 ctx->current_buf = buf;
586 XvImage *current_image = ctx->xvimage[ctx->current_buf];
587 mpi->planes[0] = current_image->data + current_image->offsets[0];
588 mpi->stride[0] = current_image->pitches[0];
589 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
590 if (mpi->flags & MP_IMGFLAG_PLANAR) {
591 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
592 // I420
593 mpi->planes[1] = current_image->data
594 + current_image->offsets[1];
595 mpi->planes[2] = current_image->data
596 + current_image->offsets[2];
597 mpi->stride[1] = current_image->pitches[1];
598 mpi->stride[2] = current_image->pitches[2];
599 } else {
600 // YV12
601 mpi->planes[1] = current_image->data
602 + current_image->offsets[2];
603 mpi->planes[2] = current_image->data
604 + current_image->offsets[1];
605 mpi->stride[1] = current_image->pitches[2];
606 mpi->stride[2] = current_image->pitches[1];
609 mpi->flags |= MP_IMGFLAG_DIRECT;
610 mpi->priv = (void *) ctx->current_buf;
611 return VO_TRUE;
613 return VO_FALSE;
616 static int query_format(struct xvctx *ctx, uint32_t format)
618 uint32_t i;
619 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
621 /* check image formats */
622 for (i = 0; i < ctx->formats; i++) {
623 if (ctx->fo[i].id == format)
624 return flag; //xv_format = fo[i].id;
626 return 0;
629 static void uninit(struct vo *vo)
631 struct xvctx *ctx = vo->priv;
632 int i;
634 ctx->visible_buf = -1;
635 if (ctx->ai)
636 XvFreeAdaptorInfo(ctx->ai);
637 ctx->ai = NULL;
638 if (ctx->fo) {
639 XFree(ctx->fo);
640 ctx->fo = NULL;
642 for (i = 0; i < ctx->total_buffers; i++)
643 deallocate_xvimage(vo, i);
644 #ifdef CONFIG_XF86VM
645 if (ctx->mode_switched)
646 vo_vm_close(vo);
647 #endif
648 if (ctx->event_fd_registered)
649 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
650 // uninit() shouldn't get called unless initialization went past vo_init()
651 vo_x11_uninit(vo);
654 static int x11_fd_callback(void *ctx, int fd)
656 struct vo *vo = ctx;
657 check_events(vo);
658 return mplayer_get_key(vo->key_fifo, 0);
661 static int preinit(struct vo *vo, const char *arg)
663 XvPortID xv_p;
664 int busy_ports = 0;
665 unsigned int i;
666 strarg_t ck_src_arg = { 0, NULL };
667 strarg_t ck_method_arg = { 0, NULL };
668 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
669 vo->priv = ctx;
670 struct vo_x11_state *x11 = vo->x11;
671 int xv_adaptor = -1;
673 opt_t subopts[] = {
674 /* name arg type arg var test */
675 {"port", OPT_ARG_INT, &x11->xv_port, (opt_test_f) int_pos},
676 {"adaptor", OPT_ARG_INT, &xv_adaptor, (opt_test_f) int_non_neg},
677 {"ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck},
678 {"ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm},
679 {NULL}
682 x11->xv_port = 0;
684 /* parse suboptions */
685 if (subopt_parse(arg, subopts) != 0) {
686 return -1;
689 /* modify colorkey settings according to the given options */
690 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
692 if (!vo_init(vo))
693 return -1;
695 /* check for Xvideo extension */
696 unsigned int ver, rel, req, ev, err;
697 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
698 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvNotSupportedByX11);
699 goto error;
702 /* check for Xvideo support */
703 if (Success !=
704 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
705 &ctx->adaptors, &ctx->ai)) {
706 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed);
707 goto error;
710 /* check adaptors */
711 if (x11->xv_port) {
712 int port_found;
714 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
715 if ((ctx->ai[i].type & XvInputMask)
716 && (ctx->ai[i].type & XvImageMask)) {
717 for (xv_p = ctx->ai[i].base_id;
718 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
719 ++xv_p) {
720 if (xv_p == x11->xv_port) {
721 port_found = 1;
722 break;
727 if (port_found) {
728 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
729 x11->xv_port = 0;
730 } else {
731 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_InvalidPortParameter);
732 x11->xv_port = 0;
736 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
737 /* check if adaptor number has been specified */
738 if (xv_adaptor != -1 && xv_adaptor != i)
739 continue;
741 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
742 for (xv_p = ctx->ai[i].base_id;
743 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
744 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
745 x11->xv_port = xv_p;
746 mp_msg(MSGT_VO, MSGL_V,
747 "[VO_XV] Using Xv Adapter #%d (%s)\n",
748 i, ctx->ai[i].name);
749 break;
750 } else {
751 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_CouldNotGrabPort,
752 (int) xv_p);
753 ++busy_ports;
757 if (!x11->xv_port) {
758 if (busy_ports)
759 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_CouldNotFindFreePort);
760 else
761 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_NoXvideoSupport);
762 goto error;
765 if (!vo_xv_init_colorkey(vo)) {
766 goto error; // bail out, colorkey setup failed
768 vo_xv_enable_vsync(vo);
769 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
771 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
772 (int *) &ctx->formats);
774 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
775 x11_fd_callback, NULL, vo);
776 ctx->event_fd_registered = 1;
777 return 0;
779 error:
780 uninit(vo); // free resources
781 return -1;
784 static int control(struct vo *vo, uint32_t request, void *data)
786 struct xvctx *ctx = vo->priv;
787 struct vo_x11_state *x11 = vo->x11;
788 switch (request) {
789 case VOCTRL_PAUSE:
790 return (ctx->is_paused = 1);
791 case VOCTRL_RESUME:
792 return (ctx->is_paused = 0);
793 case VOCTRL_QUERY_FORMAT:
794 return query_format(ctx, *((uint32_t *) data));
795 case VOCTRL_GET_IMAGE:
796 return get_image(ctx, data);
797 case VOCTRL_DRAW_IMAGE:
798 return draw_image(vo, data);
799 case VOCTRL_GUISUPPORT:
800 return VO_TRUE;
801 case VOCTRL_GET_PANSCAN:
802 if (!vo->config_ok || !vo_fs)
803 return VO_FALSE;
804 return VO_TRUE;
805 case VOCTRL_FULLSCREEN:
806 vo_x11_fullscreen(vo);
807 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
808 case VOCTRL_SET_PANSCAN:
809 if ((vo_fs && (vo_panscan != vo->panscan_amount))
810 || (!vo_fs && vo->panscan_amount)) {
811 int old_y = vo->panscan_y;
813 panscan_calc(vo);
815 if (old_y != vo->panscan_y) {
816 vo_x11_clearwindow_part(vo, x11->window,
817 vo->dwidth + vo->panscan_x - 1,
818 vo->dheight + vo->panscan_y - 1, 1);
819 vo_xv_draw_colorkey(vo, ctx->drwX - (vo->panscan_x >> 1),
820 ctx->drwY - (vo->panscan_y >> 1),
821 vo->dwidth + vo->panscan_x - 1,
822 vo->dheight + vo->panscan_y - 1);
823 flip_page(vo);
826 return VO_TRUE;
827 case VOCTRL_SET_EQUALIZER:
829 struct voctrl_set_equalizer_args *args = data;
830 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
832 case VOCTRL_GET_EQUALIZER:
834 struct voctrl_get_equalizer_args *args = data;
835 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
837 case VOCTRL_ONTOP:
838 vo_x11_ontop(vo);
839 return VO_TRUE;
840 case VOCTRL_UPDATE_SCREENINFO:
841 update_xinerama_info(vo);
842 return VO_TRUE;
843 case VOCTRL_REDRAW_OSD:
844 return redraw_osd(vo, data);
846 return VO_NOTIMPL;
849 const struct vo_driver video_out_xv = {
850 .is_new = 1,
851 .info = &info,
852 .preinit = preinit,
853 .config = config,
854 .control = control,
855 .draw_frame = draw_frame,
856 .draw_slice = draw_slice,
857 .draw_osd = draw_osd,
858 .flip_page = flip_page,
859 .check_events = check_events,
860 .uninit = uninit