3 * This file is part of FFmpeg.
5 * FFmpeg is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * FFmpeg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with FFmpeg; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Libavfilter multithreading support
27 #include "libavutil/common.h"
28 #include "libavutil/cpu.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/thread.h"
36 typedef struct ThreadContext {
41 avfilter_action_func *func;
43 /* per-execute parameters */
50 pthread_cond_t last_job_cond;
51 pthread_cond_t current_job_cond;
52 pthread_mutex_t current_job_lock;
54 unsigned int current_execute;
58 static void* attribute_align_arg worker(void *v)
61 int our_job = c->nb_jobs;
62 int nb_threads = c->nb_threads;
63 unsigned int last_execute = 0;
66 pthread_mutex_lock(&c->current_job_lock);
67 self_id = c->current_job++;
69 while (our_job >= c->nb_jobs) {
70 if (c->current_job == nb_threads + c->nb_jobs)
71 pthread_cond_signal(&c->last_job_cond);
73 while (last_execute == c->current_execute && !c->done)
74 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
75 last_execute = c->current_execute;
79 pthread_mutex_unlock(&c->current_job_lock);
83 pthread_mutex_unlock(&c->current_job_lock);
85 c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
87 pthread_mutex_lock(&c->current_job_lock);
88 our_job = c->current_job++;
92 static void slice_thread_uninit(ThreadContext *c)
96 pthread_mutex_lock(&c->current_job_lock);
98 pthread_cond_broadcast(&c->current_job_cond);
99 pthread_mutex_unlock(&c->current_job_lock);
101 for (i = 0; i < c->nb_threads; i++)
102 pthread_join(c->workers[i], NULL);
104 pthread_mutex_destroy(&c->current_job_lock);
105 pthread_cond_destroy(&c->current_job_cond);
106 pthread_cond_destroy(&c->last_job_cond);
107 av_freep(&c->workers);
110 static void slice_thread_park_workers(ThreadContext *c)
112 while (c->current_job != c->nb_threads + c->nb_jobs)
113 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
114 pthread_mutex_unlock(&c->current_job_lock);
117 static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
118 void *arg, int *ret, int nb_jobs)
120 ThreadContext *c = ctx->graph->internal->thread;
126 pthread_mutex_lock(&c->current_job_lock);
128 c->current_job = c->nb_threads;
129 c->nb_jobs = nb_jobs;
135 c->nb_rets = nb_jobs;
137 c->rets = &dummy_ret;
140 c->current_execute++;
142 pthread_cond_broadcast(&c->current_job_cond);
144 slice_thread_park_workers(c);
149 static int thread_init_internal(ThreadContext *c, int nb_threads)
154 int nb_cpus = av_cpu_count();
155 // use number of cores + 1 as thread count if there is more than one
157 nb_threads = nb_cpus + 1;
165 c->nb_threads = nb_threads;
166 c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
168 return AVERROR(ENOMEM);
174 pthread_cond_init(&c->current_job_cond, NULL);
175 pthread_cond_init(&c->last_job_cond, NULL);
177 pthread_mutex_init(&c->current_job_lock, NULL);
178 pthread_mutex_lock(&c->current_job_lock);
179 for (i = 0; i < nb_threads; i++) {
180 ret = pthread_create(&c->workers[i], NULL, worker, c);
182 pthread_mutex_unlock(&c->current_job_lock);
184 slice_thread_uninit(c);
189 slice_thread_park_workers(c);
191 return c->nb_threads;
194 int ff_graph_thread_init(AVFilterGraph *graph)
202 if (graph->nb_threads == 1) {
203 graph->thread_type = 0;
207 graph->internal->thread = av_mallocz(sizeof(ThreadContext));
208 if (!graph->internal->thread)
209 return AVERROR(ENOMEM);
211 ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
213 av_freep(&graph->internal->thread);
214 graph->thread_type = 0;
215 graph->nb_threads = 1;
216 return (ret < 0) ? ret : 0;
218 graph->nb_threads = ret;
220 graph->internal->thread_execute = thread_execute;
225 void ff_graph_thread_free(AVFilterGraph *graph)
227 if (graph->internal->thread)
228 slice_thread_uninit(graph->internal->thread);
229 av_freep(&graph->internal->thread);