|
39 | 39 | #include "sub/osd.h" |
40 | 40 | #include "video/hwdec.h" |
41 | 41 | #include "filters/f_decoder_wrapper.h" |
| 42 | +#include "filters/f_lavfi.h" |
42 | 43 | #include "video/out/vo.h" |
43 | 44 |
|
44 | 45 | #include "core.h" |
@@ -155,10 +156,33 @@ static void vo_chain_uninit(struct vo_chain *vo_c) |
155 | 156 | // this does not free the VO |
156 | 157 | } |
157 | 158 |
|
| 159 | +static void uninit_grid(struct MPContext *mpctx) |
| 160 | +{ |
| 161 | + struct track *primary = mpctx->vo_chain->track; |
| 162 | + if (!primary || !primary->stream || !primary->stream->tile_grid) |
| 163 | + return; |
| 164 | + |
| 165 | + struct mp_tile_grid *grid = primary->stream->tile_grid; |
| 166 | + for (int n = 0; n < mpctx->num_tracks; n++) { |
| 167 | + struct track *track = mpctx->tracks[n]; |
| 168 | + if (!track->stream || track->stream->tile_grid != grid) |
| 169 | + continue; |
| 170 | + if (track->sink) { |
| 171 | + mp_pin_disconnect(track->sink); |
| 172 | + track->sink = NULL; |
| 173 | + } |
| 174 | + if (track != primary) |
| 175 | + track->dec = NULL; |
| 176 | + track->selected = false; |
| 177 | + reselect_demux_stream(mpctx, track, false); |
| 178 | + } |
| 179 | +} |
| 180 | + |
158 | 181 | void uninit_video_chain(struct MPContext *mpctx) |
159 | 182 | { |
160 | 183 | if (mpctx->vo_chain) { |
161 | 184 | reset_video_state(mpctx); |
| 185 | + uninit_grid(mpctx); |
162 | 186 | vo_chain_uninit(mpctx->vo_chain); |
163 | 187 | mpctx->vo_chain = NULL; |
164 | 188 |
|
@@ -201,14 +225,151 @@ int init_video_decoder(struct MPContext *mpctx, struct track *track) |
201 | 225 | return 0; |
202 | 226 | } |
203 | 227 |
|
| 228 | +static char *tile_grid_graph(void *ctx, const struct mp_tile_grid *grid) |
| 229 | +{ |
| 230 | + bstr buf = {0}; |
| 231 | + |
| 232 | + for (int i = 0; i < grid->nb_tiles; i++) |
| 233 | + bstr_xappend_asprintf(ctx, &buf, "[in%d]", i); |
| 234 | + |
| 235 | + bstr_xappend_asprintf(ctx, &buf, "xstack=inputs=%d:layout=", grid->nb_tiles); |
| 236 | + for (int i = 0; i < grid->nb_tiles; i++) { |
| 237 | + if (i > 0) |
| 238 | + bstr_xappend(ctx, &buf, bstr0("|")); |
| 239 | + bstr_xappend_asprintf(ctx, &buf, "%d_%d", grid->tiles[i].horizontal, |
| 240 | + grid->tiles[i].vertical); |
| 241 | + } |
| 242 | + bstr_xappend_asprintf(ctx, &buf, |
| 243 | + ":fill=0x%02X%02X%02X@0x%02X", |
| 244 | + grid->background[0], grid->background[1], |
| 245 | + grid->background[2], grid->background[3]); |
| 246 | + |
| 247 | + if (grid->coded_width != grid->width || grid->coded_height != grid->height) { |
| 248 | + bstr_xappend_asprintf(ctx, &buf, ",crop=w=%d:h=%d:x=%d:y=%d", grid->width, |
| 249 | + grid->height, grid->horizontal_offset, grid->vertical_offset); |
| 250 | + } |
| 251 | + |
| 252 | + bstr_xappend(ctx, &buf, bstr0("[vo]")); |
| 253 | + return buf.start; |
| 254 | +} |
| 255 | + |
| 256 | +static struct track *find_tile_track(struct MPContext *mpctx, |
| 257 | + const struct mp_tile_grid *tg, int tile_idx) |
| 258 | +{ |
| 259 | + |
| 260 | + int wanted_ff = tg->tiles[tile_idx].ff_index; |
| 261 | + for (int n = 0; n < mpctx->num_tracks; n++) { |
| 262 | + struct track *t = mpctx->tracks[n]; |
| 263 | + if (t->ff_index == wanted_ff && t->stream && t->stream->tile_grid == tg) |
| 264 | + return t; |
| 265 | + } |
| 266 | + return NULL; |
| 267 | +} |
| 268 | + |
| 269 | +static void reinit_video_chain_tiled(struct MPContext *mpctx, struct track *track) |
| 270 | +{ |
| 271 | + struct mp_tile_grid *grid = track->stream->tile_grid; |
| 272 | + mp_assert(grid); |
| 273 | + |
| 274 | + for (int i = 0; i < grid->nb_tiles; i++) { |
| 275 | + struct track *t = find_tile_track(mpctx, grid, i); |
| 276 | + if (t) { |
| 277 | + t->selected = true; |
| 278 | + reselect_demux_stream(mpctx, t, false); |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + reinit_video_chain_src(mpctx, NULL); |
| 283 | + if (!mpctx->vo_chain) |
| 284 | + return; |
| 285 | + |
| 286 | + struct vo_chain *vo_c = mpctx->vo_chain; |
| 287 | + |
| 288 | + void *tmp = talloc_new(NULL); |
| 289 | + char *graph_str = tile_grid_graph(tmp, grid); |
| 290 | + MP_VERBOSE(mpctx, "Tile grid xstack graph: %s\n", graph_str); |
| 291 | + |
| 292 | + struct mp_lavfi *lavfi = |
| 293 | + mp_lavfi_create_graph(vo_c->filter->f, 0, false, NULL, NULL, graph_str); |
| 294 | + talloc_free(tmp); |
| 295 | + |
| 296 | + if (!lavfi) { |
| 297 | + MP_ERR(mpctx, "Failed to create tile grid filtergraph.\n"); |
| 298 | + goto err_out; |
| 299 | + } |
| 300 | + |
| 301 | + struct mp_filter *lavfi_f = lavfi->f; |
| 302 | + |
| 303 | + struct mp_pin *out_pad = mp_filter_get_named_pin(lavfi_f, "vo"); |
| 304 | + if (!out_pad || mp_pin_get_dir(out_pad) != MP_PIN_OUT) { |
| 305 | + MP_ERR(mpctx, "Tile grid filtergraph missing output pin 'vo'.\n"); |
| 306 | + goto err_out; |
| 307 | + } |
| 308 | + vo_c->filter_src = out_pad; |
| 309 | + mp_pin_connect(vo_c->filter->f->pins[0], vo_c->filter_src); |
| 310 | + |
| 311 | + for (int i = 0; i < grid->nb_tiles; i++) { |
| 312 | + struct track *tile_track = find_tile_track(mpctx, grid, i); |
| 313 | + if (!tile_track) { |
| 314 | + MP_ERR(mpctx, "No track found for tile %d (ff_index %d).\n", |
| 315 | + i, grid->tiles[i].ff_index); |
| 316 | + goto err_out; |
| 317 | + } |
| 318 | + |
| 319 | + tile_track->vo_c = vo_c; |
| 320 | + bool result = init_video_decoder(mpctx, tile_track); |
| 321 | + // vo_chain_uninit() only unsets vo_c on the primary track |
| 322 | + // (vo_c->track). |
| 323 | + tile_track->vo_c = NULL; |
| 324 | + if (!result) |
| 325 | + goto err_out; |
| 326 | + |
| 327 | + char label[16]; |
| 328 | + snprintf(label, sizeof(label), "in%d", i); |
| 329 | + struct mp_pin *in_pad = mp_filter_get_named_pin(lavfi_f, label); |
| 330 | + if (!in_pad || mp_pin_get_dir(in_pad) != MP_PIN_IN) { |
| 331 | + MP_ERR(mpctx, "Tile grid filtergraph missing input pin '%s'.\n", |
| 332 | + label); |
| 333 | + goto err_out; |
| 334 | + } |
| 335 | + tile_track->sink = in_pad; |
| 336 | + mp_pin_connect(tile_track->sink, tile_track->dec->f->pins[0]); |
| 337 | + } |
| 338 | + |
| 339 | + struct track *primary = find_tile_track(mpctx, grid, 0); |
| 340 | + vo_c->track = primary; |
| 341 | + primary->vo_c = vo_c; |
| 342 | + vo_c->filter->container_fps = |
| 343 | + mp_decoder_wrapper_get_container_fps(primary->dec); |
| 344 | + vo_c->is_coverart = !!primary->attached_picture; |
| 345 | + vo_c->is_sparse = primary->stream->still_image || vo_c->is_coverart; |
| 346 | + |
| 347 | + if (vo_c->is_coverart) |
| 348 | + mp_decoder_wrapper_set_coverart_flag(track->dec, true); |
| 349 | + |
| 350 | + MP_VERBOSE(mpctx, "Tile grid: assembling %d tile(s) into %dx%d image.\n", |
| 351 | + grid->nb_tiles, grid->width, grid->height); |
| 352 | + return; |
| 353 | + |
| 354 | +err_out: |
| 355 | + uninit_video_chain(mpctx); |
| 356 | + error_on_track(mpctx, track); |
| 357 | + handle_force_window(mpctx, true); |
| 358 | +} |
| 359 | + |
204 | 360 | void reinit_video_chain(struct MPContext *mpctx) |
205 | 361 | { |
206 | 362 | struct track *track = mpctx->current_track[0][STREAM_VIDEO]; |
207 | 363 | if (!track || !track->stream) { |
208 | 364 | error_on_track(mpctx, track); |
209 | 365 | return; |
210 | 366 | } |
211 | | - reinit_video_chain_src(mpctx, track); |
| 367 | + |
| 368 | + if (track->stream->tile_grid) { |
| 369 | + reinit_video_chain_tiled(mpctx, track); |
| 370 | + } else { |
| 371 | + reinit_video_chain_src(mpctx, track); |
| 372 | + } |
212 | 373 | } |
213 | 374 |
|
214 | 375 | static void filter_update_subtitles(void *ctx, double pts) |
|
0 commit comments