]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_transpose.c
lavfi/transpose: support more pix fmts
[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     AVFilterFormats *pix_fmts = NULL;
90     int fmt;
91
92     for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
93         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
94         if (!(desc->flags & PIX_FMT_PAL ||
95               desc->flags & PIX_FMT_HWACCEL ||
96               desc->flags & PIX_FMT_BITSTREAM ||
97               desc->log2_chroma_w != desc->log2_chroma_h))
98             ff_add_format(&pix_fmts, fmt);
99     }
100
101
102     ff_set_common_formats(ctx, pix_fmts);
103     return 0;
104 }
105
106 static int config_props_output(AVFilterLink *outlink)
107 {
108     AVFilterContext *ctx = outlink->src;
109     TransContext *trans = ctx->priv;
110     AVFilterLink *inlink = ctx->inputs[0];
111     const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
112     const AVPixFmtDescriptor *desc_in  = av_pix_fmt_desc_get(inlink->format);
113
114     if (trans->dir&4) {
115         av_log(ctx, AV_LOG_WARNING,
116                "dir values greater than 3 are deprecated, use the passthrough option instead\n");
117         trans->dir &= 3;
118         trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
119     }
120
121     if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
122         (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
123         av_log(ctx, AV_LOG_VERBOSE,
124                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
125                inlink->w, inlink->h, inlink->w, inlink->h);
126         return 0;
127     } else {
128         trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
129     }
130
131     trans->hsub = desc_in->log2_chroma_w;
132     trans->vsub = desc_in->log2_chroma_h;
133
134     av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
135
136     outlink->w = inlink->h;
137     outlink->h = inlink->w;
138
139     if (inlink->sample_aspect_ratio.num){
140         outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
141     } else
142         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
143
144     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
145            inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
146            trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
147            trans->dir == 0 || trans->dir == 3);
148     return 0;
149 }
150
151 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
152 {
153     TransContext *trans = inlink->dst->priv;
154
155     return trans->passthrough ?
156         ff_null_get_video_buffer   (inlink, perms, w, h) :
157         ff_default_get_video_buffer(inlink, perms, w, h);
158 }
159
160 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
161 {
162     TransContext *trans = inlink->dst->priv;
163     AVFilterLink *outlink = inlink->dst->outputs[0];
164     AVFilterBufferRef *out;
165     int plane;
166
167     if (trans->passthrough)
168         return ff_filter_frame(outlink, in);
169
170     out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
171     if (!out) {
172         avfilter_unref_bufferp(&in);
173         return AVERROR(ENOMEM);
174     }
175
176     out->pts = in->pts;
177
178     if (in->video->sample_aspect_ratio.num == 0) {
179         out->video->sample_aspect_ratio = in->video->sample_aspect_ratio;
180     } else {
181         out->video->sample_aspect_ratio.num = in->video->sample_aspect_ratio.den;
182         out->video->sample_aspect_ratio.den = in->video->sample_aspect_ratio.num;
183     }
184
185     for (plane = 0; out->data[plane]; plane++) {
186         int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
187         int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
188         int pixstep = trans->pixsteps[plane];
189         int inh  = in->video->h>>vsub;
190         int outw = out->video->w>>hsub;
191         int outh = out->video->h>>vsub;
192         uint8_t *dst, *src;
193         int dstlinesize, srclinesize;
194         int x, y;
195
196         dst = out->data[plane];
197         dstlinesize = out->linesize[plane];
198         src = in->data[plane];
199         srclinesize = in->linesize[plane];
200
201         if (trans->dir&1) {
202             src +=  in->linesize[plane] * (inh-1);
203             srclinesize *= -1;
204         }
205
206         if (trans->dir&2) {
207             dst += out->linesize[plane] * (outh-1);
208             dstlinesize *= -1;
209         }
210
211         for (y = 0; y < outh; y++) {
212             switch (pixstep) {
213             case 1:
214                 for (x = 0; x < outw; x++)
215                     dst[x] = src[x*srclinesize + y];
216                 break;
217             case 2:
218                 for (x = 0; x < outw; x++)
219                     *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
220                 break;
221             case 3:
222                 for (x = 0; x < outw; x++) {
223                     int32_t v = AV_RB24(src + x*srclinesize + y*3);
224                     AV_WB24(dst + 3*x, v);
225                 }
226                 break;
227             case 4:
228                 for (x = 0; x < outw; x++)
229                     *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
230                 break;
231             case 6:
232                 for (x = 0; x < outw; x++) {
233                     int64_t v = AV_RB48(src + x*srclinesize + y*6);
234                     AV_WB48(dst + 6*x, v);
235                 }
236                 break;
237             case 8:
238                 for (x = 0; x < outw; x++)
239                     *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*srclinesize + y*8));
240                 break;
241             }
242             dst += dstlinesize;
243         }
244     }
245
246     avfilter_unref_bufferp(&in);
247     return ff_filter_frame(outlink, out);
248 }
249
250 static const AVFilterPad avfilter_vf_transpose_inputs[] = {
251     {
252         .name        = "default",
253         .type        = AVMEDIA_TYPE_VIDEO,
254         .get_video_buffer= get_video_buffer,
255         .filter_frame = filter_frame,
256         .min_perms   = AV_PERM_READ,
257     },
258     { NULL }
259 };
260
261 static const AVFilterPad avfilter_vf_transpose_outputs[] = {
262     {
263         .name         = "default",
264         .config_props = config_props_output,
265         .type         = AVMEDIA_TYPE_VIDEO,
266     },
267     { NULL }
268 };
269
270 AVFilter avfilter_vf_transpose = {
271     .name      = "transpose",
272     .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
273
274     .init = init,
275     .priv_size = sizeof(TransContext),
276
277     .query_formats = query_formats,
278
279     .inputs    = avfilter_vf_transpose_inputs,
280     .outputs   = avfilter_vf_transpose_outputs,
281     .priv_class = &transpose_class,
282 };