diff options
author | Mickaƫl Raulet <mraulet@insa-rennes.fr> | 2013-10-20 22:48:03 +0200 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-10-22 19:55:47 +0200 |
commit | e146c326b91297ff900db632eb0dba30289e9ee6 (patch) | |
tree | ddd66afe6f0dd4c9d2c8f6dade92fda7e15b663e | |
parent | 519395e4ddbdafc6049c88f2867cde9a1f270a57 (diff) | |
download | ffmpeg-e146c326b91297ff900db632eb0dba30289e9ee6.tar.gz |
avcodec/pthread: add wpp api
cherry picked from commit c7765f3295fe7dc0653161c6a3d3e1778b76ee67
cherry picked from commit 0008c4979fc1d1bc24d4d2c791715f6dd017563c
Conflicts:
libavcodec/utils.c
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
-rw-r--r-- | libavcodec/pthread.c | 65 | ||||
-rw-r--r-- | libavcodec/thread.h | 5 | ||||
-rw-r--r-- | libavcodec/utils.c | 17 |
3 files changed, 87 insertions, 0 deletions
diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c index 4c6d8784a7..b8beac3ed1 100644 --- a/libavcodec/pthread.c +++ b/libavcodec/pthread.c @@ -66,6 +66,12 @@ typedef struct ThreadContext { unsigned current_execute; int current_job; int done; + + int *entries; + int entries_count; + int thread_count; + pthread_cond_t *progress_cond; + pthread_mutex_t *progress_mutex; } ThreadContext; /** @@ -1126,3 +1132,62 @@ void ff_thread_free(AVCodecContext *avctx) else thread_free(avctx); } + +void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n) +{ + ThreadContext *p = avctx->thread_opaque; + int *entries = p->entries; + + pthread_mutex_lock(&p->progress_mutex[thread]); + entries[field] +=n; + pthread_cond_signal(&p->progress_cond[thread]); + pthread_mutex_unlock(&p->progress_mutex[thread]); +} + +void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift) +{ + ThreadContext *p = avctx->thread_opaque; + int *entries = p->entries; + + if (!entries || !field) return; + + thread = thread ? thread - 1 : p->thread_count - 1; + + pthread_mutex_lock(&p->progress_mutex[thread]); + while ((entries[field - 1] - entries[field]) < shift){ + pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]); + } + pthread_mutex_unlock(&p->progress_mutex[thread]); +} + +int ff_alloc_entries(AVCodecContext *avctx, int count) +{ + int i; + + if (avctx->active_thread_type & FF_THREAD_SLICE) { + ThreadContext *p = avctx->thread_opaque; + p->thread_count = avctx->thread_count; + p->entries = av_mallocz(count * sizeof(int)); + + if (!p->entries) { + return AVERROR(ENOMEM); + } + + p->entries_count = count; + p->progress_mutex = av_malloc(p->thread_count * sizeof(pthread_mutex_t)); + p->progress_cond = av_malloc(p->thread_count * sizeof(pthread_cond_t)); + + for (i = 0; i < p->thread_count; i++) { + pthread_mutex_init(&p->progress_mutex[i], NULL); + pthread_cond_init(&p->progress_cond[i], NULL); + } + } + + return 0; +} + +void ff_reset_entries(AVCodecContext *avctx) +{ + ThreadContext *p = avctx->thread_opaque; + memset(p->entries, 0, p->entries_count * sizeof(int)); +} diff --git a/libavcodec/thread.h b/libavcodec/thread.h index 0dc04e0702..c848d7ae8b 100644 --- a/libavcodec/thread.h +++ b/libavcodec/thread.h @@ -135,4 +135,9 @@ int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src); int ff_thread_init(AVCodecContext *s); void ff_thread_free(AVCodecContext *s); +int ff_alloc_entries(AVCodecContext *avctx, int count); +void ff_reset_entries(AVCodecContext *avctx); +void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n); +void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift); + #endif /* AVCODEC_THREAD_H */ diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 37d6ee3a64..cde8a5331b 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -3269,6 +3269,23 @@ int ff_thread_can_start_frame(AVCodecContext *avctx) return 1; } +int ff_alloc_entries(AVCodecContext *avctx, int count) +{ + return 0; +} + +void ff_reset_entries(AVCodecContext *avctx) +{ +} + +void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift) +{ +} + +void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n) +{ +} + #endif enum AVMediaType avcodec_get_type(enum AVCodecID codec_id) |