From 8c3eeb6188e646012f8cc817ca7810dc3e94be7b Mon Sep 17 00:00:00 2001 From: mchanan Date: Wed, 1 Jan 2020 12:26:11 +0200 Subject: [PATCH] add color commpression with gzip --- src/ethernet/IdecompressFrame.h | 3 +- src/ethernet/decompressFrameGzip.cpp | 10 +++++-- src/ethernet/decompressFrameGzip.h | 3 +- src/ethernet/decompressFrameRVL.cpp | 2 +- src/ethernet/decompressFrameRVL.h | 3 +- src/ethernet/ethernet-device.cpp | 10 +++++-- src/ethernet/ethernet-device.h | 3 +- tools/rs-server/IcompressFrame.h | 3 +- tools/rs-server/RsSource.cpp | 10 ++++--- tools/rs-server/compressFrameGzip.cpp | 42 +++++++++++++++------------ tools/rs-server/compressFrameGzip.h | 3 +- tools/rs-server/compressFrameRVL.cpp | 2 +- tools/rs-server/compressFrameRVL.h | 3 +- 13 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/ethernet/IdecompressFrame.h b/src/ethernet/IdecompressFrame.h index 6a74491383..87916968c5 100644 --- a/src/ethernet/IdecompressFrame.h +++ b/src/ethernet/IdecompressFrame.h @@ -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; }; diff --git a/src/ethernet/decompressFrameGzip.cpp b/src/ethernet/decompressFrameGzip.cpp index b00f241520..3180df6b9d 100644 --- a/src/ethernet/decompressFrameGzip.cpp +++ b/src/ethernet/decompressFrameGzip.cpp @@ -17,9 +17,8 @@ #include #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; @@ -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); } \ No newline at end of file diff --git a/src/ethernet/decompressFrameGzip.h b/src/ethernet/decompressFrameGzip.h index 5e473874d5..7928313d08 100644 --- a/src/ethernet/decompressFrameGzip.h +++ b/src/ethernet/decompressFrameGzip.h @@ -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; diff --git a/src/ethernet/decompressFrameRVL.cpp b/src/ethernet/decompressFrameRVL.cpp index d78bf426ee..ef139ea0bd 100644 --- a/src/ethernet/decompressFrameRVL.cpp +++ b/src/ethernet/decompressFrameRVL.cpp @@ -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; diff --git a/src/ethernet/decompressFrameRVL.h b/src/ethernet/decompressFrameRVL.h index 2cd7644dad..a983fe33a8 100644 --- a/src/ethernet/decompressFrameRVL.h +++ b/src/ethernet/decompressFrameRVL.h @@ -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; diff --git a/src/ethernet/ethernet-device.cpp b/src/ethernet/ethernet-device.cpp index 3d1e9d256f..9447723f70 100644 --- a/src/ethernet/ethernet-device.cpp +++ b/src/ethernet/ethernet-device.cpp @@ -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 } @@ -173,9 +174,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 diff --git a/src/ethernet/ethernet-device.h b/src/ethernet/ethernet-device.h index 9a240559a3..9b3dd5259d 100644 --- a/src/ethernet/ethernet-device.h +++ b/src/ethernet/ethernet-device.h @@ -132,7 +132,8 @@ namespace rs2 rs2_software_video_frame last_frame[2]; std::vector pixels_buff[2]; - IdecompressFrame* idecomress; + IdecompressFrame* iDecomressColor; + IdecompressFrame* iDecomressDepth; }; class RS_RTSPFrameCallback: public RTSPCallback diff --git a/tools/rs-server/IcompressFrame.h b/tools/rs-server/IcompressFrame.h index ee9f5011dd..ae5b14c3e5 100644 --- a/tools/rs-server/IcompressFrame.h +++ b/tools/rs-server/IcompressFrame.h @@ -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; }; diff --git a/tools/rs-server/RsSource.cpp b/tools/rs-server/RsSource.cpp index aa0f30185a..feb026162c 100644 --- a/tools/rs-server/RsSource.cpp +++ b/tools/rs-server/RsSource.cpp @@ -104,7 +104,8 @@ 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()) { @@ -127,12 +128,13 @@ void RsDeviceSource::deliverRSFrame() fFrameSize = newFrameSize; } gettimeofday(&fPresentationTime, NULL); // If you have a more accurate time - e.g., from an encoder - then use that instead. - //// memmove(fTo, frame.get_data(), fFrameSize); - //// unsigned char b[640*480*2]; #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, 640*480*2); diff --git a/tools/rs-server/compressFrameGzip.cpp b/tools/rs-server/compressFrameGzip.cpp index cfb3d47b8b..bb99af4309 100644 --- a/tools/rs-server/compressFrameGzip.cpp +++ b/tools/rs-server/compressFrameGzip.cpp @@ -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); } \ No newline at end of file diff --git a/tools/rs-server/compressFrameGzip.h b/tools/rs-server/compressFrameGzip.h index 1170ec90c5..c4d89dcc65 100644 --- a/tools/rs-server/compressFrameGzip.h +++ b/tools/rs-server/compressFrameGzip.h @@ -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; }; diff --git a/tools/rs-server/compressFrameRVL.cpp b/tools/rs-server/compressFrameRVL.cpp index 5f8c3beac3..53936546cb 100644 --- a/tools/rs-server/compressFrameRVL.cpp +++ b/tools/rs-server/compressFrameRVL.cpp @@ -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; diff --git a/tools/rs-server/compressFrameRVL.h b/tools/rs-server/compressFrameRVL.h index e567206edb..50fba6e229 100644 --- a/tools/rs-server/compressFrameRVL.h +++ b/tools/rs-server/compressFrameRVL.h @@ -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;