Skip to content

Commit

Permalink
Merge pull request IntelRealSense#21 from IntelRealSense/mchanan_colo…
Browse files Browse the repository at this point in the history
…r_compression

add color compression with gzip
  • Loading branch information
mchanan authored Jan 2, 2020
2 parents 0e47623 + 8c3eeb6 commit 8785a34
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/ethernet/IdecompressFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

class IdecompressFrame {
public:
virtual void decompressFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf) = 0;
virtual void decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf) = 0;
virtual void decompressDepthFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf) = 0;
};
10 changes: 7 additions & 3 deletions src/ethernet/decompressFrameGzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
#include <zlib.h>
#include "decompressFrameGzip.h"

void decompressFrameGzip::decompressFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
{

void decompressFrameGzip::decompressDepthFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
{
unsigned int compressedSize;
int windowsBits = 15;
int GZIP_ENCODING = 16;
Expand All @@ -46,4 +45,9 @@ void decompressFrameGzip::decompressFrame(unsigned char* buffer, int size, unsi
float zipRatio = fullSizeSum/(float)compressedSizeSum;
frameCounter++;
printf("gzip zip ratio is: %0.2f , frameCounter: %d\n", zipRatio, frameCounter);
}

void decompressFrameGzip::decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
{
decompressDepthFrame(buffer,size, uncompressedBuf);
}
3 changes: 2 additions & 1 deletion src/ethernet/decompressFrameGzip.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
class decompressFrameGzip :public IdecompressFrame
{
public:
void decompressFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
void decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
void decompressDepthFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
private:
long long fullSizeSum = 0, compressedSizeSum = 0, frameCounter = 0; //for ratio statistics
z_stream strm;
Expand Down
2 changes: 1 addition & 1 deletion src/ethernet/decompressFrameRVL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int decompressFrameRVL::decodeVLE()
return value;
}

void decompressFrameRVL::decompressFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
void decompressFrameRVL::decompressDepthFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
{
short* uncompressedBuf2 = (short*)uncompressedBuf;
pBuffer = (int*)buffer + 1;
Expand Down
3 changes: 2 additions & 1 deletion src/ethernet/decompressFrameRVL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
class decompressFrameRVL :public IdecompressFrame
{
public:
void decompressFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
void decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf) {};
void decompressDepthFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
private:
int decodeVLE();
int *pBuffer, word, nibblesWritten;
Expand Down
10 changes: 7 additions & 3 deletions src/ethernet/ethernet-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ rs2::ethernet_device::ethernet_device()
{
dev = rs2_create_software_device(NULL);
#ifdef COMPRESSION
idecomress = decompressFrameFactory::create(zipMethod::gzip);
iDecomressColor = decompressFrameFactory::create(zipMethod::gzip);
iDecomressDepth = decompressFrameFactory::create(zipMethod::gzip);
#endif
}

Expand Down Expand Up @@ -178,9 +179,12 @@ void rs2::ethernet_device::pull_from_queue(int stream_index)
#ifdef COMPRESSION
if (stream_index == 0) {
// depth
idecomress->decompressFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(last_frame[stream_index].pixels));
iDecomressDepth->decompressDepthFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(last_frame[stream_index].pixels));
} else if(stream_index == 1){
//color
iDecomressColor->decompressColorFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(last_frame[stream_index].pixels));
//iDecomressColor->decompressColorFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(last_frame[stream_index].pixels));
} else {
// other -> color
#endif
memcpy(last_frame[stream_index].pixels, frame->m_buffer, frame->m_size);
#ifdef COMPRESSION
Expand Down
3 changes: 2 additions & 1 deletion src/ethernet/ethernet-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ namespace rs2
rs2_software_video_frame last_frame[2];
std::vector<uint8_t> pixels_buff[2];

IdecompressFrame* idecomress;
IdecompressFrame* iDecomressColor;
IdecompressFrame* iDecomressDepth;
};

class RS_RTSPFrameCallback: public RTSPCallback
Expand Down
3 changes: 2 additions & 1 deletion tools/rs-server/IcompressFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

