/src/libheif/libheif/brands.cc
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2025 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 | | #include "brands.h" |
22 | | #include "file.h" |
23 | | #include "sequences/track_visual.h" |
24 | | #include <utility> |
25 | | |
26 | | |
27 | | static bool check_mif1(const HeifContext* ctx) |
28 | 0 | { |
29 | 0 | auto file = ctx->get_heif_file(); |
30 | |
|
31 | 0 | auto meta = file->get_meta_box(); |
32 | 0 | if (!meta || meta->get_version() != 0) { |
33 | 0 | return false; |
34 | 0 | } |
35 | | |
36 | 0 | auto hdlr = meta->get_child_box<Box_hdlr>(); |
37 | 0 | if (!hdlr || hdlr->get_version() != 0) { |
38 | 0 | return false; |
39 | 0 | } |
40 | | |
41 | 0 | auto iloc = meta->get_child_box<Box_iloc>(); |
42 | 0 | if (!iloc || iloc->get_version() > 2) { |
43 | 0 | return false; |
44 | 0 | } |
45 | | |
46 | 0 | auto iinf = meta->get_child_box<Box_iinf>(); |
47 | 0 | if (!iinf || iinf->get_version() > 1) { |
48 | 0 | return false; |
49 | 0 | } |
50 | | |
51 | 0 | auto infe = iinf->get_child_box<Box_infe>(); |
52 | 0 | if (!infe || infe->get_version() < 2 || infe->get_version() > 3) { |
53 | 0 | return false; |
54 | 0 | } |
55 | | |
56 | 0 | auto pitm = meta->get_child_box<Box_pitm>(); |
57 | 0 | if (!pitm || pitm->get_version() > 1) { |
58 | 0 | return false; |
59 | 0 | } |
60 | | |
61 | 0 | auto iprp = meta->get_child_box<Box_iprp>(); |
62 | 0 | if (!iprp) { |
63 | 0 | return false; |
64 | 0 | } |
65 | | |
66 | 0 | return true; |
67 | 0 | } |
68 | | |
69 | | |
70 | | std::vector<std::shared_ptr<const ImageItem>> get_primary_and_alternative_images(const HeifContext* ctx) |
71 | 0 | { |
72 | 0 | auto img = ctx->get_primary_image(false); |
73 | 0 | if (img) { |
74 | 0 | return {std::move(img)}; |
75 | 0 | } |
76 | 0 | else { |
77 | 0 | return {}; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | |
82 | | std::vector<heif_brand2> compute_compatible_brands(const HeifContext* ctx, heif_brand2* out_main_brand) |
83 | 0 | { |
84 | 0 | std::vector<heif_brand2> compatible_brands; |
85 | |
|
86 | 0 | heif_brand2 dummy; |
87 | 0 | if (out_main_brand == nullptr) { |
88 | 0 | out_main_brand = &dummy; // so that we do not have to check for NULL out_main_brand in the following |
89 | 0 | } |
90 | |
|
91 | 0 | *out_main_brand = 0; |
92 | | |
93 | | // --- "mif" brands |
94 | |
|
95 | 0 | bool is_mif1 = check_mif1(ctx); |
96 | |
|
97 | 0 | if (is_mif1) { |
98 | 0 | compatible_brands.push_back(heif_brand2_mif1); |
99 | 0 | *out_main_brand = heif_brand2_mif1; |
100 | 0 | } |
101 | |
|
102 | 0 | bool is_structural_image = is_mif1; |
103 | | |
104 | | |
105 | | // --- image brand |
106 | |
|
107 | 0 | std::vector<std::shared_ptr<const ImageItem>> images = get_primary_and_alternative_images(ctx); |
108 | |
|
109 | 0 | bool miaf_compatible = true; |
110 | |
|
111 | 0 | for (auto& img : images) { |
112 | 0 | heif_brand2 brand = img->get_compatible_brand(); |
113 | 0 | if (brand != 0 && is_structural_image) { |
114 | 0 | compatible_brands.push_back(brand); |
115 | 0 | } |
116 | |
|
117 | 0 | if (!img->is_miaf_compatible()) { |
118 | 0 | miaf_compatible = false; |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | // --- "miaf" |
123 | |
|
124 | 0 | if (miaf_compatible && is_structural_image && !images.empty()) { |
125 | 0 | compatible_brands.push_back(heif_brand2_miaf); |
126 | 0 | } |
127 | | |
128 | | |
129 | | // --- main brand is first image brand |
130 | |
|
131 | 0 | if (!images.empty()) { |
132 | 0 | heif_brand2 brand = images[0]->get_compatible_brand(); |
133 | 0 | if (brand != 0) { |
134 | 0 | *out_main_brand = brand; |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | | // --- --- sequences |
139 | |
|
140 | 0 | if (ctx->has_sequence()) { |
141 | 0 | compatible_brands.push_back(heif_brand2_msf1); |
142 | 0 | compatible_brands.push_back(heif_brand2_isom); |
143 | |
|
144 | 0 | auto track_result = ctx->get_track(0); |
145 | 0 | assert(track_result); |
146 | |
|
147 | 0 | std::shared_ptr<const Track> track = *track_result; |
148 | 0 | std::shared_ptr<const Track_Visual> visual_track = std::dynamic_pointer_cast<const Track_Visual>(track); |
149 | |
|
150 | 0 | if (visual_track) { |
151 | 0 | heif_brand2 track_brand = visual_track->get_compatible_brand(); |
152 | 0 | if (track_brand != 0) { |
153 | 0 | compatible_brands.push_back(track_brand); |
154 | | |
155 | | // overwrite any image brand |
156 | 0 | *out_main_brand = track_brand; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | // if we don't have a track brand, use at least the sequence structural brand |
161 | 0 | if (*out_main_brand == 0) { |
162 | 0 | *out_main_brand = heif_brand2_msf1; |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | // --- "unif" brand |
167 | |
|
168 | 0 | if (ctx->get_unif()) { |
169 | 0 | compatible_brands.push_back(heif_brand2_unif); |
170 | 0 | } |
171 | | |
172 | | // --- remove duplicate brands |
173 | |
|
174 | 0 | std::vector<heif_brand2> unique_brands; |
175 | 0 | for (auto brand : compatible_brands) { |
176 | 0 | if (std::find(unique_brands.begin(), unique_brands.end(), brand) == unique_brands.end()) { |
177 | 0 | unique_brands.push_back(brand); |
178 | 0 | } |
179 | 0 | } |
180 | |
|
181 | 0 | return unique_brands; |
182 | 0 | } |
183 | | |