-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsrestore.py
More file actions
621 lines (560 loc) · 24.7 KB
/
Copy pathsrestore.py
File metadata and controls
621 lines (560 loc) · 24.7 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
from __future__ import annotations
import importlib
import math
from functools import partial
from typing import Any, Mapping, Optional, Sequence, Union
import vapoursynth as vs
from helpers import Depth, GetPlane, scale_value, cround
from ChangeFPS import ChangeFPS
core = vs.core
def sRestoreMUVs(
source: vs.VideoNode,
frate: Optional[numbers.Real] = None,
omode: int = 6,
speed: Optional[int] = None,
mode: int = 2,
thresh: int = 16,
dclip: Optional[vs.VideoNode] = None
) -> vs.VideoNode:
""" srestore v2.7e
srestore with serialized execution by explicit node processing dependency
modified from havsfunc's srestore function
https://github.com/HomeOfVapourSynthEvolution/havsfunc/blob/e236281cd8c1dd6b1b0cc906844944b79b1b52fa/havsfunc.py#L1899-L2227
"""
if not isinstance(source, vs.VideoNode):
raise vs.Error('srestore: this is not a clip')
if source.format.color_family != vs.YUV:
raise vs.Error('srestore: only YUV format is supported')
if dclip is None:
dclip = source
elif not isinstance(dclip, vs.VideoNode):
raise vs.Error("srestore: 'dclip' is not a clip")
elif dclip.format.color_family != vs.YUV:
raise vs.Error('srestore: only YUV format is supported')
bits = source.format.bits_per_sample
neutral = 1 << (bits - 1)
peak = (1 << bits) - 1
###### parameters & other necessary vars ######
srad = math.sqrt(abs(speed)) * 4 if speed is not None and abs(speed) >= 1 else 12
irate = source.fps_num / source.fps_den
bsize = 16 if speed is not None and speed > 0 else 32
bom = isinstance(omode, str)
thr = abs(thresh) + 0.01
if bom or abs(omode - 3) < 2.5:
frfac = 1
elif frate is not None:
if frate * 5 < irate or frate > irate:
frfac = 1
else:
frfac = abs(frate) / irate
elif cround(irate * 10010) % 30000 == 0:
frfac = 1001 / 2400
else:
frfac = 480 / 1001
if abs(frfac * 1001 - cround(frfac * 1001)) < 0.01:
numr = cround(frfac * 1001)
elif abs(1001 / frfac - cround(1001 / frfac)) < 0.01:
numr = 1001
else:
numr = cround(frfac * 9000)
if (
frate is not None and
abs(irate * numr / cround(numr / frfac) - frate) > abs(irate * cround(frate * 100) / cround(irate * 100) - frate)
):
numr = cround(frate * 100)
denm = cround(numr / frfac)
###### source preparation & lut ######
if abs(mode) >= 2 and not bom:
mec = core.std.Merge(source, source.std.Trim(first=1), weight=[0, 0.5])
mec = core.std.Merge(mec, source.std.Trim(first=1), weight=[0.5, 0])
if dclip.format.id != vs.YUV420P8:
dclip = dclip.resize.Bicubic(format=vs.YUV420P8)
dclip = dclip.resize.Point(
dclip.width if srad == 4 else int(dclip.width / 2 / srad + 4) * 4,
dclip.height if srad == 4 else int(dclip.height / 2 / srad + 4) * 4
)
dclip = dclip.std.Trim(first=2)
EXPR = core.akarin.Expr if hasattr(core, 'akarin') else core.cranexpr.Expr if hasattr(core, 'cranexpr') else core.std.Expr
if mode < 0:
dclip = core.std.StackVertical([
core.std.StackHorizontal([GetPlane(dclip, 1), GetPlane(dclip, 2)]),
GetPlane(dclip, 0)
])
else:
dclip = GetPlane(dclip, 0)
if bom:
dclip = EXPR(dclip, expr=['x 0.5 * 64 +'])
expr1 = 'x 128 - y 128 - * 0 > x 128 - abs y 128 - abs < x 128 - 128 x - * y 128 - 128 y - * ? x y + 256 - dup * ? 0.25 * 128 +'
expr2 = 'x y - dup * 3 * x y + 256 - dup * - 128 +'
diff = core.std.MakeDiff(dclip, dclip.std.Trim(first=1))
if not bom:
bclp = EXPR([diff, diff.std.Trim(first=1)], expr=[expr1]).resize.Bilinear(bsize, bsize)
else:
bclp = EXPR([
diff.std.Trim(first=1),
core.std.MergeDiff(diff, diff.std.Trim(first=2))
], expr=[expr2])
bclp = bclp.resize.Bilinear(bsize, bsize)
dclp = diff.std.Trim(first=1).std.Lut(function=lambda x: max(cround(abs(x - 128) ** 1.1 - 1), 0))
dclp = dclp.resize.Bilinear(bsize, bsize)
###### postprocessing ######
if bom:
sourceDuplicate = source.std.DuplicateFrames(frames=[0])
sourceTrim1 = source.std.Trim(first=1)
sourceTrim2 = source.std.Trim(first=2)
unblend1 = EXPR([sourceDuplicate, source], expr=['x -1 * y 2 * +'])
unblend2 = EXPR([sourceTrim1, sourceTrim2], expr=['x 2 * y -1 * +'])
qmask1 = core.std.MakeDiff(
unblend1.std.Convolution(matrix=[1, 1, 1, 1, 0, 1, 1, 1, 1], planes=[0]),
unblend1,
planes=[0]
)
qmask2 = core.std.MakeDiff(
unblend2.std.Convolution(matrix=[1, 1, 1, 1, 0, 1, 1, 1, 1], planes=[0]),
unblend2,
planes=[0]
)
diffm = core.std.MakeDiff(sourceDuplicate, source, planes=[0]).std.Maximum(planes=[0])
bmask = EXPR([qmask1, qmask2], expr=[f'x {neutral} - dup * dup y {neutral} - dup * + / {peak} *', ''])
expr = (
'x 2 * y < x {i} < and 0 y 2 * x < y {i} < and {peak} x x y + / {j} * {k} + ? ?'
.format(i=scale_value(4, 8, bits), peak=peak, j=scale_value(200, 8, bits), k=scale_value(28, 8, bits))
)
dmask = EXPR([diffm, diffm.std.Trim(first=2)], expr=[expr, ''])
pmask = EXPR([dmask, bmask], expr=[f'y 0 > y {peak} < and x 0 = x {peak} = or and x y ?', ''])
matrix = [1, 2, 1, 2, 4, 2, 1, 2, 1]
omode = omode.lower()
if omode == 'pp0':
fin = EXPR([sourceDuplicate, source, sourceTrim1, sourceTrim2], expr=['x -0.5 * y + z + a -0.5 * +'])
elif omode == 'pp1':
fin = core.std.MaskedMerge(
unblend1,
unblend2,
EXPR(dmask.std.Convolution(matrix=matrix, planes=[0]), expr=['', repr(neutral)])
)
elif omode == 'pp2':
fin = core.std.MaskedMerge(
unblend1,
unblend2,
bmask.std.Convolution(matrix=matrix, planes=[0]), first_plane=True
)
elif omode == 'pp3':
fin = core.std.MaskedMerge(
unblend1,
unblend2,
pmask.std.Convolution(matrix=matrix, planes=[0]), first_plane=True
)
fin = fin.std.Convolution(matrix=matrix, planes=[1, 2])
else:
raise vs.Error('srestore: unexpected value for omode')
class SRestoreState:
def __init__(self):
self.lfr = -100
self.offs = 0
self.ldet = -100
self.lpos = 0
self.d32 = None
self.d21 = None
self.d10 = None
self.d01 = None
self.d12 = None
self.d23 = None
self.d34 = None
self.m42 = None
self.m31 = None
self.m20 = None
self.m11 = None
self.m02 = None
self.m13 = None
self.m24 = None
self.bp2 = None
self.bp1 = None
self.bn0 = None
self.bn1 = None
self.bn2 = None
self.bn3 = None
self.cp2 = None
self.cp1 = None
self.cn0 = None
self.cn1 = None
self.cn2 = None
self.cn3 = None
def update_from_frame(self, f: vs.VideoFrame):
if f is not None and hasattr(f, 'props'):
props = f.props
self.lfr = props.get("_lfr", self.lfr)
self.offs = props.get("_offs", self.offs)
self.ldet = props.get("_ldet", self.ldet)
self.lpos = props.get("_lpos", self.lpos)
self.d32 = props.get("_d32", self.d32)
self.d21 = props.get("_d21", self.d21)
self.d10 = props.get("_d10", self.d10)
self.d01 = props.get("_d01", self.d01)
self.d12 = props.get("_d12", self.d12)
self.d23 = props.get("_d23", self.d23)
self.d34 = props.get("_d34", self.d34)
self.m42 = props.get("_m42", self.m42)
self.m31 = props.get("_m31", self.m31)
self.m20 = props.get("_m20", self.m20)
self.m11 = props.get("_m11", self.m11)
self.m02 = props.get("_m02", self.m02)
self.m13 = props.get("_m13", self.m13)
self.m24 = props.get("_m24", self.m24)
self.bp2 = props.get("_bp2", self.bp2)
self.bp1 = props.get("_bp1", self.bp1)
self.bn0 = props.get("_bn0", self.bn0)
self.bn1 = props.get("_bn1", self.bn1)
self.bn2 = props.get("_bn2", self.bn2)
self.bn3 = props.get("_bn3", self.bn3)
self.cp2 = props.get("_cp2", self.cp2)
self.cp1 = props.get("_cp1", self.cp1)
self.cn0 = props.get("_cn0", self.cn0)
self.cn1 = props.get("_cn1", self.cn1)
self.cn2 = props.get("_cn2", self.cn2)
self.cn3 = props.get("_cn3", self.cn3)
def to_frame_props(self) -> dict:
props = {
"_lfr": self.lfr,
"_offs": self.offs,
"_ldet": self.ldet,
"_lpos": self.lpos
}
if self.d32 is not None: props["_d32"] = self.d32
if self.d21 is not None: props["_d21"] = self.d21
if self.d10 is not None: props["_d10"] = self.d10
if self.d01 is not None: props["_d01"] = self.d01
if self.d12 is not None: props["_d12"] = self.d12
if self.d23 is not None: props["_d23"] = self.d23
if self.d34 is not None: props["_d34"] = self.d34
if self.m42 is not None: props["_m42"] = self.m42
if self.m31 is not None: props["_m31"] = self.m31
if self.m20 is not None: props["_m20"] = self.m20
if self.m11 is not None: props["_m11"] = self.m11
if self.m02 is not None: props["_m02"] = self.m02
if self.m13 is not None: props["_m13"] = self.m13
if self.m24 is not None: props["_m24"] = self.m24
if self.bp2 is not None: props["_bp2"] = self.bp2
if self.bp1 is not None: props["_bp1"] = self.bp1
if self.bn0 is not None: props["_bn0"] = self.bn0
if self.bn1 is not None: props["_bn1"] = self.bn1
if self.bn2 is not None: props["_bn2"] = self.bn2
if self.bn3 is not None: props["_bn3"] = self.bn3
if self.cp2 is not None: props["_cp2"] = self.cp2
if self.cp1 is not None: props["_cp1"] = self.cp1
if self.cn0 is not None: props["_cn0"] = self.cn0
if self.cn1 is not None: props["_cn1"] = self.cn1
if self.cn2 is not None: props["_cn2"] = self.cn2
if self.cn3 is not None: props["_cn3"] = self.cn3
return props
def srestore_inside(n: int, f: List[vs.VideoFrame], real_n: int, state_obj: SRestoreState) -> vs.VideoNode:
n = real_n
if n > 0:
state_obj.update_from_frame(f[3])
### preparation ###
jmp = state_obj.lfr + 1 == n
cfo = ((n % denm) * numr * 2 + denm + numr) % (2 * denm) - denm
bfo = cfo > -numr and cfo <= numr
state_obj.lfr = n
if bfo:
if state_obj.offs <= -4 * numr:
state_obj.offs = state_obj.offs + 2 * denm
elif state_obj.offs >= 4 * numr:
state_obj.offs = state_obj.offs - 2 * denm
pos = 0 if frfac == 1 else -cround((cfo + state_obj.offs) / (2 * numr)) if bfo else state_obj.lpos
cof = cfo + state_obj.offs + 2 * numr * pos
state_obj.ldet = -1 if n + pos == state_obj.ldet else n + pos
### diff value shifting ###
d_v = f[1].props['PlaneStatsMax'] + 0.015625
if jmp:
d43 = state_obj.d32
state_obj.d32 = state_obj.d21
state_obj.d21 = state_obj.d10
state_obj.d10 = state_obj.d01
state_obj.d01 = state_obj.d12
state_obj.d12 = state_obj.d23
state_obj.d23 = state_obj.d34
else:
d43 = state_obj.d32 = state_obj.d21 = state_obj.d10 = state_obj.d01 = state_obj.d12 = state_obj.d23 = d_v
state_obj.d34 = d_v
m_v = f[2].props['PlaneStatsDiff'] * 255 + 0.015625 if not bom and abs(omode) > 5 else 1
if jmp:
m53 = state_obj.m42
state_obj.m42 = state_obj.m31
state_obj.m31 = state_obj.m20
state_obj.m20 = state_obj.m11
state_obj.m11 = state_obj.m02
state_obj.m02 = state_obj.m13
state_obj.m13 = state_obj.m24
else:
m53 = state_obj.m42 = state_obj.m31 = state_obj.m20 = state_obj.m11 = state_obj.m02 = state_obj.m13 = m_v
state_obj.m24 = m_v
### get blend and clear values ###
b_v = 128 - f[0].props['PlaneStatsMin']
if b_v < 1:
b_v = 0.125
c_v = f[0].props['PlaneStatsMax'] - 128
if c_v < 1:
c_v = 0.125
### blend value shifting ###
if jmp:
bp3 = state_obj.bp2
state_obj.bp2 = state_obj.bp1
state_obj.bp1 = state_obj.bn0
state_obj.bn0 = state_obj.bn1
state_obj.bn1 = state_obj.bn2
state_obj.bn2 = state_obj.bn3
else:
bp3 = b_v - c_v if bom else b_v
state_obj.bp2 = state_obj.bp1 = state_obj.bn0 = state_obj.bn1 = state_obj.bn2 = bp3
state_obj.bn3 = b_v - c_v if bom else b_v
### clear value shifting ###
if jmp:
cp3 = state_obj.cp2
state_obj.cp2 = state_obj.cp1
state_obj.cp1 = state_obj.cn0
state_obj.cn0 = state_obj.cn1
state_obj.cn1 = state_obj.cn2
state_obj.cn2 = state_obj.cn3
else:
cp3 = state_obj.cp2 = state_obj.cp1 = state_obj.cn0 = state_obj.cn1 = state_obj.cn2 = c_v
state_obj.cn3 = c_v
### used detection values ###
bb = [bp3, state_obj.bp2, state_obj.bp1, state_obj.bn0, state_obj.bn1][pos + 2]
bc = [state_obj.bp2, state_obj.bp1, state_obj.bn0, state_obj.bn1, state_obj.bn2][pos + 2]
bn = [state_obj.bp1, state_obj.bn0, state_obj.bn1, state_obj.bn2, state_obj.bn3][pos + 2]
cb = [cp3, state_obj.cp2, state_obj.cp1, state_obj.cn0, state_obj.cn1][pos + 2]
cc = [state_obj.cp2, state_obj.cp1, state_obj.cn0, state_obj.cn1, state_obj.cn2][pos + 2]
cn = [state_obj.cp1, state_obj.cn0, state_obj.cn1, state_obj.cn2, state_obj.cn3][pos + 2]
dbb = [d43, state_obj.d32, state_obj.d21, state_obj.d10, state_obj.d01][pos + 2]
dbc = [state_obj.d32, state_obj.d21, state_obj.d10, state_obj.d01, state_obj.d12][pos + 2]
dcn = [state_obj.d21, state_obj.d10, state_obj.d01, state_obj.d12, state_obj.d23][pos + 2]
dnn = [state_obj.d10, state_obj.d01, state_obj.d12, state_obj.d23, state_obj.d34][pos + 2]
dn2 = [state_obj.d01, state_obj.d12, state_obj.d23, state_obj.d34, state_obj.d34][pos + 2]
mb1 = [m53, state_obj.m42, state_obj.m31, state_obj.m20, state_obj.m11][pos + 2]
mb = [state_obj.m42, state_obj.m31, state_obj.m20, state_obj.m11, state_obj.m02][pos + 2]
mc = [state_obj.m31, state_obj.m20, state_obj.m11, state_obj.m02, state_obj.m13][pos + 2]
mn = [state_obj.m20, state_obj.m11, state_obj.m02, state_obj.m13, state_obj.m24][pos + 2]
mn1 = [state_obj.m11, state_obj.m02, state_obj.m13, state_obj.m24, 0.01][pos + 2]
### basic calculation ###
bbool = 0.8 * bc * cb > bb * cc and 0.8 * bc * cn > bn * cc and bc * bc > cc
blend = (
bbool and
bc * 5 > cc and
dbc + dcn > 1.5 * thr and
(dbb < 7 * dbc or dbb < 8 * dcn) and
(dnn < 8 * dcn or dnn < 7 * dbc) and
(
mb < mb1 and mb < mc or
mn < mn1 and mn < mc or
(dbb + dnn) * 4 < dbc + dcn or
(bb * cc * 5 < bc * cb or mb > thr) and (bn * cc * 5 < bc * cn or mn > thr) and bc > thr
)
)
clear = (
dbb + dbc > thr and
dcn + dnn > thr and
(bc < 2 * bb or bc < 2 * bn) and
(dbb + dnn) * 2 > dbc + dcn and
(
mc < 0.96 * mb and mc < 0.96 * mn and (bb * 2 > cb or bn * 2 > cn) and cc > cb and cc > cn or
frfac > 0.45 and frfac < 0.55 and 0.8 * mc > mb1 and 0.8 * mc > mn1 and mb > 0.8 * mn and mn > 0.8 * mb
)
)
highd = dcn > 5 * dbc and dcn > 5 * dnn and dcn > thr and dbc < thr and dnn < thr
lowd = (
dcn * 5 < dbc and
dcn * 5 < dnn and
dbc > thr and
dnn > thr and
dcn < thr and
frfac > 0.35 and
(frfac < 0.51 or dcn * 5 < dbb)
)
res = (
d43 < thr and
state_obj.d32 < thr and
state_obj.d21 < thr and
state_obj.d10 < thr and
state_obj.d01 < thr and
state_obj.d12 < thr and
state_obj.d23 < thr and
state_obj.d34 < thr or
dbc * 4 < dbb and
dcn * 4 < dbb and
dnn * 4 < dbb and
dn2 * 4 < dbb or
dcn * 4 < dbc and
dnn * 4 < dbc and
dn2 * 4 < dbc
)
### offset calculation ###
if blend:
odm = denm
elif clear:
odm = 0
elif highd:
odm = denm - numr
elif lowd:
odm = 2 * denm - numr
else:
odm = cof
odm += cround((cof - odm) / (2 * denm)) * 2 * denm
if blend:
odr = denm - numr
elif clear or highd:
odr = numr
elif frfac < 0.5:
odr = 2 * numr
else:
odr = 2 * (denm - numr)
odr *= 0.9
if state_obj.ldet >= 0:
if cof > odm + odr:
if cof - state_obj.offs - odm - odr > denm and res:
cof = odm + 2 * denm - odr
else:
cof = odm + odr
elif cof < odm - odr:
if state_obj.offs > denm and res:
cof = odm - 2 * denm + odr
else:
cof = odm - odr
elif state_obj.offs < -1.15 * denm and res:
cof += 2 * denm
elif state_obj.offs > 1.25 * denm and res:
cof -= 2 * denm
state_obj.offs = 0 if frfac == 1 else cof - cfo - 2 * numr * pos
state_obj.lpos = pos
if frfac == 1:
opos = 0
else:
opos = -cround((cfo + state_obj.offs + (denm if bfo and state_obj.offs <= -4 * numr else 0)) / (2 * numr))
pos = min(max(opos, -2), 2)
### frame output calculation - resync - dup ###
dbb = [d43, state_obj.d32, state_obj.d21, state_obj.d10, state_obj.d01][pos + 2]
dbc = [state_obj.d32, state_obj.d21, state_obj.d10, state_obj.d01, state_obj.d12][pos + 2]
dcn = [state_obj.d21, state_obj.d10, state_obj.d01, state_obj.d12, state_obj.d23][pos + 2]
dnn = [state_obj.d10, state_obj.d01, state_obj.d12, state_obj.d23, state_obj.d34][pos + 2]
### dup_hq - merge ###
if opos != pos or abs(mode) < 2 or abs(mode) == 3:
dup = 0
elif (
dcn * 5 < dbc and dnn * 5 < dbc and (dcn < 1.25 * thr or bn < bc and pos == state_obj.lpos) or
(dcn * dcn < dbc or dcn * 5 < dbc) and bn < bc and pos == state_obj.lpos and dnn < 0.9 * dbc or
dnn * 9 < dbc and dcn * 3 < dbc
):
dup = 1
elif (
(dbc * dbc < dcn or dbc * 5 < dcn) and bb < bc and pos == state_obj.lpos and dbb < 0.9 * dcn or
dbb * 9 < dcn and dbc * 3 < dcn or
dbb * 5 < dcn and dbc * 5 < dcn and (dbc < 1.25 * thr or bb < bc and pos == state_obj.lpos)
):
dup = -1
else:
dup = 0
mer = (
not bom and
opos == pos and
dup == 0 and
abs(mode) > 2 and
(
dbc * 8 < dcn or
dbc * 8 < dbb or
dcn * 8 < dbc or
dcn * 8 < dnn or
dbc * 2 < thr or
dcn * 2 < thr or
dnn * 9 < dbc and dcn * 3 < dbc or
dbb * 9 < dcn and dbc * 3 < dcn
)
)
### deblend - doubleblend removal - postprocessing ###
add = (
state_obj.bp1 * state_obj.cn2 > state_obj.bn2 * state_obj.cp1 * (1 + thr * 0.01) and
state_obj.bn0 * state_obj.cn2 > state_obj.bn2 * state_obj.cn0 * (1 + thr * 0.01) and
state_obj.cn2 * state_obj.bn1 > state_obj.cn1 * state_obj.bn2 * (1 + thr * 0.01)
)
if bom:
if state_obj.bn0 > state_obj.bp2 and state_obj.bn0 >= state_obj.bp1 and state_obj.bn0 > state_obj.bn1 and state_obj.bn0 > state_obj.bn2 and state_obj.cn0 < 125:
if state_obj.d12 * state_obj.d12 < state_obj.d10 or state_obj.d12 * 9 < state_obj.d10:
dup = 1
elif state_obj.d10 * state_obj.d10 < state_obj.d12 or state_obj.d10 * 9 < state_obj.d12:
dup = 0
else:
dup = 4
elif state_obj.bp1 > bp3 and state_obj.bp1 >= state_obj.bp2 and state_obj.bp1 > state_obj.bn0 and state_obj.bp1 > state_obj.bn1:
dup = 1
else:
dup = 0
elif dup == 0:
if omode > 0 and omode < 5:
if not bbool:
dup = 0
elif omode == 4 and state_obj.bp1 * state_obj.cn1 < state_obj.bn1 * state_obj.cp1 or omode == 3 and state_obj.d10 < state_obj.d01 or omode == 1:
dup = -1
else:
dup = 1
elif omode == 5:
if (
state_obj.bp1 * state_obj.cp2 > state_obj.bp2 * state_obj.cp1 * (1 + thr * 0.01) and
state_obj.bn0 * state_obj.cp2 > state_obj.bp2 * state_obj.cn0 * (1 + thr * 0.01) and
state_obj.cp2 * state_obj.bn1 > state_obj.cn1 * state_obj.bp2 * (1 + thr * 0.01) and
(not add or state_obj.cp2 * state_obj.bn2 > state_obj.cn2 * state_obj.bp2)
):
dup = -2
elif add:
dup = 2
elif state_obj.bn0 * state_obj.cp1 > state_obj.bp1 * state_obj.cn0 and (state_obj.bn0 * state_obj.cn1 < state_obj.bn1 * state_obj.cn0 or state_obj.cp1 * state_obj.bn1 > state_obj.cn1 * state_obj.bp1):
dup = -1
elif state_obj.bn0 * state_obj.cn1 > state_obj.bn1 * state_obj.cn0:
dup = 1
else:
dup = 0
else:
dup = 0
### output clip ###
if dup == 4:
ret = fin[n] if n < fin.num_frames else fin[fin.num_frames - 1]
else:
oclp = mec if mer and dup == 0 else source
opos += dup - (1 if dup == 0 and mer and dbc < dcn else 0)
# Calculate the actual frame number to output
target_frame = n + opos
# Ensure the target frame is within valid bounds
if target_frame < 0:
target_frame = 0
elif target_frame >= oclp.num_frames:
target_frame = oclp.num_frames - 1
ret = oclp[target_frame]
props = state_obj.to_frame_props()
if hasattr(core.std, "SetFrameProps"):
return core.std.SetFrameProps(ret, **props)
else:
for k, v in props.items():
ret = core.std.SetFrameProp(ret, prop=k, intval=v)
return ret
###### evaluation call & output calculation ######
bclpYStats = bclp.std.PlaneStats()
dclpYStats = dclp.std.PlaneStats()
dclipYStats = core.std.PlaneStats(dclip, dclip.std.Trim(first=2))
# https://github.com/vapoursynth/vapoursynth/blob/55e7d0e989359c23782fc1e0d4aa1c0c35838a80/src/core/vsapi.cpp#L151-L152
def get_frame(clip: vs.VideoNode, n: int) -> vs.VideoNode:
return clip[min(n, clip.num_frames - 1)]
last_frames: List[vs.VideoNode] = []
state_obj = SRestoreState()
for n in range(source.num_frames):
prop_src = [
get_frame(bclpYStats, n),
get_frame(dclpYStats, n),
get_frame(dclipYStats, n)
]
if n > 0:
prop_src.append(last_frames[-1])
frame = source[n].std.FrameEval(
eval=partial(srestore_inside, real_n=n, state_obj=state_obj),
prop_src=prop_src
)
last_frames.append(frame)
last = core.std.Splice(last_frames)
###### final decimation ######
return ChangeFPS(last, source.fps_num * numr, source.fps_den * denm)