aboutsummaryrefslogtreecommitdiffstats
path: root/benchmarks/CHStone/jpeg/marker.c
blob: 48802652b54dca296c1703c7461e3c697923091b (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
+--------------------------------------------------------------------------+
| CHStone : a suite of benchmark programs for C-based High-Level Synthesis |
| ======================================================================== |
|                                                                          |
| * Collected and Modified : Y. Hara, H. Tomiyama, S. Honda,               |
|                            H. Takada and K. Ishii                        |
|                            Nagoya University, Japan                      |
|                                                                          |
| * Remark :                                                               |
|    1. This source code is modified to unify the formats of the benchmark |
|       programs in CHStone.                                               |
|    2. Test vectors are added for CHStone.                                |
|    3. If "main_result" is 0 at the end of the program, the program is    |
|       correctly executed.                                                |
|    4. Please follow the copyright of each benchmark program.             |
+--------------------------------------------------------------------------+
*/
/*
 *  Read the head of the marker
 *
 *  @(#) $Id: marker.c,v 1.2 2003/07/18 10:19:21 honda Exp $
 */
/*************************************************************
Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
Group. If you use this software, you agree to the following: This
program package is purely experimental, and is licensed "as is".
Permission is granted to use, modify, and distribute this program
without charge for any purpose, provided this license/ disclaimer
notice appears in the copies.  No warranty or maintenance is given,
either expressed or implied.  In no event shall the author(s) be
liable to you or a third party for any special, incidental,
consequential, or other damages, arising out of the use or inability
to use the program for any purpose (or the loss of data), even if we
have been advised of such possibilities.  Any public reference or
advertisement of this source code should refer to it as the Portable
Video Research Group (PVRG) code, and not by any author(s) (or
Stanford University) name.
*************************************************************/
/*
************************************************************
marker.c

This file contains the Marker library which uses the direct buffer
access routines bgetc...

************************************************************
*/

/* Only for the marker needed at the baseline */
/* JPEG marker codes */
#define    M_SOI   0xd8		/* Start of Image */
#define    M_SOF0  0xc0		/* Baseline DCT ( Huffman ) */
#define    M_SOS   0xda		/* Start of Scan ( Head of Compressed Data ) */
#define    M_DHT   0xc4		/* Huffman Table */
#define    M_DQT   0xdb		/* Quantization Table */
#define    M_EOI   0xd9		/* End of Image */

/*
+--------------------------------------------------------------------------+
| * Test Vectors (added for CHStone) for "read_markers"                    |
|     out_unread_marker : expected output data                             |
+--------------------------------------------------------------------------+
*/
#define READ_MARKERS_REP 10
int i_marker = 0;
const int out_unread_marker[READ_MARKERS_REP] =
  { 0xd8, 0xe0, 0xdb, 0xdb, 0xc0, 0xc4, 0xc4, 0xc4, 0xc4, 0xda };

/*
+--------------------------------------------------------------------------+
| * Test Vectors (added for CHStone) for "get_sof"                         |
|     out_length_get_sof : expected output data                            |
|     out_data_precision_get_sof : expected output data                    |
|     out_p_jinfo_image_height_get_sof : expected output data              |
|     out_p_jinfo_image_width_get_sof : expected output data               |
|     out_p_jinfo_num_components_get_sof : expected output data            |
|     out_index_get_sof : expected output data                             |
|     out_id_get_sof : expected output data                                |
|     out_h_samp_factor_get_sof : expected output data                     |
|     out_v_samp_factor_get_sof : expected output data                     |
|     out_quant_tbl_no_get_sof : expected output data                      |
+--------------------------------------------------------------------------+
*/
#define GET_SOF_REP 3
int out_length_get_sof = 17;
int out_data_precision_get_sof = 8;
int out_p_jinfo_image_height_get_sof = 59;
int out_p_jinfo_image_width_get_sof = 90;
int out_p_jinfo_num_components_get_sof = 3;
const int out_index_get_sof[GET_SOF_REP] = { 0, 1, 2 };
const int out_id_get_sof[GET_SOF_REP] = { 1, 2, 3 };
const int out_h_samp_factor_get_sof[GET_SOF_REP] = { 2, 1, 1 };
const int out_v_samp_factor_get_sof[GET_SOF_REP] = { 2, 1, 1 };
const int out_quant_tbl_no_get_sof[GET_SOF_REP] = { 0, 1, 1 };

/*
+--------------------------------------------------------------------------+
| * Test Vectors (added for CHStone) for "get_sos"                         |
|     out_length_get_sos : expected output data                            |
|     out_num_comp_get_sos : expected output data                          |
|     out_comp_id_get_sos : expected output data                           |
|     out_dc_tbl_no_get_sos : expected output data                         |
|     out_ac_tbl_no_get_sos : expected output data                         |
+--------------------------------------------------------------------------+
*/
#define GET_SOS_REP 3
int i_get_sos = 0;
int out_length_get_sos = 12;
int out_num_comp_get_sos = 3;
const int out_comp_id_get_sos[GET_SOS_REP] = { 1, 2, 3 };
const int out_dc_tbl_no_get_sos[GET_SOS_REP] = { 0, 1, 1 };
const int out_ac_tbl_no_get_sos[GET_SOS_REP] = { 0, 1, 1 };

/*
+--------------------------------------------------------------------------+
| * Test Vectors (added for CHStone) for "get_dht"                         |
|     out_length_get_dht : expected output data                            |
|     out_index_get_dht : expected output data                             |
|     out_count_get_dht : expected output data                             |
+--------------------------------------------------------------------------+
*/
#define GET_DHT_REP 4
int i_get_dht = 0;
const int out_length_get_dht[GET_DHT_REP] = { 29, 179, 29, 179 };
const int out_index_get_dht[GET_DHT_REP] = { 0x0, 0x10, 0x1, 0x11 };
const int out_count_get_dht[GET_DHT_REP] = { 12, 162, 12, 162 };

/*
+--------------------------------------------------------------------------+
| * Test Vectors (added for CHStone) for "get_dqt"                         |
|     out_length_get_dqt : expected output data                            |
|     out_prec_get_dht : expected output data                              |
|     out_num_get_dht : expected output data                               |
+--------------------------------------------------------------------------+
*/
#define GET_DQT_REP 2
int i_get_dqt = 0;
const int out_length_get_dqt[GET_DQT_REP] = { 65, 65 };
const int out_prec_get_dht[GET_DQT_REP] = { 0, 0 };
const int out_num_get_dht[GET_DQT_REP] = { 0, 1 };


/*
 * Initialize Quantization Table
 */
const int izigzag_index[64] = { 0, 1, 8, 16, 9, 2, 3, 10,
  17, 24, 32, 25, 18, 11, 4, 5,
  12, 19, 26, 33, 40, 48, 41, 34,
  27, 20, 13, 6, 7, 14, 21, 28,
  35, 42, 49, 56, 57, 50, 43, 36,
  29, 22, 15, 23, 30, 37, 44, 51,
  58, 59, 52, 45, 38, 31, 39, 46,
  53, 60, 61, 54, 47, 55, 62, 63
};


/*
 *  Read Buffer
 */
static unsigned char *ReadBuf;


/*
 *  Read from Buffer
 */
int
read_byte (void)
{
  return *ReadBuf++;
}


short
read_word (void)
{
  short c;

  c = *ReadBuf++ << 8;
  c |= *ReadBuf++;

  return c;
}

int
first_marker (void)
{
  int c1, c2;
  c1 = read_byte ();
  c2 = read_byte ();

  if (c1 != 0xFF || c2 != M_SOI)
    {
      main_result++;
      printf ("Not Jpeg File!\n");
      EXIT;
    }

  return c2;
}


int
next_marker (void)
{
  int c;

  for (;;)
    {
      c = read_byte ();

      while (c != 0xff)
	c = read_byte ();

      do
	{
	  c = read_byte ();
	}
      while (c == 0xff);
      if (c != 0)
	break;
    }
  return c;
}


/*
 *  Baseline DCT ( Huffman )  
 */
void
get_sof ()
{
  int ci, c;
  int length;
  char *p_comp_info_index;
  char *p_comp_info_id;
  char *p_comp_info_h_samp_factor;
  char *p_comp_info_v_samp_factor;
  char *p_comp_info_quant_tbl_no;

  length = read_word ();
  p_jinfo_data_precision = read_byte ();
  p_jinfo_image_height = read_word ();
  p_jinfo_image_width = read_word ();
  p_jinfo_num_components = read_byte ();

  printf ("length         = %d\n", length);
  printf ("data_precision = %d\n", p_jinfo_data_precision);
  printf ("image_height   = %d\n", p_jinfo_image_height);
  printf ("image_width    = %d\n", p_jinfo_image_width);
  printf ("num_components = %d\n", p_jinfo_num_components);

  if (length != out_length_get_sof)
    {
        main_result++;
    }
  if (p_jinfo_data_precision != out_data_precision_get_sof)
    {
        main_result++;
    }
  if (p_jinfo_image_height != out_p_jinfo_image_height_get_sof)
    {
        main_result++;
    }
  if (p_jinfo_image_width != out_p_jinfo_image_width_get_sof)
    {
        main_result++;
    }
  if (p_jinfo_num_components != out_p_jinfo_num_components_get_sof)
    {
        main_result++;
    }

  length -= 8;

  /* Omit error check */

  /* Check components */
  for (ci = 0; ci < p_jinfo_num_components; ci++)
    {
      p_comp_info_index = &p_jinfo_comps_info_index[ci];
      p_comp_info_id = &p_jinfo_comps_info_id[ci];
      p_comp_info_h_samp_factor = &p_jinfo_comps_info_h_samp_factor[ci];
      p_comp_info_v_samp_factor = &p_jinfo_comps_info_v_samp_factor[ci];
      p_comp_info_quant_tbl_no = &p_jinfo_comps_info_quant_tbl_no[ci];

      *p_comp_info_index = ci;
      *p_comp_info_id = read_byte ();
      c = read_byte ();
      *p_comp_info_h_samp_factor = (c >> 4) & 15;
      *p_comp_info_v_samp_factor = (c) & 15;
      *p_comp_info_quant_tbl_no = read_byte ();

      printf (" index         = %d\n", *p_comp_info_index);
      printf (" id            = %d\n", *p_comp_info_id);
      printf (" h_samp_factor = %d\n", *p_comp_info_h_samp_factor);
      printf (" v_samp_factor = %d\n", *p_comp_info_v_samp_factor);
      printf (" quant_tbl_no  = %d\n\n", *p_comp_info_quant_tbl_no);

      if (*p_comp_info_index != out_index_get_sof[ci])
	{
		main_result++;
	}
      if (*p_comp_info_id != out_id_get_sof[ci])
	{
		main_result++;
	}
      if (*p_comp_info_h_samp_factor != out_h_samp_factor_get_sof[ci])
	{
		main_result++;
	}
      if (*p_comp_info_v_samp_factor != out_v_samp_factor_get_sof[ci])
	{
		main_result++;
	}
      if (*p_comp_info_quant_tbl_no != out_quant_tbl_no_get_sof[ci])
	{
		main_result++;
	}

    }

  /*
   *  Determine Sampling Factor
   *  Only 1_1_1 and 4_1_1 are supported
   */
  if (p_jinfo_comps_info_h_samp_factor[0] == 2)
    {
      p_jinfo_smp_fact = SF4_1_1;
      printf ("\nSampling Factor is 4:1:1\n");
    }
  else
    {
      p_jinfo_smp_fact = SF1_1_1;
      printf ("\nSampling Factor is 1:1:1\n");
    }
}


void
get_sos ()
{
  int length, num_comp;
  int i, c, cc, ci, j;
  char *p_comp_info_id;
  char *p_comp_info_dc_tbl_no;
  char *p_comp_info_ac_tbl_no;

  length = read_word ();
  num_comp = read_byte ();

  printf (" length = %d\n", length);
  printf (" num_comp = %d\n", num_comp);

  if (length != out_length_get_sos)
    {
        main_result++;
    }
  if (num_comp != out_num_comp_get_sos)
    {
        main_result++;
    }

  /* Decode each component */
  for (i = 0; i < num_comp; i++)
    {
      cc = read_byte ();
      c = read_byte ();

      for (ci = 0; ci < p_jinfo_num_components; ci++)
	{
	  p_comp_info_id = &p_jinfo_comps_info_id[ci];
	  p_comp_info_dc_tbl_no = &p_jinfo_comps_info_dc_tbl_no[ci];
	  p_comp_info_ac_tbl_no = &p_jinfo_comps_info_ac_tbl_no[ci];

	  if (cc == *p_comp_info_id)
	    goto id_found;
	}
      main_result++;
      printf ("Bad Component ID!\n");
      EXIT;

    id_found:
      *p_comp_info_dc_tbl_no = (c >> 4) & 15;
      *p_comp_info_ac_tbl_no = (c) & 15;

      printf (" comp_id       = %d\n", cc);
      printf (" dc_tbl_no     = %d\n", *p_comp_info_dc_tbl_no);
      printf (" ac_tbl_no     = %d\n", *p_comp_info_ac_tbl_no);

      if (cc != out_comp_id_get_sos[i_get_sos])
	{
		main_result++;
	}
      if (*p_comp_info_dc_tbl_no != out_dc_tbl_no_get_sos[i_get_sos])
	{
		main_result++;
	}
      if (*p_comp_info_ac_tbl_no != out_ac_tbl_no_get_sos[i_get_sos])
	{
		main_result++;
	}
      i_get_sos++;

    }

  /* Pass parameters; Ss, Se, Ah and Al for progressive JPEG */
  j = 3;
  while (j--)
    {
      c = read_byte ();
    }

  /*
   * Define the Buffer at this point as the head of data
   */
  p_jinfo_jpeg_data = ReadBuf;

}


/*
 * Get Huffman Table
 */
void
get_dht ()
{
  int length;
  int index, i, count;
  int *p_xhtbl_bits;
  int *p_xhtbl_huffval;

  length = read_word ();
  length -= 2;

  printf (" length = %d\n", length);

  if (length != out_length_get_dht[i_get_dht])
    {
        main_result++;
    }

  while (length > 16)
    {
      index = read_byte ();

      printf (" index = 0x%x\n", index);

      if (index != out_index_get_dht[i_get_dht])
	{
        main_result++;
}

      if (index & 0x10)
	{
	  /* AC */
	  index -= 0x10;
	  p_xhtbl_bits = p_jinfo_ac_xhuff_tbl_bits[index];
	  p_xhtbl_huffval = p_jinfo_ac_xhuff_tbl_huffval[index];
	}
      else
	{
	  /* DC */
	  p_xhtbl_bits = p_jinfo_dc_xhuff_tbl_bits[index];
	  p_xhtbl_huffval = p_jinfo_dc_xhuff_tbl_huffval[index];
	}

      count = 0;

      for (i = 1; i <= 16; i++)
	{
	  p_xhtbl_bits[i] = read_byte ();
	  count += p_xhtbl_bits[i];
	}

      printf (" count = %d\n", count);

      if (count != out_count_get_dht[i_get_dht])
	{
        main_result++;
    }
      i_get_dht++;

      length -= 1 + 16;

      for (i = 0; i < count; i++)
	{
	  p_xhtbl_huffval[i] = read_byte ();
	}

      length -= count;
    }
}


void
get_dqt ()
{
  int length;
  int prec, num, i;
  unsigned int tmp;
  unsigned int *p_quant_tbl;

  length = read_word ();
  length -= 2;

  printf (" length = %d\n", length);

  if (length != out_length_get_dqt[i_get_dqt])
    {
        main_result++;
    }

  while (length > 0)
    {
      num = read_byte ();
      /* Precision 0:8bit, 1:16bit */
      prec = num >> 4;
      /* Table Number */
      num &= 0x0f;

      printf (" prec = %d\n", prec);
      printf (" num  = %d\n", num);

      if (prec != out_prec_get_dht[i_get_dqt])
	{
        main_result++;
    }
      if (num != out_num_get_dht[i_get_dqt])
	{
        main_result++;
    }
      i_get_dqt++;

      p_quant_tbl = &p_jinfo_quant_tbl_quantval[num][DCTSIZE2];
      for (i = 0; i < DCTSIZE2; i++)
	{
	  if (prec)
	    tmp = read_word ();
	  else
	    tmp = read_byte ();
	  p_quant_tbl[izigzag_index[i]] = (unsigned short) tmp;
	}

      length -= DCTSIZE2 + 1;
      if (prec)
	length -= DCTSIZE2;
    }
}



void
read_markers (unsigned char *buf)
{
  int unread_marker;
  int sow_SOI;

  ReadBuf = buf;

  sow_SOI = 0;

  unread_marker = 0;

  /* Read the head of the marker */
  for (;;)
    {
      if (!sow_SOI)
	{
	  unread_marker = first_marker ();
	}
      else
	{
	  unread_marker = next_marker ();
	}

      printf ("\nmarker = 0x%x\n", unread_marker);

      if (unread_marker != out_unread_marker[i_marker++])
	{
		main_result++;
	}


      switch (unread_marker)
	{
	case M_SOI:		/* Start of Image */
	  sow_SOI = 1;
	  break;

	case M_SOF0:		/* Baseline DCT ( Huffman ) */
	  get_sof ();
	  break;

	case M_SOS:		/* Start of Scan ( Head of Compressed Data ) */
	  get_sos ();
	  return;

	case M_DHT:
	  get_dht ();
	  break;

	case M_DQT:
	  get_dqt ();
	  break;

	case M_EOI:
	  return;
	}
    }
}