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.

386 lines
16KB

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