Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This reduces colored noise and edge artifacts in RGB gain maps, as shown in #487. The examples of improved results there use the approach from this PR. Both versions in those JPEG comparisons use full-resolution Q80 YCbCr 4:4:4 gain maps.
While the primary motivation for this PR is to improve image quality, it has a secondary benefit in that it reduces file size (likely caused by the artifacts not compressing as well). In a native test of 20 HDR/SDR pairs at roughly 2K and 4K in P3, all 40 files became smaller, saving 11.0% overall.
The approach impacts time to calculate the gain map modestly. Median API3 encoding time increased 3.5%, with individual configuration medians ranging from 1.3% faster to 6.1% slower. These timings were measured using a supplied, precompressed SDR base and exclude base creation and file I/O.
Change
When a channel is nearly zero in both SDR and HDR inputs, small input differences can produce large swings in gain. Those gains contribute little to that channel's reconstructed value, but lossy map encoding can spread their variation into other channels. This may result in significant noise or artifact, such as in areas of uniform color (such as blue sky gradients) or high frequency detail.
This PR uses a symmetric
203/255-nit offset in the gain calculation and matching normalized1/255SDR and HDR metadata offsets to make those ratios more stable. It builds on the offset approach in #358 while preserving the existing boost-range selection and caller controls.The change applies to best-quality multichannel generation with supplied HDR/SDR inputs in the shared JPEG, AVIF and HEIF path. Single-channel, realtime and automatic HDR-to-SDR tone-mapping behavior remain unchanged. It does not change map subsampling and is independent of the AVIF/HEIF 4:4:4 proposal #486 (these PRs are analogous in terms of cross channel error causing artifact, but the underlying issues are different - with this PR relating to gain map calculation and the other PR addressing AVIF/HEIF encoding using chroma subsampling).
Tradeoffs
Quality is not uniformly better by every measurement. In the earlier 180-configuration size/gamut study, whole-image luminance error decreased in 161 configurations and increased in 19, with some local regions also showing higher error. The visual examples demonstrate useful improvements, not a guarantee for every image. The offset is an empirical compromise; smaller, larger and selective alternatives had mixed results.
Ranges that exclude 0 stops retain the original math to avoid clipping the adjusted gains. Explicit caller bounds excluding 0 bypass the new calculation up front. When such a range is discovered from the image, regeneration adds cost: an earlier synthetic test measured 56.3% longer API3 encoding. None of the 180 study configurations or 40 native timing configurations triggered this fallback.
Validation and measurement details
JPEG quality and size
1/510,1/255and2/255, plus two selective alternatives. Halving the offset improved luminance RMSE on two charts but worsened it on three photos and increased aggregate file size by 1.29% relative to1/255. This supports the chosen value for the tested inputs, not a universally optimal setting.Native timing
Correctness and compatibility
AVIF and HEIF