-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysnc.py
174 lines (133 loc) · 5.46 KB
/
pysnc.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
# This file wraps APIs from libspasenc.so in Python
from __future__ import division
from math import ceil
# from ctypes import *
from ctypes import cdll, c_int, c_ubyte, c_double, c_long, c_longlong, c_char_p, POINTER, sizeof, byref, cast, memmove, Structure
# code types
RAND_SNC = 0
BAND_SNC = 1
RLNC = 9
# decoder types
GG_DECODER = 0
OA_DECODER = 1
BD_DECODER = 2
CBD_DECODER = 3
PP_DECODER = 4
# scheduling algorithm of recoder
TRIV_SCHED = 0
RAND_SCHED = 1
RAND_SCHED_SYS = 2
MLPI_SCHED = 3
MLPI_SCHED_SYS = 4
NURAND_SCHED = 5
class snc_context(Structure):
pass
class snc_packet(Structure):
_fields_ = [("gid", c_int),
("ucid", c_int),
("coes", POINTER(c_ubyte)),
("syms", POINTER(c_ubyte))]
def serialize(self, size_g, size_p, bnc):
""" Serialize an SNC packet to a binary byte string
"""
pktstr = bytearray()
pktstr += c_int(self.gid)
pktstr += c_int(self.ucid)
if bnc == 1:
ce_len = int(ceil(size_g/8)) # Python 2/3 compatibility
else:
ce_len = size_g
pktstr += cast(self.coes, POINTER(c_ubyte * ce_len))[0]
pktstr += cast(self.syms, POINTER(c_ubyte * size_p))[0]
return pktstr
def deserialize(self, pktstr, size_g, size_p, bnc):
""" Deserialize a byte stream and fill in an existing snc_packet
instance
"""
self.gid = c_int.from_buffer_copy(pktstr)
self.ucid = c_int.from_buffer_copy(pktstr, sizeof(c_int))
if bnc == 1:
ce_len = int(ceil(size_g/8)) # Python 2/3 compatibility
else:
ce_len = size_g
coes = (c_ubyte * ce_len).from_buffer_copy(pktstr, 2*sizeof(c_int))
memmove(self.coes, coes, ce_len)
syms = (c_ubyte * size_p).from_buffer_copy(pktstr, 2*sizeof(c_int)+ce_len)
memmove(self.syms, syms, size_p)
class snc_parameters(Structure):
_fields_ = [("datasize", c_long),
("pcrate", c_double),
("size_b", c_int),
("size_g", c_int),
("size_p", c_int),
("type", c_int),
("bpc", c_int),
("bnc", c_int),
("sys", c_int),
("seed", c_int)]
class snc_decoder(Structure):
pass
class snc_buffer(Structure):
pass
snc = cdll.LoadLibrary("libsparsenc.so")
##########################
# Wrap encoder functions #
##########################
snc.snc_create_enc_context.argtypes = [POINTER(c_ubyte), POINTER(snc_parameters)]
snc.snc_create_enc_context.restype = POINTER(snc_context)
snc.snc_get_parameters.argtypes = [POINTER(snc_context)]
snc.snc_get_parameters.restype = POINTER(snc_parameters)
snc.snc_load_file_to_context.argtypes = [c_char_p, c_long, POINTER(snc_context)]
snc.snc_load_file_to_context.restype = c_int
snc.snc_free_enc_context.argtypes = [POINTER(snc_context)]
snc.snc_free_enc_context.restype = None
snc.snc_recover_data.argtypes = [POINTER(snc_context)]
snc.snc_recover_data.restype = POINTER(c_ubyte)
snc.snc_free_recovered.argtypes = [POINTER(c_ubyte)]
snc.snc_free_recovered.restype = None
snc.snc_recover_to_file.argtypes = [c_char_p, POINTER(snc_context)]
snc.snc_recover_to_file.restype = c_long
snc.snc_alloc_empty_packet.argtypes = [POINTER(snc_parameters)]
snc.snc_alloc_empty_packet.restype = POINTER(snc_packet)
snc.snc_generate_packet.argtypes = [POINTER(snc_context)]
snc.snc_generate_packet.restype = POINTER(snc_packet)
snc.snc_generate_packet_im.argtypes = [POINTER(snc_context), POINTER(snc_packet)]
snc.snc_generate_packet_im.restype = c_int
snc.snc_free_packet.argtypes = [POINTER(snc_packet)]
snc.snc_free_packet.restype = None
snc.print_code_summary.argtypes = [POINTER(snc_context), c_double, c_double]
snc.print_code_summary.restype = None
##########################
# Wrap decoder functions #
##########################
snc.snc_create_decoder.argtypes = [POINTER(snc_parameters), c_int]
snc.snc_create_decoder.restype = POINTER(snc_decoder)
snc.snc_get_enc_context.argtypes = [POINTER(snc_decoder)]
snc.snc_get_enc_context.restype = POINTER(snc_context)
snc.snc_process_packet.argtypes = [POINTER(snc_decoder), POINTER(snc_packet)]
snc.snc_process_packet.restype = None
snc.snc_decoder_finished.argtypes = [POINTER(snc_decoder)]
snc.snc_decoder_finished.restype = c_int
snc.snc_decode_overhead.argtypes = [POINTER(snc_decoder)]
snc.snc_decode_overhead.restype = c_double
snc.snc_decode_cost.argtypes = [POINTER(snc_decoder)]
snc.snc_decode_cost.restype = c_double
snc.snc_free_decoder.argtypes = [POINTER(snc_decoder)]
snc.snc_free_decoder.restype = None
snc.snc_save_decoder_context.argtypes = [POINTER(snc_decoder), c_char_p]
snc.snc_save_decoder_context.restype = c_long
snc.snc_restore_decoder.argtypes = [c_char_p]
snc.snc_restore_decoder.restype = POINTER(snc_decoder)
#################################
# Wrap recoder/buffer functions #
#################################
snc.snc_create_buffer.argtypes = [POINTER(snc_parameters), c_int]
snc.snc_create_buffer.restype = POINTER(snc_buffer)
snc.snc_buffer_packet.argtypes = [POINTER(snc_buffer), POINTER(snc_packet)]
snc.snc_buffer_packet.restype = None
snc.snc_recode_packet.argtypes = [POINTER(snc_buffer), c_int]
snc.snc_recode_packet.restype = POINTER(snc_packet)
snc.snc_recode_packet_im.argtypes = [POINTER(snc_buffer), POINTER(snc_packet), c_int]
snc.snc_recode_packet_im.restype = c_int
snc.snc_free_buffer.argtypes = [POINTER(snc_buffer)]
snc.snc_free_buffer.restype = None