| 1 |
/* |
| 2 |
* HEIF codec. |
| 3 |
* Copyright (c) 2025 Dirk Farin <[email protected]> |
| 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 "SAI_datafile.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <fstream> |
| 25 |
#include <iostream> |
| 26 |
#include <sstream> |
| 27 |
|
| 28 |
|
| 29 |
SAI_datafile::~SAI_datafile() |
| 30 |
{ |
| 31 |
heif_tai_clock_info_release(tai_clock_info); |
| 32 |
|
| 33 |
for (auto tai : tai_timestamps) { |
| 34 |
heif_tai_timestamp_packet_release(tai); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
void SAI_datafile::handleHeaderEntry(const std::string& code, |
| 40 |
const std::vector<std::string>& values) |
| 41 |
{ |
| 42 |
if (code == "suid") { |
| 43 |
active_sais.push_back(code); |
| 44 |
if (!values.empty()) { |
| 45 |
std::cerr << "Invalid 'suid' header line. May not have additional parameters.\n"; |
| 46 |
exit(5); |
| 47 |
} |
| 48 |
} |
| 49 |
else if (code == "stai") { |
| 50 |
active_sais.push_back(code); |
| 51 |
if (values.size() > 4) { |
| 52 |
std::cerr << "Invalid 'stai' header line. May not have more than 4 parameters.\n"; |
| 53 |
exit(5); |
| 54 |
} |
| 55 |
|
| 56 |
tai_clock_info = heif_tai_clock_info_alloc(); |
| 57 |
|
| 58 |
for (size_t i=0;i<values.size();i++) { |
| 59 |
int64_t val = std::stoll(values[i]); |
| 60 |
if (i==1 && (val < 0 || val > 0xffffffff)) { |
| 61 |
std::cerr << "Invalid SAI tai clock info entry in header\n"; |
| 62 |
exit(5); |
| 63 |
} |
| 64 |
|
| 65 |
if (i==2 && (val < 0 || val > 0x7fffffff)) { |
| 66 |
std::cerr << "Invalid SAI tai clock info entry in header\n"; |
| 67 |
exit(5); |
| 68 |
} |
| 69 |
|
| 70 |
if (i==3 && (val < 0 || val > 0xff)) { |
| 71 |
std::cerr << "Invalid SAI tai clock info entry in header\n"; |
| 72 |
exit(5); |
| 73 |
} |
| 74 |
|
| 75 |
switch (i) { |
| 76 |
case 0: |
| 77 |
tai_clock_info->time_uncertainty = val; |
| 78 |
break; |
| 79 |
case 1: |
| 80 |
tai_clock_info->clock_resolution = static_cast<uint32_t>(val); |
| 81 |
break; |
| 82 |
case 2: |
| 83 |
tai_clock_info->clock_drift_rate = static_cast<int32_t>(val); |
| 84 |
break; |
| 85 |
case 3: |
| 86 |
tai_clock_info->clock_type = static_cast<uint8_t>(val); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
else { |
| 92 |
std::cerr << "Unknown code in SAI data file header: " << code << "\n"; |
| 93 |
exit(5); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void SAI_datafile::handleMainEntry(const std::vector<std::string>& values, int line, int main_item_line) |
| 98 |
{ |
| 99 |
if (active_sais.empty()) { |
| 100 |
std::cerr << "Invalid SAI data file: data received, but no SAIs defined."; |
| 101 |
exit(5); |
| 102 |
} |
| 103 |
|
| 104 |
size_t idx = main_item_line % active_sais.size(); |
| 105 |
if (active_sais[idx] == "suid") { |
| 106 |
if (values.size() > 1) { |
| 107 |
std::cerr << "Invalid SAI content-id entry in line " << line << "\n"; |
| 108 |
exit(5); |
| 109 |
} |
| 110 |
|
| 111 |
if (values.empty()) { |
| 112 |
gimi_content_ids.push_back({}); |
| 113 |
} |
| 114 |
else { |
| 115 |
gimi_content_ids.push_back(values[0]); |
| 116 |
} |
| 117 |
} |
| 118 |
else if (active_sais[idx] == "stai") { |
| 119 |
if (values.size() > 4) { |
| 120 |
std::cerr << "Invalid SAI timestamp entry in line " << line << "\n"; |
| 121 |
exit(5); |
| 122 |
} |
| 123 |
|
| 124 |
if (values.empty()) { |
| 125 |
tai_timestamps.push_back(nullptr); |
| 126 |
} |
| 127 |
else { |
| 128 |
heif_tai_timestamp_packet* tai = heif_tai_timestamp_packet_alloc(); |
| 129 |
for (size_t i=0;i<values.size();i++) { |
| 130 |
int64_t val = std::stoll(values[i]); |
| 131 |
if (i>=1 && i<=3 && (val < 0 || val > 1)) { |
| 132 |
std::cerr << "Invalid SAI timestamp entry in line " << line << "\n"; |
| 133 |
exit(5); |
| 134 |
} |
| 135 |
|
| 136 |
switch (i) { |
| 137 |
case 0: |
| 138 |
tai->tai_timestamp = val; |
| 139 |
break; |
| 140 |
case 1: |
| 141 |
tai->synchronization_state = static_cast<uint8_t>(val); |
| 142 |
break; |
| 143 |
case 2: |
| 144 |
tai->timestamp_generation_failure = static_cast<uint8_t>(val); |
| 145 |
break; |
| 146 |
case 3: |
| 147 |
tai->timestamp_is_modified = static_cast<uint8_t>(val); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
tai_timestamps.push_back(tai); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
bool SAI_datafile::isSeparatorLine(const std::string& line) |
| 158 |
{ |
| 159 |
return line.starts_with("---"); |
| 160 |
} |
| 161 |
|
| 162 |
std::vector<std::string> SAI_datafile::splitCSV(const std::string& line) |
| 163 |
{ |
| 164 |
std::vector<std::string> parts; |
| 165 |
std::stringstream ss(line); |
| 166 |
std::string item; |
| 167 |
|
| 168 |
while (std::getline(ss, item, ',')) { |
| 169 |
// Trim whitespace |
| 170 |
item.erase(item.begin(), |
| 171 |
std::find_if(item.begin(), item.end(), [](unsigned char ch){ return !std::isspace(ch); })); |
| 172 |
item.erase( |
| 173 |
std::find_if(item.rbegin(), item.rend(), [](unsigned char ch){ return !std::isspace(ch); }).base(), |
| 174 |
item.end() |
| 175 |
); |
| 176 |
parts.push_back(item); |
| 177 |
} |
| 178 |
|
| 179 |
return parts; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
void SAI_datafile::load_sai_data_from_file(const char* sai_file) |
| 184 |
{ |
| 185 |
std::ifstream istr(sai_file); |
| 186 |
if (!istr) { |
| 187 |
std::cerr << "Could not open SAI data file\n"; |
| 188 |
exit(5); |
| 189 |
} |
| 190 |
|
| 191 |
// --- read header |
| 192 |
|
| 193 |
std::string line; |
| 194 |
bool inHeader = true; |
| 195 |
int line_counter = 0; |
| 196 |
int main_item_line = 0; |
| 197 |
|
| 198 |
while (std::getline(istr, line)) { |
| 199 |
line_counter++; |
| 200 |
|
| 201 |
if (inHeader && line.empty()) |
| 202 |
continue; |
| 203 |
|
| 204 |
if (inHeader && isSeparatorLine(line)) { |
| 205 |
// Switch to main part |
| 206 |
inHeader = false; |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
if (inHeader) { |
| 211 |
// Header line: starts with 4-character code, optional CSV list |
| 212 |
if (line.size() < 4) { |
| 213 |
std::cerr << "Invalid header line: " << line << "\n"; |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
std::string code = line.substr(0, 4); |
| 218 |
|
| 219 |
std::vector<std::string> values; |
| 220 |
if (line.size() > 4) { |
| 221 |
// Skip the space after the code, if present |
| 222 |
std::string rest = line.substr(4); |
| 223 |
if (!rest.empty() && (rest[0] == ' ' || rest[0] == '\t')) |
| 224 |
rest.erase(0, 1); |
| 225 |
|
| 226 |
if (!rest.empty()) |
| 227 |
values = splitCSV(rest); |
| 228 |
} |
| 229 |
|
| 230 |
handleHeaderEntry(code, values); |
| 231 |
} |
| 232 |
else { |
| 233 |
// Main section: entire line is CSV |
| 234 |
auto values = splitCSV(line); |
| 235 |
handleMainEntry(values, line_counter, main_item_line++); |
| 236 |
} |
| 237 |
} |
| 238 |
} |