vf_vflip.c 2.88 KB
Newer Older
1
/*
2
 * Copyright (c) 2007 Bobby Bingham
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
18 19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
22
 * @file
23 24 25
 * video vertical flip filter
 */

26
#include "libavutil/internal.h"
27
#include "libavutil/pixdesc.h"
28
#include "avfilter.h"
29
#include "internal.h"
30
#include "video.h"
31

32
typedef struct FlipContext {
33 34 35 36 37 38
    int vsub;   ///< vertical chroma subsampling
} FlipContext;

static int config_input(AVFilterLink *link)
{
    FlipContext *flip = link->dst->priv;
39
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
40

41
    flip->vsub = desc->log2_chroma_h;
42 43 44 45

    return 0;
}

46
static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
47 48
{
    FlipContext *flip = link->dst->priv;
49
    AVFrame *frame;
50 51
    int i;

52 53
    frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
    if (!frame)
54 55
        return NULL;

56 57 58
    for (i = 0; i < 4; i ++) {
        int vsub = i == 1 || i == 2 ? flip->vsub : 0;

59 60 61
        if (frame->data[i]) {
            frame->data[i] += ((h >> vsub) - 1) * frame->linesize[i];
            frame->linesize[i] = -frame->linesize[i];
62 63 64
        }
    }

65
    return frame;
66 67
}

68
static int filter_frame(AVFilterLink *link, AVFrame *frame)
69 70 71 72
{
    FlipContext *flip = link->dst->priv;
    int i;

73 74 75
    for (i = 0; i < 4; i ++) {
        int vsub = i == 1 || i == 2 ? flip->vsub : 0;

76 77 78
        if (frame->data[i]) {
            frame->data[i] += ((link->h >> vsub)-1) * frame->linesize[i];
            frame->linesize[i] = -frame->linesize[i];
79 80 81
        }
    }

82
    return ff_filter_frame(link->dst->outputs[0], frame);
83
}
84 85 86 87 88
static const AVFilterPad avfilter_vf_vflip_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .get_video_buffer = get_video_buffer,
89
        .filter_frame     = filter_frame,
90 91 92 93 94 95 96 97 98 99 100 101 102
        .config_props     = config_input,
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_vflip_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

103
AVFilter ff_vf_vflip = {
104
    .name      = "vflip",
105 106
    .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),

107 108
    .priv_size = sizeof(FlipContext),

109 110
    .inputs    = avfilter_vf_vflip_inputs,
    .outputs   = avfilter_vf_vflip_outputs,
111
};