[PATCH] Implement libopencv smooth filter.

Stefano Sabatini stefano.sabatini-lala
Tue Aug 3 02:22:20 CEST 2010


---
 configure                  |    8 ++
 doc/filters.texi           |   27 +++++++
 libavfilter/Makefile       |    1 +
 libavfilter/allfilters.c   |    1 +
 libavfilter/vf_libopencv.c |  169 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 206 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_libopencv.c

diff --git a/configure b/configure
index 9b86247..d23f505 100755
--- a/configure
+++ b/configure
@@ -164,6 +164,7 @@ External library support:
   --enable-bzlib           enable bzlib [autodetect]
   --enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no]
   --enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no]
+  --enable-libopencv       enable filtering via libopencv [no]
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
                            and libraw1394 [no]
   --enable-libdirac        enable Dirac support via libdirac [no]
@@ -878,6 +879,7 @@ CONFIG_LIST="
     libnut
     libopencore_amrnb
     libopencore_amrwb
+    libopencv
     libopenjpeg
     librtmp
     libschroedinger
@@ -1380,6 +1382,9 @@ rtp_protocol_select="udp_protocol"
 tcp_protocol_deps="network"
 udp_protocol_deps="network"
 
+# filters
+ocv_smooth_filter_deps="libopencv"
+
 # libraries
 avdevice_deps="avcodec avformat"
 avformat_deps="avcodec"
@@ -2690,6 +2695,8 @@ enabled libmp3lame && require  libmp3lame lame/lame.h lame_init -lmp3lame
 enabled libnut     && require  libnut libnut.h nut_demuxer_init -lnut
 enabled libopencore_amrnb  && require libopencore_amrnb opencore-amrnb/interf_dec.h Decoder_Interface_init -lopencore-amrnb
 enabled libopencore_amrwb  && require libopencore_amrwb opencore-amrwb/dec_if.h D_IF_init -lopencore-amrwb
+enabled libopencv  && { check_lib opencv/cv.h cvCreateImageHeader $(pkg-config --libs opencv) ||
+                        die "ERROR: libopencv not found"; }
 enabled libopenjpeg && require libopenjpeg openjpeg.h opj_version -lopenjpeg
 enabled librtmp    && { check_lib librtmp/rtmp.h RTMP_Socket $(pkg-config --libs librtmp) ||
                         die "ERROR: librtmp not found or RTMP_Socket() missing, librtmp version must be >= 2.2.f"; }
@@ -2971,6 +2978,7 @@ echo "libmp3lame enabled        ${libmp3lame-no}"
 echo "libnut enabled            ${libnut-no}"
 echo "libopencore-amrnb support ${libopencore_amrnb-no}"
 echo "libopencore-amrwb support ${libopencore_amrwb-no}"
+echo "libopencv support         ${libopencv-no}"
 echo "libopenjpeg enabled       ${libopenjpeg-no}"
 echo "librtmp enabled           ${librtmp-no}"
 echo "libschroedinger enabled   ${libschroedinger-no}"
diff --git a/doc/filters.texi b/doc/filters.texi
index a00e850..15872e9 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -103,6 +103,33 @@ input to the vflip filter.
 
 Pass the video source unchanged to the output.
 
