File: SAI_datafile.cc

package info (click to toggle)
libheif 1.21.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,872 kB
  • sloc: cpp: 91,129; python: 3,032; sh: 1,048; ansic: 453; javascript: 160; makefile: 76
file content (238 lines) | stat: -rw-r--r-- 6,306 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * HEIF codec.
 * Copyright (c) 2025 Dirk Farin <dirk.farin@gmail.com>
 *
 * This file is part of libheif.
 *
 * libheif is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * libheif is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "SAI_datafile.h"

#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>


SAI_datafile::~SAI_datafile()
{
  heif_tai_clock_info_release(tai_clock_info);

  for (auto tai : tai_timestamps) {
    heif_tai_timestamp_packet_release(tai);
  }
}


void SAI_datafile::handleHeaderEntry(const std::string& code,
                                const std::vector<std::string>& values)
{
  if (code == "suid") {
    active_sais.push_back(code);
    if (!values.empty()) {
      std::cerr << "Invalid 'suid' header line. May not have additional parameters.\n";
      exit(5);
    }
  }
  else if (code == "stai") {
    active_sais.push_back(code);
    if (values.size() > 4) {
      std::cerr << "Invalid 'stai' header line. May not have more than 4 parameters.\n";
      exit(5);
    }

    tai_clock_info = heif_tai_clock_info_alloc();

    for (size_t i=0;i<values.size();i++) {
      int64_t val = std::stoll(values[i]);
      if (i==1 && (val < 0 || val > 0xffffffff)) {
        std::cerr << "Invalid SAI tai clock info entry in header\n";
        exit(5);
      }

      if (i==2 && (val < 0 || val > 0x7fffffff)) {
        std::cerr << "Invalid SAI tai clock info entry in header\n";
        exit(5);
      }

      if (i==3 && (val < 0 || val > 0xff)) {
        std::cerr << "Invalid SAI tai clock info entry in header\n";
        exit(5);
      }

      switch (i) {
        case 0:
          tai_clock_info->time_uncertainty = val;
          break;
        case 1:
          tai_clock_info->clock_resolution = static_cast<uint32_t>(val);
          break;
        case 2:
          tai_clock_info->clock_drift_rate = static_cast<int32_t>(val);
          break;
        case 3:
          tai_clock_info->clock_type = static_cast<uint8_t>(val);
          break;
      }
    }
  }
  else {
    std::cerr << "Unknown code in SAI data file header: " << code << "\n";
    exit(5);
  }
}

void SAI_datafile::handleMainEntry(const std::vector<std::string>& values, int line, int main_item_line)
{
  if (active_sais.empty()) {
    std::cerr << "Invalid SAI data file: data received, but no SAIs defined.";
    exit(5);
  }

  size_t idx = main_item_line % active_sais.size();
  if (active_sais[idx] == "suid") {
    if (values.size() > 1) {
      std::cerr << "Invalid SAI content-id entry in line " << line << "\n";
      exit(5);
    }

    if (values.empty()) {
      gimi_content_ids.push_back({});
    }
    else {
      gimi_content_ids.push_back(values[0]);
    }
  }
  else if (active_sais[idx] == "stai") {
    if (values.size() > 4) {
      std::cerr << "Invalid SAI timestamp entry in line " << line << "\n";
      exit(5);
    }

    if (values.empty()) {
      tai_timestamps.push_back(nullptr);
    }
    else {
      heif_tai_timestamp_packet* tai = heif_tai_timestamp_packet_alloc();
      for (size_t i=0;i<values.size();i++) {
        int64_t val = std::stoll(values[i]);
        if (i>=1 && i<=3 && (val < 0 || val > 1)) {
          std::cerr << "Invalid SAI timestamp entry in line " << line << "\n";
          exit(5);
        }

        switch (i) {
          case 0:
            tai->tai_timestamp = val;
            break;
          case 1:
            tai->synchronization_state = static_cast<uint8_t>(val);
            break;
          case 2:
            tai->timestamp_generation_failure = static_cast<uint8_t>(val);
            break;
          case 3:
            tai->timestamp_is_modified = static_cast<uint8_t>(val);
            break;
        }
      }

      tai_timestamps.push_back(tai);
    }
  }
}

bool SAI_datafile::isSeparatorLine(const std::string& line)
{
  return line.starts_with("---");
}

std::vector<std::string> SAI_datafile::splitCSV(const std::string& line)
{
  std::vector<std::string> parts;
  std::stringstream ss(line);
  std::string item;

  while (std::getline(ss, item, ',')) {
    // Trim whitespace
    item.erase(item.begin(),
        std::find_if(item.begin(), item.end(), [](unsigned char ch){ return !std::isspace(ch); }));
    item.erase(
        std::find_if(item.rbegin(), item.rend(), [](unsigned char ch){ return !std::isspace(ch); }).base(),
        item.end()
    );
    parts.push_back(item);
  }

  return parts;
}


void SAI_datafile::load_sai_data_from_file(const char* sai_file)
{
  std::ifstream istr(sai_file);
  if (!istr) {
    std::cerr << "Could not open SAI data file\n";
    exit(5);
  }

  // --- read header

  std::string line;
  bool inHeader = true;
  int line_counter = 0;
  int main_item_line = 0;

  while (std::getline(istr, line)) {
    line_counter++;

    if (inHeader && line.empty())
      continue;

    if (inHeader && isSeparatorLine(line)) {
      // Switch to main part
      inHeader = false;
      continue;
    }

    if (inHeader) {
      // Header line: starts with 4-character code, optional CSV list
      if (line.size() < 4) {
        std::cerr << "Invalid header line: " << line << "\n";
        continue;
      }

      std::string code = line.substr(0, 4);

      std::vector<std::string> values;
      if (line.size() > 4) {
        // Skip the space after the code, if present
        std::string rest = line.substr(4);
        if (!rest.empty() && (rest[0] == ' ' || rest[0] == '\t'))
          rest.erase(0, 1);

        if (!rest.empty())
          values = splitCSV(rest);
      }

      handleHeaderEntry(code, values);
    }
    else {
      // Main section: entire line is CSV
      auto values = splitCSV(line);
      handleMainEntry(values, line_counter, main_item_line++);
    }
  }
}