annotate pytouhou/utils/bitstream.pyx @ 487:711c75115675

Various netplay-related fixes.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 28 Sep 2013 20:11:41 +0200
parents feecdb4a8928
children 1b532e7dd521
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 0
diff changeset
14
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
15 cdef class BitStream:
367
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
16 cdef public object io
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
17 cdef unsigned int bits
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
18 cdef unsigned char byte
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
19 cdef bytes bytes
252
b5c7369abd7c Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
20
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
21
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
22 def __init__(self, io):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 self.io = io
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
28 def __enter__(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
29 return self
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
30
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
31
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
32 def __exit__(self, type, value, traceback):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
33 return self.io.__exit__(type, value, traceback)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
34
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
35
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
36 def seek(self, offset, whence=0):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 self.io.seek(offset, whence)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
42 def tell(self):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 return self.io.tell()
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
46 def tell2(self):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 return self.io.tell(), self.bits
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
468
feecdb4a8928 Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 417
diff changeset
50 cpdef unsigned char read_bit(self) except? -1:
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 if not self.bits:
367
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
52 self.bytes = self.io.read(1)
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
53 self.byte = (<unsigned char*> self.bytes)[0]
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 self.bits = 8
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 self.bits -= 1
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 return (self.byte >> self.bits) & 0x01
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58
468
feecdb4a8928 Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 417
diff changeset
59 cpdef unsigned int read(self, unsigned int nb_bits) except? -1:
367
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
60 cdef unsigned int value = 0, read = 0
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
61 cdef unsigned int nb_bits2 = nb_bits
252
b5c7369abd7c Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
62
367
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
63 while nb_bits2:
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
64 if not self.bits:
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
65 self.bytes = self.io.read(1)
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
66 self.byte = (<unsigned char*> self.bytes)[0]
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
67 self.bits = 8
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
68 read = self.bits if nb_bits2 > self.bits else nb_bits2
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
69 nb_bits2 -= read
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
70 self.bits -= read
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
71 value |= (self.byte >> self.bits) << nb_bits2
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
72 return value & ((1 << nb_bits) - 1)
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
75 cpdef write_bit(self, bit):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 if self.bits == 8:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 self.io.write(chr(self.byte))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 self.byte &= ~(1 << (7 - self.bits))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 self.byte |= bit << (7 - self.bits)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 self.bits += 1
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
85 def write(self, bits, nb_bits):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 for i in range(nb_bits):
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89
417
efae61ad6efe Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 367
diff changeset
90 def flush(self):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 self.io.write(chr(self.byte))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 self.io.flush()
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95