Skip to content

Commit 655d277

Browse files
mss-parkkpchoi
andauthored
Modify default threads (#59)
* modification of default number of threads in decoder Signed-off-by: Minsoo Park <mss.park@samsung.com> * modification Signed-off-by: Minsoo Park <mss.park@samsung.com> * refactoring for parameter parser Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * normalize LF Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * fixed oapv_get_num_cpu_cores function for android Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> --------- Signed-off-by: Minsoo Park <mss.park@samsung.com> Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> Co-authored-by: kp5.choi@samsung.com <kp5.choi@samsung.com>
1 parent 15a873d commit 655d277

File tree

7 files changed

+44
-40
lines changed

7 files changed

+44
-40
lines changed

app/oapv_app_dec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static args_var_t *args_init_vars(args_parser_t *args)
120120
args_set_variable_by_key_long(opts, "verbose", &op_verbose);
121121
op_verbose = VERBOSE_SIMPLE; /* default */
122122
args_set_variable_by_key_long(opts, "threads", &vars->threads);
123-
vars->threads = 1; /* default */
123+
vars->threads = OAPV_CDESC_THREADS_AUTO; /* default */
124124
args_set_variable_by_key_long(opts, "output-depth", &vars->output_depth);
125125
args_set_variable_by_key_long(opts, "output-csp", &vars->output_csp);
126126
vars->output_csp = 0; /* default: coded CSP */

app/oapv_app_enc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static args_var_t *args_init_vars(args_parser_t *args, oapve_param_t *param)
275275
args_set_variable_by_key_long(opts, "q-matrix-c3", vars->q_matrix_c3);
276276

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

280280
args_set_variable_by_key_long(opts, "tile-w", vars->tile_w);
281281
args_set_variable_by_key_long(opts, "tile-h", vars->tile_h);

inc/oapv.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ static const oapv_dict_str_int_t oapv_param_opts_color_matrix[] = {
469469
/*****************************************************************************
470470
* coding parameters
471471
*****************************************************************************/
472+
#define OAPV_LEVEL_TO_LEVEL_IDC(level) (int)(((level) * 30.0) + 0.5)
473+
472474
typedef struct oapve_param oapve_param_t;
473475
struct oapve_param {
474476
/* profile_idc defined in spec. */
@@ -522,11 +524,10 @@ struct oapve_param {
522524
int full_range_flag;
523525
};
524526

527+
#define OAPV_CDESC_THREADS_AUTO 0
525528
/*****************************************************************************
526529
* description for encoder creation
527530
*****************************************************************************/
528-
#define OAPVE_CDESC_THREADS_AUTO 0
529-
530531
typedef struct oapve_cdesc oapve_cdesc_t;
531532
struct oapve_cdesc {
532533
int max_bs_buf_size; // max bitstream buffer size

src/oapv.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,9 @@ static int enc_ready(oapve_ctx_t *ctx)
754754
min_num_tiles = oapv_min(min_num_tiles, num_tiles);
755755
}
756756

757-
if(ctx->cdesc.threads == OAPVE_CDESC_THREADS_AUTO) {
758-
ctx->threads = oapv_min(OAPV_MAX_THREADS, oapv_min(oapv_get_num_cpu_cores(), min_num_tiles));
757+
if(ctx->cdesc.threads == OAPV_CDESC_THREADS_AUTO) {
758+
int num_cores = oapv_get_num_cpu_cores();
759+
ctx->threads = oapv_min(OAPV_MAX_THREADS, oapv_min(num_cores, min_num_tiles));
759760
}
760761
else {
761762
ctx->threads = ctx->cdesc.threads;
@@ -1820,11 +1821,11 @@ static int dec_thread_tile(void *arg)
18201821

18211822
static void dec_flush(oapvd_ctx_t *ctx)
18221823
{
1823-
if(ctx->cdesc.threads >= 2) {
1824+
if(ctx->threads >= 2) {
18241825
if(ctx->tpool) {
18251826
// thread controller instance is present
18261827
// terminate the created thread
1827-
for(int i = 0; i < ctx->cdesc.threads - 1; i++) {
1828+
for(int i = 0; i < ctx->threads - 1; i++) {
18281829
if(ctx->thread_id[i]) {
18291830
// valid thread instance
18301831
ctx->tpool->release(&ctx->thread_id[i]);
@@ -1839,7 +1840,7 @@ static void dec_flush(oapvd_ctx_t *ctx)
18391840

18401841
oapv_tpool_sync_obj_delete(&(ctx->sync_obj));
18411842

1842-
for(int i = 0; i < ctx->cdesc.threads; i++) {
1843+
for(int i = 0; i < ctx->threads; i++) {
18431844
dec_core_free(ctx->core[i]);
18441845
}
18451846
}
@@ -1848,28 +1849,37 @@ static int dec_ready(oapvd_ctx_t *ctx)
18481849
{
18491850
int i, ret = OAPV_OK;
18501851

1852+
if (ctx->cdesc.threads == OAPV_CDESC_THREADS_AUTO) {
1853+
int num_cores = oapv_get_num_cpu_cores();
1854+
ctx->threads = oapv_min(OAPV_MAX_THREADS, num_cores);
1855+
}
1856+
else {
1857+
ctx->threads = ctx->cdesc.threads;
1858+
}
1859+
oapv_assert_gv(ctx->threads > 0 && ctx->threads <= OAPV_MAX_THREADS, ret, OAPV_ERR_INVALID_ARGUMENT, ERR);
1860+
18511861
if(ctx->core[0] == NULL) {
18521862
// create cores
1853-
for(i = 0; i < ctx->cdesc.threads; i++) {
1863+
for(i = 0; i < ctx->threads; i++) {
18541864
ctx->core[i] = dec_core_alloc();
18551865
oapv_assert_gv(ctx->core[i], ret, OAPV_ERR_OUT_OF_MEMORY, ERR);
18561866
ctx->core[i]->ctx = ctx;
18571867
}
18581868
}
18591869

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

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

1869-
if(ctx->cdesc.threads >= 2) {
1879+
if(ctx->threads >= 2) {
18701880
ctx->tpool = oapv_malloc(sizeof(oapv_tpool_t));
1871-
oapv_tpool_init(ctx->tpool, ctx->cdesc.threads - 1);
1872-
for(i = 0; i < ctx->cdesc.threads - 1; i++) {
1881+
oapv_tpool_init(ctx->tpool, ctx->threads - 1);
1882+
for(i = 0; i < ctx->threads - 1; i++) {
18731883
ctx->thread_id[i] = ctx->tpool->create(ctx->tpool, i);
18741884
oapv_assert_gv(ctx->thread_id[i] != NULL, ret, OAPV_ERR_UNKNOWN, ERR);
18751885
}
@@ -1919,7 +1929,7 @@ oapvd_t oapvd_create(oapvd_cdesc_t *cdesc, int *err)
19191929
ctx = NULL;
19201930

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

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

2011-
parallel_task = (ctx->cdesc.threads > ctx->num_tiles) ? ctx->num_tiles : ctx->cdesc.threads;
2021+
parallel_task = (ctx->threads > ctx->num_tiles) ? ctx->num_tiles : ctx->threads;
20122022

20132023
/* decode tiles ************************************/
20142024
for(tidx = 0; tidx < (parallel_task - 1); tidx++) {

src/oapv_def.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ typedef struct oapvd_ctx oapvd_ctx_t;
347347

348348
struct oapvd_core {
349349
ALIGNED_16(s16 coef[OAPV_MB_D]);
350-
oapvd_ctx_t *ctx;
350+
s16 q_mat[N_C][OAPV_BLK_D];
351351

352352
int prev_dc_ctx[N_C];
353353
int prev_1st_ac_ctx[N_C];
@@ -356,10 +356,9 @@ struct oapvd_core {
356356
/* and coded as abs_dc_coeff_diff and sign_dc_coeff_diff */
357357
int qp[N_C];
358358
int dq_shift[N_C];
359-
s16 q_mat[N_C][OAPV_BLK_D];
360-
361359
int tile_idx;
362360

361+
oapvd_ctx_t *ctx;
363362
/* platform specific data, if needed */
364363
void *pf;
365364
};
@@ -385,6 +384,7 @@ struct oapvd_ctx {
385384
int num_tile_rows;
386385
int w;
387386
int h;
387+
int threads;
388388
oapv_tpool_t *tpool;
389389
oapv_thread_t thread_id[OAPV_MAX_THREADS];
390390
oapv_sync_obj_t sync_obj;

src/oapv_param.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
#include "oapv_def.h"
3333

34-
#define LEVEL_TO_LEVEL_IDC(level) (int)(((level) * 30.0) + 0.5)
35-
3634
int oapve_param_default(oapve_param_t *param)
3735
{
3836
oapv_mset(param, 0, sizeof(oapve_param_t));
@@ -47,7 +45,7 @@ int oapve_param_default(oapve_param_t *param)
4745
param->tile_h = 16 * OAPV_MB_H; // default: 256
4846

4947
param->profile_idc = OAPV_PROFILE_422_10;
50-
param->level_idc = LEVEL_TO_LEVEL_IDC(4.1);
48+
param->level_idc = OAPV_LEVEL_TO_LEVEL_IDC(4.1);
5149
param->band_idc = 2;
5250

5351
param->use_q_matrix = 0;
@@ -103,6 +101,10 @@ static int kbps_str_to_int(const char *str)
103101
char *tmp = strtok(s, "Mm ");
104102
kbps = (int)(atof(tmp) * 1000);
105103
}
104+
else if(strchr(s, 'G') || strchr(s, 'g')) {
105+
char *tmp = strtok(s, "Gg ");
106+
kbps = (int)(atof(tmp) * 1000000);
107+
}
106108
else {
107109
kbps = atoi(s);
108110
}
@@ -127,7 +129,6 @@ static int get_q_matrix(const char *str, u8 q_matrix[OAPV_BLK_D])
127129
return 0;
128130
}
129131

130-
131132
#define NAME_CMP(VAL) else if(strcmp(name, VAL)== 0)
132133
#define GET_INTEGER_OR_ERR(STR, F) { \
133134
char * left; (F) = strtol(STR, &left, 10); \
@@ -181,7 +182,7 @@ int oapve_param_parse(oapve_param_t *param, const char *name, const char *value
181182
tf0 == 3.0f || tf0 == 3.1f || tf0 == 4.0f || tf0 == 4.1f ||\
182183
tf0 == 5.0f || tf0 == 5.1f || tf0 == 6.0f || tf0 == 6.1f ||\
183184
tf0 == 7.0f || tf0 == 7.1f) {
184-
param->level_idc = LEVEL_TO_LEVEL_IDC(tf0);
185+
param->level_idc = OAPV_LEVEL_TO_LEVEL_IDC(tf0);
185186
}
186187
else {
187188
return OAPV_ERR_INVALID_ARGUMENT;

src/oapv_port.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
* POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
#if defined(LINUX)
33-
#ifndef _GNU_SOURCE
34-
#define _GNU_SOURCE // for sched_getaffinity()
35-
#endif
36-
#endif
37-
3832
#include <stdarg.h>
3933
#include "oapv_port.h"
4034

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

98-
#if defined(WIN32) || defined(WIN64)
92+
#if defined(WIN32) || defined(WIN64) || defined(_WIN32)
9993
#include <windows.h>
10094
#include <sysinfoapi.h>
101-
#elif defined(LINUX)
102-
#include <sched.h>
103-
#elif defined(MACOS)
95+
#else /* LINUX, MACOS, Android */
10496
#include <unistd.h>
10597
#endif
10698

10799
int oapv_get_num_cpu_cores(void)
108100
{
109101
int num_cores = 1; // default
110-
#if defined(WIN32) || defined(WIN64)
102+
#if defined(WIN32) || defined(WIN64) || defined(_WIN32)
111103
{
112104
SYSTEM_INFO si;
113105
GetNativeSystemInfo(&si);
114106
num_cores = si.dwNumberOfProcessors;
115107
}
116-
#elif defined(LINUX)
108+
#elif defined(_SC_NPROCESSORS_ONLN)
109+
{
110+
num_cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
111+
}
112+
#elif defined(CPU_COUNT)
117113
{
118114
cpu_set_t cset;
119115
memset(&cset, 0, sizeof(cset));
120116
if(!sched_getaffinity(0, sizeof(cset), &cset)) {
121117
num_cores = CPU_COUNT(&cset);
122118
}
123119
}
124-
#elif defined(MACOS)
125-
{
126-
num_cores = sysconf(_SC_NPROCESSORS_ONLN);
127-
}
128120
#endif
129121
return num_cores;
130122
}

0 commit comments

Comments
 (0)