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 | def auto_balance(
clip: vs.VideoNode, target_max: SupportsFloat | None = None, relative_sat: float = 1.0,
range_in: ColorRange = ColorRange.LIMITED, frame_overrides: Override | Sequence[Override] = [],
ref: vs.VideoNode | None = None, radius: int = 1, delta_thr: float = 0.4,
min_thr: float = 1.0, max_thr: float = 5.0,
min_thr_tr: float = 1.0, max_thr_tr: float = 5.0,
balance_mode: BalanceMode = BalanceMode.UNDIMMING, weight_mode: BalanceWeightMode = BalanceWeightMode.MEAN,
prop: bool = False
) -> vs.VideoNode:
import numpy as np
ref_clip = fallback(ref, clip)
assert check_variable(clip, auto_balance)
assert check_variable(ref_clip, auto_balance)
if ref_clip.format.sample_type is vs.FLOAT:
raise CustomValueError(auto_balance, 'Float auto_balance not implemented yet!')
zero = scale_value(16, 8, ref_clip, range_in, scale_offsets=True)
target = float(fallback(
target_max,
scale_value(
235, input_depth=8, output_depth=ref_clip,
range_in=range_in, scale_offsets=True
)
))
if weight_mode == BalanceWeightMode.NONE:
raise CustomValueError(auto_balance, 'Global weight mode can\'t be NONE!')
ref_stats = ref_clip.std.PlaneStats()
over_mapped = list[tuple[range, float, BalanceWeightMode]]()
if frame_overrides:
frame_overrides = [frame_overrides] if isinstance(frame_overrides, Override) else list(frame_overrides)
over_frames, over_conts, over_int_modes = list(zip(*frame_overrides))
oframes_ranges = [
range(start, stop + 1)
for start, stop in normalize_ranges(clip, list(over_frames))
]
over_mapped = list(zip(oframes_ranges, over_conts, over_int_modes))
clipfrange = range(0, clip.num_frames)
def _weighted(x: float, y: float, z: float) -> float:
return max(1e-6, x - z) / max(1e-6, y - z)
nobalanceclip = clip.std.SetFrameProps(AutoBalance=False) if prop else clip
def _autobalance(n: int, f: Sequence[vs.VideoFrame]) -> vs.VideoNode:
override: tuple[range, float, BalanceWeightMode] | None = next((x for x in over_mapped if n in x[0]), None)
psvalues: Any = np.asarray([
_weighted(target, get_prop(frame.props, 'PlaneStatsMax', int), zero) for frame in f
])
middle_idx = psvalues.size // 2
mean_value = np.mean(psvalues)
if not override and not (mean_value >= min_thr_tr and mean_value <= max_thr_tr):
return nobalanceclip
curr_value = psvalues[middle_idx]
if not override and not (curr_value >= min_thr and curr_value <= max_thr):
return nobalanceclip
if balance_mode == BalanceMode.UNDIMMING:
psvalues[psvalues < 1.0] = 1.0
elif balance_mode == BalanceMode.DIMMING:
psvalues[psvalues > 1.0] = 1.0
psvalues[(abs(psvalues - curr_value) > delta_thr)] = curr_value
def _get_cont(mode: BalanceWeightMode, frange: range) -> Any:
if mode == BalanceWeightMode.INTERPOLATE:
if radius < 1:
raise CustomValueError(auto_balance, 'Radius has to be >= 1 with BalanceWeightMode.INTERPOLATE!')
weight = (n - (frange.start - 1)) / (frange.stop - (frange.start - 1))
weighted_prev = psvalues[middle_idx - 1] * (1 - weight)
weighted_next = psvalues[middle_idx + 1] * weight
return weighted_prev + weighted_next
if mode == BalanceWeightMode.MEDIAN:
return np.median(psvalues)
if mode == BalanceWeightMode.MEAN:
return psvalues.mean()
if mode == BalanceWeightMode.MAX:
return psvalues.max()
if mode == BalanceWeightMode.MIN:
return psvalues.min()
return psvalues[middle_idx]
if override:
frange, cont, override_mode = override
if override_mode == BalanceWeightMode.NONE:
return nobalanceclip
if cont is not None:
psvalues[
max(0, middle_idx - (n - frange.start)):
min(len(psvalues), middle_idx + (frange.stop - n))
] = cont
if (override_mode != weight_mode):
cont = _get_cont(override_mode, frange)
else:
cont = _get_cont(weight_mode, clipfrange)
sat = (cont - 1) * relative_sat + 1
fix = tweak_clip(clip, cont, sat, range_in=range_in)
if prop:
return fix.std.SetFrameProps(AutoBalance=True, AutoBalanceCont=cont, AutoBalanceSat=sat)
return fix
stats_clips = [
*(ref_stats[0] * i + ref_stats[:-i] for i in range(1, radius + 1)),
ref_stats,
*(ref_stats[i:] + ref_stats[-1] * i for i in range(1, radius + 1)),
]
return clip.std.FrameEval(_autobalance, stats_clips, clip)
|