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