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.

175 lines
5.3KB

  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. return ret;
  57. }
  58. }
  59. }
  60. return ff_set_common_formats(ctx, formats);
  61. }
  62. static av_cold int shuffleplanes_config_input(AVFilterLink *inlink)
  63. {
  64. AVFilterContext *ctx = inlink->dst;
  65. ShufflePlanesContext *s = ctx->priv;
  66. int used[4] = { 0 };
  67. int i;
  68. s->copy = 0;
  69. s->planes = av_pix_fmt_count_planes(inlink->format);
  70. for (i = 0; i < s->planes; i++) {
  71. if (used[s->map[i]])
  72. s->copy = 1;
  73. used[s->map[i]]++;
  74. }
  75. return 0;
  76. }
  77. static int shuffleplanes_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  78. {
  79. AVFilterContext *ctx = inlink->dst;
  80. ShufflePlanesContext *s = ctx->priv;
  81. uint8_t *shuffled_data[4] = { NULL };
  82. int shuffled_linesize[4] = { 0 };
  83. int i, ret;
  84. for (i = 0; i < s->planes; i++) {
  85. shuffled_data[i] = frame->data[s->map[i]];
  86. shuffled_linesize[i] = frame->linesize[s->map[i]];
  87. }
  88. memcpy(frame->data, shuffled_data, sizeof(shuffled_data));
  89. memcpy(frame->linesize, shuffled_linesize, sizeof(shuffled_linesize));
  90. if (s->copy) {
  91. AVFrame *copy = ff_get_video_buffer(ctx->outputs[0], frame->width, frame->height);
  92. if (!copy) {
  93. ret = AVERROR(ENOMEM);
  94. goto fail;
  95. }
  96. av_frame_copy(copy, frame);
  97. ret = av_frame_copy_props(copy, frame);
  98. if (ret < 0) {
  99. av_frame_free(&copy);
  100. goto fail;
  101. }
  102. av_frame_free(&frame);
  103. frame = copy;
  104. }
  105. return ff_filter_frame(ctx->outputs[0], frame);
  106. fail:
  107. av_frame_free(&frame);
  108. return ret;
  109. }
  110. #define OFFSET(x) offsetof(ShufflePlanesContext, x)
  111. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  112. static const AVOption shuffleplanes_options[] = {
  113. { "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 },
  114. { "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 },
  115. { "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 },
  116. { "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 },
  117. { NULL },
  118. };
  119. AVFILTER_DEFINE_CLASS(shuffleplanes);
  120. static const AVFilterPad shuffleplanes_inputs[] = {
  121. {
  122. .name = "default",
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .config_props = shuffleplanes_config_input,
  125. .filter_frame = shuffleplanes_filter_frame,
  126. },
  127. { NULL },
  128. };
  129. static const AVFilterPad shuffleplanes_outputs[] = {
  130. {
  131. .name = "default",
  132. .type = AVMEDIA_TYPE_VIDEO,
  133. },
  134. { NULL },
  135. };
  136. AVFilter ff_vf_shuffleplanes = {
  137. .name = "shuffleplanes",
  138. .description = NULL_IF_CONFIG_SMALL("Shuffle video planes."),
  139. .priv_size = sizeof(ShufflePlanesContext),
  140. .priv_class = &shuffleplanes_class,
  141. .query_formats = query_formats,
  142. .inputs = shuffleplanes_inputs,
  143. .outputs = shuffleplanes_outputs,
  144. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  145. };