2 * Copyright (c) 2007 Benoit Fouet
3 * Copyright (c) 2010 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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
24 * horizontal flip filter
29 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/imgutils.h"
40 static const AVOption hflip_options[] = {
44 AVFILTER_DEFINE_CLASS(hflip);
46 static int query_formats(AVFilterContext *ctx)
48 AVFilterFormats *pix_fmts = NULL;
51 for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
52 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
53 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
54 desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
55 (desc->log2_chroma_w != desc->log2_chroma_h &&
56 desc->comp[0].plane == desc->comp[1].plane)) &&
57 (ret = ff_add_format(&pix_fmts, fmt)) < 0)
61 return ff_set_common_formats(ctx, pix_fmts);
64 static void hflip_byte_c(const uint8_t *src, uint8_t *dst, int w)
68 for (j = 0; j < w; j++)
72 static void hflip_short_c(const uint8_t *ssrc, uint8_t *ddst, int w)
74 const uint16_t *src = (const uint16_t *)ssrc;
75 uint16_t *dst = (uint16_t *)ddst;
78 for (j = 0; j < w; j++)
82 static void hflip_dword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
84 const uint32_t *src = (const uint32_t *)ssrc;
85 uint32_t *dst = (uint32_t *)ddst;
88 for (j = 0; j < w; j++)
92 static void hflip_b24_c(const uint8_t *src, uint8_t *dst, int w)
94 const uint8_t *in = src;
98 for (j = 0; j < w; j++, out += 3, in -= 3) {
99 int32_t v = AV_RB24(in);
105 static void hflip_b48_c(const uint8_t *src, uint8_t *dst, int w)
107 const uint8_t *in = src;
111 for (j = 0; j < w; j++, out += 6, in -= 6) {
112 int64_t v = AV_RB48(in);
118 static void hflip_qword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
120 const uint64_t *src = (const uint64_t *)ssrc;
121 uint64_t *dst = (uint64_t *)ddst;
124 for (j = 0; j < w; j++)
128 static int config_props(AVFilterLink *inlink)
130 FlipContext *s = inlink->dst->priv;
131 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
132 const int hsub = pix_desc->log2_chroma_w;
133 const int vsub = pix_desc->log2_chroma_h;
136 av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
137 s->planewidth[0] = s->planewidth[3] = inlink->w;
138 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
139 s->planeheight[0] = s->planeheight[3] = inlink->h;
140 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
141 s->bayer_plus1 = !!(pix_desc->flags & AV_PIX_FMT_FLAG_BAYER) + 1;
143 nb_planes = av_pix_fmt_count_planes(inlink->format);
145 return ff_hflip_init(s, s->max_step, nb_planes);
148 int ff_hflip_init(FlipContext *s, int step[4], int nb_planes)
152 for (i = 0; i < nb_planes; i++) {
153 step[i] *= s->bayer_plus1;
155 case 1: s->flip_line[i] = hflip_byte_c; break;
156 case 2: s->flip_line[i] = hflip_short_c; break;
157 case 3: s->flip_line[i] = hflip_b24_c; break;
158 case 4: s->flip_line[i] = hflip_dword_c; break;
159 case 6: s->flip_line[i] = hflip_b48_c; break;
160 case 8: s->flip_line[i] = hflip_qword_c; break;
166 ff_hflip_init_x86(s, step, nb_planes);
171 typedef struct ThreadData {
175 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
177 FlipContext *s = ctx->priv;
178 ThreadData *td = arg;
179 AVFrame *in = td->in;
180 AVFrame *out = td->out;
181 uint8_t *inrow, *outrow;
184 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
185 const int width = s->planewidth[plane] / s->bayer_plus1;
186 const int height = s->planeheight[plane];
187 const int start = (height * job ) / nb_jobs;
188 const int end = (height * (job+1)) / nb_jobs;
190 step = s->max_step[plane];
192 outrow = out->data[plane] + start * out->linesize[plane];
193 inrow = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
194 for (i = start; i < end; i++) {
195 s->flip_line[plane](inrow, outrow, width);
197 inrow += in ->linesize[plane];
198 outrow += out->linesize[plane];
205 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
207 AVFilterContext *ctx = inlink->dst;
208 AVFilterLink *outlink = ctx->outputs[0];
212 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
215 return AVERROR(ENOMEM);
217 av_frame_copy_props(out, in);
219 /* copy palette if required */
220 if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
221 memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
223 td.in = in, td.out = out;
224 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
227 return ff_filter_frame(outlink, out);
230 static const AVFilterPad avfilter_vf_hflip_inputs[] = {
233 .type = AVMEDIA_TYPE_VIDEO,
234 .filter_frame = filter_frame,
235 .config_props = config_props,
240 static const AVFilterPad avfilter_vf_hflip_outputs[] = {
243 .type = AVMEDIA_TYPE_VIDEO,
248 const AVFilter ff_vf_hflip = {
250 .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
251 .priv_size = sizeof(FlipContext),
252 .priv_class = &hflip_class,
253 .query_formats = query_formats,
254 .inputs = avfilter_vf_hflip_inputs,
255 .outputs = avfilter_vf_hflip_outputs,
256 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,