src/video/x11/SDL_x11yuv.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 22 Mar 2006 05:00:59 +0000
changeset 1575 3ba88cb7eb1b
parent 1402 d910939febfa
child 1642 f1211a4b7380
permissions -rw-r--r--
Updated dynamic X11 code. See details in Bugzilla #170.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This is the XFree86 Xv extension implementation of YUV video overlays */
slouken@0
    25
slouken@1361
    26
#if SDL_VIDEO_DRIVER_X11_XV
slouken@0
    27
slouken@0
    28
#include <X11/Xlib.h>
slouken@813
    29
#ifndef NO_SHARED_MEMORY
slouken@0
    30
#include <sys/ipc.h>
slouken@0
    31
#include <sys/shm.h>
slouken@0
    32
#include <X11/extensions/XShm.h>
slouken@813
    33
#endif
slouken@1361
    34
#include "../Xext/extensions/Xvlib.h"
slouken@0
    35
slouken@0
    36
#include "SDL_x11yuv_c.h"
slouken@1361
    37
#include "../SDL_yuvfuncs.h"
slouken@0
    38
slouken@0
    39
#define XFREE86_REFRESH_HACK
slouken@0
    40
#ifdef XFREE86_REFRESH_HACK
slouken@0
    41
#include "SDL_x11image_c.h"
slouken@0
    42
#endif
slouken@0
    43
slouken@0
    44
/* Workaround when pitch != width */
slouken@0
    45
#define PITCH_WORKAROUND
slouken@0
    46
slouken@182
    47
/* Fix for the NVidia GeForce 2 - use the last available adaptor */
slouken@341
    48
/*#define USE_LAST_ADAPTOR*/  /* Apparently the NVidia drivers are fixed */
slouken@182
    49
slouken@0
    50
/* The functions used to manipulate software video overlays */
slouken@0
    51
static struct private_yuvhwfuncs x11_yuvfuncs = {
slouken@0
    52
	X11_LockYUVOverlay,
slouken@0
    53
	X11_UnlockYUVOverlay,
slouken@0
    54
	X11_DisplayYUVOverlay,
slouken@0
    55
	X11_FreeYUVOverlay
slouken@0
    56
};
slouken@0
    57
slouken@0
    58
struct private_yuvhwdata {
slouken@0
    59
	int port;
slouken@813
    60
#ifndef NO_SHARED_MEMORY
slouken@813
    61
	int yuv_use_mitshm;
slouken@0
    62
	XShmSegmentInfo yuvshm;
slouken@813
    63
#endif
slouken@292
    64
	SDL_NAME(XvImage) *image;
slouken@0
    65
};
slouken@0
    66
slouken@0
    67
slouken@950
    68
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
slouken@950
    69
slouken@813
    70
#ifndef NO_SHARED_MEMORY
slouken@813
    71
/* Shared memory error handler routine */
slouken@813
    72
static int shm_error;
slouken@813
    73
static int shm_errhandler(Display *d, XErrorEvent *e)
slouken@813
    74
{
slouken@813
    75
        if ( e->error_code == BadAccess ) {
slouken@813
    76
        	shm_error = True;
slouken@813
    77
        	return(0);
slouken@813
    78
        } else
slouken@813
    79
		return(X_handler(d,e));
slouken@813
    80
}
slouken@813
    81
#endif /* !NO_SHARED_MEMORY */
slouken@813
    82
slouken@950
    83
static int xv_error;
slouken@950
    84
static int xv_errhandler(Display *d, XErrorEvent *e)
slouken@950
    85
{
slouken@950
    86
        if ( e->error_code == BadMatch ) {
slouken@950
    87
        	xv_error = True;
slouken@950
    88
        	return(0);
slouken@950
    89
        } else
slouken@950
    90
		return(X_handler(d,e));
slouken@950
    91
}
slouken@813
    92
slouken@0
    93
SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
slouken@0
    94
{
slouken@0
    95
	SDL_Overlay *overlay;
slouken@0
    96
	struct private_yuvhwdata *hwdata;
slouken@0
    97
	int xv_port;
slouken@1196
    98
	unsigned int i, j, k;
slouken@1196
    99
	unsigned int adaptors;
slouken@292
   100
	SDL_NAME(XvAdaptorInfo) *ainfo;
slouken@813
   101
	int bpp;
slouken@813
   102
#ifndef NO_SHARED_MEMORY
slouken@0
   103
	XShmSegmentInfo *yuvshm;
slouken@813
   104
#endif
slouken@0
   105
slouken@0
   106
	/* Look for the XVideo extension with a valid port for this format */
slouken@0
   107
	xv_port = -1;
slouken@292
   108
	if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) &&
