aboutsummaryrefslogtreecommitdiff
path: root/src/decoder/logical_astc_block.cc
blob: 9271e181b466b9f5b1139ddd0232f9f56ae3ba68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/decoder/logical_astc_block.h"
#include "src/decoder/endpoint_codec.h"
#include "src/decoder/footprint.h"
#include "src/decoder/integer_sequence_codec.h"
#include "src/decoder/quantization.h"
#include "src/decoder/weight_infill.h"

namespace astc_codec {

namespace {

Partition GenerateSinglePartition(Footprint footprint) {
  return Partition { footprint, /* num_parts = */ 1, /* partition_id = */ 0,
        std::vector<int>(footprint.NumPixels(), 0) };
}

static std::vector<EndpointPair> DecodeEndpoints(const IntermediateBlockData& block) {
  const int endpoint_range = block.endpoint_range
      ? block.endpoint_range.value()
      : EndpointRangeForBlock(block);
  assert(endpoint_range > 0);

  std::vector<EndpointPair> endpoints;
  for (const auto& eps : block.endpoints) {
    RgbaColor decmp_one_rgba, decmp_two_rgba;
    DecodeColorsForMode(eps.colors, endpoint_range, eps.mode,
                        &decmp_one_rgba, &decmp_two_rgba);
    endpoints.emplace_back(decmp_one_rgba, decmp_two_rgba);
  }
  return endpoints;
}

static std::vector<EndpointPair> DecodeEndpoints(const VoidExtentData& block) {
  EndpointPair eps;
  eps.first[0] = eps.second[0] = (block.r * 255) / 65535;
  eps.first[1] = eps.second[1] = (block.g * 255) / 65535;
  eps.first[2] = eps.second[2] = (block.b * 255) / 65535;
  eps.first[3] = eps.second[3] = (block.a * 255) / 65535;

  std::vector<EndpointPair> endpoints;
  endpoints.emplace_back(eps);
  return endpoints;
}

Partition ComputePartition(const Footprint& footprint,
                           const IntermediateBlockData& block) {
  if (block.partition_id) {
    const int part_id = block.partition_id.value();
    const size_t num_parts = block.endpoints.size();
    return GetASTCPartition(footprint, num_parts, part_id);
  } else {
    return GenerateSinglePartition(footprint);
  }
}

Partition ComputePartition(const Footprint& footprint, const VoidExtentData&) {
  return GenerateSinglePartition(footprint);
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////

LogicalASTCBlock::LogicalASTCBlock(const Footprint& footprint)
    : endpoints_(1),
      weights_(footprint.NumPixels(), 0),
      partition_(GenerateSinglePartition(footprint)) { }

LogicalASTCBlock::LogicalASTCBlock(const Footprint& footprint,
                                   const IntermediateBlockData& block)
    : endpoints_(DecodeEndpoints(block)),
      partition_(ComputePartition(footprint, block)) {
  CalculateWeights(footprint, block);
}

LogicalASTCBlock::LogicalASTCBlock(const Footprint& footprint,
                                   const VoidExtentData& block)
    : endpoints_(DecodeEndpoints(block)),
      partition_(ComputePartition(footprint, block)) {
  CalculateWeights(footprint, block);
}

void LogicalASTCBlock::CalculateWeights(const Footprint& footprint,
                                        const IntermediateBlockData& block) {
  const int grid_size_x = block.weight_grid_dim_x;
  const int grid_size_y = block.weight_grid_dim_y;
  const int weight_grid_size = grid_size_x * grid_size_y;

  // Either we have a dual plane and we have twice as many weights as
  // specified or we don't
  assert(block.dual_plane_channel
        ? block.weights.size() == weight_grid_size * 2
        : block.weights.size() == weight_grid_size);

  std::vector<int> unquantized;
  unquantized.reserve(weight_grid_size);

  // According to C.2.16, if we have dual-plane weights, then we have two
  // weights per texel -- one adjacent to the other. Hence, we have to skip
  // some when we decode the separate weight values.
  const int weight_frequency = (block.dual_plane_channel) ? 2 : 1;
  const int weight_range = block.weight_range;

  for (int i = 0; i < weight_grid_size; ++i) {
    const int weight = block.weights[i * weight_frequency];
    unquantized.push_back(UnquantizeWeightFromRange(weight, weight_range));
  }
  weights_ = InfillWeights(unquantized, footprint, grid_size_x, grid_size_y);

  if (block.dual_plane_channel) {
    SetDualPlaneChannel(block.dual_plane_channel.value());
    for (int i = 0; i < weight_grid_size; ++i) {
      const int weight = block.weights[i * weight_frequency + 1];
      unquantized[i] = UnquantizeWeightFromRange(weight, weight_range);
    }
    dual_plane_->weights =
        InfillWeights(unquantized, footprint, grid_size_x, grid_size_y);
  }
}

void LogicalASTCBlock::CalculateWeights(const Footprint& footprint,
                                        const VoidExtentData&) {
  weights_ = std::vector<int>(footprint.NumPixels(), 0);
}

void LogicalASTCBlock::SetWeightAt(int x, int y, int weight) {
  assert(weight >= 0);
  assert(weight <= 64);
  weights_.at(y * GetFootprint().Width() + x) = weight;
}

int LogicalASTCBlock::WeightAt(int x, int y) const {
  return weights_.at(y * GetFootprint().Width() + x);
}

void LogicalASTCBlock::SetDualPlaneWeightAt(int channel, int x, int y,
                                            int weight) {
  assert(weight >= 0);
  assert(weight <= 64);

  // If it's not a dual plane, then this has no logical meaning
  assert(IsDualPlane());

  // If it is a dual plane and the passed channel matches the query, then we
  // return the specialized weights
  if (dual_plane_->channel == channel) {
    dual_plane_->weights.at(y * GetFootprint().Width() + x) = weight;
  } else {
    // If the channel is not the special channel, then return the general weight
    SetWeightAt(x, y, weight);
  }
}

int LogicalASTCBlock::DualPlaneWeightAt(int channel, int x, int y) const {
  // If it's not a dual plane, then we just return the weight for all channels
  if (!IsDualPlane()) {
    // TODO(google): Log warning, Requesting dual-plane channel for a non
    // dual-plane block!
    return WeightAt(x, y);
  }

  // If it is a dual plane and the passed channel matches the query, then we
  // return the specialized weights
  if (dual_plane_->channel == channel) {
    return dual_plane_->weights.at(y * GetFootprint().Width() + x);
  }

  // If the channel is not the special channel, then return the general weight
  return WeightAt(x, y);
}

void LogicalASTCBlock::SetDualPlaneChannel(int channel) {
  if (channel < 0) {
    dual_plane_.clear();
  } else if (dual_plane_) {
    dual_plane_->channel = channel;
  } else {
    dual_plane_ = DualPlaneData {channel, weights_};
  }
}

RgbaColor LogicalASTCBlock::ColorAt(int x, int y) const {
  const auto footprint = GetFootprint();
  assert(x >= 0);  assert(x < footprint.Width());
  assert(y >= 0);  assert(y < footprint.Height());

  const int texel_idx = y * footprint.Width() + x;
  const int part = partition_.assignment[texel_idx];
  const auto& endpoints = endpoints_[part];

  RgbaColor result;
  for (int channel = 0; channel < 4; ++channel) {
    const int weight = (dual_plane_ && dual_plane_->channel == channel)
        ? dual_plane_->weights[texel_idx]
        : weights_[texel_idx];
    const int p0 = endpoints.first[channel];
    const int p1 = endpoints.second[channel];

    assert(p0 >= 0); assert(p0 < 256);
    assert(p1 >= 0); assert(p1 < 256);

    // According to C.2.19
    const int c0 = (p0 << 8) | p0;
    const int c1 = (p1 << 8) | p1;
    const int c = (c0 * (64 - weight) + c1 * weight + 32) / 64;
    // TODO(google): Handle conversion to sRGB or FP16 per C.2.19.
    const int quantized = ((c * 255) + 32767) / 65536;
    assert(quantized < 256);
    result[channel] = quantized;
  }

  return result;
}

void LogicalASTCBlock::SetPartition(const Partition& p) {
  assert(p.footprint == partition_.footprint &&
         "New partitions may not be for a different footprint");
  partition_ = p;
  endpoints_.resize(p.num_parts);
}

void LogicalASTCBlock::SetEndpoints(const EndpointPair& eps, int subset) {
  assert(subset < partition_.num_parts);
  assert(subset < endpoints_.size());

  endpoints_[subset] = eps;
}

base::Optional<LogicalASTCBlock> UnpackLogicalBlock(
    const Footprint& footprint, const PhysicalASTCBlock& pb) {
  if (pb.IsVoidExtent()) {
    base::Optional<VoidExtentData> ve = UnpackVoidExtent(pb);
    if (!ve) {
      return {};
    }

    return LogicalASTCBlock(footprint, ve.value());
  } else {
    base::Optional<IntermediateBlockData> ib = UnpackIntermediateBlock(pb);
    if (!ib) {
      return {};
    }

    return LogicalASTCBlock(footprint, ib.value());
  }
}

}  // namespace astc_codec