lavfi/drawutils: make ff_draw_color() accept a const rgba map
[ffmpeg.git] / 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 "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/imgutils.h"
34
35 typedef 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
40 static int query_formats(AVFilterContext *ctx)
41 {
42     static const enum PixelFormat pix_fmts[] = {
43         PIX_FMT_RGB48BE,      PIX_FMT_RGB48LE,
44         PIX_FMT_BGR48BE,      PIX_FMT_BGR48LE,
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_RGB444BE,     PIX_FMT_RGB444LE,
51         PIX_FMT_BGR565BE,     PIX_FMT_BGR565LE,
52         PIX_FMT_BGR555BE,     PIX_FMT_BGR555LE,
53         PIX_FMT_BGR444BE,     PIX_FMT_BGR444LE,
54         PIX_FMT_GRAY16BE,     PIX_FMT_GRAY16LE,
55         PIX_FMT_YUV420P16LE,  PIX_FMT_YUV420P16BE,
56         PIX_FMT_YUV422P16LE,  PIX_FMT_YUV422P16BE,
57         PIX_FMT_YUV444P16LE,  PIX_FMT_YUV444P16BE,
58         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
59         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
60         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
61         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
62         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
63         PIX_FMT_YUVA420P,
64         PIX_FMT_RGB8,         PIX_FMT_BGR8,
65         PIX_FMT_RGB4_BYTE,    PIX_FMT_BGR4_BYTE,
66         PIX_FMT_PAL8,         PIX_FMT_GRAY8,
67         PIX_FMT_NONE
68     };
69
70     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
71     return 0;
72 }
73
74 static int config_props(AVFilterLink *inlink)
75 {
76     FlipContext *flip = inlink->dst->priv;
77     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
78
79     av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
80     flip->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
81     flip->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
82
83     return 0;
84 }
85
86 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
87 {
88     AVFilterLink *outlink = inlink->dst->outputs[0];
89
90     outlink->out_buf =
91         ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
92     avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
93
94     /* copy palette if required */
95     if (av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL)
96         memcpy(inlink->dst->outputs[0]->out_buf->data[1], picref->data[1], AVPALETTE_SIZE);
97
98     return ff_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
99 }
100
101 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
102 {
103     FlipContext *flip = inlink->dst->priv;
104     AVFilterBufferRef *inpic  = inlink->cur_buf;
105     AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
106     uint8_t *inrow, *outrow;
107     int i, j, plane, step, hsub, vsub;
108
109     for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
110         step = flip->max_step[plane];
111         hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
112         vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
113
114         outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
115         inrow  = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane] + ((inlink->w >> hsub) - 1) * step;
116         for (i = 0; i < h>>vsub; i++) {
117             switch (step) {
118             case 1:
119                 for (j = 0; j < (inlink->w >> hsub); j++)
120                     outrow[j] = inrow[-j];
121             break;
122
123             case 2:
124             {
125                 uint16_t *outrow16 = (uint16_t *)outrow;
126                 uint16_t * inrow16 = (uint16_t *) inrow;
127                 for (j = 0; j < (inlink->w >> hsub); j++)
128                     outrow16[j] = inrow16[-j];
129             }
130             break;
131
132             case 3:
133             {
134                 uint8_t *in  =  inrow;
135                 uint8_t *out = outrow;
136                 for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
137                     int32_t v = AV_RB24(in);
138                     AV_WB24(out, v);
139                 }
140             }
141             break;
142
143             case 4:
144             {
145                 uint32_t *outrow32 = (uint32_t *)outrow;
146                 uint32_t * inrow32 = (uint32_t *) inrow;
147                 for (j = 0; j < (inlink->w >> hsub); j++)
148                     outrow32[j] = inrow32[-j];
149             }
150             break;
151
152             default:
153                 for (j = 0; j < (inlink->w >> hsub); j++)
154                     memcpy(outrow + j*step, inrow - j*step, step);
155             }
156
157             inrow  += inpic ->linesize[plane];
158             outrow += outpic->linesize[plane];
159         }
160     }
161
162     return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
163 }
164
165 AVFilter avfilter_vf_hflip = {
166     .name      = "hflip",
167     .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
168     .priv_size = sizeof(FlipContext),
169     .query_formats = query_formats,
170
171     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
172                                           .type            = AVMEDIA_TYPE_VIDEO,
173                                           .start_frame     = start_frame,
174                                           .draw_slice      = draw_slice,
175                                           .config_props    = config_props,
176                                           .min_perms       = AV_PERM_READ, },
177                                         { .name = NULL}},
178     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
179                                           .type            = AVMEDIA_TYPE_VIDEO, },
180                                         { .name = NULL}},
181 };