X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_libopencv.c;h=5a97e4bfb697b6a25a0adb482f2a017d1980b84c;hb=5c1585c4c3b5281835d784c5daef0069915ccd57;hp=8401b4dcb76a6f311d47ef182d53c75f991fbd8b;hpb=17fc94933b050c7b0917ee5a347b14d7b6f87580;p=ffmpeg diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c index 8401b4dcb76..5a97e4bfb69 100644 --- a/libavfilter/vf_libopencv.c +++ b/libavfilter/vf_libopencv.c @@ -1,20 +1,20 @@ /* * Copyright (c) 2010 Stefano Sabatini * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * Libav is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * FFmpeg is distributed in the hope that it will be useful, + * Libav is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -23,68 +23,71 @@ * libopencv wrapper functions */ -/* #define DEBUG */ - #include -#include +#include #include "libavutil/avstring.h" +#include "libavutil/common.h" #include "libavutil/file.h" +#include "libavutil/opt.h" #include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" -static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt) +static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt) { IplImage *tmpimg; int depth, channels_nb; - if (pixfmt == PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U; channels_nb = 1; } - else if (pixfmt == PIX_FMT_BGRA) { depth = IPL_DEPTH_8U; channels_nb = 4; } - else if (pixfmt == PIX_FMT_BGR24) { depth = IPL_DEPTH_8U; channels_nb = 3; } + if (pixfmt == AV_PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U; channels_nb = 1; } + else if (pixfmt == AV_PIX_FMT_BGRA) { depth = IPL_DEPTH_8U; channels_nb = 4; } + else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U; channels_nb = 3; } else return; - tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb); + tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb); *img = *tmpimg; - img->imageData = img->imageDataOrigin = picref->data[0]; + img->imageData = img->imageDataOrigin = frame->data[0]; img->dataOrder = IPL_DATA_ORDER_PIXEL; img->origin = IPL_ORIGIN_TL; - img->widthStep = picref->linesize[0]; + img->widthStep = frame->linesize[0]; } -static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt) +static void fill_frame_from_iplimage(AVFrame *frame, const IplImage *img, enum AVPixelFormat pixfmt) { - picref->linesize[0] = img->widthStep; - picref->data[0] = img->imageData; + frame->linesize[0] = img->widthStep; + frame->data[0] = img->imageData; } static int query_formats(AVFilterContext *ctx) { - static const enum PixelFormat pix_fmts[] = { - PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE }; - avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); return 0; } -static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { } - -typedef struct { - const char *name; - int (*init)(AVFilterContext *ctx, const char *args, void *opaque); +typedef struct OCVContext { + const AVClass *class; + char *name; + char *params; + int (*init)(AVFilterContext *ctx, const char *args); void (*uninit)(AVFilterContext *ctx); void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg); void *priv; } OCVContext; -typedef struct { +typedef struct SmoothContext { int type; int param1, param2; double param3, param4; } SmoothContext; -static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque) +static av_cold int smooth_init(AVFilterContext *ctx, const char *args) { - OCVContext *ocv = ctx->priv; - SmoothContext *smooth = ocv->priv; + OCVContext *s = ctx->priv; + SmoothContext *smooth = s->priv; char type_str[128] = "gaussian"; smooth->param1 = 3; @@ -93,7 +96,7 @@ static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opa smooth->param4 = 0.0; if (args) - sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4); + sscanf(args, "%127[^|]|%d|%d|%lf|%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4); if (!strcmp(type_str, "blur" )) smooth->type = CV_BLUR; else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE; @@ -101,7 +104,7 @@ static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opa else if (!strcmp(type_str, "gaussian" )) smooth->type = CV_GAUSSIAN; else if (!strcmp(type_str, "bilateral" )) smooth->type = CV_BILATERAL; else { - av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str); + av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown.\n", type_str); return AVERROR(EINVAL); } @@ -119,15 +122,15 @@ static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opa return AVERROR(EINVAL); } - av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n", + av_log(ctx, AV_LOG_VERBOSE, "type:%s param1:%d param2:%d param3:%f param4:%f\n", type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4); return 0; } static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg) { - OCVContext *ocv = ctx->priv; - SmoothContext *smooth = ocv->priv; + OCVContext *s = ctx->priv; + SmoothContext *smooth = s->priv; cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4); } @@ -158,7 +161,7 @@ static int read_shape_from_file(int *cols, int *rows, int **values, const char * } w++; } - if (*rows > (FF_INTERNAL_MEM_TYPE_MAX_VALUE / (sizeof(int)) / *cols)) { + if (*rows > (SIZE_MAX / sizeof(int) / *cols)) { av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n", *rows, *cols); return AVERROR_INVALIDDATA; @@ -175,7 +178,7 @@ static int read_shape_from_file(int *cols, int *rows, int **values, const char * p++; break; } else - (*values)[*cols*i + j] = !!isgraph(*(p++)); + (*values)[*cols*i + j] = !!av_isgraph(*(p++)); } } av_file_unmap(buf, size); @@ -202,7 +205,7 @@ static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx) { char shape_filename[128] = "", shape_str[32] = "rect"; int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT; - int *values = NULL, ret; + int *values = NULL, ret = 0; sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename); @@ -215,59 +218,71 @@ static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx) return ret; } else { av_log(log_ctx, AV_LOG_ERROR, - "Shape unspecified or type '%s' unknown\n.", shape_str); - return AVERROR(EINVAL); + "Shape unspecified or type '%s' unknown.\n", shape_str); + ret = AVERROR(EINVAL); + goto out; } if (rows <= 0 || cols <= 0) { av_log(log_ctx, AV_LOG_ERROR, "Invalid non-positive values for shape size %dx%d\n", cols, rows); - return AVERROR(EINVAL); + ret = AVERROR(EINVAL); + goto out; } if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) { av_log(log_ctx, AV_LOG_ERROR, "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n", anchor_x, anchor_y, cols, rows); - return AVERROR(EINVAL); + ret = AVERROR(EINVAL); + goto out; } *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values); - av_freep(&values); - if (!*kernel) - return AVERROR(ENOMEM); + if (!*kernel) { + ret = AVERROR(ENOMEM); + goto out; + } - av_log(log_ctx, AV_LOG_INFO, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n", + av_log(log_ctx, AV_LOG_VERBOSE, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n", rows, cols, anchor_x, anchor_y, shape_str); - return 0; +out: + av_freep(&values); + return ret; } -typedef struct { +typedef struct DilateContext { int nb_iterations; IplConvKernel *kernel; } DilateContext; -static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opaque) +static av_cold int dilate_init(AVFilterContext *ctx, const char *args) { - OCVContext *ocv = ctx->priv; - DilateContext *dilate = ocv->priv; + OCVContext *s = ctx->priv; + DilateContext *dilate = s->priv; char default_kernel_str[] = "3x3+0x0/rect"; - char *kernel_str; + char *kernel_str = NULL; const char *buf = args; int ret; dilate->nb_iterations = 1; - if (args) - kernel_str = av_get_token(&buf, ":"); - if ((ret = parse_iplconvkernel(&dilate->kernel, - *kernel_str ? kernel_str : default_kernel_str, - ctx)) < 0) - return ret; + if (args) { + kernel_str = av_get_token(&buf, "|"); + if (!kernel_str) + return AVERROR(ENOMEM); + } + + ret = parse_iplconvkernel(&dilate->kernel, + (!kernel_str || !*kernel_str) ? default_kernel_str + : kernel_str, + ctx); av_free(kernel_str); + if (ret < 0) + return ret; - sscanf(buf, ":%d", &dilate->nb_iterations); - av_log(ctx, AV_LOG_INFO, "iterations_nb:%d\n", dilate->nb_iterations); + sscanf(buf, "|%d", &dilate->nb_iterations); + av_log(ctx, AV_LOG_VERBOSE, "iterations_nb:%d\n", dilate->nb_iterations); if (dilate->nb_iterations <= 0) { av_log(ctx, AV_LOG_ERROR, "Invalid non-positive value '%d' for nb_iterations\n", dilate->nb_iterations); @@ -278,115 +293,140 @@ static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opa static av_cold void dilate_uninit(AVFilterContext *ctx) { - OCVContext *ocv = ctx->priv; - DilateContext *dilate = ocv->priv; + OCVContext *s = ctx->priv; + DilateContext *dilate = s->priv; cvReleaseStructuringElement(&dilate->kernel); } static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg) { - OCVContext *ocv = ctx->priv; - DilateContext *dilate = ocv->priv; + OCVContext *s = ctx->priv; + DilateContext *dilate = s->priv; cvDilate(inimg, outimg, dilate->kernel, dilate->nb_iterations); } static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg) { - OCVContext *ocv = ctx->priv; - DilateContext *dilate = ocv->priv; + OCVContext *s = ctx->priv; + DilateContext *dilate = s->priv; cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations); } -typedef struct { +typedef struct OCVFilterEntry { const char *name; size_t priv_size; - int (*init)(AVFilterContext *ctx, const char *args, void *opaque); + int (*init)(AVFilterContext *ctx, const char *args); void (*uninit)(AVFilterContext *ctx); void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg); } OCVFilterEntry; -static OCVFilterEntry ocv_filter_entries[] = { +static const OCVFilterEntry ocv_filter_entries[] = { { "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter }, { "erode", sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter }, { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter }, }; -static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) +static av_cold int init(AVFilterContext *ctx) { - OCVContext *ocv = ctx->priv; - char name[128], priv_args[1024]; + OCVContext *s = ctx->priv; int i; - char c; - - sscanf(args, "%127[^=:]%c%1023s", name, &c, priv_args); for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) { - OCVFilterEntry *entry = &ocv_filter_entries[i]; - if (!strcmp(name, entry->name)) { - ocv->name = entry->name; - ocv->init = entry->init; - ocv->uninit = entry->uninit; - ocv->end_frame_filter = entry->end_frame_filter; - - if (!(ocv->priv = av_mallocz(entry->priv_size))) + const OCVFilterEntry *entry = &ocv_filter_entries[i]; + if (!strcmp(s->name, entry->name)) { + s->init = entry->init; + s->uninit = entry->uninit; + s->end_frame_filter = entry->end_frame_filter; + + if (!(s->priv = av_mallocz(entry->priv_size))) return AVERROR(ENOMEM); - return ocv->init(ctx, priv_args, opaque); + return s->init(ctx, s->params); } } - av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name); + av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", s->name); return AVERROR(EINVAL); } static av_cold void uninit(AVFilterContext *ctx) { - OCVContext *ocv = ctx->priv; + OCVContext *s = ctx->priv; - if (ocv->uninit) - ocv->uninit(ctx); - av_free(ocv->priv); - memset(ocv, 0, sizeof(*ocv)); + if (s->uninit) + s->uninit(ctx); + av_free(s->priv); } -static void end_frame(AVFilterLink *inlink) +static int filter_frame(AVFilterLink *inlink, AVFrame *in) { AVFilterContext *ctx = inlink->dst; - OCVContext *ocv = ctx->priv; + OCVContext *s = ctx->priv; AVFilterLink *outlink= inlink->dst->outputs[0]; - AVFilterBufferRef *inpicref = inlink ->cur_buf; - AVFilterBufferRef *outpicref = outlink->out_buf; + AVFrame *out; IplImage inimg, outimg; - fill_iplimage_from_picref(&inimg , inpicref , inlink->format); - fill_iplimage_from_picref(&outimg, outpicref, inlink->format); - ocv->end_frame_filter(ctx, &inimg, &outimg); - fill_picref_from_iplimage(outpicref, &outimg, inlink->format); + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + + fill_iplimage_from_frame(&inimg , in , inlink->format); + fill_iplimage_from_frame(&outimg, out, inlink->format); + s->end_frame_filter(ctx, &inimg, &outimg); + fill_frame_from_iplimage(out, &outimg, inlink->format); + + av_frame_free(&in); - avfilter_unref_buffer(inpicref); - avfilter_draw_slice(outlink, 0, outlink->h, 1); - avfilter_end_frame(outlink); - avfilter_unref_buffer(outpicref); + return ff_filter_frame(outlink, out); } -AVFilter avfilter_vf_ocv = { +#define OFFSET(x) offsetof(OCVContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM +static const AVOption options[] = { + { "filter_name", NULL, OFFSET(name), AV_OPT_TYPE_STRING, .flags = FLAGS }, + { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS }, + { NULL }, +}; + +static const AVClass ocv_class = { + .class_name = "ocv", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const AVFilterPad avfilter_vf_ocv_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad avfilter_vf_ocv_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_ocv = { .name = "ocv", .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."), .priv_size = sizeof(OCVContext), + .priv_class = &ocv_class, .query_formats = query_formats, .init = init, .uninit = uninit, - .inputs = (AVFilterPad[]) {{ .name = "default", - .type = AVMEDIA_TYPE_VIDEO, - .draw_slice = null_draw_slice, - .end_frame = end_frame, - .min_perms = AV_PERM_READ }, - { .name = NULL}}, + .inputs = avfilter_vf_ocv_inputs, - .outputs = (AVFilterPad[]) {{ .name = "default", - .type = AVMEDIA_TYPE_VIDEO, }, - { .name = NULL}}, + .outputs = avfilter_vf_ocv_outputs, };