audio: add af_lavrresample, remove old resampling filters
[mplayer.git] / libvo / vo_xv.c
blob2300cc6fb57c587992f9cce7b6efe77a13bc12c7
1 /*
2  * X11 Xv interface
3  *
4  * This file is part of MPlayer.
5  *
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.
10  *
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.
15  *
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.
19  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <errno.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
30 #include <libavutil/common.h>
32 #include "config.h"
34 #ifdef HAVE_SHM
35 #include <sys/ipc.h>
36 #include <sys/shm.h>
37 #include <X11/extensions/XShm.h>
38 #endif
40 // Note: depends on the inclusion of X11/extensions/XShm.h
41 #include <X11/extensions/Xv.h>
42 #include <X11/extensions/Xvlib.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"
51 #include "x11_common.h"
52 #include "fastmemcpy.h"
53 #include "sub/sub.h"
54 #include "aspect.h"
55 #include "csputils.h"
56 #include "subopt-helper.h"
58 static const vo_info_t info = {
59     "X11/Xv",
60     "xv",
61     "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
62     ""
65 struct xvctx {
66     XvAdaptorInfo *ai;
67     XvImageFormatValues *fo;
68     unsigned int formats, adaptors, xv_format;
69     int current_buf;
70     int current_ip_buf;
71     int num_buffers;
72     int total_buffers;
73     bool have_image_copy;
74     bool unchanged_image;
75     int visible_buf;
76     XvImage *xvimage[2 + 1];
77     uint32_t image_width;
78     uint32_t image_height;
79     uint32_t image_format;
80     uint32_t image_d_width;
81     uint32_t image_d_height;
82     int is_paused;
83     struct vo_rect src_rect;
84     struct vo_rect dst_rect;
85     uint32_t max_width, max_height; // zero means: not set
86     int mode_switched;
87     int osd_objects_drawn;
88     void (*draw_alpha_fnc)(void *ctx, int x0, int y0, int w, int h,
89                            unsigned char *src, unsigned char *srca,
90                            int stride);
91 #ifdef HAVE_SHM
92     XShmSegmentInfo Shminfo[2 + 1];
93     int Shmem_Flag;
94 #endif
97 static void allocate_xvimage(struct vo *, int);
100 static void fixup_osd_position(struct vo *vo, int *x0, int *y0, int *w, int *h)
102     struct xvctx *ctx = vo->priv;
103     *x0 += ctx->image_width * (vo->panscan_x >> 1)
104                             / (vo->dwidth + vo->panscan_x);
105     *w = av_clip(*w, 0, ctx->image_width);
106     *h = av_clip(*h, 0, ctx->image_height);
107     *x0 = FFMIN(*x0, ctx->image_width  - *w);
108     *y0 = FFMIN(*y0, ctx->image_height - *h);
111 static void draw_alpha_yv12(void *p, int x0, int y0, int w, int h,
112                             unsigned char *src, unsigned char *srca,
113                             int stride)
115     struct vo *vo = p;
116     struct xvctx *ctx = vo->priv;
117     fixup_osd_position(vo, &x0, &y0, &w, &h);
118     vo_draw_alpha_yv12(w, h, src, srca, stride,
119                        ctx->xvimage[ctx->current_buf]->data +
120                        ctx->xvimage[ctx->current_buf]->offsets[0] +
121                        ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + x0,
122                        ctx->xvimage[ctx->current_buf]->pitches[0]);
123     ctx->osd_objects_drawn++;
126 static void draw_alpha_yuy2(void *p, int x0, int y0, int w, int h,
127                             unsigned char *src, unsigned char *srca,
128                             int stride)
130     struct vo *vo = p;
131     struct xvctx *ctx = vo->priv;
132     fixup_osd_position(vo, &x0, &y0, &w, &h);
133     vo_draw_alpha_yuy2(w, h, src, srca, stride,
134                        ctx->xvimage[ctx->current_buf]->data +
135                        ctx->xvimage[ctx->current_buf]->offsets[0] +
136                        ctx->xvimage[ctx->current_buf]->pitches[0] * y0 + 2 * x0,
137                        ctx->xvimage[ctx->current_buf]->pitches[0]);
138     ctx->osd_objects_drawn++;
141 static void draw_alpha_uyvy(void *p, int x0, int y0, int w, int h,
142                             unsigned char *src, unsigned char *srca,
143                             int stride)
145     struct vo *vo = p;
146     struct xvctx *ctx = vo->priv;
147     fixup_osd_position(vo, &x0, &y0, &w, &h);
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 resize(struct vo *vo)
167     struct xvctx *ctx = vo->priv;
169     calc_src_dst_rects(vo, ctx->image_width, ctx->image_height, &ctx->src_rect,
170                        &ctx->dst_rect, NULL, NULL);
171     struct vo_rect *dst = &ctx->dst_rect;
172     vo_x11_clearwindow_part(vo, vo->x11->window, dst->width, dst->height);
173     vo_xv_draw_colorkey(vo, dst->left, dst->top, dst->width, dst->height);
177  * connect to server, create and map window,
178  * allocate colors and (shared) memory
179  */
180 static int config(struct vo *vo, uint32_t width, uint32_t height,
181                   uint32_t d_width, uint32_t d_height, uint32_t flags,
182                   uint32_t format)
184     struct vo_x11_state *x11 = vo->x11;
185     XVisualInfo vinfo;
186     XSetWindowAttributes xswa;
187     XWindowAttributes attribs;
188     unsigned long xswamask;
189     struct xvctx *ctx = vo->priv;
190     int i;
192     ctx->image_height = height;
193     ctx->image_width = width;
194     ctx->image_format = format;
195     ctx->image_d_width = d_width;
196     ctx->image_d_height = d_height;
198     if ((ctx->max_width != 0 && ctx->max_height != 0)
199         && (ctx->image_width > ctx->max_width
200             || ctx->image_height > ctx->max_height)) {
201         mp_tmsg(MSGT_VO, MSGL_ERR, "Source image dimensions are too high: %ux%u (maximum is %ux%u)\n",
202                ctx->image_width, ctx->image_height, ctx->max_width,
203                ctx->max_height);
204         return -1;
205     }
207     ctx->visible_buf = -1;
208     ctx->have_image_copy = false;
210     /* check image formats */
211     ctx->xv_format = 0;
212     for (i = 0; i < ctx->formats; i++) {
213         mp_msg(MSGT_VO, MSGL_V, "Xvideo image format: 0x%x (%4.4s) %s\n",
214                ctx->fo[i].id, (char *) &ctx->fo[i].id,
215                (ctx->fo[i].format == XvPacked) ? "packed" : "planar");
216         if (ctx->fo[i].id == format)
217             ctx->xv_format = ctx->fo[i].id;
218     }
219     if (!ctx->xv_format)
220         return -1;
222     {
223 #ifdef CONFIG_XF86VM
224         int vm = flags & VOFLAG_MODESWITCHING;
225         if (vm) {
226             vo_vm_switch(vo);
227             ctx->mode_switched = 1;
228         }
229 #endif
230         XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
231                              &attribs);
232         XMatchVisualInfo(x11->display, x11->screen, attribs.depth, TrueColor,
233                          &vinfo);
235         xswa.border_pixel = 0;
236         xswamask = CWBorderPixel;
237         if (x11->xv_ck_info.method == CK_METHOD_BACKGROUND) {
238             xswa.background_pixel = x11->xv_colorkey;
239             xswamask |= CWBackPixel;
240         }
242         vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, vo->dwidth,
243                                 vo->dheight, flags, CopyFromParent, "xv");
244         XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
246 #ifdef CONFIG_XF86VM
247         if (vm) {
248             /* Grab the mouse pointer in our window */
249             if (vo_grabpointer)
250                 XGrabPointer(x11->display, x11->window, True, 0, GrabModeAsync,
251                              GrabModeAsync, x11->window, None, CurrentTime);
252             XSetInputFocus(x11->display, x11->window, RevertToNone,
253                            CurrentTime);
254         }
255 #endif
256     }
258     mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
259            x11->xv_port);
261     switch (ctx->xv_format) {
262     case IMGFMT_YV12:
263     case IMGFMT_I420:
264     case IMGFMT_IYUV:
265         ctx->draw_alpha_fnc = draw_alpha_yv12;
266         break;
267     case IMGFMT_YUY2:
268     case IMGFMT_YVYU:
269         ctx->draw_alpha_fnc = draw_alpha_yuy2;
270         break;
271     case IMGFMT_UYVY:
272         ctx->draw_alpha_fnc = draw_alpha_uyvy;
273         break;
274     default:
275         ctx->draw_alpha_fnc = draw_alpha_null;
276     }
278     // In case config has been called before
279     for (i = 0; i < ctx->total_buffers; i++)
280         deallocate_xvimage(vo, i);
282     ctx->num_buffers = 2;
283     ctx->total_buffers = ctx->num_buffers + 1;
285     for (i = 0; i < ctx->total_buffers; i++)
286         allocate_xvimage(vo, i);
288     ctx->current_buf = 0;
289     ctx->current_ip_buf = 0;
292     resize(vo);
294     return 0;
297 static void allocate_xvimage(struct vo *vo, int foo)
299     struct xvctx *ctx = vo->priv;
300     struct vo_x11_state *x11 = vo->x11;
301     /*
302      * allocate XvImages.  FIXME: no error checking, without
303      * mit-shm this will bomb... trzing to fix ::atmos
304      */
305 #ifdef HAVE_SHM
306     if (x11->display_is_local && XShmQueryExtension(x11->display))
307         ctx->Shmem_Flag = 1;
308     else {
309         ctx->Shmem_Flag = 0;
310         mp_tmsg(MSGT_VO, MSGL_INFO, "[VO_XV] Shared memory not supported\nReverting to normal Xv.\n");
311     }
312     if (ctx->Shmem_Flag) {
313         ctx->xvimage[foo] =
314             (XvImage *) XvShmCreateImage(x11->display, x11->xv_port,
315                                          ctx->xv_format, NULL,
316                                          ctx->image_width, ctx->image_height,
317                                          &ctx->Shminfo[foo]);
319         ctx->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
320                                          ctx->xvimage[foo]->data_size,
321                                          IPC_CREAT | 0777);
322         ctx->Shminfo[foo].shmaddr = (char *) shmat(ctx->Shminfo[foo].shmid, 0,
323                                                    0);
324         ctx->Shminfo[foo].readOnly = False;
326         ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
327         XShmAttach(x11->display, &ctx->Shminfo[foo]);
328         XSync(x11->display, False);
329         shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
330     } else
331 #endif
332     {
333         ctx->xvimage[foo] =
334             (XvImage *) XvCreateImage(x11->display, x11->xv_port,
335                                       ctx->xv_format, NULL, ctx->image_width,
336                                       ctx->image_height);
337         ctx->xvimage[foo]->data = malloc(ctx->xvimage[foo]->data_size);
338         XSync(x11->display, False);
339     }
340     memset(ctx->xvimage[foo]->data, 128, ctx->xvimage[foo]->data_size);
341     return;
344 static void deallocate_xvimage(struct vo *vo, int foo)
346     struct xvctx *ctx = vo->priv;
347 #ifdef HAVE_SHM
348     if (ctx->Shmem_Flag) {
349         XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
350         shmdt(ctx->Shminfo[foo].shmaddr);
351     } else
352 #endif
353     {
354         free(ctx->xvimage[foo]->data);
355     }
356     XFree(ctx->xvimage[foo]);
358     XSync(vo->x11->display, False);
359     return;
362 static inline void put_xvimage(struct vo *vo, XvImage *xvi)
364     struct xvctx *ctx = vo->priv;
365     struct vo_x11_state *x11 = vo->x11;
366     struct vo_rect *src = &ctx->src_rect;
367     struct vo_rect *dst = &ctx->dst_rect;
368 #ifdef HAVE_SHM
369     if (ctx->Shmem_Flag) {
370         XvShmPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
371                       src->left, src->top, src->width, src->height,
372                       dst->left, dst->top, dst->width, dst->height,
373                       False);
374     } else
375 #endif
376     {
377         XvPutImage(x11->display, x11->xv_port, x11->window, x11->vo_gc, xvi,
378                    src->left, src->top, src->width, src->height,
379                    dst->left, dst->top, dst->width, dst->height);
380     }
383 // Only copies luma for planar formats as draw_alpha doesn't change others */
384 static void copy_backup_image(struct vo *vo, int dest, int src)
386     struct xvctx *ctx = vo->priv;
388     XvImage *vb = ctx->xvimage[dest];
389     XvImage *cp = ctx->xvimage[src];
390     memcpy_pic(vb->data + vb->offsets[0], cp->data + cp->offsets[0],
391                vb->width, vb->height,
392                vb->pitches[0], cp->pitches[0]);
395 static void check_events(struct vo *vo)
397     int e = vo_x11_check_events(vo);
399     if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) {
400         resize(vo);
401         vo->want_redraw = true;
402     }
405 static void draw_osd(struct vo *vo, struct osd_state *osd)
407     struct xvctx *ctx = vo->priv;
409     ctx->osd_objects_drawn = 0;
410     osd_draw_text(osd,
411                   ctx->image_width -
412                   ctx->image_width * vo->panscan_x / (vo->dwidth +
413                                                       vo->panscan_x),
414                   ctx->image_height, ctx->draw_alpha_fnc, vo);
415     if (ctx->osd_objects_drawn)
416         ctx->unchanged_image = false;
419 static int redraw_frame(struct vo *vo)
421     struct xvctx *ctx = vo->priv;
423     if (ctx->have_image_copy)
424         copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
425     else if (ctx->unchanged_image) {
426         copy_backup_image(vo, ctx->num_buffers, ctx->visible_buf);
427         ctx->have_image_copy = true;
428     }  else
429         return false;
430     ctx->current_buf = ctx->visible_buf;
431     return true;
434 static void flip_page(struct vo *vo)
436     struct xvctx *ctx = vo->priv;
437     put_xvimage(vo, ctx->xvimage[ctx->current_buf]);
439     /* remember the currently visible buffer */
440     ctx->visible_buf = ctx->current_buf;
442     ctx->current_buf = (ctx->current_buf + 1) % ctx->num_buffers;
443     XFlush(vo->x11->display);
444     return;
447 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
448                       int h, int x, int y)
450     struct xvctx *ctx = vo->priv;
451     uint8_t *dst;
452     XvImage *current_image = ctx->xvimage[ctx->current_buf];
454     dst = current_image->data + current_image->offsets[0]
455         + current_image->pitches[0] * y + x;
456     memcpy_pic(dst, image[0], w, h, current_image->pitches[0], stride[0]);
458     x /= 2;
459     y /= 2;
460     w /= 2;
461     h /= 2;
463     dst = current_image->data + current_image->offsets[1]
464         + current_image->pitches[1] * y + x;
465     if (ctx->image_format != IMGFMT_YV12)
466         memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
467     else
468         memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
470     dst = current_image->data + current_image->offsets[2]
471         + current_image->pitches[2] * y + x;
472     if (ctx->image_format == IMGFMT_YV12)
473         memcpy_pic(dst, image[1], w, h, current_image->pitches[1], stride[1]);
474     else
475         memcpy_pic(dst, image[2], w, h, current_image->pitches[1], stride[2]);
477     return 0;
480 static mp_image_t *get_screenshot(struct vo *vo)
482     struct xvctx *ctx = vo->priv;
484     // try to get an image without OSD
485     if (ctx->have_image_copy)
486         copy_backup_image(vo, ctx->visible_buf, ctx->num_buffers);
488     XvImage *xv_image = ctx->xvimage[ctx->visible_buf];
490     int w = xv_image->width;
491     int h = xv_image->height;
493     mp_image_t *image = alloc_mpi(w, h, ctx->image_format);
495     int bytes = 1;
496     if (!(image->flags & MP_IMGFLAG_PLANAR) && (image->flags & MP_IMGFLAG_YUV))
497         // packed YUV
498         bytes = image->bpp / 8;
500     memcpy_pic(image->planes[0], xv_image->data + xv_image->offsets[0],
501                bytes * w, h, image->stride[0], xv_image->pitches[0]);
503     if (image->flags & MP_IMGFLAG_PLANAR) {
504         int swap = ctx->image_format == IMGFMT_YV12;
505         int p1 = swap ? 2 : 1;
506         int p2 = swap ? 1 : 2;
508         w /= 2;
509         h /= 2;
511         memcpy_pic(image->planes[p1], xv_image->data + xv_image->offsets[1],
512                    w, h, image->stride[p1], xv_image->pitches[1]);
513         memcpy_pic(image->planes[p2], xv_image->data + xv_image->offsets[2],
514                    w, h, image->stride[p2], xv_image->pitches[2]);
515     }
517     image->w = ctx->image_d_width;
518     image->h = ctx->image_d_height;
520     return image;
523 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
525     struct xvctx *ctx = vo->priv;
527     ctx->have_image_copy = false;
529     if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
530         ; // done
531     else if (mpi->flags & MP_IMGFLAG_PLANAR)
532         draw_slice(vo, mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
533     else if (mpi->flags & MP_IMGFLAG_YUV)
534         // packed YUV:
535         memcpy_pic(ctx->xvimage[ctx->current_buf]->data +
536                    ctx->xvimage[ctx->current_buf]->offsets[0], mpi->planes[0],
537                    mpi->w * (mpi->bpp / 8), mpi->h,
538                    ctx->xvimage[ctx->current_buf]->pitches[0], mpi->stride[0]);
539     else
540           return false;
542     if (ctx->is_paused) {
543         copy_backup_image(vo, ctx->num_buffers, ctx->current_buf);
544         ctx->have_image_copy = true;
545     }
546     ctx->unchanged_image = true;
547     return true;
550 static int query_format(struct xvctx *ctx, uint32_t format)
552     uint32_t i;
553     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
555     /* check image formats */
556     for (i = 0; i < ctx->formats; i++) {
557         if (ctx->fo[i].id == format)
558             return flag;        //xv_format = fo[i].id;
559     }
560     return 0;
563 static void uninit(struct vo *vo)
565     struct xvctx *ctx = vo->priv;
566     int i;
568     ctx->visible_buf = -1;
569     if (ctx->ai)
570         XvFreeAdaptorInfo(ctx->ai);
571     ctx->ai = NULL;
572     if (ctx->fo) {
573         XFree(ctx->fo);
574         ctx->fo = NULL;
575     }
576     for (i = 0; i < ctx->total_buffers; i++)
577         deallocate_xvimage(vo, i);
578 #ifdef CONFIG_XF86VM
579     if (ctx->mode_switched)
580         vo_vm_close(vo);
581 #endif
582     // uninit() shouldn't get called unless initialization went past vo_init()
583     vo_x11_uninit(vo);
586 static int preinit(struct vo *vo, const char *arg)
588     XvPortID xv_p;
589     int busy_ports = 0;
590     unsigned int i;
591     strarg_t ck_src_arg = { 0, NULL };
592     strarg_t ck_method_arg = { 0, NULL };
593     struct xvctx *ctx = talloc_zero(vo, struct xvctx);
594     vo->priv = ctx;
595     int xv_adaptor = -1;
597     if (!vo_init(vo))
598         return -1;
600     struct vo_x11_state *x11 = vo->x11;
602     const opt_t subopts[] =
603     {
604       /* name         arg type     arg var         test */
605       {  "port",      OPT_ARG_INT, &x11->xv_port,  int_pos },
606       {  "adaptor",   OPT_ARG_INT, &xv_adaptor,    int_non_neg },
607       {  "ck",        OPT_ARG_STR, &ck_src_arg,    xv_test_ck },
608       {  "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
609       {  NULL }
610     };
612     x11->xv_port = 0;
614     /* parse suboptions */
615     if (subopt_parse(arg, subopts) != 0)
616         goto error;
618     /* modify colorkey settings according to the given options */
619     xv_setup_colorkeyhandling(vo, ck_method_arg.str, ck_src_arg.str);
621     /* check for Xvideo extension */
622     unsigned int ver, rel, req, ev, err;
623     if (Success != XvQueryExtension(x11->display, &ver, &rel, &req, &ev, &err)) {
624         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");
625         goto error;
626     }
628     /* check for Xvideo support */
629     if (Success !=
630         XvQueryAdaptors(x11->display, DefaultRootWindow(x11->display),
631                         &ctx->adaptors, &ctx->ai)) {
632         mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_XV] XvQueryAdaptors failed.\n");
633         goto error;
634     }
636     /* check adaptors */
637     if (x11->xv_port) {
638         int port_found;
640         for (port_found = 0, i = 0; !port_found && i < ctx->adaptors; i++) {
641             if ((ctx->ai[i].type & XvInputMask)
642                 && (ctx->ai[i].type & XvImageMask)) {
643                 for (xv_p = ctx->ai[i].base_id;
644                      xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports;
645                      ++xv_p) {
646                     if (xv_p == x11->xv_port) {
647                         port_found = 1;
648                         break;
649                     }
650                 }
651             }
652         }
653         if (port_found) {
654             if (XvGrabPort(x11->display, x11->xv_port, CurrentTime))
655                 x11->xv_port = 0;
656         } else {
657             mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Invalid port parameter, overriding with port 0.\n");
658             x11->xv_port = 0;
659         }
660     }
662     for (i = 0; i < ctx->adaptors && x11->xv_port == 0; i++) {
663         /* check if adaptor number has been specified */
664         if (xv_adaptor != -1 && xv_adaptor != i)
665             continue;
667         if ((ctx->ai[i].type & XvInputMask) && (ctx->ai[i].type & XvImageMask)) {
668             for (xv_p = ctx->ai[i].base_id;
669                  xv_p < ctx->ai[i].base_id + ctx->ai[i].num_ports; ++xv_p)
670                 if (!XvGrabPort(x11->display, xv_p, CurrentTime)) {
671                     x11->xv_port = xv_p;
672                     mp_msg(MSGT_VO, MSGL_V,
673                            "[VO_XV] Using Xv Adapter #%d (%s)\n",
674                            i, ctx->ai[i].name);
675                     break;
676                 } else {
677                     mp_tmsg(MSGT_VO, MSGL_WARN, "[VO_XV] Could not grab port %i.\n",
678                            (int) xv_p);
679                     ++busy_ports;
680                 }
681         }
682     }
683     if (!x11->xv_port) {
684         if (busy_ports)
685             mp_tmsg(MSGT_VO, MSGL_ERR,
686                 "[VO_XV] Could not find free Xvideo port - maybe another process is already\n"\
687                 "[VO_XV] using it. Close all video applications, and try again. If that does\n"\
688                 "[VO_XV] not help, see 'mplayer -vo help' for other (non-xv) video out drivers.\n");
689         else
690             mp_tmsg(MSGT_VO, MSGL_ERR,
691                 "[VO_XV] It seems there is no Xvideo support for your video card available.\n"\
692                 "[VO_XV] Run 'xvinfo' to verify its Xv support and read\n"\
693                 "[VO_XV] DOCS/HTML/en/video.html#xv!\n"\
694                 "[VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.\n"\
695                 "[VO_XV] Try -vo x11.\n");
696         goto error;
697     }
699     if (!vo_xv_init_colorkey(vo)) {
700         goto error;             // bail out, colorkey setup failed
701     }
702     vo_xv_enable_vsync(vo);
703     vo_xv_get_max_img_dim(vo, &ctx->max_width, &ctx->max_height);
705     ctx->fo = XvListImageFormats(x11->display, x11->xv_port,
706                                  (int *) &ctx->formats);
708     return 0;
710   error:
711     uninit(vo);                 // free resources
712     return -1;
715 static int control(struct vo *vo, uint32_t request, void *data)
717     struct xvctx *ctx = vo->priv;
718     struct vo_x11_state *x11 = vo->x11;
719     switch (request) {
720     case VOCTRL_PAUSE:
721         return (ctx->is_paused = 1);
722     case VOCTRL_RESUME:
723         return (ctx->is_paused = 0);
724     case VOCTRL_QUERY_FORMAT:
725         return query_format(ctx, *((uint32_t *) data));
726     case VOCTRL_DRAW_IMAGE:
727         return draw_image(vo, data);
728     case VOCTRL_GET_PANSCAN:
729         return VO_TRUE;
730     case VOCTRL_FULLSCREEN:
731         vo_x11_fullscreen(vo);
732         /* indended, fallthrough to update panscan on fullscreen/windowed switch */
733     case VOCTRL_SET_PANSCAN:
734         resize(vo);
735         return VO_TRUE;
736     case VOCTRL_SET_EQUALIZER: {
737         vo->want_redraw = true;
738         struct voctrl_set_equalizer_args *args = data;
739         return vo_xv_set_eq(vo, x11->xv_port, args->name, args->value);
740     }
741     case VOCTRL_GET_EQUALIZER: {
742         struct voctrl_get_equalizer_args *args = data;
743         return vo_xv_get_eq(vo, x11->xv_port, args->name, args->valueptr);
744     }
745     case VOCTRL_SET_YUV_COLORSPACE:;
746         struct mp_csp_details* given_cspc = data;
747         int is_709 = given_cspc->format == MP_CSP_BT_709;
748         vo_xv_set_eq(vo, x11->xv_port, "bt_709", is_709 * 200 - 100);
749         vo->want_redraw = true;
750         return true;
751     case VOCTRL_GET_YUV_COLORSPACE:;
752         struct mp_csp_details* cspc = data;
753         *cspc = (struct mp_csp_details) MP_CSP_DETAILS_DEFAULTS;
754         int bt709_enabled;
755         if (vo_xv_get_eq(vo, x11->xv_port, "bt_709", &bt709_enabled))
756             cspc->format = bt709_enabled == 100 ? MP_CSP_BT_709 : MP_CSP_BT_601;
757         return true;
758     case VOCTRL_ONTOP:
759         vo_x11_ontop(vo);
760         return VO_TRUE;
761     case VOCTRL_UPDATE_SCREENINFO:
762         update_xinerama_info(vo);
763         return VO_TRUE;
764     case VOCTRL_REDRAW_FRAME:
765         return redraw_frame(vo);
766     case VOCTRL_SCREENSHOT: {
767         struct voctrl_screenshot_args *args = data;
768         args->out_image = get_screenshot(vo);
769         return true;
770     }
771     }
772     return VO_NOTIMPL;
775 const struct vo_driver video_out_xv = {
776     .is_new = 1,
777     .info = &info,
778     .preinit = preinit,
779     .config = config,
780     .control = control,
781     .draw_slice = draw_slice,
782     .draw_osd = draw_osd,
783     .flip_page = flip_page,
784     .check_events = check_events,
785     .uninit = uninit