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.

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