vf_framestep.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  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
  8. * License 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file framestep filter, inspired on libmpcodecs/vf_framestep.c by
  22. * Daniele Fornighieri <guru AT digitalfantasy it>.
  23. */
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct {
  29. const AVClass *class;
  30. int frame_step;
  31. } FrameStepContext;
  32. #define OFFSET(x) offsetof(FrameStepContext, x)
  33. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  34. static const AVOption framestep_options[] = {
  35. { "step", "set frame step", OFFSET(frame_step), AV_OPT_TYPE_INT, {.i64=1}, 1, INT_MAX, FLAGS},
  36. { NULL },
  37. };
  38. AVFILTER_DEFINE_CLASS(framestep);
  39. static int config_output_props(AVFilterLink *outlink)
  40. {
  41. AVFilterContext *ctx = outlink->src;
  42. FrameStepContext *framestep = ctx->priv;
  43. AVFilterLink *inlink = ctx->inputs[0];
  44. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  45. outlink->frame_rate =
  46. av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
  47. av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
  48. framestep->frame_step,
  49. inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
  50. outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
  51. return 0;
  52. }
  53. static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
  54. {
  55. FrameStepContext *framestep = inlink->dst->priv;
  56. if (!(inlink->frame_count % framestep->frame_step)) {
  57. return ff_filter_frame(inlink->dst->outputs[0], ref);
  58. } else {
  59. av_frame_free(&ref);
  60. return 0;
  61. }
  62. }
  63. static const AVFilterPad framestep_inputs[] = {
  64. {
  65. .name = "default",
  66. .type = AVMEDIA_TYPE_VIDEO,
  67. .filter_frame = filter_frame,
  68. },
  69. { NULL }
  70. };
  71. static const AVFilterPad framestep_outputs[] = {
  72. {
  73. .name = "default",
  74. .type = AVMEDIA_TYPE_VIDEO,
  75. .config_props = config_output_props,
  76. },
  77. { NULL }
  78. };
  79. AVFilter ff_vf_framestep = {
  80. .name = "framestep",
  81. .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
  82. .priv_size = sizeof(FrameStepContext),
  83. .priv_class = &framestep_class,
  84. .inputs = framestep_inputs,
  85. .outputs = framestep_outputs,
  86. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  87. };