mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-03-24 00:13:42 +00:00
refactoring
This commit is contained in:
@@ -37,9 +37,7 @@ typedef struct {
|
|||||||
|
|
||||||
static void _jpeg_set_dest_frame(j_compress_ptr jpeg, us_frame_s *frame);
|
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_yuv(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
|
||||||
static void _jpeg_write_scanlines_yvyu(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_rgb565(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);
|
static void _jpeg_write_scanlines_rgb24(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
|
||||||
static void _jpeg_write_scanlines_bgr24(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
|
static void _jpeg_write_scanlines_bgr24(struct jpeg_compress_struct *jpeg, const us_frame_s *frame);
|
||||||
@@ -72,22 +70,17 @@ void us_cpu_encoder_compress(const us_frame_s *src, us_frame_s *dest, unsigned q
|
|||||||
|
|
||||||
jpeg_start_compress(&jpeg, TRUE);
|
jpeg_start_compress(&jpeg, TRUE);
|
||||||
|
|
||||||
# define WRITE_SCANLINES(x_format, x_func) \
|
|
||||||
case x_format: { x_func(&jpeg, src); break; }
|
|
||||||
|
|
||||||
switch (src->format) {
|
switch (src->format) {
|
||||||
// https://www.fourcc.org/yuv.php
|
// https://www.fourcc.org/yuv.php
|
||||||
WRITE_SCANLINES(V4L2_PIX_FMT_YUYV, _jpeg_write_scanlines_yuyv);
|
case V4L2_PIX_FMT_YUYV:
|
||||||
WRITE_SCANLINES(V4L2_PIX_FMT_YVYU, _jpeg_write_scanlines_yvyu);
|
case V4L2_PIX_FMT_YVYU:
|
||||||
WRITE_SCANLINES(V4L2_PIX_FMT_UYVY, _jpeg_write_scanlines_uyvy);
|
case V4L2_PIX_FMT_UYVY: _jpeg_write_scanlines_yuv(&jpeg, src); break;
|
||||||
WRITE_SCANLINES(V4L2_PIX_FMT_RGB565, _jpeg_write_scanlines_rgb565);
|
case V4L2_PIX_FMT_RGB565: _jpeg_write_scanlines_rgb565(&jpeg, src); break;
|
||||||
WRITE_SCANLINES(V4L2_PIX_FMT_RGB24, _jpeg_write_scanlines_rgb24);
|
case V4L2_PIX_FMT_RGB24: _jpeg_write_scanlines_rgb24(&jpeg, src); break;
|
||||||
WRITE_SCANLINES(V4L2_PIX_FMT_BGR24, _jpeg_write_scanlines_bgr24);
|
case V4L2_PIX_FMT_BGR24: _jpeg_write_scanlines_bgr24(&jpeg, src); break;
|
||||||
default: assert(0 && "Unsupported input format for CPU encoder");
|
default: assert(0 && "Unsupported input format for CPU encoder"); return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# undef WRITE_SCANLINES
|
|
||||||
|
|
||||||
jpeg_finish_compress(&jpeg);
|
jpeg_finish_compress(&jpeg);
|
||||||
jpeg_destroy_compress(&jpeg);
|
jpeg_destroy_compress(&jpeg);
|
||||||
|
|
||||||
@@ -110,73 +103,7 @@ static void _jpeg_set_dest_frame(j_compress_ptr jpeg, us_frame_s *frame) {
|
|||||||
frame->used = 0;
|
frame->used = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _jpeg_write_scanlines_yuyv(struct jpeg_compress_struct *jpeg, const us_frame_s *frame) {
|
static void _jpeg_write_scanlines_yuv(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;
|
|
||||||
|
|
||||||
while (jpeg->next_scanline < frame->height) {
|
|
||||||
uint8_t *ptr = line_buf;
|
|
||||||
|
|
||||||
for (unsigned x = 0; x < frame->width; ++x) {
|
|
||||||
// See also: https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-yuyv.html
|
|
||||||
const bool is_odd_pixel = x & 1;
|
|
||||||
const uint8_t y = data[is_odd_pixel ? 2 : 0];
|
|
||||||
const uint8_t u = data[1];
|
|
||||||
const uint8_t v = data[3];
|
|
||||||
|
|
||||||
ptr[0] = y;
|
|
||||||
ptr[1] = u;
|
|
||||||
ptr[2] = v;
|
|
||||||
ptr += 3;
|
|
||||||
|
|
||||||
data += (is_odd_pixel ? 4: 0);
|
|
||||||
}
|
|
||||||
data += padding;
|
|
||||||
|
|
||||||
JSAMPROW scanlines[1] = {line_buf};
|
|
||||||
jpeg_write_scanlines(jpeg, scanlines, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(line_buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _jpeg_write_scanlines_yvyu(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;
|
|
||||||
|
|
||||||
while (jpeg->next_scanline < frame->height) {
|
|
||||||
uint8_t *ptr = line_buf;
|
|
||||||
|
|
||||||
for (unsigned x = 0; x < frame->width; ++x) {
|
|
||||||
// See also: https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-yuyv.html
|
|
||||||
const bool is_odd_pixel = x & 1;
|
|
||||||
const uint8_t y = data[is_odd_pixel ? 2 : 0];
|
|
||||||
const uint8_t u = data[3];
|
|
||||||
const uint8_t v = data[1];
|
|
||||||
|
|
||||||
ptr[0] = y;
|
|
||||||
ptr[1] = u;
|
|
||||||
ptr[2] = v;
|
|
||||||
ptr += 3;
|
|
||||||
|
|
||||||
data += (is_odd_pixel ? 4 : 0);
|
|
||||||
}
|
|
||||||
data += padding;
|
|
||||||
|
|
||||||
JSAMPROW scanlines[1] = {line_buf};
|
|
||||||
jpeg_write_scanlines(jpeg, scanlines, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(line_buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _jpeg_write_scanlines_uyvy(struct jpeg_compress_struct *jpeg, const us_frame_s *frame) {
|
|
||||||
uint8_t *line_buf;
|
uint8_t *line_buf;
|
||||||
US_CALLOC(line_buf, frame->width * 3);
|
US_CALLOC(line_buf, frame->width * 3);
|
||||||
|
|
||||||
@@ -189,9 +116,23 @@ static void _jpeg_write_scanlines_uyvy(struct jpeg_compress_struct *jpeg, const
|
|||||||
for (unsigned x = 0; x < frame->width; ++x) {
|
for (unsigned x = 0; x < frame->width; ++x) {
|
||||||
// See also: https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-uyvy.html
|
// See also: https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-uyvy.html
|
||||||
const bool is_odd_pixel = x & 1;
|
const bool is_odd_pixel = x & 1;
|
||||||
const uint8_t y = data[is_odd_pixel ? 3 : 1];
|
uint8_t y, u, v;
|
||||||
const uint8_t u = data[0];
|
if (frame->format == V4L2_PIX_FMT_YUYV) {
|
||||||
const uint8_t v = data[2];
|
y = data[is_odd_pixel ? 2 : 0];
|
||||||
|
u = data[1];
|
||||||
|
v = data[3];
|
||||||
|
} else if (frame->format == V4L2_PIX_FMT_YVYU) {
|
||||||
|
y = data[is_odd_pixel ? 2 : 0];
|
||||||
|
u = data[3];
|
||||||
|
v = data[1];
|
||||||
|
} else if (frame->format == V4L2_PIX_FMT_UYVY) {
|
||||||
|
y = data[is_odd_pixel ? 3 : 1];
|
||||||
|
u = data[0];
|
||||||
|
v = data[2];
|
||||||
|
} else {
|
||||||
|
assert(0 && "Unsupported pixel format");
|
||||||
|
return; // Makes linter happy
|
||||||
|
}
|
||||||
|
|
||||||
ptr[0] = y;
|
ptr[0] = y;
|
||||||
ptr[1] = u;
|
ptr[1] = u;
|
||||||
|
|||||||
Reference in New Issue
Block a user