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
|
31
|
34 #include "x11_common.h"
|
|
35
|
1
|
36 static vo_info_t vo_info =
|
|
37 {
|
|
38 "X11/Xv",
|
|
39 "xv",
|
|
40 "Gerd Knorr <kraxel@goldbach.in-berlin.de>",
|
|
41 ""
|
|
42 };
|
|
43
|
|
44 /* since it doesn't seem to be defined on some platforms */
|
|
45 int XShmGetEventBase(Display*);
|
|
46
|
|
47 /* local data */
|
|
48 static unsigned char *ImageData;
|
|
49
|
|
50 /* X11 related variables */
|
|
51 static Display *mydisplay;
|
|
52 static Window mywindow;
|
|
53 static GC mygc;
|
|
54 static XImage *myximage;
|
|
55 static int depth, bpp, mode;
|
|
56 static XWindowAttributes attribs;
|
|
57
|
|
58 #include <X11/extensions/Xv.h>
|
|
59 #include <X11/extensions/Xvlib.h>
|
|
60 // FIXME: dynamically allocate this stuff
|
|
61 static void allocate_xvimage(int);
|
|
62 static unsigned int ver,rel,req,ev,err;
|
|
63 static unsigned int formats, adaptors,i,xv_port,xv_format;
|
31
|
64 //static int vo_dwidth,vo_dheight;
|
1
|
65 static XvAdaptorInfo *ai;
|
|
66 static XvImageFormatValues *fo;
|
|
67 static XvImage *xvimage[1];
|
|
68
|
|
69 #include <sys/ipc.h>
|
|
70 #include <sys/shm.h>
|
|
71 #include <X11/extensions/XShm.h>
|
|
72
|
|
73 static int Shmem_Flag;
|
|
74 static int Quiet_Flag;
|
|
75 static XShmSegmentInfo Shminfo[1];
|
|
76 static int gXErrorFlag;
|
|
77 static int CompletionType = -1;
|
|
78
|
|
79 static uint32_t image_width;
|
|
80 static uint32_t image_height;
|
|
81 static uint32_t image_format;
|
|
82
|
|
83 static int get_depth(){
|
|
84 char *name = ":0.0";
|
|
85 if(getenv("DISPLAY")) name = getenv("DISPLAY");
|
|
86 mydisplay = XOpenDisplay(name);
|
|
87 if (mydisplay == NULL) return 0;
|
|
88 XGetWindowAttributes(mydisplay, DefaultRootWindow(mydisplay), &attribs);
|
|
89 depth = attribs.depth;
|
|
90 XCloseDisplay(mydisplay);
|
|
91
|
|
92 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) depth = 24;
|
|
93 printf("X11 Display color depth = %d\n",depth);
|
|
94 return depth;
|
|
95 }
|
|
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
|
31
|
194 if ( fullscreen ) vo_x11_decoration( mydisplay,mywindow,0 );
|
1
|
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);
|
31
|
259 vo_dwidth = image_width;
|
|
260 vo_dheight = image_height;
|
1
|
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
|
31
|
302 static void check_events(void)
|
1
|
303 {
|
31
|
304 int e=vo_x11_check_events(mydisplay);
|
1
|
305
|
|
306 }
|
|
307
|
|
308 static void
|
|
309 flip_page(void)
|
|
310 {
|
31
|
311 check_events();
|
1
|
312 #ifdef DISP
|
|
313 XvShmPutImage(mydisplay, xv_port, mywindow, mygc, xvimage[0],
|
|
314 0, 0, image_width, image_height,
|
31
|
315 0, 0, vo_dwidth, vo_dheight,
|
1
|
316 False);
|
|
317 XFlush(mydisplay);
|
|
318 #endif
|
|
319 return;
|
|
320 }
|
|
321
|
|
322 //draw_slice(uint8_t *src[], uint32_t slice_num)
|
|
323 static uint32_t
|
|
324 draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
|
|
325 {
|
|
326 uint8_t *src;
|
|
327 uint8_t *dst;
|
|
328 int i;
|
|
329
|
|
330 dst = xvimage[0]->data + image_width * y + x;
|
|
331 src = image[0];
|
|
332 if(w==stride[0] && w==image_width)
|
|
333 memcpy(dst,src,w*h);
|
|
334 else
|
|
335 for(i=0;i<h;i++){
|
|
336 memcpy(dst,src,w);
|
|
337 src+=stride[0];
|
|
338 dst+=image_width;
|
|
339 }
|
|
340
|
|
341 x/=2;y/=2;w/=2;h/=2;
|
|
342
|
|
343 dst = xvimage[0]->data + image_width * image_height + image_width/2 * y + x;
|
|
344 src = image[2];
|
|
345 if(w==stride[2] && w==image_width/2)
|
|
346 memcpy(dst,src,w*h);
|
|
347 else
|
|
348 for(i=0;i<h;i++){
|
|
349 memcpy(dst,src,w);
|
|
350 src+=stride[2];
|
|
351 dst+=image_width/2;
|
|
352 }
|
|
353
|
|
354 dst = xvimage[0]->data + image_width * image_height * 5 / 4 + image_width/2 * y + x;
|
|
355 src = image[1];
|
|
356 if(w==stride[1] && w==image_width/2)
|
|
357 memcpy(dst,src,w*h);
|
|
358 else
|
|
359 for(i=0;i<h;i++){
|
|
360 memcpy(dst,src,w);
|
|
361 src+=stride[1];
|
|
362 dst+=image_width/2;
|
|
363 }
|
|
364
|
|
365 return 0;
|
|
366 }
|
|
367
|
|
368
|
|
369 static uint32_t
|
|
370 draw_frame(uint8_t *src[])
|
|
371 {
|
|
372 int foo;
|
|
373
|
|
374 if(xv_format==IMGFMT_YUY2){
|
|
375 // YUY2 packed, flipped
|
|
376 #if 0
|
|
377 int i;
|
|
378 unsigned short *s=(unsigned short *)src[0];
|
|
379 unsigned short *d=(unsigned short *)xvimage[0]->data;
|
|
380 s+=image_width*image_height;
|
|
381 for(i=0;i<image_height;i++){
|
|
382 s-=image_width;
|
|
383 memcpy(d,s,image_width*2);
|
|
384 d+=image_width;
|
|
385 }
|
|
386 #else
|
|
387 memcpy(xvimage[0]->data,src[0],image_width*image_height*2);
|
|
388 #endif
|
|
389 } else {
|
|
390 // YV12 planar
|
|
391 memcpy(xvimage[0]->data,src[0],image_width*image_height);
|
|
392 memcpy(xvimage[0]->data+image_width*image_height,src[2],image_width*image_height/4);
|
|
393 memcpy(xvimage[0]->data+image_width*image_height*5/4,src[1],image_width*image_height/4);
|
|
394 }
|
|
395
|
|
396 return 0;
|
|
397 }
|
|
398
|
|
399 static uint32_t
|
|
400 query_format(uint32_t format)
|
|
401 {
|
|
402 switch(format){
|
|
403 case IMGFMT_YV12:
|
|
404 case IMGFMT_YUY2:
|
|
405 // case IMGFMT_RGB|24:
|
|
406 // case IMGFMT_BGR|32:
|
|
407 return 1;
|
|
408 }
|
|
409 return 0;
|
|
410 }
|
|
411
|
|
412 static void
|
|
413 uninit(void)
|
|
414 {
|
|
415 }
|
|
416
|
|
417
|
|
418
|