You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
18KB

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