f_realtime.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. typedef struct RealtimeContext {
  25. const AVClass *class;
  26. int64_t delta;
  27. int64_t limit;
  28. unsigned inited;
  29. } RealtimeContext;
  30. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  31. {
  32. AVFilterContext *ctx = inlink->dst;
  33. RealtimeContext *s = ctx->priv;
  34. if (frame->pts != AV_NOPTS_VALUE) {
  35. int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
  36. int64_t now = av_gettime_relative();
  37. int64_t sleep = pts - now + s->delta;
  38. if (!s->inited) {
  39. s->inited = 1;
  40. sleep = 0;
  41. s->delta = now - pts;
  42. }
  43. if (sleep > s->limit || sleep < -s->limit) {
  44. av_log(ctx, AV_LOG_WARNING,
  45. "time discontinuity detected: %"PRIi64" us, resetting\n",
  46. sleep);
  47. sleep = 0;
  48. s->delta = now - pts;
  49. }
  50. if (sleep > 0) {
  51. av_log(ctx, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep);
  52. for (; sleep > 600000000; sleep -= 600000000)
  53. av_usleep(600000000);
  54. av_usleep(sleep);
  55. }
  56. }
  57. return ff_filter_frame(inlink->dst->outputs[0], frame);
  58. }
  59. #define OFFSET(x) offsetof(RealtimeContext, x)
  60. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  61. static const AVOption options[] = {
  62. { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
  63. { NULL }
  64. };
  65. #if CONFIG_REALTIME_FILTER
  66. #define realtime_options options
  67. AVFILTER_DEFINE_CLASS(realtime);
  68. static const AVFilterPad avfilter_vf_realtime_inputs[] = {
  69. {
  70. .name = "default",
  71. .type = AVMEDIA_TYPE_VIDEO,
  72. .filter_frame = filter_frame,
  73. },
  74. { NULL }
  75. };
  76. static const AVFilterPad avfilter_vf_realtime_outputs[] = {
  77. {
  78. .name = "default",
  79. .type = AVMEDIA_TYPE_VIDEO,
  80. },
  81. { NULL }
  82. };
  83. AVFilter ff_vf_realtime = {
  84. .name = "realtime",
  85. .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
  86. .priv_size = sizeof(RealtimeContext),
  87. .priv_class = &realtime_class,
  88. .inputs = avfilter_vf_realtime_inputs,
  89. .outputs = avfilter_vf_realtime_outputs,
  90. };
  91. #endif /* CONFIG_REALTIME_FILTER */
  92. #if CONFIG_AREALTIME_FILTER
  93. #define arealtime_options options
  94. AVFILTER_DEFINE_CLASS(arealtime);
  95. static const AVFilterPad arealtime_inputs[] = {
  96. {
  97. .name = "default",
  98. .type = AVMEDIA_TYPE_AUDIO,
  99. .filter_frame = filter_frame,
  100. },
  101. { NULL }
  102. };
  103. static const AVFilterPad arealtime_outputs[] = {
  104. {
  105. .name = "default",
  106. .type = AVMEDIA_TYPE_AUDIO,
  107. },
  108. { NULL }
  109. };
  110. AVFilter ff_af_arealtime = {
  111. .name = "arealtime",
  112. .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
  113. .priv_size = sizeof(RealtimeContext),
  114. .priv_class = &arealtime_class,
  115. .inputs = arealtime_inputs,
  116. .outputs = arealtime_outputs,
  117. };
  118. #endif /* CONFIG_AREALTIME_FILTER */