avcenc: Replacing KEEP_THREADS_ACTIVE with runtime check

Test: avcenc -c enc.cfg
Change-Id: If6c8c29d2b5b1322796b8b25dc74231d13b61243
This commit is contained in:
Sushant 2025-03-20 15:04:28 +05:30 committed by ram
parent 262b845c31
commit 5e9c764903
18 changed files with 99 additions and 111 deletions

View file

@ -83,7 +83,6 @@ cc_defaults {
"-Wall",
"-Werror",
"-Wno-error=constant-conversion",
"-UKEEP_THREADS_ACTIVE",
],
arch: {
arm: {

View file

@ -18,7 +18,6 @@ set(AVC_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}")
option(ENABLE_MVC "Enables svcenc and svcdec builds" OFF)
option(ENABLE_SVC "Enables svcenc and svcdec builds" OFF)
option(ENABLE_TESTS "Enables gtest based unit tests" OFF)
option(KEEP_THREADS_ACTIVE "Enable KEEP THREADS ACTIVE" OFF)
if("${AVC_ROOT}" STREQUAL "${AVC_CONFIG_DIR}")
message(

View file

@ -558,7 +558,6 @@ IH264_ERROR_T ih264_list_dequeue(list_t *ps_list, void *pv_buf, WORD32 blocking)
return ret;
}
#ifdef KEEP_THREADS_ACTIVE
/**
*******************************************************************************
*
@ -585,4 +584,3 @@ WORD32 ih264_get_job_count_in_list(list_t *ps_list)
RETURN_IF((ih264_list_unlock(ps_list) != IH264_SUCCESS), 0);
return jobs;
}
#endif /* KEEP_THREADS_ACTIVE */

View file

@ -100,10 +100,6 @@ IH264_ERROR_T ih264_list_queue(list_t *ps_list, void *pv_buf, WORD32 blocking);
IH264_ERROR_T ih264_list_dequeue(list_t *ps_list, void *pv_buf,
WORD32 blocking);
#ifdef KEEP_THREADS_ACTIVE
WORD32 ih264_get_job_count_in_list(list_t *ps_list);
#endif /* KEEP_THREADS_ACTIVE */
#endif /* _IH264_LIST_H_ */

View file

@ -235,7 +235,6 @@ WORD32 ithread_cond_signal(void *cond)
return pthread_cond_signal((pthread_cond_t *)cond);
}
#ifdef KEEP_THREADS_ACTIVE
UWORD32 ithread_get_cond_size(void)
{
return sizeof(pthread_cond_t);
@ -245,4 +244,3 @@ WORD32 ithread_cond_broadcast(void *cond)
{
return pthread_cond_broadcast((pthread_cond_t *)cond);
}
#endif /* KEEP_THREADS_ACTIVE */

View file

@ -95,12 +95,8 @@ WORD32 ithread_cond_wait(void *cond, void *mutex);
WORD32 ithread_cond_signal(void *cond);
#ifdef KEEP_THREADS_ACTIVE
UWORD32 ithread_get_cond_size(void);
WORD32 ithread_cond_broadcast(void *cond);
#endif /* KEEP_THREADS_ACTIVE */
#endif /* _ITHREAD_H_ */

View file

@ -661,6 +661,7 @@ static IV_STATUS_T api_check_struct_sanity(iv_obj_t *ps_handle,
ps_ip->s_ive_ip.u4_max_srch_rng_x;
s_ip.s_ive_ip.u4_max_srch_rng_y =
ps_ip->s_ive_ip.u4_max_srch_rng_y;
s_ip.s_ive_ip.u4_keep_threads_active = ps_ip->s_ive_ip.u4_keep_threads_active;
for (i = 0; i < MEM_REC_CNT; i++)
{
@ -2857,13 +2858,14 @@ static WORD32 ih264e_init(codec_t *ps_codec)
/* ctl mutex init */
ithread_mutex_init(ps_codec->pv_ctl_mutex);
#ifdef KEEP_THREADS_ACTIVE
/* thread pool mutex init */
ithread_mutex_init(ps_codec->s_thread_pool.pv_thread_pool_mutex);
if (ps_codec->s_cfg.u4_keep_threads_active)
{
/* thread pool mutex init */
ithread_mutex_init(ps_codec->s_thread_pool.pv_thread_pool_mutex);
/* thread pool conditional init */
ithread_cond_init(ps_codec->s_thread_pool.pv_thread_pool_cond);
#endif /* KEEP_THREADS_ACTIVE */
/* thread pool conditional init */
ithread_cond_init(ps_codec->s_thread_pool.pv_thread_pool_cond);
}
/* Set encoder chroma format */
ps_codec->e_codec_color_format =
@ -3502,11 +3504,11 @@ static WORD32 ih264e_fill_num_mem_rec(void *pv_api_ip, void *pv_api_op)
}
DEBUG("\nMemory record Id %d = %d \n", MEM_REC_SLICE_MAP, ps_mem_rec->u4_mem_size);
#ifdef KEEP_THREADS_ACTIVE
ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_HANDLE];
/************************************************************************
* Request memory for thread pool context *
***********************************************************************/
ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_POOL];
* Request memory for thread pool context *
************************************************************************/
if (ps_ip->s_ive_ip.u4_keep_threads_active)
{
/* total size of the mem record */
WORD32 total_size = 0;
@ -3523,19 +3525,16 @@ static WORD32 ih264e_fill_num_mem_rec(void *pv_api_ip, void *pv_api_op)
/* Store the total calculated size in memory record */
ps_mem_rec->u4_mem_size = total_size;
}
DEBUG("\nMemory record Id %d = %d \n", MEM_REC_THREAD_POOL, ps_mem_rec->u4_mem_size);
#else
else
/************************************************************************
* Request memory to hold thread handles for each processing thread *
************************************************************************/
ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_HANDLE];
{
WORD32 handle_size = ithread_get_handle_size();
ps_mem_rec->u4_mem_size = MAX_PROCESS_THREADS * handle_size;
}
DEBUG("\nMemory record Id %d = %d \n", MEM_REC_THREAD_HANDLE, ps_mem_rec->u4_mem_size);
#endif /* KEEP_THREADS_ACTIVE */
/************************************************************************
* Request memory to hold mutex for control calls *
@ -4117,6 +4116,7 @@ static WORD32 ih264e_init_mem_rec(iv_obj_t *ps_codec_obj,
ps_cfg->e_soc = ps_ip->s_ive_ip.e_soc;
ps_cfg->u4_enable_recon = ps_ip->s_ive_ip.u4_enable_recon;
ps_cfg->e_rc_mode = ps_ip->s_ive_ip.e_rc_mode;
ps_cfg->u4_keep_threads_active = ps_ip->s_ive_ip.u4_keep_threads_active;
/* Validate params */
if ((ps_ip->s_ive_ip.u4_max_level < MIN_LEVEL)
@ -4449,8 +4449,8 @@ static WORD32 ih264e_init_mem_rec(iv_obj_t *ps_codec_obj,
}
}
#ifdef KEEP_THREADS_ACTIVE
ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_POOL];
ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_HANDLE];
if (ps_ip->s_ive_ip.u4_keep_threads_active)
{
/* temp var */
WORD32 i = 0;
@ -4471,9 +4471,11 @@ static WORD32 ih264e_init_mem_rec(iv_obj_t *ps_codec_obj,
pu1_buf += ALIGN128(ithread_get_handle_size());
}
}
#else
ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_HANDLE];
else
{
/* temp var */
WORD32 i = 0;
WORD32 handle_size = ithread_get_handle_size();
for (i = 0; i < MAX_PROCESS_THREADS; i++)
@ -4482,7 +4484,6 @@ static WORD32 ih264e_init_mem_rec(iv_obj_t *ps_codec_obj,
+ (i * handle_size);
}
}
#endif /* KEEP_THREADS_ACTIVE */
ps_mem_rec = &ps_mem_rec_base[MEM_REC_CTL_MUTEX];
{
@ -4996,12 +4997,17 @@ static WORD32 ih264e_retrieve_memrec(iv_obj_t *ps_codec_obj,
return IV_FAIL;
}
#ifdef KEEP_THREADS_ACTIVE
ih264e_thread_pool_shutdown(ps_codec);
#else
/* join threads upon at end of sequence */
ih264e_join_threads(ps_codec);
#endif /* KEEP_THREADS_ACTIVE */
if (ps_codec->s_cfg.u4_keep_threads_active)
{
ih264e_thread_pool_shutdown(ps_codec);
ithread_cond_destroy(ps_codec->s_thread_pool.pv_thread_pool_cond);
ithread_mutex_destroy(ps_codec->s_thread_pool.pv_thread_pool_mutex);
}
else
{
/* join threads upon at end of sequence */
ih264e_join_threads(ps_codec);
}
/* collect list of memory records used by the encoder library */
memcpy(ps_ip->s_ive_ip.ps_mem_rec, ps_codec->ps_mem_rec_backup,
@ -5013,10 +5019,6 @@ static WORD32 ih264e_retrieve_memrec(iv_obj_t *ps_codec_obj,
ih264_list_free(ps_codec->pv_proc_jobq);
ithread_mutex_destroy(ps_codec->pv_ctl_mutex);
ithread_mutex_destroy(ps_codec->pv_entropy_mutex);
#ifdef KEEP_THREADS_ACTIVE
ithread_cond_destroy(ps_codec->s_thread_pool.pv_thread_pool_cond);
ithread_mutex_destroy(ps_codec->s_thread_pool.pv_thread_pool_mutex);
#endif /* KEEP_THREADS_ACTIVE */
ih264_buf_mgr_free((buf_mgr_t *)ps_codec->pv_mv_buf_mgr);
ih264_buf_mgr_free((buf_mgr_t *)ps_codec->pv_ref_buf_mgr);
@ -6196,10 +6198,11 @@ static WORD32 ih264e_reset(iv_obj_t *ps_codec_obj,
if (ps_codec != NULL)
{
#ifdef KEEP_THREADS_ACTIVE
/* Shutdown active threads before reinitialization */
ih264e_thread_pool_shutdown(ps_codec);
#endif
if (ps_codec->s_cfg.u4_keep_threads_active)
{
/* Shutdown active threads before reinitialization */
ih264e_thread_pool_shutdown(ps_codec);
}
ih264e_init(ps_codec);
}
else

View file

@ -469,17 +469,10 @@ enum
*/
MEM_REC_SLICE_MAP,
#ifdef KEEP_THREADS_ACTIVE
/**
* Holds thread pool
*/
MEM_REC_THREAD_POOL,
#else
/**
* Holds thread handles
*/
MEM_REC_THREAD_HANDLE,
#endif /* KEEP_THREADS_ACTIVE */
/**
* Holds control call mutex

View file

@ -109,7 +109,6 @@
/* Function Definitions */
/*****************************************************************************/
#ifdef KEEP_THREADS_ACTIVE
/**
*******************************************************************************
*
@ -378,7 +377,6 @@ WORD32 ih264e_thread_pool_sync(codec_t *ps_codec)
return ret;
}
#else
/**
******************************************************************************
@ -420,7 +418,6 @@ void ih264e_join_threads(codec_t *ps_codec)
ps_codec->i4_proc_thread_cnt = 0;
}
#endif /* KEEP_THREADS_ACTIVE */
/**
******************************************************************************
@ -859,10 +856,10 @@ WORD32 ih264e_encode(iv_obj_t *ps_codec_obj, void *pv_api_ip, void *pv_api_op)
ps_codec->i4_gen_header = 1;
}
#ifdef KEEP_THREADS_ACTIVE /* initialize thread pool */
ih264e_thread_pool_init(ps_codec);
#endif /* KEEP_THREADS_ACTIVE */
if (ps_codec->s_cfg.u4_keep_threads_active)
{
ih264e_thread_pool_init(ps_codec);
}
}
/* generate header and return when encoder is operated in header mode */
@ -944,41 +941,43 @@ WORD32 ih264e_encode(iv_obj_t *ps_codec_obj, void *pv_api_ip, void *pv_api_op)
ps_video_encode_op->s_ive_op.u4_error_code,
IV_FAIL);
#ifdef KEEP_THREADS_ACTIVE
/* reset thread pool and prepare for new frame */
ih264e_thread_pool_activate(ps_codec);
/* main thread */
ih264e_process_thread(&ps_codec->as_process[0]);
/* sync all threads */
ih264e_thread_pool_sync(ps_codec);
#else
for (i = 0; i < num_thread_cnt; i++)
if (ps_codec->s_cfg.u4_keep_threads_active)
{
ret = ithread_create(ps_codec->apv_proc_thread_handle[i],
NULL,
(void *)ih264e_process_thread,
&ps_codec->as_process[i + 1]);
if (ret != 0)
/* reset thread pool and prepare for new frame */
ih264e_thread_pool_activate(ps_codec);
/* main thread */
ih264e_process_thread(&ps_codec->as_process[0]);
/* sync all threads */
ih264e_thread_pool_sync(ps_codec);
}
else
{
for (i = 0; i < num_thread_cnt; i++)
{
printf("pthread Create Failed");
assert(0);
ret = ithread_create(ps_codec->apv_proc_thread_handle[i],
NULL,
(void *)ih264e_process_thread,
&ps_codec->as_process[i + 1]);
if (ret != 0)
{
printf("pthread Create Failed");
assert(0);
}
ps_codec->ai4_process_thread_created[i] = 1;
ps_codec->i4_proc_thread_cnt++;
}
ps_codec->ai4_process_thread_created[i] = 1;
/* launch job */
ih264e_process_thread(ps_proc);
ps_codec->i4_proc_thread_cnt++;
/* Join threads at the end of encoding a frame */
ih264e_join_threads(ps_codec);
}
/* launch job */
ih264e_process_thread(ps_proc);
/* Join threads at the end of encoding a frame */
ih264e_join_threads(ps_codec);
#endif /* KEEP_THREADS_ACTIVE */
ih264_list_reset(ps_codec->pv_proc_jobq);
ih264_list_reset(ps_codec->pv_entropy_jobq);
@ -1267,12 +1266,10 @@ WORD32 ih264e_encode(iv_obj_t *ps_codec_obj, void *pv_api_ip, void *pv_api_op)
ps_video_encode_op->s_ive_op.s_out_buf = s_out_buf.s_bits_buf;
#ifdef KEEP_THREADS_ACTIVE
if(ps_video_encode_op->s_ive_op.u4_is_last)
if (ps_codec->s_cfg.u4_keep_threads_active && ps_video_encode_op->s_ive_op.u4_is_last)
{
ih264e_thread_pool_shutdown(ps_codec);
}
#endif
return IV_SUCCESS;
}

View file

@ -41,7 +41,6 @@
/*****************************************************************************/
/* Function Declarations */
/*****************************************************************************/
#ifdef KEEP_THREADS_ACTIVE
WORD32 ih264e_thread_pool_init(codec_t *ps_codec);
@ -53,12 +52,8 @@ WORD32 ih264e_thread_pool_activate(codec_t *ps_codec);
WORD32 ih264e_thread_pool_sync(codec_t *ps_codec);
#else
void ih264e_join_threads(codec_t *ps_codec);
#endif /* KEEP_THREADS_ACTIVE */
void ih264e_compute_quality_stats(process_ctxt_t *ps_proc);
IH264E_ERROR_T ih264e_wait_for_thread(UWORD32 sleep_us);

View file

@ -582,6 +582,9 @@ typedef struct
/** SEI structure */
sei_params_t s_sei;
/** Enabling thread pool */
UWORD32 u4_keep_threads_active;
}cfg_params_t;
@ -1155,7 +1158,6 @@ typedef struct
} entropy_ctxt_t;
#ifdef KEEP_THREADS_ACTIVE
/**
******************************************************************************
* @brief The thread_pool_t structure manages a pool of worker threads,
@ -1201,7 +1203,6 @@ typedef struct
WORD32 i4_working_threads;
} thread_pool_t;
#endif /* KEEP_THREADS_ACTIVE */
/**
******************************************************************************
@ -2462,12 +2463,10 @@ struct _codec_t
*/
void *pv_out_buf_mgr_base;
#ifdef KEEP_THREADS_ACTIVE
/**
* Thread pool
*/
thread_pool_t s_thread_pool;
#endif /* KEEP_THREADS_ACTIVE */
/**
* Buffer manager for output buffers
@ -2555,12 +2554,10 @@ struct _codec_t
*/
process_ctxt_t as_process[MAX_PROCESS_CTXT];
#ifndef KEEP_THREADS_ACTIVE
/**
* Thread handle for each of the processing threads
*/
void *apv_proc_thread_handle[MAX_PROCESS_THREADS];
#endif
/**
* Structure for global PSNR

View file

@ -335,6 +335,9 @@ typedef struct {
/** Maximum search range to be used in Y direction */
UWORD32 u4_max_srch_rng_y;
/** Enabling thread pool */
UWORD32 u4_keep_threads_active;
}iv_fill_mem_rec_ip_t;

