Browse code

avfilter: add remap filter

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

F.Sluiter authored on 2016/04/05 16:36:37
Showing 6 changed files
... ...
@@ -21,6 +21,7 @@ version <next>:
21 21
 - ADPCM IMA DAT4 decoder
22 22
 - musx demuxer
23 23
 - aix demuxer
24
+- remap filter
24 25
 
25 26
 version 3.0:
26 27
 - Common Encryption (CENC) MP4 encoding and decoding support
... ...
@@ -10419,6 +10419,18 @@ less than @code{0}, the filter will try to use a good random seed on a
10419 10419
 best effort basis.
10420 10420
 @end table
10421 10421
 
10422
+@section remap
10423
+
10424
+Remap pixels using 2nd: Xmap and 3rd: Ymap input video stream.
10425
+
10426
+Destination pixel at position (X, Y) will be picked from source (x, y) position
10427
+where x = Xmap(X, Y) and y = Ymap(X, Y). If mapping values are out of range, zero
10428
+value for pixel will be used for destination pixel.
10429
+
10430
+Xmap and Ymap input video streams must be of same dimensions. Output video stream
10431
+will have Xmap/Ymap video stream dimensions.
10432
+Xmap and Ymap input video streams are 16bit depth, single channel.
10433
+
10422 10434
 @section removegrain
10423 10435
 
10424 10436
 The removegrain filter is a spatial denoiser for progressive video.
... ...
@@ -224,6 +224,7 @@ OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
224 224
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
225 225
 OBJS-$(CONFIG_RANDOM_FILTER)                 += vf_random.o
226 226
 OBJS-$(CONFIG_REALTIME_FILTER)               += f_realtime.o
227
+OBJS-$(CONFIG_REMAP_FILTER)                  += vf_remap.o framesync.o
227 228
 OBJS-$(CONFIG_REMOVEGRAIN_FILTER)            += vf_removegrain.o
228 229
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
229 230
 OBJS-$(CONFIG_REPEATFIELDS_FILTER)           += vf_repeatfields.o
... ...
@@ -244,6 +244,7 @@ void avfilter_register_all(void)
244 244
     REGISTER_FILTER(QP,             qp,             vf);
245 245
     REGISTER_FILTER(RANDOM,         random,         vf);
246 246
     REGISTER_FILTER(REALTIME,       realtime,       vf);
247
+    REGISTER_FILTER(REMAP,          remap,          vf);
247 248
     REGISTER_FILTER(REMOVEGRAIN,    removegrain,    vf);
248 249
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
249 250
     REGISTER_FILTER(REPEATFIELDS,   repeatfields,   vf);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  40