+ at section ocv_smooth
+
+Apply smooth transform using libopencv.
+
+To enable this filter install libopencv and use --enable-libopencv
+during configuration.
+
+It accepts the following parameters:
+ at var{type}:@var{param1}:@var{param2}:@var{param3}:@var{param4}.
+
+ at var{type} is the type of smooth filter to apply, and can be one of
+the following value: "blur", "blur_no_scale", "median", "gaussian",
+"bilateral". The default value is "gaussian".
+
+ at var{param1}, @var{param2}, @var{param3}, and @var{param4} are
+parameters whose meanings depend on smooth type. @var{param1} and
+ at var{param2} accept integer positive values or 0, @var{param3} and
+ at var{param4} accept float values.
+
+The default value for @var{param1} is 3, the default value for the
+other parameters is 0.
+
+These parameters corresponds to the parameters assigned to the
+libopencv function @code{cvSmooth}. Refer the official libopencv
+documentation for the exact meaning of the parameters:
+ at url{http://opencv.willowgarage.com/documentation/c/image_filtering.html}
+
 @section pad
 
 Add paddings to the input image, and places the original input at the
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 443a5c6..b93137f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -22,6 +22,7 @@ OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
+OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 66bcf96..a1c023d 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -42,6 +42,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (HFLIP,       hflip,       vf);
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
     REGISTER_FILTER (NULL,        null,        vf);
+    REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
new file mode 100644
index 0000000..c2c93e8
--- /dev/null
+++ b/libavfilter/vf_libopencv.c
@@ -0,0 +1,169 @@
+/*
+ * copyright Stefano Sabatini 2010
+ *
+ * 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
+ * libopencv wrapper functions
+ */
+
+#include "opencv/cv.h"
+#include "opencv/cxtypes.h"
+#include "avfilter.h"
+
+static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
+{
+    int i;
+    IplImage *tmpimg;
+
+    struct {
+        enum PixelFormat pixfmt;
+        int depth;
+        int channels;
+    } entry, pixfmt_iplimage_map[] = {
+        { PIX_FMT_GRAY8, IPL_DEPTH_8U, 1 },
+        { PIX_FMT_BGRA , IPL_DEPTH_8U, 4 },
+        { PIX_FMT_BGR24, IPL_DEPTH_8U, 3 },
+        { PIX_FMT_NONE                   }
+    };
+
+    for (i = 0; i < FF_ARRAY_ELEMS(pixfmt_iplimage_map); i++) {
+        entry = pixfmt_iplimage_map[i];
+        if (pixfmt == entry.pixfmt)
+            break;
+    }
+    if (entry.pixfmt == PIX_FMT_NONE)
+        assert(1);
+
+    tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, entry.depth, entry.channels);
+    *img = *tmpimg;
+    img->imageData = img->imageDataOrigin = picref->data[0];
+    img->dataOrder = IPL_DATA_ORDER_PIXEL;
+    img->origin    = IPL_ORIGIN_TL;
+    img->widthStep = picref->linesize[0];
+}
+
+static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
+{
+    picref->linesize[0] = img->widthStep;
+    picref->data[0]     = img->imageData;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum PixelFormat pix_fmts[] = {
+        PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+#if CONFIG_OCV_SMOOTH_FILTER
+
+typedef struct {
+    int type;
+    int    param1, param2;
+    double param3, param4;
+} SmoothContext;
+
+static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    SmoothContext *smooth = ctx->priv;
+    char type_str[128] = "gaussian";
+
+    smooth->param1 = 3;
+    smooth->param2 = 0;
+    smooth->param3 = 0.0;
+    smooth->param4 = 0.0;
+
+    if (args)
+        sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
+
+    if      (!strcmp(type_str, "blur"         )) smooth->type = CV_BLUR;
+    else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
+    else if (!strcmp(type_str, "median"       )) smooth->type = CV_MEDIAN;
+    else if (!strcmp(type_str, "gaussian"     )) smooth->type = CV_GAUSSIAN;
+    else if (!strcmp(type_str, "bilateral"    )) smooth->type = CV_BILATERAL;
+    else {
+        av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
+        return AVERROR(EINVAL);
+    }
+
+    av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
+           type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
+
+    if (smooth->param1 < 0 || !(smooth->param1%2)) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Invalid value '%d' for param1, it has to be a positive odd number\n",
+               smooth->param1);
+        return AVERROR(EINVAL);
+    }
+    if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
+        (smooth->param2 < 0 || !(smooth->param2%2))) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Invalid value '%d' for param2, it has to be a positive odd number\n",
+               smooth->param2);
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static void smooth_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void smooth_end_frame(AVFilterLink *inlink)
+{
+    SmoothContext *smooth = inlink->dst->priv;
+    AVFilterLink *outlink= inlink->dst->outputs[0];
+    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
+    AVFilterBufferRef *outpicref = outlink->out_buf;
+    IplImage inimg, outimg;
+
+    fill_iplimage_from_picref( &inimg, inpicref , inlink->format);
+    fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
+    cvSmooth(&inimg, &outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
+    fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
+
+    avfilter_draw_slice(outlink, 0, outlink->h, 1);
+    avfilter_end_frame(outlink);
+}
+
+AVFilter avfilter_vf_ocv_smooth = {
+    .name        = "ocv_smooth",
+    .description = "Apply smooth transform using libopencv.",
+
+    .priv_size = sizeof(SmoothContext),
+
+    .query_formats = query_formats,
+    .init = smooth_init,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .draw_slice       = smooth_draw_slice,
+                                    .end_frame        = smooth_end_frame,
+                                    .min_perms        = AV_PERM_READ },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
+
+#endif /* CONFIG_OCV_SMOOTH_FILTER */
-- 
1.7.1


--y0ulUmNC+osPPQO6--



More information about the ffmpeg-devel mailing list