diff options
Diffstat (limited to 'src/lib/smpte.cpp')
-rw-r--r-- | src/lib/smpte.cpp | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/src/lib/smpte.cpp b/src/lib/smpte.cpp new file mode 100644 index 0000000..537834c --- /dev/null +++ b/src/lib/smpte.cpp | |||
@@ -0,0 +1,221 @@ | |||
1 | #include "smpte.h" | ||
2 | #include "memutils.h" | ||
3 | #define SMPTE_DEBUG 0 | ||
4 | SMPTEgenerator::SMPTEgenerator(__uint32 p_samplerate) | ||
5 | { | ||
6 | #if (SMPTE_DEBUG==1) | ||
7 | cout << "Construct SMPTE generator with samrate=" << p_samplerate << endl; | ||
8 | #endif | ||
9 | // this->lastframe=-1; | ||
10 | this->prevbitnum=-1; | ||
11 | this->prevhalfbit=-1; | ||
12 | this->haveframe=0; | ||
13 | this->prevoutval=0; | ||
14 | this->framerate=30; // 30 is default for HD24. | ||
15 | this->bitsperframe=80; | ||
16 | this->nondrop=1; | ||
17 | this->setsamplerate(p_samplerate); | ||
18 | this->smpteword=(short*)memutils::mymalloc("smptegenerator::smptegenerator",80,sizeof(short)); | ||
19 | // pre-fill sync word | ||
20 | for (int i=0;i<64;i++) | ||
21 | { | ||
22 | this->smpteword[i]=0; | ||
23 | } | ||
24 | this->smpteword[64]=0; | ||
25 | this->smpteword[65]=0; | ||
26 | this->smpteword[66]=1; | ||
27 | this->smpteword[67]=1; | ||
28 | this->smpteword[68]=1; | ||
29 | this->smpteword[69]=1; | ||
30 | this->smpteword[70]=1; | ||
31 | this->smpteword[71]=1; | ||
32 | this->smpteword[72]=1; | ||
33 | this->smpteword[73]=1; | ||
34 | this->smpteword[74]=1; | ||
35 | this->smpteword[75]=1; | ||
36 | this->smpteword[76]=1; | ||
37 | this->smpteword[77]=1; | ||
38 | this->smpteword[78]=0; | ||
39 | this->smpteword[79]=1; | ||
40 | } | ||
41 | |||
42 | SMPTEgenerator::~SMPTEgenerator() | ||
43 | { | ||
44 | if (this==NULL) | ||
45 | { | ||
46 | return; | ||
47 | } | ||
48 | if (smpteword==NULL) | ||
49 | { | ||
50 | return; | ||
51 | } | ||
52 | memutils::myfree("SMPTE",smpteword); | ||
53 | smpteword=NULL; | ||
54 | } | ||
55 | void SMPTEgenerator::recalcrates() | ||
56 | { | ||
57 | |||
58 | this->bitspersecond=bitsperframe*framerate; | ||
59 | |||
60 | } | ||
61 | |||
62 | void SMPTEgenerator::setsamplerate(__uint32 p_samplerate) | ||
63 | { | ||
64 | this->samplerate=p_samplerate; | ||
65 | this->recalcrates(); | ||
66 | return; | ||
67 | } | ||
68 | |||
69 | void SMPTEgenerator::setframerate(__uint32 p_framerate) | ||
70 | { | ||
71 | this->framerate=p_framerate; | ||
72 | this->recalcrates(); | ||
73 | } | ||
74 | |||
75 | void SMPTEgenerator::fillword(int hour,int minute,int second,int frame) | ||
76 | { | ||
77 | #if (SMPTE_DEBUG==1) | ||
78 | cout << "fill word for " | ||
79 | << hour << ":" << minute << ":" << second <<"."<<frame << endl; | ||
80 | #endif | ||
81 | int frameunits=(frame%10); | ||
82 | int frametens=(int)((frame-frameunits)/10); | ||
83 | |||
84 | int secondsunits=(second%10); | ||
85 | int secondstens=(int)((second-secondsunits)/10); | ||
86 | |||
87 | int minuteunits=(minute%10); | ||
88 | int minutetens=(int)((minute-minuteunits)/10); | ||
89 | |||
90 | int hourunits=(hour%10); | ||
91 | int hourtens=(int)((hour-hourunits)/10); | ||
92 | |||
93 | |||
94 | this->smpteword[0]=((frameunits ) & 1); | ||
95 | this->smpteword[1]=((frameunits>>1) & 1); | ||
96 | this->smpteword[2]=((frameunits>>2) & 1); | ||
97 | this->smpteword[3]=((frameunits>>3) & 1); | ||
98 | // this->smpteword[4]=0; | ||
99 | // this->smpteword[5]=0; | ||
100 | // this->smpteword[6]=0; | ||
101 | // this->smpteword[7]=0; | ||
102 | this->smpteword[8]=((frametens) & 1); | ||
103 | this->smpteword[9]=((frametens>>1) & 1); | ||
104 | // this->smpteword[10]=0; | ||
105 | // this->smpteword[11]=0; | ||
106 | // this->smpteword[12]=0; | ||
107 | // this->smpteword[13]=0; | ||
108 | // this->smpteword[14]=0; | ||
109 | // this->smpteword[15]=0; | ||
110 | this->smpteword[16]=((secondsunits ) & 1); | ||
111 | this->smpteword[17]=((secondsunits>>1) & 1); | ||
112 | this->smpteword[18]=((secondsunits>>2) & 1); | ||
113 | this->smpteword[19]=((secondsunits>>3) & 1); | ||
114 | // this->smpteword[20]=0; | ||
115 | // this->smpteword[21]=0; | ||
116 | // this->smpteword[22]=0; | ||
117 | // this->smpteword[23]=0; | ||
118 | this->smpteword[24]=((secondstens )&1); | ||
119 | this->smpteword[25]=((secondstens>>1)&1); | ||
120 | this->smpteword[26]=((secondstens>>2)&1); | ||
121 | this->smpteword[27]=0; // is biphasemark, filled in later | ||
122 | // but reset here to prevent its influence | ||
123 | |||
124 | // this->smpteword[28]=0; | ||
125 | // this->smpteword[29]=0; | ||
126 | // this->smpteword[30]=0; | ||
127 | // this->smpteword[31]=0; | ||
128 | this->smpteword[32]=((minuteunits ) & 1); | ||
129 | this->smpteword[33]=((minuteunits>>1) & 1); | ||
130 | this->smpteword[34]=((minuteunits>>2) & 1); | ||
131 | this->smpteword[35]=((minuteunits>>3) & 1); | ||
132 | // this->smpteword[36]=0; | ||
133 | // this->smpteword[37]=0; | ||
134 | // this->smpteword[38]=0; | ||
135 | // this->smpteword[39]=0; | ||
136 | this->smpteword[40]=((minutetens ) & 1); | ||
137 | this->smpteword[41]=((minutetens>>1) & 1); | ||
138 | this->smpteword[42]=((minutetens>>2) & 1); | ||
139 | // this->smpteword[43]=0; # binary group flag bit | ||
140 | // this->smpteword[44]=0; | ||
141 | // this->smpteword[45]=0; | ||
142 | // this->smpteword[46]=0; | ||
143 | // this->smpteword[47]=0; | ||
144 | this->smpteword[48]=((hourunits )&1); | ||
145 | this->smpteword[49]=((hourunits>>1)&1); | ||
146 | this->smpteword[50]=((hourunits>>2)&1); | ||
147 | this->smpteword[51]=((hourunits>>3)&1); | ||
148 | // this->smpteword[52]=0; | ||
149 | // this->smpteword[53]=0; | ||
150 | // this->smpteword[54]=0; | ||
151 | // this->smpteword[55]=0; | ||
152 | this->smpteword[56]=((hourtens )&1); | ||
153 | this->smpteword[57]=((hourtens>>1)&1); | ||
154 | // this->smpteword[58]=0; // reserved, must be 0 | ||
155 | // this->smpteword[59]=0; // binary group flag bit | ||
156 | // this->smpteword[60]=0; | ||
157 | // this->smpteword[61]=0; | ||
158 | // this->smpteword[62]=0; | ||
159 | // this->smpteword[63]=0; | ||
160 | |||
161 | // sync word already set in constructor | ||
162 | unsigned parity = 0; | ||
163 | for (int i=0; i<80; ++i) { | ||
164 | parity += this->smpteword[i]; | ||
165 | } | ||
166 | this->smpteword[27] = (parity & 1); | ||
167 | #if (SMPTE_DEBUG==1) | ||
168 | for (int i=0;i<80;i++) | ||
169 | { | ||
170 | cout << this->smpteword[i]; | ||
171 | } | ||
172 | cout << endl; | ||
173 | #endif | ||
174 | return; | ||
175 | |||
176 | } | ||
177 | int SMPTEgenerator::modulate(int currbitval,int bitnum,int halfbit) | ||
178 | { | ||
179 | if ((bitnum!=this->prevbitnum) || ((halfbit!=this->prevhalfbit) && (currbitval==1))) | ||
180 | { | ||
181 | this->prevoutval=1-(this->prevoutval); | ||
182 | } | ||
183 | this->prevbitnum=bitnum; | ||
184 | this->prevhalfbit=halfbit; | ||
185 | return this->prevoutval; | ||
186 | } | ||
187 | |||
188 | int SMPTEgenerator::getbit(__uint32 insamnum) | ||
189 | { | ||
190 | |||
191 | this->samplesperbit=(int)((this->samplerate)/this->bitspersecond); | ||
192 | this->samplesperframe=(int)((this->samplerate)/framerate); | ||
193 | |||
194 | __uint32 sampleinsecond=insamnum % this->samplerate; | ||
195 | __uint32 currentsecond=(insamnum-sampleinsecond)/(this->samplerate); | ||
196 | __uint32 currenthour=int(currentsecond/3600); | ||
197 | currentsecond-=3600*currenthour; | ||
198 | __uint32 currentminute=int(currentsecond/60); | ||
199 | currentsecond-=60*currentminute; | ||
200 | |||
201 | __uint32 currentframe=(int)(sampleinsecond/this->samplesperframe); | ||
202 | __uint32 sampleinframe=sampleinsecond-(currentframe*this->samplesperframe); | ||
203 | __uint32 bitinframe=(int)(sampleinframe/this->samplesperbit); | ||
204 | __uint32 subbit=(int)(sampleinframe-(bitinframe*this->samplesperbit)); | ||
205 | __uint32 halfbit=(this->samplesperbit/2); | ||
206 | if ((bitinframe==0)&&(subbit==0)) | ||
207 | { | ||
208 | this->haveframe=0; | ||
209 | } | ||
210 | if (this->haveframe==0) | ||
211 | { | ||
212 | this->fillword(currenthour,currentminute,currentsecond,currentframe); | ||
213 | this->haveframe=1; | ||
214 | } | ||
215 | |||
216 | if (subbit<halfbit) { | ||
217 | return this->modulate(smpteword[bitinframe],bitinframe,0); | ||
218 | } | ||
219 | return this->modulate(smpteword[bitinframe],bitinframe,1); | ||
220 | } | ||
221 | |||