… | |
… | |
26 | |
26 | |
27 | void **b_addr; |
27 | void **b_addr; |
28 | void **b_mmap; |
28 | void **b_mmap; |
29 | int *b_sz; |
29 | int *b_sz; |
30 | SV **b_sv; |
30 | SV **b_sv; |
|
|
31 | int **idx; /* the decoding indices */ |
31 | } *Algorithm__FEC; |
32 | } *Algorithm__FEC; |
32 | |
33 | |
33 | static void |
34 | static void |
34 | chk_array (SV *sv, int size, const char *func, const char *var) |
35 | chk_array (SV *sv, int size, const char *func, const char *var) |
35 | { |
36 | { |
… | |
… | |
53 | |
54 | |
54 | Safefree (self->b_addr); self->b_addr = 0; |
55 | Safefree (self->b_addr); self->b_addr = 0; |
55 | Safefree (self->b_mmap); self->b_mmap = 0; |
56 | Safefree (self->b_mmap); self->b_mmap = 0; |
56 | Safefree (self->b_sz ); self->b_sz = 0; |
57 | Safefree (self->b_sz ); self->b_sz = 0; |
57 | Safefree (self->b_sv ); self->b_sv = 0; |
58 | Safefree (self->b_sv ); self->b_sv = 0; |
|
|
59 | Safefree (self->idx ); self->idx = 0; |
58 | } |
60 | } |
59 | |
61 | |
60 | static void |
62 | static void |
61 | realloc_files (struct state *self) |
63 | realloc_files (struct state *self) |
62 | { |
64 | { |
… | |
… | |
179 | RETVAL->ep = encoded_packets; |
181 | RETVAL->ep = encoded_packets; |
180 | OUTPUT: |
182 | OUTPUT: |
181 | RETVAL |
183 | RETVAL |
182 | |
184 | |
183 | void |
185 | void |
184 | set_blocks (self, blocks) |
186 | set_encode_blocks (self, blocks) |
185 | Algorithm::FEC self |
187 | Algorithm::FEC self |
186 | SV * blocks |
188 | SV * blocks |
187 | CODE: |
189 | CODE: |
188 | |
190 | |
189 | free_files (self); |
191 | free_files (self); |
190 | |
192 | |
191 | if (SvOK (blocks)) |
193 | if (SvOK (blocks)) |
192 | { |
194 | { |
193 | chk_array (blocks, self->dp, "encode", "blocks"); |
195 | chk_array (blocks, self->dp, "set_encode_blocks", "blocks"); |
194 | open_files (self, (AV *)SvRV (blocks), 0); |
196 | open_files (self, (AV *)SvRV (blocks), 0); |
195 | } |
197 | } |
196 | |
198 | |
197 | SV * |
199 | SV * |
198 | encode (self, block_index) |
200 | encode (self, block_index) |
… | |
… | |
203 | if (block_index < 0 || self->ep <= block_index) |
205 | if (block_index < 0 || self->ep <= block_index) |
204 | croak ("encode: block_index %d out of range, must be 0 <= block_index < %d", |
206 | croak ("encode: block_index %d out of range, must be 0 <= block_index < %d", |
205 | block_index, self->ep); |
207 | block_index, self->ep); |
206 | |
208 | |
207 | if (!self->b_addr) |
209 | if (!self->b_addr) |
208 | croak ("no blocks specified by a preceding call to set_blocks"); |
210 | croak ("no blocks specified by a preceding call to set_encode_blocks"); |
209 | |
211 | |
210 | force_addrs (self, self->dp); |
212 | force_addrs (self, self->dp); |
211 | |
213 | |
212 | RETVAL = newSV (self->sz); |
214 | RETVAL = newSV (self->sz); |
213 | if (!RETVAL) |
215 | if (!RETVAL) |
214 | croak ("unable to allocate result block (out of memory)"); |
216 | croak ("unable to allocate result block (out of memory)"); |
|
|
217 | |
215 | SvPOK_only (RETVAL); |
218 | SvPOK_only (RETVAL); |
216 | SvCUR_set (RETVAL, self->sz); |
219 | SvCUR_set (RETVAL, self->sz); |
217 | |
220 | |
218 | self->imp->fec_encode (self->code, self->b_addr, |
221 | self->imp->fec_encode (self->code, self->b_addr, |
219 | SvPVX (RETVAL), block_index, self->sz); |
222 | SvPVX (RETVAL), block_index, self->sz); |
220 | |
223 | |
221 | OUTPUT: |
224 | OUTPUT: |
222 | RETVAL |
225 | RETVAL |
223 | |
226 | |
224 | void |
227 | void |
225 | decode (self, blocks, indices) |
228 | set_decode_blocks (self, blocks, indices) |
226 | Algorithm::FEC self |
229 | Algorithm::FEC self |
227 | SV * blocks |
230 | SV * blocks |
228 | SV * indices |
231 | SV * indices |
229 | CODE: |
232 | CODE: |
230 | { |
233 | { |
231 | int i; |
234 | int i; |
232 | int *idx; |
|
|
233 | |
235 | |
234 | chk_array (blocks, self->dp, "decode", "blocks"); |
236 | chk_array (blocks, self->dp, "set_decode_blocks", "blocks"); |
235 | chk_array (indices, self->dp, "decode", "indices"); |
237 | chk_array (indices, self->dp, "set_decode_blocks", "indices"); |
236 | open_files (self, (AV *)SvRV (blocks), 1); |
|
|
237 | |
238 | |
238 | force_addrs (self, self->dp); |
|
|
239 | |
|
|
240 | Newz (0, idx, self->dp, int); |
239 | Newz (0, self->idx, self->dp, int); |
241 | |
240 | |
242 | /* copy and check */ |
241 | /* copy and check */ |
243 | for (i = 0; i < self->dp; i++) |
242 | for (i = 0; i < self->dp; i++) |
244 | { |
243 | { |
245 | SV *a = *av_fetch ((AV *)SvRV (indices), i, 1); |
244 | self->idx[i] = SvIV (*av_fetch ((AV *)SvRV (indices), i, 1)); |
246 | idx[i] = SvIV (a); |
|
|
247 | sv_setiv (a, i); |
|
|
248 | |
245 | |
249 | if (idx[i] < 0 || idx[i] >= self->ep) |
246 | if (self->idx[i] < 0 || self->idx[i] >= self->ep) |
250 | { |
247 | { |
251 | Safefree (idx); |
248 | free_files (); |
252 | croak ("index %d in array out of bounds (0 <= %d < %d != true)", |
249 | croak ("index %d in array out of bounds (0 <= %d < %d != true)", |
253 | i, idx[i], self->ep); |
250 | i, self->idx[i], self->ep); |
254 | } |
251 | } |
255 | } |
252 | } |
256 | |
253 | |
257 | /* |
254 | /* |
258 | * do the same shuffling as fec_decode does here, |
255 | * do the same shuffling as fec_decode does here, |
259 | * so we know the order. |
256 | * so we know the order. |
260 | */ |
257 | */ |
261 | for (i = 0; i < self->dp; i++) |
258 | for (i = 0; i < self->dp; i++) |
262 | while (idx[i] < self->dp && idx[i] != i) |
259 | while (self->idx[i] < self->dp && self->idx[i] != i) |
263 | { |
260 | { |
264 | SV **a, **b; |
261 | SV **a, **b, **e, **f; |
265 | int d; |
262 | int d; |
266 | void *p; |
263 | void *p; |
267 | SV *s; |
264 | SV *s; |
268 | int c = idx[i]; |
265 | int j = self->idx[i]; |
269 | |
266 | |
270 | if (idx[c] == c) |
267 | if (self->idx[j] == j) |
271 | { |
268 | { |
272 | Safefree (idx); |
269 | free_files (); |
273 | croak ("error while shuffling, duplicate indices? (idx[i]%d i%d idx[c]%d c%d", idx[i],i,idx[c],c); |
270 | croak ("error while shuffling, duplicate indices?"); |
274 | } |
271 | } |
275 | |
272 | |
276 | a = av_fetch ((AV *)SvRV (indices), i, 1); |
273 | a = av_fetch ((AV *)SvRV (indices), i, 1); |
277 | b = av_fetch ((AV *)SvRV (indices), c, 1); |
274 | b = av_fetch ((AV *)SvRV (indices), j, 1); |
|
|
275 | e = av_fetch ((AV *)SvRV (blocks ), i, 1); |
|
|
276 | f = av_fetch ((AV *)SvRV (blocks ), j, 1); |
278 | |
277 | |
279 | d = idx[i]; idx[i] = idx[c]; idx[c] = d; |
278 | d = self->idx[i]; self->idx[i] = self->idx[j]; self->idx[j] = d; |
280 | p = self->b_addr[i]; self->b_addr[i] = self->b_addr[c]; self->b_addr[c] = p; |
|
|
281 | s = *a; *a = *b; *b = s; |
279 | s = *a; *a = *b; *b = s; |
|
|
280 | s = *e; *e = *f; *f = s; |
282 | } |
281 | } |
283 | |
282 | |
|
|
283 | open_files (self, (AV *)SvRV (blocks), 1); |
|
|
284 | } |
|
|
285 | |
|
|
286 | void |
|
|
287 | decode (self) |
|
|
288 | Algorithm::FEC self |
|
|
289 | CODE: |
|
|
290 | |
|
|
291 | if (!self->idx) |
|
|
292 | croak ("index array must be set by a prior call to set_decode_blocks"; |
|
|
293 | |
|
|
294 | force_addrs (self, self->dp); |
284 | self->imp->fec_decode (self->code, self->b_addr, idx, self->sz); |
295 | self->imp->fec_decode (self->code, self->b_addr, self->idx, self->sz); |
285 | |
|
|
286 | Safefree (idx); |
|
|
287 | |
|
|
288 | free_files (self); |
296 | free_files (self); |
289 | } |
|
|
290 | |
297 | |
291 | void |
298 | void |
292 | copy (self, srcblock, dstblock) |
299 | copy (self, srcblock, dstblock) |
293 | Algorithm::FEC self |
300 | Algorithm::FEC self |
294 | SV * srcblock |
301 | SV * srcblock |