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.

183 lines
5.0KB

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