mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-02-28 20:56:33 +00:00
encoder runtime
This commit is contained in:
@@ -47,11 +47,16 @@ static const struct {
|
||||
|
||||
|
||||
struct encoder_t *encoder_init() {
|
||||
struct encoder_runtime_t *run;
|
||||
struct encoder_t *encoder;
|
||||
|
||||
A_CALLOC(run, 1);
|
||||
run->type = ENCODER_TYPE_CPU;
|
||||
|
||||
A_CALLOC(encoder, 1);
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
encoder->type = run->type;
|
||||
encoder->quality = 80;
|
||||
encoder->run = run;
|
||||
return encoder;
|
||||
}
|
||||
|
||||
@@ -61,15 +66,16 @@ void encoder_prepare(struct encoder_t *encoder, struct device_t *dev) {
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
assert(encoder->type != ENCODER_TYPE_UNKNOWN);
|
||||
encoder->run->type = encoder->type;
|
||||
|
||||
if (encoder->type != ENCODER_TYPE_CPU) {
|
||||
if (encoder->run->type != ENCODER_TYPE_CPU) {
|
||||
LOG_DEBUG("Initializing encoder ...");
|
||||
}
|
||||
|
||||
LOG_INFO("Using JPEG quality: %u%%", encoder->quality);
|
||||
|
||||
# ifdef OMX_ENCODER
|
||||
if (encoder->type == ENCODER_TYPE_OMX) {
|
||||
if (encoder->run->type == ENCODER_TYPE_OMX) {
|
||||
if (dev->n_workers > OMX_MAX_ENCODERS) {
|
||||
LOG_INFO(
|
||||
"OMX-based encoder can only work with %u worker threads; forced --workers=%u",
|
||||
@@ -77,11 +83,11 @@ void encoder_prepare(struct encoder_t *encoder, struct device_t *dev) {
|
||||
);
|
||||
dev->n_workers = OMX_MAX_ENCODERS;
|
||||
}
|
||||
encoder->n_omxs = dev->n_workers;
|
||||
encoder->run->n_omxs = dev->n_workers;
|
||||
|
||||
A_CALLOC(encoder->omxs, encoder->n_omxs);
|
||||
for (unsigned index = 0; index < encoder->n_omxs; ++index) {
|
||||
if ((encoder->omxs[index] = omx_encoder_init()) == NULL) {
|
||||
A_CALLOC(encoder->run->omxs, encoder->run->n_omxs);
|
||||
for (unsigned index = 0; index < encoder->run->n_omxs; ++index) {
|
||||
if ((encoder->run->omxs[index] = omx_encoder_init()) == NULL) {
|
||||
goto use_fallback;
|
||||
}
|
||||
}
|
||||
@@ -94,21 +100,22 @@ void encoder_prepare(struct encoder_t *encoder, struct device_t *dev) {
|
||||
# pragma GCC diagnostic push
|
||||
use_fallback:
|
||||
LOG_ERROR("Can't initialize selected encoder, using CPU instead it");
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
encoder->run->type = ENCODER_TYPE_CPU;
|
||||
# pragma GCC diagnostic pop
|
||||
}
|
||||
|
||||
void encoder_destroy(struct encoder_t *encoder) {
|
||||
# ifdef OMX_ENCODER
|
||||
if (encoder->omxs) {
|
||||
for (unsigned index = 0; index < encoder->n_omxs; ++index) {
|
||||
if (encoder->omxs[index]) {
|
||||
omx_encoder_destroy(encoder->omxs[index]);
|
||||
if (encoder->run->omxs) {
|
||||
for (unsigned index = 0; index < encoder->run->n_omxs; ++index) {
|
||||
if (encoder->run->omxs[index]) {
|
||||
omx_encoder_destroy(encoder->run->omxs[index]);
|
||||
}
|
||||
}
|
||||
free(encoder->omxs);
|
||||
free(encoder->run->omxs);
|
||||
}
|
||||
# endif
|
||||
free(encoder->run);
|
||||
free(encoder);
|
||||
}
|
||||
|
||||
@@ -124,13 +131,13 @@ enum encoder_type_t encoder_parse_type(const char *const str) {
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic push
|
||||
void encoder_prepare_live(struct encoder_t *encoder, struct device_t *dev) {
|
||||
assert(encoder->type != ENCODER_TYPE_UNKNOWN);
|
||||
assert(encoder->run->type != ENCODER_TYPE_UNKNOWN);
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
# ifdef OMX_ENCODER
|
||||
if (encoder->type == ENCODER_TYPE_OMX) {
|
||||
for (unsigned index = 0; index < encoder->n_omxs; ++index) {
|
||||
if (omx_encoder_prepare_live(encoder->omxs[index], dev, encoder->quality) < 0) {
|
||||
if (encoder->run->type == ENCODER_TYPE_OMX) {
|
||||
for (unsigned index = 0; index < encoder->run->n_omxs; ++index) {
|
||||
if (omx_encoder_prepare_live(encoder->run->omxs[index], dev, encoder->quality) < 0) {
|
||||
goto use_fallback;
|
||||
}
|
||||
}
|
||||
@@ -143,7 +150,7 @@ void encoder_prepare_live(struct encoder_t *encoder, struct device_t *dev) {
|
||||
# pragma GCC diagnostic push
|
||||
use_fallback:
|
||||
LOG_ERROR("Can't prepare selected encoder, falling back to CPU");
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
encoder->run->type = ENCODER_TYPE_CPU;
|
||||
# pragma GCC diagnostic pop
|
||||
}
|
||||
|
||||
@@ -153,14 +160,14 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev,
|
||||
const unsigned worker_number, const unsigned buf_index) {
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
assert(encoder->type != ENCODER_TYPE_UNKNOWN);
|
||||
assert(encoder->run->type != ENCODER_TYPE_UNKNOWN);
|
||||
|
||||
if (encoder->type == ENCODER_TYPE_CPU) {
|
||||
if (encoder->run->type == ENCODER_TYPE_CPU) {
|
||||
jpeg_encoder_compress_buffer(dev, buf_index, encoder->quality);
|
||||
}
|
||||
# ifdef OMX_ENCODER
|
||||
else if (encoder->type == ENCODER_TYPE_OMX) {
|
||||
if (omx_encoder_compress_buffer(encoder->omxs[worker_number], dev, buf_index) < 0) {
|
||||
else if (encoder->run->type == ENCODER_TYPE_OMX) {
|
||||
if (omx_encoder_compress_buffer(encoder->run->omxs[worker_number], dev, buf_index) < 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -172,7 +179,7 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev,
|
||||
# pragma GCC diagnostic push
|
||||
error:
|
||||
LOG_INFO("HW compressing error, falling back to CPU");
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
encoder->run->type = ENCODER_TYPE_CPU;
|
||||
return -1;
|
||||
# pragma GCC diagnostic pop
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user