vf_vflip.c 3.4 KB

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