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.

413 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. pix_formats = ff_make_format_list(s->format ? gray_pix_fmts : pix_fmts);
  107. if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->outcfg.formats)) < 0 ||
  108. (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->incfg.formats)) < 0)
  109. return ret;
  110. map_formats = ff_make_format_list(map_fmts);
  111. if ((ret = ff_formats_ref(map_formats, &ctx->inputs[1]->outcfg.formats)) < 0)
  112. return ret;
  113. return ff_formats_ref(map_formats, &ctx->inputs[2]->outcfg.formats);
  114. }
  115. /**
  116. * remap_planar algorithm expects planes of same size
  117. * pixels are copied from source to target using :
  118. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  119. */
  120. #define DEFINE_REMAP_PLANAR_FUNC(name, bits, div) \
  121. static int remap_planar##bits##_##name##_slice(AVFilterContext *ctx, void *arg, \
  122. int jobnr, int nb_jobs) \
  123. { \
  124. RemapContext *s = ctx->priv; \
  125. const ThreadData *td = arg; \
  126. const AVFrame *in = td->in; \
  127. const AVFrame *xin = td->xin; \
  128. const AVFrame *yin = td->yin; \
  129. const AVFrame *out = td->out; \
  130. const int slice_start = (out->height * jobnr ) / nb_jobs; \
  131. const int slice_end = (out->height * (jobnr+1)) / nb_jobs; \
  132. const int xlinesize = xin->linesize[0] / 2; \
  133. const int ylinesize = yin->linesize[0] / 2; \
  134. int x , y, plane; \
  135. \
  136. for (plane = 0; plane < td->nb_planes ; plane++) { \
  137. const int dlinesize = out->linesize[plane] / div; \
  138. const uint##bits##_t *src = (const uint##bits##_t *)in->data[plane]; \
  139. uint##bits##_t *dst = (uint##bits##_t *)out->data[plane] + slice_start * dlinesize; \
  140. const int slinesize = in->linesize[plane] / div; \
  141. const uint16_t *xmap = (const uint16_t *)xin->data[0] + slice_start * xlinesize; \
  142. const uint16_t *ymap = (const uint16_t *)yin->data[0] + slice_start * ylinesize; \
  143. const int color = s->fill_color[plane]; \
  144. \
  145. for (y = slice_start; y < slice_end; y++) { \
  146. for (x = 0; x < out->width; x++) { \
  147. if (ymap[x] < in->height && xmap[x] < in->width) { \
  148. dst[x] = src[ymap[x] * slinesize + xmap[x]]; \
  149. } else { \
  150. dst[x] = color; \
  151. } \
  152. } \
  153. dst += dlinesize; \
  154. xmap += xlinesize; \
  155. ymap += ylinesize; \
  156. } \
  157. } \
  158. \
  159. return 0; \
  160. }
  161. DEFINE_REMAP_PLANAR_FUNC(nearest, 8, 1)
  162. DEFINE_REMAP_PLANAR_FUNC(nearest, 16, 2)
  163. /**
  164. * remap_packed algorithm expects pixels with both padded bits (step) and
  165. * number of components correctly set.
  166. * pixels are copied from source to target using :
  167. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  168. */
  169. #define DEFINE_REMAP_PACKED_FUNC(name, bits, div) \
  170. static int remap_packed##bits##_##name##_slice(AVFilterContext *ctx, void *arg, \
  171. int jobnr, int nb_jobs) \
  172. { \
  173. RemapContext *s = ctx->priv; \
  174. const ThreadData *td = arg; \
  175. const AVFrame *in = td->in; \
  176. const AVFrame *xin = td->xin; \
  177. const AVFrame *yin = td->yin; \
  178. const AVFrame *out = td->out; \
  179. const int slice_start = (out->height * jobnr ) / nb_jobs; \
  180. const int slice_end = (out->height * (jobnr+1)) / nb_jobs; \
  181. const int dlinesize = out->linesize[0] / div; \
  182. const int slinesize = in->linesize[0] / div; \
  183. const int xlinesize = xin->linesize[0] / 2; \
  184. const int ylinesize = yin->linesize[0] / 2; \
  185. const uint##bits##_t *src = (const uint##bits##_t *)in->data[0]; \
  186. uint##bits##_t *dst = (uint##bits##_t *)out->data[0] + slice_start * dlinesize; \
  187. const uint16_t *xmap = (const uint16_t *)xin->data[0] + slice_start * xlinesize; \
  188. const uint16_t *ymap = (const uint16_t *)yin->data[0] + slice_start * ylinesize; \
  189. const int step = td->step / div; \
  190. int c, x, y; \
  191. \
  192. for (y = slice_start; y < slice_end; y++) { \
  193. for (x = 0; x < out->width; x++) { \
  194. for (c = 0; c < td->nb_components; c++) { \
  195. if (ymap[x] < in->height && xmap[x] < in->width) { \
  196. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c]; \
  197. } else { \
  198. dst[x * step + c] = s->fill_color[c]; \
  199. } \
  200. } \
  201. } \
  202. dst += dlinesize; \
  203. xmap += xlinesize; \
  204. ymap += ylinesize; \
  205. } \
  206. \
  207. return 0; \
  208. }
  209. DEFINE_REMAP_PACKED_FUNC(nearest, 8, 1)
  210. DEFINE_REMAP_PACKED_FUNC(nearest, 16, 2)
  211. static int config_input(AVFilterLink *inlink)
  212. {
  213. AVFilterContext *ctx = inlink->dst;
  214. RemapContext *s = ctx->priv;
  215. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  216. int depth = desc->comp[0].depth;
  217. int is_rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
  218. int factor = 1 << (depth - 8);
  219. uint8_t rgba_map[4];
  220. ff_fill_rgba_map(rgba_map, inlink->format);
  221. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  222. s->nb_components = desc->nb_components;
  223. if (is_rgb) {
  224. s->fill_color[rgba_map[0]] = s->fill_rgba[0] * factor;
  225. s->fill_color[rgba_map[1]] = s->fill_rgba[1] * factor;
  226. s->fill_color[rgba_map[2]] = s->fill_rgba[2] * factor;
  227. s->fill_color[rgba_map[3]] = s->fill_rgba[3] * factor;
  228. } else {
  229. s->fill_color[0] = RGB_TO_Y_BT709(s->fill_rgba[0], s->fill_rgba[1], s->fill_rgba[2]) * factor;
  230. s->fill_color[1] = RGB_TO_U_BT709(s->fill_rgba[0], s->fill_rgba[1], s->fill_rgba[2], 0) * factor;
  231. s->fill_color[2] = RGB_TO_V_BT709(s->fill_rgba[0], s->fill_rgba[1], s->fill_rgba[2], 0) * factor;
  232. s->fill_color[3] = s->fill_rgba[3] * factor;
  233. }
  234. if (depth == 8) {
  235. if (s->nb_planes > 1 || s->nb_components == 1) {
  236. s->remap_slice = remap_planar8_nearest_slice;
  237. } else {
  238. s->remap_slice = remap_packed8_nearest_slice;
  239. }
  240. } else {
  241. if (s->nb_planes > 1 || s->nb_components == 1) {
  242. s->remap_slice = remap_planar16_nearest_slice;
  243. } else {
  244. s->remap_slice = remap_packed16_nearest_slice;
  245. }
  246. }
  247. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  248. return 0;
  249. }
  250. static int process_frame(FFFrameSync *fs)
  251. {
  252. AVFilterContext *ctx = fs->parent;
  253. RemapContext *s = fs->opaque;
  254. AVFilterLink *outlink = ctx->outputs[0];
  255. AVFrame *out, *in, *xpic, *ypic;
  256. int ret;
  257. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  258. (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
  259. (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
  260. return ret;
  261. if (ctx->is_disabled) {
  262. out = av_frame_clone(in);
  263. if (!out)
  264. return AVERROR(ENOMEM);
  265. } else {
  266. ThreadData td;
  267. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  268. if (!out)
  269. return AVERROR(ENOMEM);
  270. av_frame_copy_props(out, in);
  271. td.in = in;
  272. td.xin = xpic;
  273. td.yin = ypic;
  274. td.out = out;
  275. td.nb_planes = s->nb_planes;
  276. td.nb_components = s->nb_components;
  277. td.step = s->step;
  278. ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  279. }
  280. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  281. return ff_filter_frame(outlink, out);
  282. }
  283. static int config_output(AVFilterLink *outlink)
  284. {
  285. AVFilterContext *ctx = outlink->src;
  286. RemapContext *s = ctx->priv;
  287. AVFilterLink *srclink = ctx->inputs[0];
  288. AVFilterLink *xlink = ctx->inputs[1];
  289. AVFilterLink *ylink = ctx->inputs[2];
  290. FFFrameSyncIn *in;
  291. int ret;
  292. if (xlink->w != ylink->w || xlink->h != ylink->h) {
  293. av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
  294. "(size %dx%d) do not match the corresponding "
  295. "third input link %s parameters (%dx%d)\n",
  296. ctx->input_pads[1].name, xlink->w, xlink->h,
  297. ctx->input_pads[2].name, ylink->w, ylink->h);
  298. return AVERROR(EINVAL);
  299. }
  300. outlink->w = xlink->w;
  301. outlink->h = xlink->h;
  302. outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
  303. outlink->frame_rate = srclink->frame_rate;
  304. ret = ff_framesync_init(&s->fs, ctx, 3);
  305. if (ret < 0)
  306. return ret;
  307. in = s->fs.in;
  308. in[0].time_base = srclink->time_base;
  309. in[1].time_base = xlink->time_base;
  310. in[2].time_base = ylink->time_base;
  311. in[0].sync = 2;
  312. in[0].before = EXT_STOP;
  313. in[0].after = EXT_STOP;
  314. in[1].sync = 1;
  315. in[1].before = EXT_NULL;
  316. in[1].after = EXT_INFINITY;
  317. in[2].sync = 1;
  318. in[2].before = EXT_NULL;
  319. in[2].after = EXT_INFINITY;
  320. s->fs.opaque = s;
  321. s->fs.on_event = process_frame;
  322. ret = ff_framesync_configure(&s->fs);
  323. outlink->time_base = s->fs.time_base;
  324. return ret;
  325. }
  326. static int activate(AVFilterContext *ctx)
  327. {
  328. RemapContext *s = ctx->priv;
  329. return ff_framesync_activate(&s->fs);
  330. }
  331. static av_cold void uninit(AVFilterContext *ctx)
  332. {
  333. RemapContext *s = ctx->priv;
  334. ff_framesync_uninit(&s->fs);
  335. }
  336. static const AVFilterPad remap_inputs[] = {
  337. {
  338. .name = "source",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .config_props = config_input,
  341. },
  342. {
  343. .name = "xmap",
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. },
  346. {
  347. .name = "ymap",
  348. .type = AVMEDIA_TYPE_VIDEO,
  349. },
  350. { NULL }
  351. };
  352. static const AVFilterPad remap_outputs[] = {
  353. {
  354. .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .config_props = config_output,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_vf_remap = {
  361. .name = "remap",
  362. .description = NULL_IF_CONFIG_SMALL("Remap pixels."),
  363. .priv_size = sizeof(RemapContext),
  364. .uninit = uninit,
  365. .query_formats = query_formats,
  366. .activate = activate,
  367. .inputs = remap_inputs,
  368. .outputs = remap_outputs,
  369. .priv_class = &remap_class,
  370. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  371. };