2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * libSDL output device
27 #include <SDL_thread.h>
29 #include "libavutil/avstring.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/time.h"
43 int window_width, window_height; /**< size of the window */
44 int window_fullscreen;
46 SDL_Rect overlay_rect;
49 int sdl_was_already_inited;
50 SDL_Thread *event_thread;
53 int init_ret; /* return code used to signal initialization errors */
58 static const struct sdl_overlay_pix_fmt_entry {
59 enum AVPixelFormat pix_fmt; int overlay_fmt;
60 } sdl_overlay_pix_fmt_map[] = {
61 { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
62 { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
63 { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
64 { AV_PIX_FMT_NONE, 0 },
67 static int sdl_write_trailer(AVFormatContext *s)
69 SDLContext *sdl = s->priv_data;
74 SDL_FreeYUVOverlay(sdl->overlay);
76 if (sdl->event_thread)
77 SDL_WaitThread(sdl->event_thread, NULL);
78 sdl->event_thread = NULL;
80 SDL_DestroyMutex(sdl->mutex);
83 SDL_DestroyCond(sdl->init_cond);
84 sdl->init_cond = NULL;
86 if (!sdl->sdl_was_already_inited)
92 static void compute_overlay_rect(AVFormatContext *s)
94 AVRational sar, dar; /* sample and display aspect ratios */
95 SDLContext *sdl = s->priv_data;
96 AVStream *st = s->streams[0];
97 AVCodecContext *encctx = st->codec;
98 SDL_Rect *overlay_rect = &sdl->overlay_rect;
100 /* compute overlay width and height from the codec context information */
101 sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
102 dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
104 /* we suppose the screen has a 1/1 sample aspect ratio */
105 if (sdl->window_width && sdl->window_height) {
106 /* fit in the window */
107 if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
109 overlay_rect->w = sdl->window_width;
110 overlay_rect->h = av_rescale(overlay_rect->w, dar.den, dar.num);
113 overlay_rect->h = sdl->window_height;
114 overlay_rect->w = av_rescale(overlay_rect->h, dar.num, dar.den);
117 if (sar.num > sar.den) {
118 overlay_rect->w = encctx->width;
119 overlay_rect->h = av_rescale(overlay_rect->w, dar.den, dar.num);
121 overlay_rect->h = encctx->height;
122 overlay_rect->w = av_rescale(overlay_rect->h, dar.num, dar.den);
124 sdl->window_width = overlay_rect->w;
125 sdl->window_height = overlay_rect->h;
128 overlay_rect->x = (sdl->window_width - overlay_rect->w) / 2;
129 overlay_rect->y = (sdl->window_height - overlay_rect->h) / 2;
132 #define SDL_BASE_FLAGS (SDL_SWSURFACE|SDL_RESIZABLE)
134 static int event_thread(void *arg)
136 AVFormatContext *s = arg;
137 SDLContext *sdl = s->priv_data;
138 int flags = SDL_BASE_FLAGS | (sdl->window_fullscreen ? SDL_FULLSCREEN : 0);
139 AVStream *st = s->streams[0];
140 AVCodecContext *encctx = st->codec;
143 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
144 av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
145 sdl->init_ret = AVERROR(EINVAL);
149 SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
150 sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
153 av_log(sdl, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
154 sdl->init_ret = AVERROR(EINVAL);
158 sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
159 sdl->overlay_fmt, sdl->surface);
160 if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
161 av_log(s, AV_LOG_ERROR,
162 "SDL does not support an overlay with size of %dx%d pixels\n",
163 encctx->width, encctx->height);
164 sdl->init_ret = AVERROR(EINVAL);
169 av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
170 encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt),
171 sdl->overlay_rect.w, sdl->overlay_rect.h);
174 SDL_LockMutex(sdl->mutex);
176 SDL_UnlockMutex(sdl->mutex);
177 SDL_CondSignal(sdl->init_cond);
179 if (sdl->init_ret < 0)
180 return sdl->init_ret;
187 ret = SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS);
189 av_log(s, AV_LOG_ERROR, "Error when getting SDL event: %s\n", SDL_GetError());
197 switch (event.type) {
199 switch (event.key.keysym.sym) {
210 case SDL_VIDEORESIZE:
211 sdl->window_width = event.resize.w;
212 sdl->window_height = event.resize.h;
214 SDL_LockMutex(sdl->mutex);
215 sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height, 24, SDL_BASE_FLAGS);
217 av_log(s, AV_LOG_ERROR, "Failed to set SDL video mode: %s\n", SDL_GetError());
220 compute_overlay_rect(s);
222 SDL_UnlockMutex(sdl->mutex);
233 static int sdl_write_header(AVFormatContext *s)
235 SDLContext *sdl = s->priv_data;
236 AVStream *st = s->streams[0];
237 AVCodecContext *encctx = st->codec;
240 if (!sdl->window_title)
241 sdl->window_title = av_strdup(s->filename);
242 if (!sdl->icon_title)
243 sdl->icon_title = av_strdup(sdl->window_title);
245 if (SDL_WasInit(SDL_INIT_VIDEO)) {
246 av_log(s, AV_LOG_ERROR,
247 "SDL video subsystem was already inited, aborting\n");
248 sdl->sdl_was_already_inited = 1;
249 ret = AVERROR(EINVAL);
253 if ( s->nb_streams > 1
254 || encctx->codec_type != AVMEDIA_TYPE_VIDEO
255 || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
256 av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
257 ret = AVERROR(EINVAL);
261 for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
262 if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
263 sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
268 if (!sdl->overlay_fmt) {
269 av_log(s, AV_LOG_ERROR,
270 "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
271 av_get_pix_fmt_name(encctx->pix_fmt));
272 ret = AVERROR(EINVAL);
276 /* compute overlay width and height from the codec context information */
277 compute_overlay_rect(s);
279 sdl->init_cond = SDL_CreateCond();
280 if (!sdl->init_cond) {
281 av_log(s, AV_LOG_ERROR, "Could not create SDL condition variable: %s\n", SDL_GetError());
282 ret = AVERROR_EXTERNAL;
285 sdl->mutex = SDL_CreateMutex();
287 av_log(s, AV_LOG_ERROR, "Could not create SDL mutex: %s\n", SDL_GetError());
288 ret = AVERROR_EXTERNAL;
291 sdl->event_thread = SDL_CreateThread(event_thread, s);
292 if (!sdl->event_thread) {
293 av_log(s, AV_LOG_ERROR, "Could not create SDL event thread: %s\n", SDL_GetError());
294 ret = AVERROR_EXTERNAL;
298 /* wait until the video system has been inited */
299 SDL_LockMutex(sdl->mutex);
300 while (!sdl->inited) {
301 SDL_CondWait(sdl->init_cond, sdl->mutex);
303 SDL_UnlockMutex(sdl->mutex);
304 if (sdl->init_ret < 0) {
311 sdl_write_trailer(s);
315 static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
317 SDLContext *sdl = s->priv_data;
318 AVCodecContext *encctx = s->streams[0]->codec;
324 sdl_write_trailer(s);
327 av_image_fill_arrays(data, linesize, pkt->data, encctx->pix_fmt, encctx->width, encctx->height, 1);
329 SDL_LockMutex(sdl->mutex);
330 SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
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];
337 SDL_DisplayYUVOverlay(sdl->overlay, &sdl->overlay_rect);
338 SDL_UnlockYUVOverlay(sdl->overlay);
340 SDL_UpdateRect(sdl->surface,
341 sdl->overlay_rect.x, sdl->overlay_rect.y,
342 sdl->overlay_rect.w, sdl->overlay_rect.h);
343 SDL_UnlockMutex(sdl->mutex);
348 #define OFFSET(x) offsetof(SDLContext,x)
350 static const AVOption options[] = {
351 { "window_title", "set SDL window title", OFFSET(window_title), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
352 { "icon_title", "set SDL iconified window title", OFFSET(icon_title) , AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
353 { "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
354 { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
358 static const AVClass sdl_class = {
359 .class_name = "sdl outdev",
360 .item_name = av_default_item_name,
362 .version = LIBAVUTIL_VERSION_INT,
363 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
366 AVOutputFormat ff_sdl_muxer = {
368 .long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
369 .priv_data_size = sizeof(SDLContext),
370 .audio_codec = AV_CODEC_ID_NONE,
371 .video_codec = AV_CODEC_ID_RAWVIDEO,
372 .write_header = sdl_write_header,
373 .write_packet = sdl_write_packet,
374 .write_trailer = sdl_write_trailer,
375 .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
376 .priv_class = &sdl_class,