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/ForwardDefinitions.h>
22#include <vrs/VrsExport.h>
23#include <vrs/WriteFileHandler.h>
24#include <vrs/helpers/IOVector.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
68VRS_API string toPrettyName(CompressionPreset preset);
69VRS_API string toString(CompressionPreset preset);
70template <>
71VRS_API CompressionPreset toEnum<CompressionPreset>(const string& presetName);
72
73VRS_API string toString(CompressionType compressionType);
74template <>
75VRS_API CompressionType toEnum<CompressionType>(const string& compressionTypeName);
76
82class VRS_API Compressor {
83 public:
84 static const size_t kMinByteCountForCompression; // don't try to compress small payloads
85
86 Compressor();
88
99 int compress(
100 const void* data,
101 size_t dataSize,
102 CompressionPreset preset,
103 uint32_t& outCompressedSize,
104 size_t headerSpace = 0);
105
117 int startFrame(size_t frameSize, CompressionPreset zstdPreset, uint32_t& outSize);
129 int addFrameData(
130 WriteFileHandler& file,
131 const void* data,
132 size_t dataSize,
133 uint32_t& inOutCompressedSize,
134 size_t maxCompressedSize = 0);
145 int endFrame(WriteFileHandler& file, uint32_t& inOutCompressedSize, size_t maxCompressedSize = 0);
146
149 const void* getData() const {
150 return buffer_.data();
151 }
153 template <class HeaderType>
154 HeaderType* getHeader() {
155 return reinterpret_cast<HeaderType*>(buffer_.data());
156 }
157 CompressionType getCompressionType() const;
158
160 void clear() {
161 buffer_.release();
162 }
163
164 static bool shouldTryToCompress(CompressionPreset preset, size_t size);
165
166 private:
167 class CompressorImpl;
168 std::unique_ptr<CompressorImpl> impl_;
169 helpers::IOVector<uint8_t> buffer_;
170};
171
172} // namespace vrs
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:82
const void * getData() const
Definition Compressor.h:149
void clear()
Deallocate the internal buffer's storage.
Definition Compressor.h:160
HeaderType * getHeader()
Get the space reserved for a header.
Definition Compressor.h:154
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:43
Definition Compressor.cpp:119
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:39