mixer: fix lowering hw volume while muted
[mplayer.git] / libvo / vo_xv.c
blobe7377547262cafed43811a981379210a7afe1f39
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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <errno.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
30 #include <libavutil/common.h>
32 #include "config.h"
34 #ifdef HAVE_SHM
35 #include <sys/ipc.h>
36 #include <sys/shm.h>
37 #include <X11/extensions/XShm.h>
38 #endif
40 // Note: depends on the inclusion of X11/extensions/XShm.h
41 #include <X11/extensions/Xv.h>
42 #include <X11/extensions/Xvlib.h>
44 #include "options.h"
45 #include "talloc.h"
46 #include "mp_msg.h"
47 #include "video_out.h"
48 #include "libmpcodecs/vfcap.h"
49 #include "libmpcodecs/mp_image.h"
50 #include "osd.h"
51 #include "x11_common.h"
52 #include "fastmemcpy.h"
53 #include "sub/sub.h"
54 #include "aspect.h"
55 #include "csputils.h"
56 #include "subopt-helper.h"
58 static const vo_info_t info = {
59 "X11/Xv",
60 "xv",
61 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
65 struct xvctx {
66 XvAdaptorInfo *ai;
67 XvImageFormatValues *fo;
68 unsigned int formats, adaptors, xv_format;
69 int current_buf;
70 int current_ip_buf;
71 int num_buffers;
72 int total_buffers;
73 bool have_image_copy;
74 bool unchanged_image;
75 int visible_buf;
76 XvImage *xvimage[2 + 1];
77 uint32_t image_width;
78 uint32_t image_height;
79 uint32_t image_format;
80 uint32_t image_d_width;
81 uint32_t image_d_height;
82 int is_paused;
83 struct vo_rect src_rect;
84 struct vo_rect dst_rect;
85 uint32_t max_width, max_height; // zero means: not set
86 int mode_switched;
87 int osd_objects_drawn;
88 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
89 unsigned char *src, unsigned char *srca,
90 int stride);
91 #ifdef HAVE_SHM
92 XShmSegmentInfo Shminfo[2 + 1];
93 int Shmem_Flag;
94 #endif
97 static void allocate_xvimage(struct vo *, int);
100 static void fixup_osd_position(struct vo *vo, int *x0, int *y0, int *w, int *h)
102 struct xvctx *ctx = vo->priv;
103 *x0 += ctx->image_width * (vo->panscan_x >> 1)
104 / (vo->dwidth + vo->panscan_x);
105 *w = av_clip(*w, 0, ctx->image_width);
106 *h = av_clip(*h, 0, ctx->image_height);
107 *x0 = FFMIN(*x0, ctx->image_width - *w);
108 *y0 = FFMIN(*y0, ctx->image_height - *h);
111 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
112 unsigned char *src, unsigned char *srca,
113 int stride)
115 struct vo *vo = p;
116 struct xvctx *ctx = vo->priv;
117 fixup_osd_position(vo, &x0, &y0, &w, &h);
118 vo_draw_alpha_yv12(w, h, src, srca, stride,
119 ctx->xvimage[ctx->current_buf]->data +
120 ctx->xvimage[ctx->current_buf]->offsets[0] +
121 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
122 ctx->xvimage[ctx->current_buf]->pitches[0]);
123 ctx->osd_objects_drawn++;
126 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
127 unsigned char *src, unsigned char *srca,
128 int stride)
130 struct vo *vo = p;
131 struct xvctx *ctx = vo->priv;
132 fixup_osd_position(vo, &x0, &y0, &w, &h);
133 vo_draw_alpha_yuy2(w, h, src, srca, stride,
134 ctx->xvimage[ctx->current_buf]->data +
135 ctx->xvimage[ctx->current_buf]->offsets[0] +
136 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
137 ctx->xvimage[ctx->current_buf]->pitches[0]);
138 ctx->osd_objects_drawn++;
141 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
142 unsigned char *src, unsigned char *srca,
143 int stride)
145 struct vo *vo = p;
146 struct xvctx *ctx = vo->priv;
147 fixup_osd_position(vo, &x0, &y0, &w, &h);
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);
165 static void resize(struct vo *vo)
167 struct xvctx *ctx = vo->priv;
169 calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
170 &ctx->dst_rect, NULL, NULL);
171 struct vo_rect *dst = &ctx->dst_rect;
172 vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height);
173 vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
177 * connect to server, create and map window,
178 * allocate colors and (shared) memory
180 static int config(struct vo *vo, uint32_t width, uint32_t height,
181 uint32_t d_width, uint32_t d_height, uint32_t flags,
182 uint32_t format)
184 struct vo_x11_state *x11 = vo->x11;
185 XVisualInfo vinfo;
186 XSetWindowAttributes xswa;
187 XWindowAttributes attribs;
188 unsigned long xswamask;
189 int depth;
190 struct xvctx *ctx = vo->priv;
191 int i;
193 ctx->image_height = height;
194 ctx->image_width = width;
195 ctx->image_format = format;
196 ctx->image_d_width = d_width;
197 ctx->image_d_height = d_height;
199 if ((ctx->max_width != 0 && ctx->max_height != 0)
200 && (ctx->image_width > ctx->max_width
201 || ctx->image_height > ctx->max_height)) {
202 mp_tmsg(MSGT_VO, MSGL_ERR, "Source image dimensions are too high: %ux%u (maximum is %ux%u)\n",
203 ctx->image_width, ctx->image_height, ctx->max_width,
204 ctx->max_height);
205 return -1;
208 ctx->visible_buf = -1;
209 ctx->have_image_copy = false;
211 /* check image formats */
212 ctx->xv_format = 0;
213 for (i = 0; i < ctx->formats; i++) {
214 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
215 ctx->fo[i].id, (char *) &ctx->fo[i].id,
216 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
217 if (ctx->fo[i].id == format)
218 ctx->xv_format = ctx->fo[i].id;
220 if (!ctx->xv_format)
221 return -1;
224 #ifdef CONFIG_XF86VM
225 int vm = flags & VOFLAG_MODESWITCHING;
226 if (vm) {
227 vo_vm_switch(vo);
228 ctx->mode_switched = 1;
230 #endif
231 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
232 &attribs);
233 depth = attribs.depth;
234 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
235 depth = 24;
236 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
238 xswa.border_pixel = 0;
239 xswamask = CWBorderPixel;
240 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND) {
241 xswa.background_pixel = x11->xv_colorkey;
242 xswamask |= CWBackPixel;
245 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
246 vo->dheight, flags, CopyFromParent, "xv");
247 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
249 #ifdef CONFIG_XF86VM
250 if (vm) {
251 /* Grab the mouse pointer in our window */
252 if (vo_grabpointer)
253 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
254 GrabModeAsync, x11->window, None, CurrentTime);
255 XSetInputFocus(x11->display, x11->window, RevertToNone,
256 CurrentTime);
258 #endif
261 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
262 x11->xv_port);
264 switch (ctx->xv_format) {
265 case IMGFMT_YV12:
266 case IMGFMT_I420:
267 case IMGFMT_IYUV:
268 ctx->draw_alpha_fnc = draw_alpha_yv12;
269 break;
270 case IMGFMT_YUY2:
271 case IMGFMT_YVYU:
272 ctx->draw_alpha_fnc = draw_alpha_yuy2;
273 break;
274 case IMGFMT_UYVY:
275 ctx->draw_alpha_fnc = draw_alpha_uyvy;
276 break;
277 default:
278 ctx->draw_alpha_fnc = draw_alpha_null;
281 // In case config has been called before
282 for (i = 0; i < ctx->total_buffers; i++)
283 deallocate_xvimage(vo, i);
285 ctx->num_buffers = 2;
286 ctx->total_buffers = ctx->num_buffers + 1;
288 for (i = 0; i < ctx->total_buffers; i++)
289 allocate_xvimage(vo, i);
291 ctx->current_buf = 0;
292 ctx->current_ip_buf = 0;
295 resize(vo);
297 return 0;
300 static void allocate_xvimage(struct vo *vo, int foo)
302 struct xvctx *ctx = vo->priv;
303 struct vo_x11_state *x11 = vo->x11;
305 * allocate XvImages. FIXME: no error checking, without
306 * mit-shm this will bomb... trzing to fix ::atmos
308 #ifdef HAVE_SHM
309 if (x11->display_is_local && XShmQueryExtension(x11->display))
310 ctx->Shmem_Flag = 1;
311 else {
312 ctx->Shmem_Flag = 0;
313 mp_tmsg(MSGT_VO, MSGL_INFO, "[VO_XV] Shared memory not supported\nReverting to normal Xv.\n");
315 if (ctx->Shmem_Flag) {
316 ctx->xvimage[foo] =
317 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
318 ctx->xv_format, NULL,
319 ctx->image_width, ctx->image_height,
320 &ctx->Shminfo[foo]);
322 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
323 ctx->xvimage[foo]->data_size,
324 IPC_CREAT | 0777);
325 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
327 ctx->Shminfo[foo].readOnly = False;
329 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
330 XShmAttach(x11->display, &ctx->Shminfo[foo]);
331 XSync(x11->display, False);
332 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
333 } else
334 #endif
336 ctx->xvimage[foo] =
337 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
338 ctx->xv_format, NULL, ctx->image_width,
339 ctx->image_height);
340 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
341 XSync(x11->display, False);
343 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
344 return;
347 static void deallocate_xvimage(struct vo *vo, int foo)
349 struct xvctx *ctx = vo->priv;
350 #ifdef HAVE_SHM
351 if (ctx->Shmem_Flag) {
352 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
353 shmdt(ctx->Shminfo[foo].shmaddr);
354 } else
355 #endif
357 free(ctx->xvimage[foo]->data);
359 XFree(ctx->xvimage[foo]);
361 XSync(vo->x11->display, False);
362 return;
365 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
367 struct xvctx *ctx = vo->priv;
368 struct vo_x11_state *x11 = vo->x11;
369 struct vo_rect *src = &ctx->src_rect;
370 struct vo_rect *dst = &ctx->dst_rect;
371 #ifdef HAVE_SHM
372 if (ctx->Shmem_Flag) {
373 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
374 src->left, src->top, src->width, src->height,
375 dst->left, dst->top, dst->width, dst->height,
376 False);
377 } else
378 #endif
380 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
381 src->left, src->top, src->width, src->height,
382 dst->left, dst->top, dst->width, dst->height);
386 // Only copies luma for planar formats as draw_alpha doesn't change others */
387 static void copy_backup_image(struct vo *vo, int dest, int src)
389 struct xvctx *ctx = vo->priv;
391 XvImage *vb = ctx->xvimage[dest];
392 XvImage *cp = ctx->xvimage[src];
393 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
394 vb->width, vb->height,
395 vb->pitches[0], cp->pitches[0]);
398 static void check_events(struct vo *vo)
400 int e = vo_x11_check_events(vo);
402 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) {
403 resize(vo);
404 vo->want_redraw = true;
408 static void draw_osd(struct vo *vo, struct osd_state *osd)
410 struct xvctx *ctx = vo->priv;
412 ctx->osd_objects_drawn = 0;
413 osd_draw_text(osd,
414 ctx->image_width -
415 ctx->image_width * vo->panscan_x / (vo->dwidth +
416 vo->panscan_x),
417 ctx->image_height, ctx->draw_alpha_fnc, vo);
418 if (ctx->osd_objects_drawn)
419 ctx->unchanged_image = false;
422 static int redraw_frame(struct vo *vo)
424 struct xvctx *ctx = vo->priv;
426 if (ctx->have_image_copy)
427 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
428 else if (ctx->unchanged_image) {
429 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
430 ctx->have_image_copy = true;
431 } else
432 return false;
433 ctx->current_buf = ctx->visible_buf;
434 return true;
437 static void flip_page(struct vo *vo)
439 struct xvctx *ctx = vo->priv;
440 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
442 /* remember the currently visible buffer */
443 ctx->visible_buf = ctx->current_buf;
445 ctx->current_buf = (ctx->current_buf + 1) % ctx->num_buffers;
446 XFlush(vo->x11->display);
447 return;
450 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
451 int h, int x, int y)
453 struct xvctx *ctx = vo->priv;
454 uint8_t *dst;
455 XvImage *current_image = ctx->xvimage[ctx->current_buf];
457 dst = current_image->data + current_image->offsets[0]
458 + current_image->pitches[0] * y + x;
459 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
461 x /= 2;
462 y /= 2;
463 w /= 2;
464 h /= 2;
466 dst = current_image->data + current_image->offsets[1]
467 + current_image->pitches[1] * y + x;
468 if (ctx->image_format != IMGFMT_YV12)
469 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
470 else
471 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
473 dst = current_image->data + current_image->offsets[2]
474 + current_image->pitches[2] * y + x;
475 if (ctx->image_format == IMGFMT_YV12)
476 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
477 else
478 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
480 return 0;
483 static mp_image_t *get_screenshot(struct vo *vo)
485 struct xvctx *ctx = vo->priv;
487 // try to get an image without OSD
488 if (ctx->have_image_copy)
489 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
491 XvImage *xv_image = ctx->xvimage[ctx->visible_buf];
493 int w = xv_image->width;
494 int h = xv_image->height;
496 mp_image_t *image = alloc_mpi(w, h, ctx->image_format);
498 int bytes = 1;
499 if (!(image->flags & MP_IMGFLAG_PLANAR) && (image->flags & MP_IMGFLAG_YUV))
500 // packed YUV
501 bytes = image->bpp / 8;
503 memcpy_pic(image->planes[0], xv_image->data + xv_image->offsets[0],
504 bytes * w, h, image->stride[0], xv_image->pitches[0]);
506 if (image->flags & MP_IMGFLAG_PLANAR) {
507 int swap = ctx->image_format == IMGFMT_YV12;
508 int p1 = swap ? 2 : 1;
509 int p2 = swap ? 1 : 2;
511 w /= 2;
512 h /= 2;
514 memcpy_pic(image->planes[p1], xv_image->data + xv_image->offsets[1],
515 w, h, image->stride[p1], xv_image->pitches[1]);
516 memcpy_pic(image->planes[p2], xv_image->data + xv_image->offsets[2],
517 w, h, image->stride[p2], xv_image->pitches[2]);
520 image->w = ctx->image_d_width;
521 image->h = ctx->image_d_height;
523 return image;
526 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
528 struct xvctx *ctx = vo->priv;
530 ctx->have_image_copy = false;
532 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
533 ; // done
534 else if (mpi->flags & MP_IMGFLAG_PLANAR)
535 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
536 else if (mpi->flags & MP_IMGFLAG_YUV)
537 // packed YUV:
538 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
539 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
540 mpi->w * (mpi->bpp / 8), mpi->h,
541 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
542 else
543 return false;
545 if (ctx->is_paused) {
546 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
547 ctx->have_image_copy = true;
549 ctx->unchanged_image = true;
550 return true;
553 static int query_format(struct xvctx *ctx, uint32_t format)
555 uint32_t i;
556 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
558 /* check image formats */
559 for (i = 0; i < ctx->formats; i++) {
560 if (ctx->fo[i].id == format)
561 return flag; //xv_format = fo[i].id;
563 return 0;
566 static void uninit(struct vo *vo)
568 struct xvctx *ctx = vo->priv;
569 int i;
571 ctx->visible_buf = -1;
572 if (ctx->ai)
573 XvFreeAdaptorInfo(ctx->ai);
574 ctx->ai = NULL;
575 if (ctx->fo) {
576 XFree(ctx->fo);
577 ctx->fo = NULL;
579 for (i = 0; i < ctx->total_buffers; i++)
580 deallocate_xvimage(vo, i);
581 #ifdef CONFIG_XF86VM
582 if (ctx->mode_switched)
583 vo_vm_close(vo);
584 #endif
585 // uninit() shouldn't get called unless initialization went past vo_init()
586 vo_x11_uninit(vo);
589 static int preinit(struct vo *vo, const char *arg)
591 XvPortID xv_p;
592 int busy_ports = 0;
593 unsigned int i;
594 strarg_t ck_src_arg = { 0, NULL };
595 strarg_t ck_method_arg = { 0, NULL };
596 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
597 vo->priv = ctx;
598 int xv_adaptor = -1;
600 if (!vo_init(vo))
601 return -1;
603 struct vo_x11_state *x11 = vo->x11;
605 const opt_t subopts[] =
607 /* name arg type arg var test */
608 { "port", OPT_ARG_INT, &x11->xv_port, int_pos },
609 { "adaptor", OPT_ARG_INT, &xv_adaptor, int_non_neg },
610 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
611 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
612 { NULL }
615 x11->xv_port = 0;
617 /* parse suboptions */
618 if (subopt_parse(arg, subopts) != 0)
619 goto error;
621 /* modify colorkey settings according to the given options */
622 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
624 /* check for Xvideo extension */
625 unsigned int ver, rel, req, ev, err;
626 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
627 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] Sorry, Xv not supported by this X11 version/driver\n[VO_XV] ******** Try with -vo x11 or -vo sdl *********\n");
628 goto error;
631 /* check for Xvideo support */
632 if (Success !=
633 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
634 &ctx->adaptors, &ctx->ai)) {
635 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] XvQueryAdaptors failed.\n");
636 goto error;
639 /* check adaptors */
640 if (x11->xv_port) {
641 int port_found;
643 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
644 if ((ctx->ai[i].type & XvInputMask)
645 && (ctx->ai[i].type & XvImageMask)) {
646 for (xv_p = ctx->ai[i].base_id;
647 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
648 ++xv_p) {
649 if (xv_p == x11->xv_port) {
650 port_found = 1;
651 break;
656 if (port_found) {
657 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
658 x11->xv_port = 0;
659 } else {
660 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Invalid port parameter, overriding with port 0.\n");
661 x11->xv_port = 0;
665 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
666 /* check if adaptor number has been specified */
667 if (xv_adaptor != -1 && xv_adaptor != i)
668 continue;
670 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
671 for (xv_p = ctx->ai[i].base_id;
672 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
673 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
674 x11->xv_port = xv_p;
675 mp_msg(MSGT_VO, MSGL_V,
676 "[VO_XV] Using Xv Adapter #%d (%s)\n",
677 i, ctx->ai[i].name);
678 break;
679 } else {
680 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Could not grab port %i.\n",
681 (int) xv_p);
682 ++busy_ports;
686 if (!x11->xv_port) {
687 if (busy_ports)
688 mp_tmsg(MSGT_VO, MSGL_ERR,
689 "[VO_XV] Could not find free Xvideo port - maybe another process is already\n"\
690 "[VO_XV] using it. Close all video applications, and try again. If that does\n"\
691 "[VO_XV] not help, see 'mplayer -vo help' for other (non-xv) video out drivers.\n");
692 else
693 mp_tmsg(MSGT_VO, MSGL_ERR,
694 "[VO_XV] It seems there is no Xvideo support for your video card available.\n"\
695 "[VO_XV] Run 'xvinfo' to verify its Xv support and read\n"\
696 "[VO_XV] DOCS/HTML/en/video.html#xv!\n"\
697 "[VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.\n"\
698 "[VO_XV] Try -vo x11.\n");
699 goto error;
702 if (!vo_xv_init_colorkey(vo)) {
703 goto error; // bail out, colorkey setup failed
705 vo_xv_enable_vsync(vo);
706 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
708 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
709 (int *) &ctx->formats);
711 return 0;
713 error:
714 uninit(vo); // free resources
715 return -1;
718 static int control(struct vo *vo, uint32_t request, void *data)
720 struct xvctx *ctx = vo->priv;
721 struct vo_x11_state *x11 = vo->x11;
722 switch (request) {
723 case VOCTRL_PAUSE:
724 return (ctx->is_paused = 1);
725 case VOCTRL_RESUME:
726 return (ctx->is_paused = 0);
727 case VOCTRL_QUERY_FORMAT:
728 return query_format(ctx, *((uint32_t *) data));
729 case VOCTRL_DRAW_IMAGE:
730 return draw_image(vo, data);
731 case VOCTRL_GET_PANSCAN:
732 return VO_TRUE;
733 case VOCTRL_FULLSCREEN:
734 vo_x11_fullscreen(vo);
735 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
736 case VOCTRL_SET_PANSCAN:
737 resize(vo);
738 return VO_TRUE;
739 case VOCTRL_SET_EQUALIZER: {
740 vo->want_redraw = true;
741 struct voctrl_set_equalizer_args *args = data;
742 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
744 case VOCTRL_GET_EQUALIZER: {
745 struct voctrl_get_equalizer_args *args = data;
746 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
748 case VOCTRL_SET_YUV_COLORSPACE:;
749 struct mp_csp_details* given_cspc = data;
750 int is_709 = given_cspc->format == MP_CSP_BT_709;
751 vo_xv_set_eq(vo, x11->xv_port, "bt_709", is_709 * 200 - 100);
752 vo->want_redraw = true;
753 return true;
754 case VOCTRL_GET_YUV_COLORSPACE:;
755 struct mp_csp_details* cspc = data;
756 *cspc = (struct mp_csp_details) MP_CSP_DETAILS_DEFAULTS;
757 int bt709_enabled;
758 if (vo_xv_get_eq(vo, x11->xv_port, "bt_709", &bt709_enabled))
759 cspc->format = bt709_enabled == 100 ? MP_CSP_BT_709 : MP_CSP_BT_601;
760 return true;
761 case VOCTRL_ONTOP:
762 vo_x11_ontop(vo);
763 return VO_TRUE;
764 case VOCTRL_UPDATE_SCREENINFO:
765 update_xinerama_info(vo);
766 return VO_TRUE;
767 case VOCTRL_REDRAW_FRAME:
768 return redraw_frame(vo);
769 case VOCTRL_SCREENSHOT: {
770 struct voctrl_screenshot_args *args = data;
771 args->out_image = get_screenshot(vo);
772 return true;
775 return VO_NOTIMPL;
778 const struct vo_driver video_out_xv = {
779 .is_new = 1,
780 .info = &info,
781 .preinit = preinit,
782 .config = config,
783 .control = control,
784 .draw_slice = draw_slice,
785 .draw_osd = draw_osd,
786 .flip_page = flip_page,
787 .check_events = check_events,
788 .uninit = uninit