FFmpeg
vf_swaprect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Paul B. Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avassert.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26 
27 #include "avfilter.h"
28 #include "filters.h"
29 #include "formats.h"
30 #include "video.h"
31 
32 typedef struct SwapRectContext {
33  const AVClass *class;
34  char *w, *h;
35  char *x1, *y1;
36  char *x2, *y2;
37 
38  int nb_planes;
39  int pixsteps[4];
40 
42  uint8_t *temp;
44 
45 #define OFFSET(x) offsetof(SwapRectContext, x)
46 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
47 static const AVOption swaprect_options[] = {
48  { "w", "set rect width", OFFSET(w), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
49  { "h", "set rect height", OFFSET(h), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
50  { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
51  { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
52  { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
53  { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
54  { NULL },
55 };
56 
57 AVFILTER_DEFINE_CLASS(swaprect);
58 
60 {
61  int reject_flags = AV_PIX_FMT_FLAG_PAL |
64 
65  return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
66 }
67 
68 static const char *const var_names[] = { "w", "h", "a", "n", "t",
69 #if FF_API_FRAME_PKT
70  "pos",
71 #endif
72  "sar", "dar", NULL };
74 #if FF_API_FRAME_PKT
75  VAR_POS,
76 #endif
78 
80 {
82  AVFilterContext *ctx = inlink->dst;
83  AVFilterLink *outlink = ctx->outputs[0];
84  SwapRectContext *s = ctx->priv;
85  double var_values[VAR_VARS_NB];
86  int x1[4], y1[4];
87  int x2[4], y2[4];
88  int aw[4], ah[4];
89  int lw[4], lh[4];
90  int pw[4], ph[4];
91  double dw, dh;
92  double dx1, dy1;
93  double dx2, dy2;
94  int y, p, w, h, ret;
95 
96  var_values[VAR_W] = inlink->w;
97  var_values[VAR_H] = inlink->h;
98  var_values[VAR_A] = (float) inlink->w / inlink->h;
99  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
100  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
101  var_values[VAR_N] = inl->frame_count_out;
102  var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
103 #if FF_API_FRAME_PKT
105  var_values[VAR_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
107 #endif
108 
109  ret = av_expr_parse_and_eval(&dw, s->w,
110  var_names, &var_values[0],
111  NULL, NULL, NULL, NULL,
112  0, 0, ctx);
113  if (ret < 0)
114  return ret;
115 
116  ret = av_expr_parse_and_eval(&dh, s->h,
117  var_names, &var_values[0],
118  NULL, NULL, NULL, NULL,
119  0, 0, ctx);
120  if (ret < 0)
121  return ret;
122 
123  ret = av_expr_parse_and_eval(&dx1, s->x1,
124  var_names, &var_values[0],
125  NULL, NULL, NULL, NULL,
126  0, 0, ctx);
127  if (ret < 0)
128  return ret;
129 
130  ret = av_expr_parse_and_eval(&dy1, s->y1,
131  var_names, &var_values[0],
132  NULL, NULL, NULL, NULL,
133  0, 0, ctx);
134  if (ret < 0)
135  return ret;
136 
137  ret = av_expr_parse_and_eval(&dx2, s->x2,
138  var_names, &var_values[0],
139  NULL, NULL, NULL, NULL,
140  0, 0, ctx);
141  if (ret < 0)
142  return ret;
143 
144  ret = av_expr_parse_and_eval(&dy2, s->y2,
145  var_names, &var_values[0],
146  NULL, NULL, NULL, NULL,
147  0, 0, ctx);
148  if (ret < 0)
149  return ret;
150 
151  w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
152 
153  x1[0] = av_clip(x1[0], 0, inlink->w - 1);
154  y1[0] = av_clip(y1[0], 0, inlink->h - 1);
155 
156  x2[0] = av_clip(x2[0], 0, inlink->w - 1);
157  y2[0] = av_clip(y2[0], 0, inlink->h - 1);
158 
159  ah[1] = ah[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
160  ah[0] = ah[3] = h;
161  aw[1] = aw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
162  aw[0] = aw[3] = w;
163 
164  w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
165  h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
166 
167  ph[1] = ph[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
168  ph[0] = ph[3] = h;
169  pw[1] = pw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
170  pw[0] = pw[3] = w;
171 
172  lh[1] = lh[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
173  lh[0] = lh[3] = inlink->h;
174  lw[1] = lw[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
175  lw[0] = lw[3] = inlink->w;
176 
177  x1[1] = x1[2] = (x1[0] >> s->desc->log2_chroma_w);
178  x1[0] = x1[3] = x1[0];
179  y1[1] = y1[2] = (y1[0] >> s->desc->log2_chroma_h);
180  y1[0] = y1[3] = y1[0];
181 
182  x2[1] = x2[2] = (x2[0] >> s->desc->log2_chroma_w);
183  x2[0] = x2[3] = x2[0];
184  y2[1] = y2[2] = (y2[0] >> s->desc->log2_chroma_h);
185  y2[0] = y2[3] = y2[0];
186 
187 
188  av_assert0(FFMAX(x1[1], x2[1]) + pw[1] <= lw[1]);
189  av_assert0(FFMAX(y1[1], y2[1]) + ph[1] <= lh[1]);
190 
191  for (p = 0; p < s->nb_planes; p++) {
192  if (ph[p] == ah[p] && pw[p] == aw[p]) {
193  uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
194  uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
195 
196  for (y = 0; y < ph[p]; y++) {
197  memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
198  memmove(src, dst, pw[p] * s->pixsteps[p]);
199  memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
200  src += in->linesize[p];
201  dst += in->linesize[p];
202  }
203  }
204  }
205 
206  return ff_filter_frame(outlink, in);
207 }
208 
210 {
211  AVFilterContext *ctx = inlink->dst;
212  SwapRectContext *s = ctx->priv;
213 
214  if (!s->w || !s->h ||
215  !s->x1 || !s->y1 ||
216  !s->x2 || !s->y2)
217  return AVERROR(EINVAL);
218 
219  s->desc = av_pix_fmt_desc_get(inlink->format);
220  av_image_fill_max_pixsteps(s->pixsteps, NULL, s->desc);
221  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
222 
223  s->temp = av_malloc_array(inlink->w, s->pixsteps[0]);
224  if (!s->temp)
225  return AVERROR(ENOMEM);
226 
227  return 0;
228 }
229 
231 {
232  SwapRectContext *s = ctx->priv;
233  av_freep(&s->temp);
234 }
235 
236 static const AVFilterPad inputs[] = {
237  {
238  .name = "default",
239  .type = AVMEDIA_TYPE_VIDEO,
241  .filter_frame = filter_frame,
242  .config_props = config_input,
243  },
244 };
245 
247  .name = "swaprect",
248  .description = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
249  .priv_size = sizeof(SwapRectContext),
250  .priv_class = &swaprect_class,
251  .uninit = uninit,
256  .process_command = ff_filter_process_command,
257 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
av_clip
#define av_clip
Definition: common.h:100
SwapRectContext::y1
char * y1
Definition: vf_swaprect.c:35
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
opt.h
FLAGS
#define FLAGS
Definition: vf_swaprect.c:46
SwapRectContext::y2
char * y2
Definition: vf_swaprect.c:36
SwapRectContext::temp
uint8_t * temp
Definition: vf_swaprect.c:42
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1025
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_swaprect.c:230
SwapRectContext::desc
const AVPixFmtDescriptor * desc
Definition: vf_swaprect.c:41
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
ph
static int FUNC() ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
Definition: cbs_h266_syntax_template.c:3034
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:429
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_swaprect.c:59
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3005
SwapRectContext::pixsteps
int pixsteps[4]
Definition: vf_swaprect.c:39
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
VAR_H
@ VAR_H
Definition: vf_swaprect.c:73
SwapRectContext
Definition: vf_swaprect.c:32
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
avassert.h
ff_vf_swaprect
const AVFilter ff_vf_swaprect
Definition: vf_swaprect.c:246
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int 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:867
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
float
float
Definition: af_crystalizer.c:122
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
filters.h
VAR_N
@ VAR_N
Definition: vf_swaprect.c:73
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SwapRectContext::x2
char * x2
Definition: vf_swaprect.c:36
VAR_T
@ VAR_T
Definition: vf_swaprect.c:73
SwapRectContext::h
char * h
Definition: vf_swaprect.c:34
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
NAN
#define NAN
Definition: mathematics.h:115
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
VAR_POS
@ VAR_POS
Definition: noise.c:56
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
eval.h
AVFILTERPAD_FLAG_NEEDS_WRITABLE
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition: filters.h:57
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
av_expr_parse_and_eval
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:803
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
SwapRectContext::w
char * w
Definition: vf_swaprect.c:34
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV_PIX_FMT_FLAG_BITSTREAM
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:124
AVFrame::pkt_pos
attribute_deprecated int64_t pkt_pos
reordered pos from the last AVPacket that has been input into the decoder
Definition: frame.h:699
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:896
VAR_SAR
@ VAR_SAR
Definition: vf_swaprect.c:77
SwapRectContext::x1
char * x1
Definition: vf_swaprect.c:35
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:182
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:553
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(swaprect)
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
VAR_W
@ VAR_W
Definition: vf_swaprect.c:73
VAR_A
@ VAR_A
Definition: vf_swaprect.c:73
OFFSET
#define OFFSET(x)
Definition: vf_swaprect.c:45
swaprect_options
static const AVOption swaprect_options[]
Definition: vf_swaprect.c:47
avfilter.h
SwapRectContext::nb_planes
int nb_planes
Definition: vf_swaprect.c:38
av_image_fill_max_pixsteps
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:35
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
var_names
static const char *const var_names[]
Definition: vf_swaprect.c:68
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
inputs
static const AVFilterPad inputs[]
Definition: vf_swaprect.c:236
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
h
h
Definition: vp9dsp_template.c:2070
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236
src
#define src
Definition: vp8dsp.c:248
VAR_DAR
@ VAR_DAR
Definition: vf_swaprect.c:77
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_swaprect.c:79
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_swaprect.c:209
VAR_VARS_NB
@ VAR_VARS_NB
Definition: vf_swaprect.c:77