VSE: replace Subsampled3x3 filter by a general Box filter #117584

Merged
Aras Pranckevicius merged 1 commits from aras_p/blender:vse_box_filter into main 2024-01-29 18:41:40 +01:00

Part of overall "improve filtering situation" (#116980): replace Subsampled3x3 (added for blender 3.5 in f210842a72 et al.) strip scaling filter with a general Box filter.

Subsampled3x3 is really a Box filter ("average pixel values over NxM region"), hardcoded to 3x3 size. As such, it works pretty well when downscaling images by 3x on each axis. But starts to break down and introduce aliasing at other scaling factors. When scaling up or scaling down by less than 3x, using total of 9 samples is a bit of overkill and hurts performance.

So instead, calculate the amount of NxM samples needed by looking at scaling factors on X/Y axes. Note: use at least 2 samples on each axis, so that when rotation is present, the result edges will get some anti-aliasing, just like it was happening in previous filter implementation.

Notes

The (very recently added) sequencer_render_filter.box test is failing, as expected; the expected image needs to be updated with merge of this PR.

Example images

Source: this perspective grid pattern image. The result images will be the same input, scaled down in VSE and then scaled up again with an adjustment layer using Nearest filtering:
subsgrid0_source.png

Scaling that down by 3x using currently default Bilinear filtering produces a lot of aliasing, which the current Subsampled3x3 improves quite a bit. That is good so far:
subsgrid1_3x_bilin.png subsgrid1_3x_subs.png

However, at 4x downscaling, previous Subsampled3x3 vs the new Box:
subsgrid1_4x_subs.png subsgrid1_4x_subsBox.png

At 15x downscaling, old vs new:
subsgrid1_15x_subs.png subsgrid1_15x_subsBox.png

Or another case, this image:
subs0_source.png

Downscaling it exactly by 12x looks almost correct:
subs1_12x.png

However downscaling it by 30x is complete nonsense!
subs1_30x.png

The new Box filter, downscaling by 30x looks like this which is way more sensible:
subs1_30xBox.png

Part of overall "improve filtering situation" (#116980): replace Subsampled3x3 (added for blender 3.5 in f210842a7259 et al.) strip scaling filter with a general Box filter. Subsampled3x3 is really a Box filter ("average pixel values over NxM region"), hardcoded to 3x3 size. As such, it works pretty well when downscaling images by 3x on each axis. But starts to break down and introduce aliasing at other scaling factors. When scaling up or scaling down by less than 3x, using total of 9 samples is a bit of overkill and hurts performance. So instead, calculate the amount of NxM samples needed by looking at scaling factors on X/Y axes. Note: use at least 2 samples on each axis, so that when rotation is present, the result edges will get some anti-aliasing, just like it was happening in previous filter implementation. ### Notes The (very recently added) `sequencer_render_filter.box` test is failing, as expected; the expected image needs to be updated with merge of this PR. ### Example images Source: this perspective grid pattern image. The result images will be the same input, scaled down in VSE and then scaled up again with an adjustment layer using Nearest filtering: ![subsgrid0_source.png](/attachments/8ae069fe-e67d-463e-88d3-4e45a39af114) Scaling that down by 3x using currently default Bilinear filtering produces a lot of aliasing, which the current Subsampled3x3 improves quite a bit. That is good so far: ![subsgrid1_3x_bilin.png](/attachments/7671e521-781c-42a8-809b-3ffa8bee978a) ![subsgrid1_3x_subs.png](/attachments/827f2a55-8984-4c60-9e23-3eafec9d9f6d) However, at 4x downscaling, previous Subsampled3x3 vs the new Box: ![subsgrid1_4x_subs.png](/attachments/1ff05a63-dbff-4a9d-b0dc-a76615746ca9) ![subsgrid1_4x_subsBox.png](/attachments/706142c2-0128-4eb1-8d1c-42cac42cb202) At 15x downscaling, old vs new: ![subsgrid1_15x_subs.png](/attachments/92bdd43a-530b-4e58-8a4e-20a5d17a60d2) ![subsgrid1_15x_subsBox.png](/attachments/5d719682-1f70-4448-80c4-c3043227decf) Or another case, this image: ![subs0_source.png](/attachments/26c9a6b7-54d9-415d-a38c-0f1af8b7c807) Downscaling it exactly by 12x looks almost correct: ![subs1_12x.png](/attachments/c8d4f3c3-cd11-41fc-a63f-c0cf14328afe) However downscaling it by 30x is complete nonsense! ![subs1_30x.png](/attachments/5e96f6fe-2daa-48e1-91e9-1c9d2ed72aee) The new Box filter, downscaling by 30x looks like this which is way more sensible: ![subs1_30xBox.png](/attachments/432708f5-960e-4ae9-9d34-7a4a3dbde9d6)
Aras Pranckevicius force-pushed vse_box_filter from 5dcc8e4c74 to c25412142d 2024-01-28 20:27:59 +01:00 Compare
Author
Member

@blender-bot build

@blender-bot build
Aras Pranckevicius changed title from WIP: VSE: replace Subsampled3x3 filter by a general Box filter to VSE: replace Subsampled3x3 filter by a general Box filter 2024-01-28 20:54:51 +01:00
Aras Pranckevicius added this to the Video Sequencer project 2024-01-28 20:54:56 +01:00
Aras Pranckevicius requested review from Sergey Sharybin 2024-01-29 05:59:59 +01:00
Sergey Sharybin requested changes 2024-01-29 11:10:35 +01:00
Sergey Sharybin left a comment
Owner

Having O(2) complexity could be good for the 3x3 size, but if we allow larger filter sizes we need to avoid it.

The box filter is separable, so you can apply it independently along the X and Y axis, significantly lowering the computational cost.

Having O(2) complexity could be good for the 3x3 size, but if we allow larger filter sizes we need to avoid it. The box filter is [separable](https://en.wikipedia.org/wiki/Separable_filter), so you can apply it independently along the X and Y axis, significantly lowering the computational cost.
Author
Member

Having O(2) complexity could be good for the 3x3 size, but if we allow larger filter sizes we need to avoid it.

@Sergey I skipped on that so far since I thought that "hey, the upper count of total amount of samples done is bounded by the size of the source media", but you're right, I should at least try a separable filter. Will do!

> Having O(2) complexity could be good for the 3x3 size, but if we allow larger filter sizes we need to avoid it. @Sergey I skipped on that so far since I thought that "hey, the upper count of total amount of samples done is bounded by the size of the source media", but you're right, I should at least try a separable filter. Will do!
Author
Member

@Sergey though now that I think about it, I'm not quite sure that separable approach would even work here at all. This is not convolution, this is resampling. Each NxM block of source pixels gets averaged into one destination pixel. As a result, all source pixels are read once, and they have to be. There's no way to rearrange or optimize anything; all the source pixels have to be sampled. If this was convolution (e.g. "box blur"), then sure, doing convolution in one direction and then in the other direction would save a lot of sampling, but this is not a convolution.

@Sergey though now that I think about it, I'm not quite sure that separable approach would even work here at all. This is not _convolution_, this is _resampling_. Each NxM block of source pixels gets averaged into one destination pixel. As a result, all source pixels are read once, and they have to be. There's no way to rearrange or optimize anything; all the source pixels have to be sampled. If this was convolution (e.g. "box blur"), then sure, doing convolution in one direction and then in the other direction would save a lot of sampling, but this is not a convolution.
Sergey Sharybin approved these changes 2024-01-29 18:16:11 +01:00
Sergey Sharybin left a comment
Owner

This is not convolution, this is resampling

This is a good point. Somehow wasn't that obvious, and thought it is first doing some sort of LPF before resampling. Sorry for the confusion.

> This is not convolution, this is resampling This is a good point. Somehow wasn't that obvious, and thought it is first doing some sort of LPF before resampling. Sorry for the confusion.
Aras Pranckevicius merged commit 5bd1e0bb22 into main 2024-01-29 18:41:40 +01:00
Aras Pranckevicius deleted branch vse_box_filter 2024-01-29 18:41:42 +01:00
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 Assignees
2 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#117584
No description provided.