• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static const int HIGH_SPEED_PROFILE_COLOR_BYTES = 8;
99 static const int HIGH_SPEED_PROFILE_WEIGHT_BYTES = 16;
100 /* See header for documentation. */
symbolic_to_physical(const block_size_descriptor & bsd,const symbolic_compressed_block & scb,physical_compressed_block & pcb)101 void symbolic_to_physical(
102 	const block_size_descriptor& bsd,
103 	const symbolic_compressed_block& scb,
104 	physical_compressed_block& pcb
105 ) {
106 	assert(scb.block_type != SYM_BTYPE_ERROR);
107 	if (scb.privateProfile == HIGH_SPEED_PROFILE)
108 	{
109 		uint8_t weightbuf[HIGH_SPEED_PROFILE_WEIGHT_BYTES] = {0};
110 		encode_ise(QUANT_6, HIGH_SPEED_PROFILE_WEIGHT_BYTES, scb.weights, weightbuf, 0);
111 		for (int i = 0; i < HIGH_SPEED_PROFILE_WEIGHT_BYTES; i++)
112 		{
113 			pcb.data[i] = static_cast<uint8_t>(bitrev8(weightbuf[HIGH_SPEED_PROFILE_WEIGHT_BYTES - 1 - i]));
114 		}
115 		pcb.data[0] = 0x43; // the first byte of every block stream is 0x43 for HIGH_SPEED_PROFILE
116 		pcb.data[1] = 0x80; // the second byte of every block stream is 0x80 for HIGH_SPEED_PROFILE
117 		pcb.data[2] = 0x01; // the third (2 idx) byte of every block stream is 0x01 for HIGH_SPEED_PROFILE
118 		uint8_t values_to_encode[HIGH_SPEED_PROFILE_COLOR_BYTES];
119 		for (int j = 0; j < HIGH_SPEED_PROFILE_COLOR_BYTES; j++)
120 		{
121 			values_to_encode[j] = scb.color_values[0][j];
122 		}
123 		encode_ise(scb.get_color_quant_mode(), HIGH_SPEED_PROFILE_COLOR_BYTES,
124 			values_to_encode, pcb.data, 17); // the color is starting from 17th bit for HIGH_SPEED_PROFILE
125 		return;
126 	}
127 
128 	// Constant color block using UNORM16 colors
129 	if (scb.block_type == SYM_BTYPE_CONST_U16)
130 	{
131 		// There is currently no attempt to coalesce larger void-extents
132 		static const uint8_t cbytes[8] { 0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
133 		for (unsigned int i = 0; i < 8; i++)
134 		{
135 			pcb.data[i] = cbytes[i];
136 		}
137 
138 		for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++)
139 		{
140 			pcb.data[2 * i + 8] = scb.constant_color[i] & 0xFF;
141 			pcb.data[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF;
142 		}
143 
144 		return;
145 	}
146 
147 	// Constant color block using FP16 colors
148 	if (scb.block_type == SYM_BTYPE_CONST_F16)
149 	{
150 		// There is currently no attempt to coalesce larger void-extents
151 		static const uint8_t cbytes[8]  { 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
152 		for (unsigned int i = 0; i < 8; i++)
153 		{
154 			pcb.data[i] = cbytes[i];
155 		}
156 
157 		for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++)
158 		{
159 			pcb.data[2 * i + 8] = scb.constant_color[i] & 0xFF;
160 			pcb.data[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF;
161 		}
162 
163 		return;
164 	}
165 
166 	unsigned int partition_count = scb.partition_count;
167 
168 	// Compress the weights.
169 	// They are encoded as an ordinary integer-sequence, then bit-reversed
170 	uint8_t weightbuf[16] { 0 };
171 
172 	const auto& bm = bsd.get_block_mode(scb.block_mode);
173 	const auto& di = bsd.get_decimation_info(bm.decimation_mode);
174 	int weight_count = di.weight_count;
175 	quant_method weight_quant_method = bm.get_weight_quant_mode();
176 	int is_dual_plane = bm.is_dual_plane;
177 
178 	int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
179 
180 	int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method);
181 
182 	if (is_dual_plane)
183 	{
184 		uint8_t weights[64];
185 		for (int i = 0; i < weight_count; i++)
186 		{
187 			weights[2 * i] = scb.weights[i];
188 			weights[2 * i + 1] = scb.weights[i + WEIGHTS_PLANE2_OFFSET];
189 		}
190 		encode_ise(weight_quant_method, real_weight_count, weights, weightbuf, 0);
191 	}
192 	else
193 	{
194 		encode_ise(weight_quant_method, weight_count, scb.weights, weightbuf, 0);
195 	}
196 
197 	for (int i = 0; i < 16; i++)
198 	{
199 		pcb.data[i] = static_cast<uint8_t>(bitrev8(weightbuf[15 - i]));
200 	}
201 
202 	write_bits(scb.block_mode, 11, 0, pcb.data);
203 	write_bits(partition_count - 1, 2, 11, pcb.data);
204 
205 	int below_weights_pos = 128 - bits_for_weights;
206 
207 	// Encode partition index and color endpoint types for blocks with 2+ partitions
208 	if (partition_count > 1)
209 	{
210 		write_bits(scb.partition_index, 6, 13, pcb.data);
211 		write_bits(scb.partition_index >> 6, PARTITION_INDEX_BITS - 6, 19, pcb.data);
212 
213 		if (scb.color_formats_matched)
214 		{
215 			write_bits(scb.color_formats[0] << 2, 6, 13 + PARTITION_INDEX_BITS, pcb.data);
216 		}
217 		else
218 		{
219 			// Check endpoint types for each partition to determine the lowest class present
220 			int low_class = 4;
221 
222 			for (unsigned int i = 0; i < partition_count; i++)
223 			{
224 				int class_of_format = scb.color_formats[i] >> 2;
225 				low_class = astc::min(class_of_format, low_class);
226 			}
227 
228 			if (low_class == 3)
229 			{
230 				low_class = 2;
231 			}
232 
233 			int encoded_type = low_class + 1;
234 			int bitpos = 2;
235 
236 			for (unsigned int i = 0; i < partition_count; i++)
237 			{
238 				int classbit_of_format = (scb.color_formats[i] >> 2) - low_class;
239 				encoded_type |= classbit_of_format << bitpos;
240 				bitpos++;
241 			}
242 
243 			for (unsigned int i = 0; i < partition_count; i++)
244 			{
245 				int lowbits_of_format = scb.color_formats[i] & 3;
246 				encoded_type |= lowbits_of_format << bitpos;
247 				bitpos += 2;
248 			}
249 
250 			int encoded_type_lowpart = encoded_type & 0x3F;
251 			int encoded_type_highpart = encoded_type >> 6;
252 			int encoded_type_highpart_size = (3 * partition_count) - 4;
253 			int encoded_type_highpart_pos = 128 - bits_for_weights - encoded_type_highpart_size;
254 			write_bits(encoded_type_lowpart, 6, 13 + PARTITION_INDEX_BITS, pcb.data);
255 			write_bits(encoded_type_highpart, encoded_type_highpart_size, encoded_type_highpart_pos, pcb.data);
256 			below_weights_pos -= encoded_type_highpart_size;
257 		}
258 	}
259 	else
260 	{
261 		write_bits(scb.color_formats[0], 4, 13, pcb.data);
262 	}
263 
264 	// In dual-plane mode, encode the color component of the second plane of weights
265 	if (is_dual_plane)
266 	{
267 		write_bits(scb.plane2_component, 2, below_weights_pos - 2, pcb.data);
268 	}
269 
270 	// Encode the color components
271 	uint8_t values_to_encode[32];
272 	int valuecount_to_encode = 0;
273 	for (unsigned int i = 0; i < scb.partition_count; i++)
274 	{
275 		int vals = 2 * (scb.color_formats[i] >> 2) + 2;
276 		assert(vals <= 8);
277 		for (int j = 0; j < vals; j++)
278 		{
279 			values_to_encode[j + valuecount_to_encode] = scb.color_values[i][j];
280 		}
281 		valuecount_to_encode += vals;
282 	}
283 
284 	encode_ise(scb.get_color_quant_mode(), valuecount_to_encode, values_to_encode, pcb.data,
285 	           scb.partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS);
286 }
287 
288 /* See header for documentation. */
physical_to_symbolic(const block_size_descriptor & bsd,const physical_compressed_block & pcb,symbolic_compressed_block & scb)289 void physical_to_symbolic(
290 	const block_size_descriptor& bsd,
291 	const physical_compressed_block& pcb,
292 	symbolic_compressed_block& scb
293 ) {
294 	uint8_t bswapped[16];
295 
296 	scb.block_type = SYM_BTYPE_NONCONST;
297 
298 	// Extract header fields
299 	int block_mode = read_bits(11, 0, pcb.data);
300 	if ((block_mode & 0x1FF) == 0x1FC)
301 	{
302 		// Constant color block
303 
304 		// Check what format the data has
305 		if (block_mode & 0x200)
306 		{
307 			scb.block_type = SYM_BTYPE_CONST_F16;
308 		}
309 		else
310 		{
311 			scb.block_type = SYM_BTYPE_CONST_U16;
312 		}
313 
314 		scb.partition_count = 0;
315 		for (int i = 0; i < 4; i++)
316 		{
317 			scb.constant_color[i] = pcb.data[2 * i + 8] | (pcb.data[2 * i + 9] << 8);
318 		}
319 
320 		// Additionally, check that the void-extent
321 		if (bsd.zdim == 1)
322 		{
323 			// 2D void-extent
324 			int rsvbits = read_bits(2, 10, pcb.data);
325 			if (rsvbits != 3)
326 			{
327 				scb.block_type = SYM_BTYPE_ERROR;
328 				return;
329 			}
330 
331 			int vx_low_s = read_bits(8, 12, pcb.data) | (read_bits(5, 12 + 8, pcb.data) << 8);
332 			int vx_high_s = read_bits(8, 25, pcb.data) | (read_bits(5, 25 + 8, pcb.data) << 8);
333 			int vx_low_t = read_bits(8, 38, pcb.data) | (read_bits(5, 38 + 8, pcb.data) << 8);
334 			int vx_high_t = read_bits(8, 51, pcb.data) | (read_bits(5, 51 + 8, pcb.data) << 8);
335 
336 			int all_ones = vx_low_s == 0x1FFF && vx_high_s == 0x1FFF && vx_low_t == 0x1FFF && vx_high_t == 0x1FFF;
337 
338 			if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t) && !all_ones)
339 			{
340 				scb.block_type = SYM_BTYPE_ERROR;
341 				return;
342 			}
343 		}
344 		else
345 		{
346 			// 3D void-extent
347 			int vx_low_s = read_bits(9, 10, pcb.data);
348 			int vx_high_s = read_bits(9, 19, pcb.data);
349 			int vx_low_t = read_bits(9, 28, pcb.data);
350 			int vx_high_t = read_bits(9, 37, pcb.data);
351 			int vx_low_p = read_bits(9, 46, pcb.data);
352 			int vx_high_p = read_bits(9, 55, pcb.data);
353 
354 			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;
355 
356 			if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t || vx_low_p >= vx_high_p) && !all_ones)
357 			{
358 				scb.block_type = SYM_BTYPE_ERROR;
359 				return;
360 			}
361 		}
362 
363 		return;
364 	}
365 
366 	unsigned int packed_index = bsd.block_mode_packed_index[block_mode];
367 	if (packed_index == BLOCK_BAD_BLOCK_MODE)
368 	{
369 		scb.block_type = SYM_BTYPE_ERROR;
370 		return;
371 	}
372 
373 	const auto& bm = bsd.get_block_mode(block_mode);
374 	const auto& di = bsd.get_decimation_info(bm.decimation_mode);
375 
376 	int weight_count = di.weight_count;
377 	quant_method weight_quant_method = static_cast<quant_method>(bm.quant_mode);
378 	int is_dual_plane = bm.is_dual_plane;
379 
380 	int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
381 
382 	int partition_count = read_bits(2, 11, pcb.data) + 1;
383 
384 	scb.block_mode = static_cast<uint16_t>(block_mode);
385 	scb.partition_count = static_cast<uint8_t>(partition_count);
386 
387 	for (int i = 0; i < 16; i++)
388 	{
389 		bswapped[i] = static_cast<uint8_t>(bitrev8(pcb.data[15 - i]));
390 	}
391 
392 	int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method);
393 
394 	int below_weights_pos = 128 - bits_for_weights;
395 
396 	if (is_dual_plane)
397 	{
398 		uint8_t indices[64];
399 		decode_ise(weight_quant_method, real_weight_count, bswapped, indices, 0);
400 		for (int i = 0; i < weight_count; i++)
401 		{
402 			scb.weights[i] = indices[2 * i];
403 			scb.weights[i + WEIGHTS_PLANE2_OFFSET] = indices[2 * i + 1];
404 		}
405 	}
406 	else
407 	{
408 		decode_ise(weight_quant_method, weight_count, bswapped, scb.weights, 0);
409 	}
410 
411 	if (is_dual_plane && partition_count == 4)
412 	{
413 		scb.block_type = SYM_BTYPE_ERROR;
414 		return;
415 	}
416 
417 	scb.color_formats_matched = 0;
418 
419 	// Determine the format of each endpoint pair
420 	int color_formats[BLOCK_MAX_PARTITIONS];
421 	int encoded_type_highpart_size = 0;
422 	if (partition_count == 1)
423 	{
424 		color_formats[0] = read_bits(4, 13, pcb.data);
425 		scb.partition_index = 0;
426 	}
427 	else
428 	{
429 		encoded_type_highpart_size = (3 * partition_count) - 4;
430 		below_weights_pos -= encoded_type_highpart_size;
431 		int encoded_type = read_bits(6, 13 + PARTITION_INDEX_BITS, pcb.data) | (read_bits(encoded_type_highpart_size, below_weights_pos, pcb.data) << 6);
432 		int baseclass = encoded_type & 0x3;
433 		if (baseclass == 0)
434 		{
435 			for (int i = 0; i < partition_count; i++)
436 			{
437 				color_formats[i] = (encoded_type >> 2) & 0xF;
438 			}
439 
440 			below_weights_pos += encoded_type_highpart_size;
441 			scb.color_formats_matched = 1;
442 			encoded_type_highpart_size = 0;
443 		}
444 		else
445 		{
446 			int bitpos = 2;
447 			baseclass--;
448 
449 			for (int i = 0; i < partition_count; i++)
450 			{
451 				color_formats[i] = (((encoded_type >> bitpos) & 1) + baseclass) << 2;
452 				bitpos++;
453 			}
454 
455 			for (int i = 0; i < partition_count; i++)
456 			{
457 				color_formats[i] |= (encoded_type >> bitpos) & 3;
458 				bitpos += 2;
459 			}
460 		}
461 		scb.partition_index = static_cast<uint16_t>(read_bits(6, 13, pcb.data) | (read_bits(PARTITION_INDEX_BITS - 6, 19, pcb.data) << 6));
462 	}
463 
464 	for (int i = 0; i < partition_count; i++)
465 	{
466 		scb.color_formats[i] = static_cast<uint8_t>(color_formats[i]);
467 	}
468 
469 	// Determine number of color endpoint integers
470 	int color_integer_count = 0;
471 	for (int i = 0; i < partition_count; i++)
472 	{
473 		int endpoint_class = color_formats[i] >> 2;
474 		color_integer_count += (endpoint_class + 1) * 2;
475 	}
476 
477 	if (color_integer_count > 18)
478 	{
479 		scb.block_type = SYM_BTYPE_ERROR;
480 		return;
481 	}
482 
483 	// Determine the color endpoint format to use
484 	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 };
485 	int color_bits = color_bits_arr[partition_count] - bits_for_weights - encoded_type_highpart_size;
486 	if (is_dual_plane)
487 	{
488 		color_bits -= 2;
489 	}
490 
491 	if (color_bits < 0)
492 	{
493 		color_bits = 0;
494 	}
495 
496 	int color_quant_level = quant_mode_table[color_integer_count >> 1][color_bits];
497 	if (color_quant_level < QUANT_6)
498 	{
499 		scb.block_type = SYM_BTYPE_ERROR;
500 		return;
501 	}
502 
503 	// Unpack the integer color values and assign to endpoints
504 	scb.quant_mode = static_cast<quant_method>(color_quant_level);
505 	uint8_t values_to_decode[32];
506 	decode_ise(static_cast<quant_method>(color_quant_level), color_integer_count, pcb.data,
507 	           values_to_decode, (partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS));
508 
509 	int valuecount_to_decode = 0;
510 	for (int i = 0; i < partition_count; i++)
511 	{
512 		int vals = 2 * (color_formats[i] >> 2) + 2;
513 		for (int j = 0; j < vals; j++)
514 		{
515 			scb.color_values[i][j] = values_to_decode[j + valuecount_to_decode];
516 		}
517 		valuecount_to_decode += vals;
518 	}
519 
520 	// Fetch component for second-plane in the case of dual plane of weights.
521 	if (is_dual_plane)
522 	{
523 		scb.plane2_component = static_cast<int8_t>(read_bits(2, below_weights_pos - 2, pcb.data));
524 	}
525 }
526