summaryrefslogtreecommitdiff
path: root/libavcodec/pthread.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2011-12-23 03:00:12 +0100
committerMichael Niedermayer <michaelni@gmx.at>2011-12-23 03:25:51 +0100
commitd1c28e35300130f0ee28a3e5bbeb2cea403fad57 (patch)
treea18d0b0989962e2e2cab201fbf6128ae8332d8a4 /libavcodec/pthread.c
parent9f50dafe9025555f11e66e3b09cf3db2cd53cfb2 (diff)
parent4e8d6218c3cb8b9feffb70f8a53859540b975b36 (diff)
downloadffmpeg-d1c28e35300130f0ee28a3e5bbeb2cea403fad57.tar.gz
Merge remote-tracking branch 'qatar/master'
* qatar/master: build: fix standalone compilation of OMA muxer build: fix standalone compilation of Microsoft XMV demuxer build: fix standalone compilation of Core Audio Format demuxer kvmc: fix invalid reads 4xm: Add a check in decode_i_frame to prevent buffer overreads adpcm: fix IMA SMJPEG decoding options: set minimum for "threads" to zero bsd: use number of logical CPUs as automatic thread count windows: use number of CPUs as automatic thread count linux: use number of CPUs as automatic thread count pthreads: reset active_thread_type when slice thread_init returrns early v410dec: include correct headers Drop ALT_ prefix from BITSTREAM_READER_LE name. lavfi: always build vsrc_buffer. ra144enc: zero the reflection coeffs if the filter is unstable sws: readd PAL8 to isPacked() mov: Don't stick the QuickTime field ordering atom in extradata. truespeech: fix invalid reads in truespeech_apply_twopoint_filter() Conflicts: configure libavcodec/4xm.c libavcodec/avcodec.h libavfilter/Makefile libavfilter/allfilters.c libavformat/Makefile libswscale/swscale_internal.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec/pthread.c')
-rw-r--r--libavcodec/pthread.c63
1 files changed, 62 insertions, 1 deletions
diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c
index bca01edef5..e8bc318711 100644
--- a/libavcodec/pthread.c
+++ b/libavcodec/pthread.c
@@ -30,6 +30,17 @@
*/
#include "config.h"
+
+#if HAVE_SCHED_GETAFFINITY
+#define _GNU_SOURCE
+#include <sched.h>
+#elif HAVE_GETSYSTEMINFO
+#include <windows.h>
+#elif HAVE_SYSCTL
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#endif
+
#include "avcodec.h"
#include "internal.h"
#include "thread.h"
@@ -135,6 +146,40 @@ typedef struct FrameThreadContext {
int die; ///< Set when threads should exit.
} FrameThreadContext;
+
+/* H264 slice threading seems to be buggy with more than 16 threads,
+ * limit the number of threads to 16 for automatic detection */
+#define MAX_AUTO_THREADS 16
+
+static int get_logical_cpus(AVCodecContext *avctx)
+{
+ int ret, nb_cpus = 1;
+#if HAVE_SCHED_GETAFFINITY
+ cpu_set_t cpuset;
+
+ CPU_ZERO(&cpuset);
+
+ ret = sched_getaffinity(0, sizeof(cpuset), &cpuset);
+ if (!ret) {
+ nb_cpus = CPU_COUNT(&cpuset);
+ }
+#elif HAVE_GETSYSTEMINFO
+ SYSTEM_INFO sysinfo;
+ GetSystemInfo(&sysinfo);
+ nb_cpus = sysinfo.dwNumberOfProcessors;
+#elif HAVE_SYSCTL
+ int mib[2] = { CTL_HW, HW_NCPU };
+ size_t len = sizeof(nb_cpus);
+
+ ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0);
+ if (ret == -1)
+ nb_cpus = 0;
+#endif
+ av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
+ return FFMIN(nb_cpus, MAX_AUTO_THREADS);
+}
+
+
static void* attribute_align_arg worker(void *v)
{
AVCodecContext *avctx = v;
@@ -239,8 +284,17 @@ static int thread_init(AVCodecContext *avctx)
ThreadContext *c;
int thread_count = avctx->thread_count;
- if (thread_count <= 1)
+ if (!thread_count) {
+ int nb_cpus = get_logical_cpus(avctx);
+ // use number of cores + 1 as thread count if there is motre than one
+ if (nb_cpus > 1)
+ thread_count = avctx->thread_count = nb_cpus + 1;
+ }
+
+ if (thread_count <= 1) {
+ avctx->active_thread_type = 0;
return 0;
+ }
c = av_mallocz(sizeof(ThreadContext));
if (!c)
@@ -704,6 +758,13 @@ static int frame_thread_init(AVCodecContext *avctx)
FrameThreadContext *fctx;
int i, err = 0;
+ if (!thread_count) {
+ int nb_cpus = get_logical_cpus(avctx);
+ // use number of cores + 1 as thread count if there is motre than one
+ if (nb_cpus > 1)
+ thread_count = avctx->thread_count = nb_cpus + 1;
+ }
+
if (thread_count <= 1) {
avctx->active_thread_type = 0;
return 0;