vf_swapuv.c 3.1 KB

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