class IcompressFrame {
public:
virtual int compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf) = 0;
virtual int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf) = 0;
virtual int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf) = 0;
};
8 changes: 6 additions & 2 deletions tools/rs-server/RsSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void RsDeviceSource::doGetNextFrame()
void RsDeviceSource::deliverRSFrame()
{
#ifdef COMPRESSION
IcompressFrame* iCompress = compressFrameFactory::create(zipMethod::gzip);
IcompressFrame* iCompressColor = compressFrameFactory::create(zipMethod::gzip);
IcompressFrame* iCompressDepth = compressFrameFactory::create(zipMethod::gzip);
#endif
if (!isCurrentlyAwaitingData())
{
Expand All @@ -124,7 +125,10 @@ void RsDeviceSource::deliverRSFrame()
#ifdef COMPRESSION
if(fParams.sensorID == 0)
{
iCompress->compressFrame(fbuf, fFrameSize, fTo);
iCompressDepth->compressDepthFrame(fbuf, fFrameSize, fTo);
} else if(fParams.sensorID == 1)
{
iCompressColor->compressColorFrame(fbuf, fFrameSize, fTo);
} else {
#endif
memmove(fTo, fbuf, fFrameSize);
Expand Down
42 changes: 23 additions & 19 deletions tools/rs-server/compressFrameGzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
#include "compressFrameGzip.h"


int compressFrameGzip::compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf)
int compressFrameGzip::compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf)
{
int windowsBits = 15;
int GZIP_ENCODING = 16;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = (Bytef *)buffer;
strm.avail_in = size;
strm.next_out = (Bytef *)compressedBuf + sizeof(unsigned int);
strm.avail_out = size;
int windowsBits = 15;
int GZIP_ENCODING = 16;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = (Bytef *)buffer;
strm.avail_in = size;
strm.next_out = (Bytef *)compressedBuf + sizeof(unsigned int);
strm.avail_out = size;
int z_result = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,windowsBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
z_result = deflate(&strm, Z_FINISH);
assert(z_result != Z_STREAM_ERROR && z_result != Z_BUF_ERROR);
assert(z_result == Z_STREAM_END);
deflateEnd(&strm);
unsigned int compressedSize = strm.total_out;
printf("finish compression with GZIP, full size: %u, compressed size: %lu\n",size, compressedSize );
memcpy(compressedBuf, &compressedSize, sizeof(unsigned int));
return compressedSize;
}

int z_result = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,windowsBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
z_result = deflate(&strm, Z_FINISH);
assert(z_result != Z_STREAM_ERROR && z_result != Z_BUF_ERROR);
assert(z_result == Z_STREAM_END);
deflateEnd(&strm);
unsigned int compressedSize = strm.total_out;
printf("finish compression with GZIP, full size: %u, compressed size: %lu\n",size, compressedSize );
memcpy(compressedBuf, &compressedSize, sizeof(unsigned int));
return compressedSize;
int compressFrameGzip::compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf)
{
compressDepthFrame(buffer, size, compressedBuf);
}
3 changes: 2 additions & 1 deletion tools/rs-server/compressFrameGzip.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
class compressFrameGzip :public IcompressFrame
{
public:
int compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
private:
z_stream strm;
};
2 changes: 1 addition & 1 deletion tools/rs-server/compressFrameRVL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int compressFrameRVL::EncodeVLE(int value)
} while (value);
}

int compressFrameRVL::compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf)
int compressFrameRVL::compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf)
{
short * buffer2 = (short *)buffer;
int * pHead = pBuffer = (int*)compressedBuf + 1;
Expand Down
3 changes: 2 additions & 1 deletion tools/rs-server/compressFrameRVL.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
class compressFrameRVL :public IcompressFrame
{
public:
int compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf){};
int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
private:
int EncodeVLE(int value);
int *pBuffer, word, nibblesWritten;
Expand Down

0 comments on commit 8785a34

Please sign in to comment.