src/video/x11/SDL_x11yuv.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 292 eadc0746dfaf
child 341 6ca967a46bf1
permissions -rw-r--r--
Updated copyright information for 2002
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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@0
    35
#include <sys/ipc.h>
slouken@0
    36
#include <sys/shm.h>
slouken@0
    37
#include <X11/extensions/XShm.h>
slouken@292
    38
#include <XFree86/extensions/Xvlib.h>
slouken@0
    39
slouken@0
    40
#include "SDL_error.h"
slouken@0
    41
#include "SDL_video.h"
slouken@0
    42
#include "SDL_x11yuv_c.h"
slouken@0
    43
#include "SDL_yuvfuncs.h"
slouken@0
    44
slouken@0
    45
#define XFREE86_REFRESH_HACK
slouken@0
    46
#ifdef XFREE86_REFRESH_HACK
slouken@0
    47
#include "SDL_x11image_c.h"
slouken@0
    48
#endif
slouken@0
    49
slouken@0
    50
/* Workaround when pitch != width */
slouken@0
    51
#define PITCH_WORKAROUND
slouken@0
    52
slouken@182
    53
/* Fix for the NVidia GeForce 2 - use the last available adaptor */
slouken@182
    54
#define USE_LAST_ADAPTOR
slouken@182
    55
slouken@0
    56
/* The functions used to manipulate software video overlays */
slouken@0
    57
static struct private_yuvhwfuncs x11_yuvfuncs = {
slouken@0
    58
	X11_LockYUVOverlay,
slouken@0
    59
	X11_UnlockYUVOverlay,
slouken@0
    60
	X11_DisplayYUVOverlay,
slouken@0
    61
	X11_FreeYUVOverlay
slouken@0
    62
};
slouken@0
    63
slouken@0
    64
struct private_yuvhwdata {
slouken@0
    65
	int port;
slouken@0
    66
	XShmSegmentInfo yuvshm;
slouken@292
    67
	SDL_NAME(XvImage) *image;
slouken@0
    68
};
slouken@0
    69
slouken@0
    70
slouken@0
    71
SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
slouken@0
    72
{
slouken@0
    73
	SDL_Overlay *overlay;
slouken@0
    74
	struct private_yuvhwdata *hwdata;
slouken@0
    75
	int xv_port;
slouken@0
    76
	int i, j, k;
slouken@0
    77
	int adaptors;
slouken@292
    78
	SDL_NAME(XvAdaptorInfo) *ainfo;
slouken@0
    79
	XShmSegmentInfo *yuvshm;
slouken@0
    80
slouken@0
    81
	/* Look for the XVideo extension with a valid port for this format */
slouken@0
    82
	xv_port = -1;
slouken@292
    83
	if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) &&
