[PATCH] Implement SDL display sink.

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


---
 configure                      |    2 +
 libavfilter/Makefile           |    1 +
 libavfilter/allfilters.c       |    1 +
 libavfilter/vsink_sdldisplay.c |  159 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 163 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vsink_sdldisplay.c

diff --git a/configure b/configure
index a882501..dba6e26 100755
--- a/configure
+++ b/configure
@@ -1408,6 +1408,7 @@ cropdetect_filter_deps="gpl"
 frei0r_filter_deps="frei0r dlopen strtok_r"
 frei0r_src_filter_deps="frei0r dlopen strtok_r"
 ocv_smooth_filter_deps="libopencv"
+sdldisplay_filter_deps="sdl"
 yadif_filter_deps="gpl"
 
 # libraries
@@ -2796,6 +2797,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/libavfilter/Makefile b/libavfilter/Makefile
index a8237c5..201659c 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -51,6 +51,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 56ff02d..dd6103d 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -72,4 +72,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..9044b8c
--- /dev/null
+++ b/libavfilter/vsink_sdldisplay.c
@@ -0,0 +1,159 @@
+/*
+ * 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 "avfilter.h"
+
+typedef struct {
+    SDL_Surface *surface;
+    SDL_Overlay *overlay;
+} DisplayContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    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->overlay) {
+        SDL_FreeYUVOverlay(dpy->overlay);
+        dpy->overlay = NULL;
+    }
+    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);
+    }
+
+    /* fixme: make this customizable */
+    /* SDL_WM_SetCaption("SDL video display", "SDL video display"); */
+
+    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.1


--1LKvkjL3sHcu1TtY--



More information about the ffmpeg-devel mailing list