1 // SPDX-License-Identifier: Apache-2.0
2 // ----------------------------------------------------------------------------
3 // Copyright 2011-2021 Arm Limited
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 // use this file except in compliance with the License. You may obtain a copy
7 // of the License at:
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16 // ----------------------------------------------------------------------------
17 
18 /**
19  * @brief Functions for converting between symbolic and physical encodings.
20  */
21 
22 #include "astcenc_internal.h"
23 
24 #include <cassert>
25 
26 /**
27  * @brief Write up to 8 bits at an arbitrary bit offset.
28  *
29  * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so
30  * may span two separate bytes in memory.
31  *
32  * @param         value       The value to write.
33  * @param         bitcount    The number of bits to write, starting from LSB.
34  * @param         bitoffset   The bit offset to store at, between 0 and 7.
35  * @param[in,out] ptr         The data pointer to write to.
36  */
write_bits(int value,int bitcount,int bitoffset,uint8_t * ptr)37 static inline void write_bits(
38 	int value,
39 	int bitcount,
40 	int bitoffset,
41 	uint8_t* ptr
42 ) {
43 	int mask = (1 << bitcount) - 1;
44 	value &= mask;
45 	ptr += bitoffset >> 3;
46 	bitoffset &= 7;
47 	value <<= bitoffset;
48 	mask <<= bitoffset;
49 	mask = ~mask;
50 
51 	ptr[0] &= mask;
52 	ptr[0] |= value;
53 	ptr[1] &= mask >> 8;
54 	ptr[1] |= value >> 8;
55 }
56 
57 /**
58  * @brief Read up to 8 bits at an arbitrary bit offset.
59  *
60  * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may
61  * span two separate bytes in memory.
62  *
63  * @param         bitcount    The number of bits to read.
64  * @param         bitoffset   The bit offset to read from, between 0 and 7.
65  * @param[in,out] ptr         The data pointer to read from.
66  *
67  * @return The read value.
68  */
read_bits(int bitcount,int bitoffset,const uint8_t * ptr)69 static inline int read_bits(
70 	int bitcount,
71 	int bitoffset,
72 	const uint8_t* ptr
73 ) {
74 	int mask = (1 << bitcount) - 1;
75 	ptr += bitoffset >> 3;
76 	bitoffset &= 7;
77 	int value = ptr[0] | (ptr[1] << 8);
78 	value >>= bitoffset;
79 	value &= mask;
80 	return value;
81 }
82 
83 /**
84  * @brief Reverse bits in a byte.
85  *
86  * @param p   The value to reverse.
87   *
88  * @return The reversed result.
89  */
bitrev8(int p)90 static inline int bitrev8(int p)
91 {
92 	p = ((p & 0x0F) << 4) | ((p >> 4) & 0x0F);
93 	p = ((p & 0x33) << 2) | ((p >> 2) & 0x33);
94 	p = ((p & 0x55) << 1) | ((p >> 1) & 0x55);
95 	return p;
96 }
97 
98 /* See header for documentation. */
symbolic_to_physical(const block_size_descriptor & bsd,const symbolic_compressed_block & scb,physical_compressed_block & pcb)99 void symbolic_to_physical(
100 	const block_size_descriptor& bsd,
101 	const symbolic_compressed_block& scb,
102 	physical_compressed_block& pcb
103 ) {
104 	assert(scb.block_type != SYM_BTYPE_ERROR);
105 
106 	// Constant color block using UNORM16 colors
107 	if (scb.block_type == SYM_BTYPE_CONST_U16)
108 	{
109 		// There is currently no attempt to coalesce larger void-extents
110 		static const uint8_t cbytes[8] { 0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
111 		for (unsigned int i = 0; i < 8; i++)
112 		{
113 			pcb.data[i] = cbytes[i];
114 		}
115 
116 		for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++)
117 		{
118 			pcb.data[2 * i + 8] = scb.constant_color[i] & 0xFF;
119 			pcb.data[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF;
120 		}
121 
122 		return;
123 	}
124 
125 	// Constant color block using FP16 colors
126 	if (scb.block_type == SYM_BTYPE_CONST_F16)
127 	{
128 		// There is currently no attempt to coalesce larger void-extents
129 		static const uint8_t cbytes[8]  { 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
130 		for (unsigned int i = 0; i < 8; i++)
131 		{
132 			pcb.data[i] = cbytes[i];
133 		}
134 
135 		for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++)
136 		{
137 			pcb.data[2 * i + 8] = scb.constant_color[i] & 0xFF;
138 			pcb.data[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF;
139 		}
140 
141 		return;
142 	}
143 
144 	unsigned int partition_count = scb.partition_count;
145 
146 	// Compress the weights.
147 	// They are encoded as an ordinary integer-sequence, then bit-reversed
148 	uint8_t weightbuf[16] { 0 };
149 
150 	const auto& bm = bsd.get_block_mode(scb.block_mode);
151 	const auto& di = bsd.get_decimation_info(bm.decimation_mode);
152 	int weight_count = di.weight_count;
153 	quant_method weight_quant_method = bm.get_weight_quant_mode();
154 	float weight_quant_levels = static_cast<float>(get_quant_level(weight_quant_method));
155 	int is_dual_plane = bm.is_dual_plane;
156 
157 	const auto& qat = quant_and_xfer_tables[weight_quant_method];
158 
159 	int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
160 
161 	int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method);
162 
163 	uint8_t weights[64];
164 	if (is_dual_plane)
165 	{
166 		for (int i = 0; i < weight_count; i++)
167 		{
168 			float uqw = static_cast<float>(scb.weights[i]);
169 			float qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f);
170 			int qwi = static_cast<int>(qw + 0.5f);
171 			weights[2 * i] = qat.scramble_map[qwi];
172 
173 			uqw = static_cast<float>(scb.weights[i + WEIGHTS_PLANE2_OFFSET]);
174 			qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f);
175 			qwi = static_cast<int>(qw + 0.5f);
176 			weights[2 * i + 1] = qat.scramble_map[qwi];
177 		}
178 	}
179 	else
180 	{
181 		for (int i = 0; i < weight_count; i++)
182 		{
183 			float uqw = static_cast<float>(scb.weights[i]);
184 			float qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f);
185 			int qwi = static_cast<int>(qw + 0.5f);
186 			weights[i] = qat.scramble_map[qwi];
187 		}
188 	}
189 
190 	encode_ise(weight_quant_method, real_weight_count, weights, weightbuf, 0);
191 
192 	for (int i = 0; i < 16; i++)
193 	{
194 		pcb.data[i] = static_cast<uint8_t>(bitrev8(weightbuf[15 - i]));
195 	}
196 
197 	write_bits(scb.block_mode, 11, 0, pcb.data);
198 	write_bits(partition_count - 1, 2, 11, pcb.data);
199 
200 	int below_weights_pos = 128 - bits_for_weights;
201 
202 	// Encode partition index and color endpoint types for blocks with 2+ partitions
203 	if (partition_count > 1)
204 	{
205 		write_bits(scb.partition_index, 6, 13, pcb.data);
206 		write_bits(scb.partition_index >> 6, PARTITION_INDEX_BITS - 6, 19, pcb.data);
207 
208 		if (scb.color_formats_matched)
209 		{
210 			write_bits(scb.color_formats[0] << 2, 6, 13 + PARTITION_INDEX_BITS, pcb.data);
211 		}
212 		else
213 		{
214 			// Check endpoint types for each partition to determine the lowest class present
215 			int low_class = 4;
216 
217 			for (unsigned int i = 0; i < partition_count; i++)
218 			{
219 				int class_of_format = scb.color_formats[i] >> 2;
220 				low_class = astc::min(class_of_format, low_class);
221 			}
222 
223 			if (low_class == 3)
224 			{
225 				low_class = 2;
226 			}
227 
228 			int encoded_type = low_class + 1;
229 			int bitpos = 2;
230 
231 			for (unsigned int i = 0; i < partition_count; i++)
232 			{
233 				int classbit_of_format = (scb.color_formats[i] >> 2) - low_class;
234 				encoded_type |= classbit_of_format << bitpos;
235 				bitpos++;
236 			}
237 
238 			for (unsigned int i = 0; i < partition_count; i++)
239 			{
240 				int lowbits_of_format = scb.color_formats[i] & 3;
241 				encoded_type |= lowbits_of_format << bitpos;
242 				bitpos += 2;
243 			}
244 
245 			int encoded_type_lowpart = encoded_type & 0x3F;
246 			int encoded_type_highpart = encoded_type >> 6;
247 			int encoded_type_highpart_size = (3 * partition_count) - 4;
248 			int encoded_type_highpart_pos = 128 - bits_for_weights - encoded_type_highpart_size;
249 			write_bits(encoded_type_lowpart, 6, 13 + PARTITION_INDEX_BITS, pcb.data);
250 			write_bits(encoded_type_highpart, encoded_type_highpart_size, encoded_type_highpart_pos, pcb.data);
251 			below_weights_pos -= encoded_type_highpart_size;
252 		}
253 	}
254 	else
255 	{
256 		write_bits(scb.color_formats[0], 4, 13, pcb.data);
257 	}
258 
259 	// In dual-plane mode, encode the color component of the second plane of weights
260 	if (is_dual_plane)
261 	{
262 		write_bits(scb.plane2_component, 2, below_weights_pos - 2, pcb.data);
263 	}
264 
265 	// Encode the color components
266 	uint8_t values_to_encode[32];
267 	int valuecount_to_encode = 0;
268 	for (unsigned int i = 0; i < scb.partition_count; i++)
269 	{
270 		int vals = 2 * (scb.color_formats[i] >> 2) + 2;
271 		assert(vals <= 8);
272 		for (int j = 0; j < vals; j++)
273 		{
274 			values_to_encode[j + valuecount_to_encode] = scb.color_values[i][j];
275 		}
276 		valuecount_to_encode += vals;
277 	}
278 
279 	encode_ise(scb.get_color_quant_mode(), valuecount_to_encode, values_to_encode, pcb.data,
280 	           scb.partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS);
281 }
282 
283 /* See header for documentation. */
physical_to_symbolic(const block_size_descriptor & bsd,const physical_compressed_block & pcb,symbolic_compressed_block & scb)284 void physical_to_symbolic(
285 	const block_size_descriptor& bsd,
286 	const physical_compressed_block& pcb,
287 	symbolic_compressed_block& scb
288 ) {
289 	uint8_t bswapped[16];
290 
291 	scb.block_type = SYM_BTYPE_NONCONST;
292 
293 	// Extract header fields
294 	int block_mode = read_bits(11, 0, pcb.data);
295 	if ((block_mode & 0x1FF) == 0x1FC)
296 	{
297 		// Constant color block
298 
299 		// Check what format the data has
300 		if (block_mode & 0x200)
301 		{
302 			scb.block_type = SYM_BTYPE_CONST_F16;
303 		}
304 		else
305 		{
306 			scb.block_type = SYM_BTYPE_CONST_U16;
307 		}
308 
309 		scb.partition_count = 0;
310 		for (int i = 0; i < 4; i++)
311 		{
312 			scb.constant_color[i] = pcb.data[2 * i + 8] | (pcb.data[2 * i + 9] << 8);
313 		}
314 
315 		// Additionally, check that the void-extent
316 		if (bsd.zdim == 1)
317 		{
318 			// 2D void-extent
319 			int rsvbits = read_bits(2, 10, pcb.data);
320 			if (rsvbits != 3)
321 			{
322 				scb.block_type = SYM_BTYPE_ERROR;
323 				return;
324 			}
325 
326 			int vx_low_s = read_bits(8, 12, pcb.data) | (read_bits(5, 12 + 8, pcb.data) << 8);
327 			int vx_high_s = read_bits(8, 25, pcb.data) | (read_bits(5, 25 + 8, pcb.data) << 8);
328 			int vx_low_t = read_bits(8, 38, pcb.data) | (read_bits(5, 38 + 8, pcb.data) << 8);
329 			int vx_high_t = read_bits(8, 51, pcb.data) | (read_bits(5, 51 + 8, pcb.data) << 8);
330 
331 			int all_ones = vx_low_s == 0x1FFF && vx_high_s == 0x1FFF && vx_low_t == 0x1FFF && vx_high_t == 0x1FFF;
332 
333 			if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t) && !all_ones)
334 			{
335 				scb.block_type = SYM_BTYPE_ERROR;
336 				return;
337 			}
338 		}
339 		else
340 		{
341 			// 3D void-extent
342 			int vx_low_s = read_bits(9, 10, pcb.data);
343 			int vx_high_s = read_bits(9, 19, pcb.data);
344 			int vx_low_t = read_bits(9, 28, pcb.data);
345 			int vx_high_t = read_bits(9, 37, pcb.data);
346 			int vx_low_p = read_bits(9, 46, pcb.data);
347 			int vx_high_p = read_bits(9, 55, pcb.data);
348 
349 			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;
350 
351 			if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t || vx_low_p >= vx_high_p) && !all_ones)
352 			{
353 				scb.block_type = SYM_BTYPE_ERROR;
354 				return;
355 			}
356 		}
357 
358 		return;
359 	}
360 
361 	unsigned int packed_index = bsd.block_mode_packed_index[block_mode];
362 	if (packed_index == BLOCK_BAD_BLOCK_MODE)
363 	{
364 		scb.block_type = SYM_BTYPE_ERROR;
365 		return;
366 	}
367 
368 	const auto& bm = bsd.get_block_mode(block_mode);
369 	const auto& di = bsd.get_decimation_info(bm.decimation_mode);
370 
371 	int weight_count = di.weight_count;
372 	quant_method weight_quant_method = static_cast<quant_method>(bm.quant_mode);
373 	int is_dual_plane = bm.is_dual_plane;
374 
375 	int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
376 
377 	int partition_count = read_bits(2, 11, pcb.data) + 1;
378 
379 	scb.block_mode = static_cast<uint16_t>(block_mode);
380 	scb.partition_count = static_cast<uint8_t>(partition_count);
381 
382 	for (int i = 0; i < 16; i++)
383 	{
384 		bswapped[i] = static_cast<uint8_t>(bitrev8(pcb.data[15 - i]));
385 	}
386 
387 	int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method);
388 
389 	int below_weights_pos = 128 - bits_for_weights;
390 
391 	uint8_t indices[64];
392 	const auto& qat = quant_and_xfer_tables[weight_quant_method];
393 
394 	decode_ise(weight_quant_method, real_weight_count, bswapped, indices, 0);
395 
396 	if (is_dual_plane)
397 	{
398 		for (int i = 0; i < weight_count; i++)
399 		{
400 			scb.weights[i] = qat.unscramble_and_unquant_map[indices[2 * i]];
401 			scb.weights[i + WEIGHTS_PLANE2_OFFSET] = qat.unscramble_and_unquant_map[indices[2 * i + 1]];
402 		}
403 	}
404 	else
405 	{
406 		for (int i = 0; i < weight_count; i++)
407 		{
408 			scb.weights[i] = qat.unscramble_and_unquant_map[indices[i]];
409 		}
410 	}
411 
412 	if (is_dual_plane && partition_count == 4)
413 	{
414 		scb.block_type = SYM_BTYPE_ERROR;
415 		return;
416 	}
417 
418 	scb.color_formats_matched = 0;
419 
420 	// Determine the format of each endpoint pair
421 	int color_formats[BLOCK_MAX_PARTITIONS];
422 	int encoded_type_highpart_size = 0;
423 	if (partition_count == 1)
424 	{
425 		color_formats[0] = read_bits(4, 13, pcb.data);
426 		scb.partition_index = 0;
427 	}
428 	else
429 	{
430 		encoded_type_highpart_size = (3 * partition_count) - 4;
431 		below_weights_pos -= encoded_type_highpart_size;
432 		int encoded_type = read_bits(6, 13 + PARTITION_INDEX_BITS, pcb.data) | (read_bits(encoded_type_highpart_size, below_weights_pos, pcb.data) << 6);
433 		int baseclass = encoded_type & 0x3;
434 		if (baseclass == 0)
435 		{
436 			for (int i = 0; i < partition_count; i++)
437 			{
438 				color_formats[i] = (encoded_type >> 2) & 0xF;
439 			}
440 
441 			below_weights_pos += encoded_type_highpart_size;
442 			scb.color_formats_matched = 1;
443 			encoded_type_highpart_size = 0;
444 		}
445 		else
446 		{
447 			int bitpos = 2;
448 			baseclass--;
449 
450 			for (int i = 0; i < partition_count; i++)
451 			{
452 				color_formats[i] = (((encoded_type >> bitpos) & 1) + baseclass) << 2;
453 				bitpos++;
454 			}
455 
456 			for (int i = 0; i < partition_count; i++)
457 			{
458 				color_formats[i] |= (encoded_type >> bitpos) & 3;
459 				bitpos += 2;
460 			}
461 		}
462 		scb.partition_index = static_cast<uint16_t>(read_bits(6, 13, pcb.data) | (read_bits(PARTITION_INDEX_BITS - 6, 19, pcb.data) << 6));
463 	}
464 
465 	for (int i = 0; i < partition_count; i++)
466 	{
467 		scb.color_formats[i] = static_cast<uint8_t>(color_formats[i]);
468 	}
469 
470 	// Determine number of color endpoint integers
471 	int color_integer_count = 0;
472 	for (int i = 0; i < partition_count; i++)
473 	{
474 		int endpoint_class = color_formats[i] >> 2;
475 		color_integer_count += (endpoint_class + 1) * 2;
476 	}
477 
478 	if (color_integer_count > 18)
479 	{
480 		scb.block_type = SYM_BTYPE_ERROR;
481 		return;
482 	}
483 
484 	// Determine the color endpoint format to use
485 	static const int color_bits_arr[5] { -1, 115 - 4, 113 - 4 - PARTITION_INDEX_BITS, 113 - 4 - PARTITION_INDEX_BITS, 113 - 4 - PARTITION_INDEX_BITS };
486 	int color_bits = color_bits_arr[partition_count] - bits_for_weights - encoded_type_highpart_size;
487 	if (is_dual_plane)
488 	{
489 		color_bits -= 2;
490 	}
491 
492 	if (color_bits < 0)
493 	{
494 		color_bits = 0;
495 	}
496 
497 	int color_quant_level = quant_mode_table[color_integer_count >> 1][color_bits];
498 	if (color_quant_level < QUANT_6)
499 	{
500 		scb.block_type = SYM_BTYPE_ERROR;
501 		return;
502 	}
503 
504 	// Unpack the integer color values and assign to endpoints
505 	scb.quant_mode = static_cast<quant_method>(color_quant_level);
506 	uint8_t values_to_decode[32];
507 	decode_ise(static_cast<quant_method>(color_quant_level), color_integer_count, pcb.data,
508 	           values_to_decode, (partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS));
509 
510 	int valuecount_to_decode = 0;
511 	for (int i = 0; i < partition_count; i++)
512 	{
513 		int vals = 2 * (color_formats[i] >> 2) + 2;
514 		for (int j = 0; j < vals; j++)
515 		{
516 			scb.color_values[i][j] = values_to_decode[j + valuecount_to_decode];
517 		}
518 		valuecount_to_decode += vals;
519 	}
520 
521 	// Fetch component for second-plane in the case of dual plane of weights.
522 	if (is_dual_plane)
523 	{
524 		scb.plane2_component = static_cast<int8_t>(read_bits(2, below_weights_pos - 2, pcb.data));
525 	}
526 }
527