1 /*
2  * Copyright (c) 2007 - 2015 Joseph Gaeddert
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 //
24 // FEC, repeat code
25 //
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "liquid.internal.h"
31 
32 // create rep3 codec object
fec_rep3_create(void * _opts)33 fec fec_rep3_create(void * _opts)
34 {
35     fec q = (fec) malloc(sizeof(struct fec_s));
36 
37     q->scheme = LIQUID_FEC_REP3;
38     q->rate = fec_get_rate(q->scheme);
39 
40     q->encode_func      = &fec_rep3_encode;
41     q->decode_func      = &fec_rep3_decode;
42     q->decode_soft_func = &fec_rep3_decode_soft;
43 
44     return q;
45 }
46 
47 // destroy rep3 object
fec_rep3_destroy(fec _q)48 void fec_rep3_destroy(fec _q)
49 {
50     free(_q);
51 }
52 
53 // print rep3 object
fec_rep3_print(fec _q)54 void fec_rep3_print(fec _q)
55 {
56     printf("fec_rep3 [r: %3.2f]\n", _q->rate);
57 }
58 
59 // encode block of data using rep3 encoder
60 //
61 //  _q              :   encoder/decoder object
62 //  _dec_msg_len    :   decoded message length (number of bytes)
63 //  _msg_dec        :   decoded message [size: 1 x _dec_msg_len]
64 //  _msg_enc        :   encoded message [size: 1 x 3*_dec_msg_len]
fec_rep3_encode(fec _q,unsigned int _dec_msg_len,unsigned char * _msg_dec,unsigned char * _msg_enc)65 void fec_rep3_encode(fec _q,
66                      unsigned int _dec_msg_len,
67                      unsigned char *_msg_dec,
68                      unsigned char *_msg_enc)
69 {
70     unsigned int i;
71     for (i=0; i<3; i++) {
72         memcpy(&_msg_enc[i*_dec_msg_len], _msg_dec, _dec_msg_len);
73     }
74 }
75 
76 // decode block of data using rep3 decoder
77 //
78 //  _q              :   encoder/decoder object
79 //  _dec_msg_len    :   decoded message length (number of bytes)
80 //  _msg_enc        :   encoded message [size: 1 x 3*_dec_msg_len]
81 //  _msg_dec        :   decoded message [size: 1 x _dec_msg_len]
fec_rep3_decode(fec _q,unsigned int _dec_msg_len,unsigned char * _msg_enc,unsigned char * _msg_dec)82 void fec_rep3_decode(fec _q,
83                      unsigned int _dec_msg_len,
84                      unsigned char *_msg_enc,
85                      unsigned char *_msg_dec)
86 {
87     unsigned char s0, s1, s2;
88     unsigned int i, num_errors=0;
89     for (i=0; i<_dec_msg_len; i++) {
90         s0 = _msg_enc[i];
91         s1 = _msg_enc[i +   _dec_msg_len];
92         s2 = _msg_enc[i + 2*_dec_msg_len];
93 
94         //  s0  s1  s2  |   y   e
95         //  ------------+---------
96         //  0   0   0   |   0   0
97         //  0   0   1   |   0   1
98         //  0   1   0   |   0   1
99         //  0   1   1   |   1   1
100         //  1   0   0   |   0   1
101         //  1   0   1   |   1   1
102         //  1   1   0   |   1   1
103         //  1   1   1   |   1   0
104 
105         _msg_dec[i] = (s0 & s1) | (s0 & s2) | (s1 & s2);
106 
107         //num_errors += (s0 ^ s1) | (s0 ^ s2) | (s1 ^ s2) ? 1 : 0;
108         num_errors += 0;
109     }
110     //return num_errors;
111 }
112 
113 // decode block of data using rep3 decoder (soft metrics)
114 //
115 //  _q              :   encoder/decoder object
116 //  _dec_msg_len    :   decoded message length (number of bytes)
117 //  _msg_enc        :   encoded message [size: 1 x 3*_dec_msg_len]
118 //  _msg_dec        :   decoded message [size: 1 x _dec_msg_len]
fec_rep3_decode_soft(fec _q,unsigned int _dec_msg_len,unsigned char * _msg_enc,unsigned char * _msg_dec)119 void fec_rep3_decode_soft(fec _q,
120                           unsigned int _dec_msg_len,
121                           unsigned char * _msg_enc,
122                           unsigned char * _msg_dec)
123 {
124     unsigned char s0, s1, s2;
125     unsigned int i;
126     unsigned int j;
127     unsigned int s_hat;
128     //unsigned int num_errors=0;
129     for (i=0; i<_dec_msg_len; i++) {
130 
131         // clear decoded message
132         _msg_dec[i] = 0x00;
133 
134         for (j=0; j<8; j++) {
135             s0 = _msg_enc[8*i                    + j];
136             s1 = _msg_enc[8*(i +   _dec_msg_len) + j];
137             s2 = _msg_enc[8*(i + 2*_dec_msg_len) + j];
138 
139             // average three symbols and make decision
140             s_hat = (s0 + s1 + s2)/3;
141 
142             _msg_dec[i] |= (s_hat > LIQUID_SOFTBIT_ERASURE) ? (1 << (8-j-1)) : 0x00;
143 
144         }
145 
146     }
147 }
148