Merge svn changes up to r30529
[mplayer/kovensky.git] / libvo / vo_xv.c
blob54eecd20159d34af6a411fa931325ed5b8224d6d
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 #include "libavutil/common.h"
70 static const vo_info_t info = {
71 "X11/Xv",
72 "xv",
73 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
77 #ifdef HAVE_SHM
78 #include <sys/ipc.h>
79 #include <sys/shm.h>
80 #include <X11/extensions/XShm.h>
81 #endif
83 // Note: depends on the inclusion of X11/extensions/XShm.h
84 #include <X11/extensions/Xv.h>
85 #include <X11/extensions/Xvlib.h>
87 struct xvctx {
88 XvAdaptorInfo *ai;
89 XvImageFormatValues *fo;
90 unsigned int formats, adaptors, xv_format;
91 int current_buf;
92 int current_ip_buf;
93 int num_buffers;
94 int total_buffers;
95 int have_visible_image_copy;
96 int have_next_image_copy;
97 int unchanged_visible_image;
98 int unchanged_next_image;
99 int visible_buf;
100 XvImage *xvimage[NUM_BUFFERS + 1];
101 uint32_t image_width;
102 uint32_t image_height;
103 uint32_t image_format;
104 int is_paused;
105 struct vo_rect src_rect;
106 struct vo_rect dst_rect;
107 uint32_t max_width, max_height; // zero means: not set
108 int event_fd_registered; // for uninit called from preinit
109 int mode_switched;
110 int osd_objects_drawn;
111 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
112 unsigned char *src, unsigned char *srca,
113 int stride);
114 #ifdef HAVE_SHM
115 XShmSegmentInfo Shminfo[NUM_BUFFERS + 1];
116 int Shmem_Flag;
117 #endif
120 static void allocate_xvimage(struct vo *, int);
123 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
124 unsigned char *src, unsigned char *srca,
125 int stride)
127 struct vo *vo = p;
128 struct xvctx *ctx = vo->priv;
129 x0 += ctx->image_width * (vo->panscan_x >> 1)
130 / (vo->dwidth + vo->panscan_x);
131 vo_draw_alpha_yv12(w, h, src, srca, stride,
132 ctx->xvimage[ctx->current_buf]->data +
133 ctx->xvimage[ctx->current_buf]->offsets[0] +
134 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
135 ctx->xvimage[ctx->current_buf]->pitches[0]);
136 ctx->osd_objects_drawn++;
139 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
140 unsigned char *src, unsigned char *srca,
141 int stride)
143 struct vo *vo = p;
144 struct xvctx *ctx = vo->priv;
145 x0 += ctx->image_width * (vo->panscan_x >> 1)
146 / (vo->dwidth + vo->panscan_x);
147 vo_draw_alpha_yuy2(w, h, src, srca, stride,
148 ctx->xvimage[ctx->current_buf]->data +
149 ctx->xvimage[ctx->current_buf]->offsets[0] +
150 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
151 ctx->xvimage[ctx->current_buf]->pitches[0]);
152 ctx->osd_objects_drawn++;
155 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
156 unsigned char *src, unsigned char *srca,
157 int stride)
159 struct vo *vo = p;
160 struct xvctx *ctx = vo->priv;
161 x0 += ctx->image_width * (vo->panscan_x >> 1)
162 / (vo->dwidth + vo->panscan_x);
163 vo_draw_alpha_yuy2(w, h, src, srca, stride,
164 ctx->xvimage[ctx->current_buf]->data +
165 ctx->xvimage[ctx->current_buf]->offsets[0] +
166 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
167 ctx->xvimage[ctx->current_buf]->pitches[0]);
168 ctx->osd_objects_drawn++;
171 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
172 unsigned char *src, unsigned char *srca,
173 int stride)
178 static void deallocate_xvimage(struct vo *vo, int foo);
180 static void resize(struct vo *vo)
182 struct xvctx *ctx = vo->priv;
184 calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
185 &ctx->dst_rect, NULL, NULL);
186 struct vo_rect *dst = &ctx->dst_rect;
187 vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height, 1);
188 vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
192 * connect to server, create and map window,
193 * allocate colors and (shared) memory
195 static int config(struct vo *vo, uint32_t width, uint32_t height,
196 uint32_t d_width, uint32_t d_height, uint32_t flags,
197 char *title, uint32_t format)
199 struct vo_x11_state *x11 = vo->x11;
200 XVisualInfo vinfo;
201 XSetWindowAttributes xswa;
202 XWindowAttributes attribs;
203 unsigned long xswamask;
204 int depth;
205 struct xvctx *ctx = vo->priv;
206 int i;
208 ctx->image_height = height;
209 ctx->image_width = width;
210 ctx->image_format = format;
212 if ((ctx->max_width != 0 && ctx->max_height != 0)
213 && (ctx->image_width > ctx->max_width
214 || ctx->image_height > ctx->max_height)) {
215 mp_tmsg(MSGT_VO, MSGL_ERR, "Source image dimensions are too high: %ux%u (maximum is %ux%u)\n",
216 ctx->image_width, ctx->image_height, ctx->max_width,
217 ctx->max_height);
218 return -1;
221 ctx->visible_buf = -1;
222 ctx->have_visible_image_copy = false;
223 ctx->have_next_image_copy = false;
225 /* check image formats */
226 ctx->xv_format = 0;
227 for (i = 0; i < ctx->formats; i++) {
228 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
229 ctx->fo[i].id, (char *) &ctx->fo[i].id,
230 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
231 if (ctx->fo[i].id == format)
232 ctx->xv_format = ctx->fo[i].id;
234 if (!ctx->xv_format)
235 return -1;
238 #ifdef CONFIG_XF86VM
239 int vm = flags & VOFLAG_MODESWITCHING;
240 if (vm) {
241 vo_vm_switch(vo);
242 ctx->mode_switched = 1;
244 #endif
245 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
246 &attribs);
247 depth = attribs.depth;
248 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
249 depth = 24;
250 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
252 xswa.background_pixel = 0;
253 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND)
254 xswa.background_pixel = x11->xv_colorkey;
255 xswa.border_pixel = 0;
256 xswamask = CWBackPixel | CWBorderPixel;
258 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
259 vo->dheight, flags, CopyFromParent, "xv",
260 title);
261 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
263 #ifdef CONFIG_XF86VM
264 if (vm) {
265 /* Grab the mouse pointer in our window */
266 if (vo_grabpointer)
267 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
268 GrabModeAsync, x11->window, None, CurrentTime);
269 XSetInputFocus(x11->display, x11->window, RevertToNone,
270 CurrentTime);
272 #endif
275 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
276 x11->xv_port);
278 switch (ctx->xv_format) {
279 case IMGFMT_YV12:
280 case IMGFMT_I420:
281 case IMGFMT_IYUV:
282 ctx->draw_alpha_fnc = draw_alpha_yv12;
283 break;
284 case IMGFMT_YUY2:
285 case IMGFMT_YVYU:
286 ctx->draw_alpha_fnc = draw_alpha_yuy2;
287 break;
288 case IMGFMT_UYVY:
289 ctx->draw_alpha_fnc = draw_alpha_uyvy;
290 break;
291 default:
292 ctx->draw_alpha_fnc = draw_alpha_null;
295 // In case config has been called before
296 for (i = 0; i < ctx->total_buffers; i++)
297 deallocate_xvimage(vo, i);
299 ctx->num_buffers =
300 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
301 ctx->total_buffers = ctx->num_buffers + 1;
303 for (i = 0; i < ctx->total_buffers; i++)
304 allocate_xvimage(vo, i);
306 ctx->current_buf = 0;
307 ctx->current_ip_buf = 0;
310 resize(vo);
312 return 0;
315 static void allocate_xvimage(struct vo *vo, int foo)
317 struct xvctx *ctx = vo->priv;
318 struct vo_x11_state *x11 = vo->x11;
320 * allocate XvImages. FIXME: no error checking, without
321 * mit-shm this will bomb... trzing to fix ::atmos
323 #ifdef HAVE_SHM
324 if (x11->display_is_local && XShmQueryExtension(x11->display))
325 ctx->Shmem_Flag = 1;
326 else {
327 ctx->Shmem_Flag = 0;
328 mp_tmsg(MSGT_VO, MSGL_INFO, "[VO_XV] Shared memory not supported\nReverting to normal Xv.\n");
330 if (ctx->Shmem_Flag) {
331 ctx->xvimage[foo] =
332 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
333 ctx->xv_format, NULL,
334 ctx->image_width, ctx->image_height,
335 &ctx->Shminfo[foo]);
337 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
338 ctx->xvimage[foo]->data_size,
339 IPC_CREAT | 0777);
340 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
342 ctx->Shminfo[foo].readOnly = False;
344 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
345 XShmAttach(x11->display, &ctx->Shminfo[foo]);
346 XSync(x11->display, False);
347 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
348 } else
349 #endif
351 ctx->xvimage[foo] =
352 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
353 ctx->xv_format, NULL, ctx->image_width,
354 ctx->image_height);
355 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
356 XSync(x11->display, False);
358 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
359 return;
362 static void deallocate_xvimage(struct vo *vo, int foo)
364 struct xvctx *ctx = vo->priv;
365 #ifdef HAVE_SHM
366 if (ctx->Shmem_Flag) {
367 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
368 shmdt(ctx->Shminfo[foo].shmaddr);
369 } else
370 #endif
372 free(ctx->xvimage[foo]->data);
374 XFree(ctx->xvimage[foo]);
376 XSync(vo->x11->display, False);
377 return;
380 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
382 struct xvctx *ctx = vo->priv;
383 struct vo_x11_state *x11 = vo->x11;
384 struct vo_rect *src = &ctx->src_rect;
385 struct vo_rect *dst = &ctx->dst_rect;
386 #ifdef HAVE_SHM
387 if (ctx->Shmem_Flag) {
388 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
389 src->left, src->top, src->width, src->height,
390 dst->left, dst->top, dst->width, dst->height,
391 False);
392 } else
393 #endif
395 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
396 src->left, src->top, src->width, src->height,
397 dst->left, dst->top, dst->width, dst->height);
401 // Only copies luma for planar formats as draw_alpha doesn't change others */
402 static void copy_backup_image(struct vo *vo, int dest, int src)
404 struct xvctx *ctx = vo->priv;
406 XvImage *vb = ctx->xvimage[dest];
407 XvImage *cp = ctx->xvimage[src];
408 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
409 vb->width, vb->height,
410 vb->pitches[0], cp->pitches[0]);
413 static void check_events(struct vo *vo)
415 struct xvctx *ctx = vo->priv;
416 int e = vo_x11_check_events(vo);
418 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
419 resize(vo);
421 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
422 /* did we already draw a buffer */
423 if (ctx->visible_buf != -1) {
424 /* redraw the last visible buffer */
425 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
430 static void draw_osd(struct vo *vo, struct osd_state *osd)
432 struct xvctx *ctx = vo->priv;
434 ctx->osd_objects_drawn = 0;
435 osd_draw_text(osd,
436 ctx->image_width -
437 ctx->image_width * vo->panscan_x / (vo->dwidth +
438 vo->panscan_x),
439 ctx->image_height, ctx->draw_alpha_fnc, vo);
440 if (ctx->osd_objects_drawn)
441 ctx->unchanged_next_image = false;
444 static int redraw_osd(struct vo *vo, struct osd_state *osd)
446 struct xvctx *ctx = vo->priv;
448 if (ctx->have_visible_image_copy)
449 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
450 else if (ctx->unchanged_visible_image) {
451 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
452 ctx->have_visible_image_copy = true;
454 else
455 return false;
456 int temp = ctx->current_buf;
457 ctx->current_buf = ctx->visible_buf;
458 draw_osd(vo, osd);
459 ctx->current_buf = temp;
460 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
461 return true;
464 static void flip_page(struct vo *vo)
466 struct xvctx *ctx = vo->priv;
467 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
469 /* remember the currently visible buffer */
470 ctx->visible_buf = ctx->current_buf;
472 ctx->have_visible_image_copy = ctx->have_next_image_copy;
473 ctx->have_next_image_copy = false;
474 ctx->unchanged_visible_image = ctx->unchanged_next_image;
475 ctx->unchanged_next_image = false;
477 if (ctx->num_buffers > 1) {
478 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
479 ctx->num_buffers);
480 XFlush(vo->x11->display);
481 } else
482 XSync(vo->x11->display, False);
483 return;
486 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
487 int h, int x, int y)
489 struct xvctx *ctx = vo->priv;
490 uint8_t *dst;
491 XvImage *current_image = ctx->xvimage[ctx->current_buf];
493 dst = current_image->data + current_image->offsets[0]
494 + current_image->pitches[0] * y + x;
495 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
497 x /= 2;
498 y /= 2;
499 w /= 2;
500 h /= 2;
502 dst = current_image->data + current_image->offsets[1]
503 + current_image->pitches[1] * y + x;
504 if (ctx->image_format != IMGFMT_YV12)
505 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
506 else
507 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
509 dst = current_image->data + current_image->offsets[2]
510 + current_image->pitches[2] * y + x;
511 if (ctx->image_format == IMGFMT_YV12)
512 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
513 else
514 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
516 return 0;
519 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
521 struct xvctx *ctx = vo->priv;
523 ctx->have_next_image_copy = false;
525 if (mpi->flags & MP_IMGFLAG_DIRECT)
526 // direct rendering:
527 ctx->current_buf = (int) (mpi->priv); // hack!
528 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
529 ; // done
530 else if (mpi->flags & MP_IMGFLAG_PLANAR)
531 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
532 else if (mpi->flags & MP_IMGFLAG_YUV)
533 // packed YUV:
534 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
535 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
536 mpi->w * (mpi->bpp / 8), mpi->h,
537 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
538 else
539 return false;
541 if (ctx->is_paused) {
542 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
543 ctx->have_next_image_copy = true;
545 ctx->unchanged_next_image = true;
546 return true;
549 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
551 // we shouldn't change current_buf unless we do DR!
552 int buf = ctx->current_buf;
554 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
555 return VO_FALSE; // it is not static
556 if (mpi->imgfmt != ctx->image_format)
557 return VO_FALSE; // needs conversion :(
558 if (mpi->flags & MP_IMGFLAG_READABLE
559 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
560 // reference (I/P) frame of IP or IPB:
561 if (ctx->num_buffers < 2)
562 return VO_FALSE; // not enough
563 ctx->current_ip_buf ^= 1;
564 // for IPB with 2 buffers we can DR only one of the 2 P frames:
565 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
566 && ctx->current_ip_buf)
567 return VO_FALSE;
568 buf = ctx->current_ip_buf;
569 if (mpi->type == MP_IMGTYPE_IPB)
570 ++buf; // preserve space for B
572 if (mpi->height > ctx->xvimage[buf]->height)
573 return VO_FALSE; //buffer to small
574 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
575 return VO_FALSE; //buffer to small
576 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
577 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
578 ctx->current_buf = buf;
579 XvImage *current_image = ctx->xvimage[ctx->current_buf];
580 mpi->planes[0] = current_image->data + current_image->offsets[0];
581 mpi->stride[0] = current_image->pitches[0];
582 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
583 if (mpi->flags & MP_IMGFLAG_PLANAR) {
584 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
585 // I420
586 mpi->planes[1] = current_image->data
587 + current_image->offsets[1];
588 mpi->planes[2] = current_image->data
589 + current_image->offsets[2];
590 mpi->stride[1] = current_image->pitches[1];
591 mpi->stride[2] = current_image->pitches[2];
592 } else {
593 // YV12
594 mpi->planes[1] = current_image->data
595 + current_image->offsets[2];
596 mpi->planes[2] = current_image->data
597 + current_image->offsets[1];
598 mpi->stride[1] = current_image->pitches[2];
599 mpi->stride[2] = current_image->pitches[1];
602 mpi->flags |= MP_IMGFLAG_DIRECT;
603 mpi->priv = (void *) ctx->current_buf;
604 return VO_TRUE;
606 return VO_FALSE;
609 static int query_format(struct xvctx *ctx, uint32_t format)
611 uint32_t i;
612 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
614 /* check image formats */
615 for (i = 0; i < ctx->formats; i++) {
616 if (ctx->fo[i].id == format)
617 return flag; //xv_format = fo[i].id;
619 return 0;
622 static void uninit(struct vo *vo)
624 struct xvctx *ctx = vo->priv;
625 int i;
627 ctx->visible_buf = -1;
628 if (ctx->ai)
629 XvFreeAdaptorInfo(ctx->ai);
630 ctx->ai = NULL;
631 if (ctx->fo) {
632 XFree(ctx->fo);
633 ctx->fo = NULL;
635 for (i = 0; i < ctx->total_buffers; i++)
636 deallocate_xvimage(vo, i);
637 #ifdef CONFIG_XF86VM
638 if (ctx->mode_switched)
639 vo_vm_close(vo);
640 #endif
641 if (ctx->event_fd_registered)
642 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
643 // uninit() shouldn't get called unless initialization went past vo_init()
644 vo_x11_uninit(vo);
647 static int x11_fd_callback(void *ctx, int fd)
649 struct vo *vo = ctx;
650 check_events(vo);
651 return mplayer_get_key(vo->key_fifo, 0);
654 static int preinit(struct vo *vo, const char *arg)
656 XvPortID xv_p;
657 int busy_ports = 0;
658 unsigned int i;
659 strarg_t ck_src_arg = { 0, NULL };
660 strarg_t ck_method_arg = { 0, NULL };
661 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
662 vo->priv = ctx;
663 struct vo_x11_state *x11 = vo->x11;
664 int xv_adaptor = -1;
666 const opt_t subopts[] =
668 /* name arg type arg var test */
669 { "port", OPT_ARG_INT, &x11->xv_port, int_pos },
670 { "adaptor", OPT_ARG_INT, &xv_adaptor, int_non_neg },
671 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
672 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
673 { NULL }
676 x11->xv_port = 0;
678 /* parse suboptions */
679 if (subopt_parse(arg, subopts) != 0) {
680 return -1;
683 /* modify colorkey settings according to the given options */
684 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
686 if (!vo_init(vo))
687 return -1;
689 /* check for Xvideo extension */
690 unsigned int ver, rel, req, ev, err;
691 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
692 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");
693 goto error;
696 /* check for Xvideo support */
697 if (Success !=
698 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
699 &ctx->adaptors, &ctx->ai)) {
700 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] XvQueryAdaptors failed.\n");
701 goto error;
704 /* check adaptors */
705 if (x11->xv_port) {
706 int port_found;
708 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
709 if ((ctx->ai[i].type & XvInputMask)
710 && (ctx->ai[i].type & XvImageMask)) {
711 for (xv_p = ctx->ai[i].base_id;
712 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
713 ++xv_p) {
714 if (xv_p == x11->xv_port) {
715 port_found = 1;
716 break;
721 if (port_found) {
722 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
723 x11->xv_port = 0;
724 } else {
725 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Invalid port parameter, overriding with port 0.\n");
726 x11->xv_port = 0;
730 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
731 /* check if adaptor number has been specified */
732 if (xv_adaptor != -1 && xv_adaptor != i)
733 continue;
735 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
736 for (xv_p = ctx->ai[i].base_id;
737 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
738 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
739 x11->xv_port = xv_p;
740 mp_msg(MSGT_VO, MSGL_V,
741 "[VO_XV] Using Xv Adapter #%d (%s)\n",
742 i, ctx->ai[i].name);
743 break;
744 } else {
745 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Could not grab port %i.\n",
746 (int) xv_p);
747 ++busy_ports;
751 if (!x11->xv_port) {
752 if (busy_ports)
753 mp_tmsg(MSGT_VO, MSGL_ERR,
754 "[VO_XV] Could not find free Xvideo port - maybe another process is already\n"\
755 "[VO_XV] using it. Close all video applications, and try again. If that does\n"\
756 "[VO_XV] not help, see 'mplayer -vo help' for other (non-xv) video out drivers.\n");
757 else
758 mp_tmsg(MSGT_VO, MSGL_ERR,
759 "[VO_XV] It seems there is no Xvideo support for your video card available.\n"\
760 "[VO_XV] Run 'xvinfo' to verify its Xv support and read\n"\
761 "[VO_XV] DOCS/HTML/en/video.html#xv!\n"\
762 "[VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.\n"\
763 "[VO_XV] Try -vo x11.\n");
764 goto error;
767 if (!vo_xv_init_colorkey(vo)) {
768 goto error; // bail out, colorkey setup failed
770 vo_xv_enable_vsync(vo);
771 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
773 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
774 (int *) &ctx->formats);
776 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
777 x11_fd_callback, NULL, vo);
778 ctx->event_fd_registered = 1;
779 return 0;
781 error:
782 uninit(vo); // free resources
783 return -1;
786 static int control(struct vo *vo, uint32_t request, void *data)
788 struct xvctx *ctx = vo->priv;
789 struct vo_x11_state *x11 = vo->x11;
790 switch (request) {
791 case VOCTRL_PAUSE:
792 return (ctx->is_paused = 1);
793 case VOCTRL_RESUME:
794 return (ctx->is_paused = 0);
795 case VOCTRL_QUERY_FORMAT:
796 return query_format(ctx, *((uint32_t *) data));
797 case VOCTRL_GET_IMAGE:
798 return get_image(ctx, data);
799 case VOCTRL_DRAW_IMAGE:
800 return draw_image(vo, data);
801 case VOCTRL_GET_PANSCAN:
802 return VO_TRUE;
803 case VOCTRL_FULLSCREEN:
804 vo_x11_fullscreen(vo);
805 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
806 case VOCTRL_SET_PANSCAN:
807 resize(vo);
808 return VO_TRUE;
809 case VOCTRL_SET_EQUALIZER:
811 struct voctrl_set_equalizer_args *args = data;
812 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
814 case VOCTRL_GET_EQUALIZER:
816 struct voctrl_get_equalizer_args *args = data;
817 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
819 case VOCTRL_SET_YUV_COLORSPACE:;
820 int given_cspc = *(int *)data % 2;
821 return vo_xv_set_eq(vo, x11->xv_port, "bt_709", given_cspc * 200 - 100);
822 case VOCTRL_GET_YUV_COLORSPACE:;
823 int bt709_enabled;
824 if (!vo_xv_get_eq(vo, x11->xv_port, "bt_709", &bt709_enabled))
825 return false;
826 *(int *)data = bt709_enabled == 100;
827 return true;
828 case VOCTRL_ONTOP:
829 vo_x11_ontop(vo);
830 return VO_TRUE;
831 case VOCTRL_UPDATE_SCREENINFO:
832 update_xinerama_info(vo);
833 return VO_TRUE;
834 case VOCTRL_REDRAW_OSD:
835 return redraw_osd(vo, data);
837 return VO_NOTIMPL;
840 const struct vo_driver video_out_xv = {
841 .is_new = 1,
842 .info = &info,
843 .preinit = preinit,
844 .config = config,
845 .control = control,
846 .draw_slice = draw_slice,
847 .draw_osd = draw_osd,
848 .flip_page = flip_page,
849 .check_events = check_events,
850 .uninit = uninit