forked from robotika/h264-drone-vision
/
bittables.py
143 lines (133 loc) · 7.97 KB
/
bittables.py
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
from array import array
from random import randint,seed
coefTokenMapping1 = { '1':(0,0), '000101':(0,1), '01':(1,1), '00000111':(0,2), '000100':(1,2), '001':(2,2),
'000000111':(0,3), '00000110':(1,3), '0000101':(2,3), '00011':(3,3), '0000000111':(0,4), '000000110':(1,4),
'00000101':(2,4), '000011':(3,4), '00000000111':(0,5), '0000000110':(1,5), '000000101':(2,5), '0000100':(3,5),
'0000000001111':(0,6), '00000000110':(1,6), '0000000101':(2,6), '00000100':(3,6), '0000000001011':(0,7),
'0000000001110':(1,7), '00000000101':(2,7), '000000100':(3,7), '0000000001000':(0,8), '0000000001010':(1,8),
'0000000001101':(2,8), '0000000100':(3,8), '00000000001111':(0,9), '00000000001110':(1,9), '0000000001001':(2,9),
'00000000100':(3,9), '00000000001011':(0,10), '00000000001010':(1,10), '00000000001101':(2,10), '0000000001100':(3,10),
'000000000001111':(0,11), '000000000001110':(1,11), '00000000001001':(2,11), '00000000001100':(3,11),
'000000000001011':(0,12), '000000000001010':(1,12), '000000000001101':(2,12), '00000000001000':(3,12),
'0000000000001111':(0,13), '000000000000001':(1,13), '000000000001001':(2,13), '000000000001100':(3,13),
'0000000000001011':(0,14), '0000000000001110':(1,14), '0000000000001101':(2,14), '000000000001000':(3,14),
'0000000000000111':(0,15), '0000000000001010':(1,15), '0000000000001001':(2,15), '0000000000001100':(3,15),
'0000000000000100':(0,16), '0000000000000110':(1,16), '0000000000000101':(2,16), '0000000000001000':(3,16) } # 0 <= nC < 2
coefTokenMapping23 = { '11':(0,0), '001011':(0,1), '10':(1,1), '000111':(0,2), '00111':(1,2), '011':(2,2),
'0000111':(0,3), '001010':(1,3), '001001':(2,3), '0101':(3,3), '00000111':(0,4), '000110':(1,4),
'000101':(2,4), '0100':(3,4), '00000100':(0,5), '0000110':(1,5), '0000101':(2,5), '00110':(3,5),
'000000111':(0,6), '00000110':(1,6), '00000101':(2,6), '001000':(3,6), '00000001111':(0,7),
'000000110':(1,7), '000000101':(2,7), '000100':(3,7), '00000001011':(0,8), '00000001110':(1,8),
'00000001101':(2,8), '0000100':(3,8), '000000001111':(0,9), '00000001010':(1,9), '00000001001':(2,9),
'000000100':(3,9), '000000001011':(0,10), '000000001110':(1,10), '000000001101':(2,10), '00000001100':(3,10),
'000000001000':(0,11), '000000001010':(1,11), '000000001001':(2,11), '00000001000':(3,11), '0000000001111':(0,12),
'0000000001110':(1,12), '0000000001101':(2,12), '000000001100':(3,12), '0000000001011':(0,13),
'0000000001010':(1,13), '0000000001001':(2,13), '0000000001100':(3,13), '0000000000111':(0,14),
'00000000001011':(1,14), '0000000000110':(2,14), '0000000001000':(3,14), '00000000001001':(0,15),
'00000000001000':(1,15), '00000000001010':(2,15), '0000000000001':(3,15), '00000000000111':(0,16),
'00000000000110':(1,16), '00000000000101':(2,16), '00000000000100':(3,16) } # 2 <= nC < 4
coefTokenMapping4567 = { '1111':(0,0), '001111':(0,1), '1110':(1,1), '001011':(0,2), '01111':(1,2), '1101':(2,2),
'001000':(0,3), '01100':(1,3), '01110':(2,3), '1100':(3,3), '0001111':(0,4), '01010':(1,4), '01011':(2,4),
'1011':(3,4), '0001011':(0,5), '01000':(1,5), '01001':(2,5), '1010':(3,5), '0001001':(0,6), '001110':(1,6),
'001101':(2,6), '1001':(3,6), '0001000':(0,7), '001010':(1,7), '001001':(2,7), '1000':(3,7), '00001111':(0,8),
'0001110':(1,8), '0001101':(2,8), '01101':(3,8), '00001011':(0,9), '00001110':(1,9), '0001010':(2,9), '001100':(3,9),
'000001111':(0,10), '00001010':(1,10), '00001101':(2,10), '0001100':(3,10), '000001011':(0,11), '000001110':(1,11),
'00001001':(2,11), '00001100':(3,11), '000001000':(0,12), '000001010':(1,12), '000001101':(2,12), '00001000':(3,12),
'0000001101':(0,13), '000000111':(1,13), '000001001':(2,13), '000001100':(3,13), '0000001001':(0,14), '0000001100':(1,14),
'0000001011':(2,14), '0000001010':(3,14), '0000000101':(0,15), '0000001000':(1,15), '0000000111':(2,15),
'0000000110':(3,15), '0000000001':(0,16), '0000000100':(1,16), '0000000011':(2,16), '0000000010':(3,16) } # 4 <= nC < 8
coefTokenMapping8andUp = { '000011':(0,0), '000000':(0,1), '000001':(1,1), '000100':(0,2), '000101':(1,2), '000110':(2,2),
'001000':(0,3), '001001':(1,3), '001010':(2,3), '001011':(3,3), '001100':(0,4), '001101':(1,4), '001110':(2,4),
'001111':(3,4), '010000':(0,5), '010001':(1,5), '010010':(2,5), '010011':(3,5), '010100':(0,6), '010101':(1,6),
'010110':(2,6), '010111':(3,6), '011000':(0,7), '011001':(1,7), '011010':(2,7), '011011':(3,7), '011100':(0,8),
'011101':(1,8), '011110':(2,8), '011111':(3,8), '100000':(0,9), '100001':(1,9), '100010':(2,9), '100011':(3,9),
'100100':(0,10), '100101':(1,10), '100110':(2,10), '100111':(3,10), '101000':(0,11), '101001':(1,11), '101010':(2,11),
'101011':(3,11), '101100':(0,12), '101101':(1,12), '101110':(2,12), '101111':(3,12), '110000':(0,13), '110001':(1,13),
'110010':(2,13), '110011':(3,13), '110100':(0,14), '110101':(1,14), '110110':(2,14), '110111':(3,14), '111000':(0,15),
'111001':(1,15), '111010':(2,15), '111011':(3,15), '111100':(0,16), '111101':(1,16), '111110':(2,16), '111111':(3,16) }
coefTokenMappingOther = { '01':(0,0), '000111':(0,1), '1':(1,1), '000100':(0,2), '000110':(1,2),
'001':(2,2), '000011':(0,3), '0000011':(1,3), '0000010':(2,3), '000101':(3,3),
'000010':(0,4), '00000011':(1,4), '00000010':(2,4), '0000000':(3,4) } # nC == -1
def makeAutomat(mapping):
"""automat is array of integers (signed 16bits)
on position 2*i is stored state transfer from state i if 0 is read
on position 2*i+1 is stored transfer if 1 is read
if transfer & 1 than transfer links to result array
otherwise transfer>>1 is new state, transfers from new states are in automat on positions transfer and transfer+1
in results, the lies on positions transfer-1 and transfer
"""
automat=array('i',[-1,-1])
results=array('i')
for bits, (d1,d2) in mapping.iteritems():
state=0
#make state transfer for all but last bit
for bit in bits[:-1]:
bit=int(bit)
if automat[state|bit]==-1:
automat[state|bit]=len(automat)
automat.extend([-1,-1])
state=automat[state|bit]
assert not state&1
#make last bit link to endstates table
bit=int(bits[-1])
assert automat[state|bit]==-1
automat[state|bit]=len(results)|1
results.extend([d1,d2])
return automat, results
def bitGeneratorWithGlobalCount(data, offset=0):
global Count
c=0
bitlen=-offset
while True:
while bitlen<Count:
c=(c<<8)|ord(data.next())
bitlen+=8
mask=(2**Count)-1
offset=bitlen-Count
bitlen-=Count
yield (c>>offset) & mask
def automat2dot(automat,result):
import subprocess,os
fn='struct.dot'
fo=r'c:\struct.png'
gwpath=r'c:\Program Files\Graphviz2.29'
file=open(fn,'w')
file.write('''
digraph dummy{
ordering=out;
rankdir=LR;
''')
for i,transfer in enumerate(automat):
state=(i>>1)<<1
if transfer&1:
end="e%i"%transfer
else:
end="%i"%transfer
file.write('"%i"->"%s" [label="%i"]\n'%(state,end,i&1))
for i,(d1,d2) in enumerate(zip(result[::2],result[1::2])):
file.write('"e%i" [shape=box; label="(%i, %i)"]\n'%((i<<1)+1, d1, d2))
file.write('"e-1" [shape=box; label="Not defined"]\n')
file.write('}')
file.close()
subprocess.call([os.path.join(gwpath,r"bin\dot.exe"), "-Tpng","-o"+fo, fn])
subprocess.Popen([r"c:\Program Files\IrfanView\i_view32.exe", fo])
def randGen():
seed(8)
while True:
yield chr(randint(0,0xff))
if __name__=="__main__":
global Count
Count=1
r=randGen()
b=bitGeneratorWithGlobalCount(r)
mapTable, endstates=makeAutomat(coefTokenMapping1)
#automat2dot(mapTable, endstates)
state=0
res=[]
while True:
bit=b.next()
state=mapTable[state | bit]
if state&1:
res.append((endstates[state-1],endstates[state]))
print res[-1]
state=0