FFmpeg
Data Structures | Functions | Variables
cms.c File Reference
#include <math.h>
#include <string.h>
#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/csp.h"
#include "libavutil/slicethread.h"
#include "cms.h"
#include "csputils.h"
#include "libswscale/swscale.h"
#include "utils.h"

Go to the source code of this file.

Data Structures

struct  RGB
 
struct  IPT
 
struct  ICh
 
struct  Gamut
 
struct  CmsCtx
 

Functions

bool sws_color_map_noop (const SwsColorMap *map)
 Returns true if the given color map is a semantic no-op - that is, the overall RGB end to end transform would an identity mapping. More...
 
static const float hull (float I)
 
static av_always_inline ICh ipt2ich (IPT c)
 
static av_always_inline IPT ich2ipt (ICh c)
 
static Gamut gamut_from_colorspace (SwsColor fmt)
 
static av_always_inline IPT rgb2ipt (RGB c, const SwsMatrix3x3 rgb2lms)
 
static av_always_inline RGB ipt2rgb (IPT c, const SwsMatrix3x3 lms2rgb)
 
static bool ingamut (IPT c, Gamut gamut)
 
static ICh desat_bounded (float I, float h, float Cmin, float Cmax, Gamut gamut)
 
static ICh saturate (float hue, Gamut gamut)
 
static float softclip (float value, float source, float target)
 
static ICh mix_exp (ICh c, float x, float gamma, float base)
 Something like fmixf(base, c, x) but follows an exponential curve, note that this can be used to extend 'c' outwards for x > 1. More...
 
static float scale_gamma (float gamma, ICh ich, Gamut gamut)
 Drop gamma for colors approaching black and achromatic to avoid numerical instabilities, and excessive brightness boosting of grain, while also strongly boosting gamma for values exceeding the target peak. More...
 
static IPT clip_gamma (IPT ipt, float gamma, Gamut gamut)
 
static void st2094_pick_knee (float src_max, float src_min, float src_avg, float dst_max, float dst_min, float *out_src_knee, float *out_dst_knee)
 Helper function to pick a knee point based on the * HDR10+ brightness metadata and scene brightness average matching. More...
 
static void tone_map_setup (CmsCtx *ctx, bool dynamic)
 
static av_always_inline IPT tone_map_apply (const CmsCtx *ctx, IPT ipt)
 
static IPT perceptual (const CmsCtx *ctx, IPT ipt)
 
static IPT relative (const CmsCtx *ctx, IPT ipt)
 
static IPT absolute (const CmsCtx *ctx, IPT ipt)
 
static IPT saturation (const CmsCtx *ctx, IPT ipt)
 
static av_always_inline av_const uint16_t av_round16f (float x)
 
static av_always_inline void update_hue_peaks (CmsCtx *ctx, float P, float T)
 
