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