vf_shuffleframes.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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] < -1 || 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 = 0;
  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. }
  82. 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. if (x >= 0) {
  88. out = av_frame_clone(s->frames[x]);
  89. if (!out)
  90. return AVERROR(ENOMEM);
  91. out->pts = s->pts[n];
  92. ret = ff_filter_frame(ctx->outputs[0], out);
  93. }
  94. s->in_frames--;
  95. }
  96. for (n = 0; n < s->nb_frames; n++)
  97. av_frame_free(&s->frames[n]);
  98. }
  99. return ret;
  100. }
  101. static av_cold void uninit(AVFilterContext *ctx)
  102. {
  103. ShuffleFramesContext *s = ctx->priv;
  104. while (s->in_frames > 0) {
  105. s->in_frames--;
  106. av_frame_free(&s->frames[s->in_frames]);
  107. }
  108. av_freep(&s->frames);
  109. av_freep(&s->map);
  110. av_freep(&s->pts);
  111. }
  112. #define OFFSET(x) offsetof(ShuffleFramesContext, x)
  113. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  114. static const AVOption shuffleframes_options[] = {
  115. { "mapping", "set destination indexes of input frames", OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  116. { NULL },
  117. };
  118. AVFILTER_DEFINE_CLASS(shuffleframes);
  119. static const AVFilterPad shuffleframes_inputs[] = {
  120. {
  121. .name = "default",
  122. .type = AVMEDIA_TYPE_VIDEO,
  123. .filter_frame = filter_frame,
  124. },
  125. { NULL },
  126. };
  127. static const AVFilterPad shuffleframes_outputs[] = {
  128. {
  129. .name = "default",
  130. .type = AVMEDIA_TYPE_VIDEO,
  131. },
  132. { NULL },
  133. };
  134. AVFilter ff_vf_shuffleframes = {
  135. .name = "shuffleframes",
  136. .description = NULL_IF_CONFIG_SMALL("Shuffle video frames."),
  137. .priv_size = sizeof(ShuffleFramesContext),
  138. .priv_class = &shuffleframes_class,
  139. .init = init,
  140. .uninit = uninit,
  141. .inputs = shuffleframes_inputs,
  142. .outputs = shuffleframes_outputs,
  143. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  144. };