Remove unused static variable pass, fixes the warning:
[mplayer/greg.git] / libvo / vo_xv.c
blob2a3898889c9b6c4df6defc4e45934b6c798e9779
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>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "help_mp.h"
26 #include "video_out.h"
27 #include "video_out_internal.h"
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <errno.h>
34 #include "x11_common.h"
36 #include "fastmemcpy.h"
37 #include "sub.h"
38 #include "aspect.h"
40 #include "subopt-helper.h"
42 #include "input/input.h"
44 #ifdef HAVE_NEW_GUI
45 #include "gui/interface.h"
46 #endif
48 #include "libavutil/common.h"
50 static const vo_info_t info = {
51 "X11/Xv",
52 "xv",
53 "Gerd Knorr <kraxel@goldbach.in-berlin.de> and others",
57 const LIBVO_EXTERN(xv)
58 #ifdef HAVE_SHM
59 #include <sys/ipc.h>
60 #include <sys/shm.h>
61 #include <X11/extensions/XShm.h>
63 static XShmSegmentInfo Shminfo[NUM_BUFFERS];
64 static int Shmem_Flag;
65 #endif
67 // Note: depends on the inclusion of X11/extensions/XShm.h
68 #include <X11/extensions/Xv.h>
69 #include <X11/extensions/Xvlib.h>
71 // FIXME: dynamically allocate this stuff
72 static void allocate_xvimage(int);
73 static unsigned int ver, rel, req, ev, err;
74 static unsigned int formats, adaptors, xv_format;
75 static XvAdaptorInfo *ai = NULL;
76 static XvImageFormatValues *fo=NULL;
78 static int current_buf = 0;
79 static int current_ip_buf = 0;
80 static int num_buffers = 1; // default
81 static int visible_buf = -1; // -1 means: no buffer was drawn yet
82 static XvImage *xvimage[NUM_BUFFERS];
85 static uint32_t image_width;
86 static uint32_t image_height;
87 static uint32_t image_format;
88 static int flip_flag;
90 static int int_pause;
92 static Window mRoot;
93 static uint32_t drwX, drwY, drwBorderWidth, drwDepth;
94 static uint32_t max_width = 0, max_height = 0; // zero means: not set
96 static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
97 unsigned char *src, unsigned char *srca,
98 int stride);
100 static void draw_alpha_yv12(int x0, int y0, int w, int h,
101 unsigned char *src, unsigned char *srca,
102 int stride)
104 x0 += image_width * (vo_panscan_x >> 1) / (vo_dwidth + vo_panscan_x);
105 vo_draw_alpha_yv12(w, h, src, srca, stride,
106 xvimage[current_buf]->data +
107 xvimage[current_buf]->offsets[0] +
108 xvimage[current_buf]->pitches[0] * y0 + x0,
109 xvimage[current_buf]->pitches[0]);
112 static void draw_alpha_yuy2(int x0, int y0, int w, int h,
113 unsigned char *src, unsigned char *srca,
114 int stride)
116 x0 += image_width * (vo_panscan_x >> 1) / (vo_dwidth + vo_panscan_x);
117 vo_draw_alpha_yuy2(w, h, src, srca, stride,
118 xvimage[current_buf]->data +
119 xvimage[current_buf]->offsets[0] +
120 xvimage[current_buf]->pitches[0] * y0 + 2 * x0,
121 xvimage[current_buf]->pitches[0]);
124 static void draw_alpha_uyvy(int x0, int y0, int w, int h,
125 unsigned char *src, unsigned char *srca,
126 int stride)
128 x0 += image_width * (vo_panscan_x >> 1) / (vo_dwidth + vo_panscan_x);
129 vo_draw_alpha_yuy2(w, h, src, srca, stride,
130 xvimage[current_buf]->data +
131 xvimage[current_buf]->offsets[0] +
132 xvimage[current_buf]->pitches[0] * y0 + 2 * x0 + 1,
133 xvimage[current_buf]->pitches[0]);
136 static void draw_alpha_null(int x0, int y0, int w, int h,
137 unsigned char *src, unsigned char *srca,
138 int stride)
143 static void deallocate_xvimage(int foo);
145 static void calc_drwXY(uint32_t *drwX, uint32_t *drwY) {
146 *drwX = *drwY = 0;
147 if (vo_fs) {
148 aspect(&vo_dwidth, &vo_dheight, A_ZOOM);
149 vo_dwidth = FFMIN(vo_dwidth, vo_screenwidth);
150 vo_dheight = FFMIN(vo_dheight, vo_screenheight);
151 *drwX = (vo_screenwidth - vo_dwidth) / 2;
152 *drwY = (vo_screenheight - vo_dheight) / 2;
153 mp_msg(MSGT_VO, MSGL_V, "[xv-fs] dx: %d dy: %d dw: %d dh: %d\n",
154 *drwX, *drwY, vo_dwidth, vo_dheight);
155 } else if (WinID == 0) {
156 *drwX = vo_dx;
157 *drwY = vo_dy;
162 * connect to server, create and map window,
163 * allocate colors and (shared) memory
165 static int config(uint32_t width, uint32_t height, uint32_t d_width,
166 uint32_t d_height, uint32_t flags, char *title,
167 uint32_t format)
169 XSizeHints hint;
170 XVisualInfo vinfo;
171 XGCValues xgcv;
172 XSetWindowAttributes xswa;
173 XWindowAttributes attribs;
174 unsigned long xswamask;
175 int depth;
177 #ifdef HAVE_XF86VM
178 int vm = 0;
179 unsigned int modeline_width, modeline_height;
180 static uint32_t vm_width;
181 static uint32_t vm_height;
182 #endif
184 image_height = height;
185 image_width = width;
186 image_format = format;
188 if ((max_width != 0 && max_height != 0) &&
189 (image_width > max_width || image_height > max_height))
191 mp_msg( MSGT_VO, MSGL_ERR, MSGTR_VO_XV_ImagedimTooHigh,
192 image_width, image_height, max_width, max_height);
193 return -1;
196 vo_mouse_autohide = 1;
198 int_pause = 0;
199 visible_buf = -1;
201 #ifdef HAVE_XF86VM
202 if (flags & VOFLAG_MODESWITCHING)
203 vm = 1;
204 #endif
205 flip_flag = flags & VOFLAG_FLIPPING;
206 num_buffers =
207 vo_doublebuffering ? (vo_directrendering ? NUM_BUFFERS : 2) : 1;
209 /* check image formats */
211 unsigned int i;
213 xv_format = 0;
214 for (i = 0; i < formats; i++)
216 mp_msg(MSGT_VO, MSGL_V,
217 "Xvideo image format: 0x%x (%4.4s) %s\n", fo[i].id,
218 (char *) &fo[i].id,
219 (fo[i].format == XvPacked) ? "packed" : "planar");
220 if (fo[i].id == format)
221 xv_format = fo[i].id;
223 if (!xv_format)
224 return -1;
227 #ifdef HAVE_NEW_GUI
228 if (use_gui)
229 guiGetEvent(guiSetShVideo, 0); // let the GUI to setup/resize our window
230 else
231 #endif
233 hint.x = vo_dx;
234 hint.y = vo_dy;
235 hint.width = d_width;
236 hint.height = d_height;
237 #ifdef HAVE_XF86VM
238 if (vm)
240 if ((d_width == 0) && (d_height == 0))
242 vm_width = image_width;
243 vm_height = image_height;
244 } else
246 vm_width = d_width;
247 vm_height = d_height;
249 vo_vm_switch(vm_width, vm_height, &modeline_width,
250 &modeline_height);
251 hint.x = (vo_screenwidth - modeline_width) / 2;
252 hint.y = (vo_screenheight - modeline_height) / 2;
253 hint.width = modeline_width;
254 hint.height = modeline_height;
255 aspect_save_screenres(modeline_width, modeline_height);
256 } else
257 #endif
258 hint.flags = PPosition | PSize /* | PBaseSize */ ;
259 hint.base_width = hint.width;
260 hint.base_height = hint.height;
261 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay),
262 &attribs);
263 depth = attribs.depth;
264 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
265 depth = 24;
266 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
268 xswa.background_pixel = 0;
269 if (xv_ck_info.method == CK_METHOD_BACKGROUND)
271 xswa.background_pixel = xv_colorkey;
273 xswa.border_pixel = 0;
274 xswamask = CWBackPixel | CWBorderPixel;
276 if (WinID >= 0)
278 vo_window = WinID ? ((Window) WinID) : mRootWin;
279 if (WinID)
281 XUnmapWindow(mDisplay, vo_window);
282 XChangeWindowAttributes(mDisplay, vo_window, xswamask,
283 &xswa);
284 vo_x11_selectinput_witherr(mDisplay, vo_window,
285 StructureNotifyMask |
286 KeyPressMask |
287 PropertyChangeMask |
288 PointerMotionMask |
289 ButtonPressMask |
290 ButtonReleaseMask |
291 ExposureMask);
292 XMapWindow(mDisplay, vo_window);
293 XGetGeometry(mDisplay, vo_window, &mRoot,
294 &drwX, &drwY, &vo_dwidth, &vo_dheight,
295 &drwBorderWidth, &drwDepth);
296 if (vo_dwidth <= 0) vo_dwidth = d_width;
297 if (vo_dheight <= 0) vo_dheight = d_height;
298 aspect_save_prescale(vo_dwidth, vo_dheight);
300 } else
302 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height,
303 flags, CopyFromParent, "xv", title);
304 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
307 if (vo_gc != None)
308 XFreeGC(mDisplay, vo_gc);
309 vo_gc = XCreateGC(mDisplay, vo_window, 0L, &xgcv);
310 XSync(mDisplay, False);
311 #ifdef HAVE_XF86VM
312 if (vm)
314 /* Grab the mouse pointer in our window */
315 if (vo_grabpointer)
316 XGrabPointer(mDisplay, vo_window, True, 0,
317 GrabModeAsync, GrabModeAsync,
318 vo_window, None, CurrentTime);
319 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
321 #endif
324 mp_msg(MSGT_VO, MSGL_V, "using Xvideo port %d for hw scaling\n",
325 xv_port);
327 switch (xv_format)
329 case IMGFMT_YV12:
330 case IMGFMT_I420:
331 case IMGFMT_IYUV:
332 draw_alpha_fnc = draw_alpha_yv12;
333 break;
334 case IMGFMT_YUY2:
335 case IMGFMT_YVYU:
336 draw_alpha_fnc = draw_alpha_yuy2;
337 break;
338 case IMGFMT_UYVY:
339 draw_alpha_fnc = draw_alpha_uyvy;
340 break;
341 default:
342 draw_alpha_fnc = draw_alpha_null;
345 if (vo_config_count)
346 for (current_buf = 0; current_buf < num_buffers; ++current_buf)
347 deallocate_xvimage(current_buf);
349 for (current_buf = 0; current_buf < num_buffers; ++current_buf)
350 allocate_xvimage(current_buf);
352 current_buf = 0;
353 current_ip_buf = 0;
355 #if 0
356 set_gamma_correction();
357 #endif
359 aspect(&vo_dwidth, &vo_dheight, A_NOZOOM);
360 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0) vo_fs = 1;
361 calc_drwXY(&drwX, &drwY);
363 panscan_calc();
365 vo_xv_draw_colorkey(drwX - (vo_panscan_x >> 1),
366 drwY - (vo_panscan_y >> 1),
367 vo_dwidth + vo_panscan_x - 1,
368 vo_dheight + vo_panscan_y - 1);
371 #if 0
372 #ifdef HAVE_SHM
373 if (Shmem_Flag)
375 XvShmPutImage(mDisplay, xv_port, vo_window, vo_gc,
376 xvimage[current_buf], 0, 0, image_width,
377 image_height, drwX, drwY, 1, 1, False);
378 XvShmPutImage(mDisplay, xv_port, vo_window, vo_gc,
379 xvimage[current_buf], 0, 0, image_width,
380 image_height, drwX, drwY, vo_dwidth,
381 (vo_fs ? vo_dheight - 1 : vo_dheight), False);
382 } else
383 #endif
385 XvPutImage(mDisplay, xv_port, vo_window, vo_gc,
386 xvimage[current_buf], 0, 0, image_width, image_height,
387 drwX, drwY, 1, 1);
388 XvPutImage(mDisplay, xv_port, vo_window, vo_gc,
389 xvimage[current_buf], 0, 0, image_width, image_height,
390 drwX, drwY, vo_dwidth,
391 (vo_fs ? vo_dheight - 1 : vo_dheight));
393 #endif
395 mp_msg(MSGT_VO, MSGL_V, "[xv] dx: %d dy: %d dw: %d dh: %d\n", drwX,
396 drwY, vo_dwidth, vo_dheight);
398 if (vo_ontop)
399 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
401 return 0;
404 static void allocate_xvimage(int foo)
407 * allocate XvImages. FIXME: no error checking, without
408 * mit-shm this will bomb... trzing to fix ::atmos
410 #ifdef HAVE_SHM
411 if (mLocalDisplay && XShmQueryExtension(mDisplay))
412 Shmem_Flag = 1;
413 else
415 Shmem_Flag = 0;
416 mp_msg(MSGT_VO, MSGL_INFO,
417 MSGTR_LIBVO_XV_SharedMemoryNotSupported);
419 if (Shmem_Flag)
421 xvimage[foo] =
422 (XvImage *) XvShmCreateImage(mDisplay, xv_port, xv_format,
423 NULL, image_width, image_height,
424 &Shminfo[foo]);
426 Shminfo[foo].shmid =
427 shmget(IPC_PRIVATE, xvimage[foo]->data_size, IPC_CREAT | 0777);
428 Shminfo[foo].shmaddr = (char *) shmat(Shminfo[foo].shmid, 0, 0);
429 Shminfo[foo].readOnly = False;
431 xvimage[foo]->data = Shminfo[foo].shmaddr;
432 XShmAttach(mDisplay, &Shminfo[foo]);
433 XSync(mDisplay, False);
434 shmctl(Shminfo[foo].shmid, IPC_RMID, 0);
435 } else
436 #endif
438 xvimage[foo] =
439 (XvImage *) XvCreateImage(mDisplay, xv_port, xv_format, NULL,
440 image_width, image_height);
441 xvimage[foo]->data = malloc(xvimage[foo]->data_size);
442 XSync(mDisplay, False);
444 memset(xvimage[foo]->data, 128, xvimage[foo]->data_size);
445 return;
448 static void deallocate_xvimage(int foo)
450 #ifdef HAVE_SHM
451 if (Shmem_Flag)
453 XShmDetach(mDisplay, &Shminfo[foo]);
454 shmdt(Shminfo[foo].shmaddr);
455 } else
456 #endif
458 free(xvimage[foo]->data);
460 XFree(xvimage[foo]);
462 XSync(mDisplay, False);
463 return;
466 static inline void put_xvimage( XvImage * xvi )
468 #ifdef HAVE_SHM
469 if (Shmem_Flag)
471 XvShmPutImage(mDisplay, xv_port, vo_window, vo_gc,
472 xvi, 0, 0, image_width,
473 image_height, drwX - (vo_panscan_x >> 1),
474 drwY - (vo_panscan_y >> 1), vo_dwidth + vo_panscan_x,
475 vo_dheight + vo_panscan_y,
476 False);
477 } else
478 #endif
480 XvPutImage(mDisplay, xv_port, vo_window, vo_gc,
481 xvi, 0, 0, image_width, image_height,
482 drwX - (vo_panscan_x >> 1), drwY - (vo_panscan_y >> 1),
483 vo_dwidth + vo_panscan_x,
484 vo_dheight + vo_panscan_y);
488 static void check_events(void)
490 int e = vo_x11_check_events(mDisplay);
492 if (e & VO_EVENT_RESIZE)
494 XGetGeometry(mDisplay, vo_window, &mRoot, &drwX, &drwY, &vo_dwidth,
495 &vo_dheight, &drwBorderWidth, &drwDepth);
496 mp_msg(MSGT_VO, MSGL_V, "[xv] dx: %d dy: %d dw: %d dh: %d\n", drwX,
497 drwY, vo_dwidth, vo_dheight);
499 calc_drwXY(&drwX, &drwY);
502 if (e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE)
504 vo_xv_draw_colorkey(drwX - (vo_panscan_x >> 1),
505 drwY - (vo_panscan_y >> 1),
506 vo_dwidth + vo_panscan_x - 1,
507 vo_dheight + vo_panscan_y - 1);
510 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && int_pause)
512 /* did we already draw a buffer */
513 if ( visible_buf != -1 )
515 /* redraw the last visible buffer */
516 put_xvimage( xvimage[visible_buf] );
521 static void draw_osd(void)
523 vo_draw_text(image_width -
524 image_width * vo_panscan_x / (vo_dwidth + vo_panscan_x),
525 image_height, draw_alpha_fnc);
528 static void flip_page(void)
530 put_xvimage( xvimage[current_buf] );
532 /* remember the currently visible buffer */
533 visible_buf = current_buf;
535 if (num_buffers > 1)
537 current_buf =
538 vo_directrendering ? 0 : ((current_buf + 1) % num_buffers);
539 XFlush(mDisplay);
540 } else
541 XSync(mDisplay, False);
542 return;
545 static int draw_slice(uint8_t * image[], int stride[], int w, int h,
546 int x, int y)
548 uint8_t *dst;
550 dst = xvimage[current_buf]->data + xvimage[current_buf]->offsets[0] +
551 xvimage[current_buf]->pitches[0] * y + x;
552 memcpy_pic(dst, image[0], w, h, xvimage[current_buf]->pitches[0],
553 stride[0]);
555 x /= 2;
556 y /= 2;
557 w /= 2;
558 h /= 2;
560 dst = xvimage[current_buf]->data + xvimage[current_buf]->offsets[1] +
561 xvimage[current_buf]->pitches[1] * y + x;
562 if (image_format != IMGFMT_YV12)
563 memcpy_pic(dst, image[1], w, h, xvimage[current_buf]->pitches[1],
564 stride[1]);
565 else
566 memcpy_pic(dst, image[2], w, h, xvimage[current_buf]->pitches[1],
567 stride[2]);
569 dst = xvimage[current_buf]->data + xvimage[current_buf]->offsets[2] +
570 xvimage[current_buf]->pitches[2] * y + x;
571 if (image_format == IMGFMT_YV12)
572 memcpy_pic(dst, image[1], w, h, xvimage[current_buf]->pitches[1],
573 stride[1]);
574 else
575 memcpy_pic(dst, image[2], w, h, xvimage[current_buf]->pitches[1],
576 stride[2]);
578 return 0;
581 static int draw_frame(uint8_t * src[])
583 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_XV_DrawFrameCalled);
584 return -1;
587 static uint32_t draw_image(mp_image_t * mpi)
589 if (mpi->flags & MP_IMGFLAG_DIRECT)
591 // direct rendering:
592 current_buf = (int) (mpi->priv); // hack!
593 return VO_TRUE;
595 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
596 return VO_TRUE; // done
597 if (mpi->flags & MP_IMGFLAG_PLANAR)
599 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
600 return VO_TRUE;
602 if (mpi->flags & MP_IMGFLAG_YUV)
604 // packed YUV:
605 memcpy_pic(xvimage[current_buf]->data +
606 xvimage[current_buf]->offsets[0], mpi->planes[0],
607 mpi->w * (mpi->bpp / 8), mpi->h,
608 xvimage[current_buf]->pitches[0], mpi->stride[0]);
609 return VO_TRUE;
611 return VO_FALSE; // not (yet) supported
614 static uint32_t get_image(mp_image_t * mpi)
616 int buf = current_buf; // we shouldn't change current_buf unless we do DR!
618 if (mpi->type == MP_IMGTYPE_STATIC && num_buffers > 1)
619 return VO_FALSE; // it is not static
620 if (mpi->imgfmt != image_format)
621 return VO_FALSE; // needs conversion :(
622 // if(mpi->flags&MP_IMGFLAG_READABLE) return VO_FALSE; // slow video ram
623 if (mpi->flags & MP_IMGFLAG_READABLE &&
624 (mpi->type == MP_IMGTYPE_IPB || mpi->type == MP_IMGTYPE_IP))
626 // reference (I/P) frame of IP or IPB:
627 if (num_buffers < 2)
628 return VO_FALSE; // not enough
629 current_ip_buf ^= 1;
630 // for IPB with 2 buffers we can DR only one of the 2 P frames:
631 if (mpi->type == MP_IMGTYPE_IPB && num_buffers < 3
632 && current_ip_buf)
633 return VO_FALSE;
634 buf = current_ip_buf;
635 if (mpi->type == MP_IMGTYPE_IPB)
636 ++buf; // preserve space for B
638 if (mpi->height > xvimage[buf]->height)
639 return VO_FALSE; //buffer to small
640 if (mpi->width * (mpi->bpp / 8) > xvimage[buf]->pitches[0])
641 return VO_FALSE; //buffer to small
642 if ((mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))
643 || (mpi->width * (mpi->bpp / 8) == xvimage[buf]->pitches[0]))
645 current_buf = buf;
646 mpi->planes[0] =
647 xvimage[current_buf]->data + xvimage[current_buf]->offsets[0];
648 mpi->stride[0] = xvimage[current_buf]->pitches[0];
649 mpi->width = mpi->stride[0] / (mpi->bpp / 8);
650 if (mpi->flags & MP_IMGFLAG_PLANAR)
652 if (mpi->flags & MP_IMGFLAG_SWAPPED)
654 // I420
655 mpi->planes[1] =
656 xvimage[current_buf]->data +
657 xvimage[current_buf]->offsets[1];
658 mpi->planes[2] =
659 xvimage[current_buf]->data +
660 xvimage[current_buf]->offsets[2];
661 mpi->stride[1] = xvimage[current_buf]->pitches[1];
662 mpi->stride[2] = xvimage[current_buf]->pitches[2];
663 } else
665 // YV12
666 mpi->planes[1] =
667 xvimage[current_buf]->data +
668 xvimage[current_buf]->offsets[2];
669 mpi->planes[2] =
670 xvimage[current_buf]->data +
671 xvimage[current_buf]->offsets[1];
672 mpi->stride[1] = xvimage[current_buf]->pitches[2];
673 mpi->stride[2] = xvimage[current_buf]->pitches[1];
676 mpi->flags |= MP_IMGFLAG_DIRECT;
677 mpi->priv = (void *) current_buf;
678 // printf("mga: get_image() SUCCESS -> Direct Rendering ENABLED\n");
679 return VO_TRUE;
681 return VO_FALSE;
684 static int query_format(uint32_t format)
686 uint32_t i;
687 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
689 /* check image formats */
690 for (i = 0; i < formats; i++)
692 if (fo[i].id == format)
693 return flag; //xv_format = fo[i].id;
695 return 0;
698 static void uninit(void)
700 int i;
702 if (!vo_config_count)
703 return;
704 visible_buf = -1;
705 XvFreeAdaptorInfo(ai);
706 ai = NULL;
707 if(fo){
708 XFree(fo);
709 fo=NULL;
711 for (i = 0; i < num_buffers; i++)
712 deallocate_xvimage(i);
713 #ifdef HAVE_XF86VM
714 vo_vm_close(mDisplay);
715 #endif
716 mp_input_rm_event_fd(ConnectionNumber(mDisplay));
717 vo_x11_uninit();
720 static int preinit(const char *arg)
722 XvPortID xv_p;
723 int busy_ports = 0;
724 unsigned int i;
725 strarg_t ck_src_arg = { 0, NULL };
726 strarg_t ck_method_arg = { 0, NULL };
728 opt_t subopts[] =
730 /* name arg type arg var test */
731 { "port", OPT_ARG_INT, &xv_port, (opt_test_f)int_pos },
732 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
733 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
734 { NULL }
737 xv_port = 0;
739 /* parse suboptions */
740 if ( subopt_parse( arg, subopts ) != 0 )
742 return -1;
745 /* modify colorkey settings according to the given options */
746 xv_setup_colorkeyhandling( ck_method_arg.str, ck_src_arg.str );
748 if (!vo_init())
749 return -1;
751 /* check for Xvideo extension */
752 if (Success != XvQueryExtension(mDisplay, &ver, &rel, &req, &ev, &err))
754 mp_msg(MSGT_VO, MSGL_ERR,
755 MSGTR_LIBVO_XV_XvNotSupportedByX11);
756 return -1;
759 /* check for Xvideo support */
760 if (Success !=
761 XvQueryAdaptors(mDisplay, DefaultRootWindow(mDisplay), &adaptors,
762 &ai))
764 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_XV_XvQueryAdaptorsFailed);
765 return -1;
768 /* check adaptors */
769 if (xv_port)
771 int port_found;
773 for (port_found = 0, i = 0; !port_found && i < adaptors; i++)
775 if ((ai[i].type & XvInputMask) && (ai[i].type & XvImageMask))
777 for (xv_p = ai[i].base_id;
778 xv_p < ai[i].base_id + ai[i].num_ports; ++xv_p)
780 if (xv_p == xv_port)
782 port_found = 1;
783 break;
788 if (port_found)
790 if (XvGrabPort(mDisplay, xv_port, CurrentTime))
791 xv_port = 0;
792 } else
794 mp_msg(MSGT_VO, MSGL_WARN,
795 MSGTR_LIBVO_XV_InvalidPortParameter);
796 xv_port = 0;
800 for (i = 0; i < adaptors && xv_port == 0; i++)
802 if ((ai[i].type & XvInputMask) && (ai[i].type & XvImageMask))
804 for (xv_p = ai[i].base_id;
805 xv_p < ai[i].base_id + ai[i].num_ports; ++xv_p)
806 if (!XvGrabPort(mDisplay, xv_p, CurrentTime))
808 xv_port = xv_p;
809 break;
810 } else
812 mp_msg(MSGT_VO, MSGL_WARN,
813 MSGTR_LIBVO_XV_CouldNotGrabPort, (int) xv_p);
814 ++busy_ports;
818 if (!xv_port)
820 if (busy_ports)
821 mp_msg(MSGT_VO, MSGL_ERR,
822 MSGTR_LIBVO_XV_CouldNotFindFreePort);
823 else
824 mp_msg(MSGT_VO, MSGL_ERR,
825 MSGTR_LIBVO_XV_NoXvideoSupport);
826 return -1;
829 if ( !vo_xv_init_colorkey() )
831 return -1; // bail out, colorkey setup failed
833 vo_xv_enable_vsync();
834 vo_xv_get_max_img_dim( &max_width, &max_height );
836 fo = XvListImageFormats(mDisplay, xv_port, (int *) &formats);
838 mp_input_add_event_fd(ConnectionNumber(mDisplay), check_events);
839 return 0;
842 static int control(uint32_t request, void *data, ...)
844 switch (request)
846 case VOCTRL_PAUSE:
847 return (int_pause = 1);
848 case VOCTRL_RESUME:
849 return (int_pause = 0);
850 case VOCTRL_QUERY_FORMAT:
851 return query_format(*((uint32_t *) data));
852 case VOCTRL_GET_IMAGE:
853 return get_image(data);
854 case VOCTRL_DRAW_IMAGE:
855 return draw_image(data);
856 case VOCTRL_GUISUPPORT:
857 return VO_TRUE;
858 case VOCTRL_GET_PANSCAN:
859 if (!vo_config_count || !vo_fs)
860 return VO_FALSE;
861 return VO_TRUE;
862 case VOCTRL_FULLSCREEN:
863 vo_x11_fullscreen();
864 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
865 case VOCTRL_SET_PANSCAN:
866 if ((vo_fs && (vo_panscan != vo_panscan_amount))
867 || (!vo_fs && vo_panscan_amount))
869 int old_y = vo_panscan_y;
871 panscan_calc();
873 if (old_y != vo_panscan_y)
875 vo_x11_clearwindow_part(mDisplay, vo_window,
876 vo_dwidth + vo_panscan_x - 1,
877 vo_dheight + vo_panscan_y - 1,
879 vo_xv_draw_colorkey(drwX - (vo_panscan_x >> 1),
880 drwY - (vo_panscan_y >> 1),
881 vo_dwidth + vo_panscan_x - 1,
882 vo_dheight + vo_panscan_y - 1);
883 flip_page();
886 return VO_TRUE;
887 case VOCTRL_SET_EQUALIZER:
889 va_list ap;
890 int value;
892 va_start(ap, data);
893 value = va_arg(ap, int);
895 va_end(ap);
897 return (vo_xv_set_eq(xv_port, data, value));
899 case VOCTRL_GET_EQUALIZER:
901 va_list ap;
902 int *value;
904 va_start(ap, data);
905 value = va_arg(ap, int *);
907 va_end(ap);
909 return (vo_xv_get_eq(xv_port, data, value));
911 case VOCTRL_ONTOP:
912 vo_x11_ontop();
913 return VO_TRUE;
914 case VOCTRL_UPDATE_SCREENINFO:
915 update_xinerama_info();
916 return VO_TRUE;
918 return VO_NOTIMPL;