ffcine-render 0.4.0

ffcine render path: Pipeline, Grade, encoder settings, and the ffmpeg + libplacebo (Vulkan) backend
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! libplacebo user shaders (mpv `.hook` format) generated by ffcine.

/// Parameters of [`look`]: the prototype's post-LUT grade (`render.py` `grade_filters`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LookParams {
    /// Stops, in display-linear light (BT.1886 2.4) ahead of the rest of the look.
    pub exposure: f32,
    pub warmth: f32,
    pub tint: f32,
    pub contrast: f32,
    pub gamma: f32,
    pub lift: f32,
    pub gain: f32,
    pub saturation: f32,
}

impl Default for LookParams {
    fn default() -> Self {
        Self {
            exposure: 0.0,
            warmth: 0.0,
            tint: 0.0,
            contrast: 0.0,
            gamma: 1.0,
            lift: 0.0,
            gain: 1.0,
            saturation: 1.0,
        }
    }
}

impl LookParams {
    pub fn is_identity(&self) -> bool {
        *self == Self::default()
    }

    /// `(name, value)` for the [`look`] hook's `//!PARAM`s.
    pub fn params(&self) -> Vec<(String, f64)> {
        [
            ("exposure", self.exposure),
            ("warmth", self.warmth),
            ("tint", self.tint),
            ("contrast", self.contrast),
            ("gamma", self.gamma),
            ("lift", self.lift),
            ("gain", self.gain),
            ("saturation", self.saturation),
        ]
        .into_iter()
        .map(|(k, v)| (k.to_owned(), f64::from(v)))
        .collect()
    }

    /// The [`look`] hook on one gamma-encoded RGB value, on the CPU (the LUT baker's look).
    #[allow(clippy::many_single_char_names)] // the shader's names
    pub fn apply(&self, rgb: [f32; 3]) -> [f32; 3] {
        use std::f32::consts::PI;
        let mut v = rgb.map(|c| c.clamp(0.0, 1.0));
        if self.exposure != 0.0 {
            let k = self.exposure.exp2();
            v = v.map(|c| (c.powf(2.4) * k).powf(1.0 / 2.4).clamp(0.0, 1.0));
        }
        let l = f32::midpoint(v[0].max(v[1]).max(v[2]), v[0].min(v[1]).min(v[2]));
        let (a, b, k) = (4.0, 0.333, 0.7);
        let ws = ((b - l) * a + 0.5).clamp(0.0, 1.0) * k;
        let wm =
            ((l - b) * a + 0.5).clamp(0.0, 1.0) * ((1.0 - l - b) * a + 0.5).clamp(0.0, 1.0) * k;
        let wh = ((l + b - 1.0) * a + 0.5).clamp(0.0, 1.0) * k;
        let (w, t) = (self.warmth, self.tint);
        let shift = [
            (ws * 0.4 + wm + wh * 0.6) * w,
            -wm * t,
            -(ws * 0.4 + wm + wh * 0.6) * w,
        ];
        let y = std::array::from_fn::<f32, 3, _>(|c| {
            let x = (v[c] + shift[c])
                .clamp(0.0, 1.0)
                .powf(1.0 / self.gamma.max(0.01));
            let x = x - self.contrast / PI * 0.5 * (2.0 * PI * x).sin();
            (self.lift + (self.gain - self.lift) * x).clamp(0.0, 1.0)
        });
        let luma = 0.2126 * y[0] + 0.7152 * y[1] + 0.0722 * y[2];
        y.map(|c| (luma + (c - luma) * self.saturation).clamp(0.0, 1.0))
    }
}

/// The prototype's post-LUT chain as one `PREOUTPUT` hook (gamma-encoded RGB after the LUT),
/// its parameters as `//!PARAM`s so every variant shares one compiled shader: `exposure` (stops,
/// a gain in display-linear light, gamma 2.4), then `colorbalance` (`rs/bs = ±0.4 w`, `rm = w`, `gm = −tint`, `bm = −w`, `rh/bh = ±0.6 w`, `pl=0`,
/// ffmpeg's shadow/mid/highlight weights), then `curves` (`y = x^(1/gamma)`,
/// `y −= contrast/π · ½ sin 2πy`, `y = lift + (gain − lift) y`, evaluated exactly instead of
/// through 17 spline points), then the Rec.709-luma saturation matrix.
pub fn look() -> &'static str {
    r"//!PARAM exposure
