dualinput.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(mainpic->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 = s->fs.in;
  42. ff_framesync_init(&s->fs, ctx, 2);
  43. s->fs.opaque = s;
  44. s->fs.on_event = process_frame;
  45. in[0].time_base = ctx->inputs[0]->time_base;
  46. in[1].time_base = ctx->inputs[1]->time_base;
  47. in[0].sync = 2;
  48. in[0].before = EXT_STOP;
  49. in[0].after = EXT_INFINITY;
  50. in[1].sync = 1;
  51. in[1].before = EXT_NULL;
  52. in[1].after = EXT_INFINITY;
  53. if (s->shortest)
  54. in[0].after = in[1].after = EXT_STOP;
  55. if (!s->repeatlast) {
  56. in[1].after = EXT_NULL;
  57. in[1].sync = 0;
  58. }
  59. return ff_framesync_configure(&s->fs);
  60. }
  61. int ff_dualinput_filter_frame(FFDualInputContext *s,
  62. AVFilterLink *inlink, AVFrame *in)
  63. {
  64. return ff_framesync_filter_frame(&s->fs, inlink, in);
  65. }
  66. int ff_dualinput_request_frame(FFDualInputContext *s, AVFilterLink *outlink)
  67. {
  68. return ff_framesync_request_frame(&s->fs, outlink);
  69. }
  70. void ff_dualinput_uninit(FFDualInputContext *s)
  71. {
  72. ff_framesync_uninit(&s->fs);
  73. }