X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_mix.c;h=0ca60d5522e12e6287143e643542c74639d95029;hb=a04ad248a05e7b613abe09b3bb067f555108d794;hp=873bef9dc3c82d58848f9866916f22cfa3420034;hpb=6fc762b4fd2c28ef7a0689a1df5ce200e5f5948f;p=ffmpeg diff --git a/libavfilter/vf_mix.c b/libavfilter/vf_mix.c index 873bef9dc3c..0ca60d5522e 100644 --- a/libavfilter/vf_mix.c +++ b/libavfilter/vf_mix.c @@ -55,26 +55,56 @@ typedef struct MixContext { static int query_formats(AVFilterContext *ctx) { - AVFilterFormats *pix_fmts = NULL; - int fmt, ret; - - for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) { - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); - if (!(desc->flags & AV_PIX_FMT_FLAG_PAL || - desc->flags & AV_PIX_FMT_FLAG_HWACCEL || - desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) && - (ret = ff_add_format(&pix_fmts, fmt)) < 0) - return ret; + AVFilterFormats *formats = NULL; + int ret; + + ret = ff_formats_pixdesc_filter(&formats, 0, + AV_PIX_FMT_FLAG_BITSTREAM | + AV_PIX_FMT_FLAG_PAL | + AV_PIX_FMT_FLAG_HWACCEL); + if (ret < 0) + return ret; + return ff_set_common_formats(ctx, formats); +} + +static int parse_weights(AVFilterContext *ctx) +{ + MixContext *s = ctx->priv; + char *p, *arg, *saveptr = NULL; + 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; } - return ff_set_common_formats(ctx, pix_fmts); + 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; - char *p, *arg, *saveptr = NULL; - int i, ret, last = 0; + int ret; s->tmix = !strcmp(ctx->filter->name, "tmix"); @@ -87,7 +117,7 @@ static av_cold int init(AVFilterContext *ctx) return AVERROR(ENOMEM); if (!s->tmix) { - for (i = 0; i < s->nb_inputs; i++) { + for (int i = 0; i < s->nb_inputs; i++) { AVFilterPad pad = { 0 }; pad.type = AVMEDIA_TYPE_VIDEO; @@ -102,27 +132,7 @@ static av_cold int init(AVFilterContext *ctx) } } - p = s->weights_str; - for (i = 0; i < s->nb_inputs; i++) { - if (!(arg = av_strtok(p, " ", &saveptr))) - break; - - p = NULL; - av_sscanf(arg, "%f", &s->weights[i]); - 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; + return parse_weights(ctx); } typedef struct ThreadData { @@ -201,6 +211,14 @@ static int process_frame(FFFrameSync *fs) return ret; } + if (ctx->is_disabled) { + out = av_frame_clone(s->frames[0]); + if (!out) + return AVERROR(ENOMEM); + out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base); + return ff_filter_frame(outlink, out); + } + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) return AVERROR(ENOMEM); @@ -217,8 +235,8 @@ static int config_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; MixContext *s = ctx->priv; - AVRational time_base = ctx->inputs[0]->time_base; AVRational frame_rate = ctx->inputs[0]->frame_rate; + AVRational sar = ctx->inputs[0]->sample_aspect_ratio; AVFilterLink *inlink = ctx->inputs[0]; int height = ctx->inputs[0]->h; int width = ctx->inputs[0]->w; @@ -252,8 +270,8 @@ static int config_output(AVFilterLink *outlink) outlink->w = width; outlink->h = height; - outlink->time_base = time_base; outlink->frame_rate = frame_rate; + outlink->sample_aspect_ratio = sar; if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0) return ret; @@ -271,7 +289,10 @@ static int config_output(AVFilterLink *outlink) in[i].after = (s->duration == 1 || (s->duration == 2 && i == 0)) ? EXT_STOP : EXT_INFINITY; } - return ff_framesync_configure(&s->fs); + ret = ff_framesync_configure(&s->fs); + outlink->time_base = s->fs.time_base; + + return ret; } static av_cold void uninit(AVFilterContext *ctx) @@ -286,12 +307,24 @@ static av_cold void uninit(AVFilterContext *ctx) for (i = 0; i < ctx->nb_inputs; i++) av_freep(&ctx->input_pads[i].name); } else { - for (i = 0; i < s->nb_frames; i++) + for (i = 0; i < s->nb_frames && s->frames; i++) av_frame_free(&s->frames[i]); } 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) { MixContext *s = ctx->priv; @@ -300,11 +333,12 @@ static int activate(AVFilterContext *ctx) #define OFFSET(x) offsetof(MixContext, x) #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[] = { - { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS }, - { "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = FLAGS }, - { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 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 = TFLAGS }, + { "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" }, { "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" }, @@ -324,7 +358,7 @@ static const AVFilterPad outputs[] = { #if CONFIG_MIX_FILTER AVFILTER_DEFINE_CLASS(mix); -AVFilter ff_vf_mix = { +const AVFilter ff_vf_mix = { .name = "mix", .description = NULL_IF_CONFIG_SMALL("Mix video inputs."), .priv_size = sizeof(MixContext), @@ -334,7 +368,9 @@ AVFilter ff_vf_mix = { .init = init, .uninit = uninit, .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, + .process_command = process_command, }; #endif /* CONFIG_MIX_FILTER */ @@ -383,8 +419,8 @@ static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in) 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 }, - { "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = FLAGS }, - { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .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 = TFLAGS }, { NULL }, }; @@ -399,7 +435,7 @@ static const AVFilterPad inputs[] = { AVFILTER_DEFINE_CLASS(tmix); -AVFilter ff_vf_tmix = { +const AVFilter ff_vf_tmix = { .name = "tmix", .description = NULL_IF_CONFIG_SMALL("Mix successive video frames."), .priv_size = sizeof(MixContext), @@ -410,6 +446,7 @@ AVFilter ff_vf_tmix = { .init = init, .uninit = uninit, .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, + .process_command = process_command, }; #endif /* CONFIG_TMIX_FILTER */