Skip to content

Commit

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

Mchanan color jpeg compress
  • Loading branch information
mchanan authored Jan 16, 2020
2 parents 0b07ac3 + bad0112 commit 7c5c6f8
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/ethernet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
# target_link_libraries(${PROJECT_NAME} ${DEPENDENCIES})

find_package(ZLIB REQUIRED)
find_package(JPEG REQUIRED)

target_link_libraries(${PROJECT_NAME}
PRIVATE ${DEPENDENCIES}
Expand All @@ -50,6 +51,7 @@ target_link_libraries(${PROJECT_NAME}
#groupsock
#liveMedia
${ZLIB_LIBRARIES}
${JPEG_LIBRARIES}
)

if(COMPRESSION)
Expand Down
4 changes: 4 additions & 0 deletions src/ethernet/decompressFrameFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <zlib.h>
#include "decompressFrameRVL.h"
#include "decompressFrameGzip.h"
#include "decompressFrameJpeg.h"
#include "decompressFrameFactory.h"

IdecompressFrame* decompressFrameFactory::create(zipMethod zipMeth)
Expand All @@ -18,6 +19,9 @@ IdecompressFrame* decompressFrameFactory::create(zipMethod zipMeth)
case zipMethod::rvl:
return new decompressFrameRVL();
break;
case zipMethod::Jpeg:
return new decompressFrameJpeg();
break;
default:
printf("unknown zip method\n");
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/ethernet/decompressFrameFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
typedef enum zipMethod {
gzip,
rvl,
Jpeg,
} zipMethod;

class decompressFrameFactory
Expand Down
4 changes: 2 additions & 2 deletions src/ethernet/decompressFrameGzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void decompressFrameGzip::decompressDepthFrame(unsigned char* buffer, int size,
//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);
printf("finish depth decompression, full size: %lu , compressed size %u \n", strm.total_out, compressedSize);

//statistic:
fullSizeSum += size;
Expand All @@ -50,4 +50,4 @@ void decompressFrameGzip::decompressDepthFrame(unsigned char* buffer, int size,
void decompressFrameGzip::decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
{
decompressDepthFrame(buffer,size, uncompressedBuf);
}
}
54 changes: 54 additions & 0 deletions src/ethernet/decompressFrameJpeg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <iomanip>
#include <cstdint>
#include <cstring>
#include "decompressFrameJpeg.h"
#include <stdio.h>
#include "jpeglib.h"

//TODO: add this code for RGB format
// (void) jpeg_read_scanlines(&cinfo, destBuffer, 1);
// for (int i = 0; i < cinfo.output_width h; i ++) {
// ptr[i] = destBuffer[0][i];
// ptr[i+ 1] = destBuffer[0][i +1];
// ptr[i+ 2] = destBuffer[0][i +2];
// }
// ptr += cinfo.output_width * cinfo.output_component;
//(void) jpeg_read_scanlines(&cinfo, destBuffer, 1);
//memcpy(ptr,destBuffer[0], 640*3);
//ptr+= 640*3;

void decompressFrameJpeg::decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf)
{
struct jpeg_error_mgr jerr;
struct jpeg_decompress_struct cinfo;
unsigned char * ptr = uncompressedBuf;
uint64_t compressedSize = 0;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
memcpy(&compressedSize, buffer, sizeof(unsigned int));
unsigned char* data = buffer + sizeof(unsigned int);
jpeg_mem_src(&cinfo, data , compressedSize);
(void) jpeg_read_header(&cinfo, TRUE);
cinfo.out_color_space = JCS_YCbCr;
(void) jpeg_start_decompress(&cinfo);

uint64_t row_stride = cinfo.output_width * cinfo.output_components;
JSAMPARRAY destBuffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, destBuffer, 1);
for (int i = 0; i < cinfo.output_width ; i += 2) { //input strides by 4 bytes, output strides by 6 (2 pixels)
ptr[i*2] = destBuffer[0][i*3]; // Y (unique to this pixel)
ptr[i*2 + 1] = destBuffer[0][i*3 + 1]; // U (shared between pixels)
ptr[i*2 + 2] = destBuffer[0][i*3 + 3]; // Y (unique to this pixel)
ptr[i*2 + 3] = destBuffer[0][i*3 + 2]; // V (shared between pixels)
}
ptr+= cinfo.output_width *2;
}

(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
printf("finish color decompression with jpeg, full size: %lu , compressed size %u \n", size, compressedSize);
}
20 changes: 20 additions & 0 deletions src/ethernet/decompressFrameJpeg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <functional>
#include <list>
#include <iostream>
#include <iomanip>
#include <cassert>
#include "IdecompressFrame.h"

