You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.3KB

  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/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct FlipContext {
  31. const AVClass *class;
  32. int vsub; ///< vertical chroma subsampling
  33. int bayer;
  34. } FlipContext;
  35. static const AVOption vflip_options[] = {
  36. { NULL }
  37. };
  38. AVFILTER_DEFINE_CLASS(vflip);
  39. static int config_input(AVFilterLink *link)
  40. {
  41. FlipContext *flip = link->dst->priv;
  42. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  43. flip->vsub = desc->log2_chroma_h;
  44. flip->bayer = !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
  45. return 0;
  46. }
  47. static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
  48. {
  49. FlipContext *flip = link->dst->priv;
  50. AVFrame *frame;
  51. int i;
  52. frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
  53. if (!frame)
  54. return NULL;
  55. for (i = 0; i < 4; i ++) {
  56. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  57. int height = AV_CEIL_RSHIFT(h, vsub);
  58. if (frame->data[i]) {
  59. frame->data[i] += (height - 1) * frame->linesize[i];
  60. frame->linesize[i] = -frame->linesize[i];
  61. }
  62. }
  63. return frame;
  64. }
  65. static int flip_bayer(AVFilterLink *link, AVFrame *in)
  66. {
  67. AVFilterContext *ctx = link->dst;
  68. AVFilterLink *outlink = ctx->outputs[0];
  69. AVFrame *out;
  70. uint8_t *inrow = in->data[0], *outrow;
  71. int i, width = outlink->w << (av_pix_fmt_desc_get(link->format)->comp[0].step > 1);
  72. if (outlink->h & 1) {
  73. av_log(ctx, AV_LOG_ERROR, "Bayer vertical flip needs even height\n");
  74. return AVERROR_INVALIDDATA;
  75. }
  76. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  77. if (!out) {
  78. av_frame_free(&in);
  79. return AVERROR(ENOMEM);
  80. }
  81. av_frame_copy_props(out, in);
  82. outrow = out->data[0] + out->linesize[0] * (outlink->h - 2);
  83. for (i = 0; i < outlink->h >> 1; i++) {
  84. memcpy(outrow, inrow, width);
  85. memcpy(outrow + out->linesize[0], inrow + in->linesize[0], width);
  86. inrow += 2 * in->linesize[0];
  87. outrow -= 2 * out->linesize[0];
  88. }
  89. av_frame_free(&in);
  90. return ff_filter_frame(outlink, out);
  91. }
  92. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  93. {
  94. FlipContext *flip = link->dst->priv;
  95. int i;
  96. if (flip->bayer)
  97. return flip_bayer(link, frame);
  98. for (i = 0; i < 4; i ++) {
  99. int vsub = i == 1 || i == 2 ? flip->vsub : 0;
  100. int height = AV_CEIL_RSHIFT(link->h, vsub);
  101. if (frame->data[i]) {
  102. frame->data[i] += (height - 1) * frame->linesize[i];
  103. frame->linesize[i] = -frame->linesize[i];
  104. }
  105. }
  106. return ff_filter_frame(link->dst->outputs[0], frame);
  107. }
  108. static const AVFilterPad avfilter_vf_vflip_inputs[] = {
  109. {
  110. .name = "default",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .get_video_buffer = get_video_buffer,
  113. .filter_frame = filter_frame,
  114. .config_props = config_input,
  115. },
  116. { NULL }
  117. };
  118. static const AVFilterPad avfilter_vf_vflip_outputs[] = {
  119. {
  120. .name = "default",
  121. .type = AVMEDIA_TYPE_VIDEO,
  122. },
  123. { NULL }
  124. };
  125. AVFilter ff_vf_vflip = {
  126. .name = "vflip",
  127. .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
  128. .priv_size = sizeof(FlipContext),
  129. .priv_class = &vflip_class,
  130. .inputs = avfilter_vf_vflip_inputs,
  131. .outputs = avfilter_vf_vflip_outputs,
  132. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  133. };