ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Algorithm-FEC/FEC.pm
(Generate patch)

Comparing Algorithm-FEC/FEC.pm (file contents):
Revision 1.2 by root, Tue Sep 9 06:07:28 2003 UTC vs.
Revision 1.9 by root, Sun Jun 27 12:43:08 2004 UTC

1=head1 NAME 1=head1 NAME
2 2
3Convert::FEC - Forward Error Correction using Vandermonde Matrices 3Algorithm::FEC - Forward Error Correction using Vandermonde Matrices
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Convert::FEC; 7 use Algorithm::FEC;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module is an interface to the fec library by Luigi Rizzo et al., see 11This module is an interface to the fec library by Luigi Rizzo et al., see
12the file README.fec in the distribution for more details. 12the file README.fec in the distribution for more details.
13 13
14This library implements a simple (C<encoded_packets>,C<data_packets>) 14This library implements a simple (C<encoded_blocks>,C<data_blocks>)
15erasure code based on Vandermonde matrices. The encoder takes 15erasure code based on Vandermonde matrices. The encoder takes
16C<data_packets> packets of size C<block_size> each, and is able to produce 16C<data_blocks> blocks of size C<block_size> each, and is able to produce
17up to C<encoded_packets> different encoded packets, numbered from C<0> 17up to C<encoded_blocks> different encoded blocks, numbered from C<0>
18to C<encoded_packets-1>, such that any subset of C<data_packets> members 18to C<encoded_blocks-1>, such that any subset of C<data_blocks> members
19permits reconstruction of the original data. 19permits reconstruction of the original data.
20 20
21Allowed values for C<data_packets> and C<encoded_packets> must obey the 21Allowed values for C<data_blocks> and C<encoded_blocks> must obey the
22following equation: 22following equation:
23 23
24 data_packets <= encoded_packets <= MAXBLOCKS 24 data_blocks <= encoded_blocks <= MAXBLOCKS
25 25
26Where C<MAXBLOCKS=256> for the fast implementation and C<MAXBLOCKS=65536> 26Where C<MAXBLOCKS=256> for the fast implementation and C<MAXBLOCKS=65536>
27for the slow implementation (the implementation is chosen automatically). 27for the slow implementation (the implementation is chosen automatically).
28 28
29=over 4 29=over 4
30 30
31=cut 31=cut
32 32
33package Convert::FEC; 33package Algorithm::FEC;
34 34
35require XSLoader; 35require XSLoader;
36 36
37no warnings; 37no warnings;
38 38
39$VERSION = 0.01; 39$VERSION = 0.5;
40 40
41XSLoader::load Convert::FEC, $VERSION; 41XSLoader::load Algorithm::FEC, $VERSION;
42 42
43=item $fec = new data_packets, encoded_packets, blocksize 43=item $fec = new Algorithm::FEC $data_blocks, $encoded_blocks, $blocksize
44 44
45Creates a new Algorithm::FEC object with the given parameters.
46
45=item $fec->set_blocks ([array_of_blocks]) 47=item $fec->set_encode_blocks ([array_of_blocks])
46 48
47Sets the data blocks used for the encoding. Each member of the array can either be: 49Sets the data blocks used for the encoding. Each member of the array can either be:
48 50
49=over 4 51=over 4
50 52
63file. 65file.
64 66
65=back 67=back
66 68
67If your data is not of the required size (i.e. a multiple of C<blocksize> 69If your data is not of the required size (i.e. a multiple of C<blocksize>
68bytes), then you must pad it (e.g. with zero bytes) on encoding, and 70bytes), then you must pad it (e.g. with zero bytes) on encoding (and you
69truncate it after decoding. 71should truncate it after decoding). Otherwise, this library croaks.
72
73Future versions might instead load the short segment into memory or extend
74your scalar (this might enable nice tricks, like C<$fec->copy (..., my
75$x)> :). Mail me if you want this to happen.
70 76
71If called without arguments, the internal storage associated with the 77If called without arguments, the internal storage associated with the
72blocks is freed again. 78blocks is freed again.
73 79
74=item $block = $fec->encode (block_index) 80=item $block = $fec->encode ($block_index)
75 81
76Creates a single encoded packet of index C<block_index>, which must be 82Creates a single encoded block of index C<block_index>, which must be
77between C<0> and C<encoded_packets-1> (inclusive). The blocks from C<0> to 83between C<0> and C<encoded_blocks-1> (inclusive). The blocks from C<0> to
78C<data_packets-1> are simply copies of the original data blocks. 84C<data_blocks-1> are simply copies of the original data blocks.
79 85
80The encoded block is returned as a perl scalar (so the blocks should fit 86The encoded block is returned as a perl scalar (so the blocks should fit
81into memory. If this is a problem for you mail me and I'll make it a file. 87into memory. If this is a problem for you mail me and I'll make it a file.
82 88
83=item $fec->decode ([array_of_blocks], [array_of_indices]) 89=item $fec->set_decode_blocks ([array_of_blocks], [array_of_indices])
84 90
85Decode C<data_packets> of blocks (see C<set_blocks> for the 91Prepares to decode C<data_blocks> of blocks (see C<set_encode_blocks> for
86C<array_of_blocks> parameter). 92the C<array_of_blocks> parameter).
87 93
88Since these are not necessarily the original data blocks, an array of 94Since these are not necessarily the original data blocks, an array of
89indices (ranging from C<0> to C<encoded_packets-1>) must be supplied as 95indices (ranging from C<0> to C<encoded_blocks-1>) must be supplied as
90the second arrayref. 96the second arrayref.
91 97
92Both arrays must have exactly C<data_packets> entries. 98Both arrays must have exactly C<data_blocks> entries.
93 99
94After decoding, the blocks will be modified in place (if necessary), and 100This method also reorders the blocks and index array in place (if
95the array of indices will be updates to reflect the changes: The n-th 101necessary) to reflect the order the blocks will have in the decoded
96entry in the indices array is the index of the n-th data block of the 102result.
97file. 103
104Both arrays must have exactly C<data_blocks> entries.
105
106The index array represents the decoded ordering, in that the n-th entry
107in the indices array corresponds to the n-th data block of the decoded
108result. The value stored in the n-th place in the array will contain the
109index of the encoded data block.
110
111Input blocks with indices less than C<data_blocks> will be moved to their
112final position (block k to position k), while the gaps between them will
113be filled with check blocks. The decoding process will not modify the
114already decoded data blocks, but will modify the check blocks.
98 115
99That is, if you call this function with C<indices = [4,3,1]>, with 116That is, if you call this function with C<indices = [4,3,1]>, with
100C<data_packets = 3>, then this array will be returned: C<[0,2,1]>. This 117C<data_blocks = 3>, then this array will be returned: C<[0,2,1]>. This
101means that input block C<0> corresponds file block C<0>, input block C<1> 118means that input block C<0> corresponds to file block C<0>, input block
102to file block C<2> and input block C<2> to data block C<1>. 119C<1> to file block C<2> and input block C<2> to data block C<1>.
103 120
104You can just iterate over this array and write out the corresponding data 121You can just iterate over this array and write out the corresponding data
105block (although this is inefficient). 122block (although this is inefficient):
106 123
107Only input blocks with indices >= C<data_packets> will be modified, blocks 124 for my $i (0 .. $#idx)
108that already contain the original data will just be reordered. 125 if ($idx[$i] != $i) # need we move this block?
126 copy encoded block $idx[$i] to position $i
127 }
128 }
109 129
130The C<copy> method can be helpful here.
131
110This method destroys the block array as set up by C<set_blocks>. 132This method destroys the block array as set up by C<set_encode_blocks>.
133
134=item $fec->shuffle ([array_of_blocks], [array_of_indices])
135
136The same same as C<set_decode_blocks>, with the exception that the blocks
137are not actually set for decoding.
138
139This method is not normally used, but if you want to move blocks
140around after reodering and before decoding, then calling Cshuffle>
141followed by C<set_decode_blocks> incurs lower overhead than calling
142C<set_decode_blocks> twice, as files are not mmapped etc.
143
144=item $fec->decode
145
146Decode the blocks set by a prior call to C<set_decode_blocks>.
147
148This method destroys the block array as set up by C<set_decode_blocks>.
111 149
112=item $fec->copy ($srcblock, $dstblock) 150=item $fec->copy ($srcblock, $dstblock)
113 151
114Utility function that simply copies one block (specified like in 152Utility function that simply copies one block (specified like in
115C<set_blocks>) into another. This, btw., destroys the blocks set by 153C<set_encode_blocks>) into another. This, btw., destroys the blocks set by
116C<set_blocks>. 154C<set_*_blocks>.
117 155
118If you don't understand why this helps, feel free to ignore it :) 156=back
119 157
120=item COMPATIBILITY 158=head1 COMPATIBILITY
121 159
122The way this module works is compatible with the way freenet 160The way this module works is compatible with the way freenet
123(L<http://freenet.sf.net>) encodes files. Comaptibility to other file 161(L<http://freenet.sf.net>) encodes files. Comaptibility to other file
124formats or networks is not know, please tell me if you find more examples. 162formats or networks is not know, please tell me if you find more examples.
125 163
129user, just to see that this rather rarely-used module is actually being 167user, just to see that this rather rarely-used module is actually being
130used (except for freenet ;) 168used (except for freenet ;)
131 169
132=head1 BUGS 170=head1 BUGS
133 171
172 * too complicated.
134 * largely untested, please change this. 173 * largely untested, please change this.
135 * file descriptors are not supported, but should be. 174 * file descriptors are not supported, but should be.
136 * utility functions for files should be provided. 175 * utility functions for files should be provided.
137 * 16 bit version not tested 176 * 16 bit version not tested
138 177

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines