1 From b52c216539bdbee830e0d306b372037d4e0cb35f Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= <Reimar.Doeffinger@gmx.de>
3 Date: Sun, 8 Mar 2015 19:44:12 +0100
4 Subject: [PATCH] pthread: Fix ff_thread_get_format issues when called outside
7 Patch part of the XBMC patch set for ffmpeg, downloaded from
8 https://github.com/xbmc/FFmpeg/.
10 Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
12 libavcodec/pthread_frame.c | 22 +++++++++++++++++++---
13 1 file changed, 19 insertions(+), 3 deletions(-)
15 diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
16 index 5a4ab84..c29d0a9 100644
17 --- a/libavcodec/pthread_frame.c
18 +++ b/libavcodec/pthread_frame.c
20 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
22 typedef struct PerThreadContext {
24 struct FrameThreadContext *parent;
27 @@ -83,7 +84,8 @@ typedef struct PerThreadContext {
28 * Set when the codec calls get_format().
29 * State is returned to STATE_SETTING_UP afterwards.
31 - STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
32 + STATE_SETUP_FINISHED, ///< Set after the codec has called ff_thread_finish_setup().
33 + STATE_UPDATE_CONTEXT, ///< Main thread is updating its context
37 @@ -105,6 +107,7 @@ typedef struct PerThreadContext {
38 * Context stored in the client AVCodecInternal thread_ctx.
40 typedef struct FrameThreadContext {
42 PerThreadContext *threads; ///< The contexts for each thread.
43 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
45 @@ -143,6 +146,7 @@ static attribute_align_arg void *frame_worker_thread(void *arg)
46 AVCodecContext *avctx = p->avctx;
47 const AVCodec *codec = avctx->codec;
49 + av_assert0(!p->main_thread);
50 pthread_mutex_lock(&p->mutex);
52 while (p->state == STATE_INPUT_READY && !fctx->die)
53 @@ -330,6 +334,8 @@ static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
55 pthread_mutex_lock(&p->mutex);
57 + p->state = STATE_UPDATE_CONTEXT;
59 release_delayed_buffers(p);
62 @@ -408,6 +414,7 @@ int ff_thread_decode_frame(AVCodecContext *avctx,
63 int finished = fctx->next_finished;
66 + av_assert0(fctx->main_thread);
69 * Submit a packet to the next decoding thread.
70 @@ -515,6 +522,7 @@ void ff_thread_finish_setup(AVCodecContext *avctx) {
72 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
74 + av_assert0(!p->main_thread);
75 if(p->state == STATE_SETUP_FINISHED){
76 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
78 @@ -549,6 +557,7 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
79 const AVCodec *codec = avctx->codec;
82 + av_assert0(fctx->main_thread);
83 park_frame_worker_threads(fctx, thread_count);
85 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
86 @@ -634,6 +643,7 @@ int ff_frame_thread_init(AVCodecContext *avctx)
89 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
90 + fctx->main_thread = 1;
92 fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
93 pthread_mutex_init(&fctx->buffer_mutex, NULL);
94 @@ -718,6 +728,7 @@ void ff_thread_flush(AVCodecContext *avctx)
98 + av_assert0(fctx->main_thread);
99 park_frame_worker_threads(fctx, avctx->thread_count);
100 if (fctx->prev_thread) {
101 if (fctx->prev_thread != &fctx->threads[0])
102 @@ -743,7 +754,10 @@ void ff_thread_flush(AVCodecContext *avctx)
103 int ff_thread_can_start_frame(AVCodecContext *avctx)
105 PerThreadContext *p = avctx->internal->thread_ctx;
106 - if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
107 + if (!(avctx->active_thread_type&FF_THREAD_FRAME))
109 + av_assert0(!p->main_thread);
110 + if (p->state != STATE_SETTING_UP &&
111 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
114 @@ -762,6 +776,7 @@ static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int
115 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
116 return ff_get_buffer(avctx, f->f, flags);
118 + av_assert0(!p->main_thread);
119 if (p->state != STATE_SETTING_UP &&
120 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
121 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
122 @@ -819,7 +834,8 @@ enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixe
123 enum AVPixelFormat res;
124 PerThreadContext *p = avctx->internal->thread_ctx;
125 if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
126 - avctx->get_format == avcodec_default_get_format)
127 + avctx->get_format == avcodec_default_get_format ||
128 + p->main_thread || p->state == STATE_UPDATE_CONTEXT)
129 return ff_get_format(avctx, fmt);
130 if (p->state != STATE_SETTING_UP) {
131 av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");