class decompressFrameJpeg :public IdecompressFrame
{
public:
void decompressColorFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf);
void decompressDepthFrame(unsigned char* buffer, int size, unsigned char* uncompressedBuf){};

};
2 changes: 1 addition & 1 deletion src/ethernet/ethernet-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ rs2::ethernet_device::ethernet_device()
{
dev = rs2_create_software_device(NULL);
#ifdef COMPRESSION
iDecomressColor = decompressFrameFactory::create(zipMethod::gzip);
iDecomressColor = decompressFrameFactory::create(zipMethod::Jpeg);
iDecomressDepth = decompressFrameFactory::create(zipMethod::gzip);
#endif

Expand Down
17 changes: 8 additions & 9 deletions src/ethernet/ip_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ ip_device::ip_device(std::string ip_address, rs2::software_device sw_device)
this->is_device_alive = true;

#ifdef COMPRESSION
idecomress = decompressFrameFactory::create(zipMethod::gzip);
iDecomressColor = decompressFrameFactory::create(zipMethod::Jpeg);
iDecomressDepth = decompressFrameFactory::create(zipMethod::gzip);
#endif

//init device data
Expand Down Expand Up @@ -252,20 +253,18 @@ void ip_device::inject_frames_loop(std::shared_ptr<rs_rtp_stream> rtp_stream)
{
// depth
//std:: cout <<"\t@@@ before com the frame"<<std::endl;
idecomress->decompressDepthFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(rtp_stream.get()->frame_data_buff.pixels));
iDecomressDepth->decompressDepthFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(rtp_stream.get()->frame_data_buff.pixels));
//std:: cout <<"\t@@@ after com the frame"<<std::endl;
} else if(type==rs2_stream::RS2_STREAM_COLOR) {
// other -> color
#endif
//std:: cout <<"\t@@@ color frame"<<std::endl;
memcpy(rtp_stream.get()->frame_data_buff.pixels, frame->m_buffer, frame->m_size);
#ifdef COMPRESSION
}
iDecomressColor->decompressColorFrame((unsigned char *)frame->m_buffer, frame->m_size, (unsigned char*)(rtp_stream.get()->frame_data_buff.pixels));
}
else
{
std::cerr <<" BAD type"<<std::endl;
exit(-1);
}
#else
memcpy(rtp_stream.get()->frame_data_buff.pixels, frame->m_buffer, frame->m_size);
#endif
rtp_stream.get()->frame_data_buff.timestamp = frame->m_timestamp.tv_sec;
rtp_stream.get()->frame_data_buff.frame_number++;
Expand Down Expand Up @@ -296,4 +295,4 @@ void ip_device::inject_frames_loop(std::shared_ptr<rs_rtp_stream> rtp_stream)
// 4.1. create and add sw sensor to sw device
// 4.2. add streams per sensor
//5. register tear_down function to sw_device dtor
*/
*/
2 changes: 1 addition & 1 deletion src/ethernet/ip_device.hh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ip_device

rs2::software_device sw_dev;

IdecompressFrame* idecomress;
IdecompressFrame *iDecomressColor, *iDecomressDepth;

std::thread sw_device_status_check;

Expand Down
3 changes: 2 additions & 1 deletion tools/rs-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ endif()
set_property(TARGET rs-server PROPERTY CXX_STANDARD 11)

find_package(ZLIB REQUIRED)
find_package(JPEG REQUIRED)

target_link_libraries(rs-server ${DEPENDENCIES} realsense2 ${ZLIB_LIBRARIES})
target_link_libraries(rs-server ${DEPENDENCIES} realsense2 ${ZLIB_LIBRARIES} ${JPEG_LIBRARIES})

if(COMPRESSION)
add_definitions(-DCOMPRESSION)
Expand Down
2 changes: 1 addition & 1 deletion tools/rs-server/IcompressFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

