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,
80 static const enum AVPixelFormat map_fmts[] = {
84 AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
87 if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
88 !(map_formats = ff_make_format_list(map_fmts))) {
89 ret = AVERROR(ENOMEM);
92 if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
93 (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
94 (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
95 (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
100 av_freep(&pix_formats->formats);
101 av_freep(&pix_formats);
103 av_freep(&map_formats->formats);
104 av_freep(&map_formats);
109 * remap_planar algorithm expects planes of same size
110 * pixels are copied from source to target using :
111 * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
113 static void remap_planar(RemapContext *s, const AVFrame *in,
114 const AVFrame *xin, const AVFrame *yin,
117 const int xlinesize = xin->linesize[0] / 2;
118 const int ylinesize = yin->linesize[0] / 2;
121 for (plane = 0; plane < s->nb_planes ; plane++) {
122 uint8_t *dst = out->data[plane];
123 const int dlinesize = out->linesize[plane];
124 const uint8_t *src = in->data[plane];
125 const int slinesize = in->linesize[plane];
126 const uint16_t *xmap = (const uint16_t *)xin->data[0];
127 const uint16_t *ymap = (const uint16_t *)yin->data[0];
129 for (y = 0; y < out->height; y++) {
130 for (x = 0; x < out->width; x++) {
131 if (ymap[x] < in->height && xmap[x] < in->width) {
132 dst[x] = src[ymap[x] * slinesize + xmap[x]];
145 * remap_packed algorithm expects pixels with both padded bits (step) and
146 * number of components correctly set.
147 * pixels are copied from source to target using :
148 * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
150 static void remap_packed(RemapContext *s, const AVFrame *in,
151 const AVFrame *xin, const AVFrame *yin,
154 uint8_t *dst = out->data[0];
155 const uint8_t *src = in->data[0];
156 const int dlinesize = out->linesize[0];
157 const int slinesize = in->linesize[0];
158 const int xlinesize = xin->linesize[0] / 2;
159 const int ylinesize = yin->linesize[0] / 2;
160 const uint16_t *xmap = (const uint16_t *)xin->data[0];
161 const uint16_t *ymap = (const uint16_t *)yin->data[0];
162 const int step = s->step;
165 for (y = 0; y < out->height; y++) {
166 for (x = 0; x < out->width; x++) {
167 for (c = 0; c < s->nb_components; c++) {
168 if (ymap[x] < in->height && xmap[x] < in->width) {
169 dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
171 dst[x * step + c] = 0;
181 static int config_input(AVFilterLink *inlink)
183 AVFilterContext *ctx = inlink->dst;
184 RemapContext *s = ctx->priv;
185 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
187 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
188 s->nb_components = desc->nb_components;
190 if (s->nb_planes > 1 || s->nb_components == 1) {
191 s->remap = remap_planar;
193 s->remap = remap_packed;
196 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
200 static int process_frame(FFFrameSync *fs)
202 AVFilterContext *ctx = fs->parent;
203 RemapContext *s = fs->opaque;
204 AVFilterLink *outlink = ctx->outputs[0];
205 AVFrame *out, *in, *xpic, *ypic;
208 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
209 (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
210 (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
213 if (ctx->is_disabled) {
214 out = av_frame_clone(in);
216 return AVERROR(ENOMEM);
218 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
220 return AVERROR(ENOMEM);
221 av_frame_copy_props(out, in);
223 s->remap(s, in, xpic, ypic, out);
225 out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
227 return ff_filter_frame(outlink, out);
230 static int config_output(AVFilterLink *outlink)
232 AVFilterContext *ctx = outlink->src;
233 RemapContext *s = ctx->priv;
234 AVFilterLink *srclink = ctx->inputs[0];
235 AVFilterLink *xlink = ctx->inputs[1];
236 AVFilterLink *ylink = ctx->inputs[2];
240 if (xlink->w != ylink->w || xlink->h != ylink->h) {
241 av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
242 "(size %dx%d) do not match the corresponding "
243 "third input link %s parameters (%dx%d)\n",
244 ctx->input_pads[1].name, xlink->w, xlink->h,
245 ctx->input_pads[2].name, ylink->w, ylink->h);
246 return AVERROR(EINVAL);
249 outlink->w = xlink->w;
250 outlink->h = xlink->h;
251 outlink->time_base = srclink->time_base;
252 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
253 outlink->frame_rate = srclink->frame_rate;
255 ret = ff_framesync_init(&s->fs, ctx, 3);
260 in[0].time_base = srclink->time_base;
261 in[1].time_base = xlink->time_base;
262 in[2].time_base = ylink->time_base;
264 in[0].before = EXT_STOP;
265 in[0].after = EXT_STOP;
267 in[1].before = EXT_NULL;
268 in[1].after = EXT_INFINITY;
270 in[2].before = EXT_NULL;
271 in[2].after = EXT_INFINITY;
273 s->fs.on_event = process_frame;
275 return ff_framesync_configure(&s->fs);
278 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
280 RemapContext *s = inlink->dst->priv;
281 return ff_framesync_filter_frame(&s->fs, inlink, buf);
284 static int request_frame(AVFilterLink *outlink)
286 RemapContext *s = outlink->src->priv;
287 return ff_framesync_request_frame(&s->fs, outlink);
290 static av_cold void uninit(AVFilterContext *ctx)
292 RemapContext *s = ctx->priv;
294 ff_framesync_uninit(&s->fs);
297 static const AVFilterPad remap_inputs[] = {
300 .type = AVMEDIA_TYPE_VIDEO,
301 .filter_frame = filter_frame,
302 .config_props = config_input,
306 .type = AVMEDIA_TYPE_VIDEO,
307 .filter_frame = filter_frame,
311 .type = AVMEDIA_TYPE_VIDEO,
312 .filter_frame = filter_frame,
317 static const AVFilterPad remap_outputs[] = {
320 .type = AVMEDIA_TYPE_VIDEO,
321 .config_props = config_output,
322 .request_frame = request_frame,
327 AVFilter ff_vf_remap = {
329 .description = NULL_IF_CONFIG_SMALL("Remap pixels"),
330 .priv_size = sizeof(RemapContext),
332 .query_formats = query_formats,
333 .inputs = remap_inputs,
334 .outputs = remap_outputs,
335 .priv_class = &remap_class,
336 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,