VO: Don't reset pause status in VO config() functions
[mplayer/glamo.git] / libvo / vo_xv.c
blob6f38c909295d99b4fd36c289c44f7af59174d0d3
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->visible_buf = -1;
227 ctx->have_visible_image_copy = false;
228 ctx->have_next_image_copy = false;
230 /* check image formats */
231 ctx->xv_format = 0;
232 for (i = 0; i < ctx->formats; i++) {
233 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
234 ctx->fo[i].id, (char *) &ctx->fo[i].id,
235 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
236 if (ctx->fo[i].id == format)
237 ctx->xv_format = ctx->fo[i].id;
239 if (!ctx->xv_format)
240 return -1;
242 #ifdef CONFIG_GUI
243 if (use_gui)
244 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
245 else
246 #endif
248 #ifdef CONFIG_XF86VM
249 int vm = flags & VOFLAG_MODESWITCHING;
250 if (vm) {
251 vo_vm_switch(vo);
252 ctx->mode_switched = 1;
254 #endif
255 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
256 &attribs);
257 depth = attribs.depth;
258 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
259 depth = 24;
260 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
262 xswa.background_pixel = 0;
263 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND)
264 xswa.background_pixel = x11->xv_colorkey;
265 xswa.border_pixel = 0;
266 xswamask = CWBackPixel | CWBorderPixel;
268 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
269 vo->dheight, flags, CopyFromParent, "xv",
270 title);
271 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
273 #ifdef CONFIG_XF86VM
274 if (vm) {
275 /* Grab the mouse pointer in our window */
276 if (vo_grabpointer)
277 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
278 GrabModeAsync, x11->window, None, CurrentTime);
279 XSetInputFocus(x11->display, x11->window, RevertToNone,
280 CurrentTime);
282 #endif
285 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
286 x11->xv_port);
288 switch (ctx->xv_format) {
289 case IMGFMT_YV12:
290 case IMGFMT_I420:
291 case IMGFMT_IYUV:
292 ctx->draw_alpha_fnc = draw_alpha_yv12;
293 break;
294 case IMGFMT_YUY2:
295 case IMGFMT_YVYU:
296 ctx->draw_alpha_fnc = draw_alpha_yuy2;
297 break;
298 case IMGFMT_UYVY:
299 ctx->draw_alpha_fnc = draw_alpha_uyvy;
300 break;
301 default:
302 ctx->draw_alpha_fnc = draw_alpha_null;
305 // In case config has been called before
306 for (i = 0; i < ctx->total_buffers; i++)
307 deallocate_xvimage(vo, i);
309 ctx->num_buffers =
310 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
311 ctx->total_buffers = ctx->num_buffers + 1;
313 for (i = 0; i < ctx->total_buffers; i++)
314 allocate_xvimage(vo, i);
316 ctx->current_buf = 0;
317 ctx->current_ip_buf = 0;
320 resize(vo);
322 return 0;
325 static void allocate_xvimage(struct vo *vo, int foo)
327 struct xvctx *ctx = vo->priv;
328 struct vo_x11_state *x11 = vo->x11;
330 * allocate XvImages. FIXME: no error checking, without
331 * mit-shm this will bomb... trzing to fix ::atmos
333 #ifdef HAVE_SHM
334 if (x11->display_is_local && XShmQueryExtension(x11->display))
335 ctx->Shmem_Flag = 1;
336 else {
337 ctx->Shmem_Flag = 0;
338 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_LIBVO_XV_SharedMemoryNotSupported);
340 if (ctx->Shmem_Flag) {
341 ctx->xvimage[foo] =
342 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
343 ctx->xv_format, NULL,
344 ctx->image_width, ctx->image_height,
345 &ctx->Shminfo[foo]);
347 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
348 ctx->xvimage[foo]->data_size,
349 IPC_CREAT | 0777);
350 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
352 ctx->Shminfo[foo].readOnly = False;
354 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
355 XShmAttach(x11->display, &ctx->Shminfo[foo]);
356 XSync(x11->display, False);
357 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
358 } else
359 #endif
361 ctx->xvimage[foo] =
362 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
363 ctx->xv_format, NULL, ctx->image_width,
364 ctx->image_height);
365 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
366 XSync(x11->display, False);
368 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
369 return;
372 static void deallocate_xvimage(struct vo *vo, int foo)
374 struct xvctx *ctx = vo->priv;
375 #ifdef HAVE_SHM
376 if (ctx->Shmem_Flag) {
377 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
378 shmdt(ctx->Shminfo[foo].shmaddr);
379 } else
380 #endif
382 free(ctx->xvimage[foo]->data);
384 XFree(ctx->xvimage[foo]);
386 XSync(vo->x11->display, False);
387 return;
390 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
392 struct xvctx *ctx = vo->priv;
393 struct vo_x11_state *x11 = vo->x11;
394 struct vo_rect *src = &ctx->src_rect;
395 struct vo_rect *dst = &ctx->dst_rect;
396 #ifdef HAVE_SHM
397 if (ctx->Shmem_Flag) {
398 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
399 src->left, src->top, src->width, src->height,
400 dst->left, dst->top, dst->width, dst->height,
401 False);
402 } else
403 #endif
405 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
406 src->left, src->top, src->width, src->height,
407 dst->left, dst->top, dst->width, dst->height);
411 // Only copies luma for planar formats as draw_alpha doesn't change others */
412 void copy_backup_image(struct vo *vo, int dest, int src)
414 struct xvctx *ctx = vo->priv;
416 XvImage *vb = ctx->xvimage[dest];
417 XvImage *cp = ctx->xvimage[src];
418 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
419 vb->width, vb->height,
420 vb->pitches[0], cp->pitches[0]);
423 static void check_events(struct vo *vo)
425 struct xvctx *ctx = vo->priv;
426 struct vo_x11_state *x11 = vo->x11;
427 int e = vo_x11_check_events(vo);
429 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
430 resize(vo);
432 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
433 /* did we already draw a buffer */
434 if (ctx->visible_buf != -1) {
435 /* redraw the last visible buffer */
436 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
441 static void draw_osd(struct vo *vo, struct osd_state *osd)
443 struct xvctx *ctx = vo->priv;
445 ctx->osd_objects_drawn = 0;
446 osd_draw_text(osd,
447 ctx->image_width -
448 ctx->image_width * vo->panscan_x / (vo->dwidth +
449 vo->panscan_x),
450 ctx->image_height, ctx->draw_alpha_fnc, vo);
451 if (ctx->osd_objects_drawn)
452 ctx->unchanged_next_image = false;
455 static int redraw_osd(struct vo *vo, struct osd_state *osd)
457 struct xvctx *ctx = vo->priv;
459 if (ctx->have_visible_image_copy)
460 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
461 else if (ctx->unchanged_visible_image) {
462 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
463 ctx->have_visible_image_copy = true;
465 else
466 return false;
467 int temp = ctx->current_buf;
468 ctx->current_buf = ctx->visible_buf;
469 draw_osd(vo, osd);
470 ctx->current_buf = temp;
471 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
472 return true;
475 static void flip_page(struct vo *vo)
477 struct xvctx *ctx = vo->priv;
478 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
480 /* remember the currently visible buffer */
481 ctx->visible_buf = ctx->current_buf;
483 ctx->have_visible_image_copy = ctx->have_next_image_copy;
484 ctx->have_next_image_copy = false;
485 ctx->unchanged_visible_image = ctx->unchanged_next_image;
486 ctx->unchanged_next_image = false;
488 if (ctx->num_buffers > 1) {
489 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
490 ctx->num_buffers);
491 XFlush(vo->x11->display);
492 } else
493 XSync(vo->x11->display, False);
494 return;
497 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
498 int h, int x, int y)
500 struct xvctx *ctx = vo->priv;
501 uint8_t *dst;
502 XvImage *current_image = ctx->xvimage[ctx->current_buf];
504 dst = current_image->data + current_image->offsets[0]
505 + current_image->pitches[0] * y + x;
506 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
508 x /= 2;
509 y /= 2;
510 w /= 2;
511 h /= 2;
513 dst = current_image->data + current_image->offsets[1]
514 + current_image->pitches[1] * y + x;
515 if (ctx->image_format != IMGFMT_YV12)
516 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
517 else
518 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
520 dst = current_image->data + current_image->offsets[2]
521 + current_image->pitches[2] * y + x;
522 if (ctx->image_format == IMGFMT_YV12)
523 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
524 else
525 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
527 return 0;
530 static int draw_frame(struct vo *vo, uint8_t *src[])
532 return VO_ERROR;
535 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
537 struct xvctx *ctx = vo->priv;
539 ctx->have_next_image_copy = false;
541 if (mpi->flags & MP_IMGFLAG_DIRECT)
542 // direct rendering:
543 ctx->current_buf = (int) (mpi->priv); // hack!
544 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
545 ; // done
546 else if (mpi->flags & MP_IMGFLAG_PLANAR)
547 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
548 else if (mpi->flags & MP_IMGFLAG_YUV)
549 // packed YUV:
550 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
551 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
552 mpi->w * (mpi->bpp / 8), mpi->h,
553 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
554 else
555 return false;
557 if (ctx->is_paused) {
558 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
559 ctx->have_next_image_copy = true;
561 ctx->unchanged_next_image = true;
562 return true;
565 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
567 // we shouldn't change current_buf unless we do DR!
568 int buf = ctx->current_buf;
570 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
571 return VO_FALSE; // it is not static
572 if (mpi->imgfmt != ctx->image_format)
573 return VO_FALSE; // needs conversion :(
574 if (mpi->flags & MP_IMGFLAG_READABLE
575 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
576 // reference (I/P) frame of IP or IPB:
577 if (ctx->num_buffers < 2)
578 return VO_FALSE; // not enough
579 ctx->current_ip_buf ^= 1;
580 // for IPB with 2 buffers we can DR only one of the 2 P frames:
581 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
582 && ctx->current_ip_buf)
583 return VO_FALSE;
584 buf = ctx->current_ip_buf;
585 if (mpi->type == MP_IMGTYPE_IPB)
586 ++buf; // preserve space for B
588 if (mpi->height > ctx->xvimage[buf]->height)
589 return VO_FALSE; //buffer to small
590 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
591 return VO_FALSE; //buffer to small
592 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
593 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
594 ctx->current_buf = buf;
595 XvImage *current_image = ctx->xvimage[ctx->current_buf];
596 mpi->planes[0] = current_image->data + current_image->offsets[0];
597 mpi->stride[0] = current_image->pitches[0];
598 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
599 if (mpi->flags & MP_IMGFLAG_PLANAR) {
600 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
601 // I420
602 mpi->planes[1] = current_image->data
603 + current_image->offsets[1];
604 mpi->planes[2] = current_image->data
605 + current_image->offsets[2];
606 mpi->stride[1] = current_image->pitches[1];
607 mpi->stride[2] = current_image->pitches[2];
608 } else {
609 // YV12
610 mpi->planes[1] = current_image->data
611 + current_image->offsets[2];
612 mpi->planes[2] = current_image->data
613 + current_image->offsets[1];
614 mpi->stride[1] = current_image->pitches[2];
615 mpi->stride[2] = current_image->pitches[1];
618 mpi->flags |= MP_IMGFLAG_DIRECT;
619 mpi->priv = (void *) ctx->current_buf;
620 return VO_TRUE;
622 return VO_FALSE;
625 static int query_format(struct xvctx *ctx, uint32_t format)
627 uint32_t i;
628 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
630 /* check image formats */
631 for (i = 0; i < ctx->formats; i++) {
632 if (ctx->fo[i].id == format)
633 return flag; //xv_format = fo[i].id;
635 return 0;
638 static void uninit(struct vo *vo)
640 struct xvctx *ctx = vo->priv;
641 int i;
643 ctx->visible_buf = -1;
644 if (ctx->ai)
645 XvFreeAdaptorInfo(ctx->ai);
646 ctx->ai = NULL;
647 if (ctx->fo) {
648 XFree(ctx->fo);
649 ctx->fo = NULL;
651 for (i = 0; i < ctx->total_buffers; i++)
652 deallocate_xvimage(vo, i);
653 #ifdef CONFIG_XF86VM
654 if (ctx->mode_switched)
655 vo_vm_close(vo);
656 #endif
657 if (ctx->event_fd_registered)
658 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
659 // uninit() shouldn't get called unless initialization went past vo_init()
660 vo_x11_uninit(vo);
663 static int x11_fd_callback(void *ctx, int fd)
665 struct vo *vo = ctx;
666 check_events(vo);
667 return mplayer_get_key(vo->key_fifo, 0);
670 static int preinit(struct vo *vo, const char *arg)
672 XvPortID xv_p;
673 int busy_ports = 0;
674 unsigned int i;
675 strarg_t ck_src_arg = { 0, NULL };
676 strarg_t ck_method_arg = { 0, NULL };
677 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
678 vo->priv = ctx;
679 struct vo_x11_state *x11 = vo->x11;
680 int xv_adaptor = -1;
682 const opt_t subopts[] = {
683 /* name arg type arg var test */
684 {"port", OPT_ARG_INT, &x11->xv_port, (opt_test_f) int_pos},
685 {"adaptor", OPT_ARG_INT, &xv_adaptor, (opt_test_f) int_non_neg},
686 {"ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck},
687 {"ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm},
688 {NULL}
691 x11->xv_port = 0;
693 /* parse suboptions */
694 if (subopt_parse(arg, subopts) != 0) {
695 return -1;
698 /* modify colorkey settings according to the given options */
699 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
701 if (!vo_init(vo))
702 return -1;
704 /* check for Xvideo extension */
705 unsigned int ver, rel, req, ev, err;
706 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
707 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvNotSupportedByX11);
708 goto error;
711 /* check for Xvideo support */
712 if (Success !=
713 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
714 &ctx->adaptors, &ctx->ai)) {
715 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed);
716 goto error;
719 /* check adaptors */
720 if (x11->xv_port) {
721 int port_found;
723 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
724 if ((ctx->ai[i].type & XvInputMask)
725 && (ctx->ai[i].type & XvImageMask)) {
726 for (xv_p = ctx->ai[i].base_id;
727 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
728 ++xv_p) {
729 if (xv_p == x11->xv_port) {
730 port_found = 1;
731 break;
736 if (port_found) {
737 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
738 x11->xv_port = 0;
739 } else {
740 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_InvalidPortParameter);
741 x11->xv_port = 0;
745 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
746 /* check if adaptor number has been specified */
747 if (xv_adaptor != -1 && xv_adaptor != i)
748 continue;
750 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
751 for (xv_p = ctx->ai[i].base_id;
752 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
753 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
754 x11->xv_port = xv_p;
755 mp_msg(MSGT_VO, MSGL_V,
756 "[VO_XV] Using Xv Adapter #%d (%s)\n",
757 i, ctx->ai[i].name);
758 break;
759 } else {
760 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_CouldNotGrabPort,
761 (int) xv_p);
762 ++busy_ports;
766 if (!x11->xv_port) {
767 if (busy_ports)
768 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_CouldNotFindFreePort);
769 else
770 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_NoXvideoSupport);
771 goto error;
774 if (!vo_xv_init_colorkey(vo)) {
775 goto error; // bail out, colorkey setup failed
777 vo_xv_enable_vsync(vo);
778 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
780 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
781 (int *) &ctx->formats);
783 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
784 x11_fd_callback, NULL, vo);
785 ctx->event_fd_registered = 1;
786 return 0;
788 error:
789 uninit(vo); // free resources
790 return -1;
793 static int control(struct vo *vo, uint32_t request, void *data)
795 struct xvctx *ctx = vo->priv;
796 struct vo_x11_state *x11 = vo->x11;
797 switch (request) {
798 case VOCTRL_PAUSE:
799 return (ctx->is_paused = 1);
800 case VOCTRL_RESUME:
801 return (ctx->is_paused = 0);
802 case VOCTRL_QUERY_FORMAT:
803 return query_format(ctx, *((uint32_t *) data));
804 case VOCTRL_GET_IMAGE:
805 return get_image(ctx, data);
806 case VOCTRL_DRAW_IMAGE:
807 return draw_image(vo, data);
808 case VOCTRL_GUISUPPORT:
809 return VO_TRUE;
810 case VOCTRL_GET_PANSCAN:
811 if (!vo->config_ok || !vo_fs)
812 return VO_FALSE;
813 return VO_TRUE;
814 case VOCTRL_FULLSCREEN:
815 vo_x11_fullscreen(vo);
816 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
817 case VOCTRL_SET_PANSCAN:
818 if ((vo_fs && (vo_panscan != vo->panscan_amount))
819 || (!vo_fs && vo->panscan_amount)) {
820 int old_y = vo->panscan_y;
822 panscan_calc(vo);
824 if (old_y != vo->panscan_y) {
825 resize(vo);
826 flip_page(vo);
829 return VO_TRUE;
830 case VOCTRL_SET_EQUALIZER:
832 struct voctrl_set_equalizer_args *args = data;
833 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
835 case VOCTRL_GET_EQUALIZER:
837 struct voctrl_get_equalizer_args *args = data;
838 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
840 case VOCTRL_ONTOP:
841 vo_x11_ontop(vo);
842 return VO_TRUE;
843 case VOCTRL_UPDATE_SCREENINFO:
844 update_xinerama_info(vo);
845 return VO_TRUE;
846 case VOCTRL_REDRAW_OSD:
847 return redraw_osd(vo, data);
849 return VO_NOTIMPL;
852 const struct vo_driver video_out_xv = {
853 .is_new = 1,
854 .info = &info,
855 .preinit = preinit,
856 .config = config,
857 .control = control,
858 .draw_frame = draw_frame,
859 .draw_slice = draw_slice,
860 .draw_osd = draw_osd,
861 .flip_page = flip_page,
862 .check_events = check_events,
863 .uninit = uninit