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 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 "libavutil/opt.h"
36 #include "avfilter.h"
37 #include "formats.h"
38 #include "internal.h"
39 #include "video.h"
40 
41 typedef enum {
46 
52 };
53 
54 typedef struct {
55  const AVClass *class;
56  int hsub, vsub;
57  int pixsteps[4];
58 
59  PassthroughType passthrough; ///< landscape passthrough mode enabled
60  enum TransposeDir dir;
61 } TransContext;
62 
64 {
65  AVFilterFormats *pix_fmts = NULL;
66  int fmt;
67 
68  for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
69  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
70  if (!(desc->flags & PIX_FMT_PAL ||
71  desc->flags & PIX_FMT_HWACCEL ||
72  desc->flags & PIX_FMT_BITSTREAM ||
73  desc->log2_chroma_w != desc->log2_chroma_h))
74  ff_add_format(&pix_fmts, fmt);
75  }
76 
77 
78  ff_set_common_formats(ctx, pix_fmts);
79  return 0;
80 }
81 
82 static int config_props_output(AVFilterLink *outlink)
83 {
84  AVFilterContext *ctx = outlink->src;
85  TransContext *trans = ctx->priv;
86  AVFilterLink *inlink = ctx->inputs[0];
87  const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
88  const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
89 
90  if (trans->dir&4) {
92  "dir values greater than 3 are deprecated, use the passthrough option instead\n");
93  trans->dir &= 3;
95  }
96 
97  if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
98  (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
100  "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
101  inlink->w, inlink->h, inlink->w, inlink->h);
102  return 0;
103  } else {
105  }
106 
107  trans->hsub = desc_in->log2_chroma_w;
108  trans->vsub = desc_in->log2_chroma_h;
109 
110  av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
111 
112  outlink->w = inlink->h;
113  outlink->h = inlink->w;
114 
115  if (inlink->sample_aspect_ratio.num){
116  outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
117  } else
118  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
119 
120  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
121  inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
122  trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
123  trans->dir == 0 || trans->dir == 3);
124  return 0;
125 }
126 
127 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
128 {
129  TransContext *trans = inlink->dst->priv;
130 
131  return trans->passthrough ?
132  ff_null_get_video_buffer (inlink, w, h) :
133  ff_default_get_video_buffer(inlink, w, h);
134 }
135 
136 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
137 {
138  TransContext *trans = inlink->dst->priv;
139  AVFilterLink *outlink = inlink->dst->outputs[0];
140  AVFrame *out;
141  int plane;
142 
143  if (trans->passthrough)
144  return ff_filter_frame(outlink, in);
145 
146  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
147  if (!out) {
148  av_frame_free(&in);
149  return AVERROR(ENOMEM);
150  }
151 
152  out->pts = in->pts;
153 
154  if (in->sample_aspect_ratio.num == 0) {
156  } else {
159  }
160 
161  for (plane = 0; out->data[plane]; plane++) {
162  int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
163  int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
164  int pixstep = trans->pixsteps[plane];
165  int inh = in->height >> vsub;
166  int outw = out->width >> hsub;
167  int outh = out->height >> vsub;
168  uint8_t *dst, *src;
169  int dstlinesize, srclinesize;
170  int x, y;
171 
172  dst = out->data[plane];
173  dstlinesize = out->linesize[plane];
174  src = in->data[plane];
175  srclinesize = in->linesize[plane];
176 
177  if (trans->dir&1) {
178  src += in->linesize[plane] * (inh-1);
179  srclinesize *= -1;
180  }
181 
182  if (trans->dir&2) {
183  dst += out->linesize[plane] * (outh-1);
184  dstlinesize *= -1;
185  }
186 
187  for (y = 0; y < outh; y++) {
188  switch (pixstep) {
189  case 1:
190  for (x = 0; x < outw; x++)
191  dst[x] = src[x*srclinesize + y];
192  break;
193  case 2:
194  for (x = 0; x < outw; x++)
195  *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
196  break;
197  case 3:
198  for (x = 0; x < outw; x++) {
199  int32_t v = AV_RB24(src + x*srclinesize + y*3);
200  AV_WB24(dst + 3*x, v);
201  }
202  break;
203  case 4:
204  for (x = 0; x < outw; x++)
205  *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
206  break;
207  case 6:
208  for (x = 0; x < outw; x++) {
209  int64_t v = AV_RB48(src + x*srclinesize + y*6);
210  AV_WB48(dst + 6*x, v);
211  }
212  break;
213  case 8:
214  for (x = 0; x < outw; x++)
215  *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*srclinesize + y*8));
216  break;
217  }
218  dst += dstlinesize;
219  }
220  }
221 
222  av_frame_free(&in);
223  return ff_filter_frame(outlink, out);
224 }
225 
226 #define OFFSET(x) offsetof(TransContext, x)
227 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
228 
229 static const AVOption transpose_options[] = {
230  { "dir", "Transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP },
232  { "cclock_flip", "counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
233  { "clock", "clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
234  { "cclock", "counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
235  { "clock_flip", "clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
236 
237  { "passthrough", "do not apply transposition if the input matches the specified geometry",
238  OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
239  { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
240  { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
241  { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
242 
243  { NULL },
244 };
245 
247 
249  {
250  .name = "default",
251  .type = AVMEDIA_TYPE_VIDEO,
252  .get_video_buffer= get_video_buffer,
253  .filter_frame = filter_frame,
254  },
255  { NULL }
256 };
257 
259  {
260  .name = "default",
261  .config_props = config_props_output,
262  .type = AVMEDIA_TYPE_VIDEO,
263  },
264  { NULL }
265 };
266 
268  .name = "transpose",
269  .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
270 
271  .priv_size = sizeof(TransContext),
272  .priv_class = &transpose_class,
273 
275 
276  .inputs = avfilter_vf_transpose_inputs,
277  .outputs = avfilter_vf_transpose_outputs,
278 };
float v
#define AV_RB48(x)
Definition: intreadwrite.h:464
AVFILTER_DEFINE_CLASS(transpose)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
const char * fmt
Definition: avisynth_c.h:669
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
TransposeDir
Definition: vf_transpose.c:47
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
int num
numerator
Definition: rational.h:44
#define AV_RB24
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
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:258
output residual component w
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:32
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
AVOptions.
#define AV_WB48(p, darg)
Definition: intreadwrite.h:473
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
enum TransposeDir dir
Definition: vf_transpose.c:60
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:545
AVFilter avfilter_vf_transpose
Definition: vf_transpose.c:267
A filter pad used for either input or output.
Discrete Time axis x
#define transpose(x)
int width
width and height of the video frame
Definition: frame.h:122
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
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:545
#define PIX_FMT_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:90
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:344
int pixsteps[4]
Definition: vf_transpose.c:57
common internal API header
#define AV_LOG_VERBOSE
Definition: log.h:157
int32_t
#define AV_WB24(p, d)
Definition: intreadwrite.h:442
static int query_formats(AVFilterContext *ctx)
Definition: vf_transpose.c:63
#define PIX_FMT_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:91
#define FLAGS
Definition: vf_transpose.c:227
PassthroughType
Definition: vf_transpose.c:41
static const AVOption transpose_options[]
Definition: vf_transpose.c:229
#define PIX_FMT_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:92
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
static AVFrame * get_video_buffer(AVFilterLink *inlink, int w, int h)
Definition: vf_transpose.c:127
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:154
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:43
PassthroughType passthrough
landscape passthrough mode enabled
Definition: vf_transpose.c:59
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
int den
denominator
Definition: rational.h:45
function y
Definition: D.m:1
#define OFFSET(x)
Definition: vf_transpose.c:226
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
static const AVFilterPad avfilter_vf_transpose_inputs[]
Definition: vf_transpose.c:248
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:237
int height
Definition: frame.h:122
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_transpose.c:136
internal API functions
static int config_props_output(AVFilterLink *outlink)
Definition: vf_transpose.c:82
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs