2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Slice multithreading support functions
22 * @see doc/multithreading.txt
29 #include "pthread_internal.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/cpu.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/thread.h"
37 #include "libavutil/slicethread.h"
39 typedef int (action_func)(AVCodecContext *c, void *arg);
40 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
41 typedef int (main_func)(AVCodecContext *c);
43 typedef struct SliceThreadContext {
44 AVSliceThread *thread;
55 pthread_cond_t *progress_cond;
56 pthread_mutex_t *progress_mutex;
59 static void main_function(void *priv) {
60 AVCodecContext *avctx = priv;
61 SliceThreadContext *c = avctx->internal->thread_ctx;
65 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
67 AVCodecContext *avctx = priv;
68 SliceThreadContext *c = avctx->internal->thread_ctx;
71 ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
72 : c->func2(avctx, c->args, jobnr, threadnr);
77 void ff_slice_thread_free(AVCodecContext *avctx)
79 SliceThreadContext *c = avctx->internal->thread_ctx;
82 avpriv_slicethread_free(&c->thread);
84 for (i = 0; i < c->thread_count; i++) {
85 pthread_mutex_destroy(&c->progress_mutex[i]);
86 pthread_cond_destroy(&c->progress_cond[i]);
89 av_freep(&c->entries);
90 av_freep(&c->progress_mutex);
91 av_freep(&c->progress_cond);
92 av_freep(&avctx->internal->thread_ctx);
95 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
97 SliceThreadContext *c = avctx->internal->thread_ctx;
99 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
100 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
105 c->job_size = job_size;
110 avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc );
114 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
116 SliceThreadContext *c = avctx->internal->thread_ctx;
118 return thread_execute(avctx, NULL, arg, ret, job_count, 0);
121 int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count)
123 SliceThreadContext *c = avctx->internal->thread_ctx;
125 c->mainfunc = mainfunc;
126 return thread_execute(avctx, NULL, arg, ret, job_count, 0);
129 int ff_slice_thread_init(AVCodecContext *avctx)
131 SliceThreadContext *c;
132 int thread_count = avctx->thread_count;
133 static void (*mainfunc)(void *);
135 // We cannot do this in the encoder init as the threads are created before
136 if (av_codec_is_encoder(avctx->codec) &&
137 avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
138 avctx->height > 2800)
139 thread_count = avctx->thread_count = 1;
142 int nb_cpus = av_cpu_count();
144 nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
145 // use number of cores + 1 as thread count if there is more than one
147 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
149 thread_count = avctx->thread_count = 1;
152 if (thread_count <= 1) {
153 avctx->active_thread_type = 0;
157 avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
158 mainfunc = avctx->codec->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF ? &main_function : NULL;
159 if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, mainfunc, thread_count)) <= 1) {
161 avpriv_slicethread_free(&c->thread);
162 av_freep(&avctx->internal->thread_ctx);
163 avctx->thread_count = 1;
164 avctx->active_thread_type = 0;
167 avctx->thread_count = thread_count;
169 avctx->execute = thread_execute;
170 avctx->execute2 = thread_execute2;
174 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
176 SliceThreadContext *p = avctx->internal->thread_ctx;
177 int *entries = p->entries;
179 pthread_mutex_lock(&p->progress_mutex[thread]);
181 pthread_cond_signal(&p->progress_cond[thread]);
182 pthread_mutex_unlock(&p->progress_mutex[thread]);
185 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
187 SliceThreadContext *p = avctx->internal->thread_ctx;
188 int *entries = p->entries;
190 if (!entries || !field) return;
192 thread = thread ? thread - 1 : p->thread_count - 1;
194 pthread_mutex_lock(&p->progress_mutex[thread]);
195 while ((entries[field - 1] - entries[field]) < shift){
196 pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
198 pthread_mutex_unlock(&p->progress_mutex[thread]);
201 int ff_alloc_entries(AVCodecContext *avctx, int count)
205 if (avctx->active_thread_type & FF_THREAD_SLICE) {
206 SliceThreadContext *p = avctx->internal->thread_ctx;
209 av_assert0(p->thread_count == avctx->thread_count);
210 av_freep(&p->entries);
213 p->thread_count = avctx->thread_count;
214 p->entries = av_mallocz_array(count, sizeof(int));
216 if (!p->progress_mutex) {
217 p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
218 p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
221 if (!p->entries || !p->progress_mutex || !p->progress_cond) {
222 av_freep(&p->entries);
223 av_freep(&p->progress_mutex);
224 av_freep(&p->progress_cond);
225 return AVERROR(ENOMEM);
227 p->entries_count = count;
229 for (i = 0; i < p->thread_count; i++) {
230 pthread_mutex_init(&p->progress_mutex[i], NULL);
231 pthread_cond_init(&p->progress_cond[i], NULL);
238 void ff_reset_entries(AVCodecContext *avctx)
240 SliceThreadContext *p = avctx->internal->thread_ctx;
241 memset(p->entries, 0, p->entries_count * sizeof(int));