2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Vitor Sessak
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/pixdesc.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
43 /* 0 Rotate by 90 degrees counterclockwise and vflip. */
44 /* 1 Rotate by 90 degrees clockwise. */
45 /* 2 Rotate by 90 degrees counterclockwise. */
46 /* 3 Rotate by 90 degrees clockwise and vflip. */
50 static av_cold int init(AVFilterContext *ctx, const char *args)
52 TransContext *trans = ctx->priv;
56 sscanf(args, "%d", &trans->dir);
58 if (trans->dir < 0 || trans->dir > 3) {
59 av_log(ctx, AV_LOG_ERROR, "Invalid value %d not between 0 and 3.\n",
61 return AVERROR(EINVAL);
66 static int query_formats(AVFilterContext *ctx)
68 enum AVPixelFormat pix_fmts[] = {
69 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
70 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
71 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
72 AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
73 AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
74 AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
75 AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
76 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
77 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
78 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
79 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
80 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
81 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
82 AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
83 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
84 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
85 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
86 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
87 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
88 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
92 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
96 static int config_props_output(AVFilterLink *outlink)
98 AVFilterContext *ctx = outlink->src;
99 TransContext *trans = ctx->priv;
100 AVFilterLink *inlink = ctx->inputs[0];
101 const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
102 const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
104 trans->hsub = desc_in->log2_chroma_w;
105 trans->vsub = desc_in->log2_chroma_h;
107 av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
109 outlink->w = inlink->h;
110 outlink->h = inlink->w;
112 if (inlink->sample_aspect_ratio.num){
113 outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
115 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
117 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
118 inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
119 trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
120 trans->dir == 0 || trans->dir == 3);
124 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
126 AVFilterLink *outlink = inlink->dst->outputs[0];
127 AVFilterBufferRef *buf_out;
129 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
130 outlink->w, outlink->h);
131 if (!outlink->out_buf)
132 return AVERROR(ENOMEM);
134 outlink->out_buf->pts = picref->pts;
136 if (picref->video->pixel_aspect.num == 0) {
137 outlink->out_buf->video->pixel_aspect = picref->video->pixel_aspect;
139 outlink->out_buf->video->pixel_aspect.num = picref->video->pixel_aspect.den;
140 outlink->out_buf->video->pixel_aspect.den = picref->video->pixel_aspect.num;
143 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
145 return AVERROR(ENOMEM);
146 return ff_start_frame(outlink, buf_out);
149 static int end_frame(AVFilterLink *inlink)
151 TransContext *trans = inlink->dst->priv;
152 AVFilterBufferRef *inpic = inlink->cur_buf;
153 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
154 AVFilterLink *outlink = inlink->dst->outputs[0];
157 for (plane = 0; outpic->data[plane]; plane++) {
158 int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
159 int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
160 int pixstep = trans->pixsteps[plane];
161 int inh = inpic->video->h>>vsub;
162 int outw = outpic->video->w>>hsub;
163 int outh = outpic->video->h>>vsub;
165 int outlinesize, inlinesize;
168 out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
169 in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
172 in += inpic->linesize[plane] * (inh-1);
177 out += outpic->linesize[plane] * (outh-1);
181 for (y = 0; y < outh; y++) {
184 for (x = 0; x < outw; x++)
185 out[x] = in[x*inlinesize + y];
188 for (x = 0; x < outw; x++)
189 *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
192 for (x = 0; x < outw; x++) {
193 int32_t v = AV_RB24(in + x*inlinesize + y*3);
194 AV_WB24(out + 3*x, v);
198 for (x = 0; x < outw; x++)
199 *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
206 if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
207 (ret = ff_end_frame(outlink)) < 0)
212 static const AVFilterPad avfilter_vf_transpose_inputs[] = {
215 .type = AVMEDIA_TYPE_VIDEO,
216 .start_frame = start_frame,
217 .end_frame = end_frame,
218 .min_perms = AV_PERM_READ,
223 static const AVFilterPad avfilter_vf_transpose_outputs[] = {
226 .config_props = config_props_output,
227 .type = AVMEDIA_TYPE_VIDEO,
232 AVFilter avfilter_vf_transpose = {
234 .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
237 .priv_size = sizeof(TransContext),
239 .query_formats = query_formats,
241 .inputs = avfilter_vf_transpose_inputs,
242 .outputs = avfilter_vf_transpose_outputs,