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