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.

279 lines
8.2KB

  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 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_framesync_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. out->sample_aspect_ratio = outlink->sample_aspect_ratio;
  92. for (i = 0; i < s->nb_inputs; i++) {
  93. AVFilterLink *inlink = ctx->inputs[i];
  94. int linesize[4];
  95. int height[4];
  96. if ((ret = av_image_fill_linesizes(linesize, inlink->format, inlink->w)) < 0) {
  97. av_frame_free(&out);
  98. return ret;
  99. }
  100. height[1] = height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  101. height[0] = height[3] = inlink->h;
  102. for (p = 0; p < s->nb_planes; p++) {
  103. if (s->is_vertical) {
  104. av_image_copy_plane(out->data[p] + offset[p] * out->linesize[p],
  105. out->linesize[p],
  106. in[i]->data[p],
  107. in[i]->linesize[p],
  108. linesize[p], height[p]);
  109. offset[p] += height[p];
  110. } else {
  111. av_image_copy_plane(out->data[p] + offset[p],
  112. out->linesize[p],
  113. in[i]->data[p],
  114. in[i]->linesize[p],
  115. linesize[p], height[p]);
  116. offset[p] += linesize[p];
  117. }
  118. }
  119. }
  120. return ff_filter_frame(outlink, out);
  121. }
  122. static int config_output(AVFilterLink *outlink)
  123. {
  124. AVFilterContext *ctx = outlink->src;
  125. StackContext *s = ctx->priv;
  126. AVRational time_base = ctx->inputs[0]->time_base;
  127. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  128. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  129. int height = ctx->inputs[0]->h;
  130. int width = ctx->inputs[0]->w;
  131. FFFrameSyncIn *in;
  132. int i, ret;
  133. if (s->is_vertical) {
  134. for (i = 1; i < s->nb_inputs; i++) {
  135. if (ctx->inputs[i]->w != width) {
  136. 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);
  137. return AVERROR(EINVAL);
  138. }
  139. height += ctx->inputs[i]->h;
  140. }
  141. } else {
  142. for (i = 1; i < s->nb_inputs; i++) {
  143. if (ctx->inputs[i]->h != height) {
  144. 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);
  145. return AVERROR(EINVAL);
  146. }
  147. width += ctx->inputs[i]->w;
  148. }
  149. }
  150. s->desc = av_pix_fmt_desc_get(outlink->format);
  151. if (!s->desc)
  152. return AVERROR_BUG;
  153. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  154. outlink->w = width;
  155. outlink->h = height;
  156. outlink->time_base = time_base;
  157. outlink->frame_rate = frame_rate;
  158. outlink->sample_aspect_ratio = sar;
  159. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  160. return ret;
  161. in = s->fs.in;
  162. s->fs.opaque = s;
  163. s->fs.on_event = process_frame;
  164. for (i = 0; i < s->nb_inputs; i++) {
  165. AVFilterLink *inlink = ctx->inputs[i];
  166. in[i].time_base = inlink->time_base;
  167. in[i].sync = 1;
  168. in[i].before = EXT_STOP;
  169. in[i].after = s->shortest ? EXT_STOP : EXT_INFINITY;
  170. }
  171. return ff_framesync_configure(&s->fs);
  172. }
  173. static av_cold void uninit(AVFilterContext *ctx)
  174. {
  175. StackContext *s = ctx->priv;
  176. int i;
  177. ff_framesync_uninit(&s->fs);
  178. av_freep(&s->frames);
  179. for (i = 0; i < ctx->nb_inputs; i++)
  180. av_freep(&ctx->input_pads[i].name);
  181. }
  182. static int activate(AVFilterContext *ctx)
  183. {
  184. StackContext *s = ctx->priv;
  185. return ff_framesync_activate(&s->fs);
  186. }
  187. #define OFFSET(x) offsetof(StackContext, x)
  188. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  189. static const AVOption stack_options[] = {
  190. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  191. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  192. { NULL },
  193. };
  194. static const AVFilterPad outputs[] = {
  195. {
  196. .name = "default",
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. .config_props = config_output,
  199. },
  200. { NULL }
  201. };
  202. #if CONFIG_HSTACK_FILTER
  203. #define hstack_options stack_options
  204. AVFILTER_DEFINE_CLASS(hstack);
  205. AVFilter ff_vf_hstack = {
  206. .name = "hstack",
  207. .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
  208. .priv_size = sizeof(StackContext),
  209. .priv_class = &hstack_class,
  210. .query_formats = query_formats,
  211. .outputs = outputs,
  212. .init = init,
  213. .uninit = uninit,
  214. .activate = activate,
  215. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  216. };
  217. #endif /* CONFIG_HSTACK_FILTER */
  218. #if CONFIG_VSTACK_FILTER
  219. #define vstack_options stack_options
  220. AVFILTER_DEFINE_CLASS(vstack);
  221. AVFilter ff_vf_vstack = {
  222. .name = "vstack",
  223. .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
  224. .priv_size = sizeof(StackContext),
  225. .priv_class = &vstack_class,
  226. .query_formats = query_formats,
  227. .outputs = outputs,
  228. .init = init,
  229. .uninit = uninit,
  230. .activate = activate,
  231. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  232. };
  233. #endif /* CONFIG_VSTACK_FILTER */