Libav
vf_transpose.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Vitor Sessak
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
28 #include <stdio.h>
29 
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39 
45 };
46 
47 typedef struct {
48  const AVClass *class;
49  int hsub, vsub;
50  int pixsteps[4];
51 
52  enum TransposeDir dir;
53 } TransContext;
54 
56 {
57  enum AVPixelFormat pix_fmts[] = {
79  };
80 
82  return 0;
83 }
84 
85 static int config_props_output(AVFilterLink *outlink)
86 {
87  AVFilterContext *ctx = outlink->src;
88  TransContext *trans = ctx->priv;
89  AVFilterLink *inlink = ctx->inputs[0];
90  const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
91  const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
92 
93  trans->hsub = desc_in->log2_chroma_w;
94  trans->vsub = desc_in->log2_chroma_h;
95 
96  av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
97 
98  outlink->w = inlink->h;
99  outlink->h = inlink->w;
100 
101  if (inlink->sample_aspect_ratio.num){
102  outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
103  } else
104  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
105 
106  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
107  inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
108  trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
109  trans->dir == 0 || trans->dir == 3);
110  return 0;
111 }
112 
113 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
114 {
115  AVFilterLink *outlink = inlink->dst->outputs[0];
116  TransContext *trans = inlink->dst->priv;
117  AVFrame *out;
118  int plane;
119 
120  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
121  if (!out) {
122  av_frame_free(&in);
123  return AVERROR(ENOMEM);
124  }
125 
126  out->pts = in->pts;
127 
128  if (in->sample_aspect_ratio.num == 0) {
130  } else {
133  }
134 
135  for (plane = 0; out->data[plane]; plane++) {
136  int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
137  int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
138  int pixstep = trans->pixsteps[plane];
139  int inh = in->height >> vsub;
140  int outw = out->width >> hsub;
141  int outh = out->height >> vsub;
142  uint8_t *dst, *src;
143  int dstlinesize, srclinesize;
144  int x, y;
145 
146  dst = out->data[plane];
147  dstlinesize = out->linesize[plane];
148  src = in->data[plane];
149  srclinesize = in->linesize[plane];
150 
151  if (trans->dir&1) {
152  src += in->linesize[plane] * (inh-1);
153  srclinesize *= -1;
154  }
155 
156  if (trans->dir&2) {
157  dst += out->linesize[plane] * (outh-1);
158  dstlinesize *= -1;
159  }
160 
161  for (y = 0; y < outh; y++) {
162  switch (pixstep) {
163  case 1:
164  for (x = 0; x < outw; x++)
165  dst[x] = src[x*srclinesize + y];
166  break;
167  case 2:
168  for (x = 0; x < outw; x++)
169  *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
170  break;
171  case 3:
172  for (x = 0; x < outw; x++) {
173  int32_t v = AV_RB24(src + x*srclinesize + y*3);
174  AV_WB24(dst + 3*x, v);
175  }
176  break;
177  case 4:
178  for (x = 0; x < outw; x++)
179  *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
180  break;
181  }
182  dst += dstlinesize;
183  }
184  }
185 
186  av_frame_free(&in);
187  return ff_filter_frame(outlink, out);
188 }
189 
190 #define OFFSET(x) offsetof(TransContext, x)
191 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
192 static const AVOption options[] = {
193  { "dir", "Transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP },
195  { "cclock_flip", "counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
196  { "clock", "clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
197  { "cclock", "counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
198  { "clock_flip", "clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
199  { NULL },
200 };
201 
202 static const AVClass transpose_class = {
203  .class_name = "transpose",
204  .item_name = av_default_item_name,
205  .option = options,
206  .version = LIBAVUTIL_VERSION_INT,
207 };
208 
210  {
211  .name = "default",
212  .type = AVMEDIA_TYPE_VIDEO,
213  .filter_frame = filter_frame,
214  },
215  { NULL }
216 };
217 
219  {
220  .name = "default",
221  .config_props = config_props_output,
222  .type = AVMEDIA_TYPE_VIDEO,
223  },
224  { NULL }
225 };
226 
228  .name = "transpose",
229  .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
230 
231  .priv_size = sizeof(TransContext),
232  .priv_class = &transpose_class,
233 
235 
236  .inputs = avfilter_vf_transpose_inputs,
237  .outputs = avfilter_vf_transpose_outputs,
238 };