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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::{
fs::File,
io::{BufWriter, Write},
path::Path,
};
use arrayvec::ArrayVec;
use crate::{GrainTableSegment, ScalingPoints, DEFAULT_GRAIN_SEED, NUM_Y_POINTS};
const PQ_M1: f32 = 2610. / 16384.;
const PQ_M2: f32 = 128. * 2523. / 4096.;
const PQ_C1: f32 = 3424. / 4096.;
const PQ_C2: f32 = 32. * 2413. / 4096.;
const PQ_C3: f32 = 32. * 2392. / 4096.;
const BT1886_WHITEPOINT: f32 = 203.;
const BT1886_BLACKPOINT: f32 = 0.1;
const BT1886_GAMMA: f32 = 2.4;
#[inline(always)]
fn bt1886_inv_whitepoint() -> f32 {
BT1886_WHITEPOINT.powf(1.0 / BT1886_GAMMA)
}
#[inline(always)]
fn bt1886_inv_blackpoint() -> f32 {
BT1886_BLACKPOINT.powf(1.0 / BT1886_GAMMA)
}
#[inline(always)]
fn bt1886_alpha() -> f32 {
(bt1886_inv_whitepoint() - bt1886_inv_blackpoint()).powf(BT1886_GAMMA)
}
#[inline(always)]
fn bt1886_beta() -> f32 {
bt1886_inv_blackpoint() / (bt1886_inv_whitepoint() - bt1886_inv_blackpoint())
}
#[derive(Debug, Clone, Copy)]
pub struct NoiseGenArgs {
pub iso_setting: u32,
pub width: u32,
pub height: u32,
pub transfer_function: TransferFunction,
pub chroma_grain: bool,
pub random_seed: Option<u16>,
}
#[must_use]
pub fn generate_photon_noise_params(
start_time: u64,
end_time: u64,
args: NoiseGenArgs,
) -> GrainTableSegment {
GrainTableSegment {
start_time,
end_time,
scaling_points_y: generate_luma_noise_points(args),
scaling_points_cb: ArrayVec::new(),
scaling_points_cr: ArrayVec::new(),
scaling_shift: 8,
ar_coeff_lag: 0,
ar_coeffs_y: ArrayVec::new(),
ar_coeffs_cb: ArrayVec::try_from([0].as_slice())
.expect("Cannot fail creation from const array"),
ar_coeffs_cr: ArrayVec::try_from([0].as_slice())
.expect("Cannot fail creation from const array"),
ar_coeff_shift: 6,
cb_mult: 0,
cb_luma_mult: 0,
cb_offset: 0,
cr_mult: 0,
cr_luma_mult: 0,
cr_offset: 0,
overlap_flag: true,
chroma_scaling_from_luma: args.chroma_grain,
grain_scale_shift: 0,
random_seed: args.random_seed.unwrap_or(DEFAULT_GRAIN_SEED),
}
}
#[must_use]
#[cfg(feature = "unstable")]
pub fn generate_film_grain_params(
start_time: u64,
end_time: u64,
args: NoiseGenArgs,
) -> GrainTableSegment {
todo!("SCIENCE");
}
pub fn write_grain_table<P: AsRef<Path>>(
filename: P,
params: &[GrainTableSegment],
) -> anyhow::Result<()> {
let mut file = BufWriter::new(File::create(filename)?);
writeln!(&mut file, "filmgrn1")?;
for segment in params {
write_film_grain_segment(segment, &mut file)?;
}
file.flush()?;
Ok(())
}
fn write_film_grain_segment(
params: &GrainTableSegment,
output: &mut BufWriter<File>,
) -> anyhow::Result<()> {
writeln!(
output,
"E {} {} 1 {} 1",
params.start_time, params.end_time, params.random_seed,
)?;
writeln!(
output,
"\tp {} {} {} {} {} {} {} {} {} {} {} {}",
params.ar_coeff_lag,
params.ar_coeff_shift,
params.grain_scale_shift,
params.scaling_shift,
u8::from(params.chroma_scaling_from_luma),
u8::from(params.overlap_flag),
params.cb_mult,
params.cb_luma_mult,
params.cb_offset,
params.cr_mult,
params.cr_luma_mult,
params.cr_offset
)?;
write!(output, "\tsY {} ", params.scaling_points_y.len())?;
for point in ¶ms.scaling_points_y {
write!(output, " {} {}", point[0], point[1])?;
}
writeln!(output)?;
write!(output, "\tsCb {}", params.scaling_points_cb.len())?;
for point in ¶ms.scaling_points_cb {
write!(output, " {} {}", point[0], point[1])?;
}
writeln!(output)?;
write!(output, "\tsCr {}", params.scaling_points_cr.len())?;
for point in ¶ms.scaling_points_cr {
write!(output, " {} {}", point[0], point[1])?;
}
writeln!(output)?;
write!(output, "\tcY")?;
for coeff in ¶ms.ar_coeffs_y {
write!(output, " {}", *coeff)?;
}
writeln!(output)?;
write!(output, "\tcCb")?;
for coeff in ¶ms.ar_coeffs_cb {
write!(output, " {}", *coeff)?;
}
writeln!(output)?;
write!(output, "\tcCr")?;
for coeff in ¶ms.ar_coeffs_cr {
write!(output, " {}", *coeff)?;
}
writeln!(output)?;
Ok(())
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferFunction {
BT1886,
SMPTE2084,
}
impl TransferFunction {
#[must_use]
pub fn to_linear(self, x: f32) -> f32 {
match self {
TransferFunction::BT1886 => {
let luma = bt1886_alpha() * (x + bt1886_beta()).powf(BT1886_GAMMA);
luma / BT1886_WHITEPOINT
}
TransferFunction::SMPTE2084 => {
let pq_pow_inv_m2 = x.powf(1. / PQ_M2);
(0_f32.max(pq_pow_inv_m2 - PQ_C1) / (PQ_C2 - PQ_C3 * pq_pow_inv_m2))
.powf(1. / PQ_M1)
}
}
}
#[allow(clippy::wrong_self_convention)]
#[must_use]
pub fn from_linear(self, x: f32) -> f32 {
match self {
TransferFunction::BT1886 => {
let luma = x * BT1886_WHITEPOINT;
(luma / bt1886_alpha()).powf(1.0 / BT1886_GAMMA) - bt1886_beta()
}
TransferFunction::SMPTE2084 => {
if x < f32::EPSILON {
return 0.0;
}
let linear_pow_m1 = x.powf(PQ_M1);
(PQ_C2.mul_add(linear_pow_m1, PQ_C1) / PQ_C3.mul_add(linear_pow_m1, 1.)).powf(PQ_M2)
}
}
}
#[inline(always)]
#[must_use]
pub fn mid_tone(self) -> f32 {
self.to_linear(0.5)
}
}
fn generate_luma_noise_points(args: NoiseGenArgs) -> ScalingPoints {
const PHOTONS_PER_SQ_MICRON_PER_LUX_SECOND: f32 = 11260.;
const EFFECTIVE_QUANTUM_EFFICIENCY: f32 = 0.2;
const PHOTO_RESPONSE_NON_UNIFORMITY: f32 = 0.005;
const INPUT_REFERRED_READ_NOISE: f32 = 1.5;
const SENSOR_AREA: f32 = 36_000. * 24_000.;
let mid_tone_exposure = 10. / args.iso_setting as f32;
let pixel_area_microns = SENSOR_AREA / (args.width * args.height) as f32;
let mid_tone_electrons_per_pixel = EFFECTIVE_QUANTUM_EFFICIENCY
* PHOTONS_PER_SQ_MICRON_PER_LUX_SECOND
* mid_tone_exposure
* pixel_area_microns;
let max_electrons_per_pixel = mid_tone_electrons_per_pixel / args.transfer_function.mid_tone();
let mut scaling_points = ScalingPoints::default();
for i in 0..NUM_Y_POINTS {
let x = i as f32 / (NUM_Y_POINTS as f32 - 1.);
let linear = args.transfer_function.to_linear(x);
let electrons_per_pixel = max_electrons_per_pixel * linear;
let noise_in_electrons = (PHOTO_RESPONSE_NON_UNIFORMITY
* PHOTO_RESPONSE_NON_UNIFORMITY
* electrons_per_pixel)
.mul_add(
electrons_per_pixel,
INPUT_REFERRED_READ_NOISE.mul_add(INPUT_REFERRED_READ_NOISE, electrons_per_pixel),
)
.sqrt();
let linear_noise = noise_in_electrons / max_electrons_per_pixel;
let linear_range_start = 0_f32.max(linear - 2. * linear_noise);
let linear_range_end = 1_f32.min(2_f32.mul_add(linear_noise, linear));
let tf_slope = (args.transfer_function.from_linear(linear_range_end)
- args.transfer_function.from_linear(linear_range_start))
/ (linear_range_end - linear_range_start);
let encoded_noise = linear_noise * tf_slope;
let x = (255. * x).round() as u8;
let encoded_noise = 255_f32.min((255. * 7.88 * encoded_noise).round()) as u8;
scaling_points.push([x, encoded_noise]);
}
scaling_points
}
#[cfg(test)]
mod tests {
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
use super::*;
#[quickcheck]
fn bt1886_to_linear_within_range(x: f32) -> TestResult {
if !(0.0..=1.0).contains(&x) || x.is_nan() {
return TestResult::discard();
}
let tx = TransferFunction::BT1886;
let res = tx.to_linear(x);
TestResult::from_bool((0.0..=1.0).contains(&res))
}
#[quickcheck]
fn bt1886_to_linear_reverts_correctly(x: f32) -> TestResult {
if !(0.0..=1.0).contains(&x) || x.is_nan() {
return TestResult::discard();
}
let tx = TransferFunction::BT1886;
let res = tx.to_linear(x);
let res = tx.from_linear(res);
TestResult::from_bool((x - res).abs() < f32::EPSILON)
}
#[quickcheck]
fn smpte2084_to_linear_within_range(x: f32) -> TestResult {
if !(0.0..=1.0).contains(&x) || x.is_nan() {
return TestResult::discard();
}
let tx = TransferFunction::SMPTE2084;
let res = tx.to_linear(x);
TestResult::from_bool((0.0..=1.0).contains(&res))
}
#[quickcheck]
fn smpte2084_to_linear_reverts_correctly(x: f32) -> TestResult {
if !(0.0..=1.0).contains(&x) || x.is_nan() {
return TestResult::discard();
}
let tx = TransferFunction::SMPTE2084;
let res = tx.to_linear(x);
let res = tx.from_linear(res);
TestResult::from_bool((x - res).abs() < f32::EPSILON)
}
}