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.

143 lines
4.2KB

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