[PATCH] Implement SDL display sink.

Stefano Sabatini stefano.sabatini-lala
Wed Dec 1 17:44:12 CET 2010


---
 configure                      |    2 +
 ffplay.c                       |    5 +
 libavfilter/Makefile           |    1 +
 libavfilter/allfilters.c       |    1 +
 libavfilter/vsink_sdldisplay.c |  187 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 196 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vsink_sdldisplay.c

diff --git a/configure b/configure
index 3176a89..445df90 100755
--- a/configure
+++ b/configure
@@ -1414,6 +1414,7 @@ frei0r_filter_deps="frei0r dlopen strtok_r"
 frei0r_src_filter_deps="frei0r dlopen strtok_r"
 hqdn3d_filter_deps="gpl"
 ocv_filter_deps="libopencv"
+sdldisplay_filter_deps="sdl"
 yadif_filter_deps="gpl"
 
 # libraries
@@ -2803,6 +2804,7 @@ if "${SDL_CONFIG}" --version > /dev/null 2>&1; then
     enable sdl &&
     check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
 fi
+enabled sdl && add_cflags $sdl_cflags && add_extralibs $sdl_libs
 
 texi2html -version > /dev/null 2>&1 && enable texi2html || disable texi2html
 
diff --git a/ffplay.c b/ffplay.c
index 38a2fe1..5fa98a3 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -3140,6 +3140,11 @@ int main(int argc, char **argv)
 #if !defined(__MINGW32__) && !defined(__APPLE__)
     flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
 #endif
+    if (SDL_WasInit(SDL_INIT_VIDEO)) {
+        av_log(NULL, AV_LOG_ERROR,
+               "SDL video subsystem was already inited, aborting.\n");
+        exit(1);
+    }
     if (SDL_Init (flags)) {
         fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
         exit(1);
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 731678f..dd7b7aa 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -53,6 +53,7 @@ OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_nullsrc.o
 
 OBJS-$(CONFIG_NULLSINK_FILTER)               += vsink_nullsink.o
+OBJS-$(CONFIG_SDLDISPLAY_FILTER)             += vsink_sdldisplay.o
 
 -include $(SUBDIR)$(ARCH)/Makefile
 
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 59c6f7f..b664dc4 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -74,4 +74,5 @@ void avfilter_register_all(void)
     REGISTER_FILTER (NULLSRC,     nullsrc,     vsrc);
 
     REGISTER_FILTER (NULLSINK,    nullsink,    vsink);
+    REGISTER_FILTER (SDLDISPLAY,  sdldisplay,  vsink);
 }
