FFmpeg
f_bench.c
Go to the documentation of this file.
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 "libavutil/opt.h"
20 #include "libavutil/time.h"
21 #include "avfilter.h"
22 #include "formats.h"
23 #include "internal.h"
24 
29 };
30 
31 typedef struct BenchContext {
32  const AVClass *class;
33  int action;
34  int64_t max, min;
35  int64_t sum;
36  int n;
37 } BenchContext;
38 
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" }, \
45  { NULL } \
46 }
47 
48 #define START_TIME_KEY "lavfi.bench.start_time"
49 #define T2F(v) ((v) / 1000000.)
50 
52 {
53  BenchContext *s = ctx->priv;
54  s->min = INT64_MAX;
55  s->max = INT64_MIN;
56  return 0;
57 }
58 
60 {
61  AVFilterContext *ctx = inlink->dst;
62  BenchContext *s = ctx->priv;
63  AVFilterLink *outlink = ctx->outputs[0];
64  const int64_t t = av_gettime();
65 
66  if (t < 0)
67  return ff_filter_frame(outlink, in);
68 
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);
73  if (e) {
74  const int64_t start = strtoll(e->value, NULL, 0);
75  const int64_t diff = t - start;
76  s->sum += diff;
77  s->n++;
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));
82  }
83  av_dict_set(&in->metadata, START_TIME_KEY, NULL, 0);
84  }
85 
86  return ff_filter_frame(outlink, in);
87 }
88 
89 #if CONFIG_BENCH_FILTER
92 
93 static const AVFilterPad bench_inputs[] = {
94  {
95  .name = "default",
96  .type = AVMEDIA_TYPE_VIDEO,
97  .filter_frame = filter_frame,
98  },
99  { NULL }
100 };
101 
102 static const AVFilterPad bench_outputs[] = {
103  {
104  .name = "default",
105  .type = AVMEDIA_TYPE_VIDEO,
106  },
107  { NULL }
108 };
109 
111  .name = "bench",
112  .description = NULL_IF_CONFIG_SMALL("Benchmark part of a filtergraph."),
113  .priv_size = sizeof(BenchContext),
114  .init = init,
115  .inputs = bench_inputs,
116  .outputs = bench_outputs,
117  .priv_class = &bench_class,
118 };
119 #endif /* CONFIG_BENCH_FILTER */
120 
121 #if CONFIG_ABENCH_FILTER
123 AVFILTER_DEFINE_CLASS(abench);
124 
125 static const AVFilterPad abench_inputs[] = {
126  {
127  .name = "default",
128  .type = AVMEDIA_TYPE_AUDIO,
129  .filter_frame = filter_frame,
130  },
131  { NULL }
132 };
133 
134 static const AVFilterPad abench_outputs[] = {
135  {
136  .name = "default",
137  .type = AVMEDIA_TYPE_AUDIO,
138  },
139  { NULL }
140 };
141 
143  .name = "abench",
144  .description = NULL_IF_CONFIG_SMALL("Benchmark part of a filtergraph."),
145  .priv_size = sizeof(BenchContext),
146  .init = init,
147  .inputs = abench_inputs,
148  .outputs = abench_outputs,
149  .priv_class = &abench_class,
150 };
151 #endif /* CONFIG_ABENCH_FILTER */
BenchContext::action
int action
Definition: f_bench.c:33
opt.h
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:279
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
BenchContext::min
int64_t min
Definition: f_bench.c:34
NB_ACTION
@ NB_ACTION
Definition: f_bench.c:28
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
AV_OPT_FLAG_FILTERING_PARAM
#define AV_OPT_FLAG_FILTERING_PARAM
a generic parameter which can be set by the user for filtering
Definition: opt.h:291
formats.h
BenchContext::max
int64_t max
Definition: f_bench.c:34
start
void INT64 start
Definition: avisynth_c.h:767
BenchContext::sum
int64_t sum
Definition: f_bench.c:35
START_TIME_KEY
#define START_TIME_KEY
Definition: f_bench.c:48
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
ff_vf_bench
AVFilter ff_vf_bench
av_cold
#define av_cold
Definition: attributes.h:84
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
T2F
#define T2F(v)
Definition: f_bench.c:49
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
DEFINE_OPTIONS
#define DEFINE_OPTIONS(filt_name, FLAGS)
Definition: f_bench.c:40
BenchContext
Definition: f_bench.c:31
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:278
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
ACTION_START
@ ACTION_START
Definition: f_bench.c:26
BenchAction
BenchAction
Definition: f_bench.c:25
time.h
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
internal.h
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
BenchContext::n
int n
Definition: f_bench.c:36
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_bench.c:51
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
ff_af_abench
AVFilter ff_af_abench
avfilter.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: f_bench.c:59
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it.
Definition: dict.c:147
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:136
AVDictionaryEntry
Definition: dict.h:81
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ACTION_STOP
@ ACTION_STOP
Definition: f_bench.c:27
AVDictionaryEntry::value
char * value
Definition: dict.h:83