2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Vitor Sessak
5 * This file is part of FFmpeg.
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.
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.
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
24 * transposition filter
25 * Based on MPlayer libmpcodecs/vf_rotate.c.
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"
41 TRANSPOSE_PT_TYPE_NONE,
42 TRANSPOSE_PT_TYPE_LANDSCAPE,
43 TRANSPOSE_PT_TYPE_PORTRAIT,
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. */
56 PassthroughType passthrough; ///< landscape passthrough mode enabled
59 #define OFFSET(x) offsetof(TransContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62 static const AVOption transpose_options[] = {
63 { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, {.i64=0}, 0, 7, FLAGS },
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" },
74 AVFILTER_DEFINE_CLASS(transpose);
76 static int query_formats(AVFilterContext *ctx)
78 AVFilterFormats *pix_fmts = NULL;
81 for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
82 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
83 if (!(desc->flags & PIX_FMT_PAL ||
84 desc->flags & PIX_FMT_HWACCEL ||
85 desc->flags & PIX_FMT_BITSTREAM ||
86 desc->log2_chroma_w != desc->log2_chroma_h))
87 ff_add_format(&pix_fmts, fmt);
91 ff_set_common_formats(ctx, pix_fmts);
95 static int config_props_output(AVFilterLink *outlink)
97 AVFilterContext *ctx = outlink->src;
98 TransContext *trans = ctx->priv;
99 AVFilterLink *inlink = ctx->inputs[0];
100 const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
101 const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
104 av_log(ctx, AV_LOG_WARNING,
105 "dir values greater than 3 are deprecated, use the passthrough option instead\n");
107 trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
110 if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
111 (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
112 av_log(ctx, AV_LOG_VERBOSE,
113 "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
114 inlink->w, inlink->h, inlink->w, inlink->h);
117 trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
120 trans->hsub = desc_in->log2_chroma_w;
121 trans->vsub = desc_in->log2_chroma_h;
123 av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
125 outlink->w = inlink->h;
126 outlink->h = inlink->w;
128 if (inlink->sample_aspect_ratio.num){
129 outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
131 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
133 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
134 inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
135 trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
136 trans->dir == 0 || trans->dir == 3);
140 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
142 TransContext *trans = inlink->dst->priv;
144 return trans->passthrough ?
145 ff_null_get_video_buffer (inlink, w, h) :
146 ff_default_get_video_buffer(inlink, w, h);
149 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
151 TransContext *trans = inlink->dst->priv;
152 AVFilterLink *outlink = inlink->dst->outputs[0];
156 if (trans->passthrough)
157 return ff_filter_frame(outlink, in);
159 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
162 return AVERROR(ENOMEM);
167 if (in->sample_aspect_ratio.num == 0) {
168 out->sample_aspect_ratio = in->sample_aspect_ratio;
170 out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
171 out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
174 for (plane = 0; out->data[plane]; plane++) {
175 int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
176 int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
177 int pixstep = trans->pixsteps[plane];
178 int inh = in->height >> vsub;
179 int outw = out->width >> hsub;
180 int outh = out->height >> vsub;
182 int dstlinesize, srclinesize;
185 dst = out->data[plane];
186 dstlinesize = out->linesize[plane];
187 src = in->data[plane];
188 srclinesize = in->linesize[plane];
191 src += in->linesize[plane] * (inh-1);
196 dst += out->linesize[plane] * (outh-1);
200 for (y = 0; y < outh; y++) {
203 for (x = 0; x < outw; x++)
204 dst[x] = src[x*srclinesize + y];
207 for (x = 0; x < outw; x++)
208 *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
211 for (x = 0; x < outw; x++) {
212 int32_t v = AV_RB24(src + x*srclinesize + y*3);
213 AV_WB24(dst + 3*x, v);
217 for (x = 0; x < outw; x++)
218 *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
221 for (x = 0; x < outw; x++) {
222 int64_t v = AV_RB48(src + x*srclinesize + y*6);
223 AV_WB48(dst + 6*x, v);
227 for (x = 0; x < outw; x++)
228 *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*srclinesize + y*8));
236 return ff_filter_frame(outlink, out);
239 static const AVFilterPad avfilter_vf_transpose_inputs[] = {
242 .type = AVMEDIA_TYPE_VIDEO,
243 .get_video_buffer= get_video_buffer,
244 .filter_frame = filter_frame,
249 static const AVFilterPad avfilter_vf_transpose_outputs[] = {
252 .config_props = config_props_output,
253 .type = AVMEDIA_TYPE_VIDEO,
258 static const char *const shorthand[] = { "dir", "passthrough", NULL };
260 AVFilter avfilter_vf_transpose = {
262 .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
264 .priv_size = sizeof(TransContext),
266 .query_formats = query_formats,
268 .inputs = avfilter_vf_transpose_inputs,
269 .outputs = avfilter_vf_transpose_outputs,
270 .priv_class = &transpose_class,
271 .shorthand = shorthand,