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 <vrs/ForwardDefinitions.h>
24#include <vrs/VrsExport.h>
25#include <vrs/WriteFileHandler.h>
26
27namespace vrs {
28
40 Undefined = -1,
41 None = 0,
42 Lz4Fast,
43 Lz4Tight,
44
45 ZstdFaster,
46 ZstdFast,
47 ZstdLight,
48 ZstdMedium,
49 ZstdHeavy,
50 ZstdHigh,
51 ZstdTight,
52 ZstdMax,
53
54 COUNT,
55
56 FirstLz4Preset = Lz4Fast,
57 LastLz4Preset = Lz4Tight,
58
59 FirstZstdPreset = ZstdFaster,
60 LastZstdPreset = ZstdMax,
61
62 CompressedFirst = Lz4Fast,
63 CompressedLast = ZstdMax,
64
65 Default = ZstdFaster
66
67};
68
69VRS_API string toPrettyName(CompressionPreset preset);
70VRS_API string toString(CompressionPreset preset);
71template <>
72VRS_API CompressionPreset toEnum<CompressionPreset>(const string& presetName);
73
74VRS_API string toString(CompressionType compressionType);
75template <>
76VRS_API CompressionType toEnum<CompressionType>(const string& compressionTypeName);
77
83class VRS_API Compressor {
84 public:
85 static const size_t kMinByteCountForCompression; // don't try to compress small payloads
86
87 Compressor();
89
99 uint32_t
100 compress(const void* data, size_t dataSize, CompressionPreset preset, size_t headerSpace = 0);
101
113 int startFrame(size_t frameSize, CompressionPreset zstdPreset, uint32_t& outSize);
125 int addFrameData(
126 WriteFileHandler& file,
127 const void* data,
128 size_t dataSize,
129 uint32_t& inOutCompressedSize,
130 size_t maxCompressedSize = 0);
140 int endFrame(WriteFileHandler& file, uint32_t& inOutCompressedSize, size_t maxCompressedSize = 0);
141
144 const void* getData() const {
145 return buffer_.data();
146 }
148 template <class HeaderType>
149 HeaderType* getHeader() {
150 return reinterpret_cast<HeaderType*>(buffer_.data());
151 }
152 CompressionType getCompressionType() const;
153
155 void clear() {
156 std::vector<uninitialized_byte> blank;
157 buffer_.swap(blank);
158 }
159
160 static bool shouldTryToCompress(CompressionPreset preset, size_t size);
161
162 struct uninitialized_byte final {
163 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, modernize-use-equals-default)
164 uninitialized_byte() {} // do not use '= default' as it will initialize byte!
165 uint8_t byte;
166 };
167
168 private:
169 class CompressorImpl;
170 std::unique_ptr<CompressorImpl> impl_;
171 std::vector<uninitialized_byte> buffer_;
172};
173
174} // namespace vrs
Definition Compressor.cpp:131
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:83
const void * getData() const
Definition Compressor.h:144
void clear()
Really deallocate the buffer's memory (clear() doesn't do that)
Definition Compressor.h:155
HeaderType * getHeader()
Get the space reserved for a header.
Definition Compressor.h:149
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:43
Definition Compressor.cpp:113
CompressionPreset
VRS Compression setting.
Definition Compressor.h:39
@ 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:38
Definition Compressor.h:162