//!TYPE float
0.0
//!PARAM warmth
//!TYPE float
0.0
//!PARAM tint
//!TYPE float
0.0
//!PARAM contrast
//!TYPE float
0.0
//!PARAM gamma
//!TYPE float
//!MINIMUM 0.01
1.0
//!PARAM lift
//!TYPE float
0.0
//!PARAM gain
//!TYPE float
1.0
//!PARAM saturation
//!TYPE float
1.0
//!HOOK PREOUTPUT
//!BIND HOOKED
//!DESC ffcine look (exposure, colorbalance, curves, saturation)
vec4 hook() {
    vec4 c = HOOKED_texOff(vec2(0.0));
    vec3 v = clamp(c.rgb, 0.0, 1.0);
    if (exposure != 0.0)
        v = clamp(pow(pow(v, vec3(2.4)) * exp2(exposure), vec3(1.0 / 2.4)), 0.0, 1.0);
    float l = 0.5 * (max(v.r, max(v.g, v.b)) + min(v.r, min(v.g, v.b)));
    const float a = 4.0, b = 0.333, k = 0.7;
    float ws = clamp((b - l) * a + 0.5, 0.0, 1.0) * k;
    float wm = clamp((l - b) * a + 0.5, 0.0, 1.0) * clamp((1.0 - l - b) * a + 0.5, 0.0, 1.0) * k;
    float wh = clamp((l + b - 1.0) * a + 0.5, 0.0, 1.0) * k;
    v += ws * vec3(0.4 * warmth, 0.0, -0.4 * warmth);
    v += wm * vec3(warmth, -tint, -warmth);
    v += wh * vec3(0.6 * warmth, 0.0, -0.6 * warmth);
    v = clamp(v, 0.0, 1.0);
    vec3 y = pow(v, vec3(1.0 / gamma));
    y -= contrast / 3.14159265 * 0.5 * sin(6.28318531 * y);
    y = clamp(lift + (gain - lift) * y, 0.0, 1.0);
    float luma = dot(y, vec3(0.2126, 0.7152, 0.0722));
    c.rgb = clamp(mix(vec3(luma), y, saturation), 0.0, 1.0);
    return c;
}
"
}

/// ffmpeg's `unsharp=5:5:amount` (luma only) as a `PREOUTPUT` hook: after scaling and the LUT, on
/// gamma-encoded RGB, where the CPU chain runs it. The 5x5 kernel is the separable binomial
/// `[1 4 6 4 1]/16` blur, `out = in + amount * (in - blur)` on BT.601 luma (what ffmpeg's
/// auto-inserted RGB -> YUV conversion uses); adding the same delta to R, G and B moves Y only.
///
/// `None` for `amount == 0`.
pub fn unsharp(amount: f32) -> Option<String> {
    if amount == 0.0 {
        return None;
    }
    Some(format!(
        r"//!HOOK PREOUTPUT
//!BIND HOOKED
//!DESC unsharp 5x5 luma amount={amount}
vec4 hook() {{
    const vec3 k = vec3(0.299, 0.587, 0.114);
    const float w[5] = float[5](1.0, 4.0, 6.0, 4.0, 1.0);
    vec4 c = HOOKED_texOff(vec2(0.0));
    float b = 0.0;
    for (int j = -2; j <= 2; j++)
        for (int i = -2; i <= 2; i++)
            b += w[i + 2] * w[j + 2] * dot(HOOKED_texOff(vec2(float(i), float(j))).rgb, k);
    c.rgb += {amount:.4} * (dot(c.rgb, k) - b / 256.0);
    return c;
}}
"
    ))
}

/// Per-shot finishing after the look, as one `PREOUTPUT` hook (the prototype's `post` filters in
/// `render.py`): `darken` (RGB multiplier), ffmpeg's `vignette` (`angle` in radians; the factor
/// is `cos(angle · d)^4` with `d` the distance from the middle over the half diagonal, 0 past it),
/// the shutter `flash` (`lift`: blacks raised towards white, `out = lift + (1 - lift) in`, as
/// `colorlevels romin`), then a `fade` towards `fade_r/g/b` (`fade` 1 = the picture, 0 = the colour,
/// ffmpeg's `fade` on gamma-encoded values). `fade` and `lift` are per-frame tracks.
pub fn finish() -> &'static str {
    r"//!PARAM darken
