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