2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/opt.h"
20 #include "libavutil/time.h"
31 typedef struct BenchContext {
39 #define OFFSET(x) offsetof(BenchContext, x)
40 #define DEFINE_OPTIONS(filt_name, FLAGS) \
41 static const AVOption filt_name##_options[] = { \
42 { "action", "set action", OFFSET(action), AV_OPT_TYPE_INT, {.i64=ACTION_START}, 0, NB_ACTION-1, FLAGS, "action" }, \
43 { "start", "start timer", 0, AV_OPT_TYPE_CONST, {.i64=ACTION_START}, INT_MIN, INT_MAX, FLAGS, "action" }, \
44 { "stop", "stop timer", 0, AV_OPT_TYPE_CONST, {.i64=ACTION_STOP}, INT_MIN, INT_MAX, FLAGS, "action" }, \
48 #define START_TIME_KEY "lavfi.bench.start_time"
49 #define T2F(v) ((v) / 1000000.)
51 static av_cold int init(AVFilterContext *ctx)
53 BenchContext *s = ctx->priv;
59 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
61 AVFilterContext *ctx = inlink->dst;
62 BenchContext *s = ctx->priv;
63 AVFilterLink *outlink = ctx->outputs[0];
64 const int64_t t = av_gettime();
67 return ff_filter_frame(outlink, in);
69 if (s->action == ACTION_START) {
70 av_dict_set_int(&in->metadata, START_TIME_KEY, t, 0);
71 } else if (s->action == ACTION_STOP) {
72 AVDictionaryEntry *e = av_dict_get(in->metadata, START_TIME_KEY, NULL, 0);
74 const int64_t start = strtoll(e->value, NULL, 0);
75 const int64_t diff = t - start;
78 s->min = FFMIN(s->min, diff);
79 s->max = FFMAX(s->max, diff);
80 av_log(s, AV_LOG_INFO, "t:%f avg:%f max:%f min:%f\n",
81 T2F(diff), T2F(s->sum / s->n), T2F(s->max), T2F(s->min));
83 av_dict_set(&in->metadata, START_TIME_KEY, NULL, 0);
86 return ff_filter_frame(outlink, in);
89 #if CONFIG_BENCH_FILTER
90 DEFINE_OPTIONS(bench, AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM);
91 AVFILTER_DEFINE_CLASS(bench);
93 static const AVFilterPad bench_inputs[] = {
96 .type = AVMEDIA_TYPE_VIDEO,
97 .filter_frame = filter_frame,
102 static const AVFilterPad bench_outputs[] = {
105 .type = AVMEDIA_TYPE_VIDEO,
110 AVFilter ff_vf_bench = {
112 .description = NULL_IF_CONFIG_SMALL("Benchmark part of a filtergraph."),
113 .priv_size = sizeof(BenchContext),
115 .inputs = bench_inputs,
116 .outputs = bench_outputs,
117 .priv_class = &bench_class,
119 #endif /* CONFIG_BENCH_FILTER */
121 #if CONFIG_ABENCH_FILTER
122 DEFINE_OPTIONS(abench, AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM);
123 AVFILTER_DEFINE_CLASS(abench);
125 static const AVFilterPad abench_inputs[] = {
128 .type = AVMEDIA_TYPE_AUDIO,
129 .filter_frame = filter_frame,
134 static const AVFilterPad abench_outputs[] = {
137 .type = AVMEDIA_TYPE_AUDIO,
142 AVFilter ff_af_abench = {
144 .description = NULL_IF_CONFIG_SMALL("Benchmark part of a filtergraph."),
145 .priv_size = sizeof(BenchContext),
147 .inputs = abench_inputs,
148 .outputs = abench_outputs,
149 .priv_class = &abench_class,
151 #endif /* CONFIG_ABENCH_FILTER */