ffmpeg: cache swscale contexts instead of re-creating them #118130
No reviewers
Labels
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
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
Viewport & EEVEE
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
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
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
4 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#118130
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "aras_p/blender:ffmpeg_swscale_cache"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
ffmpeg's libswscale is used to do RGB<->YUV conversions on movie reading
and writing. The "context" for the scale operation was being created
and destroyed for each movie clip / animation. Now, maintain a cache
of the scale contexts instead.
E.g. in Gold edit, it only ever needs two contexts (one for reading
source movie clips since they are all exactly the same resolution
and format; and one for rendering the resulting movie).
During playback, on some of the "slow" frames (camera cuts) this
saves 10-20ms (Windows, Ryzen 5950X). Rendering whole movie goes
from 390sec to 376sec.
@blender-bot build
This seems quite nice speedup for a simple change :)
Are these contexts "heavy"? As in, if there is a lot of media with mismatched resolutions, are we fine having all contexts in-memory, or do we want to eventually free unused ones, to give renderer more RAM?
@Sergey yeah I was thinking about that. They do internally create some threads, some lookup tables and possibly some buffers. Haven't measured how "heavy" they are exactly. I was thinking what should be the "proper logic" for that though, I would imagine:
Having some LRU mechanic of N contexts seems like a good thing.
I wouldn't worry too much about flushing it entirely on playback stop. Odds that you'll resume playback very soon, and having lower latency will be welcome.
I have experimented a bit yesterday and created 2 builds to compare delays during cuts: #118112 and #118114.
From the results, it seems that all ffmpeg data other than
AVCodecContext
uses only 14MB. So I wouldn't be conservative with how much of swscale contexts would you keep around. IMO it would be fine to not care about that at all especially if these are re-used.I have an idea how to combine those 2 PR's to significantly improve this decoding delay by keeping around some data ready to go. If
AVCodecContext
could be shared across anims (technically it can be) it would improve this much much more, but that may be bit more tricky.This PR seems to be well aligned with what I would want to do.
@iss not directly related to this, but I've been looking at better "culling" of strips (#117790) which particularly on Gold seem to bring in quite some benefits (none of the strips there cover "whole screen", but it often happens that there are three movies one atop another, with only topmost being visible).
I need to add more extensive testing to that before it's ready, but so far looking promising. Feels like that, plus faster ffmpeg "context things", might be good to avoid the worst playback hiccups.
Yes, I have noticed that. It's nice to have a way to detect such case.
The media used in Gold edit are quite optimized for fast decoding. I am not sure, but wouldn't be surprised if 4K movie with large GOP size would take much longer to create codec context. Also other users may be facing situations where they need to render 3 movies at once. So IMO this delay should be optimized out if possible.
Added the context scale cache max cap. Now no more than 32 spare swscale contexts are kept around.
@blender-bot build