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.

416 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_NONE
  79. };
  80. static const enum AVPixelFormat map_fmts[] = {
  81. AV_PIX_FMT_GRAY16,
  82. AV_PIX_FMT_NONE
  83. };
  84. AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
  85. int ret;
  86. if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
  87. !(map_formats = ff_make_format_list(map_fmts))) {
  88. ret = AVERROR(ENOMEM);
  89. goto fail;
  90. }
  91. if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
  92. (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
  93. (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
  94. (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
  95. goto fail;
  96. return 0;
  97. fail:
  98. if (pix_formats)
  99. av_freep(&pix_formats->formats);
  100. av_freep(&pix_formats);
  101. if (map_formats)
  102. av_freep(&map_formats->formats);
  103. av_freep(&map_formats);
  104. return ret;
  105. }
  106. /**
  107. * remap_planar algorithm expects planes of same size
  108. * pixels are copied from source to target using :
  109. * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
  110. */
  111. static void remap_planar(RemapContext *s, const AVFrame *in,
  112. const AVFrame *xin, const AVFrame *yin,
  113. AVFrame *out)
  114. {
  115. const int xlinesize = xin->linesize[0] / 2;
  116. const int ylinesize = yin->linesize[0] / 2;
  117. int x , y, plane;
  118. for (plane = 0; plane < s->nb_planes ; plane++) {
  119. uint8_t *dst = out->data[plane];
  120. const int dlinesize = out->linesize[plane];
  121. const uint8_t *src = in->data[plane];
  122. const int slinesize = in->linesize[plane];
  123. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  124. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  125. for (y = 0; y < out->height; y++) {
  126. for (x = 0; x < out->width; x++) {
  127. if (ymap[x] < in->height && xmap[x] < in->width) {
  128. dst[x] = src[ymap[x] * slinesize + xmap[x]];
  129. } else {
  130. dst[x] = 0;
  131. }
  132. }
  133. dst += dlinesize;
  134. xmap += xlinesize;
  135. ymap += ylinesize;
  136. }
  137. }
  138. }
  139. static void remap_planar16(RemapContext *s, const AVFrame *in,
  140. const AVFrame *xin, const AVFrame *yin,
  141. AVFrame *out)
  142. {
  143. const int xlinesize = xin->linesize[0] / 2;
  144. const int ylinesize = yin->linesize[0] / 2;
  145. int x , y, plane;
  146. for (plane = 0; plane < s->nb_planes ; plane++) {
  147. uint16_t *dst = (uint16_t *)out->data[plane];
  148. const int dlinesize = out->linesize[plane] / 2;
  149. const uint16_t *src = (const uint16_t *)in->data[plane];
  150. const int slinesize = in->linesize[plane] / 2;
  151. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  152. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  153. for (y = 0; y < out->height; y++) {
  154. for (x = 0; x < out->width; x++) {
  155. if (ymap[x] < in->height && xmap[x] < in->width) {
  156. dst[x] = src[ymap[x] * slinesize + xmap[x]];
  157. } else {
  158. dst[x] = 0;
  159. }
  160. }
  161. dst += dlinesize;
  162. xmap += xlinesize;
  163. ymap += ylinesize;
  164. }
  165. }
  166. }
  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. static void remap_packed(RemapContext *s, const AVFrame *in,
  174. const AVFrame *xin, const AVFrame *yin,
  175. AVFrame *out)
  176. {
  177. uint8_t *dst = out->data[0];
  178. const uint8_t *src = in->data[0];
  179. const int dlinesize = out->linesize[0];
  180. const int slinesize = in->linesize[0];
  181. const int xlinesize = xin->linesize[0] / 2;
  182. const int ylinesize = yin->linesize[0] / 2;
  183. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  184. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  185. const int step = s->step;
  186. int c, x, y;
  187. for (y = 0; y < out->height; y++) {
  188. for (x = 0; x < out->width; x++) {
  189. for (c = 0; c < s->nb_components; c++) {
  190. if (ymap[x] < in->height && xmap[x] < in->width) {
  191. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
  192. } else {
  193. dst[x * step + c] = 0;
  194. }
  195. }
  196. }
  197. dst += dlinesize;
  198. xmap += xlinesize;
  199. ymap += ylinesize;
  200. }
  201. }
  202. static void remap_packed16(RemapContext *s, const AVFrame *in,
  203. const AVFrame *xin, const AVFrame *yin,
  204. AVFrame *out)
  205. {
  206. uint16_t *dst = (uint16_t *)out->data[0];
  207. const uint16_t *src = (const uint16_t *)in->data[0];
  208. const int dlinesize = out->linesize[0] / 2;
  209. const int slinesize = in->linesize[0] / 2;
  210. const int xlinesize = xin->linesize[0] / 2;
  211. const int ylinesize = yin->linesize[0] / 2;
  212. const uint16_t *xmap = (const uint16_t *)xin->data[0];
  213. const uint16_t *ymap = (const uint16_t *)yin->data[0];
  214. const int step = s->step / 2;
  215. int c, x, y;
  216. for (y = 0; y < out->height; y++) {
  217. for (x = 0; x < out->width; x++) {
  218. for (c = 0; c < s->nb_components; c++) {
  219. if (ymap[x] < in->height && xmap[x] < in->width) {
  220. dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
  221. } else {
  222. dst[x * step + c] = 0;
  223. }
  224. }
  225. }
  226. dst += dlinesize;
  227. xmap += xlinesize;
  228. ymap += ylinesize;
  229. }
  230. }
  231. static int config_input(AVFilterLink *inlink)
  232. {
  233. AVFilterContext *ctx = inlink->dst;
  234. RemapContext *s = ctx->priv;
  235. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  236. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  237. s->nb_components = desc->nb_components;
  238. if (desc->comp[0].depth == 8) {
  239. if (s->nb_planes > 1 || s->nb_components == 1) {
  240. s->remap = remap_planar;
  241. } else {
  242. s->remap = remap_packed;
  243. }
  244. } else {
  245. if (s->nb_planes > 1 || s->nb_components == 1) {
  246. s->remap = remap_planar16;
  247. } else {
  248. s->remap = remap_packed16;
  249. }
  250. }
  251. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  252. return 0;
  253. }
  254. static int process_frame(FFFrameSync *fs)
  255. {
  256. AVFilterContext *ctx = fs->parent;
  257. RemapContext *s = fs->opaque;
  258. AVFilterLink *outlink = ctx->outputs[0];
  259. AVFrame *out, *in, *xpic, *ypic;
  260. int ret;
  261. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  262. (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
  263. (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
  264. return ret;
  265. if (ctx->is_disabled) {
  266. out = av_frame_clone(in);
  267. if (!out)
  268. return AVERROR(ENOMEM);
  269. } else {
  270. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  271. if (!out)
  272. return AVERROR(ENOMEM);
  273. av_frame_copy_props(out, in);
  274. s->remap(s, in, xpic, ypic, out);
  275. }
  276. out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
  277. return ff_filter_frame(outlink, out);
  278. }
  279. static int config_output(AVFilterLink *outlink)
  280. {
  281. AVFilterContext *ctx = outlink->src;
  282. RemapContext *s = ctx->priv;
  283. AVFilterLink *srclink = ctx->inputs[0];
  284. AVFilterLink *xlink = ctx->inputs[1];
  285. AVFilterLink *ylink = ctx->inputs[2];
  286. FFFrameSyncIn *in;
  287. int ret;
  288. if (xlink->w != ylink->w || xlink->h != ylink->h) {
  289. av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
  290. "(size %dx%d) do not match the corresponding "
  291. "third input link %s parameters (%dx%d)\n",
  292. ctx->input_pads[1].name, xlink->w, xlink->h,
  293. ctx->input_pads[2].name, ylink->w, ylink->h);
  294. return AVERROR(EINVAL);
  295. }
  296. outlink->w = xlink->w;
  297. outlink->h = xlink->h;
  298. outlink->time_base = srclink->time_base;
  299. outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
  300. outlink->frame_rate = srclink->frame_rate;
  301. ret = ff_framesync_init(&s->fs, ctx, 3);
  302. if (ret < 0)
  303. return ret;
  304. in = s->fs.in;
  305. in[0].time_base = srclink->time_base;
  306. in[1].time_base = xlink->time_base;
  307. in[2].time_base = ylink->time_base;
  308. in[0].sync = 2;
  309. in[0].before = EXT_STOP;
  310. in[0].after = EXT_STOP;
  311. in[1].sync = 1;
  312. in[1].before = EXT_NULL;
  313. in[1].after = EXT_INFINITY;
  314. in[2].sync = 1;
  315. in[2].before = EXT_NULL;
  316. in[2].after = EXT_INFINITY;
  317. s->fs.opaque = s;
  318. s->fs.on_event = process_frame;
  319. return ff_framesync_configure(&s->fs);
  320. }
  321. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  322. {
  323. RemapContext *s = inlink->dst->priv;
  324. return ff_framesync_filter_frame(&s->fs, inlink, buf);
  325. }
  326. static int request_frame(AVFilterLink *outlink)
  327. {
  328. RemapContext *s = outlink->src->priv;
  329. return ff_framesync_request_frame(&s->fs, outlink);
  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. .filter_frame = filter_frame,
  341. .config_props = config_input,
  342. },
  343. {
  344. .name = "xmap",
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. .filter_frame = filter_frame,
  347. },
  348. {
  349. .name = "ymap",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. .filter_frame = filter_frame,
  352. },
  353. { NULL }
  354. };
  355. static const AVFilterPad remap_outputs[] = {
  356. {
  357. .name = "default",
  358. .type = AVMEDIA_TYPE_VIDEO,
  359. .config_props = config_output,
  360. .request_frame = request_frame,
  361. },
  362. { NULL }
  363. };
  364. AVFilter ff_vf_remap = {
  365. .name = "remap",
  366. .description = NULL_IF_CONFIG_SMALL("Remap pixels."),
  367. .priv_size = sizeof(RemapContext),
  368. .uninit = uninit,
  369. .query_formats = query_formats,
  370. .inputs = remap_inputs,
  371. .outputs = remap_outputs,
  372. .priv_class = &remap_class,
  373. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  374. };