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.

168 lines
5.0KB

  1. /*
  2. * Copyright (c) 2019 Paul B Mahol
  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. #include "libavutil/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "filters.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct FreezeFramesContext {
  30. const AVClass *class;
  31. int64_t first, last, replace;
  32. AVFrame *replace_frame;
  33. } FreezeFramesContext;
  34. #define OFFSET(x) offsetof(FreezeFramesContext, x)
  35. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  36. static const AVOption freezeframes_options[] = {
  37. { "first", "set first frame to freeze", OFFSET(first), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
  38. { "last", "set last frame to freeze", OFFSET(last), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
  39. { "replace", "set frame to replace", OFFSET(replace), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
  40. { NULL },
  41. };
  42. AVFILTER_DEFINE_CLASS(freezeframes);
  43. static int config_output(AVFilterLink *outlink)
  44. {
  45. AVFilterContext *ctx = outlink->src;
  46. AVFilterLink *sourcelink = ctx->inputs[0];
  47. AVFilterLink *replacelink = ctx->inputs[1];
  48. if (sourcelink->w != replacelink->w || sourcelink->h != replacelink->h) {
  49. av_log(ctx, AV_LOG_ERROR,
  50. "Input frame sizes do not match (%dx%d vs %dx%d).\n",
  51. sourcelink->w, sourcelink->h,
  52. replacelink->w, replacelink->h);
  53. return AVERROR(EINVAL);
  54. }
  55. outlink->w = sourcelink->w;
  56. outlink->h = sourcelink->h;
  57. outlink->time_base = sourcelink->time_base;
  58. outlink->sample_aspect_ratio = sourcelink->sample_aspect_ratio;
  59. outlink->frame_rate = sourcelink->frame_rate;
  60. return 0;
  61. }
  62. static int activate(AVFilterContext *ctx)
  63. {
  64. AVFilterLink *outlink = ctx->outputs[0];
  65. FreezeFramesContext *s = ctx->priv;
  66. AVFrame *frame = NULL;
  67. int drop = ctx->inputs[0]->frame_count_out >= s->first &&
  68. ctx->inputs[0]->frame_count_out <= s->last;
  69. int replace = ctx->inputs[1]->frame_count_out == s->replace;
  70. int ret;
  71. FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
  72. if (drop && s->replace_frame) {
  73. ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
  74. if (ret < 0)
  75. return ret;
  76. if (frame) {
  77. int64_t dropped_pts = frame->pts;
  78. av_frame_free(&frame);
  79. frame = av_frame_clone(s->replace_frame);
  80. if (!frame)
  81. return AVERROR(ENOMEM);
  82. frame->pts = dropped_pts;
  83. return ff_filter_frame(outlink, frame);
  84. }
  85. } else if (!drop) {
  86. ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
  87. if (ret < 0)
  88. return ret;
  89. if (frame)
  90. return ff_filter_frame(outlink, frame);
  91. }
  92. ret = ff_inlink_consume_frame(ctx->inputs[1], &frame);
  93. if (ret < 0)
  94. return ret;
  95. if (replace && frame) {
  96. s->replace_frame = frame;
  97. } else if (frame) {
  98. av_frame_free(&frame);
  99. }
  100. FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
  101. FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
  102. if (!drop || (drop && s->replace_frame))
  103. FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[0]);
  104. if (!s->replace_frame)
  105. FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[1]);
  106. return FFERROR_NOT_READY;
  107. }
  108. static av_cold void uninit(AVFilterContext *ctx)
  109. {
  110. FreezeFramesContext *s = ctx->priv;
  111. av_frame_free(&s->replace_frame);
  112. }
  113. static const AVFilterPad freezeframes_inputs[] = {
  114. {
  115. .name = "source",
  116. .type = AVMEDIA_TYPE_VIDEO,
  117. },
  118. {
  119. .name = "replace",
  120. .type = AVMEDIA_TYPE_VIDEO,
  121. },
  122. { NULL },
  123. };
  124. static const AVFilterPad freezeframes_outputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_VIDEO,
  128. .config_props = config_output,
  129. },
  130. { NULL },
  131. };
  132. AVFilter ff_vf_freezeframes = {
  133. .name = "freezeframes",
  134. .description = NULL_IF_CONFIG_SMALL("Freeze video frames."),
  135. .priv_size = sizeof(FreezeFramesContext),
  136. .priv_class = &freezeframes_class,
  137. .inputs = freezeframes_inputs,
  138. .outputs = freezeframes_outputs,
  139. .activate = activate,
  140. .uninit = uninit,
  141. };