diff --git a/include/OpenMPUtilities.h b/include/OpenMPUtilities.h
index 9810d636..5b5107c1 100644
--- a/include/OpenMPUtilities.h
+++ b/include/OpenMPUtilities.h
@@ -41,5 +41,12 @@
#define OPEN_MP_NUM_PROCESSORS (std::min(omp_get_num_procs(), std::max(2, openshot::Settings::Instance()->OMP_THREADS) ))
#define FF_NUM_PROCESSORS (std::min(omp_get_num_procs(), std::max(2, openshot::Settings::Instance()->FF_THREADS) ))
+// Set max-active-levels to the max supported, if possible
+// (supported_active_levels is OpenMP 5.0 (November 2018) or later, only.)
+#if (_OPENMP >= 201811)
+ #define OPEN_MP_MAX_ACTIVE openmp_get_supported_active_levels()
+#else
+ #define OPEN_MP_MAX_ACTIVE OPEN_MP_NUM_PROCESSORS
+#endif
#endif
diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp
index 12b9776a..fea4f60b 100644
--- a/src/FFmpegReader.cpp
+++ b/src/FFmpegReader.cpp
@@ -91,6 +91,11 @@ FFmpegReader::FFmpegReader(const std::string& path, bool inspect_reader)
current_video_frame(0), has_missing_frames(false), num_packets_since_video_frame(0), num_checks_since_final(0),
packet(NULL) {
+ // Configure OpenMP parallelism
+ // Default number of threads per section
+ omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
+ // Allow nested parallel sections as deeply as supported
+ omp_set_max_active_levels(OPEN_MP_MAX_ACTIVE);
// Initialize FFMpeg, and register all formats and codecs
AV_REGISTER_ALL
@@ -877,11 +882,6 @@ std::shared_ptr FFmpegReader::ReadStream(int64_t requested_frame) {
int minimum_packets = OPEN_MP_NUM_PROCESSORS;
int max_packets = 4096;
- // Set the number of threads in OpenMP
- omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
- // Allow nested OpenMP sections
- omp_set_nested(true);
-
// Debug output
ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::ReadStream", "requested_frame", requested_frame, "OPEN_MP_NUM_PROCESSORS", OPEN_MP_NUM_PROCESSORS);
diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp
index eff2507e..39a3768f 100644
--- a/src/FFmpegWriter.cpp
+++ b/src/FFmpegWriter.cpp
@@ -95,6 +95,12 @@ FFmpegWriter::FFmpegWriter(const std::string& path) :
info.has_audio = false;
info.has_video = false;
+ // Configure OpenMP parallelism
+ // Default number of threads per block
+ omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
+ // Allow nested parallel sections as deeply as supported
+ omp_set_max_active_levels(OPEN_MP_MAX_ACTIVE);
+
// Initialize FFMpeg, and register all formats and codecs
AV_REGISTER_ALL
@@ -714,11 +720,6 @@ void FFmpegWriter::write_queued_frames() {
spooled_video_frames.clear();
spooled_audio_frames.clear();
- // Set the number of threads in OpenMP
- omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
- // Allow nested OpenMP sections
- omp_set_nested(true);
-
// Create blank exception
bool has_error_encoding_video = false;
diff --git a/src/Timeline.cpp b/src/Timeline.cpp
index 124058ac..89f0992c 100644
--- a/src/Timeline.cpp
+++ b/src/Timeline.cpp
@@ -67,7 +67,13 @@ Timeline::Timeline(int width, int height, Fraction fps, int sample_rate, int cha
info.acodec = "openshot::timeline";
info.vcodec = "openshot::timeline";
- // Init max image size
+ // Configure OpenMP parallelism
+ // Default number of threads per block
+ omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
+ // Allow nested parallel sections as deeply as supported
+ omp_set_max_active_levels(OPEN_MP_MAX_ACTIVE);
+
+ // Init max image size
SetMaxSize(info.width, info.height);
// Init cache
@@ -194,6 +200,12 @@ Timeline::Timeline(std::string projectPath, bool convert_absolute_paths) :
info.has_video = true;
info.has_audio = true;
+ // Configure OpenMP parallelism
+ // Default number of threads per section
+ omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
+ // Allow nested parallel sections as deeply as supported
+ omp_set_max_active_levels(OPEN_MP_MAX_ACTIVE);
+
// Init max image size
SetMaxSize(info.width, info.height);
@@ -892,10 +904,6 @@ std::shared_ptr Timeline::GetFrame(int64_t requested_frame)
#pragma omp critical (T_GetFrame)
nearby_clips = find_intersecting_clips(requested_frame, minimum_frames, true);
- omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
- // Allow nested OpenMP sections
- omp_set_nested(true);
-
// Debug output
ZmqLogger::Instance()->AppendDebugMethod("Timeline::GetFrame", "requested_frame", requested_frame, "minimum_frames", minimum_frames, "OPEN_MP_NUM_PROCESSORS", OPEN_MP_NUM_PROCESSORS);