vf_stack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2015 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/avstring.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "framesync.h"
  28. #include "video.h"
  29. typedef struct StackItem {
  30. int x[4], y[4];
  31. int linesize[4];
  32. int height[4];
  33. } StackItem;
  34. typedef struct StackContext {
  35. const AVClass *class;
  36. const AVPixFmtDescriptor *desc;
  37. int nb_inputs;
  38. char *layout;
  39. int shortest;
  40. int is_vertical;
  41. int is_horizontal;
  42. int nb_planes;
  43. StackItem *items;
  44. AVFrame **frames;
  45. FFFrameSync fs;
  46. } StackContext;
  47. static int query_formats(AVFilterContext *ctx)
  48. {
  49. AVFilterFormats *pix_fmts = NULL;
  50. int fmt, ret;
  51. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  52. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  53. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  54. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  55. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  56. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  57. return ret;
  58. }
  59. return ff_set_common_formats(ctx, pix_fmts);
  60. }
  61. static av_cold int init(AVFilterContext *ctx)
  62. {
  63. StackContext *s = ctx->priv;
  64. int i, ret;
  65. if (!strcmp(ctx->filter->name, "vstack"))
  66. s->is_vertical = 1;
  67. if (!strcmp(ctx->filter->name, "hstack"))
  68. s->is_horizontal = 1;
  69. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  70. if (!s->frames)
  71. return AVERROR(ENOMEM);
  72. if (!strcmp(ctx->filter->name, "xstack")) {
  73. if (!s->layout) {
  74. if (s->nb_inputs == 2) {
  75. s->layout = av_strdup("0_0|w0_0");
  76. if (!s->layout)
  77. return AVERROR(ENOMEM);
  78. } else {
  79. av_log(ctx, AV_LOG_ERROR, "No layout specified.\n");
  80. return AVERROR(EINVAL);
  81. }
  82. }
  83. s->items = av_calloc(s->nb_inputs, sizeof(*s->items));
  84. if (!s->items)
  85. return AVERROR(ENOMEM);
  86. }
  87. for (i = 0; i < s->nb_inputs; i++) {
  88. AVFilterPad pad = { 0 };
  89. pad.type = AVMEDIA_TYPE_VIDEO;
  90. pad.name = av_asprintf("input%d", i);
  91. if (!pad.name)
  92. return AVERROR(ENOMEM);
  93. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  94. av_freep(&pad.name);
  95. return ret;
  96. }
  97. }
  98. return 0;
  99. }
  100. static int process_frame(FFFrameSync *fs)
  101. {
  102. AVFilterContext *ctx = fs->parent;
  103. AVFilterLink *outlink = ctx->outputs[0];
  104. StackContext *s = fs->opaque;
  105. AVFrame **in = s->frames;
  106. AVFrame *out;
  107. int i, p, ret, offset[4] = { 0 };
  108. for (i = 0; i < s->nb_inputs; i++) {
  109. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  110. return ret;
  111. }
  112. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  113. if (!out)
  114. return AVERROR(ENOMEM);
  115. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  116. out->sample_aspect_ratio = outlink->sample_aspect_ratio;
  117. for (i = 0; i < s->nb_inputs; i++) {
  118. AVFilterLink *inlink = ctx->inputs[i];
  119. int linesize[4];
  120. int height[4];
  121. if (s->is_horizontal || s->is_vertical) {
  122. if ((ret = av_image_fill_linesizes(linesize, inlink->format, inlink->w)) < 0) {
  123. av_frame_free(&out);
  124. return ret;
  125. }
  126. height[1] = height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  127. height[0] = height[3] = inlink->h;
  128. }
  129. for (p = 0; p < s->nb_planes; p++) {
  130. if (s->is_vertical) {
  131. av_image_copy_plane(out->data[p] + offset[p] * out->linesize[p],
  132. out->linesize[p],
  133. in[i]->data[p],
  134. in[i]->linesize[p],
  135. linesize[p], height[p]);
  136. offset[p] += height[p];
  137. } else if (s->is_horizontal) {
  138. av_image_copy_plane(out->data[p] + offset[p],
  139. out->linesize[p],
  140. in[i]->data[p],
  141. in[i]->linesize[p],
  142. linesize[p], height[p]);
  143. offset[p] += linesize[p];
  144. } else {
  145. StackItem *item = &s->items[i];
  146. av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
  147. out->linesize[p],
  148. in[i]->data[p],
  149. in[i]->linesize[p],
  150. item->linesize[p], item->height[p]);
  151. }
  152. }
  153. }
  154. return ff_filter_frame(outlink, out);
  155. }
  156. static int config_output(AVFilterLink *outlink)
  157. {
  158. AVFilterContext *ctx = outlink->src;
  159. StackContext *s = ctx->priv;
  160. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  161. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  162. int height = ctx->inputs[0]->h;
  163. int width = ctx->inputs[0]->w;
  164. FFFrameSyncIn *in;
  165. int i, ret;
  166. s->desc = av_pix_fmt_desc_get(outlink->format);
  167. if (!s->desc)
  168. return AVERROR_BUG;
  169. if (s->is_vertical) {
  170. for (i = 1; i < s->nb_inputs; i++) {
  171. if (ctx->inputs[i]->w != width) {
  172. av_log(ctx, AV_LOG_ERROR, "Input %d width %d does not match input %d width %d.\n", i, ctx->inputs[i]->w, 0, width);
  173. return AVERROR(EINVAL);
  174. }
  175. height += ctx->inputs[i]->h;
  176. }
  177. } else if (s->is_horizontal) {
  178. for (i = 1; i < s->nb_inputs; i++) {
  179. if (ctx->inputs[i]->h != height) {
  180. av_log(ctx, AV_LOG_ERROR, "Input %d height %d does not match input %d height %d.\n", i, ctx->inputs[i]->h, 0, height);
  181. return AVERROR(EINVAL);
  182. }
  183. width += ctx->inputs[i]->w;
  184. }
  185. } else {
  186. char *arg, *p = s->layout, *saveptr = NULL;
  187. char *arg2, *p2, *saveptr2 = NULL;
  188. char *arg3, *p3, *saveptr3 = NULL;
  189. int inw, inh, size;
  190. for (i = 0; i < s->nb_inputs; i++) {
  191. AVFilterLink *inlink = ctx->inputs[i];
  192. StackItem *item = &s->items[i];
  193. if (!(arg = av_strtok(p, "|", &saveptr)))
  194. return AVERROR(EINVAL);
  195. p = NULL;
  196. if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
  197. return ret;
  198. }
  199. item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  200. item->height[0] = item->height[3] = inlink->h;
  201. p2 = arg;
  202. inw = inh = 0;
  203. for (int j = 0; j < 2; j++) {
  204. if (!(arg2 = av_strtok(p2, "_", &saveptr2)))
  205. return AVERROR(EINVAL);
  206. p2 = NULL;
  207. p3 = arg2;
  208. while ((arg3 = av_strtok(p3, "+", &saveptr3))) {
  209. p3 = NULL;
  210. if (sscanf(arg3, "w%d", &size) == 1) {
  211. if (size == i || size < 0 || size >= s->nb_inputs)
  212. return AVERROR(EINVAL);
  213. if (!j)
  214. inw += ctx->inputs[size]->w;
  215. else
  216. inh += ctx->inputs[size]->w;
  217. } else if (sscanf(arg3, "h%d", &size) == 1) {
  218. if (size == i || size < 0 || size >= s->nb_inputs)
  219. return AVERROR(EINVAL);
  220. if (!j)
  221. inw += ctx->inputs[size]->h;
  222. else
  223. inh += ctx->inputs[size]->h;
  224. } else if (sscanf(arg3, "%d", &size) == 1) {
  225. if (size < 0)
  226. return AVERROR(EINVAL);
  227. if (!j)
  228. inw += size;
  229. else
  230. inh += size;
  231. } else {
  232. return AVERROR(EINVAL);
  233. }
  234. }
  235. }
  236. if ((ret = av_image_fill_linesizes(item->x, inlink->format, inw)) < 0) {
  237. return ret;
  238. }
  239. item->y[1] = item->y[2] = AV_CEIL_RSHIFT(inh, s->desc->log2_chroma_h);
  240. item->y[0] = item->y[3] = inh;
  241. width = FFMAX(width, inlink->w + inw);
  242. height = FFMAX(height, inlink->h + inh);
  243. }
  244. }
  245. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  246. outlink->w = width;
  247. outlink->h = height;
  248. outlink->frame_rate = frame_rate;
  249. outlink->sample_aspect_ratio = sar;
  250. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  251. return ret;
  252. in = s->fs.in;
  253. s->fs.opaque = s;
  254. s->fs.on_event = process_frame;
  255. for (i = 0; i < s->nb_inputs; i++) {
  256. AVFilterLink *inlink = ctx->inputs[i];
  257. in[i].time_base = inlink->time_base;
  258. in[i].sync = 1;
  259. in[i].before = EXT_STOP;
  260. in[i].after = s->shortest ? EXT_STOP : EXT_INFINITY;
  261. }
  262. ret = ff_framesync_configure(&s->fs);
  263. outlink->time_base = s->fs.time_base;
  264. return ret;
  265. }
  266. static av_cold void uninit(AVFilterContext *ctx)
  267. {
  268. StackContext *s = ctx->priv;
  269. int i;
  270. ff_framesync_uninit(&s->fs);
  271. av_freep(&s->frames);
  272. av_freep(&s->items);
  273. for (i = 0; i < ctx->nb_inputs; i++)
  274. av_freep(&ctx->input_pads[i].name);
  275. }
  276. static int activate(AVFilterContext *ctx)
  277. {
  278. StackContext *s = ctx->priv;
  279. return ff_framesync_activate(&s->fs);
  280. }
  281. #define OFFSET(x) offsetof(StackContext, x)
  282. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  283. static const AVOption stack_options[] = {
  284. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  285. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  286. { NULL },
  287. };
  288. static const AVFilterPad outputs[] = {
  289. {
  290. .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .config_props = config_output,
  293. },
  294. { NULL }
  295. };
  296. #if CONFIG_HSTACK_FILTER
  297. #define hstack_options stack_options
  298. AVFILTER_DEFINE_CLASS(hstack);
  299. AVFilter ff_vf_hstack = {
  300. .name = "hstack",
  301. .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
  302. .priv_size = sizeof(StackContext),
  303. .priv_class = &hstack_class,
  304. .query_formats = query_formats,
  305. .outputs = outputs,
  306. .init = init,
  307. .uninit = uninit,
  308. .activate = activate,
  309. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  310. };
  311. #endif /* CONFIG_HSTACK_FILTER */
  312. #if CONFIG_VSTACK_FILTER
  313. #define vstack_options stack_options
  314. AVFILTER_DEFINE_CLASS(vstack);
  315. AVFilter ff_vf_vstack = {
  316. .name = "vstack",
  317. .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
  318. .priv_size = sizeof(StackContext),
  319. .priv_class = &vstack_class,
  320. .query_formats = query_formats,
  321. .outputs = outputs,
  322. .init = init,
  323. .uninit = uninit,
  324. .activate = activate,
  325. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  326. };
  327. #endif /* CONFIG_VSTACK_FILTER */
  328. #if CONFIG_XSTACK_FILTER
  329. static const AVOption xstack_options[] = {
  330. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  331. { "layout", "set custom layout", OFFSET(layout), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, .flags = FLAGS },
  332. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  333. { NULL },
  334. };
  335. AVFILTER_DEFINE_CLASS(xstack);
  336. AVFilter ff_vf_xstack = {
  337. .name = "xstack",
  338. .description = NULL_IF_CONFIG_SMALL("Stack video inputs into custom layout."),
  339. .priv_size = sizeof(StackContext),
  340. .priv_class = &xstack_class,
  341. .query_formats = query_formats,
  342. .outputs = outputs,
  343. .init = init,
  344. .uninit = uninit,
  345. .activate = activate,
  346. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  347. };
  348. #endif /* CONFIG_XSTACK_FILTER */