vf_vflip.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  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. * 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. int height = FF_CEIL_RSHIFT(h, vsub);
  50. if (frame->data[i]) {
  51. frame->data[i] += (height - 1) * frame->linesize[i];
  52. frame->linesize[i] = -frame->linesize[i];
  53. }
  54. }
  55. return frame;
  56. }
  57. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  58. {
  59. FlipContext *flip = link->dst->priv;
  60. int i;
  61. for (i = 0; i < 4; i ++) {
  62. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  63. int height = FF_CEIL_RSHIFT(link->h, vsub);
  64. if (frame->data[i]) {
  65. frame->data[i] += (height - 1) * frame->linesize[i];
  66. frame->linesize[i] = -frame->linesize[i];
  67. }
  68. }
  69. return ff_filter_frame(link->dst->outputs[0], frame);
  70. }
  71. static const AVFilterPad avfilter_vf_vflip_inputs[] = {
  72. {
  73. .name = "default",
  74. .type = AVMEDIA_TYPE_VIDEO,
  75. .get_video_buffer = get_video_buffer,
  76. .filter_frame = filter_frame,
  77. .config_props = config_input,
  78. },
  79. { NULL }
  80. };
  81. static const AVFilterPad avfilter_vf_vflip_outputs[] = {
  82. {
  83. .name = "default",
  84. .type = AVMEDIA_TYPE_VIDEO,
  85. },
  86. { NULL }
  87. };
  88. AVFilter ff_vf_vflip = {
  89. .name = "vflip",
  90. .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
  91. .priv_size = sizeof(FlipContext),
  92. .inputs = avfilter_vf_vflip_inputs,
  93. .outputs = avfilter_vf_vflip_outputs,
  94. };