VSE: Add anim manager #118670

Open
Richard Antalik wants to merge 43 commits from iss/blender:anim-sharing-2 into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

When playing back composition in VSE consisting of multiple movie
strips, there is noticable delay when first frame of movie strip is
displayed. This happens, because anim(ImBufAnim) is being loaded.
Anims consume quite a bit of memory - about 100MB per strip, so they are
freed as soon as they are not needed for drawing preview.

This patch implements cache for anims, so that multiple strips can reuse
this resource and anim prefetching, which loads anims in background.
Delays during playback are mainly reduced by prefetching. Cache is used
for resource sharing and minimizing of memory usage (about 5x in test
file based on Gold edit).

There are 2 classes that implements this functionality:
AnimManager is interfacing with VSE code. It provides access to strip
anims. This is done with functions strip_anims_acquire(), which
causes, that anims will be locked, so they have to be unlocked by
strip_anims_release(). It implements prefetching and freeing of
"unused" anims - manage_anims(). And finally it owns the anim cache.

ShareableAnim is wrapper of ImBufAnim. It implements loading and
freeing of anims, on lower level. To facilitate sharing, it does user
counting. It supports multiview setups by storing vector of anims.
These are stored in order of how they are assigned to the strip. So
setups where one of strips which use same input file can be set up as
multiview while another is single view.

DNA Sequence::anims field is marked as deprecated.

On user level, Prefetching does load anims of all strips that are closer
than 512 frames to current frame. Anims of all other strips are freed.
Prefetching and freeing does not happen when user is scrubbing.

When playing back composition in VSE consisting of multiple movie strips, there is noticable delay when first frame of movie strip is displayed. This happens, because anim(`ImBufAnim`) is being loaded. Anims consume quite a bit of memory - about 100MB per strip, so they are freed as soon as they are not needed for drawing preview. This patch implements cache for anims, so that multiple strips can reuse this resource and anim prefetching, which loads anims in background. Delays during playback are mainly reduced by prefetching. Cache is used for resource sharing and minimizing of memory usage (about 5x in test file based on Gold edit). There are 2 classes that implements this functionality: `AnimManager` is interfacing with VSE code. It provides access to strip anims. This is done with functions `strip_anims_acquire()`, which causes, that anims will be locked, so they have to be unlocked by `strip_anims_release()`. It implements prefetching and freeing of "unused" anims - `manage_anims()`. And finally it owns the anim cache. `ShareableAnim` is wrapper of `ImBufAnim`. It implements loading and freeing of anims, on lower level. To facilitate sharing, it does user counting. It supports multiview setups by storing vector of anims. These are stored in order of how they are assigned to the strip. So setups where one of strips which use same input file can be set up as multiview while another is single view. DNA `Sequence::anims` field is marked as deprecated. On user level, Prefetching does load anims of all strips that are closer than 512 frames to current frame. Anims of all other strips are freed. Prefetching and freeing does not happen when user is scrubbing.
Richard Antalik added 4 commits 2024-02-23 16:37:37 +01:00
So far, refactoring has been done.
Ultimately this was due to missing nullptr check before trying to
process the image, but this should have been caught when loading
ImBufAnims. If any is missing, cancel multiview loading and load
movie as if it was single view only.
Richard Antalik added 1 commit 2024-02-23 16:40:44 +01:00
Richard Antalik added 2 commits 2024-02-23 21:49:17 +01:00
Richard Antalik added 1 commit 2024-02-23 22:08:36 +01:00
Richard Antalik added 1 commit 2024-02-24 19:29:20 +01:00
Richard Antalik added 1 commit 2024-02-24 20:07:39 +01:00
Richard Antalik added 1 commit 2024-02-24 20:44:19 +01:00
Richard Antalik changed title from WIP: VSE: share ImBufAnim resource between strips to VSE: share ImBufAnim resource between strips 2024-02-24 20:55:46 +01:00
Richard Antalik added 1 commit 2024-03-12 09:55:56 +01:00
Details are TODO
Richard Antalik added 1 commit 2024-03-21 20:55:22 +01:00
Richard Antalik changed title from VSE: share ImBufAnim resource between strips to [WIP] VSE: share ImBufAnim resource between strips 2024-03-21 20:59:14 +01:00
Richard Antalik added 1 commit 2024-03-21 21:02:23 +01:00
Author
Member

Note to self: The manager has to only prefetch anims. Strips need to acquire anim in main thread, because they are freed by depshraph.

