Browse code

lavdev: add SDL output device

Stefano Sabatini authored on 2011/05/27 08:34:35
Showing 7 changed files
... ...
@@ -19,6 +19,7 @@ version <next>:
19 19
 - 9bit and 10bit FFV1 encoding / decoding
20 20
 - split filter added
21 21
 - select filter added
22
+- sdl output device added
22 23
 
23 24
 
24 25
 version 0.7_beta1:
... ...
@@ -1471,6 +1471,7 @@ jack_indev_deps="jack_jack_h sem_timedwait"
1471 1471
 libdc1394_indev_deps="libdc1394"
1472 1472
 oss_indev_deps_any="soundcard_h sys_soundcard_h"
1473 1473
 oss_outdev_deps_any="soundcard_h sys_soundcard_h"
1474
+sdl_outdev_deps="sdl"
1474 1475
 sndio_indev_deps="sndio_h"
1475 1476
 sndio_outdev_deps="sndio_h"
1476 1477
 v4l_indev_deps="linux_videodev_h"
... ...
@@ -2959,6 +2960,7 @@ else
2959 2959
     check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
2960 2960
   fi
2961 2961
 fi
2962
+enabled sdl && add_cflags $sdl_cflags && add_extralibs $sdl_libs
2962 2963
 
2963 2964
 texi2html -version > /dev/null 2>&1 && enable texi2html || disable texi2html
2964 2965
 
... ...
@@ -26,6 +26,49 @@ ALSA (Advanced Linux Sound Architecture) output device.
26 26
 
27 27
 OSS (Open Sound System) output device.
28 28
 
