adpcm: don't duplicate identical AVSampleFmt array for each encoder.
[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
27#include "avfilter.h"
b74a1da4 28#include "formats.h"
9d0bfc50 29#include "internal.h"
803391f7 30#include "video.h"
a1e171df
SS
31#include "libavutil/pixdesc.h"
32#include "libavutil/intreadwrite.h"
737eb597 33#include "libavutil/imgutils.h"
a1e171df
SS
34
35typedef struct {
36 int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
37 int hsub, vsub; ///< chroma subsampling
38} FlipContext;
39
40static int query_formats(AVFilterContext *ctx)
41{
42 static const enum PixelFormat pix_fmts[] = {
43 PIX_FMT_RGB48BE, PIX_FMT_RGB48LE,
c90626b2 44 PIX_FMT_BGR48BE, PIX_FMT_BGR48LE,
a1e171df
SS
45 PIX_FMT_ARGB, PIX_FMT_RGBA,
46 PIX_FMT_ABGR, PIX_FMT_BGRA,
47 PIX_FMT_RGB24, PIX_FMT_BGR24,
48 PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
49 PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
50 PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
51 PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
52 PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
53 PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
54 PIX_FMT_YUV422P16LE, PIX_FMT_YUV422P16BE,
55 PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
56 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
57 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
58 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
59 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
60 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
61 PIX_FMT_YUVA420P,
62 PIX_FMT_RGB8, PIX_FMT_BGR8,
63 PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
64 PIX_FMT_PAL8, PIX_FMT_GRAY8,
65 PIX_FMT_NONE
66 };
67
b74a1da4 68 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
a1e171df
SS
69 return 0;
70}
71
72static int config_props(AVFilterLink *inlink)
73{
74 FlipContext *flip = inlink->dst->priv;
75 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
a1e171df 76
e16f217c 77 av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
a1e171df
SS
78 flip->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
79 flip->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
80
81 return 0;
82}
83
84static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
85{
86 FlipContext *flip = inlink->dst->priv;
87 AVFilterBufferRef *inpic = inlink->cur_buf;
88 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
89 uint8_t *inrow, *outrow;
90 int i, j, plane, step, hsub, vsub;
91
92 for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
93 step = flip->max_step[plane];
94 hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
95 vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
96
97 outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
98 inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane] + ((inlink->w >> hsub) - 1) * step;
99 for (i = 0; i < h>>vsub; i++) {
100 switch (step) {
101 case 1:
a1e171df
SS
102 for (j = 0; j < (inlink->w >> hsub); j++)
103 outrow[j] = inrow[-j];
a1e171df
SS
104 break;
105
106 case 2:
107 {
108 uint16_t *outrow16 = (uint16_t *)outrow;
109 uint16_t * inrow16 = (uint16_t *) inrow;
110 for (j = 0; j < (inlink->w >> hsub); j++)
111 outrow16[j] = inrow16[-j];
112 }
113 break;
114
115 case 3:
116 {
117 uint8_t *in = inrow;
118 uint8_t *out = outrow;
119 for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
120 int32_t v = AV_RB24(in);
121 AV_WB24(out, v);
122 }
123 }
124 break;
125
126 case 4:
127 {
128 uint32_t *outrow32 = (uint32_t *)outrow;
129 uint32_t * inrow32 = (uint32_t *) inrow;
130 for (j = 0; j < (inlink->w >> hsub); j++)
131 outrow32[j] = inrow32[-j];
132 }
133 break;
134
135 default:
136 for (j = 0; j < (inlink->w >> hsub); j++)
137 memcpy(outrow + j*step, inrow - j*step, step);
138 }
139
140 inrow += inpic ->linesize[plane];
141 outrow += outpic->linesize[plane];
142 }
143 }
144
803391f7 145 ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
a1e171df
SS
146}
147
148AVFilter avfilter_vf_hflip = {
149 .name = "hflip",
150 .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
151 .priv_size = sizeof(FlipContext),
152 .query_formats = query_formats,
153
154 .inputs = (AVFilterPad[]) {{ .name = "default",
155 .type = AVMEDIA_TYPE_VIDEO,
156 .draw_slice = draw_slice,
157 .config_props = config_props,
158 .min_perms = AV_PERM_READ, },
159 { .name = NULL}},
160 .outputs = (AVFilterPad[]) {{ .name = "default",
161 .type = AVMEDIA_TYPE_VIDEO, },
162 { .name = NULL}},
163};