forked from sahaRatul/sela
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrice.c
More file actions
189 lines (168 loc) · 3.71 KB
/
Copy pathrice.c
File metadata and controls
189 lines (168 loc) · 3.71 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <malloc.h>
#include "rice.h"
/*signed data to unsigned data*/
int signed_to_unsigned(const unsigned int data_size,const short *input,unsigned short *output)
{
unsigned int i;
for(i = 0; i < data_size; i++)
{
if(input[i] < 0)
output[i] = (-2 * input[i]) - 1;
if(input[i] >= 0)
output[i] = 2 * input[i];
}
return(0);
}
/*unsigned data to signed data*/
int unsigned_to_signed(const unsigned int data_size,const unsigned short *input,short *output)
{
unsigned int i;
for(i = 0; i < data_size; i++)
{
if((input[i] & 0x01) == 0x01)//Odd number
output[i] = -((input[i] + 1) >> 1);
if((input[i] & 0x01) == 0x00)//Even number
output[i] = input[i] >> 1;
}
return(0);
}
/*calculate optimum rice parameter for encoding a block of data*/
unsigned short get_opt_rice_param(const unsigned short *data,int data_size,unsigned int *req_bits)
{
int i = 0,j;
unsigned int bits[MAX_RICE_PARAM];
unsigned int temp;
unsigned short best_index;
for(i = 0; i < MAX_RICE_PARAM; i++)
{
temp = 0;
for(j = 0; j < data_size; j++)
{
/*data[j]/2^k;*/
temp += data[j] >> i;
temp += 1;
temp += i;
}
bits[i] = temp;
}
//Get the minimum parameter
temp = bits[0];
best_index = 0;
*req_bits = bits[0];
for(i = 1; i < MAX_RICE_PARAM; i++)
{
if(bits[i] < temp)
{
temp = bits[i];
*req_bits = bits[i];
best_index = (short)i;
}
}
return best_index;
}
/*Encode a block of data using Golomb-Rice coding*/
unsigned int rice_encode_block(short param,const unsigned short *input,int size,unsigned short *encoded)
{
int i,j,temp,written = 0;
unsigned int bits;
rice_encode_context context;
rice_encode_context *ctx = &context;
ctx->buffer = 0;
ctx->filled = 0;
ctx->bits = 0;
for(i = 0; i < size; i++)
{
//temp = floor(input[i]/2^param)
temp = input[i] >> param;
//Write out 'temp' number of ones
for(j = 0; j < temp; j++)
{
ctx->buffer = ctx->buffer | (0x0001 << ctx->filled);
ctx->bits++;
if(ctx->filled == 15)
{
encoded[written++] = ctx->buffer;
ctx->buffer = 0;
ctx->filled = 0;
}
else
ctx->filled++;
}
//Write out a zero bit
ctx->buffer = ctx->buffer | (0x0000 << ctx->filled);
ctx->bits++;
if(ctx->filled == 15)
{
encoded[written++] = ctx->buffer;
ctx->buffer = 0;
ctx->filled = 0;
}
else
ctx->filled++;
//Write out the last 'param' bits of input[i]
for (j = param - 1; j >= 0; j--)
{
ctx->buffer = ctx->buffer | (((input[i] >> j) & 0x0001) << ctx->filled);
ctx->bits++;
if(ctx->filled == 15)
{
encoded[written++] = ctx->buffer;
ctx->buffer = 0;
ctx->filled = 0;
}
else
ctx->filled++;
}
}
//Flush buffer if not empty
if(ctx->filled != 0)
encoded[written] = ctx->buffer;
bits = ctx->bits;
return(bits);
}
/*Decode a block of data encoded using Golomb-Rice coding*/
unsigned int rice_decode_block(short param,const unsigned short *encoded,int count,unsigned short *decoded)
{
int i = 0,j = 0,q,k = 0;
short tmp;
rice_decode_context context;
rice_decode_context *ctx = &context;
ctx->filled = 0;
while(k < count)
{
//Count ones until a zero is encountered
q = 0;
while(1)
{
if(ctx->filled == 0)
{
ctx->filled = 16;
ctx->buffer = encoded[i++];
}
tmp = ctx->buffer & 0x0001;
ctx->buffer = ctx->buffer >> 1;
ctx->filled--;
if(tmp == 0)
break;
q++;
}
decoded[k] = 0;
decoded[k] = q << param; //x = q * 2^k
//Read out the last 'param' bits of input
for(j = param - 1; j >= 0; j--)
{
if(ctx->filled == 0)
{
ctx->filled = 16;
ctx->buffer = encoded[i++];
}
tmp = ctx->buffer & 0x0001;
ctx->buffer = ctx->buffer >> 1;
ctx->filled--;
decoded[k] = decoded[k] | (tmp << j);
}
k++;
}
return(k);
}