vf_remap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * @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. #include "libavutil/imgutils.h"
  38. #include "libavutil/pixdesc.h"
  39. #include "libavutil/opt.h"
  40. #include "avfilter.h"
  41. #include "formats.h"
  42. #include "framesync.h"
  43. #include "internal.h"
  44. #include "video.h"
  45. typedef struct RemapContext {
  46. const AVClass *class;
  47. int nb_planes;
  48. int nb_components;
  49. int step;
  50. FFFrameSync fs;
  51. void (*remap)(struct RemapContext *s, const AVFrame *in,
  52. const AVFrame *xin, const AVFrame *yin,
  53. AVFrame *out);
  54. } RemapContext;
  55. #define OFFSET(x) offsetof(RemapContext, x)
  56. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  57. static const AVOption remap_options[] = {
  58. { NULL }
  59. };
  60. AVFILTER_DEFINE_CLASS(remap);
  61. static int query_formats(AVFilterContext *ctx)
  62. {
  63. static const enum AVPixelFormat pix_fmts[] = {
  64. AV_PIX_FMT_YUVA444P,
  65. AV_PIX_FMT_YUV444P,
  66. AV_PIX_FMT_YUVJ444P,
  67. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  68. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  69. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  70. AV_PIX_FMT_NONE
  71. };
  72. static const enum AVPixelFormat map_fmts[] = {
  73. AV_PIX_FMT_GRAY16,
  74. AV_PIX_FMT_NONE
  75. };
  76. AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
  77. int ret;
  78. if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
  79. !(map_formats = ff_make_format_list(map_fmts))) {
  80. ret = AVERROR(ENOMEM);
  81. goto fail;
  82. }
  83. if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
  84. (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  85. (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
  86. (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
  87. goto fail;
  88. return 0;
  89. fail:
  90. if (pix_formats)
  91. av_freep(&pix_formats->formats);
  92. av_freep(&pix_formats);
  93. if (map_formats)
  94. av_freep(&map_formats->formats);
  95. av_freep(&map_formats);
  96. return ret;
  97. }
  98. /**
  99. * remap_planar algorithm expects planes of same size
  100. * pixels are copied from source to target using :
  101. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  102. */
  103. static void remap_planar(RemapContext *s, const AVFrame *in,
  104. const AVFrame *xin, const AVFrame *yin,
  105. AVFrame *out)
  106. {
  107. const int xlinesize = xin->linesize[0] / 2;
  108. const int ylinesize = yin->linesize[0] / 2;
  109. int x , y, plane;
  110. for (plane = 0; plane < s->nb_planes ; plane++) {
  111. uint8_t *dst = out->data[plane];
  112. const int dlinesize = out->linesize[plane];
  113. const uint8_t *src = in->data[plane];
  114. const int slinesize = in->linesize[plane];
  115. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  116. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  117. for (y = 0; y < out->height; y++) {
  118. for (x = 0; x < out->width; x++) {
  119. if (ymap[x] < in->height && xmap[x] < in->width) {
  120. dst[x] = src[ymap[x] * slinesize + xmap[x]];
  121. } else {
  122. dst[x] = 0;
  123. }
  124. }
  125. dst += dlinesize;
  126. xmap += xlinesize;
  127. ymap += ylinesize;
  128. }
  129. }
  130. }
  131. /**
  132. * remap_packed algorithm expects pixels with both padded bits (step) and
  133. * number of components correctly set.
  134. * pixels are copied from source to target using :
  135. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  136. */
  137. static void remap_packed(RemapContext *s, const AVFrame *in,
  138. const AVFrame *xin, const AVFrame *yin,
  139. AVFrame *out)
  140. {
  141. uint8_t *dst = out->data[0];
  142. const uint8_t *src = in->data[0];
  143. const int dlinesize = out->linesize[0];
  144. const int slinesize = in->linesize[0];
  145. const int xlinesize = xin->linesize[0] / 2;
  146. const int ylinesize = yin->linesize[0] / 2;
  147. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  148. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  149. const int step = s->step;
  150. int c, x, y;
  151. for (y = 0; y < out->height; y++) {
  152. for (x = 0; x < out->width; x++) {
  153. for (c = 0; c < s->nb_components; c++) {
  154. if (ymap[x] < in->height && xmap[x] < in->width) {
  155. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
  156. } else {
  157. dst[x * step + c] = 0;
  158. }
  159. }
  160. }
  161. dst += dlinesize;
  162. xmap += xlinesize;
  163. ymap += ylinesize;
  164. }
  165. }
  166. static int config_input(AVFilterLink *inlink)
  167. {
  168. AVFilterContext *ctx = inlink->dst;
  169. RemapContext *s = ctx->priv;
  170. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  171. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  172. s->nb_components = desc->nb_components;
  173. if (s->nb_planes > 1 || s->nb_components == 1) {
  174. s->remap = remap_planar;
  175. } else {
  176. s->remap = remap_packed;
  177. }
  178. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  179. return 0;
  180. }
  181. static int process_frame(FFFrameSync *fs)
  182. {
  183. AVFilterContext *ctx = fs->parent;
  184. RemapContext *s = fs->opaque;
  185. AVFilterLink *outlink = ctx->outputs[0];
  186. AVFrame *out, *in, *xpic, *ypic;
  187. int ret;
  188. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  189. (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
  190. (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
  191. return ret;
  192. if (ctx->is_disabled) {
  193. out = av_frame_clone(in);
  194. if (!out)
  195. return AVERROR(ENOMEM);
  196. } else {
  197. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  198. if (!out)
  199. return AVERROR(ENOMEM);
  200. av_frame_copy_props(out, in);
  201. s->remap(s, in, xpic, ypic, out);
  202. }
  203. out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
  204. return ff_filter_frame(outlink, out);
  205. }
  206. static int config_output(AVFilterLink *outlink)
  207. {
  208. AVFilterContext *ctx = outlink->src;
  209. RemapContext *s = ctx->priv;
  210. AVFilterLink *srclink = ctx->inputs[0];
  211. AVFilterLink *xlink = ctx->inputs[1];
  212. AVFilterLink *ylink = ctx->inputs[2];
  213. FFFrameSyncIn *in;
  214. int ret;
  215. if (xlink->w != ylink->w || xlink->h != ylink->h) {
  216. av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
  217. "(size %dx%d) do not match the corresponding "
  218. "third input link %s parameters (%dx%d)\n",
  219. ctx->input_pads[1].name, xlink->w, xlink->h,
  220. ctx->input_pads[2].name, ylink->w, ylink->h);
  221. return AVERROR(EINVAL);
  222. }
  223. outlink->w = xlink->w;
  224. outlink->h = xlink->h;
  225. outlink->time_base = srclink->time_base;
  226. outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
  227. outlink->frame_rate = srclink->frame_rate;
  228. ret = ff_framesync_init(&s->fs, ctx, 3);
  229. if (ret < 0)
  230. return ret;
  231. in = s->fs.in;
  232. in[0].time_base = srclink->time_base;
  233. in[1].time_base = xlink->time_base;
  234. in[2].time_base = ylink->time_base;
  235. in[0].sync = 2;
  236. in[0].before = EXT_STOP;
  237. in[0].after = EXT_STOP;
  238. in[1].sync = 1;
  239. in[1].before = EXT_NULL;
  240. in[1].after = EXT_INFINITY;
  241. in[2].sync = 1;
  242. in[2].before = EXT_NULL;
  243. in[2].after = EXT_INFINITY;
  244. s->fs.opaque = s;
  245. s->fs.on_event = process_frame;
  246. return ff_framesync_configure(&s->fs);
  247. }
  248. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  249. {
  250. RemapContext *s = inlink->dst->priv;
  251. return ff_framesync_filter_frame(&s->fs, inlink, buf);
  252. }
  253. static int request_frame(AVFilterLink *outlink)
  254. {
  255. RemapContext *s = outlink->src->priv;
  256. return ff_framesync_request_frame(&s->fs, outlink);
  257. }
  258. static av_cold void uninit(AVFilterContext *ctx)
  259. {
  260. RemapContext *s = ctx->priv;
  261. ff_framesync_uninit(&s->fs);
  262. }
  263. static const AVFilterPad remap_inputs[] = {
  264. {
  265. .name = "source",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. .filter_frame = filter_frame,
  268. .config_props = config_input,
  269. },
  270. {
  271. .name = "xmap",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .filter_frame = filter_frame,
  274. },
  275. {
  276. .name = "ymap",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .filter_frame = filter_frame,
  279. },
  280. { NULL }
  281. };
  282. static const AVFilterPad remap_outputs[] = {
  283. {
  284. .name = "default",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .config_props = config_output,
  287. .request_frame = request_frame,
  288. },
  289. { NULL }
  290. };
  291. AVFilter ff_vf_remap = {
  292. .name = "remap",
  293. .description = NULL_IF_CONFIG_SMALL("Remap pixels"),
  294. .priv_size = sizeof(RemapContext),
  295. .uninit = uninit,
  296. .query_formats = query_formats,
  297. .inputs = remap_inputs,
  298. .outputs = remap_outputs,
  299. .priv_class = &remap_class,
  300. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  301. };