[FFmpeg-devel] [PATCH 2/4] Implement ocv_dilate libopencv filter wrapper.
Stefano Sabatini
stefano.sabatini-lala
Wed Nov 10 23:14:43 CET 2010
On date Saturday 2010-10-30 12:58:09 +0200, Michael Niedermayer encoded:
> On Sun, Oct 10, 2010 at 06:50:00PM +0200, Stefano Sabatini wrote:
[...]
> > 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
I don't want to add a dependency on libavformat just for reading a
file.
> > + 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
Fixed.
> > + *cols = FFMAX(*cols, w); w = 0;
> > + } else
> > + w++;
>
> overflow
Fixed.
> > + }
> > + 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
Should be fixed.
> > +
> > + /* 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
Put under ifdef DEBUG.
> > +
> > + 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?
Should be fixed as well.
Also add more controls on the input values.
--
FFmpeg = Fantastic & Free Mystic Philosofic Explosive Gymnast
More information about the ffmpeg-devel
mailing list