cache: don't modify argument when stream control fails
[mplayer.git] / libvo / vo_xv.c
blobcecbc68b7ea417a87d92c6fd524e4ee4c6c6cf7b
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 "video_out.h"
48 #include "libmpcodecs/vfcap.h"
49 #include "libmpcodecs/mp_image.h"
50 #include "osd.h"
52 #include <X11/Xlib.h>
53 #include <X11/Xutil.h>
54 #include <errno.h>
56 #include "x11_common.h"
58 #include "fastmemcpy.h"
59 #include "sub/sub.h"
60 #include "aspect.h"
62 #include "subopt-helper.h"
64 #include "libavutil/common.h"
66 static const vo_info_t info = {
67 "X11/Xv",
68 "xv",
69 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
73 #ifdef HAVE_SHM
74 #include <sys/ipc.h>
75 #include <sys/shm.h>
76 #include <X11/extensions/XShm.h>
77 #endif
79 // Note: depends on the inclusion of X11/extensions/XShm.h
80 #include <X11/extensions/Xv.h>
81 #include <X11/extensions/Xvlib.h>
83 struct xvctx {
84 XvAdaptorInfo *ai;
85 XvImageFormatValues *fo;
86 unsigned int formats, adaptors, xv_format;
87 int current_buf;
88 int current_ip_buf;
89 int num_buffers;
90 int total_buffers;
91 int have_visible_image_copy;
92 int have_next_image_copy;
93 int unchanged_visible_image;
94 int unchanged_next_image;
95 int visible_buf;
96 XvImage *xvimage[NUM_BUFFERS + 1];
97 uint32_t image_width;
98 uint32_t image_height;
99 uint32_t image_format;
100 int is_paused;
101 struct vo_rect src_rect;
102 struct vo_rect dst_rect;
103 uint32_t max_width, max_height; // zero means: not set
104 int mode_switched;
105 int osd_objects_drawn;
106 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
107 unsigned char *src, unsigned char *srca,
108 int stride);
109 #ifdef HAVE_SHM
110 XShmSegmentInfo Shminfo[NUM_BUFFERS + 1];
111 int Shmem_Flag;
112 #endif
115 static void allocate_xvimage(struct vo *, int);
118 static void fixup_osd_position(struct vo *vo, int *x0, int *y0, int *w, int *h)
120 struct xvctx *ctx = vo->priv;
121 *x0 += ctx->image_width * (vo->panscan_x >> 1)
122 / (vo->dwidth + vo->panscan_x);
123 *w = av_clip(*w, 0, ctx->image_width);
124 *h = av_clip(*h, 0, ctx->image_height);
125 *x0 = FFMIN(*x0, ctx->image_width - *w);
126 *y0 = FFMIN(*y0, ctx->image_height - *h);
129 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
130 unsigned char *src, unsigned char *srca,
131 int stride)
133 struct vo *vo = p;
134 struct xvctx *ctx = vo->priv;
135 fixup_osd_position(vo, &x0, &y0, &w, &h);
136 vo_draw_alpha_yv12(w, h, src, srca, stride,
137 ctx->xvimage[ctx->current_buf]->data +
138 ctx->xvimage[ctx->current_buf]->offsets[0] +
139 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
140 ctx->xvimage[ctx->current_buf]->pitches[0]);
141 ctx->osd_objects_drawn++;
144 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
145 unsigned char *src, unsigned char *srca,
146 int stride)
148 struct vo *vo = p;
149 struct xvctx *ctx = vo->priv;
150 fixup_osd_position(vo, &x0, &y0, &w, &h);
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 fixup_osd_position(vo, &x0, &y0, &w, &h);
166 vo_draw_alpha_yuy2(w, h, src, srca, stride,
167 ctx->xvimage[ctx->current_buf]->data +
168 ctx->xvimage[ctx->current_buf]->offsets[0] +
169 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
170 ctx->xvimage[ctx->current_buf]->pitches[0]);
171 ctx->osd_objects_drawn++;
174 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
175 unsigned char *src, unsigned char *srca,
176 int stride)
181 static void deallocate_xvimage(struct vo *vo, int foo);
183 static void resize(struct vo *vo)
185 struct xvctx *ctx = vo->priv;
187 calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
188 &ctx->dst_rect, NULL, NULL);
189 struct vo_rect *dst = &ctx->dst_rect;
190 vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height, 1);
191 vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
195 * connect to server, create and map window,
196 * allocate colors and (shared) memory
198 static int config(struct vo *vo, uint32_t width, uint32_t height,
199 uint32_t d_width, uint32_t d_height, uint32_t flags,
200 char *title, uint32_t format)
202 struct vo_x11_state *x11 = vo->x11;
203 XVisualInfo vinfo;
204 XSetWindowAttributes xswa;
205 XWindowAttributes attribs;
206 unsigned long xswamask;
207 int depth;
208 struct xvctx *ctx = vo->priv;
209 int i;
211 ctx->image_height = height;
212 ctx->image_width = width;
213 ctx->image_format = format;
215 if ((ctx->max_width != 0 && ctx->max_height != 0)
216 && (ctx->image_width > ctx->max_width
217 || ctx->image_height > ctx->max_height)) {
218 mp_tmsg(MSGT_VO, MSGL_ERR, "Source image dimensions are too high: %ux%u (maximum is %ux%u)\n",
219 ctx->image_width, ctx->image_height, ctx->max_width,
220 ctx->max_height);
221 return -1;
224 ctx->visible_buf = -1;
225 ctx->have_visible_image_copy = false;
226 ctx->have_next_image_copy = false;
228 /* check image formats */
229 ctx->xv_format = 0;
230 for (i = 0; i < ctx->formats; i++) {
231 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
232 ctx->fo[i].id, (char *) &ctx->fo[i].id,
233 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
234 if (ctx->fo[i].id == format)
235 ctx->xv_format = ctx->fo[i].id;
237 if (!ctx->xv_format)
238 return -1;
241 #ifdef CONFIG_XF86VM
242 int vm = flags & VOFLAG_MODESWITCHING;
243 if (vm) {
244 vo_vm_switch(vo);
245 ctx->mode_switched = 1;
247 #endif
248 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
249 &attribs);
250 depth = attribs.depth;
251 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
252 depth = 24;
253 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
255 xswa.border_pixel = 0;
256 xswamask = CWBorderPixel;
257 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND) {
258 xswa.background_pixel = x11->xv_colorkey;
259 xswamask |= CWBackPixel;
262 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
263 vo->dheight, flags, CopyFromParent, "xv",
264 title);
265 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
267 #ifdef CONFIG_XF86VM
268 if (vm) {
269 /* Grab the mouse pointer in our window */
270 if (vo_grabpointer)
271 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
272 GrabModeAsync, x11->window, None, CurrentTime);
273 XSetInputFocus(x11->display, x11->window, RevertToNone,
274 CurrentTime);
276 #endif
279 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
280 x11->xv_port);
282 switch (ctx->xv_format) {
283 case IMGFMT_YV12:
284 case IMGFMT_I420:
285 case IMGFMT_IYUV:
286 ctx->draw_alpha_fnc = draw_alpha_yv12;
287 break;
288 case IMGFMT_YUY2:
289 case IMGFMT_YVYU:
290 ctx->draw_alpha_fnc = draw_alpha_yuy2;
291 break;
292 case IMGFMT_UYVY:
293 ctx->draw_alpha_fnc = draw_alpha_uyvy;
294 break;
295 default:
296 ctx->draw_alpha_fnc = draw_alpha_null;
299 // In case config has been called before
300 for (i = 0; i < ctx->total_buffers; i++)
301 deallocate_xvimage(vo, i);
303 ctx->num_buffers =
304 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
305 ctx->total_buffers = ctx->num_buffers + 1;
307 for (i = 0; i < ctx->total_buffers; i++)
308 allocate_xvimage(vo, i);
310 ctx->current_buf = 0;
311 ctx->current_ip_buf = 0;
314 resize(vo);
316 return 0;
319 static void allocate_xvimage(struct vo *vo, int foo)
321 struct xvctx *ctx = vo->priv;
322 struct vo_x11_state *x11 = vo->x11;
324 * allocate XvImages. FIXME: no error checking, without
325 * mit-shm this will bomb... trzing to fix ::atmos
327 #ifdef HAVE_SHM
328 if (x11->display_is_local && XShmQueryExtension(x11->display))
329 ctx->Shmem_Flag = 1;
330 else {
331 ctx->Shmem_Flag = 0;
332 mp_tmsg(MSGT_VO, MSGL_INFO, "[VO_XV] Shared memory not supported\nReverting to normal Xv.\n");
334 if (ctx->Shmem_Flag) {
335 ctx->xvimage[foo] =
336 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
337 ctx->xv_format, NULL,
338 ctx->image_width, ctx->image_height,
339 &ctx->Shminfo[foo]);
341 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
342 ctx->xvimage[foo]->data_size,
343 IPC_CREAT | 0777);
344 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
346 ctx->Shminfo[foo].readOnly = False;
348 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
349 XShmAttach(x11->display, &ctx->Shminfo[foo]);
350 XSync(x11->display, False);
351 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
352 } else
353 #endif
355 ctx->xvimage[foo] =
356 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
357 ctx->xv_format, NULL, ctx->image_width,
358 ctx->image_height);
359 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
360 XSync(x11->display, False);
362 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
363 return;
366 static void deallocate_xvimage(struct vo *vo, int foo)
368 struct xvctx *ctx = vo->priv;
369 #ifdef HAVE_SHM
370 if (ctx->Shmem_Flag) {
371 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
372 shmdt(ctx->Shminfo[foo].shmaddr);
373 } else
374 #endif
376 free(ctx->xvimage[foo]->data);
378 XFree(ctx->xvimage[foo]);
380 XSync(vo->x11->display, False);
381 return;
384 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
386 struct xvctx *ctx = vo->priv;
387 struct vo_x11_state *x11 = vo->x11;
388 struct vo_rect *src = &ctx->src_rect;
389 struct vo_rect *dst = &ctx->dst_rect;
390 #ifdef HAVE_SHM
391 if (ctx->Shmem_Flag) {
392 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
393 src->left, src->top, src->width, src->height,
394 dst->left, dst->top, dst->width, dst->height,
395 False);
396 } else
397 #endif
399 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
400 src->left, src->top, src->width, src->height,
401 dst->left, dst->top, dst->width, dst->height);
405 // Only copies luma for planar formats as draw_alpha doesn't change others */
406 static void copy_backup_image(struct vo *vo, int dest, int src)
408 struct xvctx *ctx = vo->priv;
410 XvImage *vb = ctx->xvimage[dest];
411 XvImage *cp = ctx->xvimage[src];
412 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
413 vb->width, vb->height,
414 vb->pitches[0], cp->pitches[0]);
417 static void check_events(struct vo *vo)
419 struct xvctx *ctx = vo->priv;
420 int e = vo_x11_check_events(vo);
422 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
423 resize(vo);
425 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
426 /* did we already draw a buffer */
427 if (ctx->visible_buf != -1) {
428 /* redraw the last visible buffer */
429 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
434 static void draw_osd(struct vo *vo, struct osd_state *osd)
436 struct xvctx *ctx = vo->priv;
438 ctx->osd_objects_drawn = 0;
439 osd_draw_text(osd,
440 ctx->image_width -
441 ctx->image_width * vo->panscan_x / (vo->dwidth +
442 vo->panscan_x),
443 ctx->image_height, ctx->draw_alpha_fnc, vo);
444 if (ctx->osd_objects_drawn)
445 ctx->unchanged_next_image = false;
448 static int redraw_osd(struct vo *vo, struct osd_state *osd)
450 struct xvctx *ctx = vo->priv;
452 if (ctx->have_visible_image_copy)
453 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
454 else if (ctx->unchanged_visible_image) {
455 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
456 ctx->have_visible_image_copy = true;
458 else
459 return false;
460 int temp = ctx->current_buf;
461 ctx->current_buf = ctx->visible_buf;
462 draw_osd(vo, osd);
463 ctx->current_buf = temp;
464 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
465 return true;
468 static void flip_page(struct vo *vo)
470 struct xvctx *ctx = vo->priv;
471 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
473 /* remember the currently visible buffer */
474 ctx->visible_buf = ctx->current_buf;
476 ctx->have_visible_image_copy = ctx->have_next_image_copy;
477 ctx->have_next_image_copy = false;
478 ctx->unchanged_visible_image = ctx->unchanged_next_image;
479 ctx->unchanged_next_image = false;
481 if (ctx->num_buffers > 1) {
482 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
483 ctx->num_buffers);
484 XFlush(vo->x11->display);
485 } else
486 XSync(vo->x11->display, False);
487 return;
490 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
491 int h, int x, int y)
493 struct xvctx *ctx = vo->priv;
494 uint8_t *dst;
495 XvImage *current_image = ctx->xvimage[ctx->current_buf];
497 dst = current_image->data + current_image->offsets[0]
498 + current_image->pitches[0] * y + x;
499 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
501 x /= 2;
502 y /= 2;
503 w /= 2;
504 h /= 2;
506 dst = current_image->data + current_image->offsets[1]
507 + current_image->pitches[1] * y + x;
508 if (ctx->image_format != IMGFMT_YV12)
509 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
510 else
511 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
513 dst = current_image->data + current_image->offsets[2]
514 + current_image->pitches[2] * 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 return 0;
523 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
525 struct xvctx *ctx = vo->priv;
527 ctx->have_next_image_copy = false;
529 if (mpi->flags & MP_IMGFLAG_DIRECT)
530 // direct rendering:
531 ctx->current_buf = (size_t)(mpi->priv); // hack!
532 else 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_next_image_copy = true;
549 ctx->unchanged_next_image = true;
550 return true;
553 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
555 // we shouldn't change current_buf unless we do DR!
556 int buf = ctx->current_buf;
558 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
559 return VO_FALSE; // it is not static
560 if (mpi->imgfmt != ctx->image_format)
561 return VO_FALSE; // needs conversion :(
562 if (mpi->flags & MP_IMGFLAG_READABLE
563 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
564 // reference (I/P) frame of IP or IPB:
565 if (ctx->num_buffers < 2)
566 return VO_FALSE; // not enough
567 ctx->current_ip_buf ^= 1;
568 // for IPB with 2 buffers we can DR only one of the 2 P frames:
569 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
570 && ctx->current_ip_buf)
571 return VO_FALSE;
572 buf = ctx->current_ip_buf;
573 if (mpi->type == MP_IMGTYPE_IPB)
574 ++buf; // preserve space for B
576 if (mpi->height > ctx->xvimage[buf]->height)
577 return VO_FALSE; //buffer to small
578 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
579 return VO_FALSE; //buffer to small
580 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
581 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
582 ctx->current_buf = buf;
583 XvImage *current_image = ctx->xvimage[ctx->current_buf];
584 mpi->planes[0] = current_image->data + current_image->offsets[0];
585 mpi->stride[0] = current_image->pitches[0];
586 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
587 if (mpi->flags & MP_IMGFLAG_PLANAR) {
588 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
589 // I420
590 mpi->planes[1] = current_image->data
591 + current_image->offsets[1];
592 mpi->planes[2] = current_image->data
593 + current_image->offsets[2];
594 mpi->stride[1] = current_image->pitches[1];
595 mpi->stride[2] = current_image->pitches[2];
596 } else {
597 // YV12
598 mpi->planes[1] = current_image->data
599 + current_image->offsets[2];
600 mpi->planes[2] = current_image->data
601 + current_image->offsets[1];
602 mpi->stride[1] = current_image->pitches[2];
603 mpi->stride[2] = current_image->pitches[1];
606 mpi->flags |= MP_IMGFLAG_DIRECT;
607 mpi->priv = (void *)(size_t)ctx->current_buf;
608 return VO_TRUE;
610 return VO_FALSE;
613 static int query_format(struct xvctx *ctx, uint32_t format)
615 uint32_t i;
616 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
618 /* check image formats */
619 for (i = 0; i < ctx->formats; i++) {
620 if (ctx->fo[i].id == format)
621 return flag; //xv_format = fo[i].id;
623 return 0;
626 static void uninit(struct vo *vo)
628 struct xvctx *ctx = vo->priv;
629 int i;
631 ctx->visible_buf = -1;
632 if (ctx->ai)
633 XvFreeAdaptorInfo(ctx->ai);
634 ctx->ai = NULL;
635 if (ctx->fo) {
636 XFree(ctx->fo);
637 ctx->fo = NULL;
639 for (i = 0; i < ctx->total_buffers; i++)
640 deallocate_xvimage(vo, i);
641 #ifdef CONFIG_XF86VM
642 if (ctx->mode_switched)
643 vo_vm_close(vo);
644 #endif
645 // uninit() shouldn't get called unless initialization went past vo_init()
646 vo_x11_uninit(vo);
649 static int preinit(struct vo *vo, const char *arg)
651 XvPortID xv_p;
652 int busy_ports = 0;
653 unsigned int i;
654 strarg_t ck_src_arg = { 0, NULL };
655 strarg_t ck_method_arg = { 0, NULL };
656 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
657 vo->priv = ctx;
658 struct vo_x11_state *x11 = vo->x11;
659 int xv_adaptor = -1;
661 const opt_t subopts[] =
663 /* name arg type arg var test */
664 { "port", OPT_ARG_INT, &x11->xv_port, int_pos },
665 { "adaptor", OPT_ARG_INT, &xv_adaptor, int_non_neg },
666 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
667 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
668 { NULL }
671 x11->xv_port = 0;
673 /* parse suboptions */
674 if (subopt_parse(arg, subopts) != 0) {
675 return -1;
678 /* modify colorkey settings according to the given options */
679 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
681 if (!vo_init(vo))
682 return -1;
684 /* check for Xvideo extension */
685 unsigned int ver, rel, req, ev, err;
686 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
687 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");
688 goto error;
691 /* check for Xvideo support */
692 if (Success !=
693 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
694 &ctx->adaptors, &ctx->ai)) {
695 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] XvQueryAdaptors failed.\n");
696 goto error;
699 /* check adaptors */
700 if (x11->xv_port) {
701 int port_found;
703 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
704 if ((ctx->ai[i].type & XvInputMask)
705 && (ctx->ai[i].type & XvImageMask)) {
706 for (xv_p = ctx->ai[i].base_id;
707 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
708 ++xv_p) {
709 if (xv_p == x11->xv_port) {
710 port_found = 1;
711 break;
716 if (port_found) {
717 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
718 x11->xv_port = 0;
719 } else {
720 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Invalid port parameter, overriding with port 0.\n");
721 x11->xv_port = 0;
725 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
726 /* check if adaptor number has been specified */
727 if (xv_adaptor != -1 && xv_adaptor != i)
728 continue;
730 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
731 for (xv_p = ctx->ai[i].base_id;
732 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
733 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
734 x11->xv_port = xv_p;
735 mp_msg(MSGT_VO, MSGL_V,
736 "[VO_XV] Using Xv Adapter #%d (%s)\n",
737 i, ctx->ai[i].name);
738 break;
739 } else {
740 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Could not grab port %i.\n",
741 (int) xv_p);
742 ++busy_ports;
746 if (!x11->xv_port) {
747 if (busy_ports)
748 mp_tmsg(MSGT_VO, MSGL_ERR,
749 "[VO_XV] Could not find free Xvideo port - maybe another process is already\n"\
750 "[VO_XV] using it. Close all video applications, and try again. If that does\n"\
751 "[VO_XV] not help, see 'mplayer -vo help' for other (non-xv) video out drivers.\n");
752 else
753 mp_tmsg(MSGT_VO, MSGL_ERR,
754 "[VO_XV] It seems there is no Xvideo support for your video card available.\n"\
755 "[VO_XV] Run 'xvinfo' to verify its Xv support and read\n"\
756 "[VO_XV] DOCS/HTML/en/video.html#xv!\n"\
757 "[VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.\n"\
758 "[VO_XV] Try -vo x11.\n");
759 goto error;
762 if (!vo_xv_init_colorkey(vo)) {
763 goto error; // bail out, colorkey setup failed
765 vo_xv_enable_vsync(vo);
766 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
768 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
769 (int *) &ctx->formats);
771 return 0;
773 error:
774 uninit(vo); // free resources
775 return -1;
778 static int control(struct vo *vo, uint32_t request, void *data)
780 struct xvctx *ctx = vo->priv;
781 struct vo_x11_state *x11 = vo->x11;
782 switch (request) {
783 case VOCTRL_PAUSE:
784 return (ctx->is_paused = 1);
785 case VOCTRL_RESUME:
786 return (ctx->is_paused = 0);
787 case VOCTRL_QUERY_FORMAT:
788 return query_format(ctx, *((uint32_t *) data));
789 case VOCTRL_GET_IMAGE:
790 return get_image(ctx, data);
791 case VOCTRL_DRAW_IMAGE:
792 return draw_image(vo, data);
793 case VOCTRL_GET_PANSCAN:
794 return VO_TRUE;
795 case VOCTRL_FULLSCREEN:
796 vo_x11_fullscreen(vo);
797 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
798 case VOCTRL_SET_PANSCAN:
799 resize(vo);
800 return VO_TRUE;
801 case VOCTRL_SET_EQUALIZER:
803 struct voctrl_set_equalizer_args *args = data;
804 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
806 case VOCTRL_GET_EQUALIZER:
808 struct voctrl_get_equalizer_args *args = data;
809 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
811 case VOCTRL_SET_YUV_COLORSPACE:;
812 int given_cspc = *(int *)data % 2;
813 return vo_xv_set_eq(vo, x11->xv_port, "bt_709", given_cspc * 200 - 100);
814 case VOCTRL_GET_YUV_COLORSPACE:;
815 int bt709_enabled;
816 if (!vo_xv_get_eq(vo, x11->xv_port, "bt_709", &bt709_enabled))
817 return false;
818 *(int *)data = bt709_enabled == 100;
819 return true;
820 case VOCTRL_ONTOP:
821 vo_x11_ontop(vo);
822 return VO_TRUE;
823 case VOCTRL_UPDATE_SCREENINFO:
824 update_xinerama_info(vo);
825 return VO_TRUE;
826 case VOCTRL_REDRAW_OSD:
827 return redraw_osd(vo, data);
829 return VO_NOTIMPL;
832 const struct vo_driver video_out_xv = {
833 .is_new = 1,
834 .info = &info,
835 .preinit = preinit,
836 .config = config,
837 .control = control,
838 .draw_slice = draw_slice,
839 .draw_osd = draw_osd,
840 .flip_page = flip_page,
841 .check_events = check_events,
842 .uninit = uninit