2010-09-14 16:21:13 +03:00
|
|
|
/*
|
2010-11-28 12:22:58 +02:00
|
|
|
* Copyright (c) 2010 Stefano Sabatini
|
2010-09-14 16:21:13 +03:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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,
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* libopencv wrapper functions
|
|
|
|
*/
|
|
|
|
|
2016-01-22 00:17:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
#if HAVE_OPENCV2_CORE_CORE_C_H
|
|
|
|
#include <opencv2/core/core_c.h>
|
|
|
|
#include <opencv2/imgproc/imgproc_c.h>
|
|
|
|
#else
|
2010-09-16 11:47:34 +03:00
|
|
|
#include <opencv/cv.h>
|
2011-05-20 12:36:16 +03:00
|
|
|
#include <opencv/cxcore.h>
|
2016-01-22 00:17:47 +02:00
|
|
|
#endif
|
2010-12-26 13:27:05 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2012-08-16 14:24:26 +03:00
|
|
|
#include "libavutil/common.h"
|
2010-12-26 13:27:05 +02:00
|
|
|
#include "libavutil/file.h"
|
2013-02-25 23:21:29 +03:00
|
|
|
#include "libavutil/opt.h"
|
2010-09-14 16:21:13 +03:00
|
|
|
#include "avfilter.h"
|
2012-06-07 00:14:36 +03:00
|
|
|
#include "formats.h"
|
2012-11-28 22:10:51 +03:00
|
|
|
#include "internal.h"
|
2012-06-07 00:14:36 +03:00
|
|
|
#include "video.h"
|
2010-09-14 16:21:13 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt)
|
2010-09-14 16:21:13 +03:00
|
|
|
{
|
|
|
|
IplImage *tmpimg;
|
|
|
|
int depth, channels_nb;
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
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; }
|
2010-09-14 16:21:13 +03:00
|
|
|
else return;
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
|
2010-09-14 16:21:13 +03:00
|
|
|
*img = *tmpimg;
|
2012-11-28 10:41:07 +03:00
|
|
|
img->imageData = img->imageDataOrigin = frame->data[0];
|
2010-09-14 16:21:13 +03:00
|
|
|
img->dataOrder = IPL_DATA_ORDER_PIXEL;
|
|
|
|
img->origin = IPL_ORIGIN_TL;
|
2012-11-28 10:41:07 +03:00
|
|
|
img->widthStep = frame->linesize[0];
|
2010-09-14 16:21:13 +03:00
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static void fill_frame_from_iplimage(AVFrame *frame, const IplImage *img, enum AVPixelFormat pixfmt)
|
2010-09-14 16:21:13 +03:00
|
|
|
{
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->linesize[0] = img->widthStep;
|
|
|
|
frame->data[0] = img->imageData;
|
2010-09-14 16:21:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-06 13:10:34 +03:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
|
2010-09-14 16:21:13 +03:00
|
|
|
};
|
2021-08-10 01:25:31 +02:00
|
|
|
return ff_set_common_formats_from_list(ctx, pix_fmts);
|
2010-09-14 16:21:13 +03:00
|
|
|
}
|
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct OCVContext {
|
2013-02-25 23:21:29 +03:00
|
|
|
const AVClass *class;
|
|
|
|
char *name;
|
|
|
|
char *params;
|
2013-04-12 13:37:33 +03:00
|
|
|
int (*init)(AVFilterContext *ctx, const char *args);
|
2010-12-23 19:24:19 +02:00
|
|
|
void (*uninit)(AVFilterContext *ctx);
|
|
|
|
void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
|
|
|
|
void *priv;
|
|
|
|
} OCVContext;
|
2010-09-14 16:21:13 +03:00
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct SmoothContext {
|
2010-09-14 16:21:13 +03:00
|
|
|
int type;
|
|
|
|
int param1, param2;
|
|
|
|
double param3, param4;
|
|
|
|
} SmoothContext;
|
|
|
|
|
2013-04-12 13:37:33 +03:00
|
|
|
static av_cold int smooth_init(AVFilterContext *ctx, const char *args)
|
2010-09-14 16:21:13 +03:00
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
|
|
|
SmoothContext *smooth = s->priv;
|
2010-09-14 16:21:13 +03:00
|
|
|
char type_str[128] = "gaussian";
|
|
|
|
|
|
|
|
smooth->param1 = 3;
|
|
|
|
smooth->param2 = 0;
|
|
|
|
smooth->param3 = 0.0;
|
|
|
|
smooth->param4 = 0.0;
|
|
|
|
|
|
|
|
if (args)
|
2013-02-25 23:21:29 +03:00
|
|
|
sscanf(args, "%127[^|]|%d|%d|%lf|%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
|
2010-09-14 16:21:13 +03:00
|
|
|
|
|
|
|
if (!strcmp(type_str, "blur" )) smooth->type = CV_BLUR;
|
|
|
|
else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
|
|
|
|
else if (!strcmp(type_str, "median" )) smooth->type = CV_MEDIAN;
|
|
|
|
else if (!strcmp(type_str, "gaussian" )) smooth->type = CV_GAUSSIAN;
|
|
|
|
else if (!strcmp(type_str, "bilateral" )) smooth->type = CV_BILATERAL;
|
|
|
|
else {
|
2012-08-03 15:56:31 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown.\n", type_str);
|
2010-09-14 16:21:13 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smooth->param1 < 0 || !(smooth->param1%2)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid value '%d' for param1, it has to be a positive odd number\n",
|
|
|
|
smooth->param1);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
|
|
|
|
(smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
|
|
|
|
smooth->param2);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
|
2010-09-14 16:21:13 +03:00
|
|
|
type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-23 19:24:19 +02:00
|
|
|
static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
|
2010-09-14 16:21:13 +03:00
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
|
|
|
SmoothContext *smooth = s->priv;
|
2010-12-23 19:24:19 +02:00
|
|
|
cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
|
|
|
|
}
|
|
|
|
|
2010-12-26 13:27:05 +02:00
|
|
|
static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename,
|
|
|
|
void *log_ctx)
|
|
|
|
{
|
|
|
|
uint8_t *buf, *p, *pend;
|
|
|
|
size_t size;
|
|
|
|
int ret, i, j, w;
|
|
|
|
|
|
|
|
if ((ret = av_file_map(filename, &buf, &size, 0, log_ctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* prescan file to get the number of lines and the maximum width */
|
|
|
|
w = 0;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (buf[i] == '\n') {
|
|
|
|
if (*rows == INT_MAX) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of rows in the file\n");
|
2017-01-11 03:59:55 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto end;
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
++(*rows);
|
|
|
|
*cols = FFMAX(*cols, w);
|
|
|
|
w = 0;
|
|
|
|
} else if (w == INT_MAX) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Overflow on the number of columns in the file\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
w++;
|
|
|
|
}
|
2011-04-21 11:31:48 +03:00
|
|
|
if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
|
2010-12-26 13:27:05 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n",
|
|
|
|
*rows, *cols);
|
2017-01-11 03:59:55 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto end;
|
|
|
|
}
|
2021-09-14 21:31:53 +02:00
|
|
|
if (!(*values = av_calloc(sizeof(int) * *rows, *cols))) {
|
2017-01-11 03:59:55 +02:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fill *values */
|
|
|
|
p = buf;
|
|
|
|
pend = buf + size-1;
|
|
|
|
for (i = 0; i < *rows; i++) {
|
|
|
|
for (j = 0;; j++) {
|
|
|
|
if (p > pend || *p == '\n') {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
} else
|
2013-03-04 00:06:48 +03:00
|
|
|
(*values)[*cols*i + j] = !!av_isgraph(*(p++));
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 03:59:55 +02:00
|
|
|
|
|
|
|
end:
|
2010-12-26 13:27:05 +02:00
|
|
|
av_file_unmap(buf, size);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-01-23 18:15:37 +02:00
|
|
|
int *values = NULL, ret = 0;
|
2010-12-26 13:27:05 +02:00
|
|
|
|
|
|
|
sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
|
|
|
|
|
|
|
|
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, shape_filename, log_ctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2012-08-03 15:56:31 +03:00
|
|
|
"Shape unspecified or type '%s' unknown.\n", shape_str);
|
2015-01-23 18:15:37 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto out;
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rows <= 0 || cols <= 0) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid non-positive values for shape size %dx%d\n", cols, rows);
|
2015-01-23 18:15:37 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto out;
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-01-23 18:15:37 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto out;
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
|
2015-01-23 18:15:37 +02:00
|
|
|
if (!*kernel) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto out;
|
|
|
|
}
|
2010-12-26 13:27:05 +02:00
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(log_ctx, AV_LOG_VERBOSE, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n",
|
2010-12-26 13:27:05 +02:00
|
|
|
rows, cols, anchor_x, anchor_y, shape_str);
|
2015-01-23 18:15:37 +02:00
|
|
|
out:
|
|
|
|
av_freep(&values);
|
|
|
|
return ret;
|
2010-12-26 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct DilateContext {
|
2010-12-26 13:27:05 +02:00
|
|
|
int nb_iterations;
|
|
|
|
IplConvKernel *kernel;
|
|
|
|
} DilateContext;
|
|
|
|
|
2013-04-12 13:37:33 +03:00
|
|
|
static av_cold int dilate_init(AVFilterContext *ctx, const char *args)
|
2010-12-26 13:27:05 +02:00
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
|
|
|
DilateContext *dilate = s->priv;
|
2010-12-26 13:27:05 +02:00
|
|
|
char default_kernel_str[] = "3x3+0x0/rect";
|
2015-02-01 03:02:05 +02:00
|
|
|
char *kernel_str = NULL;
|
2010-12-26 13:27:05 +02:00
|
|
|
const char *buf = args;
|
|
|
|
int ret;
|
|
|
|
|
2015-02-01 03:02:05 +02:00
|
|
|
if (args) {
|
2013-02-25 23:21:29 +03:00
|
|
|
kernel_str = av_get_token(&buf, "|");
|
2015-02-04 00:34:10 +02:00
|
|
|
|
2015-02-01 03:02:05 +02:00
|
|
|
if (!kernel_str)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = parse_iplconvkernel(&dilate->kernel,
|
|
|
|
(!kernel_str || !*kernel_str) ? default_kernel_str
|
|
|
|
: kernel_str,
|
|
|
|
ctx);
|
2010-12-26 13:27:05 +02:00
|
|
|
av_free(kernel_str);
|
2015-02-01 03:02:05 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-12-26 13:27:05 +02:00
|
|
|
|
2013-04-12 14:01:17 +03:00
|
|
|
if (!buf || sscanf(buf, "|%d", &dilate->nb_iterations) != 1)
|
|
|
|
dilate->nb_iterations = 1;
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "iterations_nb:%d\n", dilate->nb_iterations);
|
2010-12-26 13:27:05 +02:00
|
|
|
if (dilate->nb_iterations <= 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid non-positive value '%d' for nb_iterations\n",
|
|
|
|
dilate->nb_iterations);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void dilate_uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
|
|
|
DilateContext *dilate = s->priv;
|
2010-12-26 13:27:05 +02:00
|
|
|
|
|
|
|
cvReleaseStructuringElement(&dilate->kernel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
|
|
|
DilateContext *dilate = s->priv;
|
2010-12-26 13:27:05 +02:00
|
|
|
cvDilate(inimg, outimg, dilate->kernel, dilate->nb_iterations);
|
|
|
|
}
|
|
|
|
|
2010-12-26 13:27:09 +02:00
|
|
|
static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
|
|
|
DilateContext *dilate = s->priv;
|
2010-12-26 13:27:09 +02:00
|
|
|
cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations);
|
|
|
|
}
|
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct OCVFilterEntry {
|
2010-12-23 19:24:19 +02:00
|
|
|
const char *name;
|
|
|
|
size_t priv_size;
|
2013-04-12 13:37:33 +03:00
|
|
|
int (*init)(AVFilterContext *ctx, const char *args);
|
2010-12-23 19:24:19 +02:00
|
|
|
void (*uninit)(AVFilterContext *ctx);
|
|
|
|
void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
|
|
|
|
} OCVFilterEntry;
|
|
|
|
|
2015-05-02 15:02:01 +02:00
|
|
|
static const OCVFilterEntry ocv_filter_entries[] = {
|
2010-12-26 13:27:05 +02:00
|
|
|
{ "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter },
|
2010-12-26 13:27:09 +02:00
|
|
|
{ "erode", sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter },
|
2010-12-23 19:24:19 +02:00
|
|
|
{ "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
|
|
|
|
};
|
|
|
|
|
2013-03-13 10:26:39 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2010-12-23 19:24:19 +02:00
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
2010-12-23 19:24:19 +02:00
|
|
|
int i;
|
|
|
|
|
2013-05-16 17:36:59 +03:00
|
|
|
if (!s->name) {
|
2013-04-12 13:44:23 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No libopencv filter name specified\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2010-12-23 19:24:19 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
|
2015-05-02 15:02:01 +02:00
|
|
|
const OCVFilterEntry *entry = &ocv_filter_entries[i];
|
2013-03-18 22:44:36 +03:00
|
|
|
if (!strcmp(s->name, entry->name)) {
|
|
|
|
s->init = entry->init;
|
|
|
|
s->uninit = entry->uninit;
|
|
|
|
s->end_frame_filter = entry->end_frame_filter;
|
2010-12-23 19:24:19 +02:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
if (!(s->priv = av_mallocz(entry->priv_size)))
|
2010-12-23 19:24:19 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2013-03-18 22:44:36 +03:00
|
|
|
return s->init(ctx, s->params);
|
2010-12-23 19:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", s->name);
|
2010-12-23 19:24:19 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
2010-12-23 19:24:19 +02:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
if (s->uninit)
|
|
|
|
s->uninit(ctx);
|
2014-12-19 16:04:34 +02:00
|
|
|
av_freep(&s->priv);
|
2010-12-23 19:24:19 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
2010-12-23 19:24:19 +02:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2013-03-18 22:44:36 +03:00
|
|
|
OCVContext *s = ctx->priv;
|
2010-12-23 19:24:19 +02:00
|
|
|
AVFilterLink *outlink= inlink->dst->outputs[0];
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *out;
|
2010-09-14 16:21:13 +03:00
|
|
|
IplImage inimg, outimg;
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
2012-11-28 22:10:51 +03:00
|
|
|
if (!out) {
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&in);
|
2012-11-28 22:10:51 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_copy_props(out, in);
|
2012-11-28 22:10:51 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
fill_iplimage_from_frame(&inimg , in , inlink->format);
|
|
|
|
fill_iplimage_from_frame(&outimg, out, inlink->format);
|
2013-03-18 22:44:36 +03:00
|
|
|
s->end_frame_filter(ctx, &inimg, &outimg);
|
2012-11-28 10:41:07 +03:00
|
|
|
fill_frame_from_iplimage(out, &outimg, inlink->format);
|
2010-09-14 16:21:13 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&in);
|
2012-11-28 22:10:51 +03:00
|
|
|
|
|
|
|
return ff_filter_frame(outlink, out);
|
2010-09-14 16:21:13 +03:00
|
|
|
}
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
#define OFFSET(x) offsetof(OCVContext, x)
|
2013-04-12 16:47:16 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
|
2013-05-27 14:28:17 +03:00
|
|
|
static const AVOption ocv_options[] = {
|
2013-02-25 23:21:29 +03:00
|
|
|
{ "filter_name", NULL, OFFSET(name), AV_OPT_TYPE_STRING, .flags = FLAGS },
|
|
|
|
{ "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2013-02-25 23:21:29 +03:00
|
|
|
};
|
|
|
|
|
2013-05-27 14:28:17 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(ocv);
|
2013-02-25 23:21:29 +03:00
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_ocv_inputs[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-11-28 22:10:51 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_ocv_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_ocv = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "ocv",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
|
|
|
|
.priv_size = sizeof(OCVContext),
|
|
|
|
.priv_class = &ocv_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(avfilter_vf_ocv_inputs),
|
|
|
|
FILTER_OUTPUTS(avfilter_vf_ocv_outputs),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2010-09-14 16:21:13 +03:00
|
|
|
};
|