slouken@292
   109
	     (Success == SDL_NAME(XvQueryAdaptors)(GFX_Display,
slouken@0
   110
	                                 RootWindow(GFX_Display, SDL_Screen),
slouken@0
   111
	                                 &adaptors, &ainfo)) ) {
slouken@182
   112
#ifdef USE_LAST_ADAPTOR
slouken@659
   113
		for ( i=0; i < adaptors; ++i )
slouken@182
   114
#else
slouken@659
   115
		for ( i=0; (i < adaptors) && (xv_port == -1); ++i )
slouken@182
   116
#endif /* USE_LAST_ADAPTOR */
slouken@659
   117
		{
slouken@0
   118
			/* Check to see if the visual can be used */
slouken@0
   119
			if ( BUGGY_XFREE86(<=, 4001) ) {
slouken@0
   120
				int visual_ok = 0;
slouken@0
   121
				for ( j=0; j<ainfo[i].num_formats; ++j ) {
slouken@0
   122
					if ( ainfo[i].formats[j].visual_id ==
slouken@0
   123
							SDL_Visual->visualid ) {
slouken@0
   124
						visual_ok = 1;
slouken@0
   125
						break;
slouken@0
   126
					}
slouken@0
   127
				}
slouken@0
   128
				if ( ! visual_ok ) {
slouken@0
   129
					continue;
slouken@0
   130
				}
slouken@0
   131
			}
slouken@0
   132
			if ( (ainfo[i].type & XvInputMask) &&
slouken@0
   133
			     (ainfo[i].type & XvImageMask) ) {
slouken@0
   134
				int num_formats;
slouken@292
   135
				SDL_NAME(XvImageFormatValues) *formats;
slouken@292
   136
				formats = SDL_NAME(XvListImageFormats)(GFX_Display,
slouken@0
   137
				              ainfo[i].base_id, &num_formats);
slouken@182
   138
#ifdef USE_LAST_ADAPTOR
slouken@659
   139
				for ( j=0; j < num_formats; ++j )
slouken@182
   140
#else
slouken@659
   141
				for ( j=0; (j < num_formats) && (xv_port == -1); ++j )
slouken@182
   142
#endif /* USE_LAST_ADAPTOR */
slouken@659
   143
				{
slouken@0
   144
					if ( (Uint32)formats[j].id == format ) {
slouken@0
   145
						for ( k=0; k < ainfo[i].num_ports; ++k ) {
slouken@292
   146
							if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) {
slouken@0
   147
								xv_port = ainfo[i].base_id+k;
slouken@0
   148
								break;
slouken@0
   149
							}
slouken@0
   150
						}
slouken@0
   151
					}
slouken@0
   152
				}
slouken@410
   153
				if ( formats ) {
icculus@1575
   154
					XFree(formats);
slouken@410
   155
				}
slouken@0
   156
			}
slouken@0
   157
		}
slouken@409
   158
		SDL_NAME(XvFreeAdaptorInfo)(ainfo);
slouken@0
   159
	}
icculus@429
   160
slouken@813
   161
	/* Precalculate the bpp for the pitch workaround below */
slouken@813
   162
	switch (format) {
slouken@813
   163
	    /* Add any other cases we need to support... */
slouken@813
   164
	    case SDL_YUY2_OVERLAY:
slouken@813
   165
	    case SDL_UYVY_OVERLAY:
slouken@813
   166
	    case SDL_YVYU_OVERLAY:
slouken@813
   167
		bpp = 2;
slouken@813
   168
		break;
slouken@813
   169
	    default:
slouken@813
   170
		bpp = 1;
slouken@813
   171
		break;
slouken@813
   172
	}
slouken@813
   173
icculus@434
   174
