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 "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 typedef struct {
40  int hsub, vsub;
41  int pixsteps[4];
42 
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. */
47  int dir;
48 } TransContext;
49 
50 static av_cold int init(AVFilterContext *ctx, const char *args)
51 {
52  TransContext *trans = ctx->priv;
53  trans->dir = 0;
54 
55  if (args)
56  sscanf(args, "%d", &trans->dir);
57 
58  if (trans->dir < 0 || trans->dir > 3) {
59  av_log(ctx, AV_LOG_ERROR, "Invalid value %d not between 0 and 3.\n",
60  trans->dir);
61  return AVERROR(EINVAL);
62  }
63  return 0;
64 }
65 
67 {
68  enum AVPixelFormat pix_fmts[] = {
90  };
91 
93  return 0;
94 }
95 
96 static int config_props_output(AVFilterLink *outlink)
97 {
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);
103 
104  trans->hsub = desc_in->log2_chroma_w;
105  trans->vsub = desc_in->log2_chroma_h;
106 
107  av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
108 
109  outlink->w = inlink->h;
110  outlink->h = inlink->w;
111 
112  if (inlink->sample_aspect_ratio.num){
113  outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
114  } else
115  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
116 
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);
121  return 0;
122 }
123 
125 {
126  AVFilterLink *outlink = inlink->dst->outputs[0];
127  TransContext *trans = inlink->dst->priv;
128  AVFilterBufferRef *out;
129  int plane;
130 
131  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
132  if (!out) {
134  return AVERROR(ENOMEM);
135  }
136 
137  out->pts = in->pts;
138 
139  if (in->video->pixel_aspect.num == 0) {
140  out->video->pixel_aspect = in->video->pixel_aspect;
141  } else {
144  }
145 
146  for (plane = 0; out->data[plane]; plane++) {
147  int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
148  int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
149  int pixstep = trans->pixsteps[plane];
150  int inh = in->video->h>>vsub;
151  int outw = out->video->w>>hsub;
152  int outh = out->video->h>>vsub;
153  uint8_t *dst, *src;
154  int dstlinesize, srclinesize;
155  int x, y;
156 
157  dst = out->data[plane];
158  dstlinesize = out->linesize[plane];
159  src = in->data[plane];
160  srclinesize = in->linesize[plane];
161 
162  if (trans->dir&1) {
163  src += in->linesize[plane] * (inh-1);
164  srclinesize *= -1;
165  }
166 
167  if (trans->dir&2) {
168  dst += out->linesize[plane] * (outh-1);
169  dstlinesize *= -1;
170  }
171 
172  for (y = 0; y < outh; y++) {
173  switch (pixstep) {
174  case 1:
175  for (x = 0; x < outw; x++)
176  dst[x] = src[x*srclinesize + y];
177  break;
178  case 2:
179  for (x = 0; x < outw; x++)
180  *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
181  break;
182  case 3:
183  for (x = 0; x < outw; x++) {
184  int32_t v = AV_RB24(src + x*srclinesize + y*3);
185  AV_WB24(dst + 3*x, v);
186  }
187  break;
188  case 4:
189  for (x = 0; x < outw; x++)
190  *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
191  break;
192  }
193  dst += dstlinesize;
194  }
195  }
196 
198  return ff_filter_frame(outlink, out);
199 }
200 
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_VIDEO,
205  .filter_frame = filter_frame,
206  .min_perms = AV_PERM_READ,
207  },
208  { NULL }
209 };
210 
212  {
213  .name = "default",
214  .config_props = config_props_output,
215  .type = AVMEDIA_TYPE_VIDEO,
216  },
217  { NULL }
218 };
219 
221  .name = "transpose",
222  .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
223 
224  .init = init,
225  .priv_size = sizeof(TransContext),
226 
228 
229  .inputs = avfilter_vf_transpose_inputs,
230  .outputs = avfilter_vf_transpose_outputs,
231 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
int num
numerator
Definition: rational.h:44
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:114
#define AV_RB24
Definition: intreadwrite.h:64
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:117
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:125
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
static const AVFilterPad avfilter_vf_transpose_outputs[]
Definition: vf_transpose.c:211
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:112
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:86
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:30
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_transpose.c:50
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:111
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:101
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
Definition: vf_transpose.c:124
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
AVFilter avfilter_vf_transpose
Definition: vf_transpose.c:220
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:124
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
A filter pad used for either input or output.
Definition: internal.h:33
AVFilterBufferRef * ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:145
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:130
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
Definition: pixfmt.h:128
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int pixsteps[4]
Definition: vf_transpose.c:41
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
common internal API header
as above, but U and V bytes are swapped
Definition: pixfmt.h:91
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:89
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
int32_t
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
static int query_formats(AVFilterContext *ctx)
Definition: vf_transpose.c:66
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:116
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:84
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:126
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Filter definition.
Definition: avfilter.h:371
Y , 16bpp, big-endian.
Definition: pixfmt.h:98
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:372
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), little-endian, most significant bit to 1 ...
Definition: pixfmt.h:119
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:113
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:129
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:127
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:87
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
int den
denominator
Definition: rational.h:45
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
Y , 16bpp, little-endian.
Definition: pixfmt.h:99
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
static const AVFilterPad avfilter_vf_transpose_inputs[]
Definition: vf_transpose.c:201
An instance of a filter.
Definition: avfilter.h:418
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), big-endian, most significant bit to 1 ...
Definition: pixfmt.h:118
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
internal API functions
static int config_props_output(AVFilterLink *outlink)
Definition: vf_transpose.c:96
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63