vo_xv: Always support updating OSD if none was drawn yet
[mplayer/glamo.git] / libvo / vo_xv.c
blob9c4dd391dfbc1dcfa3d8435d65a523832468c557
1 /* vo_xv.c, X11 Xv interface */
3 // Number of buffers _FOR_DOUBLEBUFFERING_MODE_
4 // Use option -double to enable double buffering! (default: single buffer)
5 #define NUM_BUFFERS 3
7 /*
8 Buffer allocation:
10 -nodr:
11 1: TEMP
12 2: 2*TEMP
14 -dr:
15 1: TEMP
16 3: 2*STATIC+TEMP
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <stdbool.h>
25 #include "config.h"
26 #include "options.h"
27 #include "talloc.h"
28 #include "mp_msg.h"
29 #include "help_mp.h"
30 #include "video_out.h"
31 #include "libmpcodecs/vfcap.h"
32 #include "libmpcodecs/mp_image.h"
33 #include "osd.h"
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #include <errno.h>
39 #include "x11_common.h"
41 #include "fastmemcpy.h"
42 #include "sub.h"
43 #include "aspect.h"
45 #include "subopt-helper.h"
47 #include "input/input.h"
48 #include "mp_fifo.h"
50 #ifdef CONFIG_GUI
51 #include "gui/interface.h"
52 #endif
54 #include "libavutil/common.h"
56 static const vo_info_t info = {
57 "X11/Xv",
58 "xv",
59 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
63 #ifdef HAVE_SHM
64 #include <sys/ipc.h>
65 #include <sys/shm.h>
66 #include <X11/extensions/XShm.h>
67 #endif
69 // Note: depends on the inclusion of X11/extensions/XShm.h
70 #include <X11/extensions/Xv.h>
71 #include <X11/extensions/Xvlib.h>
73 struct xvctx {
74 XvAdaptorInfo *ai;
75 XvImageFormatValues *fo;
76 unsigned int formats, adaptors, xv_format;
77 int current_buf;
78 int current_ip_buf;
79 int num_buffers;
80 int total_buffers;
81 int have_visible_image_copy;
82 int have_next_image_copy;
83 int unchanged_visible_image;
84 int unchanged_next_image;
85 int visible_buf;
86 XvImage *xvimage[NUM_BUFFERS + 1];
87 uint32_t image_width;
88 uint32_t image_height;
89 uint32_t image_format;
90 int is_paused;
91 uint32_t drwX, drwY;
92 uint32_t max_width, max_height; // zero means: not set
93 int event_fd_registered; // for uninit called from preinit
94 int mode_switched;
95 int osd_objects_drawn;
96 void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
97 unsigned char *src, unsigned char *srca,
98 int stride);
99 #ifdef HAVE_SHM
100 XShmSegmentInfo Shminfo[NUM_BUFFERS];
101 int Shmem_Flag;
102 #endif
105 static void allocate_xvimage(struct vo *, int);
108 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
109 unsigned char *src, unsigned char *srca,
110 int stride)
112 struct vo *vo = p;
113 struct xvctx *ctx = vo->priv;
114 x0 += ctx->image_width * (vo->panscan_x >> 1)
115 / (vo->dwidth + vo->panscan_x);
116 vo_draw_alpha_yv12(w, h, src, srca, stride,
117 ctx->xvimage[ctx->current_buf]->data +
118 ctx->xvimage[ctx->current_buf]->offsets[0] +
119 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
120 ctx->xvimage[ctx->current_buf]->pitches[0]);
121 ctx->osd_objects_drawn++;
124 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
125 unsigned char *src, unsigned char *srca,
126 int stride)
128 struct vo *vo = p;
129 struct xvctx *ctx = vo->priv;
130 x0 += ctx->image_width * (vo->panscan_x >> 1)
131 / (vo->dwidth + vo->panscan_x);
132 vo_draw_alpha_yuy2(w, h, src, srca, stride,
133 ctx->xvimage[ctx->current_buf]->data +
134 ctx->xvimage[ctx->current_buf]->offsets[0] +
135 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
136 ctx->xvimage[ctx->current_buf]->pitches[0]);
137 ctx->osd_objects_drawn++;
140 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
141 unsigned char *src, unsigned char *srca,
142 int stride)
144 struct vo *vo = p;
145 struct xvctx *ctx = vo->priv;
146 x0 += ctx->image_width * (vo->panscan_x >> 1)
147 / (vo->dwidth + vo->panscan_x);
148 vo_draw_alpha_yuy2(w, h, src, srca, stride,
149 ctx->xvimage[ctx->current_buf]->data +
150 ctx->xvimage[ctx->current_buf]->offsets[0] +
151 ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0 + 1,
152 ctx->xvimage[ctx->current_buf]->pitches[0]);
153 ctx->osd_objects_drawn++;
156 static void draw_alpha_null(void *p, int x0, int y0, int w, int h,
157 unsigned char *src, unsigned char *srca,
158 int stride)
163 static void deallocate_xvimage(struct vo *vo, int foo);
165 static void calc_drwXY(struct vo *vo, uint32_t *drwX, uint32_t *drwY)
167 struct MPOpts *opts = vo->opts;
168 *drwX = *drwY = 0;
169 if (vo_fs) {
170 aspect(vo, &vo->dwidth, &vo->dheight, A_ZOOM);
171 vo->dwidth = FFMIN(vo->dwidth, opts->vo_screenwidth);
172 vo->dheight = FFMIN(vo->dheight, opts->vo_screenheight);
173 *drwX = (opts->vo_screenwidth - vo->dwidth) / 2;
174 *drwY = (opts->vo_screenheight - vo->dheight) / 2;
175 mp_msg(MSGT_VO, MSGL_V, "[xv-fs] dx: %d dy: %d dw: %d dh: %d\n", *drwX,
176 *drwY, vo->dwidth, vo->dheight);
177 } else if (WinID == 0) {
178 *drwX = vo->dx;
179 *drwY = vo->dy;
184 * connect to server, create and map window,
185 * allocate colors and (shared) memory
187 static int config(struct vo *vo, uint32_t width, uint32_t height,
188 uint32_t d_width, uint32_t d_height, uint32_t flags,
189 char *title, uint32_t format)
191 struct MPOpts *opts = vo->opts;
192 struct vo_x11_state *x11 = vo->x11;
193 XVisualInfo vinfo;
194 XSetWindowAttributes xswa;
195 XWindowAttributes attribs;
196 unsigned long xswamask;
197 int depth;
198 struct xvctx *ctx = vo->priv;
199 int i;
201 ctx->image_height = height;
202 ctx->image_width = width;
203 ctx->image_format = format;
205 if ((ctx->max_width != 0 && ctx->max_height != 0)
206 && (ctx->image_width > ctx->max_width
207 || ctx->image_height > ctx->max_height)) {
208 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_VO_XV_ImagedimTooHigh,
209 ctx->image_width, ctx->image_height, ctx->max_width,
210 ctx->max_height);
211 return -1;
214 ctx->is_paused = 0;
215 ctx->visible_buf = -1;
216 ctx->have_visible_image_copy = false;
217 ctx->have_next_image_copy = false;
219 /* check image formats */
220 ctx->xv_format = 0;
221 for (i = 0; i < ctx->formats; i++) {
222 mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
223 ctx->fo[i].id, (char *) &ctx->fo[i].id,
224 (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
225 if (ctx->fo[i].id == format)
226 ctx->xv_format = ctx->fo[i].id;
228 if (!ctx->xv_format)
229 return -1;
231 #ifdef CONFIG_GUI
232 if (use_gui)
233 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
234 else
235 #endif
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;
308 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
309 vo_fs = 1;
310 calc_drwXY(vo, &ctx->drwX, &ctx->drwY);
312 panscan_calc(vo);
314 vo_xv_draw_colorkey(vo, ctx->drwX - (vo->panscan_x >> 1),
315 ctx->drwY - (vo->panscan_y >> 1),
316 vo->dwidth + vo->panscan_x - 1,
317 vo->dheight + vo->panscan_y - 1);
319 mp_msg(MSGT_VO, MSGL_V, "[xv] dx: %d dy: %d dw: %d dh: %d\n", ctx->drwX,
320 ctx->drwY, vo->dwidth, vo->dheight);
322 return 0;
325 static void allocate_xvimage(struct vo *vo, int foo)
327 struct xvctx *ctx = vo->priv;
328 struct vo_x11_state *x11 = vo->x11;
330 * allocate XvImages. FIXME: no error checking, without
331 * mit-shm this will bomb... trzing to fix ::atmos
333 #ifdef HAVE_SHM
334 if (x11->display_is_local && XShmQueryExtension(x11->display))
335 ctx->Shmem_Flag = 1;
336 else {
337 ctx->Shmem_Flag = 0;
338 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_LIBVO_XV_SharedMemoryNotSupported);
340 if (ctx->Shmem_Flag) {
341 ctx->xvimage[foo] =
342 (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
343 ctx->xv_format, NULL,
344 ctx->image_width, ctx->image_height,
345 &ctx->Shminfo[foo]);
347 ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
348 ctx->xvimage[foo]->data_size,
349 IPC_CREAT | 0777);
350 ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
352 ctx->Shminfo[foo].readOnly = False;
354 ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
355 XShmAttach(x11->display, &ctx->Shminfo[foo]);
356 XSync(x11->display, False);
357 shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
358 } else
359 #endif
361 ctx->xvimage[foo] =
362 (XvImage *) XvCreateImage(x11->display, x11->xv_port,
363 ctx->xv_format, NULL, ctx->image_width,
364 ctx->image_height);
365 ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
366 XSync(x11->display, False);
368 memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
369 return;
372 static void deallocate_xvimage(struct vo *vo, int foo)
374 struct xvctx *ctx = vo->priv;
375 #ifdef HAVE_SHM
376 if (ctx->Shmem_Flag) {
377 XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
378 shmdt(ctx->Shminfo[foo].shmaddr);
379 } else
380 #endif
382 free(ctx->xvimage[foo]->data);
384 XFree(ctx->xvimage[foo]);
386 XSync(vo->x11->display, False);
387 return;
390 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
392 struct xvctx *ctx = vo->priv;
393 struct vo_x11_state *x11 = vo->x11;
394 #ifdef HAVE_SHM
395 if (ctx->Shmem_Flag) {
396 XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
397 0, 0, ctx->image_width, ctx->image_height,
398 ctx->drwX - (vo->panscan_x >> 1),
399 ctx->drwY - (vo->panscan_y >> 1),
400 vo->dwidth + vo->panscan_x, vo->dheight + vo->panscan_y,
401 False);
402 } else
403 #endif
405 XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi, 0,
406 0, ctx->image_width, ctx->image_height,
407 ctx->drwX - (vo->panscan_x >> 1),
408 ctx->drwY - (vo->panscan_y >> 1),
409 vo->dwidth + vo->panscan_x, vo->dheight + vo->panscan_y);
413 // Only copies luma for planar formats as draw_alpha doesn't change others */
414 void copy_backup_image(struct vo *vo, int dest, int src)
416 struct xvctx *ctx = vo->priv;
418 XvImage *vb = ctx->xvimage[dest];
419 XvImage *cp = ctx->xvimage[src];
420 memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
421 vb->width, vb->height,
422 vb->pitches[0], cp->pitches[0]);
425 static void check_events(struct vo *vo)
427 struct xvctx *ctx = vo->priv;
428 struct vo_x11_state *x11 = vo->x11;
429 int e = vo_x11_check_events(vo);
431 if (e & VO_EVENT_RESIZE)
432 calc_drwXY(vo, &ctx->drwX, &ctx->drwY);
434 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) {
435 vo_xv_draw_colorkey(vo, ctx->drwX - (vo->panscan_x >> 1),
436 ctx->drwY - (vo->panscan_y >> 1),
437 vo->dwidth + vo->panscan_x - 1,
438 vo->dheight + vo->panscan_y - 1);
441 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && ctx->is_paused) {
442 /* did we already draw a buffer */
443 if (ctx->visible_buf != -1) {
444 /* redraw the last visible buffer */
445 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
450 static void draw_osd(struct vo *vo, struct osd_state *osd)
452 struct xvctx *ctx = vo->priv;
454 ctx->osd_objects_drawn = 0;
455 osd_draw_text(osd,
456 ctx->image_width -
457 ctx->image_width * vo->panscan_x / (vo->dwidth +
458 vo->panscan_x),
459 ctx->image_height, ctx->draw_alpha_fnc, vo);
460 if (ctx->osd_objects_drawn)
461 ctx->unchanged_next_image = false;
464 static int redraw_osd(struct vo *vo, struct osd_state *osd)
466 struct xvctx *ctx = vo->priv;
468 if (ctx->have_visible_image_copy)
469 copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
470 else if (ctx->unchanged_visible_image) {
471 copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
472 ctx->have_visible_image_copy = true;
474 else
475 return false;
476 int temp = ctx->current_buf;
477 ctx->current_buf = ctx->visible_buf;
478 draw_osd(vo, osd);
479 ctx->current_buf = temp;
480 put_xvimage(vo, ctx->xvimage[ctx->visible_buf]);
481 return true;
484 static void flip_page(struct vo *vo)
486 struct xvctx *ctx = vo->priv;
487 put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
489 /* remember the currently visible buffer */
490 ctx->visible_buf = ctx->current_buf;
492 ctx->have_visible_image_copy = ctx->have_next_image_copy;
493 ctx->have_next_image_copy = false;
494 ctx->unchanged_visible_image = ctx->unchanged_next_image;
495 ctx->unchanged_next_image = false;
497 if (ctx->num_buffers > 1) {
498 ctx->current_buf = vo_directrendering ? 0 : ((ctx->current_buf + 1) %
499 ctx->num_buffers);
500 XFlush(vo->x11->display);
501 } else
502 XSync(vo->x11->display, False);
503 return;
506 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
507 int h, int x, int y)
509 struct xvctx *ctx = vo->priv;
510 uint8_t *dst;
511 XvImage *current_image = ctx->xvimage[ctx->current_buf];
513 dst = current_image->data + current_image->offsets[0]
514 + current_image->pitches[0] * y + x;
515 memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
517 x /= 2;
518 y /= 2;
519 w /= 2;
520 h /= 2;
522 dst = current_image->data + current_image->offsets[1]
523 + current_image->pitches[1] * y + x;
524 if (ctx->image_format != IMGFMT_YV12)
525 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
526 else
527 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
529 dst = current_image->data + current_image->offsets[2]
530 + current_image->pitches[2] * y + x;
531 if (ctx->image_format == IMGFMT_YV12)
532 memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
533 else
534 memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
536 return 0;
539 static int draw_frame(struct vo *vo, uint8_t *src[])
541 return VO_ERROR;
544 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
546 struct xvctx *ctx = vo->priv;
548 ctx->have_next_image_copy = false;
550 if (mpi->flags & MP_IMGFLAG_DIRECT)
551 // direct rendering:
552 ctx->current_buf = (int) (mpi->priv); // hack!
553 else if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
554 ; // done
555 else if (mpi->flags & MP_IMGFLAG_PLANAR)
556 draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
557 else if (mpi->flags & MP_IMGFLAG_YUV)
558 // packed YUV:
559 memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
560 ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
561 mpi->w * (mpi->bpp / 8), mpi->h,
562 ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
563 else
564 return false;
566 if (ctx->is_paused) {
567 copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
568 ctx->have_next_image_copy = true;
570 ctx->unchanged_next_image = true;
571 return true;
574 static uint32_t get_image(struct xvctx *ctx, mp_image_t *mpi)
576 // we shouldn't change current_buf unless we do DR!
577 int buf = ctx->current_buf;
579 if (mpi->type == MP_IMGTYPE_STATIC && ctx->num_buffers > 1)
580 return VO_FALSE; // it is not static
581 if (mpi->imgfmt != ctx->image_format)
582 return VO_FALSE; // needs conversion :(
583 if (mpi->flags & MP_IMGFLAG_READABLE
584 && (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP)) {
585 // reference (I/P) frame of IP or IPB:
586 if (ctx->num_buffers < 2)
587 return VO_FALSE; // not enough
588 ctx->current_ip_buf ^= 1;
589 // for IPB with 2 buffers we can DR only one of the 2 P frames:
590 if (mpi->type == MP_IMGTYPE_IPB && ctx->num_buffers < 3
591 && ctx->current_ip_buf)
592 return VO_FALSE;
593 buf = ctx->current_ip_buf;
594 if (mpi->type == MP_IMGTYPE_IPB)
595 ++buf; // preserve space for B
597 if (mpi->height > ctx->xvimage[buf]->height)
598 return VO_FALSE; //buffer to small
599 if (mpi->width * (mpi->bpp / 8) > ctx->xvimage[buf]->pitches[0])
600 return VO_FALSE; //buffer to small
601 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
602 || (mpi->width * (mpi->bpp / 8) == ctx->xvimage[buf]->pitches[0])) {
603 ctx->current_buf = buf;
604 XvImage *current_image = ctx->xvimage[ctx->current_buf];
605 mpi->planes[0] = current_image->data + current_image->offsets[0];
606 mpi->stride[0] = current_image->pitches[0];
607 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
608 if (mpi->flags & MP_IMGFLAG_PLANAR) {
609 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
610 // I420
611 mpi->planes[1] = current_image->data
612 + current_image->offsets[1];
613 mpi->planes[2] = current_image->data
614 + current_image->offsets[2];
615 mpi->stride[1] = current_image->pitches[1];
616 mpi->stride[2] = current_image->pitches[2];
617 } else {
618 // YV12
619 mpi->planes[1] = current_image->data
620 + current_image->offsets[2];
621 mpi->planes[2] = current_image->data
622 + current_image->offsets[1];
623 mpi->stride[1] = current_image->pitches[2];
624 mpi->stride[2] = current_image->pitches[1];
627 mpi->flags |= MP_IMGFLAG_DIRECT;
628 mpi->priv = (void *) ctx->current_buf;
629 return VO_TRUE;
631 return VO_FALSE;
634 static int query_format(struct xvctx *ctx, uint32_t format)
636 uint32_t i;
637 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
639 /* check image formats */
640 for (i = 0; i < ctx->formats; i++) {
641 if (ctx->fo[i].id == format)
642 return flag; //xv_format = fo[i].id;
644 return 0;
647 static void uninit(struct vo *vo)
649 struct xvctx *ctx = vo->priv;
650 int i;
652 ctx->visible_buf = -1;
653 if (ctx->ai)
654 XvFreeAdaptorInfo(ctx->ai);
655 ctx->ai = NULL;
656 if (ctx->fo) {
657 XFree(ctx->fo);
658 ctx->fo = NULL;
660 for (i = 0; i < ctx->total_buffers; i++)
661 deallocate_xvimage(vo, i);
662 #ifdef CONFIG_XF86VM
663 if (ctx->mode_switched)
664 vo_vm_close(vo);
665 #endif
666 if (ctx->event_fd_registered)
667 mp_input_rm_key_fd(vo->input_ctx, ConnectionNumber(vo->x11->display));
668 // uninit() shouldn't get called unless initialization went past vo_init()
669 vo_x11_uninit(vo);
672 static int x11_fd_callback(void *ctx, int fd)
674 struct vo *vo = ctx;
675 check_events(vo);
676 return mplayer_get_key(vo->key_fifo, 0);
679 static int preinit(struct vo *vo, const char *arg)
681 XvPortID xv_p;
682 int busy_ports = 0;
683 unsigned int i;
684 strarg_t ck_src_arg = { 0, NULL };
685 strarg_t ck_method_arg = { 0, NULL };
686 struct xvctx *ctx = talloc_zero(vo, struct xvctx);
687 vo->priv = ctx;
688 struct vo_x11_state *x11 = vo->x11;
689 int xv_adaptor = -1;
691 opt_t subopts[] = {
692 /* name arg type arg var test */
693 {"port", OPT_ARG_INT, &x11->xv_port, (opt_test_f) int_pos},
694 {"adaptor", OPT_ARG_INT, &xv_adaptor, (opt_test_f) int_non_neg},
695 {"ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck},
696 {"ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm},
697 {NULL}
700 x11->xv_port = 0;
702 /* parse suboptions */
703 if (subopt_parse(arg, subopts) != 0) {
704 return -1;
707 /* modify colorkey settings according to the given options */
708 xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
710 if (!vo_init(vo))
711 return -1;
713 /* check for Xvideo extension */
714 unsigned int ver, rel, req, ev, err;
715 if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
716 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvNotSupportedByX11);
717 goto error;
720 /* check for Xvideo support */
721 if (Success !=
722 XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
723 &ctx->adaptors, &ctx->ai)) {
724 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed);
725 goto error;
728 /* check adaptors */
729 if (x11->xv_port) {
730 int port_found;
732 for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
733 if ((ctx->ai[i].type & XvInputMask)
734 && (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;
737 ++xv_p) {
738 if (xv_p == x11->xv_port) {
739 port_found = 1;
740 break;
745 if (port_found) {
746 if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
747 x11->xv_port = 0;
748 } else {
749 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_InvalidPortParameter);
750 x11->xv_port = 0;
754 for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
755 /* check if adaptor number has been specified */
756 if (xv_adaptor != -1 && xv_adaptor != i)
757 continue;
759 if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
760 for (xv_p = ctx->ai[i].base_id;
761 xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
762 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
763 x11->xv_port = xv_p;
764 mp_msg(MSGT_VO, MSGL_V,
765 "[VO_XV] Using Xv Adapter #%d (%s)\n",
766 i, ctx->ai[i].name);
767 break;
768 } else {
769 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_LIBVO_XV_CouldNotGrabPort,
770 (int) xv_p);
771 ++busy_ports;
775 if (!x11->xv_port) {
776 if (busy_ports)
777 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_CouldNotFindFreePort);
778 else
779 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_NoXvideoSupport);
780 goto error;
783 if (!vo_xv_init_colorkey(vo)) {
784 goto error; // bail out, colorkey setup failed
786 vo_xv_enable_vsync(vo);
787 vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
789 ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
790 (int *) &ctx->formats);
792 mp_input_add_key_fd(vo->input_ctx, ConnectionNumber(x11->display), 1,
793 x11_fd_callback, NULL, vo);
794 ctx->event_fd_registered = 1;
795 return 0;
797 error:
798 uninit(vo); // free resources
799 return -1;
802 static int control(struct vo *vo, uint32_t request, void *data)
804 struct xvctx *ctx = vo->priv;
805 struct vo_x11_state *x11 = vo->x11;
806 switch (request) {
807 case VOCTRL_PAUSE:
808 return (ctx->is_paused = 1);
809 case VOCTRL_RESUME:
810 return (ctx->is_paused = 0);
811 case VOCTRL_QUERY_FORMAT:
812 return query_format(ctx, *((uint32_t *) data));
813 case VOCTRL_GET_IMAGE:
814 return get_image(ctx, data);
815 case VOCTRL_DRAW_IMAGE:
816 return draw_image(vo, data);
817 case VOCTRL_GUISUPPORT:
818 return VO_TRUE;
819 case VOCTRL_GET_PANSCAN:
820 if (!vo->config_ok || !vo_fs)
821 return VO_FALSE;
822 return VO_TRUE;
823 case VOCTRL_FULLSCREEN:
824 vo_x11_fullscreen(vo);
825 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
826 case VOCTRL_SET_PANSCAN:
827 if ((vo_fs && (vo_panscan != vo->panscan_amount))
828 || (!vo_fs && vo->panscan_amount)) {
829 int old_y = vo->panscan_y;
831 panscan_calc(vo);
833 if (old_y != vo->panscan_y) {
834 vo_x11_clearwindow_part(vo, x11->window,
835 vo->dwidth + vo->panscan_x - 1,
836 vo->dheight + vo->panscan_y - 1, 1);
837 vo_xv_draw_colorkey(vo, ctx->drwX - (vo->panscan_x >> 1),
838 ctx->drwY - (vo->panscan_y >> 1),
839 vo->dwidth + vo->panscan_x - 1,
840 vo->dheight + vo->panscan_y - 1);
841 flip_page(vo);
844 return VO_TRUE;
845 case VOCTRL_SET_EQUALIZER:
847 struct voctrl_set_equalizer_args *args = data;
848 return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
850 case VOCTRL_GET_EQUALIZER:
852 struct voctrl_get_equalizer_args *args = data;
853 return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
855 case VOCTRL_ONTOP:
856 vo_x11_ontop(vo);
857 return VO_TRUE;
858 case VOCTRL_UPDATE_SCREENINFO:
859 update_xinerama_info(vo);
860 return VO_TRUE;
861 case VOCTRL_REDRAW_OSD:
862 return redraw_osd(vo, data);
864 return VO_NOTIMPL;
867 const struct vo_driver video_out_xv = {
868 .is_new = 1,
869 .info = &info,
870 .preinit = preinit,
871 .config = config,
872 .control = control,
873 .draw_frame = draw_frame,
874 .draw_slice = draw_slice,
875 .draw_osd = draw_osd,
876 .flip_page = flip_page,
877 .check_events = check_events,
878 .uninit = uninit