#if 0
icculus@429
   175
    /*
icculus@429
   176
     * !!! FIXME:
icculus@429
   177
     * "Here are some diffs for X11 and yuv.  Note that the last part 2nd
icculus@429
   178
     *  diff should probably be a new call to XvQueryAdaptorFree with ainfo
icculus@429
   179
     *  and the number of adaptors, instead of the loop through like I did."
icculus@434
   180
     *
icculus@434
   181
     *  ACHTUNG: This is broken! It looks like XvFreeAdaptorInfo does this
icculus@434
   182
     *  for you, so we end up with a double-free. I need to look at this
icculus@434
   183
     *  more closely...  --ryan.
icculus@429
   184
     */
icculus@429
   185
 	for ( i=0; i < adaptors; ++i ) {
icculus@429
   186
 	  if (ainfo[i].name != NULL) Xfree(ainfo[i].name);
icculus@429
   187
 	  if (ainfo[i].formats != NULL) Xfree(ainfo[i].formats);
icculus@429
   188
   	}
icculus@429
   189
 	Xfree(ainfo);
icculus@434
   190
#endif
icculus@429
   191
slouken@0
   192
	if ( xv_port == -1 ) {
slouken@0
   193
		SDL_SetError("No available video ports for requested format");
slouken@0
   194
		return(NULL);
slouken@0
   195
	}
slouken@0
   196
slouken@950
   197
	/* Enable auto-painting of the overlay colorkey */
slouken@950
   198
	{
slouken@950
   199
		static const char *attr[] = { "XV_AUTOPAINT_COLORKEY", "XV_AUTOPAINT_COLOURKEY" };
slouken@950
   200
		unsigned int i;
slouken@950
   201
slouken@950
   202
		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, True);
icculus@1575
   203
		X_handler = XSetErrorHandler(xv_errhandler);
slouken@950
   204
		for ( i=0; i < sizeof(attr)/(sizeof attr[0]); ++i ) {
slouken@950
   205
			Atom a;
slouken@950
   206
slouken@950
   207
			xv_error = False;
icculus@1575
   208
			a = XInternAtom(GFX_Display, attr[i], True);
slouken@950
   209
			if ( a != None ) {
slouken@950
   210
     				SDL_NAME(XvSetPortAttribute)(GFX_Display, xv_port, a, 1);
icculus@1575
   211
				XSync(GFX_Display, True);
slouken@950
   212
				if ( ! xv_error ) {
slouken@950
   213
					break;
slouken@950
   214
				}
slouken@950
   215
			}
slouken@950
   216
		}
icculus@1575
   217
		XSetErrorHandler(X_handler);
slouken@950
   218
		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, False);
slouken@950
   219
	}
slouken@950
   220
slouken@0
   221
	/* Create the overlay structure */
slouken@1336
   222
	overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
slouken@0
   223
	if ( overlay == NULL ) {
slouken@292
   224
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
slouken@0
   225
		SDL_OutOfMemory();
slouken@0
   226
		return(NULL);
slouken@0
   227
	}
slouken@1336
   228
	SDL_memset(overlay, 0, (sizeof *overlay));
slouken@0
   229
slouken@0
   230
	/* Fill in the basic members */
slouken@0
   231
	overlay->format = format;
slouken@0
   232
	overlay->w = width;
slouken@0
   233
	overlay->h = height;
slouken@0
   234
slouken@0
   235
	/* Set up the YUV surface function structure */
slouken@0
   236
	overlay->hwfuncs = &x11_yuvfuncs;
slouken@0
   237
	overlay->hw_overlay = 1;
slouken@0
   238
slouken@0
   239
	/* Create the pixel data and lookup tables */
slouken@1336
   240
	hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata);
slouken@0
   241
	overlay->hwdata = hwdata;
slouken@0
   242
	if ( hwdata == NULL ) {
slouken@292
   243
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
slouken@0
   244
		SDL_OutOfMemory();
slouken@0
   245
		SDL_FreeYUVOverlay(overlay);
slouken@0
   246
		return(NULL);
slouken@0
   247
	}
slouken@813
   248
	hwdata->port = xv_port;
slouken@813
   249
#ifndef NO_SHARED_MEMORY
slouken@0
   250
	yuvshm = &hwdata->yuvshm;
slouken@1336
   251
	SDL_memset(yuvshm, 0, sizeof(*yuvshm));
slouken@292
   252
	hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
slouken@813
   253
						   0, width, height, yuvshm);
slouken@813
   254
#ifdef PITCH_WORKAROUND
slouken@813
   255
	if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
