[PATCH 1/3] Re-design the libopencv wrapper to make it more generic.

Stefano Sabatini stefano.sabatini-lala
Sat Oct 2 16:39:25 CEST 2010


The same filter will be used for managing all the libopencv filtering
functions.
---
 configure                  |    2 +-
 libavfilter/Makefile       |    2 +-
 libavfilter/allfilters.c   |    2 +-
 libavfilter/vf_libopencv.c |   90 +++++++++++++++++++++++++++++++++++++-------
 4 files changed, 79 insertions(+), 17 deletions(-)

diff --git a/configure b/configure
index 21ea161..8432c12 100755
--- a/configure
+++ b/configure
@@ -1400,7 +1400,7 @@ udp_protocol_deps="network"
 # filters
 blackframe_filter_deps="gpl"
 frei0r_filter_deps="frei0r dlopen strtok_r"
-ocv_smooth_filter_deps="libopencv"
+ocv_filter_deps="libopencv"
 yadif_filter_deps="gpl"
 
 # libraries
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 74e55bb..bc28820 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -30,7 +30,7 @@ OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.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_OCV_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 c5da648..3613677 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -50,7 +50,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 (OCV,         ocv,         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
index c2b5962..0e3da4d 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -63,7 +63,13 @@ static int query_formats(AVFilterContext *ctx)
 
 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
 
-#if CONFIG_OCV_SMOOTH_FILTER
+typedef struct {
+    const char *name;
+    int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
+    void (*uninit)(AVFilterContext *ctx);
+    void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
+    void *priv;
+} OCVContext;
 
 typedef struct {
     int type;
@@ -73,7 +79,8 @@ typedef struct {
 
 static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
 {
-    SmoothContext *smooth = ctx->priv;
+    OCVContext *ocv = ctx->priv;
+    SmoothContext *smooth = ocv->priv;
     char type_str[128] = "gaussian";
 
     smooth->param1 = 3;
@@ -113,17 +120,73 @@ static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opa
     return 0;
 }
 
-static void smooth_end_frame(AVFilterLink *inlink)
+static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
 {
-    SmoothContext *smooth = inlink->dst->priv;
-    AVFilterLink *outlink = inlink->dst->outputs[0];
+    OCVContext *ocv = ctx->priv;
+    SmoothContext *smooth = ocv->priv;
+    cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
+}
+
+typedef struct {
+    const char *name;
+    size_t priv_size;
+    int  (*init)(AVFilterContext *ctx, const char *args, void *opaque);
+    void (*uninit)(AVFilterContext *ctx);
+    void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
+} OCVFilterEntry;
+
+static OCVFilterEntry ocv_filter_entries[] = {
+    { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
+};
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    OCVContext *ocv = ctx->priv;
+    char name[128], priv_args[1024];
+    int i;
+
+    sscanf(args, "%127[^:]=%1023s", name, priv_args);
+
+    for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
+        OCVFilterEntry *entry = &ocv_filter_entries[i];
+        if (!strcmp(name, entry->name)) {
+            ocv->name             = entry->name;
+            ocv->init             = entry->init;
+            ocv->uninit           = entry->uninit;
+            ocv->end_frame_filter = entry->end_frame_filter;
+
+            if (!(ocv->priv = av_mallocz(entry->priv_size)))
+                return AVERROR(ENOMEM);
+            return ocv->init(ctx, priv_args, opaque);
+        }
+    }
+
+    av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name);
+    return AVERROR(EINVAL);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    OCVContext *ocv = ctx->priv;
+
+    if (ocv->uninit)
+        ocv->uninit(ctx);
+    av_free(ocv->priv);
+    memset(ocv, 0, sizeof(*ocv));
+}
+
+static void end_frame(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    OCVContext *ocv = ctx->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);
+    ocv->end_frame_filter(ctx, &inimg, &outimg);
     fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
 
     avfilter_unref_buffer(inpicref);
@@ -132,19 +195,20 @@ static void smooth_end_frame(AVFilterLink *inlink)
     avfilter_unref_buffer(outpicref);
 }
 
-AVFilter avfilter_vf_ocv_smooth = {
-    .name        = "ocv_smooth",
-    .description = NULL_IF_CONFIG_SMALL("Apply smooth transform using libopencv."),
+AVFilter avfilter_vf_ocv = {
+    .name        = "ocv",
+    .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
 
-    .priv_size = sizeof(SmoothContext),
+    .priv_size = sizeof(OCVContext),
 
     .query_formats = query_formats,
-    .init = smooth_init,
+    .init = init,
+    .uninit = uninit,
 
     .inputs    = (AVFilterPad[]) {{ .name             = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO,
                                     .draw_slice       = null_draw_slice,
-                                    .end_frame        = smooth_end_frame,
+                                    .end_frame        = end_frame,
                                     .min_perms        = AV_PERM_READ },
                                   { .name = NULL}},
 
@@ -152,5 +216,3 @@ AVFilter avfilter_vf_ocv_smooth = {
                                     .type             = AVMEDIA_TYPE_VIDEO, },
                                   { .name = NULL}},
 };
-
-#endif /* CONFIG_OCV_SMOOTH_FILTER */
-- 
1.7.1


--HlL+5n6rz5pIUxbD--



More information about the ffmpeg-devel mailing list