-
Notifications
You must be signed in to change notification settings - Fork 5
/
bittables.py
195 lines (179 loc) · 11 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
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
from array import array
from random import randint,seed
import types
coefTokenMapping01 = { '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
levelMapping = { '1':0, '01':1, '001':2, '0001':3, '00001':4, '000001':5,
'0000001':6, '00000001':7, '000000001':8, '0000000001':9, '00000000001':10,
'000000000001':11, '0000000000001':12, '00000000000001':13,
'000000000000001':14, '0000000000000001':15 } # Tab 9-6, page 180
runBeforeMapping = {} # Table 9-10, page 182
runBeforeMapping[1] = { '1':0, '0': 1 }
runBeforeMapping[2] = { '1':0, '01': 1, '00':2 }
runBeforeMapping[3] = { '11':0, '10': 1, '01':2, '00':3 }
runBeforeMapping[4] = { '11':0, '10': 1, '01':2, '001':3, '000':4 }
runBeforeMapping[5] = { '11':0, '10': 1, '011':2, '010':3, '001':4, '000':5 }
runBeforeMapping[6] = { '11':0, '000': 1, '001':2, '011':3, '010':4, '101':5, '100':6 }
runBeforeMapping[7] = { '111':0, '110': 1, '101':2, '100':3, '011':4, '010':5, '001':6, '0001':7,
'00001':8, '000001':9, '0000001':10, '00000001':11, '000000001':12, '0000000001':13, '00000000001':14}
# ChromaDC
totalZerosMappingDC = {} # Table 9-7, page 181
totalZerosMappingDC[1] = { '1':0, '01':1, '001':2, '000':3 }
totalZerosMappingDC[2] = { '1':0, '01':1, '00':2 }
totalZerosMappingDC[3] = { '1':0, '0':1 }
totalZerosMapping = {}
totalZerosMapping[1] = { '1':0, '011':1, '010':2, '0011':3, '0010':4, '00011':5, '00010':6, '000011':7,
'000010':8, '0000011':9, '0000010':10, '00000011':11, '00000010':12, '000000011':13, '000000010':14, '000000001':15 }
totalZerosMapping[2] = { '111':0, '110':1, '101':2, '100':3, '011':4, '0101':5, '0100':6,
'0011':7, '0010':8, '00011':9, '00010':10, '000011':11, '000010':12, '000001':13, '000000':14}
totalZerosMapping[3] = { '0101':0, '111':1, '110':2, '101':3, '0100':4, '0011':5, '100':6,
'011':7, '0010':8, '00011':9, '00010':10, '000001':11, '00001':12, '000000':13 }
totalZerosMapping[4] = { '00011':0, '111':1, '0101':2, '0100':3, '110':4, '101':5, '100':6,
'0011':7, '011':8, '0010':9, '00010':10, '00001':11, '00000':12 }
totalZerosMapping[5] = { '0101':0, '0100':1, '0011':2, '111':3, '110':4, '101':5, '100':6,
'011':7, '0010':8, '00001':9, '0001':10, '00000':11 }
totalZerosMapping[6] = { '000001':0, '00001':1, '111':2, '110':3, '101':4, '100':5, '011':6,
'010':7, '0001':8, '001':9, '000000':10 }
totalZerosMapping[7] = { '000001':0, '00001':1, '101':2, '100':3, '011':4, '11':5, '010':6, '0001':7, '001':8, '000000':9 }
totalZerosMapping[8] = { '000001':0, '0001':1, '00001':2, '011':3, '11':4, '10':5, '010':6, '001':7, '000000':8 }
totalZerosMapping[9] = { '000001':0, '000000':1, '0001':2, '11':3, '10':4, '001':5, '01':6, '00001':7 }
totalZerosMapping[10] = { '00001':0, '00000':1, '001':2, '11':3, '10':4, '01':5, '0001':6 }
totalZerosMapping[11] = { '0000':0, '0001':1, '001':2, '010':3, '1':4, '011':5 }
totalZerosMapping[12] = { '0000':0, '0001':1, '01':2, '1':3, '001':4 }
totalZerosMapping[13] = { '000':0, '001':1, '1':2, '01':3 }
totalZerosMapping[14] = { '00':0, '01':1, '1':2 }
totalZerosMapping[15] = { '0':0, '1':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, val 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] = 2*len(results) + 1
if type(val) == types.TupleType:
results.extend( val )
lenVal = len(val)
else:
results.append( val )
lenVal = 1
return automat, results, lenVal
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(coefTokenMapping01)
#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