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>
28 typedef struct ThreadContext{
29 AVCodecContext *avctx;
33 int (*func)(AVCodecContext *c, void *arg);
38 static void * thread_func(void *v){
42 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
43 sem_wait(&c->work_sem);
44 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
46 c->ret= c->func(c->avctx, c->arg);
49 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
50 sem_post(&c->done_sem);
57 * free what has been allocated by avcodec_thread_init().
58 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
60 void avcodec_thread_free(AVCodecContext *s){
61 ThreadContext *c= s->thread_opaque;
64 for(i=0; i<s->thread_count; i++){
67 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
68 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
71 sem_post(&c[i].work_sem);
72 pthread_join(c[i].thread, NULL);
73 sem_destroy(&c[i].work_sem);
74 sem_destroy(&c[i].done_sem);
77 av_freep(&s->thread_opaque);
80 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
81 ThreadContext *c= s->thread_opaque;
84 assert(s == c->avctx);
85 assert(count <= s->thread_count);
87 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
89 for(i=0; i<count; i++){
90 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
91 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
96 sem_post(&c[i].work_sem);
98 for(i=0; i<count; i++){
99 sem_wait(&c[i].done_sem);
101 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
102 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
105 if(ret) ret[i]= c[i].ret;
110 int avcodec_thread_init(AVCodecContext *s, int thread_count){
114 s->thread_count= thread_count;
116 assert(!s->thread_opaque);
117 c= av_mallocz(sizeof(ThreadContext)*thread_count);
120 for(i=0; i<thread_count; i++){
121 //printf("init semaphors %d\n", i); fflush(stdout);
123 if(sem_init(&c[i].work_sem, 0, 0))
125 if(sem_init(&c[i].done_sem, 0, 0))
127 //printf("create thread %d\n", i); fflush(stdout);
128 if(pthread_create(&c[i].thread, NULL, thread_func, &c[i]))
131 //printf("init done\n"); fflush(stdout);
133 s->execute= avcodec_thread_execute;
137 avcodec_thread_free(s);