]> 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     enum PixelFormat pix_fmts[] = {
90         PIX_FMT_ARGB,         PIX_FMT_RGBA,
91         PIX_FMT_ABGR,         PIX_FMT_BGRA,
92         PIX_FMT_RGB24,        PIX_FMT_BGR24,
93         PIX_FMT_RGB565BE,     PIX_FMT_RGB565LE,
94         PIX_FMT_RGB555BE,     PIX_FMT_RGB555LE,
95         PIX_FMT_BGR565BE,     PIX_FMT_BGR565LE,
96         PIX_FMT_BGR555BE,     PIX_FMT_BGR555LE,
97         PIX_FMT_GRAY16BE,     PIX_FMT_GRAY16LE,
98         PIX_FMT_YUV420P16LE,  PIX_FMT_YUV420P16BE,
99         PIX_FMT_YUV444P16LE,  PIX_FMT_YUV444P16BE,
100         PIX_FMT_NV12,         PIX_FMT_NV21,
101         PIX_FMT_RGB8,         PIX_FMT_BGR8,
102         PIX_FMT_RGB4_BYTE,    PIX_FMT_BGR4_BYTE,
103         PIX_FMT_YUV444P,      PIX_FMT_YUVJ444P,
104         PIX_FMT_YUV420P,      PIX_FMT_YUVJ420P,
105         PIX_FMT_YUV410P,
106         PIX_FMT_YUVA420P,     PIX_FMT_GRAY8,
107         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 *pixdesc = &av_pix_fmt_descriptors[outlink->format];
120
121     if (trans->dir&4) {
122         av_log(ctx, AV_LOG_WARNING,
123                "dir values greater than 3 are deprecated, use the passthrough option instead\n");
124         trans->dir &= 3;
125         trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
126     }
127
128     if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
129         (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
130         av_log(ctx, AV_LOG_VERBOSE,
131                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
132                inlink->w, inlink->h, inlink->w, inlink->h);
133         return 0;
134     } else {
135         trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
136     }
137
138     trans->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
139     trans->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
140
141     av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
142
143     outlink->w = inlink->h;
144     outlink->h = inlink->w;
145
146     if (inlink->sample_aspect_ratio.num){
147         outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
148     } else
149         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
150
151     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
152            inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
153            trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
154            trans->dir == 0 || trans->dir == 3);
155     return 0;
156 }
157
158 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
159 {
160     TransContext *trans = inlink->dst->priv;
161
162     return trans->passthrough ?
163         ff_null_get_video_buffer   (inlink, perms, w, h) :
164         ff_default_get_video_buffer(inlink, perms, w, h);
165 }
166
167 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
168 {
169     TransContext *trans = inlink->dst->priv;
170     AVFilterLink *outlink = inlink->dst->outputs[0];
171     AVFilterBufferRef *buf_out;
172
173     if (trans->passthrough)
174         return ff_null_start_frame(inlink, picref);
175
176     outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
177                                            outlink->w, outlink->h);
178     if (!outlink->out_buf)
179         return AVERROR(ENOMEM);
180
181     outlink->out_buf->pts = picref->pts;
182
183     if (picref->video->sample_aspect_ratio.num == 0) {
184         outlink->out_buf->video->sample_aspect_ratio = picref->video->sample_aspect_ratio;
185     } else {
186         outlink->out_buf->video->sample_aspect_ratio.num = picref->video->sample_aspect_ratio.den;
187         outlink->out_buf->video->sample_aspect_ratio.den = picref->video->sample_aspect_ratio.num;
188     }
189
190     buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
191     if (!buf_out)
192         return AVERROR(ENOMEM);
193     return ff_start_frame(outlink, buf_out);
194 }
195
196 static int end_frame(AVFilterLink *inlink)
197 {
198     TransContext *trans = inlink->dst->priv;
199     AVFilterBufferRef *inpic  = inlink->cur_buf;
200     AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
201     AVFilterLink *outlink = inlink->dst->outputs[0];
202     int plane, ret;
203
204     if (trans->passthrough)
205         return ff_null_end_frame(inlink);
206
207     for (plane = 0; outpic->data[plane]; plane++) {
208         int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
209         int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
210         int pixstep = trans->pixsteps[plane];
211         int inh  = inpic->video->h>>vsub;
212         int outw = outpic->video->w>>hsub;
213         int outh = outpic->video->h>>vsub;
214         uint8_t *out, *in;
215         int outlinesize, inlinesize;
216         int x, y;
217
218         out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
219         in  = inpic ->data[plane]; inlinesize  = inpic ->linesize[plane];
220
221         if (trans->dir&1) {
222             in +=  inpic->linesize[plane] * (inh-1);
223             inlinesize *= -1;
224         }
225
226         if (trans->dir&2) {
227             out += outpic->linesize[plane] * (outh-1);
228             outlinesize *= -1;
229         }
230
231         for (y = 0; y < outh; y++) {
232             switch (pixstep) {
233             case 1:
234                 for (x = 0; x < outw; x++)
235                     out[x] = in[x*inlinesize + y];
236                 break;
237             case 2:
238                 for (x = 0; x < outw; x++)
239                     *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
240                 break;
241             case 3:
242                 for (x = 0; x < outw; x++) {
243                     int32_t v = AV_RB24(in + x*inlinesize + y*3);
244                     AV_WB24(out + 3*x, v);
245                 }
246                 break;
247             case 4:
248                 for (x = 0; x < outw; x++)
249                     *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
250                 break;
251             }
252             out += outlinesize;
253         }
254     }
255
256     if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
257         (ret = ff_end_frame(outlink)) < 0)
258         return ret;
259     return 0;
260 }
261
262 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
263 {
264     TransContext *trans = inlink->dst->priv;
265
266     return trans->passthrough ? ff_null_draw_slice(inlink, y, h, slice_dir) : 0;
267 }
268
269 AVFilter avfilter_vf_transpose = {
270     .name      = "transpose",
271     .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
272
273     .init = init,
274     .priv_size = sizeof(TransContext),
275
276     .query_formats = query_formats,
277
278     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
279                                           .type            = AVMEDIA_TYPE_VIDEO,
280                                           .get_video_buffer= get_video_buffer,
281                                           .start_frame     = start_frame,
282                                           .draw_slice      = draw_slice,
283                                           .end_frame       = end_frame,
284                                           .min_perms       = AV_PERM_READ, },
285                                         { .name = NULL}},
286     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
287                                           .config_props    = config_props_output,
288                                           .type            = AVMEDIA_TYPE_VIDEO, },
289                                         { .name = NULL}},
290     .priv_class = &transpose_class,
291 };