Skip to content
  • Niklas Haas's avatar
    shaders/colorspace: completely refactor HDR peak detection · 9b4aecb1
    Niklas Haas authored
    The previous approach of using an FIR with tunable hard threshold for
    scene changes had several problems:
    
    - the FIR involved annoying dynamic buffer sizes, high VRAM usage,
      and the FIR sum was prone to numerical overflow which limited the
      number of frames we could average over.
    
    - the hard scene change detection was prone to both false positives and
      false negatives, each with their own (annoying) issues.
    
    Scrap this entirely and switch to a dual approach of using a simple
    single-pole IIR low pass filter to smooth out noise, while using a
    softer scene change curve (with tunable low and high thresholds), based
    on `smoothstep`. The IIR filter is extremely simple in its
    implementation and has an arbitrarily user-tunable cutoff frequency,
    while the smoothstep-based scene change curve provides a good, tunable
    tradeoff between adaptation speed and stability - without exhibiting
    either of the traditional issues associated with the hard cutoff.
    
    Another way to think about the new options is that the "low threshold"
    provides a margin of error within which we don't care about small
    fluctuations in the scene (which will therefore be smoothed out by the
    IIR filter).
    
    While redesigning the algorithm, I also redesigned the API - so that
    peak detection and tone mapping are separate, discrete steps that can be
    done as part of two different shaders. (Or as part of the same shader)
    This is required for #40, and in particular, means that issue is now
    within reach.
    
    cf. https://github.com/mpv-player/mpv/pull/6415
    9b4aecb1