1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ASTC_CODEC_DECODER_INTERMEDIATE_ASTC_BLOCK_H_
16 #define ASTC_CODEC_DECODER_INTERMEDIATE_ASTC_BLOCK_H_
17 
18 #include "src/base/optional.h"
19 #include "src/base/uint128.h"
20 #include "src/decoder/physical_astc_block.h"
21 
22 #include <array>
23 #include <vector>
24 
25 namespace astc_codec {
26 
27 // From Table C.2.7 -- These are the only valid ranges that weight
28 // values can take.
29 constexpr std::array<int, 12> kValidWeightRanges =
30 {{ 1, 2, 3, 4, 5, 7, 9, 11, 15, 19, 23, 31 }};
31 
32 // Void extent data are all the ASTC blocks that are labeled for having a
33 // constant color. In the ASTC spec, some of these blocks may optionally
34 // have "void extent coordinates" that describe how far in texture space
35 // the constant color should span. If these coordinates are not valid,
36 // then the coordinates are all set to a fully saturated bit mask
37 // ((1 << 13) - 1) and the block is treated as a singular constant color.
38 // We call both types of these blocks "void extent" to remove confusion
39 // in our code.
40 struct VoidExtentData {
41   uint16_t r;
42   uint16_t g;
43   uint16_t b;
44   uint16_t a;
45   std::array<uint16_t, 4> coords;
46 };
47 
48 // Intermediate endpoint data. Really this is just an endpoint mode
49 // and a couple of values that represent the data used to decode the
50 // RGB values from the color endpoint mode.
51 struct IntermediateEndpointData {
52   ColorEndpointMode mode;
53   std::vector<int> colors;
54 };
55 
56 // This is an unpacked physical ASTC block, but it does not have enough
57 // information to be worked with logically. It is simply a container of
58 // all of the unpacked ASTC information. It is used as a staging area
59 // for the information that is later Pack()'d into a PhysicalASTCBlock.
60 struct IntermediateBlockData {
61   int weight_grid_dim_x;
62   int weight_grid_dim_y;
63   int weight_range;
64 
65   // Quantized, non-interpolated weights
66   std::vector<int> weights;
67 
68   // The 10-bit partition ID if we need one
69   base::Optional<int> partition_id;
70 
71   // The dual-plane channel in [0, 3] if it exists.
72   base::Optional<int> dual_plane_channel;
73 
74   // The quantized/encoded endpoint values for this block. The range of each
75   // endpoint value is specified by |endpoint_range|, if it exists. If not, the
76   // range can be queried by calling EndpointRangeForBlock
77   std::vector<IntermediateEndpointData> endpoints;
78 
79   // The range [0, endpoint_range] that any one endpoint value can take. Users
80   // should not write to this value themselves. If it is empty at the time
81   // someone calls Pack(), it will be automatically inferred. Otherwise, it is
82   // set by Unpack() based on what the underlying encoding specified.
83   base::Optional<int> endpoint_range;
84 };
85 
86 // Returns the maximum value that a given endpoint value can take according to
87 // the other settings in the block. Ignores the |endpoint_range| member
88 // variable. Returns negative values on error:
89 //  -1 : Too many bits required to store weight grid
90 //  -2 : There are too few bits allocated for color endpoint data according to
91 //       C.2.24 in the ASTC spec
92 int EndpointRangeForBlock(const IntermediateBlockData& data);
93 inline int EndpointRangeForBlock(const VoidExtentData& data);
94 
95 // Unpacks the physical ASTC block into the intermediate block. Returns false
96 // if the physical block is an error encoded block, or if the physical block
97 // is a void extent block. On error the contents of ib are undefined.
98 base::Optional<IntermediateBlockData> UnpackIntermediateBlock(
99     const PhysicalASTCBlock& pb);
100 
101 // Unpacks the physical ASTC block into a void extent block. Returns false
102 // if the physical block is an error encoded block, or if the physical block
103 // is an intermediate block. On error the contents of ib are undefined.
104 base::Optional<VoidExtentData> UnpackVoidExtent(const PhysicalASTCBlock& pb);
105 
106 // Packs the given intermediate block into a physical block. Returns an error
107 // string if the provided values in the intermediate block emit an illegal ASTC
108 // encoding. In this case the results in the physical block are undefined.
109 base::Optional<std::string> Pack(const IntermediateBlockData& data,
110                                  base::UInt128* pb);
111 
112 // Packs the given void extent block into a physical block. Returns an error
113 // string if the provided values in the void extent block emit an illegal ASTC
114 // encoding. In this case the results in the physical block are undefined.
115 base::Optional<std::string> Pack(const VoidExtentData& data, base::UInt128* pb);
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 //
119 // Impl
120 
EndpointRangeForBlock(const VoidExtentData &)121 inline int EndpointRangeForBlock(const VoidExtentData&) {
122   // Void extent blocks use 16-bit ARGB definitions
123   return (1 << 16) - 1;
124 }
125 
126 }  // namespace astc_codec
127 
128 #endif  // ASTC_CODEC_DECODER_INTERMEDIATE_ASTC_BLOCK_H_
129