1 /*
2 * Copyright (c) 2011 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * libSDL output device
24 */
25
26 #include <SDL.h>
27 #include <SDL_thread.h>
28
35
44
47
52 int init_ret;
/* return code used to signal initialization errors */
56
64 };
65
67 {
69
71
73 SDL_FreeYUVOverlay(sdl->
overlay);
79 SDL_DestroyMutex(sdl->
mutex);
84
86 SDL_Quit();
87
88 return 0;
89 }
90
92 {
93 AVRational sar, dar;
/* sample and display aspect ratios */
98
99 /* compute overlay width and height from the codec context information */
102
103 /* we suppose the screen has a 1/1 sample aspect ratio */
105 /* fit in the window */
107 /* fit in width */
110 } else {
111 /* fit in height */
114 }
115 } else {
117 overlay_rect->w = encctx->
width;
119 } else {
120 overlay_rect->h = encctx->
height;
122 }
125 }
126
127 overlay_rect->x = (sdl->
window_width - overlay_rect->w) / 2;
128 overlay_rect->y = (sdl->
window_height - overlay_rect->h) / 2;
129 }
130
131 #define SDL_BASE_FLAGS (SDL_SWSURFACE|SDL_RESIZABLE)
132
134 {
140
141 /* initialization */
142 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
145 goto init_end;
146 }
147
150 24, flags);
154 goto init_end;
155 }
156
161 "SDL does not support an overlay with size of %dx%d pixels\n",
164 goto init_end;
165 }
166
171
172 init_end:
173 SDL_LockMutex(sdl->
mutex);
175 SDL_UnlockMutex(sdl->
mutex);
177
180
181 /* event loop */
184 SDL_Event event;
185 SDL_PumpEvents();
186 ret = SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS);
187 if (ret < 0) {
189 continue;
190 }
191 if (ret == 0) {
192 SDL_Delay(10);
193 continue;
194 }
195
196 switch (event.type) {
197 case SDL_KEYDOWN:
198 switch (event.key.keysym.sym) {
199 case SDLK_ESCAPE:
200 case SDLK_q:
202 break;
203 }
204 break;
205 case SDL_QUIT:
207 break;
208
209 case SDL_VIDEORESIZE:
212
213 SDL_LockMutex(sdl->
mutex);
218 } else {
220 }
221 SDL_UnlockMutex(sdl->
mutex);
222 break;
223
224 default:
225 break;
226 }
227 }
228
229 return 0;
230 }
231
233 {
238
243
244 if (SDL_WasInit(SDL_INIT_VIDEO)) {
246 "SDL video subsystem was already inited, aborting\n");
249 goto fail;
250 }
251
257 goto fail;
258 }
259
263 break;
264 }
265 }
266
269 "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
272 goto fail;
273 }
274
275 /* compute overlay width and height from the codec context information */
277
280 av_log(s,
AV_LOG_ERROR,
"Could not create SDL condition variable: %s\n", SDL_GetError());
282 goto fail;
283 }
284 sdl->
mutex = SDL_CreateMutex();
288 goto fail;
289 }
294 goto fail;
295 }
296
297 /* wait until the video system has been inited */
298 SDL_LockMutex(sdl->
mutex);
301 }
302 SDL_UnlockMutex(sdl->
mutex);
305 goto fail;
306 }
307 return 0;
308
309 fail:
312 }
313
315 {
319 int i;
320
324 }
326
327 SDL_LockMutex(sdl->
mutex);
329 SDL_MapRGB(sdl->
surface->format, 0, 0, 0));
330 SDL_LockYUVOverlay(sdl->
overlay);
331 for (i = 0; i < 3; i++) {
334 }
336 SDL_UnlockYUVOverlay(sdl->
overlay);
337
341 SDL_UnlockMutex(sdl->
mutex);
342
343 return 0;
344 }
345
346 #define OFFSET(x) offsetof(SDLContext,x)
347
354 };
355
362 };
363
374 .priv_class = &sdl_class,
375 };