Blender V5.3
movie_reader_cache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include <atomic>
12#include <limits>
13#include <memory>
14#include <optional>
15#include <string>
16#include <utility>
17
18#include "DNA_scene_types.h"
19#include "DNA_sequence_types.h"
20
21#include "BLI_mutex.hh"
22#include "BLI_path_utils.hh"
23#include "BLI_vector.hh"
24
25#include "BKE_main.hh"
26
27#include "MEM_guardedalloc.h"
28
29#include "SEQ_sequencer.hh"
30
32#include "IMB_imbuf.hh"
33
34#include "MOV_read.hh"
35
36#include "multiview.hh"
37#include "proxy.hh"
38
39namespace blender::seq {
40
41/* Give each top-level render a unique timestamp. Keep the active timestamp thread-local so nested
42 * scene renders inherit it without concurrent renders (e.g. prefetch) changing it. */
43static std::atomic<uint64_t> next_timestamp = 0;
44static thread_local uint64_t current_timestamp = 0;
45
46/* Give each rendering thread a unique owner id. Concurrent renders must not share the same movie
47 * reader because it holds a single decode position. Attempt to reuse same reader could lead to
48 * forcing seek for each decode yielding poor performance. */
49static std::atomic<int> next_owner = 0;
50static thread_local int current_owner = 0;
51
53{
54 if (current_owner == 0) {
55 current_owner = next_owner.fetch_add(1, std::memory_order_relaxed) + 1;
56 }
57 return current_owner;
58}
59
61 std::string source_filepath;
62 std::string multiview_filepath;
63 std::string colorspace;
64 std::string proxy_dir;
65 std::string multiview_suffix;
67 int stream_index = 0;
68
69 bool operator==(const MovieReaderKey &other) const = default;
70};
71
86
88 static constexpr int64_t max_entries = 16;
89 static constexpr uint64_t stale_after_timestamps = 8;
90 static constexpr int backward_seek_penalty = 32;
91
95
96 MovieReaderCache() = default;
97 ~MovieReaderCache() = default;
98
99 MovieReaderAccessor acquire(const MovieReaderKey &key, int frame_index, uint64_t timestamp);
101 void invalidate(const std::string &source_filepath);
102 void clear();
103 void release(MovieReaderCacheEntry &entry);
108};
109
111{
112 int64_t oldest_index = -1;
113 for (const int64_t i : entries_.index_range()) {
114 const MovieReaderCacheEntry &entry = *entries_[i];
115 const uint64_t age = timestamp_counter_ > entry.timestamp ?
117 0;
118 if (entry.is_accessed || age < stale_after_timestamps) {
119 continue;
120 }
121 if (oldest_index == -1 || entry.timestamp < entries_[oldest_index]->timestamp) {
122 oldest_index = i;
123 }
124 }
125 if (oldest_index == -1) {
126 return false;
127 }
128 entries_.remove(oldest_index);
129 return true;
130}
131
133{
134 while (entries_.size() > max_entries) {
136 break;
137 }
138 }
139}
140
142 const Strip &strip,
143 char r_filepath[FILE_MAX])
144{
145 BLI_path_join(r_filepath, FILE_MAX, strip.data->dirpath, strip.data->stripdata->filename);
146 BLI_path_abs(r_filepath, ID_BLEND_PATH_FROM_GLOBAL(&scene.id));
147}
148
150 const Strip &strip,
151 const std::optional<int> view_id)
152{
153 /* Readers are shared by source and decode settings, independently of the strip using them. */
154 MovieReaderKey key;
155 char filepath[FILE_MAX];
156 movie_reader_source_filepath_get(scene, strip, filepath);
157 key.source_filepath = filepath;
158
159 const bool use_multiview = view_id.has_value() && (strip.flag & SEQ_USE_VIEWS) != 0 &&
160 (scene.r.scemode & R_MULTIVIEW) != 0;
161 if (use_multiview && strip.views_format == R_IMF_VIEWS_INDIVIDUAL) {
162 char filepath_view[FILE_MAX];
163 const char *suffix = nullptr;
165 scene, filepath, *view_id, filepath_view, sizeof(filepath_view), &suffix))
166 {
167 key.multiview_filepath = filepath_view;
168 key.multiview_suffix = suffix;
169 }
170 }
171
173 key.stream_index = strip.streamindex;
174 key.colorspace = strip.data->colorspace_settings.name;
175
176 char proxy_dir[FILE_MAX];
177 if (seq_proxy_get_custom_dir(*scene.ed, strip, proxy_dir)) {
178 key.proxy_dir = proxy_dir;
179 }
180 return key;
181}
182
184 const int frame_index,
185 const uint64_t timestamp)
186{
187 /* An entry can be used only once per rendered frame. Penalize backward seeks, as decoding
188 * forwards is generally cheaper. */
189 std::unique_lock lock(mutex_);
190 if (timestamp_counter_ != timestamp) {
191 timestamp_counter_ = timestamp;
193 }
194 MovieReaderCacheEntry *best = nullptr;
195 int best_score = std::numeric_limits<int>::max();
196
197 const uint64_t owner = current_owner_get();
198 for (const std::unique_ptr<MovieReaderCacheEntry> &candidate : entries_) {
199 if (candidate->is_accessed || candidate->invalidated || candidate->owner != owner ||
200 candidate->timestamp == timestamp_counter_ || !(candidate->key == key))
201 {
202 continue;
203 }
204 const int distance = frame_index - candidate->frame_index;
205 const int score = distance >= 0 ? distance : -distance * backward_seek_penalty;
206 if (score < best_score) {
207 best = candidate.get();
208 best_score = score;
209 }
210 }
211
212 if (best == nullptr) {
213 if (entries_.size() >= max_entries) {
215 }
216 auto entry = std::make_unique<MovieReaderCacheEntry>();
217 entry->key = key;
218 entry->owner = owner;
219 best = entry.get();
220 entries_.append(std::move(entry));
221 }
222
223 best->is_accessed = true;
225 lock.unlock();
226
227 reader_open(*best);
228 return MovieReaderAccessor(this, best);
229}
230
232{
233 /* Property queries do not seek, so any free matching reader is suitable even if it was used by
234 * another strip during the current rendered frame. Prefer an initialized reader, then the most
235 * recently used one. Do not evict unrelated readers here; render-frame acquisition handles
236 * stale-entry cleanup. */
237 std::unique_lock lock(mutex_);
238 MovieReaderCacheEntry *best = nullptr;
239 bool best_is_initialized = false;
240 for (const std::unique_ptr<MovieReaderCacheEntry> &candidate : entries_) {
241 if (candidate->is_accessed || candidate->invalidated || !(candidate->key == key)) {
242 continue;
243 }
244 const bool candidate_is_initialized = candidate->reader != nullptr &&
245 MOV_is_initialized_and_valid(candidate->reader);
246 if (best == nullptr || (candidate_is_initialized && !best_is_initialized) ||
247 (candidate_is_initialized == best_is_initialized &&
248 candidate->timestamp > best->timestamp))
249 {
250 best = candidate.get();
251 best_is_initialized = candidate_is_initialized;
252 }
253 }
254
255 if (best == nullptr) {
256 auto entry = std::make_unique<MovieReaderCacheEntry>();
257 entry->key = key;
258 entry->owner = current_owner_get();
259 best = entry.get();
260 entries_.append(std::move(entry));
261 }
262
263 best->is_accessed = true;
264 /* This access does not decode a rendered frame, so do not reserve the reader for the current
265 * render timestamp. */
266 lock.unlock();
267
268 reader_open(*best);
269 if (!reader_ensure_initialized(*best)) {
270 MOV_close(best->reader);
271 best->reader = nullptr;
272 std::lock_guard failure_lock(mutex_);
273 best->invalidated = true;
274 }
275 return MovieReaderAccessor(this, best);
276}
277
279{
280 if (entry.reader != nullptr) {
281 return;
282 }
283
284 const MovieReaderKey &key = entry.key;
285 ColorManagedColorspaceSettings colorspace_settings;
286 IMB_colormanagement_colorspace_settings_set(&colorspace_settings, key.colorspace.c_str());
287 const std::string &filepath = key.multiview_filepath.empty() ? key.source_filepath :
289 entry.reader = MOV_open_file(
290 filepath.c_str(), key.flags, key.stream_index, true, &colorspace_settings);
291 if (entry.reader == nullptr) {
292 return;
293 }
294 if (!key.proxy_dir.empty()) {
295 seq_proxy_index_dir_set(entry.reader, key.proxy_dir.c_str());
296 }
297 if (!key.multiview_suffix.empty()) {
299 }
300
301 if (!key.multiview_filepath.empty() && !reader_ensure_initialized(entry)) {
302 MOV_close(entry.reader);
303 entry.reader = nullptr;
304 }
305}
306
308{
309 if (entry.reader == nullptr) {
310 return false;
311 }
313 return true;
314 }
315
316 ImBuf *ibuf = MOV_decode_frame(entry.reader, 0, IMB_PROXY_NONE);
317 if (ibuf == nullptr) {
318 return false;
319 }
320 IMB_freeImBuf(ibuf);
321 entry.frame_index = 0;
323}
324
326{
327 std::lock_guard lock(mutex_);
328 entry.is_accessed = false;
329 if (entry.invalidated) {
330 for (const int64_t i : entries_.index_range()) {
331 if (entries_[i].get() == &entry) {
332 entries_.remove(i);
333 break;
334 }
335 }
336 }
337}
338
339void MovieReaderCache::invalidate(const std::string &source_filepath)
340{
341 std::lock_guard lock(mutex_);
342 for (int64_t i = entries_.size(); i-- > 0;) {
343 MovieReaderCacheEntry &entry = *entries_[i];
344 if (entry.key.source_filepath != source_filepath) {
345 continue;
346 }
347 if (entry.is_accessed) {
348 entry.invalidated = true;
349 }
350 else {
351 entries_.remove(i);
352 }
353 }
354}
355
357{
358 std::lock_guard lock(mutex_);
359 for (int64_t i = entries_.size(); i-- > 0;) {
360 MovieReaderCacheEntry &entry = *entries_[i];
361 if (entry.is_accessed) {
362 entry.invalidated = true;
363 }
364 else {
365 entries_.remove(i);
366 }
367 }
368}
369
371 : cache_(cache), entry_(entry)
372{
373}
374
376 : cache_(other.cache_), entry_(other.entry_)
377{
378 other.cache_ = nullptr;
379 other.entry_ = nullptr;
380}
381
383{
384 if (this != &other) {
385 release();
386 cache_ = other.cache_;
387 entry_ = other.entry_;
388 other.cache_ = nullptr;
389 other.entry_ = nullptr;
390 }
391 return *this;
392}
393
398
399void MovieReaderAccessor::release()
400{
401 if (cache_ != nullptr) {
402 cache_->release(*entry_);
403 cache_ = nullptr;
404 entry_ = nullptr;
405 }
406}
407
408MovieReaderAccessor::operator bool() const
409{
410 return entry_ != nullptr && entry_->reader != nullptr;
411}
412
413ImBuf *MovieReaderAccessor::decode_frame(const int frame_index, const IMB_Proxy_Size proxy_size)
414{
415 ImBuf *ibuf = MOV_decode_frame(entry_->reader, frame_index, proxy_size);
416 if (ibuf != nullptr) {
417 entry_->frame_index = frame_index;
418 }
419 return ibuf;
420}
421
423{
424 return entry_ != nullptr && !entry_->key.multiview_filepath.empty();
425}
426
428{
429 return entry_ ? entry_->reader : nullptr;
430}
431
433{
434 return entry_ ? entry_->reader : nullptr;
435}
436
438 const Scene &key_scene,
439 const Strip &strip,
440 const int frame_index)
441{
442 return cache_scene.ed->runtime->movie_reader_cache->acquire(
443 movie_reader_key_get(key_scene, strip, std::nullopt), frame_index, current_timestamp);
444}
445
447 const Scene &key_scene,
448 const Strip &strip,
449 const int view_id,
450 const int frame_index)
451{
452 return cache_scene.ed->runtime->movie_reader_cache->acquire(
453 movie_reader_key_get(key_scene, strip, view_id), frame_index, current_timestamp);
454}
455
457 const Strip &strip)
458{
460 movie_reader_key_get(scene, strip, std::nullopt));
461}
462
464 const Strip &strip,
465 const int view_id)
466{
468 movie_reader_key_get(scene, strip, view_id));
469}
470
472{
474 if (!reader && reader.uses_multiview_filepath()) {
475 reader = {};
476 return movie_reader_cache_acquire_any_base(scene, strip);
477 }
478 return reader;
479}
480
481bool movie_reader_cache_can_produce_frames(const Scene &scene, const Strip &strip)
482{
483 const bool use_multiview = (strip.flag & SEQ_USE_VIEWS) != 0 &&
484 (scene.r.scemode & R_MULTIVIEW) != 0;
485 if (use_multiview && strip.views_format == R_IMF_VIEWS_INDIVIDUAL) {
487 const int files_num = seq_multiview_num_files_get(&scene, strip.views_format);
488 if (files_num <= 0) {
489 return bool(movie_reader_cache_acquire_any_base(scene, strip));
490 }
491
492 MovieReaderAccessor primary_reader = movie_reader_cache_acquire_any_view(scene, strip, 0);
493 if (!primary_reader.uses_multiview_filepath()) {
494 return bool(primary_reader);
495 }
496
497 bool all_readers_open = bool(primary_reader);
498 readers.append(std::move(primary_reader));
499 for (int view_id = 1; view_id < files_num && all_readers_open; view_id++) {
500 readers.append(movie_reader_cache_acquire_any_view(scene, strip, view_id));
501 all_readers_open = bool(readers.last());
502 }
503 if (all_readers_open) {
504 return true;
505 }
506 }
507 return bool(movie_reader_cache_acquire_any_base(scene, strip));
508}
509
511{
512 return MEM_new<MovieReaderCache>(__func__);
513}
514
516{
517 MEM_delete(cache);
518}
519
521{
522 current_timestamp = next_timestamp.fetch_add(1, std::memory_order_relaxed) + 1;
523}
524
525void movie_reader_cache_invalidate(Scene &scene, const Strip &strip)
526{
527 char filepath[FILE_MAX];
528 movie_reader_source_filepath_get(scene, strip, filepath);
529 scene.ed->runtime->movie_reader_cache->invalidate(filepath);
530}
531
533{
535}
536
537} // namespace blender::seq
#define FILE_MAX
#define BLI_path_join(...)
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
Read Blender memory allocation and freeing.
volatile int lock
void append(const T &value)
const T & last(const int64_t n=0) const
const T & data(const Action &owning_action) const
MovieReaderAccessor & operator=(const MovieReaderAccessor &)=delete
ImBuf * decode_frame(int frame_index, IMB_Proxy_Size proxy_size)
float distance(float, float) RET
static std::atomic< int > next_owner
MovieReaderAccessor movie_reader_cache_acquire_view(Scene &cache_scene, const Scene &key_scene, const Strip &strip, const int view_id, const int frame_index)
void movie_reader_cache_timestamp_bump()
void movie_reader_cache_clear(Scene &scene)
static void movie_reader_source_filepath_get(const Scene &scene, const Strip &strip, char r_filepath[FILE_MAX])
static uint64_t current_owner_get()
MovieReaderAccessor movie_reader_cache_acquire_any(const Scene &scene, const Strip &strip)
void movie_reader_cache_invalidate(Scene &scene, const Strip &strip)
int seq_multiview_num_files_get(const Scene *scene, eImageFormat_ViewsFormat views_format)
Definition multiview.cc:29
void seq_proxy_index_dir_set(MovieReader *anim, const char *base_dir)
Definition proxy.cc:784
bool seq_multiview_view_filepath_get(const Scene &scene, const char *filepath, const int view_id, char *r_filepath, const size_t r_filepath_maxncpy, const char **r_suffix)
Definition multiview.cc:55
static MovieReaderAccessor movie_reader_cache_acquire_any_view(const Scene &scene, const Strip &strip, const int view_id)
MovieReaderCache * movie_reader_cache_create()
static MovieReaderAccessor movie_reader_cache_acquire_any_base(const Scene &scene, const Strip &strip)
static uint64_t current_timestamp
static MovieReaderKey movie_reader_key_get(const Scene &scene, const Strip &strip, const std::optional< int > view_id)
static std::atomic< uint64_t > next_timestamp
bool seq_proxy_get_custom_dir(const Editing &ed, const Strip &strip, char r_dirpath[FILE_MAX])
Definition proxy.cc:194
static int current_owner
MovieReaderAccessor movie_reader_cache_acquire(Scene &cache_scene, const Scene &key_scene, const Strip &strip, const int frame_index)
bool movie_reader_cache_can_produce_frames(const Scene &scene, const Strip &strip)
void movie_reader_cache_destroy(MovieReaderCache *cache)
bool MOV_is_initialized_and_valid(const MovieReader *anim)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void MOV_close(MovieReader *anim)
Definition movie_read.cc:70
ImBuf * MOV_decode_frame(MovieReader *anim, int frame_index, IMB_Proxy_Size preview_size)
void MOV_set_multiview_suffix(MovieReader *anim, const char *suffix)
MovieReader * MOV_open_file(const char *filepath, const ImBufFlags ib_flags, const int streamindex, const bool keep_original_colorspace, ColorManagedColorspaceSettings *colorspace_settings)
void IMB_freeImBuf(ImBuf *ibuf)
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
std::mutex Mutex
Definition BLI_mutex.hh:51
@ R_IMF_VIEWS_INDIVIDUAL
seq::EditingRuntime * runtime
struct Editing * ed
struct RenderData r
MovieReaderCache * movie_reader_cache
void invalidate(const std::string &source_filepath)
MovieReaderAccessor acquire(const MovieReaderKey &key, int frame_index, uint64_t timestamp)
Vector< std::unique_ptr< MovieReaderCacheEntry > > entries_
void release(MovieReaderCacheEntry &entry)
static constexpr int64_t max_entries
void reader_open(MovieReaderCacheEntry &entry)
MovieReaderAccessor acquire_any(const MovieReaderKey &key)
static constexpr int backward_seek_penalty
static constexpr uint64_t stale_after_timestamps
bool reader_ensure_initialized(MovieReaderCacheEntry &entry)
bool operator==(const MovieReaderKey &other) const =default