Skip to content

Commit

Permalink
Merge pull request IntelRealSense#8 from IntelRealSense/mchanan_compress
Browse files Browse the repository at this point in the history
cleanup for compression
  • Loading branch information
mchanan authored Dec 22, 2019
2 parents 895828a + dcad9a6 commit 23b5aa3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/ethernet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)

# target_link_libraries(${PROJECT_NAME} ${DEPENDENCIES})

find_library(ZLIB_LIB libz PATHS /usr/local/lib/)
find_package(ZLIB REQUIRED)

target_link_libraries(${PROJECT_NAME}
PRIVATE ${DEPENDENCIES}
Expand All @@ -54,7 +54,7 @@ target_link_libraries(${PROJECT_NAME}
#UsageEnvironment
#groupsock
#liveMedia
${ZLIB_LIB}
${ZLIB_LIBRARIES}
)

target_include_directories(${PROJECT_NAME} PUBLIC "rtsp_client/*.h")
Expand Down
22 changes: 10 additions & 12 deletions src/ethernet/decompressFrameGzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,31 @@

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

unsigned int compressedSize;
int windowsBits = 15;
int GZIP_ENCODING = 16;
memcpy(&compressedSize, buffer,sizeof(unsigned int));
//printf("all buff size %d, compressed size %u \n", size, compressedSize );
assert(compressedSize < size);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
int windowsBits = 15;
int GZIP_ENCODING = 16;
strm.next_in = (Bytef *)buffer + sizeof(unsigned int);
strm.avail_in = size;
strm.next_out = (Bytef *)uncompressedBuf;
strm.avail_out = size;
int z_result = inflateInit2(&strm, windowsBits | GZIP_ENCODING);
z_result = inflate(&strm, Z_FINISH);
// assert(z_result != Z_STREAM_ERROR && z_result != Z_BUF_ERROR && z_result == Z_STREAM_END);//fix condition
if (z_result == Z_STREAM_ERROR ) printf("ERROR: stream error\n");
if (z_result == Z_BUF_ERROR ) printf("ERROR: buffer error\n");
if (z_result != Z_STREAM_END ) printf("ERROR: stream end\n");
// assert(z_result != Z_STREAM_ERROR );
// assert(z_result != Z_BUF_ERROR);
// assert(z_result == Z_STREAM_END);
//assert(z_result != Z_STREAM_ERROR );
//assert(z_result != Z_BUF_ERROR);
//assert(z_result == Z_STREAM_END);
inflateEnd(&strm);
printf("finish decompression, full size: %lu , compressed size %u \n", strm.total_out, compressedSize);

//statistic:
fullSizeSum += size;
compressedSizeSum += compressedSize;
float zipRatio = fullSizeSum/(float)compressedSizeSum;
frameCounter++;
printf("zip ratio is: %0.2f , frameCounter: %d\n", zipRatio, frameCounter);
printf("gzip zip ratio is: %0.2f , frameCounter: %d\n", zipRatio, frameCounter);
}
1 change: 1 addition & 0 deletions src/ethernet/decompressFrameGzip.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ class decompressFrameGzip :public IdecompressFrame
void decompressFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
private:
long long fullSizeSum = 0, compressedSizeSum = 0, frameCounter = 0; //for ratio statistics
z_stream strm;

};
10 changes: 6 additions & 4 deletions src/ethernet/decompressFrameRVL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,27 @@ void decompressFrameRVL::decompressFrame(unsigned char* buffer, int size, unsign
int numPixelsToDecode = size/2;
while (numPixelsToDecode)
{
int zeros = decodeVLE(); // number of zeros
int zeros = decodeVLE();
numPixelsToDecode -= zeros;
for (; zeros; zeros--)
*uncompressedBuf2++ = 0;
int nonzeros = decodeVLE(); // number of nonzeros
int nonzeros = decodeVLE();
numPixelsToDecode -= nonzeros;
for (; nonzeros; nonzeros--)
{
int positive = decodeVLE(); // nonzero value
int positive = decodeVLE();
int delta = (positive >> 1) ^ -(positive & 1);
current = previous + delta;
*uncompressedBuf2++ = current;
previous = current;
}
}
printf("finish decompression, full size: %lu , compressed size %d \n",size, compressedSize);

//statistic:
fullSizeSum += size;
compressedSizeSum += compressedSize;
float zipRatio = fullSizeSum/(float)compressedSizeSum;
frameCounter++;
printf("zip ratio is: %0.2f , frameCounter: %d\n", zipRatio, frameCounter);
printf("rvl zip ratio is: %0.2f , frameCounter: %d\n", zipRatio, frameCounter);
}
6 changes: 2 additions & 4 deletions tools/rs-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ endif()

set_property(TARGET rs-server PROPERTY CXX_STANDARD 11)

target_link_libraries(rs-server ${DEPENDENCIES} realsense2)
find_package(ZLIB REQUIRED)

# zlib
find_library(ZLIB_LIB libz PATHS /usr/local/lib/)
target_link_libraries(rs-server "${ZLIB_LIB}")
target_link_libraries(rs-server ${DEPENDENCIES} realsense2 ${ZLIB_LIBRARIES})

set_target_properties (rs-server PROPERTIES
FOLDER Tools
Expand Down
11 changes: 4 additions & 7 deletions tools/rs-server/compressFrameGzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@

int compressFrameGzip::compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf)
{
z_stream strm;
memset(compressedBuf, 0, size);//?
int windowsBits = 15;
int GZIP_ENCODING = 16;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
int windowsBits = 15;
int GZIP_ENCODING = 16;
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 && z_result == Z_STREAM_END);//fix condition
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));
// for(int i=0; i<100 ; ++i)
// std::cout << std::hex << (int)compressedBuf[i] << " ";
return compressedSize;
}
3 changes: 3 additions & 0 deletions tools/rs-server/compressFrameGzip.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include "IcompressFrame.h"
#include <zlib.h>

class compressFrameGzip :public IcompressFrame
{
public:
int compressFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
private:
z_stream strm;
};
10 changes: 4 additions & 6 deletions tools/rs-server/compressFrameRVL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ int compressFrameRVL::compressFrame(unsigned char* buffer, int size, unsigned ch
{
int zeros = 0, nonzeros = 0;
for (; (buffer2 != end) && !*buffer2; buffer2++, zeros++);
EncodeVLE(zeros); // number of zeros
EncodeVLE(zeros);
for (short* p = buffer2; (p != end) && *p++; nonzeros++);
EncodeVLE(nonzeros); // number of nonzeros
EncodeVLE(nonzeros);
for (int i = 0; i < nonzeros; i++)
{
short current = *buffer2++;
int delta = current - previous;
int positive = (delta << 1) ^ (delta >> 31);
EncodeVLE(positive); // nonzero value
EncodeVLE(positive);
previous = current;
}
}
Expand All @@ -50,7 +50,5 @@ int compressFrameRVL::compressFrame(unsigned char* buffer, int size, unsigned ch
int compressedSize = int((char*)pBuffer - (char*)pHead);
printf("finish compression with RVL, full size: %u, compressed size: %lu\n",size, compressedSize);
memcpy(compressedBuf, &compressedSize, sizeof(unsigned int));
// for(int i=0; i<100 ; ++i)
// std::cout << std::hex << (int)compressedBuf[i] << " ";
return compressedSize; // num bytes
return compressedSize;
}

0 comments on commit 23b5aa3

Please sign in to comment.