/src/libheif/libheif/codecs/uncompressed/decoder_abstract.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include <cstring> |
22 | | #include <algorithm> |
23 | | #include <iostream> |
24 | | #include <cassert> |
25 | | #include <utility> |
26 | | |
27 | | #if ((defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__PGI)) && __GNUC__ < 9) || (defined(__clang__) && __clang_major__ < 10) |
28 | | #include <type_traits> |
29 | | #else |
30 | | #include <bit> |
31 | | #endif |
32 | | |
33 | | #include "common_utils.h" |
34 | | #include "context.h" |
35 | | #include "compression.h" |
36 | | #include "error.h" |
37 | | #include "libheif/heif.h" |
38 | | #include "unc_types.h" |
39 | | #include "unc_boxes.h" |
40 | | #include "unc_codec.h" |
41 | | #include "decoder_abstract.h" |
42 | | #include "codecs/decoder.h" |
43 | | #include "codecs/uncompressed/unc_codec.h" |
44 | | |
45 | | |
46 | | AbstractDecoder::AbstractDecoder(uint32_t width, uint32_t height, const std::shared_ptr<const Box_cmpd> cmpd, const std::shared_ptr<const Box_uncC> uncC) : |
47 | 0 | m_width(width), |
48 | 0 | m_height(height), |
49 | 0 | m_cmpd(std::move(cmpd)), |
50 | 0 | m_uncC(std::move(uncC)) |
51 | 0 | { |
52 | 0 | m_tile_height = m_height / m_uncC->get_number_of_tile_rows(); |
53 | 0 | m_tile_width = m_width / m_uncC->get_number_of_tile_columns(); |
54 | |
|
55 | 0 | assert(m_tile_width > 0); |
56 | 0 | assert(m_tile_height > 0); |
57 | 0 | } |
58 | | |
59 | | void AbstractDecoder::buildChannelList(std::shared_ptr<HeifPixelImage>& img) |
60 | 0 | { |
61 | 0 | for (Box_uncC::Component component : m_uncC->get_components()) { |
62 | 0 | ChannelListEntry entry = buildChannelListEntry(component, img); |
63 | 0 | channelList.push_back(entry); |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | void AbstractDecoder::memcpy_to_native_endian(uint8_t* dst, uint32_t value, uint32_t bytes_per_sample) |
68 | 0 | { |
69 | | // TODO: this assumes that the file endianness is always big-endian. The endianness flags in the uncC header are not taken into account yet. |
70 | |
|
71 | 0 | if (bytes_per_sample==1) { |
72 | 0 | *dst = static_cast<uint8_t>(value); |
73 | 0 | return; |
74 | 0 | } |
75 | 0 | else if (std::endian::native == std::endian::big) { |
76 | 0 | for (uint32_t i = 0; i < bytes_per_sample; i++) { |
77 | 0 | dst[bytes_per_sample - 1 - i] = static_cast<uint8_t>((value >> (i * 8)) & 0xFF); |
78 | 0 | } |
79 | 0 | } |
80 | 0 | else { |
81 | 0 | for (uint32_t i = 0; i < bytes_per_sample; i++) { |
82 | 0 | dst[i] = static_cast<uint8_t>((value >> (i * 8)) & 0xFF); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | void AbstractDecoder::processComponentSample(UncompressedBitReader& srcBits, const ChannelListEntry& entry, uint64_t dst_row_offset, uint32_t tile_column, uint32_t tile_x) |
88 | 0 | { |
89 | 0 | uint64_t dst_col_number = static_cast<uint64_t>(tile_column) * entry.tile_width + tile_x; |
90 | 0 | uint64_t dst_column_offset = dst_col_number * entry.bytes_per_component_sample; |
91 | 0 | int val = srcBits.get_bits(entry.bits_per_component_sample); // get_bits() reads input in big-endian order |
92 | 0 | memcpy_to_native_endian(entry.dst_plane + dst_row_offset + dst_column_offset, val, entry.bytes_per_component_sample); |
93 | 0 | } |
94 | | |
95 | | // Handles the case where a row consists of a single component type |
96 | | // Not valid for Pixel interleave |
97 | | // Not valid for the Cb/Cr channels in Mixed Interleave |
98 | | // Not valid for multi-Y pixel interleave |
99 | | void AbstractDecoder::processComponentRow(ChannelListEntry& entry, UncompressedBitReader& srcBits, uint64_t dst_row_offset, uint32_t tile_column) |
100 | 0 | { |
101 | 0 | for (uint32_t tile_x = 0; tile_x < entry.tile_width; tile_x++) { |
102 | 0 | if (entry.component_alignment != 0) { |
103 | 0 | srcBits.skip_to_byte_boundary(); |
104 | 0 | int numPadBits = (entry.component_alignment * 8) - entry.bits_per_component_sample; |
105 | 0 | srcBits.skip_bits(numPadBits); |
106 | 0 | } |
107 | 0 | processComponentSample(srcBits, entry, dst_row_offset, tile_column, tile_x); |
108 | 0 | } |
109 | 0 | srcBits.skip_to_byte_boundary(); |
110 | 0 | } |
111 | | |
112 | | void AbstractDecoder::processComponentTileSample(UncompressedBitReader& srcBits, const ChannelListEntry& entry, uint64_t dst_offset, uint32_t tile_x) |
113 | 0 | { |
114 | 0 | uint64_t dst_sample_offset = uint64_t{tile_x} * entry.bytes_per_component_sample; |
115 | 0 | int val = srcBits.get_bits(entry.bits_per_component_sample); |
116 | 0 | memcpy_to_native_endian(entry.dst_plane + dst_offset + dst_sample_offset, val, entry.bytes_per_component_sample); |
117 | 0 | } |
118 | | |
119 | | // Handles the case where a row consists of a single component type |
120 | | // Not valid for Pixel interleave |
121 | | // Not valid for the Cb/Cr channels in Mixed Interleave |
122 | | // Not valid for multi-Y pixel interleave |
123 | | void AbstractDecoder::processComponentTileRow(ChannelListEntry& entry, UncompressedBitReader& srcBits, uint64_t dst_offset) |
124 | 0 | { |
125 | 0 | for (uint32_t tile_x = 0; tile_x < entry.tile_width; tile_x++) { |
126 | 0 | if (entry.component_alignment != 0) { |
127 | 0 | srcBits.skip_to_byte_boundary(); |
128 | 0 | int numPadBits = (entry.component_alignment * 8) - entry.bits_per_component_sample; |
129 | 0 | srcBits.skip_bits(numPadBits); |
130 | 0 | } |
131 | 0 | processComponentTileSample(srcBits, entry, dst_offset, tile_x); |
132 | 0 | } |
133 | 0 | srcBits.skip_to_byte_boundary(); |
134 | 0 | } |
135 | | |
136 | | |
137 | | AbstractDecoder::ChannelListEntry AbstractDecoder::buildChannelListEntry(Box_uncC::Component component, |
138 | | std::shared_ptr<HeifPixelImage>& img) |
139 | 0 | { |
140 | 0 | ChannelListEntry entry; |
141 | 0 | entry.use_channel = map_uncompressed_component_to_channel(m_cmpd, m_uncC, component, &(entry.channel)); |
142 | 0 | entry.dst_plane = img->get_plane(entry.channel, &(entry.dst_plane_stride)); |
143 | 0 | entry.tile_width = m_tile_width; |
144 | 0 | entry.tile_height = m_tile_height; |
145 | 0 | entry.other_chroma_dst_plane_stride = 0; // will be overwritten below if used |
146 | 0 | if ((entry.channel == heif_channel_Cb) || (entry.channel == heif_channel_Cr)) { |
147 | 0 | if (m_uncC->get_sampling_type() == sampling_mode_422) { |
148 | 0 | entry.tile_width /= 2; |
149 | 0 | } |
150 | 0 | else if (m_uncC->get_sampling_type() == sampling_mode_420) { |
151 | 0 | entry.tile_width /= 2; |
152 | 0 | entry.tile_height /= 2; |
153 | 0 | } |
154 | 0 | if (entry.channel == heif_channel_Cb) { |
155 | 0 | entry.other_chroma_dst_plane = img->get_plane(heif_channel_Cr, &(entry.other_chroma_dst_plane_stride)); |
156 | 0 | } |
157 | 0 | else if (entry.channel == heif_channel_Cr) { |
158 | 0 | entry.other_chroma_dst_plane = img->get_plane(heif_channel_Cb, &(entry.other_chroma_dst_plane_stride)); |
159 | 0 | } |
160 | 0 | } |
161 | 0 | entry.bits_per_component_sample = component.component_bit_depth; |
162 | 0 | entry.component_alignment = component.component_align_size; |
163 | 0 | entry.bytes_per_component_sample = (component.component_bit_depth + 7) / 8; |
164 | 0 | entry.bytes_per_tile_row_src = entry.tile_width * entry.bytes_per_component_sample; |
165 | 0 | return entry; |
166 | 0 | } |
167 | | |
168 | | |
169 | | const Error AbstractDecoder::get_compressed_image_data_uncompressed(const DataExtent& dataExtent, |
170 | | const UncompressedImageCodec::unci_properties& properties, |
171 | | std::vector<uint8_t>* data, |
172 | | uint64_t range_start_offset, uint64_t range_size, |
173 | | uint32_t tile_idx, |
174 | | const Box_iloc::Item* item) const |
175 | 0 | { |
176 | | // --- get codec configuration |
177 | |
|
178 | 0 | std::shared_ptr<const Box_cmpC> cmpC_box = properties.cmpC; |
179 | 0 | std::shared_ptr<const Box_icef> icef_box = properties.icef; |
180 | |
|
181 | 0 | if (!cmpC_box) { |
182 | | // assume no generic compression |
183 | 0 | auto readResult = dataExtent.read_data(range_start_offset, range_size); |
184 | 0 | if (readResult.error) { |
185 | 0 | return readResult.error; |
186 | 0 | } |
187 | | |
188 | 0 | data->insert(data->end(), readResult.value.begin(), readResult.value.end()); |
189 | |
|
190 | 0 | return Error::Ok; |
191 | 0 | } |
192 | | |
193 | 0 | if (icef_box && cmpC_box->get_compressed_unit_type() == heif_cmpC_compressed_unit_type_image_tile) { |
194 | 0 | const auto& units = icef_box->get_units(); |
195 | 0 | if (tile_idx >= units.size()) { |
196 | 0 | return {heif_error_Invalid_input, |
197 | 0 | heif_suberror_Unspecified, |
198 | 0 | "no icef-box entry for tile index"}; |
199 | 0 | } |
200 | | |
201 | 0 | const auto unit = units[tile_idx]; |
202 | | |
203 | | // get data needed for one tile |
204 | 0 | Result<std::vector<uint8_t>> readingResult = dataExtent.read_data(unit.unit_offset, unit.unit_size); |
205 | 0 | if (readingResult.error) { |
206 | 0 | return readingResult.error; |
207 | 0 | } |
208 | | |
209 | 0 | const std::vector<uint8_t>& compressed_bytes = readingResult.value; |
210 | | |
211 | | // decompress only the unit |
212 | 0 | auto dataResult = do_decompress_data(cmpC_box, compressed_bytes); |
213 | 0 | if (dataResult.error) { |
214 | 0 | return dataResult.error; |
215 | 0 | } |
216 | | |
217 | 0 | *data = std::move(*dataResult); |
218 | 0 | } |
219 | 0 | else if (icef_box) { |
220 | | // get all data and decode all |
221 | 0 | Result<std::vector<uint8_t>*> readResult = dataExtent.read_data(); |
222 | 0 | if (readResult.error) { |
223 | 0 | return readResult.error; |
224 | 0 | } |
225 | | |
226 | 0 | const std::vector<uint8_t>& compressed_bytes = *readResult.value; |
227 | |
|
228 | 0 | for (Box_icef::CompressedUnitInfo unit_info : icef_box->get_units()) { |
229 | 0 | auto unit_start = compressed_bytes.begin() + unit_info.unit_offset; |
230 | 0 | auto unit_end = unit_start + unit_info.unit_size; |
231 | 0 | std::vector<uint8_t> compressed_unit_data = std::vector<uint8_t>(unit_start, unit_end); |
232 | |
|
233 | 0 | auto dataResult = do_decompress_data(cmpC_box, std::move(compressed_unit_data)); |
234 | 0 | if (dataResult.error) { |
235 | 0 | return dataResult.error; |
236 | 0 | } |
237 | | |
238 | 0 | const std::vector<uint8_t>& uncompressed_unit_data= dataResult.value; |
239 | 0 | data->insert(data->end(), uncompressed_unit_data.data(), uncompressed_unit_data.data() + uncompressed_unit_data.size()); |
240 | 0 | } |
241 | | |
242 | | // cut out the range that we actually need |
243 | 0 | memcpy(data->data(), data->data() + range_start_offset, range_size); |
244 | 0 | data->resize(range_size); |
245 | 0 | } |
246 | 0 | else { |
247 | | // get all data and decode all |
248 | 0 | Result<std::vector<uint8_t>*> readResult = dataExtent.read_data(); |
249 | 0 | if (readResult.error) { |
250 | 0 | return readResult.error; |
251 | 0 | } |
252 | | |
253 | 0 | std::vector<uint8_t> compressed_bytes = *readResult.value; |
254 | | |
255 | | // Decode as a single blob |
256 | 0 | auto dataResult = do_decompress_data(cmpC_box, compressed_bytes); |
257 | 0 | if (dataResult.error) { |
258 | 0 | return dataResult.error; |
259 | 0 | } |
260 | | |
261 | 0 | *data = std::move(*dataResult); |
262 | | |
263 | | // cut out the range that we actually need |
264 | 0 | memcpy(data->data(), data->data() + range_start_offset, range_size); |
265 | 0 | data->resize(range_size); |
266 | 0 | } |
267 | | |
268 | 0 | return Error::Ok; |
269 | 0 | } |
270 | | |
271 | | |
272 | | Result<std::vector<uint8_t>> AbstractDecoder::do_decompress_data(std::shared_ptr<const Box_cmpC>& cmpC_box, |
273 | | std::vector<uint8_t> compressed_data) const |
274 | 0 | { |
275 | 0 | if (cmpC_box->get_compression_type() == fourcc("brot")) { |
276 | 0 | #if HAVE_BROTLI |
277 | 0 | return decompress_brotli(compressed_data); |
278 | | #else |
279 | | std::stringstream sstr; |
280 | | sstr << "cannot decode unci item with brotli compression - not enabled" << std::endl; |
281 | | return Error(heif_error_Unsupported_feature, |
282 | | heif_suberror_Unsupported_generic_compression_method, |
283 | | sstr.str()); |
284 | | #endif |
285 | 0 | } |
286 | 0 | else if (cmpC_box->get_compression_type() == fourcc("zlib")) { |
287 | 0 | #if HAVE_ZLIB |
288 | 0 | return decompress_zlib(compressed_data); |
289 | | #else |
290 | | std::stringstream sstr; |
291 | | sstr << "cannot decode unci item with zlib compression - not enabled" << std::endl; |
292 | | return Error(heif_error_Unsupported_feature, |
293 | | heif_suberror_Unsupported_generic_compression_method, |
294 | | sstr.str()); |
295 | | #endif |
296 | 0 | } |
297 | 0 | else if (cmpC_box->get_compression_type() == fourcc("defl")) { |
298 | 0 | #if HAVE_ZLIB |
299 | 0 | return decompress_deflate(compressed_data); |
300 | | #else |
301 | | std::stringstream sstr; |
302 | | sstr << "cannot decode unci item with deflate compression - not enabled" << std::endl; |
303 | | return Error(heif_error_Unsupported_feature, |
304 | | heif_suberror_Unsupported_generic_compression_method, |
305 | | sstr.str()); |
306 | | #endif |
307 | 0 | } |
308 | 0 | else { |
309 | 0 | std::stringstream sstr; |
310 | 0 | sstr << "cannot decode unci item with unsupported compression type: " << cmpC_box->get_compression_type() << std::endl; |
311 | 0 | return Error(heif_error_Unsupported_feature, |
312 | 0 | heif_suberror_Unsupported_generic_compression_method, |
313 | 0 | sstr.str()); |
314 | 0 | } |
315 | 0 | } |