Debian Codegraph @

tests/mini_box.cc

raw
1 /*
2 libheif AVC (H.264) unit tests
3
4 MIT License
5
6 Copyright (c) 2023 Brad Hards <[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 <bitstream.h>
29 #include <box.h>
30 #include "error.h"
31 #include <logging.h>
32 #include <cstdint>
33 #include <fstream>
34 #include <iostream>
35 #include <memory>
36 #include <mini.h>
37 #include "test_utils.h"
38 #include "test-config.h"
39 #include <file_layout.h>
40
41 TEST_CASE("mini")
42 {
43 std::vector<uint8_t> byteArray{
44 0x00, 0x00, 0x00, 0x10, 0x66, 0x74, 0x79, 0x70,
45 0x6d, 0x69, 0x66, 0x33, 0x61, 0x76, 0x69, 0x66,
46 0x00, 0x00, 0x00, 0x4a, 0x6d, 0x69, 0x6e, 0x69,
47 0x08, 0x18, 0x00, 0xff, 0x01, 0xfe, 0xe0, 0x03,
48 0x40, 0x81, 0x20, 0x00, 0x00, 0x12, 0x00, 0x0a,
49 0x09, 0x38, 0x1d, 0xff, 0xff, 0xd8, 0x40, 0x43,
50 0x41, 0xa4, 0x32, 0x26, 0x11, 0x90, 0x01, 0x86,
51 0x18, 0x61, 0x00, 0xb4, 0x83, 0x5a, 0x70, 0x50,
52 0x8b, 0xe5, 0x7d, 0xf5, 0xc7, 0xd3, 0x6e, 0x92,
53 0xea, 0x80, 0x01, 0x50, 0x91, 0xc4, 0x06, 0xa3,
54 0xe1, 0xca, 0x44, 0x43, 0xe7, 0xb8, 0x67, 0x43,
55 0xea, 0x80};
56
57 auto reader = std::make_shared<StreamReader_memory>(byteArray.data(),
58 byteArray.size(), false);
59
60 BitstreamRange range(reader, byteArray.size());
61 std::shared_ptr<Box> box;
62 Error error = Box::read(range, &box, heif_get_global_security_limits());
63 REQUIRE(error == Error::Ok);
64 REQUIRE(range.error() == 0);
65
66 REQUIRE(box->get_short_type() == fourcc("ftyp"));
67 REQUIRE(box->get_type_string() == "ftyp");
68 std::shared_ptr<Box_ftyp> ftyp = std::dynamic_pointer_cast<Box_ftyp>(box);
69 REQUIRE(ftyp->get_major_brand() == fourcc("mif3"));
70 REQUIRE(ftyp->get_minor_version() == fourcc("avif"));
71 REQUIRE(ftyp->list_brands().size() == 0);
72 Indent indent;
73 std::string dumpResult = box->dump(indent);
74 REQUIRE(dumpResult == "Box: ftyp ----- (File Type)\n"
75 "size: 16 (header size: 8)\n"
76 "major brand: mif3\n"
77 "minor version: avif\n"
78 "compatible brands: \n");
79
80 error = Box::read(range, &box, heif_get_global_security_limits());
81 REQUIRE(error == Error::Ok);
82 REQUIRE(range.error() == 0);
83
84 REQUIRE(box->get_short_type() == fourcc("mini"));
85 REQUIRE(box->get_type_string() == "mini");
86 std::shared_ptr<Box_mini> mini = std::dynamic_pointer_cast<Box_mini>(box);
87 REQUIRE(mini->get_exif_flag() == false);
88 REQUIRE(mini->get_xmp_flag() == false);
89 REQUIRE(mini->get_bit_depth() == 8);
90 REQUIRE(mini->get_colour_primaries() == 1);
91 REQUIRE(mini->get_transfer_characteristics() == 13);
92 REQUIRE(mini->get_matrix_coefficients() == 6);
93 REQUIRE(mini->get_width() == 256);
94 REQUIRE(mini->get_height() == 256);
95 REQUIRE(mini->get_main_item_codec_config().size() == 4);
96 REQUIRE((int)(mini->get_main_item_codec_config().data()[0]) == 0x81);
97 REQUIRE((int)(mini->get_main_item_codec_config().data()[1]) == 0x20);
98 REQUIRE((int)(mini->get_main_item_codec_config().data()[2]) == 0x00);
99 REQUIRE((int)(mini->get_main_item_codec_config().data()[3]) == 0x00);
100 dumpResult = box->dump(indent);
101 REQUIRE(dumpResult == "Box: mini -----\n"
102 "size: 74 (header size: 8)\n"
103 "version: 0\n"
104 "explicit_codec_types_flag: 0\n"
105 "float_flag: 0\n"
106 "full_range_flag: 1\n"
107 "alpha_flag: 0\n"
108 "explicit_cicp_flag: 0\n"
109 "hdr_flag: 0\n"
110 "icc_flag: 0\n"
111 "exif_flag: 0\n"
112 "xmp_flag: 0\n"
113 "chroma_subsampling: 3\n"
114 "orientation: 1\n"
115 "width: 256\n"
116 "height: 256\n"
117 "bit_depth: 8\n"
118 "colour_primaries: 1\n"
119 "transfer_characteristics: 13\n"
120 "matrix_coefficients: 6\n"
121 "main_item_code_config size: 4\n"
122 "main_item_data offset: 37, size: 53\n");
123 }
124
125 TEST_CASE("check mini+alpha version")
126 {
127 auto istr = std::unique_ptr<std::istream>(new std::ifstream(tests_data_directory + "/simple_osm_tile_alpha.avif", std::ios::binary));
128 auto reader = std::make_shared<StreamReader_istream>(std::move(istr));
129 FileLayout file;
130 Error err = file.read(reader, heif_get_global_security_limits());
131 REQUIRE(err.error_code == heif_error_Ok);
132
133 std::shared_ptr<Box_mini> mini = file.get_mini_box();
134 REQUIRE(mini->get_exif_flag() == false);
135 REQUIRE(mini->get_xmp_flag() == false);
136 REQUIRE(mini->get_bit_depth() == 8);
137 REQUIRE(mini->get_colour_primaries() == 2);
138 REQUIRE(mini->get_transfer_characteristics() == 2);
139 REQUIRE(mini->get_matrix_coefficients() == 6);
140 REQUIRE(mini->get_width() == 256);
141 REQUIRE(mini->get_height() == 256);
142 REQUIRE(mini->get_main_item_codec_config().size() == 4);
143 REQUIRE((int)(mini->get_main_item_codec_config().data()[0]) == 0x81);
144 REQUIRE((int)(mini->get_main_item_codec_config().data()[1]) == 0x20);
145 REQUIRE((int)(mini->get_main_item_codec_config().data()[2]) == 0x00);
146 REQUIRE((int)(mini->get_main_item_codec_config().data()[3]) == 0x00);
147 Indent indent;
148 std::string dumpResult = mini->dump(indent);
149 REQUIRE(dumpResult == "Box: mini -----\n"
150 "size: 788 (header size: 8)\n"
151 "version: 0\n"
152 "explicit_codec_types_flag: 0\n"
153 "float_flag: 0\n"
154 "full_range_flag: 1\n"
155 "alpha_flag: 1\n"
156 "explicit_cicp_flag: 0\n"
157 "hdr_flag: 0\n"
158 "icc_flag: 1\n"
159 "exif_flag: 0\n"
160 "xmp_flag: 0\n"
161 "chroma_subsampling: 3\n"
162 "orientation: 1\n"
163 "width: 256\n"
164 "height: 256\n"
165 "bit_depth: 8\n"
166 "alpha_is_premultiplied: 0\n"
167 "colour_primaries: 2\n"
168 "transfer_characteristics: 2\n"
169 "matrix_coefficients: 6\n"
170 "alpha_item_code_config size: 4\n"
171 "main_item_code_config size: 4\n"
172 "icc_data size: 672\n"
173 "alpha_item_data offset: 717, size: 34\n"
174 "main_item_data offset: 751, size: 53\n");
175 }
176
177 TEST_CASE("check mini+exif+xmp version")
178 {
179 auto istr = std::unique_ptr<std::istream>(new std::ifstream(tests_data_directory + "/simple_osm_tile_meta.avif", std::ios::binary));
180 auto reader = std::make_shared<StreamReader_istream>(std::move(istr));
181 FileLayout file;
182 Error err = file.read(reader, heif_get_global_security_limits());
183 REQUIRE(err.error_code == heif_error_Ok);
184
185 std::shared_ptr<Box_mini> mini = file.get_mini_box();
186 REQUIRE(mini->get_exif_flag() == true);
187 REQUIRE(mini->get_xmp_flag() == true);
188 REQUIRE(mini->get_bit_depth() == 8);
189 REQUIRE(mini->get_colour_primaries() == 2);
190 REQUIRE(mini->get_transfer_characteristics() == 2);
191 REQUIRE(mini->get_matrix_coefficients() == 6);
192 REQUIRE(mini->get_width() == 256);
193 REQUIRE(mini->get_height() == 256);
194 REQUIRE(mini->get_main_item_codec_config().size() == 4);
195 REQUIRE((int)(mini->get_main_item_codec_config().data()[0]) == 0x81);
196 REQUIRE((int)(mini->get_main_item_codec_config().data()[1]) == 0x20);
197 REQUIRE((int)(mini->get_main_item_codec_config().data()[2]) == 0x00);
198 REQUIRE((int)(mini->get_main_item_codec_config().data()[3]) == 0x00);
199 Indent indent;
200 std::string dumpResult = mini->dump(indent);
201 REQUIRE(dumpResult == "Box: mini -----\n"
202 "size: 4388 (header size: 8)\n"
203 "version: 0\n"
204 "explicit_codec_types_flag: 0\n"
205 "float_flag: 0\n"
206 "full_range_flag: 1\n"
207 "alpha_flag: 0\n"
208 "explicit_cicp_flag: 0\n"
209 "hdr_flag: 0\n"
210 "icc_flag: 1\n"
211 "exif_flag: 1\n"
212 "xmp_flag: 1\n"
213 "chroma_subsampling: 3\n"
214 "orientation: 1\n"
215 "width: 256\n"
216 "height: 256\n"
217 "bit_depth: 8\n"
218 "colour_primaries: 2\n"
219 "transfer_characteristics: 2\n"
220 "matrix_coefficients: 6\n"
221 "main_item_code_config size: 4\n"
222 "icc_data size: 672\n"
223 "main_item_data offset: 717, size: 53\n"
224 "exif_data offset: 770, size: 208\n"
225 "xmp_data offset: 978, size: 3426\n");
226 }
227
228
229 TEST_CASE("check heif mini")
230 {
231 auto istr = std::unique_ptr<std::istream>(new std::ifstream(tests_data_directory + "/lightning_mini.heif", std::ios::binary));
232 auto reader = std::make_shared<StreamReader_istream>(std::move(istr));
233 FileLayout file;
234 Error err = file.read(reader, heif_get_global_security_limits());
235 REQUIRE(err.error_code == heif_error_Ok);
236
237 std::shared_ptr<Box_mini> mini = file.get_mini_box();
238 REQUIRE(mini->get_exif_flag() == false);
239 REQUIRE(mini->get_xmp_flag() == false);
240 REQUIRE(mini->get_bit_depth() == 8);
241 REQUIRE(mini->get_colour_primaries() == 1);
242 REQUIRE(mini->get_transfer_characteristics() == 13);
243 REQUIRE(mini->get_matrix_coefficients() == 6);
244 REQUIRE(mini->get_width() == 128);
245 REQUIRE(mini->get_height() == 128);
246 REQUIRE(mini->get_main_item_codec_config().size() == 112);
247 Indent indent;
248 std::string dumpResult = mini->dump(indent);
249 REQUIRE(dumpResult == "Box: mini -----\n"
250 "size: 4710 (header size: 8)\n"
251 "version: 0\n"
252 "explicit_codec_types_flag: 0\n"
253 "float_flag: 0\n"
254 "full_range_flag: 1\n"
255 "alpha_flag: 0\n"
256 "explicit_cicp_flag: 0\n"
257 "hdr_flag: 0\n"
258 "icc_flag: 0\n"
259 "exif_flag: 0\n"
260 "xmp_flag: 0\n"
261 "chroma_subsampling: 1\n"
262 "orientation: 1\n"
263 "width: 128\n"
264 "height: 128\n"
265 "chroma_is_horizontally_centered: 0\n"
266 "chroma_is_vertically_centered: 0\n"
267 "bit_depth: 8\n"
268 "colour_primaries: 1\n"
269 "transfer_characteristics: 13\n"
270 "matrix_coefficients: 6\n"
271 "main_item_code_config size: 112\n"
272 "main_item_data offset: 144, size: 4582\n");
273 }
274