| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // This #define must be before any #include's | ||
| 2 | #define _FILE_OFFSET_BITS 64 | ||
| 3 | #define __STDC_FORMAT_MACROS | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cerrno> | ||
| 7 | #include <cstring> | ||
| 8 | #include <memory> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include <atomic> | ||
| 12 | #include <condition_variable> | ||
| 13 | #include <deque> | ||
| 14 | #include <list> | ||
| 15 | #include <map> | ||
| 16 | #include <mutex> | ||
| 17 | #include <thread> | ||
| 18 | |||
| 19 | #include <pthread.h> | ||
| 20 | |||
| 21 | #include <chrono> | ||
| 22 | using namespace std::literals; | ||
| 23 | using namespace std::chrono_literals; | ||
| 24 | using namespace std::literals::chrono_literals; | ||
| 25 | |||
| 26 | #include <sys/file.h> | ||
| 27 | #include <sys/stat.h> | ||
| 28 | #include <sys/types.h> | ||
| 29 | #include <fcntl.h> | ||
| 30 | #include <inttypes.h> | ||
| 31 | #include <stdio.h> | ||
| 32 | |||
| 33 | #ifdef _MSC_VER | ||
| 34 | #include <wx/wx.h> | ||
| 35 | int gettimeofday(struct timeval* tp, struct timezone* tzp) { | ||
| 36 | // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's | ||
| 37 | // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) | ||
| 38 | // until 00:00:00 January 1, 1970 | ||
| 39 | static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL); | ||
| 40 | |||
| 41 | SYSTEMTIME system_time; | ||
| 42 | FILETIME file_time; | ||
| 43 | uint64_t time; | ||
| 44 | |||
| 45 | GetSystemTime(&system_time); | ||
| 46 | SystemTimeToFileTime(&system_time, &file_time); | ||
| 47 | time = ((uint64_t)file_time.dwLowDateTime); | ||
| 48 | time += ((uint64_t)file_time.dwHighDateTime) << 32; | ||
| 49 | |||
| 50 | tp->tv_sec = (long)((time - EPOCH) / 10000000L); | ||
| 51 | tp->tv_usec = (long)(system_time.wMilliseconds * 1000); | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | #define ftello _ftelli64 | ||
| 55 | #define fseeko _fseeki64 | ||
| 56 | |||
| 57 | #else | ||
| 58 | #include <sys/time.h> | ||
| 59 | #include <unistd.h> | ||
| 60 | #endif | ||
| 61 | |||
| 62 | #include "FSEQFile.h" | ||
| 63 | |||
| 64 | 76 | static void SetThreadName(const std::string& name) { | |
| 65 | #ifdef PLATFORM_OSX | ||
| 66 | pthread_setname_np(name.c_str()); | ||
| 67 | #else | ||
| 68 | 76 | pthread_setname_np(pthread_self(), name.c_str()); | |
| 69 | #endif | ||
| 70 | 76 | } | |
| 71 | |||
| 72 | #if defined(PLATFORM_OSX) | ||
| 73 | #define PLATFORM_UNKNOWN | ||
| 74 | #endif | ||
| 75 | |||
| 76 | #if defined(PLATFORM_PI) || defined(PLATFORM_BBB) || defined(PLATFORM_BB64) || defined(PLATFORM_UNKNOWN) || defined(PLATFORM_DEBIAN) || defined(PLATFORM_FEDORA) || defined(PLATFORM_UBUNTU) || defined(PLATFORM_MINT) | ||
| 77 | // for FPP, use FPP logging | ||
| 78 | #include "Warnings.h" | ||
| 79 | #include "log.h" | ||
| 80 | ✗ | inline void AddSlowStorageWarning() { | |
| 81 | ✗ | WarningHolder::AddWarningTimeout(90, 18, "FSEQ Data Block not available - Likely slow storage"); | |
| 82 | ✗ | } | |
| 83 | #else | ||
| 84 | // compiling within xLights, use log4cpp | ||
| 85 | #define PLATFORM_UNKNOWN | ||
| 86 | #include <log4cpp/Category.hh> | ||
| 87 | template<typename... Args> | ||
| 88 | static void LogErr(int i, const char* fmt, Args... args) { | ||
| 89 | static log4cpp::Category& fseq_logger_base = log4cpp::Category::getInstance(std::string("log_base")); | ||
| 90 | char buf[256]; | ||
| 91 | const char* nfmt = fmt; | ||
| 92 | if (fmt[strlen(fmt) - 1] == '\n') { | ||
| 93 | strcpy(buf, fmt); | ||
| 94 | buf[strlen(fmt) - 1] = 0; | ||
| 95 | nfmt = buf; | ||
| 96 | } | ||
| 97 | fseq_logger_base.error(nfmt, args...); | ||
| 98 | } | ||
| 99 | template<typename... Args> | ||
| 100 | static void LogInfo(int i, const char* fmt, Args... args) { | ||
| 101 | static log4cpp::Category& fseq_logger_base = log4cpp::Category::getInstance(std::string("log_base")); | ||
| 102 | char buf[256]; | ||
| 103 | const char* nfmt = fmt; | ||
| 104 | if (fmt[strlen(fmt) - 1] == '\n') { | ||
| 105 | strcpy(buf, fmt); | ||
| 106 | buf[strlen(fmt) - 1] = 0; | ||
| 107 | nfmt = buf; | ||
| 108 | } | ||
| 109 | fseq_logger_base.info(nfmt, args...); | ||
| 110 | } | ||
| 111 | template<typename... Args> | ||
| 112 | static void LogDebug(int i, const char* fmt, Args... args) { | ||
| 113 | static log4cpp::Category& fseq_logger_base = log4cpp::Category::getInstance(std::string("log_base")); | ||
| 114 | char buf[256]; | ||
| 115 | const char* nfmt = fmt; | ||
| 116 | if (fmt[strlen(fmt) - 1] == '\n') { | ||
| 117 | strcpy(buf, fmt); | ||
| 118 | buf[strlen(fmt) - 1] = 0; | ||
| 119 | nfmt = buf; | ||
| 120 | } | ||
| 121 | fseq_logger_base.debug(nfmt, args...); | ||
| 122 | } | ||
| 123 | inline void AddSlowStorageWarning() { | ||
| 124 | } | ||
| 125 | |||
| 126 | #define VB_SEQUENCE 1 | ||
| 127 | #define VB_ALL 0 | ||
| 128 | #endif | ||
| 129 | |||
| 130 | #ifndef NO_ZSTD | ||
| 131 | #include <zstd.h> | ||
| 132 | #endif | ||
| 133 | #ifndef NO_ZLIB | ||
| 134 | #include <zlib.h> | ||
| 135 | #endif | ||
| 136 | |||
| 137 | using FrameData = FSEQFile::FrameData; | ||
| 138 | |||
| 139 | 12 | inline void DumpHeader(const char* title, unsigned char data[], int len) { | |
| 140 | 12 | int x = 0; | |
| 141 | char tmpStr[128]; | ||
| 142 | |||
| 143 | 12 | snprintf(tmpStr, 128, "%s: (%d bytes)\n", title, len); | |
| 144 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | LogInfo(VB_ALL, tmpStr); |
| 145 | |||
| 146 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 12 times.
|
100 | for (int y = 0; y < len; y++) { |
| 147 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 78 times.
|
88 | if (x == 0) { |
| 148 | 10 | snprintf(tmpStr, 128, "%06x: ", y); | |
| 149 | } | ||
| 150 | 88 | snprintf(tmpStr + strlen(tmpStr), 128 - strlen(tmpStr), "%02x ", (int)(data[y] & 0xFF)); | |
| 151 | 88 | x++; | |
| 152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (x == 16) { |
| 153 | ✗ | x = 0; | |
| 154 | ✗ | snprintf(tmpStr + strlen(tmpStr), 128 - strlen(tmpStr), "\n"); | |
| 155 | ✗ | LogInfo(VB_ALL, tmpStr); | |
| 156 | } | ||
| 157 | } | ||
| 158 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | if (x != 0) { |
| 159 | 10 | snprintf(tmpStr + strlen(tmpStr), 128 - strlen(tmpStr), "\n"); | |
| 160 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | LogInfo(VB_ALL, tmpStr); |
| 161 | } | ||
| 162 | 12 | } | |
| 163 | |||
| 164 | ✗ | inline uint64_t GetTime(void) { | |
| 165 | struct timeval now_tv; | ||
| 166 | ✗ | gettimeofday(&now_tv, NULL); | |
| 167 | ✗ | return now_tv.tv_sec * 1000000LL + now_tv.tv_usec; | |
| 168 | } | ||
| 169 | |||
| 170 | 1266 | inline long roundTo4Internal(long i) { | |
| 171 | 1266 | long remainder = i % 4; | |
| 172 |
2/2✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 126 times.
|
1266 | if (remainder == 0) { |
| 173 | 1140 | return i; | |
| 174 | } | ||
| 175 | 126 | return i + 4 - remainder; | |
| 176 | } | ||
| 177 | |||
| 178 | 8376 | inline uint32_t read4ByteUInt(const uint8_t* data) { | |
| 179 | 8376 | uint32_t r = (data[0]) + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); | |
| 180 | 8376 | return r; | |
| 181 | } | ||
| 182 | 10840 | inline uint32_t read3ByteUInt(const uint8_t* data) { | |
| 183 | 10840 | uint32_t r = (data[0]) + (data[1] << 8) + (data[2] << 16); | |
| 184 | 10840 | return r; | |
| 185 | } | ||
| 186 | 8442 | inline uint32_t read2ByteUInt(const uint8_t* data) { | |
| 187 | 8442 | uint32_t r = (data[0]) + (data[1] << 8); | |
| 188 | 8442 | return r; | |
| 189 | } | ||
| 190 | 1334 | inline void write2ByteUInt(uint8_t* data, uint32_t v) { | |
| 191 | 1334 | data[0] = (uint8_t)(v & 0xFF); | |
| 192 | 1334 | data[1] = (uint8_t)((v >> 8) & 0xFF); | |
| 193 | 1334 | } | |
| 194 | 7028 | inline void write3ByteUInt(uint8_t* data, uint32_t v) { | |
| 195 | 7028 | data[0] = (uint8_t)(v & 0xFF); | |
| 196 | 7028 | data[1] = (uint8_t)((v >> 8) & 0xFF); | |
| 197 | 7028 | data[2] = (uint8_t)((v >> 16) & 0xFF); | |
| 198 | 7028 | } | |
| 199 | 5648 | inline void write4ByteUInt(uint8_t* data, uint32_t v) { | |
| 200 | 5648 | data[0] = (uint8_t)(v & 0xFF); | |
| 201 | 5648 | data[1] = (uint8_t)((v >> 8) & 0xFF); | |
| 202 | 5648 | data[2] = (uint8_t)((v >> 16) & 0xFF); | |
| 203 | 5648 | data[3] = (uint8_t)((v >> 24) & 0xFF); | |
| 204 | 5648 | } | |
| 205 | |||
| 206 | static const int V1FSEQ_MINOR_VERSION = 0; | ||
| 207 | static const int V1FSEQ_MAJOR_VERSION = 1; | ||
| 208 | |||
| 209 | static const int V2FSEQ_MINOR_VERSION = 0; | ||
| 210 | static const int V2FSEQ_MAJOR_VERSION = 2; | ||
| 211 | |||
| 212 | static const int V1ESEQ_MINOR_VERSION = 0; | ||
| 213 | static const int V1ESEQ_MAJOR_VERSION = 2; | ||
| 214 | static const int V1ESEQ_HEADER_IDENTIFIER = 'E'; | ||
| 215 | static const int V1ESEQ_CHANNEL_DATA_OFFSET = 20; | ||
| 216 | static const int V1ESEQ_STEP_TIME = 50; | ||
| 217 | |||
| 218 | // Largest channel count any real writer input buffer can hold. Sequence | ||
| 219 | // m_seqData is FPPD_MAX_CHANNELS (8192*1024) and the fsequtils buffers are | ||
| 220 | // sized to match, so clamping file-fed counts/ranges here to the same ceiling | ||
| 221 | // keeps the write path inside every real caller buffer. Valid files sit far | ||
| 222 | // below it, so this is a no-op for them. Kept file-local to avoid a | ||
| 223 | // Sequence -> FSEQFile include inversion (cf. FPPD_MAX_CHANNELS in Sequence.h). | ||
| 224 | static constexpr uint64_t MAX_CHANNEL_SPACE = (uint64_t)8192 * 1024; | ||
| 225 | |||
| 226 | 2127 | FSEQFile* FSEQFile::openFSEQFile(const std::string& fn) { | |
| 227 |
1/2✓ Branch 0 taken 2127 times.
✗ Branch 1 not taken.
|
2127 | FILE* seqFile = fopen((const char*)fn.c_str(), "rb"); |
| 228 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2126 times.
|
2127 | if (seqFile == NULL) { |
| 229 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogErr(VB_SEQUENCE, "Error pre-reading FSEQ file (%s), fopen returned NULL\n", fn.c_str()); |
| 230 | 1 | return nullptr; | |
| 231 | } | ||
| 232 | 2126 | flock(fileno(seqFile), LOCK_SH); | |
| 233 |
1/2✓ Branch 0 taken 2126 times.
✗ Branch 1 not taken.
|
2126 | fseeko(seqFile, 0L, SEEK_SET); |
| 234 | |||
| 235 | // An initial read request of 8 bytes covers the file identifier, version fields and channel data offset | ||
| 236 | // This is the minimum needed to validate the file and prepare the proper sized buffer for a larger read | ||
| 237 | static const int initialReadLen = 8; | ||
| 238 | |||
| 239 | unsigned char headerPeek[initialReadLen]; | ||
| 240 |
1/2✓ Branch 0 taken 2126 times.
✗ Branch 1 not taken.
|
2126 | int bytesRead = fread(headerPeek, 1, initialReadLen, seqFile); |
| 241 | #ifndef PLATFORM_UNKNOWN | ||
| 242 | 109 | posix_fadvise(fileno(seqFile), 0, 0, POSIX_FADV_SEQUENTIAL); | |
| 243 | 109 | posix_fadvise(fileno(seqFile), 0, 1024 * 1024, POSIX_FADV_WILLNEED); | |
| 244 | #endif | ||
| 245 | |||
| 246 | // Validate bytesRead covers at least the initial read length | ||
| 247 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2124 times.
|
2126 | if (bytesRead < initialReadLen) { |
| 248 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "Error pre-reading FSEQ file (%s) header, required %d bytes but read %d\n", fn.c_str(), initialReadLen, bytesRead); |
| 249 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | DumpHeader("File hader peek:", headerPeek, bytesRead); |
| 250 | 2 | flock(fileno(seqFile), LOCK_UN); | |
| 251 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | fclose(seqFile); |
| 252 | 2 | return nullptr; | |
| 253 | } | ||
| 254 | |||
| 255 | // Validate the 4 byte file format identifier is supported | ||
| 256 |
8/12✓ Branch 0 taken 55 times.
✓ Branch 1 taken 2069 times.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 2121 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2121 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2121 times.
|
2124 | if ((headerPeek[0] != 'P' && headerPeek[0] != 'F' && headerPeek[0] != V1ESEQ_HEADER_IDENTIFIER) || headerPeek[1] != 'S' || headerPeek[2] != 'E' || headerPeek[3] != 'Q') { |
| 257 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LogErr(VB_SEQUENCE, "Error pre-reading FSEQ file (%s) header, invalid identifier\n", fn.c_str()); |
| 258 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | DumpHeader("File header peek:", headerPeek, bytesRead); |
| 259 | 3 | flock(fileno(seqFile), LOCK_UN); | |
| 260 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | fclose(seqFile); |
| 261 | 3 | return nullptr; | |
| 262 | } | ||
| 263 | |||
| 264 | 2121 | uint64_t seqChanDataOffset = read2ByteUInt(&headerPeek[4]); | |
| 265 | 2121 | int seqVersionMinor = headerPeek[6]; | |
| 266 | 2121 | int seqVersionMajor = headerPeek[7]; | |
| 267 | |||
| 268 | // Test for a ESEQ file (identifier[0] == 'E') | ||
| 269 | // ESEQ files are uncompressed V2 FSEQ files with a custom header | ||
| 270 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2069 times.
|
2121 | if (headerPeek[0] == V1ESEQ_HEADER_IDENTIFIER) { |
| 271 | 52 | seqChanDataOffset = V1ESEQ_CHANNEL_DATA_OFFSET; | |
| 272 | 52 | seqVersionMajor = V1ESEQ_MAJOR_VERSION; | |
| 273 | 52 | seqVersionMinor = V1ESEQ_MINOR_VERSION; | |
| 274 | } | ||
| 275 | |||
| 276 | // The ctors index fixed header fields directly, so require at least the | ||
| 277 | // bytes they touch before building the header buffer. Anything shorter is | ||
| 278 | // truncated/corrupt: base reads [18] (normal) or [8..11] (ESEQ), V2 reads | ||
| 279 | // [20],[21],[22],[24..31], V2-ESEQ reads [12..19]. All callers already | ||
| 280 | // handle a nullptr return, and valid files are far larger than these floors. | ||
| 281 | 2121 | size_t minHeaderSize = 19; | |
| 282 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2069 times.
|
2121 | if (headerPeek[0] == V1ESEQ_HEADER_IDENTIFIER) { |
| 283 | 52 | minHeaderSize = 20; | |
| 284 |
2/2✓ Branch 0 taken 2065 times.
✓ Branch 1 taken 4 times.
|
2069 | } else if (seqVersionMajor == V2FSEQ_MAJOR_VERSION) { |
| 285 | 2065 | minHeaderSize = 32; | |
| 286 | } | ||
| 287 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2116 times.
|
2121 | if (seqChanDataOffset < minHeaderSize) { |
| 288 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogErr(VB_SEQUENCE, "Error reading FSEQ file (%s) header, channel data offset %" PRIu64 " is below minimum %d bytes, file is truncated\n", |
| 289 | fn.c_str(), seqChanDataOffset, (int)minHeaderSize); | ||
| 290 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | DumpHeader("File header peek:", headerPeek, bytesRead); |
| 291 | 5 | flock(fileno(seqFile), LOCK_UN); | |
| 292 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | fclose(seqFile); |
| 293 | 5 | return nullptr; | |
| 294 | } | ||
| 295 | |||
| 296 | // Read the full header size (beginning at 0 and ending at seqChanDataOffset) | ||
| 297 |
1/2✓ Branch 0 taken 2116 times.
✗ Branch 1 not taken.
|
2116 | std::vector<uint8_t> header(seqChanDataOffset); |
| 298 |
1/2✓ Branch 0 taken 2116 times.
✗ Branch 1 not taken.
|
2116 | fseeko(seqFile, 0L, SEEK_SET); |
| 299 |
1/2✓ Branch 0 taken 2116 times.
✗ Branch 1 not taken.
|
2116 | bytesRead = fread(&header[0], 1, seqChanDataOffset, seqFile); |
| 300 | |||
| 301 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2114 times.
|
2116 | if (bytesRead != seqChanDataOffset) { |
| 302 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "Error reading FSEQ file (%s) header, length is %d bytes but read %d\n", fn.c_str(), seqChanDataOffset, bytesRead); |
| 303 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | DumpHeader("File header:", &header[0], bytesRead); |
| 304 | 2 | flock(fileno(seqFile), LOCK_UN); | |
| 305 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | fclose(seqFile); |
| 306 | 2 | return nullptr; | |
| 307 | } | ||
| 308 | |||
| 309 | // Validate the major version is supported | ||
| 310 | // Return a file wrapper to handle version specific metadata | ||
| 311 | 2114 | FSEQFile* file = nullptr; | |
| 312 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2111 times.
|
2114 | if (seqVersionMajor == V1FSEQ_MAJOR_VERSION) { |
| 313 |
3/8✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
3 | file = new V1FSEQFile(fn, seqFile, header); |
| 314 |
1/2✓ Branch 0 taken 2111 times.
✗ Branch 1 not taken.
|
2111 | } else if (seqVersionMajor == V2FSEQ_MAJOR_VERSION) { |
| 315 |
3/8✓ Branch 0 taken 2111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2111 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2111 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
2111 | file = new V2FSEQFile(fn, seqFile, header); |
| 316 | } else { | ||
| 317 | ✗ | LogErr(VB_SEQUENCE, "Error opening FSEQ file (%s), unknown version %d.%d\n", fn.c_str(), seqVersionMajor, seqVersionMinor); | |
| 318 | ✗ | DumpHeader("File header:", &header[0], bytesRead); | |
| 319 | ✗ | flock(fileno(seqFile), LOCK_UN); | |
| 320 | ✗ | fclose(seqFile); | |
| 321 | ✗ | return nullptr; | |
| 322 | } | ||
| 323 | |||
| 324 |
1/2✓ Branch 0 taken 2114 times.
✗ Branch 1 not taken.
|
2114 | file->dumpInfo(); |
| 325 | 2114 | return file; | |
| 326 | 2116 | } | |
| 327 | 634 | FSEQFile* FSEQFile::createFSEQFile(const std::string& fn, | |
| 328 | int version, | ||
| 329 | CompressionType ct, | ||
| 330 | int level) { | ||
| 331 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 628 times.
|
634 | if (version == V1FSEQ_MAJOR_VERSION) { |
| 332 |
2/6✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
6 | V1FSEQFile* f = new V1FSEQFile(fn); |
| 333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!f->m_seqFile) { |
| 334 | ✗ | delete f; | |
| 335 | ✗ | f = nullptr; | |
| 336 | } | ||
| 337 | 6 | return f; | |
| 338 |
1/2✓ Branch 0 taken 628 times.
✗ Branch 1 not taken.
|
628 | } else if (version == V2FSEQ_MAJOR_VERSION) { |
| 339 |
2/6✓ Branch 0 taken 628 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 628 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
628 | V2FSEQFile* f = new V2FSEQFile(fn, ct, level); |
| 340 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 627 times.
|
628 | if (!f->m_seqFile) { |
| 341 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | delete f; |
| 342 | 1 | f = nullptr; | |
| 343 | } | ||
| 344 | 628 | return f; | |
| 345 | } | ||
| 346 | ✗ | LogErr(VB_SEQUENCE, "Error creating FSEQ file (%s), unknown version %d\n", fn.c_str(), version); | |
| 347 | ✗ | return nullptr; | |
| 348 | } | ||
| 349 | ✗ | std::string FSEQFile::getMediaFilename(const std::string& fn) { | |
| 350 | ✗ | std::unique_ptr<FSEQFile> file(FSEQFile::openFSEQFile(fn)); | |
| 351 | ✗ | if (file) { | |
| 352 | ✗ | return file->getMediaFilename(); | |
| 353 | } | ||
| 354 | ✗ | return ""; | |
| 355 | ✗ | } | |
| 356 | ✗ | std::string FSEQFile::getMediaFilename() const { | |
| 357 | ✗ | for (auto& a : m_variableHeaders) { | |
| 358 | ✗ | if (a.code[0] == 'm' && a.code[1] == 'f') { | |
| 359 | ✗ | const auto& data = a.getData(); | |
| 360 | ✗ | if (data.empty()) { | |
| 361 | ✗ | continue; | |
| 362 | } | ||
| 363 | ✗ | const char* d = (const char*)data.data(); | |
| 364 | ✗ | size_t len = strnlen(d, data.size()); | |
| 365 | ✗ | if (len == 0) { | |
| 366 | ✗ | continue; | |
| 367 | } | ||
| 368 | ✗ | return std::string(d, len); | |
| 369 | } | ||
| 370 | } | ||
| 371 | ✗ | return ""; | |
| 372 | } | ||
| 373 | |||
| 374 | static const int FSEQ_DEFAULT_STEP_TIME = 50; | ||
| 375 | static const int FSEQ_VARIABLE_HEADER_SIZE = 4; | ||
| 376 | |||
| 377 | 634 | FSEQFile::FSEQFile(const std::string& fn) : | |
| 378 | 634 | m_filename(fn), | |
| 379 | 634 | m_seqNumFrames(0), | |
| 380 | 634 | m_seqChannelCount(0), | |
| 381 | 634 | m_seqStepTime(FSEQ_DEFAULT_STEP_TIME), | |
| 382 | 634 | m_variableHeaders(), | |
| 383 | 634 | m_uniqueId(0), | |
| 384 | 634 | m_seqFileSize(0), | |
| 385 | 634 | m_memoryBuffer(), | |
| 386 | 634 | m_seqChanDataOffset(0), | |
| 387 | 634 | m_memoryBufferPos(0) { | |
| 388 |
2/4✓ Branch 0 taken 634 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 634 times.
|
634 | if (fn == "-memory-") { |
| 389 | ✗ | m_seqFile = nullptr; | |
| 390 | ✗ | m_memoryBuffer.reserve(1024 * 1024); | |
| 391 | } else { | ||
| 392 |
1/2✓ Branch 0 taken 634 times.
✗ Branch 1 not taken.
|
634 | m_seqFile = fopen((const char*)fn.c_str(), "wb"); |
| 393 |
2/2✓ Branch 0 taken 633 times.
✓ Branch 1 taken 1 times.
|
634 | if (m_seqFile) { |
| 394 | 633 | flock(fileno(m_seqFile), LOCK_EX); | |
| 395 | } else { | ||
| 396 | // createFSEQFile() checks m_seqFile and returns nullptr; the | ||
| 397 | // caller is expected to handle that, so only log here. | ||
| 398 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogErr(VB_SEQUENCE, "Error creating FSEQ file (%s), fopen returned NULL: %s\n", fn.c_str(), strerror(errno)); |
| 399 | } | ||
| 400 | } | ||
| 401 | 634 | } | |
| 402 | |||
| 403 | 2747 | void FSEQFile::dumpInfo(bool indent) { | |
| 404 | 2747 | char ind[5] = " "; | |
| 405 |
2/2✓ Branch 0 taken 2114 times.
✓ Branch 1 taken 633 times.
|
2747 | if (!indent) { |
| 406 | 2114 | ind[0] = 0; | |
| 407 | } | ||
| 408 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sSequence File Information\n", ind); |
| 409 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sseqFilename : %s\n", ind, m_filename.c_str()); |
| 410 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sseqVersion : %d.%d\n", ind, m_seqVersionMajor, m_seqVersionMinor); |
| 411 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sseqChanDataOffset : %d\n", ind, m_seqChanDataOffset); |
| 412 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sseqChannelCount : %d\n", ind, m_seqChannelCount); |
| 413 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sseqNumPeriods : %d\n", ind, m_seqNumFrames); |
| 414 |
1/2✓ Branch 0 taken 2747 times.
✗ Branch 1 not taken.
|
2747 | LogDebug(VB_SEQUENCE, "%sseqStepTime : %dms\n", ind, m_seqStepTime); |
| 415 | 2747 | } | |
| 416 | |||
| 417 | 36 | void FSEQFile::initializeFromFSEQ(const FSEQFile& fseq) { | |
| 418 | 36 | m_seqNumFrames = fseq.m_seqNumFrames; | |
| 419 | 36 | m_seqChannelCount = fseq.m_seqChannelCount; | |
| 420 | 36 | m_seqStepTime = fseq.m_seqStepTime; | |
| 421 | 36 | m_variableHeaders = fseq.m_variableHeaders; | |
| 422 | 36 | m_uniqueId = fseq.m_uniqueId; | |
| 423 | |||
| 424 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (fseq.getVersionMajor() >= 2) { |
| 425 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | const V2FSEQFile* v2 = dynamic_cast<const V2FSEQFile*>(&fseq); |
| 426 |
5/6✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 34 times.
|
36 | if (v2 && !v2->m_sparseRanges.empty()) { |
| 427 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | for (auto& a : v2->m_sparseRanges) { |
| 428 | // File-fed: use 64-bit so a.first + a.second cannot wrap, and | ||
| 429 | // cap the expansion at channel space. Without the cap a crafted | ||
| 430 | // sparse end (3-byte fields allow ~33M) inflates m_seqChannelCount | ||
| 431 | // far beyond any real input buffer, turning the dense write path | ||
| 432 | // (write(data, getChannelCount())) into an OOB read. | ||
| 433 | 5 | uint64_t end = (uint64_t)a.first + (uint64_t)a.second; | |
| 434 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (end > MAX_CHANNEL_SPACE) { |
| 435 | ✗ | end = MAX_CHANNEL_SPACE; | |
| 436 | } | ||
| 437 | 5 | m_seqChannelCount = std::max(m_seqChannelCount, (uint32_t)end); | |
| 438 | } | ||
| 439 | } | ||
| 440 | } | ||
| 441 | 36 | } | |
| 442 | |||
| 443 | 2114 | FSEQFile::FSEQFile(const std::string& fn, FILE* file, const std::vector<uint8_t>& header) : | |
| 444 | 2114 | m_filename(fn), | |
| 445 | 2114 | m_seqFile(file), | |
| 446 | 2114 | m_uniqueId(0), | |
| 447 | 2114 | m_memoryBuffer(), | |
| 448 | 2114 | m_memoryBufferPos(0) { | |
| 449 |
1/2✓ Branch 0 taken 2114 times.
✗ Branch 1 not taken.
|
2114 | fseeko(m_seqFile, 0L, SEEK_END); |
| 450 |
1/2✓ Branch 0 taken 2114 times.
✗ Branch 1 not taken.
|
2114 | m_seqFileSize = ftello(m_seqFile); |
| 451 |
1/2✓ Branch 0 taken 2114 times.
✗ Branch 1 not taken.
|
2114 | fseeko(m_seqFile, 0L, SEEK_SET); |
| 452 | |||
| 453 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 2064 times.
|
2114 | if (header[0] == V1ESEQ_HEADER_IDENTIFIER) { |
| 454 | 50 | m_seqChanDataOffset = V1ESEQ_CHANNEL_DATA_OFFSET; | |
| 455 | 50 | m_seqVersionMinor = V1ESEQ_MINOR_VERSION; | |
| 456 | 50 | m_seqVersionMajor = V1ESEQ_MAJOR_VERSION; | |
| 457 | 50 | m_seqChannelCount = read4ByteUInt(&header[8]); | |
| 458 | 50 | m_seqStepTime = V1ESEQ_STEP_TIME; | |
| 459 | // Both operands of the frame-count division come straight out of the | ||
| 460 | // file, and openFSEQFile() only validates the 'ESEQ' magic - so a | ||
| 461 | // truncated or corrupt effect file reaches here unfiltered. A zero | ||
| 462 | // channel count divides by zero, which on 32-bit ARM traps in | ||
| 463 | // __aeabi_ldiv0 and takes fppd down from StartEffect(); a file shorter | ||
| 464 | // than the header underflows the unsigned subtraction into a huge frame | ||
| 465 | // count. Report either as an empty sequence and let the caller's | ||
| 466 | // existing zero-frame handling deal with it. | ||
| 467 |
3/4✓ Branch 0 taken 48 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
|
50 | if (m_seqChannelCount == 0 || m_seqFileSize < (uint64_t)V1ESEQ_CHANNEL_DATA_OFFSET) { |
| 468 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "Invalid ESEQ file %s: %u channels, %" PRIu64 " bytes\n", |
| 469 | m_filename.c_str(), m_seqChannelCount, m_seqFileSize); | ||
| 470 | 2 | m_seqNumFrames = 0; | |
| 471 | } else { | ||
| 472 | 48 | m_seqNumFrames = (m_seqFileSize - V1ESEQ_CHANNEL_DATA_OFFSET) / m_seqChannelCount; | |
| 473 | } | ||
| 474 | } else { | ||
| 475 | 2064 | m_seqChanDataOffset = read2ByteUInt(&header[4]); | |
| 476 | 2064 | m_seqVersionMinor = header[6]; | |
| 477 | 2064 | m_seqVersionMajor = header[7]; | |
| 478 | 2064 | m_seqChannelCount = read4ByteUInt(&header[10]); | |
| 479 | 2064 | m_seqNumFrames = read4ByteUInt(&header[14]); | |
| 480 | 2064 | m_seqStepTime = header[18]; | |
| 481 | // The step time is one unvalidated byte out of the file, and every | ||
| 482 | // consumer divides by it: a truncated or zero-filled header yields 0 | ||
| 483 | // and the first "which frame are we on?" division traps (SIGFPE on | ||
| 484 | // x86_64, __aeabi_idiv0 on 32-bit ARM) and takes fppd down from the | ||
| 485 | // playlist's main loop. Clamp it here, at the single point it enters | ||
| 486 | // the program, rather than guarding each divide. | ||
| 487 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2062 times.
|
2064 | if (m_seqStepTime <= 0) { |
| 488 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "Invalid FSEQ file %s: step time %d, using %dms\n", |
| 489 | m_filename.c_str(), m_seqStepTime, FSEQ_DEFAULT_STEP_TIME); | ||
| 490 | 2 | m_seqStepTime = FSEQ_DEFAULT_STEP_TIME; | |
| 491 | } | ||
| 492 | } | ||
| 493 | 2114 | } | |
| 494 | 5262 | FSEQFile::~FSEQFile() { | |
| 495 |
2/2✓ Branch 0 taken 2630 times.
✓ Branch 1 taken 1 times.
|
5262 | if (m_seqFile) { |
| 496 | 5260 | flock(fileno(m_seqFile), LOCK_UN); | |
| 497 | 5260 | fclose(m_seqFile); | |
| 498 | } | ||
| 499 | 5262 | } | |
| 500 | |||
| 501 | 346088 | int FSEQFile::seek(uint64_t location, int origin) { | |
| 502 |
1/2✓ Branch 0 taken 346088 times.
✗ Branch 1 not taken.
|
346088 | if (m_seqFile) { |
| 503 | 346088 | return fseeko(m_seqFile, location, origin); | |
| 504 | ✗ | } else if (origin == SEEK_SET) { | |
| 505 | ✗ | m_memoryBufferPos = location; | |
| 506 | ✗ | } else if (origin == SEEK_CUR) { | |
| 507 | ✗ | m_memoryBufferPos += location; | |
| 508 | ✗ | } else if (origin == SEEK_END) { | |
| 509 | ✗ | m_memoryBufferPos = m_memoryBuffer.size(); | |
| 510 | } | ||
| 511 | ✗ | return 0; | |
| 512 | } | ||
| 513 | |||
| 514 | 2243 | uint64_t FSEQFile::tell() { | |
| 515 |
1/2✓ Branch 0 taken 2243 times.
✗ Branch 1 not taken.
|
2243 | if (m_seqFile) { |
| 516 | 2243 | return ftello(m_seqFile); | |
| 517 | } | ||
| 518 | ✗ | return m_memoryBufferPos; | |
| 519 | } | ||
| 520 | |||
| 521 | 378042 | uint64_t FSEQFile::write(const void* ptr, uint64_t size) { | |
| 522 |
1/2✓ Branch 0 taken 378042 times.
✗ Branch 1 not taken.
|
378042 | if (m_seqFile) { |
| 523 | 378042 | return fwrite(ptr, 1, size, m_seqFile); | |
| 524 | } | ||
| 525 | ✗ | if ((m_memoryBufferPos + size) > m_memoryBuffer.size()) { | |
| 526 | ✗ | m_memoryBuffer.resize(m_memoryBufferPos + size); | |
| 527 | } | ||
| 528 | ✗ | memcpy(&m_memoryBuffer[m_memoryBufferPos], ptr, size); | |
| 529 | ✗ | m_memoryBufferPos += size; | |
| 530 | ✗ | return size; | |
| 531 | } | ||
| 532 | |||
| 533 | 174559 | uint64_t FSEQFile::read(void* ptr, uint64_t size) { | |
| 534 | 174559 | return fread(ptr, 1, size, m_seqFile); | |
| 535 | } | ||
| 536 | |||
| 537 | 205 | void FSEQFile::preload(uint64_t pos, uint64_t size) { | |
| 538 | #ifndef PLATFORM_UNKNOWN | ||
| 539 | ✗ | if (posix_fadvise(fileno(m_seqFile), pos, size, POSIX_FADV_WILLNEED) != 0) { | |
| 540 | ✗ | LogErr(VB_SEQUENCE, "Could not advise kernel %d size: %d\n", (int)pos, (int)size); | |
| 541 | } | ||
| 542 | #endif | ||
| 543 | 205 | } | |
| 544 | |||
| 545 | 122 | inline bool isRecognizedStringVariableHeader(uint8_t a, uint8_t b) { | |
| 546 | // mf - media filename | ||
| 547 | // sp - sequence producer | ||
| 548 | // see https://github.com/FalconChristmas/fpp/blob/master/docs/FSEQ_Sequence_File_Format.txt#L48 for more information | ||
| 549 |
6/8✓ Branch 0 taken 49 times.
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 57 times.
✗ Branch 7 not taken.
|
122 | return (a == 'm' && b == 'f') || (a == 's' && b == 'p'); |
| 550 | } | ||
| 551 | 16 | inline bool isRecognizedBinaryVariableHeader(uint8_t a, uint8_t b) { | |
| 552 | // FC - FPP Commands | ||
| 553 | // FE - FPP Effects | ||
| 554 | // ED - Extended data | ||
| 555 | // SR - Sparse ranges past the 255 the fixed header can count | ||
| 556 | // XS - xLight xsq (zstd compressed binary) | ||
| 557 | // XN - xLight xlights_network.xml (zstd compressed binary) | ||
| 558 | // XR - xLight xlights_rgbeffects.xml (zstd compressed binary) | ||
| 559 |
5/28✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
|
16 | return (a == 'F' && b == 'C') || (a == 'F' && b == 'E') || (a == 'E' && b == 'D') || (a == 'X' && b == 'S') || (a == 'X' && b == 'N') || (a == 'X' && b == 'R') || (a == 'S' && b == 'R'); |
| 560 | } | ||
| 561 | 251 | void FSEQFile::VariableHeader::loadData() const { | |
| 562 |
6/8✓ Branch 0 taken 8 times.
✓ Branch 1 taken 243 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 243 times.
✓ Branch 7 taken 8 times.
|
251 | if (!data.empty() || length == 0 || !fseqFile) { |
| 563 | 243 | return; | |
| 564 | } | ||
| 565 | |||
| 566 | // offset/length come straight from the file (ED header): never allocate or | ||
| 567 | // read past EOF. An unchecked length is a multi-GB resize (OOM) from a tiny | ||
| 568 | // file, and an offset past EOF makes the read return nothing. | ||
| 569 | 8 | uint64_t fileSize = fseqFile->m_seqFileSize; | |
| 570 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (offset >= fileSize) { |
| 571 | ✗ | return; // leave empty; consumers treat empty as absent | |
| 572 | } | ||
| 573 | 8 | uint64_t toRead = length; | |
| 574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (toRead > fileSize - offset) { |
| 575 | ✗ | toRead = fileSize - offset; | |
| 576 | } | ||
| 577 | |||
| 578 | // seek to the offset and read the data | ||
| 579 | 8 | uint64_t currentPos = fseqFile->tell(); | |
| 580 | 8 | fseqFile->seek(offset, SEEK_SET); | |
| 581 | 8 | data.resize((size_t)toRead); | |
| 582 | 8 | uint64_t got = fseqFile->read(data.data(), toRead); | |
| 583 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (got < toRead) { |
| 584 | ✗ | data.resize((size_t)got); | |
| 585 | } | ||
| 586 | 8 | fseqFile->seek(currentPos, SEEK_SET); | |
| 587 | } | ||
| 588 | |||
| 589 | 2064 | void FSEQFile::parseVariableHeaders(const std::vector<uint8_t>& header, int readIndex) { | |
| 590 | 2064 | const int VariableCodeSize = 2; | |
| 591 | 2064 | const int VariableLengthSize = 2; | |
| 592 | |||
| 593 | // when encoding, the header size is rounded to the nearest multiple of 4 | ||
| 594 | // this comparison ensures that there is enough bytes left to at least constitute a 2 byte length + 2 byte code | ||
| 595 |
2/2✓ Branch 0 taken 135 times.
✓ Branch 1 taken 2059 times.
|
2194 | while (readIndex + FSEQ_VARIABLE_HEADER_SIZE < header.size()) { |
| 596 | 135 | int dataLength = read2ByteUInt(&header[readIndex]); | |
| 597 | 135 | readIndex += VariableLengthSize; | |
| 598 | |||
| 599 | 135 | uint8_t code0 = header[readIndex]; | |
| 600 | 135 | uint8_t code1 = header[readIndex + 1]; | |
| 601 | 135 | readIndex += VariableCodeSize; | |
| 602 | |||
| 603 | 135 | VariableHeader vheader(this); | |
| 604 | 135 | vheader.code[0] = code0; | |
| 605 | 135 | vheader.code[1] = code1; | |
| 606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
|
135 | if (dataLength <= FSEQ_VARIABLE_HEADER_SIZE) { |
| 607 | // empty data, advance only the length of the 2 byte code | ||
| 608 | ✗ | LogInfo(VB_SEQUENCE, "VariableHeader has 0 length data: %c%c", code0, code1); | |
| 609 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 127 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
135 | } else if (code0 == 'E' && code1 == 'D') { |
| 610 | // The actual data is elsewhere in the file | ||
| 611 | // The 2-byte codes + 8-byte offset + 4-byte length below need 14 | ||
| 612 | // bytes; a truncated ED block would otherwise memcpy past the end. | ||
| 613 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (readIndex + 14 > header.size()) { |
| 614 | ✗ | LogErr(VB_SEQUENCE, "VariableHeader 'ED' is truncated, %d bytes remain but 14 needed\n", | |
| 615 | (int)(header.size() - readIndex)); | ||
| 616 | ✗ | return; | |
| 617 | } | ||
| 618 | 8 | code0 = header[readIndex]; | |
| 619 | 8 | code1 = header[readIndex + 1]; | |
| 620 | 8 | readIndex += VariableCodeSize; | |
| 621 | 8 | vheader.code[0] = code0; | |
| 622 | 8 | vheader.code[1] = code1; | |
| 623 | 8 | vheader.extendedData = true; | |
| 624 | |||
| 625 | uint64_t offset; | ||
| 626 | 8 | memcpy(&offset, &header[readIndex], 8); | |
| 627 | uint32_t len; | ||
| 628 | 8 | memcpy(&len, &header[readIndex + 8], 4); | |
| 629 | |||
| 630 | 8 | vheader.setDataLocation(offset, len); | |
| 631 | 8 | readIndex += 12; | |
| 632 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 122 times.
|
135 | } else if (readIndex + (dataLength - FSEQ_VARIABLE_HEADER_SIZE) > header.size()) { |
| 633 | // ensure the data length is contained within the header | ||
| 634 | // this is primarily protection against hand modified, or corrupted, sequence files | ||
| 635 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogErr(VB_SEQUENCE, "VariableHeader '%c%c' has out of bounds data length: %d bytes, max length: %d bytes\n", |
| 636 | code0, code1, readIndex + dataLength, header.size()); | ||
| 637 | |||
| 638 | // there is no reasonable way to recover from this error - the reported dataLength is longer than possible | ||
| 639 | // return from parsing variable headers and let the program attempt to read the rest of the file | ||
| 640 | 5 | return; | |
| 641 | } else { | ||
| 642 | // log when reading unrecongized variable headers | ||
| 643 | 122 | dataLength -= FSEQ_VARIABLE_HEADER_SIZE; | |
| 644 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 16 times.
|
122 | if (isRecognizedStringVariableHeader(code0, code1)) { |
| 645 | // print a warning if the data is not null terminated | ||
| 646 | // this is to assist debugging potential string related issues | ||
| 647 | // the data is not forcibly null terminated to avoid mutating unknown data | ||
| 648 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
|
106 | if (header.size() < readIndex + dataLength) { |
| 649 | ✗ | LogErr(VB_SEQUENCE, "VariableHeader %c%c data exceeds header buffer size! %d > %d\n", | |
| 650 | code0, code1, (readIndex + dataLength), header.size()); | ||
| 651 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 103 times.
|
106 | } else if (header[readIndex + dataLength - 1] != '\0') { |
| 652 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LogErr(VB_SEQUENCE, "VariableHeader %c%c data is not NULL terminated!\n", code0, code1); |
| 653 | } | ||
| 654 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | } else if (!isRecognizedBinaryVariableHeader(code0, code1)) { |
| 655 | ✗ | LogDebug(VB_SEQUENCE, "Unrecognized VariableHeader code: %c%c, length: %d bytes\n", code0, code1, dataLength); | |
| 656 | } | ||
| 657 | |||
| 658 |
1/2✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
|
122 | vheader.resizeData(dataLength); |
| 659 |
1/2✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
|
122 | memcpy(vheader.getData().data(), &header[readIndex], dataLength); |
| 660 | |||
| 661 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 122 times.
✗ Branch 3 not taken.
|
122 | LogDebug(VB_SEQUENCE, "Variable Header: Code %c%c, Length: %d, Extended: %s, ExtDataOffset: %" PRIu64 "\n", vheader.code[0], vheader.code[1], vheader.getDataLength(), vheader.extendedData ? "Yes" : "No", vheader.getExtDataOffset()); |
| 662 | |||
| 663 | // advance the length of the data | ||
| 664 | // readIndex now points at the next VariableHeader's length (if any) | ||
| 665 | 122 | readIndex += dataLength; | |
| 666 | } | ||
| 667 |
1/2✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
|
130 | m_variableHeaders.push_back(vheader); |
| 668 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 5 times.
|
135 | } |
| 669 | } | ||
| 670 | 633 | void FSEQFile::finalize() { | |
| 671 | 633 | fflush(m_seqFile); | |
| 672 | 633 | } | |
| 673 | |||
| 674 | static const int V1FSEQ_HEADER_SIZE = 28; | ||
| 675 | |||
| 676 | 6 | V1FSEQFile::V1FSEQFile(const std::string& fn) : | |
| 677 | FSEQFile(fn), | ||
| 678 | 6 | m_dataBlockSize(0) { | |
| 679 | 6 | m_seqVersionMinor = V1FSEQ_MINOR_VERSION; | |
| 680 | 6 | m_seqVersionMajor = V1FSEQ_MAJOR_VERSION; | |
| 681 | 6 | } | |
| 682 | |||
| 683 | 6 | void V1FSEQFile::writeHeader() { | |
| 684 | // Cap a file-inflated count (see initializeFromFSEQ) so the header and | ||
| 685 | // the per-frame writes agree. No-op for valid files (<= channel space). | ||
| 686 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if ((uint64_t)m_seqChannelCount > MAX_CHANNEL_SPACE) { |
| 687 | ✗ | m_seqChannelCount = (uint32_t)MAX_CHANNEL_SPACE; | |
| 688 | } | ||
| 689 | // Additional file format documentation available at: | ||
| 690 | // https://github.com/FalconChristmas/fpp/blob/master/docs/FSEQ_Sequence_File_Format.txt#L1 | ||
| 691 | |||
| 692 | // Compute headerSize to include the header and variable headers | ||
| 693 | 6 | int headerSize = V1FSEQ_HEADER_SIZE; | |
| 694 | 6 | headerSize += m_variableHeaders.size() * FSEQ_VARIABLE_HEADER_SIZE; | |
| 695 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | for (auto& a : m_variableHeaders) { |
| 696 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | headerSize += a.getData().size(); |
| 697 | } | ||
| 698 | |||
| 699 | // Round to a product of 4 for better memory alignment | ||
| 700 | 6 | m_seqChanDataOffset = roundTo4Internal(headerSize); | |
| 701 | |||
| 702 | // Use m_seqChanDataOffset for buffer size to avoid additional writes or buffer allocations | ||
| 703 | // It also comes pre-memory aligned to avoid additional padding | ||
| 704 | 6 | uint8_t* header = (uint8_t*)malloc(m_seqChanDataOffset); | |
| 705 | 6 | memset(header, 0, m_seqChanDataOffset); | |
| 706 | |||
| 707 | // File identifier (PSEQ) - 4 bytes | ||
| 708 | 6 | header[0] = 'P'; | |
| 709 | 6 | header[1] = 'S'; | |
| 710 | 6 | header[2] = 'E'; | |
| 711 | 6 | header[3] = 'Q'; | |
| 712 | |||
| 713 | // Channel data start offset - 2 bytes | ||
| 714 | 6 | write2ByteUInt(&header[4], m_seqChanDataOffset); | |
| 715 | |||
| 716 | // File format version - 2 bytes | ||
| 717 | 6 | header[6] = m_seqVersionMinor; | |
| 718 | 6 | header[7] = m_seqVersionMajor; | |
| 719 | |||
| 720 | // Fixed header length - 2 bytes | ||
| 721 | // Unlike m_seqChanDataOffset this is a static value and does not include m_variableHeaders | ||
| 722 | 6 | write2ByteUInt(&header[8], V1FSEQ_HEADER_SIZE); | |
| 723 | // Channel count - 4 bytes | ||
| 724 | 6 | write4ByteUInt(&header[10], m_seqChannelCount); | |
| 725 | // Number of frames - 4 bytes | ||
| 726 | 6 | write4ByteUInt(&header[14], m_seqNumFrames); | |
| 727 | |||
| 728 | // Step time in milliseconds - 1 byte | ||
| 729 | 6 | header[18] = m_seqStepTime; | |
| 730 | // Flags (unused & reserved, should be 0) - 1 byte | ||
| 731 | 6 | header[19] = 0; | |
| 732 | |||
| 733 | // Universe count (unused by fpp, should be 0) - 2 bytes | ||
| 734 | 6 | write2ByteUInt(&header[20], 0); | |
| 735 | // Universe size (unused by fpp, should be 0) - 2 bytes | ||
| 736 | 6 | write2ByteUInt(&header[22], 0); | |
| 737 | |||
| 738 | // Gamma (unused by fpp, should be 1) - 1 byte | ||
| 739 | 6 | header[24] = 1; | |
| 740 | // Color order (unused by fpp, should be 2) - 1 byte | ||
| 741 | 6 | header[25] = 2; | |
| 742 | // Unused and reserved field(s), should be 0 - 2 bytes | ||
| 743 | 6 | header[26] = 0; | |
| 744 | 6 | header[27] = 0; | |
| 745 | |||
| 746 | 6 | int writePos = V1FSEQ_HEADER_SIZE; | |
| 747 | |||
| 748 | // Variable headers | ||
| 749 | // 4 byte size minimum (2 byte length + 2 byte code) | ||
| 750 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | for (auto& a : m_variableHeaders) { |
| 751 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | auto& data = a.getData(); |
| 752 | 3 | uint32_t len = FSEQ_VARIABLE_HEADER_SIZE + data.size(); | |
| 753 | 3 | write2ByteUInt(&header[writePos], len); | |
| 754 | 3 | header[writePos + 2] = a.code[0]; | |
| 755 | 3 | header[writePos + 3] = a.code[1]; | |
| 756 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (!data.empty()) { |
| 757 | 3 | memcpy(&header[writePos + 4], data.data(), data.size()); | |
| 758 | } | ||
| 759 | 3 | writePos += len; | |
| 760 | } | ||
| 761 | |||
| 762 | // Validate final write position matches expected channel data offset | ||
| 763 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (roundTo4Internal(writePos) != m_seqChanDataOffset) { |
| 764 | ✗ | LogErr(VB_SEQUENCE, "Final write position (%d, roundTo4 = %d) does not match channel data offset (%d)! This means the header size failed to compute an accurate buffer size.\n", writePos, roundTo4Internal(writePos), m_seqChanDataOffset); | |
| 765 | } | ||
| 766 | |||
| 767 | // Write full header at once | ||
| 768 | // header buffer is sized to the value of m_seqChanDataOffset, which comes padded for memory alignment | ||
| 769 | // If writePos extends past m_seqChanDataOffset (in error), writing m_seqChanDataOffset prevents data overflow | ||
| 770 | 6 | write(header, m_seqChanDataOffset); | |
| 771 | 6 | free(header); | |
| 772 | 6 | LogDebug(VB_SEQUENCE, "Setup for writing v1 FSEQ\n"); | |
| 773 | 6 | dumpInfo(true); | |
| 774 | 6 | } | |
| 775 | |||
| 776 | // Uncompressed channel data is frameSize bytes a frame from dataOffset, so a | ||
| 777 | // file cut short (an interrupted upload, a full disk) holds fewer frames than | ||
| 778 | // its header says. Without this every frame past the end failed its read and | ||
| 779 | // logged it -- a line a frame for the rest of the sequence. Returns the frame | ||
| 780 | // count the file can actually supply, having logged a short file once. | ||
| 781 | 2066 | static uint32_t FramesPresent(const std::string& fn, uint32_t headerFrames, uint64_t fileSize, | |
| 782 | uint64_t dataOffset, uint64_t frameSize) { | ||
| 783 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2064 times.
|
2066 | if (frameSize == 0) { |
| 784 | 2 | return headerFrames; | |
| 785 | } | ||
| 786 |
2/2✓ Branch 0 taken 2062 times.
✓ Branch 1 taken 2 times.
|
2064 | uint64_t present = fileSize > dataOffset ? (fileSize - dataOffset) / frameSize : 0; |
| 787 |
2/2✓ Branch 0 taken 2063 times.
✓ Branch 1 taken 1 times.
|
2064 | if (present >= headerFrames) { |
| 788 | 2063 | return headerFrames; | |
| 789 | } | ||
| 790 | 1 | LogErr(VB_SEQUENCE, "FSEQ file %s is truncated: its header says %u frames but it holds %" PRIu64 ". Playing those.\n", | |
| 791 | fn.c_str(), headerFrames, present); | ||
| 792 | 1 | return (uint32_t)present; | |
| 793 | } | ||
| 794 | |||
| 795 | 3 | V1FSEQFile::V1FSEQFile(const std::string& fn, FILE* file, const std::vector<uint8_t>& header) : | |
| 796 | 3 | FSEQFile(fn, file, header) { | |
| 797 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | parseVariableHeaders(header, V1FSEQ_HEADER_SIZE); |
| 798 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | m_seqNumFrames = FramesPresent(fn, m_seqNumFrames, m_seqFileSize, m_seqChanDataOffset, m_seqChannelCount); |
| 799 | |||
| 800 | // Use the last modified time for the uniqueId | ||
| 801 | struct stat stats; | ||
| 802 | 3 | fstat(fileno(file), &stats); | |
| 803 | 3 | m_uniqueId = stats.st_mtime; | |
| 804 | 3 | } | |
| 805 | |||
| 806 | 36 | V1FSEQFile::~V1FSEQFile() { | |
| 807 | 36 | } | |
| 808 | |||
| 809 | // What every frame read between two prepareRead() calls shares: the ranges | ||
| 810 | // read and a pool of channel buffers of the size they add up to. Playback | ||
| 811 | // reads frames ahead on one thread and frees them on the channel output | ||
| 812 | // thread, a frame each 10-50 ms. Allocating each buffer fresh put a free() on | ||
| 813 | // the output thread for every frame, and a 32-bit glibc serves a block over | ||
| 814 | // 512 KB with mmap, so that free was an munmap -- measured at 0.5 ms a frame, | ||
| 815 | // 2.6 ms at worst, for 600 KB frames on an AM335x -- and the next frame's read | ||
| 816 | // page-faulted its buffer back in. A frame hands its buffer back here instead. | ||
| 817 | // The frames hold the context, so a frame may outlive its file or a later | ||
| 818 | // prepareRead() on it. | ||
| 819 | class FrameReadContext { | ||
| 820 | public: | ||
| 821 | 2138 | FrameReadContext(const std::vector<std::pair<uint32_t, uint32_t>>& r, uint32_t sz) : | |
| 822 | 2138 | ranges(r), | |
| 823 | 2138 | size(sz) {} | |
| 824 | 4048 | ~FrameReadContext() { | |
| 825 |
2/2✓ Branch 0 taken 8269 times.
✓ Branch 1 taken 2024 times.
|
10293 | for (auto* b : m_free) { |
| 826 | 8269 | free(b); | |
| 827 | } | ||
| 828 | 2024 | } | |
| 829 | // A zeroed buffer of size bytes, or nullptr if none could be allocated. | ||
| 830 | 217217 | uint8_t* take() { | |
| 831 | { | ||
| 832 |
1/2✓ Branch 0 taken 217217 times.
✗ Branch 1 not taken.
|
217217 | std::lock_guard<std::mutex> lk(m_lock); |
| 833 |
2/2✓ Branch 0 taken 180099 times.
✓ Branch 1 taken 37118 times.
|
217217 | if (!m_free.empty()) { |
| 834 | 180099 | uint8_t* b = m_free.back(); | |
| 835 | 180099 | m_free.pop_back(); | |
| 836 | 180099 | memset(b, 0, size); | |
| 837 | 180099 | return b; | |
| 838 | } | ||
| 839 |
2/2✓ Branch 0 taken 37118 times.
✓ Branch 1 taken 180099 times.
|
217217 | } |
| 840 | 37118 | return (uint8_t*)calloc(1, size); | |
| 841 | } | ||
| 842 | 216983 | void give(uint8_t* b) { | |
| 843 | { | ||
| 844 |
1/2✓ Branch 0 taken 216983 times.
✗ Branch 1 not taken.
|
216983 | std::lock_guard<std::mutex> lk(m_lock); |
| 845 | // Enough for the read-ahead to keep pace with playback; a skip | ||
| 846 | // that drops the whole cache at once frees the rest. | ||
| 847 |
2/2✓ Branch 0 taken 188477 times.
✓ Branch 1 taken 28506 times.
|
216983 | if (m_free.size() < 8) { |
| 848 |
1/2✓ Branch 0 taken 188477 times.
✗ Branch 1 not taken.
|
188477 | m_free.push_back(b); |
| 849 | 188477 | return; | |
| 850 | } | ||
| 851 |
2/2✓ Branch 0 taken 28506 times.
✓ Branch 1 taken 188477 times.
|
216983 | } |
| 852 | 28506 | free(b); | |
| 853 | } | ||
| 854 | |||
| 855 | const std::vector<std::pair<uint32_t, uint32_t>> ranges; | ||
| 856 | const uint32_t size; | ||
| 857 | |||
| 858 | private: | ||
| 859 | std::mutex m_lock; | ||
| 860 | std::vector<uint8_t*> m_free; | ||
| 861 | }; | ||
| 862 | |||
| 863 | class UncompressedFrameData : public FSEQFile::FrameData { | ||
| 864 | public: | ||
| 865 | 217217 | UncompressedFrameData(uint32_t frame, std::shared_ptr<FrameReadContext> ctx) : | |
| 866 | FrameData(frame), | ||
| 867 | 434434 | m_size(ctx->size), | |
| 868 | // Zeroed: every reader returns the frame even when a seek or read | ||
| 869 | // failed part-way (a truncated file), and an unzeroed buffer put | ||
| 870 | // whatever the heap held -- or the previous frame -- on the lights. | ||
| 871 |
1/2✓ Branch 0 taken 217217 times.
✗ Branch 1 not taken.
|
217217 | m_data(ctx->take()), |
| 872 | 217217 | m_ranges(ctx->ranges), | |
| 873 | 434434 | m_ctx(std::move(ctx)) { | |
| 874 | 217217 | } | |
| 875 | // A frame with no context to share (V1 files): one of its own. | ||
| 876 | 202 | UncompressedFrameData(uint32_t frame, | |
| 877 | uint32_t sz, | ||
| 878 | 202 | const std::vector<std::pair<uint32_t, uint32_t>>& ranges) : | |
| 879 |
2/4✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
✗ Branch 3 not taken.
|
202 | UncompressedFrameData(frame, std::make_shared<FrameReadContext>(ranges, sz)) { |
| 880 | 202 | } | |
| 881 | 867932 | virtual ~UncompressedFrameData() { | |
| 882 |
1/2✓ Branch 0 taken 216983 times.
✗ Branch 1 not taken.
|
433966 | if (m_data != nullptr) { |
| 883 | 433966 | m_ctx->give(m_data); | |
| 884 | } | ||
| 885 | 867932 | } | |
| 886 | |||
| 887 | 216729 | virtual bool readFrame(uint8_t* data, uint32_t maxChannels) override { | |
| 888 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216729 times.
|
216729 | if (m_data == nullptr) |
| 889 | ✗ | return false; | |
| 890 | 216729 | uint32_t offset = 0; | |
| 891 |
2/2✓ Branch 0 taken 363651 times.
✓ Branch 1 taken 216729 times.
|
580380 | for (auto& rng : m_ranges) { |
| 892 | 363651 | uint32_t toRead = rng.second; | |
| 893 |
1/2✓ Branch 0 taken 363651 times.
✗ Branch 1 not taken.
|
363651 | if ((uint64_t)offset + toRead <= m_size) { |
| 894 | // Sparse ranges come from the file (or the caller), so a start | ||
| 895 | // past the destination must copy nothing -- the subtraction | ||
| 896 | // below would otherwise underflow unsigned and memcpy off the | ||
| 897 | // end of the channel buffer. The source offset still advances | ||
| 898 | // so later ranges stay aligned. | ||
| 899 |
2/2✓ Branch 0 taken 363623 times.
✓ Branch 1 taken 28 times.
|
363651 | if (rng.first < maxChannels) { |
| 900 | 363623 | uint32_t toCopy = std::min(toRead, maxChannels - rng.first); | |
| 901 | 363623 | memcpy(&data[rng.first], &m_data[offset], toCopy); | |
| 902 | } | ||
| 903 | 363651 | offset += toRead; | |
| 904 | } else { | ||
| 905 | ✗ | return false; | |
| 906 | } | ||
| 907 | } | ||
| 908 | 216729 | return true; | |
| 909 | } | ||
| 910 | |||
| 911 | uint32_t m_size; | ||
| 912 | uint8_t* m_data; | ||
| 913 | const std::vector<std::pair<uint32_t, uint32_t>>& m_ranges; | ||
| 914 | |||
| 915 | private: | ||
| 916 | std::shared_ptr<FrameReadContext> m_ctx; | ||
| 917 | }; | ||
| 918 | |||
| 919 | // A frame whose channel buffer could not be allocated is handed back untouched: | ||
| 920 | // readFrame() already reports false for a null buffer, whereas the read/memcpy | ||
| 921 | // paths below would write through it. malloc() failing here is not hypothetical -- | ||
| 922 | // a channel range that starts past the end of the sequence turns m_dataBlockSize | ||
| 923 | // into a multi-gigabyte request, which no controller can satisfy. | ||
| 924 | 217217 | static bool frameBufferAllocated(const UncompressedFrameData* data, uint32_t frame, uint32_t sz) { | |
| 925 |
1/2✓ Branch 0 taken 217217 times.
✗ Branch 1 not taken.
|
217217 | if (data->m_data != nullptr) { |
| 926 | 217217 | return true; | |
| 927 | } | ||
| 928 | ✗ | LogErr(VB_SEQUENCE, "Failed to allocate %u bytes of channel data for frame %d\n", sz, (int)frame); | |
| 929 | ✗ | return false; | |
| 930 | } | ||
| 931 | |||
| 932 | 2 | void V1FSEQFile::prepareRead(const std::vector<std::pair<uint32_t, uint32_t>>& ranges, uint32_t startFrame) { | |
| 933 | 2 | m_rangesToRead.clear(); | |
| 934 | 2 | m_dataBlockSize = 0; | |
| 935 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | for (auto rng : ranges) { |
| 936 | // make sure we don't read beyond the end of the sequence data. | ||
| 937 | // Skip any range that starts past the channels present in the file; without | ||
| 938 | // this guard "m_seqChannelCount - rng.first" underflows (unsigned), producing a | ||
| 939 | // huge length that later overruns a buffer. Mirrors V2FSEQFile::prepareRead. | ||
| 940 | 2 | int toRead = rng.second; | |
| 941 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (rng.first < m_seqChannelCount) { |
| 942 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if ((rng.first + toRead) > m_seqChannelCount) { |
| 943 | ✗ | toRead = m_seqChannelCount - rng.first; | |
| 944 | ✗ | rng.second = toRead; | |
| 945 | } | ||
| 946 | 2 | m_dataBlockSize += toRead; | |
| 947 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | m_rangesToRead.push_back(rng); |
| 948 | } | ||
| 949 | } | ||
| 950 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (m_dataBlockSize == 0) { |
| 951 | ✗ | m_rangesToRead.push_back(std::pair<uint32_t, uint32_t>(0, getMaxChannel())); | |
| 952 | ✗ | m_dataBlockSize = getMaxChannel(); | |
| 953 | } | ||
| 954 | 2 | FrameData* f = getFrame(startFrame); | |
| 955 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (f) { |
| 956 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | delete f; |
| 957 | } | ||
| 958 | 2 | } | |
| 959 | |||
| 960 | 202 | FrameData* V1FSEQFile::getFrame(uint32_t frame) { | |
| 961 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
|
202 | if (m_rangesToRead.empty()) { |
| 962 | ✗ | std::vector<std::pair<uint32_t, uint32_t>> range; | |
| 963 | ✗ | range.push_back(std::pair<uint32_t, uint32_t>(0, m_seqChannelCount)); | |
| 964 | ✗ | prepareRead(range, frame); | |
| 965 | ✗ | } | |
| 966 | 202 | uint64_t offset = m_seqChannelCount; | |
| 967 | 202 | offset *= frame; | |
| 968 | 202 | offset += m_seqChanDataOffset; | |
| 969 | |||
| 970 |
2/6✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 202 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
202 | UncompressedFrameData* data = new UncompressedFrameData(frame, m_dataBlockSize, m_rangesToRead); |
| 971 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
|
202 | if (!frameBufferAllocated(data, frame, m_dataBlockSize)) { |
| 972 | ✗ | return data; | |
| 973 | } | ||
| 974 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
|
202 | if (seek(offset, SEEK_SET)) { |
| 975 | ✗ | LogErr(VB_SEQUENCE, "Failed to seek to proper offset for channel data for frame %d! %" PRIu64 "\n", frame, offset); | |
| 976 | ✗ | return data; | |
| 977 | } | ||
| 978 | 202 | uint32_t sz = 0; | |
| 979 | // read the ranges into the buffer | ||
| 980 |
2/2✓ Branch 0 taken 202 times.
✓ Branch 1 taken 202 times.
|
404 | for (auto& rng : data->m_ranges) { |
| 981 |
1/2✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
|
202 | if (rng.first < m_seqChannelCount) { |
| 982 | 202 | int toRead = rng.second; | |
| 983 | 202 | uint64_t doffset = offset; | |
| 984 | 202 | doffset += rng.first; | |
| 985 |
1/2✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
|
202 | seek(doffset, SEEK_SET); |
| 986 |
1/2✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
|
202 | size_t bread = read(&data->m_data[sz], toRead); |
| 987 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
|
202 | if (bread != toRead) { |
| 988 | ✗ | LogErr(VB_SEQUENCE, "Failed to read channel data for frame %d! Needed to read %d but read %d\n", | |
| 989 | frame, toRead, (int)bread); | ||
| 990 | } | ||
| 991 | 202 | sz += toRead; | |
| 992 | } | ||
| 993 | } | ||
| 994 | 202 | return data; | |
| 995 | } | ||
| 996 | |||
| 997 | 3211 | void V1FSEQFile::addFrame(uint32_t frame, | |
| 998 | const uint8_t* data) { | ||
| 999 | 3211 | uint64_t toWrite = m_seqChannelCount; | |
| 1000 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3211 times.
|
3211 | if (toWrite > MAX_CHANNEL_SPACE) { |
| 1001 | ✗ | toWrite = MAX_CHANNEL_SPACE; | |
| 1002 | } | ||
| 1003 | 3211 | write(data, toWrite); | |
| 1004 | 3211 | } | |
| 1005 | |||
| 1006 | 6 | void V1FSEQFile::finalize() { | |
| 1007 | 6 | FSEQFile::finalize(); | |
| 1008 | 6 | } | |
| 1009 | |||
| 1010 | 1 | uint32_t V1FSEQFile::getMaxChannel() const { | |
| 1011 | 1 | return m_seqChannelCount; | |
| 1012 | } | ||
| 1013 | |||
| 1014 | static constexpr int V2FSEQ_HEADER_SIZE = 32; | ||
| 1015 | static constexpr int V2FSEQ_SPARSE_RANGE_SIZE = 6; | ||
| 1016 | static constexpr int V2FSEQ_COMPRESSION_BLOCK_SIZE = 8; | ||
| 1017 | #if !defined(NO_ZLIB) || !defined(NO_ZSTD) | ||
| 1018 | // Size of the writers' compressed output buffer, chosen from the memory | ||
| 1019 | // available. Every writer reads it through here. It used to be a static that | ||
| 1020 | // only the ZSTD handler's constructor ever filled in, so a process that wrote a | ||
| 1021 | // zlib file without having built a ZSTD handler first -- fsequtils merging | ||
| 1022 | // uncompressed or zlib inputs into zlib output -- got a 0 byte buffer, and | ||
| 1023 | // deflate(Z_FINISH), unable to make progress, spun forever. | ||
| 1024 | 3530 | static int V2OutBufferSize() { | |
| 1025 | 38 | static const int size = []() { | |
| 1026 | 38 | uint64_t pages = sysconf(_SC_PHYS_PAGES); | |
| 1027 | 38 | uint64_t page_size = sysconf(_SC_PAGESIZE); | |
| 1028 | 38 | uint64_t total_mem = pages * page_size; | |
| 1029 | // more than 1GB of RAM, use a bit larger buffer | ||
| 1030 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
|
38 | return total_mem > (1024 * 1024 * 1024) ? 32 * 1024 * 1024 : 8 * 1024 * 1024; |
| 1031 |
3/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3492 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
3530 | }(); |
| 1032 | 3530 | return size; | |
| 1033 | } | ||
| 1034 | static constexpr int V2FSEQ_OUT_BUFFER_FLUSH_SIZE = 4 * 1024 * 1024; // 50% full, flush it | ||
| 1035 | static constexpr int V2FSEQ_OUT_COMPRESSION_BLOCK_SIZE = 64 * 1024; // 64KB blocks | ||
| 1036 | #endif | ||
| 1037 | |||
| 1038 | class V2Handler { | ||
| 1039 | public: | ||
| 1040 | 2739 | V2Handler(V2FSEQFile* f) : | |
| 1041 | 2739 | m_file(f) { | |
| 1042 | 2739 | m_seqChanDataOffset = f->m_seqChanDataOffset; | |
| 1043 | 2739 | } | |
| 1044 | 5244 | virtual ~V2Handler() {} | |
| 1045 | |||
| 1046 | virtual uint8_t getCompressionType() = 0; | ||
| 1047 | virtual FrameData* getFrame(uint32_t frame) = 0; | ||
| 1048 | |||
| 1049 | 3241 | virtual uint32_t computeMaxBlocks(int max = 255) { return 0; } | |
| 1050 | virtual void addFrame(uint32_t frame, const uint8_t* data) = 0; | ||
| 1051 | virtual std::string GetType() const = 0; | ||
| 1052 | |||
| 1053 | 345668 | int seek(uint64_t location, int origin) { | |
| 1054 | 345668 | return m_file->seek(location, origin); | |
| 1055 | } | ||
| 1056 | 2235 | uint64_t tell() { | |
| 1057 | 2235 | return m_file->tell(); | |
| 1058 | } | ||
| 1059 | 374198 | uint64_t write(const void* ptr, uint64_t size) { | |
| 1060 | 374198 | return m_file->write(ptr, size); | |
| 1061 | } | ||
| 1062 | 174349 | uint64_t read(void* ptr, uint64_t size) { | |
| 1063 | 174349 | return m_file->read(ptr, size); | |
| 1064 | } | ||
| 1065 | 157 | void preload(uint64_t pos, uint64_t size) { | |
| 1066 | 157 | m_file->preload(pos, size); | |
| 1067 | 157 | } | |
| 1068 | |||
| 1069 | ✗ | virtual void prepareRead(uint32_t frame) {} | |
| 1070 | |||
| 1071 | // Shared by the frames read until the next prepareRead(). | ||
| 1072 | 217015 | std::shared_ptr<FrameReadContext> readContext() { | |
| 1073 |
5/6✓ Branch 0 taken 215079 times.
✓ Branch 1 taken 1936 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 215079 times.
✓ Branch 4 taken 1936 times.
✓ Branch 5 taken 215079 times.
|
217015 | if (!m_readContext || m_readContext->size != m_file->m_dataBlockSize) { |
| 1074 |
1/2✓ Branch 0 taken 1936 times.
✗ Branch 1 not taken.
|
1936 | m_readContext = std::make_shared<FrameReadContext>(m_file->m_rangesToRead, m_file->m_dataBlockSize); |
| 1075 | } | ||
| 1076 | 217015 | return m_readContext; | |
| 1077 | } | ||
| 1078 | std::shared_ptr<FrameReadContext> m_readContext; | ||
| 1079 | |||
| 1080 | 627 | virtual void finalize() { | |
| 1081 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 574 times.
|
627 | if (!m_file->getVariableHeaders().empty()) { |
| 1082 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 53 times.
|
106 | for (int x = 0; x < m_variableHeaderOffsets.size(); x++) { |
| 1083 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
|
53 | if (m_variableHeaderOffsets[x] != 0) { |
| 1084 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | uint64_t curEnd = tell(); |
| 1085 | 4 | auto& h = m_file->getVariableHeaders()[x]; | |
| 1086 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | auto& data = h.getData(); |
| 1087 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!data.empty()) { |
| 1088 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | write(data.data(), data.size()); |
| 1089 | } | ||
| 1090 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | size_t cur = tell(); |
| 1091 | 4 | uint64_t off = m_variableHeaderOffsets[x]; | |
| 1092 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | seek(off, SEEK_SET); |
| 1093 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | write(&curEnd, 8); |
| 1094 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | seek(cur, SEEK_SET); |
| 1095 | } | ||
| 1096 | } | ||
| 1097 | } | ||
| 1098 | 627 | } | |
| 1099 | |||
| 1100 | V2FSEQFile* m_file = nullptr; | ||
| 1101 | uint64_t m_seqChanDataOffset = 0; | ||
| 1102 | |||
| 1103 | std::vector<uint64_t> m_variableHeaderOffsets; | ||
| 1104 | }; | ||
| 1105 | |||
| 1106 | class V2NoneCompressionHandler : public V2Handler { | ||
| 1107 | public: | ||
| 1108 | 2652 | V2NoneCompressionHandler(V2FSEQFile* f) : | |
| 1109 | 2652 | V2Handler(f) {} | |
| 1110 | 10140 | virtual ~V2NoneCompressionHandler() {} | |
| 1111 | |||
| 1112 | 589 | virtual uint8_t getCompressionType() override { return 0; } | |
| 1113 | ✗ | virtual std::string GetType() const override { return "No Compression"; } | |
| 1114 | 1900 | virtual void prepareRead(uint32_t frame) override { | |
| 1115 | 1900 | FrameData* f = getFrame(frame); | |
| 1116 |
1/2✓ Branch 0 taken 1900 times.
✗ Branch 1 not taken.
|
1900 | if (f) { |
| 1117 |
1/2✓ Branch 0 taken 1900 times.
✗ Branch 1 not taken.
|
1900 | delete f; |
| 1118 | } | ||
| 1119 | 1900 | } | |
| 1120 | 172631 | virtual FrameData* getFrame(uint32_t frame) override { | |
| 1121 |
3/8✓ Branch 0 taken 172631 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 172631 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 172631 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
172631 | UncompressedFrameData* data = new UncompressedFrameData(frame, readContext()); |
| 1122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 172631 times.
|
172631 | if (!frameBufferAllocated(data, frame, m_file->m_dataBlockSize)) { |
| 1123 | ✗ | return data; | |
| 1124 | } | ||
| 1125 | 172631 | uint64_t offset = m_file->getChannelCount(); | |
| 1126 | 172631 | offset *= frame; | |
| 1127 | 172631 | offset += m_seqChanDataOffset; | |
| 1128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 172631 times.
|
172631 | if (seek(offset, SEEK_SET)) { |
| 1129 | ✗ | LogErr(VB_SEQUENCE, "Failed to seek to proper offset for channel data! %" PRIu64 "\n", offset); | |
| 1130 | ✗ | return data; | |
| 1131 | } | ||
| 1132 |
2/2✓ Branch 0 taken 171235 times.
✓ Branch 1 taken 1396 times.
|
172631 | if (m_file->m_sparseRanges.empty()) { |
| 1133 | 171235 | uint32_t sz = 0; | |
| 1134 | // read the ranges into the buffer | ||
| 1135 |
2/2✓ Branch 0 taken 171256 times.
✓ Branch 1 taken 171235 times.
|
342491 | for (auto& rng : data->m_ranges) { |
| 1136 |
1/2✓ Branch 0 taken 171256 times.
✗ Branch 1 not taken.
|
171256 | if (rng.first < m_file->getChannelCount()) { |
| 1137 | 171256 | int toRead = rng.second; | |
| 1138 | 171256 | uint64_t doffset = offset; | |
| 1139 | 171256 | doffset += rng.first; | |
| 1140 |
1/2✓ Branch 0 taken 171256 times.
✗ Branch 1 not taken.
|
171256 | seek(doffset, SEEK_SET); |
| 1141 |
1/2✓ Branch 0 taken 171256 times.
✗ Branch 1 not taken.
|
171256 | size_t bread = read(&data->m_data[sz], toRead); |
| 1142 |
2/2✓ Branch 0 taken 238 times.
✓ Branch 1 taken 171018 times.
|
171256 | if (bread != toRead) { |
| 1143 |
1/2✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
|
238 | LogErr(VB_SEQUENCE, "Failed to read channel data! Needed to read %d but read %d\n", toRead, (int)bread); |
| 1144 | } | ||
| 1145 | 171256 | sz += toRead; | |
| 1146 | } | ||
| 1147 | } | ||
| 1148 | } else { | ||
| 1149 | 1396 | size_t bread = read(data->m_data, m_file->m_dataBlockSize); | |
| 1150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1396 times.
|
1396 | if (bread != m_file->m_dataBlockSize) { |
| 1151 | ✗ | LogErr(VB_SEQUENCE, "Failed to read channel data! Needed to read %d but read %d\n", m_file->m_dataBlockSize, (int)bread); | |
| 1152 | } | ||
| 1153 | } | ||
| 1154 | 172631 | return data; | |
| 1155 | } | ||
| 1156 | 254665 | virtual void addFrame(uint32_t frame, const uint8_t* data) override { | |
| 1157 | // Sparse positions index the caller's full input buffer, NOT the packed | ||
| 1158 | // channel count (getMaxChannel extends by design), so bound them to | ||
| 1159 | // channel space. The ceiling matches every real input buffer | ||
| 1160 | // (Sequence m_seqData == FPPD_MAX_CHANNELS; fsequtils buffers are sized | ||
| 1161 | // to match) and writeHeader() normalizes to the same ceiling, so header | ||
| 1162 | // and body agree. No-op for valid files, whose ranges sit far below it. | ||
| 1163 |
2/2✓ Branch 0 taken 250864 times.
✓ Branch 1 taken 3801 times.
|
254665 | if (m_file->m_sparseRanges.empty()) { |
| 1164 | 250864 | uint64_t toWrite = m_file->getChannelCount(); | |
| 1165 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 250864 times.
|
250864 | if (toWrite > MAX_CHANNEL_SPACE) { |
| 1166 | ✗ | toWrite = MAX_CHANNEL_SPACE; | |
| 1167 | } | ||
| 1168 | 250864 | write(data, toWrite); | |
| 1169 | } else { | ||
| 1170 |
2/2✓ Branch 0 taken 118942 times.
✓ Branch 1 taken 3801 times.
|
122743 | for (auto& a : m_file->m_sparseRanges) { |
| 1171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118942 times.
|
118942 | if ((uint64_t)a.first >= MAX_CHANNEL_SPACE) { |
| 1172 | ✗ | continue; | |
| 1173 | } | ||
| 1174 | 118942 | uint64_t len = a.second; | |
| 1175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118942 times.
|
118942 | if ((uint64_t)a.first + len > MAX_CHANNEL_SPACE) { |
| 1176 | ✗ | len = MAX_CHANNEL_SPACE - a.first; | |
| 1177 | } | ||
| 1178 |
1/2✓ Branch 0 taken 118942 times.
✗ Branch 1 not taken.
|
118942 | write(&data[a.first], (uint32_t)len); |
| 1179 | } | ||
| 1180 | } | ||
| 1181 | 254665 | } | |
| 1182 | }; | ||
| 1183 | class V2CompressedHandler : public V2Handler { | ||
| 1184 | public: | ||
| 1185 | 87 | V2CompressedHandler(V2FSEQFile* f) : | |
| 1186 | V2Handler(f), | ||
| 1187 | 87 | m_maxBlocks(0), | |
| 1188 | 87 | m_curBlock(99999), | |
| 1189 | 87 | m_framesPerBlock(0), | |
| 1190 | 87 | m_curFrameInBlock(0), | |
| 1191 | 87 | m_haveReadThread(false) { | |
| 1192 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 39 times.
|
87 | if (!m_file->m_frameOffsets.empty()) { |
| 1193 | 48 | m_maxBlocks = m_file->m_frameOffsets.size() - 1; | |
| 1194 | } | ||
| 1195 | 87 | } | |
| 1196 | 174 | virtual ~V2CompressedHandler() { | |
| 1197 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 71 times.
|
174 | if (m_haveReadThread) { |
| 1198 | 32 | m_readThreadRunning = false; | |
| 1199 | 32 | m_readSignal.notify_all(); | |
| 1200 | 32 | pthread_join(m_readThread, nullptr); | |
| 1201 | } | ||
| 1202 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 87 times.
|
438 | for (auto& a : m_blockMap) { |
| 1203 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 78 times.
|
264 | if (a.second) { |
| 1204 | 108 | free(a.second); | |
| 1205 | } | ||
| 1206 | } | ||
| 1207 | 174 | m_blockMap.clear(); | |
| 1208 | 174 | } | |
| 1209 | |||
| 1210 | 124 | virtual uint32_t computeMaxBlocks(int maxNumBlocks) override { | |
| 1211 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 38 times.
|
124 | if (m_maxBlocks > 0) { |
| 1212 | 86 | return m_maxBlocks; | |
| 1213 | } | ||
| 1214 | // determine a good number of compression blocks | ||
| 1215 | 38 | uint64_t datasize = m_file->getChannelCount(); | |
| 1216 | 38 | uint64_t numFrames = m_file->getNumFrames(); | |
| 1217 | 38 | datasize *= numFrames; | |
| 1218 | 38 | uint64_t numBlocks = datasize / V2FSEQ_OUT_COMPRESSION_BLOCK_SIZE; | |
| 1219 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 33 times.
|
38 | if (numBlocks > maxNumBlocks) { |
| 1220 | // need a lot of blocks, use as many as we can | ||
| 1221 | 5 | numBlocks = maxNumBlocks; | |
| 1222 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 14 times.
|
33 | } else if (numBlocks < 1) { |
| 1223 | 19 | numBlocks = 1; | |
| 1224 | } | ||
| 1225 | 38 | m_framesPerBlock = numFrames / numBlocks; | |
| 1226 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 33 times.
|
38 | if (m_framesPerBlock < 2) |
| 1227 | 5 | m_framesPerBlock = 2; | |
| 1228 | 38 | m_curFrameInBlock = 0; | |
| 1229 | 38 | m_curBlock = 0; | |
| 1230 | |||
| 1231 | 38 | numBlocks = m_file->getNumFrames() / m_framesPerBlock + 1; | |
| 1232 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
|
40 | while (numBlocks > maxNumBlocks) { |
| 1233 | 2 | m_framesPerBlock++; | |
| 1234 | 2 | numBlocks = m_file->getNumFrames() / m_framesPerBlock + 1; | |
| 1235 | } | ||
| 1236 | // first block is going to be smaller, so add some blocks | ||
| 1237 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
|
38 | if (numBlocks < (maxNumBlocks - 1)) { |
| 1238 | 37 | numBlocks += 2; | |
| 1239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } else if (numBlocks < maxNumBlocks) { |
| 1240 | ✗ | numBlocks++; | |
| 1241 | } | ||
| 1242 | 38 | m_maxBlocks = numBlocks; | |
| 1243 | 38 | m_curBlock = 0; | |
| 1244 | 38 | return m_maxBlocks; | |
| 1245 | } | ||
| 1246 | |||
| 1247 | 38 | virtual void finalize() override { | |
| 1248 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | uint64_t lastFrame = tell(); |
| 1249 | 38 | uint64_t off = V2FSEQ_HEADER_SIZE; | |
| 1250 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | seek(off, SEEK_SET); |
| 1251 | 38 | int count = m_file->m_frameOffsets.size(); | |
| 1252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (count == 0) { |
| 1253 | // not good, count should never be 0 unless no frames were written, | ||
| 1254 | // this really should never happen, but I've seen fseq files without the | ||
| 1255 | // blocks filled in so I know it DOES happen, just haven't figured out | ||
| 1256 | // how it's possible yet. | ||
| 1257 | ✗ | LogErr(VB_SEQUENCE, "Error writing fseq file. No compressed blocks created.\n"); | |
| 1258 | |||
| 1259 | // we'll use the offset to the data for the 0 frame | ||
| 1260 | ✗ | seek(0, SEEK_SET); | |
| 1261 | uint8_t header[10]; | ||
| 1262 | ✗ | read(header, 10); | |
| 1263 | ✗ | int seqChanDataOffset = read2ByteUInt(&header[4]); | |
| 1264 | ✗ | seek(off, SEEK_SET); | |
| 1265 | ✗ | m_file->m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(0, seqChanDataOffset)); | |
| 1266 | ✗ | count++; | |
| 1267 | } | ||
| 1268 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | m_file->m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(99999999, lastFrame)); |
| 1269 |
2/2✓ Branch 0 taken 2189 times.
✓ Branch 1 taken 38 times.
|
2227 | for (int x = 0; x < count; x++) { |
| 1270 | uint8_t buf[8]; | ||
| 1271 | 2189 | uint32_t frame = m_file->m_frameOffsets[x].first; | |
| 1272 | 2189 | write4ByteUInt(buf, frame); | |
| 1273 | |||
| 1274 | 2189 | uint64_t len64 = m_file->m_frameOffsets[x + 1].second; | |
| 1275 | 2189 | len64 -= m_file->m_frameOffsets[x].second; | |
| 1276 | 2189 | uint32_t len = len64; | |
| 1277 | 2189 | write4ByteUInt(&buf[4], len); | |
| 1278 |
1/2✓ Branch 0 taken 2189 times.
✗ Branch 1 not taken.
|
2189 | write(buf, 8); |
| 1279 | // printf("%d %d: %d\n", x, frame, len); | ||
| 1280 | } | ||
| 1281 | 38 | m_file->m_frameOffsets.pop_back(); | |
| 1282 | |||
| 1283 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | seek(lastFrame, SEEK_SET); |
| 1284 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | V2Handler::finalize(); |
| 1285 | 38 | } | |
| 1286 | |||
| 1287 | // True once a subclass has taken over reading entirely (see the bulk read | ||
| 1288 | // in V2ZSTDCompressionHandler), in which case the read-ahead thread below | ||
| 1289 | // would only prefetch blocks nobody ever asks getBlock() for. | ||
| 1290 | 5 | virtual bool bulkReadActive() { return false; } | |
| 1291 | |||
| 1292 | // Index of the block holding `frame`. m_frameOffsets ends with a sentinel | ||
| 1293 | // entry past the last frame, but reaching that sentinel is not a bound we | ||
| 1294 | // can rely on: the frame number is not always ours to trust - a MultiSync | ||
| 1295 | // sync packet or a playlist start position can name a frame from a longer | ||
| 1296 | // copy of the sequence than this box holds - and the start frames it is | ||
| 1297 | // compared against come out of the file's own block table. The size of the | ||
| 1298 | // table is the bound. | ||
| 1299 | 104 | uint32_t findBlockForFrame(uint32_t frame) const { | |
| 1300 | 104 | uint32_t block = 0; | |
| 1301 | // the last entry terminates the table, so the last real block is size() - 2 | ||
| 1302 |
6/6✓ Branch 0 taken 278 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 195 times.
✓ Branch 3 taken 83 times.
✓ Branch 4 taken 195 times.
✓ Branch 5 taken 104 times.
|
299 | while ((block + 2) < m_file->m_frameOffsets.size() && frame >= m_file->m_frameOffsets[block + 1].first) { |
| 1303 | 195 | block++; | |
| 1304 | } | ||
| 1305 | 104 | return block; | |
| 1306 | } | ||
| 1307 | |||
| 1308 | 37 | virtual void prepareRead(uint32_t frame) override { | |
| 1309 |
3/4✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 22 times.
|
37 | if (bulkReadActive()) { |
| 1310 | 15 | return; | |
| 1311 | } | ||
| 1312 | // start reading the first couple blocks immediately | ||
| 1313 | 22 | int block = findBlockForFrame(frame); | |
| 1314 | |||
| 1315 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | LogDebug(VB_SEQUENCE, "Preparing to read starting frame: %d block: %d\n", frame, block); |
| 1316 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | m_blocksToRead.push_back(block); |
| 1317 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | m_blocksToRead.push_back(block + 1); |
| 1318 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | m_blocksToRead.push_back(block + 2); |
| 1319 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | m_blocksToRead.push_back(block + 3); |
| 1320 | 22 | m_firstBlock = block; | |
| 1321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (m_haveReadThread) { |
| 1322 | // Already reading ahead for this file. Starting another here | ||
| 1323 | // would leak the previous thread, which stays joinable and | ||
| 1324 | // spinning for the life of the daemon; the blocks queued above are | ||
| 1325 | // all the existing thread needs. | ||
| 1326 | ✗ | m_readSignal.notify_all(); | |
| 1327 | ✗ | return; | |
| 1328 | } | ||
| 1329 | 22 | m_readThreadRunning = true; | |
| 1330 | 22 | int rc = startReadThread(); | |
| 1331 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 16 times.
|
22 | if (rc != 0) { |
| 1332 | // Every open compressed FSEQ gets its own read-ahead thread, so a | ||
| 1333 | // box running a lot of FSEQ overlay effects at once can run the | ||
| 1334 | // process out of threads or (on 32 bit) address space. The | ||
| 1335 | // read-ahead is an optimization, not a requirement - getBlock() | ||
| 1336 | // reads inline without it - so degrade rather than fail the read. | ||
| 1337 | 6 | m_readThreadRunning = false; | |
| 1338 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | LogWarn(VB_SEQUENCE, "Could not start FSEQ read ahead thread (%s), reading blocks inline\n", strerror(rc)); |
| 1339 | } | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | // The read-ahead thread only reads and frees blocks, so it gets a small | ||
| 1343 | // stack rather than the 8 MB default. On a 32 bit player each open | ||
| 1344 | // compressed sequence - every FSEQ effect is one - otherwise reserved | ||
| 1345 | // 8 MB of address space for a thread that uses a few KB of it. | ||
| 1346 | static constexpr size_t READ_AHEAD_STACK_SIZE = 256 * 1024; | ||
| 1347 | |||
| 1348 | 22 | int startReadThread() { | |
| 1349 | pthread_attr_t attr; | ||
| 1350 | 22 | pthread_attr_init(&attr); | |
| 1351 | 22 | pthread_attr_setstacksize(&attr, READ_AHEAD_STACK_SIZE); | |
| 1352 | 22 | int rc = pthread_create(&m_readThread, &attr, readAheadMain, this); | |
| 1353 | 22 | pthread_attr_destroy(&attr); | |
| 1354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (rc == EINVAL) { |
| 1355 | // glibc carves static TLS out of the stack, and refuses a stack | ||
| 1356 | // too small to hold it; the default size always fits. | ||
| 1357 | ✗ | rc = pthread_create(&m_readThread, nullptr, readAheadMain, this); | |
| 1358 | } | ||
| 1359 | 22 | m_haveReadThread = (rc == 0); | |
| 1360 | 22 | return rc; | |
| 1361 | } | ||
| 1362 | 16 | static void* readAheadMain(void* arg) { | |
| 1363 | 16 | static_cast<V2CompressedHandler*>(arg)->readAheadLoop(); | |
| 1364 | 16 | return nullptr; | |
| 1365 | } | ||
| 1366 | 16 | void readAheadLoop() { | |
| 1367 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | SetThreadName("FSEQReadThread"); |
| 1368 |
2/2✓ Branch 0 taken 2184 times.
✓ Branch 1 taken 16 times.
|
2200 | while (m_readThreadRunning) { |
| 1369 |
1/2✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
|
2184 | std::unique_lock<std::mutex> readerlock(m_readMutex); |
| 1370 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 2028 times.
|
2184 | if (!m_blocksToRead.empty()) { |
| 1371 |
1/2✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
|
156 | readQueuedBlock(readerlock); |
| 1372 | } else { | ||
| 1373 |
2/4✓ Branch 0 taken 2028 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2028 times.
✗ Branch 3 not taken.
|
2028 | m_readSignal.wait_for(readerlock, 25ms); |
| 1374 | } | ||
| 1375 | 2184 | } | |
| 1376 | 16 | } | |
| 1377 | |||
| 1378 | // Compressed size of `block`: the buffer readQueuedBlock() allocates and | ||
| 1379 | // the input length every decoder is given. They must come from this one | ||
| 1380 | // place. They used to clamp separately -- readQueuedBlock() against | ||
| 1381 | // frames x channels in 32 bit arithmetic, which wraps for a big show, the | ||
| 1382 | // decoders against the same product in 64 bit or not at all -- and the | ||
| 1383 | // decoder read off the end of the buffer whenever they disagreed. The | ||
| 1384 | // product was never a bound on a compressed block anyway: a short, | ||
| 1385 | // noisy sequence compresses to more bytes than it has channel data, and | ||
| 1386 | // clamping it cut off a valid block. The header parse bounds the block | ||
| 1387 | // table by the file, which is the real limit. | ||
| 1388 | 3392 | uint64_t blockLength(uint32_t block) const { | |
| 1389 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3392 times.
|
3392 | if (block + 1 >= m_file->m_frameOffsets.size()) { |
| 1390 | ✗ | return 0; | |
| 1391 | } | ||
| 1392 | 3392 | return m_file->m_frameOffsets[block + 1].second - m_file->m_frameOffsets[block].second; | |
| 1393 | } | ||
| 1394 | |||
| 1395 | // Reads the block at the front of m_blocksToRead into m_blockMap. Called | ||
| 1396 | // by the read-ahead thread, and by getBlock() when that thread could not | ||
| 1397 | // be created. readerlock is held on entry and on return but is dropped | ||
| 1398 | // around the file I/O. | ||
| 1399 | 204 | void readQueuedBlock(std::unique_lock<std::mutex>& readerlock) { | |
| 1400 | 204 | int block = m_blocksToRead.front(); | |
| 1401 | 204 | m_blocksToRead.pop_front(); | |
| 1402 |
1/2✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
|
204 | uint8_t* data = m_blockMap[block]; |
| 1403 |
6/6✓ Branch 0 taken 167 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 83 times.
✓ Branch 4 taken 84 times.
✓ Branch 5 taken 120 times.
|
204 | if (!data && block < (m_file->m_frameOffsets.size() - 1)) { |
| 1404 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | readerlock.unlock(); |
| 1405 | 84 | uint64_t offset = m_file->m_frameOffsets[block].second; | |
| 1406 | 84 | uint64_t size = blockLength(block); | |
| 1407 | // At least one byte, so an empty block still reads as present | ||
| 1408 | // rather than as a block getBlock() must keep waiting for. | ||
| 1409 |
2/2✓ Branch 0 taken 82 times.
✓ Branch 1 taken 2 times.
|
84 | data = (uint8_t*)malloc(size ? size : 1); |
| 1410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (!data) { |
| 1411 | // Leave the block unread; getBlock() asks for it again. | ||
| 1412 | ✗ | LogWarn(VB_SEQUENCE, "Could not allocate %" PRIu64 " bytes for sequence block %d\n", size, block); | |
| 1413 | } else { | ||
| 1414 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | seek(offset, SEEK_SET); |
| 1415 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | uint64_t got = read(data, size); |
| 1416 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (got < size) { |
| 1417 | // Hand the decoder zeros, not whatever malloc returned. | ||
| 1418 | ✗ | LogErr(VB_SEQUENCE, "Short read of sequence block %d: wanted %" PRIu64 " bytes at %" PRIu64 ", got %" PRIu64 "\n", | |
| 1419 | block, size, offset, got); | ||
| 1420 | ✗ | memset(data + got, 0, size - got); | |
| 1421 | } | ||
| 1422 | } | ||
| 1423 | |||
| 1424 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | readerlock.lock(); |
| 1425 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | m_blockMap[block] = data; |
| 1426 | 84 | m_readSignal.notify_all(); | |
| 1427 | } | ||
| 1428 | 204 | } | |
| 1429 | |||
| 1430 | 62 | void preloadBlock(int block) { | |
| 1431 |
2/2✓ Branch 0 taken 248 times.
✓ Branch 1 taken 62 times.
|
310 | for (int b = block; b < block + 4; b++) { |
| 1432 | // let the kernel know that we'll likely need the next few blocks in the near future | ||
| 1433 |
2/2✓ Branch 0 taken 157 times.
✓ Branch 1 taken 91 times.
|
248 | if (b < m_file->m_frameOffsets.size() - 1) { |
| 1434 | 157 | uint64_t len2 = m_file->m_frameOffsets[b + 1].second; | |
| 1435 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 44 times.
|
157 | if (b < m_file->m_frameOffsets.size() - 2) { |
| 1436 | 113 | len2 = m_file->m_frameOffsets[b + 2].second; | |
| 1437 | } | ||
| 1438 | 157 | len2 -= m_file->m_frameOffsets[b].second; | |
| 1439 | 157 | uint64_t pos = m_file->m_frameOffsets[b].second; | |
| 1440 |
1/2✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
|
157 | preload(pos, len2); |
| 1441 | } | ||
| 1442 |
1/2✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
|
248 | std::unique_lock<std::mutex> readerlock(m_readMutex); |
| 1443 |
1/2✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
|
248 | m_blocksToRead.push_back(b); |
| 1444 | 248 | m_readSignal.notify_all(); | |
| 1445 | 248 | } | |
| 1446 | 62 | } | |
| 1447 | 82 | uint8_t* getBlock(int block) { | |
| 1448 |
1/2✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
|
82 | std::unique_lock<std::mutex> readerlock(m_readMutex); |
| 1449 |
1/2✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
|
82 | uint8_t* data = m_blockMap[block]; |
| 1450 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 82 times.
|
142 | while (data == nullptr) { |
| 1451 |
4/8✓ Branch 0 taken 12 times.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 60 times.
|
60 | if (m_haveReadThread && (block > (m_firstBlock + 3)) && m_firstBlock) { |
| 1452 | // if not one of the first few blocks and it's not already | ||
| 1453 | // available, then something is really slow | ||
| 1454 | ✗ | AddSlowStorageWarning(); | |
| 1455 | ✗ | LogWarn(VB_SEQUENCE, "Data block not available when needed %d/%d. First block requested: %d. Likely slow storage.\n", block, m_maxBlocks, m_firstBlock); | |
| 1456 | ✗ | LogWarn(VB_SEQUENCE, "Blocks: %d First: %d\n", m_blocksToRead.size(), m_blocksToRead.empty() ? -1 : m_blocksToRead.front()); | |
| 1457 | } | ||
| 1458 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | m_blocksToRead.push_front(block); |
| 1459 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 48 times.
|
60 | if (m_haveReadThread) { |
| 1460 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | m_readSignal.wait_for(readerlock, 10s); |
| 1461 | } else { | ||
| 1462 | // No read ahead thread, so nothing else will service the queue | ||
| 1463 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | readQueuedBlock(readerlock); |
| 1464 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
|
48 | if (m_blockMap[block] == nullptr) { |
| 1465 | // the block cannot be produced at all (past the end of the | ||
| 1466 | // file); wait like the read ahead path does rather than | ||
| 1467 | // spinning on it | ||
| 1468 | ✗ | m_readSignal.wait_for(readerlock, 10s); | |
| 1469 | } | ||
| 1470 | } | ||
| 1471 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | data = m_blockMap[block]; |
| 1472 | } | ||
| 1473 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 52 times.
|
82 | if (block > 2) { |
| 1474 | // clean up old blocks we don't need anymore | ||
| 1475 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | uint8_t* old = m_blockMap[block - 2]; |
| 1476 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | m_blockMap[block - 2] = nullptr; |
| 1477 | 30 | free(old); | |
| 1478 | } | ||
| 1479 | 82 | return data; | |
| 1480 | 82 | } | |
| 1481 | |||
| 1482 | // for compressed files, this is the compression data | ||
| 1483 | uint32_t m_framesPerBlock; | ||
| 1484 | uint32_t m_curFrameInBlock; | ||
| 1485 | uint32_t m_curBlock; | ||
| 1486 | uint32_t m_maxBlocks; | ||
| 1487 | |||
| 1488 | std::atomic_bool m_readThreadRunning; | ||
| 1489 | pthread_t m_readThread; | ||
| 1490 | std::atomic_bool m_haveReadThread; | ||
| 1491 | std::mutex m_readMutex; | ||
| 1492 | std::map<int, uint8_t*> m_blockMap; | ||
| 1493 | std::list<int> m_blocksToRead; | ||
| 1494 | std::condition_variable m_readSignal; | ||
| 1495 | int m_firstBlock = 0; | ||
| 1496 | }; | ||
| 1497 | |||
| 1498 | #ifndef NO_ZSTD | ||
| 1499 | class V2ZSTDCompressionHandler : public V2CompressedHandler { | ||
| 1500 | public: | ||
| 1501 | 74 | V2ZSTDCompressionHandler(V2FSEQFile* f) : | |
| 1502 | V2CompressedHandler(f), | ||
| 1503 | 74 | m_cctx(nullptr), | |
| 1504 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
74 | m_dctx(nullptr) { |
| 1505 | // No output buffer yet: a reader sizes it to the block it is reading | ||
| 1506 | // in getFrame(), and only a writer needs the V2OutBufferSize() staging | ||
| 1507 | // buffer (allocated by its first addFrame()). Allocating that up front | ||
| 1508 | // cost every open compressed sequence 32 MB it never used: one per FSEQ | ||
| 1509 | // effect, so a 32 bit player running a few dozen at once ran out of | ||
| 1510 | // address space. | ||
| 1511 | 74 | m_outBuffer.pos = 0; | |
| 1512 | 74 | m_outBuffer.size = 0; | |
| 1513 | 74 | m_outBuffer.dst = nullptr; | |
| 1514 | 74 | m_inBuffer.src = nullptr; | |
| 1515 | 74 | m_inBuffer.size = 0; | |
| 1516 | 74 | m_inBuffer.pos = 0; | |
| 1517 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
74 | LogDebug(VB_SEQUENCE, " Prepared to read/write a ZSTD compress fseq file.\n"); |
| 1518 | 74 | } | |
| 1519 | 296 | virtual ~V2ZSTDCompressionHandler() { | |
| 1520 | 148 | bulkShutdown(); | |
| 1521 | 148 | free(m_outBuffer.dst); | |
| 1522 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 41 times.
|
148 | if (m_cctx) { |
| 1523 | 66 | ZSTD_freeCStream(m_cctx); | |
| 1524 | } | ||
| 1525 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 58 times.
|
148 | if (m_dctx) { |
| 1526 | 32 | ZSTD_freeDStream(m_dctx); | |
| 1527 | } | ||
| 1528 | 296 | } | |
| 1529 | 33 | virtual uint8_t getCompressionType() override { return 1; } | |
| 1530 | ✗ | virtual std::string GetType() const override { return "Compressed ZSTD"; } | |
| 1531 | |||
| 1532 | // ---- ReadPattern::Bulk: decompress whole blocks ahead, in parallel ---- | ||
| 1533 | // | ||
| 1534 | // Each seekable block is an independent zstd frame, so blocks can be | ||
| 1535 | // decompressed concurrently. A caller that reads every frame front to back | ||
| 1536 | // (fseq conversion, or xLights loading a whole sequence) can therefore | ||
| 1537 | // spread the decompression across cores instead of doing it one frame at a | ||
| 1538 | // time on the calling thread. Playback does not: it wants the next frame | ||
| 1539 | // now and nothing else, which is what the streaming path below already | ||
| 1540 | // does, so ReadPattern::Streaming - the default - never comes near this. | ||
| 1541 | // | ||
| 1542 | // Blocks are held in a ring of slots indexed block % slots. The ring is | ||
| 1543 | // only ever filled over the half open range [m_bulkWindowStart, | ||
| 1544 | // m_bulkNextDispatch), which is never wider than the ring, so that index | ||
| 1545 | // cannot collide. Slot buffers and worker threads are both reused for the | ||
| 1546 | // life of the read: blocks are small and numerous (commonly a few frames | ||
| 1547 | // each, thousands per file), so allocating either per block costs far more | ||
| 1548 | // than the decompression itself on a slow ARM host. | ||
| 1549 | // | ||
| 1550 | // This reads its own blocks rather than going through the read-ahead thread | ||
| 1551 | // and getBlock(): it needs several blocks in flight at once, while getBlock | ||
| 1552 | // hands out one and frees the one two behind it. prepareRead() therefore | ||
| 1553 | // skips starting that thread when this path is active (bulkReadActive()). | ||
| 1554 | static constexpr uint64_t BULK_INFLIGHT_BYTE_BUDGET = 256ull * 1024 * 1024; | ||
| 1555 | static constexpr uint32_t NO_BLOCK = 0xFFFFFFFF; | ||
| 1556 | enum BulkSlotState { SLOT_EMPTY = 0, | ||
| 1557 | SLOT_QUEUED = 1, | ||
| 1558 | SLOT_DONE = 2 }; | ||
| 1559 | struct BulkSlot { | ||
| 1560 | uint32_t block = NO_BLOCK; | ||
| 1561 | uint32_t startFrame = 0; | ||
| 1562 | int state = SLOT_EMPTY; | ||
| 1563 | std::vector<uint8_t> comp; | ||
| 1564 | std::vector<uint8_t> out; | ||
| 1565 | size_t outSize = 0; | ||
| 1566 | }; | ||
| 1567 | |||
| 1568 | 32 | virtual bool bulkReadActive() override { | |
| 1569 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (!m_bulkConfigured) { |
| 1570 | 32 | bulkConfigure(); | |
| 1571 | } | ||
| 1572 | 32 | return m_bulkWorkers > 0; | |
| 1573 | } | ||
| 1574 | 32 | void bulkConfigure() { | |
| 1575 | 32 | m_bulkConfigured = true; | |
| 1576 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
|
32 | if (m_file->getReadPattern() != FSEQFile::ReadPattern::Bulk) { |
| 1577 | 17 | return; | |
| 1578 | } | ||
| 1579 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (m_file->m_frameOffsets.size() < 3) { |
| 1580 | ✗ | return; // one real block at most - nothing to overlap | |
| 1581 | } | ||
| 1582 | 15 | unsigned int hw = std::thread::hardware_concurrency(); | |
| 1583 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (hw <= 1) { |
| 1584 | // Nothing to win: decompressing a block on "another" core that does | ||
| 1585 | // not exist just buys a thread and some context switches. Measured | ||
| 1586 | // as a wash on a single core BeagleBone, so stay on the simpler path. | ||
| 1587 | ✗ | return; | |
| 1588 | } | ||
| 1589 | // Size a slot from the largest block the file actually contains. The | ||
| 1590 | // uncompressed side comes from the frame counts in the header and can | ||
| 1591 | // never be inferred from the compressed size: pixel data routinely | ||
| 1592 | // expands 10:1 and an all-black block far more. Each slot also keeps | ||
| 1593 | // the compressed bytes it read, and neither buffer ever shrinks, so | ||
| 1594 | // both count against the budget. | ||
| 1595 | 15 | uint32_t maxFramesPerBlock = 0; | |
| 1596 | 15 | uint64_t maxCompBytes = 0; | |
| 1597 |
2/2✓ Branch 0 taken 1613 times.
✓ Branch 1 taken 15 times.
|
1628 | for (size_t i = 0; i + 1 < m_file->m_frameOffsets.size(); i++) { |
| 1598 | 1613 | uint32_t end = m_file->m_frameOffsets[i + 1].first; | |
| 1599 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1598 times.
|
1613 | if (end > m_file->getNumFrames()) { |
| 1600 | 15 | end = m_file->getNumFrames(); | |
| 1601 | } | ||
| 1602 | 1613 | uint32_t start = m_file->m_frameOffsets[i].first; | |
| 1603 |
3/4✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 1585 times.
|
1613 | if (end > start && (end - start) > maxFramesPerBlock) { |
| 1604 | 28 | maxFramesPerBlock = end - start; | |
| 1605 | } | ||
| 1606 | 1613 | uint64_t comp = blockLength(i); | |
| 1607 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1582 times.
|
1613 | if (comp > maxCompBytes) { |
| 1608 | 31 | maxCompBytes = comp; | |
| 1609 | } | ||
| 1610 | } | ||
| 1611 | 15 | uint64_t slotBytes = (uint64_t)maxFramesPerBlock * m_file->getChannelCount() + maxCompBytes; | |
| 1612 |
2/4✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
|
15 | if (maxFramesPerBlock == 0 || slotBytes == 0) { |
| 1613 | ✗ | return; | |
| 1614 | } | ||
| 1615 | // The window is (workers + 1) slots. If even two blow the budget - a | ||
| 1616 | // legacy file with few, huge blocks - stay on the streaming path, which | ||
| 1617 | // only ever buffers one. | ||
| 1618 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (slotBytes * 2 > BULK_INFLIGHT_BYTE_BUDGET) { |
| 1619 | ✗ | LogDebug(VB_SEQUENCE, " Bulk read declined: %" PRIu64 " bytes per block exceeds the budget.\n", slotBytes); | |
| 1620 | ✗ | return; | |
| 1621 | } | ||
| 1622 | 15 | uint64_t byBudget = (BULK_INFLIGHT_BYTE_BUDGET / slotBytes) - 1; | |
| 1623 | 15 | uint64_t workers = hw < byBudget ? hw : byBudget; | |
| 1624 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (workers < 1) { |
| 1625 | ✗ | workers = 1; | |
| 1626 | } | ||
| 1627 | 15 | m_bulkWorkers = (int)workers; | |
| 1628 | 15 | m_bulkSlots.resize(m_bulkWorkers + 1); | |
| 1629 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 15 times.
|
75 | for (int i = 0; i < m_bulkWorkers; i++) { |
| 1630 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
120 | m_bulkPool.emplace_back([this]() { bulkWorker(); }); |
| 1631 | } | ||
| 1632 | 15 | LogDebug(VB_SEQUENCE, " Bulk read: %d workers, %" PRIu64 " bytes per block.\n", m_bulkWorkers, slotBytes); | |
| 1633 | } | ||
| 1634 | 60 | void bulkWorker() { | |
| 1635 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | SetThreadName("FSEQBulkDecomp"); |
| 1636 | // One decompression stream per worker, reused for every block it takes. | ||
| 1637 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | ZSTD_DStream* dctx = ZSTD_createDStream(); |
| 1638 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | std::unique_lock<std::mutex> lk(m_bulkMutex); |
| 1639 | while (true) { | ||
| 1640 |
5/6✓ Branch 0 taken 1673 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3285 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 1613 times.
✓ Branch 5 taken 1672 times.
|
5018 | m_bulkWork.wait(lk, [this]() { return m_bulkStop || !m_bulkQueue.empty(); }); |
| 1641 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1613 times.
|
1673 | if (m_bulkStop) { |
| 1642 | 60 | break; | |
| 1643 | } | ||
| 1644 | 1613 | uint32_t si = m_bulkQueue.front(); | |
| 1645 | 1613 | m_bulkQueue.pop_front(); | |
| 1646 | 1613 | BulkSlot& s = m_bulkSlots[si]; | |
| 1647 | 1613 | size_t outSize = s.outSize; | |
| 1648 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | lk.unlock(); |
| 1649 | |||
| 1650 | // A block's byte range can hold more than one zstd frame, so this | ||
| 1651 | // decompresses as a stream and stops once the frames the block | ||
| 1652 | // contributes are out. One-shot ZSTD_decompress would instead | ||
| 1653 | // insist on room for every concatenated frame in the range. | ||
| 1654 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | ZSTD_initDStream(dctx); |
| 1655 | 1613 | ZSTD_inBuffer_s in = { s.comp.data(), s.comp.size(), 0 }; | |
| 1656 | 1613 | ZSTD_outBuffer_s out = { s.out.data(), outSize, 0 }; | |
| 1657 |
3/4✓ Branch 0 taken 1807 times.
✓ Branch 1 taken 1612 times.
✓ Branch 2 taken 1807 times.
✗ Branch 3 not taken.
|
3419 | while (out.pos < outSize && in.pos < in.size) { |
| 1658 |
1/2✓ Branch 0 taken 1807 times.
✗ Branch 1 not taken.
|
1807 | size_t r = ZSTD_decompressStream(dctx, &out, &in); |
| 1659 |
3/4✓ Branch 0 taken 1807 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1806 times.
|
1807 | if (ZSTD_isError(r)) { |
| 1660 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | LogErr(VB_SEQUENCE, "Failed to decompress block %d: %s\n", (int)s.block, ZSTD_getErrorName(r)); |
| 1661 | 1 | break; | |
| 1662 | } | ||
| 1663 |
4/6✓ Branch 0 taken 1806 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 194 times.
✓ Branch 3 taken 1612 times.
✓ Branch 4 taken 194 times.
✗ Branch 5 not taken.
|
1806 | if (r == 0 && out.pos < outSize && in.pos < in.size) { |
| 1664 |
1/2✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
|
194 | ZSTD_initDStream(dctx); // on to the next concatenated frame |
| 1665 | } | ||
| 1666 | } | ||
| 1667 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1612 times.
|
1613 | if (out.pos < outSize) { |
| 1668 | // Leave no stale bytes from whatever block used this slot last. | ||
| 1669 | 1 | memset(&s.out[out.pos], 0, outSize - out.pos); | |
| 1670 | } | ||
| 1671 | |||
| 1672 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | lk.lock(); |
| 1673 | 1613 | s.state = SLOT_DONE; | |
| 1674 | 1613 | m_bulkDone.notify_all(); | |
| 1675 | 1613 | } | |
| 1676 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | ZSTD_freeDStream(dctx); |
| 1677 | 60 | } | |
| 1678 | 74 | void bulkShutdown() { | |
| 1679 |
2/2✓ Branch 0 taken 59 times.
✓ Branch 1 taken 15 times.
|
74 | if (m_bulkPool.empty()) { |
| 1680 | 59 | return; | |
| 1681 | } | ||
| 1682 | { | ||
| 1683 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | std::unique_lock<std::mutex> lk(m_bulkMutex); |
| 1684 | 15 | m_bulkStop = true; | |
| 1685 | 15 | m_bulkWork.notify_all(); | |
| 1686 | 15 | } | |
| 1687 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 15 times.
|
75 | for (auto& t : m_bulkPool) { |
| 1688 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | if (t.joinable()) { |
| 1689 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | t.join(); |
| 1690 | } | ||
| 1691 | } | ||
| 1692 | 15 | m_bulkPool.clear(); | |
| 1693 | } | ||
| 1694 | // Reads the block's compressed bytes here on the calling thread, then hands | ||
| 1695 | // the decompression to the pool. | ||
| 1696 | 1613 | void bulkDispatch(uint32_t block) { | |
| 1697 | 1613 | uint32_t si = block % m_bulkSlots.size(); | |
| 1698 | { | ||
| 1699 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | std::unique_lock<std::mutex> lk(m_bulkMutex); |
| 1700 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
3226 | m_bulkDone.wait(lk, [this, si]() { return m_bulkSlots[si].state == SLOT_EMPTY; }); |
| 1701 | 1613 | } | |
| 1702 | 1613 | BulkSlot& s = m_bulkSlots[si]; | |
| 1703 | 1613 | s.block = block; | |
| 1704 | 1613 | s.startFrame = m_file->m_frameOffsets[block].first; | |
| 1705 | 1613 | uint32_t endFrame = m_file->m_frameOffsets[block + 1].first; | |
| 1706 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1598 times.
|
1613 | if (endFrame > m_file->getNumFrames()) { |
| 1707 | 15 | endFrame = m_file->getNumFrames(); | |
| 1708 | } | ||
| 1709 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | uint32_t frames = endFrame > s.startFrame ? endFrame - s.startFrame : 0; |
| 1710 | 1613 | s.outSize = (size_t)frames * m_file->getChannelCount(); | |
| 1711 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 1525 times.
|
1613 | if (s.out.size() < s.outSize) { |
| 1712 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | s.out.resize(s.outSize); |
| 1713 | } | ||
| 1714 | |||
| 1715 | 1613 | uint64_t offset = m_file->m_frameOffsets[block].second; | |
| 1716 | 1613 | uint64_t len = blockLength(block); | |
| 1717 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | s.comp.resize(len); |
| 1718 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | seek(offset, SEEK_SET); |
| 1719 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | uint64_t bread = read(s.comp.data(), len); |
| 1720 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
|
1613 | if (bread != len) { |
| 1721 | ✗ | LogErr(VB_SEQUENCE, "Failed to read block %d! Needed %" PRIu64 " but read %" PRIu64 "\n", (int)block, len, bread); | |
| 1722 | ✗ | s.comp.resize(bread); | |
| 1723 | } | ||
| 1724 | |||
| 1725 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | std::unique_lock<std::mutex> lk(m_bulkMutex); |
| 1726 | 1613 | s.state = SLOT_QUEUED; | |
| 1727 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | m_bulkQueue.push_back(si); |
| 1728 | 1613 | m_bulkWork.notify_one(); | |
| 1729 | 1613 | } | |
| 1730 | // A block that was dispatched but skipped over may still be mid-decompress, | ||
| 1731 | // so wait for the worker to drop it before handing its buffers back out. | ||
| 1732 | 1598 | void bulkRelease(uint32_t block) { | |
| 1733 | 1598 | uint32_t si = block % m_bulkSlots.size(); | |
| 1734 |
1/2✓ Branch 0 taken 1598 times.
✗ Branch 1 not taken.
|
1598 | std::unique_lock<std::mutex> lk(m_bulkMutex); |
| 1735 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1598 times.
|
1598 | if (m_bulkSlots[si].block != block) { |
| 1736 | ✗ | return; | |
| 1737 | } | ||
| 1738 |
1/2✓ Branch 0 taken 1598 times.
✗ Branch 1 not taken.
|
3196 | m_bulkDone.wait(lk, [this, si]() { return m_bulkSlots[si].state != SLOT_QUEUED; }); |
| 1739 | 1598 | m_bulkSlots[si].state = SLOT_EMPTY; | |
| 1740 | 1598 | m_bulkSlots[si].block = NO_BLOCK; | |
| 1741 | 1598 | m_bulkDone.notify_all(); | |
| 1742 |
1/2✓ Branch 0 taken 1598 times.
✗ Branch 1 not taken.
|
1598 | } |
| 1743 | ✗ | void bulkDrain() { | |
| 1744 | ✗ | std::unique_lock<std::mutex> lk(m_bulkMutex); | |
| 1745 | ✗ | for (auto& s : m_bulkSlots) { | |
| 1746 | ✗ | m_bulkDone.wait(lk, [&s]() { return s.state != SLOT_QUEUED; }); | |
| 1747 | ✗ | s.state = SLOT_EMPTY; | |
| 1748 | ✗ | s.block = NO_BLOCK; | |
| 1749 | } | ||
| 1750 | ✗ | m_bulkCurBlock = NO_BLOCK; | |
| 1751 | ✗ | } | |
| 1752 | 42314 | BulkSlot* bulkGetBlock(uint32_t block) { | |
| 1753 | 42314 | uint32_t maxBlock = m_file->m_frameOffsets.size() - 1; | |
| 1754 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42314 times.
|
42314 | if (block >= maxBlock) { |
| 1755 | ✗ | return nullptr; | |
| 1756 | } | ||
| 1757 |
2/2✓ Branch 0 taken 40701 times.
✓ Branch 1 taken 1613 times.
|
42314 | if (block == m_bulkCurBlock) { |
| 1758 | 40701 | return &m_bulkSlots[block % m_bulkSlots.size()]; // still held - no locking | |
| 1759 | } | ||
| 1760 |
2/4✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1613 times.
|
1613 | if (block < m_bulkWindowStart || block > m_bulkNextDispatch) { |
| 1761 | ✗ | bulkDrain(); // seek outside the window: restart the pipeline here | |
| 1762 | ✗ | m_bulkWindowStart = m_bulkNextDispatch = block; | |
| 1763 | } | ||
| 1764 |
2/2✓ Branch 0 taken 1598 times.
✓ Branch 1 taken 1613 times.
|
3211 | while (m_bulkWindowStart < block) { |
| 1765 |
1/2✓ Branch 0 taken 1598 times.
✗ Branch 1 not taken.
|
1598 | bulkRelease(m_bulkWindowStart); |
| 1766 | 1598 | m_bulkWindowStart++; | |
| 1767 | } | ||
| 1768 | 1613 | uint64_t limit = (uint64_t)block + m_bulkSlots.size(); | |
| 1769 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1553 times.
|
1613 | if (limit > maxBlock) { |
| 1770 | 60 | limit = maxBlock; | |
| 1771 | } | ||
| 1772 |
2/2✓ Branch 0 taken 1613 times.
✓ Branch 1 taken 1613 times.
|
3226 | while (m_bulkNextDispatch < limit) { |
| 1773 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | bulkDispatch(m_bulkNextDispatch++); |
| 1774 | } | ||
| 1775 | 1613 | uint32_t si = block % m_bulkSlots.size(); | |
| 1776 |
1/2✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
|
1613 | std::unique_lock<std::mutex> lk(m_bulkMutex); |
| 1777 |
4/6✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1613 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 1613 times.
✗ Branch 5 not taken.
|
3230 | m_bulkDone.wait(lk, [this, si, block]() { return m_bulkSlots[si].state == SLOT_DONE && m_bulkSlots[si].block == block; }); |
| 1778 | 1613 | m_bulkCurBlock = block; | |
| 1779 | 1613 | return &m_bulkSlots[si]; | |
| 1780 | 1613 | } | |
| 1781 | 42314 | FrameData* getFrameBulk(uint32_t frame) { | |
| 1782 | // Resume from the block already in hand - a bulk caller walks the frames | ||
| 1783 | // in order, so this is almost always either that block or the next one. | ||
| 1784 | 42314 | uint32_t block = m_bulkCurBlock; | |
| 1785 |
5/6✓ Branch 0 taken 42299 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 42299 times.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 42299 times.
|
42314 | if (block == NO_BLOCK || frame < m_file->m_frameOffsets[block].first) { |
| 1786 | 15 | block = 0; | |
| 1787 | } | ||
| 1788 |
5/6✓ Branch 0 taken 43912 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1598 times.
✓ Branch 3 taken 42314 times.
✓ Branch 4 taken 1598 times.
✓ Branch 5 taken 42314 times.
|
43912 | while (block + 1 < m_file->m_frameOffsets.size() && frame >= m_file->m_frameOffsets[block + 1].first) { |
| 1789 | 1598 | block++; | |
| 1790 | } | ||
| 1791 | 42314 | BulkSlot* b = bulkGetBlock(block); | |
| 1792 |
3/8✓ Branch 0 taken 42314 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42314 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42314 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
42314 | UncompressedFrameData* data = new UncompressedFrameData(frame, readContext()); |
| 1793 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42314 times.
|
42314 | if (!frameBufferAllocated(data, frame, m_file->m_dataBlockSize)) { |
| 1794 | ✗ | return data; | |
| 1795 | } | ||
| 1796 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42314 times.
|
42314 | if (b == nullptr) { |
| 1797 | ✗ | LogErr(VB_SEQUENCE, "Failed to get block %d for frame %d\n", (int)block, (int)frame); | |
| 1798 | ✗ | return data; | |
| 1799 | } | ||
| 1800 | 42314 | uint64_t fidx = (uint64_t)(frame - b->startFrame) * m_file->getChannelCount(); | |
| 1801 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42314 times.
|
42314 | if ((fidx + m_file->getChannelCount()) > b->outSize) { |
| 1802 | ✗ | LogErr(VB_SEQUENCE, "Frame %d falls outside block %d\n", (int)frame, (int)block); | |
| 1803 | ✗ | return data; | |
| 1804 | } | ||
| 1805 | 42314 | const uint8_t* fdata = b->out.data(); | |
| 1806 |
2/2✓ Branch 0 taken 3001 times.
✓ Branch 1 taken 39313 times.
|
42314 | if (!m_file->m_sparseRanges.empty()) { |
| 1807 | 3001 | memcpy(data->m_data, &fdata[fidx], m_file->getChannelCount()); | |
| 1808 | } else { | ||
| 1809 | 39313 | uint32_t sz = 0; | |
| 1810 | // read the ranges into the buffer | ||
| 1811 |
2/2✓ Branch 0 taken 42314 times.
✓ Branch 1 taken 39313 times.
|
81627 | for (auto& rng : data->m_ranges) { |
| 1812 |
1/2✓ Branch 0 taken 42314 times.
✗ Branch 1 not taken.
|
42314 | if (rng.first < m_file->getChannelCount()) { |
| 1813 | 42314 | uint64_t start = fidx + rng.first; | |
| 1814 | 42314 | memcpy(&data->m_data[sz], &fdata[start], rng.second); | |
| 1815 | 42314 | sz += rng.second; | |
| 1816 | } | ||
| 1817 | } | ||
| 1818 | } | ||
| 1819 | 42314 | return data; | |
| 1820 | } | ||
| 1821 | |||
| 1822 | 43704 | virtual FrameData* getFrame(uint32_t frame) override { | |
| 1823 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43704 times.
|
43704 | if (!m_bulkConfigured) { |
| 1824 | ✗ | bulkConfigure(); | |
| 1825 | } | ||
| 1826 |
2/2✓ Branch 0 taken 42314 times.
✓ Branch 1 taken 1390 times.
|
43704 | if (m_bulkWorkers > 0) { |
| 1827 | 42314 | return getFrameBulk(frame); | |
| 1828 | } | ||
| 1829 |
7/8✓ Branch 0 taken 1374 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1374 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 1337 times.
✓ Branch 6 taken 53 times.
✓ Branch 7 taken 1337 times.
|
1390 | if ((m_curBlock + 1) >= m_file->m_frameOffsets.size() || (frame < m_file->m_frameOffsets[m_curBlock].first) || (frame >= m_file->m_frameOffsets[m_curBlock + 1].first)) { |
| 1830 | // frame is not in the current block | ||
| 1831 | 53 | m_curBlock = findBlockForFrame(frame); | |
| 1832 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 37 times.
|
53 | if (m_dctx == nullptr) { |
| 1833 | 16 | m_dctx = ZSTD_createDStream(); | |
| 1834 | } | ||
| 1835 | 53 | ZSTD_initDStream(m_dctx); | |
| 1836 | |||
| 1837 | 53 | m_inBuffer.pos = 0; | |
| 1838 | 53 | m_inBuffer.size = blockLength(m_curBlock); | |
| 1839 | |||
| 1840 | 53 | m_inBuffer.src = getBlock(m_curBlock); | |
| 1841 | |||
| 1842 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 15 times.
|
53 | if (m_curBlock < m_file->m_frameOffsets.size() - 2) { |
| 1843 | // let the kernel know that we'll likely need the next block in the near future | ||
| 1844 | 38 | preloadBlock(m_curBlock + 1); | |
| 1845 | } | ||
| 1846 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 37 times.
|
53 | uint32_t blockEnd = m_file->m_frameOffsets[m_curBlock + 1].first > m_file->getNumFrames() ? m_file->getNumFrames() : m_file->m_frameOffsets[m_curBlock + 1].first; |
| 1847 | 53 | uint32_t blockStart = m_file->m_frameOffsets[m_curBlock].first; | |
| 1848 |
1/2✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
|
53 | m_framesPerBlock = blockEnd > blockStart ? blockEnd - blockStart : 0; |
| 1849 | 53 | uint64_t needed = (uint64_t)m_framesPerBlock * m_file->getChannelCount(); | |
| 1850 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 29 times.
|
53 | if (needed > m_outBufferCapacity) { |
| 1851 | 24 | free(m_outBuffer.dst); | |
| 1852 | 24 | m_outBuffer.dst = malloc(needed); | |
| 1853 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | m_outBufferCapacity = m_outBuffer.dst ? needed : 0; |
| 1854 | } | ||
| 1855 | 53 | m_outBuffer.size = 0; | |
| 1856 | 53 | m_outBuffer.pos = 0; | |
| 1857 | 53 | m_curFrameInBlock = 0; | |
| 1858 | } | ||
| 1859 |
3/8✓ Branch 0 taken 1390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1390 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1390 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
1390 | UncompressedFrameData* data = new UncompressedFrameData(frame, readContext()); |
| 1860 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1390 times.
|
1390 | if (!frameBufferAllocated(data, frame, m_file->m_dataBlockSize)) { |
| 1861 | ✗ | return data; | |
| 1862 | } | ||
| 1863 | |||
| 1864 | 1390 | uint32_t blockStart = m_file->m_frameOffsets[m_curBlock].first; | |
| 1865 |
1/2✓ Branch 0 taken 1390 times.
✗ Branch 1 not taken.
|
1390 | uint64_t frameEnd = frame < blockStart ? 0 : ((uint64_t)(frame - blockStart) + 1) * m_file->getChannelCount(); |
| 1866 |
3/6✓ Branch 0 taken 1390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1390 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1390 times.
|
1390 | if (m_outBuffer.dst == nullptr || frame < blockStart || frameEnd > m_outBufferCapacity) { |
| 1867 | // The block table did not put this frame where it said it would, or | ||
| 1868 | // the buffer for the block could not be allocated. Either way the | ||
| 1869 | // frame lies outside the decompressed block, which is what the old | ||
| 1870 | // "fidx < 0" test here was reaching for - but fidx is unsigned, so | ||
| 1871 | // it could never fire, and the read ran off the buffer instead. | ||
| 1872 | ✗ | LogErr(VB_SEQUENCE, "Frame %d is not within block %d (frames %d to %d). Aborting frame load.\n", | |
| 1873 | (int)frame, (int)m_curBlock, (int)blockStart, (int)(blockStart + m_framesPerBlock)); | ||
| 1874 | ✗ | return data; | |
| 1875 | } | ||
| 1876 | 1390 | uint32_t frameInBlock = frame - blockStart; | |
| 1877 |
1/2✓ Branch 0 taken 1390 times.
✗ Branch 1 not taken.
|
1390 | if (frameInBlock >= m_curFrameInBlock) { |
| 1878 | 1390 | m_outBuffer.size = frameEnd; | |
| 1879 | 1390 | size_t zr = ZSTD_decompressStream(m_dctx, &m_outBuffer, &m_inBuffer); | |
| 1880 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1390 times.
|
1390 | if (ZSTD_isError(zr)) { |
| 1881 | // The stream is unusable after an error (zstd.h), so don't feed | ||
| 1882 | // it again: blank the rest of the block and treat every frame | ||
| 1883 | // in it as decoded. The next block re-initializes the stream. | ||
| 1884 | ✗ | LogErr(VB_SEQUENCE, "Corrupt compressed data in sequence block %d at frame %d: %s\n", | |
| 1885 | (int)m_curBlock, (int)frame, ZSTD_getErrorName(zr)); | ||
| 1886 | ✗ | uint64_t blockBytes = (uint64_t)m_framesPerBlock * m_file->getChannelCount(); | |
| 1887 | ✗ | if (m_outBuffer.pos < blockBytes) { | |
| 1888 | ✗ | memset((uint8_t*)m_outBuffer.dst + m_outBuffer.pos, 0, blockBytes - m_outBuffer.pos); | |
| 1889 | } | ||
| 1890 | ✗ | m_curFrameInBlock = m_framesPerBlock; | |
| 1891 | } else { | ||
| 1892 | 1390 | m_curFrameInBlock = frameInBlock + 1; | |
| 1893 | } | ||
| 1894 | } | ||
| 1895 | |||
| 1896 | 1390 | uint64_t fidx = (uint64_t)frameInBlock * m_file->getChannelCount(); | |
| 1897 | 1390 | uint8_t* fdata = (uint8_t*)m_outBuffer.dst; | |
| 1898 | |||
| 1899 |
2/2✓ Branch 0 taken 320 times.
✓ Branch 1 taken 1070 times.
|
1390 | if (!m_file->m_sparseRanges.empty()) { |
| 1900 | 320 | memcpy(data->m_data, &fdata[fidx], m_file->getChannelCount()); | |
| 1901 | } else { | ||
| 1902 | 1070 | uint32_t sz = 0; | |
| 1903 | // read the ranges into the buffer | ||
| 1904 |
2/2✓ Branch 0 taken 1070 times.
✓ Branch 1 taken 1070 times.
|
2140 | for (auto& rng : data->m_ranges) { |
| 1905 |
1/2✓ Branch 0 taken 1070 times.
✗ Branch 1 not taken.
|
1070 | if (rng.first < m_file->getChannelCount()) { |
| 1906 | 1070 | uint64_t start = fidx + rng.first; | |
| 1907 | 1070 | memcpy(&data->m_data[sz], &fdata[start], rng.second); | |
| 1908 | 1070 | sz += rng.second; | |
| 1909 | } | ||
| 1910 | } | ||
| 1911 | } | ||
| 1912 | 1390 | return data; | |
| 1913 | } | ||
| 1914 | // Feeds all of `input` to the stream. A zstd error consumes nothing and | ||
| 1915 | // leaves the stream unusable, so stop on one rather than spin forever. | ||
| 1916 | 144854 | void compressData(ZSTD_CStream* m_cctx, ZSTD_inBuffer_s& input, ZSTD_outBuffer_s& output) { | |
| 1917 |
2/2✓ Branch 0 taken 144854 times.
✓ Branch 1 taken 144834 times.
|
289688 | while (input.pos < input.size) { |
| 1918 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144854 times.
|
144854 | if (output.pos > V2FSEQ_OUT_BUFFER_FLUSH_SIZE) { |
| 1919 | ✗ | write(output.dst, output.pos); | |
| 1920 | ✗ | output.pos = 0; | |
| 1921 | } | ||
| 1922 | 144854 | size_t zr = ZSTD_compressStream2(m_cctx, &output, &input, ZSTD_e_continue); | |
| 1923 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 144834 times.
|
144854 | if (ZSTD_isError(zr)) { |
| 1924 | 20 | LogErr(VB_SEQUENCE, "zstd could not compress sequence block %d: %s\n", (int)m_curBlock, ZSTD_getErrorName(zr)); | |
| 1925 | 20 | return; | |
| 1926 | } | ||
| 1927 | } | ||
| 1928 | } | ||
| 1929 | // Ends the current zstd frame. ZSTD_compressStream2 returns the bytes | ||
| 1930 | // still to flush, but its error codes are also non-zero, so an unchecked | ||
| 1931 | // "> 0" loop never terminates on an error. | ||
| 1932 | 2086 | void endBlock() { | |
| 1933 | 2086 | ZSTD_inBuffer_s input = { | |
| 1934 | 0, 0, 0 | ||
| 1935 | }; | ||
| 1936 | size_t zr; | ||
| 1937 |
3/4✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2084 times.
|
2086 | while ((zr = ZSTD_compressStream2(m_cctx, &m_outBuffer, &input, ZSTD_e_end)) > 0) { |
| 1938 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (ZSTD_isError(zr)) { |
| 1939 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | LogErr(VB_SEQUENCE, "zstd could not finish sequence block %d: %s\n", (int)m_curBlock, ZSTD_getErrorName(zr)); |
| 1940 | 2 | break; | |
| 1941 | } | ||
| 1942 | ✗ | write(m_outBuffer.dst, m_outBuffer.pos); | |
| 1943 | ✗ | m_outBuffer.pos = 0; | |
| 1944 | } | ||
| 1945 |
1/2✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
|
2086 | write(m_outBuffer.dst, m_outBuffer.pos); |
| 1946 | 2086 | m_outBuffer.pos = 0; | |
| 1947 | 2086 | } | |
| 1948 | 102872 | virtual void addFrame(uint32_t frame, const uint8_t* data) override { | |
| 1949 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 102839 times.
|
102872 | if (m_cctx == nullptr) { |
| 1950 | 33 | m_cctx = ZSTD_createCStream(); | |
| 1951 | } | ||
| 1952 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 102839 times.
|
102872 | if (m_outBuffer.dst == nullptr) { |
| 1953 | 33 | m_outBuffer.dst = malloc(V2OutBufferSize()); | |
| 1954 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (m_outBuffer.dst == nullptr) { |
| 1955 | ✗ | LogErr(VB_SEQUENCE, "Could not allocate the %d byte zstd output buffer\n", V2OutBufferSize()); | |
| 1956 | ✗ | return; | |
| 1957 | } | ||
| 1958 | 33 | m_outBuffer.size = V2OutBufferSize(); | |
| 1959 | 33 | m_outBufferCapacity = m_outBuffer.size; | |
| 1960 | 33 | m_outBuffer.pos = 0; | |
| 1961 | } | ||
| 1962 |
2/2✓ Branch 0 taken 2086 times.
✓ Branch 1 taken 100786 times.
|
102872 | if (m_curFrameInBlock == 0) { |
| 1963 |
1/2✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
|
2086 | uint64_t offset = tell(); |
| 1964 | // LogDebug(VB_SEQUENCE, " Preparing to create a compressed block of data starting at frame %d, offset %" PRIu64 ".\n", frame, offset); | ||
| 1965 |
1/2✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
|
2086 | m_file->m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(frame, offset)); |
| 1966 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2082 times.
|
2086 | int clevel = m_file->m_compressionLevel == -99 ? 2 : m_file->m_compressionLevel; |
| 1967 |
2/4✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2086 times.
|
2086 | if (clevel < -25 || clevel > 25) { |
| 1968 | ✗ | clevel = 2; | |
| 1969 | } | ||
| 1970 |
6/8✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2053 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✓ Branch 7 taken 2053 times.
|
2086 | if (frame == 0 && (ZSTD_versionNumber() > 10305)) { |
| 1971 | // first frame needs to be grabbed as fast as possible | ||
| 1972 | // or remotes may be off by a few frames at start. Thus, | ||
| 1973 | // if using recent zstd, we'll use the negative levels | ||
| 1974 | // for the first block so the decompression can | ||
| 1975 | // be as fast as possible | ||
| 1976 | 33 | clevel = -10; | |
| 1977 | } | ||
| 1978 |
3/8✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2086 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2086 times.
|
2086 | if (ZSTD_versionNumber() <= 10305 && clevel < 0) { |
| 1979 | ✗ | clevel = 0; | |
| 1980 | } | ||
| 1981 |
1/2✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
|
2086 | ZSTD_initCStream(m_cctx, clevel); |
| 1982 | } | ||
| 1983 | |||
| 1984 | 102872 | uint8_t* curData = (uint8_t*)data; | |
| 1985 |
2/2✓ Branch 0 taken 99651 times.
✓ Branch 1 taken 3221 times.
|
102872 | if (m_file->m_sparseRanges.empty()) { |
| 1986 | 99651 | uint64_t toCompress = m_file->getChannelCount(); | |
| 1987 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 99651 times.
|
99651 | if (toCompress > MAX_CHANNEL_SPACE) { |
| 1988 | ✗ | toCompress = MAX_CHANNEL_SPACE; | |
| 1989 | } | ||
| 1990 | 99651 | ZSTD_inBuffer_s input = { | |
| 1991 | curData, | ||
| 1992 | (size_t)toCompress, | ||
| 1993 | 0 | ||
| 1994 | 99651 | }; | |
| 1995 |
1/2✓ Branch 0 taken 99651 times.
✗ Branch 1 not taken.
|
99651 | compressData(m_cctx, input, m_outBuffer); |
| 1996 | } else { | ||
| 1997 |
2/2✓ Branch 0 taken 45203 times.
✓ Branch 1 taken 3221 times.
|
48424 | for (auto& a : m_file->m_sparseRanges) { |
| 1998 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45203 times.
|
45203 | if ((uint64_t)a.first >= MAX_CHANNEL_SPACE) { |
| 1999 | ✗ | continue; | |
| 2000 | } | ||
| 2001 | 45203 | uint64_t len = a.second; | |
| 2002 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45203 times.
|
45203 | if ((uint64_t)a.first + len > MAX_CHANNEL_SPACE) { |
| 2003 | ✗ | len = MAX_CHANNEL_SPACE - a.first; | |
| 2004 | } | ||
| 2005 | 45203 | ZSTD_inBuffer_s input = { | |
| 2006 | 45203 | &curData[a.first], | |
| 2007 | (size_t)len, | ||
| 2008 | 0 | ||
| 2009 | 45203 | }; | |
| 2010 |
1/2✓ Branch 0 taken 45203 times.
✗ Branch 1 not taken.
|
45203 | compressData(m_cctx, input, m_outBuffer); |
| 2011 | } | ||
| 2012 | } | ||
| 2013 | |||
| 2014 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 102869 times.
|
102872 | if (m_outBuffer.pos > V2FSEQ_OUT_BUFFER_FLUSH_SIZE) { |
| 2015 | // buffer is getting full, better flush it | ||
| 2016 | 3 | write(m_outBuffer.dst, m_outBuffer.pos); | |
| 2017 | 3 | m_outBuffer.pos = 0; | |
| 2018 | } | ||
| 2019 | |||
| 2020 | 102872 | m_curFrameInBlock++; | |
| 2021 | // if we hit the max per block OR we're in the first block and hit frame #10 | ||
| 2022 | // we'll start a new block. We want the first block to be small so startup is | ||
| 2023 | // quicker and we can get the first few frames as fast as possible. | ||
| 2024 |
9/10✓ Branch 0 taken 291 times.
✓ Branch 1 taken 102581 times.
✓ Branch 2 taken 263 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 2028 times.
✓ Branch 5 taken 100816 times.
✓ Branch 6 taken 2028 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2056 times.
✓ Branch 9 taken 100816 times.
|
102872 | if ((m_curBlock == 0 && m_curFrameInBlock == 10) || (m_curFrameInBlock >= m_framesPerBlock && m_file->m_frameOffsets.size() < m_maxBlocks)) { |
| 2025 | 2056 | endBlock(); | |
| 2026 | // LogDebug(VB_SEQUENCE, " Finalized block of data ending at frame %d. Frames in block: %d.\n", frame, m_curFrameInBlock); | ||
| 2027 | 2056 | m_curFrameInBlock = 0; | |
| 2028 | 2056 | m_curBlock++; | |
| 2029 | } | ||
| 2030 | } | ||
| 2031 | 33 | virtual void finalize() override { | |
| 2032 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3 times.
|
33 | if (m_curFrameInBlock) { |
| 2033 | 30 | endBlock(); | |
| 2034 | 30 | LogDebug(VB_SEQUENCE, " Finalized last block of data. Frames in block: %d.\n", m_curFrameInBlock); | |
| 2035 | 30 | m_curFrameInBlock = 0; | |
| 2036 | 30 | m_curBlock++; | |
| 2037 | } | ||
| 2038 | 33 | V2CompressedHandler::finalize(); | |
| 2039 | 33 | } | |
| 2040 | |||
| 2041 | ZSTD_CCtx* m_cctx = nullptr; | ||
| 2042 | ZSTD_DStream* m_dctx = nullptr; | ||
| 2043 | |||
| 2044 | // Block-parallel read state (see bulkConfigure). m_bulkWorkers == 0 selects | ||
| 2045 | // the serial streaming path, which is what ReadPattern::Streaming - the | ||
| 2046 | // default, and all playback - always gets. | ||
| 2047 | bool m_bulkConfigured = false; | ||
| 2048 | int m_bulkWorkers = 0; | ||
| 2049 | std::vector<BulkSlot> m_bulkSlots; | ||
| 2050 | std::vector<std::thread> m_bulkPool; | ||
| 2051 | std::deque<uint32_t> m_bulkQueue; // slot indices waiting to be decompressed | ||
| 2052 | std::mutex m_bulkMutex; | ||
| 2053 | std::condition_variable m_bulkWork; // wakes the workers | ||
| 2054 | std::condition_variable m_bulkDone; // wakes the caller | ||
| 2055 | bool m_bulkStop = false; | ||
| 2056 | uint32_t m_bulkNextDispatch = 0; | ||
| 2057 | uint32_t m_bulkWindowStart = 0; | ||
| 2058 | uint32_t m_bulkCurBlock = NO_BLOCK; | ||
| 2059 | ZSTD_outBuffer_s m_outBuffer; | ||
| 2060 | ZSTD_inBuffer_s m_inBuffer; | ||
| 2061 | // What m_outBuffer.dst can actually hold. m_outBuffer.size is the limit | ||
| 2062 | // handed to zstd for one frame and moves with every frame read, so it | ||
| 2063 | // cannot answer "does this fit". | ||
| 2064 | size_t m_outBufferCapacity = 0; | ||
| 2065 | }; | ||
| 2066 | #endif | ||
| 2067 | |||
| 2068 | #ifndef NO_ZLIB | ||
| 2069 | class V2ZLIBCompressionHandler : public V2CompressedHandler { | ||
| 2070 | public: | ||
| 2071 | 13 | V2ZLIBCompressionHandler(V2FSEQFile* f) : | |
| 2072 | V2CompressedHandler(f), | ||
| 2073 | 13 | m_stream(nullptr), | |
| 2074 | 13 | m_outBuffer(nullptr), | |
| 2075 | 13 | m_inBuffer(nullptr) { | |
| 2076 | 13 | } | |
| 2077 | 52 | virtual ~V2ZLIBCompressionHandler() { | |
| 2078 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3 times.
|
26 | if (m_outBuffer) { |
| 2079 | 20 | free(m_outBuffer); | |
| 2080 | } | ||
| 2081 | // Only a writer still holds a stream here: the read side ends and | ||
| 2082 | // frees its inflate stream after every block. The writer's last | ||
| 2083 | // block was finished by finalize() but its deflate state never ended. | ||
| 2084 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
|
26 | if (m_stream) { |
| 2085 | 10 | deflateEnd(m_stream); | |
| 2086 | 10 | free(m_stream); | |
| 2087 | } | ||
| 2088 | 52 | } | |
| 2089 | 5 | virtual uint8_t getCompressionType() override { return 2; } | |
| 2090 | ✗ | virtual std::string GetType() const override { return "Compressed ZLIB"; } | |
| 2091 | |||
| 2092 | 680 | virtual FrameData* getFrame(uint32_t frame) override { | |
| 2093 |
7/8✓ Branch 0 taken 675 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 675 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 651 times.
✓ Branch 6 taken 29 times.
✓ Branch 7 taken 651 times.
|
680 | if ((m_curBlock + 1) >= m_file->m_frameOffsets.size() || (frame < m_file->m_frameOffsets[m_curBlock].first) || (frame >= m_file->m_frameOffsets[m_curBlock + 1].first)) { |
| 2094 | // frame is not in the current block | ||
| 2095 | 29 | m_curBlock = findBlockForFrame(frame); | |
| 2096 | |||
| 2097 | 29 | uint64_t len = blockLength(m_curBlock); | |
| 2098 | 29 | m_inBuffer = getBlock(m_curBlock); | |
| 2099 | |||
| 2100 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 5 times.
|
29 | if (m_curBlock < m_file->m_frameOffsets.size() - 2) { |
| 2101 | // let the kernel know that we'll likely need the next block in the near future | ||
| 2102 | 24 | preloadBlock(m_curBlock + 1); | |
| 2103 | } | ||
| 2104 | |||
| 2105 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | if (m_stream == nullptr) { |
| 2106 | 29 | m_stream = (z_stream*)calloc(1, sizeof(z_stream)); | |
| 2107 | 29 | m_stream->next_in = m_inBuffer; | |
| 2108 | 29 | m_stream->avail_in = len; | |
| 2109 | 29 | inflateInit(m_stream); | |
| 2110 | } | ||
| 2111 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 5 times.
|
29 | if (m_outBuffer != nullptr) { |
| 2112 | 24 | free(m_outBuffer); | |
| 2113 | } | ||
| 2114 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 24 times.
|
29 | uint32_t blockEnd = m_file->m_frameOffsets[m_curBlock + 1].first > m_file->getNumFrames() ? m_file->getNumFrames() : m_file->m_frameOffsets[m_curBlock + 1].first; |
| 2115 | 29 | uint32_t blockStart = m_file->m_frameOffsets[m_curBlock].first; | |
| 2116 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | uint32_t numFrames = blockEnd > blockStart ? blockEnd - blockStart : 0; |
| 2117 | 29 | uint64_t outsize = (uint64_t)numFrames * m_file->getChannelCount(); | |
| 2118 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | m_outBuffer = outsize ? (uint8_t*)malloc(outsize) : nullptr; |
| 2119 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | m_outBufferSize = m_outBuffer ? outsize : 0; |
| 2120 | 29 | m_stream->next_out = m_outBuffer; | |
| 2121 | 29 | m_stream->avail_out = m_outBufferSize; | |
| 2122 | |||
| 2123 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | if (m_outBuffer != nullptr) { |
| 2124 | 29 | inflate(m_stream, Z_SYNC_FLUSH); | |
| 2125 | } | ||
| 2126 | 29 | inflateEnd(m_stream); | |
| 2127 | 29 | free(m_stream); | |
| 2128 | 29 | m_stream = nullptr; | |
| 2129 | } | ||
| 2130 |
3/8✓ Branch 0 taken 680 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
680 | UncompressedFrameData* data = new UncompressedFrameData(frame, readContext()); |
| 2131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 680 times.
|
680 | if (!frameBufferAllocated(data, frame, m_file->m_dataBlockSize)) { |
| 2132 | ✗ | return data; | |
| 2133 | } | ||
| 2134 | |||
| 2135 | 680 | uint32_t blockStart = m_file->m_frameOffsets[m_curBlock].first; | |
| 2136 |
1/2✓ Branch 0 taken 680 times.
✗ Branch 1 not taken.
|
680 | uint64_t frameEnd = frame < blockStart ? 0 : ((uint64_t)(frame - blockStart) + 1) * m_file->getChannelCount(); |
| 2137 |
3/6✓ Branch 0 taken 680 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
|
680 | if (m_outBuffer == nullptr || frame < blockStart || frameEnd > m_outBufferSize) { |
| 2138 | // see the matching check in the ZSTD handler - the block table can | ||
| 2139 | // put this frame outside the block it selected | ||
| 2140 | ✗ | LogErr(VB_SEQUENCE, "Frame %d is not within block %d (starts at frame %d). Aborting frame load.\n", | |
| 2141 | (int)frame, (int)m_curBlock, (int)blockStart); | ||
| 2142 | ✗ | return data; | |
| 2143 | } | ||
| 2144 | 680 | uint64_t fidx = (uint64_t)(frame - blockStart) * m_file->getChannelCount(); | |
| 2145 | 680 | uint8_t* fdata = (uint8_t*)m_outBuffer; | |
| 2146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 680 times.
|
680 | if (!m_file->m_sparseRanges.empty()) { |
| 2147 | ✗ | memcpy(data->m_data, &fdata[fidx], m_file->getChannelCount()); | |
| 2148 | } else { | ||
| 2149 | 680 | uint32_t sz = 0; | |
| 2150 | // read the ranges into the buffer | ||
| 2151 |
2/2✓ Branch 0 taken 680 times.
✓ Branch 1 taken 680 times.
|
1360 | for (auto& rng : data->m_ranges) { |
| 2152 |
1/2✓ Branch 0 taken 680 times.
✗ Branch 1 not taken.
|
680 | if (rng.first < m_file->getChannelCount()) { |
| 2153 | 680 | memcpy(&data->m_data[sz], &fdata[fidx + rng.first], rng.second); | |
| 2154 | 680 | sz += rng.second; | |
| 2155 | } | ||
| 2156 | } | ||
| 2157 | } | ||
| 2158 | 680 | return data; | |
| 2159 | } | ||
| 2160 | // One deflate() call stops once the output buffer is full, leaving the | ||
| 2161 | // rest of the input unconsumed; the next caller then points next_in | ||
| 2162 | // somewhere else and that remainder is silently dropped. Keep going, | ||
| 2163 | // flushing as needed, until all of it has been taken. | ||
| 2164 | 3144 | void deflateInput(uint8_t* in, uint32_t len) { | |
| 2165 | 3144 | m_stream->next_in = in; | |
| 2166 | 3144 | m_stream->avail_in = len; | |
| 2167 |
2/2✓ Branch 0 taken 3144 times.
✓ Branch 1 taken 3144 times.
|
6288 | while (m_stream->avail_in > 0) { |
| 2168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3144 times.
|
3144 | if (m_stream->avail_out == 0) { |
| 2169 | ✗ | write(m_outBuffer, V2OutBufferSize()); | |
| 2170 | ✗ | m_stream->next_out = m_outBuffer; | |
| 2171 | ✗ | m_stream->avail_out = V2OutBufferSize(); | |
| 2172 | } | ||
| 2173 | 3144 | int zr = deflate(m_stream, Z_NO_FLUSH); | |
| 2174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3144 times.
|
3144 | if (zr != Z_OK) { |
| 2175 | ✗ | LogErr(VB_SEQUENCE, "zlib could not compress sequence block %d: %d\n", (int)m_curBlock, zr); | |
| 2176 | ✗ | break; | |
| 2177 | } | ||
| 2178 | } | ||
| 2179 | 3144 | } | |
| 2180 | 3144 | virtual void addFrame(uint32_t frame, const uint8_t* data) override { | |
| 2181 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3139 times.
|
3144 | if (m_outBuffer == nullptr) { |
| 2182 | 5 | m_outBuffer = (uint8_t*)malloc(V2OutBufferSize()); | |
| 2183 | } | ||
| 2184 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3139 times.
|
3144 | if (m_stream == nullptr) { |
| 2185 | 5 | m_stream = (z_stream*)calloc(1, sizeof(z_stream)); | |
| 2186 | } | ||
| 2187 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 3041 times.
|
3144 | if (m_curFrameInBlock == 0) { |
| 2188 |
1/2✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
|
103 | uint64_t offset = tell(); |
| 2189 |
1/2✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
|
103 | m_file->m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(frame, offset)); |
| 2190 |
1/2✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
|
103 | deflateEnd(m_stream); |
| 2191 | 103 | memset(m_stream, 0, sizeof(z_stream)); | |
| 2192 | } | ||
| 2193 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 3041 times.
|
3144 | if (m_curFrameInBlock == 0) { |
| 2194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | int clevel = m_file->m_compressionLevel == -99 ? 3 : m_file->m_compressionLevel; |
| 2195 |
2/4✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 103 times.
|
103 | if (clevel < 0 || clevel > 9) { |
| 2196 | ✗ | clevel = 3; | |
| 2197 | } | ||
| 2198 | 103 | deflateInit(m_stream, clevel); | |
| 2199 | 103 | m_stream->next_out = m_outBuffer; | |
| 2200 | 103 | m_stream->avail_out = V2OutBufferSize(); | |
| 2201 | } | ||
| 2202 | |||
| 2203 | 3144 | uint8_t* curData = (uint8_t*)data; | |
| 2204 |
1/2✓ Branch 0 taken 3144 times.
✗ Branch 1 not taken.
|
3144 | if (m_file->m_sparseRanges.empty()) { |
| 2205 | 3144 | uint64_t toCompress = m_file->getChannelCount(); | |
| 2206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3144 times.
|
3144 | if (toCompress > MAX_CHANNEL_SPACE) { |
| 2207 | ✗ | toCompress = MAX_CHANNEL_SPACE; | |
| 2208 | } | ||
| 2209 | 3144 | deflateInput(curData, (uint32_t)toCompress); | |
| 2210 | } else { | ||
| 2211 | ✗ | for (auto& a : m_file->m_sparseRanges) { | |
| 2212 | ✗ | if ((uint64_t)a.first >= MAX_CHANNEL_SPACE) { | |
| 2213 | ✗ | continue; | |
| 2214 | } | ||
| 2215 | ✗ | uint64_t len = a.second; | |
| 2216 | ✗ | if ((uint64_t)a.first + len > MAX_CHANNEL_SPACE) { | |
| 2217 | ✗ | len = MAX_CHANNEL_SPACE - a.first; | |
| 2218 | } | ||
| 2219 | ✗ | deflateInput(&curData[a.first], (uint32_t)len); | |
| 2220 | } | ||
| 2221 | } | ||
| 2222 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3141 times.
|
3144 | if (m_stream->avail_out < (V2OutBufferSize() - V2FSEQ_OUT_BUFFER_FLUSH_SIZE)) { |
| 2223 | // buffer is getting full, better flush it | ||
| 2224 | 3 | uint64_t sz = V2OutBufferSize(); | |
| 2225 | 3 | sz -= m_stream->avail_out; | |
| 2226 | 3 | write(m_outBuffer, sz); | |
| 2227 | 3 | m_stream->next_out = m_outBuffer; | |
| 2228 | 3 | m_stream->avail_out = V2OutBufferSize(); | |
| 2229 | } | ||
| 2230 | 3144 | m_curFrameInBlock++; | |
| 2231 | // if we hit the max per block OR we're in the first block and hit frame #10 | ||
| 2232 | // we'll start a new block. We want the first block to be small so startup is | ||
| 2233 | // quicker and we can get the first few frames as fast as possible. | ||
| 2234 |
9/10✓ Branch 0 taken 42 times.
✓ Branch 1 taken 3102 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 94 times.
✓ Branch 5 taken 3046 times.
✓ Branch 6 taken 94 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 98 times.
✓ Branch 9 taken 3046 times.
|
3144 | if ((m_curBlock == 0 && m_curFrameInBlock == 10) || (m_curFrameInBlock == m_framesPerBlock && m_file->m_frameOffsets.size() < m_maxBlocks)) { |
| 2235 | int zr; | ||
| 2236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
|
98 | while ((zr = deflate(m_stream, Z_FINISH)) != Z_STREAM_END) { |
| 2237 | ✗ | if (zr != Z_OK) { | |
| 2238 | ✗ | LogErr(VB_SEQUENCE, "zlib could not finish sequence block %d: %d\n", (int)m_curBlock, zr); | |
| 2239 | ✗ | break; | |
| 2240 | } | ||
| 2241 | ✗ | uint64_t sz = V2OutBufferSize(); | |
| 2242 | ✗ | sz -= m_stream->avail_out; | |
| 2243 | ✗ | write(m_outBuffer, sz); | |
| 2244 | ✗ | m_stream->next_out = m_outBuffer; | |
| 2245 | ✗ | m_stream->avail_out = V2OutBufferSize(); | |
| 2246 | } | ||
| 2247 | 98 | uint64_t sz = V2OutBufferSize(); | |
| 2248 | 98 | sz -= m_stream->avail_out; | |
| 2249 | 98 | write(m_outBuffer, sz); | |
| 2250 | 98 | m_stream->next_out = m_outBuffer; | |
| 2251 | 98 | m_stream->avail_out = V2OutBufferSize(); | |
| 2252 | |||
| 2253 | 98 | m_curFrameInBlock = 0; | |
| 2254 | 98 | m_curBlock++; | |
| 2255 | } | ||
| 2256 | 3144 | } | |
| 2257 | 5 | virtual void finalize() override { | |
| 2258 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (m_curFrameInBlock) { |
| 2259 | int zr; | ||
| 2260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | while ((zr = deflate(m_stream, Z_FINISH)) != Z_STREAM_END) { |
| 2261 | ✗ | if (zr != Z_OK) { | |
| 2262 | ✗ | LogErr(VB_SEQUENCE, "zlib could not finish sequence block %d: %d\n", (int)m_curBlock, zr); | |
| 2263 | ✗ | break; | |
| 2264 | } | ||
| 2265 | ✗ | uint64_t sz = V2OutBufferSize(); | |
| 2266 | ✗ | sz -= m_stream->avail_out; | |
| 2267 | ✗ | write(m_outBuffer, sz); | |
| 2268 | ✗ | m_stream->next_out = m_outBuffer; | |
| 2269 | ✗ | m_stream->avail_out = V2OutBufferSize(); | |
| 2270 | } | ||
| 2271 | 5 | uint64_t sz = V2OutBufferSize(); | |
| 2272 | 5 | sz -= m_stream->avail_out; | |
| 2273 | 5 | write(m_outBuffer, sz); | |
| 2274 | 5 | m_stream->next_out = m_outBuffer; | |
| 2275 | 5 | m_stream->avail_out = V2OutBufferSize(); | |
| 2276 | |||
| 2277 | 5 | m_curFrameInBlock = 0; | |
| 2278 | 5 | m_curBlock++; | |
| 2279 | } | ||
| 2280 | 5 | V2CompressedHandler::finalize(); | |
| 2281 | 5 | } | |
| 2282 | |||
| 2283 | z_stream* m_stream; | ||
| 2284 | uint8_t* m_outBuffer; | ||
| 2285 | // bytes m_outBuffer can hold on the read side (0 when it holds the fixed | ||
| 2286 | // size write buffer, which the read path never touches) | ||
| 2287 | uint64_t m_outBufferSize = 0; | ||
| 2288 | uint8_t* m_inBuffer; | ||
| 2289 | }; | ||
| 2290 | #endif | ||
| 2291 | |||
| 2292 | 2739 | void V2FSEQFile::createHandler() { | |
| 2293 |
3/4✓ Branch 0 taken 2652 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
2739 | switch (m_compressionType) { |
| 2294 | 2652 | case CompressionType::none: | |
| 2295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2652 times.
|
2652 | m_handler = new V2NoneCompressionHandler(this); |
| 2296 | 2652 | break; | |
| 2297 | 74 | case CompressionType::zstd: | |
| 2298 | #ifdef NO_ZSTD | ||
| 2299 | LogErr(VB_ALL, "No support for zstd compression"); | ||
| 2300 | #else | ||
| 2301 |
2/6✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 74 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
74 | m_handler = new V2ZSTDCompressionHandler(this); |
| 2302 | #endif | ||
| 2303 | 74 | break; | |
| 2304 | 13 | case CompressionType::zlib: | |
| 2305 | #ifdef NO_ZLIB | ||
| 2306 | LogErr(VB_ALL, "No support for zlib compression"); | ||
| 2307 | #else | ||
| 2308 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | m_handler = new V2ZLIBCompressionHandler(this); |
| 2309 | #endif | ||
| 2310 | 13 | break; | |
| 2311 | } | ||
| 2312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2739 times.
|
2739 | if (m_handler == nullptr) { |
| 2313 | ✗ | LogDebug(VB_SEQUENCE, "Creating a default none compression handler. %d", (int)m_compressionType); | |
| 2314 | ✗ | m_handler = new V2NoneCompressionHandler(this); | |
| 2315 | } | ||
| 2316 | 2739 | } | |
| 2317 | |||
| 2318 | 628 | V2FSEQFile::V2FSEQFile(const std::string& fn, CompressionType ct, int cl) : | |
| 2319 | FSEQFile(fn), | ||
| 2320 | 628 | m_compressionType(ct), | |
| 2321 | 628 | m_compressionLevel(cl), | |
| 2322 | 628 | m_handler(nullptr), | |
| 2323 | 628 | m_allowExtendedBlocks(false) { | |
| 2324 | 628 | m_seqVersionMajor = V2FSEQ_MAJOR_VERSION; | |
| 2325 | 628 | m_seqVersionMinor = V2FSEQ_MINOR_VERSION; | |
| 2326 | |||
| 2327 |
1/2✓ Branch 0 taken 628 times.
✗ Branch 1 not taken.
|
628 | createHandler(); |
| 2328 | 628 | } | |
| 2329 | 16 | void V2FSEQFile::fitSparseRangesToHeader(std::vector<std::pair<uint32_t, uint32_t>>& ranges) { | |
| 2330 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | if (ranges.size() <= 255) { |
| 2331 | 12 | return; | |
| 2332 | } | ||
| 2333 | 4 | std::sort(ranges.begin(), ranges.end()); | |
| 2334 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 4 times.
|
184 | while (ranges.size() > 255) { |
| 2335 | 180 | size_t best = 1; | |
| 2336 | 180 | uint32_t bestGap = UINT32_MAX; | |
| 2337 |
2/2✓ Branch 0 taken 49860 times.
✓ Branch 1 taken 180 times.
|
50040 | for (size_t i = 1; i < ranges.size(); ++i) { |
| 2338 | 49860 | uint32_t prevEnd = ranges[i - 1].first + ranges[i - 1].second; | |
| 2339 |
1/2✓ Branch 0 taken 49860 times.
✗ Branch 1 not taken.
|
49860 | uint32_t gap = ranges[i].first > prevEnd ? ranges[i].first - prevEnd : 0; |
| 2340 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 49680 times.
|
49860 | if (gap < bestGap) { |
| 2341 | 180 | bestGap = gap; | |
| 2342 | 180 | best = i; | |
| 2343 | } | ||
| 2344 | } | ||
| 2345 | 180 | auto& prev = ranges[best - 1]; | |
| 2346 | 180 | uint32_t end = std::max(prev.first + prev.second, ranges[best].first + ranges[best].second); | |
| 2347 | 180 | prev.second = end - prev.first; | |
| 2348 |
1/2✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 | ranges.erase(ranges.begin() + best); |
| 2349 | } | ||
| 2350 | } | ||
| 2351 | |||
| 2352 | 627 | void V2FSEQFile::writeHeader() { | |
| 2353 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 606 times.
|
627 | if (!m_sparseRanges.empty()) { |
| 2354 | // make sure the sparse ranges fit, and then | ||
| 2355 | // recalculate the channel count for in the fseq. | ||
| 2356 | // Clamp to channel space in 64-bit as well: addFrame() enforces the | ||
| 2357 | // same ceiling, so normalizing here keeps the header's packed count | ||
| 2358 | // (sum of lens) equal to the bytes each frame actually writes. | ||
| 2359 | // Without this, a clamped range would leave frames short and shift | ||
| 2360 | // every subsequent frame in the file. | ||
| 2361 | 21 | std::vector<std::pair<uint32_t, uint32_t>> newRanges; | |
| 2362 |
2/2✓ Branch 0 taken 2674 times.
✓ Branch 1 taken 21 times.
|
2695 | for (auto& a : m_sparseRanges) { |
| 2363 | 2674 | uint64_t start = a.first; | |
| 2364 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2674 times.
|
2674 | if (start >= MAX_CHANNEL_SPACE) { |
| 2365 | ✗ | continue; | |
| 2366 | } | ||
| 2367 | 2674 | uint64_t len = a.second; | |
| 2368 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2674 times.
|
2674 | if (start + len > MAX_CHANNEL_SPACE) { |
| 2369 | ✗ | len = MAX_CHANNEL_SPACE - start; | |
| 2370 | } | ||
| 2371 |
1/2✓ Branch 0 taken 2674 times.
✗ Branch 1 not taken.
|
2674 | if (start < m_seqChannelCount) { |
| 2372 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2674 times.
|
2674 | if (start + len > m_seqChannelCount) { |
| 2373 | ✗ | len = (uint64_t)m_seqChannelCount - start; | |
| 2374 | } | ||
| 2375 |
1/2✓ Branch 0 taken 2674 times.
✗ Branch 1 not taken.
|
2674 | newRanges.push_back(std::pair<uint32_t, uint32_t>((uint32_t)start, (uint32_t)len)); |
| 2376 | } | ||
| 2377 | } | ||
| 2378 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | m_sparseRanges = newRanges; |
| 2379 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5 times.
|
21 | if (m_seqVersionMinor < 3) { |
| 2380 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | fitSparseRangesToHeader(m_sparseRanges); |
| 2381 | } | ||
| 2382 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | if (!m_sparseRanges.empty()) { |
| 2383 | 21 | m_seqChannelCount = 0; | |
| 2384 |
2/2✓ Branch 0 taken 2494 times.
✓ Branch 1 taken 21 times.
|
2515 | for (auto& a : m_sparseRanges) { |
| 2385 | 2494 | m_seqChannelCount += a.second; | |
| 2386 | } | ||
| 2387 | } | ||
| 2388 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 605 times.
|
627 | } else if ((uint64_t)m_seqChannelCount > MAX_CHANNEL_SPACE) { |
| 2389 | // Dense output with a file-inflated count (see initializeFromFSEQ): | ||
| 2390 | // cap it so the header and the per-frame writes agree. | ||
| 2391 | 1 | m_seqChannelCount = (uint32_t)MAX_CHANNEL_SPACE; | |
| 2392 | } | ||
| 2393 | |||
| 2394 | // Additional file format documentation available at: | ||
| 2395 | // https://github.com/FalconChristmas/fpp/blob/master/docs/FSEQ_Sequence_File_Format.txt#L17 | ||
| 2396 | |||
| 2397 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 617 times.
|
627 | uint16_t maxBlocks = m_handler->computeMaxBlocks(m_allowExtendedBlocks ? 4095 : 255) & 0xFFF; |
| 2398 | |||
| 2399 | // A table longer than the header can count goes whole into an 'SR' block. The | ||
| 2400 | // header keeps the first 255 so readers that predate 'SR' still play those channels. | ||
| 2401 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
676 | std::erase_if(m_variableHeaders, [](const VariableHeader& vh) { return vh.code[0] == 'S' && vh.code[1] == 'R'; }); |
| 2402 | 627 | size_t headerRanges = m_sparseRanges.size(); | |
| 2403 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 623 times.
|
627 | if (headerRanges > 255) { |
| 2404 | 4 | headerRanges = 255; | |
| 2405 | 4 | VariableHeader sr; | |
| 2406 | 4 | sr.code[0] = 'S'; | |
| 2407 | 4 | sr.code[1] = 'R'; | |
| 2408 | 4 | sr.extendedData = true; | |
| 2409 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | sr.resizeData(m_sparseRanges.size() * V2FSEQ_SPARSE_RANGE_SIZE); |
| 2410 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | auto& data = sr.getData(); |
| 2411 |
2/2✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 4 times.
|
1204 | for (size_t i = 0; i < m_sparseRanges.size(); i++) { |
| 2412 | 1200 | write3ByteUInt(&data[i * V2FSEQ_SPARSE_RANGE_SIZE], m_sparseRanges[i].first); | |
| 2413 | 1200 | write3ByteUInt(&data[i * V2FSEQ_SPARSE_RANGE_SIZE + 3], m_sparseRanges[i].second); | |
| 2414 | } | ||
| 2415 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | m_variableHeaders.push_back(sr); |
| 2416 | 4 | } | |
| 2417 | |||
| 2418 | // Compute headerSize to include the header, compression blocks and sparse ranges | ||
| 2419 | 627 | int headerSize = V2FSEQ_HEADER_SIZE; | |
| 2420 | 627 | headerSize += maxBlocks * V2FSEQ_COMPRESSION_BLOCK_SIZE; | |
| 2421 | 627 | headerSize += headerRanges * V2FSEQ_SPARSE_RANGE_SIZE; | |
| 2422 | |||
| 2423 | // Channel data offset is the headerSize plus size of variable headers | ||
| 2424 | // Round to a product of 4 for better memory alignment | ||
| 2425 | 627 | m_seqChanDataOffset = headerSize; | |
| 2426 | 627 | uint64_t seqChanDataOffset2 = headerSize; | |
| 2427 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 627 times.
|
680 | for (auto& a : m_variableHeaders) { |
| 2428 | 53 | uint32_t sze = a.getDataLength() + FSEQ_VARIABLE_HEADER_SIZE; | |
| 2429 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
|
53 | if (a.extendedData) { |
| 2430 | 4 | seqChanDataOffset2 += FSEQ_VARIABLE_HEADER_SIZE + 14; | |
| 2431 | 4 | m_seqChanDataOffset += FSEQ_VARIABLE_HEADER_SIZE + 14; | |
| 2432 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2 times.
|
49 | } else if (sze <= (14 + FSEQ_VARIABLE_HEADER_SIZE)) { |
| 2433 | // smaller than the extended data type so would never be output | ||
| 2434 | // as extended data | ||
| 2435 | 47 | m_seqChanDataOffset += sze; | |
| 2436 | 47 | seqChanDataOffset2 += sze; // smaller than the shifted size | |
| 2437 | } else { | ||
| 2438 | // could potentially be placed in the extended area if the header is too large | ||
| 2439 | // record both sizes to compare later | ||
| 2440 | 2 | m_seqChanDataOffset += sze; | |
| 2441 | 2 | seqChanDataOffset2 += FSEQ_VARIABLE_HEADER_SIZE + 14; // 64bit offset into file, 32bit length, new 2byte code | |
| 2442 | } | ||
| 2443 | } | ||
| 2444 | 627 | bool forceExtended = false; | |
| 2445 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 620 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
627 | if (m_seqVersionMinor >= 2 && (m_seqChanDataOffset >= 0xFFFF)) { |
| 2446 | // beyond the byte header size. The variable header data will need to be stored elsewhere | ||
| 2447 | ✗ | m_seqChanDataOffset = seqChanDataOffset2; | |
| 2448 | ✗ | forceExtended = true; | |
| 2449 | } | ||
| 2450 | 627 | m_seqChanDataOffset = roundTo4Internal(m_seqChanDataOffset); | |
| 2451 | |||
| 2452 | // Use m_seqChanDataOffset for buffer size to avoid additional writes or buffer allocations | ||
| 2453 | // It also comes pre-memory aligned to avoid adding padding | ||
| 2454 | // uint8_t header[m_seqChanDataOffset]; // MSW does not support non compile time constant array sizes | ||
| 2455 | 627 | uint8_t* header = (uint8_t*)malloc(m_seqChanDataOffset); | |
| 2456 | 627 | memset(header, 0, m_seqChanDataOffset); | |
| 2457 | |||
| 2458 | // File identifier (PSEQ) - 4 bytes | ||
| 2459 | 627 | header[0] = 'P'; | |
| 2460 | 627 | header[1] = 'S'; | |
| 2461 | 627 | header[2] = 'E'; | |
| 2462 | 627 | header[3] = 'Q'; | |
| 2463 | |||
| 2464 | // Channel data start offset - 2 bytes | ||
| 2465 | 627 | write2ByteUInt(&header[4], m_seqChanDataOffset); | |
| 2466 | |||
| 2467 | // File format version - 2 bytes | ||
| 2468 | // 2.3 only changes how the sparse ranges are stored, so a file that doesn't | ||
| 2469 | // need it is marked 2.2 and older readers take it without complaint. | ||
| 2470 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 622 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
627 | header[6] = (m_seqVersionMinor >= 3 && headerRanges == m_sparseRanges.size()) ? 2 : m_seqVersionMinor; |
| 2471 | 627 | header[7] = m_seqVersionMajor; | |
| 2472 | |||
| 2473 | // Computed header length - 2 bytes | ||
| 2474 | 627 | write2ByteUInt(&header[8], headerSize); | |
| 2475 | // Channel count - 4 bytes | ||
| 2476 | 627 | write4ByteUInt(&header[10], m_seqChannelCount); | |
| 2477 | // Number of frames - 4 bytes | ||
| 2478 | 627 | write4ByteUInt(&header[14], m_seqNumFrames); | |
| 2479 | |||
| 2480 | // Step time in milliseconds - 1 byte | ||
| 2481 | 627 | header[18] = m_seqStepTime; | |
| 2482 | // Flags (unused & reserved, should be 0) - 1 byte | ||
| 2483 | 627 | header[19] = 0; | |
| 2484 | // Compression type and upper 4 bits of max blocks- 1 byte | ||
| 2485 | 627 | header[20] = ((maxBlocks >> 4) & 0xF0) | m_handler->getCompressionType(); | |
| 2486 | // Number of blocks in compressed channel data (should be 0 if not compressed) - 1 byte, lower 8 bits | ||
| 2487 | 627 | header[21] = maxBlocks & 0xFF; | |
| 2488 | // Number of ranges in sparse range index - 1 byte | ||
| 2489 | 627 | header[22] = headerRanges; | |
| 2490 | // Flags (unused & reserved, should be 0) - 1 byte | ||
| 2491 | 627 | header[23] = 0; | |
| 2492 | |||
| 2493 | // Timestamp based UUID - 8 bytes | ||
| 2494 |
2/2✓ Branch 0 taken 593 times.
✓ Branch 1 taken 34 times.
|
627 | if (m_uniqueId == 0) { |
| 2495 | 593 | m_uniqueId = GetTime(); | |
| 2496 | } | ||
| 2497 | 627 | memcpy(&header[24], &m_uniqueId, sizeof(m_uniqueId)); | |
| 2498 | |||
| 2499 | 627 | int writePos = V2FSEQ_HEADER_SIZE; | |
| 2500 | |||
| 2501 | // Empty compression blocks are automatically added when calculating headerSize (see maxBlocks) | ||
| 2502 | // Their data is initialized to 0 by memset and computed later | ||
| 2503 | 627 | writePos += maxBlocks * V2FSEQ_COMPRESSION_BLOCK_SIZE; | |
| 2504 | |||
| 2505 | // Sparse ranges | ||
| 2506 | // 6 byte size (3 byte value + 3 byte value) | ||
| 2507 |
2/2✓ Branch 0 taken 2314 times.
✓ Branch 1 taken 627 times.
|
2941 | for (size_t i = 0; i < headerRanges; i++) { |
| 2508 | 2314 | write3ByteUInt(&header[writePos], m_sparseRanges[i].first); | |
| 2509 | 2314 | write3ByteUInt(&header[writePos + 3], m_sparseRanges[i].second); | |
| 2510 | 2314 | writePos += V2FSEQ_SPARSE_RANGE_SIZE; | |
| 2511 | } | ||
| 2512 | |||
| 2513 | // Variable headers | ||
| 2514 | // 4 byte size minimum (2 byte length + 2 byte code) | ||
| 2515 | 627 | int idx = 0; | |
| 2516 | 627 | m_handler->m_variableHeaderOffsets.resize(m_variableHeaders.size()); | |
| 2517 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 627 times.
|
680 | for (auto& a : m_variableHeaders) { |
| 2518 | 53 | uint32_t len = FSEQ_VARIABLE_HEADER_SIZE + a.getDataLength(); | |
| 2519 | 53 | bool doExtended = a.extendedData; | |
| 2520 |
3/6✓ Branch 0 taken 49 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
53 | if (!doExtended && forceExtended && len > 18) { |
| 2521 | // longer than the extended header and we need to save space | ||
| 2522 | ✗ | doExtended = true; | |
| 2523 | } | ||
| 2524 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
|
53 | if (doExtended) { |
| 2525 | 4 | len = 14 + FSEQ_VARIABLE_HEADER_SIZE; | |
| 2526 | } | ||
| 2527 | 53 | write2ByteUInt(&header[writePos], len); | |
| 2528 | 53 | writePos += 2; | |
| 2529 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
|
53 | if (doExtended) { |
| 2530 | 4 | header[writePos] = 'E'; | |
| 2531 | 4 | header[writePos + 1] = 'D'; | |
| 2532 | 4 | writePos += 2; | |
| 2533 | } | ||
| 2534 | 53 | header[writePos] = a.code[0]; | |
| 2535 | 53 | header[writePos + 1] = a.code[1]; | |
| 2536 | 53 | writePos += 2; | |
| 2537 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
|
53 | if (doExtended) { |
| 2538 | 4 | memset(&header[writePos], 0, 8); | |
| 2539 | 4 | m_handler->m_variableHeaderOffsets[idx] = writePos; | |
| 2540 | 4 | writePos += 8; // file position | |
| 2541 | 4 | write4ByteUInt(&header[writePos], a.getDataLength()); | |
| 2542 | 4 | writePos += 4; | |
| 2543 | } else { | ||
| 2544 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | auto& data = a.getData(); |
| 2545 | 49 | m_handler->m_variableHeaderOffsets[idx] = 0; | |
| 2546 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (!data.empty()) { |
| 2547 | 49 | memcpy(&header[writePos], data.data(), data.size()); | |
| 2548 | } | ||
| 2549 | 49 | writePos += data.size(); | |
| 2550 | } | ||
| 2551 | 53 | ++idx; | |
| 2552 | } | ||
| 2553 | |||
| 2554 | // Validate final write position matches expected channel data offset | ||
| 2555 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
|
627 | if (roundTo4Internal(writePos) != m_seqChanDataOffset) { |
| 2556 | ✗ | LogErr(VB_SEQUENCE, "Final write position (%d, roundTo4 = %d) does not match channel data offset (%d)! This means the header size failed to compute an accurate buffer size.\n", writePos, roundTo4Internal(writePos), m_seqChanDataOffset); | |
| 2557 | } | ||
| 2558 | |||
| 2559 | // Write full header at once | ||
| 2560 | // header buffer is sized to the value of m_seqChanDataOffset, which comes padded for memory alignment | ||
| 2561 | // If writePos extends past m_seqChanDataOffset (in error), writing m_seqChanDataOffset prevents data overflow | ||
| 2562 | 627 | write(header, m_seqChanDataOffset); | |
| 2563 | |||
| 2564 | 627 | free(header); | |
| 2565 | |||
| 2566 | 627 | LogDebug(VB_SEQUENCE, "Setup for writing v2 FSEQ\n"); | |
| 2567 | 627 | dumpInfo(true); | |
| 2568 | 627 | } | |
| 2569 | |||
| 2570 | 2111 | V2FSEQFile::V2FSEQFile(const std::string& fn, FILE* file, const std::vector<uint8_t>& header) : | |
| 2571 | FSEQFile(fn, file, header), | ||
| 2572 | 2111 | m_compressionType(none), | |
| 2573 | 2111 | m_handler(nullptr) { | |
| 2574 |
2/4✓ Branch 0 taken 2111 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2111 times.
|
2111 | if (m_seqVersionMajor == 2 && m_seqVersionMinor > 3) { |
| 2575 | ✗ | LogErr(VB_SEQUENCE, "Unknown minor version: %d. FSEQ may not load properly.\n", m_seqVersionMinor); | |
| 2576 | } | ||
| 2577 | |||
| 2578 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 2061 times.
|
2111 | if (header[0] == V1ESEQ_HEADER_IDENTIFIER) { |
| 2579 | 50 | m_compressionType = CompressionType::none; | |
| 2580 | |||
| 2581 | 50 | uint32_t modelStart = read4ByteUInt(&header[12]); | |
| 2582 | 50 | uint32_t modelLen = read4ByteUInt(&header[16]); | |
| 2583 | |||
| 2584 | // ESEQ files use 1 based start channels, offset to start at 0 | ||
| 2585 |
2/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
|
50 | m_sparseRanges.push_back(std::pair<uint32_t, uint32_t>(modelStart ? modelStart - 1 : modelStart, modelLen)); |
| 2586 | } else { | ||
| 2587 |
3/4✓ Branch 0 taken 2013 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
2061 | switch (header[20] & 0xF) { |
| 2588 | 2013 | case 0: | |
| 2589 | 2013 | m_compressionType = CompressionType::none; | |
| 2590 | 2013 | break; | |
| 2591 | 40 | case 1: | |
| 2592 | 40 | m_compressionType = CompressionType::zstd; | |
| 2593 | 40 | break; | |
| 2594 | 8 | case 2: | |
| 2595 | 8 | m_compressionType = CompressionType::zlib; | |
| 2596 | 8 | break; | |
| 2597 | ✗ | default: | |
| 2598 | ✗ | LogErr(VB_SEQUENCE, "Unknown compression type: %d\n", (int)header[20]); | |
| 2599 | } | ||
| 2600 | |||
| 2601 | // readPos tracks the reader index for variable length data past the fixed header size | ||
| 2602 | // This is used to check for reader index overflows | ||
| 2603 | 2061 | int readPos = V2FSEQ_HEADER_SIZE; | |
| 2604 | |||
| 2605 | // Read compression blocks | ||
| 2606 | // 8 byte size each (4 byte firstFrame + 4 byte length) | ||
| 2607 | // header[21] is the "max blocks count" field, lower 8 bits | ||
| 2608 | // header[20] & 0xF0 is the upper 4 bits, total 12 bits | ||
| 2609 | // for maximum of 4095 blocks | ||
| 2610 | 2061 | uint64_t lastBlockOffset = m_seqChanDataOffset; | |
| 2611 | 2061 | uint32_t numBlocks = header[20]; | |
| 2612 | 2061 | numBlocks &= 0xF0; | |
| 2613 | 2061 | numBlocks <<= 4; | |
| 2614 | 2061 | numBlocks |= header[21]; | |
| 2615 | |||
| 2616 | // The block and sparse range counts are file data too; don't let them | ||
| 2617 | // walk readPos past the header buffer. | ||
| 2618 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2059 times.
|
2061 | if ((uint64_t)readPos + (uint64_t)numBlocks * V2FSEQ_COMPRESSION_BLOCK_SIZE > header.size()) { |
| 2619 | 2 | uint32_t fits = (header.size() - readPos) / V2FSEQ_COMPRESSION_BLOCK_SIZE; | |
| 2620 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "FSEQ header claims %d compression blocks but only has room for %d. File is truncated or corrupt.\n", |
| 2621 | (int)numBlocks, (int)fits); | ||
| 2622 | 2 | numBlocks = fits; | |
| 2623 | } | ||
| 2624 | |||
| 2625 | 2061 | uint32_t lastFirstFrame = 0; | |
| 2626 |
2/2✓ Branch 0 taken 2049 times.
✓ Branch 1 taken 2061 times.
|
4110 | for (uint32_t i = 0; i < numBlocks; i++) { |
| 2627 | 2049 | uint32_t firstFrame = read4ByteUInt(&header[readPos]); | |
| 2628 | 2049 | uint64_t length = read4ByteUInt(&header[readPos + 4]); | |
| 2629 | |||
| 2630 |
2/2✓ Branch 0 taken 2002 times.
✓ Branch 1 taken 47 times.
|
2049 | if (length > 0) { |
| 2631 | // These start frames are file data. Every reader indexes a | ||
| 2632 | // block as (frame - block start) and searches the table by | ||
| 2633 | // walking forward, so a table that is not in order - the | ||
| 2634 | // writer's own finalize() has seen files whose block table was | ||
| 2635 | // never filled in - drives that index off the front of the | ||
| 2636 | // decompressed block. Hold the ordering here, where the table | ||
| 2637 | // is built, rather than at each of the places that trust it. | ||
| 2638 |
5/8✓ Branch 0 taken 2002 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 1954 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2002 times.
|
2002 | if (firstFrame < lastFirstFrame || (m_frameOffsets.empty() && firstFrame != 0)) { |
| 2639 | ✗ | LogErr(VB_SEQUENCE, "FSEQ block %d claims to start at frame %d, out of order with the block before it at %d. Block table is corrupt.\n", | |
| 2640 | (int)i, (int)firstFrame, (int)lastFirstFrame); | ||
| 2641 | ✗ | firstFrame = lastFirstFrame; | |
| 2642 | } | ||
| 2643 |
1/2✓ Branch 0 taken 2002 times.
✗ Branch 1 not taken.
|
2002 | m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(firstFrame, lastBlockOffset)); |
| 2644 | 2002 | lastFirstFrame = firstFrame; | |
| 2645 | 2002 | lastBlockOffset += length; | |
| 2646 | } | ||
| 2647 | |||
| 2648 | 2049 | readPos += V2FSEQ_COMPRESSION_BLOCK_SIZE; | |
| 2649 | |||
| 2650 | // Duplicated legacy behavior | ||
| 2651 | // Preloads up to [length] bytes starting at m_seqChanDataOffset | ||
| 2652 | // This pre-buffers the first compression block | ||
| 2653 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 2001 times.
|
2049 | if (i == 0) { |
| 2654 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | preload(m_seqChanDataOffset, length); |
| 2655 | } | ||
| 2656 | } | ||
| 2657 | |||
| 2658 | // The block lengths read above sum to exactly the channel data, so once | ||
| 2659 | // every block has been seen lastBlockOffset is the end of the channel | ||
| 2660 | // data. Anything the writer put after it (extended variable headers, | ||
| 2661 | // embedded files) is not part of any block. | ||
| 2662 | 2061 | const bool haveBlocks = !m_frameOffsets.empty(); | |
| 2663 | 2061 | uint64_t chanDataEnd = lastBlockOffset; | |
| 2664 | |||
| 2665 |
2/2✓ Branch 0 taken 2013 times.
✓ Branch 1 taken 48 times.
|
2061 | if (m_compressionType == CompressionType::none) { |
| 2666 | // Push frame offsets that cover the entire file length given the channel data is effectively a single block | ||
| 2667 | // For uncompressed blocks, maxBlocks should always be 0 and m_frameOffsets initially empty | ||
| 2668 |
1/2✓ Branch 0 taken 2013 times.
✗ Branch 1 not taken.
|
2013 | m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(0, m_seqChanDataOffset)); |
| 2669 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | } else if (m_frameOffsets.size() == 0) { |
| 2670 | ✗ | LogErr(VB_SEQUENCE, "FSEQ file corrupt: did not load any block references from header.\n"); | |
| 2671 | |||
| 2672 | // File is flagged as compressed but no compression blocks were read | ||
| 2673 | // The file is likely corrupted, read the full channel data as a single block as a recovery attempt | ||
| 2674 | ✗ | m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(0, m_seqChanDataOffset)); | |
| 2675 | } | ||
| 2676 | |||
| 2677 | // Push a final frame offset that terminates the last block. Without a | ||
| 2678 | // usable block table (uncompressed, or the corrupt-file recovery above) | ||
| 2679 | // fall back to the file size so the last block still covers the data. | ||
| 2680 |
5/6✓ Branch 0 taken 48 times.
✓ Branch 1 taken 2013 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 44 times.
|
2061 | if (!haveBlocks || chanDataEnd > m_seqFileSize || chanDataEnd <= m_seqChanDataOffset) { |
| 2681 | 2017 | chanDataEnd = m_seqFileSize; | |
| 2682 | } | ||
| 2683 | // Every reader takes a block's length as the next block's offset minus | ||
| 2684 | // its own, so no block may start past the end of the data. A table | ||
| 2685 | // claiming more bytes than the file holds -- truncated file, corrupt | ||
| 2686 | // table -- otherwise leaves the later blocks past chanDataEnd and their | ||
| 2687 | // lengths underflow to nearly 2^64. Clamp here, where the table is | ||
| 2688 | // built, so the offsets are non-decreasing and bounded by the file for | ||
| 2689 | // every reader. | ||
| 2690 | 2061 | bool clamped = false; | |
| 2691 |
2/2✓ Branch 0 taken 4015 times.
✓ Branch 1 taken 2061 times.
|
6076 | for (auto& fo : m_frameOffsets) { |
| 2692 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4013 times.
|
4015 | if (fo.second > chanDataEnd) { |
| 2693 | 2 | fo.second = chanDataEnd; | |
| 2694 | 2 | clamped = true; | |
| 2695 | } | ||
| 2696 | } | ||
| 2697 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2059 times.
|
2061 | if (clamped) { |
| 2698 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "FSEQ block table runs past the end of the channel data (%" PRIu64 " bytes). File is truncated or corrupt.\n", |
| 2699 | chanDataEnd); | ||
| 2700 | } | ||
| 2701 |
1/2✓ Branch 0 taken 2061 times.
✗ Branch 1 not taken.
|
2061 | m_frameOffsets.push_back(std::pair<uint32_t, uint64_t>(getNumFrames() + 2, chanDataEnd)); |
| 2702 | |||
| 2703 | // Read sparse ranges | ||
| 2704 | // 6 byte size each (3 byte firstChannel + 3 byte length) | ||
| 2705 | // header[22] is the "sparse range count" field | ||
| 2706 | 2061 | int numRanges = header[22]; | |
| 2707 | // Writers before 2.3 stored tables of more than 255 ranges with only the low | ||
| 2708 | // byte of the count. Take the count implied by the header size (bytes 8-9) when | ||
| 2709 | // its low byte matches, the table fits in the header, and the ranges add up to | ||
| 2710 | // the channel count. | ||
| 2711 | 2061 | int tableEnd = read2ByteUInt(&header[8]); | |
| 2712 |
5/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2060 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2060 times.
|
2062 | if (tableEnd > readPos + numRanges * V2FSEQ_SPARSE_RANGE_SIZE && tableEnd <= (int)header.size() && |
| 2713 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | (tableEnd - readPos) % V2FSEQ_SPARSE_RANGE_SIZE == 0) { |
| 2714 | 1 | int fullRanges = (tableEnd - readPos) / V2FSEQ_SPARSE_RANGE_SIZE; | |
| 2715 | 1 | uint64_t total = 0; | |
| 2716 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1 times.
|
301 | for (int i = 0; i < fullRanges; i++) { |
| 2717 | 300 | total += read3ByteUInt(&header[readPos + i * V2FSEQ_SPARSE_RANGE_SIZE + 3]); | |
| 2718 | } | ||
| 2719 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if ((fullRanges & 0xFF) == numRanges && total == m_seqChannelCount) { |
| 2720 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_SEQUENCE, "FSEQ header range count %d is the low byte of %d, using %d.\n", numRanges, fullRanges, fullRanges); |
| 2721 | 1 | numRanges = fullRanges; | |
| 2722 | } | ||
| 2723 | } | ||
| 2724 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2061 times.
|
2061 | if (readPos + numRanges * V2FSEQ_SPARSE_RANGE_SIZE > (int)header.size()) { |
| 2725 | ✗ | LogErr(VB_SEQUENCE, "FSEQ header claims %d sparse ranges but only has room for %d. File is truncated or corrupt.\n", | |
| 2726 | numRanges, ((int)header.size() - readPos) / V2FSEQ_SPARSE_RANGE_SIZE); | ||
| 2727 | ✗ | numRanges = ((int)header.size() - readPos) / V2FSEQ_SPARSE_RANGE_SIZE; | |
| 2728 | } | ||
| 2729 |
2/2✓ Branch 0 taken 2870 times.
✓ Branch 1 taken 2061 times.
|
4931 | for (int i = 0; i < numRanges; i++) { |
| 2730 | 2870 | uint32_t startChan = read3ByteUInt(&header[readPos]); | |
| 2731 | 2870 | uint32_t length = read3ByteUInt(&header[readPos + 3]); | |
| 2732 | |||
| 2733 |
1/2✓ Branch 0 taken 2870 times.
✗ Branch 1 not taken.
|
2870 | m_sparseRanges.push_back(std::pair<uint32_t, uint32_t>(startChan, length)); |
| 2734 | |||
| 2735 | 2870 | readPos += V2FSEQ_SPARSE_RANGE_SIZE; | |
| 2736 | } | ||
| 2737 | |||
| 2738 | // Validate read position matches expected header size | ||
| 2739 | // This does not include the variable headers length | ||
| 2740 | 2061 | uint16_t headerSize = read2ByteUInt(&header[8]); | |
| 2741 | |||
| 2742 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2059 times.
|
2061 | if (readPos != headerSize) { |
| 2743 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "Read position (%d) does not match expected header size %d!\n", readPos, headerSize); |
| 2744 | } | ||
| 2745 | |||
| 2746 | // Read timestamp based UUID - 8 bytes | ||
| 2747 | // This does not advance readPos since it is a fixed index | ||
| 2748 | 2061 | m_uniqueId = *((uint64_t*)&header[24]); | |
| 2749 | |||
| 2750 | // The remainder of the buffer (m_seqChanDataOffset - headerSize) contains an unknown count of variable headers | ||
| 2751 | // This will loop and continue reading until it hits padding or m_seqChanDataOffset | ||
| 2752 | // As long as readPos == headerSize prior to this call, the read is a success | ||
| 2753 |
1/2✓ Branch 0 taken 2061 times.
✗ Branch 1 not taken.
|
2061 | parseVariableHeaders(header, readPos); |
| 2754 | |||
| 2755 | // The last m_frameOffsets entry may need to be adjusted if there is extended data as the extended data is | ||
| 2756 | // placed after the channel data, but the frame offsets and sizes need to be within the channel data area | ||
| 2757 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2061 times.
|
2189 | for (auto& a : m_variableHeaders) { |
| 2758 |
6/6✓ Branch 0 taken 8 times.
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 123 times.
|
128 | if (a.extendedData && a.getExtDataOffset() < m_frameOffsets.back().second) { |
| 2759 | 5 | m_frameOffsets.back().second = a.getExtDataOffset(); | |
| 2760 | } | ||
| 2761 | } | ||
| 2762 | |||
| 2763 | // 2.3+: the whole table when it is longer than the 255 the header can count. | ||
| 2764 | // Only take it when it extends the header's table and covers the channel count, | ||
| 2765 | // so a stale copy carried along by an older writer is ignored. | ||
| 2766 |
1/2✓ Branch 0 taken 2061 times.
✗ Branch 1 not taken.
|
2061 | auto sr = std::find_if(m_variableHeaders.begin(), m_variableHeaders.end(), |
| 2767 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
128 | [](const VariableHeader& vh) { return vh.code[0] == 'S' && vh.code[1] == 'R'; }); |
| 2768 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2053 times.
|
2061 | if (sr != m_variableHeaders.end()) { |
| 2769 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | const auto& data = sr->getData(); |
| 2770 | 8 | std::vector<std::pair<uint32_t, uint32_t>> ranges; | |
| 2771 | 8 | uint64_t total = 0; | |
| 2772 |
2/2✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 8 times.
|
2408 | for (size_t pos = 0; pos + V2FSEQ_SPARSE_RANGE_SIZE <= data.size(); pos += V2FSEQ_SPARSE_RANGE_SIZE) { |
| 2773 |
1/2✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
|
2400 | ranges.emplace_back(read3ByteUInt(&data[pos]), read3ByteUInt(&data[pos + 3])); |
| 2774 | 2400 | total += ranges.back().second; | |
| 2775 | } | ||
| 2776 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
16 | if (data.size() % V2FSEQ_SPARSE_RANGE_SIZE == 0 && ranges.size() > 255 && m_sparseRanges.size() == 255 && |
| 2777 |
7/10✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 2 times.
|
16 | std::equal(m_sparseRanges.begin(), m_sparseRanges.end(), ranges.begin()) && total == m_seqChannelCount) { |
| 2778 | 6 | m_sparseRanges = std::move(ranges); | |
| 2779 | } else { | ||
| 2780 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogErr(VB_SEQUENCE, "FSEQ extended sparse range block does not match the header, using the %d ranges in the header.\n", (int)m_sparseRanges.size()); |
| 2781 | } | ||
| 2782 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | m_variableHeaders.erase(sr); |
| 2783 | 8 | } | |
| 2784 | } | ||
| 2785 | |||
| 2786 |
2/2✓ Branch 0 taken 2063 times.
✓ Branch 1 taken 48 times.
|
2111 | if (m_compressionType == CompressionType::none) { |
| 2787 |
1/2✓ Branch 0 taken 2063 times.
✗ Branch 1 not taken.
|
2063 | m_seqNumFrames = FramesPresent(fn, m_seqNumFrames, m_seqFileSize, m_seqChanDataOffset, m_seqChannelCount); |
| 2788 | } | ||
| 2789 |
1/2✓ Branch 0 taken 2111 times.
✗ Branch 1 not taken.
|
2111 | createHandler(); |
| 2790 | 2111 | } | |
| 2791 | 10488 | V2FSEQFile::~V2FSEQFile() { | |
| 2792 |
1/2✓ Branch 0 taken 2622 times.
✗ Branch 1 not taken.
|
5244 | if (m_handler) { |
| 2793 |
1/2✓ Branch 0 taken 2622 times.
✗ Branch 1 not taken.
|
5244 | delete m_handler; |
| 2794 | } | ||
| 2795 | 10488 | } | |
| 2796 | 2738 | void V2FSEQFile::dumpInfo(bool indent) { | |
| 2797 |
1/2✓ Branch 0 taken 2738 times.
✗ Branch 1 not taken.
|
2738 | FSEQFile::dumpInfo(indent); |
| 2798 | 2738 | char ind[5] = " "; | |
| 2799 |
2/2✓ Branch 0 taken 2111 times.
✓ Branch 1 taken 627 times.
|
2738 | if (!indent) { |
| 2800 | 2111 | ind[0] = 0; | |
| 2801 | } | ||
| 2802 | |||
| 2803 |
1/2✓ Branch 0 taken 2738 times.
✗ Branch 1 not taken.
|
2738 | LogDebug(VB_SEQUENCE, "%sSequence File Information\n", ind); |
| 2804 |
1/2✓ Branch 0 taken 2738 times.
✗ Branch 1 not taken.
|
2738 | LogDebug(VB_SEQUENCE, "%scompressionType : %d\n", ind, m_compressionType); |
| 2805 |
2/4✓ Branch 0 taken 2738 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2738 times.
✗ Branch 3 not taken.
|
2738 | LogDebug(VB_SEQUENCE, "%snumBlocks : %d\n", ind, m_handler->computeMaxBlocks()); |
| 2806 | // Commented out to declutter the logs ... we can add it back in if we start seeing issues | ||
| 2807 | // for (auto &a : m_frameOffsets) { | ||
| 2808 | // LogDebug(VB_SEQUENCE, "%s %d : %" PRIu64 "\n", ind, a.first, a.second); | ||
| 2809 | //} | ||
| 2810 |
1/2✓ Branch 0 taken 2738 times.
✗ Branch 1 not taken.
|
2738 | LogDebug(VB_SEQUENCE, "%snumRanges : %d\n", ind, m_sparseRanges.size()); |
| 2811 | // Commented out to declutter the logs ... we can add it back in if we start seeing issues | ||
| 2812 | // for (auto &a : m_sparseRanges) { | ||
| 2813 | // LogDebug(VB_SEQUENCE, "%s Start: %d Len: %d\n", ind, a.first, a.second); | ||
| 2814 | //} | ||
| 2815 | 2738 | } | |
| 2816 | |||
| 2817 | 1939 | bool isWithinRange(const std::vector<std::pair<uint32_t, uint32_t>>& ranges, uint32_t start_channel, uint32_t channel_count) { | |
| 2818 |
2/2✓ Branch 0 taken 4203 times.
✓ Branch 1 taken 79 times.
|
4282 | for (auto& a : ranges) { |
| 2819 |
6/6✓ Branch 0 taken 1909 times.
✓ Branch 1 taken 2294 times.
✓ Branch 2 taken 1905 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 1860 times.
✓ Branch 5 taken 45 times.
|
4203 | if ((start_channel >= a.first && start_channel < (a.first + a.second)) && ((start_channel + channel_count - 1) < (a.first + a.second))) { |
| 2820 | 1860 | return true; | |
| 2821 | } | ||
| 2822 | } | ||
| 2823 | 79 | return false; | |
| 2824 | } | ||
| 2825 | |||
| 2826 | 1937 | void V2FSEQFile::prepareRead(const std::vector<std::pair<uint32_t, uint32_t>>& ranges, uint32_t startFrame) { | |
| 2827 |
2/2✓ Branch 0 taken 1882 times.
✓ Branch 1 taken 55 times.
|
1937 | if (m_sparseRanges.empty()) { |
| 2828 | 1882 | m_rangesToRead.clear(); | |
| 2829 | 1882 | m_dataBlockSize = 0; | |
| 2830 |
2/2✓ Branch 0 taken 1884 times.
✓ Branch 1 taken 1882 times.
|
3766 | for (auto rng : ranges) { |
| 2831 | // make sure we don't read beyond the end of the sequence data | ||
| 2832 | 1884 | int toRead = rng.second; | |
| 2833 | |||
| 2834 |
2/2✓ Branch 0 taken 1883 times.
✓ Branch 1 taken 1 times.
|
1884 | if (rng.first < m_seqChannelCount) { |
| 2835 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1850 times.
|
1883 | if ((rng.first + toRead) > m_seqChannelCount) { |
| 2836 | 33 | toRead = m_seqChannelCount - rng.first; | |
| 2837 | 33 | rng.second = toRead; | |
| 2838 | } | ||
| 2839 | 1883 | m_dataBlockSize += toRead; | |
| 2840 |
1/2✓ Branch 0 taken 1883 times.
✗ Branch 1 not taken.
|
1883 | m_rangesToRead.push_back(rng); |
| 2841 | } | ||
| 2842 | } | ||
| 2843 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1881 times.
|
1882 | if (m_dataBlockSize == 0) { |
| 2844 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | m_rangesToRead.push_back(std::pair<uint32_t, uint32_t>(0, getMaxChannel())); |
| 2845 | 1 | m_dataBlockSize = getMaxChannel(); | |
| 2846 | } | ||
| 2847 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 50 times.
|
55 | } else if (m_compressionType != CompressionType::none) { |
| 2848 | // with compression, there is no way to NOT read the entire frame of data, we'll just | ||
| 2849 | // use the sparse data range since we'll have everything anyway so the ranges | ||
| 2850 | // needed is relatively irrelevant | ||
| 2851 | 5 | m_dataBlockSize = m_seqChannelCount; | |
| 2852 | 5 | m_rangesToRead = m_sparseRanges; | |
| 2853 | } else { | ||
| 2854 | // no compression with sparse ranges | ||
| 2855 | // FIXME - an intersection between the two would be useful, but hard | ||
| 2856 | // for now, just assume that if it's sparse, it has all the data that is needed | ||
| 2857 | // and read everything | ||
| 2858 | 50 | m_dataBlockSize = m_seqChannelCount; | |
| 2859 | 50 | m_rangesToRead = m_sparseRanges; | |
| 2860 | } | ||
| 2861 |
2/2✓ Branch 0 taken 1939 times.
✓ Branch 1 taken 1937 times.
|
3876 | for (auto const& [st, cnt] : ranges) { |
| 2862 |
3/4✓ Branch 0 taken 1939 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 1860 times.
|
1939 | if (!isWithinRange(m_rangesToRead, st, cnt)) { |
| 2863 |
1/2✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
|
79 | LogErr(VB_SEQUENCE, "Requested output range outside read ranges. Requested %d channels starting at %d\n", cnt, st); |
| 2864 | } | ||
| 2865 | } | ||
| 2866 | 1937 | m_handler->m_readContext.reset(); | |
| 2867 | 1937 | m_handler->prepareRead(startFrame); | |
| 2868 | 1937 | } | |
| 2869 | 215125 | FrameData* V2FSEQFile::getFrame(uint32_t frame) { | |
| 2870 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 214969 times.
|
215125 | if (m_rangesToRead.empty()) { |
| 2871 | 156 | std::vector<std::pair<uint32_t, uint32_t>> range; | |
| 2872 |
2/4✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
|
156 | range.push_back(std::pair<uint32_t, uint32_t>(0, getMaxChannel())); |
| 2873 |
1/2✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
|
156 | prepareRead(range, frame); |
| 2874 | 156 | } | |
| 2875 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 215115 times.
|
215125 | if (frame >= m_seqNumFrames) { |
| 2876 | 10 | return nullptr; | |
| 2877 | } | ||
| 2878 |
1/2✓ Branch 0 taken 215115 times.
✗ Branch 1 not taken.
|
215115 | if (m_handler != nullptr) { |
| 2879 | 215115 | FrameData* fd = nullptr; | |
| 2880 | try { | ||
| 2881 |
1/2✓ Branch 0 taken 215115 times.
✗ Branch 1 not taken.
|
215115 | fd = m_handler->getFrame(frame); |
| 2882 | ✗ | } catch (...) { | |
| 2883 | ✗ | LogErr(VB_SEQUENCE, "Error getting frame from handler %s.\n", m_handler->GetType().c_str()); | |
| 2884 | ✗ | } | |
| 2885 | 215115 | return fd; | |
| 2886 | } | ||
| 2887 | ✗ | return nullptr; | |
| 2888 | } | ||
| 2889 | 360681 | void V2FSEQFile::addFrame(uint32_t frame, | |
| 2890 | const uint8_t* data) { | ||
| 2891 |
1/2✓ Branch 0 taken 360681 times.
✗ Branch 1 not taken.
|
360681 | if (m_handler != nullptr) { |
| 2892 | 360681 | m_handler->addFrame(frame, data); | |
| 2893 | } | ||
| 2894 | 360681 | } | |
| 2895 | |||
| 2896 | 627 | void V2FSEQFile::finalize() { | |
| 2897 |
1/2✓ Branch 0 taken 627 times.
✗ Branch 1 not taken.
|
627 | if (m_handler != nullptr) { |
| 2898 | 627 | m_handler->finalize(); | |
| 2899 | } | ||
| 2900 | 627 | FSEQFile::finalize(); | |
| 2901 | 627 | } | |
| 2902 | |||
| 2903 | 216 | uint32_t V2FSEQFile::getMaxChannel() const { | |
| 2904 | 216 | uint64_t ret = m_seqChannelCount; | |
| 2905 |
2/2✓ Branch 0 taken 905 times.
✓ Branch 1 taken 216 times.
|
1121 | for (auto& a : m_sparseRanges) { |
| 2906 | 905 | uint64_t m = (uint64_t)a.first + (uint64_t)a.second; | |
| 2907 |
2/2✓ Branch 0 taken 464 times.
✓ Branch 1 taken 441 times.
|
905 | if (m > ret) { |
| 2908 | 464 | ret = m; | |
| 2909 | } | ||
| 2910 | } | ||
| 2911 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
|
216 | if (ret > 0xFFFFFFFFu) { |
| 2912 | ✗ | ret = 0xFFFFFFFFu; | |
| 2913 | } | ||
| 2914 | 216 | return (uint32_t)ret; | |
| 2915 | } | ||
| 2916 |