f_bench.c 4.6 KB

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