slouken@813
   256
		/* Ajust overlay width according to pitch */ 
icculus@1575
   257
		XFree(hwdata->image);
slouken@813
   258
		width = hwdata->image->pitches[0] / bpp;
slouken@813
   259
		hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
slouken@813
   260
							   0, width, height, yuvshm);
slouken@813
   261
	}
slouken@813
   262
#endif /* PITCH_WORKAROUND */
slouken@813
   263
	hwdata->yuv_use_mitshm = (hwdata->image != NULL);
slouken@813
   264
	if ( hwdata->yuv_use_mitshm ) {
slouken@813
   265
		yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
slouken@813
   266
				       IPC_CREAT | 0777);
slouken@813
   267
		if ( yuvshm->shmid >= 0 ) {
slouken@813
   268
			yuvshm->shmaddr = (char *)shmat(yuvshm->shmid, 0, 0);
slouken@813
   269
			yuvshm->readOnly = False;
slouken@813
   270
			if ( yuvshm->shmaddr != (char *)-1 ) {
slouken@813
   271
				shm_error = False;
icculus@1575
   272
				X_handler = XSetErrorHandler(shm_errhandler);
icculus@1575
   273
				XShmAttach(GFX_Display, yuvshm);
icculus@1575
   274
				XSync(GFX_Display, True);
icculus@1575
   275
				XSetErrorHandler(X_handler);
slouken@813
   276
				if ( shm_error )
slouken@813
   277
					shmdt(yuvshm->shmaddr);
slouken@813
   278
			} else {
slouken@813
   279
				shm_error = True;
slouken@813
   280
			}
slouken@813
   281
			shmctl(yuvshm->shmid, IPC_RMID, NULL);
slouken@813
   282
		} else {
slouken@813
   283
			shm_error = True;
slouken@813
   284
		}
slouken@813
   285
		if ( shm_error ) {
icculus@1575
   286
			XFree(hwdata->image);
slouken@813
   287
			hwdata->yuv_use_mitshm = 0;
slouken@813
   288
		} else {
slouken@813
   289
			hwdata->image->data = yuvshm->shmaddr;
slouken@813
   290
		}
slouken@813
   291
	}
slouken@813
   292
	if ( !hwdata->yuv_use_mitshm )
slouken@813
   293
#endif /* NO_SHARED_MEMORY */
slouken@813
   294
	{
slouken@813
   295
		hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
slouken@813
   296
							0, width, height);
slouken@0
   297
slouken@0
   298
#ifdef PITCH_WORKAROUND
slouken@813
   299
		if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
slouken@813
   300
			/* Ajust overlay width according to pitch */ 
icculus@1575
   301
			XFree(hwdata->image);
slouken@813
   302
			width = hwdata->image->pitches[0] / bpp;
slouken@813
   303
			hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
slouken@813
   304
								0, width, height);
slouken@813
   305
		}
slouken@813
   306
#endif /* PITCH_WORKAROUND */
slouken@813
   307
		if ( hwdata->image == NULL ) {
slouken@813
   308
			SDL_SetError("Couldn't create XVideo image");
slouken@813
   309
			SDL_FreeYUVOverlay(overlay);
slouken@813
   310
			return(NULL);
slouken@813
   311
		}
slouken@1336
   312
		hwdata->image->data = SDL_malloc(hwdata->image->data_size);
slouken@813
   313
		if ( hwdata->image->data == NULL ) {
slouken@813
   314
			SDL_OutOfMemory();
slouken@813
   315
			SDL_FreeYUVOverlay(overlay);
slouken@813
   316
			return(NULL);
slouken@813
   317
		}
slouken@0
   318
	}
slouken@0
   319
slouken@0
   320
	/* Find the pitch and offset values for the overlay */
slouken@0
   321
	overlay->planes = hwdata->image->num_planes;
slouken@1336
   322
	overlay->pitches = (Uint16 *)SDL_malloc(overlay->planes * sizeof(Uint16));
slouken@1336
   323
	overlay->pixels = (Uint8 **)SDL_malloc(overlay->planes * sizeof(Uint8 *));
slouken@0
   324
	if ( !overlay->pitches || !overlay->pixels ) {
slouken@0
   325
		SDL_OutOfMemory();
slouken@0
   326
		SDL_FreeYUVOverlay(overlay);
slouken@0
   327
		return(NULL);
slouken@0
   328
	}