View file

@ -325,11 +325,14 @@ typedef struct
/** Slice parameter */
UWORD32 u4_slice_param;
/** Processor architecture */
IV_ARCH_T e_arch;
/** Processor architecture */
IV_ARCH_T e_arch;
/** SOC details */
IV_SOC_T e_soc;
/** SOC details */
IV_SOC_T e_soc;
/** Enabling thread pool */
UWORD32 u4_keep_threads_active;
}ive_init_ip_t;

View file

@ -90,7 +90,3 @@ add_library(libavcenc STATIC ${LIBAVC_COMMON_SRCS} ${LIBAVC_COMMON_ASMS}
${LIBAVCENC_SRCS} ${LIBAVCENC_ASMS})
target_compile_definitions(libavcenc PRIVATE N_MB_ENABLE)
if(KEEP_THREADS_ACTIVE)
target_compile_definitions(libavcenc PRIVATE KEEP_THREADS_ACTIVE)
endif()

View file

@ -384,6 +384,8 @@ typedef struct
ih264e_ctl_set_sei_ave_params_ip_t s_sei_ave_params;
ih264e_ctl_set_sei_sii_params_ip_t s_sei_sii_params;
UWORD32 u4_keep_threads_active;
} app_ctxt_t;

View file

@ -44,4 +44,5 @@
--bframes 0
--adaptive_intra_refresh 0
--air_refresh_period 30
--keep_threads_active 0

