vf_mergeplanes.c 10 KB

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