]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_transpose.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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 <stdio.h>
29
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/internal.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39
40 typedef enum {
41     TRANSPOSE_PT_TYPE_NONE,
42     TRANSPOSE_PT_TYPE_LANDSCAPE,
43     TRANSPOSE_PT_TYPE_PORTRAIT,
44 } PassthroughType;
45
46 typedef struct {
47     const AVClass *class;
48     int hsub, vsub;
49     int pixsteps[4];
50
51     /* 0    Rotate by 90 degrees counterclockwise and vflip. */
52     /* 1    Rotate by 90 degrees clockwise.                  */
53     /* 2    Rotate by 90 degrees counterclockwise.           */
54     /* 3    Rotate by 90 degrees clockwise and vflip.        */
55     int dir;
56     PassthroughType passthrough; ///< landscape passthrough mode enabled
57 } TransContext;
58
59 #define OFFSET(x) offsetof(TransContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61
62 static const AVOption transpose_options[] = {
63     { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, {.i64=0},  0, 7, FLAGS },
64
65     { "passthrough", "do not apply transposition if the input matches the specified geometry",
66       OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE},  0, INT_MAX, FLAGS, "passthrough" },
67     { "none",      "always apply transposition",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE},      INT_MIN, INT_MAX, FLAGS, "passthrough" },
68     { "portrait",  "preserve portrait geometry",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT},  INT_MIN, INT_MAX, FLAGS, "passthrough" },
69     { "landscape", "preserve landscape geometry",  0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
70
71     { NULL },
72 };
73
74 AVFILTER_DEFINE_CLASS(transpose);
75
76 static av_cold int init(AVFilterContext *ctx, const char *args)
77 {
78     TransContext *trans = ctx->priv;
79     const char *shorthand[] = { "dir", "passthrough", NULL };
80
81     trans->class = &transpose_class;
82     av_opt_set_defaults(trans);
83
84     return av_opt_set_from_string(trans, args, shorthand, "=", ":");
85 }
86
87 static int query_formats(AVFilterContext *ctx)
88 {
89     static const enum AVPixelFormat pix_fmts[] = {
90         AV_PIX_FMT_ARGB,         AV_PIX_FMT_RGBA,
91         AV_PIX_FMT_ABGR,         AV_PIX_FMT_BGRA,
92         AV_PIX_FMT_RGB24,        AV_PIX_FMT_BGR24,
93         AV_PIX_FMT_RGB565BE,     AV_PIX_FMT_RGB565LE,
94         AV_PIX_FMT_RGB555BE,     AV_PIX_FMT_RGB555LE,
95         AV_PIX_FMT_BGR565BE,     AV_PIX_FMT_BGR565LE,
96         AV_PIX_FMT_BGR555BE,     AV_PIX_FMT_BGR555LE,
97         AV_PIX_FMT_GRAY16BE,     AV_PIX_FMT_GRAY16LE,
98         AV_PIX_FMT_YUV420P16LE,  AV_PIX_FMT_YUV420P16BE,
99         AV_PIX_FMT_YUV444P16LE,  AV_PIX_FMT_YUV444P16BE,
100         AV_PIX_FMT_NV12,         AV_PIX_FMT_NV21,
101         AV_PIX_FMT_RGB8,         AV_PIX_FMT_BGR8,
102         AV_PIX_FMT_RGB4_BYTE,    AV_PIX_FMT_BGR4_BYTE,
103         AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUVJ444P,
104         AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUVJ420P,
105         AV_PIX_FMT_YUV410P,
106         AV_PIX_FMT_YUVA420P,     AV_PIX_FMT_GRAY8,
107         AV_PIX_FMT_NONE
108     };
109
110     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
111     return 0;
112 }
113
114 static int config_props_output(AVFilterLink *outlink)
115 {
116     AVFilterContext *ctx = outlink->src;
117     TransContext *trans = ctx->priv;
118     AVFilterLink *inlink = ctx->inputs[0];
119     const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
120     const AVPixFmtDescriptor *desc_in  = av_pix_fmt_desc_get(inlink->format);
121
122     if (trans->dir&4) {
123         av_log(ctx, AV_LOG_WARNING,
124                "dir values greater than 3 are deprecated, use the passthrough option instead\n");
125         trans->dir &= 3;
126         trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
127     }
128
129     if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
130         (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
131         av_log(ctx, AV_LOG_VERBOSE,
132                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
133                inlink->w, inlink->h, inlink->w, inlink->h);
134         return 0;
135     } else {
136         trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
137     }
138
139     trans->hsub = desc_in->log2_chroma_w;
140     trans->vsub = desc_in->log2_chroma_h;
141
142     av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
143
144     outlink->w = inlink->h;
145     outlink->h = inlink->w;
146
147     if (inlink->sample_aspect_ratio.num){
148         outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
149     } else
150         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
151
152     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
153            inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
154            trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
155            trans->dir == 0 || trans->dir == 3);
156     return 0;
157 }
158
159 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
160 {
161     TransContext *trans = inlink->dst->priv;
162
163     return trans->passthrough ?
164         ff_null_get_video_buffer   (inlink, perms, w, h) :
165         ff_default_get_video_buffer(inlink, perms, w, h);
166 }
167
168 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
169 {
170     TransContext *trans = inlink->dst->priv;
171     AVFilterLink *outlink = inlink->dst->outputs[0];
172     AVFilterBufferRef *out;
173     int plane;
174
175     if (trans->passthrough)
176         return ff_filter_frame(outlink, in);
177
178     out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
179     if (!out) {
180         avfilter_unref_bufferp(&in);
181         return AVERROR(ENOMEM);
182     }
183
184     out->pts = in->pts;
185
186     if (in->video->sample_aspect_ratio.num == 0) {
187         out->video->sample_aspect_ratio = in->video->sample_aspect_ratio;
188     } else {
189         out->video->sample_aspect_ratio.num = in->video->sample_aspect_ratio.den;
190         out->video->sample_aspect_ratio.den = in->video->sample_aspect_ratio.num;
191     }
192
193     for (plane = 0; out->data[plane]; plane++) {
194         int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
195         int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
196         int pixstep = trans->pixsteps[plane];
197         int inh  = in->video->h>>vsub;
198         int outw = out->video->w>>hsub;
199         int outh = out->video->h>>vsub;
200         uint8_t *dst, *src;
201         int dstlinesize, srclinesize;
202         int x, y;
203
204         dst = out->data[plane];
205         dstlinesize = out->linesize[plane];
206         src = in->data[plane];
207         srclinesize = in->linesize[plane];
208
209         if (trans->dir&1) {
210             src +=  in->linesize[plane] * (inh-1);
211             srclinesize *= -1;
212         }
213
214         if (trans->dir&2) {
215             dst += out->linesize[plane] * (outh-1);
216             dstlinesize *= -1;
217         }
218
219         for (y = 0; y < outh; y++) {
220             switch (pixstep) {
221             case 1:
222                 for (x = 0; x < outw; x++)
223                     dst[x] = src[x*srclinesize + y];
224                 break;
225             case 2:
226                 for (x = 0; x < outw; x++)
227                     *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
228                 break;
229             case 3:
230                 for (x = 0; x < outw; x++) {
231                     int32_t v = AV_RB24(src + x*srclinesize + y*3);
232                     AV_WB24(dst + 3*x, v);
233                 }
234                 break;
235             case 4:
236                 for (x = 0; x < outw; x++)
237                     *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
238                 break;
239             }
240             dst += dstlinesize;
241         }
242     }
243
244     avfilter_unref_bufferp(&in);
245     return ff_filter_frame(outlink, out);
246 }
247
248 static const AVFilterPad avfilter_vf_transpose_inputs[] = {
249     {
250         .name        = "default",
251         .type        = AVMEDIA_TYPE_VIDEO,
252         .get_video_buffer= get_video_buffer,
253         .filter_frame = filter_frame,
254         .min_perms   = AV_PERM_READ,
255     },
256     { NULL }
257 };
258
259 static const AVFilterPad avfilter_vf_transpose_outputs[] = {
260     {
261         .name         = "default",
262         .config_props = config_props_output,
263         .type         = AVMEDIA_TYPE_VIDEO,
264     },
265     { NULL }
266 };
267
268 AVFilter avfilter_vf_transpose = {
269     .name      = "transpose",
270     .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
271
272     .init = init,
273     .priv_size = sizeof(TransContext),
274
275     .query_formats = query_formats,
276
277     .inputs    = avfilter_vf_transpose_inputs,
278     .outputs   = avfilter_vf_transpose_outputs,
279     .priv_class = &transpose_class,
280 };