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.

276 lines
8.0KB

  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 "framesync2.h"
  28. #include "video.h"
  29. typedef struct StackContext {
  30. const AVClass *class;
  31. const AVPixFmtDescriptor *desc;
  32. int nb_inputs;
  33. int shortest;
  34. int is_vertical;
  35. int nb_planes;
  36. AVFrame **frames;
  37. FFFrameSync fs;
  38. } StackContext;
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. AVFilterFormats *pix_fmts = NULL;
  42. int fmt, ret;
  43. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  44. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  45. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  46. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  47. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  48. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  49. return ret;
  50. }
  51. return ff_set_common_formats(ctx, pix_fmts);
  52. }
  53. static av_cold int init(AVFilterContext *ctx)
  54. {
  55. StackContext *s = ctx->priv;
  56. int i, ret;
  57. if (!strcmp(ctx->filter->name, "vstack"))
  58. s->is_vertical = 1;
  59. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  60. if (!s->frames)
  61. return AVERROR(ENOMEM);
  62. for (i = 0; i < s->nb_inputs; i++) {
  63. AVFilterPad pad = { 0 };
  64. pad.type = AVMEDIA_TYPE_VIDEO;
  65. pad.name = av_asprintf("input%d", i);
  66. if (!pad.name)
  67. return AVERROR(ENOMEM);
  68. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  69. av_freep(&pad.name);
  70. return ret;
  71. }
  72. }
  73. return 0;
  74. }
  75. static int process_frame(FFFrameSync *fs)
  76. {
  77. AVFilterContext *ctx = fs->parent;
  78. AVFilterLink *outlink = ctx->outputs[0];
  79. StackContext *s = fs->opaque;
  80. AVFrame **in = s->frames;
  81. AVFrame *out;
  82. int i, p, ret, offset[4] = { 0 };
  83. for (i = 0; i < s->nb_inputs; i++) {
  84. if ((ret = ff_framesync2_get_frame(&s->fs, i, &in[i], 0)) < 0)
  85. return ret;
  86. }
  87. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  88. if (!out)
  89. return AVERROR(ENOMEM);
  90. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  91. for (i = 0; i < s->nb_inputs; i++) {
  92. AVFilterLink *inlink = ctx->inputs[i];
  93. int linesize[4];
  94. int height[4];
  95. if ((ret = av_image_fill_linesizes(linesize, inlink->format, inlink->w)) < 0) {
  96. av_frame_free(&out);
  97. return ret;
  98. }
  99. height[1] = height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  100. height[0] = height[3] = inlink->h;
  101. for (p = 0; p < s->nb_planes; p++) {
  102. if (s->is_vertical) {
  103. av_image_copy_plane(out->data[p] + offset[p] * out->linesize[p],
  104. out->linesize[p],
  105. in[i]->data[p],
  106. in[i]->linesize[p],
  107. linesize[p], height[p]);
  108. offset[p] += height[p];
  109. } else {
  110. av_image_copy_plane(out->data[p] + offset[p],
  111. out->linesize[p],
  112. in[i]->data[p],
  113. in[i]->linesize[p],
  114. linesize[p], height[p]);
  115. offset[p] += linesize[p];
  116. }
  117. }
  118. }
  119. return ff_filter_frame(outlink, out);
  120. }
  121. static int config_output(AVFilterLink *outlink)
  122. {
  123. AVFilterContext *ctx = outlink->src;
  124. StackContext *s = ctx->priv;
  125. AVRational time_base = ctx->inputs[0]->time_base;
  126. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  127. int height = ctx->inputs[0]->h;
  128. int width = ctx->inputs[0]->w;
  129. FFFrameSyncIn *in;
  130. int i, ret;
  131. if (s->is_vertical) {
  132. for (i = 1; i < s->nb_inputs; i++) {
  133. if (ctx->inputs[i]->w != width) {
  134. 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);
  135. return AVERROR(EINVAL);
  136. }
  137. height += ctx->inputs[i]->h;
  138. }
  139. } else {
  140. for (i = 1; i < s->nb_inputs; i++) {
  141. if (ctx->inputs[i]->h != height) {
  142. 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);
  143. return AVERROR(EINVAL);
  144. }
  145. width += ctx->inputs[i]->w;
  146. }
  147. }
  148. s->desc = av_pix_fmt_desc_get(outlink->format);
  149. if (!s->desc)
  150. return AVERROR_BUG;
  151. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  152. outlink->w = width;
  153. outlink->h = height;
  154. outlink->time_base = time_base;
  155. outlink->frame_rate = frame_rate;
  156. if ((ret = ff_framesync2_init(&s->fs, ctx, s->nb_inputs)) < 0)
  157. return ret;
  158. in = s->fs.in;
  159. s->fs.opaque = s;
  160. s->fs.on_event = process_frame;
  161. for (i = 0; i < s->nb_inputs; i++) {
  162. AVFilterLink *inlink = ctx->inputs[i];
  163. in[i].time_base = inlink->time_base;
  164. in[i].sync = 1;
  165. in[i].before = EXT_STOP;
  166. in[i].after = s->shortest ? EXT_STOP : EXT_INFINITY;
  167. }
  168. return ff_framesync2_configure(&s->fs);
  169. }
  170. static av_cold void uninit(AVFilterContext *ctx)
  171. {
  172. StackContext *s = ctx->priv;
  173. int i;
  174. ff_framesync2_uninit(&s->fs);
  175. av_freep(&s->frames);
  176. for (i = 0; i < ctx->nb_inputs; i++)
  177. av_freep(&ctx->input_pads[i].name);
  178. }
  179. static int activate(AVFilterContext *ctx)
  180. {
  181. StackContext *s = ctx->priv;
  182. return ff_framesync2_activate(&s->fs);
  183. }
  184. #define OFFSET(x) offsetof(StackContext, x)
  185. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  186. static const AVOption stack_options[] = {
  187. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  188. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  189. { NULL },
  190. };
  191. static const AVFilterPad outputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. .config_props = config_output,
  196. },
  197. { NULL }
  198. };
  199. #if CONFIG_HSTACK_FILTER
  200. #define hstack_options stack_options
  201. AVFILTER_DEFINE_CLASS(hstack);
  202. AVFilter ff_vf_hstack = {
  203. .name = "hstack",
  204. .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
  205. .priv_size = sizeof(StackContext),
  206. .priv_class = &hstack_class,
  207. .query_formats = query_formats,
  208. .outputs = outputs,
  209. .init = init,
  210. .uninit = uninit,
  211. .activate = activate,
  212. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  213. };
  214. #endif /* CONFIG_HSTACK_FILTER */
  215. #if CONFIG_VSTACK_FILTER
  216. #define vstack_options stack_options
  217. AVFILTER_DEFINE_CLASS(vstack);
  218. AVFilter ff_vf_vstack = {
  219. .name = "vstack",
  220. .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
  221. .priv_size = sizeof(StackContext),
  222. .priv_class = &vstack_class,
  223. .query_formats = query_formats,
  224. .outputs = outputs,
  225. .init = init,
  226. .uninit = uninit,
  227. .activate = activate,
  228. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  229. };
  230. #endif /* CONFIG_VSTACK_FILTER */