FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sdl.c
Go to the documentation of this file.
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 
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/time.h"
34 #include "avdevice.h"
35 
36 typedef struct {
37  AVClass *class;
38  SDL_Surface *surface;
39  SDL_Overlay *overlay;
40  char *window_title;
41  char *icon_title;
42  int window_width, window_height; /**< size of the window */
44 
45  SDL_Rect overlay_rect;
47 
49  SDL_Thread *event_thread;
50  SDL_mutex *mutex;
51  SDL_cond *init_cond;
52  int init_ret; /* return code used to signal initialization errors */
53  int inited;
54  int quit;
55 } SDLContext;
56 
57 static const struct sdl_overlay_pix_fmt_entry {
60  { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
61  { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
62  { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
63  { AV_PIX_FMT_NONE, 0 },
64 };
65 
67 {
68  SDLContext *sdl = s->priv_data;
69 
70  sdl->quit = 1;
71 
72  if (sdl->overlay)
73  SDL_FreeYUVOverlay(sdl->overlay);
74  sdl->overlay = NULL;
75  if (sdl->event_thread)
76  SDL_WaitThread(sdl->event_thread, NULL);
77  sdl->event_thread = NULL;
78  if (sdl->mutex)
79  SDL_DestroyMutex(sdl->mutex);
80  sdl->mutex = NULL;
81  if (sdl->init_cond)
82  SDL_DestroyCond(sdl->init_cond);
83  sdl->init_cond = NULL;
84 
85  if (!sdl->sdl_was_already_inited)
86  SDL_Quit();
87 
88  return 0;
89 }
90 
92 {
93  AVRational sar, dar; /* sample and display aspect ratios */
94  SDLContext *sdl = s->priv_data;
95  AVStream *st = s->streams[0];
96  AVCodecContext *encctx = st->codec;
97  SDL_Rect *overlay_rect = &sdl->overlay_rect;
98 
99  /* compute overlay width and height from the codec context information */
100  sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
101  dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
102 
103  /* we suppose the screen has a 1/1 sample aspect ratio */
104  if (sdl->window_width && sdl->window_height) {
105  /* fit in the window */
106  if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
107  /* fit in width */
108  overlay_rect->w = sdl->window_width;
109  overlay_rect->h = av_rescale(overlay_rect->w, dar.den, dar.num);
110  } else {
111  /* fit in height */
112  overlay_rect->h = sdl->window_height;
113  overlay_rect->w = av_rescale(overlay_rect->h, dar.num, dar.den);
114  }
115  } else {
116  if (sar.num > sar.den) {
117  overlay_rect->w = encctx->width;
118  overlay_rect->h = av_rescale(overlay_rect->w, dar.den, dar.num);
119  } else {
120  overlay_rect->h = encctx->height;
121  overlay_rect->w = av_rescale(overlay_rect->h, dar.num, dar.den);
122  }
123  sdl->window_width = overlay_rect->w;
124  sdl->window_height = overlay_rect->h;
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 
133 static int event_thread(void *arg)
134 {
135  AVFormatContext *s = arg;
136  SDLContext *sdl = s->priv_data;
137  int flags = SDL_BASE_FLAGS | (sdl->window_fullscreen ? SDL_FULLSCREEN : 0);
138  AVStream *st = s->streams[0];
139  AVCodecContext *encctx = st->codec;
140 
141  /* initialization */
142  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
143  av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
144  sdl->init_ret = AVERROR(EINVAL);
145  goto init_end;
146  }
147 
148  SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
149  sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
150  24, flags);
151  if (!sdl->surface) {
152  av_log(sdl, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
153  sdl->init_ret = AVERROR(EINVAL);
154  goto init_end;
155  }
156 
157  sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
158  sdl->overlay_fmt, sdl->surface);
159  if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
160  av_log(s, AV_LOG_ERROR,
161  "SDL does not support an overlay with size of %dx%d pixels\n",
162  encctx->width, encctx->height);
163  sdl->init_ret = AVERROR(EINVAL);
164  goto init_end;
165  }
166 
167  sdl->init_ret = 0;
168  av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
169  encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt),
170  sdl->overlay_rect.w, sdl->overlay_rect.h);
171 
172 init_end:
173  SDL_LockMutex(sdl->mutex);
174  sdl->inited = 1;
175  SDL_UnlockMutex(sdl->mutex);
176  SDL_CondSignal(sdl->init_cond);
177 
178  if (sdl->init_ret < 0)
179  return sdl->init_ret;
180 
181  /* event loop */
182  while (!sdl->quit) {
183  int ret;
184  SDL_Event event;
185  SDL_PumpEvents();
186  ret = SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS);
187  if (ret < 0) {
188  av_log(s, AV_LOG_ERROR, "Error when getting SDL event: %s\n", SDL_GetError());
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:
201  sdl->quit = 1;
202  break;
203  }
204  break;
205  case SDL_QUIT:
206  sdl->quit = 1;
207  break;
208 
209  case SDL_VIDEORESIZE:
210  sdl->window_width = event.resize.w;
211  sdl->window_height = event.resize.h;
212 
213  SDL_LockMutex(sdl->mutex);
214  sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height, 24, SDL_BASE_FLAGS);
215  if (!sdl->surface) {
216  av_log(s, AV_LOG_ERROR, "Failed to set SDL video mode: %s\n", SDL_GetError());
217  sdl->quit = 1;
218  } else {
220  }
221  SDL_UnlockMutex(sdl->mutex);
222  break;
223 
224  default:
225  break;
226  }
227  }
228 
229  return 0;
230 }
231 
233 {
234  SDLContext *sdl = s->priv_data;
235  AVStream *st = s->streams[0];
236  AVCodecContext *encctx = st->codec;
237  int i, ret;
238 
239  if (!sdl->window_title)
240  sdl->window_title = av_strdup(s->filename);
241  if (!sdl->icon_title)
242  sdl->icon_title = av_strdup(sdl->window_title);
243 
244  if (SDL_WasInit(SDL_INIT_VIDEO)) {
245  av_log(s, AV_LOG_ERROR,
246  "SDL video subsystem was already inited, aborting\n");
247  sdl->sdl_was_already_inited = 1;
248  ret = AVERROR(EINVAL);
249  goto fail;
250  }
251 
252  if ( s->nb_streams > 1
253  || encctx->codec_type != AVMEDIA_TYPE_VIDEO
254  || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
255  av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
256  ret = AVERROR(EINVAL);
257  goto fail;
258  }
259 
260  for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
261  if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
263  break;
264  }
265  }
266 
267  if (!sdl->overlay_fmt) {
268  av_log(s, AV_LOG_ERROR,
269  "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
270  av_get_pix_fmt_name(encctx->pix_fmt));
271  ret = AVERROR(EINVAL);
272  goto fail;
273  }
274 
275  /* compute overlay width and height from the codec context information */
277 
278  sdl->init_cond = SDL_CreateCond();
279  if (!sdl->init_cond) {
280  av_log(s, AV_LOG_ERROR, "Could not create SDL condition variable: %s\n", SDL_GetError());
281  ret = AVERROR_EXTERNAL;
282  goto fail;
283  }
284  sdl->mutex = SDL_CreateMutex();
285  if (!sdl->mutex) {
286  av_log(s, AV_LOG_ERROR, "Could not create SDL mutex: %s\n", SDL_GetError());
287  ret = AVERROR_EXTERNAL;
288  goto fail;
289  }
290  sdl->event_thread = SDL_CreateThread(event_thread, s);
291  if (!sdl->event_thread) {
292  av_log(s, AV_LOG_ERROR, "Could not create SDL event thread: %s\n", SDL_GetError());
293  ret = AVERROR_EXTERNAL;
294  goto fail;
295  }
296 
297  /* wait until the video system has been inited */
298  SDL_LockMutex(sdl->mutex);
299  while (!sdl->inited) {
300  SDL_CondWait(sdl->init_cond, sdl->mutex);
301  }
302  SDL_UnlockMutex(sdl->mutex);
303  if (sdl->init_ret < 0) {
304  ret = sdl->init_ret;
305  goto fail;
306  }
307  return 0;
308 
309 fail:
311  return ret;
312 }
313 
315 {
316  SDLContext *sdl = s->priv_data;
317  AVCodecContext *encctx = s->streams[0]->codec;
318  AVPicture pict;
319  int i;
320 
321  if (sdl->quit) {
323  return AVERROR(EIO);
324  }
325  avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
326 
327  SDL_LockMutex(sdl->mutex);
328  SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
329  SDL_MapRGB(sdl->surface->format, 0, 0, 0));
330  SDL_LockYUVOverlay(sdl->overlay);
331  for (i = 0; i < 3; i++) {
332  sdl->overlay->pixels [i] = pict.data [i];
333  sdl->overlay->pitches[i] = pict.linesize[i];
334  }
335  SDL_DisplayYUVOverlay(sdl->overlay, &sdl->overlay_rect);
336  SDL_UnlockYUVOverlay(sdl->overlay);
337 
338  SDL_UpdateRect(sdl->surface,
339  sdl->overlay_rect.x, sdl->overlay_rect.y,
340  sdl->overlay_rect.w, sdl->overlay_rect.h);
341  SDL_UnlockMutex(sdl->mutex);
342 
343  return 0;
344 }
345 
346 #define OFFSET(x) offsetof(SDLContext,x)
347 
348 static const AVOption options[] = {
349  { "window_title", "set SDL window title", OFFSET(window_title), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
350  { "icon_title", "set SDL iconified window title", OFFSET(icon_title) , AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
351  { "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
352  { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
353  { NULL },
354 };
355 
356 static const AVClass sdl_class = {
357  .class_name = "sdl outdev",
358  .item_name = av_default_item_name,
359  .option = options,
360  .version = LIBAVUTIL_VERSION_INT,
362 };
363 
365  .name = "sdl",
366  .long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
367  .priv_data_size = sizeof(SDLContext),
368  .audio_codec = AV_CODEC_ID_NONE,
369  .video_codec = AV_CODEC_ID_RAWVIDEO,
374  .priv_class = &sdl_class,
375 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Definition: sdl.c:36
static enum AVPixelFormat pix_fmt
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3454
int quit
Definition: sdl.c:54
int overlay_fmt
Definition: sdl.c:58
AVOption.
Definition: opt.h:255
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:914
int num
numerator
Definition: rational.h:44
SDL_cond * init_cond
Definition: sdl.c:51
static int sdl_write_header(AVFormatContext *s)
Definition: sdl.c:232
enum AVPixelFormat pix_fmt
Definition: sdl.c:58
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
static AVPacket pkt
int init_ret
Definition: sdl.c:52
Picture data structure.
Definition: avcodec.h:3452
int avpicture_fill(AVPicture *picture, const uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
Setup the picture fields based on the specified image parameters and the provided image data buffer...
Definition: avpicture.c:34
SDL_Overlay * overlay
Definition: sdl.c:39
Format I/O context.
Definition: avformat.h:1272
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int window_fullscreen
Definition: sdl.c:43
AVOptions.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
Definition: avcodec.h:3453
SDL_Thread * event_thread
Definition: sdl.c:49
SDL_mutex * mutex
Definition: sdl.c:50
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
uint8_t * data
Definition: avcodec.h:1162
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
SDL_Rect overlay_rect
Definition: sdl.c:45
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:285
Main libavdevice API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void compute_overlay_rect(AVFormatContext *s)
Definition: sdl.c:91
Definition: sdl.c:57
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
const char * arg
Definition: jacosubdec.c:66
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1328
static int sdl_write_trailer(AVFormatContext *s)
Definition: sdl.c:66
char filename[1024]
input or output filename
Definition: avformat.h:1348
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:127
char * icon_title
Definition: sdl.c:41
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:513
int sdl_was_already_inited
Definition: sdl.c:48
int window_height
size of the window
Definition: sdl.c:42
int inited
Definition: sdl.c:53
#define OFFSET(x)
Definition: sdl.c:346
Stream structure.
Definition: avformat.h:842
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:472
int window_width
Definition: sdl.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1249
enum AVCodecID codec_id
Definition: avcodec.h:1258
static int event_thread(void *arg)
Definition: sdl.c:133
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
main external API structure.
Definition: avcodec.h:1241
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:64
static const char * window_title
Definition: ffplay.c:305
Describe the class of an AVClass context structure.
Definition: log.h:67
rational number numerator/denominator
Definition: rational.h:43
static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sdl.c:314
static const struct sdl_overlay_pix_fmt_entry sdl_overlay_pix_fmt_map[]
static const AVOption options[]
Definition: sdl.c:348
offset must point to two consecutive integers
Definition: opt.h:232
misc parsing utilities
#define SDL_BASE_FLAGS
Definition: sdl.c:131
static int flags
Definition: cpu.c:47
AVOutputFormat ff_sdl_muxer
Definition: sdl.c:364
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
int overlay_fmt
Definition: sdl.c:46
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
int den
denominator
Definition: rational.h:45
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
SDL_Surface * surface
Definition: sdl.c:38
char * window_title
Definition: sdl.c:40
void * priv_data
Format private data.
Definition: avformat.h:1300
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
static const AVClass sdl_class
Definition: sdl.c:356
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.
Definition: pixdesc.c:2011
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86