Blender V5.3
transform_convert_sequencer_retiming.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_screen_types.h"
12#include "DNA_sequence_types.h"
13#include "DNA_space_types.h"
14
15#include "BLI_math_matrix_c.hh"
16#include "BLI_math_vector_c.hh"
17#include "BLI_rect.hh"
18
19#include "BKE_context.hh"
20
21#include "SEQ_channels.hh"
22#include "SEQ_iterator.hh"
23#include "SEQ_relations.hh"
24#include "SEQ_retiming.hh"
25#include "SEQ_sequencer.hh"
26#include "SEQ_transform.hh"
27
28#include "transform.hh"
29#include "transform_convert.hh"
30
31namespace blender::ed::transform {
32
33namespace {
34
36struct TransDataSeq {
37 Strip *strip;
38 int orig_timeline_frame;
39 int key_index; /* Some actions may need to destroy original data, use index to access it. */
40};
41
42} // namespace
43
44static TransData *SeqToTransData(const Scene *scene,
45 Strip *strip,
46 const SeqRetimingKey *key,
47 TransData *td,
48 TransData2D *td2d,
49 TransDataSeq *tdseq)
50{
51
52 td2d->loc[0] = seq::retiming_key_frame_get(scene, strip, key);
53 td2d->loc[1] = key->retiming_factor;
54 td2d->loc2d = nullptr;
55 td->loc = td2d->loc;
56 copy_v3_v3(td->iloc, td->loc);
57 copy_v3_v3(td->center, td->loc);
58 memset(td->axismtx, 0, sizeof(td->axismtx));
59 td->axismtx[2][2] = 1.0f;
60 unit_m3(td->mtx);
61 unit_m3(td->smtx);
62
63 tdseq->strip = strip;
64 tdseq->orig_timeline_frame = seq::retiming_key_frame_get(scene, strip, key);
65 tdseq->key_index = seq::retiming_key_index_get(strip, key);
66
67 td->extra = static_cast<void *>(tdseq);
68 td->flag |= TD_SELECTED;
69 td->dist = 0.0;
70
71 return td;
72}
73
74static void freeSeqData(TransInfo *t, TransDataContainer *tc, TransCustomData *custom_data)
75{
76 const TransData *const td = tc->data;
77 Scene *scene = t->scene;
79
80 /* Handle overlapping strips. */
81
82 VectorSet<Strip *> transformed_strips;
83 for (int i = 0; i < tc->data_len; i++) {
84 Strip *strip = (static_cast<TransDataSeq *>((td + i)->extra))->strip;
85 transformed_strips.add(strip);
86 }
87
88 ListBaseT<Strip> *seqbasep = seq::active_seqbase_get(ed);
90
91 VectorSet<Strip *> dependant;
92 dependant.add_multiple(transformed_strips);
93 dependant.remove_if([&](Strip *strip) { return seq::transform_strip_can_be_translated(strip); });
94
95 if (seq_transform_check_overlap(transformed_strips)) {
96 const bool use_sync_markers = ((t->area->spacedata.first_as<SpaceSeq>())->flag &
97 SEQ_MARKER_TRANS) != 0;
99 scene, seqbasep, transformed_strips, use_sync_markers, dependant);
100 }
101
102 if ((custom_data->data != nullptr) && custom_data->use_free) {
103 TransSeq *ts = static_cast<TransSeq *>(custom_data->data);
104 MEM_delete(static_cast<TransDataSeq *>(ts->tdseq));
105 MEM_delete(ts);
106 custom_data->data = nullptr;
107 }
108}
109
111 const Scene *scene,
112 const Map<SeqRetimingKey *, Strip *> &selection)
113{
114 TransSeq *ts = static_cast<TransSeq *>(TRANS_DATA_CONTAINER_FIRST_SINGLE(t)->custom.type.data);
115 const Editing *ed = seq::editing_get(scene);
116
117 /* Prevent snaps and change in `values` past `offset_clamp` for all transformed retiming keys. */
118 BLI_rcti_init(&ts->offset_clamp, INT_MIN, INT_MAX, 0, 0);
119
120 /* Disable axis constraints, retiming keys can only be moved horizontally. */
121 t->flag |= T_NO_CONSTRAINT;
122
123 for (auto item : selection.items()) {
124 SeqRetimingKey *key = item.key;
125
126 /* Transition retiming key. */
129 {
131 SeqRetimingKey *key_end = key_start + 1;
132 SeqRetimingKey *key_prev = key_start - 1;
133 SeqRetimingKey *key_next = key_end + 1;
134
135 const float midpoint = key_start->original_strip_frame_index;
136
137 /* Ensure start transition key cannot pass the previous key, or linked end transition key
138 * cannot pass the next key. This transform behavior is symmetrical and limited by the
139 * smallest distance between keys. */
140 const int max_offset = min_ii(key_start->strip_frame_index - key_prev->strip_frame_index - 1,
141 key_next->strip_frame_index - key_end->strip_frame_index - 1);
142
143 if (key_start->flag & SEQ_KEY_SELECTED) {
144 /* Ensure start transition key cannot pass the midpoint. */
146 ts->offset_clamp.xmax);
147 ts->offset_clamp.xmin = max_ii(-max_offset, ts->offset_clamp.xmin);
148 }
149 else {
150 /* Ensure end transition key cannot pass the midpoint. */
151 ts->offset_clamp.xmin = max_ii(-(key_end->strip_frame_index - midpoint - 1),
152 ts->offset_clamp.xmin);
153 ts->offset_clamp.xmax = min_ii(max_offset, ts->offset_clamp.xmax);
154 }
155 }
156 /* Non-transition retiming key. */
157 else {
158 Strip *strip = item.value;
159 SeqRetimingKey *key_prev = key - 1, *key_next = key + 1;
160 if (!seq::retiming_is_last_key(strip, key)) {
161 /* Ensure that this key cannot pass the next key. */
162 ts->offset_clamp.xmax = min_ii(key_next->strip_frame_index - key->strip_frame_index - 1,
163 ts->offset_clamp.xmax);
164 /* TODO(john): There is an off-by-one error for the last "fake" key's `strip_frame_index`,
165 * which is 1 less than it should be. This is not an immediate issue but should be fixed.
166 */
167 }
168 if (key->strip_frame_index != 0) {
169 /* Ensure that this key cannot pass the previous key. */
170 ts->offset_clamp.xmin = max_ii(-(key->strip_frame_index - key_prev->strip_frame_index - 1),
171 ts->offset_clamp.xmin);
172 }
173 }
174 }
175}
176
178{
179 const Editing *ed = seq::editing_get(t->scene);
180 if (ed == nullptr) {
181 return;
182 }
183
184 const ListBaseT<SeqTimelineChannel> *channels = seq::channels_displayed_get(ed);
186 selection.remove_if([&](auto item) { return seq::transform_is_locked(channels, item.value); });
187
188 if (selection.is_empty()) {
189 return;
190 }
191
194 tc->data_len = selection.size();
195
196 TransSeq *ts = MEM_new<TransSeq>(__func__);
197 tc->custom.type.data = ts;
198 tc->custom.type.use_free = true;
199
200 TransData *td = MEM_new_array_zeroed<TransData>(tc->data_len, "TransSeq TransData");
201 TransData2D *td2d = MEM_new_array_zeroed<TransData2D>(tc->data_len, "TransSeq TransData2D");
202 TransDataSeq *tdseq = MEM_new_array_zeroed<TransDataSeq>(tc->data_len, "TransSeq TransDataSeq");
203 tc->data = td;
204 tc->data_2d = td2d;
205 ts->tdseq = tdseq;
206
207 for (auto item : selection.items()) {
208 SeqToTransData(t->scene, item.value, item.key, td++, td2d++, tdseq++);
209 }
210
211 create_trans_seq_clamp_data(t, t->scene, selection);
212}
213
215{
217 const TransData *td = nullptr;
218 int i;
219
220 VectorSet<Strip *> transformed_strips;
221
222 for (i = 0, td = tc->data; i < tc->data_len; i++, td++) {
223 const TransDataSeq *tdseq = static_cast<TransDataSeq *>(td->extra);
224 Strip *strip = tdseq->strip;
225
226 if (!seq::retiming_show_keys(strip)) {
227 continue;
228 }
229
230 float offset[2];
231 float offset_clamped[2];
232 sub_v2_v2v2(offset, td->loc, td->iloc);
233 copy_v2_v2(offset_clamped, offset);
234
235 transform_convert_sequencer_clamp(t, offset_clamped);
236 const int new_frame = round_fl_to_int(td->iloc[0] + offset_clamped[0]);
237
238 transformed_strips.add(strip);
239
240 /* Calculate translation. */
241
242 const MutableSpan keys = seq::retiming_keys_get(strip);
243 SeqRetimingKey *key = &keys[tdseq->key_index];
244
247 {
249 }
250 else {
251 seq::retiming_key_frame_set(t->scene, strip, key, new_frame);
252 }
253
255 }
256
257 /* Test overlap, displays red outline. */
260 for (Strip *strip : transformed_strips) {
261 strip->runtime->flag &= ~seq::StripRuntimeFlag::Overlap;
263 strip->runtime->flag |= seq::StripRuntimeFlag::Overlap;
264 }
265 }
266}
267
273
274} // namespace blender::ed::transform
Read Blender memory allocation and freeing.
int64_t size() const
Definition BLI_map.hh:976
bool is_empty() const
Definition BLI_map.hh:986
int64_t remove_if(Predicate &&predicate)
Definition BLI_map.hh:948
ItemIterator items() const &
Definition BLI_map.hh:902
bool add(const Key &key)
void add_multiple(Span< Key > keys)
int64_t remove_if(Predicate &&predicate)
#define INT_MAX
int midpoint(int a, int b)
static TransData * SeqToTransData(Scene *scene, TransData *td, TransData2D *td2d, TransDataSeq *tdsq, Strip *strip, int flag, int sel_flag)
bool transform_convert_sequencer_clamp(const TransInfo *t, float r_val[2])
static void recalcData_sequencer_retiming(TransInfo *t)
static void freeSeqData(TransInfo *t, TransDataContainer *tc, TransCustomData *custom_data)
static void create_trans_seq_clamp_data(TransInfo *t, const Scene *scene)
bool seq_transform_check_overlap(Span< Strip * > transformed_strips)
static void createTransSeqRetimingData(bContext *, TransInfo *t)
int retiming_key_index_get(const Strip *strip, const SeqRetimingKey *key)
MutableSpan< SeqRetimingKey > retiming_keys_get(const Strip *strip)
void relations_invalidate_cache(Scene *scene, Strip *strip)
bool retiming_selection_has_whole_transition(const Editing *ed, SeqRetimingKey *key)
ListBaseT< Strip > * active_seqbase_get(const Editing *ed)
Definition sequencer.cc:463
void transform_handle_overlap(Scene *scene, ListBaseT< Strip > *seqbasep, Span< Strip * > source_strips, bool use_sync_markers, Span< Strip * > time_dependent_strips)
bool transform_test_overlap(const Scene *scene, Strip *strip1, Strip *strip2)
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
SeqRetimingKey * retiming_transition_start_get(SeqRetimingKey *key)
int retiming_key_frame_get(const Scene *scene, const Strip *strip, const SeqRetimingKey *key)
void expand_strips(Editing *ed, VectorSet< Strip * > &strips, const StripRelation include)
Definition iterator.cc:70
bool retiming_key_is_transition_type(const SeqRetimingKey *key)
bool retiming_show_keys(const Strip *strip)
void retiming_transition_key_frame_set(const Scene *scene, const Strip *strip, SeqRetimingKey *key, const int frame)
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)
Map< SeqRetimingKey *, Strip * > retiming_selection_get(const Editing *ed)
void retiming_key_frame_set(const Scene *scene, Strip *strip, SeqRetimingKey *key, int frame)
bool retiming_is_last_key(const Strip *strip, const SeqRetimingKey *key)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE int round_fl_to_int(float a)
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE int max_ii(int a, int b)
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
void unit_m3(float m[3][3])
MINLINE int min_ii(int a, int b)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
ListBaseT< SpaceLink > spacedata
void(*) free_cb(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:655
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition transform.hh:45
conversion and adaptation of different datablocks to a common struct.