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