]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_libopencv.c
lavf/http: Remove superfluous parenthesis.
[ffmpeg] / libavfilter / vf_libopencv.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  *
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 #include <opencv/cv.h>
27 #include <opencv/cxcore.h>
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/file.h"
31 #include "libavutil/opt.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36
37 static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt)
38 {
39     IplImage *tmpimg;
40     int depth, channels_nb;
41
42     if      (pixfmt == AV_PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U;  channels_nb = 1; }
43     else if (pixfmt == AV_PIX_FMT_BGRA)  { depth = IPL_DEPTH_8U;  channels_nb = 4; }
44     else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
45     else return;
46
47     tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
48     *img = *tmpimg;
49     img->imageData = img->imageDataOrigin = frame->data[0];
50     img->dataOrder = IPL_DATA_ORDER_PIXEL;
51     img->origin    = IPL_ORIGIN_TL;
52     img->widthStep = frame->linesize[0];
53 }
54
55 static void fill_frame_from_iplimage(AVFrame *frame, const IplImage *img, enum AVPixelFormat pixfmt)
56 {
57     frame->linesize[0] = img->widthStep;
58     frame->data[0]     = img->imageData;
59 }
60
61 static int query_formats(AVFilterContext *ctx)
62 {
63     static const enum AVPixelFormat pix_fmts[] = {
64         AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
65     };
66     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
67     if (!fmts_list)
68         return AVERROR(ENOMEM);
69     return ff_set_common_formats(ctx, fmts_list);
70 }
71
72 typedef struct OCVContext {
73     const AVClass *class;
74     char *name;
75     char *params;
76     int (*init)(AVFilterContext *ctx, const char *args);
77     void (*uninit)(AVFilterContext *ctx);
78     void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
79     void *priv;
80 } OCVContext;
81
82 typedef struct SmoothContext {
83     int type;
84     int    param1, param2;
85     double param3, param4;
86 } SmoothContext;
87
88 static av_cold int smooth_init(AVFilterContext *ctx, const char *args)
89 {
90     OCVContext *s = ctx->priv;
91     SmoothContext *smooth = s->priv;
92     char type_str[128] = "gaussian";
93
94     smooth->param1 = 3;
95     smooth->param2 = 0;
96     smooth->param3 = 0.0;
97     smooth->param4 = 0.0;
98
99     if (args)
100         sscanf(args, "%127[^|]|%d|%d|%lf|%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
101
102     if      (!strcmp(type_str, "blur"         )) smooth->type = CV_BLUR;
103     else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
104     else if (!strcmp(type_str, "median"       )) smooth->type = CV_MEDIAN;
105     else if (!strcmp(type_str, "gaussian"     )) smooth->type = CV_GAUSSIAN;
106     else if (!strcmp(type_str, "bilateral"    )) smooth->type = CV_BILATERAL;
107     else {
108         av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown.\n", type_str);
109         return AVERROR(EINVAL);
110     }
111
112     if (smooth->param1 < 0 || !(smooth->param1%2)) {
113         av_log(ctx, AV_LOG_ERROR,
114                "Invalid value '%d' for param1, it has to be a positive odd number\n",
115                smooth->param1);
116         return AVERROR(EINVAL);
117     }
118     if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
119         (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
120         av_log(ctx, AV_LOG_ERROR,
121                "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
122                smooth->param2);
123         return AVERROR(EINVAL);
124     }
125
126     av_log(ctx, AV_LOG_VERBOSE, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
127            type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
128     return 0;
129 }
130
131 static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
132 {
133     OCVContext *s = ctx->priv;
134     SmoothContext *smooth = s->priv;
135     cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
136 }
137
138 static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename,
139                                 void *log_ctx)
140 {
141     uint8_t *buf, *p, *pend;
142     size_t size;
143     int ret, i, j, w;
144
145     if ((ret = av_file_map(filename, &buf, &size, 0, log_ctx)) < 0)
146         return ret;
147
148     /* prescan file to get the number of lines and the maximum width */
149     w = 0;
150     for (i = 0; i < size; i++) {
151         if (buf[i] == '\n') {
152             if (*rows == INT_MAX) {
153                 av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of rows in the file\n");
154                 return AVERROR_INVALIDDATA;
155             }
156             ++(*rows);
157             *cols = FFMAX(*cols, w);
158             w = 0;
159         } else if (w == INT_MAX) {
160             av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of columns in the file\n");
161             return AVERROR_INVALIDDATA;
162         }
163         w++;
164     }
165     if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
166         av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n",
167                *rows, *cols);
168         return AVERROR_INVALIDDATA;
169     }
170     if (!(*values = av_mallocz_array(sizeof(int) * *rows, *cols)))
171         return AVERROR(ENOMEM);
172
173     /* fill *values */
174     p    = buf;
175     pend = buf + size-1;
176     for (i = 0; i < *rows; i++) {
177         for (j = 0;; j++) {
178             if (p > pend || *p == '\n') {
179                 p++;
180                 break;
181             } else
182                 (*values)[*cols*i + j] = !!av_isgraph(*(p++));
183         }
184     }
185     av_file_unmap(buf, size);
186
187 #ifdef DEBUG
188     {
189         char *line;
190         if (!(line = av_malloc(*cols + 1)))
191             return AVERROR(ENOMEM);
192         for (i = 0; i < *rows; i++) {
193             for (j = 0; j < *cols; j++)
194                 line[j] = (*values)[i * *cols + j] ? '@' : ' ';
195             line[j] = 0;
196             av_log(log_ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
197         }
198         av_free(line);
199     }
200 #endif
201
202     return 0;
203 }
204
205 static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx)
206 {
207     char shape_filename[128] = "", shape_str[32] = "rect";
208     int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT;
209     int *values = NULL, ret = 0;
210
211     sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
212
213     if      (!strcmp(shape_str, "rect"   )) shape = CV_SHAPE_RECT;
214     else if (!strcmp(shape_str, "cross"  )) shape = CV_SHAPE_CROSS;
215     else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
216     else if (!strcmp(shape_str, "custom" )) {
217         shape = CV_SHAPE_CUSTOM;
218         if ((ret = read_shape_from_file(&cols, &rows, &values, shape_filename, log_ctx)) < 0)
219             return ret;
220     } else {
221         av_log(log_ctx, AV_LOG_ERROR,
222                "Shape unspecified or type '%s' unknown.\n", shape_str);
223         ret = AVERROR(EINVAL);
224         goto out;
225     }
226
227     if (rows <= 0 || cols <= 0) {
228         av_log(log_ctx, AV_LOG_ERROR,
229                "Invalid non-positive values for shape size %dx%d\n", cols, rows);
230         ret = AVERROR(EINVAL);
231         goto out;
232     }
233
234     if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) {
235         av_log(log_ctx, AV_LOG_ERROR,
236                "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n",
237                anchor_x, anchor_y, cols, rows);
238         ret = AVERROR(EINVAL);
239         goto out;
240     }
241
242     *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
243     if (!*kernel) {
244         ret = AVERROR(ENOMEM);
245         goto out;
246     }
247
248     av_log(log_ctx, AV_LOG_VERBOSE, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n",
249            rows, cols, anchor_x, anchor_y, shape_str);
250 out:
251     av_freep(&values);
252     return ret;
253 }
254
255 typedef struct DilateContext {
256     int nb_iterations;
257     IplConvKernel *kernel;
258 } DilateContext;
259
260 static av_cold int dilate_init(AVFilterContext *ctx, const char *args)
261 {
262     OCVContext *s = ctx->priv;
263     DilateContext *dilate = s->priv;
264     char default_kernel_str[] = "3x3+0x0/rect";
265     char *kernel_str = NULL;
266     const char *buf = args;
267     int ret;
268
269     if (args) {
270         kernel_str = av_get_token(&buf, "|");
271
272         if (!kernel_str)
273             return AVERROR(ENOMEM);
274     }
275
276     ret = parse_iplconvkernel(&dilate->kernel,
277                               (!kernel_str || !*kernel_str) ? default_kernel_str
278                                                             : kernel_str,
279                               ctx);
280     av_free(kernel_str);
281     if (ret < 0)
282         return ret;
283
284     if (!buf || sscanf(buf, "|%d", &dilate->nb_iterations) != 1)
285         dilate->nb_iterations = 1;
286     av_log(ctx, AV_LOG_VERBOSE, "iterations_nb:%d\n", dilate->nb_iterations);
287     if (dilate->nb_iterations <= 0) {
288         av_log(ctx, AV_LOG_ERROR, "Invalid non-positive value '%d' for nb_iterations\n",
289                dilate->nb_iterations);
290         return AVERROR(EINVAL);
291     }
292     return 0;
293 }
294
295 static av_cold void dilate_uninit(AVFilterContext *ctx)
296 {
297     OCVContext *s = ctx->priv;
298     DilateContext *dilate = s->priv;
299
300     cvReleaseStructuringElement(&dilate->kernel);
301 }
302
303 static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
304 {
305     OCVContext *s = ctx->priv;
306     DilateContext *dilate = s->priv;
307     cvDilate(inimg, outimg, dilate->kernel, dilate->nb_iterations);
308 }
309
310 static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
311 {
312     OCVContext *s = ctx->priv;
313     DilateContext *dilate = s->priv;
314     cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations);
315 }
316
317 typedef struct OCVFilterEntry {
318     const char *name;
319     size_t priv_size;
320     int  (*init)(AVFilterContext *ctx, const char *args);
321     void (*uninit)(AVFilterContext *ctx);
322     void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
323 } OCVFilterEntry;
324
325 static const OCVFilterEntry ocv_filter_entries[] = {
326     { "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter },
327     { "erode",  sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter  },
328     { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
329 };
330
331 static av_cold int init(AVFilterContext *ctx)
332 {
333     OCVContext *s = ctx->priv;
334     int i;
335
336     if (!s->name) {
337         av_log(ctx, AV_LOG_ERROR, "No libopencv filter name specified\n");
338         return AVERROR(EINVAL);
339     }
340     for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
341         const OCVFilterEntry *entry = &ocv_filter_entries[i];
342         if (!strcmp(s->name, entry->name)) {
343             s->init             = entry->init;
344             s->uninit           = entry->uninit;
345             s->end_frame_filter = entry->end_frame_filter;
346
347             if (!(s->priv = av_mallocz(entry->priv_size)))
348                 return AVERROR(ENOMEM);
349             return s->init(ctx, s->params);
350         }
351     }
352
353     av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", s->name);
354     return AVERROR(EINVAL);
355 }
356
357 static av_cold void uninit(AVFilterContext *ctx)
358 {
359     OCVContext *s = ctx->priv;
360
361     if (s->uninit)
362         s->uninit(ctx);
363     av_freep(&s->priv);
364 }
365
366 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
367 {
368     AVFilterContext *ctx = inlink->dst;
369     OCVContext *s = ctx->priv;
370     AVFilterLink *outlink= inlink->dst->outputs[0];
371     AVFrame *out;
372     IplImage inimg, outimg;
373
374     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
375     if (!out) {
376         av_frame_free(&in);
377         return AVERROR(ENOMEM);
378     }
379     av_frame_copy_props(out, in);
380
381     fill_iplimage_from_frame(&inimg , in , inlink->format);
382     fill_iplimage_from_frame(&outimg, out, inlink->format);
383     s->end_frame_filter(ctx, &inimg, &outimg);
384     fill_frame_from_iplimage(out, &outimg, inlink->format);
385
386     av_frame_free(&in);
387
388     return ff_filter_frame(outlink, out);
389 }
390
391 #define OFFSET(x) offsetof(OCVContext, x)
392 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
393 static const AVOption ocv_options[] = {
394     { "filter_name",   NULL, OFFSET(name),   AV_OPT_TYPE_STRING, .flags = FLAGS },
395     { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
396     { NULL }
397 };
398
399 AVFILTER_DEFINE_CLASS(ocv);
400
401 static const AVFilterPad avfilter_vf_ocv_inputs[] = {
402     {
403         .name         = "default",
404         .type         = AVMEDIA_TYPE_VIDEO,
405         .filter_frame = filter_frame,
406     },
407     { NULL }
408 };
409
410 static const AVFilterPad avfilter_vf_ocv_outputs[] = {
411     {
412         .name = "default",
413         .type = AVMEDIA_TYPE_VIDEO,
414     },
415     { NULL }
416 };
417
418 AVFilter ff_vf_ocv = {
419     .name          = "ocv",
420     .description   = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
421     .priv_size     = sizeof(OCVContext),
422     .priv_class    = &ocv_class,
423     .query_formats = query_formats,
424     .init          = init,
425     .uninit        = uninit,
426     .inputs        = avfilter_vf_ocv_inputs,
427     .outputs       = avfilter_vf_ocv_outputs,
428 };