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
3.6KB

  1. /*
  2. * Copyright (c) 2015 Derek Buitenhuis
  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/opt.h"
  21. #include "avfilter.h"
  22. #include "formats.h"
  23. #include "internal.h"
  24. #include "video.h"
  25. #define DEFAULT_LENGTH 300
  26. typedef struct ReverseContext {
  27. int nb_frames;
  28. AVFrame **frames;
  29. unsigned int frames_size;
  30. unsigned int pts_size;
  31. int64_t *pts;
  32. int flush_idx;
  33. } ReverseContext;
  34. static av_cold int init(AVFilterContext *ctx)
  35. {
  36. ReverseContext *s = ctx->priv;
  37. s->pts = av_fast_realloc(NULL, &s->pts_size,
  38. DEFAULT_LENGTH * sizeof(*(s->pts)));
  39. if (!s->pts)
  40. return AVERROR(ENOMEM);
  41. s->frames = av_fast_realloc(NULL, &s->frames_size,
  42. DEFAULT_LENGTH * sizeof(*(s->frames)));
  43. if (!s->frames) {
  44. av_freep(&s->pts);
  45. return AVERROR(ENOMEM);
  46. }
  47. return 0;
  48. }
  49. static av_cold void uninit(AVFilterContext *ctx)
  50. {
  51. ReverseContext *s = ctx->priv;
  52. av_freep(&s->pts);
  53. av_freep(&s->frames);
  54. }
  55. static int config_output(AVFilterLink *outlink)
  56. {
  57. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  58. return 0;
  59. }
  60. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  61. {
  62. AVFilterContext *ctx = inlink->dst;
  63. ReverseContext *s = ctx->priv;
  64. if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
  65. void *ptr;
  66. ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
  67. if (!ptr)
  68. return AVERROR(ENOMEM);
  69. s->pts = ptr;
  70. ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
  71. if (!ptr)
  72. return AVERROR(ENOMEM);
  73. s->frames = ptr;
  74. }
  75. s->frames[s->nb_frames] = in;
  76. s->pts[s->nb_frames] = in->pts;
  77. s->nb_frames++;
  78. return 0;
  79. }
  80. static int request_frame(AVFilterLink *outlink)
  81. {
  82. AVFilterContext *ctx = outlink->src;
  83. ReverseContext *s = ctx->priv;
  84. int ret;
  85. ret = ff_request_frame(ctx->inputs[0]);
  86. if (ret == AVERROR_EOF && s->nb_frames > 0) {
  87. AVFrame *out = s->frames[s->nb_frames - 1];
  88. out->pts = s->pts[s->flush_idx++];
  89. ret = ff_filter_frame(outlink, out);
  90. s->nb_frames--;
  91. }
  92. return ret;
  93. }
  94. static const AVFilterPad reverse_inputs[] = {
  95. {
  96. .name = "default",
  97. .type = AVMEDIA_TYPE_VIDEO,
  98. .filter_frame = filter_frame,
  99. },
  100. { NULL }
  101. };
  102. static const AVFilterPad reverse_outputs[] = {
  103. {
  104. .name = "default",
  105. .type = AVMEDIA_TYPE_VIDEO,
  106. .request_frame = request_frame,
  107. .config_props = config_output,
  108. },
  109. { NULL }
  110. };
  111. AVFilter ff_vf_reverse = {
  112. .name = "reverse",
  113. .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
  114. .priv_size = sizeof(ReverseContext),
  115. .init = init,
  116. .uninit = uninit,
  117. .inputs = reverse_inputs,
  118. .outputs = reverse_outputs,
  119. };