vf_libopencv.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20 
26 /* #define DEBUG */
27 
28 #include <opencv/cv.h>
29 #include <opencv/cxcore.h>
30 #include "libavutil/avstring.h"
31 #include "libavutil/file.h"
32 #include "avfilter.h"
33 
34 static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
35 {
36  IplImage *tmpimg;
37  int depth, channels_nb;
38 
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; }
42  else return;
43 
44  tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
45  *img = *tmpimg;
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];
50 }
51 
52 static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
53 {
54  picref->linesize[0] = img->widthStep;
55  picref->data[0] = img->imageData;
56 }
57 
59 {
60  static const enum PixelFormat pix_fmts[] = {
62  };
63 
65  return 0;
66 }
67 
68 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
69 
70 typedef struct {
71  const char *name;
72  int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
74  void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
75  void *priv;
76 } OCVContext;
77 
78 typedef struct {
79  int type;
80  int param1, param2;
81  double param3, param4;
83 
84 static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
85 {
86  OCVContext *ocv = ctx->priv;
87  SmoothContext *smooth = ocv->priv;
88  char type_str[128] = "gaussian";
89 
90  smooth->param1 = 3;
91  smooth->param2 = 0;
92  smooth->param3 = 0.0;
93  smooth->param4 = 0.0;
94 
95  if (args)
96  sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
97 
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;
103  else {
104  av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
105  return AVERROR(EINVAL);
106  }
107 
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",
111  smooth->param1);
112  return AVERROR(EINVAL);
113  }
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",
118  smooth->param2);
119  return AVERROR(EINVAL);
120  }
121 
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);
124  return 0;
125 }
126 
127 static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
128 {
129  OCVContext *ocv = ctx->priv;
130  SmoothContext *smooth = ocv->priv;
131  cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
132 }
133 
134 static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename,
135  void *log_ctx)
136 {
137  uint8_t *buf, *p, *pend;
138  size_t size;
139  int ret, i, j, w;
140 
141  if ((ret = av_file_map(filename, &buf, &size, 0, log_ctx)) < 0)
142  return ret;
143 
144  /* prescan file to get the number of lines and the maximum width */
145  w = 0;
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;
151  }
152  ++(*rows);
153  *cols = FFMAX(*cols, w);
154  w = 0;
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;
158  }
159  w++;
160  }
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",
163  *rows, *cols);
164  return AVERROR_INVALIDDATA;
165  }
166  if (!(*values = av_mallocz(sizeof(int) * *rows * *cols)))
167  return AVERROR(ENOMEM);
168 
169  /* fill *values */
170  p = buf;
171  pend = buf + size-1;
172  for (i = 0; i < *rows; i++) {
173  for (j = 0;; j++) {
174  if (p > pend || *p == '\n') {
175  p++;
176  break;
177  } else
178  (*values)[*cols*i + j] = !!isgraph(*(p++));
179  }
180  }
181  av_file_unmap(buf, size);
182 
183 #ifdef DEBUG
184  {
185  char *line;
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] ? '@' : ' ';
191  line[j] = 0;
192  av_log(log_ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
193  }
194  av_free(line);
195  }
196 #endif
197 
198  return 0;
199 }
200 
201 static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx)
202 {
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;
206 
207  sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
208 
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)
215  return ret;
216  } else {
217  av_log(log_ctx, AV_LOG_ERROR,
218  "Shape unspecified or type '%s' unknown\n.", shape_str);
219  return AVERROR(EINVAL);
220  }
221 
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);
226  }
227 
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);
233  }
234 
235  *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
236  av_freep(&values);
237  if (!*kernel)
238  return AVERROR(ENOMEM);
239 
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);
242  return 0;
243 }
244 
245 typedef struct {
247  IplConvKernel *kernel;
248 } DilateContext;
249 
250 static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opaque)
251 {
252  OCVContext *ocv = ctx->priv;
253  DilateContext *dilate = ocv->priv;
254  char default_kernel_str[] = "3x3+0x0/rect";
255  char *kernel_str;
256  const char *buf = args;
257  int ret;
258 
259  dilate->nb_iterations = 1;
260 
261  if (args)
262  kernel_str = av_get_token(&buf, ":");
263  if ((ret = parse_iplconvkernel(&dilate->kernel,
264  *kernel_str ? kernel_str : default_kernel_str,
265  ctx)) < 0)
266  return ret;
267  av_free(kernel_str);
268 
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);
275  }
276  return 0;
277 }
278 
280 {
281  OCVContext *ocv = ctx->priv;
282  DilateContext *dilate = ocv->priv;
283 
284  cvReleaseStructuringElement(&dilate->kernel);
285 }
286 
287 static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
288 {
289  OCVContext *ocv = ctx->priv;
290  DilateContext *dilate = ocv->priv;
291  cvDilate(inimg, outimg, dilate->kernel, dilate->nb_iterations);
292 }
293 
294 static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
295 {
296  OCVContext *ocv = ctx->priv;
297  DilateContext *dilate = ocv->priv;
298  cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations);
299 }
300 
301 typedef struct {
302  const char *name;
303  size_t priv_size;
304  int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
306  void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
308 
312  { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
313 };
314 
315 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
316 {
317  OCVContext *ocv = ctx->priv;
318  char name[128], priv_args[1024];
319  int i;
320  char c;
321 
322  sscanf(args, "%127[^=:]%c%1023s", name, &c, priv_args);
323 
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;
331 
332  if (!(ocv->priv = av_mallocz(entry->priv_size)))
333  return AVERROR(ENOMEM);
334  return ocv->init(ctx, priv_args, opaque);
335  }
336  }
337 
338  av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name);
339  return AVERROR(EINVAL);
340 }
341 
342 static av_cold void uninit(AVFilterContext *ctx)
343 {
344  OCVContext *ocv = ctx->priv;
345 
346  if (ocv->uninit)
347  ocv->uninit(ctx);
348  av_free(ocv->priv);
349  memset(ocv, 0, sizeof(*ocv));
350 }
351 
352 static void end_frame(AVFilterLink *inlink)
353 {
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;
360 
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);
365 
366  avfilter_unref_buffer(inpicref);
367  avfilter_draw_slice(outlink, 0, outlink->h, 1);
368  avfilter_end_frame(outlink);
369  avfilter_unref_buffer(outpicref);
370 }
371 
373  .name = "ocv",
374  .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
375 
376  .priv_size = sizeof(OCVContext),
377 
379  .init = init,
380  .uninit = uninit,
381 
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 },
387  { .name = NULL}},
388 
389  .outputs = (AVFilterPad[]) {{ .name = "default",
390  .type = AVMEDIA_TYPE_VIDEO, },
391  { .name = NULL}},
392 };