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 "libavutil/intreadwrite.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/imgutils.h"
31 #include "avfilter.h"
32 
33 typedef struct {
34  int hsub, vsub;
35  int pixsteps[4];
36 
37  /* 0 Rotate by 90 degrees counterclockwise and vflip. */
38  /* 1 Rotate by 90 degrees clockwise. */
39  /* 2 Rotate by 90 degrees counterclockwise. */
40  /* 3 Rotate by 90 degrees clockwise and vflip. */
41  int dir;
42 } TransContext;
43 
44 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
45 {
46  TransContext *trans = ctx->priv;
47  trans->dir = 0;
48 
49  if (args)
50  sscanf(args, "%d", &trans->dir);
51 
52  if (trans->dir < 0 || trans->dir > 3) {
53  av_log(ctx, AV_LOG_ERROR, "Invalid value %d not between 0 and 3.\n",
54  trans->dir);
55  return AVERROR(EINVAL);
56  }
57  return 0;
58 }
59 
61 {
62  enum PixelFormat pix_fmts[] = {
84  };
85 
87  return 0;
88 }
89 
90 static int config_props_output(AVFilterLink *outlink)
91 {
92  AVFilterContext *ctx = outlink->src;
93  TransContext *trans = ctx->priv;
94  AVFilterLink *inlink = ctx->inputs[0];
95  const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
96 
99 
100  av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
101 
102  outlink->w = inlink->h;
103  outlink->h = inlink->w;
104 
105  if (inlink->sample_aspect_ratio.num){
106  outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
107  } else
108  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
109 
110  av_log(ctx, AV_LOG_INFO, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
111  inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
112  trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
113  trans->dir == 0 || trans->dir == 3);
114  return 0;
115 }
116 
117 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
118 {
119  AVFilterLink *outlink = inlink->dst->outputs[0];
120 
121  outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
122  outlink->w, outlink->h);
123  outlink->out_buf->pts = picref->pts;
124 
125  if (picref->video->pixel_aspect.num == 0) {
126  outlink->out_buf->video->pixel_aspect = picref->video->pixel_aspect;
127  } else {
128  outlink->out_buf->video->pixel_aspect.num = picref->video->pixel_aspect.den;
129  outlink->out_buf->video->pixel_aspect.den = picref->video->pixel_aspect.num;
130  }
131 
132  avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
133 }
134 
135 static void end_frame(AVFilterLink *inlink)
136 {
137  TransContext *trans = inlink->dst->priv;
138  AVFilterBufferRef *inpic = inlink->cur_buf;
139  AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
140  AVFilterLink *outlink = inlink->dst->outputs[0];
141  int plane;
142 
143  for (plane = 0; outpic->data[plane]; plane++) {
144  int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
145  int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
146  int pixstep = trans->pixsteps[plane];
147  int inh = inpic->video->h>>vsub;
148  int outw = outpic->video->w>>hsub;
149  int outh = outpic->video->h>>vsub;
150  uint8_t *out, *in;
151  int outlinesize, inlinesize;
152  int x, y;
153 
154  out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
155  in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
156 
157  if (trans->dir&1) {
158  in += inpic->linesize[plane] * (inh-1);
159  inlinesize *= -1;
160  }
161 
162  if (trans->dir&2) {
163  out += outpic->linesize[plane] * (outh-1);
164  outlinesize *= -1;
165  }
166 
167  for (y = 0; y < outh; y++) {
168  switch (pixstep) {
169  case 1:
170  for (x = 0; x < outw; x++)
171  out[x] = in[x*inlinesize + y];
172  break;
173  case 2:
174  for (x = 0; x < outw; x++)
175  *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
176  break;
177  case 3:
178  for (x = 0; x < outw; x++) {
179  int32_t v = AV_RB24(in + x*inlinesize + y*3);
180  AV_WB24(out + 3*x, v);
181  }
182  break;
183  case 4:
184  for (x = 0; x < outw; x++)
185  *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
186  break;
187  }
188  out += outlinesize;
189  }
190  }
191 
192  avfilter_unref_buffer(inpic);
193  avfilter_draw_slice(outlink, 0, outpic->video->h, 1);
194  avfilter_end_frame(outlink);
195  avfilter_unref_buffer(outpic);
196 }
197 
199  .name = "transpose",
200  .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
201 
202  .init = init,
203  .priv_size = sizeof(TransContext),
204 
206 
207  .inputs = (AVFilterPad[]) {{ .name = "default",
208  .type = AVMEDIA_TYPE_VIDEO,
209  .start_frame = start_frame,
210  .end_frame = end_frame,
211  .min_perms = AV_PERM_READ, },
212  { .name = NULL}},
213  .outputs = (AVFilterPad[]) {{ .name = "default",
214  .config_props = config_props_output,
215  .type = AVMEDIA_TYPE_VIDEO, },
216  { .name = NULL}},
217 };
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:68
AVFilterBufferRef * avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: avfilter.c:289
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:141
int linesize[8]
number of bytes per line
Definition: avfilter.h:127
misc image utilities
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
int num
numerator
Definition: rational.h:44
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:77
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:125
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:71
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
Definition: vf_transpose.c:117
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
#define v(n)
Definition: regs.h:34
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:29
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:81
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), big-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:78
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:547
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:124
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:86
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), little-endian, most significant bit to 1 ...
Definition: pixfmt.h:118
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:70
void avfilter_end_frame(AVFilterLink *link)
Notifie the next filter that the current frame has finished.
Definition: avfilter.c:430
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:76
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:110
AVFilter avfilter_vf_transpose
Definition: vf_transpose.c:198
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:109
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian
Definition: pixfmt.h:116
int64_t pts
presentation timestamp.
Definition: avfilter.h:135
A filter pad used for either input or output.
Definition: avfilter.h:312
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:111
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: defaults.c:234
int h
image height
Definition: avfilter.h:108
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
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:553
packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian
Definition: pixfmt.h:115
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVFilterFormats * avfilter_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:90
const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB]
The array of all the pixel format descriptors.
Definition: pixdesc.c:119
int pixsteps[4]
Definition: vf_transpose.c:35
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:69
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:85
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:88
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:126
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:100
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
static int query_formats(AVFilterContext *ctx)
Definition: vf_transpose.c:60
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
Definition: avfilter.c:447
as above, but U and V bytes are swapped
Definition: pixfmt.h:90
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:128
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: avfilter.c:73
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
static void end_frame(AVFilterLink *inlink)
Definition: vf_transpose.c:135
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:112
Filter definition.
Definition: avfilter.h:497
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: avfilter.c:47
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:113
packed BGR 5:5:5, 16bpp, (msb)1A 5B 5G 5R(lsb), big-endian, most significant bit to 1 ...
Definition: pixfmt.h:117
rational number numerator/denominator
Definition: rational.h:43
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:83
const char * name
filter name
Definition: avfilter.h:498
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:67
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_RB24
Definition: bytestream.h:89
PixelFormat
Pixel format.
Definition: pixfmt.h:62
int den
denominator
Definition: rational.h:45
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:82
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:66
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:126
An instance of a filter.
Definition: avfilter.h:538
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
#define AV_LOG_INFO
Definition: log.h:119
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
Y , 8bpp.
Definition: pixfmt.h:72
static int config_props_output(AVFilterLink *outlink)
Definition: vf_transpose.c:90
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
Definition: vf_transpose.c:44
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:129
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101