Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
439 lines (398 loc) · 13.5 KB

SDL_x11yuv.c

File metadata and controls

439 lines (398 loc) · 13.5 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
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
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
/* This is the XFree86 Xv extension implementation of YUV video overlays */
Feb 16, 2006
Feb 16, 2006
26
#if SDL_VIDEO_DRIVER_X11_XV
Apr 26, 2001
Apr 26, 2001
27
28
#include <X11/Xlib.h>
Feb 12, 2004
Feb 12, 2004
29
#ifndef NO_SHARED_MEMORY
Apr 26, 2001
Apr 26, 2001
30
31
32
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
Feb 12, 2004
Feb 12, 2004
33
#endif
Feb 16, 2006
Feb 16, 2006
34
#include "../Xext/extensions/Xvlib.h"
Apr 26, 2001
Apr 26, 2001
35
36
#include "SDL_x11yuv_c.h"
Feb 16, 2006
Feb 16, 2006
37
#include "../SDL_yuvfuncs.h"
Apr 26, 2001
Apr 26, 2001
38
39
40
41
42
43
44
45
46
#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
47
/* Fix for the NVidia GeForce 2 - use the last available adaptor */
May 28, 2006
May 28, 2006
48
49
50
#if 0 /* Apparently the NVidia drivers are fixed */
#define USE_LAST_ADAPTOR
#endif
Sep 14, 2001
Sep 14, 2001
51
Apr 26, 2001
Apr 26, 2001
52
53
/* The functions used to manipulate software video overlays */
static struct private_yuvhwfuncs x11_yuvfuncs = {
May 28, 2006
May 28, 2006
54
55
56
57
X11_LockYUVOverlay,
X11_UnlockYUVOverlay,
X11_DisplayYUVOverlay,
X11_FreeYUVOverlay
Apr 26, 2001
Apr 26, 2001
58
59
};
May 28, 2006
May 28, 2006
60
61
62
struct private_yuvhwdata
{
int port;
Feb 12, 2004
Feb 12, 2004
63
#ifndef NO_SHARED_MEMORY
May 28, 2006
May 28, 2006
64
65
int yuv_use_mitshm;
XShmSegmentInfo yuvshm;
Feb 12, 2004
Feb 12, 2004
66
#endif
May 28, 2006
May 28, 2006
67
SDL_NAME (XvImage) * image;
Apr 26, 2001
Apr 26, 2001
68
69
70
};
May 28, 2006
May 28, 2006
71
static int (*X_handler) (Display *, XErrorEvent *) = NULL;
Aug 25, 2004
Aug 25, 2004
72
Feb 12, 2004
Feb 12, 2004
73
74
75
#ifndef NO_SHARED_MEMORY
/* Shared memory error handler routine */
static int shm_error;
May 28, 2006
May 28, 2006
76
77
static int
shm_errhandler (Display * d, XErrorEvent * e)
Feb 12, 2004
Feb 12, 2004
78
{
May 28, 2006
May 28, 2006
79
80
81
82
83
if (e->error_code == BadAccess) {
shm_error = True;
return (0);
} else
return (X_handler (d, e));
Feb 12, 2004
Feb 12, 2004
84
85
86
}
#endif /* !NO_SHARED_MEMORY */
Aug 25, 2004
Aug 25, 2004
87
static int xv_error;
May 28, 2006
May 28, 2006
88
89
static int
xv_errhandler (Display * d, XErrorEvent * e)
Aug 25, 2004
Aug 25, 2004
90
{
May 28, 2006
May 28, 2006
91
92
93
94
95
if (e->error_code == BadMatch) {
xv_error = True;
return (0);
} else
return (X_handler (d, e));
Aug 25, 2004
Aug 25, 2004
96
}
Feb 12, 2004
Feb 12, 2004
97
May 28, 2006
May 28, 2006
98
99
100
SDL_Overlay *
X11_CreateYUVOverlay (_THIS, int width, int height, Uint32 format,
SDL_Surface * display)
Apr 26, 2001
Apr 26, 2001
101
{
May 28, 2006
May 28, 2006
102
103
104
105
106
107
108
SDL_Overlay *overlay;
struct private_yuvhwdata *hwdata;
int xv_port;
unsigned int i, j, k;
unsigned int adaptors;
SDL_NAME (XvAdaptorInfo) * ainfo;
int bpp;
Feb 12, 2004
Feb 12, 2004
109
#ifndef NO_SHARED_MEMORY
May 28, 2006
May 28, 2006
110
XShmSegmentInfo *yuvshm;
Feb 12, 2004
Feb 12, 2004
111
#endif
Apr 26, 2001
Apr 26, 2001
112
May 28, 2006
May 28, 2006
113
114
115
116
117
118
119
120
/* Look for the XVideo extension with a valid port for this format */
xv_port = -1;
if ((Success ==
SDL_NAME (XvQueryExtension) (GFX_Display, &j, &j, &j, &j, &j))
&& (Success ==
SDL_NAME (XvQueryAdaptors) (GFX_Display,
RootWindow (GFX_Display, SDL_Screen),
&adaptors, &ainfo))) {
Sep 14, 2001
Sep 14, 2001
121
#ifdef USE_LAST_ADAPTOR
May 28, 2006
May 28, 2006
122
for (i = 0; i < adaptors; ++i)
Sep 14, 2001
Sep 14, 2001
123
#else
May 28, 2006
May 28, 2006
124
for (i = 0; (i < adaptors) && (xv_port == -1); ++i)
Sep 14, 2001
Sep 14, 2001
125
#endif /* USE_LAST_ADAPTOR */
May 28, 2006
May 28, 2006
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
/* 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;
SDL_NAME (XvImageFormatValues) * formats;
formats = SDL_NAME (XvListImageFormats) (GFX_Display,
ainfo[i].
base_id,
&num_formats);
Sep 14, 2001
Sep 14, 2001
148
#ifdef USE_LAST_ADAPTOR
May 28, 2006
May 28, 2006
149
for (j = 0; j < num_formats; ++j)
Sep 14, 2001
Sep 14, 2001
150
#else
May 28, 2006
May 28, 2006
151
for (j = 0; (j < num_formats) && (xv_port == -1); ++j)
Sep 14, 2001
Sep 14, 2001
152
#endif /* USE_LAST_ADAPTOR */
May 28, 2006
May 28, 2006
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
if ((Uint32) formats[j].id == format) {
for (k = 0; k < ainfo[i].num_ports; ++k) {
if (Success == SDL_NAME (XvGrabPort)
(GFX_Display,
ainfo[i].base_id + k, CurrentTime)) {
xv_port = ainfo[i].base_id + k;
break;
}
}
}
}
if (formats) {
XFree (formats);
}
}
}
SDL_NAME (XvFreeAdaptorInfo) (ainfo);
}
/* 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;
}
Feb 12, 2004
Feb 12, 2004
185
Aug 12, 2002
Aug 12, 2002
186
#if 0
Jul 30, 2002
Jul 30, 2002
187
188
189
190
191
/*
* !!! 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
192
193
194
195
*
* 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
196
*/
May 28, 2006
May 28, 2006
197
198
199
200
201
202
203
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
204
#endif
Jul 30, 2002
Jul 30, 2002
205
May 28, 2006
May 28, 2006
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
if (xv_port == -1) {
SDL_SetError ("No available video ports for requested format");
return (NULL);
}
/* 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);
X_handler = XSetErrorHandler (xv_errhandler);
for (i = 0; i < sizeof (attr) / (sizeof attr[0]); ++i) {
Atom a;
xv_error = False;
a = XInternAtom (GFX_Display, attr[i], True);
if (a != None) {
SDL_NAME (XvSetPortAttribute) (GFX_Display, xv_port, a, 1);
XSync (GFX_Display, True);
if (!xv_error) {
break;
}
}
}
XSetErrorHandler (X_handler);
SDL_NAME (XvSelectPortNotify) (GFX_Display, xv_port, False);
}
/* Create the overlay structure */
overlay = (SDL_Overlay *) SDL_malloc (sizeof *overlay);
if (overlay == NULL) {
SDL_NAME (XvUngrabPort) (GFX_Display, xv_port, CurrentTime);
SDL_OutOfMemory ();
return (NULL);
}
SDL_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 *) SDL_malloc (sizeof *hwdata);
overlay->hwdata = hwdata;
if (hwdata == NULL) {
SDL_NAME (XvUngrabPort) (GFX_Display, xv_port, CurrentTime);
SDL_OutOfMemory ();
SDL_FreeYUVOverlay (overlay);
return (NULL);
}
hwdata->port = xv_port;
Feb 12, 2004
Feb 12, 2004
264
#ifndef NO_SHARED_MEMORY
May 28, 2006
May 28, 2006
265
266
267
268
yuvshm = &hwdata->yuvshm;
SDL_memset (yuvshm, 0, sizeof (*yuvshm));
hwdata->image = SDL_NAME (XvShmCreateImage) (GFX_Display, xv_port, format,
0, width, height, yuvshm);
Apr 26, 2001
Apr 26, 2001
269
#ifdef PITCH_WORKAROUND
May 28, 2006
May 28, 2006
270
271
272
273
274
275
276
277
if (hwdata->image != NULL && hwdata->image->pitches[0] != (width * bpp)) {
/* Ajust overlay width according to pitch */
XFree (hwdata->image);
width = hwdata->image->pitches[0] / bpp;
hwdata->image =
SDL_NAME (XvShmCreateImage) (GFX_Display, xv_port, format, 0,
width, height, yuvshm);
}
Feb 12, 2004
Feb 12, 2004
278
#endif /* PITCH_WORKAROUND */
May 28, 2006
May 28, 2006
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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;
X_handler = XSetErrorHandler (shm_errhandler);
XShmAttach (GFX_Display, yuvshm);
XSync (GFX_Display, True);
XSetErrorHandler (X_handler);
if (shm_error)
shmdt (yuvshm->shmaddr);
} else {
shm_error = True;
}
shmctl (yuvshm->shmid, IPC_RMID, NULL);
} else {
shm_error = True;
}
if (shm_error) {
XFree (hwdata->image);
hwdata->yuv_use_mitshm = 0;
} else {
hwdata->image->data = yuvshm->shmaddr;
}
}
if (!hwdata->yuv_use_mitshm)
Feb 12, 2004
Feb 12, 2004
309
#endif /* NO_SHARED_MEMORY */
May 28, 2006
May 28, 2006
310
311
312
313
{
hwdata->image =
SDL_NAME (XvCreateImage) (GFX_Display, xv_port, format, 0,
width, height);
Apr 26, 2001
Apr 26, 2001
314
Feb 12, 2004
Feb 12, 2004
315
#ifdef PITCH_WORKAROUND
May 28, 2006
May 28, 2006
316
317
318
319
320
321
322
323
324
if (hwdata->image != NULL
&& hwdata->image->pitches[0] != (width * bpp)) {
/* Ajust overlay width according to pitch */
XFree (hwdata->image);
width = hwdata->image->pitches[0] / bpp;
hwdata->image =
SDL_NAME (XvCreateImage) (GFX_Display, xv_port, format, 0,
width, height);
}
Feb 12, 2004
Feb 12, 2004
325
#endif /* PITCH_WORKAROUND */
May 28, 2006
May 28, 2006
326
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
if (hwdata->image == NULL) {
SDL_SetError ("Couldn't create XVideo image");
SDL_FreeYUVOverlay (overlay);
return (NULL);
}
hwdata->image->data = SDL_malloc (hwdata->image->data_size);
if (hwdata->image->data == NULL) {
SDL_OutOfMemory ();
SDL_FreeYUVOverlay (overlay);
return (NULL);
}
}
/* Find the pitch and offset values for the overlay */
overlay->planes = hwdata->image->num_planes;
overlay->pitches =
(Uint16 *) SDL_malloc (overlay->planes * sizeof (Uint16));
overlay->pixels =
(Uint8 **) SDL_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];
}
Apr 26, 2001
Apr 26, 2001
355
356
#ifdef XFREE86_REFRESH_HACK
May 28, 2006
May 28, 2006
357
358
359
360
361
/* 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);
Apr 26, 2001
Apr 26, 2001
362
363
#endif
May 28, 2006
May 28, 2006
364
365
/* We're all done.. */
return (overlay);
Apr 26, 2001
Apr 26, 2001
366
367
}
May 28, 2006
May 28, 2006
368
369
int
X11_LockYUVOverlay (_THIS, SDL_Overlay * overlay)
Apr 26, 2001
Apr 26, 2001
370
{
May 28, 2006
May 28, 2006
371
return (0);
Apr 26, 2001
Apr 26, 2001
372
373
}
May 28, 2006
May 28, 2006
374
375
void
X11_UnlockYUVOverlay (_THIS, SDL_Overlay * overlay)
Apr 26, 2001
Apr 26, 2001
376
{
May 28, 2006
May 28, 2006
377
return;
Apr 26, 2001
Apr 26, 2001
378
379
}
May 28, 2006
May 28, 2006
380
381
382
int
X11_DisplayYUVOverlay (_THIS, SDL_Overlay * overlay, SDL_Rect * src,
SDL_Rect * dst)
Apr 26, 2001
Apr 26, 2001
383
{
May 28, 2006
May 28, 2006
384
struct private_yuvhwdata *hwdata;
Apr 26, 2001
Apr 26, 2001
385
May 28, 2006
May 28, 2006
386
hwdata = overlay->hwdata;
Apr 17, 2006
Apr 17, 2006
387
Feb 12, 2004
Feb 12, 2004
388
#ifndef NO_SHARED_MEMORY
May 28, 2006
May 28, 2006
389
390
391
392
393
394
if (hwdata->yuv_use_mitshm) {
SDL_NAME (XvShmPutImage) (GFX_Display, hwdata->port, SDL_Window,
SDL_GC, hwdata->image, src->x, src->y,
src->w, src->h, dst->x, dst->y, dst->w,
dst->h, False);
} else
Feb 12, 2004
Feb 12, 2004
395
#endif
May 28, 2006
May 28, 2006
396
397
398
399
400
401
402
403
{
SDL_NAME (XvPutImage) (GFX_Display, hwdata->port, SDL_Window,
SDL_GC, hwdata->image, src->x, src->y,
src->w, src->h, dst->x, dst->y, dst->w,
dst->h);
}
XSync (GFX_Display, False);
return (0);
Apr 26, 2001
Apr 26, 2001
404
405
}
May 28, 2006
May 28, 2006
406
407
void
X11_FreeYUVOverlay (_THIS, SDL_Overlay * overlay)
Apr 26, 2001
Apr 26, 2001
408
{
May 28, 2006
May 28, 2006
409
struct private_yuvhwdata *hwdata;
Apr 26, 2001
Apr 26, 2001
410
May 28, 2006
May 28, 2006
411
412
413
hwdata = overlay->hwdata;
if (hwdata) {
SDL_NAME (XvUngrabPort) (GFX_Display, hwdata->port, CurrentTime);
Feb 12, 2004
Feb 12, 2004
414
#ifndef NO_SHARED_MEMORY
May 28, 2006
May 28, 2006
415
416
417
418
if (hwdata->yuv_use_mitshm) {
XShmDetach (GFX_Display, &hwdata->yuvshm);
shmdt (hwdata->yuvshm.shmaddr);
}
Feb 12, 2004
Feb 12, 2004
419
#endif
May 28, 2006
May 28, 2006
420
421
422
423
424
425
426
427
428
429
430
431
432
if (hwdata->image) {
XFree (hwdata->image);
}
SDL_free (hwdata);
}
if (overlay->pitches) {
SDL_free (overlay->pitches);
overlay->pitches = NULL;
}
if (overlay->pixels) {
SDL_free (overlay->pixels);
overlay->pixels = NULL;
}
Apr 26, 2001
Apr 26, 2001
433
#ifdef XFREE86_REFRESH_HACK
May 28, 2006
May 28, 2006
434
X11_EnableAutoRefresh (this);
Apr 26, 2001
Apr 26, 2001
435
436
437
#endif
}
Feb 16, 2006
Feb 16, 2006
438
#endif /* SDL_VIDEO_DRIVER_X11_XV */
May 28, 2006
May 28, 2006
439
/* vi: set ts=4 sw=4 expandtab: */