Merge commit '38ecc3702d
'
* commit '38ecc3702d
':
pthread: store thread contexts in AVCodecInternal instead of AVCodecContext
Conflicts:
libavcodec/internal.h
libavcodec/utils.c
Merged-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
commit
3fc26d8073
7 changed files with 44 additions and 39 deletions
|
@ -2681,13 +2681,13 @@ typedef struct AVCodecContext {
|
||||||
*/
|
*/
|
||||||
int (*execute2)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count);
|
int (*execute2)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count);
|
||||||
|
|
||||||
|
#if FF_API_THREAD_OPAQUE
|
||||||
/**
|
/**
|
||||||
* thread opaque
|
* @deprecated this field should not be used from outside of lavc
|
||||||
* Can be used by execute() to store some per AVCodecContext stuff.
|
|
||||||
* - encoding: set by execute()
|
|
||||||
* - decoding: set by execute()
|
|
||||||
*/
|
*/
|
||||||
|
attribute_deprecated
|
||||||
void *thread_opaque;
|
void *thread_opaque;
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* noise vs. sse weight for the nsse comparsion function
|
* noise vs. sse weight for the nsse comparsion function
|
||||||
|
|
|
@ -95,6 +95,8 @@ typedef struct AVCodecInternal {
|
||||||
|
|
||||||
FramePool *pool;
|
FramePool *pool;
|
||||||
|
|
||||||
|
void *thread_ctx;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* temporary buffer used for encoders to store their bitstream
|
* temporary buffer used for encoders to store their bitstream
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -173,7 +173,6 @@ int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
|
||||||
dest->codec = NULL;
|
dest->codec = NULL;
|
||||||
dest->slice_offset = NULL;
|
dest->slice_offset = NULL;
|
||||||
dest->hwaccel = NULL;
|
dest->hwaccel = NULL;
|
||||||
dest->thread_opaque = NULL;
|
|
||||||
dest->internal = NULL;
|
dest->internal = NULL;
|
||||||
|
|
||||||
/* reallocate values that should be allocated separately */
|
/* reallocate values that should be allocated separately */
|
||||||
|
|
|
@ -48,7 +48,7 @@
|
||||||
#include "libavutil/mem.h"
|
#include "libavutil/mem.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context used by codec threads and stored in their AVCodecContext thread_opaque.
|
* Context used by codec threads and stored in their AVCodecInternal thread_ctx.
|
||||||
*/
|
*/
|
||||||
typedef struct PerThreadContext {
|
typedef struct PerThreadContext {
|
||||||
struct FrameThreadContext *parent;
|
struct FrameThreadContext *parent;
|
||||||
|
@ -102,7 +102,7 @@ typedef struct PerThreadContext {
|
||||||
} PerThreadContext;
|
} PerThreadContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context stored in the client AVCodecContext thread_opaque.
|
* Context stored in the client AVCodecInternal thread_ctx.
|
||||||
*/
|
*/
|
||||||
typedef struct FrameThreadContext {
|
typedef struct FrameThreadContext {
|
||||||
PerThreadContext *threads; ///< The contexts for each thread.
|
PerThreadContext *threads; ///< The contexts for each thread.
|
||||||
|
@ -401,7 +401,7 @@ int ff_thread_decode_frame(AVCodecContext *avctx,
|
||||||
AVFrame *picture, int *got_picture_ptr,
|
AVFrame *picture, int *got_picture_ptr,
|
||||||
AVPacket *avpkt)
|
AVPacket *avpkt)
|
||||||
{
|
{
|
||||||
FrameThreadContext *fctx = avctx->thread_opaque;
|
FrameThreadContext *fctx = avctx->internal->thread_ctx;
|
||||||
int finished = fctx->next_finished;
|
int finished = fctx->next_finished;
|
||||||
PerThreadContext *p;
|
PerThreadContext *p;
|
||||||
int err;
|
int err;
|
||||||
|
@ -478,7 +478,7 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field)
|
||||||
|
|
||||||
if (!progress || progress[field] >= n) return;
|
if (!progress || progress[field] >= n) return;
|
||||||
|
|
||||||
p = f->owner->thread_opaque;
|
p = f->owner->internal->thread_ctx;
|
||||||
|
|
||||||
if (f->owner->debug&FF_DEBUG_THREADS)
|
if (f->owner->debug&FF_DEBUG_THREADS)
|
||||||
av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
|
av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
|
||||||
|
@ -496,7 +496,7 @@ void ff_thread_await_progress(ThreadFrame *f, int n, int field)
|
||||||
|
|
||||||
if (!progress || progress[field] >= n) return;
|
if (!progress || progress[field] >= n) return;
|
||||||
|
|
||||||
p = f->owner->thread_opaque;
|
p = f->owner->internal->thread_ctx;
|
||||||
|
|
||||||
if (f->owner->debug&FF_DEBUG_THREADS)
|
if (f->owner->debug&FF_DEBUG_THREADS)
|
||||||
av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
|
av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
|
||||||
|
@ -508,7 +508,7 @@ void ff_thread_await_progress(ThreadFrame *f, int n, int field)
|
||||||
}
|
}
|
||||||
|
|
||||||
void ff_thread_finish_setup(AVCodecContext *avctx) {
|
void ff_thread_finish_setup(AVCodecContext *avctx) {
|
||||||
PerThreadContext *p = avctx->thread_opaque;
|
PerThreadContext *p = avctx->internal->thread_ctx;
|
||||||
|
|
||||||
if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
|
if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
|
||||||
|
|
||||||
|
@ -542,7 +542,7 @@ static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count
|
||||||
|
|
||||||
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
|
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
|
||||||
{
|
{
|
||||||
FrameThreadContext *fctx = avctx->thread_opaque;
|
FrameThreadContext *fctx = avctx->internal->thread_ctx;
|
||||||
const AVCodec *codec = avctx->codec;
|
const AVCodec *codec = avctx->codec;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -591,16 +591,16 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
|
||||||
|
|
||||||
if (i) {
|
if (i) {
|
||||||
av_freep(&p->avctx->priv_data);
|
av_freep(&p->avctx->priv_data);
|
||||||
av_freep(&p->avctx->internal);
|
|
||||||
av_freep(&p->avctx->slice_offset);
|
av_freep(&p->avctx->slice_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
av_freep(&p->avctx->internal);
|
||||||
av_freep(&p->avctx);
|
av_freep(&p->avctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
av_freep(&fctx->threads);
|
av_freep(&fctx->threads);
|
||||||
pthread_mutex_destroy(&fctx->buffer_mutex);
|
pthread_mutex_destroy(&fctx->buffer_mutex);
|
||||||
av_freep(&avctx->thread_opaque);
|
av_freep(&avctx->internal->thread_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ff_frame_thread_init(AVCodecContext *avctx)
|
int ff_frame_thread_init(AVCodecContext *avctx)
|
||||||
|
@ -631,7 +631,7 @@ int ff_frame_thread_init(AVCodecContext *avctx)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
|
avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
|
||||||
|
|
||||||
fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
|
fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
|
||||||
pthread_mutex_init(&fctx->buffer_mutex, NULL);
|
pthread_mutex_init(&fctx->buffer_mutex, NULL);
|
||||||
|
@ -656,9 +656,16 @@ int ff_frame_thread_init(AVCodecContext *avctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
*copy = *src;
|
*copy = *src;
|
||||||
copy->thread_opaque = p;
|
|
||||||
copy->pkt = &p->avpkt;
|
copy->pkt = &p->avpkt;
|
||||||
|
|
||||||
|
copy->internal = av_malloc(sizeof(AVCodecInternal));
|
||||||
|
if (!copy->internal) {
|
||||||
|
err = AVERROR(ENOMEM);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
*copy->internal = *src->internal;
|
||||||
|
copy->internal->thread_ctx = p;
|
||||||
|
|
||||||
if (!i) {
|
if (!i) {
|
||||||
src = copy;
|
src = copy;
|
||||||
|
|
||||||
|
@ -673,12 +680,6 @@ int ff_frame_thread_init(AVCodecContext *avctx)
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
|
memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
|
||||||
copy->internal = av_malloc(sizeof(AVCodecInternal));
|
|
||||||
if (!copy->internal) {
|
|
||||||
err = AVERROR(ENOMEM);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
*copy->internal = *src->internal;
|
|
||||||
copy->internal->is_copy = 1;
|
copy->internal->is_copy = 1;
|
||||||
|
|
||||||
if (codec->init_thread_copy)
|
if (codec->init_thread_copy)
|
||||||
|
@ -704,9 +705,9 @@ error:
|
||||||
void ff_thread_flush(AVCodecContext *avctx)
|
void ff_thread_flush(AVCodecContext *avctx)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
FrameThreadContext *fctx = avctx->thread_opaque;
|
FrameThreadContext *fctx = avctx->internal->thread_ctx;
|
||||||
|
|
||||||
if (!avctx->thread_opaque) return;
|
if (!fctx) return;
|
||||||
|
|
||||||
park_frame_worker_threads(fctx, avctx->thread_count);
|
park_frame_worker_threads(fctx, avctx->thread_count);
|
||||||
if (fctx->prev_thread) {
|
if (fctx->prev_thread) {
|
||||||
|
@ -731,7 +732,7 @@ void ff_thread_flush(AVCodecContext *avctx)
|
||||||
|
|
||||||
int ff_thread_can_start_frame(AVCodecContext *avctx)
|
int ff_thread_can_start_frame(AVCodecContext *avctx)
|
||||||
{
|
{
|
||||||
PerThreadContext *p = avctx->thread_opaque;
|
PerThreadContext *p = avctx->internal->thread_ctx;
|
||||||
if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
|
if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
|
||||||
(avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
|
(avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -741,7 +742,7 @@ int ff_thread_can_start_frame(AVCodecContext *avctx)
|
||||||
|
|
||||||
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
|
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
|
||||||
{
|
{
|
||||||
PerThreadContext *p = avctx->thread_opaque;
|
PerThreadContext *p = avctx->internal->thread_ctx;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
f->owner = avctx;
|
f->owner = avctx;
|
||||||
|
@ -806,7 +807,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
|
||||||
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
|
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
|
||||||
{
|
{
|
||||||
enum AVPixelFormat res;
|
enum AVPixelFormat res;
|
||||||
PerThreadContext *p = avctx->thread_opaque;
|
PerThreadContext *p = avctx->internal->thread_ctx;
|
||||||
if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
|
if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
|
||||||
avctx->get_format == avcodec_default_get_format)
|
avctx->get_format == avcodec_default_get_format)
|
||||||
return avctx->get_format(avctx, fmt);
|
return avctx->get_format(avctx, fmt);
|
||||||
|
@ -839,7 +840,7 @@ int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
|
||||||
|
|
||||||
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
|
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
|
||||||
{
|
{
|
||||||
PerThreadContext *p = avctx->thread_opaque;
|
PerThreadContext *p = avctx->internal->thread_ctx;
|
||||||
FrameThreadContext *fctx;
|
FrameThreadContext *fctx;
|
||||||
AVFrame *dst, *tmp;
|
AVFrame *dst, *tmp;
|
||||||
FF_DISABLE_DEPRECATION_WARNINGS
|
FF_DISABLE_DEPRECATION_WARNINGS
|
||||||
|
|
|
@ -71,7 +71,7 @@ typedef struct SliceThreadContext {
|
||||||
static void* attribute_align_arg worker(void *v)
|
static void* attribute_align_arg worker(void *v)
|
||||||
{
|
{
|
||||||
AVCodecContext *avctx = v;
|
AVCodecContext *avctx = v;
|
||||||
SliceThreadContext *c = avctx->thread_opaque;
|
SliceThreadContext *c = avctx->internal->thread_ctx;
|
||||||
unsigned last_execute = 0;
|
unsigned last_execute = 0;
|
||||||
int our_job = c->job_count;
|
int our_job = c->job_count;
|
||||||
int thread_count = avctx->thread_count;
|
int thread_count = avctx->thread_count;
|
||||||
|
@ -106,7 +106,7 @@ static void* attribute_align_arg worker(void *v)
|
||||||
|
|
||||||
void ff_slice_thread_free(AVCodecContext *avctx)
|
void ff_slice_thread_free(AVCodecContext *avctx)
|
||||||
{
|
{
|
||||||
SliceThreadContext *c = avctx->thread_opaque;
|
SliceThreadContext *c = avctx->internal->thread_ctx;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
pthread_mutex_lock(&c->current_job_lock);
|
pthread_mutex_lock(&c->current_job_lock);
|
||||||
|
@ -121,7 +121,7 @@ void ff_slice_thread_free(AVCodecContext *avctx)
|
||||||
pthread_cond_destroy(&c->current_job_cond);
|
pthread_cond_destroy(&c->current_job_cond);
|
||||||
pthread_cond_destroy(&c->last_job_cond);
|
pthread_cond_destroy(&c->last_job_cond);
|
||||||
av_free(c->workers);
|
av_free(c->workers);
|
||||||
av_freep(&avctx->thread_opaque);
|
av_freep(&avctx->internal->thread_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
|
static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
|
||||||
|
@ -133,7 +133,7 @@ static av_always_inline void thread_park_workers(SliceThreadContext *c, int thre
|
||||||
|
|
||||||
static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
|
static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
|
||||||
{
|
{
|
||||||
SliceThreadContext *c = avctx->thread_opaque;
|
SliceThreadContext *c = avctx->internal->thread_ctx;
|
||||||
int dummy_ret;
|
int dummy_ret;
|
||||||
|
|
||||||
if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
|
if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
|
||||||
|
@ -166,7 +166,7 @@ static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, i
|
||||||
|
|
||||||
static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
|
static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
|
||||||
{
|
{
|
||||||
SliceThreadContext *c = avctx->thread_opaque;
|
SliceThreadContext *c = avctx->internal->thread_ctx;
|
||||||
c->func2 = func2;
|
c->func2 = func2;
|
||||||
return thread_execute(avctx, NULL, arg, ret, job_count, 0);
|
return thread_execute(avctx, NULL, arg, ret, job_count, 0);
|
||||||
}
|
}
|
||||||
|
@ -207,7 +207,7 @@ int ff_slice_thread_init(AVCodecContext *avctx)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
avctx->thread_opaque = c;
|
avctx->internal->thread_ctx = c;
|
||||||
c->current_job = 0;
|
c->current_job = 0;
|
||||||
c->job_count = 0;
|
c->job_count = 0;
|
||||||
c->job_size = 0;
|
c->job_size = 0;
|
||||||
|
@ -234,7 +234,7 @@ int ff_slice_thread_init(AVCodecContext *avctx)
|
||||||
|
|
||||||
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
|
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
|
||||||
{
|
{
|
||||||
SliceThreadContext *p = avctx->thread_opaque;
|
SliceThreadContext *p = avctx->internal->thread_ctx;
|
||||||
int *entries = p->entries;
|
int *entries = p->entries;
|
||||||
|
|
||||||
pthread_mutex_lock(&p->progress_mutex[thread]);
|
pthread_mutex_lock(&p->progress_mutex[thread]);
|
||||||
|
@ -245,7 +245,7 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, in
|
||||||
|
|
||||||
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
|
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
|
||||||
{
|
{
|
||||||
SliceThreadContext *p = avctx->thread_opaque;
|
SliceThreadContext *p = avctx->internal->thread_ctx;
|
||||||
int *entries = p->entries;
|
int *entries = p->entries;
|
||||||
|
|
||||||
if (!entries || !field) return;
|
if (!entries || !field) return;
|
||||||
|
@ -264,7 +264,7 @@ int ff_alloc_entries(AVCodecContext *avctx, int count)
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (avctx->active_thread_type & FF_THREAD_SLICE) {
|
if (avctx->active_thread_type & FF_THREAD_SLICE) {
|
||||||
SliceThreadContext *p = avctx->thread_opaque;
|
SliceThreadContext *p = avctx->internal->thread_ctx;
|
||||||
p->thread_count = avctx->thread_count;
|
p->thread_count = avctx->thread_count;
|
||||||
p->entries = av_mallocz(count * sizeof(int));
|
p->entries = av_mallocz(count * sizeof(int));
|
||||||
|
|
||||||
|
@ -287,6 +287,6 @@ int ff_alloc_entries(AVCodecContext *avctx, int count)
|
||||||
|
|
||||||
void ff_reset_entries(AVCodecContext *avctx)
|
void ff_reset_entries(AVCodecContext *avctx)
|
||||||
{
|
{
|
||||||
SliceThreadContext *p = avctx->thread_opaque;
|
SliceThreadContext *p = avctx->internal->thread_ctx;
|
||||||
memset(p->entries, 0, p->entries_count * sizeof(int));
|
memset(p->entries, 0, p->entries_count * sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2558,7 +2558,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
|
||||||
ff_frame_thread_encoder_free(avctx);
|
ff_frame_thread_encoder_free(avctx);
|
||||||
ff_lock_avcodec(avctx);
|
ff_lock_avcodec(avctx);
|
||||||
}
|
}
|
||||||
if (HAVE_THREADS && avctx->thread_opaque)
|
if (HAVE_THREADS && avctx->internal->thread_ctx)
|
||||||
ff_thread_free(avctx);
|
ff_thread_free(avctx);
|
||||||
if (avctx->codec && avctx->codec->close)
|
if (avctx->codec && avctx->codec->close)
|
||||||
avctx->codec->close(avctx);
|
avctx->codec->close(avctx);
|
||||||
|
|
|
@ -108,5 +108,8 @@
|
||||||
#ifndef FF_API_ASPECT_EXTENDED
|
#ifndef FF_API_ASPECT_EXTENDED
|
||||||
#define FF_API_ASPECT_EXTENDED (LIBAVCODEC_VERSION_MAJOR < 56)
|
#define FF_API_ASPECT_EXTENDED (LIBAVCODEC_VERSION_MAJOR < 56)
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef FF_API_THREAD_OPAQUE
|
||||||
|
#define FF_API_THREAD_OPAQUE (LIBAVCODEC_VERSION_MAJOR < 56)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* AVCODEC_VERSION_H */
|
#endif /* AVCODEC_VERSION_H */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue