Browse code

Implement libopencv smooth filter.

Originally committed as revision 25118 to svn://svn.ffmpeg.org/ffmpeg/trunk

Stefano Sabatini authored on 2010/09/14 22:21:13
Showing 7 changed files
... ...
@@ -35,6 +35,7 @@ version <next>:
35 35
 - MMS-HTTP support
36 36
 - G.722 ADPCM audio decoder
37 37
 - R10k video decoder
38
+- ocv_smooth filter
38 39
 
39 40
 
40 41
 version 0.6:
... ...
@@ -164,6 +164,7 @@ External library support:
164 164
   --enable-bzlib           enable bzlib [autodetect]
165 165
   --enable-libopencore-amrnb enable AMR-NB de/encoding via libopencore-amrnb [no]
166 166
   --enable-libopencore-amrwb enable AMR-WB decoding via libopencore-amrwb [no]
167
+  --enable-libopencv       enable video filtering via libopencv [no]
167 168
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
168 169
                            and libraw1394 [no]
169 170
   --enable-libdirac        enable Dirac support via libdirac [no]
... ...
@@ -887,6 +888,7 @@ CONFIG_LIST="
887 887
     libnut
888 888
     libopencore_amrnb
889 889
     libopencore_amrwb
890
+    libopencv
890 891
     libopenjpeg
891 892
     librtmp
892 893
     libschroedinger
... ...
@@ -1390,6 +1392,9 @@ rtp_protocol_select="udp_protocol"
1390 1390
 tcp_protocol_deps="network"
1391 1391
 udp_protocol_deps="network"
1392 1392
 
1393
+# filters
1394
+ocv_smooth_filter_deps="libopencv"
1395
+
1393 1396
 # libraries
1394 1397
 avdevice_deps="avcodec avformat"
1395 1398
 avformat_deps="avcodec"
... ...
@@ -2713,6 +2718,8 @@ enabled libmp3lame && require  libmp3lame lame/lame.h lame_init -lmp3lame
2713 2713
 enabled libnut     && require  libnut libnut.h nut_demuxer_init -lnut
2714 2714
 enabled libopencore_amrnb  && require libopencore_amrnb opencore-amrnb/interf_dec.h Decoder_Interface_init -lopencore-amrnb
2715 2715
 enabled libopencore_amrwb  && require libopencore_amrwb opencore-amrwb/dec_if.h D_IF_init -lopencore-amrwb
2716
+enabled libopencv  && { check_lib opencv/cv.h cvCreateImageHeader $(pkg-config --libs opencv) ||
2717
+                        die "ERROR: libopencv not found"; }
2716 2718
 enabled libopenjpeg && require libopenjpeg openjpeg.h opj_version -lopenjpeg
2717 2719
 enabled librtmp    && { check_lib librtmp/rtmp.h RTMP_Socket $(pkg-config --libs librtmp) ||
2718 2720
                         die "ERROR: librtmp not found or RTMP_Socket() missing, librtmp version must be >= 2.2.f"; }
... ...
@@ -2997,6 +3004,7 @@ echo "libmp3lame enabled        ${libmp3lame-no}"
2997 2997
 echo "libnut enabled            ${libnut-no}"
2998 2998
 echo "libopencore-amrnb support ${libopencore_amrnb-no}"
2999 2999
 echo "libopencore-amrwb support ${libopencore_amrwb-no}"
3000
+echo "libopencv support         ${libopencv-no}"
3000 3001
 echo "libopenjpeg enabled       ${libopenjpeg-no}"
3001 3002
 echo "librtmp enabled           ${librtmp-no}"
3002 3003
 echo "libschroedinger enabled   ${libschroedinger-no}"
... ...
@@ -112,6 +112,33 @@ input to the vflip filter.
112 112
 
113 113
 Pass the video source unchanged to the output.
114 114
 
