X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_delogo.c;h=6069c30163187f7d2313167ac06d1a697984bc1b;hb=3a370868dc33061a20d1fd99274e65167d7a78ac;hp=065d0936415b3bf0613d03f12665610a7cc39ae7;hpb=185aa5e896e15ae96145609944bfc6bbb239bc64;p=ffmpeg diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c index 065d0936415..6069c301631 100644 --- a/libavfilter/vf_delogo.c +++ b/libavfilter/vf_delogo.c @@ -31,10 +31,51 @@ #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 +}; + +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 @@ -126,7 +167,7 @@ static void apply_delogo(uint8_t *dst, int dst_linesize, botleft[x-logo_x1-1] + botleft[x-logo_x1+1]) * weightb; weight = (weightl + weightr + weightt + weightb) * 3U; - interp = ROUNDED_DIV(interp, weight); + interp = (interp + (weight >> 1)) / weight; if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) { @@ -156,26 +197,34 @@ 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 }, -#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 }, - { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, -#endif - { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, FLAGS }, + { "x", "set logo x position", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS }, + { "y", "set logo y position", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS }, + { "w", "set logo width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS }, + { "h", "set logo height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "-1" }, 0, 0, FLAGS }, + { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { NULL } }; AVFILTER_DEFINE_CLASS(delogo); +static av_cold void uninit(AVFilterContext *ctx) +{ + DelogoContext *s = ctx->priv; + + av_expr_free(s->x_pexpr); s->x_pexpr = NULL; + av_expr_free(s->y_pexpr); s->y_pexpr = NULL; + av_expr_free(s->w_pexpr); s->w_pexpr = NULL; + av_expr_free(s->h_pexpr); s->h_pexpr = NULL; +} + static int query_formats(AVFilterContext *ctx) { @@ -194,6 +243,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) { \ @@ -205,16 +266,8 @@ static av_cold int init(AVFilterContext *ctx) CHECK_UNSET_OPT(w); CHECK_UNSET_OPT(h); -#if LIBAVFILTER_VERSION_MAJOR < 7 - if (s->band == 0) { /* Unset, use default */ - av_log(ctx, AV_LOG_WARNING, "Note: default band value was changed from 4 to 1.\n"); - s->band = 1; - } else if (s->band != 1) { - av_log(ctx, AV_LOG_WARNING, "Option band is deprecated.\n"); - } -#else s->band = 1; -#endif + av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n", s->x, s->y, s->w, s->h, s->band, s->show); @@ -251,6 +304,40 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) int direct = 0; int plane; AVRational sar; + int ret; + + 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 (s->x + (s->band - 1) <= 0 || s->x + s->w - (s->band*2 - 2) > inlink->w || + s->y + (s->band - 1) <= 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) { + av_log(s, AV_LOG_WARNING, "Logo area is outside of the frame," + " auto set the area inside of the frame\n"); + } + + if (s->x + (s->band - 1) <= 0) + s->x = 1 + s->band; + if (s->y + (s->band - 1) <= 0) + s->y = 1 + s->band; + if (s->x + s->w - (s->band*2 - 2) > inlink->w) + s->w = inlink->w - s->x - (s->band*2 - 2); + if (s->y + s->h - (s->band*2 - 2) > inlink->h) + s->h = inlink->h - s->y - (s->band*2 - 2); + + ret = config_input(inlink); + if (ret < 0) { + av_frame_free(&in); + return ret; + } + + s->w += s->band*2; + s->h += s->band*2; + s->x -= s->band; + s->y -= s->band; if (av_frame_is_writable(in)) { direct = 1; @@ -317,6 +404,7 @@ AVFilter ff_vf_delogo = { .priv_size = sizeof(DelogoContext), .priv_class = &delogo_class, .init = init, + .uninit = uninit, .query_formats = query_formats, .inputs = avfilter_vf_delogo_inputs, .outputs = avfilter_vf_delogo_outputs,