f_cue.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2018 Marton Balint
  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 "filters.h"
  24. #include "internal.h"
  25. typedef struct CueContext {
  26. const AVClass *class;
  27. int64_t first_pts;
  28. int64_t cue;
  29. int64_t preroll;
  30. int64_t buffer;
  31. int status;
  32. } CueContext;
  33. static int activate(AVFilterContext *ctx)
  34. {
  35. AVFilterLink *inlink = ctx->inputs[0];
  36. AVFilterLink *outlink = ctx->outputs[0];
  37. CueContext *s = ctx->priv;
  38. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  39. if (ff_inlink_queued_frames(inlink)) {
  40. AVFrame *frame = ff_inlink_peek_frame(inlink, 0);
  41. int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
  42. if (!s->status) {
  43. s->first_pts = pts;
  44. s->status++;
  45. }
  46. if (s->status == 1) {
  47. if (pts - s->first_pts < s->preroll) {
  48. int ret = ff_inlink_consume_frame(inlink, &frame);
  49. if (ret < 0)
  50. return ret;
  51. return ff_filter_frame(outlink, frame);
  52. }
  53. s->first_pts = pts;
  54. s->status++;
  55. }
  56. if (s->status == 2) {
  57. frame = ff_inlink_peek_frame(inlink, ff_inlink_queued_frames(inlink) - 1);
  58. pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
  59. if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
  60. s->status++;
  61. }
  62. if (s->status == 3) {
  63. int64_t diff;
  64. while ((diff = (av_gettime() - s->cue)) < 0)
  65. av_usleep(av_clip(-diff / 2, 100, 1000000));
  66. s->status++;
  67. }
  68. if (s->status == 4) {
  69. int ret = ff_inlink_consume_frame(inlink, &frame);
  70. if (ret < 0)
  71. return ret;
  72. return ff_filter_frame(outlink, frame);
  73. }
  74. }
  75. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  76. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  77. return FFERROR_NOT_READY;
  78. }
  79. #define OFFSET(x) offsetof(CueContext, x)
  80. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  81. static const AVOption options[] = {
  82. { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
  83. { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
  84. { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
  85. { NULL }
  86. };
  87. #if CONFIG_CUE_FILTER
  88. #define cue_options options
  89. AVFILTER_DEFINE_CLASS(cue);
  90. static const AVFilterPad cue_inputs[] = {
  91. {
  92. .name = "default",
  93. .type = AVMEDIA_TYPE_VIDEO,
  94. },
  95. { NULL }
  96. };
  97. static const AVFilterPad cue_outputs[] = {
  98. {
  99. .name = "default",
  100. .type = AVMEDIA_TYPE_VIDEO,
  101. },
  102. { NULL }
  103. };
  104. AVFilter ff_vf_cue = {
  105. .name = "cue",
  106. .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
  107. .priv_size = sizeof(CueContext),
  108. .priv_class = &cue_class,
  109. .inputs = cue_inputs,
  110. .outputs = cue_outputs,
  111. .activate = activate,
  112. };
  113. #endif /* CONFIG_CUE_FILTER */
  114. #if CONFIG_ACUE_FILTER
  115. #define acue_options options
  116. AVFILTER_DEFINE_CLASS(acue);
  117. static const AVFilterPad acue_inputs[] = {
  118. {
  119. .name = "default",
  120. .type = AVMEDIA_TYPE_AUDIO,
  121. },
  122. { NULL }
  123. };
  124. static const AVFilterPad acue_outputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_AUDIO,
  128. },
  129. { NULL }
  130. };
  131. AVFilter ff_af_acue = {
  132. .name = "acue",
  133. .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
  134. .priv_size = sizeof(CueContext),
  135. .priv_class = &acue_class,
  136. .inputs = acue_inputs,
  137. .outputs = acue_outputs,
  138. .activate = activate,
  139. };
  140. #endif /* CONFIG_ACUE_FILTER */