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.

272 lines
7.9KB

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