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,
88 static const enum AVPixelFormat map_fmts[] = {
92 AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
95 if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
96 !(map_formats = ff_make_format_list(map_fmts))) {
97 ret = AVERROR(ENOMEM);
100 if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
101 (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
102 (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
103 (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
108 av_freep(&pix_formats->formats);
109 av_freep(&pix_formats);
111 av_freep(&map_formats->formats);
112 av_freep(&map_formats);
117 * remap_planar algorithm expects planes of same size
118 * pixels are copied from source to target using :
119 * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
121 static void remap_planar(RemapContext *s, const AVFrame *in,
122 const AVFrame *xin, const AVFrame *yin,
125 const int xlinesize = xin->linesize[0] / 2;
126 const int ylinesize = yin->linesize[0] / 2;
129 for (plane = 0; plane < s->nb_planes ; plane++) {
130 uint8_t *dst = out->data[plane];
131 const int dlinesize = out->linesize[plane];
132 const uint8_t *src = in->data[plane];
133 const int slinesize = in->linesize[plane];
134 const uint16_t *xmap = (const uint16_t *)xin->data[0];
135 const uint16_t *ymap = (const uint16_t *)yin->data[0];
137 for (y = 0; y < out->height; y++) {
138 for (x = 0; x < out->width; x++) {
139 if (ymap[x] < in->height && xmap[x] < in->width) {
140 dst[x] = src[ymap[x] * slinesize + xmap[x]];
152 static void remap_planar16(RemapContext *s, const AVFrame *in,
153 const AVFrame *xin, const AVFrame *yin,
156 const int xlinesize = xin->linesize[0] / 2;
157 const int ylinesize = yin->linesize[0] / 2;
160 for (plane = 0; plane < s->nb_planes ; plane++) {
161 uint16_t *dst = (uint16_t *)out->data[plane];
162 const int dlinesize = out->linesize[plane] / 2;
163 const uint16_t *src = (const uint16_t *)in->data[plane];
164 const int slinesize = in->linesize[plane] / 2;
165 const uint16_t *xmap = (const uint16_t *)xin->data[0];
166 const uint16_t *ymap = (const uint16_t *)yin->data[0];
168 for (y = 0; y < out->height; y++) {
169 for (x = 0; x < out->width; x++) {
170 if (ymap[x] < in->height && xmap[x] < in->width) {
171 dst[x] = src[ymap[x] * slinesize + xmap[x]];
184 * remap_packed algorithm expects pixels with both padded bits (step) and
185 * number of components correctly set.
186 * pixels are copied from source to target using :
187 * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
189 static void remap_packed(RemapContext *s, const AVFrame *in,
190 const AVFrame *xin, const AVFrame *yin,
193 uint8_t *dst = out->data[0];
194 const uint8_t *src = in->data[0];
195 const int dlinesize = out->linesize[0];
196 const int slinesize = in->linesize[0];
197 const int xlinesize = xin->linesize[0] / 2;
198 const int ylinesize = yin->linesize[0] / 2;
199 const uint16_t *xmap = (const uint16_t *)xin->data[0];
200 const uint16_t *ymap = (const uint16_t *)yin->data[0];
201 const int step = s->step;
204 for (y = 0; y < out->height; y++) {
205 for (x = 0; x < out->width; x++) {
206 for (c = 0; c < s->nb_components; c++) {
207 if (ymap[x] < in->height && xmap[x] < in->width) {
208 dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
210 dst[x * step + c] = 0;
220 static void remap_packed16(RemapContext *s, const AVFrame *in,
221 const AVFrame *xin, const AVFrame *yin,
224 uint16_t *dst = (uint16_t *)out->data[0];
225 const uint16_t *src = (const uint16_t *)in->data[0];
226 const int dlinesize = out->linesize[0] / 2;
227 const int slinesize = in->linesize[0] / 2;
228 const int xlinesize = xin->linesize[0] / 2;
229 const int ylinesize = yin->linesize[0] / 2;
230 const uint16_t *xmap = (const uint16_t *)xin->data[0];
231 const uint16_t *ymap = (const uint16_t *)yin->data[0];
232 const int step = s->step / 2;
235 for (y = 0; y < out->height; y++) {
236 for (x = 0; x < out->width; x++) {
237 for (c = 0; c < s->nb_components; c++) {
238 if (ymap[x] < in->height && xmap[x] < in->width) {
239 dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
241 dst[x * step + c] = 0;
251 static int config_input(AVFilterLink *inlink)
253 AVFilterContext *ctx = inlink->dst;
254 RemapContext *s = ctx->priv;
255 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
257 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
258 s->nb_components = desc->nb_components;
260 if (desc->comp[0].depth == 8) {
261 if (s->nb_planes > 1 || s->nb_components == 1) {
262 s->remap = remap_planar;
264 s->remap = remap_packed;
267 if (s->nb_planes > 1 || s->nb_components == 1) {
268 s->remap = remap_planar16;
270 s->remap = remap_packed16;
274 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
278 static int process_frame(FFFrameSync *fs)
280 AVFilterContext *ctx = fs->parent;
281 RemapContext *s = fs->opaque;
282 AVFilterLink *outlink = ctx->outputs[0];
283 AVFrame *out, *in, *xpic, *ypic;
286 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
287 (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
288 (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
291 if (ctx->is_disabled) {
292 out = av_frame_clone(in);
294 return AVERROR(ENOMEM);
296 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
298 return AVERROR(ENOMEM);
299 av_frame_copy_props(out, in);
301 s->remap(s, in, xpic, ypic, out);
303 out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
305 return ff_filter_frame(outlink, out);
308 static int config_output(AVFilterLink *outlink)
310 AVFilterContext *ctx = outlink->src;
311 RemapContext *s = ctx->priv;
312 AVFilterLink *srclink = ctx->inputs[0];
313 AVFilterLink *xlink = ctx->inputs[1];
314 AVFilterLink *ylink = ctx->inputs[2];
318 if (xlink->w != ylink->w || xlink->h != ylink->h) {
319 av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
320 "(size %dx%d) do not match the corresponding "
321 "third input link %s parameters (%dx%d)\n",
322 ctx->input_pads[1].name, xlink->w, xlink->h,
323 ctx->input_pads[2].name, ylink->w, ylink->h);
324 return AVERROR(EINVAL);
327 outlink->w = xlink->w;
328 outlink->h = xlink->h;
329 outlink->time_base = srclink->time_base;
330 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
331 outlink->frame_rate = srclink->frame_rate;
333 ret = ff_framesync_init(&s->fs, ctx, 3);
338 in[0].time_base = srclink->time_base;
339 in[1].time_base = xlink->time_base;
340 in[2].time_base = ylink->time_base;
342 in[0].before = EXT_STOP;
343 in[0].after = EXT_STOP;
345 in[1].before = EXT_NULL;
346 in[1].after = EXT_INFINITY;
348 in[2].before = EXT_NULL;
349 in[2].after = EXT_INFINITY;
351 s->fs.on_event = process_frame;
353 return ff_framesync_configure(&s->fs);
356 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
358 RemapContext *s = inlink->dst->priv;
359 return ff_framesync_filter_frame(&s->fs, inlink, buf);
362 static int request_frame(AVFilterLink *outlink)
364 RemapContext *s = outlink->src->priv;
365 return ff_framesync_request_frame(&s->fs, outlink);
368 static av_cold void uninit(AVFilterContext *ctx)
370 RemapContext *s = ctx->priv;
372 ff_framesync_uninit(&s->fs);
375 static const AVFilterPad remap_inputs[] = {
378 .type = AVMEDIA_TYPE_VIDEO,
379 .filter_frame = filter_frame,
380 .config_props = config_input,
384 .type = AVMEDIA_TYPE_VIDEO,
385 .filter_frame = filter_frame,
389 .type = AVMEDIA_TYPE_VIDEO,
390 .filter_frame = filter_frame,
395 static const AVFilterPad remap_outputs[] = {
398 .type = AVMEDIA_TYPE_VIDEO,
399 .config_props = config_output,
400 .request_frame = request_frame,
405 AVFilter ff_vf_remap = {
407 .description = NULL_IF_CONFIG_SMALL("Remap pixels."),
408 .priv_size = sizeof(RemapContext),
410 .query_formats = query_formats,
411 .inputs = remap_inputs,
412 .outputs = remap_outputs,
413 .priv_class = &remap_class,
414 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,