vf_shuffleframes.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2015 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 "internal.h"
  27. #include "video.h"
  28. typedef struct ShuffleFramesContext {
  29. const AVClass *class;
  30. char *mapping;
  31. AVFrame **frames;
  32. int *map;
  33. int64_t *pts;
  34. int in_frames;
  35. int nb_frames;
  36. } ShuffleFramesContext;
  37. static av_cold int init(AVFilterContext *ctx)
  38. {
  39. ShuffleFramesContext *s = ctx->priv;
  40. char *mapping, *saveptr = NULL, *p;
  41. int n, nb_items;
  42. nb_items = 1;
  43. for (p = s->mapping; *p; p++) {
  44. if (*p == '|' || *p == ' ')
  45. nb_items++;
  46. }
  47. s->frames = av_calloc(nb_items, sizeof(*s->frames));
  48. s->map = av_calloc(nb_items, sizeof(*s->map));
  49. s->pts = av_calloc(nb_items, sizeof(*s->pts));
  50. if (!s->map || !s->frames || !s->pts) {
  51. return AVERROR(ENOMEM);
  52. }
  53. mapping = av_strdup(s->mapping);
  54. if (!mapping)
  55. return AVERROR(ENOMEM);
  56. for (n = 0; n < nb_items; n++) {
  57. char *map = av_strtok(n == 0 ? mapping : NULL, " |", &saveptr);
  58. if (!map || sscanf(map, "%d", &s->map[n]) != 1) {
  59. av_free(mapping);
  60. return AVERROR(EINVAL);
  61. }
  62. if (s->map[n] < 0 || s->map[n] >= nb_items) {
  63. av_log(ctx, AV_LOG_ERROR, "Index out of range.\n");
  64. av_free(mapping);
  65. return AVERROR(EINVAL);
  66. }
  67. }
  68. s->nb_frames = nb_items;
  69. av_free(mapping);
  70. return 0;
  71. }
  72. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  73. {
  74. AVFilterContext *ctx = inlink->dst;
  75. ShuffleFramesContext *s = ctx->priv;
  76. int ret;
  77. if (s->in_frames < s->nb_frames) {
  78. s->frames[s->in_frames] = frame;
  79. s->pts[s->in_frames] = frame->pts;
  80. s->in_frames++;
  81. ret = 0;
  82. } else if (s->in_frames == s->nb_frames) {
  83. int n, x;
  84. for (n = 0; n < s->nb_frames; n++) {
  85. AVFrame *out;
  86. x = s->map[n];
  87. out = av_frame_clone(s->frames[x]);
  88. if (!out)
  89. return AVERROR(ENOMEM);
  90. out->pts = s->pts[n];
  91. ret = ff_filter_frame(ctx->outputs[0], out);
  92. s->in_frames--;
  93. }
  94. for (n = 0; n < s->nb_frames; n++)
  95. av_frame_free(&s->frames[n]);
  96. } else
  97. av_assert0(0);
  98. return ret;
  99. }
  100. static av_cold void uninit(AVFilterContext *ctx)
  101. {
  102. ShuffleFramesContext *s = ctx->priv;
  103. av_freep(&s->frames);
  104. av_freep(&s->map);
  105. av_freep(&s->pts);
  106. }
  107. #define OFFSET(x) offsetof(ShuffleFramesContext, x)
  108. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  109. static const AVOption shuffleframes_options[] = {
  110. { "mapping", "set destination indexes of input frames", OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  111. { NULL },
  112. };
  113. AVFILTER_DEFINE_CLASS(shuffleframes);
  114. static const AVFilterPad shuffleframes_inputs[] = {
  115. {
  116. .name = "default",
  117. .type = AVMEDIA_TYPE_VIDEO,
  118. .filter_frame = filter_frame,
  119. },
  120. { NULL },
  121. };
  122. static const AVFilterPad shuffleframes_outputs[] = {
  123. {
  124. .name = "default",
  125. .type = AVMEDIA_TYPE_VIDEO,
  126. },
  127. { NULL },
  128. };
  129. AVFilter ff_vf_shuffleframes = {
  130. .name = "shuffleframes",
  131. .description = NULL_IF_CONFIG_SMALL("Shuffle video frames."),
  132. .priv_size = sizeof(ShuffleFramesContext),
  133. .priv_class = &shuffleframes_class,
  134. .init = init,
  135. .uninit = uninit,
  136. .inputs = shuffleframes_inputs,
  137. .outputs = shuffleframes_outputs,
  138. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  139. };