//!TYPE float
1.0
//!PARAM vignette
//!TYPE float
0.0
//!PARAM lift
//!TYPE float
0.0
//!PARAM fade
//!TYPE float
1.0
//!PARAM fade_r
//!TYPE float
0.0
//!PARAM fade_g
//!TYPE float
0.0
//!PARAM fade_b
//!TYPE float
0.0
//!HOOK PREOUTPUT
//!BIND HOOKED
//!DESC ffcine finish (darken, vignette, flash, fade)
vec4 hook() {
    vec4 c = HOOKED_texOff(vec2(0.0));
    vec3 v = c.rgb * darken;
    if (vignette > 0.0) {
        vec2 h = 0.5 * HOOKED_size;
        float d = length(HOOKED_pos * HOOKED_size - h) / length(h);
        float k = d > 1.0 ? 0.0 : cos(vignette * d);
        v *= k * k * k * k;
    }
    v = lift + (1.0 - lift) * v;
    c.rgb = mix(vec3(fade_r, fade_g, fade_b), v, fade);
    return c;
}
"
}

/// Alpha for a [`crate::Layer`]: `opacity` times a rounded-rectangle mask with corner `radius`
/// in layer pixels (antialiased over one pixel), as an `OUTPUT` hook. Both can be per-frame
/// tracks.
pub fn alpha() -> &'static str {
    r"//!PARAM opacity
//!TYPE float
1.0
//!PARAM radius
//!TYPE float
0.0
//!HOOK OUTPUT
//!BIND HOOKED
//!DESC ffcine layer alpha (opacity, rounded corners)
vec4 hook() {
    vec4 c = HOOKED_texOff(vec2(0.0));
    vec2 h = 0.5 * HOOKED_size;
    vec2 p = HOOKED_pos * HOOKED_size - h;
    float r = clamp(radius, 0.0, min(h.x, h.y));
    vec2 q = abs(p) - h + vec2(r);
    float d = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
    float a = clamp(0.5 - d, 0.0, 1.0) * clamp(opacity, 0.0, 1.0);
    c.a *= a;
    return c;
}
"
}

/// Alpha for a comp layer (`ffcine-comp`, `docs/compositing.md#placement`): [`alpha`]'s
/// `opacity` and rounded corners (`radius`, texture pixels), times a shape mask: `shape` 0 none,
/// 1 rectangle, 2 ellipse, inside `mask_x/y/w/h` (fractions of the texture), with a soft edge
/// `feather` texture pixels wide centred on the shape's edge (antialiased over one pixel at 0),
/// and `invert` (1 keeps the outside). `radius` and `feather` are horizontal texture pixels; `ky`
/// is how many vertical texture pixels make one horizontal (a layer scaled unevenly). An `OUTPUT`
/// hook; every value can be a per-frame track.
pub fn layer_alpha() -> &'static str {
    r"//!PARAM opacity
//!TYPE float
1.0
//!PARAM ky
//!TYPE float
//!MINIMUM 0.001
1.0
//!PARAM radius
//!TYPE float
0.0
//!PARAM shape
//!TYPE float
0.0
//!PARAM mask_x
//!TYPE float
0.0
//!PARAM mask_y
//!TYPE float
0.0
//!PARAM mask_w
//!TYPE float
1.0
//!PARAM mask_h
//!TYPE float
1.0
//!PARAM feather
//!TYPE float
0.0
//!PARAM invert
//!TYPE float
0.0
//!HOOK OUTPUT
//!BIND HOOKED
//!DESC ffcine comp layer alpha (opacity, rounded corners, mask)
vec4 hook() {
    vec4 c = HOOKED_texOff(vec2(0.0));
    // In horizontal texture pixels.
    vec2 size = HOOKED_size / vec2(1.0, ky);
    vec2 h = 0.5 * size;
    vec2 p = HOOKED_pos * size - h;
    float r = clamp(radius, 0.0, min(h.x, h.y));
    vec2 q = abs(p) - h + vec2(r);
    float d = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
    float a = clamp(0.5 - d, 0.0, 1.0) * clamp(opacity, 0.0, 1.0);
    if (shape > 0.5) {
        vec2 mh = max(0.5 * vec2(mask_w, mask_h) * size, vec2(1e-3));
        vec2 mp = HOOKED_pos * size - vec2(mask_x, mask_y) * size - mh;
        float md;
        if (shape < 1.5) {
            vec2 mq = abs(mp) - mh;
            md = length(max(mq, 0.0)) + min(max(mq.x, mq.y), 0.0);
        } else {
            // The implicit (|p/h| - 1) over its gradient: px from the edge near it, so the
            // soft edge is `feather` px wide all round, not only on the short axis.
            float l = length(mp / mh);
            vec2 g = mp / (mh * mh);
            md = l < 1e-4 ? -min(mh.x, mh.y) : (l - 1.0) * l / max(length(g), 1e-9);
        }
        float m = clamp(0.5 - md / max(feather, 1.0), 0.0, 1.0);
        a *= invert > 0.5 ? 1.0 - m : m;
    }
    c.a *= a;
    return c;
}
"
}

