| @@ -31,29 +31,12 @@ · 30 unmodified lines |
| 31 | | 31 | |
| 32 | #include "config.h" | 32 | #include "config.h" |
| 33 | | 33 | |
| 34 | #if HAVE_SCHED_GETAFFINITY | | |
| 35 | #define _GNU_SOURCE | | |
| 36 | #include <sched.h> | | |
| 37 | #endif | | |
| 38 | #if HAVE_GETPROCESSAFFINITYMASK | | |
| 39 | #include <windows.h> | | |
| 40 | #endif | | |
| 41 | #if HAVE_SYSCTL | | |
| 42 | #if HAVE_SYS_PARAM_H | | |
| 43 | #include <sys/param.h> | | |
| 44 | #endif | | |
| 45 | #include <sys/types.h> | | |
| 46 | #include <sys/sysctl.h> | | |
| 47 | #endif | | |
| 48 | #if HAVE_SYSCONF | | |
| 49 | #include <unistd.h> | | |
| 50 | #endif | | |
| 51 | | | |
| 52 | #include "avcodec.h" | 34 | #include "avcodec.h" |
| 53 | #include "internal.h" | 35 | #include "internal.h" |
| 54 | #include "thread.h" | 36 | #include "thread.h" |
| 55 | #include "libavutil/avassert.h" | 37 | #include "libavutil/avassert.h" |
| 56 | #include "libavutil/common.h" | 38 | #include "libavutil/common.h" |
| | 39 | #include "libavutil/cpu.h" |
| 57 | | 40 | |
| 58 | #if HAVE_PTHREADS | 41 | #if HAVE_PTHREADS |
| 59 | #include <pthread.h> | 42 | #include <pthread.h> |
| @@ -153,40 +136,6 @@ typedef struct FrameThreadContext { · 93 unmodified lines |
| 153 | * limit the number of threads to 16 for automatic detection */ | 136 | * limit the number of threads to 16 for automatic detection */ |
| 154 | #define MAX_AUTO_THREADS 16 | 137 | #define MAX_AUTO_THREADS 16 |
| 155 | | 138 | |
| 156 | static int get_logical_cpus(AVCodecContext *avctx) | | |
| 157 | { | | |
| 158 | int ret, nb_cpus = 1; | | |
| 159 | #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) | | |
| 160 | cpu_set_t cpuset; | | |
| 161 | | | |
| 162 | CPU_ZERO(&cpuset); | | |
| 163 | | | |
| 164 | ret = sched_getaffinity(0, sizeof(cpuset), &cpuset); | | |
| 165 | if (!ret) { | | |
| 166 | nb_cpus = CPU_COUNT(&cpuset); | | |
| 167 | } | | |
| 168 | #elif HAVE_GETPROCESSAFFINITYMASK | | |
| 169 | DWORD_PTR proc_aff, sys_aff; | | |
| 170 | ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff); | | |
| 171 | if (ret) | | |
| 172 | nb_cpus = av_popcount64(proc_aff); | | |
| 173 | #elif HAVE_SYSCTL && defined(HW_NCPU) | | |
| 174 | int mib[2] = { CTL_HW, HW_NCPU }; | | |
| 175 | size_t len = sizeof(nb_cpus); | | |
| 176 | | | |
| 177 | ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0); | | |
| 178 | if (ret == -1) | | |
| 179 | nb_cpus = 0; | | |
| 180 | #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) | | |
| 181 | nb_cpus = sysconf(_SC_NPROC_ONLN); | | |
| 182 | #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) | | |
| 183 | nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); | | |
| 184 | #endif | | |
| 185 | av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); | | |
| 186 | return nb_cpus; | | |
| 187 | } | | |
| 188 | | | |
| 189 | | | |
| 190 | static void* attribute_align_arg worker(void *v) | 139 | static void* attribute_align_arg worker(void *v) |
| 191 | { | 140 | { |
| 192 | AVCodecContext *avctx = v; | 141 | AVCodecContext *avctx = v; |
| @@ -292,7 +241,8 @@ static int thread_init(AVCodecContext *avctx) · 99 unmodified lines |
| 292 | int thread_count = avctx->thread_count; | 241 | int thread_count = avctx->thread_count; |
| 293 | | 242 | |
| 294 | if (!thread_count) { | 243 | if (!thread_count) { |
| 295 | int nb_cpus = get_logical_cpus(avctx); | 244 | int nb_cpus = av_cpu_count(); |
| | 245 | av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); |
| 296 | // use number of cores + 1 as thread count if there is more than one | 246 | // use number of cores + 1 as thread count if there is more than one |
| 297 | if (nb_cpus > 1) | 247 | if (nb_cpus > 1) |
| 298 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); | 248 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); |
| @@ -790,7 +740,8 @@ static int frame_thread_init(AVCodecContext *avctx) · 491 unmodified lines |
| 790 | int i, err = 0; | 740 | int i, err = 0; |
| 791 | | 741 | |
| 792 | if (!thread_count) { | 742 | if (!thread_count) { |
| 793 | int nb_cpus = get_logical_cpus(avctx); | 743 | int nb_cpus = av_cpu_count(); |
| | 744 | av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); |
| 794 | // use number of cores + 1 as thread count if there is more than one | 745 | // use number of cores + 1 as thread count if there is more than one |
| 795 | if (nb_cpus > 1) | 746 | if (nb_cpus > 1) |
| 796 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); | 747 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); |