-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathstackAlgorithm.cpp
More file actions
304 lines (272 loc) · 8.23 KB
/
stackAlgorithm.cpp
File metadata and controls
304 lines (272 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
#include "opentimelineio/stackAlgorithm.h"
#include "opentimelineio/gap.h"
#include "opentimelineio/track.h"
#include "opentimelineio/trackAlgorithm.h"
#include "opentimelineio/transition.h"
namespace opentimelineio { namespace OPENTIMELINEIO_VERSION_NS {
typedef std::map<Track*, std::map<Composable*, TimeRange>> RangeTrackMap;
typedef std::vector<SerializableObject::Retainer<Track>> TrackRetainerVector;
static bool
_is_not_visible(
SerializableObject::Retainer<Composable> thing)
{
auto item = dynamic_retainer_cast<Item>(thing);
if (!item)
return false;
return !item->visible();
}
static void
_flatten_next_item(
RangeTrackMap& range_track_map,
Track* flat_track,
std::vector<Track*> const& tracks,
int track_index,
std::optional<TimeRange> trim_range,
ErrorStatus* error_status)
{
if (track_index < 0)
{
track_index = int(tracks.size()) - 1;
}
if (track_index < 0)
{
return;
}
Track* track = tracks[track_index];
SerializableObject::Retainer<Track> track_retainer;
if (trim_range)
{
track = track_trimmed_to_range(track, *trim_range, error_status);
if (track == nullptr || is_error(error_status))
{
return;
}
track_retainer = SerializableObject::Retainer<Track>(track);
}
std::map<Composable*, TimeRange>* track_map;
auto it = range_track_map.find(track);
if (it != range_track_map.end())
{
track_map = &it->second;
}
else
{
auto result = range_track_map.emplace(
track,
track->range_of_all_children(error_status));
if (is_error(error_status))
{
return;
}
track_map = &result.first->second;
}
auto children = track->children();
for (size_t i = 0; i < children.size(); i++)
{
std::optional<TimeRange> trim = std::nullopt;
SerializableObject::Retainer<Composable> child = children[i];
// combine all non visible children into one continuous range
while(track_index > 0 && _is_not_visible(child))
{
TimeRange child_trim = (*track_map)[child];
// offset ranges so they are in track range before being trimmed
if (trim_range)
{
trim = TimeRange(
child_trim.start_time() + trim_range->start_time(),
child_trim.duration());
(*track_map)[child] = child_trim;
}
if (trim.has_value())
{
trim = trim->duration_extended_by(child_trim.duration());
}
else
{
trim = child_trim;
}
if (i+1 < children.size())
{
child = children[i++];
}
else
{
// last item is not visible
child = nullptr;
break;
}
}
if (trim.has_value())
{
_flatten_next_item(
range_track_map,
flat_track,
tracks,
track_index - 1,
trim,
error_status);
if (is_error(error_status))
{
return;
}
}
if (child)
{
auto item = dynamic_retainer_cast<Item>(child);
if (!item)
{
if (!dynamic_retainer_cast<Transition>(child))
{
if (error_status)
{
*error_status = ErrorStatus(
ErrorStatus::TYPE_MISMATCH,
"expected item of type Item* || Transition*",
child);
}
return;
}
}
flat_track->insert_child(
static_cast<int>(flat_track->children().size()),
static_cast<Composable*>(child->clone(error_status)),
error_status);
if (is_error(error_status))
{
return;
}
}
}
// range_track_map persists over the entire duration of flatten_stack
// track_retainer.value is about to be deleted; it's entirely possible
// that a new item will be created at the same pointer location, so we
// have to clean this value out of the map now.
if (track_retainer)
{
range_track_map.erase(track_retainer);
}
}
// add a gap to end of a track if it is shorter then the longest track.
// shorter tracks are clones and get added to the tracks_retainer.
// a new track will replace the original pointer in the track vector.
static void
_normalize_tracks_lengths(
std::vector<Track*>& tracks,
TrackRetainerVector& tracks_retainer,
ErrorStatus* error_status)
{
RationalTime duration;
for (auto track: tracks)
{
duration = std::max(duration, track->duration(error_status));
if (is_error(error_status))
{
return;
}
}
for (size_t i = 0; i < tracks.size(); i++)
{
Track* old_track = tracks[i];
RationalTime track_duration = old_track->duration(error_status);
if (track_duration < duration)
{
Track* new_track =
static_cast<Track*>(old_track->clone(error_status));
if (is_error(error_status))
{
return;
}
// add track to retainer so it can be freed later
tracks_retainer.push_back(
SerializableObject::Retainer<Track>(new_track));
new_track->append_child(
new Gap(duration - track_duration),
error_status);
if (is_error(error_status))
{
return;
}
tracks[i] = new_track;
}
}
}
Track*
flatten_stack(Stack* in_stack, ErrorStatus* error_status)
{
std::vector<Track*> tracks;
// tracks are cloned if they need to be normalized
// they get added to this retainer so they can be
// freed when the algorithm is complete
TrackRetainerVector tracks_retainer;
tracks.reserve(in_stack->children().size());
for (auto c: in_stack->children())
{
if (auto track = dynamic_retainer_cast<Track>(c))
{
if (track->enabled())
{
tracks.push_back(track);
}
}
else
{
if (error_status)
{
*error_status = ErrorStatus(
ErrorStatus::TYPE_MISMATCH,
"expected item of type Track*",
c);
}
return nullptr;
}
}
_normalize_tracks_lengths(tracks, tracks_retainer, error_status);
if (is_error(error_status))
{
return nullptr;
}
Track* flat_track = new Track;
flat_track->set_name("Flattened");
RangeTrackMap range_track_map;
_flatten_next_item(
range_track_map,
flat_track,
tracks,
-1,
std::nullopt,
error_status);
return flat_track;
}
Track*
flatten_stack(std::vector<Track*> const& tracks, ErrorStatus* error_status)
{
std::vector<Track*> flat_tracks;
// tracks are cloned if they need to be normalized
// they get added to this retainer so they can be
// freed when the algorithm is complete
TrackRetainerVector tracks_retainer;
flat_tracks.reserve(tracks.size());
for (auto track: tracks)
{
flat_tracks.push_back(track);
}
_normalize_tracks_lengths(flat_tracks, tracks_retainer, error_status);
if (is_error(error_status))
{
return nullptr;
}
Track* flat_track = new Track;
flat_track->set_name("Flattened");
RangeTrackMap range_track_map;
_flatten_next_item(
range_track_map,
flat_track,
flat_tracks,
-1,
std::nullopt,
error_status);
return flat_track;
}
}} // namespace opentimelineio::OPENTIMELINEIO_VERSION_NS