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