RvlCompression.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 "RvlCompression.h"
5 #include <cstdint>
6 #include <cstring>
7 #include <iostream>
9 
10 RvlCompression::RvlCompression(int t_width, int t_height, rs2_format t_format, int t_bpp)
11  :ICompression(t_width, t_height, t_format, t_bpp)
12 {
13 }
14 
15 int RvlCompression::encodeVLE(int t_value)
16 {
17  do
18  {
19  int nibble = t_value & 0x7; // lower 3 bits
20  if(t_value >>= 3)
21  nibble |= 0x8; // more to come
22  m_word <<= 4;
23  m_word |= nibble;
24  if(++m_nibblesWritten == 8) // output word
25  {
26  *m_pBuffer++ = m_word;
27  m_nibblesWritten = 0;
28  m_word = 0;
29  }
30  } while(t_value);
31 
32  return 0;
33 }
34 
36 {
37  unsigned int nibble;
38  int value = 0, bits = 29;
39  do
40  {
41  if(!m_nibblesWritten)
42  {
43  m_word = *m_pBuffer++; // load word
44  m_nibblesWritten = 8;
45  }
46  nibble = m_word & 0xf0000000;
47  value |= (nibble << 1) >> bits;
48  m_word <<= 4;
50  bits -= 3;
51  } while(nibble & 0x80000000);
52  return value;
53 }
54 
55 int RvlCompression::compressBuffer(unsigned char* t_buffer, int t_size, unsigned char* t_compressedBuf)
56 {
57  short* buffer2 = (short*)t_buffer;
58  int* pHead = m_pBuffer = (int*)t_compressedBuf + 1;
59  m_nibblesWritten = 0;
60  short* end = buffer2 + t_size / m_bpp;
61  short previous = 0;
62  while(buffer2 != end)
63  {
64  int zeros = 0, nonzeros = 0;
65  for(; (buffer2 != end) && !*buffer2; buffer2++, zeros++)
66  ;
67  encodeVLE(zeros);
68  for(short* p = buffer2; (p != end) && *p++; nonzeros++)
69  ;
70  encodeVLE(nonzeros);
71  for(int i = 0; i < nonzeros; i++)
72  {
73  short current = *buffer2++;
74  int delta = current - previous;
75  int positive = (delta << 1) ^ (delta >> 31);
76  encodeVLE(positive);
77  previous = current;
78  }
79  }
80  if(m_nibblesWritten) // last few values
81  *m_pBuffer++ = m_word << 4 * (8 - m_nibblesWritten);
82  int compressedSize = int((char*)m_pBuffer - (char*)pHead);
83  int compressWithHeaderSize = compressedSize + sizeof(compressedSize);
84  if(compressWithHeaderSize > t_size)
85  {
86  ERR << "Compression overflow, destination buffer is smaller than the compressed size";
87  return -1;
88  }
89  memcpy(t_compressedBuf, &compressedSize, sizeof(compressedSize));
90  if(m_compFrameCounter++ % 50 == 0)
91  {
92  INF << "frame " << m_compFrameCounter << "\tdepth\tcompression\tlz4\t" << t_size << "\t/\t" << compressedSize;
93  }
94  memcpy(t_compressedBuf, &compressedSize, sizeof(compressedSize));
95  return compressWithHeaderSize;
96 }
97 
98 int RvlCompression::decompressBuffer(unsigned char* t_buffer, int t_size, unsigned char* t_uncompressedBuf)
99 {
100  short* currentPtr = (short*)t_uncompressedBuf;
101  m_pBuffer = (int*)t_buffer + 1;
102  m_nibblesWritten = 0;
103  short current, previous = 0;
104  unsigned int compressedSize;
105  int numPixelsToDecode = t_size / 2;
106  while(numPixelsToDecode)
107  {
108  int zeros = decodeVLE();
109  numPixelsToDecode -= zeros;
110  for(; zeros; zeros--)
111  *currentPtr++ = 0;
112  int nonzeros = decodeVLE();
113  numPixelsToDecode -= nonzeros;
114  for(; nonzeros; nonzeros--)
115  {
116  int positive = decodeVLE();
117  int delta = (positive >> 1) ^ -(positive & 1);
118  current = previous + delta;
119  *currentPtr++ = current;
120  previous = current;
121  }
122  }
123  int uncompressedSize = int((char*)currentPtr - (char*)t_uncompressedBuf);
124  if(m_decompFrameCounter++ % 50 == 0)
125  {
126  INF << "frame " << m_decompFrameCounter << "\tdepth\tcompression\tlz4\t" << compressedSize << "\t/\t" << uncompressedSize;
127  }
128  return uncompressedSize;
129 }
GLuint GLuint end
#define ERR
Definition: NetdevLog.h:9
GLfloat GLfloat p
Definition: glext.h:12687
int encodeVLE(int value)
int m_compFrameCounter
Definition: ICompression.h:21
#define INF
Definition: NetdevLog.h:11
GLfloat value
int m_decompFrameCounter
Definition: ICompression.h:21
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
Definition: glext.h:6898
int decompressBuffer(unsigned char *t_buffer, int t_size, unsigned char *t_uncompressedBuf)
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
RvlCompression(int t_width, int t_height, rs2_format t_format, int t_bpp)
int i
int compressBuffer(unsigned char *t_buffer, int t_size, unsigned char *t_compressedBuf)


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