vf_vflip.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. * video vertical flip filter
  23. */
  24. #include "libavutil/internal.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct FlipContext {
  30. int vsub; ///< vertical chroma subsampling
  31. } FlipContext;
  32. static int config_input(AVFilterLink *link)
  33. {
  34. FlipContext *flip = link->dst->priv;
  35. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  36. flip->vsub = desc->log2_chroma_h;
  37. return 0;
  38. }
  39. static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
  40. {
  41. FlipContext *flip = link->dst->priv;
  42. AVFrame *frame;
  43. int i;
  44. frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
  45. if (!frame)
  46. return NULL;
  47. for (i = 0; i < 4; i ++) {
  48. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  49. if (frame->data[i]) {
  50. frame->data[i] += ((h >> vsub) - 1) * frame->linesize[i];
  51. frame->linesize[i] = -frame->linesize[i];
  52. }
  53. }
  54. return frame;
  55. }
  56. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  57. {
  58. FlipContext *flip = link->dst->priv;
  59. int i;
  60. for (i = 0; i < 4; i ++) {
  61. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  62. if (frame->data[i]) {
  63. frame->data[i] += ((link->h >> vsub)-1) * frame->linesize[i];
  64. frame->linesize[i] = -frame->linesize[i];
  65. }
  66. }
  67. return ff_filter_frame(link->dst->outputs[0], frame);
  68. }
  69. static const AVFilterPad avfilter_vf_vflip_inputs[] = {
  70. {
  71. .name = "default",
  72. .type = AVMEDIA_TYPE_VIDEO,
  73. .get_video_buffer = get_video_buffer,
  74. .filter_frame = filter_frame,
  75. .config_props = config_input,
  76. },
  77. { NULL }
  78. };
  79. static const AVFilterPad avfilter_vf_vflip_outputs[] = {
  80. {
  81. .name = "default",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. },
  84. { NULL }
  85. };
  86. AVFilter ff_vf_vflip = {
  87. .name = "vflip",
  88. .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
  89. .priv_size = sizeof(FlipContext),
  90. .inputs = avfilter_vf_vflip_inputs,
  91. .outputs = avfilter_vf_vflip_outputs,
  92. };