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 | def apply_rff_video(
node: vs.VideoNode, rff: list[int], tff: list[int], prog: list[int], prog_seq: list[int]
) -> ConstantFormatVideoNode:
assert len(node) == len(rff) == len(tff) == len(prog) == len(prog_seq)
fields = list[dict[str, int]]()
tfffs = node.std.RemoveFrameProps(['_FieldBased', '_Field']).std.SeparateFields(True)
for i, current_prg_seq, current_prg, current_rff, current_tff in zip(count(), prog_seq, prog, rff, tff):
if not current_prg_seq:
if current_tff:
first_field = 2 * i
second_field = 2 * i + 1
else:
first_field = 2 * i + 1
second_field = 2 * i
fields += [
{'n': first_field, 'tf': current_tff, 'prg': False, 'repeat': False},
{'n': second_field, 'tf': not current_tff, 'prg': False, 'repeat': False}
]
if current_rff:
assert current_prg
repeat_field = deepcopy(fields[-2])
repeat_field['repeat'] = True
fields.append(repeat_field)
else:
assert current_prg
field_count = 1
if current_rff:
field_count += 1 + int(current_tff)
fields += [
{'n': 2 * i, 'tf': 1, 'prg': True, 'repeat': False},
{'n': 2 * i + 1, 'tf': 0, 'prg': True, 'repeat': False}
] * field_count
# TODO: mark known progressive frames as progressive
# assert (len(fields) % 2) == 0
if (len(fields) % 2) != 0:
warnings.warn('uneven amount of fields removing last\n')
fields = fields[:-1]
for a, tf, bf in zip(count(), fields[::2], fields[1::2]):
if tf['tf'] == bf['tf']:
bf['tf'] = not bf['tf']
warnings.warn(f'Invalid field transition at {a}')
for fcurr, fnext in zip(fields[::2], fields[1::2]):
if fcurr['tf'] == fnext['tf']:
raise CustomRuntimeError(
f'Found invalid stream with two consecutive {"top" if fcurr["tf"] else "bottom"} fields!'
)
final = remap_frames(tfffs, [x['n'] for x in fields])
def _set_field(n: int, f: vs.VideoFrame) -> vs.VideoFrame:
f = f.copy()
f.props.pop('_FieldBased', None)
f.props._Field = fields[n]['tf']
return f
final = vs.core.std.ModifyFrame(final, final, _set_field)
woven = final.std.DoubleWeave()[::2]
def _set_repeat(n: int, f: vs.VideoFrame) -> vs.VideoFrame:
f = f.copy()
if fields[n * 2]['repeat']:
f.props['RepeatedField'] = 1
elif fields[n * 2 + 1]['repeat']:
f.props['RepeatedField'] = 0
else:
f.props['RepeatedField'] = -1
return f
woven = vs.core.std.ModifyFrame(woven, woven, _set_repeat)
# TODO: this seems to not work or atleast useless since its disable for non progressive sequence which is rare
def _update_progressive(n: int, f: vs.VideoFrame) -> vs.VideoFrame:
fout = f.copy()
tf = fields[n * 2]
bf = fields[n * 2 + 1]
if tf['prg'] and bf['prg']:
fout.props['_FieldBased'] = 0
return fout
return vs.core.std.ModifyFrame(woven, woven, _update_progressive)
|