-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostprocessor.py
More file actions
1806 lines (1541 loc) · 94 KB
/
postprocessor.py
File metadata and controls
1806 lines (1541 loc) · 94 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Smoke and mirrors. Glitch artistry. Pixel-space postprocessing effects.
These effects work in linear intensity space, before gamma correction.
This module was originally released under AGPL in SillyTavern-Extras.
This module is relicensed by its author (Juha Jeronen) under the
2-clause BSD license.
"""
__all__ = ["Postprocessor",
"isotropic_noise",
"vhs_noise", "vhs_noise_pool"]
from collections import defaultdict
from functools import wraps
import inspect
import logging
import math
import time
from typing import Dict, List, Optional, Tuple, TypeVar, Union
from unpythonic import getfunc, memoize
import numpy as np
import torch
import torchvision
from .colorspace import rgb_to_yuv, yuv_to_rgb, luminance
from .upscaler import Upscaler
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
T = TypeVar("T")
Atom = Union[str, bool, int, float]
MaybeContained = Union[T, List[T], Dict[str, T]]
VHS_GLITCH_BLANK = object() # nonce value, see `analog_vhsglitches`
# ---------------------------------------------------------------------------
# Video noise generator — public API
# ---------------------------------------------------------------------------
def isotropic_noise(width: int, height: int, *,
device: torch.device,
dtype: torch.dtype = torch.float32,
sigma: float = 1.0,
double_size: bool = False) -> torch.Tensor:
"""Generate isotropic Gaussian-blurred uniform noise.
Returns a ``[height, width]`` tensor in [0, 1]. When ``sigma > 0``,
the raw uniform noise is blurred with the same kernel size in both
dimensions — contrast with `vhs_noise`, which uses horizontal-only
blur to mimic helical-scan artifacts.
`sigma`: Standard deviation for Gaussian blur (0 = no blur).
Works up to 3.0.
`double_size`: Generate at half resolution and upscale 2×.
`width` and `height` specify the final output size.
"""
gen_w, gen_h = ((width + 1) // 2, (height + 1) // 2) if double_size else (width, height)
noise = torch.rand(gen_h, gen_w, device=device, dtype=dtype)
if sigma > 0.0:
noise = noise.unsqueeze(0) # [h, w] -> [1, h, w]
ks = _blur_kernel_size(sigma)
noise = torchvision.transforms.GaussianBlur((ks, 1), sigma=sigma)(noise)
noise = torchvision.transforms.GaussianBlur((1, ks), sigma=sigma)(noise)
noise = noise.squeeze(0) # -> [h, w]
if double_size:
noise = noise.repeat_interleave(2, dim=-1).repeat_interleave(2, dim=-2)[:height, :width]
return noise
def vhs_noise(width: int, height: int, *,
device: torch.device,
dtype: torch.dtype = torch.float32,
mode: str = "PAL",
ntsc_chroma: str = "nearest",
double_size: bool = False) -> torch.Tensor:
"""Generate VHS noise.
`mode`:
``"PAL"``: Monochrome luma noise. Returns ``[1, H, W]`` in [0, 1].
PAL's phase-alternation cancels chroma errors, so tape
noise is overwhelmingly in luminance.
``"NTSC"``: Three independent noise planes (Y, U, V). Returns
``[3, H, W]``, where the `Y` channel is in [0, 1],
and the `U` and `V` channels are in [-0.5, 0.5].
NTSC has no phase-alternation error correction,
so tape speed variation and magnetic noise cause
chroma speckling and hue wander — the
"Never The Same Color" phenomenon.
`ntsc_chroma` (NTSC only): how the 4:2:0 chroma planes are
upscaled to luma resolution.
``"nearest"``: Nearest-neighbour — blocky 2×2 chroma texels.
Fast, compresses well. Retro digital look.
``"bilinear"``: Bilinear interpolation — smooth chroma transitions.
Slower, larger encoded frames. More analog look.
`double_size`: Generate noise at half resolution and upscale 2×.
Produces chunkier grain. `width` and `height` specify
the final output size either way.
Both modes use horizontal-only Gaussian blur to mimic helical-scan
artifacts. NTSC U/V planes get a wider kernel (lower chroma bandwidth
on VHS — ~500 kHz vs. ~3 MHz for luma) and are 4:2:0 subsampled
(generated at half luma resolution). This matches human vision's low
chroma spatial acuity and dramatically improves compressibility of
the rendered image.
This is the single source of truth for the VHS noise recipe used
across Raven — the postprocessor's glitch/tracking filters and the
cherrypick placeholder tiles all derive from this.
"""
gen_w, gen_h = ((width + 1) // 2, (height + 1) // 2) if double_size else (width, height)
# Y channel (luma): fine-grained horizontal runs.
noise_y = torch.rand(gen_h, gen_w, device=device, dtype=dtype).unsqueeze(0) # [1, H, W]
noise_y = torchvision.transforms.GaussianBlur((5, 1), sigma=2.0)(noise_y)
if mode == "PAL":
result = noise_y
elif mode == "NTSC":
# NTSC: independent U/V planes with coarser horizontal blur
# (lower chroma bandwidth → wider, smoother runs).
#
# 4:2:0 chroma subsampling: generate U/V at half luma resolution and
# upscale. Human vision has low chroma spatial acuity, and the coarser
# chroma blocks dramatically improve delta-based compression (QOI) —
# random per-pixel chroma is the main entropy source.
chroma_h = (gen_h + 1) // 2
chroma_w = (gen_w + 1) // 2
noise_u = torch.rand(chroma_h, chroma_w, device=device, dtype=dtype).unsqueeze(0)
noise_u = torchvision.transforms.GaussianBlur((11, 1), sigma=4.0)(noise_u)
noise_u -= 0.5
noise_v = torch.rand(chroma_h, chroma_w, device=device, dtype=dtype).unsqueeze(0)
noise_v = torchvision.transforms.GaussianBlur((11, 1), sigma=4.0)(noise_v)
noise_v -= 0.5
# Upscale chroma to luma resolution.
chroma = torch.cat([noise_u, noise_v], dim=0) # [2, chroma_h, chroma_w]
if ntsc_chroma == "bilinear": # analog aesthetic
chroma = torch.nn.functional.interpolate(
chroma.unsqueeze(0), size=(gen_h, gen_w), mode="bilinear", align_corners=False,
).squeeze(0)
else: # ntsc_chroma == "nearest": # retro digital aesthetic
chroma = (chroma.repeat_interleave(2, dim=-1)
.repeat_interleave(2, dim=-2)[:, :gen_h, :gen_w])
noise_u = chroma[0:1] # [1, H, W]
noise_v = chroma[1:2] # [1, H, W]
result = torch.cat([noise_y, noise_u, noise_v], dim=0) # [3, H, W]
else:
raise ValueError(f"vhs_noise: unknown mode {mode!r}; expected 'PAL' or 'NTSC'")
if double_size:
result = result.repeat_interleave(2, dim=-1).repeat_interleave(2, dim=-2)[..., :height, :width]
return result
def vhs_noise_pool(n: int, width: int, height: int, *,
device: torch.device,
dtype: torch.dtype = torch.float32,
tint: Tuple[float, float, float] = (0.92, 0.92, 1.0),
brightness: Tuple[float, float] = (0.04, 0.40),
mode: str = "PAL") -> List[torch.Tensor]:
"""Generate *n* unique tinted VHS noise tiles.
Each tile is a ``[4, height, width]`` RGBA tensor in [0, 1], suitable
for conversion to DPG texture format at the call site.
`tint`: per-channel multiplier (R, G, B) applied to the brightness.
Default is a subtle cool blue-gray.
`brightness`: (lo, hi) range that the raw [0, 1] noise is mapped to.
Lower values keep the placeholders dark and subdued.
`mode`: ``"PAL"`` (monochrome luma × tint) or ``"NTSC"`` (per-tile
color variation via independent chroma noise). See `vhs_noise`.
"""
lo, hi = brightness
tr, tg, tb = tint
tiles: List[torch.Tensor] = []
for _ in range(n):
noise = vhs_noise(width, height, device=device, dtype=dtype, mode=mode)
if mode == "PAL":
luma = lo + (hi - lo) * noise.squeeze(0) # [H, W]
r = luma * tr
g = luma * tg
b = luma * tb
else:
# NTSC: Y plane → brightness-mapped luma, U/V → small chroma offsets.
# Each tile gets its own random color cast — some warm, some cool.
noise_y = noise[0] # [H, W]
noise_u = noise[1] # [H, W], pre-scaled by 0.5
noise_v = noise[2]
y = lo + (hi - lo) * noise_y # [H, W]
u = 0.5 * noise_u # centered, -0.25 ... +0.25
v = 0.5 * noise_v
yuv = torch.stack([y, u, v], dim=0) # [3, H, W]
rgb = yuv_to_rgb(yuv, clamp=True) # [3, H, W]
r = rgb[0] * tr
g = rgb[1] * tg
b = rgb[2] * tb
a = torch.ones(height, width, device=device, dtype=dtype)
rgba = torch.stack([r, g, b, a], dim=0).clamp(0.0, 1.0) # [4, H, W]
tiles.append(rgba)
return tiles
# --------------------------------------------------------------------------------
# Advanced tonemapper HDR -> LDR.
# Currently not used; slow, and the input data would need some preprocessing to eliminate blank pixels.
class HistogramEqualizer:
def __init__(self,
device: torch.device,
dtype: torch.dtype,
nbins: int = 256):
self.device = device
self.dtype = dtype
self.nbins = nbins
self.display_range = np.log(255.0 / 1.0) # max/min brightness, ~45 dB for 8 bits per channel
def _compute_cdf(self,
x: torch.Tensor,
bin_edges: torch.Tensor,
discard_first_bin: bool = False,
discard_last_bin: bool = False):
"""Compute the cumulative distribution function of the data, given `bin_edges`.
The result with have `n_bins + 2` entries, where `n_bins = len(bin_edges) - 1`;
the first and last elements correspond to data less than the first bin edge,
and data greater than the last bin edge, respectively.
`discard_first_bin`: Optionally, drop the first bin. Useful for ignoring data less than the first bin edge.
`discard_last_bin`: Optionally, drop the last bin. Useful for ignoring data greater than the last bin edge.
Needed because `torch.histogram` doesn't support the CUDA backend:
https://github.com/pytorch/pytorch/issues/69519
"""
level = torch.bucketize(x, bin_edges, out_int32=True) # sum(x > v for v in bin_edges)
count = x.new_zeros(len(bin_edges) + 1, dtype=torch.float32)
count.index_put_((level,), count.new_ones(1), True)
if discard_first_bin:
count = count[1:]
if discard_last_bin:
count = count[:-1]
norm = x.numel()
cdf = torch.cumsum(count, dim=0) / norm
pdf = count / norm
return pdf, cdf
def _loghist(self, x: torch.Tensor, min_nonzero_x, max_x):
# Histogramming linearly in logarithmic space gives us a logarithmically spaced histogram.
log_bin_edges = torch.linspace(torch.log(min_nonzero_x), torch.log(max_x), self.nbins, dtype=self.dtype, device=self.device)
bin_edges = torch.exp(log_bin_edges)
# - Ignore the less-than-first-bin-edge slot to ignore black pixels (empty background). (TODO: should do this for *blank*, not *black*, but that would need some kind of mask support.)
# - Ignore the greater-than-last-bin-edge slot, since we always put the last bin edge at max(x).
pdf, cdf = self._compute_cdf(x, bin_edges, discard_first_bin=True, discard_last_bin=True)
zero = torch.tensor([0.0], device=self.device, dtype=self.dtype)
pdf = torch.cat([zero, pdf], dim=0) # pretend there's no data below the first bin edge
cdf = torch.cat([zero, cdf], dim=0) # a CDF should start from zero
return bin_edges, pdf, cdf
def loghist(self, x: torch.Tensor):
"""Logarithmically spaced histogram.
- `x` must be in a floating-point type
- negative values are ignored (these get mapped to the first bin)
"""
max_x = torch.max(x)
# min_nonzero_x = torch.min(torch.where(x > 0.0, x, max_x)) # for general data
min_nonzero_x = torch.tensor(0.01, device=self.device, dtype=self.dtype) # for images
return self._loghist(x, min_nonzero_x, max_x)
def larson(self, x: torch.Tensor):
"""Data-adaptive histogram corrector of Ward-Larson, Rushmeier and Piatko (1997).
Compresses dynamic range, but does not exaggerate contrast of sparsely populated parts of the histogram.
This is done by limiting superlinear growth of the display intensity. We modify the measured histogram,
and then histogram-equalize with the result.
This typically looks better than simple log scaling, and brings out details in the data.
"""
max_x = torch.max(x)
# min_nonzero_x = torch.min(torch.where(x > 0.0, x, max_x)) # for general data
min_nonzero_x = torch.tensor(0.01, device=self.device, dtype=self.dtype) # for images
bin_edges, pdf, cdf = self._loghist(x, min_nonzero_x, max_x)
data_range = torch.log(max_x / min_nonzero_x).cpu()
if data_range <= self.display_range: # data is LDR, nothing to do
return bin_edges, pdf, cdf
orig_pdf = pdf
orig_cdf = cdf
# Tolerance of the iteration (an arbitrary small number).
#
# Smaller tolerances produce better conformance to the ceiling (i.e. better prevent superlinear growth).
# The original article used the value 0.025.
#
tol = 0.005 * torch.max(pdf)
delta_b = data_range / self.nbins # width of one logarithmic bin
trimmings = torch.tensor(np.inf, device=self.device, dtype=self.dtype) # loop at least once
while trimmings > tol:
pdf_sum = torch.sum(pdf)
if pdf_sum < tol: # degenerate case: histogram has been zeroed out -> return original image
logger.warning("HistogramEqualizer.tonemap: histogram zeroed out, returning original histogram")
return bin_edges, orig_pdf, orig_cdf
# Cut any peaks from PDF that are above the current ceiling; calculate how much we cut in total (to decide whether to iterate again).
ceiling = pdf_sum * (delta_b / self.display_range)
excess = pdf - ceiling
trimmings = torch.sum(torch.where(excess > 0.0, excess, 0.0))
pdf = torch.where(pdf > ceiling, ceiling, pdf)
cdf = torch.cumsum(pdf, dim=0)
cdfmax = torch.max(cdf)
pdf /= cdfmax # normalize sum of adjusted histogram to 1
cdf /= cdfmax # normalize maximum of adjusted cumulative distribution function to 1
return bin_edges, pdf, cdf
def equalize_by_cdf(self,
x: torch.Tensor,
bin_edges: torch.Tensor,
cdf: torch.Tensor):
"""Histogram-equalize data by a given CDF.
See `loghist` and `larson` to get the inputs for this.
"""
discretized = torch.bucketize(x, bin_edges)
return cdf[discretized.view(-1)].reshape(discretized.shape)
# --------------------------------------------------------------------------------
def _blur_kernel_size(sigma: float) -> int:
"""Gaussian blur kernel size for a given sigma.
Uses the 2-sigma rule: the kernel reaches the point where the Gaussian
has decayed to ~1.8% of its peak. This captures ~95% of the mass.
Sufficient for a visual effects postprocessor; 3-sigma (99.7%) is overkill.
Result is always odd (required by torchvision GaussianBlur).
Minimum kernel size is 3 (torchvision requirement).
"""
return min(21, max(3, 2 * math.ceil(2 * sigma) + 1))
# Convenient for GUI auto-population so that we can specify sensible ranges for parameters once, at the implementation site (not separately at each GUI use site).
def with_metadata(**metadata):
def decorator(func):
@wraps(func)
def func_with_metadata(*args, **kwargs):
return func(*args, **kwargs)
func_with_metadata.metadata = metadata # stash it on the function object
return func_with_metadata
return decorator
class Postprocessor:
"""
`chain`: Postprocessor filter chain configuration.
For an example, see `postprocessor_defaults` in `config.py`.
Don't mind the complicated type signature; the format is just::
[(filter_name0, {param0: value0, ...}),
...]
The filter name must be a method of `Postprocessor`, taking in an image, and any number of named parameters.
To use a filter's default parameter values, supply an empty dictionary for the parameters.
The outer `Optional[List[Tuple[...]]]` just formalizes that `chain` may be omitted (to use the built-in
default chain, for testing), and the top-level format that it's an ordered list of filters. The filters
are applied in order, first to last.
The auxiliary type definitions are::
MaybeContained = Union[T, List[T], Dict[str: T]]
Atom = Union[str, bool, int, float]
The leaf value (atom) types are restricted so that filter chain configurations JSON easily.
The leaf values may actually be contained inside arbitrarily nested lists and dicts (with str keys),
which is currently not captured by the type signature (the definition should be recursive).
The chain is stored as `self.chain`. Any modifications to that attribute modify the chain,
taking effect immediately. It is recommended to update the chain atomically, by::
my_postprocessor.chain = my_new_chain
In filter descriptions:
[static] := depends only on input image, no explicit time dependence.
[dynamic] := beside input image, also depends on time. In other words,
produces animation even for a stationary input image.
"""
def __init__(self,
device: torch.device,
dtype: torch.dtype,
chain: List[Tuple[str, Dict[str, MaybeContained[Atom]]]] = None):
# We intentionally keep very little state in this class, for a more FP/REST approach with less bugs.
# Filters for static effects are stateless.
#
# We deviate from FP in that:
# - The filters MUTATE, i.e. they overwrite the image being processed.
# This is to allow optimizing their implementations for memory usage and speed.
# - The filter for a dynamic effect may store state, if needed for performing FPS correction.
self.device = device
self.dtype = dtype
self.chain = chain
self.histeq = HistogramEqualizer(self.device, self.dtype)
# Meshgrid cache for geometric position of each pixel
self._yy = None
self._xx = None
self._meshy = None
self._meshx = None
self._meshgrid_prev_h = None
self._meshgrid_prev_w = None
# FPS correction
self.CALIBRATION_FPS = 25 # design FPS for dynamic effects (for automatic FPS correction)
self.stream_start_timestamp = time.monotonic_ns() # for updating frame counter reliably (no accumulation)
self.frame_no = -1 # float, frame counter for *normalized* frame number *at CALIBRATION_FPS*
self.last_frame_no = -1
# Caches for individual dynamic effects
self.zoom_data = defaultdict(lambda: None)
self.ca_grid_cache = defaultdict(lambda: None) # name -> {"scale": float, "grid_R": Tensor, "grid_B": Tensor}
self.noise_last_image = defaultdict(lambda: None)
self.noise_last_strength = defaultdict(lambda: -1.0)
self.vhs_glitch_interval = defaultdict(lambda: 0.0)
self.vhs_glitch_last_frame_no = defaultdict(lambda: 0.0)
self.vhs_glitch_last_image = defaultdict(lambda: None)
self.vhs_glitch_last_mask = defaultdict(lambda: None)
self.vhs_headswitching_noise = defaultdict(lambda: None)
self.vhs_tracking_noise = defaultdict(lambda: None)
self.digital_glitches_interval = defaultdict(lambda: 0.0)
self.digital_glitches_last_frame_no = defaultdict(lambda: 0.0)
self.digital_glitches_grid = defaultdict(lambda: None)
def _setup_meshgrid(self, h: int, w: int) -> None:
"""Compute base meshgrid for the geometric position of each pixel.
Needed by filters that vary by position (e.g. `vignetting`) or deform
the image (e.g. `analog_rippling_hsync`). Called automatically by
`render_into` when the image size changes; can also be called directly
to prepare a `Postprocessor` for invoking individual filters outside
the render loop (e.g. in tests).
We cache the meshgrid, because this postprocessor is typically applied
to a video stream. As long as the image dimensions stay constant, we can
re-use the same meshgrid.
"""
logger.info(f"_setup_meshgrid: Computing pixel position tensors for image size {w}x{h}")
with torch.inference_mode():
self._yy = torch.linspace(-1.0, 1.0, h, dtype=self.dtype, device=self.device)
self._xx = torch.linspace(-1.0, 1.0, w, dtype=self.dtype, device=self.device)
self._meshy, self._meshx = torch.meshgrid((self._yy, self._xx), indexing="ij")
self._meshgrid_prev_h = h
self._meshgrid_prev_w = w
logger.info("_setup_meshgrid: Pixel position tensors cached")
@classmethod
@memoize
def get_filters(cls):
"""Return a list of available postprocessing filters and their default configurations.
This is convenient for dynamically populating a GUI.
Return format is `[(name0: settings0), ...]`
"""
filters = []
for name in dir(cls):
if name.startswith("_"):
continue
if name in ("get_filters", "render_into"):
continue
# The authoritative source for parameter defaults is the source code, so:
meth = getattr(cls, name) # get the method from the class
if not callable(meth): # some other kind of attribute? (e.g. cache data for filters)
continue
# is a method
func, _ = getfunc(meth) # get the underlying raw function, for use with `inspect.signature`
sig = inspect.signature(func)
# All of our filter settings have a default value.
settings = {v.name: v.default for v in sig.parameters.values() if v.default is not inspect.Parameter.empty}
# Parameter ranges are specified at definition site via our internal `with_metadata` mechanism.
ranges = {name: meth.metadata[name] for name in settings}
param_info = {"defaults": settings,
"ranges": ranges}
filters.append((name, param_info))
def rendering_priority(metadata_record):
name, _ = metadata_record
meth = getattr(cls, name)
return meth.metadata["_priority"]
return list(sorted(filters, key=rendering_priority))
def render_into(self, image):
"""Apply current postprocess chain, modifying `image` in-place."""
time_render_start = time.monotonic_ns()
chain = self.chain # read just once; other threads might reassign it while we're rendering
if not chain:
return
c, h, w = image.shape
if h != self._meshgrid_prev_h or w != self._meshgrid_prev_w:
self._setup_meshgrid(h, w)
# Update the frame counter.
#
# We consider the frame number to be a float, so that dynamic filters can decide what
# to do at fractional frame positions. For continuously animated effects (e.g. banding)
# it makes sense to interpolate continuously, whereas other effects (e.g. scanlines)
# can make their decisions based on the integer part.
#
# As always with floats, we must be careful. Note that we operate in a mindset of robust
# engineering. Since doing the Right Thing here does not cost significantly more engineering
# effort than doing the intuitive but Wrong Thing, it is preferable to go for the proper solution,
# regardless of whether it would take a centuries-long session to actually trigger a failure
# in the less robust approach.
#
# So, floating point accuracy considerations? First, we note that accumulation invites
# disaster in two ways:
#
# - Accumulating the result accumulates also representation error and roundoff error.
# - When accumulating small positive numbers to a sum total, the update eventually
# becomes too small to add, causing the counter to get stuck. (For floats, `x + ϵ = x`
# for sufficiently small ϵ dependent on the magnitude of `x`.)
#
# Fortunately, frame number is a linear function of time, and time diffs can be measured
# precisely. Thus, we can freshly compute the current frame number at each frame, completely
# bypassing the need for accumulation:
#
seconds_since_stream_start = (time_render_start - self.stream_start_timestamp) / 10**9
self.last_frame_no = self.frame_no
self.frame_no = self.CALIBRATION_FPS * seconds_since_stream_start # float!
# That leaves just the questions of how accurate the calculation is, and for how long.
# As to the first question:
#
# - Timestamps are an integer number of nanoseconds, so they are exact.
# - Dividing by 10**9, we move the decimal point. But floats are base-2, so 0.1
# is not representable in IEEE-754. So there will be some small representation error,
# which for float64 likely appears in the ~15th significant digit.
# - Basic arithmetic, such as multiplication, is guaranteed by IEEE-754
# to be accurate to the ULP.
#
# Thus, as the result, we obtain the closest number that is representable in IEEE-754,
# and the strategy works for the whole range of float64.
#
# As for the second question, floats are logarithmically spaced. So if this is left running
# "for long enough" during the same session, accuracy will eventually suffer. Instead of the
# counter getting stuck, however, this will manifest as the frame number updating by more
# than `1.0` each time it updates (i.e. whenever the elapsed number of frames reaches the
# next representable float).
#
# This could be fixed by resetting `stream_start_timestamp` once the frame number
# becomes too large. But in practice, how long does it take for this issue to occur?
# The ULP becomes 1.0 at ~5e15. To reach frame number 5e15, at the reference 25 FPS,
# the time required is 2e14 seconds, i.e. 2.31e9 days, or 6.34 million years.
# While I can almost imagine the eventual bug report, I think it's safe to ignore this.
# Apply the current filter chain.
with torch.inference_mode():
for filter_name, settings in chain:
apply_filter = getattr(self, filter_name)
apply_filter(image, **settings)
# --------------------------------------------------------------------------------
# Physical input signal
@with_metadata(center_x=[-1.0, 1.0],
center_y=[-1.0, 1.0],
factor=[1.0, 4.0],
quality=["low", "high", "ultra"],
name=["!ignore"],
_priority=-1.0)
def zoom(self, image: torch.tensor, *,
center_x: float = 0.0,
center_y: float = -0.867,
factor: float = 2.0,
quality: str = "low",
name: str = "zoom0"):
"""[dynamic] Simulated optical zoom for anime video.
The default settings zoom to the head/shoulders of most characters.
`center_x`: Center position of zoom on x axis, where image is [-1, 1].
`center_y`: Center position of zoom on y axis, where image is [-1, 1],
negative upward.
`factor`: Zoom by this much. Values larger than 1.0 zoom in.
At exactly 1.0, the zoom filter is disabled.
`quality`: One of:
"low": geometric distortion with bilinear interpolation (fast)
"high": crop, then low-quality Anime4K upscale (fast-ish)
"ultra": crop, then high-quality Anime4K upscale
How much quality you need depends on what other filters are enabled,
how large `factor` is, and how large the final size of the avatar is.
If you upscale the avatar by 2x, without much other postprocessing
than this zoom filter, and zoom in by `factor=4.0`, then the "ultra"
quality may be necessary, and might still not look good. But if you
use lots of other filters, and limit to at most `factor=2.0`, then
even "low" might look acceptable.
Dynamic only to save compute; we cache the distortion mesh and upscaler.
"""
if factor == 1.0:
return
# Recompute mesh when the filter settings change, or the video stream size changes.
do_setup = True
c, h, w = image.shape
cached_grid = self.zoom_data[name]["grid"] if name in self.zoom_data else None
size_changed = cached_grid is None or cached_grid.shape[-3] != h or cached_grid.shape[-2] != w
if not size_changed and name in self.zoom_data:
if (factor == self.zoom_data[name]["factor"] and
center_x == self.zoom_data[name]["center_x"] and
center_y == self.zoom_data[name]["center_y"] and
quality == self.zoom_data[name]["quality"]):
do_setup = False
grid = self.zoom_data[name]["grid"]
upscaler = self.zoom_data[name]["upscaler"]
if do_setup:
meshx = center_x + (self._meshx - center_x) / factor # x coordinate, [h, w]
meshy = center_y + (self._meshy - center_y) / factor # y coordinate, [h, w]
grid = torch.stack((meshx, meshy), 2) # [h, w, x/y]
grid = grid.unsqueeze(0) # batch of one
if quality in ("high", "ultra"): # need an Anime4K?
old_upscaler = self.zoom_data[name]["upscaler"] if name in self.zoom_data else None
old_quality = self.zoom_data[name]["quality"] if name in self.zoom_data else None
if size_changed or old_upscaler is None or quality != old_quality:
upscaler_quality = "high" if quality == "ultra" else "low"
upscaler = Upscaler(device=self.device, dtype=self.dtype,
upscaled_width=w, upscaled_height=h,
preset="C", quality=upscaler_quality)
else:
upscaler = old_upscaler # only factor/center changed - save some compute by recycling the existing upscaler.
else:
upscaler = None
self.zoom_data[name] = {"center_x": center_x,
"center_y": center_y,
"factor": factor,
"quality": quality,
"grid": grid,
"upscaler": upscaler}
if quality == "low": # geometric distortion
image_batch = image.unsqueeze(0) # batch of one -> [1, c, h, w]
warped = torch.nn.functional.grid_sample(image_batch, grid, mode="bilinear", padding_mode="border", align_corners=False)
warped = warped.squeeze(0) # [1, c, h, w] -> [c, h, w]
image[:, :, :] = warped
else: # "high" or "ultra" - crop, then Anime4K upscale
g = grid.squeeze(0)
top_left_xy = g[0, 0]
bottom_right_xy = g[-1, -1]
x0 = int((top_left_xy[0] + 1.0) / 2 * w)
y0 = int((top_left_xy[1] + 1.0) / 2 * h)
x1 = int((bottom_right_xy[0] + 1.0) / 2 * w)
y1 = int((bottom_right_xy[1] + 1.0) / 2 * h)
# print(x0, y0, x1, y1) # DEBUG
cropped = torchvision.transforms.functional.crop(image, top=y0, left=x0, height=(y1 - y0), width=(x1 - x0))
image[:, :, :] = upscaler.upscale(cropped)
# Defaults chosen so that they look good for a handful of characters rendered in SD Forge, with the Wai 14.0 Illustrious-SDXL checkpoint.
@with_metadata(threshold=[0.0, 1.0],
exposure=[0.1, 5.0],
sigma=[0.1, 7.0],
_priority=0.0)
def bloom(self, image: torch.tensor, *,
threshold: float = 0.560,
exposure: float = 0.842,
sigma: float = 7.0) -> None:
"""[static] Bloom effect (fake HDR). Makes the image look brighter. Popular in early 2000s anime.
Can also be used as just a camera exposure adjustment by setting `threshold=1.0` to disable the glow.
Makes bright parts of the image bleed light into their surroundings, enhancing perceived contrast.
Only makes sense when the avatar is rendered on a dark-ish background.
`threshold`: How bright is bright. 0.0 is full black (all pixels glow), 1.0 is full white (bloom disabled).
Technically, this is true relative luminance, not luma, since we work in linear RGB space.
`exposure`: Overall brightness of the output. Like in photography, higher exposure means brighter image,
saturating toward white.
`sigma`: Standard deviation for the bloom blur. Larger values produce a wider glow. Works up to 7.0.
Recommended values:
7.0 - wide, dreamy early-2000s anime bloom.
1.6 - tighter, more modern glow.
"""
# There are online tutorials for how to create this effect, see e.g.:
# https://learnopengl.com/Advanced-Lighting/Bloom
if threshold < 1.0:
# Find the bright parts.
# original_yuv = rgb_to_yuv(image[:3, :, :])
# Y = original_yuv[0, :, :]
Y = luminance(image[:3, :, :])
mask = torch.ge(Y, threshold) # [h, w]
# Make a copy of the image with just the bright parts.
mask = torch.unsqueeze(mask, 0) # -> [1, h, w]
brights = image * mask # [c, h, w]
# Blur the bright parts. Two-pass blur to save compute, since we need a very large blur kernel.
# It seems that in Torch, one large 1D blur is faster than looping with a smaller one.
#
# Although everything else in Torch takes (height, width), kernel size is given as (size_x, size_y);
# see `gaussian_blur_image` in https://pytorch.org/vision/main/_modules/torchvision/transforms/v2/functional/_misc.html
# for a hint (the part where it computes the padding).
ks = _blur_kernel_size(sigma)
brights = torchvision.transforms.GaussianBlur((ks, 1), sigma=sigma)(brights) # blur along x
brights = torchvision.transforms.GaussianBlur((1, ks), sigma=sigma)(brights) # blur along y
# Additively blend the images. Note we are working in linear intensity space, and we will now go over 1.0 intensity.
image.add_(brights)
# We now have a fake HDR image. Tonemap it back to LDR.
image[:3, :, :] = 1.0 - torch.exp(-image[:3, :, :] * exposure) # RGB: tonemap
# # TEST - apply Larson's adaptive histogram remapper to the Y (luminance) channel, and keep the color channels (U, V) as-is.
# # Doesn't look that good, the classical method seems better for this use.
# hdr_Y = luminance(image[:3, :, :]) # Eris have mercy on us, this function isn't designed for HDR data. Seems to work fine, though.
# bin_edges, pdf, cdf = self.histeq.loghist(hdr_Y) # or use `self.histeq.loghist` for classical histogram equalization
# ldr_Y = self.histeq.equalize_by_cdf(hdr_Y, bin_edges, cdf)
# image[:3, :, :] = yuv_to_rgb(torch.cat([ldr_Y.unsqueeze(0), original_yuv[1:]], dim=0))
if threshold < 1.0:
image[3, :, :] = torch.maximum(image[3, :, :], brights[3, :, :]) # alpha: max-combine
torch.clamp_(image, min=0.0, max=1.0)
# --------------------------------------------------------------------------------
# Video camera
@with_metadata(scale=[0.001, 0.05],
sigma=[0.1, 3.0],
name=["!ignore"],
_priority=1.0)
def chromatic_aberration(self, image: torch.tensor, *,
scale: float = 0.005,
sigma: float = 1.0,
name: str = "chromatic_aberration0") -> None:
"""[static] Simulate the two types of chromatic aberration in a camera lens.
Like everything else here, this is of course made of smoke and mirrors. We simulate the axial effect
(index of refraction varying w.r.t. wavelength) by geometrically scaling the RGB channels individually,
and the transverse effect (focal distance varying w.r.t. wavelength) by a gaussian blur.
`scale`: Axial CA geometric distortion parameter.
`sigma`: Transverse CA blur parameter. Works up to 3.0.
Note that in a real lens:
- Axial CA is typical at long focal lengths (e.g. tele/zoom lens)
- Axial CA increases at high F-stops (low depth of field, i.e. sharp focus at all distances)
- Transverse CA is typical at short focal lengths (e.g. macro lens)
However, in an RGB postproc effect, it is useful to apply both together, to help hide the clear-cut red/blue bands
resulting from the different geometric scalings of just three wavelengths (instead of a continuous spectrum, like
a scene lit with natural light would have).
See:
https://en.wikipedia.org/wiki/Chromatic_aberration
"""
# Axial CA: Shrink R (deflected less), pass G through (lens reference wavelength), enlarge B (deflected more).
# Cache grids — they depend only on `scale` + meshgrid (meshgrid invalidates on resolution change).
c, h, w = image.shape
cached = self.ca_grid_cache[name]
size_changed = cached is not None and (cached["grid_R"].shape[-3] != h or cached["grid_R"].shape[-2] != w)
if cached is None or size_changed or scale != cached["scale"]:
grid_R = torch.stack((self._meshx * (1.0 + scale), self._meshy * (1.0 + scale)), 2).unsqueeze(0)
grid_B = torch.stack((self._meshx * (1.0 - scale), self._meshy * (1.0 - scale)), 2).unsqueeze(0)
self.ca_grid_cache[name] = {"scale": scale, "grid_R": grid_R, "grid_B": grid_B}
else:
grid_R, grid_B = cached["grid_R"], cached["grid_B"]
# Batch R+A and B+A to halve grid_sample and GaussianBlur calls.
# R and A-via-grid_R use the same warp grid; same for B and A-via-grid_B.
# GaussianBlur processes all channels, so blurring a 2-channel tensor does both at once.
ks = _blur_kernel_size(sigma)
blur_x = torchvision.transforms.GaussianBlur((ks, 1), sigma=sigma)
blur_y = torchvision.transforms.GaussianBlur((1, ks), sigma=sigma)
# Warp R and A together with grid_R (advanced indexing → copy, safe before writes)
warped_RA = torch.nn.functional.grid_sample(image[[0, 3], :, :].unsqueeze(0),
grid_R, mode="bilinear", padding_mode="border", align_corners=False)
warped_RA = blur_y(blur_x(warped_RA)) # transverse CA
# Warp B and A together with grid_B
warped_BA = torch.nn.functional.grid_sample(image[[2, 3], :, :].unsqueeze(0),
grid_B, mode="bilinear", padding_mode="border", align_corners=False)
warped_BA = blur_y(blur_x(warped_BA)) # transverse CA
image[0, :, :] = warped_RA[0, 0, :, :]
# image[1, :, :] passed through as-is (G is the lens reference wavelength)
image[2, :, :] = warped_BA[0, 0, :, :]
# Alpha: average the R-warped, G-original, and B-warped alpha values (in-place, no temporary)
image[3, :, :].add_(warped_RA[0, 1, :, :]).add_(warped_BA[0, 1, :, :]).mul_(1.0 / 3.0)
@with_metadata(strength=[0.1, 0.5],
_priority=2.0)
def vignetting(self, image: torch.tensor, *,
strength: float = 0.42) -> None:
"""[static] Simulate vignetting (less light hitting the corners of a film frame or CCD sensor).
The profile used here is [cos(strength * d * pi)]**2, where `d` is the distance
from the center, scaled such that `d = 1.0` is reached at the corners.
Thus, at the midpoints of the frame edges, `d = 1 / sqrt(2) ~ 0.707`.
"""
euclidean_distance_from_center = (self._meshy**2 + self._meshx**2)**0.5 / 2**0.5 # [h, w]
brightness = torch.cos(strength * euclidean_distance_from_center * math.pi)**2 # [h, w]
brightness = torch.unsqueeze(brightness, 0) # -> [1, h, w]
image[:3, :, :] *= brightness
# --------------------------------------------------------------------------------
# Retouching / color grading
def _rgb_to_hue(self, rgb: List[float]) -> float:
"""Convert an RGB color to an HSL hue, for use as `bandpass_hue` in `_desaturate_impl`.
This uses a cartesian-to-polar approximation of the HSL representation,
which is fine for hue detection, but should not be taken as an authoritative
H component of an accurate RGB->HSL conversion.
"""
R, G, B = rgb
alpha = 0.5 * (2.0 * R - G - B)
beta = 3.0**0.5 / 2.0 * (G - B)
hue = math.atan2(beta, alpha) / (2.0 * math.pi) # note atan2(0, 0) := 0
hue = hue + 0.5 # convert from `[-0.5, 0.5)` to `[0, 1)`
return hue
def _desaturate_impl(self, image: torch.tensor, *,
strength: float = 1.0,
tint_rgb: List[float] = [1.0, 1.0, 1.0],
bandpass_reference_rgb: List[float] = [1.0, 0.0, 0.0],
bandpass_q: float = 0.0) -> None:
"""Shared implementation for `desaturate` and `monochrome_display`."""
R = image[0, :, :]
G = image[1, :, :]
B = image[2, :, :]
if bandpass_q > 0.0: # hue bandpass enabled?
# Calculate hue of each pixel, using a cartesian-to-polar approximation of the HSL representation.
# An approximation is fine here, because we only use this for a hue detector.
# This is faster and requires less branching than the exact hexagonal representation.
desat_alpha = 0.5 * (2.0 * R - G - B)
desat_beta = 3.0**0.5 / 2.0 * (G - B)
desat_hue = torch.atan2(desat_beta, desat_alpha) / (2.0 * math.pi) # note atan2(0, 0) := 0
desat_hue += 0.5 # convert from `[-0.5, 0.5)` to `[0, 1)`
# -> [h, w]
# Determine whether to keep this pixel or desaturate (and by how much).
#
# Calculate distance of each pixel from reference hue, accounting for wrap-around.
bandpass_hue = self._rgb_to_hue(bandpass_reference_rgb)
desat_temp1 = torch.abs(desat_hue - bandpass_hue)
desat_temp2 = torch.abs((desat_hue + 1.0) - bandpass_hue)
desat_temp3 = torch.abs(desat_hue - (bandpass_hue + 1.0))
desat_hue_distance = 2.0 * torch.minimum(torch.minimum(desat_temp1, desat_temp2),
desat_temp3) # [0, 0.5] -> [0, 1]
# -> [h, w]
# How to interpret the following factor:
# 0: far away from reference hue, pixel should be desaturated
# 1: at reference hue, pixel should be kept as-is
#
# - Pixels with their hue at least `bandpass_q` away from `bandpass_hue` are fully desaturated.
# - As distance falls below `bandpass_q`, a blend starts very gradually.
# - As the hue difference approaches zero, the pixel is fully passed through.
# - The 1.0 - ... together with the square makes a sharp spike at the reference hue.
desat_diff2 = (1.0 - torch.clamp(desat_hue_distance / bandpass_q, min=0.0, max=1.0))**2
# Gray pixels should always be desaturated, so that they get the tint applied.
# In HSL computations, gray pixels have an arbitrary hue, usually red, so we must filter them out separately.
# We can do this in YUV space.
YUV = rgb_to_yuv(image[:3, :, :])
Y = YUV[0, :, :] # -> [h, w]
U = YUV[1, :, :] # -> [h, w]
V = YUV[2, :, :] # -> [h, w]
notgray_threshold = 0.05
urel = torch.clamp(torch.abs(U) / notgray_threshold, min=0.0, max=1.0)
vrel = torch.clamp(torch.abs(V) / notgray_threshold, min=0.0, max=1.0)
notgray = (urel**2 + vrel**2)**0.5 # [h, w]; 0: completely gray; 1: has at least some color
desat_diff2 *= notgray
strength_field = strength * (1.0 - desat_diff2) # [h, w]; "field" as in physics, NOT as in CRT TV
else:
Y = luminance(image[:3, :, :]) # save some compute since in this case we don't need U and V
strength_field = strength # just a scalar!
# Desaturate, then apply tint
Y = Y.unsqueeze(0) # -> [1, h, w]
tint_color = torch.tensor(tint_rgb, device=self.device, dtype=image.dtype).unsqueeze(1).unsqueeze(2) # [c, 1, 1]
tinted_desat_image = Y * tint_color # -> [c, h, w]
# Final blend
image[:3, :, :] = (1.0 - strength_field) * image[:3, :, :] + strength_field * tinted_desat_image
# This filter is adapted from an old GLSL code I made for Panda3D 1.8 back in 2014.
@with_metadata(strength=[0.0, 1.0],
tint_rgb=["!RGB"], # hint the GUI that this parameter needs an RGB color picker
bandpass_reference_rgb=["!RGB"],
bandpass_q=[0.0, 1.0],
_priority=3.5)
def desaturate(self, image: torch.tensor, *,
strength: float = 1.0,
tint_rgb: List[float] = [1.0, 1.0, 1.0],
bandpass_reference_rgb: List[float] = [1.0, 0.0, 0.0],
bandpass_q: float = 0.0) -> None:
"""[static] Desaturate the image, with optional hue bandpass and tint.
This is an image retouching / color grading effect. It runs early in the
chain (before noise and analog degradation), so the bandpass sees clean
color data. For monochrome display simulation, see `monochrome_display`.
Does not touch the alpha channel.
`strength`: Overall blending strength of the filter (0 is off, 1 is fully applied).
`tint_rgb`: Color to multiplicatively tint the image with. Applied after desaturation.
Some example tint values:
Sepia effect: [0.8039, 0.6588, 0.5098]
No tint (off; default): [1.0, 1.0, 1.0]
`bandpass_reference_rgb`: Reference color for hue to let through the bandpass.
Use this to let e.g. red things bypass the desaturation.
The hue is extracted automatically from the given color.
`bandpass_q`: Hue bandpass band half-width, in (0, 1]. Hues farther away from `bandpass_hue`
than `bandpass_q` will be fully desaturated. The opposite colors on the color
circle are defined as having the largest possible hue difference, 1.0.
The shape of the filter is a quadratic spike centered on the reference hue,
and smoothly decaying to zero at `bandpass_q` away from the center.
The special value 0 (default) switches the hue bandpass code off,
saving some compute.
"""
self._desaturate_impl(image, strength=strength, tint_rgb=tint_rgb,
bandpass_reference_rgb=bandpass_reference_rgb, bandpass_q=bandpass_q)
# --------------------------------------------------------------------------------
# General use
def _noise_impl(self, image: torch.tensor, *,
strength: float = 0.3,
sigma: float = 1.0,
channel: str = "Y",
double_size: bool = True,
ntsc_chroma: str = "nearest",
name: str = "noise0") -> None:
"""Shared implementation for `noise` and `analog_vhs_noise`."""
# Re-randomize the noise texture whenever the normalized frame number changes, or the video stream size changes.
c, h, w = image.shape
cached = self.noise_last_image[name]
size_changed = cached is not None and (cached.shape[-2] != h or cached.shape[-1] != w)
strength_changed = (strength != self.noise_last_strength[name])
if cached is None or size_changed or strength_changed or int(self.frame_no) > int(self.last_frame_no):
c, h, w = image.shape
if channel.startswith("VHS_"):
vhs_mode = channel.removeprefix("VHS_") # "PAL" or "NTSC"
noise_image = vhs_noise(w, h, device=self.device, dtype=image.dtype,
mode=vhs_mode, ntsc_chroma=ntsc_chroma, double_size=double_size)
# PAL: [1, H, W]; NTSC: [3, H, W] — stored as-is, distinguished at application time.
# NTSC chroma is already 4:2:0 subsampled inside vhs_noise.
else:
noise_image = isotropic_noise(w, h, device=self.device, dtype=image.dtype,
sigma=sigma, double_size=double_size)
noise_image.mul_(strength) # bake in current strength to save a few full-image multiplications during rendering
self.noise_last_image[name] = noise_image
self.noise_last_strength[name] = strength
else:
noise_image = cached
base_multiplier = 1.0 - strength
if channel == "A": # alpha
image[3, :, :].mul_(base_multiplier + noise_image)
return
image_yuv = rgb_to_yuv(image[:3, :, :])
if channel == "VHS_PAL":
image_yuv[0, :, :].mul_(base_multiplier + noise_image.squeeze(0))
elif channel == "VHS_NTSC":
image_yuv = rgb_to_yuv(image[:3, :, :])
# Luma: multiplicative (same as PAL/Y modes).
image_yuv[0, :, :].mul_(base_multiplier + noise_image[0])