]> git.sesse.net Git - ffmpeg/blob - libavdevice/sdl.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavdevice / sdl.c
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 "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "libavutil/pixdesc.h"
31 #include "avdevice.h"
32
33 typedef struct {
34     AVClass *class;
35     SDL_Surface *surface;
36     SDL_Overlay *overlay;
37     char *window_title;
38     char *icon_title;
39     int window_width, window_height;
40     int overlay_width, overlay_height;
41     int overlay_fmt;
42     int sdl_was_already_inited;
43 } SDLContext;
44
45 static const struct sdl_overlay_pix_fmt_entry {
46     enum PixelFormat pix_fmt; int overlay_fmt;
47 } sdl_overlay_pix_fmt_map[] = {
48     { PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
49     { PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
50     { PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
51     { PIX_FMT_NONE,    0                },
52 };
53
54 static int sdl_write_trailer(AVFormatContext *s)
55 {
56     SDLContext *sdl = s->priv_data;
57
58     av_freep(&sdl->window_title);
59     av_freep(&sdl->icon_title);
60
61     if (sdl->overlay) {
62         SDL_FreeYUVOverlay(sdl->overlay);
63         sdl->overlay = NULL;
64     }
65     if (!sdl->sdl_was_already_inited)
66         SDL_Quit();
67
68     return 0;
69 }
70
71 static int sdl_write_header(AVFormatContext *s)
72 {
73     SDLContext *sdl = s->priv_data;
74     AVStream *st = s->streams[0];
75     AVCodecContext *encctx = st->codec;
76     float sar, dar; /* sample and display aspect ratios */
77     int i, ret;
78
79     if (!sdl->window_title)
80         sdl->window_title = av_strdup(s->filename);
81     if (!sdl->icon_title)
82         sdl->icon_title = av_strdup(sdl->window_title);
83
84     if (SDL_WasInit(SDL_INIT_VIDEO)) {
85         av_log(s, AV_LOG_ERROR,
86                "SDL video subsystem was already inited, aborting.\n");
87         sdl->sdl_was_already_inited = 1;
88         ret = AVERROR(EINVAL);
89         goto fail;
90     }
91
92     if (SDL_Init(SDL_INIT_VIDEO) != 0) {
93         av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
94         ret = AVERROR(EINVAL);
95         goto fail;
96     }
97
98     if (   s->nb_streams > 1
99         || encctx->codec_type != AVMEDIA_TYPE_VIDEO
100         || encctx->codec_id   != CODEC_ID_RAWVIDEO) {
101         av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
102         ret = AVERROR(EINVAL);
103         goto fail;
104     }
105
106     for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != PIX_FMT_NONE; i++) {
107         if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
108             sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
109             break;
110         }
111     }
112
113     if (!sdl->overlay_fmt) {
114         av_log(s, AV_LOG_ERROR,
115                "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422.\n",
116                av_get_pix_fmt_name(encctx->pix_fmt));
117         ret = AVERROR(EINVAL);
118         goto fail;
119     }
120
121     /* compute overlay width and height from the codec context information */
122     sar = st->sample_aspect_ratio.num ? av_q2d(st->sample_aspect_ratio) : 1;
123     dar = sar * (float)encctx->width / (float)encctx->height;
124
125     /* we suppose the screen has a 1/1 sample aspect ratio */
126     sdl->overlay_height = encctx->height;
127     sdl->overlay_width = ((int)rint(sdl->overlay_height * dar));
128     if (sdl->overlay_width > encctx->width) {
129         sdl->overlay_width = encctx->width;
130         sdl->overlay_height = ((int)rint(sdl->overlay_width / dar));
131     }
132
133     if (!sdl->window_width || !sdl->window_height) {
134         sdl->window_width  = sdl->overlay_width;
135         sdl->window_height = sdl->overlay_height;
136     }
137
138     SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
139     sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
140                                     24, SDL_SWSURFACE);
141     if (!sdl->surface) {
142         av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
143         ret = AVERROR(EINVAL);
144         goto fail;
145     }
146
147     sdl->overlay = SDL_CreateYUVOverlay(sdl->overlay_width, sdl->overlay_height,
148                                         sdl->overlay_fmt, sdl->surface);
149     if (!sdl->overlay || sdl->overlay->pitches[0] < sdl->overlay_width) {
150         av_log(s, AV_LOG_ERROR,
151                "SDL does not support an overlay with size of %dx%d pixels.\n",
152                sdl->overlay_width, sdl->overlay_height);
153         ret = AVERROR(EINVAL);
154         goto fail;
155     }
156
157     av_log(s, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%f -> w:%d h:%d\n",
158            encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar,
159            sdl->window_width, sdl->window_height);
160     return 0;
161
162 fail:
163     sdl_write_trailer(s);
164     return ret;
165 }
166
167 static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
168 {
169     SDLContext *sdl = s->priv_data;
170     AVCodecContext *encctx = s->streams[0]->codec;
171     SDL_Rect rect = { 0, 0, sdl->window_width, sdl->window_height };
172     AVPicture pict;
173     int i;
174
175     avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
176
177     SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
178                  SDL_MapRGB(sdl->surface->format, 0, 0, 0));
179     SDL_LockYUVOverlay(sdl->overlay);
180     for (i = 0; i < 3; i++) {
181         sdl->overlay->pixels [i] = pict.data    [i];
182         sdl->overlay->pitches[i] = pict.linesize[i];
183     }
184     SDL_DisplayYUVOverlay(sdl->overlay, &rect);
185     SDL_UnlockYUVOverlay(sdl->overlay);
186
187     SDL_UpdateRect(sdl->surface, 0, 0, sdl->overlay_width, sdl->overlay_height);
188
189     return 0;
190 }
191
192 #define OFFSET(x) offsetof(SDLContext,x)
193
194 static const AVOption options[] = {
195     { "window_title", "SDL window title",           OFFSET(window_title),  AV_OPT_TYPE_STRING, {.str = NULL }, 0,  0, AV_OPT_FLAG_ENCODING_PARAM },
196     { "icon_title",   "SDL iconified window title", OFFSET(icon_title)  ,  AV_OPT_TYPE_STRING, {.str = NULL }, 0,  0, AV_OPT_FLAG_ENCODING_PARAM },
197     { "window_size",  "SDL window forced size",     OFFSET(window_width),  AV_OPT_TYPE_IMAGE_SIZE,{.str=NULL}, 0,  0, AV_OPT_FLAG_ENCODING_PARAM },
198     { NULL },
199 };
200
201 static const AVClass sdl_class = {
202     .class_name = "sdl outdev",
203     .item_name  = av_default_item_name,
204     .option     = options,
205     .version    = LIBAVUTIL_VERSION_INT,
206 };
207
208 AVOutputFormat ff_sdl_muxer = {
209     .name           = "sdl",
210     .long_name      = NULL_IF_CONFIG_SMALL("SDL output device"),
211     .priv_data_size = sizeof(SDLContext),
212     .audio_codec    = CODEC_ID_NONE,
213     .video_codec    = CODEC_ID_RAWVIDEO,
214     .write_header   = sdl_write_header,
215     .write_packet   = sdl_write_packet,
216     .write_trailer  = sdl_write_trailer,
217     .flags          = AVFMT_NOFILE,
218     .priv_class     = &sdl_class,
219 };