2 * Copyright (c) 2010 Stefano Sabatini
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * libopencv wrapper functions
28 #include <opencv/cv.h>
29 #include <opencv/cxcore.h>
30 #include "libavutil/avstring.h"
31 #include "libavutil/file.h"
34 static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
37 int depth, channels_nb;
39 if (pixfmt == PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U; channels_nb = 1; }
40 else if (pixfmt == PIX_FMT_BGRA) { depth = IPL_DEPTH_8U; channels_nb = 4; }
41 else if (pixfmt == PIX_FMT_BGR24) { depth = IPL_DEPTH_8U; channels_nb = 3; }
44 tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
46 img->imageData = img->imageDataOrigin = picref->data[0];
47 img->dataOrder = IPL_DATA_ORDER_PIXEL;
48 img->origin = IPL_ORIGIN_TL;
49 img->widthStep = picref->linesize[0];
52 static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
54 picref->linesize[0] = img->widthStep;
55 picref->data[0] = img->imageData;
58 static int query_formats(AVFilterContext *ctx)
60 static const enum PixelFormat pix_fmts[] = {
61 PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE
64 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
68 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
72 int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
73 void (*uninit)(AVFilterContext *ctx);
74 void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
81 double param3, param4;
84 static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
86 OCVContext *ocv = ctx->priv;
87 SmoothContext *smooth = ocv->priv;
88 char type_str[128] = "gaussian";
96 sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
98 if (!strcmp(type_str, "blur" )) smooth->type = CV_BLUR;
99 else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
100 else if (!strcmp(type_str, "median" )) smooth->type = CV_MEDIAN;
101 else if (!strcmp(type_str, "gaussian" )) smooth->type = CV_GAUSSIAN;
102 else if (!strcmp(type_str, "bilateral" )) smooth->type = CV_BILATERAL;
104 av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
105 return AVERROR(EINVAL);
108 if (smooth->param1 < 0 || !(smooth->param1%2)) {
109 av_log(ctx, AV_LOG_ERROR,
110 "Invalid value '%d' for param1, it has to be a positive odd number\n",
112 return AVERROR(EINVAL);
114 if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
115 (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
116 av_log(ctx, AV_LOG_ERROR,
117 "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
119 return AVERROR(EINVAL);
122 av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
123 type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
127 static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
129 OCVContext *ocv = ctx->priv;
130 SmoothContext *smooth = ocv->priv;
131 cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
134 static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename,
137 uint8_t *buf, *p, *pend;
141 if ((ret = av_file_map(filename, &buf, &size, 0, log_ctx)) < 0)
144 /* prescan file to get the number of lines and the maximum width */
146 for (i = 0; i < size; i++) {
147 if (buf[i] == '\n') {
148 if (*rows == INT_MAX) {
149 av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of rows in the file\n");
150 return AVERROR_INVALIDDATA;
153 *cols = FFMAX(*cols, w);
155 } else if (w == INT_MAX) {
156 av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of columns in the file\n");
157 return AVERROR_INVALIDDATA;
161 if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
162 av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n",
164 return AVERROR_INVALIDDATA;
166 if (!(*values = av_mallocz(sizeof(int) * *rows * *cols)))
167 return AVERROR(ENOMEM);
172 for (i = 0; i < *rows; i++) {
174 if (p > pend || *p == '\n') {
178 (*values)[*cols*i + j] = !!isgraph(*(p++));
181 av_file_unmap(buf, size);
186 if (!(line = av_malloc(*cols + 1)))
187 return AVERROR(ENOMEM);
188 for (i = 0; i < *rows; i++) {
189 for (j = 0; j < *cols; j++)
190 line[j] = (*values)[i * *cols + j] ? '@' : ' ';
192 av_log(log_ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
201 static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx)
203 char shape_filename[128] = "", shape_str[32] = "rect";
204 int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT;
205 int *values = NULL, ret;
207 sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
209 if (!strcmp(shape_str, "rect" )) shape = CV_SHAPE_RECT;
210 else if (!strcmp(shape_str, "cross" )) shape = CV_SHAPE_CROSS;
211 else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
212 else if (!strcmp(shape_str, "custom" )) {
213 shape = CV_SHAPE_CUSTOM;
214 if ((ret = read_shape_from_file(&cols, &rows, &values, shape_filename, log_ctx)) < 0)
217 av_log(log_ctx, AV_LOG_ERROR,
218 "Shape unspecified or type '%s' unknown\n.", shape_str);
219 return AVERROR(EINVAL);
222 if (rows <= 0 || cols <= 0) {
223 av_log(log_ctx, AV_LOG_ERROR,
224 "Invalid non-positive values for shape size %dx%d\n", cols, rows);
225 return AVERROR(EINVAL);
228 if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) {
229 av_log(log_ctx, AV_LOG_ERROR,
230 "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n",
231 anchor_x, anchor_y, cols, rows);
232 return AVERROR(EINVAL);
235 *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
238 return AVERROR(ENOMEM);
240 av_log(log_ctx, AV_LOG_INFO, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n",
241 rows, cols, anchor_x, anchor_y, shape_str);
247 IplConvKernel *kernel;
250 static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opaque)
252 OCVContext *ocv = ctx->priv;
253 DilateContext *dilate = ocv->priv;
254 char default_kernel_str[] = "3x3+0x0/rect";
256 const char *buf = args;
259 dilate->nb_iterations = 1;
262 kernel_str = av_get_token(&buf, ":");
263 if ((ret = parse_iplconvkernel(&dilate->kernel,
264 *kernel_str ? kernel_str : default_kernel_str,
269 sscanf(buf, ":%d", &dilate->nb_iterations);
270 av_log(ctx, AV_LOG_INFO, "iterations_nb:%d\n", dilate->nb_iterations);
271 if (dilate->nb_iterations <= 0) {
272 av_log(ctx, AV_LOG_ERROR, "Invalid non-positive value '%d' for nb_iterations\n",
273 dilate->nb_iterations);
274 return AVERROR(EINVAL);
279 static av_cold void dilate_uninit(AVFilterContext *ctx)
281 OCVContext *ocv = ctx->priv;
282 DilateContext *dilate = ocv->priv;
284 cvReleaseStructuringElement(&dilate->kernel);
287 static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
289 OCVContext *ocv = ctx->priv;
290 DilateContext *dilate = ocv->priv;
291 cvDilate(inimg, outimg, dilate->kernel, dilate->nb_iterations);
294 static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
296 OCVContext *ocv = ctx->priv;
297 DilateContext *dilate = ocv->priv;
298 cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations);
304 int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
305 void (*uninit)(AVFilterContext *ctx);
306 void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
309 static OCVFilterEntry ocv_filter_entries[] = {
310 { "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter },
311 { "erode", sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter },
312 { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
315 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
317 OCVContext *ocv = ctx->priv;
318 char name[128], priv_args[1024];
322 sscanf(args, "%127[^=:]%c%1023s", name, &c, priv_args);
324 for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
325 OCVFilterEntry *entry = &ocv_filter_entries[i];
326 if (!strcmp(name, entry->name)) {
327 ocv->name = entry->name;
328 ocv->init = entry->init;
329 ocv->uninit = entry->uninit;
330 ocv->end_frame_filter = entry->end_frame_filter;
332 if (!(ocv->priv = av_mallocz(entry->priv_size)))
333 return AVERROR(ENOMEM);
334 return ocv->init(ctx, priv_args, opaque);
338 av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name);
339 return AVERROR(EINVAL);
342 static av_cold void uninit(AVFilterContext *ctx)
344 OCVContext *ocv = ctx->priv;
349 memset(ocv, 0, sizeof(*ocv));
352 static void end_frame(AVFilterLink *inlink)
354 AVFilterContext *ctx = inlink->dst;
355 OCVContext *ocv = ctx->priv;
356 AVFilterLink *outlink= inlink->dst->outputs[0];
357 AVFilterBufferRef *inpicref = inlink ->cur_buf;
358 AVFilterBufferRef *outpicref = outlink->out_buf;
359 IplImage inimg, outimg;
361 fill_iplimage_from_picref(&inimg , inpicref , inlink->format);
362 fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
363 ocv->end_frame_filter(ctx, &inimg, &outimg);
364 fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
366 avfilter_unref_buffer(inpicref);
367 avfilter_draw_slice(outlink, 0, outlink->h, 1);
368 avfilter_end_frame(outlink);
369 avfilter_unref_buffer(outpicref);
372 AVFilter avfilter_vf_ocv = {
374 .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
376 .priv_size = sizeof(OCVContext),
378 .query_formats = query_formats,
382 .inputs = (AVFilterPad[]) {{ .name = "default",
383 .type = AVMEDIA_TYPE_VIDEO,
384 .draw_slice = null_draw_slice,
385 .end_frame = end_frame,
386 .min_perms = AV_PERM_READ },
389 .outputs = (AVFilterPad[]) {{ .name = "default",
390 .type = AVMEDIA_TYPE_VIDEO, },