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 */
|
|
16
|
|
17 #include <stdio.h>
|
|
18 #include <stdlib.h>
|
|
19 #include <string.h>
|
|
20
|
|
21 #include "config.h"
|
|
22 #include "video_out.h"
|
|
23 #include "video_out_internal.h"
|
|
24
|
|
25 LIBVO_EXTERN( x11 )
|
|
26
|
|
27 #include <X11/Xlib.h>
|
|
28 #include <X11/Xutil.h>
|
|
29 #include <X11/extensions/XShm.h>
|
|
30 #include <errno.h>
|
|
31 #include "yuv2rgb.h"
|
|
32
|
|
33 static vo_info_t vo_info =
|
|
34 {
|
|
35 "X11 ( XImage/Shm )",
|
|
36 "x11",
|
|
37 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
|
|
38 ""
|
|
39 };
|
|
40
|
|
41 /* private prototypes */
|
|
42 static void Display_Image ( XImage * myximage,unsigned char *ImageData );
|
|
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 *mDisplay;
|
|
52 static Window mywindow;
|
|
53 static GC mygc;
|
|
54 static XImage *myximage;
|
|
55 static int depth,bpp,mode;
|
|
56 static XWindowAttributes attribs;
|
|
57 static int X_already_started=0;
|
|
58
|
|
59 static int windowwidth,windowheight;
|
|
60
|
|
61 #define SH_MEM
|
|
62
|
|
63 #ifdef SH_MEM
|
|
64
|
|
65 #include <sys/ipc.h>
|
|
66 #include <sys/shm.h>
|
|
67 #include <X11/extensions/XShm.h>
|
|
68
|
|
69 //static int HandleXError _ANSI_ARGS_( ( Display * dpy,XErrorEvent * event ) );
|
|
70 static void InstallXErrorHandler ( void );
|
|
71 static void DeInstallXErrorHandler ( void );
|
|
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 void InstallXErrorHandler()
|
|
80 {
|
|
81 //XSetErrorHandler( HandleXError );
|
|
82 XFlush( mDisplay );
|
|
83 }
|
|
84
|
|
85 static void DeInstallXErrorHandler()
|
|
86 {
|
|
87 XSetErrorHandler( NULL );
|
|
88 XFlush( mDisplay );
|
|
89 }
|
|
90
|
|
91 #endif
|
|
92
|
|
93 static uint32_t image_width;
|
|
94 static uint32_t image_height;
|
|
95 static uint32_t image_format;
|
|
96
|
|
97 extern void vo_decoration( Display * vo_Display,Window w,int d );
|
|
98
|
|
99 static Bool mEvents( Display * display,XEvent * Event,XPointer arg )
|
|
100 {
|
|
101 int i;
|
|
102 char buf[100];
|
|
103 KeySym keySym;
|
|
104 XComposeStatus stat;
|
|
105 unsigned long vo_KeyTable[512];
|
|
106
|
|
107 switch( Event->type )
|
|
108 {
|
|
109 case ConfigureNotify:
|
|
110 windowwidth=Event->xconfigure.width;
|
|
111 windowheight=Event->xconfigure.height;
|
|
112 break;
|
|
113 case KeyPress:
|
|
114 XLookupString( &Event->xkey,buf,sizeof(buf),&keySym,&stat );
|
|
115 vo_keyboard( ( (keySym&0xff00) != 0?( (keySym&0x00ff) + 256 ):( keySym ) ) );
|
|
116 break;
|
|
117 }
|
|
118 return 0;
|
|
119 }
|
|
120
|
|
121 static XEvent mEvent;
|
|
122
|
|
123 static void mThread( void )
|
|
124 {
|
|
125 printf("thread okey!\n");
|
|
126 XIfEvent( mDisplay,&mEvent,mEvents,NULL );
|
|
127 }
|
|
128
|
|
129 static uint32_t init( uint32_t width,uint32_t height,uint32_t d_width,uint32_t d_height,uint32_t fullscreen,char *title,uint32_t format )
|
|
130 {
|
|
131 int screen;
|
|
132 unsigned int fg,bg;
|
|
133 char *hello=( title == NULL ) ? "X11 render" : title;
|
|
134 char *name=":0.0";
|
|
135 XSizeHints hint;
|
|
136 XVisualInfo vinfo;
|
|
137 XEvent xev;
|
|
138 XGCValues xgcv;
|
|
139 Colormap theCmap;
|
|
140 XSetWindowAttributes xswa;
|
|
141 unsigned long xswamask;
|
|
142
|
|
143 image_height=height;
|
|
144 image_width=width;
|
|
145 image_format=format;
|
|
146
|
|
147 if ( X_already_started ) return -1;
|
|
148 if( !vo_init() ) return 0; // Can't open X11
|
|
149
|
|
150 if( getenv( "DISPLAY" ) ) name=getenv( "DISPLAY" );
|
|
151
|
|
152 mDisplay=XOpenDisplay( name );
|
|
153
|
|
154 if ( mDisplay == NULL )
|
|
155 {
|
|
156 fprintf( stderr,"Can not open display\n" );
|
|
157 return -1;
|
|
158 }
|
|
159
|
|
160 screen=DefaultScreen( mDisplay );
|
|
161
|
|
162 hint.x=0;
|
|
163 hint.y=0;
|
|
164 hint.width=image_width;
|
|
165 hint.height=image_height;
|
|
166 if ( fullscreen )
|
|
167 {
|
|
168 hint.width=vo_screenwidth;
|
|
169 hint.height=vo_screenheight;
|
|
170 }
|
|
171 windowwidth=hint.width;
|
|
172 windowheight=hint.height;
|
|
173 hint.flags=PPosition | PSize;
|
|
174
|
|
175 bg=WhitePixel( mDisplay,screen );
|
|
176 fg=BlackPixel( mDisplay,screen );
|
|
177
|
|
178 XGetWindowAttributes( mDisplay,DefaultRootWindow( mDisplay ),&attribs );
|
|
179 depth=attribs.depth;
|
|
180
|
|
181 if ( depth != 15 && depth != 16 && depth != 24 && depth != 32 ) depth=24;
|
|
182 XMatchVisualInfo( mDisplay,screen,depth,TrueColor,&vinfo );
|
|
183
|
|
184 theCmap =XCreateColormap( mDisplay,RootWindow( mDisplay,screen ),
|
|
185 vinfo.visual,AllocNone );
|
|
186
|
|
187 xswa.background_pixel=0;
|
|
188 xswa.border_pixel=1;
|
|
189 xswa.colormap=theCmap;
|
|
190 xswamask=CWBackPixel | CWBorderPixel |CWColormap;
|
|
191
|
|
192 mywindow=XCreateWindow( mDisplay,RootWindow( mDisplay,screen ),
|
|
193 hint.x,hint.y,
|
|
194 hint.width,hint.height,
|
|
195 xswa.border_pixel,depth,CopyFromParent,vinfo.visual,xswamask,&xswa );
|
|
196
|
|
197 if ( fullscreen ) vo_decoration( mDisplay,mywindow,0 );
|
|
198 XSelectInput( mDisplay,mywindow,StructureNotifyMask );
|
|
199 XSetStandardProperties( mDisplay,mywindow,hello,hello,None,NULL,0,&hint );
|
|
200 XMapWindow( mDisplay,mywindow );
|
|
201 do { XNextEvent( mDisplay,&xev ); } while ( xev.type != MapNotify || xev.xmap.event != mywindow );
|
|
202
|
|
203 XSelectInput( mDisplay,mywindow,NoEventMask );
|
|
204
|
|
205 XFlush( mDisplay );
|
|
206 XSync( mDisplay,False );
|
|
207
|
|
208 mygc=XCreateGC( mDisplay,mywindow,0L,&xgcv );
|
|
209
|
|
210 #ifdef SH_MEM
|
|
211 if ( XShmQueryExtension( mDisplay ) ) Shmem_Flag=1;
|
|
212 else
|
|
213 {
|
|
214 Shmem_Flag=0;
|
|
215 if ( !Quiet_Flag ) fprintf( stderr,"Shared memory not supported\nReverting to normal Xlib\n" );
|
|
216 }
|
|
217 if ( Shmem_Flag ) CompletionType=XShmGetEventBase( mDisplay ) + ShmCompletion;
|
|
218
|
|
219 InstallXErrorHandler();
|
|
220
|
|
221 if ( Shmem_Flag )
|
|
222 {
|
|
223 myximage=XShmCreateImage( mDisplay,vinfo.visual,depth,ZPixmap,NULL,&Shminfo[0],width,image_height );
|
|
224 if ( myximage == NULL )
|
|
225 {
|
|
226 if ( myximage != NULL ) XDestroyImage( myximage );
|
|
227 if ( !Quiet_Flag ) fprintf( stderr,"Shared memory error,disabling ( Ximage error )\n" );
|
|
228 goto shmemerror;
|
|
229 }
|
|
230 Shminfo[0].shmid=shmget( IPC_PRIVATE,
|
|
231 myximage->bytes_per_line * myximage->height ,
|
|
232 IPC_CREAT | 0777 );
|
|
233 if ( Shminfo[0].shmid < 0 )
|
|
234 {
|
|
235 XDestroyImage( myximage );
|
|
236 if ( !Quiet_Flag )
|
|
237 {
|
|
238 printf( "%s\n",strerror( errno ) );
|
|
239 perror( strerror( errno ) );
|
|
240 fprintf( stderr,"Shared memory error,disabling ( seg id error )\n" );
|
|
241 }
|
|
242 goto shmemerror;
|
|
243 }
|
|
244 Shminfo[0].shmaddr=( char * ) shmat( Shminfo[0].shmid,0,0 );
|
|
245
|
|
246 if ( Shminfo[0].shmaddr == ( ( char * ) -1 ) )
|
|
247 {
|
|
248 XDestroyImage( myximage );
|
|
249 if ( Shminfo[0].shmaddr != ( ( char * ) -1 ) ) shmdt( Shminfo[0].shmaddr );
|
|
250 if ( !Quiet_Flag ) fprintf( stderr,"Shared memory error,disabling ( address error )\n" );
|
|
251 goto shmemerror;
|
|
252 }
|
|
253 myximage->data=Shminfo[0].shmaddr;
|
|
254 ImageData=( unsigned char * ) myximage->data;
|
|
255 Shminfo[0].readOnly=False;
|
|
256 XShmAttach( mDisplay,&Shminfo[0] );
|
|
257
|
|
258 XSync( mDisplay,False );
|
|
259
|
|
260 if ( gXErrorFlag )
|
|
261 {
|
|
262 XDestroyImage( myximage );
|
|
263 shmdt( Shminfo[0].shmaddr );
|
|
264 if ( !Quiet_Flag ) fprintf( stderr,"Shared memory error,disabling.\n" );
|
|
265 gXErrorFlag=0;
|
|
266 goto shmemerror;
|
|
267 }
|
|
268 else
|
|
269 shmctl( Shminfo[0].shmid,IPC_RMID,0 );
|
|
270
|
|
271 if ( !Quiet_Flag ) fprintf( stderr,"Sharing memory.\n" );
|
|
272 }
|
|
273 else
|
|
274 {
|
|
275 shmemerror:
|
|
276 Shmem_Flag=0;
|
|
277 #endif
|
|
278 myximage=XGetImage( mDisplay,mywindow,0,0,
|
|
279 width,image_height,AllPlanes,ZPixmap );
|
|
280 ImageData=myximage->data;
|
|
281 #ifdef SH_MEM
|
|
282 }
|
|
283
|
|
284 DeInstallXErrorHandler();
|
|
285 #endif
|
|
286
|
|
287 bpp=myximage->bits_per_pixel;
|
|
288
|
|
289 fprintf( stderr,"X11 color mask: R:%X G:%X B:%X\n",myximage->red_mask,myximage->green_mask,myximage->blue_mask );
|
|
290
|
|
291 // If we have blue in the lowest bit then obviously RGB
|
|
292 mode=( ( myximage->blue_mask & 0x01 ) != 0 ) ? MODE_RGB : MODE_BGR;
|
|
293 #ifdef WORDS_BIGENDIAN
|
|
294 if ( myximage->byte_order != MSBFirst )
|
|
295 #else
|
|
296 if ( myximage->byte_order != LSBFirst )
|
|
297 #endif
|
|
298 {
|
|
299 fprintf( stderr,"No support fon non-native XImage byte order!\n" );
|
|
300 return -1;
|
|
301 }
|
|
302
|
|
303 if( format==IMGFMT_YV12 ) yuv2rgb_init( ( depth == 24 ) ? bpp : depth,mode );
|
|
304
|
|
305 XSelectInput( mDisplay,mywindow,StructureNotifyMask | KeyPressMask );
|
|
306 X_already_started++;
|
|
307
|
|
308 printf("init thread!\n");
|
|
309 vo_initthread( mThread );
|
|
310
|
|
311 return 0;
|
|
312 }
|
|
313
|
|
314 static const vo_info_t* get_info( void )
|
|
315 { return &vo_info; }
|
|
316
|
|
317 static void Terminate_Display_Process( void )
|
|
318 {
|
|
319 getchar(); /* wait for enter to remove window */
|
|
320 #ifdef SH_MEM
|
|
321 if ( Shmem_Flag )
|
|
322 {
|
|
323 XShmDetach( mDisplay,&Shminfo[0] );
|
|
324 XDestroyImage( myximage );
|
|
325 shmdt( Shminfo[0].shmaddr );
|
|
326 }
|
|
327 #endif
|
|
328 XDestroyWindow( mDisplay,mywindow );
|
|
329 XCloseDisplay( mDisplay );
|
|
330 X_already_started=0;
|
|
331 }
|
|
332
|
|
333 static void Display_Image( XImage *myximage,uint8_t *ImageData )
|
|
334 {
|
|
335 #ifdef DISP
|
|
336 #ifdef SH_MEM
|
|
337 if ( Shmem_Flag )
|
|
338 {
|
|
339 XShmPutImage( mDisplay,mywindow,mygc,myximage,
|
|
340 0,0,
|
|
341 ( windowwidth - myximage->width ) / 2,( windowheight - myximage->height ) / 2,
|
|
342 myximage->width,myximage->height,True );
|
|
343 XFlush( mDisplay );
|
|
344 }
|
|
345 else
|
|
346 #endif
|
|
347 {
|
|
348 XPutImage( mDisplay,mywindow,mygc,myximage,
|
|
349 0,0,
|
|
350 ( windowwidth - myximage->width ) / 2,( windowheight - myximage->height ) / 2,
|
|
351 myximage->width,myximage->height );
|
|
352 XFlush( mDisplay );
|
|
353 }
|
|
354 #endif
|
|
355 }
|
|
356
|
|
357 static void flip_page( void )
|
|
358 { Display_Image( myximage,ImageData ); }
|
|
359
|
|
360 static uint32_t draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y )
|
|
361 {
|
|
362 uint8_t *dst;
|
|
363
|
|
364 dst=ImageData + ( image_width * y + x ) * ( bpp/8 );
|
|
365 yuv2rgb( dst,src[0],src[1],src[2],w,h,image_width*( bpp/8 ),stride[0],stride[1] );
|
|
366 return 0;
|
|
367 }
|
|
368
|
|
369 void rgb15to16_mmx( char* s0,char* d0,int count );
|
|
370
|
|
371 #if 1
|
|
372 static uint32_t draw_frame( uint8_t *src[] )
|
|
373 {
|
|
374 if( image_format==IMGFMT_YV12 )
|
|
375 {
|
|
376 yuv2rgb( ImageData,src[0],src[1],src[2],image_width,image_height,image_width*( bpp/8 ),image_width,image_width/2 );
|
|
377 }
|
|
378 else
|
|
379 {
|
|
380 int sbpp=( ( image_format&0xFF )+7 )/8;
|
|
381 int dbpp=( bpp+7 )/8;
|
|
382 char *d=ImageData;
|
|
383 char *s=src[0];
|
|
384 //printf( "sbpp=%d dbpp=%d depth=%d bpp=%d\n",sbpp,dbpp,depth,bpp );
|
|
385 #if 0
|
|
386 // flipped BGR
|
|
387 int i;
|
|
388 //printf( "Rendering flipped BGR frame bpp=%d src=%d dst=%d\n",bpp,sbpp,dbpp );
|
|
389 s+=sbpp*image_width*image_height;
|
|
390 for( i=0;i<image_height;i++ )
|
|
391 {
|
|
392 s-=sbpp*image_width;
|
|
393 if( sbpp==dbpp ) memcpy( d,s,sbpp*image_width );
|
|
394 else
|
|
395 {
|
|
396 char *s2=s;
|
|
397 char *d2=d;
|
|
398 char *e=s2+sbpp*image_width;
|
|
399 while( s2<e )
|
|
400 {
|
|
401 d2[0]=s2[0];
|
|
402 d2[1]=s2[1];
|
|
403 d2[2]=s2[2];
|
|
404 s2+=sbpp;d2+=dbpp;
|
|
405 }
|
|
406 }
|
|
407 d+=dbpp*image_width;
|
|
408 }
|
|
409 #else
|
|
410 // memcpy( ImageData,src[0],image_width*image_height*bpp );
|
|
411 if( sbpp==dbpp )
|
|
412 {
|
|
413 //Display_Image( myximage,s );return 0;
|
|
414 #if 1
|
|
415 if( depth==16 && image_format==( IMGFMT_BGR|15 ) ){
|
|
416 // do 15bpp->16bpp
|
|
417 #ifdef HAVE_MMX
|
|
418 rgb15to16_mmx( s,d,2*image_width*image_height );
|
|
419 #else
|
|
420 unsigned short *s1=( unsigned short * )s;
|
|
421 unsigned short *d1=( unsigned short * )d;
|
|
422 unsigned short *e=s1+image_width*image_height;
|
|
423 while( s1<e )
|
|
424 {
|
|
425 register int x=*( s1++ );
|
|
426 // rrrrrggggggbbbbb
|
|
427 // 0rrrrrgggggbbbbb
|
|
428 // 0111 1111 1110 0000=0x7FE0
|
|
429 // 00000000000001 1111=0x001F
|
|
430 *( d1++ )=( x&0x001F )|( ( x&0x7FE0 )<<1 );
|
|
431 }
|
|
432 #endif
|
|
433 }
|
|
434 else
|
|
435 #endif
|
|
436 { memcpy( d,s,sbpp*image_width*image_height ); }
|
|
437 }
|
|
438 else
|
|
439 {
|
|
440 char *e=s+sbpp*image_width*image_height;
|
|
441 //printf( "libvo: using C 24->32bpp conversion\n" );
|
|
442 while( s<e )
|
|
443 {
|
|
444 d[0]=s[0];
|
|
445 d[1]=s[1];
|
|
446 d[2]=s[2];
|
|
447 s+=sbpp;d+=dbpp;
|
|
448 }
|
|
449 }
|
|
450 #endif
|
|
451 }
|
|
452 //Display_Image( myximage,ImageData );
|
|
453 return 0;
|
|
454 }
|
|
455 #endif
|
|
456
|
|
457 static uint32_t query_format( uint32_t format )
|
|
458 {
|
|
459 if( !vo_init() ) return 0; // Can't open X11
|
|
460 if( ( format&IMGFMT_BGR_MASK )==IMGFMT_BGR && ( format&0xFF )==vo_depthonscreen ) return 1;
|
|
461 switch( format )
|
|
462 {
|
|
463 case IMGFMT_YV12: return 1;
|
|
464 }
|
|
465 return 0;
|
|
466 }
|