[FFmpeg-devel] [PATCH] lavdev: add SDL output device

Stefano Sabatini stefano.sabatini-lala at poste.it
Fri May 27 11:57:18 CEST 2011


---
 Makefile                 |    2 -
 configure                |    2 +
 doc/outdevs.texi         |   40 +++++++++
 libavdevice/Makefile     |    1 +
 libavdevice/alldevices.c |    1 +
 libavdevice/sdl.c        |  217 ++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 261 insertions(+), 2 deletions(-)
 create mode 100644 libavdevice/sdl.c

diff --git a/Makefile b/Makefile
index 28f96ac..5f783ee 100644
--- a/Makefile
+++ b/Makefile
@@ -95,8 +95,6 @@ tools/%.o: tools/%.c
 -include $(wildcard tools/*.d)
 -include $(wildcard tests/*.d)
 
-ffplay.o: CFLAGS += $(SDL_CFLAGS)
-
 VERSION_SH  = $(SRC_PATH_BARE)/version.sh
 GIT_LOG     = $(SRC_PATH_BARE)/.git/logs/HEAD
 
diff --git a/configure b/configure
index c54706a..7e2ed66 100755
--- a/configure
+++ b/configure
@@ -1471,6 +1471,7 @@ jack_indev_deps="jack_jack_h sem_timedwait"
 libdc1394_indev_deps="libdc1394"
 oss_indev_deps_any="soundcard_h sys_soundcard_h"
 oss_outdev_deps_any="soundcard_h sys_soundcard_h"
+sdl_outdev_deps="sdl"
 sndio_indev_deps="sndio_h"
 sndio_outdev_deps="sndio_h"
 v4l_indev_deps="linux_videodev_h"
@@ -2966,6 +2967,7 @@ else
     check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
   fi
 fi
+enabled sdl && add_cflags $sdl_cflags && add_extralibs $sdl_libs
 
 texi2html -version > /dev/null 2>&1 && enable texi2html || disable texi2html
 
diff --git a/doc/outdevs.texi b/doc/outdevs.texi
index fbb3123..19767e7 100644
--- a/doc/outdevs.texi
+++ b/doc/outdevs.texi
@@ -26,6 +26,46 @@ ALSA (Advanced Linux Sound Architecture) output device.
 
 OSS (Open Sound System) output device.
 
+ at section sdl
+
+SDL output device.
+
+To enable this output device you need libsdl installed on your system
+when configuring your build.
+
+This output devices allows to show a video stream in an SDL
+window. Only one SDL window is allowed per application, so you can
+have only one instance of this output device in an application.
+
+ at subsection Options
+
+ at table @option
+
+ at item window_title
+Set the SDL window title, if not specified default to "SDL video
+outdev".
+
+ at item icon_title
+Set the name of the iconified SDL window, if not specified it is set
+to the same value of @var{window_title}.
+
+ at item window_size
+Set the SDL window size, can be a string of the form
+ at var{width}x at var{height} or a video size abbreviation.
+If not specified it defaults to the size of the input video.
+ at end table
+
+ at subsection Examples
+
+The following command shows the @file{ffmpeg} output is an
+SDL window, forcing its size to the qcif format:
+ at example
+ffmpeg -i INPUT -vcodec rawvideo -pix_fmt yuv420p -window_size qcif -f sdl none
+ at end example
+
+Note that the name specified for the output device is ignored, so it
+can be set to an arbitrary value ("none" in the above example).
+
 @section sndio
 
 sndio audio output device.
diff --git a/libavdevice/Makefile b/libavdevice/Makefile
index 4bcb5a3..60103a4 100644
--- a/libavdevice/Makefile
+++ b/libavdevice/Makefile
@@ -21,6 +21,7 @@ OBJS-$(CONFIG_FBDEV_INDEV)               += fbdev.o
 OBJS-$(CONFIG_JACK_INDEV)                += jack_audio.o
 OBJS-$(CONFIG_OSS_INDEV)                 += oss_audio.o
 OBJS-$(CONFIG_OSS_OUTDEV)                += oss_audio.o
+OBJS-$(CONFIG_SDL_OUTDEV)                += sdl.o
 OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_common.o sndio_dec.o
 OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_common.o sndio_enc.o
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o
diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c
index 3021f08..7846704 100644
--- a/libavdevice/alldevices.c
+++ b/libavdevice/alldevices.c
@@ -45,6 +45,7 @@ void avdevice_register_all(void)
     REGISTER_INDEV    (FBDEV, fbdev);
     REGISTER_INDEV    (JACK, jack);
     REGISTER_INOUTDEV (OSS, oss);
+    REGISTER_OUTDEV   (SDL, sdl);
     REGISTER_INOUTDEV (SNDIO, sndio);
     REGISTER_INDEV    (V4L2, v4l2);
 #if FF_API_V4L
diff --git a/libavdevice/sdl.c b/libavdevice/sdl.c
new file mode 100644
index 0000000..db35c7b
--- /dev/null
+++ b/libavdevice/sdl.c
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2011 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * libSDL output device
+ */
+
+#include <SDL.h>
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/pixdesc.h"
+#include "avdevice.h"
+
+typedef struct {
+    AVClass *class;
+    SDL_Surface *surface;
+    SDL_Overlay *overlay;
+    char *window_title;
+    char *icon_title;
+    char *window_size;
+    int window_width, window_height;
+    int overlay_width, overlay_height;
+    int overlay_fmt;
+    int sdl_was_already_inited;
+} SDLContext;
+
+struct sdl_overlay_pix_fmt_entry {
+    enum PixelFormat pix_fmt; int overlay_fmt;
+} sdl_overlay_pix_fmt_map[] = {
+    { PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
+    { PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
+    { PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
+    { PIX_FMT_NONE,    0                },
+};
+
+static int sdl_write_header(AVFormatContext *s)
+{
+    SDLContext *sdl = s->priv_data;
+    AVStream *st = s->streams[0];
+    AVCodecContext *encctx = st->codec;
+    const AVPixFmtDescriptor *pix_desc;
+    float sar, dar; /* sample and display aspect ratios */
+    int i;
+
+    if (!sdl->icon_title)
+        sdl->icon_title = av_strdup(sdl->window_title);
+
+    if (SDL_WasInit(SDL_INIT_VIDEO)) {
+        av_log(s, AV_LOG_ERROR,
+               "SDL video subsystem was already inited, aborting.\n");
+        sdl->sdl_was_already_inited = 1;
+        return AVERROR(EINVAL);
+    }
+
+    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
+        av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
+        return AVERROR(EINVAL);
+    }
+
+    if (   s->nb_streams > 1
+        || encctx->codec_type != AVMEDIA_TYPE_VIDEO
+        || encctx->codec_id   != CODEC_ID_RAWVIDEO) {
+        av_log(s, AV_LOG_ERROR, "SDL only supports one rawvideo stream\n");
+        return AVERROR(EINVAL);
+    }
+
+    for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != PIX_FMT_NONE; i++) {
+        if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
+            sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
+            break;
+        }
+    }
+
+    if (!sdl->overlay_fmt) {
+        av_log(s, AV_LOG_ERROR, "Unsupported pixel format '%s'\n",
+               av_get_pix_fmt_name(encctx->pix_fmt));
+        return AVERROR(EINVAL);
+    }
+    pix_desc = &av_pix_fmt_descriptors[encctx->pix_fmt];
+
+    if (sdl->window_size) {
+        if (av_parse_video_size(&sdl->window_width, &sdl->window_height, sdl->window_size) < 0) {
+            av_log(s, AV_LOG_ERROR, "Invalid window size: %s\n", sdl->window_size);
+            return AVERROR(EINVAL);
+        }
+    }
+
+    /* compute overlay width and height from the codec context information */
+    sar = st->sample_aspect_ratio.num ? av_q2d(st->sample_aspect_ratio) : 1;
+    dar = sar * (float)encctx->width / (float)encctx->height;
+
+    /* we suppose the screen has a 1/1 sample aspect ratio */
+    sdl->overlay_height = encctx->height;
+    sdl->overlay_width = ((int)rint(sdl->overlay_height * dar));
+    if (sdl->overlay_width > encctx->width) {
+        sdl->overlay_width = encctx->width;
+        sdl->overlay_height = ((int)rint(sdl->overlay_width / dar));
+    }
+
+    if (!sdl->window_width || !sdl->window_height) {
+        sdl->window_width  = sdl->overlay_width;
+        sdl->window_height = sdl->overlay_height;
+    }
+
+    SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
+    sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
+                                    24, SDL_SWSURFACE);
+    if (!sdl->surface) {
+        av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
+        return AVERROR(EINVAL);
+    }
+
+    sdl->overlay = SDL_CreateYUVOverlay(sdl->overlay_width, sdl->overlay_height,
+                                        sdl->overlay_fmt, sdl->surface);
+    if (!sdl->overlay || sdl->overlay->pitches[0] < sdl->overlay_width) {
+        av_log(s, AV_LOG_ERROR,
+               "SDL does not support an overlay with size of %dx%d pixels.\n",
+               sdl->overlay_width, sdl->overlay_height);
+        return AVERROR(EINVAL);
+    }
+
+    av_log(s, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%f -> w:%d h:%d\n",
+           encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar,
+           sdl->window_width, sdl->window_height);
+    return 0;
+}
+
+static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    SDLContext *sdl = s->priv_data;
+    AVCodecContext *encctx = s->streams[0]->codec;
+    SDL_Rect rect = { 0, 0, sdl->window_width, sdl->window_height };
+    AVPicture pict;
+    int i;
+
+    avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
+
+    SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
+                 SDL_MapRGB(sdl->surface->format, 0, 0, 0));
+    SDL_LockYUVOverlay(sdl->overlay);
+    for (i = 0; i < 3; i++) {
+        sdl->overlay->pixels [i] = pict.data    [i];
+        sdl->overlay->pitches[i] = pict.linesize[i];
+    }
+    SDL_DisplayYUVOverlay(sdl->overlay, &rect);
+    SDL_UnlockYUVOverlay(sdl->overlay);
+
+    SDL_UpdateRect(sdl->surface, 0, 0, sdl->overlay_width, sdl->overlay_height);
+
+    return 0;
+}
+
+static int sdl_write_trailer(AVFormatContext *avctx)
+{
+    SDLContext *sdl = avctx->priv_data;
+
+    av_freep(&sdl->window_title);
+    av_freep(&sdl->icon_title);
+    av_freep(&sdl->window_size);
+
+    if (sdl->overlay) {
+        SDL_FreeYUVOverlay(sdl->overlay);
+        sdl->overlay = NULL;
+    }
+    if (!sdl->sdl_was_already_inited)
+        SDL_Quit();
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(SDLContext,x)
+
+static const AVOption options[] = {
+    { "window_title", "SDL window title",           OFFSET(window_title),  FF_OPT_TYPE_STRING, {.str = "SDL video outdev" }, 0,  0, AV_OPT_FLAG_ENCODING_PARAM },
+    { "icon_title",   "SDL iconified window title", OFFSET(icon_title)  ,  FF_OPT_TYPE_STRING, {.str = NULL },               0,  0, AV_OPT_FLAG_ENCODING_PARAM },
+    { "window_size",  "SDL window forced size",     OFFSET(window_size) ,  FF_OPT_TYPE_STRING, {.str = NULL },               0,  0, AV_OPT_FLAG_ENCODING_PARAM },
+    { NULL },
+};
+
+static const AVClass sdl_class = {
+    .class_name = "sdl outdev",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVOutputFormat ff_sdl_muxer = {
+    .name           = "sdl",
+    .long_name      = NULL_IF_CONFIG_SMALL("SDL output device"),
+    .priv_data_size = sizeof(SDLContext),
+    .audio_codec    = CODEC_ID_NONE,
+    .video_codec    = CODEC_ID_RAWVIDEO,
+    .write_header   = sdl_write_header,
+    .write_packet   = sdl_write_packet,
+    .write_trailer  = sdl_write_trailer,
+    .flags          = AVFMT_NOFILE,
+    .priv_class     = &sdl_class,
+};
-- 
1.7.2.3



More information about the ffmpeg-devel mailing list