You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.5KB

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