Skip to content

Commit ac7e095

Browse files
committed
shift chroma to correct for group delay
1 parent 1d9df20 commit ac7e095

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

lddecode/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,7 @@ def downscale(
26512651
audio=0,
26522652
final=False,
26532653
lastfieldwritten=None,
2654+
shift=0
26542655
):
26552656
if lineinfo is None:
26562657
lineinfo = self.linelocs
@@ -2717,7 +2718,8 @@ def downscale(
27172718
self.rf.downscale_sinc_lut,
27182719
self.lineoffset,
27192720
outwidth,
2720-
wow_level_adjust_smoothing=self.wow_level_adjust_smoothing
2721+
wow_level_adjust_smoothing=self.wow_level_adjust_smoothing,
2722+
shift=shift
27212723
)
27222724

27232725
if self.rf.decode_digital_audio:

lddecode/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def scale(buf, begin, end, tgtlen, mult=1):
143143

144144

145145
@njit(nogil=True, fastmath=True)
146-
def scale_field(buf, dsout, interpolated_pixel_locs, wowfactors, sinc_lut, lineoffset, outwidth, wow_level_adjust_smoothing = 0, level_adjust_threshold = 15):
146+
def scale_field(buf, dsout, interpolated_pixel_locs, wowfactors, sinc_lut, lineoffset, outwidth, wow_level_adjust_smoothing = 0, level_adjust_threshold = 15, shift=0.0):
147147
# average out any unusual spikes in wow that happen on a per line basis
148148
# this indicates an hsync tbc error vs. being normal wow from playback speed variations
149149
# in this case for level adjusting we just want to fallback to the average wow to avoid a bright or dark line
@@ -175,7 +175,8 @@ def scale_field(buf, dsout, interpolated_pixel_locs, wowfactors, sinc_lut, lineo
175175
level_adjust = level_adjusts[i]
176176

177177
# reconstructs the waveform at the proper fractional sample position, undoing wow-induced timing variations
178-
coord = np.float32(interpolated_pixel_locs[i])
178+
# Adding the positive shift pulls future (late) samples backward into alignment.
179+
coord = np.float32(interpolated_pixel_locs[i] + shift)
179180
coord_int = int(coord)
180181

181182
# fractional phase

vhsdecode/chroma.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,11 +1682,27 @@ def process_chroma(
16821682
if field.burst_detected_line == -1:
16831683
# skip chroma if the color killer is active for the whole field
16841684
return uphet
1685+
1686+
if (
1687+
not field.rf.options.disable_phase_correction
1688+
and field.rf.color_system == "NTSC"
1689+
):
1690+
field.fieldPhaseID, target_phase = ntsc_color_framing_map[
1691+
(field.isFirstField, (field.field_number // 2) % 2)
1692+
]
1693+
chroma_shift_direction = 1 if target_phase else -1
1694+
else:
1695+
chroma_shift_direction = 0
16851696

16861697
# Run TBC/downscale on chroma (if new field, else uses cache)
16871698
# Cached if chroma process is run multiple times on one field due to track detection.
16881699
if field.chroma_tbc_buffer is None:
1689-
chroma, _, _ = ldd.Field.downscale(field, channel="demod_burst")
1700+
# shift the chroma to reverse group delay caused by the color under heterodyne filter
1701+
# this is dependent on color framing, and is disabled if color framing is disabled
1702+
# TODO: may need tuning / needs validation
1703+
chroma_subcarrier_delay_cycles = field.rf.chroma_afc.fsc_mhz * 1e6 / (2.0 * np.pi * field.rf.chroma_afc.color_under)
1704+
chroma_subcarrier_delay_samples = chroma_subcarrier_delay_cycles * 4
1705+
chroma, _, _ = ldd.Field.downscale(field, channel="demod_burst", shift=chroma_subcarrier_delay_samples * chroma_shift_direction)
16901706

16911707
# If chroma AFC is enabled
16921708
if field.rf.do_cafc:
@@ -1755,9 +1771,6 @@ def process_chroma(
17551771
not field.rf.options.disable_phase_correction
17561772
and field.rf.color_system == "NTSC"
17571773
):
1758-
field.fieldPhaseID, target_phase = ntsc_color_framing_map[
1759-
(field.isFirstField, (field.field_number // 2) % 2)
1760-
]
17611774
target_phase_even = target_phase
17621775
target_phase_odd = target_phase
17631776

0 commit comments

Comments
 (0)