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