2 * copyright (c) 2010 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * libopencv wrapper functions
26 #include <opencv/cv.h>
27 #include <opencv/cxtypes.h>
30 static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
33 int depth, channels_nb;
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; }
40 tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
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];
48 static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
50 picref->linesize[0] = img->widthStep;
51 picref->data[0] = img->imageData;
54 static int query_formats(AVFilterContext *ctx)
56 static const enum PixelFormat pix_fmts[] = {
57 PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE
60 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
64 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
66 #if CONFIG_OCV_SMOOTH_FILTER
71 double param3, param4;
74 static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
76 SmoothContext *smooth = ctx->priv;
77 char type_str[128] = "gaussian";
85 sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
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;
93 av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
94 return AVERROR(EINVAL);
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",
101 return AVERROR(EINVAL);
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",
108 return AVERROR(EINVAL);
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);
116 static void smooth_end_frame(AVFilterLink *inlink)
118 SmoothContext *smooth = inlink->dst->priv;
119 AVFilterLink *outlink = inlink->dst->outputs[0];
120 AVFilterBufferRef *inpicref = inlink ->cur_buf;
121 AVFilterBufferRef *outpicref = outlink->out_buf;
122 IplImage inimg, outimg;
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);
129 avfilter_unref_buffer(inpicref);
130 avfilter_draw_slice(outlink, 0, outlink->h, 1);
131 avfilter_end_frame(outlink);
132 avfilter_unref_buffer(outpicref);
135 AVFilter avfilter_vf_ocv_smooth = {
136 .name = "ocv_smooth",
137 .description = NULL_IF_CONFIG_SMALL("Apply smooth transform using libopencv."),
139 .priv_size = sizeof(SmoothContext),
141 .query_formats = query_formats,
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 },
151 .outputs = (AVFilterPad[]) {{ .name = "default",
152 .type = AVMEDIA_TYPE_VIDEO, },
156 #endif /* CONFIG_OCV_SMOOTH_FILTER */