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
21#include <vrs/DataSlice.h>
22#include <vrs/ForwardDefinitions.h>
23#include <vrs/VrsExport.h>
24#include <vrs/WriteFileHandler.h>
25#include <vrs/helpers/IOVector.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
100 int compress(
101 const void* data,
102 size_t dataSize,
103 CompressionPreset preset,
104 uint32_t& outCompressedSize,
105 size_t headerSpace = 0);
106
118 int compress(
119 const DataSlice* dataSlices,
120 size_t dataSliceCount,
121 CompressionPreset preset,
122 uint32_t& outCompressedSize,
123 size_t headerSpace = 0);
124
136 int startFrame(size_t frameSize, CompressionPreset preset, uint32_t& outSize);
148 int addFrameData(
149 WriteFileHandler& file,
150 const void* data,
151 size_t dataSize,
152 uint32_t& inOutCompressedSize,
153 size_t maxCompressedSize = 0);
164 int endFrame(WriteFileHandler& file, uint32_t& inOutCompressedSize, size_t maxCompressedSize = 0);
165
168 const void* getData() const {
169 return buffer_.data();
170 }
172 template <class HeaderType>
173 HeaderType* getHeader() {
174 return reinterpret_cast<HeaderType*>(buffer_.data());
175 }
176 CompressionType getCompressionType() const;
177
179 void clear() {
180 buffer_.release();
181 }
182
183 static bool shouldTryToCompress(CompressionPreset preset, size_t size);
184
185 private:
186 class CompressorImpl;
187 std::unique_ptr<CompressorImpl> impl_;
188 helpers::IOVector<uint8_t> buffer_;
189};
190
191} // namespace vrs
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:83
const void * getData() const
Definition Compressor.h:168
void clear()
Deallocate the internal buffer's storage.
Definition Compressor.h:179
HeaderType * getHeader()
Get the space reserved for a header.
Definition Compressor.h:173
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:43
Definition Compressor.cpp:129
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:41
A non-owning view of a contiguous block of memory.
Definition DataSlice.h:24