slouken@292
    84
	     (Success == SDL_NAME(XvQueryAdaptors)(GFX_Display,
slouken@0
    85
	                                 RootWindow(GFX_Display, SDL_Screen),
slouken@0
    86
	                                 &adaptors, &ainfo)) ) {
slouken@182
    87
#ifdef USE_LAST_ADAPTOR
slouken@182
    88
		for ( i=0; i < adaptors; ++i ) {
slouken@182
    89
#else
slouken@0
    90
		for ( i=0; (i < adaptors) && (xv_port == -1); ++i ) {
slouken@182
    91
#endif /* USE_LAST_ADAPTOR */
slouken@0
    92
			/* Check to see if the visual can be used */
slouken@0
    93
			if ( BUGGY_XFREE86(<=, 4001) ) {
slouken@0
    94
				int visual_ok = 0;
slouken@0
    95
				for ( j=0; j<ainfo[i].num_formats; ++j ) {
slouken@0
    96
					if ( ainfo[i].formats[j].visual_id ==
slouken@0
    97
							SDL_Visual->visualid ) {
slouken@0
    98
						visual_ok = 1;
slouken@0
    99
						break;
slouken@0
   100
					}
slouken@0
   101
				}
slouken@0
   102
				if ( ! visual_ok ) {
slouken@0
   103
					continue;
slouken@0
   104
				}
slouken@0
   105
			}
slouken@0
   106
			if ( (ainfo[i].type & XvInputMask) &&
slouken@0
   107
			     (ainfo[i].type & XvImageMask) ) {
slouken@0
   108
				int num_formats;
slouken@292
   109
				SDL_NAME(XvImageFormatValues) *formats;
slouken@292
   110
				formats = SDL_NAME(XvListImageFormats)(GFX_Display,
slouken@0
   111
				              ainfo[i].base_id, &num_formats);
slouken@182
   112
#ifdef USE_LAST_ADAPTOR
slouken@182
   113
				for ( j=0; j < num_formats; ++j ) {
slouken@182
   114
#else
slouken@0
   115
				for ( j=0; (j < num_formats) && (xv_port == -1); ++j ) {
slouken@182
   116
#endif /* USE_LAST_ADAPTOR */
slouken@0
   117
					if ( (Uint32)formats[j].id == format ) {
slouken@0
   118
						for ( k=0; k < ainfo[i].num_ports; ++k ) {
slouken@292
   119
							if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) {
slouken@0
   120
								xv_port = ainfo[i].base_id+k;
slouken@0
   121
								break;
slouken@0
   122
							}
slouken@0
   123
						}
slouken@0
   124
					}
slouken@0
   125
				}
slouken@0
   126
			}
slouken@0
   127
		}
slouken@0
   128
	}
slouken@0
   129
	if ( xv_port == -1 ) {
slouken@0
   130
		SDL_SetError("No available video ports for requested format");
slouken@0
   131
		return(NULL);
slouken@0
   132
	}
slouken@0
   133
slouken@0
   134
	/* Create the overlay structure */
slouken@0
   135
	overlay = (SDL_Overlay *)malloc(sizeof *overlay);
slouken@0
   136
	if ( overlay == NULL ) {
slouken@292
   137
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
slouken@0
   138
		SDL_OutOfMemory();
slouken@0
   139
		return(NULL);
slouken@0
   140
	}
slouken@0
   141
	memset(overlay, 0, (sizeof *overlay));
slouken@0
   142
slouken@0
   143
	/* Fill in the basic members */
slouken@0
   144
	overlay->format = format;
slouken@0
   145
	overlay->w = width;
slouken@0
   146
	overlay->h = height;
slouken@0
   147
slouken@0
   148
	/* Set up the YUV surface function structure */
slouken@0
   149
	overlay->hwfuncs = &x11_yuvfuncs;
slouken@0
   150
	overlay->hw_overlay = 1;
slouken@0
   151
slouken@0
   152
	/* Create the pixel data and lookup tables */
slouken@0
   153
	hwdata = (struct private_yuvhwdata *)malloc(sizeof *hwdata);
slouken@0
   154
	overlay->hwdata = hwdata;
slouken@0
   155
	if ( hwdata == NULL ) {
slouken@292
   156
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
slouken@0
   157
		SDL_OutOfMemory();
slouken@0
   158
		SDL_FreeYUVOverlay(overlay);
slouken@0
   159
		return(NULL);
slouken@0
   160
	}
slouken@0
   161
	yuvshm = &hwdata->yuvshm;
slouken@0
   162
	memset(yuvshm, 0, sizeof(*yuvshm));
slouken@0
   163
	hwdata->port = xv_port;
slouken@292
   164
	hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
slouken@0
   165
	                                 0, width, height, yuvshm);
slouken@0
   166
slouken@0
   167
#ifdef PITCH_WORKAROUND
slouken@0
   168
	if ( hwdata->image != NULL && hwdata->image->pitches[0] != width )
slouken@0
   169
	{
slouken@0
   170
	  /* Ajust overlay width according to pitch */ 
slouken@0
   171
	  switch (format) {
slouken@0
   172
	    case SDL_YV12_OVERLAY:
slouken@0
   173
	    case SDL_IYUV_OVERLAY:
slouken@0
   174
	        width = hwdata->image->pitches[0];
slouken@0
   175
		break;
slouken@0
   176
	    case SDL_YUY2_OVERLAY:
slouken@0
   177
	    case SDL_UYVY_OVERLAY:
slouken@0
   178
	    case SDL_YVYU_OVERLAY:
slouken@0
   179
	        width = hwdata->image->pitches[0] / 2;
slouken@0
   180
		break;
slouken@0
   181
	    default:
slouken@0
   182
		/* We should never get here (caught above) */
slouken@0
   183
		return(NULL);
slouken@0
   184
	  }
slouken@0
   185
	  
slouken@0
   186
	  XFree(hwdata->image);
slouken@292
   187
	  hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
slouken@0
   188
					   0, width, height, yuvshm);
slouken@0
   189
	}
slouken@0
   190
#endif
slouken@0
   191
slouken@0
   192
	if ( hwdata->image == NULL ) {
slouken@0
   193
		SDL_OutOfMemory();
slouken@0
   194
		SDL_FreeYUVOverlay(overlay);
slouken@0
   195
		return(NULL);
slouken@0
   196
	}
slouken@0
   197
	yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
slouken@0
   198
	                       IPC_CREAT | 0777);
slouken@0
   199
	if ( yuvshm->shmid < 0 ) {
slouken@0
   200
		SDL_SetError("Unable to get %d bytes shared memory",
slouken@0
   201
		             hwdata->image->data_size);
slouken@0
   202
		SDL_FreeYUVOverlay(overlay);
slouken@0
   203
		return(NULL);
slouken@0
   204
	}
