vf_mergeplanes.c 10 KB

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