Note to self: The manager has to only prefetch anims. Strips need to acquire anim in main thread, because they are freed by depshraph.
Richard Antalik added 4 commits 2024-05-21 17:32:19 +02:00
Richard Antalik added 1 commit 2024-05-28 22:14:24 +02:00
Richard Antalik added 1 commit 2024-05-28 22:17:24 +02:00
Richard Antalik added 2 commits 2024-05-29 00:04:32 +02:00
Richard Antalik added 1 commit 2024-05-29 01:10:59 +02:00
Richard Antalik added 2 commits 2024-05-30 00:49:24 +02:00
Richard Antalik added 2 commits 2024-06-11 15:30:07 +02:00
Hans Goudey changed title from [WIP] VSE: share ImBufAnim resource between strips to WIP: VSE: share ImBufAnim resource between strips 2024-06-12 14:54:42 +02:00
Richard Antalik added 1 commit 2024-06-12 15:47:46 +02:00
Richard Antalik added 4 commits 2024-07-14 09:26:05 +02:00
Richard Antalik added 5 commits 2024-07-18 09:47:53 +02:00
Richard Antalik added 2 commits 2024-07-18 10:16:02 +02:00
Richard Antalik changed title from WIP: VSE: share ImBufAnim resource between strips to VSE: share ImBufAnim resource between strips 2024-07-18 10:32:22 +02:00
Richard Antalik requested review from Sergey Sharybin 2024-07-18 10:47:03 +02:00
Richard Antalik requested review from Aras Pranckevicius 2024-07-18 10:47:04 +02:00
Richard Antalik added 1 commit 2024-07-18 11:10:04 +02:00
Richard Antalik added 1 commit 2024-07-18 11:49:14 +02:00
Richard Antalik changed title from VSE: share ImBufAnim resource between strips to VSE: Add anim manager 2024-07-18 11:50:47 +02:00
Richard Antalik added 1 commit 2024-07-18 12:37:46 +02:00
Richard Antalik added 1 commit 2024-07-18 12:42:47 +02:00
fix warns
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
6b8f66a566

Does all of this effectively resolve issue #118155?

Does all of this effectively resolve issue #118155?
Aras Pranckevicius reviewed 2024-07-19 09:57:15 +02:00
@ -31,10 +31,13 @@ struct bSound;
#ifdef __cplusplus
namespace blender::seq {
struct MediaPresence;
struct AnimManager;

Visual Studio emits a bunch of warnings about this, the issue being that there it is struct AnimManager but in the actual file it is class AnimManager

Visual Studio emits a bunch of warnings about this, the issue being that there it is `struct AnimManager` but in the actual file it is `class AnimManager`
Aras Pranckevicius reviewed 2024-07-19 10:12:17 +02:00
@ -0,0 +308,4 @@
for (int i : range) {
Sequence *seq = strips[i];
ShareableAnim &sh_anim = this->cache_entry_get(scene, seq);
sh_anim.mutex->lock();

While trying to test this out, I get a hang here. My test file contained two video tracks on top of each other, both referencing the same file. The hang happens when starting preview playback.

I'm on Windows, what seems to happen is that the ShareableAnim mutex that is tried to get locked here, is actually held by some thread that is already gone. I.e. some thread was apparently spawned, locked the mutex and finished without unlocking it.

While trying to test this out, I get a hang here. My test file contained two video tracks on top of each other, both referencing the same file. The hang happens when starting preview playback. I'm on Windows, what seems to happen is that the ShareableAnim mutex that is tried to get locked here, is actually held by some thread that is already gone. I.e. some thread was apparently spawned, locked the mutex and finished without unlocking it.
Aras Pranckevicius reviewed 2024-07-19 10:18:02 +02:00
@ -0,0 +1,74 @@
/* SPDX-FileCopyrightText: 2024 Blender Authors

Not related to this particular place, just wanted to raise as an issue: I consistenly get a crash when trying to drag and drop any video file into the VSE timeline, in a default empty video project.

What seems to happen is some sort of memory corruption when cleaning up some thread (not sure which one, as it already finished executing any user code and is about to wrap up at CRT/OS level). Another Blender related thread is doing sequencer_drag_drop.cc prefetch_data_fn, this bit:

    g_drop_coords.strip_len = IMB_anim_get_duration(anim, IMB_TC_NONE);
    short frs_sec;
    float frs_sec_base;
    if (IMB_anim_get_fps(anim, true, &frs_sec, &frs_sec_base)) {
      g_drop_coords.playback_rate = float(frs_sec) / frs_sec_base;
    }
    else {
      g_drop_coords.playback_rate = 0;
    }
    IMB_free_anim(anim); // <--- here
Not related to this particular place, just wanted to raise as an issue: I consistenly get a crash when trying to drag and drop any video file into the VSE timeline, in a default empty video project. What seems to happen is some sort of memory corruption when cleaning up _some_ thread (not sure which one, as it already finished executing any user code and is about to wrap up at CRT/OS level). Another Blender related thread is doing `sequencer_drag_drop.cc` `prefetch_data_fn`, this bit: ``` g_drop_coords.strip_len = IMB_anim_get_duration(anim, IMB_TC_NONE); short frs_sec; float frs_sec_base; if (IMB_anim_get_fps(anim, true, &frs_sec, &frs_sec_base)) { g_drop_coords.playback_rate = float(frs_sec) / frs_sec_base; } else { g_drop_coords.playback_rate = 0; } IMB_free_anim(anim); // <--- here ```
Aras Pranckevicius reviewed 2024-07-19 12:03:23 +02:00
@ -0,0 +21,4 @@
public:
blender::Vector<ImBufAnim *> anims; /* Ordered by view_id. */
blender::Set<Sequence *> users;
std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();

Wondering why this mutex is std::unique_ptr<std::mutex> and not just a mutex member, i.e. std::mutex?

Wondering why this mutex is `std::unique_ptr<std::mutex>` and not just a mutex member, i.e. `std::mutex`?
Author
Member

Does all of this effectively resolve issue #118155?

As far as you don't overwhelm prefetching thread, then yes. 100% solution would be to load all data in advance and never free them. So technically this is compromise, that should cover reasonable scenarios.

Thanks for review, I see there are issues still, will have to look at these after next week, as next week I will have time off.

> Does all of this effectively resolve issue #118155? As far as you don't overwhelm prefetching thread, then yes. 100% solution would be to load all data in advance and never free them. So technically this is compromise, that should cover reasonable scenarios. Thanks for review, I see there are issues still, will have to look at these after next week, as next week I will have time off.
Sergey Sharybin requested review from Francesco Siddi 2024-07-19 14:28:42 +02:00

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR118670) when ready.

I don't see a macOS build. Should it build again?

I don't see a macOS build. Should it build again?

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR118670) when ready.
Iliya Katushenock reviewed 2024-07-22 19:57:57 +02:00
@ -0,0 +24,4 @@
std::unique_ptr<std::mutex> mutex = std::make_unique<std::mutex>();
void release_from_strip(Sequence *seq);
void release_from_all_strips(void);

