diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index abce5733..d66749bb 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -278,7 +278,7 @@ void FFmpegReader::Open() { retry_decode_open = 0; // Set number of threads equal to number of processors (not to exceed 16) - pCodecCtx->thread_count = std::min(FF_NUM_PROCESSORS, 16); + pCodecCtx->thread_count = std::min(FF_VIDEO_NUM_PROCESSORS, 16); if (pCodec == NULL) { throw InvalidCodec("A valid video codec could not be found for this file.", path); @@ -524,8 +524,8 @@ void FFmpegReader::Open() { const AVCodec *aCodec = avcodec_find_decoder(codecId); aCodecCtx = AV_GET_CODEC_CONTEXT(aStream, aCodec); - // Set number of threads equal to number of processors (not to exceed 16) - aCodecCtx->thread_count = std::min(FF_NUM_PROCESSORS, 16); + // Audio encoding does not typically use more than 2 threads (most codecs use 1 thread) + aCodecCtx->thread_count = std::min(FF_AUDIO_NUM_PROCESSORS, 2); if (aCodec == NULL) { throw InvalidCodec("A valid audio codec could not be found for this file.", path); diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index ddfa67bc..2de65257 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -1334,8 +1334,9 @@ void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) { const AVCodec *codec; AV_GET_CODEC_FROM_STREAM(st, audio_codec_ctx) - // Set number of threads equal to number of processors (not to exceed 16) - audio_codec_ctx->thread_count = std::min(FF_NUM_PROCESSORS, 16); + // Audio encoding does not typically use more than 2 threads (most codecs use 1 thread) + audio_codec_ctx->thread_count = std::min(FF_AUDIO_NUM_PROCESSORS, 2); + std::cout << "FFmpegWriter audio thread_count: " << audio_codec_ctx->thread_count << std::endl; // Find the audio encoder codec = avcodec_find_encoder_by_name(info.acodec.c_str()); @@ -1409,8 +1410,8 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) { const AVCodec *codec; AV_GET_CODEC_FROM_STREAM(st, video_codec_ctx) - // Set number of threads equal to number of processors (not to exceed 16) - video_codec_ctx->thread_count = std::min(FF_NUM_PROCESSORS, 16); + // Set number of threads equal to number of processors (not to exceed 16, FFmpeg doesn't recommend more than 16) + video_codec_ctx->thread_count = std::min(FF_VIDEO_NUM_PROCESSORS, 16); #if USE_HW_ACCEL if (hw_en_on && hw_en_supported) { diff --git a/src/OpenMPUtilities.h b/src/OpenMPUtilities.h index 43932542..a50780b4 100644 --- a/src/OpenMPUtilities.h +++ b/src/OpenMPUtilities.h @@ -20,8 +20,9 @@ #include "Settings.h" // Calculate the # of OpenMP Threads to allow -#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) )) +#define OPEN_MP_NUM_PROCESSORS std::min(omp_get_num_procs(), std::max(2, openshot::Settings::Instance()->OMP_THREADS)) +#define FF_VIDEO_NUM_PROCESSORS std::min(omp_get_num_procs(), std::max(2, openshot::Settings::Instance()->FF_THREADS)) +#define FF_AUDIO_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.)