PandA-2024.02
decode.c
Go to the documentation of this file.
1 /*
2 +--------------------------------------------------------------------------+
3 | CHStone : a suite of benchmark programs for C-based High-Level Synthesis |
4 | ======================================================================== |
5 | |
6 | * Collected and Modified : Y. Hara, H. Tomiyama, S. Honda, |
7 | H. Takada and K. Ishii |
8 | Nagoya University, Japan |
9 | |
10 | * Remark : |
11 | 1. This source code is modified to unify the formats of the benchmark |
12 | programs in CHStone. |
13 | 2. Test vectors are added for CHStone. |
14 | 3. If "main_result" is 0 at the end of the program, the program is |
15 | correctly executed. |
16 | 4. Please follow the copyright of each benchmark program. |
17 +--------------------------------------------------------------------------+
18 */
19 /*************************************************************
20 Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
21 PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
22 Group. If you use this software, you agree to the following: This
23 program package is purely experimental, and is licensed "as is".
24 Permission is granted to use, modify, and distribute this program
25 without charge for any purpose, provided this license/ disclaimer
26 notice appears in the copies. No warranty or maintenance is given,
27 either expressed or implied. In no event shall the author(s) be
28 liable to you or a third party for any special, incidental,
29 consequential, or other damages, arising out of the use or inability
30 to use the program for any purpose (or the loss of data), even if we
31 have been advised of such possibilities. Any public reference or
32 advertisement of this source code should refer to it as the Portable
33 Video Research Group (PVRG) code, and not by any author(s) (or
34 Stanford University) name.
35 *************************************************************/
36 
37 /*
38 ************************************************************
39 decode.c (original: transform.c)
40 
41 This file contains the reference DCT, the zig-zag and quantization
42 algorithms.
43 
44 ************************************************************
45 */
46 /*
47  * Decoder
48  *
49  * @(#) $Id: decode.c,v 1.2 2003/07/18 10:19:21 honda Exp $
50  */
51 void ChenIDct (int *x, int *y);
52 
54 
55 const int zigzag_index[64] = /* Is zig-zag map for matrix -> scan array */
56 { 0, 1, 5, 6, 14, 15, 27, 28,
57  2, 4, 7, 13, 16, 26, 29, 42,
58  3, 8, 12, 17, 25, 30, 41, 43,
59  9, 11, 18, 24, 31, 40, 44, 53,
60  10, 19, 23, 32, 39, 45, 52, 54,
61  20, 22, 33, 38, 46, 51, 55, 60,
62  21, 34, 37, 47, 50, 56, 59, 61,
63  35, 36, 48, 49, 57, 58, 62, 63
64 };
65 
66 
67 /*
68  * IZigzagMatrix() performs an inverse zig-zag translation on the
69  * input imatrix and places the output in omatrix.
70  */
71 void
72 IZigzagMatrix (int *imatrix, int *omatrix)
73 {
74  int i;
75 
76  for (i = 0; i < DCTSIZE2; i++)
77 
78  {
79 
80 *(omatrix++) = imatrix[zigzag_index[i]];
81 
82 }
83 }
84 
85 
86 /*
87  * IQuantize() takes an input matrix and does an inverse quantization
88  * and puts the output int qmatrix.
89  */
90 void
91 IQuantize (int *matrix, unsigned int *qmatrix)
92 {
93  int *mptr;
94 
95  for (mptr = matrix; mptr < matrix + DCTSIZE2; mptr++)
96  {
97  *mptr = *mptr * (*qmatrix);
98  qmatrix++;
99  }
100 }
101 
102 
103 /*
104  * PostshiftIDctMatrix() adds 128 (2048) to all 64 elements of an 8x8 matrix.
105  * This results in strictly positive values for all pixel coefficients.
106  */
107 void
108 PostshiftIDctMatrix (int *matrix, int shift)
109 {
110  int *mptr;
111  for (mptr = matrix; mptr < matrix + DCTSIZE2; mptr++)
112  {
113  *mptr += shift;
114  }
115 }
116 
117 
118 /*
119  * BoundIDctMatrix bounds the inverse dct matrix so that no pixel has a
120  * value greater than 255 (4095) or less than 0.
121  */
122 void
123 BoundIDctMatrix (int *matrix, int Bound)
124 {
125  int *mptr;
126 
127  for (mptr = matrix; mptr < matrix + DCTSIZE2; mptr++)
128  {
129  if (*mptr < 0)
130  {
131  *mptr = 0;
132  }
133  else if (*mptr > Bound)
134  {
135  *mptr = Bound;
136  }
137  }
138 }
139 
140 
141 
142 void
143 WriteOneBlock (int *store, unsigned char *out_buf, int width, int height,
144  int voffs, int hoffs)
145 {
146  int i, e;
147 
148 
149  /* Find vertical buffer offs. */
150  for (i = voffs; i < voffs + DCTSIZE; i++)
151  {
152  if (i < height)
153  {
154  int diff;
155  diff = width * i;
156  for (e = hoffs; e < hoffs + DCTSIZE; e++)
157  {
158  if (e < width)
159  {
160  out_buf[diff + e] = (unsigned char) (*(store++));
161  }
162  else
163  {
164  break;
165  }
166  }
167  }
168  else
169  {
170  break;
171  }
172  }
173 
174 
175 }
176 
177 /*
178  * WriteBlock() writes an array of data in the integer array pointed to
179  * by store out to the driver specified by the IOB. The integer array is
180  * stored in row-major form, that is, the first row of (8) elements, the
181  * second row of (8) elements....
182  * ONLY for MCU 1:1:1
183  */
184 void
185 WriteBlock (int *store, int *p_out_vpos, int *p_out_hpos,
186  unsigned char *p_out_buf)
187 {
188  int voffs, hoffs;
189 
190  /*
191  * Get vertical offsets
192  */
193  voffs = *p_out_vpos * DCTSIZE;
194  hoffs = *p_out_hpos * DCTSIZE;
195 
196  /*
197  * Write block
198  */
199  WriteOneBlock (store,
200  p_out_buf,
202 
203  /*
204  * Add positions
205  */
206  (*p_out_hpos)++;
207  (*p_out_vpos)++;
208 
209  if (*p_out_hpos < p_jinfo_MCUWidth)
210  {
211  (*p_out_vpos)--;
212  }
213  else
214  {
215  *p_out_hpos = 0; /* If at end of image (width) */
216  }
217 }
218 
219 /*
220  * 4:1:1
221  */
222 void
223 Write4Blocks (int *store1, int *store2, int *store3, int *store4,
224  int *p_out_vpos, int *p_out_hpos, unsigned char *p_out_buf)
225 {
226  int voffs, hoffs;
227 
228  /*
229  * OX
230  * XX
231  */
232  voffs = *p_out_vpos * DCTSIZE;
233  hoffs = *p_out_hpos * DCTSIZE;
234  WriteOneBlock (store1, p_out_buf,
236 
237  /*
238  * XO
239  * XX
240  */
241  hoffs += DCTSIZE;
242  WriteOneBlock (store2, p_out_buf,
244 
245  /*
246  * XX
247  * OX
248  */
249  voffs += DCTSIZE;
250  hoffs -= DCTSIZE;
251  WriteOneBlock (store3, p_out_buf,
253 
254 
255  /*
256  * XX
257  * XO
258  */
259  hoffs += DCTSIZE;
260  WriteOneBlock (store4,
262  voffs, hoffs);
263 
264  /*
265  * Add positions
266  */
267  *p_out_hpos = *p_out_hpos + 2;
268  *p_out_vpos = *p_out_vpos + 2;
269 
270 
271  if (*p_out_hpos < p_jinfo_MCUWidth)
272  {
273  *p_out_vpos = *p_out_vpos - 2;
274  }
275  else
276  {
277  *p_out_hpos = 0; /* If at end of image (width) */
278  }
279 }
280 
281 
282 /*
283  * Transform from Yuv into RGB
284  */
285 void
286 YuvToRgb (int p, int *y_buf, int *u_buf, int *v_buf)
287 {
288  int r, g, b;
289  int y, u, v;
290  int i;
291 
292  for (i = 0; i < DCTSIZE2; i++)
293  {
294  y = y_buf[i];
295  u = u_buf[i] - 128;
296  v = v_buf[i] - 128;
297 
298  r = (y * 256 + v * 359 + 128) >> 8;
299  g = (y * 256 - u * 88 - v * 182 + 128) >> 8;
300  b = (y * 256 + u * 454 + 128) >> 8;
301 
302  if (r < 0)
303  r = 0;
304  else if (r > 255)
305  r = 255;
306 
307  if (g < 0)
308  g = 0;
309  else if (g > 255)
310  g = 255;
311 
312  if (b < 0)
313  b = 0;
314  else if (b > 255)
315  b = 255;
316 
317  rgb_buf[p][0][i] = r;
318  rgb_buf[p][1][i] = g;
319  rgb_buf[p][2][i] = b;
320 
321  }
322 }
323 
324 
325 /*
326  * Decode one block
327  */
328 void
329 decode_block (int comp_no, int *out_buf, int *HuffBuff)
330 {
331  int QuantBuff[DCTSIZE2];
332  unsigned int *p_quant_tbl;
333 
334  DecodeHuffMCU (HuffBuff, comp_no);
335 
336  IZigzagMatrix (HuffBuff, QuantBuff);
337 
338  p_quant_tbl =
340  IQuantize (QuantBuff, p_quant_tbl);
341 
342  ChenIDct (QuantBuff, out_buf);
343 
344  PostshiftIDctMatrix (out_buf, IDCT_SHIFT);
345 
346  BoundIDctMatrix (out_buf, IDCT_BOUNT);
347 
348 }
349 
350 
351 void
352 decode_start (int *out_data_image_width, int *out_data_image_height,
353  int *out_data_comp_vpos, int *out_data_comp_hpos)
354 {
355  int i;
356  int CurrentMCU = 0;
357  int HuffBuff[NUM_COMPONENT][DCTSIZE2];
358  int IDCTBuff[6][DCTSIZE2];
359 
360  /* Read buffer */
362 
363  /*
364  * Initial value of DC element is 0
365  */
366  for (i = 0; i < NUM_COMPONENT; i++)
367  {
368  HuffBuff[i][0] = 0;
369  }
370 
371  /*
372  * Set the size of image to output buffer
373  */
374  *out_data_image_width = p_jinfo_image_width;
375  *out_data_image_height = p_jinfo_image_height;
376 
377  /*
378  * Initialize output buffer
379  */
380  for (i = 0; i < RGB_NUM; i++)
381  {
382  out_data_comp_vpos[i] = 0;
383  out_data_comp_hpos[i] = 0;
384  }
385 
386 
387  if (p_jinfo_smp_fact == SF1_1_1)
388  {
389  printf ("Decode 1:1:1 NumMCU = %d\n", p_jinfo_NumMCU);
390 
391  /*
392  * 1_1_1
393  */
394  while (CurrentMCU < p_jinfo_NumMCU)
395  {
396 
397  for (i = 0; i < NUM_COMPONENT; i++)
398  {
399  decode_block (i, IDCTBuff[i], HuffBuff[i]);
400  }
401 
402 
403  YuvToRgb (0, IDCTBuff[0], IDCTBuff[1], IDCTBuff[2]);
404  /*
405  * Write
406  */
407  for (i = 0; i < RGB_NUM; i++)
408  {
409  WriteBlock (&rgb_buf[0][i][0],
410  &out_data_comp_vpos[i],
411  &out_data_comp_hpos[i], &OutData_comp_buf[i][0]);
412  }
413  CurrentMCU++;
414  }
415 
416  }
417  else
418  {
419  printf ("Decode 4:1:1 NumMCU = %d\n", p_jinfo_NumMCU);
420  /*
421  * 4_1_1
422  */
423  while (CurrentMCU < p_jinfo_NumMCU)
424  {
425  /*
426  * Decode Y element
427  * Decoding Y, U and V elements should be sequentially conducted for the use of Huffman table
428  */
429 
430  for (i = 0; i < 4; i++)
431  {
432  decode_block (0, IDCTBuff[i], HuffBuff[0]);
433  }
434 
435  /* Decode U */
436  decode_block (1, IDCTBuff[4], HuffBuff[1]);
437 
438  /* Decode V */
439  decode_block (2, IDCTBuff[5], HuffBuff[2]);
440 
441 
442  /* Transform from Yuv into RGB */
443 
444  for (i = 0; i < 4; i++)
445  {
446  YuvToRgb (i, IDCTBuff[i], IDCTBuff[4], IDCTBuff[5]);
447  }
448 
449 
450  for (i = 0; i < RGB_NUM; i++)
451  {
452  Write4Blocks (&rgb_buf[0][i][0],
453  &rgb_buf[1][i][0],
454  &rgb_buf[2][i][0],
455  &rgb_buf[3][i][0],
456  &out_data_comp_vpos[i],
457  &out_data_comp_hpos[i], &OutData_comp_buf[i][0]);
458  }
459 
460 
461  CurrentMCU += 4;
462  }
463  }
464 }
void IZigzagMatrix(int *imatrix, int *omatrix)
Definition: decode.c:72
void decode_start(int *out_data_image_width, int *out_data_image_height, int *out_data_comp_vpos, int *out_data_comp_hpos)
Definition: decode.c:352
#define DCTSIZE
Definition: decode.h:48
unsigned int p_jinfo_quant_tbl_quantval[NUM_QUANT_TBLS][DCTSIZE2]
Definition: decode.h:84
short p_jinfo_image_width
Definition: decode.h:72
char p_jinfo_comps_info_quant_tbl_no[NUM_COMPONENT]
Definition: decode.h:80
#define NUM_COMPONENT
Definition: decode.h:55
void ChenIDct(int *x, int *y)
Definition: chenidct.c:79
void PostshiftIDctMatrix(int *matrix, int shift)
Definition: decode.c:108
#define IDCT_SHIFT
Definition: decode.h:62
unsigned char * p_jinfo_jpeg_data
Definition: decode.h:106
int p_jinfo_smp_fact
Definition: decode.h:74
unsigned char * CurHuffReadBuf
Definition: huffman.h:38
#define SF1_1_1
Definition: decode.h:67
#define IDCT_BOUNT
Definition: decode.h:63
void DecodeHuffMCU(int *out_buf, int num_cmp)
Definition: huffman.c:281
void BoundIDctMatrix(int *matrix, int Bound)
Definition: decode.c:123
short p_jinfo_image_height
Definition: decode.h:71
void IQuantize(int *matrix, unsigned int *qmatrix)
Definition: decode.c:91
void YuvToRgb(int p, int *y_buf, int *u_buf, int *v_buf)
Definition: decode.c:286
int p_jinfo_NumMCU
Definition: decode.h:104
void WriteBlock(int *store, int *p_out_vpos, int *p_out_hpos, unsigned char *p_out_buf)
Definition: decode.c:185
#define DCTSIZE2
Definition: decode.h:49
int p_jinfo_MCUWidth
Definition: decode.h:102
#define RGB_NUM
Definition: decode.h:57
x
Return the smallest n such that 2^n >= _x.
void Write4Blocks(int *store1, int *store2, int *store3, int *store4, int *p_out_vpos, int *p_out_hpos, unsigned char *p_out_buf)
Definition: decode.c:223
void WriteOneBlock(int *store, unsigned char *out_buf, int width, int height, int voffs, int hoffs)
Definition: decode.c:143
const int zigzag_index[64]
Definition: decode.c:55
unsigned char OutData_comp_buf[RGB_NUM][BMP_OUT_SIZE]
Definition: init.h:46
void decode_block(int comp_no, int *out_buf, int *HuffBuff)
Definition: decode.c:329
int rgb_buf[4][RGB_NUM][DCTSIZE2]
Definition: decode.c:53

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13