libavfilter/vf_vflip.c
c38ae71f
 /*
3fa77bde
  * Copyright (c) 2007 Bobby Bingham
c38ae71f
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
c38ae71f
  * video vertical flip filter
  */
 
1d9c2dc8
 #include "libavutil/internal.h"
88bcdf10
 #include "libavutil/opt.h"
b4b66456
 #include "libavutil/pixdesc.h"
c38ae71f
 #include "avfilter.h"
9d0bfc50
 #include "internal.h"
43c7a01e
 #include "video.h"
c38ae71f
 
58400ac1
 typedef struct FlipContext {
88bcdf10
     const AVClass *class;
c38ae71f
     int vsub;   ///< vertical chroma subsampling
 } FlipContext;
 
88bcdf10
 static const AVOption vflip_options[] = {
     { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(vflip);
 
c38ae71f
 static int config_input(AVFilterLink *link)
 {
     FlipContext *flip = link->dst->priv;
59ee9f78
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
c38ae71f
 
59ee9f78
     flip->vsub = desc->log2_chroma_h;
c38ae71f
 
     return 0;
 }
 
7e350379
 static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
c38ae71f
 {
     FlipContext *flip = link->dst->priv;
7e350379
     AVFrame *frame;
c38ae71f
     int i;
 
7e350379
     frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
     if (!frame)
8f3a3ce7
         return NULL;
 
e87eb039
     for (i = 0; i < 4; i ++) {
         int vsub = i == 1 || i == 2 ? flip->vsub : 0;
21f94684
         int height = AV_CEIL_RSHIFT(h, vsub);
e87eb039
 
7e350379
         if (frame->data[i]) {
60f0e304
             frame->data[i] += (height - 1) * frame->linesize[i];
7e350379
             frame->linesize[i] = -frame->linesize[i];
c38ae71f
         }
     }
 
7e350379
     return frame;
c38ae71f
 }
 
7e350379
 static int filter_frame(AVFilterLink *link, AVFrame *frame)
c38ae71f
 {
     FlipContext *flip = link->dst->priv;
     int i;
 
e87eb039
     for (i = 0; i < 4; i ++) {
         int vsub = i == 1 || i == 2 ? flip->vsub : 0;
21f94684
         int height = AV_CEIL_RSHIFT(link->h, vsub);
e87eb039
 
267290ce
         if (frame->data[i]) {
60f0e304
             frame->data[i] += (height - 1) * frame->linesize[i];
267290ce
             frame->linesize[i] = -frame->linesize[i];
c38ae71f
         }
     }
 
267290ce
     return ff_filter_frame(link->dst->outputs[0], frame);
c38ae71f
 }
568c70e7
 static const AVFilterPad avfilter_vf_vflip_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = get_video_buffer,
267290ce
         .filter_frame     = filter_frame,
568c70e7
         .config_props     = config_input,
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_vflip_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_vflip = {
b211607b
     .name        = "vflip",
b70bc022
     .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
b211607b
     .priv_size   = sizeof(FlipContext),
88bcdf10
     .priv_class  = &vflip_class,
b211607b
     .inputs      = avfilter_vf_vflip_inputs,
     .outputs     = avfilter_vf_vflip_outputs,
88bcdf10
     .flags       = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
c38ae71f
 };