annotate ffmpeg/libavfilter/vf_libopencv.c @ 13:844d341cf643 tip

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