static void generate_slice (void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
 
int sws_color_map_generate_static (v3u16_t *lut, int size, const SwsColorMap *map)
 Generates a single end-to-end color mapping 3DLUT embedding a static tone mapping curve. More...
 
int sws_color_map_generate_dynamic (v3u16_t *input, v3u16_t *output, int size_input, int size_I, int size_PT, const SwsColorMap *map)
 Generates a split pair of 3DLUTS, going to IPT and back, allowing an arbitrary dynamic EETF to be nestled in between these two operations. More...
 
void sws_tone_map_generate (v2u16_t *lut, int size, const SwsColorMap *map)
 Generate a 1D LUT of size size adapting intensity (I) levels from the source to the destination color space. More...
 

Variables

static const float maxDelta = 5e-5f
 

Function Documentation

◆ sws_color_map_noop()

bool sws_color_map_noop ( const SwsColorMap map)

Returns true if the given color map is a semantic no-op - that is, the overall RGB end to end transform would an identity mapping.

Definition at line 34 of file cms.c.

Referenced by adapt_colors().

◆ hull()

static const float hull ( float  I)
static

Definition at line 60 of file cms.c.

Referenced by tone_map_apply().

◆ ipt2ich()

static av_always_inline ICh ipt2ich ( IPT  c)
static

Definition at line 78 of file cms.c.

Referenced by clip_gamma(), and perceptual().

◆ ich2ipt()

static av_always_inline IPT ich2ipt ( ICh  c)
static

Definition at line 87 of file cms.c.

Referenced by clip_gamma(), and desat_bounded().

◆ gamut_from_colorspace()

static Gamut gamut_from_colorspace ( SwsColor  fmt)
static

Definition at line 112 of file cms.c.

Referenced by sws_color_map_generate_dynamic(), and sws_tone_map_generate().

◆ rgb2ipt()

static av_always_inline IPT rgb2ipt ( RGB  c,
const SwsMatrix3x3  rgb2lms 
)
static

Definition at line 140 of file cms.c.

Referenced by absolute(), generate_slice(), perceptual(), and saturation().

◆ ipt2rgb()

static av_always_inline RGB ipt2rgb ( IPT  c,
const SwsMatrix3x3  lms2rgb 
)
static

Definition at line 161 of file cms.c.

Referenced by absolute(), generate_slice(), perceptual(), and saturation().

◆ ingamut()

static bool ingamut ( IPT  c,
Gamut  gamut 
)
inlinestatic

Definition at line 182 of file cms.c.

Referenced by clip_gamma(), and desat_bounded().

◆ desat_bounded()

static ICh desat_bounded ( float  I,
float  h,
float  Cmin,
float  Cmax,
Gamut  gamut 
)
inlinestatic

Definition at line 220 of file cms.c.

Referenced by clip_gamma(), and saturate().

◆ saturate()

static ICh saturate ( float  hue,
Gamut  gamut 
)
inlinestatic

Definition at line 243 of file cms.c.

Referenced by update_hue_peaks().

◆ softclip()

static float softclip ( float  value,
float  source,
float  target 
)
static

Definition at line 274 of file cms.c.

Referenced by perceptual().

◆ mix_exp()

static ICh mix_exp ( ICh  c,
float  x,
float  gamma,
float  base 
)
inlinestatic

Something like fmixf(base, c, x) but follows an exponential curve, note that this can be used to extend 'c' outwards for x > 1.

Definition at line 298 of file cms.c.

Referenced by clip_gamma().

◆ scale_gamma()

static float scale_gamma ( float  gamma,
ICh  ich,
Gamut  gamut 
)
inlinestatic

Drop gamma for colors approaching black and achromatic to avoid numerical instabilities, and excessive brightness boosting of grain, while also strongly boosting gamma for values exceeding the target peak.

Definition at line 312 of file cms.c.

Referenced by clip_gamma().

◆ clip_gamma()

static IPT clip_gamma ( IPT  ipt,
float  gamma,
Gamut  gamut 
)
inlinestatic

Definition at line 320 of file cms.c.

Referenced by absolute(), and relative().

◆ st2094_pick_knee()

static void st2094_pick_knee ( float  src_max,
float  src_min,
float  src_avg,
float  dst_max,
float  dst_min,
float out_src_knee,
float out_dst_knee 
)
static

Helper function to pick a knee point based on the * HDR10+ brightness metadata and scene brightness average matching.

Inspired by SMPTE ST2094-10, with some modifications

Choose the destnation knee by picking the perceptual adaptation point between the source knee and the desired target. This moves the knee point, on the vertical axis, closer to the 1:1 (neutral) line.

Adjust the adaptation strength towards 1 based on how close the knee point is to its extreme values (min/max knee)

Definition at line 381 of file cms.c.

Referenced by tone_map_setup().

◆ tone_map_setup()

static void tone_map_setup ( CmsCtx ctx,
bool  dynamic 
)
static

Tune the slope at the knee point slightly: raise it to a user-provided gamma exponent, multiplied by an extra tuning coefficient designed to make the slope closer to 1.0 when the difference in peaks is low, and closer to linear when the difference between peaks is high.

Solve P of order 2 for: P(in_min) = out_min P'(0.0) = slope P(0.0) = 0.0

Solve Q of order 3 for: Q(in_max) = out_max Q''(in_max) = 0.0 Q(0.0) = 0.0 Q'(0.0) = slope

Definition at line 420 of file cms.c.

Referenced by sws_color_map_generate_dynamic(), and sws_tone_map_generate().

◆ tone_map_apply()

static av_always_inline IPT tone_map_apply ( const CmsCtx ctx,
IPT  ipt 
)
static

Avoids raising saturation excessively when raising brightness, and also desaturates when reducing brightness greatly to account for the reduction in gamut volume.

Definition at line 493 of file cms.c.

Referenced by generate_slice(), and sws_tone_map_generate().

◆ perceptual()

static IPT perceptual ( const CmsCtx ctx,
IPT  ipt 
)
static

Definition at line 520 of file cms.c.

Referenced by sws_color_map_generate_dynamic().

◆ relative()

static IPT relative ( const CmsCtx ctx,
IPT  ipt 
)
static

◆ absolute()

static IPT absolute ( const CmsCtx ctx,
IPT  ipt 
)
static

Definition at line 549 of file cms.c.

Referenced by max_abs_value_w16(), and sws_color_map_generate_dynamic().

◆ saturation()

static IPT saturation ( const CmsCtx ctx,
IPT  ipt 
)
static

◆ av_round16f()

static av_always_inline av_const uint16_t av_round16f ( float  x)
static

Definition at line 565 of file cms.c.

Referenced by generate_slice(), and sws_tone_map_generate().

◆ update_hue_peaks()

static av_always_inline void update_hue_peaks ( CmsCtx ctx,
float  P,
float  T 
)
static

Definition at line 571 of file cms.c.

Referenced by generate_slice().

◆ generate_slice()

static void generate_slice ( void *  priv,
int  jobnr,
int  threadnr,
int  nb_jobs,
int  nb_threads 
)
static

Definition at line 587 of file cms.c.

Referenced by sws_color_map_generate_dynamic().

◆ sws_color_map_generate_static()

int sws_color_map_generate_static ( v3u16_t lut,
int  size,
const SwsColorMap map 
)

Generates a single end-to-end color mapping 3DLUT embedding a static tone mapping curve.

Returns 0 on success, or a negative error code on failure.

Definition at line 679 of file cms.c.

Referenced by sws_lut3d_generate().

◆ sws_color_map_generate_dynamic()

int sws_color_map_generate_dynamic ( v3u16_t input,
v3u16_t output,
int  size_input,
int  size_I,
int  size_PT,
const SwsColorMap map 
)

Generates a split pair of 3DLUTS, going to IPT and back, allowing an arbitrary dynamic EETF to be nestled in between these two operations.

See sws_tone_map_generate().

Returns 0 on success, or a negative error code on failure.

The IPT transform already implies an explicit white point adaptation from src to dst, so to get absolute colorimetric semantics we have to explicitly undo this adaptation with a * corresponding inverse.

Definition at line 684 of file cms.c.

Referenced by sws_color_map_generate_static(), and sws_lut3d_generate().

◆ sws_tone_map_generate()

void sws_tone_map_generate ( v2u16_t lut,
int  size,
const SwsColorMap map 
)

Generate a 1D LUT of size size adapting intensity (I) levels from the source to the destination color space.

The LUT is normalized to the relevant intensity range directly. The second channel of each entry returns the corresponding 15-bit scaling factor for the P/T channels. The scaling factor k may be applied as (1 << 15) - k + (PT * k >> 15).

This is designed to be used with sws_gamut_map_generate_dynamic().

Returns 0 on success, or a negative error code on failure.

Definition at line 743 of file cms.c.

Referenced by sws_lut3d_update().

Variable Documentation

◆ maxDelta

const float maxDelta = 5e-5f
static

Definition at line 216 of file cms.c.

Referenced by clip_gamma(), desat_bounded(), and saturate().