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 "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "libavutil/pixdesc.h"
39 int window_width, window_height; /**< size of the window */
40 int window_fullscreen;
41 int overlay_width, overlay_height; /**< size of the video in the window */
42 int overlay_x, overlay_y;
44 int sdl_was_already_inited;
47 static const struct sdl_overlay_pix_fmt_entry {
48 enum AVPixelFormat pix_fmt; int overlay_fmt;
49 } sdl_overlay_pix_fmt_map[] = {
50 { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
51 { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
52 { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
53 { AV_PIX_FMT_NONE, 0 },
56 static int sdl_write_trailer(AVFormatContext *s)
58 SDLContext *sdl = s->priv_data;
60 av_freep(&sdl->window_title);
61 av_freep(&sdl->icon_title);
64 SDL_FreeYUVOverlay(sdl->overlay);
67 if (!sdl->sdl_was_already_inited)
73 static int sdl_write_header(AVFormatContext *s)
75 SDLContext *sdl = s->priv_data;
76 AVStream *st = s->streams[0];
77 AVCodecContext *encctx = st->codec;
78 AVRational sar, dar; /* sample and display aspect ratios */
80 int flags = SDL_SWSURFACE | sdl->window_fullscreen ? SDL_FULLSCREEN : 0;
82 if (!sdl->window_title)
83 sdl->window_title = av_strdup(s->filename);
85 sdl->icon_title = av_strdup(sdl->window_title);
87 if (SDL_WasInit(SDL_INIT_VIDEO)) {
88 av_log(s, AV_LOG_ERROR,
89 "SDL video subsystem was already inited, aborting\n");
90 sdl->sdl_was_already_inited = 1;
91 ret = AVERROR(EINVAL);
95 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
96 av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
97 ret = AVERROR(EINVAL);
101 if ( s->nb_streams > 1
102 || encctx->codec_type != AVMEDIA_TYPE_VIDEO
103 || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
104 av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
105 ret = AVERROR(EINVAL);
109 for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
110 if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
111 sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
116 if (!sdl->overlay_fmt) {
117 av_log(s, AV_LOG_ERROR,
118 "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
119 av_get_pix_fmt_name(encctx->pix_fmt));
120 ret = AVERROR(EINVAL);
124 /* compute overlay width and height from the codec context information */
125 sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
126 dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
128 /* we suppose the screen has a 1/1 sample aspect ratio */
129 if (sdl->window_width && sdl->window_height) {
130 /* fit in the window */
131 if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
133 sdl->overlay_width = sdl->window_width;
134 sdl->overlay_height = av_rescale(sdl->overlay_width, dar.den, dar.num);
137 sdl->overlay_height = sdl->window_height;
138 sdl->overlay_width = av_rescale(sdl->overlay_height, dar.num, dar.den);
141 if (sar.num > sar.den) {
142 sdl->overlay_width = encctx->width;
143 sdl->overlay_height = av_rescale(sdl->overlay_width, dar.den, dar.num);
145 sdl->overlay_height = encctx->height;
146 sdl->overlay_width = av_rescale(sdl->overlay_height, dar.num, dar.den);
148 sdl->window_width = sdl->overlay_width;
149 sdl->window_height = sdl->overlay_height;
151 sdl->overlay_x = (sdl->window_width - sdl->overlay_width ) / 2;
152 sdl->overlay_y = (sdl->window_height - sdl->overlay_height) / 2;
154 SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
155 sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
158 av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
159 ret = AVERROR(EINVAL);
163 sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
164 sdl->overlay_fmt, sdl->surface);
165 if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
166 av_log(s, AV_LOG_ERROR,
167 "SDL does not support an overlay with size of %dx%d pixels\n",
168 encctx->width, encctx->height);
169 ret = AVERROR(EINVAL);
173 av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d\n",
174 encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar.num, sar.den,
175 sdl->overlay_width, sdl->overlay_height);
179 sdl_write_trailer(s);
183 static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
185 SDLContext *sdl = s->priv_data;
186 AVCodecContext *encctx = s->streams[0]->codec;
187 SDL_Rect rect = { sdl->overlay_x, sdl->overlay_y, sdl->overlay_width, sdl->overlay_height };
191 avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
193 SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
194 SDL_MapRGB(sdl->surface->format, 0, 0, 0));
195 SDL_LockYUVOverlay(sdl->overlay);
196 for (i = 0; i < 3; i++) {
197 sdl->overlay->pixels [i] = pict.data [i];
198 sdl->overlay->pitches[i] = pict.linesize[i];
200 SDL_DisplayYUVOverlay(sdl->overlay, &rect);
201 SDL_UnlockYUVOverlay(sdl->overlay);
203 SDL_UpdateRect(sdl->surface, rect.x, rect.y, rect.w, rect.h);
208 #define OFFSET(x) offsetof(SDLContext,x)
210 static const AVOption options[] = {
211 { "window_title", "set SDL window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
212 { "icon_title", "set SDL iconified window title", OFFSET(icon_title) , AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
213 { "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,{.str=NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
214 { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT,{.i64=0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
218 static const AVClass sdl_class = {
219 .class_name = "sdl outdev",
220 .item_name = av_default_item_name,
222 .version = LIBAVUTIL_VERSION_INT,
225 AVOutputFormat ff_sdl_muxer = {
227 .long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
228 .priv_data_size = sizeof(SDLContext),
229 .audio_codec = AV_CODEC_ID_NONE,
230 .video_codec = AV_CODEC_ID_RAWVIDEO,
231 .write_header = sdl_write_header,
232 .write_packet = sdl_write_packet,
233 .write_trailer = sdl_write_trailer,
234 .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
235 .priv_class = &sdl_class,