f_realtime.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2015 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "libavutil/time.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include <float.h>
  25. typedef struct RealtimeContext {
  26. const AVClass *class;
  27. int64_t delta;
  28. int64_t limit;
  29. double speed;
  30. unsigned inited;
  31. } RealtimeContext;
  32. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  33. {
  34. AVFilterContext *ctx = inlink->dst;
  35. RealtimeContext *s = ctx->priv;
  36. if (frame->pts != AV_NOPTS_VALUE) {
  37. int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q) / s->speed;
  38. int64_t now = av_gettime_relative();
  39. int64_t sleep = pts - now + s->delta;
  40. if (!s->inited) {
  41. s->inited = 1;
  42. sleep = 0;
  43. s->delta = now - pts;
  44. }
  45. if (FFABS(sleep) > s->limit / s->speed) {
  46. av_log(ctx, AV_LOG_WARNING,
  47. "time discontinuity detected: %"PRIi64" us, resetting\n",
  48. sleep);
  49. sleep = 0;
  50. s->delta = now - pts;
  51. }
  52. if (sleep > 0) {
  53. av_log(ctx, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep);
  54. for (; sleep > 600000000; sleep -= 600000000)
  55. av_usleep(600000000);
  56. av_usleep(sleep);
  57. }
  58. }
  59. return ff_filter_frame(inlink->dst->outputs[0], frame);
  60. }
  61. #define OFFSET(x) offsetof(RealtimeContext, x)
  62. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  63. static const AVOption options[] = {
  64. { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
  65. { "speed", "speed factor", OFFSET(speed), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, DBL_MIN, DBL_MAX, FLAGS },
  66. { NULL }
  67. };
  68. #if CONFIG_REALTIME_FILTER
  69. #define realtime_options options
  70. AVFILTER_DEFINE_CLASS(realtime);
  71. static const AVFilterPad avfilter_vf_realtime_inputs[] = {
  72. {
  73. .name = "default",
  74. .type = AVMEDIA_TYPE_VIDEO,
  75. .filter_frame = filter_frame,
  76. },
  77. { NULL }
  78. };
  79. static const AVFilterPad avfilter_vf_realtime_outputs[] = {
  80. {
  81. .name = "default",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. },
  84. { NULL }
  85. };
  86. AVFilter ff_vf_realtime = {
  87. .name = "realtime",
  88. .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
  89. .priv_size = sizeof(RealtimeContext),
  90. .priv_class = &realtime_class,
  91. .inputs = avfilter_vf_realtime_inputs,
  92. .outputs = avfilter_vf_realtime_outputs,
  93. };
  94. #endif /* CONFIG_REALTIME_FILTER */
  95. #if CONFIG_AREALTIME_FILTER
  96. #define arealtime_options options
  97. AVFILTER_DEFINE_CLASS(arealtime);
  98. static const AVFilterPad arealtime_inputs[] = {
  99. {
  100. .name = "default",
  101. .type = AVMEDIA_TYPE_AUDIO,
  102. .filter_frame = filter_frame,
  103. },
  104. { NULL }
  105. };
  106. static const AVFilterPad arealtime_outputs[] = {
  107. {
  108. .name = "default",
  109. .type = AVMEDIA_TYPE_AUDIO,
  110. },
  111. { NULL }
  112. };
  113. AVFilter ff_af_arealtime = {
  114. .name = "arealtime",
  115. .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
  116. .priv_size = sizeof(RealtimeContext),
  117. .priv_class = &arealtime_class,
  118. .inputs = arealtime_inputs,
  119. .outputs = arealtime_outputs,
  120. };
  121. #endif /* CONFIG_AREALTIME_FILTER */