Skip to content

Commit 3bbbff8

Browse files
committed
Fixing inestability issues in router code
1 parent 46cfcf2 commit 3bbbff8

File tree

6 files changed

+291
-188
lines changed

6 files changed

+291
-188
lines changed

modules/compression/include/ips_jpg_pv_model.hpp

Lines changed: 207 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -14,180 +14,246 @@
1414
#define BLOCK_ROWS 8
1515
#define BLOCK_COLS 8
1616

17-
SC_MODULE (jpg_output) {
18-
17+
SC_MODULE(jpg_output)
18+
{
19+
1920
//-----------Internal variables-------------------
20-
//const int Block_rows = 8;
21-
//const int Block_cols = 8;
22-
double* image;
21+
// const int Block_rows = 8;
22+
// const int Block_cols = 8;
23+
double *image;
2324
int image_rows = 480;
2425
int image_cols = 640;
2526
signed char eob = 127; // end of block
26-
27-
int quantificator[8][8] = { // quantization table
28-
{16,11,10,16,24,40,51,61},
29-
{12,12,14,19,26,58,60,55},
30-
{14,13,16,24,40,57,69,56},
31-
{14,17,22,29,51,87,80,62},
32-
{18,22,37,56,68,109,103,77},
33-
{24,35,55,64,81,104,113,92},
34-
{49,64,78,87,103,121,120,101},
35-
{72,92,95,98,112,100,103,99}};
36-
37-
int zigzag_index[64]={ // zigzag table
38-
0,1,5,6,14,15,27,28,
39-
2,4,7,13,16,26,29,42,
40-
3,8,12,17,25,30,41,43,
41-
9,11,18,24,31,40,44,53,
42-
10,19,23,32,39,45,52,54,
43-
20,22,33,38,46,51,55,60,
44-
21,34,37,47,50,56,59,61,
45-
35,36,48,49,57,58,62,63};
27+
28+
int quantificator[8][8] = {// quantization table
29+
{16, 11, 10, 16, 24, 40, 51, 61},
30+
{12, 12, 14, 19, 26, 58, 60, 55},
31+
{14, 13, 16, 24, 40, 57, 69, 56},
32+
{14, 17, 22, 29, 51, 87, 80, 62},
33+
{18, 22, 37, 56, 68, 109, 103, 77},
34+
{24, 35, 55, 64, 81, 104, 113, 92},
35+
{49, 64, 78, 87, 103, 121, 120, 101},
36+
{72, 92, 95, 98, 112, 100, 103, 99}};
37+
38+
int zigzag_index[64] = {// zigzag table
39+
0, 1, 5, 6, 14, 15, 27, 28,
40+
2, 4, 7, 13, 16, 26, 29, 42,
41+
3, 8, 12, 17, 25, 30, 41, 43,
42+
9, 11, 18, 24, 31, 40, 44, 53,
43+
10, 19, 23, 32, 39, 45, 52, 54,
44+
20, 22, 33, 38, 46, 51, 55, 60,
45+
21, 34, 37, 47, 50, 56, 59, 61,
46+
35, 36, 48, 49, 57, 58, 62, 63};
4647

4748
// Constructor for compressor
4849
SC_HAS_PROCESS(jpg_output);
49-
jpg_output(sc_module_name jpg_compressor, int im_rows = 480, int im_cols = 640): sc_module(jpg_compressor){
50-
if(im_rows%BLOCK_ROWS==0) {image_rows=im_rows;}
51-
else {image_rows=(im_rows/BLOCK_ROWS+1)*BLOCK_ROWS;}
52-
53-
if(im_cols%BLOCK_COLS==0) {image_cols=im_cols;}
54-
else {image_cols=(im_cols/BLOCK_COLS+1)*BLOCK_COLS;}
55-
56-
image = new double[image_rows*image_cols];
57-
//initialize the image matrix to avoid nan
58-
for(int i=0; i<(image_rows*image_cols);i++){
59-
image[i]=0;
60-
}
50+
jpg_output(sc_module_name jpg_compressor, int im_rows = 480, int im_cols = 640) : sc_module(jpg_compressor)
51+
{
52+
if (im_rows % BLOCK_ROWS == 0)
53+
{
54+
image_rows = im_rows;
55+
}
56+
else
57+
{
58+
image_rows = (im_rows / BLOCK_ROWS + 1) * BLOCK_ROWS;
59+
}
60+
61+
if (im_cols % BLOCK_COLS == 0)
62+
{
63+
image_cols = im_cols;
64+
}
65+
else
66+
{
67+
image_cols = (im_cols / BLOCK_COLS + 1) * BLOCK_COLS;
68+
}
69+
70+
image = new double[image_rows * image_cols];
71+
72+
// initialize the image matrix to avoid nan
73+
for (int i = 0; i < (image_rows * image_cols); i++)
74+
{
75+
image[i] = 0;
76+
}
6177
} // End of Constructor
6278

6379
//------------Code Starts Here-------------------------
64-
void input_pixel(int pixel_value, int row, int col) {
65-
double* i_row = &image[row * image_cols];
66-
i_row[col] = double(pixel_value);
80+
void input_pixel(int pixel_value, int row, int col)
81+
{
82+
double *i_row = &image[row * image_cols];
83+
i_row[col] = double(pixel_value);
6784
}
6885

69-
void output_pixel(int *Pixel, int row, int col) {
70-
double* i_row = &image[row * image_cols];
71-
*Pixel = int(i_row[col]);
86+
void output_pixel(int *Pixel, int row, int col)
87+
{
88+
double *i_row = &image[row * image_cols];
89+
*Pixel = int(i_row[col]);
7290
}
7391

74-
void output_byte(signed char *element, int index) {
92+
void output_byte(signed char *element, int index)
93+
{
7594
element[index] = image[index];
7695
}
7796

78-
void jpeg_compression(int *output_size) {
79-
//Level shift
80-
for(int i=0; i<(image_rows*image_cols);i++){
81-
image[i]=image[i]-128;
82-
}
83-
int number_of_blocks = image_rows*image_cols/(BLOCK_ROWS*BLOCK_COLS);
97+
void jpeg_compression(int *output_size)
98+
{
99+
// Level shift
100+
for (int i = 0; i < (image_rows * image_cols); i++)
101+
{
102+
image[i] = image[i] - 128;
103+
}
104+
105+
int number_of_blocks = image_rows * image_cols / (BLOCK_ROWS * BLOCK_COLS);
106+
84107
#ifndef USING_TLM_TB_EN
85-
int block_output[number_of_blocks][BLOCK_ROWS*BLOCK_COLS] = {0};
86-
int block_output_size[number_of_blocks] = {0};
108+
int block_output[number_of_blocks][BLOCK_ROWS * BLOCK_COLS] = {0};
109+
int block_output_size[number_of_blocks] = {0};
87110
#else
88-
int** block_output = new int*[number_of_blocks];
89-
int* block_output_size = new int[number_of_blocks];
90-
for (int i = 0; i < number_of_blocks; i++)
91-
{
92-
block_output[i] = new int[BLOCK_ROWS * BLOCK_COLS];
93-
}
94-
for (int i = 0; i < number_of_blocks; i++)
95-
{
96-
block_output_size[i] = 0;
97-
for (int j = 0; j < BLOCK_ROWS * BLOCK_COLS; j++)
111+
112+
int **block_output = new int *[number_of_blocks];
113+
int *block_output_size = new int[number_of_blocks];
114+
115+
for (int i = 0; i < number_of_blocks; i++)
98116
{
99-
block_output[i][j] = 0;
117+
block_output[i] = new int[BLOCK_ROWS * BLOCK_COLS];
118+
}
119+
120+
for (int i = 0; i < number_of_blocks; i++)
121+
{
122+
block_output_size[i] = 0;
123+
124+
for (int j = 0; j < BLOCK_ROWS * BLOCK_COLS; j++)
125+
{
126+
block_output[i][j] = 0;
127+
}
100128
}
101-
}
102129
#endif // USING_TLM_TB_EN
103-
int block_counter = 0;
104-
*output_size = 0;
105-
for(int row=0; row<image_rows; row+=BLOCK_ROWS) {
106-
double* i_row = &image[row * image_cols];
107-
for(int col=0; col<image_cols; col+=BLOCK_COLS) { //Divided the image in 8×8 blocks
108-
dct(row,col);
109-
quantization(row,col);
110-
111-
zigzag(row,col,&block_output_size[block_counter],block_output[block_counter]);
112-
*output_size += block_output_size[block_counter]+1;
113-
block_counter++;
114-
}
115-
}
116-
int output_counter = 0;
117-
for(int block_index=0;block_index<number_of_blocks;block_index++){
118-
for(int out_index=0; out_index<block_output_size[block_index];out_index++){
119-
image[output_counter]=block_output[block_index][out_index];
120-
output_counter++;
121-
}
122-
image[output_counter]=eob;
123-
output_counter++;
124-
}
130+
int block_counter = 0;
131+
*output_size = 0;
132+
133+
for (int row = 0; row < image_rows; row += BLOCK_ROWS)
134+
{
135+
for (int col = 0; col < image_cols; col += BLOCK_COLS)
136+
{
137+
// Divided the image in 8×8 blocks
138+
dct(row, col);
139+
quantization(row, col);
140+
141+
zigzag(row, col, &block_output_size[block_counter], block_output[block_counter]);
142+
*output_size += block_output_size[block_counter] + 1;
143+
block_counter++;
144+
}
145+
}
146+
147+
int output_counter = 0;
148+
149+
for (int block_index = 0; block_index < number_of_blocks; block_index++)
150+
{
151+
for (int out_index = 0; out_index < block_output_size[block_index]; out_index++)
152+
{
153+
image[output_counter] = block_output[block_index][out_index];
154+
output_counter++;
155+
}
156+
157+
image[output_counter] = eob;
158+
output_counter++;
159+
}
125160
#ifdef USING_TLM_TB_EN
126-
for (int i = 0; i < number_of_blocks; i++)
127-
{
128-
delete[] block_output[i];
129-
}
130-
delete[] block_output;
131-
delete[] block_output_size;
161+
for (int i = 0; i < number_of_blocks; i++)
162+
{
163+
delete[] block_output[i];
164+
}
165+
166+
delete[] block_output;
167+
delete[] block_output_size;
132168
#endif // USING_TLM_TB_EN
133-
}
134-
135-
void dct(int row_offset, int col_offset) {
169+
}
170+
171+
void dct(int row_offset, int col_offset)
172+
{
136173
double cos_table[BLOCK_ROWS][BLOCK_COLS];
137-
for (int row = 0; row < BLOCK_ROWS; row++) //make the cosine table
138-
{
139-
for (int col = 0; col < BLOCK_COLS; col++) {
140-
cos_table[row][col] = cos((((2*row)+1)*col*PI)/16);
141-
}
142-
}
174+
175+
// make the cosine table
176+
for (int row = 0; row < BLOCK_ROWS; row++)
177+
{
178+
for (int col = 0; col < BLOCK_COLS; col++)
179+
{
180+
cos_table[row][col] = cos((((2 * row) + 1) * col * PI) / 16);
181+
}
182+
}
183+
143184
double temp = 0.0;
144-
for(int row=row_offset; row<row_offset+BLOCK_ROWS; row++)
145-
{
146-
double* i_row = &image[row * image_cols];
147-
for(int col=col_offset; col<col_offset+BLOCK_COLS; col++) {
148-
//i_row[col] = cos_table[row-row_offset][col-col_offset];
149-
temp = 0.0;
150-
for (int x = 0; x < 8; x++){
151-
double* x_row = &image[(x+row_offset) * image_cols];
152-
for (int y = 0; y < 8; y++) {
153-
temp += x_row[y+col_offset] * cos_table[x][row-row_offset] * cos_table[y][col-col_offset];
154-
}
155-
}
156-
if ((row-row_offset == 0) && (col-col_offset == 0)) {
157-
temp /= 8.0;
158-
}
159-
else if (((row-row_offset == 0) && (col-col_offset != 0)) || ((row-row_offset != 0) && (col-col_offset == 0))){
160-
temp /= (4.0*sqrt(2.0));
161-
}
162-
else {
163-
temp /= 4.0;
185+
186+
for (int row = row_offset; row < row_offset + BLOCK_ROWS; row++)
187+
{
188+
double *i_row = &image[row * image_cols];
189+
190+
for (int col = col_offset; col < col_offset + BLOCK_COLS; col++)
191+
{
192+
// i_row[col] = cos_table[row-row_offset][col-col_offset];
193+
temp = 0.0;
194+
195+
for (int x = 0; x < 8; x++)
196+
{
197+
double *x_row = &image[(x + row_offset) * image_cols];
198+
199+
for (int y = 0; y < 8; y++)
200+
{
201+
temp += x_row[y + col_offset] * cos_table[x][row - row_offset] * cos_table[y][col - col_offset];
202+
}
203+
}
204+
205+
if ((row - row_offset == 0) && (col - col_offset == 0))
206+
{
207+
temp /= 8.0;
208+
}
209+
else if (((row - row_offset == 0) && (col - col_offset != 0)) || ((row - row_offset != 0) && (col - col_offset == 0)))
210+
{
211+
temp /= (4.0 * sqrt(2.0));
164212
}
165-
i_row[col] = temp;
213+
else
214+
{
215+
temp /= 4.0;
216+
}
217+
218+
i_row[col] = temp;
166219
}
167-
}
220+
}
168221
}
169222

170-
void quantization(int row_offset, int col_offset) {
171-
for(int row=row_offset; row<row_offset+BLOCK_ROWS; row++)
172-
{
173-
double* i_row = &image[row * image_cols];
174-
for(int col=col_offset; col<col_offset+BLOCK_COLS; col++) {
175-
i_row[col] = round(i_row[col]/quantificator[row-row_offset][col-col_offset]);
223+
void quantization(int row_offset, int col_offset)
224+
{
225+
for (int row = row_offset; row < row_offset + BLOCK_ROWS; row++)
226+
{
227+
double *i_row = &image[row * image_cols];
228+
229+
for (int col = col_offset; col < col_offset + BLOCK_COLS; col++)
230+
{
231+
i_row[col] = round(i_row[col] / quantificator[row - row_offset][col - col_offset]);
176232
}
177-
}
233+
}
178234
}
179235

180-
void zigzag(int row_offset, int col_offset, int *block_output_size, int *block_output) {
181-
int index_last_non_zero_value = 0; // index to last non-zero in a block zigzag array
182-
for(int row=row_offset; row<row_offset+BLOCK_ROWS; row++)
183-
{
184-
double* i_row = &image[row * image_cols];
185-
for(int col=col_offset; col<col_offset+BLOCK_COLS; col++) {
186-
int temp_index = zigzag_index[(row-row_offset)*8+(col-col_offset)];
187-
block_output[temp_index] = i_row[col];
188-
if(i_row[col] !=0 && temp_index>index_last_non_zero_value) {index_last_non_zero_value = temp_index+1;}
236+
void zigzag(int row_offset, int col_offset, int *block_output_size, int *block_output)
237+
{
238+
int index_last_non_zero_value = 0;
239+
240+
// index to last non-zero in a block zigzag array
241+
for (int row = row_offset; row < row_offset + BLOCK_ROWS; row++)
242+
{
243+
double *i_row = &image[row * image_cols];
244+
245+
for (int col = col_offset; col < col_offset + BLOCK_COLS; col++)
246+
{
247+
int temp_index = zigzag_index[(row - row_offset) * 8 + (col - col_offset)];
248+
block_output[temp_index] = i_row[col];
249+
250+
if (i_row[col] != 0 && temp_index > index_last_non_zero_value)
251+
{
252+
index_last_non_zero_value = temp_index + 1;
253+
}
189254
}
190-
}
255+
}
256+
191257
*block_output_size = index_last_non_zero_value;
192258
}
193-
};
259+
};

0 commit comments

Comments
 (0)