Browse code

avfilter: add swaprect filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/12/08 00:45:52
Showing 6 changed files
... ...
@@ -65,6 +65,7 @@ version <next>:
65 65
 - significant performance improvements in Windows Television (WTV) demuxer
66 66
 - nnedi deinterlacer
67 67
 - streamselect video and astreamselect audio filter
68
+- swaprect filter
68 69
 
69 70
 
70 71
 version 2.8:
... ...
@@ -11473,6 +11473,60 @@ Interpolate) pixel art scaling algorithm.
11473 11473
 
11474 11474
 Useful for enlarging pixel art images without reducing sharpness.
11475 11475
 
11476
+@section swaprect
11477
+
11478
+Swap two rectangular objects in video.
11479
+
11480
+This filter accepts the following options:
11481
+
11482
+@table @option
11483
+@item w
11484
+Set object width.
11485
+
11486
+@item h
11487
+Set object height.
11488
+
11489
+@item x1
11490
+Set 1st rect x coordinate.
11491
+
11492
+@item y1
11493
+Set 1st rect y coordinate.
11494
+
11495
+@item x2
11496
+Set 2nd rect x coordinate.
11497
+
11498
+@item y2
11499
+Set 2nd rect y coordinate.
11500
+
11501
+All expressions are evaluated once for each frame.
11502
+@end table
11503
+
11504
+The all options are expressions containing the following constants:
11505
+
11506
+@table @option
11507
+@item w
11508
+@item h
11509
+The input width and height.
11510
+
11511
+@item a
11512
+same as @var{w} / @var{h}
11513
+
11514
+@item sar
11515
+input sample aspect ratio
11516
+
11517
+@item dar
11518
+input display aspect ratio, it is the same as (@var{w} / @var{h}) * @var{sar}
11519
+
11520
+@item n
11521
+The number of the input frame, starting from 0.
11522
+
11523
+@item t
11524
+The timestamp expressed in seconds. It's NAN if the input timestamp is unknown.
11525
+
11526
+@item pos
11527
+the position in the file of the input frame, NAN if unknown
11528
+@end table
11529
+
11476 11530
 @section swapuv
11477 11531
 Swap U & V plane.
11478 11532
 
... ...
@@ -241,6 +241,7 @@ OBJS-$(CONFIG_STEREO3D_FILTER)               += vf_stereo3d.o
241 241
 OBJS-$(CONFIG_STREAMSELECT_FILTER)           += f_streamselect.o
242 242
 OBJS-$(CONFIG_SUBTITLES_FILTER)              += vf_subtitles.o
243 243
 OBJS-$(CONFIG_SUPER2XSAI_FILTER)             += vf_super2xsai.o
244
+OBJS-$(CONFIG_SWAPRECT_FILTER)               += vf_swaprect.o
244 245
 OBJS-$(CONFIG_SWAPUV_FILTER)                 += vf_swapuv.o
245 246
 OBJS-$(CONFIG_TBLEND_FILTER)                 += vf_blend.o dualinput.o framesync.o
246 247
 OBJS-$(CONFIG_TELECINE_FILTER)               += vf_telecine.o
... ...
@@ -261,6 +261,7 @@ void avfilter_register_all(void)
261 261
     REGISTER_FILTER(STREAMSELECT,   streamselect,   vf);
262 262
     REGISTER_FILTER(SUBTITLES,      subtitles,      vf);
263 263
     REGISTER_FILTER(SUPER2XSAI,     super2xsai,     vf);
264
+    REGISTER_FILTER(SWAPRECT,       swaprect,       vf);
264 265
     REGISTER_FILTER(SWAPUV,         swapuv,         vf);
265 266
     REGISTER_FILTER(TBLEND,         tblend,         vf);
