Debian Codegraph @

examples/heif_view.cc

raw
1 /*
2 libheif example application.
3
4 MIT License
5
6 Copyright (c) 2025 Dirk Farin <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27 #include <cstring>
28 #include <getopt.h>
29 #include "libheif/heif_experimental.h"
30 #include "libheif/heif_sequences.h"
31 #include <libheif/heif_tai_timestamps.h>
32
33 #if defined(HAVE_UNISTD_H)
34
35 #include <unistd.h>
36
37 #endif
38
39 #include <fstream>
40 #include <iostream>
41 #include <iomanip>
42 #include <sstream>
43 #include <cassert>
44 #include <algorithm>
45 #include <vector>
46 #include <array>
47 #include <cctype>
48 #include <memory>
49
50 #include <libheif/heif.h>
51
52 #include "common.h"
53 #include "sdl.hh"
54
55 #if defined(_MSC_VER)
56 #include "getopt.h"
57 #endif
58
59 #if !SDL_VERSION_ATLEAST(2, 0, 18)
60 #define SDL_GetTicks64 SDL_GetTicks
61 #endif
62
63 #define UNUSED(x) (void)x
64
65
66 static void show_help(const char* argv0)
67 {
68 std::cerr << " " << argv0 << " libheif version: " << heif_get_version() << "\n"
69 << "---------------------------------------\n"
70 "Usage: " << argv0 << " [options] <input-file>\n"
71 << "\n"
72 "Options:\n"
73 " -h, --help show help\n"
74 " -v, --version show version\n"
75 " --list-decoders list all available decoders (built-in and plugins)\n"
76 " -d, --decoder ID use a specific decoder (see --list-decoders)\n"
77 " --speedup FACTOR increase playback speed by FACTOR\n"
78 " --show-sai show sample auxiliary information\n"
79 " --show-frame-duration show each frame duration in milliseconds\n"
80 " --show-track-metadata show metadata attached to the track (e.g. TAI config)\n"
81 " --show-metadata-text show data in metadata track as text\n"
82 " --show-metadata-hex show data in metadata track as hex bytes\n"
83 " --show-all show all extra information\n"
84 " --ignore-editlist show the raw media timeline without repetitions\n";
85 }
86
87
88 class ContextReleaser {
89 public:
90 ContextReleaser(struct heif_context* ctx) : ctx_(ctx) {}
91
92 ~ContextReleaser()
93 {
94 heif_context_free(ctx_);
95 }
96
97 private:
98 struct heif_context* ctx_;
99 };
100
101
102 int option_list_decoders = 0;
103 double option_speedup = 1.0;
104 bool option_show_sai = false;
105 bool option_show_frame_duration = false;
106 bool option_show_track_metadata = false;
107 bool option_ignore_editlist = false;
108 enum {
109 metadata_output_none,
110 metadata_output_text,
111 metadata_output_hex
112 } option_metadata_output = metadata_output_none;
113
114 const int OPTION_SPEEDUP = 1000;
115 const int OPTION_SHOW_SAI = 1001;
116 const int OPTION_SHOW_FRAME_DURATION = 1002;
117 const int OPTION_SHOW_TRACK_METADATA = 1003;
118 const int OPTION_SHOW_ALL = 1004;
119 const int OPTION_SHOW_METADATA_TEXT = 1005;
120 const int OPTION_SHOW_METADATA_HEX = 1006;
121 const int OPTION_IGNORE_EDITLIST = 1007;
122
123 static struct option long_options[] = {
124 {(char* const) "decoder", required_argument, 0, 'd'},
125 {(char* const) "list-decoders", no_argument, &option_list_decoders, 1},
126 {(char* const) "help", no_argument, 0, 'h'},
127 {(char* const) "version", no_argument, 0, 'v'},
128 {(char* const) "speedup", required_argument, 0, OPTION_SPEEDUP},
129 {(char* const) "show-sai", no_argument, 0, OPTION_SHOW_SAI},
130 {(char* const) "show-frame-duration", no_argument, 0, OPTION_SHOW_FRAME_DURATION},
131 {(char* const) "show-track-metadata", no_argument, 0, OPTION_SHOW_TRACK_METADATA},
132 {(char* const) "show-metadata-text", no_argument, 0, OPTION_SHOW_METADATA_TEXT},
133 {(char* const) "show-metadata-hex", no_argument, 0, OPTION_SHOW_METADATA_HEX},
134 {(char* const) "show-all", no_argument, 0, OPTION_SHOW_ALL},
135 {(char* const) "ignore-editlist", no_argument, 0, OPTION_IGNORE_EDITLIST},
136 {nullptr, no_argument, nullptr, 0}
137 };
138
139
140 void output_hex(const uint8_t* data, size_t size)
141 {
142 std::cout << std::hex << std::setfill('0');
143
144 for (size_t i=0;i<size;i++) {
145 if (i%16==0) {
146 std::cout << std::setw(4) << i << " : ";
147 }
148
149 std::cout << std::setw(2) << (uint16_t)data[i];
150 if (i%16==7)
151 std::cout << " ";
152 else if (i%16==15)
153 std::cout << '\n';
154 else
155 std::cout << ' ';
156 }
157
158 if (size%16 != 15) {
159 std::cout << '\n';
160 }
161
162 std::cout << std::dec << std::setfill(' ');
163 }
164
165
166 class LibHeifInitializer {
167 public:
168 LibHeifInitializer() { heif_init(nullptr); }
169
170 ~LibHeifInitializer() { heif_deinit(); }
171 };
172
173
174 int main(int argc, char** argv)
175 {
176 // This takes care of initializing libheif and also deinitializing it at the end to free all resources.
177 LibHeifInitializer initializer;
178
179 const char* decoder_id = nullptr;
180 bool show_frame_number = false;
181
182 while (true) {
183 int option_index = 0;
184 int c = getopt_long(argc, argv, "hvd:", long_options, &option_index);
185 if (c == -1) {
186 break;
187 }
188
189 switch (c) {
190 case 'd':
191 decoder_id = optarg;
192 break;
193 case 'h':
194 show_help(argv[0]);
195 return 0;
196 case 'v':
197 heif_examples::show_version();
198 return 0;
199 case OPTION_SPEEDUP:
200 option_speedup = atof(optarg);
201 if (option_speedup <= 0) {
202 std::cerr << "Speedup must be positive.\n";
203 return 5;
204 }
205 break;
206 case OPTION_SHOW_SAI:
207 option_show_sai = true;
208 show_frame_number = true;
209 break;
210 case OPTION_SHOW_FRAME_DURATION:
211 option_show_frame_duration = true;
212 show_frame_number = true;
213 break;
214 case OPTION_SHOW_TRACK_METADATA:
215 option_show_track_metadata = true;
216 show_frame_number = true;
217 break;
218 case OPTION_SHOW_ALL:
219 option_show_sai = true;
220 option_show_frame_duration = true;
221 option_show_track_metadata = true;
222 show_frame_number = true;
223 break;
224 case OPTION_SHOW_METADATA_TEXT:
225 option_metadata_output = metadata_output_text;
226 break;
227 case OPTION_SHOW_METADATA_HEX:
228 option_metadata_output = metadata_output_hex;
229 break;
230 case OPTION_IGNORE_EDITLIST:
231 option_ignore_editlist = true;
232 break;
233 }
234 }
235
236 if (option_list_decoders) {
237 heif_examples::list_all_decoders();
238 return 0;
239 }
240
241 if (optind + 1 != argc) {
242 // Need exactly one input filename as additional argument.
243 show_help(argv[0]);
244 return 5;
245 }
246
247 std::string input_filename(argv[optind++]);
248
249
250 // --- check whether input is a supported HEIF file
251
252 if (int ret = heif_examples::check_for_valid_input_HEIF_file(input_filename)) {
253 return ret;
254 }
255
256 // --- open the HEIF file
257
258 struct heif_context* ctx = heif_context_alloc();
259 if (!ctx) {
260 fprintf(stderr, "Could not create context object\n");
261 return 1;
262 }
263
264 ContextReleaser cr(ctx);
265 struct heif_error err;
266 err = heif_context_read_from_file(ctx, input_filename.c_str(), nullptr);
267 if (err.code != 0) {
268 std::cerr << "Could not read HEIF/AVIF file: " << err.message << "\n";
269 return 1;
270 }
271
272
273 // --- error if file contains no image sequence
274
275 if (!heif_context_has_sequence(ctx)) {
276 std::cerr << "File contains no image sequence\n";
277 return 1;
278 }
279
280
281 // --- get visual track
282
283 struct heif_track* track = heif_context_get_track(ctx, 0);
284
285 uint16_t w, h;
286 heif_track_get_image_resolution(track, &w, &h);
287
288
289 // --- show track properties
290
291 if (option_show_track_metadata) {
292 const char* track_contentId = heif_track_get_gimi_track_content_id(track);
293 if (track_contentId) {
294 std::cout << "track content ID: " << track_contentId << "\n";
295 heif_string_release(track_contentId);
296 }
297
298 const heif_tai_clock_info* taic = heif_track_get_tai_clock_info_of_first_cluster(track);
299 if (taic) {
300 std::cout << "track taic: " << taic->time_uncertainty << " / " << taic->clock_resolution << " / "
301 << taic->clock_drift_rate << " / " << int(taic->clock_type) << "\n";
302 }
303 }
304
305
306 // --- find metadata track
307
308 heif_track* metadata_track = nullptr;
309 if (option_metadata_output != metadata_output_none) {
310 uint32_t metadata_track_id;
311 size_t nMetadataTracks = heif_track_find_referring_tracks(track, heif_track_reference_type_description, &metadata_track_id, 1);
312
313 if (nMetadataTracks == 1) {
314 metadata_track = heif_context_get_track(ctx, metadata_track_id);
315 }
316 }
317
318
319 // --- open output window
320
321 SDL_YUV_Display sdlWindow;
322 std::optional<std::string> sdlError = sdlWindow.init(w, h, SDL_YUV_Display::SDL_CHROMA_420, "heif-view");
323 if (sdlError) {
324 std::cerr << "Cannot open output window. " << *sdlError << "\n";
325 return 10;
326 }
327
328 std::unique_ptr<heif_decoding_options, void (*)(heif_decoding_options*)> decode_options(heif_decoding_options_alloc(), heif_decoding_options_free);
329 decode_options->convert_hdr_to_8bit = true;
330 decode_options->decoder_id = decoder_id;
331 decode_options->ignore_sequence_editlist = option_ignore_editlist;
332
333
334 // --- decoding loop
335
336 for (int frameNr = 1;; frameNr++) {
337 heif_image* out_image = nullptr;
338
339 // --- decode next sequence image
340
341 err = heif_track_decode_next_image(track, &out_image,
342 heif_colorspace_YCbCr, // TODO: find best format
343 heif_chroma_420,
344 decode_options.get());
345 if (err.code == heif_error_End_of_sequence) {
346 break;
347 }
348 else if (err.code) {
349 std::cerr << err.message << "\n";
350 return 1;
351 }
352
353 // --- wait for image presentation time
354
355 uint32_t duration = heif_image_get_duration(out_image);
356 uint64_t timescale = heif_track_get_timescale(track);
357 uint64_t duration_ms = duration * 1000 / timescale;
358
359 if (option_show_frame_duration) {
360 std::cout << "sample duration " << heif_image_get_duration(out_image) << " = " << duration_ms << " ms\n";
361 }
362
363 static const uint64_t start_time = SDL_GetTicks64();
364 static uint64_t next_frame_pts = 0;
365
366 uint64_t now_time = SDL_GetTicks64();
367 uint64_t elapsed_time = (now_time - start_time);
368 if (elapsed_time < next_frame_pts) {
369 SDL_Delay((Uint32)(next_frame_pts - elapsed_time));
370 }
371
372 next_frame_pts += static_cast<uint64_t>(static_cast<double>(duration_ms) / option_speedup);
373
374
375 // --- display image
376
377 size_t stride_Y, stride_Cb, stride_Cr;
378 const uint8_t* p_Y = heif_image_get_plane_readonly2(out_image, heif_channel_Y, &stride_Y);
379 const uint8_t* p_Cb = heif_image_get_plane_readonly2(out_image, heif_channel_Cb, &stride_Cb);
380 const uint8_t* p_Cr = heif_image_get_plane_readonly2(out_image, heif_channel_Cr, &stride_Cr);
381
382 sdlWindow.display(p_Y, p_Cb, p_Cr, static_cast<int>(stride_Y), static_cast<int>(stride_Cb));
383
384 if (show_frame_number) {
385 std::cout << "--- frame " << frameNr << "\n";
386 }
387
388 if (option_show_sai) {
389 const char* contentID = heif_image_get_gimi_sample_content_id(out_image);
390 if (contentID) {
391 std::cout << "GIMI content id: " << contentID << "\n";
392 heif_string_release(contentID);
393 }
394
395 heif_tai_timestamp_packet* timestamp;
396 err = heif_image_get_tai_timestamp(out_image, &timestamp);
397 if (err.code) {
398 std::cerr << err.message << "\n";
399 return 10;
400 }
401 else if (timestamp) {
402 std::cout << "TAI timestamp: " << timestamp->tai_timestamp << "\n";
403 heif_tai_timestamp_packet_release(timestamp);
404 }
405 }
406
407 // --- get metadata sample
408
409 static heif_raw_sequence_sample* metadata_sample = nullptr;
410 static uint64_t metadata_sample_display_time = 0;
411
412 if (metadata_track && metadata_sample == nullptr) {
413 err = heif_track_get_next_raw_sequence_sample(metadata_track, &metadata_sample);
414 if (err.code != heif_error_Ok && err.code != heif_error_End_of_sequence) {
415 std::cerr << err.message << "\n";
416 return 10;
417 }
418 }
419
420 // --- show metadata
421
422 while (metadata_sample && static_cast<uint64_t>((double)metadata_sample_display_time / option_speedup) <= elapsed_time) {
423 size_t size;
424 const uint8_t* data = heif_raw_sequence_sample_get_data(metadata_sample, &size);
425
426 std::cout << "timestamp: " << ((double)metadata_sample_display_time)/1000.0f << "sec\n";
427
428 if (option_metadata_output == metadata_output_text) {
429 std::cout << ((const char*) data) << "\n";
430 }
431 else if (option_metadata_output == metadata_output_hex) {
432 output_hex(data, size);
433 std::cout << '\n';
434 }
435
436 uint64_t metadata_timescale = heif_track_get_timescale(metadata_track);
437 uint32_t metadata_duration = heif_raw_sequence_sample_get_duration(metadata_sample);
438 metadata_sample_display_time += metadata_duration * 1000 / metadata_timescale;
439
440 heif_raw_sequence_sample_release(metadata_sample);
441 metadata_sample = nullptr;
442
443 // get next metadata sample
444
445 err = heif_track_get_next_raw_sequence_sample(metadata_track, &metadata_sample);
446 if (err.code != heif_error_Ok && err.code != heif_error_End_of_sequence) {
447 std::cerr << err.message << "\n";
448 return 10;
449 }
450 }
451
452 heif_image_release(out_image);
453
454 if (sdlWindow.doQuit()) {
455 break;
456 }
457 }
458
459 sdlWindow.close();
460
461 heif_track_release(track);
462 heif_track_release(metadata_track);
463
464 return 0;
465 }