Debian Codegraph @

tests/tai.cc

raw
1 /*
2 libheif unit tests
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 "catch_amalgamated.hpp"
28 #include "test_utils.h"
29 #include <libheif/heif.h>
30 #include <libheif/heif_tai_timestamps.h>
31
32 TEST_CASE( "image-tai" )
33 {
34 heif_error err{};
35
36 err = heif_init(nullptr);
37 REQUIRE(err.code == heif_error_Ok);
38
39 std::string filename = get_tests_output_file_path("tai-1.heic");
40
41 heif_image* img = createImage_RGB_planar();
42 heif_encoder* enc = get_encoder_or_skip_test(heif_compression_HEVC);
43 heif_context* ctx = heif_context_alloc();
44
45 heif_image_handle* handle;
46 err = heif_context_encode_image(ctx, img, enc, nullptr, &handle);
47 REQUIRE(err.code == heif_error_Ok);
48 heif_item_id itemId = heif_image_handle_get_item_id(handle);
49
50 // add TAI clock info
51
52 heif_tai_clock_info* clock_info = heif_tai_clock_info_alloc();
53 clock_info->clock_resolution = 1000;
54 clock_info->clock_drift_rate = 123;
55 clock_info->clock_type = heif_tai_clock_info_clock_type_synchronized_to_atomic_source;
56 clock_info->time_uncertainty = 999;
57
58 err = heif_item_set_property_tai_clock_info(ctx, itemId, clock_info, nullptr);
59 REQUIRE(err.code == heif_error_Ok);
60
61 // check that adding a second timestamp leads to an error
62 err = heif_item_set_property_tai_clock_info(ctx, itemId, clock_info, nullptr);
63 REQUIRE(err.code != heif_error_Ok);
64
65 heif_tai_clock_info_release(clock_info);
66
67 // add TAI timestamp
68
69 heif_tai_timestamp_packet* timestamp = heif_tai_timestamp_packet_alloc();
70 timestamp->tai_timestamp = 1234567890;
71 timestamp->synchronization_state = 1;
72 timestamp->timestamp_generation_failure = 0;
73 timestamp->timestamp_is_modified = 0;
74
75 err = heif_item_set_property_tai_timestamp(ctx, itemId, timestamp, nullptr);
76 REQUIRE(err.code == heif_error_Ok);
77
78 // check that adding a second timestamp leads to an error
79 err = heif_item_set_property_tai_timestamp(ctx, itemId, timestamp, nullptr);
80 REQUIRE(err.code != heif_error_Ok);
81
82 heif_tai_timestamp_packet_release(timestamp);
83
84 err = heif_context_write_to_file(ctx, filename.c_str());
85 REQUIRE(err.code == heif_error_Ok);
86
87 heif_image_handle_release(handle);
88 heif_context_free(ctx);
89 heif_image_release(img);
90
91
92 // --- read file
93
94 ctx = heif_context_alloc();
95 err = heif_context_read_from_file(ctx, filename.c_str(), nullptr);
96 REQUIRE(err.code == heif_error_Ok);
97
98 err = heif_context_get_primary_image_handle(ctx, &handle);
99 REQUIRE(err.code == heif_error_Ok);
100
101 itemId = heif_image_handle_get_item_id(handle);
102
103 clock_info = nullptr; // make sure that we are not accidentally using old data
104 err = heif_item_get_property_tai_clock_info(ctx, itemId, &clock_info);
105 REQUIRE(err.code == heif_error_Ok);
106 REQUIRE(clock_info != nullptr);
107
108 timestamp = nullptr; // make sure that we are not accidentally using old data
109 err = heif_item_get_property_tai_timestamp(ctx, itemId, &timestamp);
110 REQUIRE(err.code == heif_error_Ok);
111 REQUIRE(timestamp != nullptr);
112
113 REQUIRE(clock_info->clock_resolution == 1000);
114 REQUIRE(clock_info->clock_drift_rate == 123);
115 REQUIRE(clock_info->clock_type == heif_tai_clock_info_clock_type_synchronized_to_atomic_source);
116 REQUIRE(clock_info->time_uncertainty == 999);
117 heif_tai_clock_info_release(clock_info);
118
119 REQUIRE(timestamp->tai_timestamp == 1234567890);
120 REQUIRE(timestamp->synchronization_state == 1);
121 REQUIRE(timestamp->timestamp_generation_failure == 0);
122 REQUIRE(timestamp->timestamp_is_modified == 0);
123 heif_tai_timestamp_packet_release(timestamp);
124
125 // check whether we can get the timestamp also from the decoded image
126
127 err = heif_decode_image(handle, &img, heif_colorspace_undefined, heif_chroma_undefined, nullptr);
128 REQUIRE(err.code == heif_error_Ok);
129
130 timestamp = nullptr; // make sure that we are not accidentally using old data
131 err = heif_image_get_tai_timestamp(img, &timestamp);
132 REQUIRE(err.code == heif_error_Ok);
133 REQUIRE(timestamp != nullptr);
134
135 REQUIRE(timestamp->tai_timestamp == 1234567890);
136 REQUIRE(timestamp->synchronization_state == 1);
137 REQUIRE(timestamp->timestamp_generation_failure == 0);
138 REQUIRE(timestamp->timestamp_is_modified == 0);
139 heif_tai_timestamp_packet_release(timestamp);
140 }