FFmpeg  4.3.8
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/avstring.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32 
33 typedef struct SwapRectContext {
34  const AVClass *class;
35  char *w, *h;
36  char *x1, *y1;
37  char *x2, *y2;
38 
39  int nb_planes;
40  int pixsteps[4];
41 
45 
46 #define OFFSET(x) offsetof(SwapRectContext, x)
47 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
48 static const AVOption swaprect_options[] = {
49  { "w", "set rect width", OFFSET(w), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
50  { "h", "set rect height", OFFSET(h), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
51  { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
52  { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
53  { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
54  { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
55  { NULL },
56 };
57 
58 AVFILTER_DEFINE_CLASS(swaprect);
59 
61 {
63  int fmt, ret;
64 
65  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
67  if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
70  (ret = ff_add_format(&pix_fmts, fmt)) < 0)
71  return ret;
72  }
73 
74  return ff_set_common_formats(ctx, pix_fmts);
75 }
76 
77 static const char *const var_names[] = { "w", "h", "a", "n", "t", "pos", "sar", "dar", NULL };
79 
80 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
81 {
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] = inlink->frame_count_out;
102  var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
103  var_values[VAR_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
104 
105  ret = av_expr_parse_and_eval(&dw, s->w,
106  var_names, &var_values[0],
107  NULL, NULL, NULL, NULL,
108  0, 0, ctx);
109  if (ret < 0)
110  return ret;
111 
112  ret = av_expr_parse_and_eval(&dh, s->h,
113  var_names, &var_values[0],
114  NULL, NULL, NULL, NULL,
115  0, 0, ctx);
116  if (ret < 0)
117  return ret;
118 
119  ret = av_expr_parse_and_eval(&dx1, s->x1,
120  var_names, &var_values[0],
121  NULL, NULL, NULL, NULL,
122  0, 0, ctx);
123  if (ret < 0)
124  return ret;
125 
126  ret = av_expr_parse_and_eval(&dy1, s->y1,
127  var_names, &var_values[0],
128  NULL, NULL, NULL, NULL,
129  0, 0, ctx);
130  if (ret < 0)
131  return ret;
132 
133  ret = av_expr_parse_and_eval(&dx2, s->x2,
134  var_names, &var_values[0],
135  NULL, NULL, NULL, NULL,
136  0, 0, ctx);
137  if (ret < 0)
138  return ret;
139 
140  ret = av_expr_parse_and_eval(&dy2, s->y2,
141  var_names, &var_values[0],
142  NULL, NULL, NULL, NULL,
143  0, 0, ctx);
144  if (ret < 0)
145  return ret;
146 
147  w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
148 
149  x1[0] = av_clip(x1[0], 0, inlink->w - 1);
150  y1[0] = av_clip(y1[0], 0, inlink->h - 1);
151 
152  x2[0] = av_clip(x2[0], 0, inlink->w - 1);
153  y2[0] = av_clip(y2[0], 0, inlink->h - 1);
154 
155  ah[1] = ah[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
156  ah[0] = ah[3] = h;
157  aw[1] = aw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
158  aw[0] = aw[3] = w;
159 
160  w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
161  h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
162 
163  ph[1] = ph[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
164  ph[0] = ph[3] = h;
165  pw[1] = pw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
166  pw[0] = pw[3] = w;
167 
168  lh[1] = lh[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
169  lh[0] = lh[3] = inlink->h;
170  lw[1] = lw[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
171  lw[0] = lw[3] = inlink->w;
172 
173  x1[1] = x1[2] = (x1[0] >> s->desc->log2_chroma_w);
174  x1[0] = x1[3] = x1[0];
175  y1[1] = y1[2] = (y1[0] >> s->desc->log2_chroma_h);
176  y1[0] = y1[3] = y1[0];
177 
178  x2[1] = x2[2] = (x2[0] >> s->desc->log2_chroma_w);
179  x2[0] = x2[3] = x2[0];
180  y2[1] = y2[2] = (y2[0] >> s->desc->log2_chroma_h);
181  y2[0] = y2[3] = y2[0];
182 
183 
184  av_assert0(FFMAX(x1[1], x2[1]) + pw[1] <= lw[1]);
185  av_assert0(FFMAX(y1[1], y2[1]) + ph[1] <= lh[1]);
186 
187  for (p = 0; p < s->nb_planes; p++) {
188  if (ph[p] == ah[p] && pw[p] == aw[p]) {
189  uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
190  uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
191 
192  for (y = 0; y < ph[p]; y++) {
193  memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
194  memmove(src, dst, pw[p] * s->pixsteps[p]);
195  memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
196  src += in->linesize[p];
197  dst += in->linesize[p];
198  }
199  }
200  }
201 
202  return ff_filter_frame(outlink, in);
203 }
204 
205 static int config_input(AVFilterLink *inlink)
206 {
207  AVFilterContext *ctx = inlink->dst;
208  SwapRectContext *s = ctx->priv;
209 
210  if (!s->w || !s->h ||
211  !s->x1 || !s->y1 ||
212  !s->x2 || !s->y2)
213  return AVERROR(EINVAL);
214 
215  s->desc = av_pix_fmt_desc_get(inlink->format);
218 
219  s->temp = av_malloc_array(inlink->w, s->pixsteps[0]);
220  if (!s->temp)
221  return AVERROR(ENOMEM);
222 
223  return 0;
224 }
225 
227 {
228  SwapRectContext *s = ctx->priv;
229  av_freep(&s->temp);
230 }
231 
232 static const AVFilterPad inputs[] = {
233  {
234  .name = "default",
235  .type = AVMEDIA_TYPE_VIDEO,
236  .filter_frame = filter_frame,
237  .config_props = config_input,
238  .needs_writable = 1,
239  },
240  { NULL }
241 };
242 
243 static const AVFilterPad outputs[] = {
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_VIDEO,
247  },
248  { NULL }
249 };
250 
252  .name = "swaprect",
253  .description = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
254  .priv_size = sizeof(SwapRectContext),
255  .priv_class = &swaprect_class,
257  .uninit = uninit,
258  .inputs = inputs,
259  .outputs = outputs,
261 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
int64_t pkt_pos
reordered pos from the last AVPacket that has been input into the decoder
Definition: frame.h:571
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2589
Main libavfilter public API header.
uint8_t * temp
Definition: vf_swaprect.c:43
int num
Numerator.
Definition: rational.h:59
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
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 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:125
const char * name
Pad name.
Definition: internal.h:60
#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:1075
uint8_t
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_swaprect.c:80
#define av_cold
Definition: attributes.h:88
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:393
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
#define FLAGS
Definition: vf_swaprect.c:47
#define FFMIN3(a, b, c)
Definition: common.h:97
static const AVFilterPad outputs[]
Definition: vf_swaprect.c:243
AVFILTER_DEFINE_CLASS(swaprect)
static const AVFilterPad inputs[]
Definition: vf_swaprect.c:232
A filter pad used for either input or output.
Definition: internal.h:54
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_swaprect.c:226
#define src
Definition: vp8dsp.c:254
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:776
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:605
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#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:188
void * priv
private data for use by the filter
Definition: avfilter.h:353
static const char *const var_names[]
Definition: vf_swaprect.c:77
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:350
#define FFMAX(a, b)
Definition: common.h:94
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
#define NAN
Definition: mathematics.h:64
AVFormatContext * ctx
Definition: movenc.c:48
static int config_input(AVFilterLink *inlink)
Definition: vf_swaprect.c:205
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVOption swaprect_options[]
Definition: vf_swaprect.c:48
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
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)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
#define flags(name, subs,...)
Definition: cbs_av1.c:576
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
const AVPixFmtDescriptor * desc
Definition: vf_swaprect.c:42
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
#define av_freep(p)
#define av_malloc_array(a, b)
internal API functions
#define OFFSET(x)
Definition: vf_swaprect.c:46
AVFilter ff_vf_swaprect
Definition: vf_swaprect.c:251
static int query_formats(AVFilterContext *ctx)
Definition: vf_swaprect.c:60
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
simple arithmetic expression evaluator
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58