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.

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