FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_mergeplanes.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 112 139 80.6%
Branches: 50 80 62.5%

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