vf_framestep.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "avfilter.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct {
  28. int frame_step, frame_count, frame_selected;
  29. } FrameStepContext;
  30. static av_cold int init(AVFilterContext *ctx, const char *args)
  31. {
  32. FrameStepContext *framestep = ctx->priv;
  33. char *tailptr;
  34. long int n = 1;
  35. if (args) {
  36. n = strtol(args, &tailptr, 10);
  37. if (*tailptr || n <= 0 || n >= INT_MAX) {
  38. av_log(ctx, AV_LOG_ERROR,
  39. "Invalid argument '%s', must be a positive integer <= INT_MAX\n", args);
  40. return AVERROR(EINVAL);
  41. }
  42. }
  43. framestep->frame_step = n;
  44. return 0;
  45. }
  46. static int config_output_props(AVFilterLink *outlink)
  47. {
  48. AVFilterContext *ctx = outlink->src;
  49. FrameStepContext *framestep = ctx->priv;
  50. AVFilterLink *inlink = ctx->inputs[0];
  51. outlink->frame_rate =
  52. av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
  53. av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
  54. framestep->frame_step,
  55. inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
  56. outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
  57. return 0;
  58. }
  59. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
  60. {
  61. FrameStepContext *framestep = inlink->dst->priv;
  62. framestep->frame_selected = 0;
  63. if (!(framestep->frame_count++ % framestep->frame_step)) {
  64. inlink->cur_buf = NULL;
  65. framestep->frame_selected = 1;
  66. return ff_start_frame(inlink->dst->outputs[0], ref);
  67. }
  68. return 0;
  69. }
  70. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  71. {
  72. FrameStepContext *framestep = inlink->dst->priv;
  73. if (framestep->frame_selected)
  74. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  75. return 0;
  76. }
  77. static int end_frame(AVFilterLink *inlink)
  78. {
  79. FrameStepContext *framestep = inlink->dst->priv;
  80. if (framestep->frame_selected)
  81. return ff_end_frame(inlink->dst->outputs[0]);
  82. return 0;
  83. }
  84. static int request_frame(AVFilterLink *outlink)
  85. {
  86. FrameStepContext *framestep = outlink->src->priv;
  87. AVFilterLink *inlink = outlink->src->inputs[0];
  88. int ret;
  89. framestep->frame_selected = 0;
  90. do {
  91. ret = ff_request_frame(inlink);
  92. } while (!framestep->frame_selected && ret >= 0);
  93. return ret;
  94. }
  95. AVFilter avfilter_vf_framestep = {
  96. .name = "framestep",
  97. .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
  98. .init = init,
  99. .priv_size = sizeof(FrameStepContext),
  100. .inputs = (const AVFilterPad[]) {
  101. {
  102. .name = "default",
  103. .type = AVMEDIA_TYPE_VIDEO,
  104. .get_video_buffer = ff_null_get_video_buffer,
  105. .start_frame = start_frame,
  106. .draw_slice = draw_slice,
  107. .end_frame = end_frame,
  108. },
  109. { .name = NULL }
  110. },
  111. .outputs = (const AVFilterPad[]) {
  112. {
  113. .name = "default",
  114. .type = AVMEDIA_TYPE_VIDEO,
  115. .config_props = config_output_props,
  116. .request_frame = request_frame,
  117. },
  118. { .name = NULL }
  119. },
  120. };