mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-03-13 02:53:42 +00:00
improved prepare logic
This commit is contained in:
@@ -94,6 +94,13 @@ void h264_encoder_destroy(h264_encoder_s *enc) {
|
|||||||
int h264_encoder_prepare(h264_encoder_s *enc, const frame_s *frame) {
|
int h264_encoder_prepare(h264_encoder_s *enc, const frame_s *frame) {
|
||||||
LOG_INFO("H264: Configuring MMAL encoder ...");
|
LOG_INFO("H264: Configuring MMAL encoder ...");
|
||||||
|
|
||||||
|
_h264_encoder_cleanup(enc);
|
||||||
|
|
||||||
|
enc->width = frame->width;
|
||||||
|
enc->height = frame->height;
|
||||||
|
enc->format = frame->format;
|
||||||
|
enc->stride = frame->stride;
|
||||||
|
|
||||||
if (align_size(frame->width, 32) != frame->width && frame_get_padding(frame) == 0) {
|
if (align_size(frame->width, 32) != frame->width && frame_get_padding(frame) == 0) {
|
||||||
LOG_ERROR("H264: MMAL encoder can't handle unaligned width");
|
LOG_ERROR("H264: MMAL encoder can't handle unaligned width");
|
||||||
goto error;
|
goto error;
|
||||||
@@ -132,9 +139,6 @@ int h264_encoder_prepare(h264_encoder_s *enc, const frame_s *frame) {
|
|||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
_h264_encoder_cleanup(enc);
|
|
||||||
enc->wrapper->status = MMAL_SUCCESS; // Это реально надо?
|
|
||||||
|
|
||||||
{
|
{
|
||||||
PREPARE_PORT(input);
|
PREPARE_PORT(input);
|
||||||
|
|
||||||
@@ -212,11 +216,7 @@ int h264_encoder_prepare(h264_encoder_s *enc, const frame_s *frame) {
|
|||||||
ENABLE_PORT(input);
|
ENABLE_PORT(input);
|
||||||
ENABLE_PORT(output);
|
ENABLE_PORT(output);
|
||||||
|
|
||||||
enc->width = frame->width;
|
enc->prepared = true;
|
||||||
enc->height = frame->height;
|
|
||||||
enc->format = frame->format;
|
|
||||||
enc->stride = frame->stride;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
@@ -246,14 +246,15 @@ static void _h264_encoder_cleanup(h264_encoder_s *enc) {
|
|||||||
|
|
||||||
# undef DISABLE_PORT
|
# undef DISABLE_PORT
|
||||||
|
|
||||||
enc->width = 0;
|
enc->wrapper->status = MMAL_SUCCESS; // Это реально надо?
|
||||||
enc->height = 0;
|
|
||||||
enc->format = 0;
|
|
||||||
enc->stride = 0;
|
|
||||||
enc->last_online = -1;
|
enc->last_online = -1;
|
||||||
|
enc->prepared = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int h264_encoder_compress(h264_encoder_s *enc, const frame_s *src, frame_s *dest) {
|
int h264_encoder_compress(h264_encoder_s *enc, const frame_s *src, frame_s *dest) {
|
||||||
|
assert(enc->prepared);
|
||||||
|
|
||||||
frame_copy_meta(src, dest);
|
frame_copy_meta(src, dest);
|
||||||
dest->encode_begin_ts = get_now_monotonic();
|
dest->encode_begin_ts = get_now_monotonic();
|
||||||
dest->format = V4L2_PIX_FMT_H264;
|
dest->format = V4L2_PIX_FMT_H264;
|
||||||
@@ -270,6 +271,7 @@ int h264_encoder_compress(h264_encoder_s *enc, const frame_s *src, frame_s *dest
|
|||||||
LOG_DEBUG("H264: Copying source to tmp buffer ...");
|
LOG_DEBUG("H264: Copying source to tmp buffer ...");
|
||||||
frame_copy(src, enc->tmp);
|
frame_copy(src, enc->tmp);
|
||||||
assert(src->format == enc->format);
|
assert(src->format == enc->format);
|
||||||
|
assert(enc->tmp->stride == enc->stride);
|
||||||
LOG_VERBOSE("H264: Source copied; time=%Lf", get_now_monotonic() - dest->encode_begin_ts);
|
LOG_VERBOSE("H264: Source copied; time=%Lf", get_now_monotonic() - dest->encode_begin_ts);
|
||||||
}
|
}
|
||||||
src = enc->tmp;
|
src = enc->tmp;
|
||||||
|
|||||||
@@ -54,10 +54,11 @@ typedef struct {
|
|||||||
frame_s *tmp;
|
frame_s *tmp;
|
||||||
int last_online;
|
int last_online;
|
||||||
|
|
||||||
unsigned width;
|
unsigned width;
|
||||||
unsigned height;
|
unsigned height;
|
||||||
unsigned format;
|
unsigned format;
|
||||||
unsigned stride;
|
unsigned stride;
|
||||||
|
bool prepared;
|
||||||
} h264_encoder_s;
|
} h264_encoder_s;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -440,11 +440,11 @@ static void _h264_stream_destroy(h264_stream_s *h264) {
|
|||||||
|
|
||||||
static void _h264_stream_process(h264_stream_s *h264, const frame_s *frame) {
|
static void _h264_stream_process(h264_stream_s *h264, const frame_s *frame) {
|
||||||
# define NEQ(_field) (frame->_field != h264->enc->_field)
|
# define NEQ(_field) (frame->_field != h264->enc->_field)
|
||||||
if (NEQ(width) || NEQ(height) || NEQ(format)) {
|
if (NEQ(width) || NEQ(height) || NEQ(format) || NEQ(stride)) {
|
||||||
# undef NEQ
|
# undef NEQ
|
||||||
h264_encoder_prepare(h264->enc, frame);
|
h264_encoder_prepare(h264->enc, frame);
|
||||||
}
|
}
|
||||||
if (h264->enc->format) {
|
if (h264->enc->prepared) {
|
||||||
if (h264_encoder_compress(h264->enc, frame, h264->dest) == 0) {
|
if (h264_encoder_compress(h264->enc, frame, h264->dest) == 0) {
|
||||||
memsink_server_put(h264->sink, h264->dest);
|
memsink_server_put(h264->sink, h264->dest);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user