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.

144 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. void *ptr;
  65. if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
  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. }
  71. if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
  72. ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
  73. if (!ptr)
  74. return AVERROR(ENOMEM);
  75. s->frames = ptr;
  76. }
  77. s->frames[s->nb_frames] = in;
  78. s->pts[s->nb_frames] = in->pts;
  79. s->nb_frames++;
  80. return 0;
  81. }
  82. static int request_frame(AVFilterLink *outlink)
  83. {
  84. AVFilterContext *ctx = outlink->src;
  85. ReverseContext *s = ctx->priv;
  86. int ret;
  87. ret = ff_request_frame(ctx->inputs[0]);
  88. if (ret == AVERROR_EOF && s->nb_frames > 0) {
  89. AVFrame *out = s->frames[s->nb_frames - 1];
  90. out->pts = s->pts[s->flush_idx++];
  91. ret = ff_filter_frame(outlink, out);
  92. s->nb_frames--;
  93. }
  94. return ret;
  95. }
  96. static const AVFilterPad reverse_inputs[] = {
  97. {
  98. .name = "default",
  99. .type = AVMEDIA_TYPE_VIDEO,
  100. .filter_frame = filter_frame,
  101. },
  102. { NULL }
  103. };
  104. static const AVFilterPad reverse_outputs[] = {
  105. {
  106. .name = "default",
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .request_frame = request_frame,
  109. .config_props = config_output,
  110. },
  111. { NULL }
  112. };
  113. AVFilter ff_vf_reverse = {
  114. .name = "reverse",
  115. .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
  116. .priv_size = sizeof(ReverseContext),
  117. .init = init,
  118. .uninit = uninit,
  119. .inputs = reverse_inputs,
  120. .outputs = reverse_outputs,
  121. };