00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026
00027
00028 #include <opencv/cv.h>
00029 #include <opencv/cxcore.h>
00030 #include "libavutil/avstring.h"
00031 #include "libavutil/common.h"
00032 #include "libavutil/file.h"
00033 #include "avfilter.h"
00034 #include "formats.h"
00035 #include "internal.h"
00036 #include "video.h"
00037
00038 static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum AVPixelFormat pixfmt)
00039 {
00040 IplImage *tmpimg;
00041 int depth, channels_nb;
00042
00043 if (pixfmt == AV_PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U; channels_nb = 1; }
00044 else if (pixfmt == AV_PIX_FMT_BGRA) { depth = IPL_DEPTH_8U; channels_nb = 4; }
00045 else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U; channels_nb = 3; }
00046 else return;
00047
00048 tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
00049 *img = *tmpimg;
00050 img->imageData = img->imageDataOrigin = picref->data[0];
00051 img->dataOrder = IPL_DATA_ORDER_PIXEL;
00052 img->origin = IPL_ORIGIN_TL;
00053 img->widthStep = picref->linesize[0];
00054 }
00055
00056 static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum AVPixelFormat pixfmt)
00057 {
00058 picref->linesize[0] = img->widthStep;
00059 picref->data[0] = img->imageData;
00060 }
00061
00062 static int query_formats(AVFilterContext *ctx)
00063 {
00064 static const enum AVPixelFormat pix_fmts[] = {
00065 AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
00066 };
00067
00068 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00069 return 0;
00070 }
00071
00072 typedef struct {
00073 const char *name;
00074 int (*init)(AVFilterContext *ctx, const char *args);
00075 void (*uninit)(AVFilterContext *ctx);
00076 void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
00077 void *priv;
00078 } OCVContext;
00079
00080 typedef struct {
00081 int type;
00082 int param1, param2;
00083 double param3, param4;
00084 } SmoothContext;
00085
00086 static av_cold int smooth_init(AVFilterContext *ctx, const char *args)
00087 {
00088 OCVContext *ocv = ctx->priv;
00089 SmoothContext *smooth = ocv->priv;
00090 char type_str[128] = "gaussian";
00091
00092 smooth->param1 = 3;
00093 smooth->param2 = 0;
00094 smooth->param3 = 0.0;
00095 smooth->param4 = 0.0;
00096
00097 if (args)
00098 sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
00099
00100 if (!strcmp(type_str, "blur" )) smooth->type = CV_BLUR;
00101 else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
00102 else if (!strcmp(type_str, "median" )) smooth->type = CV_MEDIAN;
00103 else if (!strcmp(type_str, "gaussian" )) smooth->type = CV_GAUSSIAN;
00104 else if (!strcmp(type_str, "bilateral" )) smooth->type = CV_BILATERAL;
00105 else {
00106 av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown.\n", type_str);
00107 return AVERROR(EINVAL);
00108 }
00109
00110 if (smooth->param1 < 0 || !(smooth->param1%2)) {
00111 av_log(ctx, AV_LOG_ERROR,
00112 "Invalid value '%d' for param1, it has to be a positive odd number\n",
00113 smooth->param1);
00114 return AVERROR(EINVAL);
00115 }
00116 if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
00117 (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
00118 av_log(ctx, AV_LOG_ERROR,
00119 "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
00120 smooth->param2);
00121 return AVERROR(EINVAL);
00122 }
00123
00124 av_log(ctx, AV_LOG_VERBOSE, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
00125 type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
00126 return 0;
00127 }
00128
00129 static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
00130 {
00131 OCVContext *ocv = ctx->priv;
00132 SmoothContext *smooth = ocv->priv;
00133 cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
00134 }
00135
00136 static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename,
00137 void *log_ctx)
00138 {
00139 uint8_t *buf, *p, *pend;
00140 size_t size;
00141 int ret, i, j, w;
00142
00143 if ((ret = av_file_map(filename, &buf, &size, 0, log_ctx)) < 0)
00144 return ret;
00145
00146
00147 w = 0;
00148 for (i = 0; i < size; i++) {
00149 if (buf[i] == '\n') {
00150 if (*rows == INT_MAX) {
00151 av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of rows in the file\n");
00152 return AVERROR_INVALIDDATA;
00153 }
00154 ++(*rows);
00155 *cols = FFMAX(*cols, w);
00156 w = 0;
00157 } else if (w == INT_MAX) {
00158 av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of columns in the file\n");
00159 return AVERROR_INVALIDDATA;
00160 }
00161 w++;
00162 }
00163 if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
00164 av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n",
00165 *rows, *cols);
00166 return AVERROR_INVALIDDATA;
00167 }
00168 if (!(*values = av_mallocz(sizeof(int) * *rows * *cols)))
00169 return AVERROR(ENOMEM);
00170
00171
00172 p = buf;
00173 pend = buf + size-1;
00174 for (i = 0; i < *rows; i++) {
00175 for (j = 0;; j++) {
00176 if (p > pend || *p == '\n') {
00177 p++;
00178 break;
00179 } else
00180 (*values)[*cols*i + j] = !!isgraph(*(p++));
00181 }
00182 }
00183 av_file_unmap(buf, size);
00184
00185 #ifdef DEBUG
00186 {
00187 char *line;
00188 if (!(line = av_malloc(*cols + 1)))
00189 return AVERROR(ENOMEM);
00190 for (i = 0; i < *rows; i++) {
00191 for (j = 0; j < *cols; j++)
00192 line[j] = (*values)[i * *cols + j] ? '@' : ' ';
00193 line[j] = 0;
00194 av_log(log_ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
00195 }
00196 av_free(line);
00197 }
00198 #endif
00199
00200 return 0;
00201 }
00202
00203 static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx)
00204 {
00205 char shape_filename[128] = "", shape_str[32] = "rect";
00206 int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT;
00207 int *values = NULL, ret;
00208
00209 sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
00210
00211 if (!strcmp(shape_str, "rect" )) shape = CV_SHAPE_RECT;
00212 else if (!strcmp(shape_str, "cross" )) shape = CV_SHAPE_CROSS;
00213 else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
00214 else if (!strcmp(shape_str, "custom" )) {
00215 shape = CV_SHAPE_CUSTOM;
00216 if ((ret = read_shape_from_file(&cols, &rows, &values, shape_filename, log_ctx)) < 0)
00217 return ret;
00218 } else {
00219 av_log(log_ctx, AV_LOG_ERROR,
00220 "Shape unspecified or type '%s' unknown.\n", shape_str);
00221 return AVERROR(EINVAL);
00222 }
00223
00224 if (rows <= 0 || cols <= 0) {
00225 av_log(log_ctx, AV_LOG_ERROR,
00226 "Invalid non-positive values for shape size %dx%d\n", cols, rows);
00227 return AVERROR(EINVAL);
00228 }
00229
00230 if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) {
00231 av_log(log_ctx, AV_LOG_ERROR,
00232 "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n",
00233 anchor_x, anchor_y, cols, rows);
00234 return AVERROR(EINVAL);
00235 }
00236
00237 *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
00238 av_freep(&values);
00239 if (!*kernel)
00240 return AVERROR(ENOMEM);
00241
00242 av_log(log_ctx, AV_LOG_VERBOSE, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n",
00243 rows, cols, anchor_x, anchor_y, shape_str);
00244 return 0;
00245 }
00246
00247 typedef struct {
00248 int nb_iterations;
00249 IplConvKernel *kernel;
00250 } DilateContext;
00251
00252 static av_cold int dilate_init(AVFilterContext *ctx, const char *args)
00253 {
00254 OCVContext *ocv = ctx->priv;
00255 DilateContext *dilate = ocv->priv;
00256 char default_kernel_str[] = "3x3+0x0/rect";
00257 char *kernel_str;
00258 const char *buf = args;
00259 int ret;
00260
00261 dilate->nb_iterations = 1;
00262
00263 if (args)
00264 kernel_str = av_get_token(&buf, ":");
00265 if ((ret = parse_iplconvkernel(&dilate->kernel,
00266 *kernel_str ? kernel_str : default_kernel_str,
00267 ctx)) < 0)
00268 return ret;
00269 av_free(kernel_str);
00270
00271 sscanf(buf, ":%d", &dilate->nb_iterations);
00272 av_log(ctx, AV_LOG_VERBOSE, "iterations_nb:%d\n", dilate->nb_iterations);
00273 if (dilate->nb_iterations <= 0) {
00274 av_log(ctx, AV_LOG_ERROR, "Invalid non-positive value '%d' for nb_iterations\n",
00275 dilate->nb_iterations);
00276 return AVERROR(EINVAL);
00277 }
00278 return 0;
00279 }
00280
00281 static av_cold void dilate_uninit(AVFilterContext *ctx)
00282 {
00283 OCVContext *ocv = ctx->priv;
00284 DilateContext *dilate = ocv->priv;
00285
00286 cvReleaseStructuringElement(&dilate->kernel);
00287 }
00288
00289 static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
00290 {
00291 OCVContext *ocv = ctx->priv;
00292 DilateContext *dilate = ocv->priv;
00293 cvDilate(inimg, outimg, dilate->kernel, dilate->nb_iterations);
00294 }
00295
00296 static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
00297 {
00298 OCVContext *ocv = ctx->priv;
00299 DilateContext *dilate = ocv->priv;
00300 cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations);
00301 }
00302
00303 typedef struct {
00304 const char *name;
00305 size_t priv_size;
00306 int (*init)(AVFilterContext *ctx, const char *args);
00307 void (*uninit)(AVFilterContext *ctx);
00308 void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
00309 } OCVFilterEntry;
00310
00311 static OCVFilterEntry ocv_filter_entries[] = {
00312 { "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter },
00313 { "erode", sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter },
00314 { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
00315 };
00316
00317 static av_cold int init(AVFilterContext *ctx, const char *args)
00318 {
00319 OCVContext *ocv = ctx->priv;
00320 char name[128], priv_args[1024];
00321 int i;
00322 char c;
00323
00324 sscanf(args, "%127[^=:]%c%1023s", name, &c, priv_args);
00325
00326 for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
00327 OCVFilterEntry *entry = &ocv_filter_entries[i];
00328 if (!strcmp(name, entry->name)) {
00329 ocv->name = entry->name;
00330 ocv->init = entry->init;
00331 ocv->uninit = entry->uninit;
00332 ocv->end_frame_filter = entry->end_frame_filter;
00333
00334 if (!(ocv->priv = av_mallocz(entry->priv_size)))
00335 return AVERROR(ENOMEM);
00336 return ocv->init(ctx, priv_args);
00337 }
00338 }
00339
00340 av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name);
00341 return AVERROR(EINVAL);
00342 }
00343
00344 static av_cold void uninit(AVFilterContext *ctx)
00345 {
00346 OCVContext *ocv = ctx->priv;
00347
00348 if (ocv->uninit)
00349 ocv->uninit(ctx);
00350 av_free(ocv->priv);
00351 memset(ocv, 0, sizeof(*ocv));
00352 }
00353
00354 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
00355 {
00356 AVFilterContext *ctx = inlink->dst;
00357 OCVContext *ocv = ctx->priv;
00358 AVFilterLink *outlink= inlink->dst->outputs[0];
00359 AVFilterBufferRef *out;
00360 IplImage inimg, outimg;
00361
00362 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00363 if (!out) {
00364 avfilter_unref_bufferp(&in);
00365 return AVERROR(ENOMEM);
00366 }
00367 avfilter_copy_buffer_ref_props(out, in);
00368
00369 fill_iplimage_from_picref(&inimg , in , inlink->format);
00370 fill_iplimage_from_picref(&outimg, out, inlink->format);
00371 ocv->end_frame_filter(ctx, &inimg, &outimg);
00372 fill_picref_from_iplimage(out, &outimg, inlink->format);
00373
00374 avfilter_unref_bufferp(&in);
00375
00376 return ff_filter_frame(outlink, out);
00377 }
00378
00379 static const AVFilterPad avfilter_vf_ocv_inputs[] = {
00380 {
00381 .name = "default",
00382 .type = AVMEDIA_TYPE_VIDEO,
00383 .filter_frame = filter_frame,
00384 .min_perms = AV_PERM_READ
00385 },
00386 { NULL }
00387 };
00388
00389 static const AVFilterPad avfilter_vf_ocv_outputs[] = {
00390 {
00391 .name = "default",
00392 .type = AVMEDIA_TYPE_VIDEO,
00393 },
00394 { NULL }
00395 };
00396
00397 AVFilter avfilter_vf_ocv = {
00398 .name = "ocv",
00399 .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
00400
00401 .priv_size = sizeof(OCVContext),
00402
00403 .query_formats = query_formats,
00404 .init = init,
00405 .uninit = uninit,
00406
00407 .inputs = avfilter_vf_ocv_inputs,
00408
00409 .outputs = avfilter_vf_ocv_outputs,
00410 };