lavfi: remove 'opaque' parameter from AVFilter.init()
[ffmpeg.git] / libavfilter / vf_transpose.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Vitor Sessak
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * transposition filter
25  * Based on MPlayer libmpcodecs/vf_rotate.c.
26  */
27
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/imgutils.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct {
37     int hsub, vsub;
38     int pixsteps[4];
39
40     /* 0    Rotate by 90 degrees counterclockwise and vflip. */
41     /* 1    Rotate by 90 degrees clockwise.                  */
42     /* 2    Rotate by 90 degrees counterclockwise.           */
43     /* 3    Rotate by 90 degrees clockwise and vflip.        */
44     int dir;
45 } TransContext;
46
47 static av_cold int init(AVFilterContext *ctx, const char *args)
48 {
49     TransContext *trans = ctx->priv;
50     trans->dir = 0;
51
52     if (args)
53         sscanf(args, "%d", &trans->dir);
54
55     if (trans->dir < 0 || trans->dir > 3) {
56         av_log(ctx, AV_LOG_ERROR, "Invalid value %d not between 0 and 3.\n",
57                trans->dir);
58         return AVERROR(EINVAL);
59     }
60     return 0;
61 }
62
63 static int query_formats(AVFilterContext *ctx)
64 {
65     enum PixelFormat pix_fmts[] = {
66         PIX_FMT_ARGB,         PIX_FMT_RGBA,
67         PIX_FMT_ABGR,         PIX_FMT_BGRA,
68         PIX_FMT_RGB24,        PIX_FMT_BGR24,
69         PIX_FMT_RGB565BE,     PIX_FMT_RGB565LE,
70         PIX_FMT_RGB555BE,     PIX_FMT_RGB555LE,
71         PIX_FMT_BGR565BE,     PIX_FMT_BGR565LE,
72         PIX_FMT_BGR555BE,     PIX_FMT_BGR555LE,
73         PIX_FMT_GRAY16BE,     PIX_FMT_GRAY16LE,
74         PIX_FMT_YUV420P16LE,  PIX_FMT_YUV420P16BE,
75         PIX_FMT_YUV422P16LE,  PIX_FMT_YUV422P16BE,
76         PIX_FMT_YUV444P16LE,  PIX_FMT_YUV444P16BE,
77         PIX_FMT_NV12,         PIX_FMT_NV21,
78         PIX_FMT_RGB8,         PIX_FMT_BGR8,
79         PIX_FMT_RGB4_BYTE,    PIX_FMT_BGR4_BYTE,
80         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
81         PIX_FMT_YUV420P,      PIX_FMT_YUVJ420P,
82         PIX_FMT_YUV411P,      PIX_FMT_YUV410P,
83         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
84         PIX_FMT_YUV440P,      PIX_FMT_YUVJ440P,
85         PIX_FMT_YUVA420P,     PIX_FMT_GRAY8,
86         PIX_FMT_NONE
87     };
88
89     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
90     return 0;
91 }
92
93 static int config_props_output(AVFilterLink *outlink)
94 {
95     AVFilterContext *ctx = outlink->src;
96     TransContext *trans = ctx->priv;
97     AVFilterLink *inlink = ctx->inputs[0];
98     const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
99
100     trans->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
101     trans->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
102
103     av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
104
105     outlink->w = inlink->h;
106     outlink->h = inlink->w;
107
108     if (inlink->sample_aspect_ratio.num){
109         outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
110     } else
111         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
112
113     av_log(ctx, AV_LOG_INFO, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
114            inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
115            trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
116            trans->dir == 0 || trans->dir == 3);
117     return 0;
118 }
119
120 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
121 {
122     AVFilterLink *outlink = inlink->dst->outputs[0];
123
124     outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
125                                            outlink->w, outlink->h);
126     outlink->out_buf->pts = picref->pts;
127
128     if (picref->video->pixel_aspect.num == 0) {
129         outlink->out_buf->video->pixel_aspect = picref->video->pixel_aspect;
130     } else {
131         outlink->out_buf->video->pixel_aspect.num = picref->video->pixel_aspect.den;
132         outlink->out_buf->video->pixel_aspect.den = picref->video->pixel_aspect.num;
133     }
134
135     ff_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
136 }
137
138 static void end_frame(AVFilterLink *inlink)
139 {
140     TransContext *trans = inlink->dst->priv;
141     AVFilterBufferRef *inpic  = inlink->cur_buf;
142     AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
143     AVFilterLink *outlink = inlink->dst->outputs[0];
144     int plane;
145
146     for (plane = 0; outpic->data[plane]; plane++) {
147         int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
148         int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
149         int pixstep = trans->pixsteps[plane];
150         int inh  = inpic->video->h>>vsub;
151         int outw = outpic->video->w>>hsub;
152         int outh = outpic->video->h>>vsub;
153         uint8_t *out, *in;
154         int outlinesize, inlinesize;
155         int x, y;
156
157         out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
158         in  = inpic ->data[plane]; inlinesize  = inpic ->linesize[plane];
159
160         if (trans->dir&1) {
161             in +=  inpic->linesize[plane] * (inh-1);
162             inlinesize *= -1;
163         }
164
165         if (trans->dir&2) {
166             out += outpic->linesize[plane] * (outh-1);
167             outlinesize *= -1;
168         }
169
170         for (y = 0; y < outh; y++) {
171             switch (pixstep) {
172             case 1:
173                 for (x = 0; x < outw; x++)
174                     out[x] = in[x*inlinesize + y];
175                 break;
176             case 2:
177                 for (x = 0; x < outw; x++)
178                     *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
179                 break;
180             case 3:
181                 for (x = 0; x < outw; x++) {
182                     int32_t v = AV_RB24(in + x*inlinesize + y*3);
183                     AV_WB24(out + 3*x, v);
184                 }
185                 break;
186             case 4:
187                 for (x = 0; x < outw; x++)
188                     *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
189                 break;
190             }
191             out += outlinesize;
192         }
193     }
194
195     avfilter_unref_buffer(inpic);
196     ff_draw_slice(outlink, 0, outpic->video->h, 1);
197     ff_end_frame(outlink);
198     avfilter_unref_buffer(outpic);
199 }
200
201 AVFilter avfilter_vf_transpose = {
202     .name      = "transpose",
203     .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
204
205     .init = init,
206     .priv_size = sizeof(TransContext),
207
208     .query_formats = query_formats,
209
210     .inputs    = (AVFilterPad[]) {{ .name            = "default",
211                                     .type            = AVMEDIA_TYPE_VIDEO,
212                                     .start_frame     = start_frame,
213                                     .end_frame       = end_frame,
214                                     .min_perms       = AV_PERM_READ, },
215                                   { .name = NULL}},
216     .outputs   = (AVFilterPad[]) {{ .name            = "default",
217                                     .config_props    = config_props_output,
218                                     .type            = AVMEDIA_TYPE_VIDEO, },
219                                   { .name = NULL}},
220 };