1/*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
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/**
22 * @file
23 * swap UV filter
24 */
25
26#include "libavutil/pixdesc.h"
27#include "avfilter.h"
28#include "formats.h"
29#include "internal.h"
30#include "video.h"
31
32static void do_swap(AVFrame *frame)
33{
34    FFSWAP(uint8_t*,     frame->data[1],     frame->data[2]);
35    FFSWAP(int,          frame->linesize[1], frame->linesize[2]);
36    FFSWAP(uint64_t,     frame->error[1],    frame->error[2]);
37    FFSWAP(AVBufferRef*, frame->buf[1],      frame->buf[2]);
38}
39
40static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
41{
42    AVFrame *picref = ff_default_get_video_buffer(link, w, h);
43    do_swap(picref);
44    return picref;
45}
46
47static int filter_frame(AVFilterLink *link, AVFrame *inpicref)
48{
49    do_swap(inpicref);
50    return ff_filter_frame(link->dst->outputs[0], inpicref);
51}
52
53static int is_planar_yuv(const AVPixFmtDescriptor *desc)
54{
55    int i;
56
57    if (desc->flags & ~(AV_PIX_FMT_FLAG_BE | AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_ALPHA) ||
58        desc->nb_components < 3 ||
59        (desc->comp[1].depth_minus1 != desc->comp[2].depth_minus1))
60        return 0;
61    for (i = 0; i < desc->nb_components; i++) {
62        if (desc->comp[i].offset_plus1 != 1 ||
63            desc->comp[i].shift != 0 ||
64            desc->comp[i].plane != i)
65            return 0;
66    }
67
68    return 1;
69}
70
71static int query_formats(AVFilterContext *ctx)
72{
73    AVFilterFormats *formats = NULL;
74    int fmt;
75
76    for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
77        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
78        if (is_planar_yuv(desc))
79            ff_add_format(&formats, fmt);
80    }
81
82    ff_set_common_formats(ctx, formats);
83    return 0;
84}
85
86static const AVFilterPad swapuv_inputs[] = {
87    {
88        .name             = "default",
89        .type             = AVMEDIA_TYPE_VIDEO,
90        .get_video_buffer = get_video_buffer,
91        .filter_frame     = filter_frame,
92    },
93    { NULL }
94};
95
96static const AVFilterPad swapuv_outputs[] = {
97    {
98        .name = "default",
99        .type = AVMEDIA_TYPE_VIDEO,
100    },
101    { NULL }
102};
103
104AVFilter ff_vf_swapuv = {
105    .name          = "swapuv",
106    .description   = NULL_IF_CONFIG_SMALL("Swap U and V components."),
107    .query_formats = query_formats,
108    .inputs        = swapuv_inputs,
109    .outputs       = swapuv_outputs,
110};
111