Lz4Compression.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #include "Lz4Compression.h"
5 #include <cstring>
6 #include <iostream>
8 
9 Lz4Compression::Lz4Compression(int t_width, int t_height, rs2_format t_format, int t_bpp)
10  :ICompression(t_width, t_height, t_format, t_bpp)
11 {
12 }
13 
14 int Lz4Compression::compressBuffer(unsigned char* t_buffer, int t_size, unsigned char* t_compressedBuf)
15 {
16  const int maxDstSize = LZ4_compressBound(t_size);
17  const int compressedSize = LZ4_compress_default((const char*)t_buffer, (char*)t_compressedBuf + sizeof(int), t_size, maxDstSize);
18  if(compressedSize <= 0)
19  {
20  ERR << "Failure trying to compress the data.";
21  return -1;
22  }
23  int compressWithHeaderSize = compressedSize + sizeof(compressedSize);
24  if(compressWithHeaderSize > t_size)
25  {
26  ERR << "Compression overflow, destination buffer is smaller than the compressed size.";
27  return -1;
28  }
29  if(m_compFrameCounter++ % 50 == 0)
30  {
31  INF << "frame " << m_compFrameCounter << "\tdepth\tcompression\tlz4\t" << t_size << "\t/\t" << compressedSize;
32  }
33  memcpy(t_compressedBuf, &compressedSize, sizeof(compressedSize));
34  return compressWithHeaderSize;
35 }
36 
37 int Lz4Compression::decompressBuffer(unsigned char* t_buffer, int t_compressedSize, unsigned char* t_uncompressedBuf)
38 {
39  const int decompressed_size = LZ4_decompress_safe((const char*)t_buffer, (char*)t_uncompressedBuf, t_compressedSize, m_width * m_height * m_bpp);
40  if(decompressed_size < 0)
41  {
42  ERR << "Failure trying to decompress the frame.";
43  return -1;
44  }
45  int original_size = m_width * m_height * m_bpp;
46  if(m_decompFrameCounter++ % 50 == 0)
47  {
48  INF << "frame " << m_decompFrameCounter << "\tdepth\tdecompression\tlz4\t" << t_compressedSize << "\t/\t" << decompressed_size;
49  }
50  return decompressed_size;
51 }
#define ERR
Definition: NetdevLog.h:9
int m_compFrameCounter
Definition: ICompression.h:21
#define INF
Definition: NetdevLog.h:11
int m_decompFrameCounter
Definition: ICompression.h:21
int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:1262
int LZ4_compress_default(const char *source, char *dest, int inputSize, int maxOutputSize)
Definition: lz4.c:708
Lz4Compression(int t_width, int t_height, rs2_format t_format, int t_bpp)
int decompressBuffer(unsigned char *t_buffer, int t_size, unsigned char *t_uncompressedBuf)
int compressBuffer(unsigned char *t_buffer, int t_size, unsigned char *t_compressedBuf)
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
LZ4LIB_API char int compressedSize
Definition: lz4.h:456
LZ4LIB_API char int int maxDstSize
Definition: lz4.h:456
int LZ4_compressBound(int isize)
Definition: lz4.c:406


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:21