vf_vflip.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "video.h"
  27. typedef struct {
  28. int vsub; ///< vertical chroma subsampling
  29. } FlipContext;
  30. static int config_input(AVFilterLink *link)
  31. {
  32. FlipContext *flip = link->dst->priv;
  33. flip->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  34. return 0;
  35. }
  36. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
  37. int w, int h)
  38. {
  39. FlipContext *flip = link->dst->priv;
  40. AVFilterBufferRef *picref;
  41. int i;
  42. if (!(perms & AV_PERM_NEG_LINESIZES))
  43. return ff_default_get_video_buffer(link, perms, w, h);
  44. picref = avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  45. for (i = 0; i < 4; i ++) {
  46. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  47. if (picref->data[i]) {
  48. picref->data[i] += ((h >> vsub)-1) * picref->linesize[i];
  49. picref->linesize[i] = -picref->linesize[i];
  50. }
  51. }
  52. return picref;
  53. }
  54. static void start_frame(AVFilterLink *link, AVFilterBufferRef *inpicref)
  55. {
  56. FlipContext *flip = link->dst->priv;
  57. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  58. int i;
  59. for (i = 0; i < 4; i ++) {
  60. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  61. if (outpicref->data[i]) {
  62. outpicref->data[i] += ((link->h >> vsub)-1) * outpicref->linesize[i];
  63. outpicref->linesize[i] = -outpicref->linesize[i];
  64. }
  65. }
  66. avfilter_start_frame(link->dst->outputs[0], outpicref);
  67. }
  68. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  69. {
  70. AVFilterContext *ctx = link->dst;
  71. avfilter_draw_slice(ctx->outputs[0], link->h - (y+h), h, -1 * slice_dir);
  72. }
  73. AVFilter avfilter_vf_vflip = {
  74. .name = "vflip",
  75. .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
  76. .priv_size = sizeof(FlipContext),
  77. .inputs = (const AVFilterPad[]) {{ .name = "default",
  78. .type = AVMEDIA_TYPE_VIDEO,
  79. .get_video_buffer = get_video_buffer,
  80. .start_frame = start_frame,
  81. .draw_slice = draw_slice,
  82. .config_props = config_input, },
  83. { .name = NULL}},
  84. .outputs = (const AVFilterPad[]) {{ .name = "default",
  85. .type = AVMEDIA_TYPE_VIDEO, },
  86. { .name = NULL}},
  87. };