FFmpeg  4.4.9
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 <math.h>
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 typedef struct SwapRectContext {
36  const AVClass *class;
37  char *w, *h;
38  char *x1, *y1;
39  char *x2, *y2;
40 
41  int nb_planes;
42  int pixsteps[4];
43 
47 
48 #define OFFSET(x) offsetof(SwapRectContext, x)
49 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
50 static const AVOption swaprect_options[] = {
51  { "w", "set rect width", OFFSET(w), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
52  { "h", "set rect height", OFFSET(h), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
53  { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
54  { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
55  { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
56  { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
57  { NULL },
58 };
59 
61 
63 {
65  int fmt, ret;
66 
67  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
69  if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
70  desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
71  desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
72  (ret = ff_add_format(&pix_fmts, fmt)) < 0)
73  return ret;
74  }
75 
77 }
78 
79 static const char *const var_names[] = { "w", "h", "a", "n", "t", "pos", "sar", "dar", NULL };
81 
82 static int trunc_double_to_int(int *dst, double value)
83 {
84  if (!isfinite(value))
85  return AVERROR(EINVAL);
86 
87  value = trunc(value);
88  if (value < INT_MIN || value > INT_MAX)
89  return AVERROR(EINVAL);
90 
91  *dst = value;
92  return 0;
93 }
94 
95 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
96 {
97  AVFilterContext *ctx = inlink->dst;
98  AVFilterLink *outlink = ctx->outputs[0];
99  SwapRectContext *s = ctx->priv;
100  double var_values[VAR_VARS_NB];
101  int x1[4], y1[4];
102  int x2[4], y2[4];
103  int aw[4], ah[4];
104  int lw[4], lh[4];
105  int pw[4], ph[4];
106  double dw, dh;
107  double dx1, dy1;
108  double dx2, dy2;
109  int y, p, w, h, ret;
110 
111  var_values[VAR_W] = inlink->w;
112  var_values[VAR_H] = inlink->h;
113  var_values[VAR_A] = (float) inlink->w / inlink->h;
114  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
115  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
116  var_values[VAR_N] = inlink->frame_count_out;
117  var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
118  var_values[VAR_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
119 
120  ret = av_expr_parse_and_eval(&dw, s->w,
121  var_names, &var_values[0],
122  NULL, NULL, NULL, NULL,
123  0, 0, ctx);
124  if (ret < 0)
125  goto fail;
126 
127  ret = av_expr_parse_and_eval(&dh, s->h,
128  var_names, &var_values[0],
129  NULL, NULL, NULL, NULL,
130  0, 0, ctx);
131  if (ret < 0)
132  goto fail;
133 
134  ret = av_expr_parse_and_eval(&dx1, s->x1,
135  var_names, &var_values[0],
136  NULL, NULL, NULL, NULL,
137  0, 0, ctx);
138  if (ret < 0)
139  goto fail;
140 
141  ret = av_expr_parse_and_eval(&dy1, s->y1,
142  var_names, &var_values[0],
143  NULL, NULL, NULL, NULL,
144  0, 0, ctx);
145  if (ret < 0)
146  goto fail;
147 
148  ret = av_expr_parse_and_eval(&dx2, s->x2,
149  var_names, &var_values[0],
150  NULL, NULL, NULL, NULL,
151  0, 0, ctx);
152  if (ret < 0)
153  goto fail;
154 
155  ret = av_expr_parse_and_eval(&dy2, s->y2,
156  var_names, &var_values[0],
157  NULL, NULL, NULL, NULL,
158  0, 0, ctx);
159  if (ret < 0)
160  goto fail;
161 
162  if (trunc_double_to_int(&w, dw) < 0 ||
163  trunc_double_to_int(&h, dh) < 0 ||
164  trunc_double_to_int(&x1[0], dx1) < 0 ||
165  trunc_double_to_int(&y1[0], dy1) < 0 ||
166  trunc_double_to_int(&x2[0], dx2) < 0 ||
167  trunc_double_to_int(&y2[0], dy2) < 0) {
169  "Rectangle expression result is not representable as an integer.\n");
170  ret = AVERROR(EINVAL);
171  goto fail;
172  }
173 
174  if (w <= 0 || h <= 0) {
175  av_log(ctx, AV_LOG_ERROR, "Rectangle dimensions must be positive.\n");
176  ret = AVERROR(EINVAL);
177  goto fail;
178  }
179 
180  x1[0] = av_clip(x1[0], 0, inlink->w - 1);
181  y1[0] = av_clip(y1[0], 0, inlink->h - 1);
182 
183  x2[0] = av_clip(x2[0], 0, inlink->w - 1);
184  y2[0] = av_clip(y2[0], 0, inlink->h - 1);
185 
186  ah[1] = ah[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
187  ah[0] = ah[3] = h;
188  aw[1] = aw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
189  aw[0] = aw[3] = w;
190 
191  w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
192  h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
193 
194  ph[1] = ph[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
195  ph[0] = ph[3] = h;
196  pw[1] = pw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
197  pw[0] = pw[3] = w;
198 
199  lh[1] = lh[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
200  lh[0] = lh[3] = inlink->h;
201  lw[1] = lw[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
202  lw[0] = lw[3] = inlink->w;
203 
204  x1[1] = x1[2] = (x1[0] >> s->desc->log2_chroma_w);
205  x1[0] = x1[3] = x1[0];
206  y1[1] = y1[2] = (y1[0] >> s->desc->log2_chroma_h);
207  y1[0] = y1[3] = y1[0];
208 
209  x2[1] = x2[2] = (x2[0] >> s->desc->log2_chroma_w);
210  x2[0] = x2[3] = x2[0];
211  y2[1] = y2[2] = (y2[0] >> s->desc->log2_chroma_h);
212  y2[0] = y2[3] = y2[0];
213 
214 
215  av_assert0(FFMAX(x1[1], x2[1]) + pw[1] <= lw[1]);
216  av_assert0(FFMAX(y1[1], y2[1]) + ph[1] <= lh[1]);
217 
218  for (p = 0; p < s->nb_planes; p++) {
219  if (ph[p] == ah[p] && pw[p] == aw[p]) {
220  uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
221  uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
222 
223  for (y = 0; y < ph[p]; y++) {
224  memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
225  memmove(src, dst, pw[p] * s->pixsteps[p]);
226  memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
227  src += in->linesize[p];
228  dst += in->linesize[p];
229  }
230  }
231  }
232 
233  return ff_filter_frame(outlink, in);
234 
235 fail:
236  av_frame_free(&in);
237  return ret;
238 }
239 
240 static int config_input(AVFilterLink *inlink)
241 {
242  AVFilterContext *ctx = inlink->dst;
243  SwapRectContext *s = ctx->priv;
244  int size = 0;
245 
246  if (!s->w || !s->h ||
247  !s->x1 || !s->y1 ||
248  !s->x2 || !s->y2)
249  return AVERROR(EINVAL);
250 
251  s->desc = av_pix_fmt_desc_get(inlink->format);
252  av_image_fill_max_pixsteps(s->pixsteps, NULL, s->desc);
253  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
254 
255  for (int p = 0; p < s->nb_planes; p++) {
256  int shift = p == 1 || p == 2 ? s->desc->log2_chroma_w : 0;
257  int width = AV_CEIL_RSHIFT(inlink->w, shift);
258 
259  if (width > INT_MAX / s->pixsteps[p])
260  return AVERROR(EINVAL);
261  size = FFMAX(size, width * s->pixsteps[p]);
262  }
263 
264  s->temp = av_malloc(size);
265  if (!s->temp)
266  return AVERROR(ENOMEM);
267 
268  return 0;
269 }
270 
272 {
273  SwapRectContext *s = ctx->priv;
274  av_freep(&s->temp);
275 }
276 
277 static const AVFilterPad inputs[] = {
278  {
279  .name = "default",
280  .type = AVMEDIA_TYPE_VIDEO,
281  .filter_frame = filter_frame,
282  .config_props = config_input,
283  .needs_writable = 1,
284  },
285  { NULL }
286 };
287 
288 static const AVFilterPad outputs[] = {
289  {
290  .name = "default",
291  .type = AVMEDIA_TYPE_VIDEO,
292  },
293  { NULL }
294 };
295 
297  .name = "swaprect",
298  .description = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
299  .priv_size = sizeof(SwapRectContext),
300  .priv_class = &swaprect_class,
302  .uninit = uninit,
303  .inputs = inputs,
304  .outputs = outputs,
307 };
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
#define av_cold
Definition: attributes.h:88
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=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);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_AARCH64) ff_audio_convert_init_aarch64(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_log(ac->avr, AV_LOG_TRACE, "%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-> in
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
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:885
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define av_clip
Definition: common.h:122
#define FFMAX(a, b)
Definition: common.h:103
#define FFMIN3(a, b, c)
Definition: common.h:106
#define NULL
Definition: coverity.c:32
static __device__ float trunc(float a)
Definition: cuda_runtime.h:179
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:791
double value
Definition: eval.c:100
simple arithmetic expression evaluator
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:587
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:332
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#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:126
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
misc image utilities
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define isfinite(x)
Definition: libm.h:359
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
#define NAN
Definition: mathematics.h:64
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
static int shift(int a, int b)
Definition: sonic.c:82
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int num
Numerator.
Definition: rational.h:59
const AVPixFmtDescriptor * desc
Definition: vf_swaprect.c:44
uint8_t * temp
Definition: vf_swaprect.c:45
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVFormatContext * ctx
Definition: movenc.c:48
#define width
int size
AVFILTER_DEFINE_CLASS(swaprect)
AVFilter ff_vf_swaprect
Definition: vf_swaprect.c:296
@ VAR_H
Definition: vf_swaprect.c:80
@ VAR_POS
Definition: vf_swaprect.c:80
@ VAR_SAR
Definition: vf_swaprect.c:80
@ VAR_N
Definition: vf_swaprect.c:80
@ VAR_W
Definition: vf_swaprect.c:80
@ VAR_A
Definition: vf_swaprect.c:80
@ VAR_VARS_NB
Definition: vf_swaprect.c:80
@ VAR_DAR
Definition: vf_swaprect.c:80
@ VAR_T
Definition: vf_swaprect.c:80
static int query_formats(AVFilterContext *ctx)
Definition: vf_swaprect.c:62
static int config_input(AVFilterLink *inlink)
Definition: vf_swaprect.c:240
static int trunc_double_to_int(int *dst, double value)
Definition: vf_swaprect.c:82
#define FLAGS
Definition: vf_swaprect.c:49
static const AVFilterPad inputs[]
Definition: vf_swaprect.c:277
static const AVFilterPad outputs[]
Definition: vf_swaprect.c:288
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_swaprect.c:95
static const char *const var_names[]
Definition: vf_swaprect.c:79
static const AVOption swaprect_options[]
Definition: vf_swaprect.c:50
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_swaprect.c:271
#define OFFSET(x)
Definition: vf_swaprect.c:48