diff --git a/libavfilter/vsink_sdldisplay.c b/libavfilter/vsink_sdldisplay.c
new file mode 100644
index 0000000..27d076e
--- /dev/null
+++ b/libavfilter/vsink_sdldisplay.c
@@ -0,0 +1,187 @@
+/*
+ * Copyright (c) 2010 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
+ */
+
+#include "SDL.h"
+#include "libavutil/avstring.h"
+#include "avfilter.h"
+
+typedef struct {
+    SDL_Surface *surface;
+    SDL_Overlay *overlay;
+    char *window_title;
+    char *iconified_window_name;
+    int was_already_inited;
+} DisplayContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    DisplayContext *dpy = ctx->priv;
+    const char *buf = args;
+
+    if (args) {
+        dpy->window_title = av_get_token(&buf, ":");
+        if (*buf++)
+            dpy->iconified_window_name = av_get_token(&buf, "");
+    }
+    if (!dpy->window_title)
+        dpy->window_title = av_strdup("SDL video display");
+    if (!dpy->iconified_window_name)
+        dpy->iconified_window_name = av_strdup("SDL video display");
+
+    if (SDL_WasInit(SDL_INIT_VIDEO)) {
+        av_log(ctx, AV_LOG_ERROR,
+               "SDL video subsystem was already inited, aborting.\n");
+        dpy->was_already_inited = 1;
+        return AVERROR(EINVAL);
+    }
+
+    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
+        av_log(ctx, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+av_cold static void uninit(AVFilterContext *ctx)
+{
+    DisplayContext *dpy = ctx->priv;
+
+    if (dpy->window_title)
+        av_freep(&dpy->window_title);
+    if (dpy->iconified_window_name)
+        av_freep(&dpy->iconified_window_name);
+    if (dpy->overlay) {
+        SDL_FreeYUVOverlay(dpy->overlay);
+        dpy->overlay = NULL;
+    }
+    if (!dpy->was_already_inited)
+        SDL_Quit();
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum PixelFormat pix_fmts[] = {
+        PIX_FMT_YUV420P, PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_props(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    DisplayContext *dpy = ctx->priv;
+
+    dpy->surface = SDL_SetVideoMode(inlink->w, inlink->h, 24, SDL_SWSURFACE);
+    if (!dpy->surface) {
+        av_log(ctx, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
+        return AVERROR(EINVAL);
+    }
+
+    SDL_WM_SetCaption(dpy->window_title, dpy->iconified_window_name);
+
+    dpy->overlay = SDL_CreateYUVOverlay(inlink->w, inlink->h,
+                                        SDL_YV12_OVERLAY, dpy->surface);
+    if (!dpy->overlay || dpy->overlay->pitches[0] < inlink->w) {
+        av_log(ctx, AV_LOG_ERROR,
+               "The SDL video system does not support an image with size of %dx%d pixels.\n",
+               inlink->w, inlink->h);
+        return AVERROR(EINVAL);
+    }
+
+    SDL_FillRect(dpy->surface, &dpy->surface->clip_rect,
+                 SDL_MapRGB(dpy->surface->format, 0, 0, 0));
+
+    return 0;
+}
+
+static void null_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) { }
+
+static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink) {
+    AVFilterContext *ctx = inlink->dst;
+    DisplayContext *dpy = ctx->priv;
+    AVFilterBufferRef *inpic = inlink->cur_buf;
+    float aspect_ratio;
+    int width, height;
+    SDL_Rect rect;
+
+    if (inpic->video->pixel_aspect.num == 0)
+        aspect_ratio = 0;
+    else
+        aspect_ratio = av_q2d(inpic->video->pixel_aspect);
+
+    if (aspect_ratio <= 0.0)
+        aspect_ratio = 1.0;
+    aspect_ratio *= (float)inlink->w / (float)inlink->h;
+
+    /* XXX: we suppose the screen has a 1.0 pixel ratio */
+    height = inlink->h;
+    width = ((int)rint(height * aspect_ratio)) & ~1;
+    if (width > inlink->w) {
+        width = inlink->w;
+        height = ((int)rint(width / aspect_ratio)) & ~1;
+    }
+
+    rect.x = 0;
+    rect.y = 0;
+    rect.w = width;
+    rect.h = height;
+
+    SDL_LockYUVOverlay(dpy->overlay);
+    dpy->overlay->pixels[0] = inpic->data[0];
+    dpy->overlay->pixels[2] = inpic->data[1];
+    dpy->overlay->pixels[1] = inpic->data[2];
+
+    dpy->overlay->pitches[0] = inpic->linesize[0];
+    dpy->overlay->pitches[2] = inpic->linesize[1];
+    dpy->overlay->pitches[1] = inpic->linesize[2];
+
+    SDL_DisplayYUVOverlay(dpy->overlay, &rect);
+    SDL_UnlockYUVOverlay(dpy->overlay);
+
+    SDL_UpdateRect(dpy->surface, 0, 0, inlink->w, inlink->h);
+    avfilter_unref_buffer(inpic);
+}
+
+AVFilter avfilter_vsink_sdldisplay = {
+    .name        = "sdldisplay",
+    .description = NULL_IF_CONFIG_SMALL("Display the input video in a SDL window."),
+    .priv_size = sizeof(DisplayContext),
+
+    .init = init,
+    .uninit = uninit,
+    .query_formats = query_formats,
+
+    .inputs = (AVFilterPad[]) {{
+            .name            = "default",
+            .type            = AVMEDIA_TYPE_VIDEO,
+            .config_props    = config_props,
+            .start_frame     = null_start_frame,
+            .draw_slice      = null_draw_slice,
+            .end_frame       = end_frame, },
+
+            { .name = NULL},
+    },
+    .outputs   = (AVFilterPad[]) {{ .name = NULL }},
+};
-- 
1.7.2.3


--C7zPtVaVf+AK4Oqc--



More information about the ffmpeg-devel mailing list