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.

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