• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include "config_components.h"
20 
21 #include "libavutil/opt.h"
22 #include "libavutil/time.h"
23 #include "avfilter.h"
24 #include "formats.h"
25 #include "internal.h"
26 
27 enum BenchAction {
28     ACTION_START,
29     ACTION_STOP,
30     NB_ACTION
31 };
32 
33 typedef struct BenchContext {
34     const AVClass *class;
35     int action;
36     int64_t max, min;
37     int64_t sum;
38     int n;
39 } BenchContext;
40 
41 #define OFFSET(x) offsetof(BenchContext, x)
42 #define DEFINE_OPTIONS(filt_name, FLAGS)                                                                                \
43 static const AVOption filt_name##_options[] = {                                                                         \
44     { "action", "set action", OFFSET(action), AV_OPT_TYPE_INT, {.i64=ACTION_START}, 0, NB_ACTION-1, FLAGS, "action" },  \
45         { "start", "start timer",  0, AV_OPT_TYPE_CONST, {.i64=ACTION_START}, INT_MIN, INT_MAX, FLAGS, "action" },      \
46         { "stop",  "stop timer",   0, AV_OPT_TYPE_CONST, {.i64=ACTION_STOP},  INT_MIN, INT_MAX, FLAGS, "action" },      \
47     { NULL }                                                                                                            \
48 }
49 
50 #define START_TIME_KEY "lavfi.bench.start_time"
51 #define T2F(v) ((v) / 1000000.)
52 
init(AVFilterContext * ctx)53 static av_cold int init(AVFilterContext *ctx)
54 {
55     BenchContext *s = ctx->priv;
56     s->min = INT64_MAX;
57     s->max = INT64_MIN;
58     return 0;
59 }
60 
filter_frame(AVFilterLink * inlink,AVFrame * in)61 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
62 {
63     AVFilterContext *ctx = inlink->dst;
64     BenchContext *s = ctx->priv;
65     AVFilterLink *outlink = ctx->outputs[0];
66     const int64_t t = av_gettime();
67 
68     if (t < 0)
69         return ff_filter_frame(outlink, in);
70 
71     if (s->action == ACTION_START) {
72         av_dict_set_int(&in->metadata, START_TIME_KEY, t, 0);
73     } else if (s->action == ACTION_STOP) {
74         AVDictionaryEntry *e = av_dict_get(in->metadata, START_TIME_KEY, NULL, 0);
75         if (e) {
76             const int64_t start = strtoll(e->value, NULL, 0);
77             const int64_t diff = t - start;
78             s->sum += diff;
79             s->n++;
80             s->min = FFMIN(s->min, diff);
81             s->max = FFMAX(s->max, diff);
82             av_log(s, AV_LOG_INFO, "t:%f avg:%f max:%f min:%f\n",
83                    T2F(diff), T2F(s->sum / s->n), T2F(s->max), T2F(s->min));
84         }
85         av_dict_set(&in->metadata, START_TIME_KEY, NULL, 0);
86     }
87 
88     return ff_filter_frame(outlink, in);
89 }
90 
91 #if CONFIG_BENCH_FILTER
92 DEFINE_OPTIONS(bench, AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM);
93 AVFILTER_DEFINE_CLASS(bench);
94 
95 static const AVFilterPad bench_inputs[] = {
96     {
97         .name         = "default",
98         .type         = AVMEDIA_TYPE_VIDEO,
99         .filter_frame = filter_frame,
100     },
101 };
102 
103 static const AVFilterPad bench_outputs[] = {
104     {
105         .name = "default",
106         .type = AVMEDIA_TYPE_VIDEO,
107     },
108 };
109 
110 const AVFilter ff_vf_bench = {
111     .name          = "bench",
112     .description   = NULL_IF_CONFIG_SMALL("Benchmark part of a filtergraph."),
113     .priv_size     = sizeof(BenchContext),
114     .init          = init,
115     FILTER_INPUTS(bench_inputs),
116     FILTER_OUTPUTS(bench_outputs),
117     .priv_class    = &bench_class,
118     .flags         = AVFILTER_FLAG_METADATA_ONLY,
119 };
120 #endif /* CONFIG_BENCH_FILTER */
121 
122 #if CONFIG_ABENCH_FILTER
123 DEFINE_OPTIONS(abench, AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM);
124 AVFILTER_DEFINE_CLASS(abench);
125 
126 static const AVFilterPad abench_inputs[] = {
127     {
128         .name         = "default",
129         .type         = AVMEDIA_TYPE_AUDIO,
130         .filter_frame = filter_frame,
131     },
132 };
133 
134 static const AVFilterPad abench_outputs[] = {
135     {
136         .name = "default",
137         .type = AVMEDIA_TYPE_AUDIO,
138     },
139 };
140 
141 const AVFilter ff_af_abench = {
142     .name          = "abench",
143     .description   = NULL_IF_CONFIG_SMALL("Benchmark part of a filtergraph."),
144     .priv_size     = sizeof(BenchContext),
145     .init          = init,
146     FILTER_INPUTS(abench_inputs),
147     FILTER_OUTPUTS(abench_outputs),
148     .priv_class    = &abench_class,
149     .flags         = AVFILTER_FLAG_METADATA_ONLY,
150 };
151 #endif /* CONFIG_ABENCH_FILTER */
152