release_from_all_strips(void) -> release_from_all_strips()

`release_from_all_strips(void)` -> `release_from_all_strips()`
@ -0,0 +42,4 @@
/**
* Load anims used by strips and lock them so they won't be freed.
*/
void strip_anims_acquire(const Scene *scene, blender::Vector<Sequence *> strips);

blender::Vector<Sequence *> &r_strips

`blender::Vector<Sequence *> &r_strips`
@ -0,0 +52,4 @@
/**
* Get anims used by `seq`.
*/
blender::Vector<ImBufAnim *> &strip_anims_get(const Scene *scene, const Sequence *seq);

Result should be by value.

Result should be by value.
@ -0,0 +173,4 @@
}
if (is_multiview(scene, seq)) {
blender::Vector<ImBufAnim *> new_anims = multiview_anims_get(

Should be const

Should be const
@ -0,0 +187,4 @@
}
}
for (int i = 0; i < this->anims.size(); i++) {

for (const int i : this->anims.index_range())

`for (const int i : this->anims.index_range())`
@ -0,0 +222,4 @@
return false;
});
blender::Vector<Sequence *> strips_sorted = strips.as_span();

strips_sorted = strips;

`strips_sorted = strips;`
@ -0,0 +305,4 @@
strips = remove_duplicates_for_parallel_load(scene, strips);
threading::parallel_for(strips.index_range(), 1, [&](const IndexRange range) {
for (int i : range) {

const int i : range

`const int i : range`
@ -0,0 +310,4 @@
ShareableAnim &sh_anim = this->cache_entry_get(scene, seq);
sh_anim.mutex->lock();
sh_anim.acquire_anims(scene, seq);

Just for record, its much better to just use EnumerableThreadSpecific to accumulate elements from different threads and in result just move all of them into class fields instead of have mutex just for that.

Just for record, its much better to just use `EnumerableThreadSpecific` to accumulate elements from different threads and in result just move all of them into class fields instead of have mutex just for that.
@ -372,0 +378,4 @@
AnimManager *manager = seq_anim_manager_ensure(SEQ_editing_get(scene));
manager->strip_anims_acquire(scene, seq);
blender::Vector<ImBufAnim *> anims = manager->strip_anims_get(scene, seq);
int count = anims.size();

const int count

`const int count`
@ -1239,1 +1248,3 @@
BLI_listbase_count_at_most(&seq->anims, totfiles + 1) == totfiles;
std::min(anim_count, totfiles + 1) == totfiles;
if (anims.size() == 0) {

is_empty()

`is_empty()`
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u anim-sharing-2:iss-anim-sharing-2
git checkout iss-anim-sharing-2
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#118670
No description provided.