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