slouken@0
   205
	yuvshm->shmaddr  = (char *) shmat(yuvshm->shmid, 0, 0);
slouken@0
   206
	yuvshm->readOnly = False;
slouken@0
   207
	hwdata->image->data = yuvshm->shmaddr;
slouken@0
   208
slouken@0
   209
	XShmAttach(GFX_Display, yuvshm);
slouken@0
   210
	XSync(GFX_Display, False);
slouken@0
   211
	shmctl(yuvshm->shmid, IPC_RMID, 0);
slouken@0
   212
slouken@0
   213
	/* Find the pitch and offset values for the overlay */
slouken@0
   214
	overlay->planes = hwdata->image->num_planes;
slouken@0
   215
	overlay->pitches = (Uint16 *)malloc(overlay->planes * sizeof(Uint16));
slouken@0
   216
	overlay->pixels = (Uint8 **)malloc(overlay->planes * sizeof(Uint8 *));
slouken@0
   217
	if ( !overlay->pitches || !overlay->pixels ) {
slouken@0
   218
		SDL_OutOfMemory();
slouken@0
   219
		SDL_FreeYUVOverlay(overlay);
slouken@0
   220
		return(NULL);
slouken@0
   221
	}
slouken@0
   222
	for ( i=0; i<overlay->planes; ++i ) {
slouken@0
   223
		overlay->pitches[i] = hwdata->image->pitches[i];
slouken@0
   224
		overlay->pixels[i] = (Uint8 *)hwdata->image->data +
slouken@0
   225
		                              hwdata->image->offsets[i];
slouken@0
   226
	}
slouken@0
   227
slouken@0
   228
#ifdef XFREE86_REFRESH_HACK
slouken@0
   229
	/* Work around an XFree86 X server bug (?)
slouken@0
   230
	   We can't perform normal updates in windows that have video
slouken@0
   231
	   being output to them.  See SDL_x11image.c for more details.
slouken@0
   232
	 */
slouken@0
   233
	X11_DisableAutoRefresh(this);
slouken@0
   234
#endif
slouken@0
   235
slouken@0
   236
	/* We're all done.. */
slouken@0
   237
	return(overlay);
slouken@0
   238
}
slouken@0
   239
slouken@0
   240
int X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
slouken@0
   241
{
slouken@0
   242
	return(0);
slouken@0
   243
}
slouken@0
   244
slouken@0
   245
void X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
slouken@0
   246
{
slouken@0
   247
	return;
slouken@0
   248
}
slouken@0
   249
slouken@0
   250
int X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
slouken@0
   251
{
slouken@0
   252
	struct private_yuvhwdata *hwdata;
slouken@0
   253
slouken@0
   254
	hwdata = overlay->hwdata;
slouken@292
   255
	SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
slouken@0
   256
	              hwdata->image, 0, 0, overlay->w, overlay->h,
slouken@0
   257
	              dstrect->x, dstrect->y, dstrect->w, dstrect->h, False);
slouken@0
   258
	XSync(GFX_Display, False);
slouken@0
   259
	return(0);
slouken@0
   260
}
slouken@0
   261
slouken@0
   262
void X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
slouken@0
   263
{
slouken@0
   264
	struct private_yuvhwdata *hwdata;
slouken@0
   265
slouken@0
   266
	hwdata = overlay->hwdata;
slouken@0
   267
	if ( hwdata ) {
slouken@292
   268
		SDL_NAME(XvUngrabPort)(GFX_Display, hwdata->port, CurrentTime);
slouken@0
   269
		if ( hwdata->yuvshm.shmaddr ) {
slouken@0
   270
			XShmDetach(GFX_Display, &hwdata->yuvshm);
slouken@0
   271
			shmdt(hwdata->yuvshm.shmaddr);
slouken@0
   272
		}
slouken@0
   273
		if ( hwdata->image ) {
slouken@0
   274
			XFree(hwdata->image);
slouken@0
   275
		}
slouken@0
   276
		free(hwdata);
slouken@0
   277
	}
slouken@0
   278
	if ( overlay->pitches ) {
slouken@0
   279
		free(overlay->pitches);
slouken@0
   280
		overlay->pitches = NULL;
slouken@0
   281
	}
slouken@0
   282
	if ( overlay->pixels ) {
slouken@0
   283
		free(overlay->pixels);
slouken@0
   284
		overlay->pixels = NULL;
slouken@0
   285
	}
slouken@0
   286
#ifdef XFREE86_REFRESH_HACK
slouken@0
   287
	X11_EnableAutoRefresh(this);
slouken@0
   288
#endif
slouken@0
   289
}
slouken@0
   290
slouken@0
   291
#endif /* XFREE86_XV */