vf_mergeplanes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (c) 2013 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/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "framesync.h"
  28. typedef struct InputParam {
  29. int depth[4];
  30. int nb_planes;
  31. int planewidth[4];
  32. int planeheight[4];
  33. } InputParam;
  34. typedef struct MergePlanesContext {
  35. const AVClass *class;
  36. int64_t mapping;
  37. const enum AVPixelFormat out_fmt;
  38. int nb_inputs;
  39. int nb_planes;
  40. int planewidth[4];
  41. int planeheight[4];
  42. int map[4][2];
  43. const AVPixFmtDescriptor *outdesc;
  44. FFFrameSync fs;
  45. } MergePlanesContext;
  46. #define OFFSET(x) offsetof(MergePlanesContext, x)
  47. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  48. static const AVOption mergeplanes_options[] = {
  49. { "mapping", "set input to output plane mapping", OFFSET(mapping), AV_OPT_TYPE_INT, {.i64=0}, 0, 0x33333333, FLAGS },
  50. { "format", "set output pixel format", OFFSET(out_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64=AV_PIX_FMT_YUVA444P}, 0, INT_MAX, .flags=FLAGS },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(mergeplanes);
  54. static av_cold int init(AVFilterContext *ctx)
  55. {
  56. MergePlanesContext *s = ctx->priv;
  57. int64_t m = s->mapping;
  58. int i, ret;
  59. s->outdesc = av_pix_fmt_desc_get(s->out_fmt);
  60. if (!(s->outdesc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
  61. s->outdesc->nb_components < 2) {
  62. av_log(ctx, AV_LOG_ERROR, "Only planar formats with more than one component are supported.\n");
  63. return AVERROR(EINVAL);
  64. }
  65. s->nb_planes = av_pix_fmt_count_planes(s->out_fmt);
  66. for (i = s->nb_planes - 1; i >= 0; i--) {
  67. s->map[i][0] = m & 0xf;
  68. m >>= 4;
  69. s->map[i][1] = m & 0xf;
  70. m >>= 4;
  71. if (s->map[i][0] > 3 || s->map[i][1] > 3) {
  72. av_log(ctx, AV_LOG_ERROR, "Mapping with out of range input and/or plane number.\n");
  73. return AVERROR(EINVAL);
  74. }
  75. s->nb_inputs = FFMAX(s->nb_inputs, s->map[i][1] + 1);
  76. }
  77. av_assert0(s->nb_inputs && s->nb_inputs <= 4);
  78. for (i = 0; i < s->nb_inputs; i++) {
  79. AVFilterPad pad = { 0 };
  80. pad.type = AVMEDIA_TYPE_VIDEO;
  81. pad.name = av_asprintf("in%d", i);
  82. if (!pad.name)
  83. return AVERROR(ENOMEM);
  84. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0){
  85. av_freep(&pad.name);
  86. return ret;
  87. }
  88. }
  89. return 0;
  90. }
  91. static int query_formats(AVFilterContext *ctx)
  92. {
  93. MergePlanesContext *s = ctx->priv;
  94. AVFilterFormats *formats = NULL;
  95. int i, ret;
  96. s->outdesc = av_pix_fmt_desc_get(s->out_fmt);
  97. for (i = 0; av_pix_fmt_desc_get(i); i++) {
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  99. if (desc->comp[0].depth == s->outdesc->comp[0].depth &&
  100. (desc->comp[0].depth <= 8 || (desc->flags & AV_PIX_FMT_FLAG_BE) == (s->outdesc->flags & AV_PIX_FMT_FLAG_BE)) &&
  101. av_pix_fmt_count_planes(i) == desc->nb_components &&
  102. (ret = ff_add_format(&formats, i)) < 0)
  103. return ret;
  104. }
  105. for (i = 0; i < s->nb_inputs; i++)
  106. if ((ret = ff_formats_ref(formats, &ctx->inputs[i]->out_formats)) < 0)
  107. return ret;
  108. formats = NULL;
  109. if ((ret = ff_add_format(&formats, s->out_fmt)) < 0 ||
  110. (ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
  111. return ret;
  112. return 0;
  113. }
  114. static int process_frame(FFFrameSync *fs)
  115. {
  116. AVFilterContext *ctx = fs->parent;
  117. AVFilterLink *outlink = ctx->outputs[0];
  118. MergePlanesContext *s = fs->opaque;
  119. AVFrame *in[4] = { NULL };
  120. AVFrame *out;
  121. int i, ret;
  122. for (i = 0; i < s->nb_inputs; i++) {
  123. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  124. return ret;
  125. }
  126. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  127. if (!out)
  128. return AVERROR(ENOMEM);
  129. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  130. for (i = 0; i < s->nb_planes; i++) {
  131. const int input = s->map[i][1];
  132. const int plane = s->map[i][0];
  133. av_image_copy_plane(out->data[i], out->linesize[i],
  134. in[input]->data[plane], in[input]->linesize[plane],
  135. s->planewidth[i], s->planeheight[i]);
  136. }
  137. return ff_filter_frame(outlink, out);
  138. }
  139. static int config_output(AVFilterLink *outlink)
  140. {
  141. AVFilterContext *ctx = outlink->src;
  142. MergePlanesContext *s = ctx->priv;
  143. InputParam inputsp[4];
  144. FFFrameSyncIn *in;
  145. int i, ret;
  146. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  147. return ret;
  148. in = s->fs.in;
  149. s->fs.opaque = s;
  150. s->fs.on_event = process_frame;
  151. outlink->w = ctx->inputs[0]->w;
  152. outlink->h = ctx->inputs[0]->h;
  153. outlink->time_base = ctx->inputs[0]->time_base;
  154. outlink->frame_rate = ctx->inputs[0]->frame_rate;
  155. outlink->sample_aspect_ratio = ctx->inputs[0]->sample_aspect_ratio;
  156. s->planewidth[1] =
  157. s->planewidth[2] = AV_CEIL_RSHIFT(((s->outdesc->comp[1].depth > 8) + 1) * outlink->w, s->outdesc->log2_chroma_w);
  158. s->planewidth[0] =
  159. s->planewidth[3] = ((s->outdesc->comp[0].depth > 8) + 1) * outlink->w;
  160. s->planeheight[1] =
  161. s->planeheight[2] = AV_CEIL_RSHIFT(outlink->h, s->outdesc->log2_chroma_h);
  162. s->planeheight[0] =
  163. s->planeheight[3] = outlink->h;
  164. for (i = 0; i < s->nb_inputs; i++) {
  165. InputParam *inputp = &inputsp[i];
  166. AVFilterLink *inlink = ctx->inputs[i];
  167. const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(inlink->format);
  168. int j;
  169. if (outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
  170. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  171. av_log(ctx, AV_LOG_ERROR, "input #%d link %s SAR %d:%d "
  172. "does not match output link %s SAR %d:%d\n",
  173. i, ctx->input_pads[i].name,
  174. inlink->sample_aspect_ratio.num,
  175. inlink->sample_aspect_ratio.den,
  176. ctx->output_pads[0].name,
  177. outlink->sample_aspect_ratio.num,
  178. outlink->sample_aspect_ratio.den);
  179. return AVERROR(EINVAL);
  180. }
  181. inputp->planewidth[1] =
  182. inputp->planewidth[2] = AV_CEIL_RSHIFT(((indesc->comp[1].depth > 8) + 1) * inlink->w, indesc->log2_chroma_w);
  183. inputp->planewidth[0] =
  184. inputp->planewidth[3] = ((indesc->comp[0].depth > 8) + 1) * inlink->w;
  185. inputp->planeheight[1] =
  186. inputp->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, indesc->log2_chroma_h);
  187. inputp->planeheight[0] =
  188. inputp->planeheight[3] = inlink->h;
  189. inputp->nb_planes = av_pix_fmt_count_planes(inlink->format);
  190. for (j = 0; j < inputp->nb_planes; j++)
  191. inputp->depth[j] = indesc->comp[j].depth;
  192. in[i].time_base = inlink->time_base;
  193. in[i].sync = 1;
  194. in[i].before = EXT_STOP;
  195. in[i].after = EXT_STOP;
  196. }
  197. for (i = 0; i < s->nb_planes; i++) {
  198. const int input = s->map[i][1];
  199. const int plane = s->map[i][0];
  200. InputParam *inputp = &inputsp[input];
  201. if (plane + 1 > inputp->nb_planes) {
  202. av_log(ctx, AV_LOG_ERROR, "input %d does not have %d plane\n",
  203. input, plane);
  204. goto fail;
  205. }
  206. if (s->outdesc->comp[i].depth != inputp->depth[plane]) {
  207. av_log(ctx, AV_LOG_ERROR, "output plane %d depth %d does not "
  208. "match input %d plane %d depth %d\n",
  209. i, s->outdesc->comp[i].depth,
  210. input, plane, inputp->depth[plane]);
  211. goto fail;
  212. }
  213. if (s->planewidth[i] != inputp->planewidth[plane]) {
  214. av_log(ctx, AV_LOG_ERROR, "output plane %d width %d does not "
  215. "match input %d plane %d width %d\n",
  216. i, s->planewidth[i],
  217. input, plane, inputp->planewidth[plane]);
  218. goto fail;
  219. }
  220. if (s->planeheight[i] != inputp->planeheight[plane]) {
  221. av_log(ctx, AV_LOG_ERROR, "output plane %d height %d does not "
  222. "match input %d plane %d height %d\n",
  223. i, s->planeheight[i],
  224. input, plane, inputp->planeheight[plane]);
  225. goto fail;
  226. }
  227. }
  228. return ff_framesync_configure(&s->fs);
  229. fail:
  230. return AVERROR(EINVAL);
  231. }
  232. static int activate(AVFilterContext *ctx)
  233. {
  234. MergePlanesContext *s = ctx->priv;
  235. return ff_framesync_activate(&s->fs);
  236. }
  237. static av_cold void uninit(AVFilterContext *ctx)
  238. {
  239. MergePlanesContext *s = ctx->priv;
  240. int i;
  241. ff_framesync_uninit(&s->fs);
  242. for (i = 0; i < ctx->nb_inputs; i++)
  243. av_freep(&ctx->input_pads[i].name);
  244. }
  245. static const AVFilterPad mergeplanes_outputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. .config_props = config_output,
  250. },
  251. { NULL }
  252. };
  253. AVFilter ff_vf_mergeplanes = {
  254. .name = "mergeplanes",
  255. .description = NULL_IF_CONFIG_SMALL("Merge planes."),
  256. .priv_size = sizeof(MergePlanesContext),
  257. .priv_class = &mergeplanes_class,
  258. .init = init,
  259. .uninit = uninit,
  260. .query_formats = query_formats,
  261. .activate = activate,
  262. .inputs = NULL,
  263. .outputs = mergeplanes_outputs,
  264. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  265. };