2 * Copyright (c) 2007 Benoit Fouet
3 * Copyright (c) 2010 Stefano Sabatini
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * horizontal flip filter
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/imgutils.h"
38 typedef struct FlipContext {
39 int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
40 int hsub, vsub; ///< chroma subsampling
43 static int query_formats(AVFilterContext *ctx)
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,
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,
71 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
75 static int config_props(AVFilterLink *inlink)
77 FlipContext *s = inlink->dst->priv;
78 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
80 av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
81 s->hsub = pix_desc->log2_chroma_w;
82 s->vsub = pix_desc->log2_chroma_h;
87 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
89 AVFilterContext *ctx = inlink->dst;
90 FlipContext *s = ctx->priv;
91 AVFilterLink *outlink = ctx->outputs[0];
93 uint8_t *inrow, *outrow;
94 int i, j, plane, step, hsub, vsub;
96 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
99 return AVERROR(ENOMEM);
101 av_frame_copy_props(out, in);
103 for (plane = 0; plane < 4 && in->data[plane]; plane++) {
104 step = s->max_step[plane];
105 hsub = (plane == 1 || plane == 2) ? s->hsub : 0;
106 vsub = (plane == 1 || plane == 2) ? s->vsub : 0;
108 outrow = out->data[plane];
109 inrow = in ->data[plane] + ((inlink->w >> hsub) - 1) * step;
110 for (i = 0; i < in->height >> vsub; i++) {
113 for (j = 0; j < (inlink->w >> hsub); j++)
114 outrow[j] = inrow[-j];
119 uint16_t *outrow16 = (uint16_t *)outrow;
120 uint16_t * inrow16 = (uint16_t *) inrow;
121 for (j = 0; j < (inlink->w >> hsub); j++)
122 outrow16[j] = inrow16[-j];
129 uint8_t *out = outrow;
130 for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
131 int32_t v = AV_RB24(in);
139 uint32_t *outrow32 = (uint32_t *)outrow;
140 uint32_t * inrow32 = (uint32_t *) inrow;
141 for (j = 0; j < (inlink->w >> hsub); j++)
142 outrow32[j] = inrow32[-j];
147 for (j = 0; j < (inlink->w >> hsub); j++)
148 memcpy(outrow + j*step, inrow - j*step, step);
151 inrow += in ->linesize[plane];
152 outrow += out->linesize[plane];
157 return ff_filter_frame(outlink, out);
160 static const AVFilterPad avfilter_vf_hflip_inputs[] = {
163 .type = AVMEDIA_TYPE_VIDEO,
164 .filter_frame = filter_frame,
165 .config_props = config_props,
170 static const AVFilterPad avfilter_vf_hflip_outputs[] = {
173 .type = AVMEDIA_TYPE_VIDEO,
178 AVFilter ff_vf_hflip = {
180 .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
181 .priv_size = sizeof(FlipContext),
182 .query_formats = query_formats,
184 .inputs = avfilter_vf_hflip_inputs,
185 .outputs = avfilter_vf_hflip_outputs,