Blender V5.3
strip_transform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2003-2009 Blender Authors
3 * SPDX-FileCopyrightText: 2005-2006 Peter Schlaile <peter [at] schlaile [dot] de>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later */
6
10
11#include <algorithm>
12#include <optional>
13
14#include "DNA_movieclip_types.h"
15#include "DNA_scene_types.h"
16#include "DNA_sequence_types.h"
17
18#include "BLI_bounds.hh"
19#include "BLI_listbase.hh"
20#include "BLI_math_base_c.hh"
21#include "BLI_math_matrix.hh"
23#include "BLI_rect.hh"
24
25#include "BLF_api.hh"
26
27#include "SEQ_animation.hh"
28#include "SEQ_channels.hh"
29#include "SEQ_edit.hh"
30#include "SEQ_iterator.hh"
31#include "SEQ_relations.hh"
32#include "SEQ_render.hh"
33#include "SEQ_sequencer.hh"
34#include "SEQ_time.hh"
35#include "SEQ_transform.hh"
36
37#include "effects/effects.hh"
38#include "sequencer.hh"
39#include "strip_time.hh"
40
41namespace blender::seq {
42
43/* -------------------------------------------------------------------- */
46
48{
49 return (strip->flag & SEQ_SINGLE_FRAME_CONTENT) != 0;
50}
51
52bool transform_is_locked(const ListBaseT<SeqTimelineChannel> *channels, const Strip *strip)
53{
54 const SeqTimelineChannel *channel = channel_get_by_index(channels, strip->channel);
55 return strip->flag & SEQ_LOCK ||
56 (channel->is_locked() &&
57 !flag_is_set(strip->runtime->flag, StripRuntimeFlag::IgnoreChannelLock));
58}
59
61{
62 return !strip->is_effect_with_inputs();
63}
64
65bool transform_test_overlap(const Scene *scene, Strip *strip1, Strip *strip2)
66{
67 return (strip1 != strip2 && strip1->channel == strip2->channel &&
68 ((strip1->right_handle(scene) <= strip2->left_handle()) ||
69 (strip1->left_handle() >= strip2->right_handle(scene))) == 0);
70}
71
72bool transform_test_overlap(const Scene *scene, ListBaseT<Strip> *seqbasep, Strip *test)
73{
74 for (Strip &strip : *seqbasep) {
75 if (transform_test_overlap(scene, test, &strip)) {
76 return true;
77 }
78 }
79 return false;
80}
81
83
84/* -------------------------------------------------------------------- */
87
88void transform_translate_strip(Scene *evil_scene, Strip *strip, int delta)
89{
90 if (delta == 0) {
91 return;
92 }
93
94 /* Meta strips requires their content is to be translated, and then frame range of the meta is
95 * updated based on nested strips. This won't work for empty meta-strips,
96 * so they can be treated as normal strip. */
97 if (strip->type == STRIP_TYPE_META && !strip->seqbase.is_empty()) {
98 for (Strip &strip_child : strip->seqbase) {
99 transform_translate_strip(evil_scene, &strip_child, delta);
100 }
101 /* Move meta start/end points. */
102 const int left_handle = strip->left_handle();
103 const int right_handle = strip->right_handle(evil_scene);
104 strip->handles_set(evil_scene, left_handle + delta, right_handle + delta);
105 }
106 else if (strip->input1 == nullptr && strip->input2 == nullptr) { /* All other strip types. */
107 strip->start += delta;
108 /* Only to make files usable in older versions. */
109 strip->startdisp = strip->left_handle();
110 strip->enddisp = strip->right_handle(evil_scene);
111 }
112
113 offset_animdata(evil_scene, strip, delta);
114 Span<Strip *> effects = lookup_effects_by_strip(evil_scene->ed, strip);
115 strip_time_update_effects_strip_range(evil_scene, effects);
116 time_update_meta_strip_range(evil_scene, lookup_meta_by_strip(evil_scene->ed, strip));
117}
118
119static bool strips_overlap_at_offset(const Scene *scene,
120 const Strip *strip,
121 const Strip *other,
122 const int offset)
123{
124 BLI_assert(strip != other);
125 return strip->channel == other->channel &&
126 strip->left_handle() + offset < other->right_handle(scene) &&
127 strip->right_handle(scene) + offset > other->left_handle();
128}
129
130static int shuffle_frame_offset_get(const Scene *scene,
131 Span<Strip *> strips_to_shuffle,
132 ListBaseT<Strip> *seqbasep,
133 const Side side)
134{
136
137 int offset = 0;
138 int offset_prev;
139
140 do {
141 offset_prev = offset;
142 for (Strip *strip : strips_to_shuffle) {
143 for (Strip &other : *seqbasep) {
144 if (strips_to_shuffle.contains(&other) || relation_is_effect_of_strip(&other, strip) ||
145 !strips_overlap_at_offset(scene, strip, &other, offset))
146 {
147 continue;
148 }
149
150 offset = (side == Side::Left) ?
151 min_ii(offset, other.left_handle() - strip->right_handle(scene)) :
152 max_ii(offset, other.right_handle(scene) - strip->left_handle());
153 }
154 }
155 } while (offset != offset_prev);
156
157 return offset;
158}
159
161 ListBaseT<Strip> *seqbasep,
162 Span<Strip *> strips_to_shuffle,
163 Span<Strip *> time_dependent_strips,
164 const bool use_sync_markers)
165{
166 const int offset_l = shuffle_frame_offset_get(scene, strips_to_shuffle, seqbasep, Side::Left);
167 const int offset_r = shuffle_frame_offset_get(scene, strips_to_shuffle, seqbasep, Side::Right);
168 const int offset = (-offset_l < offset_r) ? offset_l : offset_r;
169
170 if (offset == 0) {
171 return;
172 }
173
174 for (Strip *strip : strips_to_shuffle) {
175 transform_translate_strip(scene, strip, offset);
176 }
177 for (Strip *strip : time_dependent_strips) {
178 offset_animdata(scene, strip, offset);
179 }
180
181 if (use_sync_markers && !scene->toolsettings->lock_markers) {
182 /* affect selected markers - it's unlikely that we will want to affect all in this way? */
183 for (TimeMarker &marker : scene->markers) {
184 if (marker.flag & SELECT) {
185 marker.frame += offset;
186 }
187 }
188 }
189}
190
191static bool shuffle_channel_is_free(const Scene *scene,
192 ListBaseT<Strip> *seqbasep,
193 const ListBaseT<SeqTimelineChannel> *channels,
194 Strip *strip)
195{
196 const SeqTimelineChannel *channel = channel_get_by_index(channels, strip->channel);
197 return !channel->is_muted() && !channel->is_locked() &&
198 !transform_test_overlap(scene, seqbasep, strip);
199}
200
201void transform_shuffle_vertical(ListBaseT<Strip> *seqbasep,
202 Span<Strip *> strips,
203 Scene *scene,
204 const int channel_delta)
205{
206 BLI_assert(ELEM(channel_delta, -1, 1));
207
208 if (!std::ranges::any_of(
209 strips, [&](Strip *strip) { return transform_test_overlap(scene, seqbasep, strip); }))
210 {
211 return;
212 }
213
214 const ListBaseT<SeqTimelineChannel> *channels = channels_displayed_get(editing_get(scene));
215 Vector<int> orig_channels;
216 int min_channel = MAX_CHANNELS;
217 int max_channel = 1;
218 for (Strip *strip : strips) {
219 orig_channels.append(strip->channel);
220 min_channel = math::min(min_channel, strip->channel);
221 max_channel = math::max(max_channel, strip->channel);
222 }
223
224 for (int delta = channel_delta; min_channel + delta >= 1 && max_channel + delta <= MAX_CHANNELS;
225 delta += channel_delta)
226 {
227 /* Temporarily override the strips' channels to easily check for overlaps. */
228 for (const int i : strips.index_range()) {
229 strips[i]->channel_set(orig_channels[i] + delta);
230 }
231 if (std::ranges::all_of(strips,
232 [&](Strip *strip) {
233 return shuffle_channel_is_free(scene, seqbasep, channels, strip);
234 }))
235 {
236 return;
237 }
238 }
239
240 /* Strips cannot be moved to next free channel, fallback to horizontal shuffling. */
241 for (const int i : strips.index_range()) {
242 strips[i]->channel_set(orig_channels[i]);
243 }
244 transform_shuffle_horizontal(scene, seqbasep, strips, {}, false);
245}
246
248{
249 VectorSet<Strip *> standalone_strips;
250
251 for (Strip *strip : transformed_strips) {
252 if (!strip->is_effect_with_inputs()) {
253 standalone_strips.add(strip);
254 }
255 }
256 return standalone_strips;
257}
258
259rcti strip_int_bounds_get(const Scene *scene, const Strip *strip)
260{
261 rcti bounds;
262 bounds.xmin = strip->left_handle();
263 bounds.xmax = strip->right_handle(scene);
264 bounds.ymin = strip->channel;
265 bounds.ymax = strip->channel;
266 return bounds;
267}
268
270{
271 Vector<Strip *> sorted_strips(strips);
272 std::ranges::sort(sorted_strips, [](const Strip *a, const Strip *b) {
273 return a->left_handle() < b->left_handle();
274 });
275
276 Vector<RippleRange> ranges;
277 for (const Strip *strip : sorted_strips) {
278 if (!ranges.is_empty() && strip->left_handle() <= ranges.last().end) {
279 /* Attempt to extend an existing range. */
280 ranges.last().end = math::max(ranges.last().end, strip->right_handle(scene));
281 ranges.last().channels.add(strip->channel);
282 }
283 else {
284 /* Start a new range. */
285 ranges.append({strip->left_handle(), strip->right_handle(scene), {strip->channel}});
286 }
287 }
288 return ranges;
289}
290
292 const Strip *strip,
293 const bool all_channels)
294{
295 return all_channels || range.channels.contains(strip->channel);
296}
297
298/* Determine whether we insert #source_strips into others, splitting them at #insert_frame.
299 *
300 * If the "ripple insert" option is disabled by the user, then #r_shuffle_delta will be filled
301 * if the strip would otherwise be in a position to insert. In this case, we need to shuffle our
302 * #source_strips forward to the nearest empty spot before rippling anything else.
303 *
304 * Possible outcomes:
305 * - No insert, no shuffle. Strips stay fixed and ripple others out of the way without splitting.
306 * - Insert, no shuffle. Strips stay fixed and split at `insert_frame` before rippling others.
307 * - No insert, shuffle. Strips must move forward before rippling others.
308 *
309 * We assert that we never insert into other strips and shuffle ourselves at the same time!
310 */
311static bool should_ripple_insert(const Scene *scene,
312 ListBaseT<Strip> *seqbasep,
313 Span<Strip *> source_strips,
314 const int insert_frame,
315 const eSeqRippleFlag ripple_flag,
316 std::optional<int> &r_shuffle_delta)
317{
318 bool do_insert = false;
319 int shuffle_delta = 0;
320
321 for (Strip &target : *seqbasep) {
322 if (source_strips.contains(&target)) {
323 continue;
324 }
325 /* The split point does not intersect in time with this strip. */
326 if (target.left_handle() >= insert_frame || target.right_handle(scene) <= insert_frame) {
327 continue;
328 }
329 for (Strip *source : source_strips) {
330 if (target.channel == source->channel) {
331 if (!(ripple_flag & SEQ_RIPPLE_INSERT)) {
332 shuffle_delta = math::max(shuffle_delta, target.right_handle(scene) - insert_frame);
333 }
334 else {
335 do_insert = true;
336 }
337 break;
338 }
339 }
340 }
341
342 BLI_assert(!(do_insert && (shuffle_delta > 0)));
343
344 if (shuffle_delta > 0) {
345 r_shuffle_delta = shuffle_delta;
346 }
347
348 return do_insert;
349}
350
351static bool sources_overlap_target(const Scene *scene, Span<Strip *> source_strips, Strip *target)
352{
353 for (Strip *source : source_strips) {
354 if (transform_test_overlap(scene, source, target)) {
355 return true;
356 }
357 }
358 return false;
359}
360
362 Span<Strip *> source_strips,
363 const RippleRange &range)
364{
365 Vector<Strip *> range_sources;
366 for (Strip *source : source_strips) {
367 if (source->left_handle() < range.end && source->right_handle(scene) > range.start) {
368 range_sources.append(source);
369 }
370 }
371 return range_sources;
372}
373
375 ListBaseT<Strip> *seqbasep,
376 Span<Strip *> source_strips,
377 const RippleRange &range,
378 const eSeqRippleFlag ripple_flag)
379{
380 const bool all_channels = (ripple_flag & SEQ_RIPPLE_ALL_CHANNELS) != 0;
381 const Vector<Strip *> range_sources = range_sources_get(scene, source_strips, range);
382
383 int start_frame = range.start;
384 int end_frame = range.end;
385
386 std::optional<int> shuffle_delta;
387 const bool do_insert = should_ripple_insert(
388 scene, seqbasep, range_sources, start_frame, ripple_flag, shuffle_delta);
389
390 /* Source strips need to be shuffled out of the way before rippling anything else. */
391 if (shuffle_delta.has_value()) {
392 for (Strip *source : extract_standalone_strips(range_sources)) {
393 transform_translate_strip(scene, source, *shuffle_delta);
394 }
395 start_frame += *shuffle_delta;
396 end_frame += *shuffle_delta;
397 }
398
399 int delta = 0;
400 int ripple_frame = start_frame;
401 if (do_insert) {
402 delta = end_frame - start_frame;
403
404 /* Split across all channels at `start_frame`.
405 * Collect candidates beforehand to avoid changes mid-iteration. */
406 Vector<Strip *> strips_to_split;
407 for (Strip &target : *seqbasep) {
408 if (source_strips.contains(&target)) {
409 continue;
410 }
411 if (target.left_handle() >= start_frame || target.right_handle(scene) <= start_frame) {
412 continue;
413 }
414 if (!transform_strip_is_on_rippled_channel(range, &target, all_channels)) {
415 continue;
416 }
417 strips_to_split.append(&target);
418 }
419
420 /* Since this is a soft split with no data duplication, we can pass a nullptr `bmain`. */
421 Main *bmain = nullptr;
422 for (Strip *target : strips_to_split) {
423 const char *error_msg = nullptr;
424 edit_strip_split(bmain, scene, seqbasep, target, start_frame, SPLIT_SOFT, true, &error_msg);
425 }
426 }
427 else {
428 /* Ripple just enough to resolve overlaps, keeping strip group directly adjacent. */
429 for (Strip &target : *seqbasep) {
430 if (source_strips.contains(&target) ||
431 !sources_overlap_target(scene, range_sources, &target))
432 {
433 continue;
434 }
435 delta = math::max(delta, end_frame - target.left_handle());
436 ripple_frame = math::min(ripple_frame, target.left_handle());
437 }
438 }
439
440 if (delta == 0) {
441 return;
442 }
443
444 /* Translate all strips by `delta`, including any right-half strips created from splits above. */
445 for (Strip &target : *seqbasep) {
446 if (source_strips.contains(&target)) {
447 continue;
448 }
449 if (target.left_handle() < ripple_frame) {
450 continue;
451 }
452 if (!transform_strip_is_on_rippled_channel(range, &target, all_channels)) {
453 continue;
454 }
455 transform_translate_strip(scene, &target, delta);
456 }
457
458 if ((ripple_flag & SEQ_RIPPLE_MARKERS) != 0 && !scene->toolsettings->lock_markers) {
459 for (TimeMarker &marker : scene->markers) {
460 if (marker.frame >= ripple_frame) {
461 marker.frame += delta;
462 }
463 }
464 }
465}
466
468 ListBaseT<Strip> *seqbasep,
469 Span<Strip *> source_strips,
470 const eSeqRippleFlag ripple_flag)
471{
472 for (const RippleRange &range : transform_ripple_ranges_get(scene, source_strips)) {
473 strip_transform_ripple_range(scene, seqbasep, source_strips, range, ripple_flag);
474 }
475}
476
478 ListBaseT<Strip> *seqbasep,
479 Span<Strip *> source_strips)
480{
482
483 /* Effects of transformed strips can be unselected. These must not be included. */
484 targets.remove_if([&](Strip *strip) { return source_strips.contains(strip); });
485 targets.remove_if([&](Strip *strip) {
486 bool does_overlap = false;
487 for (Strip *source : source_strips) {
488 if (transform_test_overlap(scene, strip, source)) {
489 does_overlap = true;
490 }
491 }
492
493 return !does_overlap;
494 });
495
496 return targets;
497}
498
500 const Strip *source,
501 Strip *target,
502 bool covers_left)
503{
504 Editing *ed = seq::editing_get(scene);
505 VectorSet<Strip *> targets;
506 targets.add(target);
508
509 /* We can't just let `left/right_handle_set` trim effect chains, since `target` may be an
510 * effect itself. So we need to find all non-effects that may be inputs to effects, and
511 * trim those. */
512 for (Strip *strip : targets) {
513 if (strip->is_effect_with_inputs()) {
514 continue;
515 }
516 if (covers_left) {
517 strip->left_handle_set(scene, source->right_handle(scene));
518 }
519 else {
520 strip->right_handle_set(scene, source->left_handle());
521 }
522 }
523}
524
526 ListBaseT<Strip> *seqbasep,
527 const Strip *source,
528 Strip *target)
529{
530 /*
531 * Split the `target` strip, move the right half over, and fit `source` inside.
532 * Return newly created strip if applicable to serve as a new overlap target.
533 *
534 * The `bmain` argument is only needed for hard splits and data duplication cases in
535 * `edit_strip_split`. Since this doesn't concern either case, we can pass `nullptr` here.
536 */
537 Main *bmain = nullptr;
538 const char *error_msg = nullptr;
539 Strip *right_strip = edit_strip_split(
540 bmain, scene, seqbasep, target, source->left_handle(), SPLIT_SOFT, true, &error_msg);
541 if (right_strip == nullptr) {
542 return nullptr;
543 }
544 right_strip->left_handle_set(scene, source->right_handle(scene));
545 return right_strip;
546}
547
549 ListBaseT<Strip> *seqbasep,
550 Span<Strip *> source_strips)
551{
552 const ListBaseT<SeqTimelineChannel> *channels = channels_displayed_get(editing_get(scene));
553
554 VectorSet<Strip *> targets = query_overwrite_targets(scene, seqbasep, source_strips);
555 VectorSet<Strip *> to_delete;
556
557 /* We must use index-based iteration since new targets may be added during overwrite splits. */
558 for (int i = 0; i < targets.size(); i++) {
559 Strip *target = targets[i];
560 for (Strip *source : source_strips) {
561 if (transform_is_locked(channels, target) || !transform_test_overlap(scene, source, target))
562 {
563 continue;
564 }
565
566 const bool covers_left = source->left_handle() <= target->left_handle();
567 const bool covers_right = source->right_handle(scene) >= target->right_handle(scene);
568
569 /* Source strip entirely overlaps target strip. */
570 if (covers_left && covers_right) {
571 to_delete.add(target);
572 }
573 /* Source strip only partially overlaps target strip. */
574 else if (covers_left || covers_right) {
575 strip_transform_handle_overwrite_trim(scene, source, target, covers_left);
576 }
577 /* Source strip exists entirely within target strip. */
578 else {
579 Strip *new_strip = strip_transform_handle_overwrite_split(scene, seqbasep, source, target);
580 if (new_strip) {
581 targets.add(new_strip);
582 }
583 }
584 }
585 }
586
587 /* Remove all entirely overlapped strips. This must be done in a separate loop because a split
588 * can invoke `edit_strip_split`, which also calls `edit_remove_flagged_strips` internally. */
589 for (Strip *strip : to_delete) {
590 edit_flag_for_removal(scene, strip);
591 }
592 edit_remove_flagged_strips(scene, seqbasep);
593}
594
596{
597 const bool left_selected = (strip->flag & SEQ_LEFTSEL) != 0;
598 const bool right_selected = (strip->flag & SEQ_RIGHTSEL) != 0;
599
600 if (left_selected && right_selected) {
601 return Side::Both;
602 }
603 if (left_selected) {
604 return Side::Left;
605 }
606 if (right_selected) {
607 return Side::Right;
608 }
609 return Side::None;
610}
611
612static int shuffle_handle_offset_get(const Scene *scene,
613 Span<Strip *> strips_to_shuffle,
614 ListBaseT<Strip> *seqbasep)
615{
616 int offset = 0;
617
618 /* Note that this algorithm looks similar to #shuffle_frame_offset_get, but the two are slightly
619 * different: handles need only a single pass, and with strips, the direction to shuffle is the
620 * same for all, but here handles can be both left & right (which we may eventually remove). */
621 for (Strip *strip : strips_to_shuffle) {
622 const bool is_left = strip_selected_handles_get(strip) == Side::Left;
623 for (Strip &other : *seqbasep) {
624 if (strips_to_shuffle.contains(&other) || relation_is_effect_of_strip(&other, strip) ||
625 !transform_test_overlap(scene, strip, &other))
626 {
627 continue;
628 }
629
630 offset = is_left ? max_ii(offset, other.right_handle(scene) - strip->left_handle()) :
631 min_ii(offset, other.left_handle() - strip->right_handle(scene));
632 }
633 }
634
635 return offset;
636}
637
638static void transform_shuffle_handles(const Scene *scene,
639 ListBaseT<Strip> *seqbasep,
640 Span<Strip *> strips_to_shuffle)
641{
642 const int offset = shuffle_handle_offset_get(scene, strips_to_shuffle, seqbasep);
643
644 if (offset == 0) {
645 return;
646 }
647
648 for (Strip *strip : strips_to_shuffle) {
650 strip->left_handle_set(scene, strip->left_handle() + offset);
651 }
652 else {
653 strip->right_handle_set(scene, strip->right_handle(scene) + offset);
654 }
655 }
656}
657
659 ListBaseT<Strip> *seqbasep,
660 Span<Strip *> source_strips,
661 Span<Strip *> time_dependent_strips,
662 bool use_sync_markers)
663{
664 VectorSet<Strip *> strips_to_shuffle;
665 VectorSet<Strip *> handles_to_shuffle;
666
667 /* If neither or both handles are selected, then add to #strips_to_shuffle
668 * for shuffling the strip as a whole. */
669 for (Strip *strip : extract_standalone_strips(source_strips)) {
671 handles_to_shuffle.add(strip);
672 }
673 else {
674 strips_to_shuffle.add(strip);
675 }
676 }
677
678 /* First shuffle handles. */
679 transform_shuffle_handles(scene, seqbasep, handles_to_shuffle);
680
681 /* Shuffle entire strips. */
683 scene, seqbasep, strips_to_shuffle, time_dependent_strips, use_sync_markers);
684}
685
687 ListBaseT<Strip> *seqbasep,
688 Span<Strip *> source_strips,
689 bool use_sync_markers,
690 Span<Strip *> time_dependent_strips)
691{
693 seqbasep,
694 source_strips,
695 use_sync_markers,
698 time_dependent_strips);
699}
700
702 ListBaseT<Strip> *seqbasep,
703 Span<Strip *> source_strips,
704 bool use_sync_markers,
705 const eSeqOverlapMode overlap_mode,
706 const eSeqRippleFlag ripple_flag,
707 Span<Strip *> time_dependent_strips)
708{
709 switch (overlap_mode) {
711 strip_transform_handle_ripple(scene, seqbasep, source_strips, ripple_flag);
712 break;
714 strip_transform_handle_overwrite(scene, seqbasep, source_strips);
715 break;
718 scene, seqbasep, source_strips, time_dependent_strips, use_sync_markers);
719 break;
720 }
721
722 /* Strips can still overlap even after being handled above. Examples: user tries to place a
723 * strip on top of its effect, on top of a locked strip, or inside a transition (split fails).
724 */
725 for (Strip *strip : source_strips) {
726 transform_shuffle_vertical(seqbasep, {strip}, scene);
727 strip->runtime->flag &= ~StripRuntimeFlag::Overlap;
728 }
729}
730
732 ListBaseT<Strip> *seqbase,
733 const int timeline_frame,
734 const int delta)
735{
736 for (Strip &strip : *seqbase) {
737 if (strip.left_handle() >= timeline_frame) {
738 transform_translate_strip(scene, &strip, delta);
739 relations_invalidate_cache(scene, &strip);
740 }
741 }
742
743 if (!scene->toolsettings->lock_markers) {
744 for (TimeMarker &marker : scene->markers) {
745 if (marker.frame >= timeline_frame) {
746 marker.frame += delta;
747 }
748 }
749 }
750}
751
753
754/* -------------------------------------------------------------------- */
757
759static int2 strip_source_size_get(const Scene *scene, const Strip *strip)
760{
761 const int2 parent_scene_size = int2(scene->r.xsch, scene->r.ysch);
762
763 switch (strip->type) {
764 case STRIP_TYPE_IMAGE: {
765 const StripElem *se = seq::render_give_stripelem(scene, strip, scene->r.cfra);
766 if (se == nullptr) {
767 return parent_scene_size;
768 }
769 return int2(se->orig_width, se->orig_height);
770 }
771 case STRIP_TYPE_MOVIE: {
772 const StripElem *se = strip->data->stripdata;
773 return int2(se->orig_width, se->orig_height);
774 }
776 const MovieClip *clip = strip->clip;
777 if (clip && clip->lastsize[0] != 0 && clip->lastsize[1] != 0) {
778 return int2(clip->lastsize[0], clip->lastsize[1]);
779 }
780 return parent_scene_size;
781 }
782 case STRIP_TYPE_SCENE: {
783 /* TODO(@john): Sequencer-input scene strips render at their parent sequencer scene's
784 * resolution; they should probably only render their own scene resolution. */
785 if (strip->scene == nullptr || (strip->flag & SEQ_SCENE_STRIPS) != 0) {
786 return parent_scene_size;
787 }
788 return int2(strip->scene->r.xsch, strip->scene->r.ysch);
789 }
790 case STRIP_TYPE_COLOR: {
791 const SolidColorVars *cv = static_cast<const SolidColorVars *>(strip->effectdata);
792 return int2(cv->width, cv->height);
793 }
794 default:
795 return parent_scene_size;
796 }
797}
798
800{
801 float2 mirror(1.0f, 1.0f);
802
803 if ((strip->flag & SEQ_FLIPX) != 0) {
804 mirror.x = -1.0f;
805 }
806 if ((strip->flag & SEQ_FLIPY) != 0) {
807 mirror.y = -1.0f;
808 }
809 return mirror;
810}
811
812int2 image_transform_box_size_get(const Scene *scene, const Strip *strip)
813{
814 const int2 source_size = strip_source_size_get(scene, strip);
815
816 if (strip->type == STRIP_TYPE_TEXT) {
817 TextVars *data = static_cast<TextVars *>(strip->effectdata);
818 std::scoped_lock runtime_lock(text_runtime_mutex_get());
819 text_effect_update_runtime(nullptr, *data, source_size);
820 BLF_disable(data->runtime->font, BLF_BOLD | BLF_ITALIC);
821 const int2 text_size(BLI_rcti_size_x(&data->runtime->text_boundbox),
822 BLI_rcti_size_y(&data->runtime->text_boundbox));
823 return text_size;
824 }
825
826 return source_size;
827}
828
829/* Convert origin from a 0->1 range (where (0,0) is the bottom left of the image)
830 * to the offset in image-space pixels from an image's center. */
831static float2 convert_origin_to_image_offset(const Scene *scene, const Strip *strip, float2 origin)
832{
833 const float2 box_size = float2(image_transform_box_size_get(scene, strip));
834 return box_size * origin - (box_size / 2.0f);
835}
836
837float2 image_transform_origin_get(const Scene *scene, const Strip *strip)
838{
839
840 const StripTransform *tr = strip->data->transform;
841 if (strip->type != STRIP_TYPE_TEXT) {
842 return tr->origin;
843 }
844
845 /* Text strips are the only type where their box is not the size of the rendered image. We must
846 * convert from an origin relative to the text box -> an origin relative to the whole render. */
847 const float2 text_size = float2(image_transform_box_size_get(scene, strip));
848 const float2 render_size(scene->r.xsch, scene->r.ysch);
849
850 /* Before we scale the text origin down to produce the render origin, we must offset the origin
851 * so that (0,0) corresponds to the center instead of (0.5, 0.5) for correct math. */
852 const float2 offset_text_origin = float2(tr->origin) - float2(0.5f);
853 const float2 render_origin = float2(0.5f) + offset_text_origin * (text_size / render_size);
854 return render_origin;
855}
856
857static float2 image_transform_viewport_scale_get(const Scene *scene, const Strip *strip)
858{
859 const float2 viewport_pixel_aspect(scene->r.xasp / scene->r.yasp, 1.0f);
860 return image_transform_mirror_factor_get(strip) * viewport_pixel_aspect;
861}
862
864{
865 const StripTransform *tr = strip->data->transform;
866 const float2 origin_offset_imgpx = convert_origin_to_image_offset(scene, strip, tr->origin);
867
868 /* Note that we do not have to consider the strip's rotation or scaling here, since they always
869 * act with respect to the origin, and thus do not change the origin's position. */
870 return (float2(tr->xofs, tr->yofs) + origin_offset_imgpx) *
872}
873
875{
876 const StripTransform *tr = strip->data->transform;
878 float2(tr->xofs, tr->yofs), tr->rotation, float2(tr->scale_x, tr->scale_y));
879 const float2 origin_offset_imgpx = convert_origin_to_image_offset(scene, strip, tr->origin);
880
882 math::from_origin_transform(matrix, origin_offset_imgpx);
883}
884
886{
887 constexpr int num_corners = 4;
888 const float2 box_size = float2(image_transform_box_size_get(scene, strip));
889
890 /* Raw quad before any rotation/scaling or text anchoring is applied.
891 *
892 * NOTE: For text strips, crops should only affect their visible result and not their bounding
893 * box. Text effects can stray outside, so crop works on the full render buffer. */
894 const StripCrop no_crop{};
895 const StripCrop *crop = (strip->type == STRIP_TYPE_TEXT) ? &no_crop : strip->data->crop;
896 float2 quad[num_corners]{
897 {(box_size.x / 2) - crop->right, (box_size.y / 2) - crop->top}, /* Top right. */
898 {(box_size.x / 2) - crop->right, (-box_size.y / 2) + crop->bottom}, /* Bottom right. */
899 {(-box_size.x / 2) + crop->left, (-box_size.y / 2) + crop->bottom}, /* Bottom left. */
900 {(-box_size.x / 2) + crop->left, (box_size.y / 2) - crop->top}, /* Top left. */
901 };
902
903 if (strip->type == STRIP_TYPE_TEXT) {
904 const TextVars *data = static_cast<TextVars *>(strip->effectdata);
905 float2 offset(0, 0);
906
907 switch (data->anchor_x) {
909 offset.x += box_size.x / 2.0f;
910 break;
912 break;
914 offset.x += -box_size.x / 2.0f;
915 break;
916 default:
917 break;
918 }
919 switch (data->anchor_y) {
921 offset.y += box_size.y / 2.0f;
922 break;
924 break;
926 offset.y += -box_size.y / 2.0f;
927 break;
928 default:
929 break;
930 }
931
932 for (float2 &corner : quad) {
933 corner += offset;
934 }
935 }
936
937 const float3x3 matrix = image_transform_matrix_get(scene, strip);
938
939 Array<float2> quad_final(num_corners);
940 for (const int i : IndexRange(num_corners)) {
941 quad_final[i] = math::transform_point(matrix, quad[i]);
942 }
943 return quad_final;
944}
945
946float2 image_preview_unit_to_px(const Scene *scene, const float2 co_src)
947{
948 return {co_src.x * scene->r.xsch, co_src.y * scene->r.ysch};
949}
950
952{
953 return {co_src.x / scene->r.xsch, co_src.y / scene->r.ysch};
954}
955
957{
958 return {float2(std::numeric_limits<float>::max()), float2(std::numeric_limits<float>::lowest())};
959}
960
962{
963 Bounds<float2> bounding_box = negative_bounds();
964
965 for (Strip *strip : strips) {
966 const Array<float2> quad = image_transform_quad_get(scene, strip);
967 const Bounds<float2> strip_bounding_box = *bounds::min_max(quad.as_span());
968 bounding_box = bounds::merge(bounding_box, strip_bounding_box);
969 }
970
971 return bounding_box;
972}
973
975
976} // namespace blender::seq
#define SELECT
Definition BKE_mask.hh:19
#define ELEM(...)
Span< T > as_span() const
Definition BLI_array.hh:254
constexpr IndexRange()=default
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
bool add(const Key &key)
int64_t size() const
int64_t remove_if(Predicate &&predicate)
void append(const T &value)
const T & data(const Action &owning_action) const
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:27
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:56
T min(const T &a, const T &b)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
MatT from_origin_transform(const MatT &transform, const VectorT origin)
T max(const T &a, const T &b)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
static VectorSet< Strip * > extract_standalone_strips(Span< Strip * > transformed_strips)
float2 image_preview_unit_to_px(const Scene *scene, const float2 co_src)
Vector< RippleRange > transform_ripple_ranges_get(const Scene *scene, Span< Strip * > strips)
Strip * lookup_meta_by_strip(Editing *ed, const Strip *key)
static VectorSet< Strip * > query_overwrite_targets(const Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips)
int2 image_transform_box_size_get(const Scene *scene, const Strip *strip)
float2 image_transform_mirror_factor_get(const Strip *strip)
void relations_invalidate_cache(Scene *scene, Strip *strip)
static bool sources_overlap_target(const Scene *scene, Span< Strip * > source_strips, Strip *target)
std::recursive_mutex & text_runtime_mutex_get()
void transform_translate_strip(Scene *evil_scene, Strip *strip, int delta)
void transform_handle_overlap(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips, bool use_sync_markers, Span< Strip * > time_dependent_strips)
rcti strip_int_bounds_get(const Scene *scene, const Strip *strip)
static int shuffle_frame_offset_get(const Scene *scene, Span< Strip * > strips_to_shuffle, ListBaseT< Strip > *seqbasep, const Side side)
static void transform_shuffle_handles(const Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > strips_to_shuffle)
Array< float2 > image_transform_quad_get(const Scene *scene, const Strip *strip)
bool transform_test_overlap(const Scene *scene, Strip *strip1, Strip *strip2)
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
static int2 strip_source_size_get(const Scene *scene, const Strip *strip)
void edit_remove_flagged_strips(Scene *scene, ListBaseT< Strip > *seqbase)
void offset_animdata(const Scene *scene, Strip *strip, float ofs)
Definition animation.cc:42
static bool shuffle_channel_is_free(const Scene *scene, ListBaseT< Strip > *seqbasep, const ListBaseT< SeqTimelineChannel > *channels, Strip *strip)
void expand_strips(Editing *ed, VectorSet< Strip * > &strips, const StripRelation include)
Definition iterator.cc:70
void text_effect_update_runtime(const RenderData *context, TextVars &text, const int2 image_size)
bool transform_single_image_check(const Strip *strip)
void strip_time_update_effects_strip_range(const Scene *scene, const Span< Strip * > effects)
float3x3 image_transform_matrix_get(const Scene *scene, const Strip *strip)
constexpr int MAX_CHANNELS
void time_update_meta_strip_range(const Scene *scene, Strip *strip_meta)
Bounds< float2 > image_transform_bounding_box_from_strips_get(Scene *scene, Span< Strip * > strips)
float2 image_transform_origin_preview_offset_get(const Scene *scene, const Strip *strip)
static void strip_transform_handle_shuffle(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips, Span< Strip * > time_dependent_strips, bool use_sync_markers)
Span< Strip * > lookup_effects_by_strip(Editing *ed, const Strip *key)
static bool strips_overlap_at_offset(const Scene *scene, const Strip *strip, const Strip *other, const int offset)
void edit_flag_for_removal(Scene *scene, Strip *strip)
Strip * edit_strip_split(Main *bmain, Scene *scene, ListBaseT< Strip > *seqbase, Strip *strip, const int timeline_frame, const eSplitMethod method, const bool ignore_connections, const char **r_error)
float2 image_transform_origin_get(const Scene *scene, const Strip *strip)
static float2 image_transform_viewport_scale_get(const Scene *scene, const Strip *strip)
SeqTimelineChannel * channel_get_by_index(const ListBaseT< SeqTimelineChannel > *channels, const int channel_index)
Definition channels.cc:59
static Bounds< float2 > negative_bounds()
bool relation_is_effect_of_strip(const Strip *effect, const Strip *input)
static float2 convert_origin_to_image_offset(const Scene *scene, const Strip *strip, float2 origin)
static Vector< Strip * > range_sources_get(const Scene *scene, Span< Strip * > source_strips, const RippleRange &range)
static void transform_shuffle_horizontal(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > strips_to_shuffle, Span< Strip * > time_dependent_strips, const bool use_sync_markers)
void transform_strips_after_frame(Scene *scene, ListBaseT< Strip > *seqbase, const int timeline_frame, const int delta)
static bool should_ripple_insert(const Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips, const int insert_frame, const eSeqRippleFlag ripple_flag, std::optional< int > &r_shuffle_delta)
float2 image_preview_unit_from_px(const Scene *scene, const float2 co_src)
static Strip * strip_transform_handle_overwrite_split(Scene *scene, ListBaseT< Strip > *seqbasep, const Strip *source, Strip *target)
bool transform_strip_is_on_rippled_channel(const RippleRange &range, const Strip *strip, const bool all_channels)
eSeqRippleFlag tool_settings_ripple_flag_get(Scene *scene)
Definition sequencer.cc:451
void transform_shuffle_vertical(ListBaseT< Strip > *seqbasep, Span< Strip * > strips, Scene *scene, const int channel_delta)
static int shuffle_handle_offset_get(const Scene *scene, Span< Strip * > strips_to_shuffle, ListBaseT< Strip > *seqbasep)
bool transform_is_locked(const ListBaseT< SeqTimelineChannel > *channels, const Strip *strip)
ListBaseT< SeqTimelineChannel > * channels_displayed_get(const Editing *ed)
Definition channels.cc:26
bool transform_strip_can_be_translated(const Strip *strip)
static void strip_transform_handle_overwrite(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips)
static void strip_transform_handle_overwrite_trim(Scene *scene, const Strip *source, Strip *target, bool covers_left)
VectorSet< Strip * > query_unselected_strips(ListBaseT< Strip > *seqbase)
Definition iterator.cc:268
static Side strip_selected_handles_get(const Strip *strip)
static void strip_transform_handle_ripple(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips, const eSeqRippleFlag ripple_flag)
eSeqOverlapMode tool_settings_overlap_mode_get(Scene *scene)
Definition sequencer.cc:445
StripElem * render_give_stripelem(const Scene *scene, const Strip *strip, int timeline_frame)
Definition render.cc:220
static void strip_transform_ripple_range(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips, const RippleRange &range, const eSeqRippleFlag ripple_flag)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE int max_ii(int a, int b)
void BLF_disable(int fontid, FontFlags flag)
Definition blf.cc:331
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
BMesh const char void * data
MatBase< float, 3, 3 > float3x3
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
@ SEQ_RIPPLE_ALL_CHANNELS
MINLINE int min_ii(int a, int b)
@ BLF_ITALIC
Definition BLF_enums.hh:49
struct Editing * ed
ListBaseT< TimeMarker > markers
struct RenderData r
struct ToolSettings * toolsettings