/src/libheif/libheif/image-items/tiled.h
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2024 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 | | #ifndef LIBHEIF_TILED_H |
22 | | #define LIBHEIF_TILED_H |
23 | | |
24 | | #include "image_item.h" |
25 | | #include "codecs/decoder.h" |
26 | | #include "box.h" |
27 | | #include <vector> |
28 | | #include <string> |
29 | | #include <memory> |
30 | | #include <utility> |
31 | | #include "libheif/heif_experimental.h" |
32 | | #include "libheif/heif_encoding.h" |
33 | | #include <set> |
34 | | |
35 | | |
36 | | Result<uint64_t> number_of_tiles(const heif_tiled_image_parameters& params, const heif_security_limits* limits); |
37 | | |
38 | | uint32_t nTiles_h(const heif_tiled_image_parameters& params); |
39 | | |
40 | | uint32_t nTiles_v(const heif_tiled_image_parameters& params); |
41 | | |
42 | | |
43 | | class Box_tilC : public FullBox |
44 | | { |
45 | | /* |
46 | | * Flags: |
47 | | * bit 0-1 - number of bits for offsets (0: 32, 1: 40, 2: 48, 3: 64) |
48 | | * bit 2-3 - number of bits for tile size (0: 0, 1: 24; 2: 32, 3: 64) |
49 | | * bit 4 - sequential ordering hint |
50 | | * bit 5 - use 64 bit dimensions (currently unused because ispe is limited to 32 bit) |
51 | | */ |
52 | | public: |
53 | | Box_tilC() |
54 | 0 | { |
55 | 0 | set_short_type(fourcc("tilC")); |
56 | 0 |
|
57 | 0 | init_heif_tiled_image_parameters(m_parameters); |
58 | 0 | } |
59 | | |
60 | 0 | bool is_essential() const override { return true; } |
61 | | |
62 | | void derive_box_version() override; |
63 | | |
64 | 0 | void set_parameters(const heif_tiled_image_parameters& params) { m_parameters = params; } |
65 | | |
66 | 0 | void set_compression_format(heif_compression_format format) { m_parameters.compression_format_fourcc = ImageItem::compression_format_to_fourcc_infe_type(format); } |
67 | | |
68 | 0 | const heif_tiled_image_parameters& get_parameters() const { return m_parameters; } |
69 | | |
70 | | Error write(StreamWriter& writer) const override; |
71 | | |
72 | | std::string dump(Indent&) const override; |
73 | | |
74 | 0 | std::vector<std::shared_ptr<Box>>& get_tile_properties() { return m_children; } |
75 | | |
76 | 0 | const std::vector<std::shared_ptr<Box>>& get_tile_properties() const { return m_children; } |
77 | | |
78 | | protected: |
79 | | Error parse(BitstreamRange& range, const heif_security_limits* limits) override; |
80 | | |
81 | | private: |
82 | | heif_tiled_image_parameters m_parameters; |
83 | | |
84 | | static void init_heif_tiled_image_parameters(heif_tiled_image_parameters& params); |
85 | | }; |
86 | | |
87 | | |
88 | | #define TILD_OFFSET_NOT_AVAILABLE 0 |
89 | | #define TILD_OFFSET_SEE_LOWER_RESOLUTION_LAYER 1 |
90 | | #define TILD_OFFSET_NOT_LOADED 10 |
91 | | |
92 | | class TiledHeader |
93 | | { |
94 | | public: |
95 | | Error set_parameters(const heif_tiled_image_parameters& params); |
96 | | |
97 | 0 | const heif_tiled_image_parameters& get_parameters() const { return m_parameters; } |
98 | | |
99 | 0 | void set_compression_format(heif_compression_format format) { m_parameters.compression_format_fourcc = ImageItem::compression_format_to_fourcc_infe_type(format); } |
100 | | |
101 | | Error read_full_offset_table(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id, const heif_security_limits* limits); |
102 | | |
103 | | Error read_offset_table_range(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id, |
104 | | uint64_t start, uint64_t end); |
105 | | |
106 | | Result<std::vector<uint8_t>> write_offset_table(); |
107 | | |
108 | | std::string dump() const; |
109 | | |
110 | | // Returns an error if `offset` does not fit in offset_field_length or |
111 | | // `size` does not fit in size_field_length. Catches the encoder-side |
112 | | // overflow that would otherwise silently truncate the field at write time. |
113 | | Error set_tild_tile_range(uint32_t tile_x, uint32_t tile_y, uint64_t offset, uint32_t size); |
114 | | |
115 | | size_t get_header_size() const; |
116 | | |
117 | 0 | size_t get_num_tiles() const { return m_offsets.size(); } |
118 | | |
119 | 0 | uint64_t get_tile_offset(uint32_t idx) const { return m_offsets[idx].offset; } |
120 | | |
121 | 0 | uint32_t get_tile_size(uint32_t idx) const { return m_offsets[idx].size; } |
122 | | |
123 | 0 | bool is_tile_offset_known(uint32_t idx) const { return m_offsets[idx].offset != TILD_OFFSET_NOT_LOADED; } |
124 | | |
125 | | uint32_t get_offset_table_entry_size() const; |
126 | | |
127 | | // Assuming that we have to read offset table 'idx', but we'd like to read more entries at once to reduce |
128 | | // the load of small network transfers (preferred: 'nEntries'). Return a range of indices to read. |
129 | | // This may be less than 'nEntries'. The returned range is [start, end) |
130 | | [[nodiscard]] std::pair<uint32_t, uint32_t> get_tile_offset_table_range_to_read(uint32_t idx, uint32_t nEntries) const; |
131 | | |
132 | | private: |
133 | | heif_tiled_image_parameters m_parameters; |
134 | | |
135 | | struct TileOffset { |
136 | | uint64_t offset = TILD_OFFSET_NOT_LOADED; |
137 | | uint32_t size = 0; |
138 | | }; |
139 | | |
140 | | // TODO uint64_t m_start_of_offset_table_in_file = 0; |
141 | | std::vector<TileOffset> m_offsets; |
142 | | |
143 | | // TODO size_t m_offset_table_start = 0; // start of offset table (= number of bytes in header) |
144 | | size_t m_header_size = 0; // including offset table |
145 | | }; |
146 | | |
147 | | |
148 | | class ImageItem_Tiled : public ImageItem |
149 | | { |
150 | | public: |
151 | | ImageItem_Tiled(HeifContext* ctx, heif_item_id id); |
152 | | |
153 | | ImageItem_Tiled(HeifContext* ctx); |
154 | | |
155 | | ~ImageItem_Tiled() override; |
156 | | |
157 | 0 | uint32_t get_infe_type() const override { return fourcc("tili"); } |
158 | | |
159 | | // TODO: nclx depends on contained format |
160 | | // const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; } |
161 | | |
162 | | heif_compression_format get_compression_format() const override; |
163 | | |
164 | | static Result<std::shared_ptr<ImageItem_Tiled>> add_new_tiled_item(HeifContext* ctx, const heif_tiled_image_parameters* parameters, |
165 | | const heif_encoder* encoder, |
166 | | const heif_encoding_options* encoding_options); |
167 | | |
168 | | Error add_image_tile(uint32_t tile_x, uint32_t tile_y, |
169 | | const std::shared_ptr<HeifPixelImage>& image, |
170 | | struct heif_encoder* encoder); |
171 | | |
172 | | |
173 | | Error initialize_decoder() override; |
174 | | |
175 | | // Copies per-component descriptions from the embedded tile item (which has |
176 | | // tile-sized dims) and rescales them to the full image's ispe dimensions, |
177 | | // so the handle exposes the same per-component metadata (datatype, |
178 | | // bit-depth, type) that the decoded image will report after a tile decode. |
179 | | void populate_component_descriptions() override; |
180 | | |
181 | | Error process_before_write() override; |
182 | | |
183 | | Error get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const override; |
184 | | |
185 | | int get_luma_bits_per_pixel() const override; |
186 | | |
187 | | int get_chroma_bits_per_pixel() const override; |
188 | | |
189 | | Result<Encoder::CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image, |
190 | | heif_encoder* encoder, |
191 | | const heif_encoding_options& options, |
192 | | heif_image_input_class input_class) override |
193 | 0 | { |
194 | 0 | return Error{heif_error_Unsupported_feature, |
195 | 0 | heif_suberror_Unspecified, "Cannot encode image to 'tild'"}; |
196 | 0 | } |
197 | | |
198 | | Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const heif_decoding_options& options, |
199 | | bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0, |
200 | | std::set<heif_item_id> processed_ids) const override; |
201 | | |
202 | | heif_brand2 get_compatible_brand() const override; |
203 | | |
204 | | // --- tild |
205 | | |
206 | 0 | void set_tild_header(const TiledHeader& header) { m_tild_header = header; } |
207 | | |
208 | 0 | TiledHeader& get_tild_header() { return m_tild_header; } |
209 | | |
210 | 0 | uint64_t get_next_tild_position() const { return m_next_tild_position; } |
211 | | |
212 | 0 | void set_next_tild_position(uint64_t pos) { m_next_tild_position = pos; } |
213 | | |
214 | | heif_image_tiling get_heif_image_tiling() const override; |
215 | | |
216 | | void get_tile_size(uint32_t& w, uint32_t& h) const override; |
217 | | |
218 | | private: |
219 | | TiledHeader m_tild_header; |
220 | | uint64_t m_next_tild_position = 0; |
221 | | |
222 | | heif_orientation m_image_orientation = heif_orientation_normal; |
223 | | heif_encoding_options* m_tile_encoding_options = nullptr; |
224 | | |
225 | | uint32_t mReadChunkSize_bytes = 64*1024; // 64 kiB |
226 | | bool m_preload_offset_table = false; |
227 | | |
228 | | std::shared_ptr<ImageItem> m_tile_item; |
229 | | std::shared_ptr<Decoder> m_tile_decoder; |
230 | | |
231 | | Result<DataExtent> |
232 | | get_compressed_data_for_tile(uint32_t tx, uint32_t ty) const; |
233 | | |
234 | | Result<std::shared_ptr<HeifPixelImage>> decode_grid_tile(const heif_decoding_options& options, uint32_t tx, uint32_t ty) const; |
235 | | |
236 | | Error load_tile_offset_entry(uint32_t idx); |
237 | | |
238 | | Error append_compressed_tile_data(std::vector<uint8_t>& data, uint32_t tx, uint32_t ty) const; |
239 | | }; |
240 | | |
241 | | |
242 | | #endif //LIBHEIF_TILED_H |