initializing global object in main()

This commit is contained in:
Devaev Maxim
2021-01-03 10:22:29 +03:00
parent f7504211e5
commit 619389970a
6 changed files with 46 additions and 50 deletions

View File

@@ -57,9 +57,6 @@ void h264_encoder_destroy(h264_encoder_s *encoder) {
if (RUN(i_handler_sem)) { if (RUN(i_handler_sem)) {
vcos_semaphore_delete(&RUN(handler_sem)); vcos_semaphore_delete(&RUN(handler_sem));
} }
if (RUN(i_bcm_host)) {
bcm_host_deinit();
}
frame_destroy(RUN(tmp)); frame_destroy(RUN(tmp));
free(encoder); free(encoder);
} }
@@ -70,14 +67,7 @@ int h264_encoder_compress(h264_encoder_s *encoder, const frame_s *src, frame_s *
assert(src->height > 0); assert(src->height > 0);
assert(src->format > 0); assert(src->format > 0);
if (!RUN(i_bcm_host)) {
LOG_INFO("Initializing BCM ...");
bcm_host_init();
RUN(i_bcm_host) = true;
}
if (!RUN(i_handler_sem)) { if (!RUN(i_handler_sem)) {
LOG_INFO("Creating VCOS semaphore ...");
if (vcos_semaphore_create(&RUN(handler_sem), "h264_handler_sem", 0) != VCOS_SUCCESS) { if (vcos_semaphore_create(&RUN(handler_sem), "h264_handler_sem", 0) != VCOS_SUCCESS) {
LOG_PERROR("Can't create VCOS semaphore"); LOG_PERROR("Can't create VCOS semaphore");
return -1; return -1;
@@ -156,18 +146,16 @@ static int _h264_encoder_configure(h264_encoder_s *encoder, const frame_s *frame
# define IFMT(_next) RUN(input_port->format->_next) # define IFMT(_next) RUN(input_port->format->_next)
IFMT(type) = MMAL_ES_TYPE_VIDEO; IFMT(type) = MMAL_ES_TYPE_VIDEO;
if (frame->format == V4L2_PIX_FMT_UYVY) { switch (frame->format) {
LOG_INFO("Using pixelformat: UYVY"); case V4L2_PIX_FMT_YUYV: IFMT(encoding) = MMAL_ENCODING_YUYV; break;
IFMT(encoding) = MMAL_ENCODING_UYVY; case V4L2_PIX_FMT_UYVY: IFMT(encoding) = MMAL_ENCODING_UYVY; break;
} else if (frame->format == V4L2_PIX_FMT_RGB24) { case V4L2_PIX_FMT_RGB565: IFMT(encoding) = MMAL_ENCODING_RGB16; break;
LOG_INFO("Using pixelformat: RGB24"); case V4L2_PIX_FMT_RGB24: IFMT(encoding) = MMAL_ENCODING_RGB24; break;
IFMT(encoding) = MMAL_ENCODING_RGB24; default:
} else { char fourcc_buf[8];
char fourcc_buf[8]; LOG_ERROR("Unsupported input format for MMAL (fourcc): %s", fourcc_to_string(frame->format, fourcc_buf, 8));
LOG_ERROR("Unsupported pixelformat (fourcc): %s", fourcc_to_string(frame->format, fourcc_buf, 8)); goto error;
goto error;
} }
LOG_INFO("Using resolution: %ux%u", frame->width, frame->height);
IFMT(es->video.width) = align_size(frame->width, 32); IFMT(es->video.width) = align_size(frame->width, 32);
IFMT(es->video.height) = align_size(frame->height, 16); IFMT(es->video.height) = align_size(frame->height, 16);
IFMT(es->video.crop.x) = 0; IFMT(es->video.crop.x) = 0;

View File

@@ -28,7 +28,6 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <bcm_host.h>
#include <interface/mmal/mmal.h> #include <interface/mmal/mmal.h>
#include <interface/mmal/mmal_format.h> #include <interface/mmal/mmal_format.h>
#include <interface/mmal/util/mmal_default_components.h> #include <interface/mmal/util/mmal_default_components.h>
@@ -46,7 +45,6 @@ typedef struct {
MMAL_PORT_T *input_port; MMAL_PORT_T *input_port;
MMAL_PORT_T *output_port; MMAL_PORT_T *output_port;
VCOS_SEMAPHORE_T handler_sem; VCOS_SEMAPHORE_T handler_sem;
bool i_bcm_host;
bool i_handler_sem; bool i_handler_sem;
unsigned width; unsigned width;

View File

@@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <bcm_host.h>
#include "../common/logging.h" #include "../common/logging.h"
#include "../common/frame.h" #include "../common/frame.h"
#include "../memsink/memsink.h" #include "../memsink/memsink.h"
@@ -10,6 +12,8 @@ int main(void) {
LOGGING_INIT; LOGGING_INIT;
log_level = 3; log_level = 3;
bcm_host_init();
frame_s *src = frame_init("src"); frame_s *src = frame_init("src");
frame_s *dest = frame_init("dest"); frame_s *dest = frame_init("dest");
h264_encoder_s *encoder = h264_encoder_init(); h264_encoder_s *encoder = h264_encoder_init();

View File

@@ -27,9 +27,6 @@ static const OMX_U32 _INPUT_PORT = 340;
static const OMX_U32 _OUTPUT_PORT = 341; static const OMX_U32 _OUTPUT_PORT = 341;
static int _i_omx = 0;
static int _vcos_semwait(VCOS_SEMAPHORE_T *sem); static int _vcos_semwait(VCOS_SEMAPHORE_T *sem);
static int _omx_init_component(omx_encoder_s *omx); static int _omx_init_component(omx_encoder_s *omx);
static int _omx_init_disable_ports(omx_encoder_s *omx); static int _omx_init_disable_ports(omx_encoder_s *omx);
@@ -65,19 +62,6 @@ omx_encoder_s *omx_encoder_init(void) {
omx_encoder_s *omx; omx_encoder_s *omx;
A_CALLOC(omx, 1); A_CALLOC(omx, 1);
assert(_i_omx >= 0);
if (_i_omx == 0) {
LOG_INFO("Initializing BCM ...");
bcm_host_init();
LOG_INFO("Initializing OMX ...");
if ((OMX_ERRORTYPE error = OMX_Init()) != OMX_ErrorNone) {
LOG_ERROR_OMX(error, "Can't initialize OMX");
goto error;
}
}
_i_omx += 1;
LOG_INFO("Initializing OMX encoder ..."); LOG_INFO("Initializing OMX encoder ...");
if (vcos_semaphore_create(&omx->handler_sem, "handler_sem", 0) != VCOS_SUCCESS) { if (vcos_semaphore_create(&omx->handler_sem, "handler_sem", 0) != VCOS_SUCCESS) {
@@ -118,16 +102,6 @@ void omx_encoder_destroy(omx_encoder_s *omx) {
} }
} }
assert(_i_omx >= 0);
_i_omx -= 1;
if (_i_omx == 0) {
LOG_INFO("Destroying OMX ...");
OMX_Deinit();
LOG_INFO("Destroying BCM ...");
bcm_host_deinit();
}
free(omx); free(omx);
} }

View File

@@ -30,7 +30,6 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <bcm_host.h>
#include <IL/OMX_Core.h> #include <IL/OMX_Core.h>
#include <IL/OMX_Component.h> #include <IL/OMX_Component.h>
#include <IL/OMX_Broadcom.h> #include <IL/OMX_Broadcom.h>

View File

@@ -35,6 +35,10 @@
#include <signal.h> #include <signal.h>
#include <pthread.h> #include <pthread.h>
#ifdef WITH_OMX
# include <bcm_host.h>
# include <IL/OMX_Core.h>
#endif
#include "../libs/common/tools.h" #include "../libs/common/tools.h"
#include "../libs/common/threading.h" #include "../libs/common/threading.h"
@@ -123,6 +127,25 @@ int main(int argc, char *argv[]) {
int exit_code = 0; int exit_code = 0;
if ((exit_code = options_parse(options, dev, encoder, stream, server)) == 0) { if ((exit_code = options_parse(options, dev, encoder, stream, server)) == 0) {
# ifdef WITH_OMX
bool i_bcm_host = false;
if (encoder->type == ENCODER_TYPE_OMX) {
LOG_INFO("Initializing BCM Host ...");
bcm_host_init();
i_bcm_host = true;
}
bool i_omx_core = false
if (encodeer->type == ENCODER_TYPE_OMX) {
LOG_INFO("Initializing OMX Core ...");
if ((OMX_ERRORTYPE error = OMX_Init()) != OMX_ErrorNone) {
LOG_ERROR_OMX(error, "Can't initialize OMX Core");
exit_code = -1;
goto omx_error;
}
i_omx_core = true;
}
# endif
# ifdef WITH_GPIO # ifdef WITH_GPIO
gpio_init(); gpio_init();
# endif # endif
@@ -151,6 +174,16 @@ int main(int argc, char *argv[]) {
gpio_set_prog_running(false); gpio_set_prog_running(false);
gpio_destroy(); gpio_destroy();
# endif # endif
# ifdef WITH_OMX
omx_error:
if (i_omx_core) {
OMX_Deinit();
}
if (i_bcm_host) {
bcm_host_deinit();
}
# endif
} }
server_destroy(server); server_destroy(server);