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