vf_displace.c 13 KB

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