dualinput.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "dualinput.h"
  19. #include "libavutil/timestamp.h"
  20. static int process_frame(FFFrameSync *fs)
  21. {
  22. AVFilterContext *ctx = fs->parent;
  23. FFDualInputContext *s = fs->opaque;
  24. AVFrame *mainpic = NULL, *secondpic = NULL;
  25. int ret = 0;
  26. if ((ret = ff_framesync_get_frame(&s->fs, 0, &mainpic, 1)) < 0 ||
  27. (ret = ff_framesync_get_frame(&s->fs, 1, &secondpic, 0)) < 0) {
  28. av_frame_free(&mainpic);
  29. return ret;
  30. }
  31. av_assert0(mainpic);
  32. mainpic->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[0]->time_base);
  33. if (secondpic && !ctx->is_disabled)
  34. mainpic = s->process(ctx, mainpic, secondpic);
  35. ret = ff_filter_frame(ctx->outputs[0], mainpic);
  36. av_assert1(ret != AVERROR(EAGAIN));
  37. return ret;
  38. }
  39. int ff_dualinput_init(AVFilterContext *ctx, FFDualInputContext *s)
  40. {
  41. FFFrameSyncIn *in;
  42. int ret;
  43. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  44. return ret;
  45. in = s->fs.in;
  46. s->fs.opaque = s;
  47. s->fs.on_event = process_frame;
  48. in[0].time_base = ctx->inputs[0]->time_base;
  49. in[1].time_base = ctx->inputs[1]->time_base;
  50. in[0].sync = 2;
  51. in[0].before = EXT_STOP;
  52. in[0].after = EXT_INFINITY;
  53. in[1].sync = 1;
  54. in[1].before = EXT_NULL;
  55. in[1].after = EXT_INFINITY;
  56. if (s->shortest)
  57. in[0].after = in[1].after = EXT_STOP;
  58. if (!s->repeatlast) {
  59. in[1].after = EXT_NULL;
  60. in[1].sync = 0;
  61. }
  62. if (s->skip_initial_unpaired) {
  63. in[1].before = EXT_STOP;
  64. }
  65. return ff_framesync_configure(&s->fs);
  66. }
  67. int ff_dualinput_filter_frame(FFDualInputContext *s,
  68. AVFilterLink *inlink, AVFrame *in)
  69. {
  70. return ff_framesync_filter_frame(&s->fs, inlink, in);
  71. }
  72. int ff_dualinput_request_frame(FFDualInputContext *s, AVFilterLink *outlink)
  73. {
  74. return ff_framesync_request_frame(&s->fs, outlink);
  75. }
  76. void ff_dualinput_uninit(FFDualInputContext *s)
  77. {
  78. ff_framesync_uninit(&s->fs);
  79. }