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.

436 lines
13KB

  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. s->items = av_calloc(s->nb_inputs, sizeof(*s->items));
  73. if (!s->items)
  74. return AVERROR(ENOMEM);
  75. if (!strcmp(ctx->filter->name, "xstack")) {
  76. if (!s->layout) {
  77. if (s->nb_inputs == 2) {
  78. s->layout = av_strdup("0_0|w0_0");
  79. if (!s->layout)
  80. return AVERROR(ENOMEM);
  81. } else {
  82. av_log(ctx, AV_LOG_ERROR, "No layout specified.\n");
  83. return AVERROR(EINVAL);
  84. }
  85. }
  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_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  101. {
  102. StackContext *s = ctx->priv;
  103. AVFrame *out = arg;
  104. AVFrame **in = s->frames;
  105. const int start = (s->nb_inputs * job ) / nb_jobs;
  106. const int end = (s->nb_inputs * (job+1)) / nb_jobs;
  107. for (int i = start; i < end; i++) {
  108. StackItem *item = &s->items[i];
  109. for (int p = 0; p < s->nb_planes; p++) {
  110. av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
  111. out->linesize[p],
  112. in[i]->data[p],
  113. in[i]->linesize[p],
  114. item->linesize[p], item->height[p]);
  115. }
  116. }
  117. return 0;
  118. }
  119. static int process_frame(FFFrameSync *fs)
  120. {
  121. AVFilterContext *ctx = fs->parent;
  122. AVFilterLink *outlink = ctx->outputs[0];
  123. StackContext *s = fs->opaque;
  124. AVFrame **in = s->frames;
  125. AVFrame *out;
  126. int i, ret;
  127. for (i = 0; i < s->nb_inputs; i++) {
  128. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  129. return ret;
  130. }
  131. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  132. if (!out)
  133. return AVERROR(ENOMEM);
  134. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  135. out->sample_aspect_ratio = outlink->sample_aspect_ratio;
  136. ctx->internal->execute(ctx, process_slice, out, NULL, FFMIN(s->nb_inputs, ff_filter_get_nb_threads(ctx)));
  137. return ff_filter_frame(outlink, out);
  138. }
  139. static int config_output(AVFilterLink *outlink)
  140. {
  141. AVFilterContext *ctx = outlink->src;
  142. StackContext *s = ctx->priv;
  143. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  144. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  145. int height = ctx->inputs[0]->h;
  146. int width = ctx->inputs[0]->w;
  147. FFFrameSyncIn *in;
  148. int i, ret;
  149. s->desc = av_pix_fmt_desc_get(outlink->format);
  150. if (!s->desc)
  151. return AVERROR_BUG;
  152. if (s->is_vertical) {
  153. for (i = 0; i < s->nb_inputs; i++) {
  154. AVFilterLink *inlink = ctx->inputs[i];
  155. StackItem *item = &s->items[i];
  156. if (ctx->inputs[i]->w != width) {
  157. 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);
  158. return AVERROR(EINVAL);
  159. }
  160. if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
  161. return ret;
  162. }
  163. item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  164. item->height[0] = item->height[3] = inlink->h;
  165. if (i) {
  166. item->y[1] = item->y[2] = AV_CEIL_RSHIFT(height, s->desc->log2_chroma_h);
  167. item->y[0] = item->y[3] = height;
  168. height += ctx->inputs[i]->h;
  169. }
  170. }
  171. } else if (s->is_horizontal) {
  172. for (i = 0; i < s->nb_inputs; i++) {
  173. AVFilterLink *inlink = ctx->inputs[i];
  174. StackItem *item = &s->items[i];
  175. if (ctx->inputs[i]->h != height) {
  176. 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);
  177. return AVERROR(EINVAL);
  178. }
  179. if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
  180. return ret;
  181. }
  182. item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  183. item->height[0] = item->height[3] = inlink->h;
  184. if (i) {
  185. if ((ret = av_image_fill_linesizes(item->x, inlink->format, width)) < 0) {
  186. return ret;
  187. }
  188. width += ctx->inputs[i]->w;
  189. }
  190. }
  191. } else {
  192. char *arg, *p = s->layout, *saveptr = NULL;
  193. char *arg2, *p2, *saveptr2 = NULL;
  194. char *arg3, *p3, *saveptr3 = NULL;
  195. int inw, inh, size;
  196. for (i = 0; i < s->nb_inputs; i++) {
  197. AVFilterLink *inlink = ctx->inputs[i];
  198. StackItem *item = &s->items[i];
  199. if (!(arg = av_strtok(p, "|", &saveptr)))
  200. return AVERROR(EINVAL);
  201. p = NULL;
  202. if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
  203. return ret;
  204. }
  205. item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  206. item->height[0] = item->height[3] = inlink->h;
  207. p2 = arg;
  208. inw = inh = 0;
  209. for (int j = 0; j < 2; j++) {
  210. if (!(arg2 = av_strtok(p2, "_", &saveptr2)))
  211. return AVERROR(EINVAL);
  212. p2 = NULL;
  213. p3 = arg2;
  214. while ((arg3 = av_strtok(p3, "+", &saveptr3))) {
  215. p3 = NULL;
  216. if (sscanf(arg3, "w%d", &size) == 1) {
  217. if (size == i || size < 0 || size >= s->nb_inputs)
  218. return AVERROR(EINVAL);
  219. if (!j)
  220. inw += ctx->inputs[size]->w;
  221. else
  222. inh += ctx->inputs[size]->w;
  223. } else if (sscanf(arg3, "h%d", &size) == 1) {
  224. if (size == i || size < 0 || size >= s->nb_inputs)
  225. return AVERROR(EINVAL);
  226. if (!j)
  227. inw += ctx->inputs[size]->h;
  228. else
  229. inh += ctx->inputs[size]->h;
  230. } else if (sscanf(arg3, "%d", &size) == 1) {
  231. if (size < 0)
  232. return AVERROR(EINVAL);
  233. if (!j)
  234. inw += size;
  235. else
  236. inh += size;
  237. } else {
  238. return AVERROR(EINVAL);
  239. }
  240. }
  241. }
  242. if ((ret = av_image_fill_linesizes(item->x, inlink->format, inw)) < 0) {
  243. return ret;
  244. }
  245. item->y[1] = item->y[2] = AV_CEIL_RSHIFT(inh, s->desc->log2_chroma_h);
  246. item->y[0] = item->y[3] = inh;
  247. width = FFMAX(width, inlink->w + inw);
  248. height = FFMAX(height, inlink->h + inh);
  249. }
  250. }
  251. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  252. outlink->w = width;
  253. outlink->h = height;
  254. outlink->frame_rate = frame_rate;
  255. outlink->sample_aspect_ratio = sar;
  256. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  257. return ret;
  258. in = s->fs.in;
  259. s->fs.opaque = s;
  260. s->fs.on_event = process_frame;
  261. for (i = 0; i < s->nb_inputs; i++) {
  262. AVFilterLink *inlink = ctx->inputs[i];
  263. in[i].time_base = inlink->time_base;
  264. in[i].sync = 1;
  265. in[i].before = EXT_STOP;
  266. in[i].after = s->shortest ? EXT_STOP : EXT_INFINITY;
  267. }
  268. ret = ff_framesync_configure(&s->fs);
  269. outlink->time_base = s->fs.time_base;
  270. return ret;
  271. }
  272. static av_cold void uninit(AVFilterContext *ctx)
  273. {
  274. StackContext *s = ctx->priv;
  275. int i;
  276. ff_framesync_uninit(&s->fs);
  277. av_freep(&s->frames);
  278. av_freep(&s->items);
  279. for (i = 0; i < ctx->nb_inputs; i++)
  280. av_freep(&ctx->input_pads[i].name);
  281. }
  282. static int activate(AVFilterContext *ctx)
  283. {
  284. StackContext *s = ctx->priv;
  285. return ff_framesync_activate(&s->fs);
  286. }
  287. #define OFFSET(x) offsetof(StackContext, x)
  288. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  289. static const AVOption stack_options[] = {
  290. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  291. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  292. { NULL },
  293. };
  294. static const AVFilterPad outputs[] = {
  295. {
  296. .name = "default",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .config_props = config_output,
  299. },
  300. { NULL }
  301. };
  302. #if CONFIG_HSTACK_FILTER
  303. #define hstack_options stack_options
  304. AVFILTER_DEFINE_CLASS(hstack);
  305. AVFilter ff_vf_hstack = {
  306. .name = "hstack",
  307. .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
  308. .priv_size = sizeof(StackContext),
  309. .priv_class = &hstack_class,
  310. .query_formats = query_formats,
  311. .outputs = outputs,
  312. .init = init,
  313. .uninit = uninit,
  314. .activate = activate,
  315. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  316. };
  317. #endif /* CONFIG_HSTACK_FILTER */
  318. #if CONFIG_VSTACK_FILTER
  319. #define vstack_options stack_options
  320. AVFILTER_DEFINE_CLASS(vstack);
  321. AVFilter ff_vf_vstack = {
  322. .name = "vstack",
  323. .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
  324. .priv_size = sizeof(StackContext),
  325. .priv_class = &vstack_class,
  326. .query_formats = query_formats,
  327. .outputs = outputs,
  328. .init = init,
  329. .uninit = uninit,
  330. .activate = activate,
  331. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  332. };
  333. #endif /* CONFIG_VSTACK_FILTER */
  334. #if CONFIG_XSTACK_FILTER
  335. static const AVOption xstack_options[] = {
  336. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  337. { "layout", "set custom layout", OFFSET(layout), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, .flags = FLAGS },
  338. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  339. { NULL },
  340. };
  341. AVFILTER_DEFINE_CLASS(xstack);
  342. AVFilter ff_vf_xstack = {
  343. .name = "xstack",
  344. .description = NULL_IF_CONFIG_SMALL("Stack video inputs into custom layout."),
  345. .priv_size = sizeof(StackContext),
  346. .priv_class = &xstack_class,
  347. .query_formats = query_formats,
  348. .outputs = outputs,
  349. .init = init,
  350. .uninit = uninit,
  351. .activate = activate,
  352. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  353. };
  354. #endif /* CONFIG_XSTACK_FILTER */