/// A solid colour layer's picture (`ffcine-comp`): `solid_r/g/b` (gamma-encoded output values,
/// as [`background`]) replacing the picture, a `PREOUTPUT` hook ahead of the finish.
pub fn solid() -> &'static str {
    r"//!PARAM solid_r
//!TYPE float
0.0
//!PARAM solid_g
//!TYPE float
0.0
//!PARAM solid_b
//!TYPE float
0.0
//!HOOK PREOUTPUT
//!BIND HOOKED
//!DESC ffcine solid
vec4 hook() {
    return vec4(clamp(vec3(solid_r, solid_g, solid_b), 0.0, 1.0), 1.0);
}
"
}

/// Directional motion blur for whip transitions: `blur` output pixels along `(dir_x, dir_y)`
/// (32 taps, box), a `PREOUTPUT` hook. `blur` is a per-frame track; 0 passes the picture.
pub fn motion_blur() -> &'static str {
    r"//!PARAM blur
//!TYPE float
0.0
//!PARAM dir_x
//!TYPE float
1.0
//!PARAM dir_y
//!TYPE float
0.0
//!HOOK PREOUTPUT
//!BIND HOOKED
//!DESC ffcine motion blur
vec4 hook() {
    if (blur < 0.5)
        return HOOKED_texOff(vec2(0.0));
    vec4 s = vec4(0.0);
    for (int i = 0; i < 32; i++) {
        float u = (float(i) / 31.0 - 0.5) * blur;
        s += HOOKED_texOff(vec2(dir_x, dir_y) * u);
    }
    return s / 32.0;
}
"
}

/// A generated background replacing the picture (split-screen backdrop, `render.py`
/// `layout_bg`): the colour `bg_r/g/b`, darkened towards the edges by
/// `1 − vignette · ((1.5 (x − ½))² + (1.8 (y − ½))²)`.
pub fn background() -> &'static str {
    r"//!PARAM bg_r
//!TYPE float
0.0
//!PARAM bg_g
//!TYPE float
0.0
//!PARAM bg_b
//!TYPE float
0.0
//!PARAM vignette
//!TYPE float
0.0
//!HOOK PREOUTPUT
//!BIND HOOKED
//!DESC ffcine background
vec4 hook() {
    vec2 u = HOOKED_pos - 0.5;
    float k = 1.0 - vignette * (pow(1.5 * u.x, 2.0) + pow(1.8 * u.y, 2.0));
    return vec4(clamp(vec3(bg_r, bg_g, bg_b) * k, 0.0, 1.0), 1.0);
}
"
}

#[cfg(test)]
mod tests {
    use super::LookParams;

    #[test]
    fn look_on_the_cpu() {
        let id = LookParams::default();
        for v in [[0.0, 0.5, 1.0], [0.2, 0.3, 0.9]] {
            let o = id.apply(v);
            assert!((0..3).all(|c| (o[c] - v[c]).abs() < 1e-6), "{o:?}");
        }
        // +1 stop doubles display-linear light.
        let up = LookParams {
            exposure: 1.0,
            ..id
        }
        .apply([0.4; 3]);
        assert!((up[0].powf(2.4) - 2.0 * 0.4f32.powf(2.4)).abs() < 1e-5);
        // Warmth pushes red up and blue down; saturation 0 is grey.
        let warm = LookParams { warmth: 0.1, ..id }.apply([0.5; 3]);
        assert!(warm[0] > 0.5 && warm[2] < 0.5);
        let grey = LookParams {
            saturation: 0.0,
            ..id
        }
        .apply([0.8, 0.2, 0.1]);
        assert!((grey[0] - grey[2]).abs() < 1e-6);
    }

    #[test]
    fn unsharp_hook() {
        assert!(super::unsharp(0.0).is_none());
        let s = super::unsharp(0.6).unwrap();
        assert!(s.starts_with("//!HOOK PREOUTPUT"));
        assert!(s.contains("c.rgb += 0.6000 *"));
    }
}