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.

163 lines
4.4KB

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