]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hflip.c
lavfi/hflip: support more formats
[ffmpeg] / libavfilter / vf_hflip.c
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  * @file
24  * horizontal flip filter
25  */
26
27 #include <string.h>
28
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/imgutils.h"
37
38 typedef 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
43 static int query_formats(AVFilterContext *ctx)
44 {
45     AVFilterFormats *pix_fmts = NULL;
46     int fmt;
47
48     for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
49         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
50         if (!(desc->flags & PIX_FMT_HWACCEL ||
51               desc->flags & PIX_FMT_BITSTREAM ||
52               (desc->log2_chroma_w != desc->log2_chroma_h &&
53                desc->comp[0].plane == desc->comp[1].plane)))
54             ff_add_format(&pix_fmts, fmt);
55     }
56
57     ff_set_common_formats(ctx, pix_fmts);
58     return 0;
59 }
60
61 static int config_props(AVFilterLink *inlink)
62 {
63     FlipContext *flip = inlink->dst->priv;
64     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
65
66     av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
67     flip->hsub = pix_desc->log2_chroma_w;
68     flip->vsub = pix_desc->log2_chroma_h;
69
70     return 0;
71 }
72
73 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
74 {
75     AVFilterContext *ctx  = inlink->dst;
76     FlipContext *flip     = ctx->priv;
77     AVFilterLink *outlink = ctx->outputs[0];
78     AVFilterBufferRef *out;
79     uint8_t *inrow, *outrow;
80     int i, j, plane, step, hsub, vsub;
81
82     out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
83     if (!out) {
84         avfilter_unref_bufferp(&in);
85         return AVERROR(ENOMEM);
86     }
87     avfilter_copy_buffer_ref_props(out, in);
88
89     /* copy palette if required */
90     if (av_pix_fmt_desc_get(inlink->format)->flags & PIX_FMT_PAL)
91         memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
92
93     for (plane = 0; plane < 4 && in->data[plane]; plane++) {
94         step = flip->max_step[plane];
95         hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
96         vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
97
98         outrow = out->data[plane];
99         inrow  = in ->data[plane] + ((inlink->w >> hsub) - 1) * step;
100         for (i = 0; i < in->video->h >> vsub; i++) {
101             switch (step) {
102             case 1:
103                 for (j = 0; j < (inlink->w >> hsub); j++)
104                     outrow[j] = inrow[-j];
105             break;
106
107             case 2:
108             {
109                 uint16_t *outrow16 = (uint16_t *)outrow;
110                 uint16_t * inrow16 = (uint16_t *) inrow;
111                 for (j = 0; j < (inlink->w >> hsub); j++)
112                     outrow16[j] = inrow16[-j];
113             }
114             break;
115
116             case 3:
117             {
118                 uint8_t *in  =  inrow;
119                 uint8_t *out = outrow;
120                 for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
121                     int32_t v = AV_RB24(in);
122                     AV_WB24(out, v);
123                 }
124             }
125             break;
126
127             case 4:
128             {
129                 uint32_t *outrow32 = (uint32_t *)outrow;
130                 uint32_t * inrow32 = (uint32_t *) inrow;
131                 for (j = 0; j < (inlink->w >> hsub); j++)
132                     outrow32[j] = inrow32[-j];
133             }
134             break;
135
136             default:
137                 for (j = 0; j < (inlink->w >> hsub); j++)
138                     memcpy(outrow + j*step, inrow - j*step, step);
139             }
140
141             inrow  += in ->linesize[plane];
142             outrow += out->linesize[plane];
143         }
144     }
145
146     avfilter_unref_bufferp(&in);
147     return ff_filter_frame(outlink, out);
148 }
149
150 static const AVFilterPad avfilter_vf_hflip_inputs[] = {
151     {
152         .name         = "default",
153         .type         = AVMEDIA_TYPE_VIDEO,
154         .filter_frame = filter_frame,
155         .config_props = config_props,
156         .min_perms    = AV_PERM_READ,
157     },
158     { NULL }
159 };
160
161 static const AVFilterPad avfilter_vf_hflip_outputs[] = {
162     {
163         .name = "default",
164         .type = AVMEDIA_TYPE_VIDEO,
165     },
166     { NULL }
167 };
168
169 AVFilter avfilter_vf_hflip = {
170     .name      = "hflip",
171     .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
172     .priv_size = sizeof(FlipContext),
173     .query_formats = query_formats,
174
175     .inputs    = avfilter_vf_hflip_inputs,
176     .outputs   = avfilter_vf_hflip_outputs,
177 };