vf_displace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  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. #include "libavutil/imgutils.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "framesync.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. enum EdgeMode {
  29. EDGE_BLANK,
  30. EDGE_SMEAR,
  31. EDGE_WRAP,
  32. EDGE_MIRROR,
  33. EDGE_NB
  34. };
  35. typedef struct DisplaceContext {
  36. const AVClass *class;
  37. int width[4], height[4];
  38. enum EdgeMode edge;
  39. int nb_planes;
  40. int nb_components;
  41. int step;
  42. uint8_t blank[4];
  43. FFFrameSync fs;
  44. void (*displace)(struct DisplaceContext *s, const AVFrame *in,
  45. const AVFrame *xpic, const AVFrame *ypic, AVFrame *out);
  46. } DisplaceContext;
  47. #define OFFSET(x) offsetof(DisplaceContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption displace_options[] = {
  50. { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=EDGE_SMEAR}, 0, EDGE_NB-1, FLAGS, "edge" },
  51. { "blank", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_BLANK}, 0, 0, FLAGS, "edge" },
  52. { "smear", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_SMEAR}, 0, 0, FLAGS, "edge" },
  53. { "wrap" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_WRAP}, 0, 0, FLAGS, "edge" },
  54. { "mirror" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_MIRROR}, 0, 0, FLAGS, "edge" },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(displace);
  58. static int query_formats(AVFilterContext *ctx)
  59. {
  60. static const enum AVPixelFormat pix_fmts[] = {
  61. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  62. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  63. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  64. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  65. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  66. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  67. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  68. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  69. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  70. AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  71. };
  72. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  73. }
  74. static void displace_planar(DisplaceContext *s, const AVFrame *in,
  75. const AVFrame *xpic, const AVFrame *ypic,
  76. AVFrame *out)
  77. {
  78. int plane, x, y;
  79. for (plane = 0; plane < s->nb_planes; plane++) {
  80. const int h = s->height[plane];
  81. const int w = s->width[plane];
  82. const int dlinesize = out->linesize[plane];
  83. const int slinesize = in->linesize[plane];
  84. const int xlinesize = xpic->linesize[plane];
  85. const int ylinesize = ypic->linesize[plane];
  86. const uint8_t *src = in->data[plane];
  87. const uint8_t *ysrc = ypic->data[plane];
  88. const uint8_t *xsrc = xpic->data[plane];
  89. uint8_t *dst = out->data[plane];
  90. const uint8_t blank = s->blank[plane];
  91. for (y = 0; y < h; y++) {
  92. switch (s->edge) {
  93. case EDGE_BLANK:
  94. for (x = 0; x < w; x++) {
  95. int Y = y + ysrc[x] - 128;
  96. int X = x + xsrc[x] - 128;
  97. if (Y < 0 || Y >= h || X < 0 || X >= w)
  98. dst[x] = blank;
  99. else
  100. dst[x] = src[Y * slinesize + X];
  101. }
  102. break;
  103. case EDGE_SMEAR:
  104. for (x = 0; x < w; x++) {
  105. int Y = av_clip(y + ysrc[x] - 128, 0, h - 1);
  106. int X = av_clip(x + xsrc[x] - 128, 0, w - 1);
  107. dst[x] = src[Y * slinesize + X];
  108. }
  109. break;
  110. case EDGE_WRAP:
  111. for (x = 0; x < w; x++) {
  112. int Y = (y + ysrc[x] - 128) % h;
  113. int X = (x + xsrc[x] - 128) % w;
  114. if (Y < 0)
  115. Y += h;
  116. if (X < 0)
  117. X += w;
  118. dst[x] = src[Y * slinesize + X];
  119. }
  120. break;
  121. case EDGE_MIRROR:
  122. for (x = 0; x < w; x++) {
  123. int Y = y + ysrc[x] - 128;
  124. int X = x + xsrc[x] - 128;
  125. if (Y < 0)
  126. Y = (-Y) % h;
  127. if (X < 0)
  128. X = (-X) % w;
  129. if (Y >= h)
  130. Y = h - (Y % h) - 1;
  131. if (X >= w)
  132. X = w - (X % w) - 1;
  133. dst[x] = src[Y * slinesize + X];
  134. }
  135. break;
  136. }
  137. ysrc += ylinesize;
  138. xsrc += xlinesize;
  139. dst += dlinesize;
  140. }
  141. }
  142. }
  143. static void displace_packed(DisplaceContext *s, const AVFrame *in,
  144. const AVFrame *xpic, const AVFrame *ypic,
  145. AVFrame *out)
  146. {
  147. const int step = s->step;
  148. const int h = s->height[0];
  149. const int w = s->width[0];
  150. const int dlinesize = out->linesize[0];
  151. const int slinesize = in->linesize[0];
  152. const int xlinesize = xpic->linesize[0];
  153. const int ylinesize = ypic->linesize[0];
  154. const uint8_t *src = in->data[0];
  155. const uint8_t *ysrc = ypic->data[0];
  156. const uint8_t *xsrc = xpic->data[0];
  157. const uint8_t *blank = s->blank;
  158. uint8_t *dst = out->data[0];
  159. int c, x, y;
  160. for (y = 0; y < h; y++) {
  161. switch (s->edge) {
  162. case EDGE_BLANK:
  163. for (x = 0; x < w; x++) {
  164. for (c = 0; c < s->nb_components; c++) {
  165. int Y = y + (ysrc[x * step + c] - 128);
  166. int X = x + (xsrc[x * step + c] - 128);
  167. if (Y < 0 || Y >= h || X < 0 || X >= w)
  168. dst[x * step + c] = blank[c];
  169. else
  170. dst[x * step + c] = src[Y * slinesize + X * step + c];
  171. }
  172. }
  173. break;
  174. case EDGE_SMEAR:
  175. for (x = 0; x < w; x++) {
  176. for (c = 0; c < s->nb_components; c++) {
  177. int Y = av_clip(y + (ysrc[x * step + c] - 128), 0, h - 1);
  178. int X = av_clip(x + (xsrc[x * step + c] - 128), 0, w - 1);
  179. dst[x * step + c] = src[Y * slinesize + X * step + c];
  180. }
  181. }
  182. break;
  183. case EDGE_WRAP:
  184. for (x = 0; x < w; x++) {
  185. for (c = 0; c < s->nb_components; c++) {
  186. int Y = (y + (ysrc[x * step + c] - 128)) % h;
  187. int X = (x + (xsrc[x * step + c] - 128)) % w;
  188. if (Y < 0)
  189. Y += h;
  190. if (X < 0)
  191. X += w;
  192. dst[x * step + c] = src[Y * slinesize + X * step + c];
  193. }
  194. }
  195. break;
  196. case EDGE_MIRROR:
  197. for (x = 0; x < w; x++) {
  198. for (c = 0; c < s->nb_components; c++) {
  199. int Y = y + ysrc[x * step + c] - 128;
  200. int X = x + xsrc[x * step + c] - 128;
  201. if (Y < 0)
  202. Y = (-Y) % h;
  203. if (X < 0)
  204. X = (-X) % w;
  205. if (Y >= h)
  206. Y = h - (Y % h) - 1;
  207. if (X >= w)
  208. X = w - (X % w) - 1;
  209. dst[x * step + c] = src[Y * slinesize + X * step + c];
  210. }
  211. }
  212. break;
  213. }
  214. ysrc += ylinesize;
  215. xsrc += xlinesize;
  216. dst += dlinesize;
  217. }
  218. }
  219. static int process_frame(FFFrameSync *fs)
  220. {
  221. AVFilterContext *ctx = fs->parent;
  222. DisplaceContext *s = fs->opaque;
  223. AVFilterLink *outlink = ctx->outputs[0];
  224. AVFrame *out, *in, *xpic, *ypic;
  225. int ret;
  226. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  227. (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
  228. (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
  229. return ret;
  230. if (ctx->is_disabled) {
  231. out = av_frame_clone(in);
  232. if (!out)
  233. return AVERROR(ENOMEM);
  234. } else {
  235. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  236. if (!out)
  237. return AVERROR(ENOMEM);
  238. av_frame_copy_props(out, in);
  239. s->displace(s, in, xpic, ypic, out);
  240. }
  241. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  242. return ff_filter_frame(outlink, out);
  243. }
  244. static int config_input(AVFilterLink *inlink)
  245. {
  246. AVFilterContext *ctx = inlink->dst;
  247. DisplaceContext *s = ctx->priv;
  248. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  249. int vsub, hsub;
  250. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  251. s->nb_components = desc->nb_components;
  252. if (s->nb_planes > 1 || s->nb_components == 1)
  253. s->displace = displace_planar;
  254. else
  255. s->displace = displace_packed;
  256. if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
  257. s->blank[1] = s->blank[2] = 128;
  258. s->blank[0] = 16;
  259. }
  260. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  261. hsub = desc->log2_chroma_w;
  262. vsub = desc->log2_chroma_h;
  263. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  264. s->height[0] = s->height[3] = inlink->h;
  265. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  266. s->width[0] = s->width[3] = inlink->w;
  267. return 0;
  268. }
  269. static int config_output(AVFilterLink *outlink)
  270. {
  271. AVFilterContext *ctx = outlink->src;
  272. DisplaceContext *s = ctx->priv;
  273. AVFilterLink *srclink = ctx->inputs[0];
  274. AVFilterLink *xlink = ctx->inputs[1];
  275. AVFilterLink *ylink = ctx->inputs[2];
  276. FFFrameSyncIn *in;
  277. int ret;
  278. if (srclink->format != xlink->format ||
  279. srclink->format != ylink->format) {
  280. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  281. return AVERROR(EINVAL);
  282. }
  283. if (srclink->w != xlink->w ||
  284. srclink->h != xlink->h ||
  285. srclink->w != ylink->w ||
  286. srclink->h != ylink->h) {
  287. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  288. "(size %dx%d) do not match the corresponding "
  289. "second input link %s parameters (%dx%d) "
  290. "and/or third input link %s parameters (%dx%d)\n",
  291. ctx->input_pads[0].name, srclink->w, srclink->h,
  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 = srclink->w;
  297. outlink->h = srclink->h;
  298. outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
  299. outlink->frame_rate = srclink->frame_rate;
  300. ret = ff_framesync_init(&s->fs, ctx, 3);
  301. if (ret < 0)
  302. return ret;
  303. in = s->fs.in;
  304. in[0].time_base = srclink->time_base;
  305. in[1].time_base = xlink->time_base;
  306. in[2].time_base = ylink->time_base;
  307. in[0].sync = 2;
  308. in[0].before = EXT_STOP;
  309. in[0].after = EXT_STOP;
  310. in[1].sync = 1;
  311. in[1].before = EXT_NULL;
  312. in[1].after = EXT_INFINITY;
  313. in[2].sync = 1;
  314. in[2].before = EXT_NULL;
  315. in[2].after = EXT_INFINITY;
  316. s->fs.opaque = s;
  317. s->fs.on_event = process_frame;
  318. ret = ff_framesync_configure(&s->fs);
  319. outlink->time_base = s->fs.time_base;
  320. return ret;
  321. }
  322. static int activate(AVFilterContext *ctx)
  323. {
  324. DisplaceContext *s = ctx->priv;
  325. return ff_framesync_activate(&s->fs);
  326. }
  327. static av_cold void uninit(AVFilterContext *ctx)
  328. {
  329. DisplaceContext *s = ctx->priv;
  330. ff_framesync_uninit(&s->fs);
  331. }
  332. static const AVFilterPad displace_inputs[] = {
  333. {
  334. .name = "source",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .config_props = config_input,
  337. },
  338. {
  339. .name = "xmap",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. },
  342. {
  343. .name = "ymap",
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. },
  346. { NULL }
  347. };
  348. static const AVFilterPad displace_outputs[] = {
  349. {
  350. .name = "default",
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .config_props = config_output,
  353. },
  354. { NULL }
  355. };
  356. AVFilter ff_vf_displace = {
  357. .name = "displace",
  358. .description = NULL_IF_CONFIG_SMALL("Displace pixels."),
  359. .priv_size = sizeof(DisplaceContext),
  360. .uninit = uninit,
  361. .query_formats = query_formats,
  362. .activate = activate,
  363. .inputs = displace_inputs,
  364. .outputs = displace_outputs,
  365. .priv_class = &displace_class,
  366. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  367. };