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.

476 lines
15KB

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