vf_remap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 format;
  48. int nb_planes;
  49. int nb_components;
  50. int step;
  51. FFFrameSync fs;
  52. int (*remap_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  53. } RemapContext;
  54. #define OFFSET(x) offsetof(RemapContext, x)
  55. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  56. static const AVOption remap_options[] = {
  57. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "format" },
  58. { "color", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "format" },
  59. { "gray", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "format" },
  60. { NULL }
  61. };
  62. AVFILTER_DEFINE_CLASS(remap);
  63. typedef struct ThreadData {
  64. AVFrame *in, *xin, *yin, *out;
  65. int nb_planes;
  66. int nb_components;
  67. int step;
  68. } ThreadData;
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. RemapContext *s = ctx->priv;
  72. static const enum AVPixelFormat pix_fmts[] = {
  73. AV_PIX_FMT_YUVA444P,
  74. AV_PIX_FMT_YUV444P,
  75. AV_PIX_FMT_YUVJ444P,
  76. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  77. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  78. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  79. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  80. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
  81. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
  82. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  83. AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  84. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  85. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  86. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  87. AV_PIX_FMT_NONE
  88. };
  89. static const enum AVPixelFormat gray_pix_fmts[] = {
  90. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  91. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
  92. AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  93. AV_PIX_FMT_NONE
  94. };
  95. static const enum AVPixelFormat map_fmts[] = {
  96. AV_PIX_FMT_GRAY16,
  97. AV_PIX_FMT_NONE
  98. };
  99. AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
  100. int ret;
  101. if (!(pix_formats = ff_make_format_list(s->format ? gray_pix_fmts : pix_fmts)) ||
  102. !(map_formats = ff_make_format_list(map_fmts))) {
  103. ret = AVERROR(ENOMEM);
  104. goto fail;
  105. }
  106. if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
  107. (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  108. (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
  109. (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
  110. goto fail;
  111. return 0;
  112. fail:
  113. if (pix_formats)
  114. av_freep(&pix_formats->formats);
  115. av_freep(&pix_formats);
  116. if (map_formats)
  117. av_freep(&map_formats->formats);
  118. av_freep(&map_formats);
  119. return ret;
  120. }
  121. /**
  122. * remap_planar algorithm expects planes of same size
  123. * pixels are copied from source to target using :
  124. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  125. */
  126. #define DEFINE_REMAP_PLANAR_FUNC(name, bits, div) \
  127. static int remap_planar##bits##_##name##_slice(AVFilterContext *ctx, void *arg, \
  128. int jobnr, int nb_jobs) \
  129. { \
  130. const ThreadData *td = (ThreadData*)arg; \
  131. const AVFrame *in = td->in; \
  132. const AVFrame *xin = td->xin; \
  133. const AVFrame *yin = td->yin; \
  134. const AVFrame *out = td->out; \
  135. const int slice_start = (out->height * jobnr ) / nb_jobs; \
  136. const int slice_end = (out->height * (jobnr+1)) / nb_jobs; \
  137. const int xlinesize = xin->linesize[0] / 2; \
  138. const int ylinesize = yin->linesize[0] / 2; \
  139. int x , y, plane; \
  140. \
  141. for (plane = 0; plane < td->nb_planes ; plane++) { \
  142. const int dlinesize = out->linesize[plane] / div; \
  143. const uint##bits##_t *src = (const uint##bits##_t *)in->data[plane]; \
  144. uint##bits##_t *dst = (uint##bits##_t *)out->data[plane] + slice_start * dlinesize; \
  145. const int slinesize = in->linesize[plane] / div; \
  146. const uint16_t *xmap = (const uint16_t *)xin->data[0] + slice_start * xlinesize; \
  147. const uint16_t *ymap = (const uint16_t *)yin->data[0] + slice_start * ylinesize; \
  148. \
  149. for (y = slice_start; y < slice_end; y++) { \
  150. for (x = 0; x < out->width; x++) { \
  151. if (ymap[x] < in->height && xmap[x] < in->width) { \
  152. dst[x] = src[ymap[x] * slinesize + xmap[x]]; \
  153. } else { \
  154. dst[x] = 0; \
  155. } \
  156. } \
  157. dst += dlinesize; \
  158. xmap += xlinesize; \
  159. ymap += ylinesize; \
  160. } \
  161. } \
  162. \
  163. return 0; \
  164. }
  165. DEFINE_REMAP_PLANAR_FUNC(nearest, 8, 1)
  166. DEFINE_REMAP_PLANAR_FUNC(nearest, 16, 2)
  167. /**
  168. * remap_packed algorithm expects pixels with both padded bits (step) and
  169. * number of components correctly set.
  170. * pixels are copied from source to target using :
  171. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  172. */
  173. #define DEFINE_REMAP_PACKED_FUNC(name, bits, div) \
  174. static int remap_packed##bits##_##name##_slice(AVFilterContext *ctx, void *arg, \
  175. int jobnr, int nb_jobs) \
  176. { \
  177. const ThreadData *td = (ThreadData*)arg; \
  178. const AVFrame *in = td->in; \
  179. const AVFrame *xin = td->xin; \
  180. const AVFrame *yin = td->yin; \
  181. const AVFrame *out = td->out; \
  182. const int slice_start = (out->height * jobnr ) / nb_jobs; \
  183. const int slice_end = (out->height * (jobnr+1)) / nb_jobs; \
  184. const int dlinesize = out->linesize[0] / div; \
  185. const int slinesize = in->linesize[0] / div; \
  186. const int xlinesize = xin->linesize[0] / 2; \
  187. const int ylinesize = yin->linesize[0] / 2; \
  188. const uint##bits##_t *src = (const uint##bits##_t *)in->data[0]; \
  189. uint##bits##_t *dst = (uint##bits##_t *)out->data[0] + slice_start * dlinesize; \
  190. const uint16_t *xmap = (const uint16_t *)xin->data[0] + slice_start * xlinesize; \
  191. const uint16_t *ymap = (const uint16_t *)yin->data[0] + slice_start * ylinesize; \
  192. const int step = td->step / div; \
  193. int c, x, y; \
  194. \
  195. for (y = slice_start; y < slice_end; y++) { \
  196. for (x = 0; x < out->width; x++) { \
  197. for (c = 0; c < td->nb_components; c++) { \
  198. if (ymap[x] < in->height && xmap[x] < in->width) { \
  199. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c]; \
  200. } else { \
  201. dst[x * step + c] = 0; \
  202. } \
  203. } \
  204. } \
  205. dst += dlinesize; \
  206. xmap += xlinesize; \
  207. ymap += ylinesize; \
  208. } \
  209. \
  210. return 0; \
  211. }
  212. DEFINE_REMAP_PACKED_FUNC(nearest, 8, 1)
  213. DEFINE_REMAP_PACKED_FUNC(nearest, 16, 2)
  214. static int config_input(AVFilterLink *inlink)
  215. {
  216. AVFilterContext *ctx = inlink->dst;
  217. RemapContext *s = ctx->priv;
  218. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  219. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  220. s->nb_components = desc->nb_components;
  221. if (desc->comp[0].depth == 8) {
  222. if (s->nb_planes > 1 || s->nb_components == 1) {
  223. s->remap_slice = remap_planar8_nearest_slice;
  224. } else {
  225. s->remap_slice = remap_packed8_nearest_slice;
  226. }
  227. } else {
  228. if (s->nb_planes > 1 || s->nb_components == 1) {
  229. s->remap_slice = remap_planar16_nearest_slice;
  230. } else {
  231. s->remap_slice = remap_packed16_nearest_slice;
  232. }
  233. }
  234. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  235. return 0;
  236. }
  237. static int process_frame(FFFrameSync *fs)
  238. {
  239. AVFilterContext *ctx = fs->parent;
  240. RemapContext *s = fs->opaque;
  241. AVFilterLink *outlink = ctx->outputs[0];
  242. AVFrame *out, *in, *xpic, *ypic;
  243. int ret;
  244. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  245. (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
  246. (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
  247. return ret;
  248. if (ctx->is_disabled) {
  249. out = av_frame_clone(in);
  250. if (!out)
  251. return AVERROR(ENOMEM);
  252. } else {
  253. ThreadData td;
  254. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  255. if (!out)
  256. return AVERROR(ENOMEM);
  257. av_frame_copy_props(out, in);
  258. td.in = in;
  259. td.xin = xpic;
  260. td.yin = ypic;
  261. td.out = out;
  262. td.nb_planes = s->nb_planes;
  263. td.nb_components = s->nb_components;
  264. td.step = s->step;
  265. ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  266. }
  267. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  268. return ff_filter_frame(outlink, out);
  269. }
  270. static int config_output(AVFilterLink *outlink)
  271. {
  272. AVFilterContext *ctx = outlink->src;
  273. RemapContext *s = ctx->priv;
  274. AVFilterLink *srclink = ctx->inputs[0];
  275. AVFilterLink *xlink = ctx->inputs[1];
  276. AVFilterLink *ylink = ctx->inputs[2];
  277. FFFrameSyncIn *in;
  278. int ret;
  279. if (xlink->w != ylink->w || xlink->h != ylink->h) {
  280. av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
  281. "(size %dx%d) do not match the corresponding "
  282. "third input link %s parameters (%dx%d)\n",
  283. ctx->input_pads[1].name, xlink->w, xlink->h,
  284. ctx->input_pads[2].name, ylink->w, ylink->h);
  285. return AVERROR(EINVAL);
  286. }
  287. outlink->w = xlink->w;
  288. outlink->h = xlink->h;
  289. outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
  290. outlink->frame_rate = srclink->frame_rate;
  291. ret = ff_framesync_init(&s->fs, ctx, 3);
  292. if (ret < 0)
  293. return ret;
  294. in = s->fs.in;
  295. in[0].time_base = srclink->time_base;
  296. in[1].time_base = xlink->time_base;
  297. in[2].time_base = ylink->time_base;
  298. in[0].sync = 2;
  299. in[0].before = EXT_STOP;
  300. in[0].after = EXT_STOP;
  301. in[1].sync = 1;
  302. in[1].before = EXT_NULL;
  303. in[1].after = EXT_INFINITY;
  304. in[2].sync = 1;
  305. in[2].before = EXT_NULL;
  306. in[2].after = EXT_INFINITY;
  307. s->fs.opaque = s;
  308. s->fs.on_event = process_frame;
  309. ret = ff_framesync_configure(&s->fs);
  310. outlink->time_base = s->fs.time_base;
  311. return ret;
  312. }
  313. static int activate(AVFilterContext *ctx)
  314. {
  315. RemapContext *s = ctx->priv;
  316. return ff_framesync_activate(&s->fs);
  317. }
  318. static av_cold void uninit(AVFilterContext *ctx)
  319. {
  320. RemapContext *s = ctx->priv;
  321. ff_framesync_uninit(&s->fs);
  322. }
  323. static const AVFilterPad remap_inputs[] = {
  324. {
  325. .name = "source",
  326. .type = AVMEDIA_TYPE_VIDEO,
  327. .config_props = config_input,
  328. },
  329. {
  330. .name = "xmap",
  331. .type = AVMEDIA_TYPE_VIDEO,
  332. },
  333. {
  334. .name = "ymap",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. },
  337. { NULL }
  338. };
  339. static const AVFilterPad remap_outputs[] = {
  340. {
  341. .name = "default",
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. .config_props = config_output,
  344. },
  345. { NULL }
  346. };
  347. AVFilter ff_vf_remap = {
  348. .name = "remap",
  349. .description = NULL_IF_CONFIG_SMALL("Remap pixels."),
  350. .priv_size = sizeof(RemapContext),
  351. .uninit = uninit,
  352. .query_formats = query_formats,
  353. .activate = activate,
  354. .inputs = remap_inputs,
  355. .outputs = remap_outputs,
  356. .priv_class = &remap_class,
  357. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  358. };