Home > database >  ffmpeg color correction algorithm
ffmpeg color correction algorithm

Time:11-30

I'm trying to sync CSS and ffmpeg color correction. The goal is to create tool that converts CSS bri-sat-con-gam filter values to corresponding ffmpeg vals and vice versa.

e.g.

-vf "eq=brightness=0.3:saturation=1.3:contrast=1.1"

->

filter="brightness(30%) saturate(130%) contrast(110%)"

While algorithms for CSS properties are available at W3C, I have failed to find ones for ffmpeg. I've tried to dig github. Starting from here I've unfolded function calls, but it looks "a bit" too hard to navigate in 20 years and 104k commits old project. :)

I'll be very grateful if anyone can help me to figure out precise formulas for brightness, saturation, contrast and gamma. Any hints. Thx.

CodePudding user response:

This is the core function:

static void create_lut(EQParameters *param)
{
    int i;
    double   g  = 1.0 / param->gamma;
    double   lw = 1.0 - param->gamma_weight;

    for (i = 0; i < 256; i  ) {
        double v = i / 255.0;
        v = param->contrast * (v - 0.5)   0.5   param->brightness;

        if (v <= 0.0) {
            param->lut[i] = 0;
        } else {
            v = v * lw   pow(v, g) * param->gamma_weight;

            if (v >= 1.0)
                param->lut[i] = 255;
            else
                param->lut[i] = 256.0 * v;
        }
    }

    param->lut_clean = 1;
}

The filter operates only on 8-bit YUV inputs. This function creates a Look Up Table mapping all 8-bit input values 0-255 to output values. Then this table is applied to the input pixels.

The functions with names of the form set_parameter like set_gamma convert the user supplied argument to the final value used in the above function. contrast is applied only to the luma plane; saturation to the chroma planes.

  • Related