2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This library 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 of the License, or (at your option) any later version.
9 * This library 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 this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <semaphore.h>
27 typedef struct JobContext{
30 int (*func)(AVCodecContext *c, void *arg);
35 typedef struct WorkerContext{
36 AVCodecContext *avctx;
43 typedef struct ThreadContext{
44 WorkerContext *worker;
47 int allocated_job_count;
50 static void * thread_func(void *v){
52 ThreadContext *c= w->avctx->thread_opaque;
56 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X enter wait\n", (int)v);
57 sem_wait(&w->work_sem);
58 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X after wait\n", (int)v);
62 for(i=0; i<c->job_count; i++){
63 int index= (i + w->start_index) % c->job_count;
64 JobContext *j= &c->job[index];
66 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X first check of %d\n", (int)v, index);
67 if(j->assigned) continue; //unsynced check, if != 0 it is already given to another worker, it never becomes available before the next execute() call so this should be safe
69 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X second check of %d\n", (int)v, index);
70 if(sem_trywait(&j->available_sem) == 0){
72 j->ret= j->func(w->avctx, j->arg);
73 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X done %d\n", (int)v, index);
76 //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X complete\n", (int)v);
77 sem_post(&w->done_sem);
84 * free what has been allocated by avcodec_thread_init().
85 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
87 void avcodec_thread_free(AVCodecContext *s){
88 ThreadContext *c= s->thread_opaque;
91 for(i=0; i<c->allocated_job_count; i++){
92 sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0);
93 sem_destroy(&c->job[i].available_sem);
97 for(i=0; i<s->thread_count; i++){
98 sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0);
99 sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0);
101 sem_post(&c->worker[i].work_sem);
102 pthread_join(c->worker[i].thread, NULL);
103 sem_destroy(&c->worker[i].work_sem);
104 sem_destroy(&c->worker[i].done_sem);
108 av_freep(&c->worker);
109 av_freep(&s->thread_opaque);
112 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int job_count){
113 ThreadContext *c= s->thread_opaque;
116 assert(s == c->avctx);
117 if(job_count > c->allocated_job_count){
118 c->job= av_realloc(c->job, job_count*sizeof(JobContext));
120 for(i=c->allocated_job_count; i<job_count; i++){
121 memset(&c->job[i], 0, sizeof(JobContext));
122 c->allocated_job_count++;
124 if(sem_init(&c->job[i].available_sem, 0, 0))
128 c->job_count= job_count;
130 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
132 for(i=0; i<job_count; i++){
133 sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0);
135 c->job[i].arg= arg[i];
136 c->job[i].func= func;
137 c->job[i].ret= 12345;
138 c->job[i].assigned= 0;
139 sem_post(&c->job[i].available_sem);
142 for(i=0; i<s->thread_count && i<job_count; i++){
143 sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0);
144 sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0);
146 c->worker[i].start_index= (i + job_count/2)/job_count;
147 //av_log(s, AV_LOG_DEBUG, "start worker %d\n", i);
148 sem_post(&c->worker[i].work_sem);
151 for(i=0; i<s->thread_count && i<job_count; i++){
152 //av_log(s, AV_LOG_DEBUG, "wait for worker %d\n", i);
153 sem_wait(&c->worker[i].done_sem);
155 sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0);
156 sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0);
159 for(i=0; i<job_count; i++){
160 sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0);
162 c->job[i].func= NULL;
163 if(ret) ret[i]= c->job[i].ret;
169 int avcodec_thread_init(AVCodecContext *s, int thread_count){
172 WorkerContext *worker;
174 s->thread_count= thread_count;
176 assert(!s->thread_opaque);
177 c= av_mallocz(sizeof(ThreadContext));
178 worker= av_mallocz(sizeof(WorkerContext)*thread_count);
182 for(i=0; i<thread_count; i++){
183 //printf("init semaphors %d\n", i); fflush(stdout);
185 if(sem_init(&worker[i].work_sem, 0, 0))
187 if(sem_init(&worker[i].done_sem, 0, 0))
189 //printf("create thread %d\n", i); fflush(stdout);
190 if(pthread_create(&worker[i].thread, NULL, thread_func, &worker[i]))
193 //printf("init done\n"); fflush(stdout);
195 s->execute= avcodec_thread_execute;
199 avcodec_thread_free(s);