Merge pull request #23014 from alalek:ffmpeg_default_threads

This commit is contained in:
Alexander Alekhin 2022-12-22 07:57:24 +00:00
commit 4acb267cf4

View File

@ -373,42 +373,6 @@ inline double get_monotonic_time_diff_ms(timespec time1, timespec time2)
}
#endif // USE_AV_INTERRUPT_CALLBACK
static int get_number_of_cpus(void)
{
#if defined _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
return (int)sysinfo.dwNumberOfProcessors;
#elif defined __linux__ || defined __HAIKU__
return (int)sysconf( _SC_NPROCESSORS_ONLN );
#elif defined __APPLE__
int numCPU=0;
int mib[4];
size_t len = sizeof(numCPU);
// set the mib for hw.ncpu
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
// get the number of CPUs from the system
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 )
{
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
if( numCPU < 1 )
numCPU = 1;
}
return (int)numCPU;
#else
return 1;
#endif
}
struct Image_FFMPEG
{
@ -998,12 +962,17 @@ public:
inline void fill_codec_context(AVCodecContext * enc, AVDictionary * dict)
{
//#ifdef FF_API_THREAD_INIT
// avcodec_thread_init(enc, get_number_of_cpus());
//#else
const int nCpus = get_number_of_cpus();
enc->thread_count = enc->thread_count ? enc->thread_count: nCpus;
//#endif
if (!enc->thread_count)
{
int nCpus = cv::getNumberOfCPUs();
int requestedThreads = std::min(nCpus, 16); // [OPENCV:FFMPEG:24] Application has requested XX threads. Using a thread count greater than 16 is not recommended.
char* threads_option = getenv("OPENCV_FFMPEG_THREADS");
if (threads_option != NULL)
{
requestedThreads = atoi(threads_option);
}
enc->thread_count = requestedThreads;
}
AVDictionaryEntry* avdiscard_entry = av_dict_get(dict, "avdiscard", NULL, 0);