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.

460 lines
15KB

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