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.

176 lines
5.4KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avstring.h"
  19. #include "libavutil/common.h"
  20. #include "libavutil/internal.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/pixfmt.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct ShufflePlanesContext {
  28. const AVClass *class;
  29. /* number of planes in the selected pixel format */
  30. int planes;
  31. /* mapping indices */
  32. int map[4];
  33. /* set to 1 if some plane is used more than once, so we need to make a copy */
  34. int copy;
  35. } ShufflePlanesContext;
  36. static int query_formats(AVFilterContext *ctx)
  37. {
  38. AVFilterFormats *formats = NULL;
  39. ShufflePlanesContext *s = ctx->priv;
  40. int fmt, ret, i;
  41. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  42. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  43. int planes = av_pix_fmt_count_planes(fmt);
  44. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
  45. !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
  46. for (i = 0; i < 4; i++) {
  47. if (s->map[i] >= planes)
  48. break;
  49. if ((desc->log2_chroma_h || desc->log2_chroma_w) &&
  50. (i == 1 || i == 2) != (s->map[i] == 1 || s->map[i] == 2))
  51. break;
  52. }
  53. if (i != 4)
  54. continue;
  55. if ((ret = ff_add_format(&formats, fmt)) < 0) {
  56. ff_formats_unref(&formats);
  57. return ret;
  58. }
  59. }
  60. }
  61. return ff_set_common_formats(ctx, formats);
  62. }
  63. static av_cold int shuffleplanes_config_input(AVFilterLink *inlink)
  64. {
  65. AVFilterContext *ctx = inlink->dst;
  66. ShufflePlanesContext *s = ctx->priv;
  67. int used[4] = { 0 };
  68. int i;
  69. s->copy = 0;
  70. s->planes = av_pix_fmt_count_planes(inlink->format);
  71. for (i = 0; i < s->planes; i++) {
  72. if (used[s->map[i]])
  73. s->copy = 1;
  74. used[s->map[i]]++;
  75. }
  76. return 0;
  77. }
  78. static int shuffleplanes_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  79. {
  80. AVFilterContext *ctx = inlink->dst;
  81. ShufflePlanesContext *s = ctx->priv;
  82. uint8_t *shuffled_data[4] = { NULL };
  83. int shuffled_linesize[4] = { 0 };
  84. int i, ret;
  85. for (i = 0; i < s->planes; i++) {
  86. shuffled_data[i] = frame->data[s->map[i]];
  87. shuffled_linesize[i] = frame->linesize[s->map[i]];
  88. }
  89. memcpy(frame->data, shuffled_data, sizeof(shuffled_data));
  90. memcpy(frame->linesize, shuffled_linesize, sizeof(shuffled_linesize));
  91. if (s->copy) {
  92. AVFrame *copy = ff_get_video_buffer(ctx->outputs[0], frame->width, frame->height);
  93. if (!copy) {
  94. ret = AVERROR(ENOMEM);
  95. goto fail;
  96. }
  97. av_frame_copy(copy, frame);
  98. ret = av_frame_copy_props(copy, frame);
  99. if (ret < 0) {
  100. av_frame_free(&copy);
  101. goto fail;
  102. }
  103. av_frame_free(&frame);
  104. frame = copy;
  105. }
  106. return ff_filter_frame(ctx->outputs[0], frame);
  107. fail:
  108. av_frame_free(&frame);
  109. return ret;
  110. }
  111. #define OFFSET(x) offsetof(ShufflePlanesContext, x)
  112. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  113. static const AVOption shuffleplanes_options[] = {
  114. { "map0", "Index of the input plane to be used as the first output plane ", OFFSET(map[0]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
  115. { "map1", "Index of the input plane to be used as the second output plane ", OFFSET(map[1]), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 3, FLAGS },
  116. { "map2", "Index of the input plane to be used as the third output plane ", OFFSET(map[2]), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, 3, FLAGS },
  117. { "map3", "Index of the input plane to be used as the fourth output plane ", OFFSET(map[3]), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 3, FLAGS },
  118. { NULL },
  119. };
  120. AVFILTER_DEFINE_CLASS(shuffleplanes);
  121. static const AVFilterPad shuffleplanes_inputs[] = {
  122. {
  123. .name = "default",
  124. .type = AVMEDIA_TYPE_VIDEO,
  125. .config_props = shuffleplanes_config_input,
  126. .filter_frame = shuffleplanes_filter_frame,
  127. },
  128. { NULL },
  129. };
  130. static const AVFilterPad shuffleplanes_outputs[] = {
  131. {
  132. .name = "default",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. },
  135. { NULL },
  136. };
  137. AVFilter ff_vf_shuffleplanes = {
  138. .name = "shuffleplanes",
  139. .description = NULL_IF_CONFIG_SMALL("Shuffle video planes."),
  140. .priv_size = sizeof(ShufflePlanesContext),
  141. .priv_class = &shuffleplanes_class,
  142. .query_formats = query_formats,
  143. .inputs = shuffleplanes_inputs,
  144. .outputs = shuffleplanes_outputs,
  145. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  146. };