1 /*----------------------------------------------------------------------------*/
2 /**
3  *	This confidential and proprietary software may be used only as
4  *	authorised by a licensing agreement from ARM Limited
5  *	(C) COPYRIGHT 2011-2012 ARM Limited
6  *	ALL RIGHTS RESERVED
7  *
8  *	The entire notice above must be reproduced on all authorised
9  *	copies and copies may only be made to the extent permitted
10  *	by a licensing agreement from ARM Limited.
11  *
12  *	@brief	Functions to convert a compressed block between the symbolic and
13  *			the physical representation.
14  */
15 /*----------------------------------------------------------------------------*/
16 
17 #include "astc_codec_internals.h"
18 
19 // routine to write up to 8 bits
write_bits(int value,int bitcount,int bitoffset,uint8_t * ptr)20 static inline void write_bits(int value, int bitcount, int bitoffset, uint8_t * ptr)
21 {
22 	int mask = (1 << bitcount) - 1;
23 	value &= mask;
24 	ptr += bitoffset >> 3;
25 	bitoffset &= 7;
26 	value <<= bitoffset;
27 	mask <<= bitoffset;
28 	mask = ~mask;
29 
30 	ptr[0] &= mask;
31 	ptr[0] |= value;
32 	ptr[1] &= mask >> 8;
33 	ptr[1] |= value >> 8;
34 }
35 
36 
37 // routine to read up to 8 bits
read_bits(int bitcount,int bitoffset,const uint8_t * ptr)38 static inline int read_bits(int bitcount, int bitoffset, const uint8_t * ptr)
39 {
40 	int mask = (1 << bitcount) - 1;
41 	ptr += bitoffset >> 3;
42 	bitoffset &= 7;
43 	int value = ptr[0] | (ptr[1] << 8);
44 	value >>= bitoffset;
45 	value &= mask;
46 	return value;
47 }
48 
49 
bitrev8(int p)50 int bitrev8(int p)
51 {
52 	p = ((p & 0xF) << 4) | ((p >> 4) & 0xF);
53 	p = ((p & 0x33) << 2) | ((p >> 2) & 0x33);
54 	p = ((p & 0x55) << 1) | ((p >> 1) & 0x55);
55 	return p;
56 }
57 
58 
59 
60 
symbolic_to_physical(int xdim,int ydim,int zdim,const symbolic_compressed_block * sc)61 physical_compressed_block symbolic_to_physical(int xdim, int ydim, int zdim, const symbolic_compressed_block * sc)
62 {
63 	int i, j;
64 	physical_compressed_block res;
65 
66 
67 	if (sc->block_mode == -2)
68 	{
69 		// UNORM16 constant-color block.
70 		// This encodes separate constant-color blocks. There is currently
71 		// no attempt to coalesce them into larger void-extents.
72 
73 		static const uint8_t cbytes[8] = { 0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
74 		for (i = 0; i < 8; i++)
75 			res.data[i] = cbytes[i];
76 
77 		for (i = 0; i < 4; i++)
78 		{
79 			res.data[2 * i + 8] = sc->constant_color[i] & 0xFF;
80 			res.data[2 * i + 9] = (sc->constant_color[i] >> 8) & 0xFF;
81 		}
82 		return res;
83 	}
84 
85 
86 	if (sc->block_mode == -1)
87 	{
88 		// FP16 constant-color block.
89 		// This encodes separate constant-color blocks. There is currently
90 		// no attempt to coalesce them into larger void-extents.
91 
92 		static const uint8_t cbytes[8] = { 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
93 		for (i = 0; i < 8; i++)
94 			res.data[i] = cbytes[i];
95 
96 		for (i = 0; i < 4; i++)
97 		{
98 			res.data[2 * i + 8] = sc->constant_color[i] & 0xFF;
99 			res.data[2 * i + 9] = (sc->constant_color[i] >> 8) & 0xFF;
100 		}
101 		return res;
102 	}
103 
104 
105 
106 	int partition_count = sc->partition_count;
107 
108 	// first, compress the weights. They are encoded as an ordinary
109 	// integer-sequence, then bit-reversed
110 	uint8_t weightbuf[16];
111 	for (i = 0; i < 16; i++)
112 		weightbuf[i] = 0;
113 
114 	const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim);
115 	const decimation_table *const *ixtab2 = bsd->decimation_tables;
116 
117 
118 	int weight_count = ixtab2[bsd->block_modes[sc->block_mode].decimation_mode]->num_weights;
119 	int weight_quantization_method = bsd->block_modes[sc->block_mode].quantization_mode;
120 	int is_dual_plane = bsd->block_modes[sc->block_mode].is_dual_plane;
121 
122 	int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
123 
124 	int bits_for_weights = compute_ise_bitcount(real_weight_count,
125 												(quantization_method) weight_quantization_method);
126 
127 
128 	if (is_dual_plane)
129 	{
130 		uint8_t weights[64];
131 		for (i = 0; i < weight_count; i++)
132 		{
133 			weights[2 * i] = sc->plane1_weights[i];
134 			weights[2 * i + 1] = sc->plane2_weights[i];
135 		}
136 		encode_ise(weight_quantization_method, real_weight_count, weights, weightbuf, 0);
137 	}
138 	else
139 	{
140 		encode_ise(weight_quantization_method, weight_count, sc->plane1_weights, weightbuf, 0);
141 	}
142 
143 	for (i = 0; i < 16; i++)
144 		res.data[i] = bitrev8(weightbuf[15 - i]);
145 
146 	write_bits(sc->block_mode, 11, 0, res.data);
147 	write_bits(partition_count - 1, 2, 11, res.data);
148 
149 	int below_weights_pos = 128 - bits_for_weights;
150 
151 	// encode partition index and color endpoint types for blocks with
152 	// 2 or more partitions.
153 	if (partition_count > 1)
154 	{
155 		write_bits(sc->partition_index, 6, 13, res.data);
156 		write_bits(sc->partition_index >> 6, PARTITION_BITS - 6, 19, res.data);
157 
158 		if (sc->color_formats_matched)
159 		{
160 			write_bits(sc->color_formats[0] << 2, 6, 13 + PARTITION_BITS, res.data);
161 		}
162 		else
163 		{
164 			// go through the selected endpoint type classes for each partition
165 			// in order to determine the lowest class present.
166 			int low_class = 4;
167 			for (i = 0; i < partition_count; i++)
168 			{
169 				int class_of_format = sc->color_formats[i] >> 2;
170 				if (class_of_format < low_class)
171 					low_class = class_of_format;
172 			}
173 			if (low_class == 3)
174 				low_class = 2;
175 			int encoded_type = low_class + 1;
176 			int bitpos = 2;
177 			for (i = 0; i < partition_count; i++)
178 			{
179 				int classbit_of_format = (sc->color_formats[i] >> 2) - low_class;
180 
181 				encoded_type |= classbit_of_format << bitpos;
182 				bitpos++;
183 			}
184 			for (i = 0; i < partition_count; i++)
185 			{
186 				int lowbits_of_format = sc->color_formats[i] & 3;
187 				encoded_type |= lowbits_of_format << bitpos;
188 				bitpos += 2;
189 			}
190 			int encoded_type_lowpart = encoded_type & 0x3F;
191 			int encoded_type_highpart = encoded_type >> 6;
192 			int encoded_type_highpart_size = (3 * partition_count) - 4;
193 			int encoded_type_highpart_pos = 128 - bits_for_weights - encoded_type_highpart_size;
194 			write_bits(encoded_type_lowpart, 6, 13 + PARTITION_BITS, res.data);
195 			write_bits(encoded_type_highpart, encoded_type_highpart_size, encoded_type_highpart_pos, res.data);
196 
197 			below_weights_pos -= encoded_type_highpart_size;
198 		}
199 	}
200 
201 	else
202 		write_bits(sc->color_formats[0], 4, 13, res.data);
203 
204 	// in dual-plane mode, encode the color component of the second plane of weights
205 	if (is_dual_plane)
206 		write_bits(sc->plane2_color_component, 2, below_weights_pos - 2, res.data);
207 
208 	// finally, encode the color bits
209 	// first, get hold of all the color components to encode
210 	uint8_t values_to_encode[32];
211 	int valuecount_to_encode = 0;
212 	for (i = 0; i < sc->partition_count; i++)
213 	{
214 		int vals = 2 * (sc->color_formats[i] >> 2) + 2;
215 		for (j = 0; j < vals; j++)
216 			values_to_encode[j + valuecount_to_encode] = sc->color_values[i][j];
217 		valuecount_to_encode += vals;
218 	}
219 	// then, encode an ISE based on them.
220 	encode_ise(sc->color_quantization_level, valuecount_to_encode, values_to_encode, res.data, (sc->partition_count == 1 ? 17 : 19 + PARTITION_BITS));
221 
222 	return res;
223 }
224 
225 
physical_to_symbolic(int xdim,int ydim,int zdim,physical_compressed_block pb,symbolic_compressed_block * res)226 void physical_to_symbolic(int xdim, int ydim, int zdim, physical_compressed_block pb, symbolic_compressed_block * res)
227 {
228 	uint8_t bswapped[16];
229 	int i, j;
230 
231 	res->error_block = 0;
232 
233 	// get hold of the block-size descriptor and the decimation tables.
234 	const block_size_descriptor *bsd = get_block_size_descriptor(xdim, ydim, zdim);
235 	const decimation_table *const *ixtab2 = bsd->decimation_tables;
236 
237 	// extract header fields
238 	int block_mode = read_bits(11, 0, pb.data);
239 
240 
241 	if ((block_mode & 0x1FF) == 0x1FC)
242 	{
243 		// void-extent block!
244 
245 		// check what format the data has
246 		if (block_mode & 0x200)
247 			res->block_mode = -1;	// floating-point
248 		else
249 			res->block_mode = -2;	// unorm16.
250 
251 		res->partition_count = 0;
252 		for (i = 0; i < 4; i++)
253 		{
254 			res->constant_color[i] = pb.data[2 * i + 8] | (pb.data[2 * i + 9] << 8);
255 		}
256 
257 		// additionally, check that the void-extent
258 		if (zdim == 1)
259 		{
260 			// 2D void-extent
261 			int rsvbits = read_bits(2, 10, pb.data);
262 			if (rsvbits != 3)
263 				res->error_block = 1;
264 
265 			int vx_low_s = read_bits(8, 12, pb.data) | (read_bits(5, 12 + 8, pb.data) << 8);
266 			int vx_high_s = read_bits(8, 25, pb.data) | (read_bits(5, 25 + 8, pb.data) << 8);
267 			int vx_low_t = read_bits(8, 38, pb.data) | (read_bits(5, 38 + 8, pb.data) << 8);
268 			int vx_high_t = read_bits(8, 51, pb.data) | (read_bits(5, 51 + 8, pb.data) << 8);
269 
270 			int all_ones = vx_low_s == 0x1FFF && vx_high_s == 0x1FFF && vx_low_t == 0x1FFF && vx_high_t == 0x1FFF;
271 
272 			if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t) && !all_ones)
273 				res->error_block = 1;
274 		}
275 		else
276 		{
277 			// 3D void-extent
278 			int vx_low_s = read_bits(9, 10, pb.data);
279 			int vx_high_s = read_bits(9, 19, pb.data);
280 			int vx_low_t = read_bits(9, 28, pb.data);
281 			int vx_high_t = read_bits(9, 37, pb.data);
282 			int vx_low_p = read_bits(9, 46, pb.data);
283 			int vx_high_p = read_bits(9, 55, pb.data);
284 
285 			int all_ones = vx_low_s == 0x1FF && vx_high_s == 0x1FF && vx_low_t == 0x1FF && vx_high_t == 0x1FF && vx_low_p == 0x1FF && vx_high_p == 0x1FF;
286 
287 			if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t || vx_low_p >= vx_high_p) && !all_ones)
288 				res->error_block = 1;
289 		}
290 
291 		return;
292 	}
293 
294 	if (bsd->block_modes[block_mode].permit_decode == 0)
295 	{
296 		res->error_block = 1;
297 		return;
298 	}
299 
300 	int weight_count = ixtab2[bsd->block_modes[block_mode].decimation_mode]->num_weights;
301 	int weight_quantization_method = bsd->block_modes[block_mode].quantization_mode;
302 	int is_dual_plane = bsd->block_modes[block_mode].is_dual_plane;
303 
304 	int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
305 
306 	int partition_count = read_bits(2, 11, pb.data) + 1;
307 
308 	res->block_mode = block_mode;
309 	res->partition_count = partition_count;
310 
311 	for (i = 0; i < 16; i++)
312 		bswapped[i] = bitrev8(pb.data[15 - i]);
313 
314 	int bits_for_weights = compute_ise_bitcount(real_weight_count,
315 												(quantization_method) weight_quantization_method);
316 
317 	int below_weights_pos = 128 - bits_for_weights;
318 
319 	if (is_dual_plane)
320 	{
321 		uint8_t indices[64];
322 		decode_ise(weight_quantization_method, real_weight_count, bswapped, indices, 0);
323 		for (i = 0; i < weight_count; i++)
324 		{
325 			res->plane1_weights[i] = indices[2 * i];
326 			res->plane2_weights[i] = indices[2 * i + 1];
327 		}
328 	}
329 	else
330 	{
331 		decode_ise(weight_quantization_method, weight_count, bswapped, res->plane1_weights, 0);
332 	}
333 
334 	if (is_dual_plane && partition_count == 4)
335 		res->error_block = 1;
336 
337 
338 
339 	res->color_formats_matched = 0;
340 
341 	// then, determine the format of each endpoint pair
342 	int color_formats[4];
343 	int encoded_type_highpart_size = 0;
344 	if (partition_count == 1)
345 	{
346 		color_formats[0] = read_bits(4, 13, pb.data);
347 		res->partition_index = 0;
348 	}
349 	else
350 	{
351 		encoded_type_highpart_size = (3 * partition_count) - 4;
352 		below_weights_pos -= encoded_type_highpart_size;
353 		int encoded_type = read_bits(6, 13 + PARTITION_BITS, pb.data) | (read_bits(encoded_type_highpart_size, below_weights_pos, pb.data) << 6);
354 		int baseclass = encoded_type & 0x3;
355 		if (baseclass == 0)
356 		{
357 			for (i = 0; i < partition_count; i++)
358 			{
359 				color_formats[i] = (encoded_type >> 2) & 0xF;
360 			}
361 			below_weights_pos += encoded_type_highpart_size;
362 			res->color_formats_matched = 1;
363 			encoded_type_highpart_size = 0;
364 		}
365 		else
366 		{
367 			int bitpos = 2;
368 			baseclass--;
369 			for (i = 0; i < partition_count; i++)
370 			{
371 				color_formats[i] = (((encoded_type >> bitpos) & 1) + baseclass) << 2;
372 				bitpos++;
373 			}
374 			for (i = 0; i < partition_count; i++)
375 			{
376 				color_formats[i] |= (encoded_type >> bitpos) & 3;
377 				bitpos += 2;
378 			}
379 		}
380 		res->partition_index = read_bits(6, 13, pb.data) | (read_bits(PARTITION_BITS - 6, 19, pb.data) << 6);
381 
382 	}
383 	for (i = 0; i < partition_count; i++)
384 		res->color_formats[i] = color_formats[i];
385 
386 
387 	// then, determine the number of integers we need to unpack for the endpoint pairs
388 	int color_integer_count = 0;
389 	for (i = 0; i < partition_count; i++)
390 	{
391 		int endpoint_class = color_formats[i] >> 2;
392 		color_integer_count += (endpoint_class + 1) * 2;
393 	}
394 
395 	if (color_integer_count > 18)
396 		res->error_block = 1;
397 
398 	// then, determine the color endpoint format to use for these integers
399 	static const int color_bits_arr[5] = { -1, 115 - 4, 113 - 4 - PARTITION_BITS, 113 - 4 - PARTITION_BITS, 113 - 4 - PARTITION_BITS };
400 	int color_bits = color_bits_arr[partition_count] - bits_for_weights - encoded_type_highpart_size;
401 	if (is_dual_plane)
402 		color_bits -= 2;
403 	if (color_bits < 0)
404 		color_bits = 0;
405 
406 	int color_quantization_level = quantization_mode_table[color_integer_count >> 1][color_bits];
407 	res->color_quantization_level = color_quantization_level;
408 	if (color_quantization_level < 4)
409 		res->error_block = 1;
410 
411 
412 	// then unpack the integer-bits
413 	uint8_t values_to_decode[32];
414 	decode_ise(color_quantization_level, color_integer_count, pb.data, values_to_decode, (partition_count == 1 ? 17 : 19 + PARTITION_BITS));
415 
416 	// and distribute them over the endpoint types
417 	int valuecount_to_decode = 0;
418 
419 	for (i = 0; i < partition_count; i++)
420 	{
421 		int vals = 2 * (color_formats[i] >> 2) + 2;
422 		for (j = 0; j < vals; j++)
423 			res->color_values[i][j] = values_to_decode[j + valuecount_to_decode];
424 		valuecount_to_decode += vals;
425 	}
426 
427 	// get hold of color component for second-plane in the case of dual plane of weights.
428 	if (is_dual_plane)
429 		res->plane2_color_component = read_bits(2, below_weights_pos - 2, pb.data);
430 
431 }
432