[FFmpeg-devel,v2] avfilter/vf_delogo: support expr in delogo filter

Submitted by Steven Liu on Aug. 27, 2019, 7:17 a.m.

Details

Message ID 20190827071709.10394-1-lq@chinaffmpeg.org
State New
Headers show

Commit Message

Steven Liu Aug. 27, 2019, 7:17 a.m.
Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavfilter/vf_delogo.c | 72 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 4 deletions(-)

Comments

lance.lmwang@gmail.com Aug. 27, 2019, 3:29 p.m.
On Tue, Aug 27, 2019 at 03:17:09PM +0800, Steven Liu wrote:
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>  libavfilter/vf_delogo.c | 72 ++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 68 insertions(+), 4 deletions(-)
> 
> diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c
> index 065d093641..cbc7e87a1c 100644
> --- a/libavfilter/vf_delogo.c
> +++ b/libavfilter/vf_delogo.c
> @@ -31,10 +31,52 @@
>  #include "libavutil/imgutils.h"
>  #include "libavutil/opt.h"
>  #include "libavutil/pixdesc.h"
> +#include "libavutil/eval.h"
>  #include "avfilter.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> +static const char * const var_names[] = {
> +    "x",
> +    "y",
> +    "w",
> +    "h",
> +    "n",            ///< number of frame
> +    "t",            ///< timestamp expressed in seconds
> +    NULL
> +};
> +
> +enum var_name {
> +    VAR_X,
> +    VAR_Y,
> +    VAR_W,
> +    VAR_H,
> +    VAR_N,
> +    VAR_T,
> +    VAR_VARS_NB
> +};
> +#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
> +
> +static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
> +{
> +    int ret;
> +    AVExpr *old = NULL;
> +
> +    if (*pexpr)
> +        old = *pexpr;
> +    ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
> +    if (ret < 0) {
> +        av_log(log_ctx, AV_LOG_ERROR,
> +               "Error when parsing the expression '%s' for %s\n",
> +               expr, option);
> +        *pexpr = old;
> +        return ret;
> +    }
> +
> +    av_expr_free(old);
> +    return 0;
> +}
> +
>  
>  /**
>   * Apply a simple delogo algorithm to the image in src and put the
> @@ -156,16 +198,19 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
>  typedef struct DelogoContext {
>      const AVClass *class;
>      int x, y, w, h, band, show;
> +    char *x_expr, *y_expr, *w_expr, *h_expr;
> +    AVExpr *x_pexpr, *y_pexpr, *w_pexpr, *h_pexpr;
> +    double var_values[VAR_VARS_NB];
>  }  DelogoContext;
>  
>  #define OFFSET(x) offsetof(DelogoContext, x)
>  #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
>  
>  static const AVOption delogo_options[]= {
> -    { "x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
> -    { "y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
> -    { "w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
> -    { "h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
> +    { "x",    "set logo x position",       OFFSET(x_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
> +    { "y",    "set logo y position",       OFFSET(y_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
> +    { "w",    "set logo width",            OFFSET(w_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
> +    { "h",    "set logo height",           OFFSET(h_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
>  #if LIBAVFILTER_VERSION_MAJOR < 7
>      /* Actual default value for band/t is 1, set in init */
>      { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  0 },  0, INT_MAX, FLAGS },
> @@ -194,6 +239,18 @@ static int query_formats(AVFilterContext *ctx)
>  static av_cold int init(AVFilterContext *ctx)
>  {
>      DelogoContext *s = ctx->priv;
> +    int ret = 0;
> +
> +    if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
> +        (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0 ||
> +        (ret = set_expr(&s->w_pexpr, s->w_expr, "w", ctx)) < 0 ||
> +        (ret = set_expr(&s->h_pexpr, s->h_expr, "h", ctx)) < 0 )
> +        return ret;
> +
> +    s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
> +    s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
> +    s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
> +    s->h = av_expr_eval(s->h_pexpr, s->var_values, s);

it'll memory leak if not to use av_expr_free after uninit I think.

>  
>  #define CHECK_UNSET_OPT(opt)                                            \
>      if (s->opt == -1) {                                            \
> @@ -252,6 +309,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>      int plane;
>      AVRational sar;
>  
> +    s->var_values[VAR_N] = inlink->frame_count_out;
> +    s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
> +    s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
> +    s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
> +    s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
> +    s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
> +
>      if (av_frame_is_writable(in)) {
>          direct = 1;
>          out = in;
> -- 
> 2.17.2 (Apple Git-113)
> 
> 
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Steven Liu Aug. 27, 2019, 9:20 p.m.
> 在 2019年8月27日,23:29,Limin Wang <lance.lmwang@gmail.com> 写道:
> 
> On Tue, Aug 27, 2019 at 03:17:09PM +0800, Steven Liu wrote:
>> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
>> ---
>> libavfilter/vf_delogo.c | 72 ++++++++++++++++++++++++++++++++++++++---
>> 1 file changed, 68 insertions(+), 4 deletions(-)
>> 
>> diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c
>> index 065d093641..cbc7e87a1c 100644
>> --- a/libavfilter/vf_delogo.c
>> +++ b/libavfilter/vf_delogo.c
>> @@ -31,10 +31,52 @@
>> #include "libavutil/imgutils.h"
>> #include "libavutil/opt.h"
>> #include "libavutil/pixdesc.h"
>> +#include "libavutil/eval.h"
>> #include "avfilter.h"
>> #include "formats.h"
>> #include "internal.h"
>> #include "video.h"
>> +static const char * const var_names[] = {
>> +    "x",
>> +    "y",
>> +    "w",
>> +    "h",
>> +    "n",            ///< number of frame
>> +    "t",            ///< timestamp expressed in seconds
>> +    NULL
>> +};
>> +
>> +enum var_name {
>> +    VAR_X,
>> +    VAR_Y,
>> +    VAR_W,
>> +    VAR_H,
>> +    VAR_N,
>> +    VAR_T,
>> +    VAR_VARS_NB
>> +};
>> +#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
>> +
>> +static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
>> +{
>> +    int ret;
>> +    AVExpr *old = NULL;
>> +
>> +    if (*pexpr)
>> +        old = *pexpr;
>> +    ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
>> +    if (ret < 0) {
>> +        av_log(log_ctx, AV_LOG_ERROR,
>> +               "Error when parsing the expression '%s' for %s\n",
>> +               expr, option);
>> +        *pexpr = old;
>> +        return ret;
>> +    }
>> +
>> +    av_expr_free(old);
>> +    return 0;
>> +}
>> +
>> 
>> /**
>>  * Apply a simple delogo algorithm to the image in src and put the
>> @@ -156,16 +198,19 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
>> typedef struct DelogoContext {
>>     const AVClass *class;
>>     int x, y, w, h, band, show;
>> +    char *x_expr, *y_expr, *w_expr, *h_expr;
>> +    AVExpr *x_pexpr, *y_pexpr, *w_pexpr, *h_pexpr;
>> +    double var_values[VAR_VARS_NB];
>> }  DelogoContext;
>> 
>> #define OFFSET(x) offsetof(DelogoContext, x)
>> #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
>> 
>> static const AVOption delogo_options[]= {
>> -    { "x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
>> -    { "y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
>> -    { "w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
>> -    { "h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
>> +    { "x",    "set logo x position",       OFFSET(x_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
>> +    { "y",    "set logo y position",       OFFSET(y_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
>> +    { "w",    "set logo width",            OFFSET(w_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
>> +    { "h",    "set logo height",           OFFSET(h_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
>> #if LIBAVFILTER_VERSION_MAJOR < 7
>>     /* Actual default value for band/t is 1, set in init */
>>     { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  0 },  0, INT_MAX, FLAGS },
>> @@ -194,6 +239,18 @@ static int query_formats(AVFilterContext *ctx)
>> static av_cold int init(AVFilterContext *ctx)
>> {
>>     DelogoContext *s = ctx->priv;
>> +    int ret = 0;
>> +
>> +    if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
>> +        (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0 ||
>> +        (ret = set_expr(&s->w_pexpr, s->w_expr, "w", ctx)) < 0 ||
>> +        (ret = set_expr(&s->h_pexpr, s->h_expr, "h", ctx)) < 0 )
>> +        return ret;
>> +
>> +    s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
>> +    s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
>> +    s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
>> +    s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
> 
> it'll memory leak if not to use av_expr_free after uninit I think.
okay, will submit new version patch.
> 
>> 
>> #define CHECK_UNSET_OPT(opt)                                            \
>>     if (s->opt == -1) {                                            \
>> @@ -252,6 +309,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>>     int plane;
>>     AVRational sar;
>> 
>> +    s->var_values[VAR_N] = inlink->frame_count_out;
>> +    s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
>> +    s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
>> +    s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
>> +    s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
>> +    s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
>> +
>>     if (av_frame_is_writable(in)) {
>>         direct = 1;
>>         out = in;
>> -- 
>> 2.17.2 (Apple Git-113)
>> 
>> 
>> 
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>> 
>> To unsubscribe, visit link above, or email
>> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Thanks
Steven

Patch hide | download patch | download mbox

diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c
index 065d093641..cbc7e87a1c 100644
--- a/libavfilter/vf_delogo.c
+++ b/libavfilter/vf_delogo.c
@@ -31,10 +31,52 @@ 
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/eval.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
+static const char * const var_names[] = {
+    "x",
+    "y",
+    "w",
+    "h",
+    "n",            ///< number of frame
+    "t",            ///< timestamp expressed in seconds
+    NULL
+};
+
+enum var_name {
+    VAR_X,
+    VAR_Y,
+    VAR_W,
+    VAR_H,
+    VAR_N,
+    VAR_T,
+    VAR_VARS_NB
+};
+#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
+
+static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
+{
+    int ret;
+    AVExpr *old = NULL;
+
+    if (*pexpr)
+        old = *pexpr;
+    ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
+    if (ret < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Error when parsing the expression '%s' for %s\n",
+               expr, option);
+        *pexpr = old;
+        return ret;
+    }
+
+    av_expr_free(old);
+    return 0;
+}
+
 
 /**
  * Apply a simple delogo algorithm to the image in src and put the
@@ -156,16 +198,19 @@  static void apply_delogo(uint8_t *dst, int dst_linesize,
 typedef struct DelogoContext {
     const AVClass *class;
     int x, y, w, h, band, show;
+    char *x_expr, *y_expr, *w_expr, *h_expr;
+    AVExpr *x_pexpr, *y_pexpr, *w_pexpr, *h_pexpr;
+    double var_values[VAR_VARS_NB];
 }  DelogoContext;
 
 #define OFFSET(x) offsetof(DelogoContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption delogo_options[]= {
-    { "x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
-    { "y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
-    { "w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
-    { "h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "x",    "set logo x position",       OFFSET(x_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "y",    "set logo y position",       OFFSET(y_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "w",    "set logo width",            OFFSET(w_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "h",    "set logo height",           OFFSET(h_expr),    AV_OPT_TYPE_STRING, { .str = "-1" }, CHAR_MIN, CHAR_MAX, FLAGS },
 #if LIBAVFILTER_VERSION_MAJOR < 7
     /* Actual default value for band/t is 1, set in init */
     { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  0 },  0, INT_MAX, FLAGS },
@@ -194,6 +239,18 @@  static int query_formats(AVFilterContext *ctx)
 static av_cold int init(AVFilterContext *ctx)
 {
     DelogoContext *s = ctx->priv;
+    int ret = 0;
+
+    if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
+        (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0 ||
+        (ret = set_expr(&s->w_pexpr, s->w_expr, "w", ctx)) < 0 ||
+        (ret = set_expr(&s->h_pexpr, s->h_expr, "h", ctx)) < 0 )
+        return ret;
+
+    s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
+    s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
+    s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
+    s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
 
 #define CHECK_UNSET_OPT(opt)                                            \
     if (s->opt == -1) {                                            \
@@ -252,6 +309,13 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     int plane;
     AVRational sar;
 
+    s->var_values[VAR_N] = inlink->frame_count_out;
+    s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
+    s->x = av_expr_eval(s->x_pexpr, s->var_values, s);
+    s->y = av_expr_eval(s->y_pexpr, s->var_values, s);
+    s->w = av_expr_eval(s->w_pexpr, s->var_values, s);
+    s->h = av_expr_eval(s->h_pexpr, s->var_values, s);
+
     if (av_frame_is_writable(in)) {
         direct = 1;
         out = in;