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