View file

@ -134,6 +134,7 @@ typedef enum
MB_INFO_TYPE,
PIC_INFO_FILE,
PIC_INFO_TYPE,
KEEP_THREADS_ACTIVE,
} ARGUMENT_T;
/*****************************************************************************/
@ -234,6 +235,7 @@ static const argument_t argument_mapping[] =
{ "--", "--mb_info_type", MB_INFO_TYPE, "MB info type\n"},
{ "--", "--pic_info_file", PIC_INFO_FILE, "Pic info file\n"},
{ "--", "--pic_info_type", PIC_INFO_TYPE, "Pic info type\n"},
{ "--", "--keep_threads_active", KEEP_THREADS_ACTIVE, "keep threads active\n"},
};
@ -777,6 +779,10 @@ void parse_argument(app_ctxt_t *ps_app_ctxt, CHAR *argument, CHAR *value)
sscanf(value, "%d", &ps_app_ctxt->u4_enable_intra_4x4);
break;
case KEEP_THREADS_ACTIVE:
sscanf(value, "%d", &ps_app_ctxt->u4_keep_threads_active);
break;
case INVALID:
default:
printf("Ignoring argument : %s\n", argument);
@ -2728,6 +2734,7 @@ int main(int argc, char *argv[])
s_fill_mem_rec_ip.s_ive_ip.e_color_format = DEFAULT_INP_COLOR_FMT;
s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
s_fill_mem_rec_ip.s_ive_ip.u4_keep_threads_active = s_app_ctxt.u4_keep_threads_active;
s_fill_mem_rec_op.s_ive_op.u4_size = sizeof(ih264e_fill_mem_rec_op_t);
@ -2810,6 +2817,7 @@ int main(int argc, char *argv[])
s_init_ip.s_ive_ip.u4_slice_param = s_app_ctxt.u4_slice_param;
s_init_ip.s_ive_ip.e_arch = s_app_ctxt.e_arch;
s_init_ip.s_ive_ip.e_soc = s_app_ctxt.e_soc;
s_init_ip.s_ive_ip.u4_keep_threads_active = s_app_ctxt.u4_keep_threads_active;
s_init_op.s_ive_op.u4_size = sizeof(ih264e_init_op_t);

