diff VSs_tinyjpeg/tinyjpeg-parse.c @ 0:a8af8b3fc99d

initial commit
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Thu, 05 Jul 2012 11:35:03 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/VSs_tinyjpeg/tinyjpeg-parse.c	Thu Jul 05 11:35:03 2012 +0200
     1.3 @@ -0,0 +1,511 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <stdint.h>
     1.8 +#include <errno.h>
     1.9 +
    1.10 +#include "tinyjpeg.h"
    1.11 +#include "tinyjpeg-internal.h"
    1.12 +
    1.13 +extern char error_string[256];
    1.14 +
    1.15 +static const unsigned char zigzag[64] =
    1.16 +{
    1.17 +	0,  1,  5,  6, 14, 15, 27, 28,
    1.18 +	2,  4,  7, 13, 16, 26, 29, 42,
    1.19 +	3,  8, 12, 17, 25, 30, 41, 43,
    1.20 +	9, 11, 18, 24, 31, 40, 44, 53,
    1.21 +	10, 19, 23, 32, 39, 45, 52, 54,
    1.22 +	20, 22, 33, 38, 46, 51, 55, 60,
    1.23 +	21, 34, 37, 47, 50, 56, 59, 61,
    1.24 +	35, 36, 48, 49, 57, 58, 62, 63
    1.25 +};
    1.26 +
    1.27 +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
    1.28 +/* IMPORTANT: these are only valid for 8-bit data precision! */
    1.29 +static const unsigned char bits_dc_luminance[17] =
    1.30 +{
    1.31 +	0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
    1.32 +};
    1.33 +static const unsigned char val_dc_luminance[] =
    1.34 +{
    1.35 +	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    1.36 +};
    1.37 +
    1.38 +static const unsigned char bits_dc_chrominance[17] =
    1.39 +{
    1.40 +	0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
    1.41 +};
    1.42 +static const unsigned char val_dc_chrominance[] =
    1.43 +{
    1.44 +	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    1.45 +};
    1.46 +
    1.47 +static const unsigned char bits_ac_luminance[17] =
    1.48 +{
    1.49 +	0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
    1.50 +};
    1.51 +static const unsigned char val_ac_luminance[] =
    1.52 +{
    1.53 +	0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
    1.54 +	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
    1.55 +	0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
    1.56 +	0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
    1.57 +	0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
    1.58 +	0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
    1.59 +	0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
    1.60 +	0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
    1.61 +	0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
    1.62 +	0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
    1.63 +	0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
    1.64 +	0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
    1.65 +	0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
    1.66 +	0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
    1.67 +	0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
    1.68 +	0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
    1.69 +	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
    1.70 +	0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
    1.71 +	0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
    1.72 +	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
    1.73 +	0xf9, 0xfa
    1.74 +};
    1.75 +
    1.76 +static const unsigned char bits_ac_chrominance[17] =
    1.77 +{
    1.78 +	0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
    1.79 +};
    1.80 +
    1.81 +static const unsigned char val_ac_chrominance[] =
    1.82 +{
    1.83 +	0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
    1.84 +	0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
    1.85 +	0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
    1.86 +	0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
    1.87 +	0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
    1.88 +	0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
    1.89 +	0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
    1.90 +	0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
    1.91 +	0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
    1.92 +	0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
    1.93 +	0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
    1.94 +	0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
    1.95 +	0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
    1.96 +	0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
    1.97 +	0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
    1.98 +	0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
    1.99 +	0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
   1.100 +	0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
   1.101 +	0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
   1.102 +	0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
   1.103 +	0xf9, 0xfa
   1.104 +};
   1.105 +
   1.106 +static void print_SOF(const unsigned char *stream)
   1.107 +{
   1.108 +	int width, height, nr_components, precision;
   1.109 +
   1.110 +	precision = stream[2];
   1.111 +	height = be16_to_cpu(stream+3);
   1.112 +	width  = be16_to_cpu(stream+5);
   1.113 +	nr_components = stream[7];
   1.114 +
   1.115 +	trace("> SOF marker\n");
   1.116 +	trace("Size:%dx%d nr_components:%d precision:%d\n",
   1.117 +				width, height,
   1.118 +			 nr_components,
   1.119 +			 precision);
   1.120 +}
   1.121 +
   1.122 +
   1.123 +/*
   1.124 + * Takes two array of bits, and build the huffman table for size, and code
   1.125 + *
   1.126 + * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
   1.127 + * code_size will be used to known how many bits this symbol is encoded.
   1.128 + * slowtable will be used when the first lookup didn't give the result.
   1.129 + */
   1.130 +static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
   1.131 +{
   1.132 +	unsigned int i, j, code, code_size, val, nbits;
   1.133 +	unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz;
   1.134 +	unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc;
   1.135 +	int next_free_entry;
   1.136 +
   1.137 +	/*
   1.138 +	 * Build a temp array
   1.139 +	 *   huffsize[X] => numbers of bits to write vals[X]
   1.140 +	 */
   1.141 +	hz = huffsize;
   1.142 +	for (i=1; i<=16; i++)
   1.143 +	{
   1.144 +		for (j=1; j<=bits[i]; j++)
   1.145 +			*hz++ = i;
   1.146 +	}
   1.147 +	*hz = 0;
   1.148 +
   1.149 +	memset(table->lookup, 0xff, sizeof(table->lookup));
   1.150 +	for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
   1.151 +		table->slowtable[i][0] = 0;
   1.152 +
   1.153 +	/* Build a temp array
   1.154 +	 *   huffcode[X] => code used to write vals[X]
   1.155 +	 */
   1.156 +	code = 0;
   1.157 +	hc = huffcode;
   1.158 +	hz = huffsize;
   1.159 +	nbits = *hz;
   1.160 +	while (*hz)
   1.161 +	{
   1.162 +		while (*hz == nbits)
   1.163 +		{
   1.164 +			*hc++ = code++;
   1.165 +			hz++;
   1.166 +		}
   1.167 +		code <<= 1;
   1.168 +		nbits++;
   1.169 +	}
   1.170 +
   1.171 +	/*
   1.172 +	 * Build the lookup table, and the slowtable if needed.
   1.173 +	 */
   1.174 +	next_free_entry = -1;
   1.175 +	for (i=0; huffsize[i]; i++)
   1.176 +	{
   1.177 +		val = vals[i];
   1.178 +		code = huffcode[i];
   1.179 +		code_size = huffsize[i];
   1.180 +
   1.181 +		trace("val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
   1.182 +
   1.183 +		table->code_size[val] = code_size;
   1.184 +		if (code_size <= HUFFMAN_HASH_NBITS)
   1.185 +		{
   1.186 +			/*
   1.187 +			 * Good: val can be put in the lookup table, so fill all value of this
   1.188 +			 * column with value val
   1.189 +			 */
   1.190 +			int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
   1.191 +			code <<= HUFFMAN_HASH_NBITS - code_size;
   1.192 +			while ( repeat-- )
   1.193 +				table->lookup[code++] = val;
   1.194 +
   1.195 +		}
   1.196 +		else
   1.197 +		{
   1.198 +			/* Perhaps sorting the array will be an optimization */
   1.199 +			uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1];
   1.200 +			while(slowtable[0])
   1.201 +				slowtable+=2;
   1.202 +			slowtable[0] = code;
   1.203 +			slowtable[1] = val;
   1.204 +			slowtable[2] = 0;
   1.205 +		}
   1.206 +
   1.207 +	}
   1.208 +}
   1.209 +
   1.210 +static void build_default_huffman_tables(struct jdec_private *priv)
   1.211 +{
   1.212 +	if (   (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
   1.213 +		&& priv->default_huffman_table_initialized)
   1.214 +		return;
   1.215 +
   1.216 +	build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]);
   1.217 +	build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]);
   1.218 +
   1.219 +	build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]);
   1.220 +	build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]);
   1.221 +
   1.222 +	priv->default_huffman_table_initialized = 1;
   1.223 +}
   1.224 +
   1.225 +/*******************************************************************************
   1.226 + *
   1.227 + * JPEG/JFIF Parsing functions
   1.228 + *
   1.229 + * Note: only a small subset of the jpeg file format is supported. No markers,
   1.230 + * nor progressive stream is supported.
   1.231 + *
   1.232 + ******************************************************************************/
   1.233 +
   1.234 +static void build_quantization_table(float *qtable, const unsigned char *ref_table)
   1.235 +{
   1.236 +	/* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
   1.237 +	 * For float AA&N IDCT method, divisors are equal to quantization
   1.238 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   1.239 +	 *   scalefactor[0] = 1
   1.240 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   1.241 +	 * We apply a further scale factor of 8.
   1.242 +	 * What's actually stored is 1/divisor so that the inner loop can
   1.243 +	 * use a multiplication rather than a division.
   1.244 +	 */
   1.245 +	int i, j;
   1.246 +	static const double aanscalefactor[8] = {
   1.247 +		1.0, 1.387039845, 1.306562965, 1.175875602,
   1.248 +		1.0, 0.785694958, 0.541196100, 0.275899379
   1.249 +	};
   1.250 +	const unsigned char *zz = zigzag;
   1.251 +
   1.252 +	for (i=0; i<8; i++) {
   1.253 +		for (j=0; j<8; j++) {
   1.254 +			*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
   1.255 +		}
   1.256 +	}
   1.257 +
   1.258 +}
   1.259 +
   1.260 +static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
   1.261 +{
   1.262 +	int qi;
   1.263 +	float *table;
   1.264 +	const unsigned char *dqt_block_end;
   1.265 +
   1.266 +	trace("> DQT marker\n");
   1.267 +	dqt_block_end = stream + be16_to_cpu(stream);
   1.268 +	stream += 2;  /* Skip length */
   1.269 +
   1.270 +	while (stream < dqt_block_end)
   1.271 +	{
   1.272 +		qi = *stream++;
   1.273 +#if SANITY_CHECK
   1.274 +		if (qi>>4)
   1.275 +			error("16 bits quantization table is not supported\n");
   1.276 +		if (qi>4)
   1.277 +			error("No more 4 quantization table is supported (got %d)\n", qi);
   1.278 +#endif
   1.279 +			table = priv->Q_tables[qi];
   1.280 +			build_quantization_table(table, stream);
   1.281 +			stream += 64;
   1.282 +	}
   1.283 +	trace("< DQT marker\n");
   1.284 +	return 0;
   1.285 +}
   1.286 +
   1.287 +static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
   1.288 +{
   1.289 +	int i, width, height, nr_components, cid, sampling_factor;
   1.290 +	int Q_table;
   1.291 +	struct component *c;
   1.292 +
   1.293 +	trace("> SOF marker\n");
   1.294 +	print_SOF(stream);
   1.295 +
   1.296 +	height = be16_to_cpu(stream+3);
   1.297 +	width  = be16_to_cpu(stream+5);
   1.298 +	nr_components = stream[7];
   1.299 +#if SANITY_CHECK
   1.300 +	if (stream[2] != 8)
   1.301 +		error("Precision other than 8 is not supported\n");
   1.302 +	if (nr_components != 3)
   1.303 +		error("We only support YUV images\n");
   1.304 +	if (height%16)
   1.305 +		error("Height need to be a multiple of 16 (current height is %d)\n", height);
   1.306 +	if (width%16)
   1.307 +		error("Width need to be a multiple of 16 (current Width is %d)\n", width);
   1.308 +#endif
   1.309 +	stream += 8;
   1.310 +	for (i=0; i<nr_components; i++) {
   1.311 +		cid = *stream++;
   1.312 +		sampling_factor = *stream++;
   1.313 +		Q_table = *stream++;
   1.314 +		c = &priv->component_infos[i];
   1.315 +#if SANITY_CHECK
   1.316 +		c->cid = cid;
   1.317 +		if (Q_table >= COMPONENTS)
   1.318 +			error("Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1);
   1.319 +#endif
   1.320 +		c->Vfactor = sampling_factor&0xf;
   1.321 +		c->Hfactor = sampling_factor>>4;
   1.322 +		c->Q_table = priv->Q_tables[Q_table];
   1.323 +		trace("Component:%d  factor:%dx%d  Quantization table:%d\n", cid, c->Hfactor, c->Hfactor, Q_table );
   1.324 +
   1.325 +	}
   1.326 +	priv->width = width;
   1.327 +	priv->height = height;
   1.328 +	priv->mcus_in_width = width/16;
   1.329 +	priv->mcus_in_height = height/16;
   1.330 +	priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height;
   1.331 +	
   1.332 +	trace("< SOF marker\n");
   1.333 +
   1.334 +	return 0;
   1.335 +}
   1.336 +
   1.337 +static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
   1.338 +{
   1.339 +	unsigned int i, cid, table;
   1.340 +	unsigned int nr_components = stream[2];
   1.341 +
   1.342 +	trace("> SOS marker\n");
   1.343 +
   1.344 +#if SANITY_CHECK
   1.345 +	if (nr_components != 3)
   1.346 +		error("We only support YCbCr image\n");
   1.347 +#endif
   1.348 +
   1.349 +		stream += 3;
   1.350 +		for (i=0;i<nr_components;i++) {
   1.351 +			cid = *stream++;
   1.352 +			table = *stream++;
   1.353 +#if SANITY_CHECK
   1.354 +			if ((table&0xf)>=4)
   1.355 +				error("We do not support more than 2 AC Huffman table\n");
   1.356 +			if ((table>>4)>=4)
   1.357 +				error("We do not support more than 2 DC Huffman table\n");
   1.358 +			if (cid != priv->component_infos[i].cid)
   1.359 +				error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n",
   1.360 +							i, cid, i, priv->component_infos[i].cid);
   1.361 +				trace("ComponentId:%d  tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4);
   1.362 +#endif
   1.363 +				priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
   1.364 +				priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
   1.365 +		}
   1.366 +		priv->stream = stream+3;
   1.367 +		trace("< SOS marker\n");
   1.368 +		return 0;
   1.369 +}
   1.370 +
   1.371 +static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
   1.372 +{
   1.373 +	unsigned int count, i;
   1.374 +	unsigned char huff_bits[17];
   1.375 +	int length, index;
   1.376 +
   1.377 +	length = be16_to_cpu(stream) - 2;
   1.378 +	stream += 2;  /* Skip length */
   1.379 +
   1.380 +	trace("> DHT marker (length=%d)\n", length);
   1.381 +
   1.382 +	while (length>0) {
   1.383 +		index = *stream++;
   1.384 +
   1.385 +		/* We need to calculate the number of bytes 'vals' will takes */
   1.386 +		huff_bits[0] = 0;
   1.387 +		count = 0;
   1.388 +		for (i=1; i<17; i++) {
   1.389 +			huff_bits[i] = *stream++;
   1.390 +			count += huff_bits[i];
   1.391 +		}
   1.392 +#if SANITY_CHECK
   1.393 +		if (count >= HUFFMAN_BITS_SIZE)
   1.394 +			error("No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE);
   1.395 +		if ( (index &0xf) >= HUFFMAN_TABLES)
   1.396 +			error("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf);
   1.397 +		trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
   1.398 +#endif
   1.399 +
   1.400 +		if (index & 0xf0 )
   1.401 +			build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]);
   1.402 +		else
   1.403 +			build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]);
   1.404 +
   1.405 +		length -= 1;
   1.406 +		length -= 16;
   1.407 +		length -= count;
   1.408 +		stream += count;
   1.409 +	}
   1.410 +	trace("< DHT marker\n");
   1.411 +	return 0;
   1.412 +}
   1.413 +
   1.414 +static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
   1.415 +{
   1.416 +	unsigned int length;
   1.417 +
   1.418 +	trace("> DRI marker\n");
   1.419 +
   1.420 +	length = be16_to_cpu(stream);
   1.421 +
   1.422 +#if SANITY_CHECK
   1.423 +	if (length != 4)
   1.424 +		error("Length of DRI marker need to be 4\n");
   1.425 +#endif
   1.426 +
   1.427 +	priv->restart_interval = be16_to_cpu(stream+2);
   1.428 +	if (priv->restart_interval == 0)
   1.429 +		priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height;
   1.430 +
   1.431 +#if DEBUG
   1.432 +	trace("Restart interval = %d\n", priv->restart_interval);
   1.433 +#endif
   1.434 +
   1.435 +	trace("< DRI marker\n");
   1.436 +
   1.437 +	return 0;
   1.438 +}
   1.439 +
   1.440 +int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
   1.441 +{
   1.442 +	int chuck_len;
   1.443 +	int marker;
   1.444 +	int sos_marker_found = 0;
   1.445 +	int dht_marker_found = 0;
   1.446 +	const unsigned char *next_chunck;
   1.447 +
   1.448 +	/* Parse marker */
   1.449 +	while (!sos_marker_found)
   1.450 +	{
   1.451 +		if (*stream++ != 0xff) {
   1.452 +			trace("Bogus jpeg format\n");
   1.453 +			return -1;
   1.454 +		}
   1.455 +		/* Skip any padding ff byte (this is normal) */
   1.456 +		while (*stream == 0xff)
   1.457 +			stream++;
   1.458 +
   1.459 +		marker = *stream++;
   1.460 +		chuck_len = be16_to_cpu(stream);
   1.461 +		next_chunck = stream + chuck_len;
   1.462 +		switch (marker)
   1.463 +		{
   1.464 +			case SOF:
   1.465 +				if (parse_SOF(priv, stream) < 0)
   1.466 +					return -1;
   1.467 +				break;
   1.468 +			case DQT:
   1.469 +				if (parse_DQT(priv, stream) < 0)
   1.470 +					return -1;
   1.471 +				break;
   1.472 +			case SOS:
   1.473 +				if (parse_SOS(priv, stream) < 0)
   1.474 +					return -1;
   1.475 +				sos_marker_found = 1;
   1.476 +				break;
   1.477 +			case DHT:
   1.478 +				if (parse_DHT(priv, stream) < 0)
   1.479 +					return -1;
   1.480 +				dht_marker_found = 1;
   1.481 +				break;
   1.482 +			case DRI:
   1.483 +				if (parse_DRI(priv, stream) < 0)
   1.484 +					return -1;
   1.485 +				break;
   1.486 +			default:
   1.487 +				trace("> Unknown marker %2.2x\n", marker);
   1.488 +				break;
   1.489 +		}
   1.490 +
   1.491 +		stream = next_chunck;
   1.492 +	}
   1.493 +
   1.494 +	if (!dht_marker_found) {
   1.495 +		trace("No Huffman table loaded, using the default one\n");
   1.496 +		build_default_huffman_tables(priv);
   1.497 +	}
   1.498 +
   1.499 +	#ifdef SANITY_CHECK
   1.500 +	if (   (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
   1.501 +		|| (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
   1.502 +		error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
   1.503 +	if (   (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
   1.504 +		|| (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
   1.505 +		error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
   1.506 +	if (   (priv->component_infos[cCb].Hfactor!=1)
   1.507 +		|| (priv->component_infos[cCr].Hfactor!=1)
   1.508 +		|| (priv->component_infos[cCb].Vfactor!=1)
   1.509 +		|| (priv->component_infos[cCr].Vfactor!=1))
   1.510 +		error("Sampling other than 1x1 for Cr and Cb is not supported");
   1.511 +	#endif
   1.512 +
   1.513 +		return 0;
   1.514 +}
   1.515 \ No newline at end of file