Fix #120077: Video output artifacts due to threaded YUV conversion #120168

Merged
Aras Pranckevicius merged 1 commits from aras_p/blender:swscale_align into main 2024-04-02 13:03:17 +02:00

ffmpeg AVFrame objects should use correct alignment between image
rows, or otherwise bad things might happen. In this particular
case, multi-threaded libswscale RGB->YUV conversion was trampling
over 4 bytes of V plane, for each thread boundary.

ffmpeg AVFrame objects should use correct alignment between image rows, or otherwise bad things might happen. In this particular case, multi-threaded libswscale RGB->YUV conversion was trampling over 4 bytes of V plane, for each thread boundary.
Aras Pranckevicius added 1 commit 2024-04-02 11:06:22 +02:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
83fe771184
Fix #120077: Video output artifacts due to threaded YUV conversion
ffmpeg AVFrame objects should use correct alignment between image
rows, or otherwise bad things might happen. In this particular
case, multi-threaded libswscale RGB->YUV conversion was tramping
over 4 bytes of V plane, for each thread boundary.
Author
Member

@blender-bot build

@blender-bot build
Aras Pranckevicius requested review from Sergey Sharybin 2024-04-02 11:10:02 +02:00
Sergey Sharybin approved these changes 2024-04-02 12:31:53 +02:00
Sergey Sharybin left a comment
Owner

It is a bit hard to do a proper review here, as it seems i can't reproduce the issue on my macOS arm64. The code and explanation looks clear though.

What is not clear is the relation of the issue with the previous refactor. it does not seem we ever passed proper alignment to av_image_get_buffer_size/av_image_fill_arrays, so not really clear why 422dd9404f introduced the issue?

It is a bit hard to do a proper review here, as it seems i can't reproduce the issue on my macOS arm64. The code and explanation looks clear though. What is not clear is the relation of the issue with the previous refactor. it does not seem we ever passed proper alignment to `av_image_get_buffer_size`/`av_image_fill_arrays`, so not really clear why 422dd9404f introduced the issue?
Author
Member

What is not clear is the relation of the issue with the previous refactor. it does not seem we ever passed proper alignment to av_image_get_buffer_size/av_image_fill_arrays, so not really clear why 422dd9404f introduced the issue?

From what I can tell, what happens is that each "slice" of image processed by sws_scale overwrites 4 bytes past the "image portion", at least on my machine for this video resolution. Without alignment, and with multiple threads, this actually adds "number of threads" artifacts right exactly where each thread slice ends, i.e. first 4 pixels of the next slice. With just one thread (as previously), I don't know if it actually overwrites past the array, or whether there is no overwriting happening. It could have been the former, just memory allocation / page granularity happens to be good enough that it was not triggering an actual crash.

And btw with most typical video resolutions the issue would not be showing up, since e.g. 1920x1080 has linesize that is already multiple of 16. But the repro video resolution has width 1080, which is not multiple of 16.

> What is not clear is the relation of the issue with the previous refactor. it does not seem we ever passed proper alignment to `av_image_get_buffer_size`/`av_image_fill_arrays`, so not really clear why 422dd9404f introduced the issue? From what I can tell, what happens is that each "slice" of image processed by sws_scale overwrites 4 bytes past the "image portion", at least on my machine for this video resolution. Without alignment, and with multiple threads, this actually adds "number of threads" artifacts right exactly where each thread slice ends, i.e. first 4 pixels of the next slice. With just one thread (as previously), I don't know if it actually overwrites past the array, or whether there is no overwriting happening. It could have been the former, just memory allocation / page granularity happens to be good enough that it was not triggering an actual crash. And btw with most typical video resolutions the issue would not be showing up, since e.g. 1920x1080 has linesize that is already multiple of 16. But the repro video resolution has width 1080, which is not multiple of 16.
Aras Pranckevicius merged commit 19ce05971d into main 2024-04-02 13:03:17 +02:00
Aras Pranckevicius deleted branch swscale_align 2024-04-02 13:03:19 +02:00

@aras_p Ah, I see. Thanks for the explanation.

@aras_p Ah, I see. Thanks for the explanation.
Contributor

it seems that on my computer (EndeavourOS) this change will generate strange output video (but in the preview windows it seems fine)

it seems that on my computer (EndeavourOS) this change will generate strange output video (but in the preview windows it seems fine)
Contributor
No description provided.
Contributor

image
In the code above, the rgb_frame's linesize is after aligning (which is 4352 in this case(my computer's align size is 64)), however, the data in "pixels" is generated in RE_render_result_rect_to_ibuf and hasn't been aligned, (so it's linesize is 1080*4=4320 in this case), so when copying the data, they should use different linesize.
image
I've changed the code to this and it works fine on my computer.

![image](/attachments/c463033b-61e0-45c3-b474-57e8c3a2d7a4) In the code above, the rgb_frame's linesize is after aligning (which is 4352 in this case(my computer's align size is 64)), however, the data in "pixels" is generated in RE_render_result_rect_to_ibuf and hasn't been aligned, (so it's linesize is 1080*4=4320 in this case), so when copying the data, they should use different linesize. ![image](/attachments/4874b62d-3676-412e-9eac-0414a6c0c279) I've changed the code to this and it works fine on my computer.
Contributor

my pr here: #120184

my pr here: https://projects.blender.org/blender/blender/pulls/120184
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
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
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
3 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#120168
No description provided.