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
36
45
48
53 int init_ret;
/* return code used to signal initialization errors */
57
65 };
66
68 {
70
72
74 SDL_FreeYUVOverlay(sdl->
overlay);
80 SDL_DestroyMutex(sdl->
mutex);
85
87 SDL_Quit();
88
89 return 0;
90 }
91
93 {
94 AVRational sar, dar;
/* sample and display aspect ratios */
99
100 /* compute overlay width and height from the codec context information */
103
104 /* we suppose the screen has a 1/1 sample aspect ratio */
106 /* fit in the window */
108 /* fit in width */
111 } else {
112 /* fit in height */
115 }
116 } else {
118 overlay_rect->w = encctx->
width;
120 } else {
121 overlay_rect->h = encctx->
height;
123 }
126 }
127
128 overlay_rect->x = (sdl->
window_width - overlay_rect->w) / 2;
129 overlay_rect->y = (sdl->
window_height - overlay_rect->h) / 2;
130 }
131
132 #define SDL_BASE_FLAGS (SDL_SWSURFACE|SDL_RESIZABLE)
133
135 {
141
142 /* initialization */
143 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
146 goto init_end;
147 }
148
151 24, flags);
155 goto init_end;
156 }
157
162 "SDL does not support an overlay with size of %dx%d pixels\n",
165 goto init_end;
166 }
167
172
173 init_end:
174 SDL_LockMutex(sdl->
mutex);
176 SDL_UnlockMutex(sdl->
mutex);
178
181
182 /* event loop */
184 int ret;
185 SDL_Event event;
186 SDL_PumpEvents();
187 ret = SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS);
188 if (ret < 0) {
190 continue;
191 }
192 if (ret == 0) {
193 SDL_Delay(10);
194 continue;
195 }
196
197 switch (event.type) {
198 case SDL_KEYDOWN:
199 switch (event.key.keysym.sym) {
200 case SDLK_ESCAPE:
201 case SDLK_q:
203 break;
204 }
205 break;
206 case SDL_QUIT:
208 break;
209
210 case SDL_VIDEORESIZE:
213
214 SDL_LockMutex(sdl->
mutex);
219 } else {
221 }
222 SDL_UnlockMutex(sdl->
mutex);
223 break;
224
225 default:
226 break;
227 }
228 }
229
230 return 0;
231 }
232
234 {
238 int i, ret;
239
244
245 if (SDL_WasInit(SDL_INIT_VIDEO)) {
247 "SDL video subsystem was already inited, aborting\n");
251 }
252
259 }
260
264 break;
265 }
266 }
267
270 "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
274 }
275
276 /* compute overlay width and height from the codec context information */
278
281 av_log(s,
AV_LOG_ERROR,
"Could not create SDL condition variable: %s\n", SDL_GetError());
284 }
285 sdl->
mutex = SDL_CreateMutex();
290 }
296 }
297
298 /* wait until the video system has been inited */
299 SDL_LockMutex(sdl->
mutex);
302 }
303 SDL_UnlockMutex(sdl->
mutex);
307 }
308 return 0;
309
312 return ret;
313 }
314
316 {
320 int linesize[4];
321 int i;
322
326 }
328
329 SDL_LockMutex(sdl->
mutex);
331 SDL_MapRGB(sdl->
surface->format, 0, 0, 0));
332 SDL_LockYUVOverlay(sdl->
overlay);
333 for (i = 0; i < 3; i++) {
334 sdl->
overlay->pixels [i] = data [i];
335 sdl->
overlay->pitches[i] = linesize[i];
336 }
338 SDL_UnlockYUVOverlay(sdl->
overlay);
339
343 SDL_UnlockMutex(sdl->
mutex);
344
345 return 0;
346 }
347
348 #define OFFSET(x) offsetof(SDLContext,x)
349
356 };
357
364 };
365
376 .priv_class = &sdl_class,
377 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
static enum AVPixelFormat pix_fmt
ptrdiff_t const GLvoid * data
#define LIBAVUTIL_VERSION_INT
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array...
static int sdl_write_header(AVFormatContext *s)
enum AVPixelFormat pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
SDL_Thread * event_thread
AVStream ** streams
A list of all streams in the file.
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Main libavdevice API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void compute_overlay_rect(AVFormatContext *s)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
static int sdl_write_trailer(AVFormatContext *s)
char filename[1024]
input or output filename
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int width
picture width / height.
static int write_trailer(AVFormatContext *s1)
int sdl_was_already_inited
int window_height
size of the window
enum AVMediaType codec_type
static int event_thread(void *arg)
char * av_strdup(const char *s)
Duplicate the string s.
main external API structure.
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
static const char * window_title
Describe the class of an AVClass context structure.
rational number numerator/denominator
static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
static const struct sdl_overlay_pix_fmt_entry sdl_overlay_pix_fmt_map[]
static const AVOption options[]
offset must point to two consecutive integers
AVOutputFormat ff_sdl_muxer
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
static const AVClass sdl_class
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
#define AVERROR_EXTERNAL
Generic error in an external library.
AVPixelFormat
Pixel format.
This structure stores compressed data.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)