View file

@ -175,6 +175,7 @@ class Codec {
uint32_t mForceIdrInterval = 0; // in number of frames
uint32_t mDynamicBitRateInterval = 0; // in number of frames
uint32_t mDynamicFrameRateInterval = 0; // in number of frames
uint32_t mKeepThreadsActive;
uint64_t mBitrate = 6000000;
float mFrameRate = 30;
iv_obj_t *mCodecCtx = nullptr;
@ -234,6 +235,7 @@ bool Codec::initEncoder(const uint8_t **pdata, size_t *psize) {
mForceIdrInterval = data[IDX_FORCE_IDR_INTERVAL] & 0x07;
mDynamicBitRateInterval = data[IDX_DYNAMIC_BITRATE_INTERVAL] & 0x07;
mDynamicFrameRateInterval = data[IDX_DYNAMIC_FRAME_RATE_INTERVAL] & 0x07;
mKeepThreadsActive = 0;
/* Getting Number of MemRecords */
iv_num_mem_rec_ip_t sNumMemRecIp{};
@ -280,6 +282,7 @@ bool Codec::initEncoder(const uint8_t **pdata, size_t *psize) {
sFillMemRecIp.u4_max_reorder_cnt = 0;
sFillMemRecIp.u4_max_srch_rng_x = 256;
sFillMemRecIp.u4_max_srch_rng_y = 256;
sFillMemRecIp.u4_keep_threads_active = mKeepThreadsActive;
if (IV_SUCCESS != ive_api_function(nullptr, &sFillMemRecIp, &sFillMemRecOp)) {
return false;
@ -327,6 +330,7 @@ bool Codec::initEncoder(const uint8_t **pdata, size_t *psize) {
sInitIp.u4_slice_param = mSliceParam;
sInitIp.e_arch = mArch;
sInitIp.e_soc = SOC_GENERIC;
sInitIp.u4_keep_threads_active = mKeepThreadsActive;
if (IV_SUCCESS != ive_api_function(mCodecCtx, &sInitIp, &sInitOp)) {
return false;