PandA-2024.02
marker.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  * Read the head of the marker
21  *
22  * @(#) $Id: marker.c,v 1.2 2003/07/18 10:19:21 honda Exp $
23  */
24 /*************************************************************
25 Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
26 PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
27 Group. If you use this software, you agree to the following: This
28 program package is purely experimental, and is licensed "as is".
29 Permission is granted to use, modify, and distribute this program
30 without charge for any purpose, provided this license/ disclaimer
31 notice appears in the copies. No warranty or maintenance is given,
32 either expressed or implied. In no event shall the author(s) be
33 liable to you or a third party for any special, incidental,
34 consequential, or other damages, arising out of the use or inability
35 to use the program for any purpose (or the loss of data), even if we
36 have been advised of such possibilities. Any public reference or
37 advertisement of this source code should refer to it as the Portable
38 Video Research Group (PVRG) code, and not by any author(s) (or
39 Stanford University) name.
40 *************************************************************/
41 /*
42 ************************************************************
43 marker.c
44 
45 This file contains the Marker library which uses the direct buffer
46 access routines bgetc...
47 
48 ************************************************************
49 */
50 
51 /* Only for the marker needed at the baseline */
52 /* JPEG marker codes */
53 #define M_SOI 0xd8 /* Start of Image */
54 #define M_SOF0 0xc0 /* Baseline DCT ( Huffman ) */
55 #define M_SOS 0xda /* Start of Scan ( Head of Compressed Data ) */
56 #define M_DHT 0xc4 /* Huffman Table */
57 #define M_DQT 0xdb /* Quantization Table */
58 #define M_EOI 0xd9 /* End of Image */
59 
60 /*
61 +--------------------------------------------------------------------------+
62 | * Test Vectors (added for CHStone) for "read_markers" |
63 | out_unread_marker : expected output data |
64 +--------------------------------------------------------------------------+
65 */
66 #define READ_MARKERS_REP 10
67 int i_marker = 0;
69  { 0xd8, 0xe0, 0xdb, 0xdb, 0xc0, 0xc4, 0xc4, 0xc4, 0xc4, 0xda };
70 
71 /*
72 +--------------------------------------------------------------------------+
73 | * Test Vectors (added for CHStone) for "get_sof" |
74 | out_length_get_sof : expected output data |
75 | out_data_precision_get_sof : expected output data |
76 | out_p_jinfo_image_height_get_sof : expected output data |
77 | out_p_jinfo_image_width_get_sof : expected output data |
78 | out_p_jinfo_num_components_get_sof : expected output data |
79 | out_index_get_sof : expected output data |
80 | out_id_get_sof : expected output data |
81 | out_h_samp_factor_get_sof : expected output data |
82 | out_v_samp_factor_get_sof : expected output data |
83 | out_quant_tbl_no_get_sof : expected output data |
84 +--------------------------------------------------------------------------+
85 */
86 #define GET_SOF_REP 3
92 const int out_index_get_sof[GET_SOF_REP] = { 0, 1, 2 };
93 const int out_id_get_sof[GET_SOF_REP] = { 1, 2, 3 };
94 const int out_h_samp_factor_get_sof[GET_SOF_REP] = { 2, 1, 1 };
95 const int out_v_samp_factor_get_sof[GET_SOF_REP] = { 2, 1, 1 };
96 const int out_quant_tbl_no_get_sof[GET_SOF_REP] = { 0, 1, 1 };
97 
98 /*
99 +--------------------------------------------------------------------------+
100 | * Test Vectors (added for CHStone) for "get_sos" |
101 | out_length_get_sos : expected output data |
102 | out_num_comp_get_sos : expected output data |
103 | out_comp_id_get_sos : expected output data |
104 | out_dc_tbl_no_get_sos : expected output data |
105 | out_ac_tbl_no_get_sos : expected output data |
106 +--------------------------------------------------------------------------+
107 */
108 #define GET_SOS_REP 3
109 int i_get_sos = 0;
112 const int out_comp_id_get_sos[GET_SOS_REP] = { 1, 2, 3 };
113 const int out_dc_tbl_no_get_sos[GET_SOS_REP] = { 0, 1, 1 };
114 const int out_ac_tbl_no_get_sos[GET_SOS_REP] = { 0, 1, 1 };
115 
116 /*
117 +--------------------------------------------------------------------------+
118 | * Test Vectors (added for CHStone) for "get_dht" |
119 | out_length_get_dht : expected output data |
120 | out_index_get_dht : expected output data |
121 | out_count_get_dht : expected output data |
122 +--------------------------------------------------------------------------+
123 */
124 #define GET_DHT_REP 4
125 int i_get_dht = 0;
126 const int out_length_get_dht[GET_DHT_REP] = { 29, 179, 29, 179 };
127 const int out_index_get_dht[GET_DHT_REP] = { 0x0, 0x10, 0x1, 0x11 };
128 const int out_count_get_dht[GET_DHT_REP] = { 12, 162, 12, 162 };
129 
130 /*
131 +--------------------------------------------------------------------------+
132 | * Test Vectors (added for CHStone) for "get_dqt" |
133 | out_length_get_dqt : expected output data |
134 | out_prec_get_dht : expected output data |
135 | out_num_get_dht : expected output data |
136 +--------------------------------------------------------------------------+
137 */
138 #define GET_DQT_REP 2
139 int i_get_dqt = 0;
140 const int out_length_get_dqt[GET_DQT_REP] = { 65, 65 };
141 const int out_prec_get_dht[GET_DQT_REP] = { 0, 0 };
142 const int out_num_get_dht[GET_DQT_REP] = { 0, 1 };
143 
144 
145 /*
146  * Initialize Quantization Table
147  */
148 const int izigzag_index[64] = { 0, 1, 8, 16, 9, 2, 3, 10,
149  17, 24, 32, 25, 18, 11, 4, 5,
150  12, 19, 26, 33, 40, 48, 41, 34,
151  27, 20, 13, 6, 7, 14, 21, 28,
152  35, 42, 49, 56, 57, 50, 43, 36,
153  29, 22, 15, 23, 30, 37, 44, 51,
154  58, 59, 52, 45, 38, 31, 39, 46,
155  53, 60, 61, 54, 47, 55, 62, 63
156 };
157 
158 
159 /*
160  * Read Buffer
161  */
162 static unsigned char *ReadBuf;
163 
164 
165 /*
166  * Read from Buffer
167  */
168 int
169 read_byte (void)
170 {
171  return *ReadBuf++;
172 }
173 
174 
175 short
176 read_word (void)
177 {
178  short c;
179 
180  c = *ReadBuf++ << 8;
181  c |= *ReadBuf++;
182 
183  return c;
184 }
185 
186 int
188 {
189  int c1, c2;
190  c1 = read_byte ();
191  c2 = read_byte ();
192 
193  if (c1 != 0xFF || c2 != M_SOI)
194  {
195  main_result++;
196  printf ("Not Jpeg File!\n");
197  EXIT;
198  }
199 
200  return c2;
201 }
202 
203 
204 int
206 {
207  int c;
208 
209  for (;;)
210  {
211  c = read_byte ();
212 
213  while (c != 0xff)
214  c = read_byte ();
215 
216  do
217  {
218  c = read_byte ();
219  }
220  while (c == 0xff);
221  if (c != 0)
222  break;
223  }
224  return c;
225 }
226 
227 
228 /*
229  * Baseline DCT ( Huffman )
230  */
231 void
233 {
234  int ci, c;
235  int length;
236  char *p_comp_info_index;
237  char *p_comp_info_id;
238  char *p_comp_info_h_samp_factor;
239  char *p_comp_info_v_samp_factor;
240  char *p_comp_info_quant_tbl_no;
241 
242  length = read_word ();
247 
248  printf ("length = %d\n", length);
249  printf ("data_precision = %d\n", p_jinfo_data_precision);
250  printf ("image_height = %d\n", p_jinfo_image_height);
251  printf ("image_width = %d\n", p_jinfo_image_width);
252  printf ("num_components = %d\n", p_jinfo_num_components);
253 
254  if (length != out_length_get_sof)
255  {
256  main_result++;
257  }
259  {
260  main_result++;
261  }
263  {
264  main_result++;
265  }
267  {
268  main_result++;
269  }
271  {
272  main_result++;
273  }
274 
275  length -= 8;
276 
277  /* Omit error check */
278 
279  /* Check components */
280  for (ci = 0; ci < p_jinfo_num_components; ci++)
281  {
282  p_comp_info_index = &p_jinfo_comps_info_index[ci];
283  p_comp_info_id = &p_jinfo_comps_info_id[ci];
284  p_comp_info_h_samp_factor = &p_jinfo_comps_info_h_samp_factor[ci];
285  p_comp_info_v_samp_factor = &p_jinfo_comps_info_v_samp_factor[ci];
286  p_comp_info_quant_tbl_no = &p_jinfo_comps_info_quant_tbl_no[ci];
287 
288  *p_comp_info_index = ci;
289  *p_comp_info_id = read_byte ();
290  c = read_byte ();
291  *p_comp_info_h_samp_factor = (c >> 4) & 15;
292  *p_comp_info_v_samp_factor = (c) & 15;
293  *p_comp_info_quant_tbl_no = read_byte ();
294 
295  printf (" index = %d\n", *p_comp_info_index);
296  printf (" id = %d\n", *p_comp_info_id);
297  printf (" h_samp_factor = %d\n", *p_comp_info_h_samp_factor);
298  printf (" v_samp_factor = %d\n", *p_comp_info_v_samp_factor);
299  printf (" quant_tbl_no = %d\n\n", *p_comp_info_quant_tbl_no);
300 
301  if (*p_comp_info_index != out_index_get_sof[ci])
302  {
303  main_result++;
304  }
305  if (*p_comp_info_id != out_id_get_sof[ci])
306  {
307  main_result++;
308  }
309  if (*p_comp_info_h_samp_factor != out_h_samp_factor_get_sof[ci])
310  {
311  main_result++;
312  }
313  if (*p_comp_info_v_samp_factor != out_v_samp_factor_get_sof[ci])
314  {
315  main_result++;
316  }
317  if (*p_comp_info_quant_tbl_no != out_quant_tbl_no_get_sof[ci])
318  {
319  main_result++;
320  }
321 
322  }
323 
324  /*
325  * Determine Sampling Factor
326  * Only 1_1_1 and 4_1_1 are supported
327  */
329  {
331  printf ("\nSampling Factor is 4:1:1\n");
332  }
333  else
334  {
336  printf ("\nSampling Factor is 1:1:1\n");
337  }
338 }
339 
340 
341 void
343 {
344  int length, num_comp;
345  int i, c, cc, ci, j;
346  char *p_comp_info_id;
347  char *p_comp_info_dc_tbl_no;
348  char *p_comp_info_ac_tbl_no;
349 
350  length = read_word ();
351  num_comp = read_byte ();
352 
353  printf (" length = %d\n", length);
354  printf (" num_comp = %d\n", num_comp);
355 
356  if (length != out_length_get_sos)
357  {
358  main_result++;
359  }
360  if (num_comp != out_num_comp_get_sos)
361  {
362  main_result++;
363  }
364 
365  /* Decode each component */
366  for (i = 0; i < num_comp; i++)
367  {
368  cc = read_byte ();
369  c = read_byte ();
370 
371  for (ci = 0; ci < p_jinfo_num_components; ci++)
372  {
373  p_comp_info_id = &p_jinfo_comps_info_id[ci];
374  p_comp_info_dc_tbl_no = &p_jinfo_comps_info_dc_tbl_no[ci];
375  p_comp_info_ac_tbl_no = &p_jinfo_comps_info_ac_tbl_no[ci];
376 
377  if (cc == *p_comp_info_id)
378  goto id_found;
379  }
380  main_result++;
381  printf ("Bad Component ID!\n");
382  EXIT;
383 
384  id_found:
385  *p_comp_info_dc_tbl_no = (c >> 4) & 15;
386  *p_comp_info_ac_tbl_no = (c) & 15;
387 
388  printf (" comp_id = %d\n", cc);
389  printf (" dc_tbl_no = %d\n", *p_comp_info_dc_tbl_no);
390  printf (" ac_tbl_no = %d\n", *p_comp_info_ac_tbl_no);
391 
392  if (cc != out_comp_id_get_sos[i_get_sos])
393  {
394  main_result++;
395  }
396  if (*p_comp_info_dc_tbl_no != out_dc_tbl_no_get_sos[i_get_sos])
397  {
398  main_result++;
399  }
400  if (*p_comp_info_ac_tbl_no != out_ac_tbl_no_get_sos[i_get_sos])
401  {
402  main_result++;
403  }
404  i_get_sos++;
405 
406  }
407 
408  /* Pass parameters; Ss, Se, Ah and Al for progressive JPEG */
409  j = 3;
410  while (j--)
411  {
412  c = read_byte ();
413  }
414 
415  /*
416  * Define the Buffer at this point as the head of data
417  */
419 
420 }
421 
422 
423 /*
424  * Get Huffman Table
425  */
426 void
428 {
429  int length;
430  int index, i, count;
431  int *p_xhtbl_bits;
432  int *p_xhtbl_huffval;
433 
434  length = read_word ();
435  length -= 2;
436 
437  printf (" length = %d\n", length);
438 
439  if (length != out_length_get_dht[i_get_dht])
440  {
441  main_result++;
442  }
443 
444  while (length > 16)
445  {
446  index = read_byte ();
447 
448  printf (" index = 0x%x\n", index);
449 
450  if (index != out_index_get_dht[i_get_dht])
451  {
452  main_result++;
453 }
454 
455  if (index & 0x10)
456  {
457  /* AC */
458  index -= 0x10;
459  p_xhtbl_bits = p_jinfo_ac_xhuff_tbl_bits[index];
460  p_xhtbl_huffval = p_jinfo_ac_xhuff_tbl_huffval[index];
461  }
462  else
463  {
464  /* DC */
465  p_xhtbl_bits = p_jinfo_dc_xhuff_tbl_bits[index];
466  p_xhtbl_huffval = p_jinfo_dc_xhuff_tbl_huffval[index];
467  }
468 
469  count = 0;
470 
471  for (i = 1; i <= 16; i++)
472  {
473  p_xhtbl_bits[i] = read_byte ();
474  count += p_xhtbl_bits[i];
475  }
476 
477  printf (" count = %d\n", count);
478 
479  if (count != out_count_get_dht[i_get_dht])
480  {
481  main_result++;
482  }
483  i_get_dht++;
484 
485  length -= 1 + 16;
486 
487  for (i = 0; i < count; i++)
488  {
489  p_xhtbl_huffval[i] = read_byte ();
490  }
491 
492  length -= count;
493  }
494 }
495 
496 
497 void
499 {
500  int length;
501  int prec, num, i;
502  unsigned int tmp;
503  unsigned int *p_quant_tbl;
504 
505  length = read_word ();
506  length -= 2;
507 
508  printf (" length = %d\n", length);
509 
510  if (length != out_length_get_dqt[i_get_dqt])
511  {
512  main_result++;
513  }
514 
515  while (length > 0)
516  {
517  num = read_byte ();
518  /* Precision 0:8bit, 1:16bit */
519  prec = num >> 4;
520  /* Table Number */
521  num &= 0x0f;
522 
523  printf (" prec = %d\n", prec);
524  printf (" num = %d\n", num);
525 
526  if (prec != out_prec_get_dht[i_get_dqt])
527  {
528  main_result++;
529  }
530  if (num != out_num_get_dht[i_get_dqt])
531  {
532  main_result++;
533  }
534  i_get_dqt++;
535 
536  p_quant_tbl = &p_jinfo_quant_tbl_quantval[num][DCTSIZE2];
537  for (i = 0; i < DCTSIZE2; i++)
538  {
539  if (prec)
540  tmp = read_word ();
541  else
542  tmp = read_byte ();
543  p_quant_tbl[izigzag_index[i]] = (unsigned short) tmp;
544  }
545 
546  length -= DCTSIZE2 + 1;
547  if (prec)
548  length -= DCTSIZE2;
549  }
550 }
551 
552 
553 
554 void
555 read_markers (unsigned char *buf)
556 {
557  int unread_marker;
558  int sow_SOI;
559 
560  ReadBuf = buf;
561 
562  sow_SOI = 0;
563 
564  unread_marker = 0;
565 
566  /* Read the head of the marker */
567  for (;;)
568  {
569  if (!sow_SOI)
570  {
571  unread_marker = first_marker ();
572  }
573  else
574  {
575  unread_marker = next_marker ();
576  }
577 
578  printf ("\nmarker = 0x%x\n", unread_marker);
579 
580  if (unread_marker != out_unread_marker[i_marker++])
581  {
582  main_result++;
583  }
584 
585 
586  switch (unread_marker)
587  {
588  case M_SOI: /* Start of Image */
589  sow_SOI = 1;
590  break;
591 
592  case M_SOF0: /* Baseline DCT ( Huffman ) */
593  get_sof ();
594  break;
595 
596  case M_SOS: /* Start of Scan ( Head of Compressed Data ) */
597  get_sos ();
598  return;
599 
600  case M_DHT:
601  get_dht ();
602  break;
603 
604  case M_DQT:
605  get_dqt ();
606  break;
607 
608  case M_EOI:
609  return;
610  }
611  }
612 }
const int out_prec_get_dht[GET_DQT_REP]
Definition: marker.c:141
char p_jinfo_data_precision
Definition: decode.h:70
#define EXIT
Definition: global.h:46
int out_p_jinfo_image_height_get_sof
Definition: marker.c:89
int read_byte(void)
Definition: marker.c:169
char p_jinfo_comps_info_ac_tbl_no[NUM_COMPONENT]
Definition: decode.h:82
int i_get_dht
Definition: marker.c:125
char p_jinfo_comps_info_id[NUM_COMPONENT]
Definition: decode.h:77
TVMArray c2[1]
TVMArray c1[1]
const int out_id_get_sof[GET_SOF_REP]
Definition: marker.c:93
char p_jinfo_comps_info_h_samp_factor[NUM_COMPONENT]
Definition: decode.h:78
const int out_dc_tbl_no_get_sos[GET_SOS_REP]
Definition: marker.c:113
const int out_count_get_dht[GET_DHT_REP]
Definition: marker.c:128
int p_jinfo_ac_xhuff_tbl_bits[NUM_HUFF_TBLS][36]
Definition: decode.h:89
int out_length_get_sos
Definition: marker.c:110
void get_dqt()
Definition: marker.c:498
const int out_length_get_dqt[GET_DQT_REP]
Definition: marker.c:140
const int out_unread_marker[READ_MARKERS_REP]
Definition: marker.c:68
#define M_SOS
Definition: marker.c:55
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
int main_result
Definition: mips.c:38
char p_jinfo_comps_info_index[NUM_COMPONENT]
Definition: decode.h:76
char p_jinfo_num_components
Definition: decode.h:73
int first_marker(void)
Definition: marker.c:187
const int out_index_get_dht[GET_DHT_REP]
Definition: marker.c:127
const int out_index_get_sof[GET_SOF_REP]
Definition: marker.c:92
void get_dht()
Definition: marker.c:427
int i_get_dqt
Definition: marker.c:139
unsigned char * p_jinfo_jpeg_data
Definition: decode.h:106
int p_jinfo_smp_fact
Definition: decode.h:74
#define M_SOI
Definition: marker.c:53
#define SF1_1_1
Definition: decode.h:67
#define READ_MARKERS_REP
Definition: marker.c:66
#define index(x, y)
Definition: Keccak.c:74
static unsigned char * ReadBuf
Definition: marker.c:162
const int out_h_samp_factor_get_sof[GET_SOF_REP]
Definition: marker.c:94
#define M_SOF0
Definition: marker.c:54
const int out_num_get_dht[GET_DQT_REP]
Definition: marker.c:142
int out_length_get_sof
Definition: marker.c:87
char p_jinfo_comps_info_v_samp_factor[NUM_COMPONENT]
Definition: decode.h:79
#define GET_DQT_REP
Definition: marker.c:138
short p_jinfo_image_height
Definition: decode.h:71
#define GET_SOF_REP
Definition: marker.c:86
int out_p_jinfo_image_width_get_sof
Definition: marker.c:90
int p_jinfo_ac_xhuff_tbl_huffval[NUM_HUFF_TBLS][257]
Definition: decode.h:90
int out_data_precision_get_sof
Definition: marker.c:88
void get_sos()
Definition: marker.c:342
short read_word(void)
Definition: marker.c:176
const int out_comp_id_get_sos[GET_SOS_REP]
Definition: marker.c:112
int out_p_jinfo_num_components_get_sof
Definition: marker.c:91
int i_get_sos
Definition: marker.c:109
void read_markers(unsigned char *buf)
Definition: marker.c:555
int out_num_comp_get_sos
Definition: marker.c:111
#define GET_DHT_REP
Definition: marker.c:124
void get_sof()
Definition: marker.c:232
const int out_ac_tbl_no_get_sos[GET_SOS_REP]
Definition: marker.c:114
#define M_DQT
Definition: marker.c:57
int next_marker(void)
Definition: marker.c:205
int i_marker
Definition: marker.c:67
#define M_DHT
Definition: marker.c:56
#define DCTSIZE2
Definition: decode.h:49
char p_jinfo_comps_info_dc_tbl_no[NUM_COMPONENT]
Definition: decode.h:81
int p_jinfo_dc_xhuff_tbl_bits[NUM_HUFF_TBLS][36]
Definition: decode.h:86
const int out_length_get_dht[GET_DHT_REP]
Definition: marker.c:126
#define GET_SOS_REP
Definition: marker.c:108
const int izigzag_index[64]
Definition: marker.c:148
const int out_quant_tbl_no_get_sof[GET_SOF_REP]
Definition: marker.c:96
#define SF4_1_1
Definition: decode.h:68
int p_jinfo_dc_xhuff_tbl_huffval[NUM_HUFF_TBLS][257]
Definition: decode.h:87
#define M_EOI
Definition: marker.c:58
const int out_v_samp_factor_get_sof[GET_SOF_REP]
Definition: marker.c:95

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