add support to passthough mjpeg images

This commit is contained in:
2023-11-18 16:23:52 +01:00
parent 24487a6a51
commit 76bd00363b
5 changed files with 72 additions and 21 deletions

29
image.h
View File

@ -6,16 +6,39 @@
class Image class Image
{ {
private:
ssize_t bufferSize = -1;
public: public:
enum {
FORMAT_RGB,
FORMAT_JPEG,
FORMAT_YUYV
} typedef format_t;
std::shared_ptr<unsigned char> data = nullptr; std::shared_ptr<unsigned char> data = nullptr;
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
uint8_t channels; uint8_t channels;
const uint32_t size() format_t format = FORMAT_RGB;
const size_t size() const
{ {
return width*height*channels; return bufferSize < 0 ? width*height*channels : bufferSize;
} }
void save(const char* filename) void setSize(size_t size)
{
bufferSize = size;
}
void setBuffer(std::shared_ptr<unsigned char> buffer, size_t size, format_t inFormat = FORMAT_RGB)
{
data = buffer;
bufferSize = size;
format = inFormat;
}
void save(const char* filename) const
{ {
std::ofstream outfile(filename); std::ofstream outfile(filename);
outfile.write((char *) data.get(), size()); outfile.write((char *) data.get(), size());

View File

@ -25,7 +25,7 @@ Image decompressJpegImage(const unsigned char* buffer, size_t size)
image.channels = info.num_components; // 3 = RGB, 4 = RGBA image.channels = info.num_components; // 3 = RGB, 4 = RGBA
// read RGB(A) scanlines one at a time into jdata[] // read RGB(A) scanlines one at a time into jdata[]
image.data = std::shared_ptr<unsigned char>(new unsigned char[image.size()]); image.data = std::shared_ptr<unsigned char>(new unsigned char[image.size()], std::default_delete<int[]>());
unsigned char* rowptr; unsigned char* rowptr;
while(info.output_scanline < image.height) while(info.output_scanline < image.height)
{ {
@ -58,7 +58,7 @@ Image decompressJpegImage(FILE *file)
image.channels = info.num_components; // 3 = RGB, 4 = RGBA image.channels = info.num_components; // 3 = RGB, 4 = RGBA
// read RGB(A) scanlines one at a time into jdata[] // read RGB(A) scanlines one at a time into jdata[]
image.data = std::shared_ptr<unsigned char>(new unsigned char[image.size()]); image.data = std::shared_ptr<unsigned char>(new unsigned char[image.size()], std::default_delete<int[]>());
unsigned char* rowptr; unsigned char* rowptr;
while ( info.output_scanline < image.height ) while ( info.output_scanline < image.height )
{ {

View File

@ -41,9 +41,11 @@ int main(int argc, char* argv[])
Config config; Config config;
argp_parse(&argp, argc, argv, 0, 0, &config); argp_parse(&argp, argc, argv, 0, 0, &config);
bool passthough = config.mjpeg && config.overlay.empty();
std::cout<<"UVOS webcam sender v0.1\n"; std::cout<<"UVOS webcam sender v0.1\n";
Webcam webcam(config.device, config.Xres, config.Yres, config.mjpeg); Webcam webcam(config.device, config.Xres, config.Yres, config.mjpeg, passthough);
std::stringstream ss; std::stringstream ss;
ss<<"nc "<<config.url<<' '<<config.port<<'\n'; ss<<"nc "<<config.url<<' '<<config.port<<'\n';
@ -68,7 +70,10 @@ int main(int argc, char* argv[])
Image frame = webcam.frame(10); Image frame = webcam.frame(10);
if(!config.overlay.empty()) if(!config.overlay.empty())
overlayImage(frame, overlay); overlayImage(frame, overlay);
compressJpegImage(netcat, &frame); if(!passthough)
compressJpegImage(netcat, &frame);
else
fwrite(frame.data.get(), frame.size(), 1, netcat);
fflush(netcat); fflush(netcat);
} }

View File

@ -85,10 +85,11 @@ static void v4lconvert_yuyv_to_rgb24(const unsigned char *src,
} }
} }
Webcam::Webcam(const string& device, int width, int height, bool mjpeg) : Webcam::Webcam(const string& device, int width, int height, bool mjpeg, bool passthrough) :
device(device), device(device),
xres(width), xres(width),
yres(height), yres(height),
passthrough_format(passthrough),
format(mjpeg ? V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_YUYV) format(mjpeg ? V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_YUYV)
{ {
open_device(); open_device();
@ -99,7 +100,7 @@ Webcam::Webcam(const string& device, int width, int height, bool mjpeg) :
rgb_frame.width = xres; rgb_frame.width = xres;
rgb_frame.height = yres; rgb_frame.height = yres;
rgb_frame.channels = 3; rgb_frame.channels = 3;
rgb_frame.data = std::shared_ptr<unsigned char>(new unsigned char[rgb_frame.size()]); rgb_frame.data = std::shared_ptr<unsigned char>(new unsigned char[rgb_frame.size()], std::default_delete<int[]>());
start_capturing(); start_capturing();
} }
@ -138,21 +139,42 @@ const Image& Webcam::frame(int timeout)
} }
int idx = read_frame(); int idx = read_frame();
if (idx != -1) { if (idx != -1) {
if (format == V4L2_PIX_FMT_YUYV) if(!passthrough_format)
{ {
v4lconvert_yuyv_to_rgb24((unsigned char *) buffers[idx].data, if (format == V4L2_PIX_FMT_YUYV)
rgb_frame.data.get(), {
xres, v4lconvert_yuyv_to_rgb24((unsigned char *) buffers[idx].data,
yres, rgb_frame.data.get(),
stride); xres,
} yres,
else if(format == V4L2_PIX_FMT_MJPEG) stride);
{ }
rgb_frame = decompressJpegImage((unsigned char *) buffers[idx].data, buffers[idx].size); else if(format == V4L2_PIX_FMT_MJPEG)
{
rgb_frame = decompressJpegImage((unsigned char *) buffers[idx].data, buffers[idx].size);
}
else
{
assert(false);
}
} }
else else
{ {
assert(false); if(format == V4L2_PIX_FMT_YUYV)
{
rgb_frame.setBuffer(std::shared_ptr<unsigned char>(new unsigned char[buffers[idx].size], std::default_delete<int[]>()),
buffers[idx].size, Image::FORMAT_YUYV);
}
else if(format == V4L2_PIX_FMT_MJPEG)
{
rgb_frame.setBuffer(std::shared_ptr<unsigned char>(new unsigned char[buffers[idx].size], std::default_delete<int[]>()),
buffers[idx].size, Image::FORMAT_RGB);
}
else
{
assert(false);
}
memcpy(rgb_frame.data.get(), buffers[idx].data, buffers[idx].size);
} }
return rgb_frame; return rgb_frame;
} }

View File

@ -18,7 +18,7 @@ class Webcam {
public: public:
Webcam(const std::string& device = "/dev/video0", Webcam(const std::string& device = "/dev/video0",
int width = 640, int width = 640,
int height = 480, bool mjpeg = false); int height = 480, bool mjpeg = false, bool passthrough = false);
~Webcam(); ~Webcam();
@ -60,6 +60,7 @@ private:
size_t stride; size_t stride;
bool force_format = true; bool force_format = true;
bool passthrough_format;
uint32_t format; uint32_t format;
}; };