34
-#define LIBAVFILTER_VERSION_MICRO 102
33
+#define LIBAVFILTER_VERSION_MINOR  41
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \
38 38
new file mode 100644
... ...
@@ -0,0 +1,337 @@
0
+/*
1
+ * Copyright (c) 2016 Floris Sluiter
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
+/**
21
+ * @file
22
+ * Pixel remap filter
23
+ * This filter copies pixel by pixel a source frame to a target frame.
24
+ * It remaps the pixels to a new x,y destination based on two files ymap/xmap.
25
+ * Map files are passed as a parameter and are in PGM format (P2 or P5),
26
+ * where the values are y(rows)/x(cols) coordinates of the source_frame.
27
+ * The *target* frame dimension is based on mapfile dimensions: specified in the
28
+ * header of the mapfile and reflected in the number of datavalues.
29
+ * Dimensions of ymap and xmap must be equal. Datavalues must be positive or zero.
30
+ * Any datavalue in the ymap or xmap which value is higher
31
+ * then the *source* frame height or width is silently ignored, leaving a
32
+ * blank/chromakey pixel. This can safely be used as a feature to create overlays.
33
+ *
34
+ * Algorithm digest:
35
+ * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
36
+ */
37
+
38
+#include "libavutil/imgutils.h"
39
+#include "libavutil/pixdesc.h"
40
+#include "libavutil/opt.h"
41
+#include "avfilter.h"
42
+#include "formats.h"
43
+#include "framesync.h"
44
+#include "internal.h"
45
+#include "video.h"
46
+
47
+typedef struct RemapContext {
48
+    const AVClass *class;
49
+    int nb_planes;
50
+    int nb_components;
51
+    int step;
52
+    FFFrameSync fs;
53
+
54
+    void (*remap)(struct RemapContext *s, const AVFrame *in,
55
+                  const AVFrame *xin, const AVFrame *yin,
56
+                  AVFrame *out);
57
+} RemapContext;
58
+
59
+#define OFFSET(x) offsetof(RemapContext, x)
60
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61
+
62
+static const AVOption remap_options[] = {
63
+    { NULL }
64
+};
65
+
66
+AVFILTER_DEFINE_CLASS(remap);
67
+
68
+static int query_formats(AVFilterContext *ctx)
69
+{
70
+    static const enum AVPixelFormat pix_fmts[] = {
71
+        AV_PIX_FMT_YUVA444P,
72
+        AV_PIX_FMT_YUV444P,
73
+        AV_PIX_FMT_YUVJ444P,
74
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
75
+        AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
76
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
77
+        AV_PIX_FMT_NONE
78
+    };
79
+    static const enum AVPixelFormat map_fmts[] = {
80
+        AV_PIX_FMT_GRAY16,
81
+        AV_PIX_FMT_NONE
82
+    };
83
+    AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
84
+    int ret;
85
+
86
+    if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
87
+        !(map_formats = ff_make_format_list(map_fmts))) {
88
+        ret = AVERROR(ENOMEM);
89
+        goto fail;
90
+    }
91
+    if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
92
+        (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
93
+        (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
94
+        (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
95
+        goto fail;
96
+    return 0;
97
+fail:
98
+    if (pix_formats)
99
+        av_freep(&pix_formats->formats);
100
+    av_freep(&pix_formats);
101
+    if (map_formats)
102
+        av_freep(&map_formats->formats);
103
+    av_freep(&map_formats);
104
+    return ret;
105
+}
106
+
107
+/**
108
+ * remap_planar algorithm expects planes of same size
109
+ * pixels are copied from source to target using :
110
+ * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
111
+ */
112
+static void remap_planar(RemapContext *s, const AVFrame *in,
113
+                         const AVFrame *xin, const AVFrame *yin,
114
+                         AVFrame *out)
115
+{
116
+    const int xlinesize = xin->linesize[0] / 2;
117
+    const int ylinesize = yin->linesize[0] / 2;
118
+    int x , y, plane;
119
+
120
+    for (plane = 0; plane < s->nb_planes ; plane++) {
121
+        uint8_t *dst         = out->data[plane];
122
+        const int dlinesize  = out->linesize[plane];
123
+        const uint8_t *src   = in->data[plane];
124
+        const int slinesize  = in->linesize[plane];
125
+        const uint16_t *xmap = (const uint16_t *)xin->data[0];
126
+        const uint16_t *ymap = (const uint16_t *)yin->data[0];
127
+
128
+        for (y = 0; y < out->height; y++) {
129
+            for (x = 0; x < out->width; x++) {
130
+                if (ymap[x] < in->height && xmap[x] < in->width) {
131
+                    dst[x] = src[ymap[x] * slinesize + xmap[x]];
132
+                } else {
133
+                    dst[x] = 0;
134
+                }
135
+            }
136
+            dst  += dlinesize;
137
+            xmap += xlinesize;
138
+            ymap += ylinesize;
139
+        }
140
+    }
141
+}
142
+
143
+/**
144
+ * remap_packed algorithm expects pixels with both padded bits (step) and
145
+ * number of components correctly set.
146
+ * pixels are copied from source to target using :
147
+ * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
148
+ */
149
+static void remap_packed(RemapContext *s, const AVFrame *in,
150
+                         const AVFrame *xin, const AVFrame *yin,
151
+                         AVFrame *out)
152
+{
153
+    uint8_t *dst = out->data[0];
154
+    const uint8_t *src  = in->data[0];
155
+    const int dlinesize = out->linesize[0];
156
+    const int slinesize = in->linesize[0];
157
+    const int xlinesize = xin->linesize[0] / 2;
158
+    const int ylinesize = yin->linesize[0] / 2;
159
+    const uint16_t *xmap = (const uint16_t *)xin->data[0];
160
+    const uint16_t *ymap = (const uint16_t *)yin->data[0];
161
+    const int step = s->step;
162
+    int c, x, y;
163
+
164
+    for (y = 0; y < out->height; y++) {
165
+        for (x = 0; x < out->width; x++) {
166
+            for (c = 0; c < s->nb_components; c++) {
167
+                if (ymap[x] < in->height && xmap[x] < in->width) {
168
+                    dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
169
+                } else {
170
+                    dst[x * step + c] = 0;
171
+                }
172
+            }
173
+        }
174
+        dst  += dlinesize;
175
+        xmap += xlinesize;
176
+        ymap += ylinesize;
177
+    }
178
+}
179
+
180
+static int config_input(AVFilterLink *inlink)
181
+{
182
+    AVFilterContext *ctx = inlink->dst;
183
+    RemapContext *s = ctx->priv;
184
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
185
+
186
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
187
+    s->nb_components = desc->nb_components;
188
+
189
+    if (s->nb_planes > 1 || s->nb_components == 1) {
190
+        s->remap = remap_planar;
191
+    } else {
192
+        s->remap = remap_packed;
193
+    }
194
+
195
+    s->step = av_get_padded_bits_per_pixel(desc) >> 3;
196
+    return 0;
197
+}
198
+
199
+static int process_frame(FFFrameSync *fs)
200
+{
201
+    AVFilterContext *ctx = fs->parent;
202
+    RemapContext *s = fs->opaque;
203
+    AVFilterLink *outlink = ctx->outputs[0];
204
+    AVFrame *out, *in, *xpic, *ypic;
205
+    int ret;
206
+
207
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &in,   0)) < 0 ||
208
+        (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
209
+        (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
210
+        return ret;
211
+
212
+    if (ctx->is_disabled) {
213
+        out = av_frame_clone(in);
214
+        if (!out)
215
+            return AVERROR(ENOMEM);
216
+    } else {
217
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
218
+        if (!out)
219
+            return AVERROR(ENOMEM);
220
+        av_frame_copy_props(out, in);
221
+
222
+        s->remap(s, in, xpic, ypic, out);
223
+    }
224
+    out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
225
+
226
+    return ff_filter_frame(outlink, out);
227
+}
228
+
229
+static int config_output(AVFilterLink *outlink)
230
+{
231
+    AVFilterContext *ctx = outlink->src;
232
+    RemapContext *s = ctx->priv;
233
+    AVFilterLink *srclink = ctx->inputs[0];
234
+    AVFilterLink *xlink = ctx->inputs[1];
235
+    AVFilterLink *ylink = ctx->inputs[2];
236
+    FFFrameSyncIn *in;
237
+    int ret;
238
+
239
+    if (xlink->w != ylink->w || xlink->h != ylink->h) {
240
+        av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
241
+               "(size %dx%d) do not match the corresponding "
242
+               "third input link %s parameters (%dx%d)\n",
243
+               ctx->input_pads[1].name, xlink->w, xlink->h,
244
+               ctx->input_pads[2].name, ylink->w, ylink->h);
245
+        return AVERROR(EINVAL);
246
+    }
247
+
248
+    outlink->w = xlink->w;
249
+    outlink->h = xlink->h;
250
+    outlink->time_base = srclink->time_base;
251
+    outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
252
+    outlink->frame_rate = srclink->frame_rate;
253
+
254
+    ret = ff_framesync_init(&s->fs, ctx, 3);
255
+    if (ret < 0)
256
+        return ret;
257
+
258
+    in = s->fs.in;
259
+    in[0].time_base = srclink->time_base;
260
+    in[1].time_base = xlink->time_base;
261
+    in[2].time_base = ylink->time_base;
262
+    in[0].sync   = 2;
263
+    in[0].before = EXT_STOP;
264
+    in[0].after  = EXT_STOP;
265
+    in[1].sync   = 1;
266
+    in[1].before = EXT_NULL;
267
+    in[1].after  = EXT_INFINITY;
268
+    in[2].sync   = 1;
269
+    in[2].before = EXT_NULL;
270
+    in[2].after  = EXT_INFINITY;
271
+    s->fs.opaque   = s;
272
+    s->fs.on_event = process_frame;
273
+
274
+    return ff_framesync_configure(&s->fs);
275
+}
276
+
277
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
278
+{
279
+    RemapContext *s = inlink->dst->priv;
280
+    return ff_framesync_filter_frame(&s->fs, inlink, buf);
281
+}
282
+
283
+static int request_frame(AVFilterLink *outlink)
284
+{
285
+    RemapContext *s = outlink->src->priv;
286
+    return ff_framesync_request_frame(&s->fs, outlink);
287
+}
288
+
289
+static av_cold void uninit(AVFilterContext *ctx)
290
+{
291
+    RemapContext *s = ctx->priv;
292
+
293
+    ff_framesync_uninit(&s->fs);
294
+}
295
+
296
+static const AVFilterPad remap_inputs[] = {
297
+    {
298
+        .name         = "source",
299
+        .type         = AVMEDIA_TYPE_VIDEO,
300
+        .filter_frame = filter_frame,
301
+        .config_props = config_input,
302
+    },
303
+    {
304
+        .name         = "xmap",
305
+        .type         = AVMEDIA_TYPE_VIDEO,
306
+        .filter_frame = filter_frame,
307
+    },
308
+    {
309
+        .name         = "ymap",
310
+        .type         = AVMEDIA_TYPE_VIDEO,
311
+        .filter_frame = filter_frame,
312
+    },
313
+    { NULL }
314
+};
315
+
316
+static const AVFilterPad remap_outputs[] = {
317
+    {
318
+        .name          = "default",
319
+        .type          = AVMEDIA_TYPE_VIDEO,
320
+        .config_props  = config_output,
321
+        .request_frame = request_frame,
322
+    },
323
+    { NULL }
324
+};
325
+
326
+AVFilter ff_vf_remap = {
327
+    .name          = "remap",
328
+    .description   = NULL_IF_CONFIG_SMALL("Remap pixels"),
329
+    .priv_size     = sizeof(RemapContext),
330
+    .uninit        = uninit,
331
+    .query_formats = query_formats,
332
+    .inputs        = remap_inputs,
333
+    .outputs       = remap_outputs,
334
+    .priv_class    = &remap_class,
335
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
336
+};