]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_remap.c
Merge commit 'e1e3a12242347dd11174b2fb9ddac8dc8df16224'
[ffmpeg] / libavfilter / vf_remap.c
1 /*
2  * Copyright (c) 2016 Floris Sluiter
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 /**
22  * @file
23  * Pixel remap filter
24  * This filter copies pixel by pixel a source frame to a target frame.
25  * It remaps the pixels to a new x,y destination based on two files ymap/xmap.
26  * Map files are passed as a parameter and are in PGM format (P2 or P5),
27  * where the values are y(rows)/x(cols) coordinates of the source_frame.
28  * The *target* frame dimension is based on mapfile dimensions: specified in the
29  * header of the mapfile and reflected in the number of datavalues.
30  * Dimensions of ymap and xmap must be equal. Datavalues must be positive or zero.
31  * Any datavalue in the ymap or xmap which value is higher
32  * then the *source* frame height or width is silently ignored, leaving a
33  * blank/chromakey pixel. This can safely be used as a feature to create overlays.
34  *
35  * Algorithm digest:
36  * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
37  */
38
39 #include "libavutil/imgutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
42 #include "avfilter.h"
43 #include "formats.h"
44 #include "framesync.h"
45 #include "internal.h"
46 #include "video.h"
47
48 typedef struct RemapContext {
49     const AVClass *class;
50     int nb_planes;
51     int nb_components;
52     int step;
53     FFFrameSync fs;
54
55     void (*remap)(struct RemapContext *s, const AVFrame *in,
56                   const AVFrame *xin, const AVFrame *yin,
57                   AVFrame *out);
58 } RemapContext;
59
60 #define OFFSET(x) offsetof(RemapContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62
63 static const AVOption remap_options[] = {
64     { NULL }
65 };
66
67 AVFILTER_DEFINE_CLASS(remap);
68
69 static int query_formats(AVFilterContext *ctx)
70 {
71     static const enum AVPixelFormat pix_fmts[] = {
72         AV_PIX_FMT_YUVA444P,
73         AV_PIX_FMT_YUV444P,
74         AV_PIX_FMT_YUVJ444P,
75         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
76         AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
77         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
78         AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
79         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
80         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
81         AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
82         AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
83         AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
84         AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
85         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
86         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
87         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
88         AV_PIX_FMT_GRAY16,
89         AV_PIX_FMT_NONE
90     };
91     static const enum AVPixelFormat map_fmts[] = {
92         AV_PIX_FMT_GRAY16,
93         AV_PIX_FMT_NONE
94     };
95     AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
96     int ret;
97
98     if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
99         !(map_formats = ff_make_format_list(map_fmts))) {
100         ret = AVERROR(ENOMEM);
101         goto fail;
102     }
103     if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
104         (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
105         (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
106         (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
107         goto fail;
108     return 0;
109 fail:
110     if (pix_formats)
111         av_freep(&pix_formats->formats);
112     av_freep(&pix_formats);
113     if (map_formats)
114         av_freep(&map_formats->formats);
115     av_freep(&map_formats);
116     return ret;
117 }
118
119 /**
120  * remap_planar algorithm expects planes of same size
121  * pixels are copied from source to target using :
122  * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
123  */
124 static void remap_planar(RemapContext *s, const AVFrame *in,
125                          const AVFrame *xin, const AVFrame *yin,
126                          AVFrame *out)
127 {
128     const int xlinesize = xin->linesize[0] / 2;
129     const int ylinesize = yin->linesize[0] / 2;
130     int x , y, plane;
131
132     for (plane = 0; plane < s->nb_planes ; plane++) {
133         uint8_t *dst         = out->data[plane];
134         const int dlinesize  = out->linesize[plane];
135         const uint8_t *src   = in->data[plane];
136         const int slinesize  = in->linesize[plane];
137         const uint16_t *xmap = (const uint16_t *)xin->data[0];
138         const uint16_t *ymap = (const uint16_t *)yin->data[0];
139
140         for (y = 0; y < out->height; y++) {
141             for (x = 0; x < out->width; x++) {
142                 if (ymap[x] < in->height && xmap[x] < in->width) {
143                     dst[x] = src[ymap[x] * slinesize + xmap[x]];
144                 } else {
145                     dst[x] = 0;
146                 }
147             }
148             dst  += dlinesize;
149             xmap += xlinesize;
150             ymap += ylinesize;
151         }
152     }
153 }
154
155 static void remap_planar16(RemapContext *s, const AVFrame *in,
156                            const AVFrame *xin, const AVFrame *yin,
157                            AVFrame *out)
158 {
159     const int xlinesize = xin->linesize[0] / 2;
160     const int ylinesize = yin->linesize[0] / 2;
161     int x , y, plane;
162
163     for (plane = 0; plane < s->nb_planes ; plane++) {
164         uint16_t *dst        = (uint16_t *)out->data[plane];
165         const int dlinesize  = out->linesize[plane] / 2;
166         const uint16_t *src  = (const uint16_t *)in->data[plane];
167         const int slinesize  = in->linesize[plane] / 2;
168         const uint16_t *xmap = (const uint16_t *)xin->data[0];
169         const uint16_t *ymap = (const uint16_t *)yin->data[0];
170
171         for (y = 0; y < out->height; y++) {
172             for (x = 0; x < out->width; x++) {
173                 if (ymap[x] < in->height && xmap[x] < in->width) {
174                     dst[x] = src[ymap[x] * slinesize + xmap[x]];
175                 } else {
176                     dst[x] = 0;
177                 }
178             }
179             dst  += dlinesize;
180             xmap += xlinesize;
181             ymap += ylinesize;
182         }
183     }
184 }
185
186 /**
187  * remap_packed algorithm expects pixels with both padded bits (step) and
188  * number of components correctly set.
189  * pixels are copied from source to target using :
190  * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
191  */
192 static void remap_packed(RemapContext *s, const AVFrame *in,
193                          const AVFrame *xin, const AVFrame *yin,
194                          AVFrame *out)
195 {
196     uint8_t *dst = out->data[0];
197     const uint8_t *src  = in->data[0];
198     const int dlinesize = out->linesize[0];
199     const int slinesize = in->linesize[0];
200     const int xlinesize = xin->linesize[0] / 2;
201     const int ylinesize = yin->linesize[0] / 2;
202     const uint16_t *xmap = (const uint16_t *)xin->data[0];
203     const uint16_t *ymap = (const uint16_t *)yin->data[0];
204     const int step = s->step;
205     int c, x, y;
206
207     for (y = 0; y < out->height; y++) {
208         for (x = 0; x < out->width; x++) {
209             for (c = 0; c < s->nb_components; c++) {
210                 if (ymap[x] < in->height && xmap[x] < in->width) {
211                     dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
212                 } else {
213                     dst[x * step + c] = 0;
214                 }
215             }
216         }
217         dst  += dlinesize;
218         xmap += xlinesize;
219         ymap += ylinesize;
220     }
221 }
222
223 static void remap_packed16(RemapContext *s, const AVFrame *in,
224                            const AVFrame *xin, const AVFrame *yin,
225                            AVFrame *out)
226 {
227     uint16_t *dst = (uint16_t *)out->data[0];
228     const uint16_t *src  = (const uint16_t *)in->data[0];
229     const int dlinesize = out->linesize[0] / 2;
230     const int slinesize = in->linesize[0] / 2;
231     const int xlinesize = xin->linesize[0] / 2;
232     const int ylinesize = yin->linesize[0] / 2;
233     const uint16_t *xmap = (const uint16_t *)xin->data[0];
234     const uint16_t *ymap = (const uint16_t *)yin->data[0];
235     const int step = s->step / 2;
236     int c, x, y;
237
238     for (y = 0; y < out->height; y++) {
239         for (x = 0; x < out->width; x++) {
240             for (c = 0; c < s->nb_components; c++) {
241                 if (ymap[x] < in->height && xmap[x] < in->width) {
242                     dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
243                 } else {
244                     dst[x * step + c] = 0;
245                 }
246             }
247         }
248         dst  += dlinesize;
249         xmap += xlinesize;
250         ymap += ylinesize;
251     }
252 }
253
254 static int config_input(AVFilterLink *inlink)
255 {
256     AVFilterContext *ctx = inlink->dst;
257     RemapContext *s = ctx->priv;
258     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
259
260     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
261     s->nb_components = desc->nb_components;
262
263     if (desc->comp[0].depth == 8) {
264         if (s->nb_planes > 1 || s->nb_components == 1) {
265             s->remap = remap_planar;
266         } else {
267             s->remap = remap_packed;
268         }
269     } else {
270         if (s->nb_planes > 1 || s->nb_components == 1) {
271             s->remap = remap_planar16;
272         } else {
273             s->remap = remap_packed16;
274         }
275     }
276
277     s->step = av_get_padded_bits_per_pixel(desc) >> 3;
278     return 0;
279 }
280
281 static int process_frame(FFFrameSync *fs)
282 {
283     AVFilterContext *ctx = fs->parent;
284     RemapContext *s = fs->opaque;
285     AVFilterLink *outlink = ctx->outputs[0];
286     AVFrame *out, *in, *xpic, *ypic;
287     int ret;
288
289     if ((ret = ff_framesync_get_frame(&s->fs, 0, &in,   0)) < 0 ||
290         (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
291         (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
292         return ret;
293
294     if (ctx->is_disabled) {
295         out = av_frame_clone(in);
296         if (!out)
297             return AVERROR(ENOMEM);
298     } else {
299         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
300         if (!out)
301             return AVERROR(ENOMEM);
302         av_frame_copy_props(out, in);
303
304         s->remap(s, in, xpic, ypic, out);
305     }
306     out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
307
308     return ff_filter_frame(outlink, out);
309 }
310
311 static int config_output(AVFilterLink *outlink)
312 {
313     AVFilterContext *ctx = outlink->src;
314     RemapContext *s = ctx->priv;
315     AVFilterLink *srclink = ctx->inputs[0];
316     AVFilterLink *xlink = ctx->inputs[1];
317     AVFilterLink *ylink = ctx->inputs[2];
318     FFFrameSyncIn *in;
319     int ret;
320
321     if (xlink->w != ylink->w || xlink->h != ylink->h) {
322         av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
323                "(size %dx%d) do not match the corresponding "
324                "third input link %s parameters (%dx%d)\n",
325                ctx->input_pads[1].name, xlink->w, xlink->h,
326                ctx->input_pads[2].name, ylink->w, ylink->h);
327         return AVERROR(EINVAL);
328     }
329
330     outlink->w = xlink->w;
331     outlink->h = xlink->h;
332     outlink->time_base = srclink->time_base;
333     outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
334     outlink->frame_rate = srclink->frame_rate;
335
336     ret = ff_framesync_init(&s->fs, ctx, 3);
337     if (ret < 0)
338         return ret;
339
340     in = s->fs.in;
341     in[0].time_base = srclink->time_base;
342     in[1].time_base = xlink->time_base;
343     in[2].time_base = ylink->time_base;
344     in[0].sync   = 2;
345     in[0].before = EXT_STOP;
346     in[0].after  = EXT_STOP;
347     in[1].sync   = 1;
348     in[1].before = EXT_NULL;
349     in[1].after  = EXT_INFINITY;
350     in[2].sync   = 1;
351     in[2].before = EXT_NULL;
352     in[2].after  = EXT_INFINITY;
353     s->fs.opaque   = s;
354     s->fs.on_event = process_frame;
355
356     return ff_framesync_configure(&s->fs);
357 }
358
359 static int activate(AVFilterContext *ctx)
360 {
361     RemapContext *s = ctx->priv;
362     return ff_framesync_activate(&s->fs);
363 }
364
365
366 static av_cold void uninit(AVFilterContext *ctx)
367 {
368     RemapContext *s = ctx->priv;
369
370     ff_framesync_uninit(&s->fs);
371 }
372
373 static const AVFilterPad remap_inputs[] = {
374     {
375         .name         = "source",
376         .type         = AVMEDIA_TYPE_VIDEO,
377         .config_props = config_input,
378     },
379     {
380         .name         = "xmap",
381         .type         = AVMEDIA_TYPE_VIDEO,
382     },
383     {
384         .name         = "ymap",
385         .type         = AVMEDIA_TYPE_VIDEO,
386     },
387     { NULL }
388 };
389
390 static const AVFilterPad remap_outputs[] = {
391     {
392         .name          = "default",
393         .type          = AVMEDIA_TYPE_VIDEO,
394         .config_props  = config_output,
395     },
396     { NULL }
397 };
398
399 AVFilter ff_vf_remap = {
400     .name          = "remap",
401     .description   = NULL_IF_CONFIG_SMALL("Remap pixels."),
402     .priv_size     = sizeof(RemapContext),
403     .uninit        = uninit,
404     .query_formats = query_formats,
405     .activate      = activate,
406     .inputs        = remap_inputs,
407     .outputs       = remap_outputs,
408     .priv_class    = &remap_class,
409     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
410 };