266 267
     REGISTER_FILTER(TELECINE,       telecine,       vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  29
33
+#define LIBAVFILTER_VERSION_MINOR  30
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,255 @@
0
+/*
1
+ * Copyright (c) 2015 Paul B. Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/avstring.h"
21
+#include "libavutil/eval.h"
22
+#include "libavutil/imgutils.h"
23
+#include "libavutil/opt.h"
24
+#include "libavutil/pixdesc.h"
25
+
26
+#include "avfilter.h"
27
+#include "formats.h"
28
+#include "internal.h"
29
+#include "video.h"
30
+
31
+typedef struct SwapRectContext {
32
+    const AVClass *class;
33
+    char *w, *h;
34
+    char *x1, *y1;
35
+    char *x2, *y2;
36
+
37
+    int nb_planes;
38
+    int pixsteps[4];
39
+
40
+    const AVPixFmtDescriptor *desc;
41
+    uint8_t *temp;
42
+} SwapRectContext;
43
+
44
+#define OFFSET(x) offsetof(SwapRectContext, x)
45
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
46
+static const AVOption swaprect_options[] = {
47
+    { "w",  "set rect width",                     OFFSET(w),  AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
48
+    { "h",  "set rect height",                    OFFSET(h),  AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
49
+    { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
50
+    { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
51
+    { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"},   0, 0, .flags = FLAGS },
52
+    { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"},   0, 0, .flags = FLAGS },
53
+    { NULL },
54
+};
55
+
56
+AVFILTER_DEFINE_CLASS(swaprect);
57
+
58
+static int query_formats(AVFilterContext *ctx)
59
+{
60
+    AVFilterFormats *pix_fmts = NULL;
61
+    int fmt, ret;
62
+
63
+    for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
64
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
65
+        if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
66
+              desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
67
+              desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
68
+            (ret = ff_add_format(&pix_fmts, fmt)) < 0)
69
+            return ret;
70
+    }
71
+
72
+    return ff_set_common_formats(ctx, pix_fmts);
73
+}
74
+
75
+static const char *const var_names[] = {   "w",   "h",   "a",   "n",   "t",   "pos",   "sar",   "dar",        NULL };
76
+enum                                   { VAR_W, VAR_H, VAR_A, VAR_N, VAR_T, VAR_POS, VAR_SAR, VAR_DAR, VAR_VARS_NB };
77
+
78
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
79
+{
80
+    AVFilterContext *ctx = inlink->dst;
81
+    AVFilterLink *outlink = ctx->outputs[0];
82
+    SwapRectContext *s = ctx->priv;
83
+    double var_values[VAR_VARS_NB];
84
+    int x1[4], y1[4];
85
+    int x2[4], y2[4];
86
+    int aw[4], ah[4];
87
+    int lw[4], lh[4];
88
+    int pw[4], ph[4];
89
+    double dw,  dh;
90
+    double dx1, dy1;
91
+    double dx2, dy2;
92
+    int y, p, w, h, ret;
93
+
94
+    var_values[VAR_W]   = inlink->w;
95
+    var_values[VAR_H]   = inlink->h;
96
+    var_values[VAR_A]   = (float) inlink->w / inlink->h;
97
+    var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
98
+    var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
99
+    var_values[VAR_N]   = inlink->frame_count;
100
+    var_values[VAR_T]   = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
101
+    var_values[VAR_POS] = av_frame_get_pkt_pos(in) == -1 ? NAN : av_frame_get_pkt_pos(in);
102
+
103
+    ret = av_expr_parse_and_eval(&dw, s->w,
104
+                                 var_names, &var_values[0],
105
+                                 NULL, NULL, NULL, NULL,
106
+                                 0, 0, ctx);
107
+    if (ret < 0)
108
+        return ret;
109
+
110
+    ret = av_expr_parse_and_eval(&dh, s->h,
111
+                                 var_names, &var_values[0],
112
+                                 NULL, NULL, NULL, NULL,
113
+                                 0, 0, ctx);
114
+    if (ret < 0)
115
+        return ret;
116
+
117
+    ret = av_expr_parse_and_eval(&dx1, s->x1,
118
+                                 var_names, &var_values[0],
119
+                                 NULL, NULL, NULL, NULL,
120
+                                 0, 0, ctx);
121
+    if (ret < 0)
122
+        return ret;
123
+
124
+    ret = av_expr_parse_and_eval(&dy1, s->y1,
125
+                                 var_names, &var_values[0],
126
+                                 NULL, NULL, NULL, NULL,
127
+                                 0, 0, ctx);
128
+    if (ret < 0)
129
+        return ret;
130
+
131
+    ret = av_expr_parse_and_eval(&dx2, s->x2,
132
+                                 var_names, &var_values[0],
133
+                                 NULL, NULL, NULL, NULL,
134
+                                 0, 0, ctx);
135
+    if (ret < 0)
136
+        return ret;
137
+
138
+    ret = av_expr_parse_and_eval(&dy2, s->y2,
139
+                                 var_names, &var_values[0],
140
+                                 NULL, NULL, NULL, NULL,
141
+                                 0, 0, ctx);
142
+    if (ret < 0)
143
+        return ret;
144
+
145
+    w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
146
+
147
+    x1[0] = av_clip(x1[0], 0, inlink->w - 1);
148
+    y1[0] = av_clip(y1[0], 0, inlink->w - 1);
149
+
150
+    x2[0] = av_clip(x2[0], 0, inlink->w - 1);
151
+    y2[0] = av_clip(y2[0], 0, inlink->w - 1);
152
+
153
+    ah[1] = ah[2] = FF_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
154
+    ah[0] = ah[3] = h;
155
+    aw[1] = aw[2] = FF_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
156
+    aw[0] = aw[3] = w;
157
+
158
+    w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
159
+    h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
160
+
161
+    ph[1] = ph[2] = FF_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
162
+    ph[0] = ph[3] = h;
163
+    pw[1] = pw[2] = FF_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
164
+    pw[0] = pw[3] = w;
165
+
166
+    lh[1] = lh[2] = FF_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
167
+    lh[0] = lh[3] = inlink->h;
168
+    lw[1] = lw[2] = FF_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
169
+    lw[0] = lw[3] = inlink->w;
170
+
171
+    x1[1] = x1[2] = FF_CEIL_RSHIFT(x1[0], s->desc->log2_chroma_w);
172
+    x1[0] = x1[3] = x1[0];
173
+    y1[1] = y1[2] = FF_CEIL_RSHIFT(y1[0], s->desc->log2_chroma_h);
174
+    y1[0] = y1[3] = y1[0];
175
+
176
+    x2[1] = x2[2] = FF_CEIL_RSHIFT(x2[0], s->desc->log2_chroma_w);
177
+    x2[0] = x2[3] = x2[0];
178
+    y2[1] = y2[2] = FF_CEIL_RSHIFT(y2[0], s->desc->log2_chroma_h);
179
+    y2[0] = y2[3] = y2[0];
180
+
181
+    for (p = 0; p < s->nb_planes; p++) {
182
+        if (ph[p] == ah[p] && pw[p] == aw[p]) {
183
+            uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
184
+            uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
185
+
186
+            for (y = 0; y < ph[p]; y++) {
187
+                memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
188
+                memmove(src, dst, pw[p] * s->pixsteps[p]);
189
+                memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
190
+                src += in->linesize[p];
191
+                dst += in->linesize[p];
192
+            }
193
+        }
194
+    }
195
+
196
+    return ff_filter_frame(outlink, in);
197
+}
198
+
199
+static int config_input(AVFilterLink *inlink)
200
+{
201
+    AVFilterContext *ctx = inlink->dst;
202
+    SwapRectContext *s = ctx->priv;
203
+
204
+    if (!s->w  || !s->h  ||
205
+        !s->x1 || !s->y1 ||
206
+        !s->x2 || !s->y2)
207
+        return AVERROR(EINVAL);
208
+
209
+    s->desc = av_pix_fmt_desc_get(inlink->format);
210
+    av_image_fill_max_pixsteps(s->pixsteps, NULL, s->desc);
211
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
212
+
213
+    s->temp = av_malloc_array(inlink->w, s->pixsteps[0]);
214
+    if (!s->temp)
215
+        return AVERROR(ENOMEM);
216
+
217
+    return 0;
218
+}
219
+
220
+static av_cold void uninit(AVFilterContext *ctx)
221
+{
222
+    SwapRectContext *s = ctx->priv;
223
+    av_freep(&s->temp);
224
+}
225
+
226
+static const AVFilterPad inputs[] = {
227
+    {
228
+        .name           = "default",
229
+        .type           = AVMEDIA_TYPE_VIDEO,
230
+        .filter_frame   = filter_frame,
231
+        .config_props   = config_input,
232
+        .needs_writable = 1,
233
+    },
234
+    { NULL }
235
+};
236
+
237
+static const AVFilterPad outputs[] = {
238
+    {
239
+        .name = "default",
240
+        .type = AVMEDIA_TYPE_VIDEO,
241
+    },
242
+    { NULL }
243
+};
244
+
245
+AVFilter ff_vf_swaprect = {
246
+    .name          = "swaprect",
247
+    .description   = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
248
+    .priv_size     = sizeof(SwapRectContext),
249
+    .priv_class    = &swaprect_class,
250
+    .query_formats = query_formats,
251
+    .uninit        = uninit,
252
+    .inputs        = inputs,
253
+    .outputs       = outputs,
254
+};