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