Blender V5.3
strip_relations.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 "DNA_scene_types.h"
12#include "DNA_sequence_types.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_math_base_c.hh"
16#include "BLI_session_uid.hh"
17#include "BLI_string.hh"
18
19#include "BKE_layer.hh"
20#include "BKE_main.hh"
21#include "BKE_report.hh"
22#include "BKE_scene.hh"
23
24#include "DEG_depsgraph.hh"
25
26#include "SEQ_iterator.hh"
27#include "SEQ_prefetch.hh"
28#include "SEQ_preview_cache.hh"
29#include "SEQ_relations.hh"
30#include "SEQ_sequencer.hh"
32#include "SEQ_utils.hh"
33
38#include "effects/effects.hh"
39#include "sequencer.hh"
40#include "utils.hh"
41
42namespace blender::seq {
43
44bool relation_is_effect_of_strip(const Strip *effect, const Strip *input)
45{
46 return ELEM(input, effect->input1, effect->input2);
47}
48
50{
51 if (flag_is_set(mode, CacheCleanup::Thumbnails)) {
53 }
54 if (flag_is_set(mode, CacheCleanup::SourceImage)) {
56 }
57 if (flag_is_set(mode, CacheCleanup::FinalImage)) {
59 }
60 if (flag_is_set(mode, CacheCleanup::IntraFrame)) {
63 }
64}
65
67{
68 if (!(scene->ed->cache_flag & SEQ_CACHE_STORE_RAW)) {
69 /* RAW caches has been disabled, clear them out. */
71 }
72 if (!(scene->ed->cache_flag & SEQ_CACHE_STORE_FINAL_OUT)) {
73 /* Final caches has been disabled, clear them out. */
75 }
76}
77
78bool is_cache_full(const Scene *scene)
79{
80 size_t cache_limit = size_t(U.memcachelimit) * 1024 * 1024;
82 cache_limit;
83}
84
86{
87 if (!is_cache_full(scene)) {
88 /* Cache is not full, we don't have to evict anything. */
89 return false;
90 }
91
92 /* Cache is full, so we want to remove some images. We always try to remove one final image,
93 * and some amount of source images for each final image, so that ratio of cached images
94 * stays the same. Depending on the frame composition complexity, there can be lots of
95 * source images cached for a single final frame; if we only removed one source image
96 * we'd eventually have the cache still filled only with source images. */
97 bool evicted_final = false;
98 bool evicted_source = false;
99 do {
100 const size_t count_final = final_image_cache_get_image_count(scene);
101 const size_t count_source = source_image_cache_get_image_count(scene);
102 evicted_final = false;
103 evicted_source = false;
104 const bool final_active = scene->ed->cache_flag & SEQ_CACHE_STORE_FINAL_OUT;
105 /* Evict one final item, and as much from source as needed to maintain ratio. */
106 if (count_final != 0) {
107 evicted_final = final_image_cache_evict(scene);
108 }
109 /* Only remove source images if there's more of them than final ones. */
110 if (count_source != 0 && (!final_active || count_source > count_final)) {
111 evicted_source = source_image_cache_evict(scene);
112 /* Only try to enforce the ratio when the final cache is active. */
113 if (evicted_source && final_active) {
114 const size_t items = divide_ceil_ul(count_source, std::max<size_t>(count_final, 1));
115 /* Start at "1" to make sure we only try to evict more frames if the ratio is above 1:1. */
116 for (size_t i = 1; i < items; i++) {
117 if (!source_image_cache_evict(scene)) {
118 /* Can't evict any more frames, stop. */
119 break;
120 }
121 }
122 }
123 }
124
125 } while (is_cache_full(scene) && (evicted_final || evicted_source));
126
127 /* Did we evict anything to free up the cache? */
128 return !(evicted_final || evicted_source);
129}
130
131static void update_range_with_effects(const Scene *scene, const Strip *strip, int2 &r_range)
132{
133 r_range.x = std::min(r_range.x, strip->left_handle());
134 r_range.y = std::max(r_range.y, strip->right_handle(scene) - 1);
135 Span<Strip *> effects = lookup_effects_by_strip(scene->ed, strip);
136 for (Strip *effect : effects) {
137 update_range_with_effects(scene, effect, r_range);
138 }
139}
140
141static void invalidate_final_cache_strip_range(Scene *scene, const Strip *strip)
142{
143 int2 range{MAXFRAME, -MAXFRAME};
144 update_range_with_effects(scene, strip, range);
145 final_image_cache_invalidate_frame_range(scene, range.x, range.y);
146}
147
149{
150 Strip *meta = lookup_meta_by_strip(editing_get(scene), strip);
151 if (meta == nullptr) {
152 return;
153 }
154
156}
157
159{
162 relations_invalidate_cache(scene, strip);
164}
165
167{
168 if (strip->effectdata && strip->type == STRIP_TYPE_SPEED) {
169 strip_effect_speed_rebuild_map(scene, strip);
170 }
171
172 /* Zero-input compositor effect source caches also need to be invalidated. */
173 if (strip->type == STRIP_TYPE_COMPOSITOR && !strip->is_effect_with_inputs()) {
175 }
176
178 intra_frame_cache_invalidate(scene, strip);
181
182 /* Needed to update VSE sound. */
184 prefetch_stop(scene);
185}
186
188{
189 Editing *ed = editing_get(scene);
190 if (ed != nullptr) {
191 ed->runtime->temporary_animation_frame = BKE_scene_frame_get(scene);
192 }
193}
194
196{
197 Editing *ed = editing_get(scene);
198 if (ed == nullptr || !ed->runtime->temporary_animation_frame.has_value()) {
199 return;
200 }
201
202 const float temp_frame = ed->runtime->temporary_animation_frame.value();
203 if (temp_frame != BKE_scene_frame_get(scene)) {
204 final_image_cache_invalidate_frame_range(scene, temp_frame, temp_frame);
205 ed->runtime->temporary_animation_frame.reset();
206 }
207}
208
209void relations_invalidate_scene_strips(const Main *bmain, const Scene *scene_target)
210{
211 for (Scene &scene : bmain->scenes) {
212 if (scene.ed != nullptr) {
213 for (Strip *strip : lookup_strips_by_scene(editing_get(&scene), scene_target)) {
214 relations_invalidate_cache_raw(&scene, strip);
215 }
216 }
217 }
218}
219
221 Scene *scene,
222 const char *old_name,
223 const char *new_name)
224{
225 for (Scene &scene_iter : bmain->scenes) {
226 Editing *ed = seq::editing_get(&scene_iter);
227 if (ed == nullptr) {
228 continue;
229 }
230 for (Strip *strip : seq::lookup_strips_by_scene(ed, scene)) {
231 BLI_assert(strip->scene_view_layer_name != nullptr);
232 if (!STREQ(strip->scene_view_layer_name, old_name)) {
233 continue;
234 }
235
236 MEM_delete(strip->scene_view_layer_name);
237 strip->scene_view_layer_name = new_name ?
238 BLI_strdup(new_name) :
240 BKE_view_layer_default_render(strip->scene)->name);
241 if (new_name == nullptr) {
242 /* View layer was deleted. */
243 seq::relations_invalidate_cache_raw(&scene_iter, strip);
244 }
245 }
246 }
247}
248
249void relations_invalidate_compositor_users(const Main *bmain, const bNodeTree *node_tree)
250{
251 for (Scene &scene : bmain->scenes) {
252 if (scene.ed != nullptr) {
253 for (Strip *strip : lookup_strips_by_compositor_node_group(editing_get(&scene), node_tree)) {
254 relations_invalidate_cache(&scene, strip);
255 }
256 }
257 }
258}
259
261{
262 for (Strip *strip : query_all_strips_recursive(&editing_get(scene)->seqbase)) {
263 if (strip->is_effect() && !strip->is_effect_with_inputs()) {
265 }
266 }
267}
268
270 MovieClip *clip_target,
271 ListBaseT<Strip> *seqbase)
272{
273 for (Strip *strip = seqbase->first(); strip != nullptr; strip = strip->next) {
274 if (strip->clip == clip_target) {
275 relations_invalidate_cache_raw(scene, strip);
276 }
277
278 if (strip->seqbase.first_ != nullptr) {
279 invalidate_movieclip_strips(scene, clip_target, &strip->seqbase);
280 }
281 }
282}
283
285{
286 for (Scene *scene = bmain->scenes.first(); scene != nullptr;
287 scene = static_cast<Scene *>(scene->id.next))
288 {
289 if (scene->ed != nullptr) {
290 invalidate_movieclip_strips(scene, clip_target, &scene->ed->seqbase);
291 }
292 }
293}
294
295void relations_free_imbuf(Scene *scene, ListBaseT<Strip> *seqbase, bool for_render)
296{
297 if (scene->ed == nullptr) {
298 return;
299 }
300
301 prefetch_stop(scene);
302 if (!for_render && seqbase == &scene->ed->seqbase) {
304 }
305
306 for (Strip &strip : *seqbase) {
307 if (for_render && strip.intersects_frame(scene, scene->r.cfra)) {
308 continue;
309 }
310
311 if (strip.data) {
312 if (!for_render && strip.type == STRIP_TYPE_MOVIE) {
314 }
315 if (strip.type == STRIP_TYPE_SPEED) {
316 strip_effect_speed_rebuild_map(scene, &strip);
317 }
318 }
319 if (strip.type == STRIP_TYPE_META) {
320 relations_free_imbuf(scene, &strip.seqbase, for_render);
321 }
322 if (strip.type == STRIP_TYPE_SCENE) {
323 /* FIXME: recurse downwards,
324 * but do recurse protection somehow! */
325 }
326 }
327}
328
330{
331 Editing *ed = editing_get(scene);
332 if (ed == nullptr) {
333 return;
334 }
335
336 relations_free_imbuf(scene, &ed->seqbase, false);
337 media_presence_free(scene);
339}
340
341static Strip *sequencer_check_scene_recursion(Scene *scene, ListBaseT<Strip> *seqbase)
342{
343 for (Strip &strip : *seqbase) {
344 if (strip.type == STRIP_TYPE_SCENE && strip.scene == scene) {
345 return &strip;
346 }
347
348 if (strip.type == STRIP_TYPE_SCENE && (strip.flag & SEQ_SCENE_STRIPS)) {
349 if (strip.scene && strip.scene->ed &&
350 sequencer_check_scene_recursion(scene, &strip.scene->ed->seqbase))
351 {
352 return &strip;
353 }
354 }
355
356 if (strip.type == STRIP_TYPE_META && sequencer_check_scene_recursion(scene, &strip.seqbase)) {
357 return &strip;
358 }
359 }
360
361 return nullptr;
362}
363
365{
366 Editing *ed = editing_get(scene);
367 if (ed == nullptr) {
368 return false;
369 }
370
371 Strip *recursive_seq = sequencer_check_scene_recursion(scene, &ed->seqbase);
372
373 if (recursive_seq != nullptr) {
374 BKE_reportf(reports,
376 "Recursion detected in video sequencer. Strip %s at frame %d will not be rendered",
377 recursive_seq->name + 2,
378 recursive_seq->left_handle());
379
380 for (Strip &strip : ed->seqbase) {
381 if (strip.type != STRIP_TYPE_SCENE && sequencer_strip_generates_image(&strip)) {
382 /* There are other strips to render, so render them. */
383 return false;
384 }
385 }
386 /* No other strips to render - cancel operator. */
387 return true;
388 }
389
390 return false;
391}
392
393bool relations_render_loop_check(Strip *strip_main, Strip *strip)
394{
395 if (strip_main == nullptr || strip == nullptr) {
396 return false;
397 }
398
399 if (strip_main == strip) {
400 return true;
401 }
402
403 if ((strip_main->input1 && relations_render_loop_check(strip_main->input1, strip)) ||
404 (strip_main->input2 && relations_render_loop_check(strip_main->input2, strip)))
405 {
406 return true;
407 }
408
409 for (StripModifierData &smd : strip_main->modifiers) {
410 if (smd.mask_strip && relations_render_loop_check(smd.mask_strip, strip)) {
411 return true;
412 }
413 }
414
415 return false;
416}
417
419{
420 strip->runtime->session_uid = BLI_session_uid_generate();
421}
422
423static bool get_uids_cb(Strip *strip, void *user_data)
424{
425 Set<SessionUID> &used_uids = *static_cast<Set<SessionUID> *>(user_data);
426 const SessionUID &session_uid = strip->runtime->session_uid;
427 if (!session_uid.is_generated()) {
428 printf("Sequence %s does not have UID generated.\n", strip->name);
429 return true;
430 }
431
432 if (used_uids.contains(session_uid)) {
433 printf("Sequence %s has duplicate UID generated.\n", strip->name);
434 return true;
435 }
436 used_uids.add(session_uid);
437 return true;
438}
439
441{
442 if (scene->ed == nullptr) {
443 return;
444 }
445
446 Set<SessionUID> used_uids;
447 foreach_strip(&scene->ed->seqbase, get_uids_cb, &used_uids);
448}
449
450bool exists_in_seqbase(const Strip *strip, const ListBaseT<Strip> *seqbase)
451{
452 for (Strip &strip_test : *seqbase) {
453 if (strip_test.type == STRIP_TYPE_META && exists_in_seqbase(strip, &strip_test.seqbase)) {
454 return true;
455 }
456 if (&strip_test == strip) {
457 return true;
458 }
459 }
460 return false;
461}
462
463} // namespace blender::seq
#define MAXFRAME
#define ELEM(...)
#define STREQ(a, b)
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void relations_update_view_layer_scene_strips(Main *bmain, Scene *scene, const char *old_name, const char *new_name)
void prefetch_stop(Scene *scene)
Definition prefetch.cc:336
Strip * lookup_meta_by_strip(Editing *ed, const Strip *key)
void relations_tag_temporary_animation_frame(Scene *scene)
bool sequencer_strip_generates_image(Strip *strip)
size_t final_image_cache_calc_memory_size(const Scene *scene)
void relations_invalidate_cache(Scene *scene, Strip *strip)
void intra_frame_cache_invalidate(Scene *scene)
void cache_settings_changed(Scene *scene)
bool relations_render_loop_check(Strip *strip_main, Strip *strip)
void final_image_cache_invalidate_frame_range(Scene *scene, const float timeline_frame_start, const float timeline_frame_end)
void thumbnail_cache_invalidate_strip(Scene *scene, const Strip *strip)
void movie_reader_cache_clear(Scene &scene)
void foreach_strip(ListBaseT< Strip > *seqbase, ForEachFunc callback, void *user_data)
Definition iterator.cc:60
static void query_all_strips_recursive(const ListBaseT< Strip > *seqbase, VectorSet< Strip * > &strips)
Definition iterator.cc:113
void relations_free_imbuf(Scene *scene, ListBaseT< Strip > *seqbase, bool for_render)
Span< Strip * > lookup_strips_by_scene(Editing *ed, const Scene *key)
void media_presence_free(Scene *scene)
void relations_invalidate_scene_strips(const Main *bmain, const Scene *scene_target)
bool source_image_cache_evict(Scene *scene)
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
static void invalidate_final_cache_strip_range(Scene *scene, const Strip *strip)
static void invalidate_raw_cache_of_parent_meta(Scene *scene, Strip *strip)
void source_image_cache_clear(Scene *scene)
void relations_invalidate_compositor_users(const Main *bmain, const bNodeTree *node_tree)
static bool get_uids_cb(Strip *strip, void *user_data)
static void update_range_with_effects(const Scene *scene, const Strip *strip, int2 &r_range)
Span< Strip * > lookup_strips_by_compositor_node_group(Editing *ed, const bNodeTree *key)
void relations_invalidate_temporary_animation_frame(Scene *scene)
void relations_invalidate_cache_raw(Scene *scene, Strip *strip)
void thumbnail_cache_clear(Scene *scene)
void source_image_cache_invalidate_strip(Scene *scene, const Strip *strip)
size_t source_image_cache_get_image_count(const Scene *scene)
void relations_invalidate_movieclip_strips(Main *bmain, MovieClip *clip_target)
void movie_metadata_invalidate(Strip &strip)
Span< Strip * > lookup_effects_by_strip(Editing *ed, const Strip *key)
void relations_refresh_all(Scene *scene)
static Strip * sequencer_check_scene_recursion(Scene *scene, ListBaseT< Strip > *seqbase)
bool exists_in_seqbase(const Strip *strip, const ListBaseT< Strip > *seqbase)
static void invalidate_movieclip_strips(Scene *scene, MovieClip *clip_target, ListBaseT< Strip > *seqbase)
void final_image_cache_clear(Scene *scene)
void strip_effect_speed_rebuild_map(Scene *scene, Strip *strip)
void relations_invalidate_generator_strips(Scene *scene)
bool final_image_cache_evict(Scene *scene)
bool is_cache_full(const Scene *scene)
void preview_cache_invalidate(Scene *scene)
void relations_session_uid_generate(Strip *strip)
bool relation_is_effect_of_strip(const Strip *effect, const Strip *input)
bool evict_caches_if_full(Scene *scene)
void media_presence_invalidate_strip(Scene *scene, const Strip *strip)
size_t final_image_cache_get_image_count(const Scene *scene)
size_t source_image_cache_calc_memory_size(const Scene *scene)
void relations_check_uids_unique_and_report(const Scene *scene)
bool relations_check_scene_recursion(Scene *scene, ReportList *reports)
void cache_cleanup(Scene *scene, CacheCleanup mode)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ RPT_WARNING
Definition BKE_report.hh:40
VecBase< int32_t, 2 > int2
MINLINE uint64_t divide_ceil_ul(uint64_t a, uint64_t b)
void DEG_id_tag_update(ID *id, unsigned int flags)
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
SessionUID BLI_session_uid_generate()
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ ID_RECALC_SEQUENCER_STRIPS
Definition DNA_ID.h:1122
ViewLayer * BKE_view_layer_default_render(const Scene *scene)
eEditingCacheFlag cache_flag
ListBaseT< Strip > seqbase
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
struct Editing * ed
struct RenderData r