author | Ryan C. Gordon <icculus@icculus.org> |
Tue, 30 Jul 2002 07:25:43 +0000 | |
changeset 429 | c1666427297c |
parent 410 | 365f57b7c4ac |
child 434 | ed58b98c0d9d |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
SDL - Simple DirectMedia Layer |
|
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
292
diff
changeset
|
3 |
Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga |
0 | 4 |
|
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Library General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2 of the License, or (at your option) any later version. |
|
9 |
||
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Library General Public License for more details. |
|
14 |
||
15 |
You should have received a copy of the GNU Library General Public |
|
16 |
License along with this library; if not, write to the Free |
|
17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 |
||
19 |
Sam Lantinga |
|
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
182
diff
changeset
|
20 |
slouken@libsdl.org |
0 | 21 |
*/ |
22 |
||
23 |
#ifdef SAVE_RCSID |
|
24 |
static char rcsid = |
|
25 |
"@(#) $Id$"; |
|
26 |
#endif |
|
27 |
||
28 |
/* This is the XFree86 Xv extension implementation of YUV video overlays */ |
|
29 |
||
30 |
#ifdef XFREE86_XV |
|
31 |
||
32 |
#include <stdlib.h> |
|
33 |
#include <string.h> |
|
34 |
#include <X11/Xlib.h> |
|
35 |
#include <sys/ipc.h> |
|
36 |
#include <sys/shm.h> |
|
37 |
#include <X11/extensions/XShm.h> |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
38 |
#include <XFree86/extensions/Xvlib.h> |
429
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
39 |
#include <X11/Xlibint.h> |
0 | 40 |
|
41 |
#include "SDL_error.h" |
|
42 |
#include "SDL_video.h" |
|
43 |
#include "SDL_x11yuv_c.h" |
|
44 |
#include "SDL_yuvfuncs.h" |
|
45 |
||
46 |
#define XFREE86_REFRESH_HACK |
|
47 |
#ifdef XFREE86_REFRESH_HACK |
|
48 |
#include "SDL_x11image_c.h" |
|
49 |
#endif |
|
50 |
||
51 |
/* Workaround when pitch != width */ |
|
52 |
#define PITCH_WORKAROUND |
|
53 |
||
182
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
54 |
/* Fix for the NVidia GeForce 2 - use the last available adaptor */ |
341
6ca967a46bf1
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
55 |
/*#define USE_LAST_ADAPTOR*/ /* Apparently the NVidia drivers are fixed */ |
182
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
56 |
|
0 | 57 |
/* The functions used to manipulate software video overlays */ |
58 |
static struct private_yuvhwfuncs x11_yuvfuncs = { |
|
59 |
X11_LockYUVOverlay, |
|
60 |
X11_UnlockYUVOverlay, |
|
61 |
X11_DisplayYUVOverlay, |
|
62 |
X11_FreeYUVOverlay |
|
63 |
}; |
|
64 |
||
65 |
struct private_yuvhwdata { |
|
66 |
int port; |
|
67 |
XShmSegmentInfo yuvshm; |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
68 |
SDL_NAME(XvImage) *image; |
0 | 69 |
}; |
70 |
||
71 |
||
72 |
SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display) |
|
73 |
{ |
|
74 |
SDL_Overlay *overlay; |
|
75 |
struct private_yuvhwdata *hwdata; |
|
76 |
int xv_port; |
|
77 |
int i, j, k; |
|
78 |
int adaptors; |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
79 |
SDL_NAME(XvAdaptorInfo) *ainfo; |
0 | 80 |
XShmSegmentInfo *yuvshm; |
81 |
||
82 |
/* Look for the XVideo extension with a valid port for this format */ |
|
83 |
xv_port = -1; |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
84 |
if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) && |
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
85 |
(Success == SDL_NAME(XvQueryAdaptors)(GFX_Display, |
0 | 86 |
RootWindow(GFX_Display, SDL_Screen), |
87 |
&adaptors, &ainfo)) ) { |
|
182
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
88 |
#ifdef USE_LAST_ADAPTOR |
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
89 |
for ( i=0; i < adaptors; ++i ) { |
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
90 |
#else |
0 | 91 |
for ( i=0; (i < adaptors) && (xv_port == -1); ++i ) { |
182
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
92 |
#endif /* USE_LAST_ADAPTOR */ |
0 | 93 |
/* Check to see if the visual can be used */ |
94 |
if ( BUGGY_XFREE86(<=, 4001) ) { |
|
95 |
int visual_ok = 0; |
|
96 |
for ( j=0; j<ainfo[i].num_formats; ++j ) { |
|
97 |
if ( ainfo[i].formats[j].visual_id == |
|
98 |
SDL_Visual->visualid ) { |
|
99 |
visual_ok = 1; |
|
100 |
break; |
|
101 |
} |
|
102 |
} |
|
103 |
if ( ! visual_ok ) { |
|
104 |
continue; |
|
105 |
} |
|
106 |
} |
|
107 |
if ( (ainfo[i].type & XvInputMask) && |
|
108 |
(ainfo[i].type & XvImageMask) ) { |
|
109 |
int num_formats; |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
110 |
SDL_NAME(XvImageFormatValues) *formats; |
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
111 |
formats = SDL_NAME(XvListImageFormats)(GFX_Display, |
0 | 112 |
ainfo[i].base_id, &num_formats); |
182
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
113 |
#ifdef USE_LAST_ADAPTOR |
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
114 |
for ( j=0; j < num_formats; ++j ) { |
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
115 |
#else |
0 | 116 |
for ( j=0; (j < num_formats) && (xv_port == -1); ++j ) { |
182
d4ebd1bbea9a
Fix XVideo on GeForce by using last available adaptor
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
117 |
#endif /* USE_LAST_ADAPTOR */ |
0 | 118 |
if ( (Uint32)formats[j].id == format ) { |
119 |
for ( k=0; k < ainfo[i].num_ports; ++k ) { |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
120 |
if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) { |
0 | 121 |
xv_port = ainfo[i].base_id+k; |
122 |
break; |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
410
365f57b7c4ac
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
409
diff
changeset
|
127 |
if ( formats ) { |
365f57b7c4ac
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
409
diff
changeset
|
128 |
XFree(formats); |
365f57b7c4ac
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
409
diff
changeset
|
129 |
} |
0 | 130 |
} |
131 |
} |
|
409
1c3cf7231cd4
Fixed memory leaks in YUV code
Sam Lantinga <slouken@libsdl.org>
parents:
341
diff
changeset
|
132 |
SDL_NAME(XvFreeAdaptorInfo)(ainfo); |
0 | 133 |
} |
429
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
134 |
|
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
135 |
/* |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
136 |
* !!! FIXME: |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
137 |
* "Here are some diffs for X11 and yuv. Note that the last part 2nd |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
138 |
* diff should probably be a new call to XvQueryAdaptorFree with ainfo |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
139 |
* and the number of adaptors, instead of the loop through like I did." |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
140 |
*/ |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
141 |
for ( i=0; i < adaptors; ++i ) { |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
142 |
if (ainfo[i].name != NULL) Xfree(ainfo[i].name); |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
143 |
if (ainfo[i].formats != NULL) Xfree(ainfo[i].formats); |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
144 |
} |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
145 |
Xfree(ainfo); |
c1666427297c
Memory leak fixes by Bill May.
Ryan C. Gordon <icculus@icculus.org>
parents:
410
diff
changeset
|
146 |
|
0 | 147 |
if ( xv_port == -1 ) { |
148 |
SDL_SetError("No available video ports for requested format"); |
|
149 |
return(NULL); |
|
150 |
} |
|
151 |
||
152 |
/* Create the overlay structure */ |
|
153 |
overlay = (SDL_Overlay *)malloc(sizeof *overlay); |
|
154 |
if ( overlay == NULL ) { |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
155 |
SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime); |
0 | 156 |
SDL_OutOfMemory(); |
157 |
return(NULL); |
|
158 |
} |
|
159 |
memset(overlay, 0, (sizeof *overlay)); |
|
160 |
||
161 |
/* Fill in the basic members */ |
|
162 |
overlay->format = format; |
|
163 |
overlay->w = width; |
|
164 |
overlay->h = height; |
|
165 |
||
166 |
/* Set up the YUV surface function structure */ |
|
167 |
overlay->hwfuncs = &x11_yuvfuncs; |
|
168 |
overlay->hw_overlay = 1; |
|
169 |
||
170 |
/* Create the pixel data and lookup tables */ |
|
171 |
hwdata = (struct private_yuvhwdata *)malloc(sizeof *hwdata); |
|
172 |
overlay->hwdata = hwdata; |
|
173 |
if ( hwdata == NULL ) { |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
174 |
SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime); |
0 | 175 |
SDL_OutOfMemory(); |
176 |
SDL_FreeYUVOverlay(overlay); |
|
177 |
return(NULL); |
|
178 |
} |
|
179 |
yuvshm = &hwdata->yuvshm; |
|
180 |
memset(yuvshm, 0, sizeof(*yuvshm)); |
|
181 |
hwdata->port = xv_port; |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
182 |
hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format, |
0 | 183 |
0, width, height, yuvshm); |
184 |
||
185 |
#ifdef PITCH_WORKAROUND |
|
186 |
if ( hwdata->image != NULL && hwdata->image->pitches[0] != width ) |
|
187 |
{ |
|
188 |
/* Ajust overlay width according to pitch */ |
|
189 |
switch (format) { |
|
190 |
case SDL_YV12_OVERLAY: |
|
191 |
case SDL_IYUV_OVERLAY: |
|
192 |
width = hwdata->image->pitches[0]; |
|
193 |
break; |
|
194 |
case SDL_YUY2_OVERLAY: |
|
195 |
case SDL_UYVY_OVERLAY: |
|
196 |
case SDL_YVYU_OVERLAY: |
|
197 |
width = hwdata->image->pitches[0] / 2; |
|
198 |
break; |
|
199 |
default: |
|
200 |
/* We should never get here (caught above) */ |
|
201 |
return(NULL); |
|
202 |
} |
|
203 |
||
204 |
XFree(hwdata->image); |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
205 |
hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format, |
0 | 206 |
0, width, height, yuvshm); |
207 |
} |
|
208 |
#endif |
|
209 |
||
210 |
if ( hwdata->image == NULL ) { |
|
211 |
SDL_OutOfMemory(); |
|
212 |
SDL_FreeYUVOverlay(overlay); |
|
213 |
return(NULL); |
|
214 |
} |
|
215 |
yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size, |
|
216 |
IPC_CREAT | 0777); |
|
217 |
if ( yuvshm->shmid < 0 ) { |
|
218 |
SDL_SetError("Unable to get %d bytes shared memory", |
|
219 |
hwdata->image->data_size); |
|
220 |
SDL_FreeYUVOverlay(overlay); |
|
221 |
return(NULL); |
|
222 |
} |
|
223 |
yuvshm->shmaddr = (char *) shmat(yuvshm->shmid, 0, 0); |
|
224 |
yuvshm->readOnly = False; |
|
225 |
hwdata->image->data = yuvshm->shmaddr; |
|
226 |
||
227 |
XShmAttach(GFX_Display, yuvshm); |
|
228 |
XSync(GFX_Display, False); |
|
229 |
shmctl(yuvshm->shmid, IPC_RMID, 0); |
|
230 |
||
231 |
/* Find the pitch and offset values for the overlay */ |
|
232 |
overlay->planes = hwdata->image->num_planes; |
|
233 |
overlay->pitches = (Uint16 *)malloc(overlay->planes * sizeof(Uint16)); |
|
234 |
overlay->pixels = (Uint8 **)malloc(overlay->planes * sizeof(Uint8 *)); |
|
235 |
if ( !overlay->pitches || !overlay->pixels ) { |
|
236 |
SDL_OutOfMemory(); |
|
237 |
SDL_FreeYUVOverlay(overlay); |
|
238 |
return(NULL); |
|
239 |
} |
|
240 |
for ( i=0; i<overlay->planes; ++i ) { |
|
241 |
overlay->pitches[i] = hwdata->image->pitches[i]; |
|
242 |
overlay->pixels[i] = (Uint8 *)hwdata->image->data + |
|
243 |
hwdata->image->offsets[i]; |
|
244 |
} |
|
245 |
||
246 |
#ifdef XFREE86_REFRESH_HACK |
|
247 |
/* Work around an XFree86 X server bug (?) |
|
248 |
We can't perform normal updates in windows that have video |
|
249 |
being output to them. See SDL_x11image.c for more details. |
|
250 |
*/ |
|
251 |
X11_DisableAutoRefresh(this); |
|
252 |
#endif |
|
253 |
||
254 |
/* We're all done.. */ |
|
255 |
return(overlay); |
|
256 |
} |
|
257 |
||
258 |
int X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay) |
|
259 |
{ |
|
260 |
return(0); |
|
261 |
} |
|
262 |
||
263 |
void X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay) |
|
264 |
{ |
|
265 |
return; |
|
266 |
} |
|
267 |
||
268 |
int X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect) |
|
269 |
{ |
|
270 |
struct private_yuvhwdata *hwdata; |
|
271 |
||
272 |
hwdata = overlay->hwdata; |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
273 |
SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC, |
0 | 274 |
hwdata->image, 0, 0, overlay->w, overlay->h, |
275 |
dstrect->x, dstrect->y, dstrect->w, dstrect->h, False); |
|
276 |
XSync(GFX_Display, False); |
|
277 |
return(0); |
|
278 |
} |
|
279 |
||
280 |
void X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay) |
|
281 |
{ |
|
282 |
struct private_yuvhwdata *hwdata; |
|
283 |
||
284 |
hwdata = overlay->hwdata; |
|
285 |
if ( hwdata ) { |
|
292
eadc0746dfaf
Added SDL_LockRect() and SDL_UnlockRect()
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
286 |
SDL_NAME(XvUngrabPort)(GFX_Display, hwdata->port, CurrentTime); |
0 | 287 |
if ( hwdata->yuvshm.shmaddr ) { |
288 |
XShmDetach(GFX_Display, &hwdata->yuvshm); |
|
289 |
shmdt(hwdata->yuvshm.shmaddr); |
|
290 |
} |
|
291 |
if ( hwdata->image ) { |
|
292 |
XFree(hwdata->image); |
|
293 |
} |
|
294 |
free(hwdata); |
|
295 |
} |
|
296 |
if ( overlay->pitches ) { |
|
297 |
free(overlay->pitches); |
|
298 |
overlay->pitches = NULL; |
|
299 |
} |
|
300 |
if ( overlay->pixels ) { |
|
301 |
free(overlay->pixels); |
|
302 |
overlay->pixels = NULL; |
|
303 |
} |
|
304 |
#ifdef XFREE86_REFRESH_HACK |
|
305 |
X11_EnableAutoRefresh(this); |
|
306 |
#endif |
|
307 |
} |
|
308 |
||
309 |
#endif /* XFREE86_XV */ |