FFmpeg
Loading...
Searching...
No Matches
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/eval.h"
25#include "libavutil/imgutils.h"
26#include "libavutil/mem.h"
27#include "libavutil/opt.h"
28
29#include "avfilter.h"
30#include "filters.h"
31#include "formats.h"
32#include "video.h"
33
34typedef struct SwapRectContext {
35 const AVClass *class;
36 char *w, *h;
37 char *x1, *y1;
38 char *x2, *y2;
39
41 int pixsteps[4];
42
44 uint8_t *temp;
46
47#define OFFSET(x) offsetof(SwapRectContext, x)
48#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
49static const AVOption swaprect_options[] = {
50 { "w", "set rect width", OFFSET(w), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
51 { "h", "set rect height", OFFSET(h), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
52 { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
53 { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
54 { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
55 { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
56 { NULL },
57};
58
60
62 AVFilterFormatsConfig **cfg_in,
63 AVFilterFormatsConfig **cfg_out)
64{
65 int reject_flags = AV_PIX_FMT_FLAG_PAL |
68
69 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
70 ff_formats_pixdesc_filter(0, reject_flags));
71}
72
73static const char *const var_names[] = { "w", "h", "a", "n", "t", "sar", "dar", NULL };
75
76static int trunc_double_to_int(int *dst, double value)
77{
78 if (!isfinite(value))
79 return AVERROR(EINVAL);
80
81 value = trunc(value);
83 return AVERROR(EINVAL);
84
85 *dst = value;
86 return 0;
87}
88
89static int filter_frame(AVFilterLink *inlink, AVFrame *in)
90{
91 FilterLink *inl = ff_filter_link(inlink);
92 AVFilterContext *ctx = inlink->dst;
93 AVFilterLink *outlink = ctx->outputs[0];
94 SwapRectContext *s = ctx->priv;
95 double var_values[VAR_VARS_NB];
96 int x1[4], y1[4];
97 int x2[4], y2[4];
98 int aw[4], ah[4];
99 int lw[4], lh[4];
100 int pw[4], ph[4];
101 double dw, dh;
102 double dx1, dy1;
103 double dx2, dy2;
104 int y, p, w, h, ret;
105
106 var_values[VAR_W] = inlink->w;
107 var_values[VAR_H] = inlink->h;
108 var_values[VAR_A] = (float) inlink->w / inlink->h;
109 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
110 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
111 var_values[VAR_N] = inl->frame_count_out;
112 var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
113
114 ret = av_expr_parse_and_eval(&dw, s->w,
115 var_names, &var_values[0],
116 NULL, NULL, NULL, NULL,
117 0, 0, ctx);
118 if (ret < 0)
119 goto fail;
120
121 ret = av_expr_parse_and_eval(&dh, s->h,
122 var_names, &var_values[0],
123 NULL, NULL, NULL, NULL,
124 0, 0, ctx);
125 if (ret < 0)
126 goto fail;
127
128 ret = av_expr_parse_and_eval(&dx1, s->x1,
129 var_names, &var_values[0],
130 NULL, NULL, NULL, NULL,
131 0, 0, ctx);
132 if (ret < 0)
133 goto fail;
134
135 ret = av_expr_parse_and_eval(&dy1, s->y1,
136 var_names, &var_values[0],
137 NULL, NULL, NULL, NULL,
138 0, 0, ctx);
139 if (ret < 0)
140 goto fail;
141
142 ret = av_expr_parse_and_eval(&dx2, s->x2,
143 var_names, &var_values[0],
144 NULL, NULL, NULL, NULL,
145 0, 0, ctx);
146 if (ret < 0)
147 goto fail;
148
149 ret = av_expr_parse_and_eval(&dy2, s->y2,
150 var_names, &var_values[0],
151 NULL, NULL, NULL, NULL,
152 0, 0, ctx);
153 if (ret < 0)
154 goto fail;
155
156 if (trunc_double_to_int(&w, dw) < 0 ||
157 trunc_double_to_int(&h, dh) < 0 ||
158 trunc_double_to_int(&x1[0], dx1) < 0 ||
159 trunc_double_to_int(&y1[0], dy1) < 0 ||
160 trunc_double_to_int(&x2[0], dx2) < 0 ||
161 trunc_double_to_int(&y2[0], dy2) < 0) {
163 "Rectangle expression result is not representable as an integer.\n");
164 ret = AVERROR(EINVAL);
165 goto fail;
166 }
167
168 if (w <= 0 || h <= 0) {
169 av_log(ctx, AV_LOG_ERROR, "Rectangle dimensions must be positive.\n");
170 ret = AVERROR(EINVAL);
171 goto fail;
172 }
173
174 x1[0] = av_clip(x1[0], 0, inlink->w - 1);
175 y1[0] = av_clip(y1[0], 0, inlink->h - 1);
176
177 x2[0] = av_clip(x2[0], 0, inlink->w - 1);
178 y2[0] = av_clip(y2[0], 0, inlink->h - 1);
179
180 ah[1] = ah[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
181 ah[0] = ah[3] = h;
182 aw[1] = aw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
183 aw[0] = aw[3] = w;
184
185 w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
186 h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
187
188 ph[1] = ph[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
189 ph[0] = ph[3] = h;
190 pw[1] = pw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
191 pw[0] = pw[3] = w;
192
193 lh[1] = lh[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
194 lh[0] = lh[3] = inlink->h;
195 lw[1] = lw[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
196 lw[0] = lw[3] = inlink->w;
197
198 x1[1] = x1[2] = (x1[0] >> s->desc->log2_chroma_w);
199 x1[0] = x1[3] = x1[0];
200 y1[1] = y1[2] = (y1[0] >> s->desc->log2_chroma_h);
201 y1[0] = y1[3] = y1[0];
202
203 x2[1] = x2[2] = (x2[0] >> s->desc->log2_chroma_w);
204 x2[0] = x2[3] = x2[0];
205 y2[1] = y2[2] = (y2[0] >> s->desc->log2_chroma_h);
206 y2[0] = y2[3] = y2[0];
207
208
209 av_assert0(FFMAX(x1[1], x2[1]) + pw[1] <= lw[1]);
210 av_assert0(FFMAX(y1[1], y2[1]) + ph[1] <= lh[1]);
211
212 for (p = 0; p < s->nb_planes; p++) {
213 if (ph[p] == ah[p] && pw[p] == aw[p]) {
214 uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
215 uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
216
217 for (y = 0; y < ph[p]; y++) {
218 memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
219 memmove(src, dst, pw[p] * s->pixsteps[p]);
220 memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
221 src += in->linesize[p];
222 dst += in->linesize[p];
223 }
224 }
225 }
226
227 return ff_filter_frame(outlink, in);
228
229fail:
230 av_frame_free(&in);
231 return ret;
232}
233
234static int config_input(AVFilterLink *inlink)
235{
236 AVFilterContext *ctx = inlink->dst;
237 SwapRectContext *s = ctx->priv;
238 int size = 0;
239
240 if (!s->w || !s->h ||
241 !s->x1 || !s->y1 ||
242 !s->x2 || !s->y2)
243 return AVERROR(EINVAL);
244
245 s->desc = av_pix_fmt_desc_get(inlink->format);
246 av_image_fill_max_pixsteps(s->pixsteps, NULL, s->desc);
247 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
248
249 for (int p = 0; p < s->nb_planes; p++) {
250 int shift = p == 1 || p == 2 ? s->desc->log2_chroma_w : 0;
251 int width = AV_CEIL_RSHIFT(inlink->w, shift);
252
253 if (width > INT_MAX / s->pixsteps[p])
254 return AVERROR(EINVAL);
255 size = FFMAX(size, width * s->pixsteps[p]);
256 }
257
258 s->temp = av_malloc(size);
259 if (!s->temp)
260 return AVERROR(ENOMEM);
261
262 return 0;
263}
264
266{
267 SwapRectContext *s = ctx->priv;
268 av_freep(&s->temp);
269}
270
271static const AVFilterPad inputs[] = {
272 {
273 .name = "default",
274 .type = AVMEDIA_TYPE_VIDEO,
276 .filter_frame = filter_frame,
277 .config_props = config_input,
278 },
279};
280
282 .p.name = "swaprect",
283 .p.description = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
284 .p.priv_class = &swaprect_class,
286 .priv_size = sizeof(SwapRectContext),
287 .uninit = uninit,
291 .process_command = ff_filter_process_command,
292};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
@ VAR_T
Definition aeval.c:53
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_swaprect
static AVFormatContext * ctx
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:42
@ VAR_H
Definition avfilter.c:607
@ VAR_W
Definition avfilter.c:606
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1112
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:950
Main libavfilter public API header.
static int FUNC ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
static __device__ float trunc(float a)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
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:839
double value
Definition eval.c:102
simple arithmetic expression evaluator
int ret
Definition ffplay.c:391
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1200
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition formats.c:629
#define fail
Definition test.h:479
@ 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:275
#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:202
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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:247
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int shift(int a, int b)
Definition bonk.c:261
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition filters.h:59
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:256
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
uint8_t w
Definition llvidencdsp.c:39
#define FFMAX(a, b)
Definition macros.h:47
#define FFMIN3(a, b, c)
Definition macros.h:50
#define NAN
Memory handling functions.
@ VAR_N
Definition noise.c:47
@ VAR_VARS_NB
Definition noise.c:59
static const char *const var_names[]
Definition noise.c:30
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3524
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition pixdesc.h:124
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
@ VAR_SAR
Definition scale_eval.c:49
@ VAR_A
Definition scale_eval.c:48
@ VAR_DAR
Definition scale_eval.c:50
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:279
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:581
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
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:524
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int num
Numerator.
Definition rational.h:59
const AVPixFmtDescriptor * desc
Definition vf_swaprect.c:43
uint8_t * temp
Definition vf_swaprect.c:44
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define width
Definition dsp.h:99
int size
static int config_input(AVFilterLink *inlink)
static int trunc_double_to_int(int *dst, double value)
Definition vf_swaprect.c:76
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_swaprect.c:89
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_swaprect.c:61
static const AVOption swaprect_options[]
Definition vf_swaprect.c:49
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
Definition vf_swaprect.c:47
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