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 * Libavfilter multithreading support
26 #include "libavutil/common.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/thread.h"
35 typedef struct ThreadContext {
40 avfilter_action_func *func;
42 /* per-execute parameters */
49 pthread_cond_t last_job_cond;
50 pthread_cond_t current_job_cond;
51 pthread_mutex_t current_job_lock;
53 unsigned int current_execute;
57 static void* attribute_align_arg worker(void *v)
60 int our_job = c->nb_jobs;
61 int nb_threads = c->nb_threads;
62 unsigned int last_execute = 0;
65 pthread_mutex_lock(&c->current_job_lock);
66 self_id = c->current_job++;
68 while (our_job >= c->nb_jobs) {
69 if (c->current_job == nb_threads + c->nb_jobs)
70 pthread_cond_signal(&c->last_job_cond);
72 while (last_execute == c->current_execute && !c->done)
73 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
74 last_execute = c->current_execute;
78 pthread_mutex_unlock(&c->current_job_lock);
82 pthread_mutex_unlock(&c->current_job_lock);
84 c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
86 pthread_mutex_lock(&c->current_job_lock);
87 our_job = c->current_job++;
91 static void slice_thread_uninit(ThreadContext *c)
95 pthread_mutex_lock(&c->current_job_lock);
97 pthread_cond_broadcast(&c->current_job_cond);
98 pthread_mutex_unlock(&c->current_job_lock);
100 for (i = 0; i < c->nb_threads; i++)
101 pthread_join(c->workers[i], NULL);
103 pthread_mutex_destroy(&c->current_job_lock);
104 pthread_cond_destroy(&c->current_job_cond);
105 pthread_cond_destroy(&c->last_job_cond);
106 av_freep(&c->workers);
109 static void slice_thread_park_workers(ThreadContext *c)
111 while (c->current_job != c->nb_threads + c->nb_jobs)
112 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
113 pthread_mutex_unlock(&c->current_job_lock);
116 static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
117 void *arg, int *ret, int nb_jobs)
119 ThreadContext *c = ctx->graph->internal->thread;
125 pthread_mutex_lock(&c->current_job_lock);
127 c->current_job = c->nb_threads;
128 c->nb_jobs = nb_jobs;
134 c->nb_rets = nb_jobs;
136 c->rets = &dummy_ret;
139 c->current_execute++;
141 pthread_cond_broadcast(&c->current_job_cond);
143 slice_thread_park_workers(c);
148 static int thread_init_internal(ThreadContext *c, int nb_threads)
153 int nb_cpus = av_cpu_count();
154 // use number of cores + 1 as thread count if there is more than one
156 nb_threads = nb_cpus + 1;
164 c->nb_threads = nb_threads;
165 c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
167 return AVERROR(ENOMEM);
173 pthread_cond_init(&c->current_job_cond, NULL);
174 pthread_cond_init(&c->last_job_cond, NULL);
176 pthread_mutex_init(&c->current_job_lock, NULL);
177 pthread_mutex_lock(&c->current_job_lock);
178 for (i = 0; i < nb_threads; i++) {
179 ret = pthread_create(&c->workers[i], NULL, worker, c);
181 pthread_mutex_unlock(&c->current_job_lock);
183 slice_thread_uninit(c);
188 slice_thread_park_workers(c);
190 return c->nb_threads;
193 int ff_graph_thread_init(AVFilterGraph *graph)
201 if (graph->nb_threads == 1) {
202 graph->thread_type = 0;
206 graph->internal->thread = av_mallocz(sizeof(ThreadContext));
207 if (!graph->internal->thread)
208 return AVERROR(ENOMEM);
210 ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
212 av_freep(&graph->internal->thread);
213 graph->thread_type = 0;
214 graph->nb_threads = 1;
215 return (ret < 0) ? ret : 0;
217 graph->nb_threads = ret;
219 graph->internal->thread_execute = thread_execute;
224 void ff_graph_thread_free(AVFilterGraph *graph)
226 if (graph->internal->thread)
227 slice_thread_uninit(graph->internal->thread);
228 av_freep(&graph->internal->thread);