TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
rscode.c
Go to the documentation of this file.
1 /*
2  * qrencode - QR Code encoder
3  *
4  * Reed solomon encoder. This code is taken from Phil Karn's libfec then
5  * editted and packed into a pair of .c and .h files.
6  *
7  * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
8  * (libfec is released under the GNU Lesser General Public License.)
9  *
10  * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 #include "config.h"
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "rscode.h"
32 
33 /* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
34  *
35  */
36 typedef unsigned char data_t;
37 
38 
42 struct _RS {
43  int mm; /* Bits per symbol */
44  int nn; /* Symbols per block (= (1<<mm)-1) */
45  data_t *alpha_to; /* log lookup table */
46  data_t *index_of; /* Antilog lookup table */
47  data_t *genpoly; /* Generator polynomial */
48  int nroots; /* Number of generator roots = number of parity symbols */
49  int fcr; /* First consecutive root, index form */
50  int prim; /* Primitive element, index form */
51  int iprim; /* prim-th root of 1, index form */
52  int pad; /* Padding bytes in shortened block */
53  int gfpoly;
54 };
55 
56 static inline int modnn(RS *rs, int x){
57  while (x >= rs->nn) {
58  x -= rs->nn;
59  x = (x >> rs->mm) + (x & rs->nn);
60  }
61  return x;
62 }
63 
64 
65 #define MODNN(x) modnn(rs,x)
66 
67 #define MM (rs->mm)
68 #define NN (rs->nn)
69 #define ALPHA_TO (rs->alpha_to)
70 #define INDEX_OF (rs->index_of)
71 #define GENPOLY (rs->genpoly)
72 #define NROOTS (rs->nroots)
73 #define FCR (rs->fcr)
74 #define PRIM (rs->prim)
75 #define IPRIM (rs->iprim)
76 #define PAD (rs->pad)
77 #define A0 (NN)
78 
79 
80 /* Initialize a Reed-Solomon codec
81  * symsize = symbol size, bits
82  * gfpoly = Field generator polynomial coefficients
83  * fcr = first root of RS code generator polynomial, index form
84  * prim = primitive element to generate polynomial roots
85  * nroots = RS code generator polynomial degree (number of roots)
86  * pad = padding bytes at front of shortened block
87  */
88 static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
89 {
90  RS *rs;
91 
92 
93 /* Common code for intializing a Reed-Solomon control block (char or int symbols)
94  * Copyright 2004 Phil Karn, KA9Q
95  * May be used under the terms of the GNU Lesser General Public License (LGPL)
96  */
97 //#undef NULL
98 //#define NULL ((void *)0)
99 
100  int i, j, sr,root,iprim;
101 
102  rs = NULL;
103  /* Check parameter ranges */
104  if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){
105  goto done;
106  }
107 
108  if(fcr < 0 || fcr >= (1<<symsize))
109  goto done;
110  if(prim <= 0 || prim >= (1<<symsize))
111  goto done;
112  if(nroots < 0 || nroots >= (1<<symsize))
113  goto done; /* Can't have more roots than symbol values! */
114  if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
115  goto done; /* Too much padding */
116 
117  rs = (RS *)calloc(1,sizeof(RS));
118  if(rs == NULL)
119  goto done;
120 
121  rs->mm = symsize;
122  rs->nn = (1<<symsize)-1;
123  rs->pad = pad;
124 
125  rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
126  if(rs->alpha_to == NULL){
127  free(rs);
128  rs = NULL;
129  goto done;
130  }
131  rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
132  if(rs->index_of == NULL){
133  free(rs->alpha_to);
134  free(rs);
135  rs = NULL;
136  goto done;
137  }
138 
139  /* Generate Galois field lookup tables */
140  rs->index_of[0] = A0; /* log(zero) = -inf */
141  rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
142  sr = 1;
143  for(i=0;i<rs->nn;i++){
144  rs->index_of[sr] = i;
145  rs->alpha_to[i] = sr;
146  sr <<= 1;
147  if(sr & (1<<symsize))
148  sr ^= gfpoly;
149  sr &= rs->nn;
150  }
151  if(sr != 1){
152  /* field generator polynomial is not primitive! */
153  free(rs->alpha_to);
154  free(rs->index_of);
155  free(rs);
156  rs = NULL;
157  goto done;
158  }
159 
160  /* Form RS code generator polynomial from its roots */
161  rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
162  if(rs->genpoly == NULL){
163  free(rs->alpha_to);
164  free(rs->index_of);
165  free(rs);
166  rs = NULL;
167  goto done;
168  }
169  rs->fcr = fcr;
170  rs->prim = prim;
171  rs->nroots = nroots;
172  rs->gfpoly = gfpoly;
173 
174  /* Find prim-th root of 1, used in decoding */
175  for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
176  ;
177  rs->iprim = iprim / prim;
178 
179  rs->genpoly[0] = 1;
180  for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
181  rs->genpoly[i+1] = 1;
182 
183  /* Multiply rs->genpoly[] by @**(root + x) */
184  for (j = i; j > 0; j--){
185  if (rs->genpoly[j] != 0)
186  rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
187  else
188  rs->genpoly[j] = rs->genpoly[j-1];
189  }
190  /* rs->genpoly[0] can never be zero */
191  rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
192  }
193  /* convert rs->genpoly[] to index form for quicker encoding */
194  for (i = 0; i <= nroots; i++)
195  rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
196  done:;
197 
198  return rs;
199 }
200 
201 RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
202 {
203  return init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad);
204 }
205 
206 
207 void free_rs_char(RS *rs)
208 {
209  free(rs->alpha_to);
210  free(rs->index_of);
211  free(rs->genpoly);
212  free(rs);
213 }
214 
215 /* The guts of the Reed-Solomon encoder, meant to be #included
216  * into a function body with the following typedefs, macros and variables supplied
217  * according to the code parameters:
218 
219  * data_t - a typedef for the data symbol
220  * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
221  * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
222  * NROOTS - the number of roots in the RS code generator polynomial,
223  * which is the same as the number of parity symbols in a block.
224  Integer variable or literal.
225  *
226  * NN - the total number of symbols in a RS block. Integer variable or literal.
227  * PAD - the number of pad symbols in a block. Integer variable or literal.
228  * ALPHA_TO - The address of an array of NN elements to convert Galois field
229  * elements in index (log) form to polynomial form. Read only.
230  * INDEX_OF - The address of an array of NN elements to convert Galois field
231  * elements in polynomial form to index (log) form. Read only.
232  * MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
233  * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
234 
235  * The memset() and memmove() functions are used. The appropriate header
236  * file declaring these functions (usually <string.h>) must be included by the calling
237  * program.
238 
239  * Copyright 2004, Phil Karn, KA9Q
240  * May be used under the terms of the GNU Lesser General Public License (LGPL)
241  */
242 
243 #undef A0
244 #define A0 (NN) /* Special reserved value encoding zero in index form */
245 
246 void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
247 {
248  int i, j;
249  data_t feedback;
250 
251  memset(parity,0,NROOTS*sizeof(data_t));
252 
253  for(i=0;i<NN-NROOTS-PAD;i++){
254  feedback = INDEX_OF[data[i] ^ parity[0]];
255  if(feedback != A0){ /* feedback term is non-zero */
256 #ifdef UNNORMALIZED
257  /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
258  * always be for the polynomials constructed by init_rs()
259  */
260  feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
261 #endif
262  for(j=1;j<NROOTS;j++)
263  parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
264  }
265  /* Shift */
266  memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
267  if(feedback != A0)
268  parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
269  else
270  parity[NROOTS-1] = 0;
271  }
272 }
int gfpoly
Definition: rscode.c:53
static int modnn(RS *rs, int x)
Definition: rscode.c:56
int nroots
Definition: rscode.c:48
#define MODNN(x)
Definition: rscode.c:65
data_t * genpoly
Definition: rscode.c:47
Reed-Solomon codec control block.
Definition: rscode.c:42
int iprim
Definition: rscode.c:51
#define NROOTS
Definition: rscode.c:72
#define GENPOLY
Definition: rscode.c:71
void free_rs_char(RS *rs)
Definition: rscode.c:207
int fcr
Definition: rscode.c:49
unsigned char data_t
Definition: rscode.c:36
data_t * alpha_to
Definition: rscode.c:45
int mm
Definition: rscode.c:43
#define PAD
Definition: rscode.c:76
#define NN
Definition: rscode.c:68
voidp calloc(uInt items, uInt size)
#define ALPHA_TO
Definition: rscode.c:69
static RS * init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
Definition: rscode.c:88
RS * init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
Definition: rscode.c:201
int pad
Definition: rscode.c:52
void free(voidpf ptr)
int nn
Definition: rscode.c:44
void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
Definition: rscode.c:246
voidp malloc(uInt size)
data_t * index_of
Definition: rscode.c:46
#define A0
Definition: rscode.c:244
int prim
Definition: rscode.c:50
#define INDEX_OF
Definition: rscode.c:70