Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/oapv_app_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static args_var_t *args_init_vars(args_parser_t *args)
args_set_variable_by_key_long(opts, "verbose", &op_verbose);
op_verbose = VERBOSE_SIMPLE; /* default */
args_set_variable_by_key_long(opts, "threads", &vars->threads);
vars->threads = 1; /* default */
vars->threads = OAPV_CDESC_THREADS_AUTO; /* default */
args_set_variable_by_key_long(opts, "output-depth", &vars->output_depth);
args_set_variable_by_key_long(opts, "output-csp", &vars->output_csp);
vars->output_csp = 0; /* default: coded CSP */
Expand Down
2 changes: 1 addition & 1 deletion app/oapv_app_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static args_var_t *args_init_vars(args_parser_t *args, oapve_param_t *param)
args_set_variable_by_key_long(opts, "q-matrix-c3", vars->q_matrix_c3);

args_set_variable_by_key_long(opts, "threads", &vars->threads);
vars->threads = OAPVE_CDESC_THREADS_AUTO; /* default */
vars->threads = OAPV_CDESC_THREADS_AUTO; /* default */

args_set_variable_by_key_long(opts, "tile-w", vars->tile_w);
args_set_variable_by_key_long(opts, "tile-h", vars->tile_h);
Expand Down
5 changes: 3 additions & 2 deletions inc/oapv.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ static const oapv_dict_str_int_t oapv_param_opts_color_matrix[] = {
/*****************************************************************************
* coding parameters
*****************************************************************************/
#define OAPV_LEVEL_TO_LEVEL_IDC(level) (int)(((level) * 30.0) + 0.5)

typedef struct oapve_param oapve_param_t;
struct oapve_param {
/* profile_idc defined in spec. */
Expand Down Expand Up @@ -522,11 +524,10 @@ struct oapve_param {
int full_range_flag;
};

#define OAPV_CDESC_THREADS_AUTO 0
/*****************************************************************************
* description for encoder creation
*****************************************************************************/
#define OAPVE_CDESC_THREADS_AUTO 0

typedef struct oapve_cdesc oapve_cdesc_t;
struct oapve_cdesc {
int max_bs_buf_size; // max bitstream buffer size
Expand Down
34 changes: 22 additions & 12 deletions src/oapv.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,9 @@ static int enc_ready(oapve_ctx_t *ctx)
min_num_tiles = oapv_min(min_num_tiles, num_tiles);
}

if(ctx->cdesc.threads == OAPVE_CDESC_THREADS_AUTO) {
ctx->threads = oapv_min(OAPV_MAX_THREADS, oapv_min(oapv_get_num_cpu_cores(), min_num_tiles));
if(ctx->cdesc.threads == OAPV_CDESC_THREADS_AUTO) {
int num_cores = oapv_get_num_cpu_cores();
ctx->threads = oapv_min(OAPV_MAX_THREADS, oapv_min(num_cores, min_num_tiles));
}
else {
ctx->threads = ctx->cdesc.threads;
Expand Down Expand Up @@ -1820,11 +1821,11 @@ static int dec_thread_tile(void *arg)

static void dec_flush(oapvd_ctx_t *ctx)
{
if(ctx->cdesc.threads >= 2) {
if(ctx->threads >= 2) {
if(ctx->tpool) {
// thread controller instance is present
// terminate the created thread
for(int i = 0; i < ctx->cdesc.threads - 1; i++) {
for(int i = 0; i < ctx->threads - 1; i++) {
if(ctx->thread_id[i]) {
// valid thread instance
ctx->tpool->release(&ctx->thread_id[i]);
Expand All @@ -1839,7 +1840,7 @@ static void dec_flush(oapvd_ctx_t *ctx)

oapv_tpool_sync_obj_delete(&(ctx->sync_obj));

for(int i = 0; i < ctx->cdesc.threads; i++) {
for(int i = 0; i < ctx->threads; i++) {
dec_core_free(ctx->core[i]);
}
}
Expand All @@ -1848,28 +1849,37 @@ static int dec_ready(oapvd_ctx_t *ctx)
{
int i, ret = OAPV_OK;

if (ctx->cdesc.threads == OAPV_CDESC_THREADS_AUTO) {
int num_cores = oapv_get_num_cpu_cores();
ctx->threads = oapv_min(OAPV_MAX_THREADS, num_cores);
}
else {
ctx->threads = ctx->cdesc.threads;
}
oapv_assert_gv(ctx->threads > 0 && ctx->threads <= OAPV_MAX_THREADS, ret, OAPV_ERR_INVALID_ARGUMENT, ERR);

if(ctx->core[0] == NULL) {
// create cores
for(i = 0; i < ctx->cdesc.threads; i++) {
for(i = 0; i < ctx->threads; i++) {
ctx->core[i] = dec_core_alloc();
oapv_assert_gv(ctx->core[i], ret, OAPV_ERR_OUT_OF_MEMORY, ERR);
ctx->core[i]->ctx = ctx;
}
}

// initialize the threads to NULL
for(i = 0; i < OAPV_MAX_THREADS; i++) {
for(i = 0; i < ctx->threads; i++) {
ctx->thread_id[i] = 0;
}

// get the context synchronization handle
ctx->sync_obj = oapv_tpool_sync_obj_create();
oapv_assert_gv(ctx->sync_obj != NULL, ret, OAPV_ERR_UNKNOWN, ERR);

if(ctx->cdesc.threads >= 2) {
if(ctx->threads >= 2) {
ctx->tpool = oapv_malloc(sizeof(oapv_tpool_t));
oapv_tpool_init(ctx->tpool, ctx->cdesc.threads - 1);
for(i = 0; i < ctx->cdesc.threads - 1; i++) {
oapv_tpool_init(ctx->tpool, ctx->threads - 1);
for(i = 0; i < ctx->threads - 1; i++) {
ctx->thread_id[i] = ctx->tpool->create(ctx->tpool, i);
oapv_assert_gv(ctx->thread_id[i] != NULL, ret, OAPV_ERR_UNKNOWN, ERR);
}
Expand Down Expand Up @@ -1919,7 +1929,7 @@ oapvd_t oapvd_create(oapvd_cdesc_t *cdesc, int *err)
ctx = NULL;

/* check if any decoder argument is correctly set */
oapv_assert_gv(cdesc->threads > 0 && cdesc->threads <= OAPV_MAX_THREADS, ret, OAPV_ERR_INVALID_ARGUMENT, ERR);
oapv_assert_gv((cdesc->threads > 0 && cdesc->threads <= OAPV_MAX_THREADS) || cdesc->threads == OAPV_CDESC_THREADS_AUTO , ret, OAPV_ERR_INVALID_ARGUMENT, ERR);

/* memory allocation for ctx and core structure */
ctx = (oapvd_ctx_t *)dec_ctx_alloc();
Expand Down Expand Up @@ -2008,7 +2018,7 @@ int oapvd_decode(oapvd_t did, oapv_bitb_t *bitb, oapv_frms_t *ofrms, oapvm_t mid
int parallel_task = 1;
int tidx = 0;

parallel_task = (ctx->cdesc.threads > ctx->num_tiles) ? ctx->num_tiles : ctx->cdesc.threads;
parallel_task = (ctx->threads > ctx->num_tiles) ? ctx->num_tiles : ctx->threads;

/* decode tiles ************************************/
for(tidx = 0; tidx < (parallel_task - 1); tidx++) {
Expand Down
6 changes: 3 additions & 3 deletions src/oapv_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ typedef struct oapvd_ctx oapvd_ctx_t;

struct oapvd_core {
ALIGNED_16(s16 coef[OAPV_MB_D]);
oapvd_ctx_t *ctx;
s16 q_mat[N_C][OAPV_BLK_D];

int prev_dc_ctx[N_C];
int prev_1st_ac_ctx[N_C];
Expand All @@ -356,10 +356,9 @@ struct oapvd_core {
/* and coded as abs_dc_coeff_diff and sign_dc_coeff_diff */
int qp[N_C];
int dq_shift[N_C];
s16 q_mat[N_C][OAPV_BLK_D];

int tile_idx;

oapvd_ctx_t *ctx;
/* platform specific data, if needed */
void *pf;
};
Expand All @@ -385,6 +384,7 @@ struct oapvd_ctx {
int num_tile_rows;
int w;
int h;
int threads;
oapv_tpool_t *tpool;
oapv_thread_t thread_id[OAPV_MAX_THREADS];
oapv_sync_obj_t sync_obj;
Expand Down
11 changes: 6 additions & 5 deletions src/oapv_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

#include "oapv_def.h"

#define LEVEL_TO_LEVEL_IDC(level) (int)(((level) * 30.0) + 0.5)

int oapve_param_default(oapve_param_t *param)
{
oapv_mset(param, 0, sizeof(oapve_param_t));
Expand All @@ -47,7 +45,7 @@ int oapve_param_default(oapve_param_t *param)
param->tile_h = 16 * OAPV_MB_H; // default: 256

param->profile_idc = OAPV_PROFILE_422_10;
param->level_idc = LEVEL_TO_LEVEL_IDC(4.1);
param->level_idc = OAPV_LEVEL_TO_LEVEL_IDC(4.1);
param->band_idc = 2;

param->use_q_matrix = 0;
Expand Down Expand Up @@ -103,6 +101,10 @@ static int kbps_str_to_int(const char *str)
char *tmp = strtok(s, "Mm ");
kbps = (int)(atof(tmp) * 1000);
}
else if(strchr(s, 'G') || strchr(s, 'g')) {
char *tmp = strtok(s, "Gg ");
kbps = (int)(atof(tmp) * 1000000);
}
else {
kbps = atoi(s);
}
Expand All @@ -127,7 +129,6 @@ static int get_q_matrix(const char *str, u8 q_matrix[OAPV_BLK_D])
return 0;
}


#define NAME_CMP(VAL) else if(strcmp(name, VAL)== 0)
#define GET_INTEGER_OR_ERR(STR, F) { \
char * left; (F) = strtol(STR, &left, 10); \
Expand Down Expand Up @@ -181,7 +182,7 @@ int oapve_param_parse(oapve_param_t *param, const char *name, const char *value
tf0 == 3.0f || tf0 == 3.1f || tf0 == 4.0f || tf0 == 4.1f ||\
tf0 == 5.0f || tf0 == 5.1f || tf0 == 6.0f || tf0 == 6.1f ||\
tf0 == 7.0f || tf0 == 7.1f) {
param->level_idc = LEVEL_TO_LEVEL_IDC(tf0);
param->level_idc = OAPV_LEVEL_TO_LEVEL_IDC(tf0);
}
else {
return OAPV_ERR_INVALID_ARGUMENT;
Expand Down
24 changes: 8 additions & 16 deletions src/oapv_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#if defined(LINUX)
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // for sched_getaffinity()
#endif
#endif

#include <stdarg.h>
#include "oapv_port.h"

Expand Down Expand Up @@ -95,36 +89,34 @@ void oapv_trace_line(char *pre)
printf("%s\n", str);
}

#if defined(WIN32) || defined(WIN64)
#if defined(WIN32) || defined(WIN64) || defined(_WIN32)
#include <windows.h>
#include <sysinfoapi.h>
#elif defined(LINUX)
#include <sched.h>
#elif defined(MACOS)
#else /* LINUX, MACOS, Android */
#include <unistd.h>
#endif

int oapv_get_num_cpu_cores(void)
{
int num_cores = 1; // default
#if defined(WIN32) || defined(WIN64)
#if defined(WIN32) || defined(WIN64) || defined(_WIN32)
{
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
num_cores = si.dwNumberOfProcessors;
}
#elif defined(LINUX)
#elif defined(_SC_NPROCESSORS_ONLN)
{
num_cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
}
#elif defined(CPU_COUNT)
{
cpu_set_t cset;
memset(&cset, 0, sizeof(cset));
if(!sched_getaffinity(0, sizeof(cset), &cset)) {
num_cores = CPU_COUNT(&cset);
}
}
#elif defined(MACOS)
{
num_cores = sysconf(_SC_NPROCESSORS_ONLN);
}
#endif
return num_cores;
}
Expand Down