2 * Copyright (c) 2016 Floris Sluiter
4 * This file is part of FFmpeg.
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.
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.
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
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.
36 * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
39 #include "libavutil/imgutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
44 #include "framesync.h"
48 typedef struct RemapContext {
55 void (*remap)(struct RemapContext *s, const AVFrame *in,
56 const AVFrame *xin, const AVFrame *yin,
60 #define OFFSET(x) offsetof(RemapContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
63 static const AVOption remap_options[] = {
67 AVFILTER_DEFINE_CLASS(remap);
69 static int query_formats(AVFilterContext *ctx)
71 static const enum AVPixelFormat pix_fmts[] = {
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,
91 static const enum AVPixelFormat map_fmts[] = {
95 AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
98 if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
99 !(map_formats = ff_make_format_list(map_fmts))) {
100 ret = AVERROR(ENOMEM);
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)
111 av_freep(&pix_formats->formats);
112 av_freep(&pix_formats);
114 av_freep(&map_formats->formats);
115 av_freep(&map_formats);
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] ];
124 static void remap_planar(RemapContext *s, const AVFrame *in,
125 const AVFrame *xin, const AVFrame *yin,
128 const int xlinesize = xin->linesize[0] / 2;
129 const int ylinesize = yin->linesize[0] / 2;
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];
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]];
155 static void remap_planar16(RemapContext *s, const AVFrame *in,
156 const AVFrame *xin, const AVFrame *yin,
159 const int xlinesize = xin->linesize[0] / 2;
160 const int ylinesize = yin->linesize[0] / 2;
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];
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]];
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] ];
192 static void remap_packed(RemapContext *s, const AVFrame *in,
193 const AVFrame *xin, const AVFrame *yin,
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;
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];
213 dst[x * step + c] = 0;
223 static void remap_packed16(RemapContext *s, const AVFrame *in,
224 const AVFrame *xin, const AVFrame *yin,
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;
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];
244 dst[x * step + c] = 0;
254 static int config_input(AVFilterLink *inlink)
256 AVFilterContext *ctx = inlink->dst;
257 RemapContext *s = ctx->priv;
258 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
260 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
261 s->nb_components = desc->nb_components;
263 if (desc->comp[0].depth == 8) {
264 if (s->nb_planes > 1 || s->nb_components == 1) {
265 s->remap = remap_planar;
267 s->remap = remap_packed;
270 if (s->nb_planes > 1 || s->nb_components == 1) {
271 s->remap = remap_planar16;
273 s->remap = remap_packed16;
277 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
281 static int process_frame(FFFrameSync *fs)
283 AVFilterContext *ctx = fs->parent;
284 RemapContext *s = fs->opaque;
285 AVFilterLink *outlink = ctx->outputs[0];
286 AVFrame *out, *in, *xpic, *ypic;
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)
294 if (ctx->is_disabled) {
295 out = av_frame_clone(in);
297 return AVERROR(ENOMEM);
299 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
301 return AVERROR(ENOMEM);
302 av_frame_copy_props(out, in);
304 s->remap(s, in, xpic, ypic, out);
306 out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
308 return ff_filter_frame(outlink, out);
311 static int config_output(AVFilterLink *outlink)
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];
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);
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;
336 ret = ff_framesync_init(&s->fs, ctx, 3);
341 in[0].time_base = srclink->time_base;
342 in[1].time_base = xlink->time_base;
343 in[2].time_base = ylink->time_base;
345 in[0].before = EXT_STOP;
346 in[0].after = EXT_STOP;
348 in[1].before = EXT_NULL;
349 in[1].after = EXT_INFINITY;
351 in[2].before = EXT_NULL;
352 in[2].after = EXT_INFINITY;
354 s->fs.on_event = process_frame;
356 return ff_framesync_configure(&s->fs);
359 static int activate(AVFilterContext *ctx)
361 RemapContext *s = ctx->priv;
362 return ff_framesync_activate(&s->fs);
366 static av_cold void uninit(AVFilterContext *ctx)
368 RemapContext *s = ctx->priv;
370 ff_framesync_uninit(&s->fs);
373 static const AVFilterPad remap_inputs[] = {
376 .type = AVMEDIA_TYPE_VIDEO,
377 .config_props = config_input,
381 .type = AVMEDIA_TYPE_VIDEO,
385 .type = AVMEDIA_TYPE_VIDEO,
390 static const AVFilterPad remap_outputs[] = {
393 .type = AVMEDIA_TYPE_VIDEO,
394 .config_props = config_output,
399 AVFilter ff_vf_remap = {
401 .description = NULL_IF_CONFIG_SMALL("Remap pixels."),
402 .priv_size = sizeof(RemapContext),
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,