1
|
1 #define DISP
|
|
2
|
|
3 /*
|
|
4 * video_out_x11.c, X11 interface
|
|
5 *
|
|
6 *
|
|
7 * Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved.
|
|
8 *
|
|
9 * Hacked into mpeg2dec by
|
|
10 *
|
|
11 * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
|
12 *
|
|
13 * 15 & 16 bpp support added by Franck Sicard <Franck.Sicard@solsoft.fr>
|
|
14 *
|
|
15 * Xv image suuport by Gerd Knorr <kraxel@goldbach.in-berlin.de>
|
|
16 */
|
|
17
|
|
18 #include <stdio.h>
|
|
19 #include <stdlib.h>
|
|
20 #include <string.h>
|
|
21
|
|
22 #include "config.h"
|
|
23 #include "video_out.h"
|
|
24 #include "video_out_internal.h"
|
|
25
|
|
26 LIBVO_EXTERN(xv)
|
|
27
|
|
28 #include <X11/Xlib.h>
|
|
29 #include <X11/Xutil.h>
|
|
30 #include <X11/extensions/XShm.h>
|
|
31 #include <errno.h>
|
|
32 #include "yuv2rgb.h"
|
|
33
|
|
34 static vo_info_t vo_info =
|
|
35 {
|
|
36 "X11/Xv",
|
|
37 "xv",
|
|
38 "Gerd Knorr <kraxel@goldbach.in-berlin.de>",
|
|
39 ""
|
|
40 };
|
|
41
|
|
42 /* since it doesn't seem to be defined on some platforms */
|
|
43 int XShmGetEventBase(Display*);
|
|
44
|
|
45 /* local data */
|
|
46 static unsigned char *ImageData;
|
|
47
|
|
48 /* X11 related variables */
|
|
49 static Display *mydisplay;
|
|
50 static Window mywindow;
|
|
51 static GC mygc;
|
|
52 static XImage *myximage;
|
|
53 static int depth, bpp, mode;
|
|
54 static XWindowAttributes attribs;
|
|
55
|
|
56 #include <X11/extensions/Xv.h>
|
|
57 #include <X11/extensions/Xvlib.h>
|
|
58 // FIXME: dynamically allocate this stuff
|
|
59 static void allocate_xvimage(int);
|
|
60 static unsigned int ver,rel,req,ev,err;
|
|
61 static unsigned int formats, adaptors,i,xv_port,xv_format;
|
|
62 static int win_width,win_height;
|
|
63 static XvAdaptorInfo *ai;
|
|
64 static XvImageFormatValues *fo;
|
|
65 static XvImage *xvimage[1];
|
|
66
|
|
67 #include <sys/ipc.h>
|
|
68 #include <sys/shm.h>
|
|
69 #include <X11/extensions/XShm.h>
|
|
70
|
|
71 static int Shmem_Flag;
|
|
72 static int Quiet_Flag;
|
|
73 static XShmSegmentInfo Shminfo[1];
|
|
74 static int gXErrorFlag;
|
|
75 static int CompletionType = -1;
|
|
76
|
|
77 static uint32_t image_width;
|
|
78 static uint32_t image_height;
|
|
79 static uint32_t image_format;
|
|
80
|
|
81 static int get_depth(){
|
|
82 char *name = ":0.0";
|
|
83 if(getenv("DISPLAY")) name = getenv("DISPLAY");
|
|
84 mydisplay = XOpenDisplay(name);
|
|
85 if (mydisplay == NULL) return 0;
|
|
86 XGetWindowAttributes(mydisplay, DefaultRootWindow(mydisplay), &attribs);
|
|
87 depth = attribs.depth;
|
|
88 XCloseDisplay(mydisplay);
|
|
89
|
|
90 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) depth = 24;
|
|
91 printf("X11 Display color depth = %d\n",depth);
|
|
92 return depth;
|
|
93 }
|
|
94
|
|
95 extern void vo_decoration( Display * vo_Display,Window w,int d );
|
|
96
|
|
97 /* connect to server, create and map window,
|
|
98 * allocate colors and (shared) memory
|
|
99 */
|
|
100 static uint32_t
|
|
101 init(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t fullscreen, char *title, uint32_t format)
|
|
102 {
|
|
103 int screen;
|
|
104 // unsigned int fg, bg;
|
|
105 char *hello = (title == NULL) ? "Xv rulez" : title;
|
|
106 char *name = ":0.0";
|
|
107 XSizeHints hint;
|
|
108 XVisualInfo vinfo;
|
|
109 XEvent xev;
|
|
110
|
|
111 XGCValues xgcv;
|
|
112 Colormap theCmap;
|
|
113 XSetWindowAttributes xswa;
|
|
114 unsigned long xswamask;
|
|
115
|
|
116 image_height = height;
|
|
117 image_width = width;
|
|
118 image_format=format;
|
|
119
|
|
120 if(getenv("DISPLAY"))
|
|
121 name = getenv("DISPLAY");
|
|
122
|
|
123 mydisplay = XOpenDisplay(name);
|
|
124
|
|
125 if (mydisplay == NULL)
|
|
126 {
|
|
127 fprintf(stderr,"Can't open display\n");
|
|
128 return -1;
|
|
129 }
|
|
130
|
|
131 screen = DefaultScreen(mydisplay);
|
|
132
|
|
133 hint.x = 0;
|
|
134 hint.y = 0;
|
|
135 hint.width = image_width;
|
|
136 hint.height = image_height;
|
|
137 hint.flags = PPosition | PSize;
|
|
138
|
|
139 /* Get some colors */
|
|
140
|
|
141 // bg = WhitePixel(mydisplay, screen);
|
|
142 // fg = BlackPixel(mydisplay, screen);
|
|
143
|
|
144 /* Make the window */
|
|
145
|
|
146 XGetWindowAttributes(mydisplay, DefaultRootWindow(mydisplay), &attribs);
|
|
147
|
|
148 /*
|
|
149 *
|
|
150 * depth in X11 terminology land is the number of bits used to
|
|
151 * actually represent the colour.
|
|
152 *
|
|
153 * bpp in X11 land means how many bits in the frame buffer per
|
|
154 * pixel.
|
|
155 *
|
|
156 * ex. 15 bit color is 15 bit depth and 16 bpp. Also 24 bit
|
|
157 * color is 24 bit depth, but can be 24 bpp or 32 bpp.
|
|
158 */
|
|
159
|
|
160 depth = attribs.depth;
|
|
161
|
|
162 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
|
|
163 {
|
|
164 /* The root window may be 8bit but there might still be
|
|
165 * visuals with other bit depths. For example this is the
|
|
166 * case on Sun/Solaris machines.
|
|
167 */
|
|
168 depth = 24;
|
|
169 }
|
|
170 //BEGIN HACK
|
|
171 //mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay),
|
|
172 //hint.x, hint.y, hint.width, hint.height, 4, fg, bg);
|
|
173 //
|
|
174 XMatchVisualInfo(mydisplay, screen, depth, TrueColor, &vinfo);
|
|
175
|
|
176 theCmap = XCreateColormap(mydisplay, RootWindow(mydisplay,screen),
|
|
177 vinfo.visual, AllocNone);
|
|
178
|
|
179 xswa.background_pixel = 0;
|
|
180 xswa.border_pixel = 1;
|
|
181 xswa.colormap = theCmap;
|
|
182 xswamask = CWBackPixel | CWBorderPixel |CWColormap;
|
|
183
|
|
184
|
|
185 mywindow = XCreateWindow(mydisplay, RootWindow(mydisplay,screen),
|
|
186 hint.x, hint.y, hint.width, hint.height, 4, depth,CopyFromParent,vinfo.visual,xswamask,&xswa);
|
|
187
|
|
188 XSelectInput(mydisplay, mywindow, StructureNotifyMask);
|
|
189
|
|
190 /* Tell other applications about this window */
|
|
191
|
|
192 XSetStandardProperties(mydisplay, mywindow, hello, hello, None, NULL, 0, &hint);
|
|
193
|
|
194 if ( fullscreen ) vo_decoration( mydisplay,mywindow,0 );
|
|
195
|
|
196 /* Map window. */
|
|
197
|
|
198 XMapWindow(mydisplay, mywindow);
|
|
199
|
|
200 /* Wait for map. */
|
|
201 do
|
|
202 {
|
|
203 XNextEvent(mydisplay, &xev);
|
|
204 }
|
|
205 while (xev.type != MapNotify || xev.xmap.event != mywindow);
|
|
206
|
|
207 XSelectInput(mydisplay, mywindow, NoEventMask);
|
|
208
|
|
209 XFlush(mydisplay);
|
|
210 XSync(mydisplay, False);
|
|
211
|
|
212 mygc = XCreateGC(mydisplay, mywindow, 0L, &xgcv);
|
|
213
|
|
214 xv_port = 0;
|
|
215 if (Success == XvQueryExtension(mydisplay,&ver,&rel,&req,&ev,&err))
|
|
216 {
|
|
217 /* check for Xvideo support */
|
|
218 if (Success != XvQueryAdaptors(mydisplay,DefaultRootWindow(mydisplay), &adaptors,&ai))
|
|
219 {
|
|
220 fprintf(stderr,"Xv: XvQueryAdaptors failed");
|
|
221 return -1;
|
|
222 }
|
|
223 /* check adaptors */
|
|
224 for (i = 0; i < adaptors; i++)
|
|
225 {
|
|
226 if ((ai[i].type & XvInputMask) && (ai[i].type & XvImageMask) && (xv_port == 0))
|
|
227 xv_port = ai[i].base_id;
|
|
228 }
|
|
229 /* check image formats */
|
|
230 if (xv_port != 0)
|
|
231 {
|
|
232 fo = XvListImageFormats(mydisplay, xv_port, (int*)&formats);
|
|
233 xv_format=0;
|
|
234 for(i = 0; i < formats; i++)
|
|
235 {
|
|
236 fprintf(stderr, "Xvideo image format: 0x%x (%4.4s) %s\n", fo[i].id,
|
|
237 (char*)&fo[i].id, (fo[i].format == XvPacked) ? "packed" : "planar");
|
|
238
|
|
239 if (fo[i].id == format)
|
|
240 {
|
|
241 xv_format = fo[i].id;
|
|
242 // break;
|
|
243 }
|
|
244 }
|
|
245 if (!xv_format) /* no matching image format not */
|
|
246 xv_port = 0;
|
|
247 }
|
|
248
|
|
249 if (xv_port != 0)
|
|
250 {
|
|
251 fprintf(stderr,"using Xvideo port %d for hw scaling\n",
|
|
252 xv_port);
|
|
253
|
|
254 allocate_xvimage(0);
|
|
255
|
|
256 /* catch window resizes */
|
|
257 XSelectInput(mydisplay, mywindow, StructureNotifyMask | KeyPressMask);
|
|
258 // XSelectInput(mydisplay, mywindow, StructureNotifyMask);
|
|
259 win_width = image_width;
|
|
260 win_height = image_height;
|
|
261 // resize:
|
|
262 XMoveResizeWindow(mydisplay,mywindow,0,0,d_width,d_height);
|
|
263
|
|
264 return 0;
|
|
265 }
|
|
266 }
|
|
267
|
|
268 printf("Sorry, Xv not supported by this X11 version/driver\n");
|
|
269 printf("******** Try with -vo x11 or -vo sdl *********\n");
|
|
270 return 1;
|
|
271
|
|
272 }
|
|
273
|
|
274 static const vo_info_t*
|
|
275 get_info(void)
|
|
276 {
|
|
277 return &vo_info;
|
|
278 }
|
|
279
|
|
280 static void
|
|
281 allocate_xvimage(int foo)
|
|
282 {
|
|
283 /* allocate XvImages. FIXME: no error checking, without
|
|
284 * mit-shm this will bomb... */
|
|
285 xvimage[foo] = XvShmCreateImage(mydisplay, xv_port, xv_format, 0, image_width, image_height, &Shminfo[foo]);
|
|
286
|
|
287 Shminfo[foo].shmid = shmget(IPC_PRIVATE, xvimage[foo]->data_size, IPC_CREAT | 0777);
|
|
288 Shminfo[foo].shmaddr = (char *) shmat(Shminfo[foo].shmid, 0, 0);
|
|
289 Shminfo[foo].readOnly = False;
|
|
290
|
|
291 xvimage[foo]->data = Shminfo[foo].shmaddr;
|
|
292 XShmAttach(mydisplay, &Shminfo[foo]);
|
|
293 XSync(mydisplay, False);
|
|
294 shmctl(Shminfo[foo].shmid, IPC_RMID, 0);
|
|
295
|
|
296 /* so we can do grayscale while testing... */
|
|
297 memset(xvimage[foo]->data,128,xvimage[foo]->data_size);
|
|
298
|
|
299 return;
|
|
300 }
|
|
301
|
|
302 #if 0
|
|
303 static void
|
|
304 check_events(void)
|
|
305 {
|
|
306 Window root;
|
|
307 XEvent event;
|
|
308 int x, y;
|
|
309 unsigned int w, h, b, d;
|
|
310
|
|
311 if (XCheckWindowEvent(mydisplay, mywindow, StructureNotifyMask, &event))
|
|
312 {
|
|
313 XGetGeometry(mydisplay, mywindow, &root, &x, &y, &w, &h, &b, &d);
|
|
314 win_width = w;
|
|
315 win_height = h;
|
|
316 }
|
|
317 }
|
|
318 #endif
|
|
319
|
|
320 static void
|
|
321 flip_page(void)
|
|
322 {
|
|
323 int i;
|
|
324 XEvent Event;
|
|
325 char buf[100];
|
|
326 KeySym keySym;
|
|
327 XComposeStatus stat;
|
|
328 unsigned long vo_KeyTable[512];
|
|
329
|
|
330 while ( XPending( mydisplay ) )
|
|
331 {
|
|
332 XNextEvent( mydisplay,&Event );
|
|
333 switch( Event.type )
|
|
334 {
|
|
335 case ConfigureNotify:
|
|
336 win_width = Event.xconfigure.width;
|
|
337 win_height = Event.xconfigure.height;
|
|
338 break;
|
|
339 case KeyPress:
|
|
340 XLookupString( &Event.xkey,buf,sizeof(buf),&keySym,&stat );
|
|
341 vo_keyboard( ( (keySym&0xff00) != 0?( (keySym&0x00ff) + 256 ):( keySym ) ) );
|
|
342 break;
|
|
343 }
|
|
344 }
|
|
345
|
|
346 // check_events();
|
|
347
|
|
348 #ifdef DISP
|
|
349 XvShmPutImage(mydisplay, xv_port, mywindow, mygc, xvimage[0],
|
|
350 0, 0, image_width, image_height,
|
|
351 0, 0, win_width, win_height,
|
|
352 False);
|
|
353 XFlush(mydisplay);
|
|
354 #endif
|
|
355 return;
|
|
356 }
|
|
357
|
|
358 //draw_slice(uint8_t *src[], uint32_t slice_num)
|
|
359 static uint32_t
|
|
360 draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
|
|
361 {
|
|
362 uint8_t *src;
|
|
363 uint8_t *dst;
|
|
364 int i;
|
|
365
|
|
366 dst = xvimage[0]->data + image_width * y + x;
|
|
367 src = image[0];
|
|
368 if(w==stride[0] && w==image_width)
|
|
369 memcpy(dst,src,w*h);
|
|
370 else
|
|
371 for(i=0;i<h;i++){
|
|
372 memcpy(dst,src,w);
|
|
373 src+=stride[0];
|
|
374 dst+=image_width;
|
|
375 }
|
|
376
|
|
377 x/=2;y/=2;w/=2;h/=2;
|
|
378
|
|
379 dst = xvimage[0]->data + image_width * image_height + image_width/2 * y + x;
|
|
380 src = image[2];
|
|
381 if(w==stride[2] && w==image_width/2)
|
|
382 memcpy(dst,src,w*h);
|
|
383 else
|
|
384 for(i=0;i<h;i++){
|
|
385 memcpy(dst,src,w);
|
|
386 src+=stride[2];
|
|
387 dst+=image_width/2;
|
|
388 }
|
|
389
|
|
390 dst = xvimage[0]->data + image_width * image_height * 5 / 4 + image_width/2 * y + x;
|
|
391 src = image[1];
|
|
392 if(w==stride[1] && w==image_width/2)
|
|
393 memcpy(dst,src,w*h);
|
|
394 else
|
|
395 for(i=0;i<h;i++){
|
|
396 memcpy(dst,src,w);
|
|
397 src+=stride[1];
|
|
398 dst+=image_width/2;
|
|
399 }
|
|
400
|
|
401 return 0;
|
|
402 }
|
|
403
|
|
404
|
|
405 static uint32_t
|
|
406 draw_frame(uint8_t *src[])
|
|
407 {
|
|
408 int foo;
|
|
409
|
|
410 // check_events();
|
|
411
|
|
412 if(xv_format==IMGFMT_YUY2){
|
|
413 // YUY2 packed, flipped
|
|
414 #if 0
|
|
415 int i;
|
|
416 unsigned short *s=(unsigned short *)src[0];
|
|
417 unsigned short *d=(unsigned short *)xvimage[0]->data;
|
|
418 s+=image_width*image_height;
|
|
419 for(i=0;i<image_height;i++){
|
|
420 s-=image_width;
|
|
421 memcpy(d,s,image_width*2);
|
|
422 d+=image_width;
|
|
423 }
|
|
424 #else
|
|
425 memcpy(xvimage[0]->data,src[0],image_width*image_height*2);
|
|
426 #endif
|
|
427 } else {
|
|
428 // YV12 planar
|
|
429 memcpy(xvimage[0]->data,src[0],image_width*image_height);
|
|
430 memcpy(xvimage[0]->data+image_width*image_height,src[2],image_width*image_height/4);
|
|
431 memcpy(xvimage[0]->data+image_width*image_height*5/4,src[1],image_width*image_height/4);
|
|
432 }
|
|
433
|
|
434 return 0;
|
|
435 }
|
|
436
|
|
437 static uint32_t
|
|
438 query_format(uint32_t format)
|
|
439 {
|
|
440 switch(format){
|
|
441 case IMGFMT_YV12:
|
|
442 case IMGFMT_YUY2:
|
|
443 // case IMGFMT_RGB|24:
|
|
444 // case IMGFMT_BGR|32:
|
|
445 return 1;
|
|
446 }
|
|
447 return 0;
|
|
448 }
|
|
449
|
|
450 static void
|
|
451 uninit(void)
|
|
452 {
|
|
453 vo_kill_eventhandler();
|
|
454 }
|
|
455
|
|
456
|
|
457
|