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.

315 lines
10KB

  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;
  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. ff_add_format(&formats, i);
  108. }
  109. for (i = 0; i < s->nb_inputs; i++)
  110. ff_formats_ref(formats, &ctx->inputs[i]->out_formats);
  111. formats = NULL;
  112. ff_add_format(&formats, s->out_fmt);
  113. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  114. return 0;
  115. }
  116. static int process_frame(FFFrameSync *fs)
  117. {
  118. AVFilterContext *ctx = fs->parent;
  119. AVFilterLink *outlink = ctx->outputs[0];
  120. MergePlanesContext *s = fs->opaque;
  121. AVFrame *in[4] = { NULL };
  122. AVFrame *out;
  123. int i, ret;
  124. for (i = 0; i < s->nb_inputs; i++) {
  125. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  126. return ret;
  127. }
  128. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  129. if (!out)
  130. return AVERROR(ENOMEM);
  131. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  132. for (i = 0; i < s->nb_planes; i++) {
  133. const int input = s->map[i][1];
  134. const int plane = s->map[i][0];
  135. av_image_copy_plane(out->data[i], out->linesize[i],
  136. in[input]->data[plane], in[input]->linesize[plane],
  137. s->planewidth[i], s->planeheight[i]);
  138. }
  139. return ff_filter_frame(outlink, out);
  140. }
  141. static int config_output(AVFilterLink *outlink)
  142. {
  143. AVFilterContext *ctx = outlink->src;
  144. MergePlanesContext *s = ctx->priv;
  145. InputParam inputsp[4];
  146. FFFrameSyncIn *in;
  147. int i, ret;
  148. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  149. return ret;
  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;
  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 != 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,
  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. };