/src/libheif/libheif/context.h
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2017 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_CONTEXT_H |
22 | | #define LIBHEIF_CONTEXT_H |
23 | | |
24 | | #include <map> |
25 | | #include <memory> |
26 | | #include <set> |
27 | | #include <string> |
28 | | #include <vector> |
29 | | #include <utility> |
30 | | |
31 | | #include "error.h" |
32 | | |
33 | | #include "libheif/heif.h" |
34 | | #include "libheif/heif_experimental.h" |
35 | | #include "libheif/heif_plugin.h" |
36 | | #include "bitstream.h" |
37 | | |
38 | | #include "box.h" // only for color_profile, TODO: maybe move the color_profiles to its own header |
39 | | #include "file.h" |
40 | | #include "region.h" |
41 | | |
42 | | #include "text.h" |
43 | | |
44 | | class HeifFile; |
45 | | |
46 | | class HeifPixelImage; |
47 | | |
48 | | class StreamWriter; |
49 | | |
50 | | class ImageItem; |
51 | | |
52 | | class Track; |
53 | | |
54 | | struct TrackOptions; |
55 | | |
56 | | |
57 | | // This is a higher-level view than HeifFile. |
58 | | // Images are grouped logically into main images and their thumbnails. |
59 | | // The class also handles automatic color-space conversion. |
60 | | class HeifContext : public ErrorBuffer |
61 | | { |
62 | | public: |
63 | | HeifContext(); |
64 | | |
65 | | ~HeifContext(); |
66 | | |
67 | 0 | void set_max_decoding_threads(int max_threads) { m_max_decoding_threads = max_threads; } |
68 | | |
69 | 381 | int get_max_decoding_threads() const { return m_max_decoding_threads; } |
70 | | |
71 | | void set_security_limits(const heif_security_limits* limits); |
72 | | |
73 | 60.4k | [[nodiscard]] heif_security_limits* get_security_limits() { return &m_limits; } |
74 | | |
75 | 74.7k | [[nodiscard]] const heif_security_limits* get_security_limits() const { return &m_limits; } |
76 | | |
77 | | Error read(const std::shared_ptr<StreamReader>& reader); |
78 | | |
79 | | Error read_from_file(const char* input_filename); |
80 | | |
81 | | Error read_from_memory(const void* data, size_t size, bool copy); |
82 | | |
83 | 191k | std::shared_ptr<HeifFile> get_heif_file() const { return m_heif_file; } |
84 | | |
85 | | |
86 | | // === image items === |
87 | | |
88 | | std::vector<std::shared_ptr<ImageItem>> get_top_level_images(bool return_error_images); |
89 | | |
90 | 0 | void insert_image_item(heif_item_id id, const std::shared_ptr<ImageItem>& img) { |
91 | 0 | m_all_images.insert(std::make_pair(id, img)); |
92 | 0 | } |
93 | | |
94 | | std::shared_ptr<ImageItem> get_image(heif_item_id id, bool return_error_images); |
95 | | |
96 | | std::shared_ptr<const ImageItem> get_image(heif_item_id id, bool return_error_images) const |
97 | 858 | { |
98 | 858 | return const_cast<HeifContext*>(this)->get_image(id, return_error_images); |
99 | 858 | } |
100 | | |
101 | | std::shared_ptr<ImageItem> get_primary_image(bool return_error_image); |
102 | | |
103 | | std::shared_ptr<const ImageItem> get_primary_image(bool return_error_image) const; |
104 | | |
105 | | bool is_image(heif_item_id ID) const; |
106 | | |
107 | | bool has_alpha(heif_item_id ID) const; |
108 | | |
109 | | Result<std::shared_ptr<HeifPixelImage>> decode_image(heif_item_id ID, |
110 | | heif_colorspace out_colorspace, |
111 | | heif_chroma out_chroma, |
112 | | const heif_decoding_options& options, |
113 | | bool decode_only_tile, uint32_t tx, uint32_t ty) const; |
114 | | |
115 | | Result<std::shared_ptr<HeifPixelImage>> convert_to_output_colorspace(std::shared_ptr<HeifPixelImage> img, |
116 | | heif_colorspace out_colorspace, |
117 | | heif_chroma out_chroma, |
118 | | const heif_decoding_options& options) const; |
119 | | |
120 | | Error get_id_of_non_virtual_child_image(heif_item_id in, heif_item_id& out) const; |
121 | | |
122 | | std::string debug_dump_boxes() const; |
123 | | |
124 | | |
125 | | // === writing === |
126 | | |
127 | | void write(StreamWriter& writer); |
128 | | |
129 | | // Create all boxes necessary for an empty HEIF file. |
130 | | // Note that this is no valid HEIF file, since some boxes (e.g. pitm) are generated, but |
131 | | // contain no valid data yet. |
132 | | void reset_to_empty_heif(); |
133 | | |
134 | | Result<std::shared_ptr<ImageItem>> encode_image(const std::shared_ptr<HeifPixelImage>& image, |
135 | | heif_encoder* encoder, |
136 | | const heif_encoding_options& options, |
137 | | heif_image_input_class input_class); |
138 | | |
139 | | void set_primary_image(const std::shared_ptr<ImageItem>& image); |
140 | | |
141 | 0 | bool is_primary_image_set() const { return m_primary_image != nullptr; } |
142 | | |
143 | | Error assign_thumbnail(const std::shared_ptr<ImageItem>& master_image, |
144 | | const std::shared_ptr<ImageItem>& thumbnail_image); |
145 | | |
146 | | Result<std::shared_ptr<ImageItem>> encode_thumbnail(const std::shared_ptr<HeifPixelImage>& image, |
147 | | heif_encoder* encoder, |
148 | | const heif_encoding_options& options, |
149 | | int bbox_size); |
150 | | |
151 | | Error add_exif_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size); |
152 | | |
153 | | Error add_XMP_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size, heif_metadata_compression compression); |
154 | | |
155 | | Error add_generic_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size, |
156 | | uint32_t item_type, const char* content_type, const char* item_uri_type, |
157 | | heif_metadata_compression compression, heif_item_id* out_item_id); |
158 | | |
159 | | heif_property_id add_property(heif_item_id targetItem, std::shared_ptr<Box> property, bool essential); |
160 | | |
161 | | Result<heif_item_id> add_pyramid_group(const std::vector<heif_item_id>& layers); |
162 | | |
163 | | // --- region items |
164 | | |
165 | | void add_region_item(std::shared_ptr<RegionItem> region_item) |
166 | 0 | { |
167 | 0 | m_region_items.push_back(std::move(region_item)); |
168 | 0 | } |
169 | | |
170 | | std::shared_ptr<RegionItem> add_region_item(uint32_t reference_width, uint32_t reference_height); |
171 | | |
172 | | std::shared_ptr<RegionItem> get_region_item(heif_item_id id) const |
173 | 0 | { |
174 | 0 | for (auto& item : m_region_items) { |
175 | 0 | if (item->item_id == id) |
176 | 0 | return item; |
177 | 0 | } |
178 | 0 |
|
179 | 0 | return nullptr; |
180 | 0 | } |
181 | | |
182 | | void add_region_referenced_mask_ref(heif_item_id region_item_id, heif_item_id mask_item_id); |
183 | | |
184 | | |
185 | | // === sequences == |
186 | | |
187 | 0 | bool has_sequence() const { return !m_tracks.empty(); } |
188 | | |
189 | 0 | int get_number_of_tracks() const { return static_cast<int>(m_tracks.size()); } |
190 | | |
191 | | std::vector<uint32_t> get_track_IDs() const; |
192 | | |
193 | | // If 0 is passed as track_id, the main visual track is returned (we assume that there is only one visual track). |
194 | | Result<std::shared_ptr<Track>> get_track(uint32_t track_id); |
195 | | |
196 | | Result<std::shared_ptr<const Track>> get_track(uint32_t track_id) const; |
197 | | |
198 | | uint32_t get_sequence_timescale() const; |
199 | | |
200 | | uint64_t get_sequence_duration() const; |
201 | | |
202 | | void set_sequence_timescale(uint32_t timescale); |
203 | | |
204 | | void set_number_of_sequence_repetitions(uint32_t repetitions); |
205 | | |
206 | | Result<std::shared_ptr<class Track_Visual>> add_visual_sequence_track(const TrackOptions*, uint32_t handler_type, |
207 | | uint16_t width, uint16_t height); |
208 | | |
209 | | Result<std::shared_ptr<class Track_Metadata>> add_uri_metadata_sequence_track(const TrackOptions*, std::string uri); |
210 | | |
211 | | void add_text_item(std::shared_ptr<TextItem> text_item) |
212 | 0 | { |
213 | 0 | m_text_items.push_back(std::move(text_item)); |
214 | 0 | } |
215 | | |
216 | | std::shared_ptr<TextItem> add_text_item(const char* content_type, const char* text); |
217 | | |
218 | | std::shared_ptr<TextItem> get_text_item(heif_item_id id) const |
219 | 0 | { |
220 | 0 | for (auto& item : m_text_items) { |
221 | 0 | if (item->get_item_id() == id) |
222 | 0 | return item; |
223 | 0 | } |
224 | 0 |
|
225 | 0 | return nullptr; |
226 | 0 | } |
227 | | |
228 | | template<typename T> |
229 | | Result<std::shared_ptr<T>> find_property(heif_item_id itemId, heif_property_id propertyId) |
230 | | { |
231 | | auto file = this->get_heif_file(); |
232 | | |
233 | | // For propertyId == 0, return the first property with this type. |
234 | | if (propertyId == 0) { |
235 | | return find_property<T>(itemId); |
236 | | } |
237 | | |
238 | | std::vector<std::shared_ptr<Box>> properties; |
239 | | Error err = file->get_properties(itemId, properties); |
240 | | if (err) { |
241 | | return err; |
242 | | } |
243 | | |
244 | | if (propertyId - 1 >= properties.size()) { |
245 | | Error(heif_error_Usage_error, heif_suberror_Invalid_property, "property index out of range"); |
246 | | } |
247 | | |
248 | | auto box = properties[propertyId - 1]; |
249 | | auto box_casted = std::dynamic_pointer_cast<T>(box); |
250 | | if (!box_casted) { |
251 | | return Error(heif_error_Usage_error, heif_suberror_Invalid_property, "wrong property type"); |
252 | | } |
253 | | |
254 | | return box_casted; |
255 | | } |
256 | | |
257 | | template<typename T> |
258 | | Result<std::shared_ptr<T>> find_property(heif_item_id itemId) { |
259 | | auto file = this->get_heif_file(); |
260 | | auto result = file->get_property_for_item<T>(itemId); |
261 | | if (!result) { |
262 | | return Error(heif_error_Invalid_input, |
263 | | heif_suberror_No_properties_assigned_to_item, |
264 | | "property not found on item"); |
265 | | } |
266 | | return result; |
267 | | } |
268 | | |
269 | | private: |
270 | | std::map<heif_item_id, std::shared_ptr<ImageItem>> m_all_images; |
271 | | |
272 | | // We store this in a vector because we need stable indices for the C API. |
273 | | // TODO: stable indices are obsolet now... |
274 | | std::vector<std::shared_ptr<ImageItem>> m_top_level_images; |
275 | | |
276 | | std::shared_ptr<ImageItem> m_primary_image; // shortcut to primary image |
277 | | |
278 | | std::shared_ptr<HeifFile> m_heif_file; |
279 | | |
280 | | int m_max_decoding_threads = 4; |
281 | | |
282 | | heif_security_limits m_limits; |
283 | | TotalMemoryTracker m_memory_tracker; |
284 | | |
285 | | std::vector<std::shared_ptr<RegionItem>> m_region_items; |
286 | | std::vector<std::shared_ptr<TextItem>> m_text_items; |
287 | | |
288 | | // --- sequences |
289 | | |
290 | | std::map<uint32_t, std::shared_ptr<Track>> m_tracks; |
291 | | uint32_t m_visual_track_id = 0; |
292 | | uint32_t m_sequence_repetitions = 1; |
293 | | |
294 | | Error interpret_heif_file(); |
295 | | |
296 | | Error interpret_heif_file_images(); |
297 | | |
298 | | Error interpret_heif_file_sequences(); |
299 | | |
300 | | void remove_top_level_image(const std::shared_ptr<ImageItem>& image); |
301 | | }; |
302 | | |
303 | | #endif |