You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.2 KiB
71 lines
2.2 KiB
4 years ago
|
#include "selfdrive/ui/replay/logreader.h"
|
||
5 years ago
|
|
||
4 years ago
|
#include <cassert>
|
||
4 years ago
|
#include <bzlib.h>
|
||
4 years ago
|
#include "selfdrive/common/util.h"
|
||
5 years ago
|
|
||
4 years ago
|
static bool decompressBZ2(std::vector<uint8_t> &dest, const char srcData[], size_t srcSize,
|
||
|
size_t outputSizeIncrement = 0x100000U) {
|
||
|
bz_stream strm = {};
|
||
|
int ret = BZ2_bzDecompressInit(&strm, 0, 0);
|
||
|
assert(ret == BZ_OK);
|
||
|
|
||
|
strm.next_in = const_cast<char *>(srcData);
|
||
|
strm.avail_in = srcSize;
|
||
|
do {
|
||
|
strm.next_out = (char *)&dest[strm.total_out_lo32];
|
||
|
strm.avail_out = dest.size() - strm.total_out_lo32;
|
||
|
ret = BZ2_bzDecompress(&strm);
|
||
|
if (ret == BZ_OK && strm.avail_in > 0 && strm.avail_out == 0) {
|
||
|
dest.resize(dest.size() + outputSizeIncrement);
|
||
|
}
|
||
|
} while (ret == BZ_OK);
|
||
5 years ago
|
|
||
4 years ago
|
BZ2_bzDecompressEnd(&strm);
|
||
|
dest.resize(strm.total_out_lo32);
|
||
|
return ret == BZ_STREAM_END;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
LogReader::~LogReader() {
|
||
4 years ago
|
for (auto e : events) delete e;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
bool LogReader::load(const std::string &file) {
|
||
4 years ago
|
raw_.resize(1024 * 1024 * 64);
|
||
4 years ago
|
std::string dat = util::read_file(file);
|
||
|
if (dat.empty() || !decompressBZ2(raw_, dat.data(), dat.size())) {
|
||
|
LOGW("bz2 decompress failed");
|
||
|
return false;
|
||
4 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
auto insertEidx = [&](CameraType type, const cereal::EncodeIndex::Reader &e) {
|
||
|
eidx[type][e.getFrameId()] = {e.getSegmentNum(), e.getSegmentId()};
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
kj::ArrayPtr<const capnp::word> words((const capnp::word *)raw_.data(), raw_.size() / sizeof(capnp::word));
|
||
4 years ago
|
while (words.size() > 0) {
|
||
4 years ago
|
try {
|
||
|
std::unique_ptr<Event> evt = std::make_unique<Event>(words);
|
||
|
switch (evt->which) {
|
||
|
case cereal::Event::ROAD_ENCODE_IDX:
|
||
|
insertEidx(RoadCam, evt->event.getRoadEncodeIdx());
|
||
|
break;
|
||
|
case cereal::Event::DRIVER_ENCODE_IDX:
|
||
|
insertEidx(DriverCam, evt->event.getDriverEncodeIdx());
|
||
|
break;
|
||
|
case cereal::Event::WIDE_ROAD_ENCODE_IDX:
|
||
|
insertEidx(WideRoadCam, evt->event.getWideRoadEncodeIdx());
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
5 years ago
|
}
|
||
4 years ago
|
words = kj::arrayPtr(evt->reader.getEnd(), words.end());
|
||
4 years ago
|
events.push_back(evt.release());
|
||
4 years ago
|
} catch (const kj::Exception &e) {
|
||
4 years ago
|
return false;
|
||
5 years ago
|
}
|
||
|
}
|
||
4 years ago
|
std::sort(events.begin(), events.end(), Event::lessThan());
|
||
|
return true;
|
||
5 years ago
|
}
|