diff options
Diffstat (limited to 'scripts/encode_smpte.pl')
-rwxr-xr-x | scripts/encode_smpte.pl | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/scripts/encode_smpte.pl b/scripts/encode_smpte.pl new file mode 100755 index 0000000..de8f427 --- /dev/null +++ b/scripts/encode_smpte.pl | |||
@@ -0,0 +1,181 @@ | |||
1 | #!/usr/bin/perl -- | ||
2 | # | ||
3 | #http://en.wikipedia.org/wiki/SMPTE_time_code | ||
4 | sub bitnum { | ||
5 | my ($val,$bit)=@_; | ||
6 | while (1==1) { | ||
7 | if ($bit<=0) { | ||
8 | return $val % 2; | ||
9 | } | ||
10 | $val=int($val/2); | ||
11 | $bit--; | ||
12 | } | ||
13 | } | ||
14 | my $lastframe=-1; | ||
15 | my @smpteword; | ||
16 | |||
17 | my $prevbitnum=-1; | ||
18 | my $prevhalfbit=-1; | ||
19 | my $prevoutval=0; | ||
20 | sub smpte_modulate { | ||
21 | my ($currbitval,$bitnum,$halfbit)=@_; | ||
22 | if (($bitnum!=$prevbitnum) || (($halfbit!=$prevhalfbit) && ($currbitval==1))) | ||
23 | { | ||
24 | $prevoutval=1-$prevoutval; | ||
25 | } | ||
26 | $prevbitnum=$bitnum; | ||
27 | $prevhalfbit=$halfbit; | ||
28 | return $prevoutval; | ||
29 | } | ||
30 | |||
31 | sub smpte_timecodebit { | ||
32 | my ($insamplerate,$framerate,$nondrop,$insamnum)=@_; | ||
33 | my $samnum=$insamnum; | ||
34 | my $samplerate=$insamplerate; | ||
35 | if ($insamplerate!=48000) | ||
36 | { | ||
37 | $samplerate=48000; | ||
38 | $samnum=$insamnum*(48000/$insamplerate); | ||
39 | } | ||
40 | my $sampleinsecond=$samnum % $samplerate; | ||
41 | my $currentsecond=($samnum-$sampleinsecond)/$samplerate; | ||
42 | my $currenthour=int($currentsecond/3600); | ||
43 | $currentsecond-=3600*$currenthour; | ||
44 | my $currentminute=int($currentsecond/60); | ||
45 | $currentsecond-=60*$currentminute; | ||
46 | my $bitsperframe=80; | ||
47 | my $bitspersecond=$bitsperframe*$framerate; | ||
48 | my $samplesperbit=$samplerate/$bitspersecond; | ||
49 | my $samplesperframe=$samplerate/$framerate; | ||
50 | my $currentframe=int($sampleinsecond/$samplesperframe); | ||
51 | my $sampleinframe=$sampleinsecond-($currentframe*$samplesperframe); | ||
52 | my $bitinframe=int($sampleinframe/$samplesperbit); | ||
53 | my $subbit=int($sampleinframe-($bitinframe*$samplesperbit)); | ||
54 | my $halfbit=($samplesperbit/2); | ||
55 | |||
56 | my $frameunits=($currentframe%10); | ||
57 | my $frametens=int(($currentframe-$frameunits)/10); | ||
58 | |||
59 | my $secondsunits=($currentsecond%10); | ||
60 | my $secondstems=int(($currentsecond-$secondunits)/10); | ||
61 | |||
62 | my $minuteunits=($currentminute%10); | ||
63 | my $minutetens=int(($currentminute-$minuteunits)/10); | ||
64 | |||
65 | my $hourunits=($currenthour%10); | ||
66 | my $hourtens=int(($currenthour-$hourunits)/10); | ||
67 | |||
68 | $smpteword[0]=bitnum($frameunits,0); | ||
69 | $smpteword[1]=bitnum($frameunits,1); | ||
70 | $smpteword[2]=bitnum($frameunits,2); | ||
71 | $smpteword[3]=bitnum($frameunits,3); | ||
72 | $smpteword[4]=0; | ||
73 | $smpteword[5]=0; | ||
74 | $smpteword[6]=0; | ||
75 | $smpteword[7]=0; | ||
76 | $smpteword[8]=bitnum($frametens,0); | ||
77 | $smpteword[9]=bitnum($frametens,1); | ||
78 | $smpteword[10]=0; | ||
79 | $smpteword[11]=0; | ||
80 | $smpteword[12]=0; | ||
81 | $smpteword[13]=0; | ||
82 | $smpteword[14]=0; | ||
83 | $smpteword[15]=0; | ||
84 | $smpteword[16]=bitnum($secondsunits,0); | ||
85 | $smpteword[17]=bitnum($secondsunits,1); | ||
86 | $smpteword[18]=bitnum($secondsunits,2); | ||
87 | $smpteword[19]=bitnum($secondsunits,3); | ||
88 | $smpteword[20]=0; | ||
89 | $smpteword[21]=0; | ||
90 | $smpteword[22]=0; | ||
91 | $smpteword[23]=0; | ||
92 | $smpteword[24]=bitnum($secondstens,0); | ||
93 | $smpteword[25]=bitnum($secondstens,1); | ||
94 | $smpteword[26]=bitnum($secondstens,2); | ||
95 | $smpteword[27]=0; #biphasemark, fill in later | ||
96 | $smpteword[28]=0; | ||
97 | $smpteword[29]=0; | ||
98 | $smpteword[30]=0; | ||
99 | $smpteword[31]=0; | ||
100 | $smpteword[32]=bitnum($minuteunits,0); | ||
101 | $smpteword[33]=bitnum($minuteunits,1); | ||
102 | $smpteword[34]=bitnum($minuteunits,2); | ||
103 | $smpteword[35]=bitnum($minuteunits,3); | ||
104 | $smpteword[36]=0; | ||
105 | $smpteword[37]=0; | ||
106 | $smpteword[38]=0; | ||
107 | $smpteword[39]=0; | ||
108 | $smpteword[40]=bitnum($minutetens,0); | ||
109 | $smpteword[41]=bitnum($minutetens,1); | ||
110 | $smpteword[42]=bitnum($minutetens,2); | ||
111 | $smpteword[43]=0; # binary group flag bit | ||
112 | $smpteword[44]=0; | ||
113 | $smpteword[45]=0; | ||
114 | $smpteword[46]=0; | ||
115 | $smpteword[47]=0; | ||
116 | $smpteword[48]=bitnum($hourunits,0); | ||
117 | $smpteword[49]=bitnum($hourunits,1); | ||
118 | $smpteword[50]=bitnum($hourunits,2); | ||
119 | $smpteword[51]=bitnum($hourunits,3); | ||
120 | $smpteword[52]=0; | ||
121 | $smpteword[53]=0; | ||
122 | $smpteword[54]=0; | ||
123 | $smpteword[55]=0; | ||
124 | $smpteword[56]=bitnum($hourtens,0); | ||
125 | $smpteword[57]=bitnum($hourtens,1); | ||
126 | $smpteword[58]=0; # reserved, must be 0 | ||
127 | $smpteword[59]=0; # binary group flag bit | ||
128 | $smpteword[60]=0; | ||
129 | $smpteword[61]=0; | ||
130 | $smpteword[62]=0; | ||
131 | $smpteword[63]=0; | ||
132 | |||
133 | # sync word follows | ||
134 | $smpteword[64]=0; | ||
135 | $smpteword[65]=0; | ||
136 | $smpteword[66]=1; | ||
137 | $smpteword[67]=1; | ||
138 | $smpteword[68]=1; | ||
139 | $smpteword[69]=1; | ||
140 | $smpteword[70]=1; | ||
141 | $smpteword[71]=1; | ||
142 | $smpteword[72]=1; | ||
143 | $smpteword[73]=1; | ||
144 | $smpteword[74]=1; | ||
145 | $smpteword[75]=1; | ||
146 | $smpteword[76]=1; | ||
147 | $smpteword[77]=1; | ||
148 | $smpteword[78]=0; | ||
149 | $smpteword[79]=1; | ||
150 | my $parity=0; | ||
151 | for ($i=0;$i<=79;$i++) { | ||
152 | $parity+=$smpteword[$i]; | ||
153 | } | ||
154 | $smpteword[27]=$parity & 1; | ||
155 | |||
156 | return $smpteword[$bitinframe]; | ||
157 | # Enable this to also perform bit modulation: | ||
158 | # ########################### | ||
159 | if ($subbit<$halfbit) { | ||
160 | return smpte_modulate($smpteword[$bitinframe],$bitinframe,0); | ||
161 | } | ||
162 | return smpte_modulate($smpteword[$bitinframe],$bitinframe,1); | ||
163 | ################################## | ||
164 | # or only use this to return the raw SMPTE bits. | ||
165 | } | ||
166 | |||
167 | my $i; | ||
168 | my $samplerate=48000; | ||
169 | my $framerate=30; | ||
170 | $startsam=0; #($samplerate/$framerate); | ||
171 | my $nondrop=true; | ||
172 | for ($samnum=$startsam;$samnum<1500000;$samnum+=10) { | ||
173 | $x=smpte_timecodebit($samplerate,$framerate,$nondrop,$samnum); | ||
174 | if ($x==0) { | ||
175 | print "0"; #chr(0); | ||
176 | } else { | ||
177 | print "1"; #chr(255); | ||
178 | } | ||
179 | |||
180 | } | ||
181 | |||