avfilter/vf_mix: add support for commands

pull/371/head
Paul B Mahol 4 years ago
parent 3583d2a837
commit d0a24bfad1
  1. 18
      doc/filters.texi
  2. 87
      libavfilter/vf_mix.c

@ -14665,6 +14665,15 @@ The duration of the first input.
@end table @end table
@end table @end table
@subsection Commands
This filter supports the following commands:
@table @option
@item weights
@item scale
Syntax is same as option with same name.
@end table
@section mpdecimate @section mpdecimate
Drop frames that do not differ greatly from the previous frame in Drop frames that do not differ greatly from the previous frame in
@ -19701,6 +19710,15 @@ tmix=frames=3:weights="-1 2 -1":scale=1
@end example @end example
@end itemize @end itemize
@subsection Commands
This filter supports the following commands:
@table @option
@item weights
@item scale
Syntax is same as option with same name.
@end table
@anchor{tonemap} @anchor{tonemap}
@section tonemap @section tonemap
Tone map colors from different dynamic ranges. Tone map colors from different dynamic ranges.

@ -67,11 +67,44 @@ static int query_formats(AVFilterContext *ctx)
return ff_set_common_formats(ctx, formats); return ff_set_common_formats(ctx, formats);
} }
static av_cold int init(AVFilterContext *ctx) static int parse_weights(AVFilterContext *ctx)
{ {
MixContext *s = ctx->priv; MixContext *s = ctx->priv;
char *p, *arg, *saveptr = NULL; char *p, *arg, *saveptr = NULL;
int i, ret, last = 0; int i, last = 0;
s->wfactor = 0.f;
p = s->weights_str;
for (i = 0; i < s->nb_inputs; i++) {
if (!(arg = av_strtok(p, " |", &saveptr)))
break;
p = NULL;
if (av_sscanf(arg, "%f", &s->weights[i]) != 1) {
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for weights[%d].\n", i);
return AVERROR(EINVAL);
}
s->wfactor += s->weights[i];
last = i;
}
for (; i < s->nb_inputs; i++) {
s->weights[i] = s->weights[last];
s->wfactor += s->weights[i];
}
if (s->scale == 0) {
s->wfactor = 1 / s->wfactor;
} else {
s->wfactor = s->scale;
}
return 0;
}
static av_cold int init(AVFilterContext *ctx)
{
MixContext *s = ctx->priv;
int ret;
s->tmix = !strcmp(ctx->filter->name, "tmix"); s->tmix = !strcmp(ctx->filter->name, "tmix");
@ -84,7 +117,7 @@ static av_cold int init(AVFilterContext *ctx)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
if (!s->tmix) { if (!s->tmix) {
for (i = 0; i < s->nb_inputs; i++) { for (int i = 0; i < s->nb_inputs; i++) {
AVFilterPad pad = { 0 }; AVFilterPad pad = { 0 };
pad.type = AVMEDIA_TYPE_VIDEO; pad.type = AVMEDIA_TYPE_VIDEO;
@ -99,30 +132,7 @@ static av_cold int init(AVFilterContext *ctx)
} }
} }
p = s->weights_str; return parse_weights(ctx);
for (i = 0; i < s->nb_inputs; i++) {
if (!(arg = av_strtok(p, " ", &saveptr)))
break;
p = NULL;
if (av_sscanf(arg, "%f", &s->weights[i]) != 1) {
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for weights[%d].\n", i);
return AVERROR(EINVAL);
}
s->wfactor += s->weights[i];
last = i;
}
for (; i < s->nb_inputs; i++) {
s->weights[i] = s->weights[last];
s->wfactor += s->weights[i];
}
if (s->scale == 0) {
s->wfactor = 1 / s->wfactor;
} else {
s->wfactor = s->scale;
}
return 0;
} }
typedef struct ThreadData { typedef struct ThreadData {
@ -303,6 +313,18 @@ static av_cold void uninit(AVFilterContext *ctx)
av_freep(&s->frames); av_freep(&s->frames);
} }
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
char *res, int res_len, int flags)
{
int ret;
ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
if (ret < 0)
return ret;
return parse_weights(ctx);
}
static int activate(AVFilterContext *ctx) static int activate(AVFilterContext *ctx)
{ {
MixContext *s = ctx->priv; MixContext *s = ctx->priv;
@ -311,11 +333,12 @@ static int activate(AVFilterContext *ctx)
#define OFFSET(x) offsetof(MixContext, x) #define OFFSET(x) offsetof(MixContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
#define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption mix_options[] = { static const AVOption mix_options[] = {
{ "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT16_MAX, .flags = FLAGS }, { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT16_MAX, .flags = FLAGS },
{ "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = FLAGS }, { "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = TFLAGS },
{ "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS }, { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = TFLAGS },
{ "duration", "how to determine end of stream", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, .flags = FLAGS, "duration" }, { "duration", "how to determine end of stream", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, .flags = FLAGS, "duration" },
{ "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "duration" }, { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "duration" },
{ "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "duration" }, { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "duration" },
@ -347,6 +370,7 @@ AVFilter ff_vf_mix = {
.activate = activate, .activate = activate,
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS | .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS |
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
.process_command = process_command,
}; };
#endif /* CONFIG_MIX_FILTER */ #endif /* CONFIG_MIX_FILTER */
@ -395,8 +419,8 @@ static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
static const AVOption tmix_options[] = { static const AVOption tmix_options[] = {
{ "frames", "set number of successive frames to mix", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 1, 128, .flags = FLAGS }, { "frames", "set number of successive frames to mix", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 1, 128, .flags = FLAGS },
{ "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = FLAGS }, { "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = TFLAGS },
{ "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS }, { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = TFLAGS },
{ NULL }, { NULL },
}; };
@ -422,6 +446,7 @@ AVFilter ff_vf_tmix = {
.init = init, .init = init,
.uninit = uninit, .uninit = uninit,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
.process_command = process_command,
}; };
#endif /* CONFIG_TMIX_FILTER */ #endif /* CONFIG_TMIX_FILTER */

Loading…
Cancel
Save