mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-02-19 08:16:31 +00:00
refactoring
This commit is contained in:
@@ -129,13 +129,13 @@ void rawsink_destroy(rawsink_s *rawsink) {
|
||||
free(rawsink);
|
||||
}
|
||||
|
||||
int rawsink_server_put(rawsink_s *rawsink, frame_s *raw) {
|
||||
int rawsink_server_put(rawsink_s *rawsink, frame_s *frame) {
|
||||
long double now = get_now_monotonic();
|
||||
|
||||
assert(rawsink->server);
|
||||
|
||||
if (raw->used > RAWSINK_MAX_DATA) {
|
||||
LOG_ERROR("RAWSINK: Can't put RAW frame: is too big (%zu > %zu)", raw->used, RAWSINK_MAX_DATA);
|
||||
if (frame->used > RAWSINK_MAX_DATA) {
|
||||
LOG_ERROR("RAWSINK: Can't put RAW frame: is too big (%zu > %zu)", frame->used, RAWSINK_MAX_DATA);
|
||||
return 0; // -2
|
||||
}
|
||||
|
||||
@@ -147,13 +147,13 @@ int rawsink_server_put(rawsink_s *rawsink, frame_s *raw) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
# define COPY(_field) rawsink->mem->_field = raw->_field
|
||||
# define COPY(_field) rawsink->mem->_field = frame->_field
|
||||
COPY(used);
|
||||
COPY(format);
|
||||
COPY(width);
|
||||
COPY(height);
|
||||
COPY(grab_ts);
|
||||
memcpy(rawsink->mem->data, raw->data, raw->used);
|
||||
memcpy(rawsink->mem->data, frame->data, frame->used);
|
||||
# undef COPY
|
||||
|
||||
if (sem_post(rawsink->sig_sem) < 0) {
|
||||
@@ -176,7 +176,7 @@ int rawsink_server_put(rawsink_s *rawsink, frame_s *raw) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rawsink_client_get(rawsink_s *rawsink, frame_s *raw) { // cppcheck-suppress unusedFunction
|
||||
int rawsink_client_get(rawsink_s *rawsink, frame_s *frame) { // cppcheck-suppress unusedFunction
|
||||
assert(!rawsink->server); // Client only
|
||||
|
||||
if (_sem_timedwait_monotonic(rawsink->sig_sem, rawsink->timeout) < 0) {
|
||||
@@ -194,12 +194,12 @@ int rawsink_client_get(rawsink_s *rawsink, frame_s *raw) { // cppcheck-suppress
|
||||
return -1;
|
||||
}
|
||||
|
||||
# define COPY(_field) raw->_field = rawsink->mem->_field
|
||||
# define COPY(_field) frame->_field = rawsink->mem->_field
|
||||
COPY(width);
|
||||
COPY(height);
|
||||
COPY(format);
|
||||
COPY(grab_ts);
|
||||
frame_set_data(raw, rawsink->mem->data, rawsink->mem->used);
|
||||
frame_set_data(frame, rawsink->mem->data, rawsink->mem->used);
|
||||
# undef COPY
|
||||
|
||||
if (flock(rawsink->fd, LOCK_UN) < 0) {
|
||||
|
||||
@@ -72,5 +72,5 @@ typedef struct {
|
||||
rawsink_s *rawsink_init(const char *name, bool server, mode_t mode, bool rm, unsigned timeout);
|
||||
void rawsink_destroy(rawsink_s *rawsink);
|
||||
|
||||
int rawsink_server_put(rawsink_s *rawsink, frame_s *raw);
|
||||
int rawsink_client_get(rawsink_s *rawsink, frame_s *raw);
|
||||
int rawsink_server_put(rawsink_s *rawsink, frame_s *frame);
|
||||
int rawsink_client_get(rawsink_s *rawsink, frame_s *frame);
|
||||
|
||||
@@ -207,26 +207,26 @@ void encoder_get_runtime_params(encoder_s *encoder, encoder_type_e *type, unsign
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic push
|
||||
int encoder_compress(encoder_s *encoder, unsigned worker_number, frame_s *raw, frame_s *frame) {
|
||||
int encoder_compress(encoder_s *encoder, unsigned worker_number, frame_s *src, frame_s *dest) {
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
assert(ER(type) != ENCODER_TYPE_UNKNOWN);
|
||||
assert(raw->used > 0);
|
||||
assert(src->used > 0);
|
||||
|
||||
frame->grab_ts = raw->grab_ts;
|
||||
frame->encode_begin_ts = get_now_monotonic();
|
||||
dest->grab_ts = src->grab_ts;
|
||||
dest->encode_begin_ts = get_now_monotonic();
|
||||
|
||||
if (ER(type) == ENCODER_TYPE_CPU) {
|
||||
LOG_VERBOSE("Compressing buffer using CPU");
|
||||
cpu_encoder_compress(raw, frame, ER(quality));
|
||||
cpu_encoder_compress(src, dest, ER(quality));
|
||||
} else if (ER(type) == ENCODER_TYPE_HW) {
|
||||
LOG_VERBOSE("Compressing buffer using HW (just copying)");
|
||||
hw_encoder_compress(raw, frame);
|
||||
hw_encoder_compress(src, dest);
|
||||
}
|
||||
# ifdef WITH_OMX
|
||||
else if (ER(type) == ENCODER_TYPE_OMX) {
|
||||
LOG_VERBOSE("Compressing buffer using OMX");
|
||||
if (omx_encoder_compress(ER(omxs[worker_number]), raw, frame) < 0) {
|
||||
if (omx_encoder_compress(ER(omxs[worker_number]), src, dest) < 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -238,11 +238,11 @@ int encoder_compress(encoder_s *encoder, unsigned worker_number, frame_s *raw, f
|
||||
}
|
||||
# endif
|
||||
|
||||
frame->encode_end_ts = get_now_monotonic();
|
||||
dest->encode_end_ts = get_now_monotonic();
|
||||
|
||||
frame->width = raw->width;
|
||||
frame->height = raw->height;
|
||||
frame->format = V4L2_PIX_FMT_JPEG;
|
||||
dest->width = src->width;
|
||||
dest->height = src->height;
|
||||
dest->format = V4L2_PIX_FMT_JPEG;
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
@@ -111,4 +111,4 @@ const char *encoder_type_to_string(encoder_type_e type);
|
||||
void encoder_prepare(encoder_s *encoder, device_s *dev);
|
||||
void encoder_get_runtime_params(encoder_s *encoder, encoder_type_e *type, unsigned *quality);
|
||||
|
||||
int encoder_compress(encoder_s *encoder, unsigned worker_number, frame_s *raw, frame_s *frame);
|
||||
int encoder_compress(encoder_s *encoder, unsigned worker_number, frame_s *src, frame_s *dest);
|
||||
|
||||
@@ -35,7 +35,7 @@ typedef struct {
|
||||
} _jpeg_dest_manager_s;
|
||||
|
||||
|
||||
static void _jpeg_set_picture(j_compress_ptr jpeg, frame_s *frame);
|
||||
static void _jpeg_set_dest_frame(j_compress_ptr jpeg, frame_s *frame);
|
||||
|
||||
static void _jpeg_write_scanlines_yuyv(
|
||||
struct jpeg_compress_struct *jpeg, const uint8_t *data,
|
||||
@@ -58,7 +58,7 @@ static boolean _jpeg_empty_output_buffer(j_compress_ptr jpeg);
|
||||
static void _jpeg_term_destination(j_compress_ptr jpeg);
|
||||
|
||||
|
||||
void cpu_encoder_compress(frame_s *raw, frame_s *frame, unsigned quality) {
|
||||
void cpu_encoder_compress(frame_s *src, frame_s *dest, unsigned quality) {
|
||||
// This function based on compress_image_to_jpeg() from mjpg-streamer
|
||||
|
||||
struct jpeg_compress_struct jpeg;
|
||||
@@ -67,10 +67,10 @@ void cpu_encoder_compress(frame_s *raw, frame_s *frame, unsigned quality) {
|
||||
jpeg.err = jpeg_std_error(&jpeg_error);
|
||||
jpeg_create_compress(&jpeg);
|
||||
|
||||
_jpeg_set_picture(&jpeg, frame);
|
||||
_jpeg_set_dest_frame(&jpeg, dest);
|
||||
|
||||
jpeg.image_width = raw->width;
|
||||
jpeg.image_height = raw->height;
|
||||
jpeg.image_width = src->width;
|
||||
jpeg.image_height = src->height;
|
||||
jpeg.input_components = 3;
|
||||
jpeg.in_color_space = JCS_RGB;
|
||||
|
||||
@@ -80,9 +80,9 @@ void cpu_encoder_compress(frame_s *raw, frame_s *frame, unsigned quality) {
|
||||
jpeg_start_compress(&jpeg, TRUE);
|
||||
|
||||
# define WRITE_SCANLINES(_format, _func) \
|
||||
case _format: { _func(&jpeg, raw->data, raw->width, raw->height); break; }
|
||||
case _format: { _func(&jpeg, src->data, src->width, src->height); break; }
|
||||
|
||||
switch (raw->format) {
|
||||
switch (src->format) {
|
||||
// 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);
|
||||
@@ -96,10 +96,10 @@ void cpu_encoder_compress(frame_s *raw, frame_s *frame, unsigned quality) {
|
||||
jpeg_finish_compress(&jpeg);
|
||||
jpeg_destroy_compress(&jpeg);
|
||||
|
||||
assert(frame->used > 0);
|
||||
assert(dest->used > 0);
|
||||
}
|
||||
|
||||
static void _jpeg_set_picture(j_compress_ptr jpeg, frame_s *frame) {
|
||||
static void _jpeg_set_dest_frame(j_compress_ptr jpeg, frame_s *frame) {
|
||||
_jpeg_dest_manager_s *dest;
|
||||
|
||||
if (jpeg->dest == NULL) {
|
||||
|
||||
@@ -35,4 +35,4 @@
|
||||
#include "../../../common/frame.h"
|
||||
|
||||
|
||||
void cpu_encoder_compress(frame_s *raw, frame_s *frame, unsigned quality);
|
||||
void cpu_encoder_compress(frame_s *src, frame_s *dest, unsigned quality);
|
||||
|
||||
@@ -49,11 +49,11 @@ int hw_encoder_prepare(device_s *dev, unsigned quality) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hw_encoder_compress(frame_s *raw, frame_s *frame) {
|
||||
if (raw->format != V4L2_PIX_FMT_MJPEG && raw->format != V4L2_PIX_FMT_JPEG) {
|
||||
void hw_encoder_compress(frame_s *src, frame_s *dest) {
|
||||
if (src->format != V4L2_PIX_FMT_MJPEG && src->format != V4L2_PIX_FMT_JPEG) {
|
||||
assert(0 && "Unsupported input format for HW encoder");
|
||||
}
|
||||
_copy_plus_huffman(raw, frame);
|
||||
_copy_plus_huffman(src, dest);
|
||||
}
|
||||
|
||||
void _copy_plus_huffman(const frame_s *src, frame_s *dest) {
|
||||
|
||||
@@ -39,4 +39,4 @@
|
||||
|
||||
|
||||
int hw_encoder_prepare(device_s *dev, unsigned quality);
|
||||
void hw_encoder_compress(frame_s *raw, frame_s *frame);
|
||||
void hw_encoder_compress(frame_s *src, frame_s *dest);
|
||||
|
||||
@@ -154,12 +154,12 @@ int omx_encoder_prepare(omx_encoder_s *omx, device_s *dev, unsigned quality) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int omx_encoder_compress(omx_encoder_s *omx, frame_s *raw, frame_s *frame) {
|
||||
int omx_encoder_compress(omx_encoder_s *omx, frame_s *src, frame_s *dest) {
|
||||
# define IN(_next) omx->input_buffer->_next
|
||||
# define OUT(_next) omx->output_buffer->_next
|
||||
|
||||
OMX_ERRORTYPE error;
|
||||
size_t slice_size = (IN(nAllocLen) < raw->used ? IN(nAllocLen) : raw->used);
|
||||
size_t slice_size = (IN(nAllocLen) < src->used ? IN(nAllocLen) : src->used);
|
||||
size_t pos = 0;
|
||||
|
||||
if ((error = OMX_FillThisBuffer(omx->encoder, omx->output_buffer)) != OMX_ErrorNone) {
|
||||
@@ -167,7 +167,7 @@ int omx_encoder_compress(omx_encoder_s *omx, frame_s *raw, frame_s *frame) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
frame->used = 0;
|
||||
dest->used = 0;
|
||||
omx->output_available = false;
|
||||
omx->input_required = true;
|
||||
|
||||
@@ -179,7 +179,7 @@ int omx_encoder_compress(omx_encoder_s *omx, frame_s *raw, frame_s *frame) {
|
||||
if (omx->output_available) {
|
||||
omx->output_available = false;
|
||||
|
||||
frame_append_data(frame, OUT(pBuffer) + OUT(nOffset), OUT(nFilledLen));
|
||||
frame_append_data(dest, OUT(pBuffer) + OUT(nOffset), OUT(nFilledLen));
|
||||
|
||||
if (OUT(nFlags) & OMX_BUFFERFLAG_ENDOFFRAME) {
|
||||
OUT(nFlags) = 0;
|
||||
@@ -195,18 +195,18 @@ int omx_encoder_compress(omx_encoder_s *omx, frame_s *raw, frame_s *frame) {
|
||||
if (omx->input_required) {
|
||||
omx->input_required = false;
|
||||
|
||||
if (pos == raw->used) {
|
||||
if (pos == src->used) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(IN(pBuffer), raw->data + pos, slice_size);
|
||||
memcpy(IN(pBuffer), src->data + pos, slice_size);
|
||||
IN(nOffset) = 0;
|
||||
IN(nFilledLen) = slice_size;
|
||||
|
||||
pos += slice_size;
|
||||
|
||||
if (pos + slice_size > raw->used) {
|
||||
slice_size = raw->used - pos;
|
||||
if (pos + slice_size > src->used) {
|
||||
slice_size = src->used - pos;
|
||||
}
|
||||
|
||||
if ((error = OMX_EmptyThisBuffer(omx->encoder, omx->input_buffer)) != OMX_ErrorNone) {
|
||||
|
||||
@@ -71,4 +71,4 @@ omx_encoder_s *omx_encoder_init(void);
|
||||
void omx_encoder_destroy(omx_encoder_s *omx);
|
||||
|
||||
int omx_encoder_prepare(omx_encoder_s *omx, device_s *dev, unsigned quality);
|
||||
int omx_encoder_compress(omx_encoder_s *omx, frame_s *raw, frame_s *frame);
|
||||
int omx_encoder_compress(omx_encoder_s *omx, frame_s *src, frame_s *dest);
|
||||
|
||||
@@ -94,7 +94,7 @@ typedef struct stream_client_sx {
|
||||
} stream_client_s;
|
||||
|
||||
typedef struct {
|
||||
frame_s *frame;
|
||||
frame_s *frame;
|
||||
unsigned captured_fps;
|
||||
unsigned queued_fps;
|
||||
bool online;
|
||||
|
||||
Reference in New Issue
Block a user