2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of FFmpeg.
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.
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.
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
23 * video vertical flip filter
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
33 typedef struct FlipContext {
35 int vsub; ///< vertical chroma subsampling
39 static const AVOption vflip_options[] = {
43 AVFILTER_DEFINE_CLASS(vflip);
45 static int config_input(AVFilterLink *link)
47 FlipContext *flip = link->dst->priv;
48 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
50 flip->vsub = desc->log2_chroma_h;
51 flip->bayer = !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
56 static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
58 FlipContext *flip = link->dst->priv;
62 frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
66 for (i = 0; i < 4; i ++) {
67 int vsub = i == 1 || i == 2 ? flip->vsub : 0;
68 int height = AV_CEIL_RSHIFT(h, vsub);
71 frame->data[i] += (height - 1) * frame->linesize[i];
72 frame->linesize[i] = -frame->linesize[i];
79 static int flip_bayer(AVFilterLink *link, AVFrame *in)
81 AVFilterContext *ctx = link->dst;
82 AVFilterLink *outlink = ctx->outputs[0];
84 uint8_t *inrow = in->data[0], *outrow;
85 int i, width = outlink->w << (av_pix_fmt_desc_get(link->format)->comp[0].step > 1);
87 av_log(ctx, AV_LOG_ERROR, "Bayer vertical flip needs even height\n");
88 return AVERROR_INVALIDDATA;
91 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
94 return AVERROR(ENOMEM);
96 av_frame_copy_props(out, in);
97 outrow = out->data[0] + out->linesize[0] * (outlink->h - 2);
98 for (i = 0; i < outlink->h >> 1; i++) {
99 memcpy(outrow, inrow, width);
100 memcpy(outrow + out->linesize[0], inrow + in->linesize[0], width);
101 inrow += 2 * in->linesize[0];
102 outrow -= 2 * out->linesize[0];
105 return ff_filter_frame(outlink, out);
108 static int filter_frame(AVFilterLink *link, AVFrame *frame)
110 FlipContext *flip = link->dst->priv;
114 return flip_bayer(link, frame);
116 for (i = 0; i < 4; i ++) {
117 int vsub = i == 1 || i == 2 ? flip->vsub : 0;
118 int height = AV_CEIL_RSHIFT(link->h, vsub);
120 if (frame->data[i]) {
121 frame->data[i] += (height - 1) * frame->linesize[i];
122 frame->linesize[i] = -frame->linesize[i];
126 return ff_filter_frame(link->dst->outputs[0], frame);
128 static const AVFilterPad avfilter_vf_vflip_inputs[] = {
131 .type = AVMEDIA_TYPE_VIDEO,
132 .get_video_buffer = get_video_buffer,
133 .filter_frame = filter_frame,
134 .config_props = config_input,
139 static const AVFilterPad avfilter_vf_vflip_outputs[] = {
142 .type = AVMEDIA_TYPE_VIDEO,
147 const AVFilter ff_vf_vflip = {
149 .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
150 .priv_size = sizeof(FlipContext),
151 .priv_class = &vflip_class,
152 .inputs = avfilter_vf_vflip_inputs,
153 .outputs = avfilter_vf_vflip_outputs,
154 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,