in lib/DepthVideo.cpp [121:298]
void DepthVideo::load(const std::string& path) {
reset();
LOG(INFO) << "Loading depth video '" << path << "'...";
path_ = path;
std::string binaryFile = path + "/video.dat";
if (!fs::exists(binaryFile)) {
throw std::runtime_error("Could not find 'video.dat'.");
}
std::ifstream is(binaryFile, std::ios::binary);
if (!is) {
throw std::runtime_error("Could not open 'video.dat'.");
}
// Header
uint32_t magic = read<uint32_t>(is);
if (magic != 0xDEADBEEF) {
throw std::runtime_error(
"Did not see magic marker at beginning of file.");
}
uint32_t fileFormat = read<uint32_t>(is);
uint32_t dpFormat = read<uint32_t>(is);
if (fileFormat > kFileFormatVersion) {
throw std::runtime_error("File format too new.");
}
if (fileFormat < kMinSupportedFileFormat) {
throw std::runtime_error("File format too old.");
}
int numFrames = read<int>(is);
// Read frames.
metaFrames_.resize(numFrames);
for (int frame = 0; frame < metaFrames_.size(); ++frame) {
float pts = read<float>(is);
metaFrames_[frame] = std::make_unique<MetaFrame>(pts);
if (frame > 0) {
metaFrames_[frame - 1]->duration_ = pts - metaFrames_[frame - 1]->pts_;
}
}
// Read color streams.
int numColorStreams = read<int>(is);
colorStreams_.resize(numColorStreams);
for (int csi = 0; csi < colorStreams_.size(); ++csi) {
// Using 'new' to access ColorStream's private constructor.
colorStreams_[csi] = std::unique_ptr<ColorStream>(new ColorStream(*this));
ColorStream& cs = *colorStreams_[csi];
cs.name_ = readstr(is);
cs.setDir(readstr(is));
cs.extension_ = readstr(is);
if (fileFormat >= 7) {
read(is, cs.type_);
} else {
if (cs.name_ == "dynamic_mask") {
cs.type_ = CV_8UC1;
} else {
cs.type_ = CV_32FC3;
}
}
cs.width_ = read<int>(is);
cs.height_ = read<int>(is);
// if (fileFormat >= 12) {
// bool hasGopTable = read<bool>(is);
// if (hasGopTable) {
// cs.gopTable_ = std::make_unique<GopTable>();
// cs.gopTable_->fread(is, fileFormat);
// }
// }
// Read color frames.
cs.frames_.resize(numFrames);
for (int frame = 0; frame < cs.frames_.size(); ++frame) {
// Using 'new' to access ColorFrame's private constructor.
cs.frames_[frame] =
std::unique_ptr<ColorFrame>(new ColorFrame(cs, frame));
}
}
// Read depth streams.
int numDepthStreams = read<int>(is);
depthStreams_.resize(numDepthStreams);
for (int dsi = 0; dsi < depthStreams_.size(); ++dsi) {
// Using 'new' to access DepthStream's private constructor.
depthStreams_[dsi] = std::unique_ptr<DepthStream>(new DepthStream(*this));
DepthStream& ds = *depthStreams_[dsi];
ds.name_ = readstr(is);
ds.setDir(readstr(is));
if (fileFormat < 10) {
// Backward-compatible reading code: implicit depth xform type.
std::string str = readstr(is);
ds.depthXformDesc_.parse(str);
ds.spatialXformDesc_.type = XformType::Spatial;
ds.spatialXformDesc_.spatialType = SpatialXformType::Identity;
} else {
ds.depthXformDesc_.fread(is);
ds.spatialXformDesc_.fread(is);
}
ds.width_ = read<int>(is);
ds.height_ = read<int>(is);
// if (fileFormat >= 13) {
// bool hasGopTable = read<bool>(is);
// if (hasGopTable) {
// ds.gopTable_ = std::make_unique<GopTable>();
// ds.gopTable_->fread(is, fileFormat);
// ds.quantTable_ = std::make_unique<QuantTable>();
// ds.quantTable_->fread(is, fileFormat);
// }
// }
// Read depth frames.
ds.frames_.resize(numFrames);
for (int frame = 0; frame < ds.frames_.size(); ++frame) {
// Using 'new' to access DepthFrame's private constructor.
ds.frames_[frame] =
std::unique_ptr<DepthFrame>(new DepthFrame(*this, ds, frame));
DepthFrame& f = *ds.frames_[frame];
f.intrinsics.fread(is, dpFormat);
f.extrinsics.fread(is, dpFormat);
if (fileFormat >= 11) {
read(is, f.enabled);
}
if (fileFormat < 10) {
// Backward-compatible reading code: implicit depth xform type.
XformDescriptor desc;
desc.type = XformType::Depth;
std::string str = readstr(is);
desc.parse(str);
f.depthXform_ = createDepthXform(desc);
is.read(
reinterpret_cast<char*>(f.depthXform_->data()),
sizeof(double) * f.depthXform_->numParams());
f.spatialXform_ = createSpatialXform(ds.spatialXformDesc_);
} else {
DepthXform* depthPtr =
static_cast<DepthXform*>(readXform(is).release());
f.depthXform_ = std::unique_ptr<DepthXform>(depthPtr);
SpatialXform* spatialPtr =
static_cast<SpatialXform*>(readXform(is).release());
f.spatialXform_ = std::unique_ptr<SpatialXform>(spatialPtr);
}
if (f.depthXform_->desc() != ds.depthXformDesc_) {
throw std::runtime_error("Inconsistent depth transform.");
}
}
}
read(is, duration_);
read(is, width_);
read(is, height_);
read(is, aspect_);
read(is, invAspect_);
metaFrames_.back()->duration_ = duration_ - metaFrames_.back()->pts_;
magic = read<uint32_t>(is);
if (magic != 0xDEADBEEF) {
throw std::runtime_error("Did not see magic marker at end of file.");
}
}