vf_swapuv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  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. /**
  21. * @file
  22. * swap UV filter
  23. */
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. static void do_swap(AVFrame *frame)
  30. {
  31. FFSWAP(uint8_t*, frame->data[1], frame->data[2]);
  32. FFSWAP(int, frame->linesize[1], frame->linesize[2]);
  33. FFSWAP(uint64_t, frame->error[1], frame->error[2]);
  34. FFSWAP(AVBufferRef*, frame->buf[1], frame->buf[2]);
  35. }
  36. static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
  37. {
  38. AVFrame *picref = ff_default_get_video_buffer(link, w, h);
  39. do_swap(picref);
  40. return picref;
  41. }
  42. static int filter_frame(AVFilterLink *link, AVFrame *inpicref)
  43. {
  44. do_swap(inpicref);
  45. return ff_filter_frame(link->dst->outputs[0], inpicref);
  46. }
  47. static int is_planar_yuv(const AVPixFmtDescriptor *desc)
  48. {
  49. int i;
  50. if (desc->flags & ~(AV_PIX_FMT_FLAG_BE | AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_ALPHA) ||
  51. desc->nb_components < 3 ||
  52. (desc->comp[1].depth_minus1 != desc->comp[2].depth_minus1))
  53. return 0;
  54. for (i = 0; i < desc->nb_components; i++) {
  55. if (desc->comp[i].offset_plus1 != 1 ||
  56. desc->comp[i].shift != 0 ||
  57. desc->comp[i].plane != i)
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. AVFilterFormats *formats = NULL;
  65. int fmt;
  66. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  67. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  68. if (is_planar_yuv(desc))
  69. ff_add_format(&formats, fmt);
  70. }
  71. ff_set_common_formats(ctx, formats);
  72. return 0;
  73. }
  74. static const AVFilterPad swapuv_inputs[] = {
  75. {
  76. .name = "default",
  77. .type = AVMEDIA_TYPE_VIDEO,
  78. .get_video_buffer = get_video_buffer,
  79. .filter_frame = filter_frame,
  80. },
  81. { NULL }
  82. };
  83. static const AVFilterPad swapuv_outputs[] = {
  84. {
  85. .name = "default",
  86. .type = AVMEDIA_TYPE_VIDEO,
  87. },
  88. { NULL }
  89. };
  90. AVFilter ff_vf_swapuv = {
  91. .name = "swapuv",
  92. .description = NULL_IF_CONFIG_SMALL("Swap U and V components."),
  93. .query_formats = query_formats,
  94. .inputs = swapuv_inputs,
  95. .outputs = swapuv_outputs,
  96. };