class IcompressFrame {
public:
virtual int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf) = 0;
virtual int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf, int width, int height) = 0;
virtual int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf) = 0;
};
6 changes: 3 additions & 3 deletions tools/rs-server/RsSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ void RsDeviceSource::doGetNextFrame()
void RsDeviceSource::deliverRSFrame(rs2::frame *frame)
{
#ifdef COMPRESSION
IcompressFrame *iCompressColor = compressFrameFactory::create(zipMethod::gzip);
IcompressFrame *iCompressDepth = compressFrameFactory::create(zipMethod::gzip);
IcompressFrame* iCompressColor = compressFrameFactory::create(zipMethod::Jpeg);
IcompressFrame* iCompressDepth = compressFrameFactory::create(zipMethod::gzip);
#endif
if (!isCurrentlyAwaitingData())
{
Expand Down Expand Up @@ -103,7 +103,7 @@ void RsDeviceSource::deliverRSFrame(rs2::frame *frame)
}
else if (stream_profile->stream_type() == RS2_STREAM_COLOR)
{
iCompressColor->compressColorFrame((unsigned char *)frame->get_data(), fFrameSize, fTo);
iCompressColor->compressColorFrame((unsigned char *)frame->get_data(), fFrameSize, fTo, stream_profile->width(),stream_profile->height());
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions tools/rs-server/compressFrameFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <zlib.h>
#include "compressFrameRVL.h"
#include "compressFrameGzip.h"
#include "compressFrameJpeg.h"
#include "compressFrameFactory.h"

IcompressFrame* compressFrameFactory::create(zipMethod zipMeth)
Expand All @@ -18,6 +19,9 @@ IcompressFrame* compressFrameFactory::create(zipMethod zipMeth)
case zipMethod::rvl:
return new compressFrameRVL();
break;
case zipMethod::Jpeg:
return new compressFrameJpeg();
break;
default:
printf("unknown zip method\n");
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions tools/rs-server/compressFrameFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
typedef enum zipMethod {
gzip,
rvl,
Jpeg,
} zipMethod;

class compressFrameFactory
Expand Down
4 changes: 2 additions & 2 deletions tools/rs-server/compressFrameGzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ int compressFrameGzip::compressDepthFrame(unsigned char* buffer, int size, unsig
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 );
printf("finish depth 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)
int compressFrameGzip::compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf, int width, int height)
{
compressDepthFrame(buffer, size, compressedBuf);
}
2 changes: 1 addition & 1 deletion tools/rs-server/compressFrameGzip.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class compressFrameGzip :public IcompressFrame
{
public:
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf, int width, int height);
int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
private:
z_stream strm;
Expand Down
55 changes: 55 additions & 0 deletions tools/rs-server/compressFrameJpeg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <iostream>
#include <cstdint>
#include <cstring>
#include "compressFrameJpeg.h"
#include <stdio.h>
#include "jpeglib.h"

int compressFrameJpeg::compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf, int width, int height)
{
struct jpeg_error_mgr jerr;
struct jpeg_compress_struct cinfo;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
uint64_t compressedSize = 0;
unsigned char * data;
int bpp = size/(width*height);

jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, &data, &compressedSize);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
row_stride = cinfo.image_width * cinfo.input_components;

unsigned char *tmprowbuf = new unsigned char[cinfo.image_width * cinfo.input_components];
row_pointer[0] = tmprowbuf;
while (cinfo.next_scanline < cinfo.image_height) {
//row_pointer[0] = & buffer[cinfo.next_scanline * row_stride];
//(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
for (int i = 0; i < cinfo.image_width; i += 2) { //input strides by 4 bytes, output strides by 6 (2 pixels)
tmprowbuf[i*3] = buffer[i*2 + 0]; // Y (unique to this pixel)
tmprowbuf[i*3 + 1] = buffer[i*2 + 1]; // U (shared between pixels)
tmprowbuf[i*3 + 2] = buffer[i*2 + 3]; // V (shared between pixels)
tmprowbuf[i*3 + 3] = buffer[i*2 + 2]; // Y (unique to this pixel)
tmprowbuf[i*3 + 4] = buffer[i*2 + 1]; // U (shared between pixels)
tmprowbuf[i*3 + 5] = buffer[i*2 + 3]; // V (shared between pixels)
}
row_pointer[0] = tmprowbuf;
buffer += cinfo.image_width * 2;
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
printf("finish color compression with jpeg, full size: %lu , compressed size %u \n", size, compressedSize);

memcpy(compressedBuf + 4, data, compressedSize);
memcpy(compressedBuf, &compressedSize, sizeof(unsigned int));
return compressedSize;
}
11 changes: 11 additions & 0 deletions tools/rs-server/compressFrameJpeg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

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

class compressFrameJpeg :public IcompressFrame
{
public:
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf, int width, int height);
int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf){};
};
2 changes: 1 addition & 1 deletion tools/rs-server/compressFrameRVL.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class compressFrameRVL :public IcompressFrame
{
public:
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf){};
int compressColorFrame(unsigned char* buffer, int size, unsigned char* compressedBuf, int width, int height){};
int compressDepthFrame(unsigned char* buffer, int size, unsigned char* compressedBuf);
private:
int EncodeVLE(int value);
Expand Down

0 comments on commit 7c5c6f8

Please sign in to comment.