2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
28 #include "framesync.h"
30 typedef struct InputParam {
37 typedef struct MergePlanesContext {
40 const enum AVPixelFormat out_fmt;
46 const AVPixFmtDescriptor *outdesc;
51 #define OFFSET(x) offsetof(MergePlanesContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 static const AVOption mergeplanes_options[] = {
54 { "mapping", "set input to output plane mapping", OFFSET(mapping), AV_OPT_TYPE_INT, {.i64=0}, 0, 0x33333333, FLAGS },
55 { "format", "set output pixel format", OFFSET(out_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64=AV_PIX_FMT_YUVA444P}, 0, INT_MAX, .flags=FLAGS },
59 AVFILTER_DEFINE_CLASS(mergeplanes);
61 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
63 MergePlanesContext *s = inlink->dst->priv;
64 return ff_framesync_filter_frame(&s->fs, inlink, in);
67 static av_cold int init(AVFilterContext *ctx)
69 MergePlanesContext *s = ctx->priv;
70 int64_t m = s->mapping;
73 s->outdesc = av_pix_fmt_desc_get(s->out_fmt);
74 if (!(s->outdesc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
75 s->outdesc->nb_components < 2) {
76 av_log(ctx, AV_LOG_ERROR, "Only planar formats with more than one component are supported.\n");
77 return AVERROR(EINVAL);
79 s->nb_planes = av_pix_fmt_count_planes(s->out_fmt);
81 for (i = s->nb_planes - 1; i >= 0; i--) {
82 s->map[i][0] = m & 0xf;
84 s->map[i][1] = m & 0xf;
87 if (s->map[i][0] > 3 || s->map[i][1] > 3) {
88 av_log(ctx, AV_LOG_ERROR, "Mapping with out of range input and/or plane number.\n");
89 return AVERROR(EINVAL);
92 s->nb_inputs = FFMAX(s->nb_inputs, s->map[i][1] + 1);
95 av_assert0(s->nb_inputs && s->nb_inputs <= 4);
97 for (i = 0; i < s->nb_inputs; i++) {
98 AVFilterPad pad = { 0 };
100 pad.type = AVMEDIA_TYPE_VIDEO;
101 pad.name = av_asprintf("in%d", i);
103 return AVERROR(ENOMEM);
104 pad.filter_frame = filter_frame;
106 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0){
115 static int query_formats(AVFilterContext *ctx)
117 MergePlanesContext *s = ctx->priv;
118 AVFilterFormats *formats = NULL;
121 s->outdesc = av_pix_fmt_desc_get(s->out_fmt);
122 for (i = 0; av_pix_fmt_desc_get(i); i++) {
123 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
124 if (desc->comp[0].depth == s->outdesc->comp[0].depth &&
125 av_pix_fmt_count_planes(i) == desc->nb_components &&
126 (ret = ff_add_format(&formats, i)) < 0)
130 for (i = 0; i < s->nb_inputs; i++)
131 if ((ret = ff_formats_ref(formats, &ctx->inputs[i]->out_formats)) < 0)
135 if ((ret = ff_add_format(&formats, s->out_fmt)) < 0 ||
136 (ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
142 static int process_frame(FFFrameSync *fs)
144 AVFilterContext *ctx = fs->parent;
145 AVFilterLink *outlink = ctx->outputs[0];
146 MergePlanesContext *s = fs->opaque;
147 AVFrame *in[4] = { NULL };
151 for (i = 0; i < s->nb_inputs; i++) {
152 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
156 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
158 return AVERROR(ENOMEM);
159 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
161 for (i = 0; i < s->nb_planes; i++) {
162 const int input = s->map[i][1];
163 const int plane = s->map[i][0];
165 av_image_copy_plane(out->data[i], out->linesize[i],
166 in[input]->data[plane], in[input]->linesize[plane],
167 s->planewidth[i], s->planeheight[i]);
170 return ff_filter_frame(outlink, out);
173 static int config_output(AVFilterLink *outlink)
175 AVFilterContext *ctx = outlink->src;
176 MergePlanesContext *s = ctx->priv;
177 InputParam inputsp[4];
181 if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
186 s->fs.on_event = process_frame;
188 outlink->w = ctx->inputs[0]->w;
189 outlink->h = ctx->inputs[0]->h;
190 outlink->time_base = ctx->inputs[0]->time_base;
191 outlink->frame_rate = ctx->inputs[0]->frame_rate;
192 outlink->sample_aspect_ratio = ctx->inputs[0]->sample_aspect_ratio;
195 s->planewidth[2] = AV_CEIL_RSHIFT(outlink->w, s->outdesc->log2_chroma_w);
197 s->planewidth[3] = outlink->w;
199 s->planeheight[2] = AV_CEIL_RSHIFT(outlink->h, s->outdesc->log2_chroma_h);
201 s->planeheight[3] = outlink->h;
203 for (i = 0; i < s->nb_inputs; i++) {
204 InputParam *inputp = &inputsp[i];
205 AVFilterLink *inlink = ctx->inputs[i];
206 const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(inlink->format);
209 if (outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
210 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
211 av_log(ctx, AV_LOG_ERROR, "input #%d link %s SAR %d:%d "
212 "does not match output link %s SAR %d:%d\n",
213 i, ctx->input_pads[i].name,
214 inlink->sample_aspect_ratio.num,
215 inlink->sample_aspect_ratio.den,
216 ctx->output_pads[0].name,
217 outlink->sample_aspect_ratio.num,
218 outlink->sample_aspect_ratio.den);
219 return AVERROR(EINVAL);
222 inputp->planewidth[1] =
223 inputp->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, indesc->log2_chroma_w);
224 inputp->planewidth[0] =
225 inputp->planewidth[3] = inlink->w;
226 inputp->planeheight[1] =
227 inputp->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, indesc->log2_chroma_h);
228 inputp->planeheight[0] =
229 inputp->planeheight[3] = inlink->h;
230 inputp->nb_planes = av_pix_fmt_count_planes(inlink->format);
232 for (j = 0; j < inputp->nb_planes; j++)
233 inputp->depth[j] = indesc->comp[j].depth;
235 in[i].time_base = inlink->time_base;
237 in[i].before = EXT_STOP;
238 in[i].after = EXT_STOP;
241 for (i = 0; i < s->nb_planes; i++) {
242 const int input = s->map[i][1];
243 const int plane = s->map[i][0];
244 InputParam *inputp = &inputsp[input];
246 if (plane + 1 > inputp->nb_planes) {
247 av_log(ctx, AV_LOG_ERROR, "input %d does not have %d plane\n",
251 if (s->outdesc->comp[i].depth != inputp->depth[plane]) {
252 av_log(ctx, AV_LOG_ERROR, "output plane %d depth %d does not "
253 "match input %d plane %d depth %d\n",
254 i, s->outdesc->comp[i].depth,
255 input, plane, inputp->depth[plane]);
258 if (s->planewidth[i] != inputp->planewidth[plane]) {
259 av_log(ctx, AV_LOG_ERROR, "output plane %d width %d does not "
260 "match input %d plane %d width %d\n",
262 input, plane, inputp->planewidth[plane]);
265 if (s->planeheight[i] != inputp->planeheight[plane]) {
266 av_log(ctx, AV_LOG_ERROR, "output plane %d height %d does not "
267 "match input %d plane %d height %d\n",
268 i, s->planeheight[i],
269 input, plane, inputp->planeheight[plane]);
274 return ff_framesync_configure(&s->fs);
276 return AVERROR(EINVAL);
279 static int request_frame(AVFilterLink *outlink)
281 MergePlanesContext *s = outlink->src->priv;
282 return ff_framesync_request_frame(&s->fs, outlink);
285 static av_cold void uninit(AVFilterContext *ctx)
287 MergePlanesContext *s = ctx->priv;
290 ff_framesync_uninit(&s->fs);
292 for (i = 0; i < ctx->nb_inputs; i++)
293 av_freep(&ctx->input_pads[i].name);
296 static const AVFilterPad mergeplanes_outputs[] = {
299 .type = AVMEDIA_TYPE_VIDEO,
300 .config_props = config_output,
301 .request_frame = request_frame,
306 AVFilter ff_vf_mergeplanes = {
307 .name = "mergeplanes",
308 .description = NULL_IF_CONFIG_SMALL("Merge planes."),
309 .priv_size = sizeof(MergePlanesContext),
310 .priv_class = &mergeplanes_class,
313 .query_formats = query_formats,
315 .outputs = mergeplanes_outputs,
316 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,