codecs.conf: add matchware screen codec fourcc MWSC
[mplayer/glamo.git] / libvo / vo_xv.c
blob0dbc6008478a3a48d75ce04a2961720f9d250fc5
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 fixup_osd_position(struct vo *vo, int *x0, int *y0, int *w, int *h)
124 struct xvctx *ctx = vo->priv;
125 *x0 += ctx->image_width * (vo->panscan_x >> 1)
126 / (vo->dwidth + vo->panscan_x);
127 *w = av_clip(*w, 0, ctx->image_width);
128 *h = av_clip(*h, 0, ctx->image_height);
129 *x0 = FFMIN(*x0, ctx->image_width - *w);
130 *y0 = FFMIN(*y0, ctx->image_height - *h);
133 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
134 unsigned char *src, unsigned char *srca,
135 int stride)
137 struct vo *vo = p;
138 struct xvctx *ctx = vo->priv;
139 fixup_osd_position(vo, &x0, &y0, &w, &h);
140 vo_draw_alpha_yv12(w, h, src, srca, stride,
141 ctx->xvimage[ctx->current_buf]->data +
142 ctx->xvimage[ctx->current_buf]->offsets[0] +
143 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
144 ctx->xvimage[ctx->current_buf]->pitches[0]);
145 ctx->osd_objects_drawn++;
148 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
149 unsigned char *src, unsigned char *srca,
150 int stride)
152 struct vo *vo = p;
153 struct xvctx *ctx = vo->priv;
154 fixup_osd_position(vo, &x0, &y0, &w, &h);
155 vo_draw_alpha_yuy2(w, h, src, srca, stride,
156 ctx->xvimage[ctx->current_buf]->data +
157 ctx->xvimage[ctx->current_buf]->offsets[0] +
158 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
159 ctx->xvimage[ctx->current_buf]->pitches[0]);
160 ctx->osd_objects_drawn++;
163 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
164 unsigned char *src, unsigned char *srca,
165 int stride)
167 struct vo *vo = p;
168 struct xvctx *ctx = vo->priv;
169 fixup_osd_position(vo, &x0, &y0, &w, &h);
170 vo_draw_alpha_yuy2(w, h, src, srca, stride,
171 ctx->xvimage[ctx->current_buf]->data +
172 ctx->xvimage[ctx->current_buf]->offsets[0] +
173 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
174 ctx->xvimage[ctx->current_buf]->pitches[0]);
175 ctx->osd_objects_drawn++;
178 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
179 unsigned char *src, unsigned char *srca,
180 int stride)
185 static void deallocate_xvimage(struct vo *vo, int foo);
187 static void resize(struct vo *vo)
189 struct xvctx *ctx = vo->priv;
191 calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
192 &ctx->dst_rect, NULL, NULL);
193 struct vo_rect *dst = &ctx->dst_rect;
194 vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height, 1);
195 vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
199 * connect to server, create and map window,
200 * allocate colors and (shared) memory
202 static int config(struct vo *vo, uint32_t width, uint32_t height,
203 uint32_t d_width, uint32_t d_height, uint32_t flags,
204 char *title, uint32_t format)
206 struct vo_x11_state *x11 = vo->x11;
207 XVisualInfo vinfo;
208 XSetWindowAttributes xswa;
209 XWindowAttributes attribs;
210 unsigned long xswamask;
211 int depth;
212 struct xvctx *ctx = vo->priv;
213 int i;
215 ctx->image_height = height;
216 ctx->image_width = width;
217 ctx->image_format = format;
219 if ((ctx->max_width != 0 && ctx->max_height != 0)
220 && (ctx->image_width > ctx->max_width
221 || ctx->image_height > ctx->max_height)) {
222 mp_tmsg(MSGT_VO, MSGL_ERR, "Source image dimensions are too high: %ux%u (maximum is %ux%u)\n",
223 ctx->image_width, ctx->image_height, ctx->max_width,
224 ctx->max_height);
225 return -1;
228 ctx->visible_buf = -1;
229 ctx->have_visible_image_copy = false;
230 ctx->have_next_image_copy = false;
232 /* check image formats */
233 ctx->xv_format = 0;
234 for (i = 0; i < ctx->formats; i++) {
235 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
236 ctx->fo[i].id, (char *) &ctx->fo[i].id,
237 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
238 if (ctx->fo[i].id == format)
239 ctx->xv_format = ctx->fo[i].id;
241 if (!ctx->xv_format)
242 return -1;
245 #ifdef CONFIG_XF86VM
246 int vm = flags & VOFLAG_MODESWITCHING;
247 if (vm) {
248 vo_vm_switch(vo);
249 ctx->mode_switched = 1;
251 #endif
252 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
253 &attribs);
254 depth = attribs.depth;
255 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
256 depth = 24;
257 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
259 xswa.background_pixel = 0;
260 if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND)
261 xswa.background_pixel = x11->xv_colorkey;
262 xswa.border_pixel = 0;
263 xswamask = CWBackPixel | CWBorderPixel;
265 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
266 vo->dheight, flags, CopyFromParent, "xv",
267 title);
268 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
270 #ifdef CONFIG_XF86VM
271 if (vm) {
272 /* Grab the mouse pointer in our window */
273 if (vo_grabpointer)
274 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
275 GrabModeAsync, x11->window, None, CurrentTime);
276 XSetInputFocus(x11->display, x11->window, RevertToNone,
277 CurrentTime);
279 #endif
282 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
283 x11->xv_port);
285 switch (ctx->xv_format) {
286 case IMGFMT_YV12:
287 case IMGFMT_I420:
288 case IMGFMT_IYUV:
289 ctx->draw_alpha_fnc = draw_alpha_yv12;
290 break;
291 case IMGFMT_YUY2:
292 case IMGFMT_YVYU:
293 ctx->draw_alpha_fnc = draw_alpha_yuy2;
294 break;
295 case IMGFMT_UYVY:
296 ctx->draw_alpha_fnc = draw_alpha_uyvy;
297 break;
298 default:
299 ctx->draw_alpha_fnc = draw_alpha_null;
302 // In case config has been called before
303 for (i = 0; i < ctx->total_buffers; i++)
304 deallocate_xvimage(vo, i);
306 ctx->num_buffers =
307 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
308 ctx->total_buffers = ctx->num_buffers + 1;
310 for (i = 0; i < ctx->total_buffers; i++)
311 allocate_xvimage(vo, i);
313 ctx->current_buf = 0;
314 ctx->current_ip_buf = 0;
317 resize(vo);
319 return 0;
322 static void allocate_xvimage(struct vo *vo, int foo)
324 struct xvctx *ctx = vo->priv;
325 struct vo_x11_state *x11 = vo->x11;
327 * allocate XvImages. FIXME: no error checking, without
328 * mit-shm this will bomb... trzing to fix ::atmos
330 #ifdef HAVE_SHM
331 if (x11->display_is_local && XShmQueryExtension(x11->display))
332 ctx->Shmem_Flag = 1;
333 else {
334 ctx->Shmem_Flag = 0;
335 mp_tmsg(MSGT_VO, MSGL_INFO, "[VO_XV] Shared memory not supported\nReverting to normal Xv.\n");
337 if (ctx->Shmem_Flag) {
338 ctx->xvimage[foo] =
339 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
340 ctx->xv_format, NULL,
341 ctx->image_width, ctx->image_height,
342 &ctx->Shminfo[foo]);
344 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
345 ctx->xvimage[foo]->data_size,
346 IPC_CREAT | 0777);
347 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
349 ctx->Shminfo[foo].readOnly = False;
351 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
352 XShmAttach(x11->display, &ctx->Shminfo[foo]);
353 XSync(x11->display, False);
354 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
355 } else
356 #endif
358 ctx->xvimage[foo] =
359 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
360 ctx->xv_format, NULL, ctx->image_width,
361 ctx->image_height);
362 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
363 XSync(x11->display, False);
365 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
366 return;
369 static void deallocate_xvimage(struct vo *vo, int foo)
371 struct xvctx *ctx = vo->priv;
372 #ifdef HAVE_SHM
373 if (ctx->Shmem_Flag) {
374 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
375 shmdt(ctx->Shminfo[foo].shmaddr);
376 } else
377 #endif
379 free(ctx->xvimage[foo]->data);
381 XFree(ctx->xvimage[foo]);
383 XSync(vo->x11->display, False);
384 return;
387 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
389 struct xvctx *ctx = vo->priv;
390 struct vo_x11_state *x11 = vo->x11;
391 struct vo_rect *src = &ctx->src_rect;
392 struct vo_rect *dst = &ctx->dst_rect;
393 #ifdef HAVE_SHM
394 if (ctx->Shmem_Flag) {
395 XvShmPutImage(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,
398 False);
399 } else
400 #endif
402 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
403 src->left, src->top, src->width, src->height,
404 dst->left, dst->top, dst->width, dst->height);
408 // Only copies luma for planar formats as draw_alpha doesn't change others */
409 static void copy_backup_image(struct vo *vo, int dest, int src)
411 struct xvctx *ctx = vo->priv;
413 XvImage *vb = ctx->xvimage[dest];
414 XvImage *cp = ctx->xvimage[src];
415 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
416 vb->width, vb->height,
417 vb->pitches[0], cp->pitches[0]);
420 static void check_events(struct vo *vo)
422 struct xvctx *ctx = vo->priv;
423 int e = vo_x11_check_events(vo);
425 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
426 resize(vo);
428 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
429 /* did we already draw a buffer */
430 if (ctx->visible_buf != -1) {
431 /* redraw the last visible buffer */
432 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
437 static void draw_osd(struct vo *vo, struct osd_state *osd)
439 struct xvctx *ctx = vo->priv;
441 ctx->osd_objects_drawn = 0;
442 osd_draw_text(osd,
443 ctx->image_width -
444 ctx->image_width * vo->panscan_x / (vo->dwidth +
445 vo->panscan_x),
446 ctx->image_height, ctx->draw_alpha_fnc, vo);
447 if (ctx->osd_objects_drawn)
448 ctx->unchanged_next_image = false;
451 static int redraw_osd(struct vo *vo, struct osd_state *osd)
453 struct xvctx *ctx = vo->priv;
455 if (ctx->have_visible_image_copy)
456 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
457 else if (ctx->unchanged_visible_image) {
458 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
459 ctx->have_visible_image_copy = true;
461 else
462 return false;
463 int temp = ctx->current_buf;
464 ctx->current_buf = ctx->visible_buf;
465 draw_osd(vo, osd);
466 ctx->current_buf = temp;
467 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
468 return true;
471 static void flip_page(struct vo *vo)
473 struct xvctx *ctx = vo->priv;
474 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
476 /* remember the currently visible buffer */
477 ctx->visible_buf = ctx->current_buf;
479 ctx->have_visible_image_copy = ctx->have_next_image_copy;
480 ctx->have_next_image_copy = false;
481 ctx->unchanged_visible_image = ctx->unchanged_next_image;
482 ctx->unchanged_next_image = false;
484 if (ctx->num_buffers > 1) {
485 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
486 ctx->num_buffers);
487 XFlush(vo->x11->display);
488 } else
489 XSync(vo->x11->display, False);
490 return;
493 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
494 int h, int x, int y)
496 struct xvctx *ctx = vo->priv;
497 uint8_t *dst;
498 XvImage *current_image = ctx->xvimage[ctx->current_buf];
500 dst = current_image->data + current_image->offsets[0]
501 + current_image->pitches[0] * y + x;
502 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
504 x /= 2;
505 y /= 2;
506 w /= 2;
507 h /= 2;
509 dst = current_image->data + current_image->offsets[1]
510 + current_image->pitches[1] * 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 dst = current_image->data + current_image->offsets[2]
517 + current_image->pitches[2] * y + x;
518 if (ctx->image_format == IMGFMT_YV12)
519 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
520 else
521 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
523 return 0;
526 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
528 struct xvctx *ctx = vo->priv;
530 ctx->have_next_image_copy = false;
532 if (mpi->flags & MP_IMGFLAG_DIRECT)
533 // direct rendering:
534 ctx->current_buf = (int) (mpi->priv); // hack!
535 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
536 ; // done
537 else if (mpi->flags & MP_IMGFLAG_PLANAR)
538 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
539 else if (mpi->flags & MP_IMGFLAG_YUV)
540 // packed YUV:
541 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
542 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
543 mpi->w * (mpi->bpp / 8), mpi->h,
544 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
545 else
546 return false;
548 if (ctx->is_paused) {
549 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
550 ctx->have_next_image_copy = true;
552 ctx->unchanged_next_image = true;
553 return true;
556 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
558 // we shouldn't change current_buf unless we do DR!
559 int buf = ctx->current_buf;
561 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
562 return VO_FALSE; // it is not static
563 if (mpi->imgfmt != ctx->image_format)
564 return VO_FALSE; // needs conversion :(
565 if (mpi->flags & MP_IMGFLAG_READABLE
566 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
567 // reference (I/P) frame of IP or IPB:
568 if (ctx->num_buffers < 2)
569 return VO_FALSE; // not enough
570 ctx->current_ip_buf ^= 1;
571 // for IPB with 2 buffers we can DR only one of the 2 P frames:
572 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
573 && ctx->current_ip_buf)
574 return VO_FALSE;
575 buf = ctx->current_ip_buf;
576 if (mpi->type == MP_IMGTYPE_IPB)
577 ++buf; // preserve space for B
579 if (mpi->height > ctx->xvimage[buf]->height)
580 return VO_FALSE; //buffer to small
581 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
582 return VO_FALSE; //buffer to small
583 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
584 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
585 ctx->current_buf = buf;
586 XvImage *current_image = ctx->xvimage[ctx->current_buf];
587 mpi->planes[0] = current_image->data + current_image->offsets[0];
588 mpi->stride[0] = current_image->pitches[0];
589 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
590 if (mpi->flags & MP_IMGFLAG_PLANAR) {
591 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
592 // I420
593 mpi->planes[1] = current_image->data
594 + current_image->offsets[1];
595 mpi->planes[2] = current_image->data
596 + current_image->offsets[2];
597 mpi->stride[1] = current_image->pitches[1];
598 mpi->stride[2] = current_image->pitches[2];
599 } else {
600 // YV12
601 mpi->planes[1] = current_image->data
602 + current_image->offsets[2];
603 mpi->planes[2] = current_image->data
604 + current_image->offsets[1];
605 mpi->stride[1] = current_image->pitches[2];
606 mpi->stride[2] = current_image->pitches[1];
609 mpi->flags |= MP_IMGFLAG_DIRECT;
610 mpi->priv = (void *) ctx->current_buf;
611 return VO_TRUE;
613 return VO_FALSE;
616 static int query_format(struct xvctx *ctx, uint32_t format)
618 uint32_t i;
619 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
621 /* check image formats */
622 for (i = 0; i < ctx->formats; i++) {
623 if (ctx->fo[i].id == format)
624 return flag; //xv_format = fo[i].id;
626 return 0;
629 static void uninit(struct vo *vo)
631 struct xvctx *ctx = vo->priv;
632 int i;
634 ctx->visible_buf = -1;
635 if (ctx->ai)
636 XvFreeAdaptorInfo(ctx->ai);
637 ctx->ai = NULL;
638 if (ctx->fo) {
639 XFree(ctx->fo);
640 ctx->fo = NULL;
642 for (i = 0; i < ctx->total_buffers; i++)
643 deallocate_xvimage(vo, i);
644 #ifdef CONFIG_XF86VM
645 if (ctx->mode_switched)
646 vo_vm_close(vo);
647 #endif
648 if (ctx->event_fd_registered)
649 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
650 // uninit() shouldn't get called unless initialization went past vo_init()
651 vo_x11_uninit(vo);
654 static int x11_fd_callback(void *ctx, int fd)
656 struct vo *vo = ctx;
657 check_events(vo);
658 return mplayer_get_key(vo->key_fifo, 0);
661 static int preinit(struct vo *vo, const char *arg)
663 XvPortID xv_p;
664 int busy_ports = 0;
665 unsigned int i;
666 strarg_t ck_src_arg = { 0, NULL };
667 strarg_t ck_method_arg = { 0, NULL };
668 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
669 vo->priv = ctx;
670 struct vo_x11_state *x11 = vo->x11;
671 int xv_adaptor = -1;
673 const opt_t subopts[] =
675 /* name arg type arg var test */
676 { "port", OPT_ARG_INT, &x11->xv_port, int_pos },
677 { "adaptor", OPT_ARG_INT, &xv_adaptor, int_non_neg },
678 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
679 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
680 { NULL }
683 x11->xv_port = 0;
685 /* parse suboptions */
686 if (subopt_parse(arg, subopts) != 0) {
687 return -1;
690 /* modify colorkey settings according to the given options */
691 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
693 if (!vo_init(vo))
694 return -1;
696 /* check for Xvideo extension */
697 unsigned int ver, rel, req, ev, err;
698 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
699 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");
700 goto error;
703 /* check for Xvideo support */
704 if (Success !=
705 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
706 &ctx->adaptors, &ctx->ai)) {
707 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] XvQueryAdaptors failed.\n");
708 goto error;
711 /* check adaptors */
712 if (x11->xv_port) {
713 int port_found;
715 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
716 if ((ctx->ai[i].type & XvInputMask)
717 && (ctx->ai[i].type & XvImageMask)) {
718 for (xv_p = ctx->ai[i].base_id;
719 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
720 ++xv_p) {
721 if (xv_p == x11->xv_port) {
722 port_found = 1;
723 break;
728 if (port_found) {
729 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
730 x11->xv_port = 0;
731 } else {
732 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Invalid port parameter, overriding with port 0.\n");
733 x11->xv_port = 0;
737 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
738 /* check if adaptor number has been specified */
739 if (xv_adaptor != -1 && xv_adaptor != i)
740 continue;
742 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
743 for (xv_p = ctx->ai[i].base_id;
744 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
745 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
746 x11->xv_port = xv_p;
747 mp_msg(MSGT_VO, MSGL_V,
748 "[VO_XV] Using Xv Adapter #%d (%s)\n",
749 i, ctx->ai[i].name);
750 break;
751 } else {
752 mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Could not grab port %i.\n",
753 (int) xv_p);
754 ++busy_ports;
758 if (!x11->xv_port) {
759 if (busy_ports)
760 mp_tmsg(MSGT_VO, MSGL_ERR,
761 "[VO_XV] Could not find free Xvideo port - maybe another process is already\n"\
762 "[VO_XV] using it. Close all video applications, and try again. If that does\n"\
763 "[VO_XV] not help, see 'mplayer -vo help' for other (non-xv) video out drivers.\n");
764 else
765 mp_tmsg(MSGT_VO, MSGL_ERR,
766 "[VO_XV] It seems there is no Xvideo support for your video card available.\n"\
767 "[VO_XV] Run 'xvinfo' to verify its Xv support and read\n"\
768 "[VO_XV] DOCS/HTML/en/video.html#xv!\n"\
769 "[VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.\n"\
770 "[VO_XV] Try -vo x11.\n");
771 goto error;
774 if (!vo_xv_init_colorkey(vo)) {
775 goto error; // bail out, colorkey setup failed
777 vo_xv_enable_vsync(vo);
778 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
780 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
781 (int *) &ctx->formats);
783 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
784 x11_fd_callback, NULL, vo);
785 ctx->event_fd_registered = 1;
786 return 0;
788 error:
789 uninit(vo); // free resources
790 return -1;
793 static int control(struct vo *vo, uint32_t request, void *data)
795 struct xvctx *ctx = vo->priv;
796 struct vo_x11_state *x11 = vo->x11;
797 switch (request) {
798 case VOCTRL_PAUSE:
799 return (ctx->is_paused = 1);
800 case VOCTRL_RESUME:
801 return (ctx->is_paused = 0);
802 case VOCTRL_QUERY_FORMAT:
803 return query_format(ctx, *((uint32_t *) data));
804 case VOCTRL_GET_IMAGE:
805 return get_image(ctx, data);
806 case VOCTRL_DRAW_IMAGE:
807 return draw_image(vo, data);
808 case VOCTRL_GET_PANSCAN:
809 return VO_TRUE;
810 case VOCTRL_FULLSCREEN:
811 vo_x11_fullscreen(vo);
812 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
813 case VOCTRL_SET_PANSCAN:
814 resize(vo);
815 return VO_TRUE;
816 case VOCTRL_SET_EQUALIZER:
818 struct voctrl_set_equalizer_args *args = data;
819 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
821 case VOCTRL_GET_EQUALIZER:
823 struct voctrl_get_equalizer_args *args = data;
824 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
826 case VOCTRL_SET_YUV_COLORSPACE:;
827 int given_cspc = *(int *)data % 2;
828 return vo_xv_set_eq(vo, x11->xv_port, "bt_709", given_cspc * 200 - 100);
829 case VOCTRL_GET_YUV_COLORSPACE:;
830 int bt709_enabled;
831 if (!vo_xv_get_eq(vo, x11->xv_port, "bt_709", &bt709_enabled))
832 return false;
833 *(int *)data = bt709_enabled == 100;
834 return true;
835 case VOCTRL_ONTOP:
836 vo_x11_ontop(vo);
837 return VO_TRUE;
838 case VOCTRL_UPDATE_SCREENINFO:
839 update_xinerama_info(vo);
840 return VO_TRUE;
841 case VOCTRL_REDRAW_OSD:
842 return redraw_osd(vo, data);
844 return VO_NOTIMPL;
847 const struct vo_driver video_out_xv = {
848 .is_new = 1,
849 .info = &info,
850 .preinit = preinit,
851 .config = config,
852 .control = control,
853 .draw_slice = draw_slice,
854 .draw_osd = draw_osd,
855 .flip_page = flip_page,
856 .check_events = check_events,
857 .uninit = uninit