Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_libopencv.c @ d370e3e9

History | View | Annotate | Download (5.61 KB)

1 6ebf0bfc Stefano Sabatini
/*
2 0a7d6e01 Stefano Sabatini
 * copyright (c) 2010 Stefano Sabatini
3 6ebf0bfc Stefano Sabatini
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * libopencv wrapper functions
24
 */
25
26 e6c4a417 Stefano Sabatini
#include <opencv/cv.h>
27
#include <opencv/cxtypes.h>
28 6ebf0bfc Stefano Sabatini
#include "avfilter.h"
29
30
static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
31
{
32
    IplImage *tmpimg;
33
    int depth, channels_nb;
34
35
    if      (pixfmt == PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U;  channels_nb = 1; }
36
    else if (pixfmt == PIX_FMT_BGRA)  { depth = IPL_DEPTH_8U;  channels_nb = 4; }
37
    else if (pixfmt == PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
38
    else return;
39
40
    tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
41
    *img = *tmpimg;
42
    img->imageData = img->imageDataOrigin = picref->data[0];
43
    img->dataOrder = IPL_DATA_ORDER_PIXEL;
44
    img->origin    = IPL_ORIGIN_TL;
45
    img->widthStep = picref->linesize[0];
46
}
47
48
static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
49
{
50
    picref->linesize[0] = img->widthStep;
51
    picref->data[0]     = img->imageData;
52
}
53
54
static int query_formats(AVFilterContext *ctx)
55
{
56
    static const enum PixelFormat pix_fmts[] = {
57
        PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE
58
    };
59
60
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
61
    return 0;
62
}
63
64
static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
65
66
#if CONFIG_OCV_SMOOTH_FILTER
67
68
typedef struct {
69
    int type;
70
    int    param1, param2;
71
    double param3, param4;
72
} SmoothContext;
73
74
static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
75
{
76
    SmoothContext *smooth = ctx->priv;
77
    char type_str[128] = "gaussian";
78
79
    smooth->param1 = 3;
80
    smooth->param2 = 0;
81
    smooth->param3 = 0.0;
82
    smooth->param4 = 0.0;
83
84
    if (args)
85
        sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
86
87
    if      (!strcmp(type_str, "blur"         )) smooth->type = CV_BLUR;
88
    else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
89
    else if (!strcmp(type_str, "median"       )) smooth->type = CV_MEDIAN;
90
    else if (!strcmp(type_str, "gaussian"     )) smooth->type = CV_GAUSSIAN;
91
    else if (!strcmp(type_str, "bilateral"    )) smooth->type = CV_BILATERAL;
92
    else {
93
        av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
94
        return AVERROR(EINVAL);
95
    }
96
97
    if (smooth->param1 < 0 || !(smooth->param1%2)) {
98
        av_log(ctx, AV_LOG_ERROR,
99
               "Invalid value '%d' for param1, it has to be a positive odd number\n",
100
               smooth->param1);
101
        return AVERROR(EINVAL);
102
    }
103
    if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
104
        (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
105
        av_log(ctx, AV_LOG_ERROR,
106
               "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
107
               smooth->param2);
108
        return AVERROR(EINVAL);
109
    }
110
111
    av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
112
           type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
113
    return 0;
114
}
115
116
static void smooth_end_frame(AVFilterLink *inlink)
117
{
118
    SmoothContext *smooth = inlink->dst->priv;
119 dcb90374 Stefano Sabatini
    AVFilterLink *outlink = inlink->dst->outputs[0];
120 6ebf0bfc Stefano Sabatini
    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
121
    AVFilterBufferRef *outpicref = outlink->out_buf;
122
    IplImage inimg, outimg;
123
124
    fill_iplimage_from_picref(&inimg , inpicref , inlink->format);
125
    fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
126
    cvSmooth(&inimg, &outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
127
    fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
128
129
    avfilter_unref_buffer(inpicref);
130
    avfilter_draw_slice(outlink, 0, outlink->h, 1);
131
    avfilter_end_frame(outlink);
132
    avfilter_unref_buffer(outpicref);
133
}
134
135
AVFilter avfilter_vf_ocv_smooth = {
136
    .name        = "ocv_smooth",
137 a6881c6a Stefano Sabatini
    .description = NULL_IF_CONFIG_SMALL("Apply smooth transform using libopencv."),
138 6ebf0bfc Stefano Sabatini
139
    .priv_size = sizeof(SmoothContext),
140
141
    .query_formats = query_formats,
142
    .init = smooth_init,
143
144
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
145
                                    .type             = AVMEDIA_TYPE_VIDEO,
146
                                    .draw_slice       = null_draw_slice,
147
                                    .end_frame        = smooth_end_frame,
148
                                    .min_perms        = AV_PERM_READ },
149
                                  { .name = NULL}},
150
151
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
152
                                    .type             = AVMEDIA_TYPE_VIDEO, },
153
                                  { .name = NULL}},
154
};
155
156
#endif /* CONFIG_OCV_SMOOTH_FILTER */