Skip to content

Commit

Permalink
Add GREY pixelformat
Browse files Browse the repository at this point in the history
Fixes pikvm#170

Monochrome cameras send only Y component of YUV image
  • Loading branch information
gudvinr committed Sep 10, 2022
1 parent 69cc45a commit d48783e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libs/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ bool us_frame_compare(const us_frame_s *a, const us_frame_s *b) {
unsigned us_frame_get_padding(const us_frame_s *frame) {
unsigned bytes_per_pixel = 0;
switch (frame->format) {
case V4L2_PIX_FMT_GREY: bytes_per_pixel = 1; break;
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_RGB565: bytes_per_pixel = 2; break;
Expand Down
1 change: 1 addition & 0 deletions src/ustreamer/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const struct {
} _FORMATS[] = {
{"YUYV", V4L2_PIX_FMT_YUYV},
{"UYVY", V4L2_PIX_FMT_UYVY},
{"GREY", V4L2_PIX_FMT_GREY},
{"RGB565", V4L2_PIX_FMT_RGB565},
{"RGB24", V4L2_PIX_FMT_RGB24},
{"MJPEG", V4L2_PIX_FMT_MJPEG},
Expand Down
31 changes: 31 additions & 0 deletions src/ustreamer/encoders/cpu/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static void _jpeg_set_dest_frame(j_compress_ptr jpeg, us_frame_s *frame);

static void _jpeg_write_scanlines_yuyv(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
static void _jpeg_write_scanlines_uyvy(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
static void _jpeg_write_scanlines_grey(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
static void _jpeg_write_scanlines_rgb565(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
static void _jpeg_write_scanlines_rgb24(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);

Expand Down Expand Up @@ -77,6 +78,7 @@ void us_cpu_encoder_compress(const us_frame_s *src, us_frame_s *dest, unsigned q
// https://www.fourcc.org/yuv.php
WRITE_SCANLINES(V4L2_PIX_FMT_YUYV, _jpeg_write_scanlines_yuyv);
WRITE_SCANLINES(V4L2_PIX_FMT_UYVY, _jpeg_write_scanlines_uyvy);
WRITE_SCANLINES(V4L2_PIX_FMT_GREY, _jpeg_write_scanlines_grey);
WRITE_SCANLINES(V4L2_PIX_FMT_RGB565, _jpeg_write_scanlines_rgb565);
WRITE_SCANLINES(V4L2_PIX_FMT_RGB24, _jpeg_write_scanlines_rgb24);
default: assert(0 && "Unsupported input format for CPU encoder");
Expand Down Expand Up @@ -187,6 +189,35 @@ static void _jpeg_write_scanlines_uyvy(struct jpeg_compress_struct *jpeg, const
free(line_buf);
}

static void _jpeg_write_scanlines_grey(struct jpeg_compress_struct *jpeg, const us_frame_s *frame) {
uint8_t *line_buf;
US_CALLOC(line_buf, frame->width * 3);

const unsigned padding = us_frame_get_padding(frame);
const uint8_t *data = frame->data;
unsigned z = 0;

while (jpeg->next_scanline < frame->height) {
uint8_t *ptr = line_buf;

for (unsigned x = 0; x < frame->width; ++x) {
const int y = data[0];

*(ptr++) = y;
*(ptr++) = y;
*(ptr++) = y;

data += 1;
}
data += padding;

JSAMPROW scanlines[1] = {line_buf};
jpeg_write_scanlines(jpeg, scanlines, 1);
}

free(line_buf);
}

#undef NORM_COMPONENT
#undef YUV_B
#undef YUV_G
Expand Down

0 comments on commit d48783e

Please sign in to comment.