[FFmpeg-devel] [PATCH 2/4] Implement ocv_dilate libopencv filter wrapper.
Michael Niedermayer
michaelni
Sat Oct 30 12:58:09 CEST 2010
On Sun, Oct 10, 2010 at 06:50:00PM +0200, Stefano Sabatini wrote:
> On date Monday 2010-09-13 18:31:59 +0200, Michael Niedermayer encoded:
> > On Sun, Sep 12, 2010 at 08:19:48PM +0200, Stefano Sabatini wrote:
> > > On date Sunday 2010-09-12 13:07:45 +0200, Michael Niedermayer encoded:
> > > > On Sat, Sep 11, 2010 at 03:59:18PM +0200, Stefano Sabatini wrote:
> > > > > ---
> > > > > configure | 1 +
> > > > > libavfilter/Makefile | 1 +
> > > > > libavfilter/allfilters.c | 1 +
> > > > > libavfilter/vf_libopencv.c | 158 ++++++++++++++++++++++++++++++++++++++++++++
> > > > > 4 files changed, 161 insertions(+), 0 deletions(-)
> > > > >
> > > > > diff --git a/configure b/configure
> > > > > index 26db167..9453fc9 100755
> > > > > --- a/configure
> > > > > +++ b/configure
> > > > > @@ -1394,6 +1394,7 @@ udp_protocol_deps="network"
> > > > >
> > > > > # filters
> > > > > ocv_smooth_filter_deps="libopencv"
> > > > > +ocv_dilate_filter_deps="libopencv"
> > > > >
> > > > > # libraries
> > > > > avdevice_deps="avcodec avformat"
> > > > > diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> > > > > index a15c3f1..394b674 100644
> > > > > --- a/libavfilter/Makefile
> > > > > +++ b/libavfilter/Makefile
> > > > > @@ -23,6 +23,7 @@ OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o
> > > > > OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
> > > > > OBJS-$(CONFIG_NOFORMAT_FILTER) += vf_format.o
> > > > > OBJS-$(CONFIG_NULL_FILTER) += vf_null.o
> > > > > +OBJS-$(CONFIG_OCV_DILATE_FILTER) += vf_libopencv.o
> > > > > OBJS-$(CONFIG_OCV_SMOOTH_FILTER) += vf_libopencv.o
> > > > > OBJS-$(CONFIG_PAD_FILTER) += vf_pad.o
> > > > > OBJS-$(CONFIG_PIXDESCTEST_FILTER) += vf_pixdesctest.o
> > > > > diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> > > > > index fb84968..da1fedb 100644
> > > > > --- a/libavfilter/allfilters.c
> > > > > +++ b/libavfilter/allfilters.c
> > > > > @@ -43,6 +43,7 @@ void avfilter_register_all(void)
> > > > > REGISTER_FILTER (HFLIP, hflip, vf);
> > > > > REGISTER_FILTER (NOFORMAT, noformat, vf);
> > > > > REGISTER_FILTER (NULL, null, vf);
> > > > > + REGISTER_FILTER (OCV_DILATE, ocv_dilate, vf);
> > > > > REGISTER_FILTER (OCV_SMOOTH, ocv_smooth, vf);
> > > > > REGISTER_FILTER (PAD, pad, vf);
> > > > > REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
> > > > > diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
> > > > > index 63d5b61..6c535f0 100644
> > > > > --- a/libavfilter/vf_libopencv.c
> > > > > +++ b/libavfilter/vf_libopencv.c
> > > > > @@ -63,6 +63,164 @@ static int query_formats(AVFilterContext *ctx)
> > > > >
> > > > > static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
> > > > >
> > > > > +static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename, void *log_ctx)
> > > > > +{
> > > > > + char *p, *buf;
> > > > > + size_t size;
> > > > > + int i, j, w;
> > > > > + FILE *f = fopen(filename, "rb");
> > > > > + char *line;
> > > > > +
> > > > > + *cols = *rows = 0;
> > > > > +
> > > > > + if (!f) {
> > > > > + av_log(log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, strerror(errno));
> > > > > + return AVERROR(errno);
> > > > > + }
> > > > > + fseek(f, 0, SEEK_END);
> > > > > + size = ftell(f);
> > > > > + fseek(f, 0, SEEK_SET);
> > > > > + buf = av_malloc(size + 1);
> > > > > + if (!buf) {
> > > > > + fclose(f);
> > > > > + return AVERROR(ENOMEM);
> > > > > + }
> > > > > + fread(buf, 1, size, f);
> > > > > + buf[size++] = 0;
> > > > > + fclose(f);
> > > > > +
> > > > > + /* prescan file to get the number of lines and the maximum width */
> > > > > + w = 0;
> > > > > + for (i = 0; i < size; i++) {
> > > > > + if (buf[i] == '\n') {
> > > > > + (*rows)++;
> > > > > + *cols = FFMAX(*cols, w); w = 0;
> > > > > + } else
> > > > > + w++;
> > > > > + }
> > > >
> > > > rows & cols can overflow for large files
> > > >
> > > >
> > > > > + av_log(log_ctx, AV_LOG_DEBUG, "rows:%d cols:%d\n", *rows, *cols);
> > > > > +
> > > > > + if (!(*values = av_mallocz(sizeof(int) * *rows * *cols)))
> > > > > + return AVERROR(ENOMEM);
> > > >
> > > > memleak and integer overflow
> > > >
> > > > and anyway, it should be possible to specify things on the command line and
> > > > not require a file.
> > > > and please explain what this filter does and why it accesses files
> > > > like this
> > >
> > > That function get a shape from a file, this is more easily specified
> > > writing the shape right into a file, e.g. by writing:
> > >
> > > *
> > > ***
> > > *****
> > > ***
> > > *
> >
> > does it make sense for this to be a second input to the filter and have a
> > filter that reads from files ?
>
> I considered it but looks much more complicate (we need a file->image
> converter, also the file needs to be read just one time during the
> configuration stage, rather when the filtering is already started and
> we can't abort anymore).
>
> Patch updated, regards.
> --
> FFmpeg = Furious and Fiendish Mastering Powerful Epic Generator
> vf_libopencv.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 131 insertions(+)
> 203ad47ad53a8be3e06fbad25345db7e34c2296c 0002-Add-dilate-libopencv-filter.patch
> From c182720ef3042269cb580f4bc1cd3d27a396ddcf Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefano.sabatini-lala at poste.it>
> Date: Sat, 2 Oct 2010 17:03:38 +0200
> Subject: [PATCH 2/3] Add dilate libopencv filter.
>
> ---
> libavfilter/vf_libopencv.c | 131 ++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 131 insertions(+), 0 deletions(-)
>
> diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
> index 0e3da4d..4f787ad 100644
> --- a/libavfilter/vf_libopencv.c
> +++ b/libavfilter/vf_libopencv.c
> @@ -127,6 +127,136 @@ static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplIm
> cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
> }
>
> +static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename, void *log_ctx)
> +{
> + char *p, *buf;
> + size_t size;
> + int i, j, w;
> + FILE *f = fopen(filename, "rb");
Should use URLProtocol
> + char *line;
> +
> + *cols = *rows = 0;
> +
> + if (!f) {
> + av_log(log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, strerror(errno));
> + return AVERROR(errno);
> + }
> + fseek(f, 0, SEEK_END);
> + size = ftell(f);
> + fseek(f, 0, SEEK_SET);
> + buf = av_malloc(size + 1);
> + if (!buf) {
> + fclose(f);
> + return AVERROR(ENOMEM);
> + }
> + fread(buf, 1, size, f);
> + buf[size++] = 0;
> + fclose(f);
> +
> + /* prescan file to get the number of lines and the maximum width */
> + w = 0;
> + for (i = 0; i < size; i++) {
> + if (buf[i] == '\n') {
> + (*rows)++;
overflow
> + *cols = FFMAX(*cols, w); w = 0;
> + } else
> + w++;
overflow
> + }
> + av_log(log_ctx, AV_LOG_DEBUG, "rows:%d cols:%d\n", *rows, *cols);
> +
> + if (*rows > INT_MAX / *cols) {
> + av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n",
> + *rows, *cols);
> + return AVERROR_INVALIDDATA;
> + }
> + if (!(*values = av_mallocz(sizeof(int) * *rows * *cols)))
> + return AVERROR(ENOMEM);
overflow
> +
> + /* fill *values */
> + p = buf;
> + for (i = 0; i < *rows; i++) {
> + for (j = 0;; j++) {
> + /* av_log(log_ctx, AV_LOG_DEBUG, "%d:%d -> '%c'\n", i, j, *p == '\n' ? 'N' : *p); */
> + if (*p == '\n') {
> + p++; break;
> + } else
> + (*values)[*cols*i + j] = !!isgraph(*(p++));
> + }
> + }
> +
> + if (!(line = av_malloc(*cols + 1)))
> + return AVERROR(ENOMEM);
> + for (i = 0; i < *rows; i++) {
> + for (j = 0; j < *cols; j++)
> + line[j] = (*values)[i * *cols + j] ? '@' : ' ';
> + line[j] = 0;
> + av_log(log_ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
> + }
> + av_free(line);
useless
> +
> + return 0;
> +}
> +
> +static int parse_iplconvkernel(IplConvKernel **kernel, char *buf, void *log_ctx)
> +{
> + char values_file_str[128], shape_str[128];
> + int cols, rows, anchor_x, anchor_y, shape = CV_SHAPE_RECT;
> + int *values = NULL, ret;
> +
> + sscanf(buf, "%dx%d+%dx%d/%127[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, values_file_str);
> + if (!strcmp(shape_str, "rect" )) shape = CV_SHAPE_RECT;
> + else if (!strcmp(shape_str, "cross" )) shape = CV_SHAPE_CROSS;
> + else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
> + else if (!strcmp(shape_str, "custom" )) {
> + shape = CV_SHAPE_CUSTOM;
> + if ((ret = read_shape_from_file(&cols, &rows, &values, values_file_str, log_ctx)) < 0)
> + return ret;
> + } else {
> + av_log(log_ctx, AV_LOG_ERROR, "Shape type '%s' unknown\n.", shape_str);
> + return AVERROR(EINVAL);
> + }
> +
> + *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
> + av_freep(&values);
> + if (!*kernel)
> + return AVERROR(ENOMEM);
> +
> + av_log(log_ctx, AV_LOG_INFO, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n",
> + rows, cols, anchor_x, anchor_y, shape_str);
> + return 0;
> +}
> +
> +typedef struct {
> + int iterations_nb;
> + IplConvKernel *kernel;
> +} DilateContext;
> +
> +static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opaque)
> +{
> + OCVContext *ocv = ctx->priv;
> + DilateContext *dilate = ocv->priv;
> + char kernel_str[256] = "3x3+0x0/rect";
> + int ret;
> +
> + dilate->iterations_nb = 1;
> +
> + if (args)
> + sscanf(args, "%d:%255c", &dilate->iterations_nb, kernel_str);
is something null terminating this string?
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
The greatest way to live with honor in this world is to be what we pretend
to be. -- Socrates
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20101030/94381566/attachment.pgp>
More information about the ffmpeg-devel
mailing list