-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignsyncs
executable file
·222 lines (205 loc) · 10.2 KB
/
alignsyncs
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
#!/usr/bin/perl
#
# Copyright (C) 2020-2022 Tony Anderson <tandersn@cs.washington.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
use strict;
use warnings;
use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END);
#~2542 samples per NTSC line at 40msps
#
our ( @avgdata, $infiledata, $infilecontrol, $outfile, $bin_file_pos, $data_chunk_1, $data_chunk_2, $hit, $i, $bin_file_posadj, $filecount, $linecount, @frame, @frame_line, $tmpvar, $ii );
our ( $linepos, $check_first_field, $bff, $DEBUG );
$infiledata = "/tmp/vhs_fm_dmod_v3.u8";
#$infiledata = "/tmp/vhs_fm_dmod_v3_atlantis.u8";
$outfile = "/tmp/outbackwards.u8";
$bin_file_pos = 1;
$filecount = 1;
$bff = 0;
$DEBUG = $ARGV[0];
open my $INDATA, '<', $infiledata or die;
binmode $INDATA;
open my $OUTDATAFULL, '>', $outfile or die;
binmode $OUTDATAFULL;
if ($DEBUG) {
# $bin_file_pos = 1004003520;
$tmpvar = `mkdir /tmp/picstemp 2>/dev/null`;
$tmpvar = `rm /tmp/picstemp/* 2>/dev/null`;
$tmpvar = `rm /tmp/out_* 2>/dev/null`;
}
$check_first_field = 0;
while (1) {
$data_chunk_1 = "";
#################################################################################
############## read in 1000 samples, and average them, and see if below 30
############## (30 is pretty tight, used 50 initially in testing)
############## this only occurs in the 2 BR pulses on lines 4,5,6 & 266,267,268,269
############## note this requires the input file to be properly "clamped"
############## if not, seek ahead a bit and repeat. This takes a while at beginning
##################################################################################
seek $INDATA, $bin_file_pos, SEEK_SET;
my $success = read $INDATA, $data_chunk_1, 1000;
die $! if not defined $success;
last if not $success;
$tmpvar = 0;
@avgdata = split //, $data_chunk_1;
foreach $data_chunk_1 (@avgdata) {
$data_chunk_1 = unpack "C", $data_chunk_1;
$tmpvar = $tmpvar + $data_chunk_1;
}
$data_chunk_1 = $tmpvar / 1000;
if ( $data_chunk_1 < 30 ) {
##################################################################################
############# found BR pulses, if this is the first pulse found when starting the
############# file, or the first pulse found after a skipped/dropped field, then
############# need to check if bottom or top field. Basically we are going to
############# move the file pointer ahead ~3.5 lines, then check the number
############# of the pulses. If a long pulse is found in the next ~5 lines then
############# this indicates it is the bottom field
##################################################################################
############# it was a judgement call on my part to drop both fields if even only
############# one is bad, so that frame/field mismatch doesn't occur, who knows
############# if this is the right thing to do?????
#################################################################################
if ( $check_first_field == 0 ) {
$check_first_field = 1;
seek $INDATA, $bin_file_pos + 7250, SEEK_SET;
my $success2 = read $INDATA, $data_chunk_2, 10168;
die $! if not defined $success2;
last if not $success2;
@avgdata = split //, $data_chunk_2;
$i = 0;
$hit = 0;
foreach $data_chunk_1 (@avgdata) {
$data_chunk_1 = unpack "C", $data_chunk_1;
if ( $data_chunk_1 > 30 ) {
$i = $i + 1;
} else {
$i = 0;
}
if ( $i > 2000 ) {
### found a line longer than 2000, not EQ PULSE, indicates bottom field
print "bottom frame first, skipping\n";
$bff = 1;
last;
}
}
}
if ( $bff == 1 ) {
############## found a bottom field when wanting a top, reset trigger and advance
############## a little less than a field's worth of samples
$bff = 0;
$bin_file_pos += 500000;
} else {
############## found a top field, read in a bit more than a frame's worth of data
$bin_file_pos = $bin_file_pos - 7350; # roughly start of frame if vsync detected was 7220
seek $INDATA, $bin_file_pos, SEEK_SET;
my $success2 = read $INDATA, $data_chunk_2, 1337592; # was 1337092, then 1337592
die $! if not defined $success2;
last if not $success2;
###############################This block of code outputs the raw frame and makes a png of it##
###############################for troubleshooting and checking aligment and that kind of stuff
if ($DEBUG) {
open my $OUTDATA, '>', "/tmp/out_$filecount.u8" or die;
binmode $OUTDATA;
print $OUTDATA $data_chunk_2;
close $OUTDATA;
$tmpvar = `ffmpeg -f rawvideo -pix_fmt gray8 -video_size 2542x526 -i /tmp/out_$filecount.u8 -vf "scale=1280x1052" /tmp/picstemp/out_$filecount.png 2>/dev/null`;
}
###############################################################################################
$filecount = $filecount + 1;
@frame = split //, $data_chunk_2;
if ( scalar @frame < 1337592 ) {
print "too small chunk\n";
last;
}
$linecount = 1;
for ( $i = 1337591 ; $i-- > 1 ; ) {
##### move backwards through the frame starting about way enough in to not catch the BR Pulses
$data_chunk_1 = unpack "C", $frame[$i];
if ( $data_chunk_1 < 30 ) {
#### found the right edge of the HSYNC PULSE, move backwards to find the start
while (1) {
$i = $i - 1;
$data_chunk_1 = unpack "C", $frame[$i];
if ( $data_chunk_1 > 30 ) {
############## found the start of the HSYNC PULSE
last;
}
}
$i = $i + 2; ################ Move forward 2 samples back into the HSYNC
########################## below, just shove 2542 samples (a lines worth @40msps) into frame line array
########################## as long as we are 2542 samples back, if not, move back 100 and try again
if ((1337591 - $i) < 2542 ) {
$i = $i - 100;
} else {
$frame_line[$linecount] = $frame[$i];
for $ii ( 1 .. 2541 ) {
$tmpvar = $frame[ $ii + $i ];
if ( !length $tmpvar ) {
print "empty line data Field: $filecount line: $linecount\n";
last;
} else {
$frame_line[$linecount] .= $tmpvar;
}
}
$linecount = $linecount + 1;
$i = $i - 2100; #### move backwards 4/5ths of a line to start looking for the next HSYNC
}
}
if ($linecount == 527 ) {
last;
}
}
$hit = 0;
$tmpvar = "";
if ($linecount < 527 ) {
$hit=1;
}
#print $linecount . "\n";
############################### loop backwards to store the backwards lines forward into the outfile
for ( $i = scalar @frame_line ; $i-- > 1 ; ) {
if ( length( $frame_line[$i] ) >= 2542 ) {
$tmpvar .= $frame_line[$i];
} else {
print "bad line length $i " . length( $frame_line[$i] ) . "\n";
$hit = 1;
}
}
if ($hit) {
print "bad line count $linecount or bad line data, skipped field $filecount\n";
$bin_file_pos += 500000;
$check_first_field = 0;
} else {
##############################################################################################
###############################This block of code outputs the raw frame and makes a png of it##
###############################for troubleshooting and checking aligment and that kind of stuff
if ($DEBUG) {
open my $OUTDATA, '>', "/tmp/out_aligned_$filecount.u8" or die;
binmode $OUTDATA;
print $OUTDATA $tmpvar;
close $OUTDATA;
$tmpvar = `ffmpeg -f rawvideo -pix_fmt gray8 -video_size 2542x526 -i /tmp/out_aligned_$filecount.u8 -vf "scale=1280x1052" /tmp/picstemp/aligned_out_$filecount.png 2>/dev/null`;
}
###############################################################################################
print $OUTDATAFULL $tmpvar;
}
@frame_line = "";
#$bin_file_pos = $bin_file_pos + 1338363 + 100; ########### move foreward about a frame
$bin_file_pos = $bin_file_pos + 1335363 + 100; ########### move foreward about a frame
print "Processed: $filecount Frames...\n";
}
}
$bin_file_pos = $bin_file_pos + 10;
}
close;
#$tmpvar = `ls /tmp |grep ^out_ |xargs -I \$ bash -c 'ffmpeg -f rawvideo -pix_fmt gray8 -video_size 2542x526 -i /tmp/\$ -vf "scale=1280x1024" /tmp/picstemp/\$\%00d.png'`;