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.

160 lines
5.4KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #define MAIN 0
  19. #define SECOND 1
  20. #include "dualinput.h"
  21. #include "libavutil/timestamp.h"
  22. static int try_filter_frame(FFDualInputContext *s,
  23. AVFilterContext *ctx, AVFrame *mainpic)
  24. {
  25. int ret;
  26. /* Discard obsolete second frames: if there is a next second frame with pts
  27. * before the main frame, we can drop the current second. */
  28. while (1) {
  29. AVFrame *next_overpic = ff_bufqueue_peek(&s->queue[SECOND], 0);
  30. if (!next_overpic && s->second_eof && !s->repeatlast) {
  31. av_frame_free(&s->second_frame);
  32. break;
  33. }
  34. if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[SECOND]->time_base,
  35. mainpic->pts, ctx->inputs[MAIN]->time_base) > 0)
  36. break;
  37. ff_bufqueue_get(&s->queue[SECOND]);
  38. av_frame_free(&s->second_frame);
  39. s->second_frame = next_overpic;
  40. }
  41. /* If there is no next frame and no EOF and the second frame is before
  42. * the main frame, we can not know yet if it will be superseded. */
  43. if (!s->queue[SECOND].available && !s->second_eof &&
  44. (!s->second_frame || av_compare_ts(s->second_frame->pts, ctx->inputs[SECOND]->time_base,
  45. mainpic->pts, ctx->inputs[MAIN]->time_base) < 0))
  46. return AVERROR(EAGAIN);
  47. /* At this point, we know that the current second frame extends to the
  48. * time of the main frame. */
  49. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  50. av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
  51. if (s->second_frame)
  52. av_dlog(ctx, " second_pts:%s second_pts_time:%s",
  53. av_ts2str(s->second_frame->pts), av_ts2timestr(s->second_frame->pts, &ctx->inputs[SECOND]->time_base));
  54. av_dlog(ctx, "\n");
  55. if (s->second_frame && !ctx->is_disabled)
  56. mainpic = s->process(ctx, mainpic, s->second_frame);
  57. ret = ff_filter_frame(ctx->outputs[0], mainpic);
  58. av_assert1(ret != AVERROR(EAGAIN));
  59. s->frame_requested = 0;
  60. return ret;
  61. }
  62. static int try_filter_next_frame(FFDualInputContext *s, AVFilterContext *ctx)
  63. {
  64. AVFrame *next_mainpic = ff_bufqueue_peek(&s->queue[MAIN], 0);
  65. int ret;
  66. if (!next_mainpic)
  67. return AVERROR(EAGAIN);
  68. if ((ret = try_filter_frame(s, ctx, next_mainpic)) == AVERROR(EAGAIN))
  69. return ret;
  70. ff_bufqueue_get(&s->queue[MAIN]);
  71. return ret;
  72. }
  73. static int flush_frames(FFDualInputContext *s, AVFilterContext *ctx)
  74. {
  75. int ret;
  76. while (!(ret = try_filter_next_frame(s, ctx)));
  77. return ret == AVERROR(EAGAIN) ? 0 : ret;
  78. }
  79. int ff_dualinput_filter_frame_main(FFDualInputContext *s,
  80. AVFilterLink *inlink, AVFrame *in)
  81. {
  82. AVFilterContext *ctx = inlink->dst;
  83. int ret;
  84. if ((ret = flush_frames(s, ctx)) < 0)
  85. return ret;
  86. if ((ret = try_filter_frame(s, ctx, in)) < 0) {
  87. if (ret != AVERROR(EAGAIN))
  88. return ret;
  89. ff_bufqueue_add(ctx, &s->queue[MAIN], in);
  90. }
  91. if (!s->second_frame)
  92. return 0;
  93. flush_frames(s, ctx);
  94. return 0;
  95. }
  96. int ff_dualinput_filter_frame_second(FFDualInputContext *s,
  97. AVFilterLink *inlink, AVFrame *in)
  98. {
  99. AVFilterContext *ctx = inlink->dst;
  100. int ret;
  101. if ((ret = flush_frames(s, ctx)) < 0)
  102. return ret;
  103. ff_bufqueue_add(ctx, &s->queue[SECOND], in);
  104. ret = try_filter_next_frame(s, ctx);
  105. return ret == AVERROR(EAGAIN) ? 0 : ret;
  106. }
  107. int ff_dualinput_request_frame(FFDualInputContext *s, AVFilterLink *outlink)
  108. {
  109. AVFilterContext *ctx = outlink->src;
  110. int input, ret;
  111. if (!try_filter_next_frame(s, ctx))
  112. return 0;
  113. s->frame_requested = 1;
  114. while (s->frame_requested) {
  115. /* TODO if we had a frame duration, we could guess more accurately */
  116. input = !s->second_eof && (s->queue[MAIN].available ||
  117. s->queue[SECOND].available < 2) ?
  118. SECOND : MAIN;
  119. ret = ff_request_frame(ctx->inputs[input]);
  120. /* EOF on main is reported immediately */
  121. if (ret == AVERROR_EOF && input == SECOND) {
  122. s->second_eof = 1;
  123. if (s->shortest)
  124. return ret;
  125. if ((ret = try_filter_next_frame(s, ctx)) != AVERROR(EAGAIN))
  126. return ret;
  127. ret = 0; /* continue requesting frames on main */
  128. }
  129. if (ret < 0)
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. void ff_dualinput_uninit(FFDualInputContext *s)
  135. {
  136. av_frame_free(&s->second_frame);
  137. ff_bufqueue_discard_all(&s->queue[MAIN]);
  138. ff_bufqueue_discard_all(&s->queue[SECOND]);
  139. }