/src/draco/src/draco/compression/mesh/mesh_sequential_decoder.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2016 The Draco Authors. |
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 | | // http://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 | | #include "draco/compression/mesh/mesh_sequential_decoder.h" |
16 | | |
17 | | #include <cstdint> |
18 | | #include <limits> |
19 | | |
20 | | #include "draco/compression/attributes/linear_sequencer.h" |
21 | | #include "draco/compression/attributes/sequential_attribute_decoders_controller.h" |
22 | | #include "draco/compression/entropy/symbol_decoding.h" |
23 | | #include "draco/core/varint_decoding.h" |
24 | | |
25 | | namespace draco { |
26 | | |
27 | 150 | MeshSequentialDecoder::MeshSequentialDecoder() {} |
28 | | |
29 | 140 | bool MeshSequentialDecoder::DecodeConnectivity() { |
30 | 140 | uint32_t num_faces; |
31 | 140 | uint32_t num_points; |
32 | 140 | #ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED |
33 | 140 | if (bitstream_version() < DRACO_BITSTREAM_VERSION(2, 2)) { |
34 | 55 | if (!buffer()->Decode(&num_faces)) { |
35 | 0 | return false; |
36 | 0 | } |
37 | 55 | if (!buffer()->Decode(&num_points)) { |
38 | 0 | return false; |
39 | 0 | } |
40 | | |
41 | 55 | } else |
42 | 85 | #endif |
43 | 85 | { |
44 | 85 | if (!DecodeVarint(&num_faces, buffer())) { |
45 | 0 | return false; |
46 | 0 | } |
47 | 85 | if (!DecodeVarint(&num_points, buffer())) { |
48 | 0 | return false; |
49 | 0 | } |
50 | 85 | } |
51 | | |
52 | | // Check that num_faces and num_points are valid values. |
53 | 140 | const uint64_t faces_64 = static_cast<uint64_t>(num_faces); |
54 | | // Compressed sequential encoding can only handle (2^32 - 1) / 3 indices. |
55 | 140 | if (faces_64 > 0xffffffff / 3) { |
56 | 0 | return false; |
57 | 0 | } |
58 | 140 | if (faces_64 > buffer()->remaining_size() / 3) { |
59 | | // The number of faces is unreasonably high, because face indices do not |
60 | | // fit in the remaining size of the buffer. |
61 | 2 | return false; |
62 | 2 | } |
63 | 138 | uint8_t connectivity_method; |
64 | 138 | if (!buffer()->Decode(&connectivity_method)) { |
65 | 0 | return false; |
66 | 0 | } |
67 | 138 | if (connectivity_method == 0) { |
68 | 85 | if (!DecodeAndDecompressIndices(num_faces)) { |
69 | 79 | return false; |
70 | 79 | } |
71 | 85 | } else { |
72 | 53 | if (num_points < 256) { |
73 | | // Decode indices as uint8_t. |
74 | 55.7k | for (uint32_t i = 0; i < num_faces; ++i) { |
75 | 55.7k | Mesh::Face face; |
76 | 222k | for (int j = 0; j < 3; ++j) { |
77 | 167k | uint8_t val; |
78 | 167k | if (!buffer()->Decode(&val)) { |
79 | 0 | return false; |
80 | 0 | } |
81 | 167k | face[j] = val; |
82 | 167k | } |
83 | 55.7k | mesh()->AddFace(face); |
84 | 55.7k | } |
85 | 34 | } else if (num_points < (1 << 16)) { |
86 | | // Decode indices as uint16_t. |
87 | 71.5k | for (uint32_t i = 0; i < num_faces; ++i) { |
88 | 71.5k | Mesh::Face face; |
89 | 286k | for (int j = 0; j < 3; ++j) { |
90 | 214k | uint16_t val; |
91 | 214k | if (!buffer()->Decode(&val)) { |
92 | 4 | return false; |
93 | 4 | } |
94 | 214k | face[j] = val; |
95 | 214k | } |
96 | 71.5k | mesh()->AddFace(face); |
97 | 71.5k | } |
98 | 10 | } else if (num_points < (1 << 21) && |
99 | 10 | bitstream_version() >= DRACO_BITSTREAM_VERSION(2, 2)) { |
100 | | // Decode indices as uint32_t. |
101 | 0 | for (uint32_t i = 0; i < num_faces; ++i) { |
102 | 0 | Mesh::Face face; |
103 | 0 | for (int j = 0; j < 3; ++j) { |
104 | 0 | uint32_t val; |
105 | 0 | if (!DecodeVarint(&val, buffer())) { |
106 | 0 | return false; |
107 | 0 | } |
108 | 0 | face[j] = val; |
109 | 0 | } |
110 | 0 | mesh()->AddFace(face); |
111 | 0 | } |
112 | 10 | } else { |
113 | | // Decode faces as uint32_t (default). |
114 | 10.4k | for (uint32_t i = 0; i < num_faces; ++i) { |
115 | 10.4k | Mesh::Face face; |
116 | 41.8k | for (int j = 0; j < 3; ++j) { |
117 | 31.3k | uint32_t val; |
118 | 31.3k | if (!buffer()->Decode(&val)) { |
119 | 6 | return false; |
120 | 6 | } |
121 | 31.3k | face[j] = val; |
122 | 31.3k | } |
123 | 10.4k | mesh()->AddFace(face); |
124 | 10.4k | } |
125 | 10 | } |
126 | 53 | } |
127 | 49 | point_cloud()->set_num_points(num_points); |
128 | 49 | return true; |
129 | 138 | } |
130 | | |
131 | 2.02k | bool MeshSequentialDecoder::CreateAttributesDecoder(int32_t att_decoder_id) { |
132 | | // Always create the basic attribute decoder. |
133 | 2.02k | return SetAttributesDecoder( |
134 | 2.02k | att_decoder_id, |
135 | 2.02k | std::unique_ptr<AttributesDecoder>( |
136 | 2.02k | new SequentialAttributeDecodersController( |
137 | 2.02k | std::unique_ptr<PointsSequencer>( |
138 | 2.02k | new LinearSequencer(point_cloud()->num_points()))))); |
139 | 2.02k | } |
140 | | |
141 | 85 | bool MeshSequentialDecoder::DecodeAndDecompressIndices(uint32_t num_faces) { |
142 | | // Get decoded indices differences that were encoded with an entropy code. |
143 | 85 | std::vector<uint32_t> indices_buffer(num_faces * 3); |
144 | 85 | if (!DecodeSymbols(num_faces * 3, 1, buffer(), indices_buffer.data())) { |
145 | 79 | return false; |
146 | 79 | } |
147 | | // Reconstruct the indices from the differences. |
148 | | // See MeshSequentialEncoder::CompressAndEncodeIndices() for more details. |
149 | 6 | int32_t last_index_value = 0; // This will always be >= 0. |
150 | 6 | int vertex_index = 0; |
151 | 95 | for (uint32_t i = 0; i < num_faces; ++i) { |
152 | 89 | Mesh::Face face; |
153 | 356 | for (int j = 0; j < 3; ++j) { |
154 | 267 | const uint32_t encoded_val = indices_buffer[vertex_index++]; |
155 | 267 | int32_t index_diff = (encoded_val >> 1); |
156 | 267 | if (encoded_val & 1) { |
157 | 199 | if (index_diff > last_index_value) { |
158 | | // Subtracting index_diff would result in a negative index. |
159 | 0 | return false; |
160 | 0 | } |
161 | 199 | index_diff = -index_diff; |
162 | 199 | } else { |
163 | 68 | if (index_diff > |
164 | 68 | (std::numeric_limits<int32_t>::max() - last_index_value)) { |
165 | | // Adding index_diff to last_index_value would overflow. |
166 | 0 | return false; |
167 | 0 | } |
168 | 68 | } |
169 | 267 | const int32_t index_value = index_diff + last_index_value; |
170 | 267 | face[j] = index_value; |
171 | 267 | last_index_value = index_value; |
172 | 267 | } |
173 | 89 | mesh()->AddFace(face); |
174 | 89 | } |
175 | 6 | return true; |
176 | 6 | } |
177 | | |
178 | | } // namespace draco |