vf_swapuv.c 3.5 KB

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