| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | #define OFFSET(x) offsetof(StackHWContext, x) | ||
| 20 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM) | ||
| 21 | |||
| 22 | #define SET_OUTPUT_REGION(region, rx, ry, rw, rh) do { \ | ||
| 23 | region->x = rx; \ | ||
| 24 | region->y = ry; \ | ||
| 25 | region->width = rw; \ | ||
| 26 | region->height = rh; \ | ||
| 27 | } while (0) | ||
| 28 | |||
| 29 | ✗ | static int init_framesync(AVFilterContext *avctx) | |
| 30 | { | ||
| 31 | ✗ | StackBaseContext *sctx = avctx->priv; | |
| 32 | int ret; | ||
| 33 | |||
| 34 | ✗ | ret = ff_framesync_init(&sctx->fs, avctx, avctx->nb_inputs); | |
| 35 | ✗ | if (ret < 0) | |
| 36 | ✗ | return ret; | |
| 37 | |||
| 38 | ✗ | sctx->fs.on_event = process_frame; | |
| 39 | ✗ | sctx->fs.opaque = sctx; | |
| 40 | |||
| 41 | ✗ | for (int i = 0; i < sctx->nb_inputs; i++) { | |
| 42 | ✗ | FFFrameSyncIn *in = &sctx->fs.in[i]; | |
| 43 | |||
| 44 | ✗ | in->before = EXT_STOP; | |
| 45 | ✗ | in->after = sctx->shortest ? EXT_STOP : EXT_INFINITY; | |
| 46 | ✗ | in->sync = 1; | |
| 47 | ✗ | in->time_base = avctx->inputs[i]->time_base; | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | return ff_framesync_configure(&sctx->fs); | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | static int config_comm_output(AVFilterLink *outlink) | |
| 54 | { | ||
| 55 | ✗ | FilterLink *outl = ff_filter_link(outlink); | |
| 56 | ✗ | AVFilterContext *avctx = outlink->src; | |
| 57 | ✗ | StackBaseContext *sctx = avctx->priv; | |
| 58 | ✗ | AVFilterLink *inlink0 = avctx->inputs[0]; | |
| 59 | ✗ | FilterLink *inl0 = ff_filter_link(inlink0); | |
| 60 | int width, height, ret; | ||
| 61 | |||
| 62 | ✗ | if (sctx->mode == STACK_H) { | |
| 63 | ✗ | height = sctx->tile_height; | |
| 64 | ✗ | width = 0; | |
| 65 | |||
| 66 | ✗ | if (!height) | |
| 67 | ✗ | height = inlink0->h; | |
| 68 | |||
| 69 | ✗ | for (int i = 0; i < sctx->nb_inputs; i++) { | |
| 70 | ✗ | AVFilterLink *inlink = avctx->inputs[i]; | |
| 71 | ✗ | StackItemRegion *region = &sctx->regions[i]; | |
| 72 | |||
| 73 | ✗ | SET_OUTPUT_REGION(region, width, 0, av_rescale(height, inlink->w, inlink->h), height); | |
| 74 | ✗ | width += av_rescale(height, inlink->w, inlink->h); | |
| 75 | } | ||
| 76 | ✗ | } else if (sctx->mode == STACK_V) { | |
| 77 | ✗ | height = 0; | |
| 78 | ✗ | width = sctx->tile_width; | |
| 79 | |||
| 80 | ✗ | if (!width) | |
| 81 | ✗ | width = inlink0->w; | |
| 82 | |||
| 83 | ✗ | for (int i = 0; i < sctx->nb_inputs; i++) { | |
| 84 | ✗ | AVFilterLink *inlink = avctx->inputs[i]; | |
| 85 | ✗ | StackItemRegion *region = &sctx->regions[i]; | |
| 86 | |||
| 87 | ✗ | SET_OUTPUT_REGION(region, 0, height, width, av_rescale(width, inlink->h, inlink->w)); | |
| 88 | ✗ | height += av_rescale(width, inlink->h, inlink->w); | |
| 89 | } | ||
| 90 | ✗ | } else if (sctx->nb_grid_rows && sctx->nb_grid_columns) { | |
| 91 | ✗ | int xpos = 0, ypos = 0; | |
| 92 | ✗ | int ow, oh, k = 0; | |
| 93 | |||
| 94 | ✗ | ow = sctx->tile_width; | |
| 95 | ✗ | oh = sctx->tile_height; | |
| 96 | |||
| 97 | ✗ | if (!ow || !oh) { | |
| 98 | ✗ | ow = avctx->inputs[0]->w; | |
| 99 | ✗ | oh = avctx->inputs[0]->h; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | for (int i = 0; i < sctx->nb_grid_columns; i++) { | |
| 103 | ✗ | ypos = 0; | |
| 104 | |||
| 105 | ✗ | for (int j = 0; j < sctx->nb_grid_rows; j++) { | |
| 106 | ✗ | StackItemRegion *region = &sctx->regions[k++]; | |
| 107 | |||
| 108 | ✗ | SET_OUTPUT_REGION(region, xpos, ypos, ow, oh); | |
| 109 | ✗ | ypos += oh; | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | xpos += ow; | |
| 113 | } | ||
| 114 | |||
| 115 | ✗ | width = ow * sctx->nb_grid_columns; | |
| 116 | ✗ | height = oh * sctx->nb_grid_rows; | |
| 117 | } else { | ||
| 118 | ✗ | char *arg, *p = sctx->layout, *saveptr = NULL; | |
| 119 | ✗ | char *arg2, *p2, *saveptr2 = NULL; | |
| 120 | ✗ | char *arg3, *p3, *saveptr3 = NULL; | |
| 121 | int xpos, ypos, size; | ||
| 122 | int ow, oh; | ||
| 123 | |||
| 124 | ✗ | width = 0; | |
| 125 | ✗ | height = 0; | |
| 126 | |||
| 127 | ✗ | for (int i = 0; i < sctx->nb_inputs; i++) { | |
| 128 | ✗ | AVFilterLink *inlink = avctx->inputs[i]; | |
| 129 | ✗ | StackItemRegion *region = &sctx->regions[i]; | |
| 130 | |||
| 131 | ✗ | ow = inlink->w; | |
| 132 | ✗ | oh = inlink->h; | |
| 133 | |||
| 134 | ✗ | if (!(arg = av_strtok(p, "|", &saveptr))) | |
| 135 | ✗ | return AVERROR(EINVAL); | |
| 136 | |||
| 137 | ✗ | p = NULL; | |
| 138 | ✗ | p2 = arg; | |
| 139 | ✗ | xpos = ypos = 0; | |
| 140 | |||
| 141 | ✗ | for (int j = 0; j < 3; j++) { | |
| 142 | ✗ | if (!(arg2 = av_strtok(p2, "_", &saveptr2))) { | |
| 143 | ✗ | if (j == 2) | |
| 144 | ✗ | break; | |
| 145 | else | ||
| 146 | ✗ | return AVERROR(EINVAL); | |
| 147 | } | ||
| 148 | |||
| 149 | ✗ | p2 = NULL; | |
| 150 | ✗ | p3 = arg2; | |
| 151 | |||
| 152 | ✗ | if (j == 2) { | |
| 153 | ✗ | if ((ret = av_parse_video_size(&ow, &oh, p3)) < 0) { | |
| 154 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid size '%s'\n", p3); | |
| 155 | ✗ | return ret; | |
| 156 | } | ||
| 157 | |||
| 158 | ✗ | break; | |
| 159 | } | ||
| 160 | |||
| 161 | ✗ | while ((arg3 = av_strtok(p3, "+", &saveptr3))) { | |
| 162 | ✗ | p3 = NULL; | |
| 163 | ✗ | if (sscanf(arg3, "w%d", &size) == 1) { | |
| 164 | ✗ | if (size == i || size < 0 || size >= sctx->nb_inputs) | |
| 165 | ✗ | return AVERROR(EINVAL); | |
| 166 | |||
| 167 | ✗ | if (!j) | |
| 168 | ✗ | xpos += sctx->regions[size].width; | |
| 169 | else | ||
| 170 | ✗ | ypos += sctx->regions[size].width; | |
| 171 | ✗ | } else if (sscanf(arg3, "h%d", &size) == 1) { | |
| 172 | ✗ | if (size == i || size < 0 || size >= sctx->nb_inputs) | |
| 173 | ✗ | return AVERROR(EINVAL); | |
| 174 | |||
| 175 | ✗ | if (!j) | |
| 176 | ✗ | xpos += sctx->regions[size].height; | |
| 177 | else | ||
| 178 | ✗ | ypos += sctx->regions[size].height; | |
| 179 | ✗ | } else if (sscanf(arg3, "%d", &size) == 1) { | |
| 180 | ✗ | if (size < 0) | |
| 181 | ✗ | return AVERROR(EINVAL); | |
| 182 | |||
| 183 | ✗ | if (!j) | |
| 184 | ✗ | xpos += size; | |
| 185 | else | ||
| 186 | ✗ | ypos += size; | |
| 187 | } else { | ||
| 188 | ✗ | return AVERROR(EINVAL); | |
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | ✗ | SET_OUTPUT_REGION(region, xpos, ypos, ow, oh); | |
| 194 | ✗ | width = FFMAX(width, xpos + ow); | |
| 195 | ✗ | height = FFMAX(height, ypos + oh); | |
| 196 | } | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | ✗ | outlink->w = width; | |
| 201 | ✗ | outlink->h = height; | |
| 202 | ✗ | outl->frame_rate = inl0->frame_rate; | |
| 203 | ✗ | outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio; | |
| 204 | |||
| 205 | ✗ | for (int i = 1; i < sctx->nb_inputs; i++) { | |
| 206 | ✗ | FilterLink *inlink = ff_filter_link(avctx->inputs[i]); | |
| 207 | ✗ | if (outl->frame_rate.num != inlink->frame_rate.num || | |
| 208 | ✗ | outl->frame_rate.den != inlink->frame_rate.den) { | |
| 209 | ✗ | av_log(avctx, AV_LOG_VERBOSE, | |
| 210 | "Video inputs have different frame rates, output will be VFR\n"); | ||
| 211 | ✗ | outl->frame_rate = av_make_q(1, 0); | |
| 212 | ✗ | break; | |
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | ✗ | ret = init_framesync(avctx); | |
| 217 | ✗ | if (ret < 0) | |
| 218 | ✗ | return ret; | |
| 219 | |||
| 220 | ✗ | outlink->time_base = sctx->fs.time_base; | |
| 221 | |||
| 222 | ✗ | return 0; | |
| 223 | } | ||
| 224 | |||
| 225 | ✗ | static int stack_init(AVFilterContext *avctx) | |
| 226 | { | ||
| 227 | ✗ | StackBaseContext *sctx = avctx->priv; | |
| 228 | int ret; | ||
| 229 | |||
| 230 | ✗ | if (!strcmp(avctx->filter->name, HSTACK_NAME)) | |
| 231 | ✗ | sctx->mode = STACK_H; | |
| 232 | ✗ | else if (!strcmp(avctx->filter->name, VSTACK_NAME)) | |
| 233 | ✗ | sctx->mode = STACK_V; | |
| 234 | else { | ||
| 235 | int is_grid; | ||
| 236 | |||
| 237 | ✗ | av_assert0(strcmp(avctx->filter->name, XSTACK_NAME) == 0); | |
| 238 | ✗ | sctx->mode = STACK_X; | |
| 239 | ✗ | is_grid = sctx->nb_grid_rows && sctx->nb_grid_columns; | |
| 240 | |||
| 241 | ✗ | if (sctx->layout && is_grid) { | |
| 242 | ✗ | av_log(avctx, AV_LOG_ERROR, "Both layout and grid were specified. Only one is allowed.\n"); | |
| 243 | ✗ | return AVERROR(EINVAL); | |
| 244 | } | ||
| 245 | |||
| 246 | ✗ | if (!sctx->layout && !is_grid) { | |
| 247 | ✗ | if (sctx->nb_inputs == 2) { | |
| 248 | ✗ | sctx->nb_grid_rows = 1; | |
| 249 | ✗ | sctx->nb_grid_columns = 2; | |
| 250 | ✗ | is_grid = 1; | |
| 251 | } else { | ||
| 252 | ✗ | av_log(avctx, AV_LOG_ERROR, "No layout or grid specified.\n"); | |
| 253 | ✗ | return AVERROR(EINVAL); | |
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | ✗ | if (is_grid) | |
| 258 | ✗ | sctx->nb_inputs = sctx->nb_grid_rows * sctx->nb_grid_columns; | |
| 259 | |||
| 260 | ✗ | if (strcmp(sctx->fillcolor_str, "none") && | |
| 261 | ✗ | av_parse_color(sctx->fillcolor, sctx->fillcolor_str, -1, avctx) >= 0) { | |
| 262 | ✗ | sctx->fillcolor_enable = 1; | |
| 263 | } else { | ||
| 264 | ✗ | sctx->fillcolor_enable = 0; | |
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | ✗ | for (int i = 0; i < sctx->nb_inputs; i++) { | |
| 269 | ✗ | AVFilterPad pad = { 0 }; | |
| 270 | |||
| 271 | ✗ | pad.type = AVMEDIA_TYPE_VIDEO; | |
| 272 | ✗ | pad.name = av_asprintf("input%d", i); | |
| 273 | |||
| 274 | ✗ | if (!pad.name) | |
| 275 | ✗ | return AVERROR(ENOMEM); | |
| 276 | |||
| 277 | ✗ | if ((ret = ff_append_inpad_free_name(avctx, &pad)) < 0) | |
| 278 | ✗ | return ret; | |
| 279 | } | ||
| 280 | |||
| 281 | ✗ | sctx->regions = av_calloc(sctx->nb_inputs, sizeof(*sctx->regions)); | |
| 282 | ✗ | if (!sctx->regions) | |
| 283 | ✗ | return AVERROR(ENOMEM); | |
| 284 | |||
| 285 | ✗ | return 0; | |
| 286 | } | ||
| 287 | |||
| 288 | ✗ | static av_cold void stack_uninit(AVFilterContext *avctx) | |
| 289 | { | ||
| 290 | ✗ | StackBaseContext *sctx = avctx->priv; | |
| 291 | |||
| 292 | ✗ | av_freep(&sctx->regions); | |
| 293 | ✗ | ff_framesync_uninit(&sctx->fs); | |
| 294 | ✗ | } | |
| 295 | |||
| 296 | ✗ | static int stack_activate(AVFilterContext *avctx) | |
| 297 | { | ||
| 298 | ✗ | StackBaseContext *sctx = avctx->priv; | |
| 299 | ✗ | return ff_framesync_activate(&sctx->fs); | |
| 300 | } | ||
| 301 | |||
| 302 | static const AVFilterPad stack_outputs[] = { | ||
| 303 | { | ||
| 304 | .name = "default", | ||
| 305 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 306 | .config_props = config_output, | ||
| 307 | }, | ||
| 308 | }; | ||
| 309 | |||
| 310 | #define STACK_COMMON_OPTS \ | ||
| 311 | { "inputs", "Set number of inputs", OFFSET(base.nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, UINT16_MAX, .flags = FLAGS }, \ | ||
| 312 | { "shortest", "Force termination when the shortest input terminates", OFFSET(base.shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
| 313 | |||
| 314 | #define DEFINE_HSTACK_OPTIONS(api) \ | ||
| 315 | static const AVOption hstack_##api##_options[] = { \ | ||
| 316 | STACK_COMMON_OPTS \ | ||
| 317 | { "height", "Set output height (0 to use the height of input 0)", OFFSET(base.tile_height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, FLAGS }, \ | ||
| 318 | { NULL } \ | ||
| 319 | } | ||
| 320 | |||
| 321 | #define DEFINE_VSTACK_OPTIONS(api) \ | ||
| 322 | static const AVOption vstack_##api##_options[] = { \ | ||
| 323 | STACK_COMMON_OPTS \ | ||
| 324 | { "width", "Set output width (0 to use the width of input 0)", OFFSET(base.tile_width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, FLAGS }, \ | ||
| 325 | { NULL } \ | ||
| 326 | } | ||
| 327 | |||
| 328 | #define DEFINE_XSTACK_OPTIONS(api) \ | ||
| 329 | static const AVOption xstack_##api##_options[] = { \ | ||
| 330 | STACK_COMMON_OPTS \ | ||
| 331 | { "layout", "Set custom layout", OFFSET(base.layout), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, .flags = FLAGS }, \ | ||
| 332 | { "grid", "set fixed size grid layout", OFFSET(base.nb_grid_columns), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, .flags = FLAGS }, \ | ||
| 333 | { "grid_tile_size", "set tile size in grid layout", OFFSET(base.tile_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, .flags = FLAGS }, \ | ||
| 334 | { "fill", "Set the color for unused pixels", OFFSET(base.fillcolor_str), AV_OPT_TYPE_STRING, {.str = "none"}, .flags = FLAGS }, \ | ||
| 335 | { NULL } \ | ||
| 336 | } | ||
| 337 | |||
| 338 | #define DEFINE_STACK_FILTER(category, api, capi, filter_flags) \ | ||
| 339 | static const AVClass category##_##api##_class = { \ | ||
| 340 | .class_name = #category "_" #api, \ | ||
| 341 | .item_name = av_default_item_name, \ | ||
| 342 | .option = category##_##api##_options, \ | ||
| 343 | .version = LIBAVUTIL_VERSION_INT, \ | ||
| 344 | }; \ | ||
| 345 | const FFFilter ff_vf_##category##_##api = { \ | ||
| 346 | .p.name = #category "_" #api, \ | ||
| 347 | .p.description = NULL_IF_CONFIG_SMALL(#capi " " #category), \ | ||
| 348 | .p.flags = AVFILTER_FLAG_DYNAMIC_INPUTS | filter_flags, \ | ||
| 349 | .p.priv_class = &category##_##api##_class, \ | ||
| 350 | .priv_size = sizeof(StackHWContext), \ | ||
| 351 | .init = api##_stack_init, \ | ||
| 352 | .uninit = api##_stack_uninit, \ | ||
| 353 | .activate = stack_activate, \ | ||
| 354 | FILTER_PIXFMTS_ARRAY(api ## _stack_pix_fmts), \ | ||
| 355 | FILTER_OUTPUTS(stack_outputs), \ | ||
| 356 | .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, \ | ||
| 357 | } | ||
| 358 |