VRS
A file format for sensor data.
Loading...
Searching...
No Matches
Compressor.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "ForwardDefinitions.h"
24#include "WriteFileHandler.h"
25
26namespace vrs {
27
39 Undefined = -1,
40 None = 0,
41 Lz4Fast,
42 Lz4Tight,
43
44 ZstdFaster,
45 ZstdFast,
46 ZstdLight,
47 ZstdMedium,
48 ZstdHeavy,
49 ZstdHigh,
50 ZstdTight,
51 ZstdMax,
52
53 COUNT,
54
55 FirstLz4Preset = Lz4Fast,
56 LastLz4Preset = Lz4Tight,
57
58 FirstZstdPreset = ZstdFaster,
59 LastZstdPreset = ZstdMax,
60
61 CompressedFirst = Lz4Fast,
62 CompressedLast = ZstdMax,
63
64 Default = ZstdFaster
65
66};
67
68std::string toPrettyName(CompressionPreset preset);
69std::string toString(CompressionPreset preset);
70template <>
71CompressionPreset toEnum<CompressionPreset>(const std::string& presetName);
72
73std::string toString(CompressionType compressionType);
74template <>
75CompressionType toEnum<CompressionType>(const std::string& compressionTypeName);
76
83 public:
84 static const size_t kMinByteCountForCompression; // don't try to compress small payloads
85
86 Compressor();
88
98 uint32_t
99 compress(const void* data, size_t dataSize, CompressionPreset preset, size_t headerSpace = 0);
100
112 int startFrame(size_t frameSize, CompressionPreset zstdPreset, uint32_t& outSize);
124 int addFrameData(
125 WriteFileHandler& file,
126 const void* data,
127 size_t dataSize,
128 uint32_t& inOutCompressedSize,
129 size_t maxCompressedSize = 0);
139 int endFrame(WriteFileHandler& file, uint32_t& inOutCompressedSize, size_t maxCompressedSize = 0);
140
143 const void* getData() const {
144 return buffer_.data();
145 }
147 template <class HeaderType>
148 HeaderType* getHeader() {
149 return reinterpret_cast<HeaderType*>(buffer_.data());
150 }
151 CompressionType getCompressionType() const;
152
154 void clear() {
155 std::vector<uninitialized_byte> blank;
156 buffer_.swap(blank);
157 }
158
159 static bool shouldTryToCompress(CompressionPreset preset, size_t size);
160
161 struct uninitialized_byte final {
162 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, modernize-use-equals-default)
163 uninitialized_byte() {} // do not use '= default' as it will initialize byte!
164 uint8_t byte;
165 };
166
167 private:
168 class CompressorImpl;
169 std::unique_ptr<CompressorImpl> impl_;
170 std::vector<uninitialized_byte> buffer_;
171};
172
173} // namespace vrs
Definition Compressor.cpp:156
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:82
uint32_t compress(const void *data, size_t dataSize, CompressionPreset preset, size_t headerSpace=0)
Definition Compressor.cpp:306
const void * getData() const
Definition Compressor.h:143
void clear()
Really deallocate the buffer's memory (clear() doesn't do that)
Definition Compressor.h:154
int startFrame(size_t frameSize, CompressionPreset zstdPreset, uint32_t &outSize)
Definition Compressor.cpp:323
int addFrameData(WriteFileHandler &file, const void *data, size_t dataSize, uint32_t &inOutCompressedSize, size_t maxCompressedSize=0)
Definition Compressor.cpp:333
int endFrame(WriteFileHandler &file, uint32_t &inOutCompressedSize, size_t maxCompressedSize=0)
Definition Compressor.cpp:345
HeaderType * getHeader()
Get the space reserved for a header.
Definition Compressor.h:148
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:45
Definition AsyncDiskFileChunk.hpp:49
CompressionPreset
VRS Compression setting.
Definition Compressor.h:38
@ Lz4Tight
Slower compression speed, very fast decompression, better compression ratio.
@ None
No compression.
@ Default
Default preset.
@ Lz4Fast
Fast compression speed, very fast decompression, not great compression ratio.
@ Undefined
when not set explicitly
CompressionType
Type of compression. Used in VRS record headers, so never modify the values.
Definition Record.h:35
Definition Compressor.h:161