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