Add hflip filter.
[libav.git] / libavfilter / vf_hflip.c
CommitLineData
a1e171df
SS
1/*
2 * Copyright (c) 2007 Benoit Fouet
3 * Copyright (c) 2010 Stefano Sabatini
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @flip
24 * horizontal flip filter
25 */
26
27#include "avfilter.h"
28#include "libavutil/pixdesc.h"
29#include "libavutil/intreadwrite.h"
30
31typedef struct {
32 int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
33 int hsub, vsub; ///< chroma subsampling
34} FlipContext;
35
36static int query_formats(AVFilterContext *ctx)
37{
38 static const enum PixelFormat pix_fmts[] = {
39 PIX_FMT_RGB48BE, PIX_FMT_RGB48LE,
40 PIX_FMT_ARGB, PIX_FMT_RGBA,
41 PIX_FMT_ABGR, PIX_FMT_BGRA,
42 PIX_FMT_RGB24, PIX_FMT_BGR24,
43 PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
44 PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
45 PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
46 PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
47 PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
48 PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
49 PIX_FMT_YUV422P16LE, PIX_FMT_YUV422P16BE,
50 PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
51 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
52 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
53 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
54 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
55 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
56 PIX_FMT_YUVA420P,
57 PIX_FMT_RGB8, PIX_FMT_BGR8,
58 PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
59 PIX_FMT_PAL8, PIX_FMT_GRAY8,
60 PIX_FMT_NONE
61 };
62
63 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
64 return 0;
65}
66
67static int config_props(AVFilterLink *inlink)
68{
69 FlipContext *flip = inlink->dst->priv;
70 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
71 int i;
72
73 memset(flip->max_step, 0, sizeof(flip->max_step));
74 for (i = 0; i < 4; i++) {
75 const AVComponentDescriptor *comp = &(pix_desc->comp[i]);
76 if ((comp->step_minus1+1) > flip->max_step[comp->plane])
77 flip->max_step[comp->plane] = comp->step_minus1+1;
78 }
79
80 flip->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
81 flip->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
82
83 return 0;
84}
85
86static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
87{
88 FlipContext *flip = inlink->dst->priv;
89 AVFilterBufferRef *inpic = inlink->cur_buf;
90 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
91 uint8_t *inrow, *outrow;
92 int i, j, plane, step, hsub, vsub;
93
94 for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
95 step = flip->max_step[plane];
96 hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
97 vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
98
99 outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
100 inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane] + ((inlink->w >> hsub) - 1) * step;
101 for (i = 0; i < h>>vsub; i++) {
102 switch (step) {
103 case 1:
104 {
105 for (j = 0; j < (inlink->w >> hsub); j++)
106 outrow[j] = inrow[-j];
107 }
108 break;
109
110 case 2:
111 {
112 uint16_t *outrow16 = (uint16_t *)outrow;
113 uint16_t * inrow16 = (uint16_t *) inrow;
114 for (j = 0; j < (inlink->w >> hsub); j++)
115 outrow16[j] = inrow16[-j];
116 }
117 break;
118
119 case 3:
120 {
121 uint8_t *in = inrow;
122 uint8_t *out = outrow;
123 for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
124 int32_t v = AV_RB24(in);
125 AV_WB24(out, v);
126 }
127 }
128 break;
129
130 case 4:
131 {
132 uint32_t *outrow32 = (uint32_t *)outrow;
133 uint32_t * inrow32 = (uint32_t *) inrow;
134 for (j = 0; j < (inlink->w >> hsub); j++)
135 outrow32[j] = inrow32[-j];
136 }
137 break;
138
139 default:
140 for (j = 0; j < (inlink->w >> hsub); j++)
141 memcpy(outrow + j*step, inrow - j*step, step);
142 }
143
144 inrow += inpic ->linesize[plane];
145 outrow += outpic->linesize[plane];
146 }
147 }
148
149 avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
150}
151
152AVFilter avfilter_vf_hflip = {
153 .name = "hflip",
154 .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
155 .priv_size = sizeof(FlipContext),
156 .query_formats = query_formats,
157
158 .inputs = (AVFilterPad[]) {{ .name = "default",
159 .type = AVMEDIA_TYPE_VIDEO,
160 .draw_slice = draw_slice,
161 .config_props = config_props,
162 .min_perms = AV_PERM_READ, },
163 { .name = NULL}},
164 .outputs = (AVFilterPad[]) {{ .name = "default",
165 .type = AVMEDIA_TYPE_VIDEO, },
166 { .name = NULL}},
167};