summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2020-04-29 11:50:17 +0200
committerEven Rouault <even.rouault@spatialys.com>2020-05-20 20:30:21 +0200
commit97eb7e0bf17b476d516262e0af462ec7eeb8f505 (patch)
tree3006779d0ae78b84909c6f52f52bb9a254a83160 /src/bin
parent1d358f25c8eabbc7c274bcc148f4f5d594ec13fe (diff)
Add multithreading support in the T1 (entropy phase) encoder
- API wise, opj_codec_set_threads() can be used on the encoding side - opj_compress has a -threads switch similar to opj_uncompress
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/jp2/opj_compress.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c
index cbc30fba..68274840 100644
--- a/src/bin/jp2/opj_compress.c
+++ b/src/bin/jp2/opj_compress.c
@@ -301,6 +301,10 @@ static void encode_help_display(void)
fprintf(stdout, " Currently supports only RPCL order.\n");
fprintf(stdout, "-C <comment>\n");
fprintf(stdout, " Add <comment> in the comment marker segment.\n");
+ if (opj_has_thread_support()) {
+ fprintf(stdout, " -threads <num_threads|ALL_CPUS>\n"
+ " Number of threads to use for encoding or ALL_CPUS for all available cores.\n");
+ }
/* UniPG>> */
#ifdef USE_JPWL
fprintf(stdout, "-W <params>\n");
@@ -579,7 +583,8 @@ static int parse_cmdline_encoder(int argc, char **argv,
img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename,
size_t indexfilename_size,
int* pOutFramerate,
- OPJ_BOOL* pOutPLT)
+ OPJ_BOOL* pOutPLT,
+ int* pOutNumThreads)
{
OPJ_UINT32 i, j;
int totlen, c;
@@ -596,7 +601,8 @@ static int parse_cmdline_encoder(int argc, char **argv,
{"jpip", NO_ARG, NULL, 'J'},
{"mct", REQ_ARG, NULL, 'Y'},
{"IMF", REQ_ARG, NULL, 'Z'},
- {"PLT", NO_ARG, NULL, 'A'}
+ {"PLT", NO_ARG, NULL, 'A'},
+ {"threads", REQ_ARG, NULL, 'B'}
};
/* parse the command line */
@@ -1679,6 +1685,19 @@ static int parse_cmdline_encoder(int argc, char **argv,
}
break;
+ /* ----------------------------------------------------- */
+ case 'B': { /* Number of threads */
+ if (strcmp(opj_optarg, "ALL_CPUS") == 0) {
+ *pOutNumThreads = opj_get_num_cpus();
+ if (*pOutNumThreads == 1) {
+ *pOutNumThreads = 0;
+ }
+ } else {
+ sscanf(opj_optarg, "%d", pOutNumThreads);
+ }
+ }
+ break;
+
/* ------------------------------------------------------ */
@@ -1860,6 +1879,7 @@ int main(int argc, char **argv)
OPJ_FLOAT64 t = opj_clock();
OPJ_BOOL PLT = OPJ_FALSE;
+ int num_threads = 0;
/* set encoding parameters to default values */
opj_set_default_encoder_parameters(&parameters);
@@ -1880,7 +1900,7 @@ int main(int argc, char **argv)
parameters.tcp_mct = (char)
255; /* This will be set later according to the input image or the provided option */
if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp,
- indexfilename, sizeof(indexfilename), &framerate, &PLT) == 1) {
+ indexfilename, sizeof(indexfilename), &framerate, &PLT, &num_threads) == 1) {
ret = 1;
goto fin;
}
@@ -2141,6 +2161,15 @@ int main(int argc, char **argv)
}
}
+ if (num_threads >= 1 &&
+ !opj_codec_set_threads(l_codec, num_threads)) {
+ fprintf(stderr, "failed to set number of threads\n");
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(image);
+ ret = 1;
+ goto fin;
+ }
+
/* open a byte stream for writing and allocate memory for all tiles */
l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
if (! l_stream) {