fate: add h263 obmc vsynth tests
[libav.git] / libavfilter / vf_hflip.c
CommitLineData
a1e171df
SS
1/*
2 * Copyright (c) 2007 Benoit Fouet
3 * Copyright (c) 2010 Stefano Sabatini
4 *
2912e87a 5 * This file is part of Libav.
a1e171df 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
a1e171df
SS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
a1e171df
SS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
a1e171df
SS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
7fe82bd7 23 * @file
a1e171df
SS
24 * horizontal flip filter
25 */
26
1d9c2dc8
MS
27#include <string.h>
28
a1e171df 29#include "avfilter.h"
b74a1da4 30#include "formats.h"
9d0bfc50 31#include "internal.h"
803391f7 32#include "video.h"
a1e171df 33#include "libavutil/pixdesc.h"
1d9c2dc8 34#include "libavutil/internal.h"
a1e171df 35#include "libavutil/intreadwrite.h"
737eb597 36#include "libavutil/imgutils.h"
a1e171df
SS
37
38typedef struct {
39 int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
40 int hsub, vsub; ///< chroma subsampling
41} FlipContext;
42
43static int query_formats(AVFilterContext *ctx)
44{
716d413c
AK
45 static const enum AVPixelFormat pix_fmts[] = {
46 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGB48LE,
47 AV_PIX_FMT_BGR48BE, AV_PIX_FMT_BGR48LE,
48 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
49 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
50 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
51 AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
52 AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
53 AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
54 AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
55 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
56 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
57 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
58 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
59 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
60 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
61 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
62 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
63 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
64 AV_PIX_FMT_YUVA420P,
65 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
66 AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
67 AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
68 AV_PIX_FMT_NONE
a1e171df
SS
69 };
70
b74a1da4 71 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
a1e171df
SS
72 return 0;
73}
74
75static int config_props(AVFilterLink *inlink)
76{
77 FlipContext *flip = inlink->dst->priv;
78 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
a1e171df 79
e16f217c 80 av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
a1e171df
SS
81 flip->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
82 flip->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
83
84 return 0;
85}
86
e9b992d0 87static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
a1e171df
SS
88{
89 FlipContext *flip = inlink->dst->priv;
90 AVFilterBufferRef *inpic = inlink->cur_buf;
91 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
92 uint8_t *inrow, *outrow;
93 int i, j, plane, step, hsub, vsub;
94
95 for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
96 step = flip->max_step[plane];
97 hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
98 vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
99
100 outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
101 inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane] + ((inlink->w >> hsub) - 1) * step;
102 for (i = 0; i < h>>vsub; i++) {
103 switch (step) {
104 case 1:
a1e171df
SS
105 for (j = 0; j < (inlink->w >> hsub); j++)
106 outrow[j] = inrow[-j];
a1e171df
SS
107 break;
108
109 case 2:
110 {
111 uint16_t *outrow16 = (uint16_t *)outrow;
112 uint16_t * inrow16 = (uint16_t *) inrow;
113 for (j = 0; j < (inlink->w >> hsub); j++)
114 outrow16[j] = inrow16[-j];
115 }
116 break;
117
118 case 3:
119 {
120 uint8_t *in = inrow;
121 uint8_t *out = outrow;
122 for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
123 int32_t v = AV_RB24(in);
124 AV_WB24(out, v);
125 }
126 }
127 break;
128
129 case 4:
130 {
131 uint32_t *outrow32 = (uint32_t *)outrow;
132 uint32_t * inrow32 = (uint32_t *) inrow;
133 for (j = 0; j < (inlink->w >> hsub); j++)
134 outrow32[j] = inrow32[-j];
135 }
136 break;
137
138 default:
139 for (j = 0; j < (inlink->w >> hsub); j++)
140 memcpy(outrow + j*step, inrow - j*step, step);
141 }
142
143 inrow += inpic ->linesize[plane];
144 outrow += outpic->linesize[plane];
145 }
146 }
147
e9b992d0 148 return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
a1e171df
SS
149}
150
151AVFilter avfilter_vf_hflip = {
152 .name = "hflip",
153 .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
154 .priv_size = sizeof(FlipContext),
155 .query_formats = query_formats,
156
3db40703
RB
157 .inputs = (const AVFilterPad[]) {{ .name = "default",
158 .type = AVMEDIA_TYPE_VIDEO,
159 .draw_slice = draw_slice,
160 .config_props = config_props,
161 .min_perms = AV_PERM_READ, },
162 { .name = NULL}},
163 .outputs = (const AVFilterPad[]) {{ .name = "default",
164 .type = AVMEDIA_TYPE_VIDEO, },
165 { .name = NULL}},
a1e171df 166};