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.

411 lines
13KB

  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_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  71. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
  72. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
  73. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  74. AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  75. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  76. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  77. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  78. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  79. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
  80. AV_PIX_FMT_GRAY16,
  81. AV_PIX_FMT_NONE
  82. };
  83. static const enum AVPixelFormat map_fmts[] = {
  84. AV_PIX_FMT_GRAY16,
  85. AV_PIX_FMT_NONE
  86. };
  87. AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
  88. int ret;
  89. if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
  90. !(map_formats = ff_make_format_list(map_fmts))) {
  91. ret = AVERROR(ENOMEM);
  92. goto fail;
  93. }
  94. if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
  95. (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  96. (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
  97. (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
  98. goto fail;
  99. return 0;
  100. fail:
  101. if (pix_formats)
  102. av_freep(&pix_formats->formats);
  103. av_freep(&pix_formats);
  104. if (map_formats)
  105. av_freep(&map_formats->formats);
  106. av_freep(&map_formats);
  107. return ret;
  108. }
  109. /**
  110. * remap_planar algorithm expects planes of same size
  111. * pixels are copied from source to target using :
  112. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  113. */
  114. static void remap_planar(RemapContext *s, const AVFrame *in,
  115. const AVFrame *xin, const AVFrame *yin,
  116. AVFrame *out)
  117. {
  118. const int xlinesize = xin->linesize[0] / 2;
  119. const int ylinesize = yin->linesize[0] / 2;
  120. int x , y, plane;
  121. for (plane = 0; plane < s->nb_planes ; plane++) {
  122. uint8_t *dst = out->data[plane];
  123. const int dlinesize = out->linesize[plane];
  124. const uint8_t *src = in->data[plane];
  125. const int slinesize = in->linesize[plane];
  126. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  127. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  128. for (y = 0; y < out->height; y++) {
  129. for (x = 0; x < out->width; x++) {
  130. if (ymap[x] < in->height && xmap[x] < in->width) {
  131. dst[x] = src[ymap[x] * slinesize + xmap[x]];
  132. } else {
  133. dst[x] = 0;
  134. }
  135. }
  136. dst += dlinesize;
  137. xmap += xlinesize;
  138. ymap += ylinesize;
  139. }
  140. }
  141. }
  142. static void remap_planar16(RemapContext *s, const AVFrame *in,
  143. const AVFrame *xin, const AVFrame *yin,
  144. AVFrame *out)
  145. {
  146. const int xlinesize = xin->linesize[0] / 2;
  147. const int ylinesize = yin->linesize[0] / 2;
  148. int x , y, plane;
  149. for (plane = 0; plane < s->nb_planes ; plane++) {
  150. uint16_t *dst = (uint16_t *)out->data[plane];
  151. const int dlinesize = out->linesize[plane] / 2;
  152. const uint16_t *src = (const uint16_t *)in->data[plane];
  153. const int slinesize = in->linesize[plane] / 2;
  154. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  155. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  156. for (y = 0; y < out->height; 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] = 0;
  162. }
  163. }
  164. dst += dlinesize;
  165. xmap += xlinesize;
  166. ymap += ylinesize;
  167. }
  168. }
  169. }
  170. /**
  171. * remap_packed algorithm expects pixels with both padded bits (step) and
  172. * number of components correctly set.
  173. * pixels are copied from source to target using :
  174. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  175. */
  176. static void remap_packed(RemapContext *s, const AVFrame *in,
  177. const AVFrame *xin, const AVFrame *yin,
  178. AVFrame *out)
  179. {
  180. uint8_t *dst = out->data[0];
  181. const uint8_t *src = in->data[0];
  182. const int dlinesize = out->linesize[0];
  183. const int slinesize = in->linesize[0];
  184. const int xlinesize = xin->linesize[0] / 2;
  185. const int ylinesize = yin->linesize[0] / 2;
  186. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  187. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  188. const int step = s->step;
  189. int c, x, y;
  190. for (y = 0; y < out->height; y++) {
  191. for (x = 0; x < out->width; x++) {
  192. for (c = 0; c < s->nb_components; c++) {
  193. if (ymap[x] < in->height && xmap[x] < in->width) {
  194. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
  195. } else {
  196. dst[x * step + c] = 0;
  197. }
  198. }
  199. }
  200. dst += dlinesize;
  201. xmap += xlinesize;
  202. ymap += ylinesize;
  203. }
  204. }
  205. static void remap_packed16(RemapContext *s, const AVFrame *in,
  206. const AVFrame *xin, const AVFrame *yin,
  207. AVFrame *out)
  208. {
  209. uint16_t *dst = (uint16_t *)out->data[0];
  210. const uint16_t *src = (const uint16_t *)in->data[0];
  211. const int dlinesize = out->linesize[0] / 2;
  212. const int slinesize = in->linesize[0] / 2;
  213. const int xlinesize = xin->linesize[0] / 2;
  214. const int ylinesize = yin->linesize[0] / 2;
  215. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  216. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  217. const int step = s->step / 2;
  218. int c, x, y;
  219. for (y = 0; y < out->height; y++) {
  220. for (x = 0; x < out->width; x++) {
  221. for (c = 0; c < s->nb_components; c++) {
  222. if (ymap[x] < in->height && xmap[x] < in->width) {
  223. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
  224. } else {
  225. dst[x * step + c] = 0;
  226. }
  227. }
  228. }
  229. dst += dlinesize;
  230. xmap += xlinesize;
  231. ymap += ylinesize;
  232. }
  233. }
  234. static int config_input(AVFilterLink *inlink)
  235. {
  236. AVFilterContext *ctx = inlink->dst;
  237. RemapContext *s = ctx->priv;
  238. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  239. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  240. s->nb_components = desc->nb_components;
  241. if (desc->comp[0].depth == 8) {
  242. if (s->nb_planes > 1 || s->nb_components == 1) {
  243. s->remap = remap_planar;
  244. } else {
  245. s->remap = remap_packed;
  246. }
  247. } else {
  248. if (s->nb_planes > 1 || s->nb_components == 1) {
  249. s->remap = remap_planar16;
  250. } else {
  251. s->remap = remap_packed16;
  252. }
  253. }
  254. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  255. return 0;
  256. }
  257. static int process_frame(FFFrameSync *fs)
  258. {
  259. AVFilterContext *ctx = fs->parent;
  260. RemapContext *s = fs->opaque;
  261. AVFilterLink *outlink = ctx->outputs[0];
  262. AVFrame *out, *in, *xpic, *ypic;
  263. int ret;
  264. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  265. (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
  266. (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
  267. return ret;
  268. if (ctx->is_disabled) {
  269. out = av_frame_clone(in);
  270. if (!out)
  271. return AVERROR(ENOMEM);
  272. } else {
  273. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  274. if (!out)
  275. return AVERROR(ENOMEM);
  276. av_frame_copy_props(out, in);
  277. s->remap(s, in, xpic, ypic, out);
  278. }
  279. out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
  280. return ff_filter_frame(outlink, out);
  281. }
  282. static int config_output(AVFilterLink *outlink)
  283. {
  284. AVFilterContext *ctx = outlink->src;
  285. RemapContext *s = ctx->priv;
  286. AVFilterLink *srclink = ctx->inputs[0];
  287. AVFilterLink *xlink = ctx->inputs[1];
  288. AVFilterLink *ylink = ctx->inputs[2];
  289. FFFrameSyncIn *in;
  290. int ret;
  291. if (xlink->w != ylink->w || xlink->h != ylink->h) {
  292. av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
  293. "(size %dx%d) do not match the corresponding "
  294. "third input link %s parameters (%dx%d)\n",
  295. ctx->input_pads[1].name, xlink->w, xlink->h,
  296. ctx->input_pads[2].name, ylink->w, ylink->h);
  297. return AVERROR(EINVAL);
  298. }
  299. outlink->w = xlink->w;
  300. outlink->h = xlink->h;
  301. outlink->time_base = srclink->time_base;
  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. return ff_framesync_configure(&s->fs);
  323. }
  324. static int activate(AVFilterContext *ctx)
  325. {
  326. RemapContext *s = ctx->priv;
  327. return ff_framesync_activate(&s->fs);
  328. }
  329. static av_cold void uninit(AVFilterContext *ctx)
  330. {
  331. RemapContext *s = ctx->priv;
  332. ff_framesync_uninit(&s->fs);
  333. }
  334. static const AVFilterPad remap_inputs[] = {
  335. {
  336. .name = "source",
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. .config_props = config_input,
  339. },
  340. {
  341. .name = "xmap",
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. },
  344. {
  345. .name = "ymap",
  346. .type = AVMEDIA_TYPE_VIDEO,
  347. },
  348. { NULL }
  349. };
  350. static const AVFilterPad remap_outputs[] = {
  351. {
  352. .name = "default",
  353. .type = AVMEDIA_TYPE_VIDEO,
  354. .config_props = config_output,
  355. },
  356. { NULL }
  357. };
  358. AVFilter ff_vf_remap = {
  359. .name = "remap",
  360. .description = NULL_IF_CONFIG_SMALL("Remap pixels."),
  361. .priv_size = sizeof(RemapContext),
  362. .uninit = uninit,
  363. .query_formats = query_formats,
  364. .activate = activate,
  365. .inputs = remap_inputs,
  366. .outputs = remap_outputs,
  367. .priv_class = &remap_class,
  368. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  369. };