slouken@0
   329
	for ( i=0; i<overlay->planes; ++i ) {
slouken@0
   330
		overlay->pitches[i] = hwdata->image->pitches[i];
slouken@0
   331
		overlay->pixels[i] = (Uint8 *)hwdata->image->data +
slouken@0
   332
		                              hwdata->image->offsets[i];
slouken@0
   333
	}
slouken@0
   334
slouken@0
   335
#ifdef XFREE86_REFRESH_HACK
slouken@0
   336
	/* Work around an XFree86 X server bug (?)
slouken@0
   337
	   We can't perform normal updates in windows that have video
slouken@0
   338
	   being output to them.  See SDL_x11image.c for more details.
slouken@0
   339
	 */
slouken@0
   340
	X11_DisableAutoRefresh(this);
slouken@0
   341
#endif
slouken@0
   342
slouken@0
   343
	/* We're all done.. */
slouken@0
   344
	return(overlay);
slouken@0
   345
}
slouken@0
   346
slouken@0
   347
int X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
slouken@0
   348
{
slouken@0
   349
	return(0);
slouken@0
   350
}
slouken@0
   351
slouken@0
   352
void X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
slouken@0
   353
{
slouken@0
   354
	return;
slouken@0
   355
}
slouken@0
   356
slouken@0
   357
int X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
slouken@0
   358
{
slouken@0
   359
	struct private_yuvhwdata *hwdata;
slouken@0
   360
slouken@0
   361
	hwdata = overlay->hwdata;
slouken@813
   362
#ifndef NO_SHARED_MEMORY
slouken@813
   363
	if ( hwdata->yuv_use_mitshm ) {
slouken@813
   364
		SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
slouken@0
   365
	              hwdata->image, 0, 0, overlay->w, overlay->h,
slouken@0
   366
	              dstrect->x, dstrect->y, dstrect->w, dstrect->h, False);
slouken@813
   367
	}
slouken@813
   368
	else
slouken@813
   369
#endif
slouken@813
   370
	{
slouken@813
   371
		SDL_NAME(XvPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
slouken@813
   372
				     hwdata->image, 0, 0, overlay->w, overlay->h,
slouken@813
   373
				     dstrect->x, dstrect->y, dstrect->w, dstrect->h);
slouken@813
   374
	}
icculus@1575
   375
	XSync(GFX_Display, False);
slouken@0
   376
	return(0);
slouken@0
   377
}
slouken@0
   378
slouken@0
   379
void X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
slouken@0
   380
{
slouken@0
   381
	struct private_yuvhwdata *hwdata;
slouken@0
   382
slouken@0
   383
	hwdata = overlay->hwdata;
slouken@0
   384
	if ( hwdata ) {
slouken@292
   385
		SDL_NAME(XvUngrabPort)(GFX_Display, hwdata->port, CurrentTime);
slouken@813
   386
#ifndef NO_SHARED_MEMORY
slouken@813
   387
		if ( hwdata->yuv_use_mitshm ) {
icculus@1575
   388
			XShmDetach(GFX_Display, &hwdata->yuvshm);
slouken@0
   389
			shmdt(hwdata->yuvshm.shmaddr);
slouken@0
   390
		}
slouken@813
   391
#endif
slouken@0
   392
		if ( hwdata->image ) {
icculus@1575
   393
			XFree(hwdata->image);
slouken@0
   394
		}
slouken@1336
   395
		SDL_free(hwdata);
slouken@0
   396
	}
slouken@0
   397
	if ( overlay->pitches ) {
slouken@1336
   398
		SDL_free(overlay->pitches);
slouken@0
   399
		overlay->pitches = NULL;
slouken@0
   400
	}
slouken@0
   401
	if ( overlay->pixels ) {
slouken@1336
   402
		SDL_free(overlay->pixels);
slouken@0
   403
		overlay->pixels = NULL;
slouken@0
   404
	}
slouken@0
   405
#ifdef XFREE86_REFRESH_HACK
slouken@0
   406
	X11_EnableAutoRefresh(this);
slouken@0
   407
#endif
slouken@0
   408
}
slouken@0
   409
slouken@1361
   410
#endif /* SDL_VIDEO_DRIVER_X11_XV */