vf_vflip.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 {
  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 AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
  40. int w, int h)
  41. {
  42. FlipContext *flip = link->dst->priv;
  43. AVFilterBufferRef *picref;
  44. int i;
  45. if (!(perms & AV_PERM_NEG_LINESIZES))
  46. return ff_default_get_video_buffer(link, perms, w, h);
  47. picref = ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  48. if (!picref)
  49. return NULL;
  50. for (i = 0; i < 4; i ++) {
  51. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  52. if (picref->data[i]) {
  53. picref->data[i] += (((h + (1<<vsub)-1) >> vsub)-1) * picref->linesize[i];
  54. picref->linesize[i] = -picref->linesize[i];
  55. }
  56. }
  57. return picref;
  58. }
  59. static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
  60. {
  61. FlipContext *flip = link->dst->priv;
  62. int i;
  63. for (i = 0; i < 4; i ++) {
  64. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  65. if (frame->data[i]) {
  66. frame->data[i] += (((link->h + (1<<vsub)-1)>> vsub)-1) * frame->linesize[i];
  67. frame->linesize[i] = -frame->linesize[i];
  68. }
  69. }
  70. return ff_filter_frame(link->dst->outputs[0], frame);
  71. }
  72. static const AVFilterPad avfilter_vf_vflip_inputs[] = {
  73. {
  74. .name = "default",
  75. .type = AVMEDIA_TYPE_VIDEO,
  76. .get_video_buffer = get_video_buffer,
  77. .filter_frame = filter_frame,
  78. .config_props = config_input,
  79. },
  80. { NULL }
  81. };
  82. static const AVFilterPad avfilter_vf_vflip_outputs[] = {
  83. {
  84. .name = "default",
  85. .type = AVMEDIA_TYPE_VIDEO,
  86. },
  87. { NULL }
  88. };
  89. AVFilter avfilter_vf_vflip = {
  90. .name = "vflip",
  91. .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
  92. .priv_size = sizeof(FlipContext),
  93. .inputs = avfilter_vf_vflip_inputs,
  94. .outputs = avfilter_vf_vflip_outputs,
  95. };