29
+@section sdl
30
+
31
+SDL (Simple Directmedia Layer) output device.
32
+
33
+This output devices allows to show a video stream in an SDL
34
+window. Only one SDL window is allowed per application, so you can
35
+have only one instance of this output device in an application.
36
+
37
+To enable this output device you need libsdl installed on your system
38
+when configuring your build.
39
+
40
+For more information about SDL, check:
41
+@url{http://www.libsdl.org/}
42
+
43
+@subsection Options
44
+
45
+@table @option
46
+
47
+@item window_title
48
+Set the SDL window title, if not specified default to "SDL video
49
+outdev".
50
+
51
+@item icon_title
52
+Set the name of the iconified SDL window, if not specified it is set
53
+to the same value of @var{window_title}.
54
+
55
+@item window_size
56
+Set the SDL window size, can be a string of the form
57
+@var{width}x@var{height} or a video size abbreviation.
58
+If not specified it defaults to the size of the input video.
59
+@end table
60
+
61
+@subsection Examples
62
+
63
+The following command shows the @file{ffmpeg} output is an
64
+SDL window, forcing its size to the qcif format:
65
+@example
66
+ffmpeg -i INPUT -vcodec rawvideo -pix_fmt yuv420p -window_size qcif -f sdl none
67
+@end example
68
+
69
+Note that the name specified for the output device is ignored, so it
70
+can be set to an arbitrary value ("none" in the above example).
71
+
29 72
 @section sndio
30 73
 
31 74
 sndio audio output device.
... ...
@@ -21,6 +21,7 @@ OBJS-$(CONFIG_FBDEV_INDEV)               += fbdev.o
21 21
 OBJS-$(CONFIG_JACK_INDEV)                += jack_audio.o
22 22
 OBJS-$(CONFIG_OSS_INDEV)                 += oss_audio.o
23 23
 OBJS-$(CONFIG_OSS_OUTDEV)                += oss_audio.o
24
+OBJS-$(CONFIG_SDL_OUTDEV)                += sdl.o
24 25
 OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_common.o sndio_dec.o
25 26
 OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_common.o sndio_enc.o
26 27
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o
... ...
@@ -45,6 +45,7 @@ void avdevice_register_all(void)
45 45
     REGISTER_INDEV    (FBDEV, fbdev);
46 46
     REGISTER_INDEV    (JACK, jack);
47 47
     REGISTER_INOUTDEV (OSS, oss);
48
+    REGISTER_OUTDEV   (SDL, sdl);
48 49
     REGISTER_INOUTDEV (SNDIO, sndio);
49 50
     REGISTER_INDEV    (V4L2, v4l2);
50 51
 #if FF_API_V4L
... ...
@@ -23,7 +23,7 @@
23 23
 #include "libavformat/avformat.h"
24 24
 
25 25
 #define LIBAVDEVICE_VERSION_MAJOR 53
26
-#define LIBAVDEVICE_VERSION_MINOR  0
26
+#define LIBAVDEVICE_VERSION_MINOR  1
27 27
 #define LIBAVDEVICE_VERSION_MICRO  0
28 28
 
29 29
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
30 30
new file mode 100644
... ...
@@ -0,0 +1,228 @@
0
+/*
1
+ * Copyright (c) 2011 Stefano Sabatini
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * libSDL output device
23
+ */
24
+
25
+#include <SDL.h>
26
+#include "libavutil/avstring.h"
27
+#include "libavutil/opt.h"
28
+#include "libavutil/parseutils.h"
29
+#include "libavutil/pixdesc.h"
30
+#include "avdevice.h"
31
+
32
+typedef struct {
33
+    AVClass *class;
34
+    SDL_Surface *surface;
35
+    SDL_Overlay *overlay;
36
+    char *window_title;
37
+    char *icon_title;
38
+    char *window_size;
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
+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
+    av_freep(&sdl->window_size);
61
+
62
+    if (sdl->overlay) {
63
+        SDL_FreeYUVOverlay(sdl->overlay);
64
+        sdl->overlay = NULL;
65
+    }
66
+    if (!sdl->sdl_was_already_inited)
67
+        SDL_Quit();
68
+
69
+    return 0;
70
+}
71
+
72
+static int sdl_write_header(AVFormatContext *s)
73
+{
74
+    SDLContext *sdl = s->priv_data;
75
+    AVStream *st = s->streams[0];
76
+    AVCodecContext *encctx = st->codec;
77
+    float sar, dar; /* sample and display aspect ratios */
78
+    int i, ret;
79
+
80
+    if (!sdl->icon_title)
81
+        sdl->icon_title = av_strdup(sdl->window_title);
82
+
83
+    if (SDL_WasInit(SDL_INIT_VIDEO)) {
84
+        av_log(s, AV_LOG_ERROR,
85
+               "SDL video subsystem was already inited, aborting.\n");
86
+        sdl->sdl_was_already_inited = 1;
87
+        ret = AVERROR(EINVAL);
88
+        goto fail;
89
+    }
90
+
91
+    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
92
+        av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
93
+        ret = AVERROR(EINVAL);
94
+        goto fail;
95
+    }
96
+
97
+    if (   s->nb_streams > 1
98
+        || encctx->codec_type != AVMEDIA_TYPE_VIDEO
99
+        || encctx->codec_id   != CODEC_ID_RAWVIDEO) {
100
+        av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
101
+        ret = AVERROR(EINVAL);
102
+        goto fail;
103
+    }
104
+
105
+    for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != PIX_FMT_NONE; i++) {
106
+        if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
107
+            sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
108
+            break;
109
+        }
110
+    }
111
+
112
+    if (!sdl->overlay_fmt) {
113
+        av_log(s, AV_LOG_ERROR,
114
+               "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422.\n",
115
+               av_get_pix_fmt_name(encctx->pix_fmt));
116
+        ret = AVERROR(EINVAL);
117
+        goto fail;
118
+    }
119
+
120
+    if (sdl->window_size) {
121
+        if (av_parse_video_size(&sdl->window_width, &sdl->window_height,
122
+                                sdl->window_size) < 0) {
123
+            av_log(s, AV_LOG_ERROR, "Invalid window size '%s'\n", sdl->window_size);
124
+            ret = AVERROR(EINVAL);
125
+            goto fail;
126
+        }
127
+    }
128
+
129
+    /* compute overlay width and height from the codec context information */
130
+    sar = st->sample_aspect_ratio.num ? av_q2d(st->sample_aspect_ratio) : 1;
131
+    dar = sar * (float)encctx->width / (float)encctx->height;
132
+
133
+    /* we suppose the screen has a 1/1 sample aspect ratio */
134
+    sdl->overlay_height = encctx->height;
135
+    sdl->overlay_width = ((int)rint(sdl->overlay_height * dar));
136
+    if (sdl->overlay_width > encctx->width) {
137
+        sdl->overlay_width = encctx->width;
138
+        sdl->overlay_height = ((int)rint(sdl->overlay_width / dar));
139
+    }
140
+
141
+    if (!sdl->window_width || !sdl->window_height) {
142
+        sdl->window_width  = sdl->overlay_width;
143
+        sdl->window_height = sdl->overlay_height;
144
+    }
145
+
146
+    SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
147
+    sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
148
+                                    24, SDL_SWSURFACE);
149
+    if (!sdl->surface) {
150
+        av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
151
+        ret = AVERROR(EINVAL);
152
+        goto fail;
153
+    }
154
+
155
+    sdl->overlay = SDL_CreateYUVOverlay(sdl->overlay_width, sdl->overlay_height,
156
+                                        sdl->overlay_fmt, sdl->surface);
157
+    if (!sdl->overlay || sdl->overlay->pitches[0] < sdl->overlay_width) {
158
+        av_log(s, AV_LOG_ERROR,
159
+               "SDL does not support an overlay with size of %dx%d pixels.\n",
160
+               sdl->overlay_width, sdl->overlay_height);
161
+        ret = AVERROR(EINVAL);
162
+        goto fail;
163
+    }
164
+
165
+    av_log(s, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%f -> w:%d h:%d\n",
166
+           encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar,
167
+           sdl->window_width, sdl->window_height);
168
+    return 0;
169
+
170
+fail:
171
+    sdl_write_trailer(s);
172
+    return ret;
173
+}
174
+
175
+static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
176
+{
177
+    SDLContext *sdl = s->priv_data;
178
+    AVCodecContext *encctx = s->streams[0]->codec;
179
+    SDL_Rect rect = { 0, 0, sdl->window_width, sdl->window_height };
180
+    AVPicture pict;
181
+    int i;
182
+
183
+    avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
184
+
185
+    SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
186
+                 SDL_MapRGB(sdl->surface->format, 0, 0, 0));
187
+    SDL_LockYUVOverlay(sdl->overlay);
188
+    for (i = 0; i < 3; i++) {
189
+        sdl->overlay->pixels [i] = pict.data    [i];
190
+        sdl->overlay->pitches[i] = pict.linesize[i];
191
+    }
192
+    SDL_DisplayYUVOverlay(sdl->overlay, &rect);
193
+    SDL_UnlockYUVOverlay(sdl->overlay);
194
+
195
+    SDL_UpdateRect(sdl->surface, 0, 0, sdl->overlay_width, sdl->overlay_height);
196
+
197
+    return 0;
198
+}
199
+
200
+#define OFFSET(x) offsetof(SDLContext,x)
201
+
202
+static const AVOption options[] = {
203
+    { "window_title", "SDL window title",           OFFSET(window_title),  FF_OPT_TYPE_STRING, {.str = "SDL video outdev" }, 0,  0, AV_OPT_FLAG_ENCODING_PARAM },
204
+    { "icon_title",   "SDL iconified window title", OFFSET(icon_title)  ,  FF_OPT_TYPE_STRING, {.str = NULL },               0,  0, AV_OPT_FLAG_ENCODING_PARAM },
205
+    { "window_size",  "SDL window forced size",     OFFSET(window_size) ,  FF_OPT_TYPE_STRING, {.str = NULL },               0,  0, AV_OPT_FLAG_ENCODING_PARAM },
206
+    { NULL },
207
+};
208
+
209
+static const AVClass sdl_class = {
210
+    .class_name = "sdl outdev",
211
+    .item_name  = av_default_item_name,
212
+    .option     = options,
213
+    .version    = LIBAVUTIL_VERSION_INT,
214
+};
215
+
216
+AVOutputFormat ff_sdl_muxer = {
217
+    .name           = "sdl",
218
+    .long_name      = NULL_IF_CONFIG_SMALL("SDL output device"),
219
+    .priv_data_size = sizeof(SDLContext),
220
+    .audio_codec    = CODEC_ID_NONE,
221
+    .video_codec    = CODEC_ID_RAWVIDEO,
222
+    .write_header   = sdl_write_header,
223
+    .write_packet   = sdl_write_packet,
224
+    .write_trailer  = sdl_write_trailer,
225
+    .flags          = AVFMT_NOFILE,
226
+    .priv_class     = &sdl_class,
227
+};