Skip to content

Latest commit

 

History

History
418 lines (377 loc) · 11.2 KB

SDL_x11yuv.c

File metadata and controls

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