115
+@section ocv_smooth
116
+
117
+Apply smooth transform using libopencv.
118
+
119
+To enable this filter install libopencv library and headers and
120
+configure FFmpeg with --enable-libopencv.
121
+
122
+It accepts the following parameters:
123
+@var{type}:@var{param1}:@var{param2}:@var{param3}:@var{param4}.
124
+
125
+@var{type} is the type of smooth filter to apply, and can be one of
126
+the following value: "blur", "blur_no_scale", "median", "gaussian",
127
+"bilateral". The default value is "gaussian".
128
+
129
+@var{param1}, @var{param2}, @var{param3}, and @var{param4} are
130
+parameters whose meanings depend on smooth type. @var{param1} and
131
+@var{param2} accept integer positive values or 0, @var{param3} and
132
+@var{param4} accept float values.
133
+
134
+The default value for @var{param1} is 3, the default value for the
135
+other parameters is 0.
136
+
137
+These parameters corresponds to the parameters assigned to the
138
+libopencv function @code{cvSmooth}. Refer the official libopencv
139
+documentation for the exact meaning of the parameters:
140
+@url{http://opencv.willowgarage.com/documentation/c/image_filtering.html}
141
+
115 142
 @section pad
116 143
 
117 144
 Add paddings to the input image, and places the original input at the
... ...
@@ -23,6 +23,7 @@ OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
23 23
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
24 24
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
25 25
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
26
+OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
26 27
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
27 28
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
28 29
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
... ...
@@ -43,6 +43,7 @@ void avfilter_register_all(void)
43 43
     REGISTER_FILTER (HFLIP,       hflip,       vf);
44 44
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
45 45
     REGISTER_FILTER (NULL,        null,        vf);
46
+    REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
46 47
     REGISTER_FILTER (PAD,         pad,         vf);
47 48
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
48 49
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
... ...
@@ -25,8 +25,8 @@
25 25
 #include "libavutil/avutil.h"
26 26
 
27 27
 #define LIBAVFILTER_VERSION_MAJOR  1
28
-#define LIBAVFILTER_VERSION_MINOR 38
29
-#define LIBAVFILTER_VERSION_MICRO  3
28
+#define LIBAVFILTER_VERSION_MINOR 39
29
+#define LIBAVFILTER_VERSION_MICRO  0
30 30
 
31 31
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
32 32
                                                LIBAVFILTER_VERSION_MINOR, \
33 33
new file mode 100644
... ...
@@ -0,0 +1,156 @@
0
+/*
1
+ * copyright Stefano Sabatini 2010
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
+ * libopencv wrapper functions
23
+ */
24
+
25
+#include "opencv/cv.h"
26
+#include "opencv/cxtypes.h"
27
+#include "avfilter.h"
28
+
29
+static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
30
+{
31
+    IplImage *tmpimg;
32
+    int depth, channels_nb;
33
+
34
+    if      (pixfmt == PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U;  channels_nb = 1; }
35
+    else if (pixfmt == PIX_FMT_BGRA)  { depth = IPL_DEPTH_8U;  channels_nb = 4; }
36
+    else if (pixfmt == PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
37
+    else return;
38
+
39
+    tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
40
+    *img = *tmpimg;
41
+    img->imageData = img->imageDataOrigin = picref->data[0];
42
+    img->dataOrder = IPL_DATA_ORDER_PIXEL;
43
+    img->origin    = IPL_ORIGIN_TL;
44
+    img->widthStep = picref->linesize[0];
45
+}
46
+
47
+static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
48
+{
49
+    picref->linesize[0] = img->widthStep;
50
+    picref->data[0]     = img->imageData;
51
+}
52
+
53
+static int query_formats(AVFilterContext *ctx)
54
+{
55
+    static const enum PixelFormat pix_fmts[] = {
56
+        PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE
57
+    };
58
+
59
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
60
+    return 0;
61
+}
62
+
63
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
64
+
65
+#if CONFIG_OCV_SMOOTH_FILTER
66
+
67
+typedef struct {
68
+    int type;
69
+    int    param1, param2;
70
+    double param3, param4;
71
+} SmoothContext;
72
+
73
+static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
74
+{
75
+    SmoothContext *smooth = ctx->priv;
76
+    char type_str[128] = "gaussian";
77
+
78
+    smooth->param1 = 3;
79
+    smooth->param2 = 0;
80
+    smooth->param3 = 0.0;
81
+    smooth->param4 = 0.0;
82
+
83
+    if (args)
84
+        sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
85
+
86
+    if      (!strcmp(type_str, "blur"         )) smooth->type = CV_BLUR;
87
+    else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
88
+    else if (!strcmp(type_str, "median"       )) smooth->type = CV_MEDIAN;
89
+    else if (!strcmp(type_str, "gaussian"     )) smooth->type = CV_GAUSSIAN;
90
+    else if (!strcmp(type_str, "bilateral"    )) smooth->type = CV_BILATERAL;
91
+    else {
92
+        av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
93
+        return AVERROR(EINVAL);
94
+    }
95
+
96
+    if (smooth->param1 < 0 || !(smooth->param1%2)) {
97
+        av_log(ctx, AV_LOG_ERROR,
98
+               "Invalid value '%d' for param1, it has to be a positive odd number\n",
99
+               smooth->param1);
100
+        return AVERROR(EINVAL);
101
+    }
102
+    if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
103
+        (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
104
+        av_log(ctx, AV_LOG_ERROR,
105
+               "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
106
+               smooth->param2);
107
+        return AVERROR(EINVAL);
108
+    }
109
+
110
+    av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
111
+           type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
112
+    return 0;
113
+}
114
+
115
+static void smooth_end_frame(AVFilterLink *inlink)
116
+{
117
+    SmoothContext *smooth = inlink->dst->priv;
118
+    AVFilterLink *outlink= inlink->dst->outputs[0];
119
+    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
120
+    AVFilterBufferRef *outpicref = outlink->out_buf;
121
+    IplImage inimg, outimg;
122
+
123
+    fill_iplimage_from_picref(&inimg , inpicref , inlink->format);
124
+    fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
125
+    cvSmooth(&inimg, &outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
126
+    fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
127
+
128
+    avfilter_unref_buffer(inpicref);
129
+    avfilter_draw_slice(outlink, 0, outlink->h, 1);
130
+    avfilter_end_frame(outlink);
131
+    avfilter_unref_buffer(outpicref);
132
+}
133
+
134
+AVFilter avfilter_vf_ocv_smooth = {
135
+    .name        = "ocv_smooth",
136
+    .description = "Apply smooth transform using libopencv.",
137
+
138
+    .priv_size = sizeof(SmoothContext),
139
+
140
+    .query_formats = query_formats,
141
+    .init = smooth_init,
142
+
143
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
144
+                                    .type             = AVMEDIA_TYPE_VIDEO,
145
+                                    .draw_slice       = null_draw_slice,
146
+                                    .end_frame        = smooth_end_frame,
147
+                                    .min_perms        = AV_PERM_READ },
148
+                                  { .name = NULL}},
149
+
150
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
151
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
152
+                                  { .name = NULL}},
153
+};
154
+
155
+#endif /* CONFIG_OCV_SMOOTH_FILTER */