vf_stack.c 8.2 KB

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