Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libvo / vo_xv.c
blob49ff146ba0a3a00a8f65ff9a7c6db474db6d333c
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.h"
60 #include "aspect.h"
62 #include "subopt-helper.h"
64 #include "input/input.h"
65 #include "mp_fifo.h"
67 #include "libavutil/common.h"
69 static const vo_info_t info = {
70 "X11/Xv",
71 "xv",
72 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
76 #ifdef HAVE_SHM
77 #include <sys/ipc.h>
78 #include <sys/shm.h>
79 #include <X11/extensions/XShm.h>
80 #endif
82 // Note: depends on the inclusion of X11/extensions/XShm.h
83 #include <X11/extensions/Xv.h>
84 #include <X11/extensions/Xvlib.h>
86 struct xvctx {
87 XvAdaptorInfo *ai;
88 XvImageFormatValues *fo;
89 unsigned int formats, adaptors, xv_format;
90 int current_buf;
91 int current_ip_buf;
92 int num_buffers;
93 int total_buffers;
94 int have_visible_image_copy;
95 int have_next_image_copy;
96 int unchanged_visible_image;
97 int unchanged_next_image;
98 int visible_buf;
99 XvImage *xvimage[NUM_BUFFERS + 1];
100 uint32_t image_width;
101 uint32_t image_height;
102 uint32_t image_format;
103 int is_paused;
104 struct vo_rect src_rect;
105 struct vo_rect dst_rect;
106 uint32_t max_width, max_height; // zero means: not set
107 int event_fd_registered; // for uninit called from preinit
108 int mode_switched;
109 int osd_objects_drawn;
110 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
111 unsigned char *src, unsigned char *srca,
112 int stride);
113 #ifdef HAVE_SHM
114 XShmSegmentInfo Shminfo[NUM_BUFFERS + 1];
115 int Shmem_Flag;
116 #endif
119 static void allocate_xvimage(struct vo *, int);
122 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
123 unsigned char *src, unsigned char *srca,
124 int stride)
126 struct vo *vo = p;
127 struct xvctx *ctx = vo->priv;
128 x0 += ctx->image_width * (vo->panscan_x >> 1)
129 / (vo->dwidth + vo->panscan_x);
130 vo_draw_alpha_yv12(w, h, src, srca, stride,
131 ctx->xvimage[ctx->current_buf]->data +
132 ctx->xvimage[ctx->current_buf]->offsets[0] +
133 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
134 ctx->xvimage[ctx->current_buf]->pitches[0]);
135 ctx->osd_objects_drawn++;
138 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
139 unsigned char *src, unsigned char *srca,
140 int stride)
142 struct vo *vo = p;
143 struct xvctx *ctx = vo->priv;
144 x0 += ctx->image_width * (vo->panscan_x >> 1)
145 / (vo->dwidth + vo->panscan_x);
146 vo_draw_alpha_yuy2(w, h, src, srca, stride,
147 ctx->xvimage[ctx->current_buf]->data +
148 ctx->xvimage[ctx->current_buf]->offsets[0] +
149 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
150 ctx->xvimage[ctx->current_buf]->pitches[0]);
151 ctx->osd_objects_drawn++;
154 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
155 unsigned char *src, unsigned char *srca,
156 int stride)
158 struct vo *vo = p;
159 struct xvctx *ctx = vo->priv;
160 x0 += ctx->image_width * (vo->panscan_x >> 1)
161 / (vo->dwidth + vo->panscan_x);
162 vo_draw_alpha_yuy2(w, h, src, srca, stride,
163 ctx->xvimage[ctx->current_buf]->data +
164 ctx->xvimage[ctx->current_buf]->offsets[0] +
165 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
166 ctx->xvimage[ctx->current_buf]->pitches[0]);
167 ctx->osd_objects_drawn++;
170 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
171 unsigned char *src, unsigned char *srca,
172 int stride)
177 static void deallocate_xvimage(struct vo *vo, int foo);
179 static void resize(struct vo *vo)
181 struct xvctx *ctx = vo->priv;
183 calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
184 &ctx->dst_rect, NULL, NULL);
185 struct vo_rect *dst = &ctx->dst_rect;
186 vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height, 1);
187 vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
191 * connect to server, create and map window,
192 * allocate colors and (shared) memory
194 static int config(struct vo *vo, uint32_t width, uint32_t height,
195 uint32_t d_width, uint32_t d_height, uint32_t flags,
196 char *title, uint32_t format)
198 struct vo_x11_state *x11 = vo->x11;
199 XVisualInfo vinfo;
200 XSetWindowAttributes xswa;
201 XWindowAttributes attribs;
202 unsigned long xswamask;
203 int depth;
204 struct xvctx *ctx = vo->priv;
205 int i;
207 ctx->image_height = height;
208 ctx->image_width = width;
209 ctx->image_format = format;
211 if ((ctx->max_width != 0 && ctx->max_height != 0)
212 && (ctx->image_width > ctx->max_width
213 || ctx->image_height > ctx->max_height)) {
214 mp_tmsg(MSGT_VO, MSGL_ERR, "Source image dimensions are too high: %ux%u (maximum is %ux%u)\n",
215 ctx->image_width, ctx->image_height, ctx->max_width,
216 ctx->max_height);
217 return -1;
220 ctx->visible_buf = -1;
221 ctx->have_visible_image_copy = false;
222 ctx->have_next_image_copy = false;
224 /* check image formats */
225 ctx->xv_format = 0;
226 for (i = 0; i < ctx->formats; i++) {
227 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
228 ctx->fo[i].id, (char *) &ctx->fo[i].id,
229 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
230 if (ctx->fo[i].id == format)
231 ctx->xv_format = ctx->fo[i].id;
233 if (!ctx->xv_format)
234 return -1;
237 #ifdef CONFIG_XF86VM
238 int vm = flags & VOFLAG_MODESWITCHING;
239 if (vm) {
240 vo_vm_switch(vo);
241 ctx->mode_switched = 1;
243 #endif
244 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
245 &attribs);
246 depth = attribs.depth;
247 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
248 depth = 24;
249 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
251 xswa.background_pixel = 0;
252 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND)
253 xswa.background_pixel = x11->xv_colorkey;
254 xswa.border_pixel = 0;
255 xswamask = CWBackPixel | CWBorderPixel;
257 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
258 vo->dheight, flags, CopyFromParent, "xv",
259 title);
260 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
262 #ifdef CONFIG_XF86VM
263 if (vm) {
264 /* Grab the mouse pointer in our window */
265 if (vo_grabpointer)
266 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
267 GrabModeAsync, x11->window, None, CurrentTime);
268 XSetInputFocus(x11->display, x11->window, RevertToNone,
269 CurrentTime);
271 #endif
274 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
275 x11->xv_port);
277 switch (ctx->xv_format) {
278 case IMGFMT_YV12:
279 case IMGFMT_I420:
280 case IMGFMT_IYUV:
281 ctx->draw_alpha_fnc = draw_alpha_yv12;
282 break;
283 case IMGFMT_YUY2:
284 case IMGFMT_YVYU:
285 ctx->draw_alpha_fnc = draw_alpha_yuy2;
286 break;
287 case IMGFMT_UYVY:
288 ctx->draw_alpha_fnc = draw_alpha_uyvy;
289 break;
290 default:
291 ctx->draw_alpha_fnc = draw_alpha_null;
294 // In case config has been called before
295 for (i = 0; i < ctx->total_buffers; i++)
296 deallocate_xvimage(vo, i);
298 ctx->num_buffers =
299 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
300 ctx->total_buffers = ctx->num_buffers + 1;
302 for (i = 0; i < ctx->total_buffers; i++)
303 allocate_xvimage(vo, i);
305 ctx->current_buf = 0;
306 ctx->current_ip_buf = 0;
309 resize(vo);
311 return 0;
314 static void allocate_xvimage(struct vo *vo, int foo)
316 struct xvctx *ctx = vo->priv;
317 struct vo_x11_state *x11 = vo->x11;
319 * allocate XvImages. FIXME: no error checking, without
320 * mit-shm this will bomb... trzing to fix ::atmos
322 #ifdef HAVE_SHM
323 if (x11->display_is_local && XShmQueryExtension(x11->display))
324 ctx->Shmem_Flag = 1;
325 else {
326 ctx->Shmem_Flag = 0;
327 mp_tmsg(MSGT_VO, MSGL_INFO, "[VO_XV] Shared memory not supported\nReverting to normal Xv.\n");
329 if (ctx->Shmem_Flag) {
330 ctx->xvimage[foo] =
331 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
332 ctx->xv_format, NULL,
333 ctx->image_width, ctx->image_height,
334 &ctx->Shminfo[foo]);
336 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
337 ctx->xvimage[foo]->data_size,
338 IPC_CREAT | 0777);
339 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
341 ctx->Shminfo[foo].readOnly = False;
343 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
344 XShmAttach(x11->display, &ctx->Shminfo[foo]);
345 XSync(x11->display, False);
346 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
347 } else
348 #endif
350 ctx->xvimage[foo] =
351 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
352 ctx->xv_format, NULL, ctx->image_width,
353 ctx->image_height);
354 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
355 XSync(x11->display, False);
357 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
358 return;
361 static void deallocate_xvimage(struct vo *vo, int foo)
363 struct xvctx *ctx = vo->priv;
364 #ifdef HAVE_SHM
365 if (ctx->Shmem_Flag) {
366 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
367 shmdt(ctx->Shminfo[foo].shmaddr);
368 } else
369 #endif
371 free(ctx->xvimage[foo]->data);
373 XFree(ctx->xvimage[foo]);
375 XSync(vo->x11->display, False);
376 return;
379 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
381 struct xvctx *ctx = vo->priv;
382 struct vo_x11_state *x11 = vo->x11;
383 struct vo_rect *src = &ctx->src_rect;
384 struct vo_rect *dst = &ctx->dst_rect;
385 #ifdef HAVE_SHM
386 if (ctx->Shmem_Flag) {
387 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
388 src->left, src->top, src->width, src->height,
389 dst->left, dst->top, dst->width, dst->height,
390 False);
391 } else
392 #endif
394 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
395 src->left, src->top, src->width, src->height,
396 dst->left, dst->top, dst->width, dst->height);
400 // Only copies luma for planar formats as draw_alpha doesn't change others */
401 static void copy_backup_image(struct vo *vo, int dest, int src)
403 struct xvctx *ctx = vo->priv;
405 XvImage *vb = ctx->xvimage[dest];
406 XvImage *cp = ctx->xvimage[src];
407 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
408 vb->width, vb->height,
409 vb->pitches[0], cp->pitches[0]);
412 static void check_events(struct vo *vo)
414 struct xvctx *ctx = vo->priv;
415 int e = vo_x11_check_events(vo);
417 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
418 resize(vo);
420 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
421 /* did we already draw a buffer */
422 if (ctx->visible_buf != -1) {
423 /* redraw the last visible buffer */
424 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
429 static void draw_osd(struct vo *vo, struct osd_state *osd)
431 struct xvctx *ctx = vo->priv;
433 ctx->osd_objects_drawn = 0;
434 osd_draw_text(osd,
435 ctx->image_width -
436 ctx->image_width * vo->panscan_x / (vo->dwidth +
437 vo->panscan_x),
438 ctx->image_height, ctx->draw_alpha_fnc, vo);
439 if (ctx->osd_objects_drawn)
440 ctx->unchanged_next_image = false;
443 static int redraw_osd(struct vo *vo, struct osd_state *osd)
445 struct xvctx *ctx = vo->priv;
447 if (ctx->have_visible_image_copy)
448 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
449 else if (ctx->unchanged_visible_image) {
450 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
451 ctx->have_visible_image_copy = true;
453 else
454 return false;
455 int temp = ctx->current_buf;
456 ctx->current_buf = ctx->visible_buf;
457 draw_osd(vo, osd);
458 ctx->current_buf = temp;
459 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
460 return true;
463 static void flip_page(struct vo *vo)
465 struct xvctx *ctx = vo->priv;
466 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
468 /* remember the currently visible buffer */
469 ctx->visible_buf = ctx->current_buf;
471 ctx->have_visible_image_copy = ctx->have_next_image_copy;
472 ctx->have_next_image_copy = false;
473 ctx->unchanged_visible_image = ctx->unchanged_next_image;
474 ctx->unchanged_next_image = false;
476 if (ctx->num_buffers > 1) {
477 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
478 ctx->num_buffers);
479 XFlush(vo->x11->display);
480 } else
481 XSync(vo->x11->display, False);
482 return;
485 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
486 int h, int x, int y)
488 struct xvctx *ctx = vo->priv;
489 uint8_t *dst;
490 XvImage *current_image = ctx->xvimage[ctx->current_buf];
492 dst = current_image->data + current_image->offsets[0]
493 + current_image->pitches[0] * y + x;
494 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
496 x /= 2;
497 y /= 2;
498 w /= 2;
499 h /= 2;
501 dst = current_image->data + current_image->offsets[1]
502 + current_image->pitches[1] * y + x;
503 if (ctx->image_format != IMGFMT_YV12)
504 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
505 else
506 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
508 dst = current_image->data + current_image->offsets[2]
509 + current_image->pitches[2] * y + x;
510 if (ctx->image_format == IMGFMT_YV12)
511 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
512 else
513 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
515 return 0;
518 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
520 struct xvctx *ctx = vo->priv;
522 ctx->have_next_image_copy = false;
524 if (mpi->flags & MP_IMGFLAG_DIRECT)
525 // direct rendering:
526 ctx->current_buf = (int) (mpi->priv); // hack!
527 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
528 ; // done
529 else if (mpi->flags & MP_IMGFLAG_PLANAR)
530 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
531 else if (mpi->flags & MP_IMGFLAG_YUV)
532 // packed YUV:
533 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
534 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
535 mpi->w * (mpi->bpp / 8), mpi->h,
536 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
537 else
538 return false;
540 if (ctx->is_paused) {
541 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
542 ctx->have_next_image_copy = true;
544 ctx->unchanged_next_image = true;
545 return true;
548 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
550 // we shouldn't change current_buf unless we do DR!
551 int buf = ctx->current_buf;
553 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
554 return VO_FALSE; // it is not static
555 if (mpi->imgfmt != ctx->image_format)
556 return VO_FALSE; // needs conversion :(
557 if (mpi->flags & MP_IMGFLAG_READABLE
558 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
559 // reference (I/P) frame of IP or IPB:
560 if (ctx->num_buffers < 2)
561 return VO_FALSE; // not enough
562 ctx->current_ip_buf ^= 1;
563 // for IPB with 2 buffers we can DR only one of the 2 P frames:
564 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
565 && ctx->current_ip_buf)
566 return VO_FALSE;
567 buf = ctx->current_ip_buf;
568 if (mpi->type == MP_IMGTYPE_IPB)
569 ++buf; // preserve space for B
571 if (mpi->height > ctx->xvimage[buf]->height)
572 return VO_FALSE; //buffer to small
573 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
574 return VO_FALSE; //buffer to small
575 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
576 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
577 ctx->current_buf = buf;
578 XvImage *current_image = ctx->xvimage[ctx->current_buf];
579 mpi->planes[0] = current_image->data + current_image->offsets[0];
580 mpi->stride[0] = current_image->pitches[0];
581 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
582 if (mpi->flags & MP_IMGFLAG_PLANAR) {
583 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
584 // I420
585 mpi->planes[1] = current_image->data
586 + current_image->offsets[1];
587 mpi->planes[2] = current_image->data
588 + current_image->offsets[2];
589 mpi->stride[1] = current_image->pitches[1];
590 mpi->stride[2] = current_image->pitches[2];
591 } else {
592 // YV12
593 mpi->planes[1] = current_image->data
594 + current_image->offsets[2];
595 mpi->planes[2] = current_image->data
596 + current_image->offsets[1];
597 mpi->stride[1] = current_image->pitches[2];
598 mpi->stride[2] = current_image->pitches[1];
601 mpi->flags |= MP_IMGFLAG_DIRECT;
602 mpi->priv = (void *) ctx->current_buf;
603 return VO_TRUE;
605 return VO_FALSE;
608 static int query_format(struct xvctx *ctx, uint32_t format)
610 uint32_t i;
611 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
613 /* check image formats */
614 for (i = 0; i < ctx->formats; i++) {
615 if (ctx->fo[i].id == format)
616 return flag; //xv_format = fo[i].id;
618 return 0;
621 static void uninit(struct vo *vo)
623 struct xvctx *ctx = vo->priv;
624 int i;
626 ctx->visible_buf = -1;
627 if (ctx->ai)
628 XvFreeAdaptorInfo(ctx->ai);
629 ctx->ai = NULL;
630 if (ctx->fo) {
631 XFree(ctx->fo);
632 ctx->fo = NULL;
634 for (i = 0; i < ctx->total_buffers; i++)
635 deallocate_xvimage(vo, i);
636 #ifdef CONFIG_XF86VM
637 if (ctx->mode_switched)
638 vo_vm_close(vo);
639 #endif
640 if (ctx->event_fd_registered)
641 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
642 // uninit() shouldn't get called unless initialization went past vo_init()
643 vo_x11_uninit(vo);
646 static int x11_fd_callback(void *ctx, int fd)
648 struct vo *vo = ctx;
649 check_events(vo);
650 return mplayer_get_key(vo->key_fifo, 0);
653 static int preinit(struct vo *vo, const char *arg)
655 XvPortID xv_p;
656 int busy_ports = 0;
657 unsigned int i;
658 strarg_t ck_src_arg = { 0, NULL };
659 strarg_t ck_method_arg = { 0, NULL };
660 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
661 vo->priv = ctx;
662 struct vo_x11_state *x11 = vo->x11;
663 int xv_adaptor = -1;
665 const opt_t subopts[] =
667 /* name arg type arg var test */
668 { "port", OPT_ARG_INT, &x11->xv_port, int_pos },
669 { "adaptor", OPT_ARG_INT, &xv_adaptor, int_non_neg },
670 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
671 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
672 { NULL }
675 x11->xv_port = 0;
677 /* parse suboptions */
678 if (subopt_parse(arg, subopts) != 0) {
679 return -1;
682 /* modify colorkey settings according to the given options */
683 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
685 if (!vo_init(vo))
686 return -1;
688 /* check for Xvideo extension */
689 unsigned int ver, rel, req, ev, err;
690 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
691 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");
692 goto error;
695 /* check for Xvideo support */
696 if (Success !=
697 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
698 &ctx->adaptors, &ctx->ai)) {
699 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] XvQueryAdaptors failed.\n");
700 goto error;
703 /* check adaptors */
704 if (x11->xv_port) {
705 int port_found;
707 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
708 if ((ctx->ai[i].type & XvInputMask)
709 && (ctx->ai[i].type & XvImageMask)) {
710 for (xv_p = ctx->ai[i].base_id;
711 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
712 ++xv_p) {
713 if (xv_p == x11->xv_port) {
714 port_found = 1;
715 break;
720 if (port_found) {
721 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
722 x11->xv_port = 0;
723 } else {
724 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Invalid port parameter, overriding with port 0.\n");
725 x11->xv_port = 0;
729 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
730 /* check if adaptor number has been specified */
731 if (xv_adaptor != -1 && xv_adaptor != i)
732 continue;
734 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
735 for (xv_p = ctx->ai[i].base_id;
736 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
737 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
738 x11->xv_port = xv_p;
739 mp_msg(MSGT_VO, MSGL_V,
740 "[VO_XV] Using Xv Adapter #%d (%s)\n",
741 i, ctx->ai[i].name);
742 break;
743 } else {
744 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Could not grab port %i.\n",
745 (int) xv_p);
746 ++busy_ports;
750 if (!x11->xv_port) {
751 if (busy_ports)
752 mp_tmsg(MSGT_VO, MSGL_ERR,
753 "[VO_XV] Could not find free Xvideo port - maybe another process is already\n"\
754 "[VO_XV] using it. Close all video applications, and try again. If that does\n"\
755 "[VO_XV] not help, see 'mplayer -vo help' for other (non-xv) video out drivers.\n");
756 else
757 mp_tmsg(MSGT_VO, MSGL_ERR,
758 "[VO_XV] It seems there is no Xvideo support for your video card available.\n"\
759 "[VO_XV] Run 'xvinfo' to verify its Xv support and read\n"\
760 "[VO_XV] DOCS/HTML/en/video.html#xv!\n"\
761 "[VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.\n"\
762 "[VO_XV] Try -vo x11.\n");
763 goto error;
766 if (!vo_xv_init_colorkey(vo)) {
767 goto error; // bail out, colorkey setup failed
769 vo_xv_enable_vsync(vo);
770 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
772 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
773 (int *) &ctx->formats);
775 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
776 x11_fd_callback, NULL, vo);
777 ctx->event_fd_registered = 1;
778 return 0;
780 error:
781 uninit(vo); // free resources
782 return -1;
785 static int control(struct vo *vo, uint32_t request, void *data)
787 struct xvctx *ctx = vo->priv;
788 struct vo_x11_state *x11 = vo->x11;
789 switch (request) {
790 case VOCTRL_PAUSE:
791 return (ctx->is_paused = 1);
792 case VOCTRL_RESUME:
793 return (ctx->is_paused = 0);
794 case VOCTRL_QUERY_FORMAT:
795 return query_format(ctx, *((uint32_t *) data));
796 case VOCTRL_GET_IMAGE:
797 return get_image(ctx, data);
798 case VOCTRL_DRAW_IMAGE:
799 return draw_image(vo, data);
800 case VOCTRL_GET_PANSCAN:
801 return VO_TRUE;
802 case VOCTRL_FULLSCREEN:
803 vo_x11_fullscreen(vo);
804 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
805 case VOCTRL_SET_PANSCAN:
806 resize(vo);
807 return VO_TRUE;
808 case VOCTRL_SET_EQUALIZER:
810 struct voctrl_set_equalizer_args *args = data;
811 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
813 case VOCTRL_GET_EQUALIZER:
815 struct voctrl_get_equalizer_args *args = data;
816 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
818 case VOCTRL_SET_YUV_COLORSPACE:;
819 int given_cspc = *(int *)data % 2;
820 return vo_xv_set_eq(vo, x11->xv_port, "bt_709", given_cspc * 200 - 100);
821 case VOCTRL_GET_YUV_COLORSPACE:;
822 int bt709_enabled;
823 if (!vo_xv_get_eq(vo, x11->xv_port, "bt_709", &bt709_enabled))
824 return false;
825 *(int *)data = bt709_enabled == 100;
826 return true;
827 case VOCTRL_ONTOP:
828 vo_x11_ontop(vo);
829 return VO_TRUE;
830 case VOCTRL_UPDATE_SCREENINFO:
831 update_xinerama_info(vo);
832 return VO_TRUE;
833 case VOCTRL_REDRAW_OSD:
834 return redraw_osd(vo, data);
836 return VO_NOTIMPL;
839 const struct vo_driver video_out_xv = {
840 .is_new = 1,
841 .info = &info,
842 .preinit = preinit,
843 .config = config,
844 .control = control,
845 .draw_slice = draw_slice,
846 .draw_osd = draw_osd,
847 .flip_page = flip_page,
848 .check_events = check_events,
849 .uninit = uninit