2 * Copyright (c) 2004 François Revol <revol@free.fr>
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
26 typedef struct ThreadContext{
27 AVCodecContext *avctx;
31 int (*func)(AVCodecContext *c, void *arg);
37 static int32 ff_thread_func(void *v){
41 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
42 acquire_sem(c->work_sem);
43 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
45 c->ret= c->func(c->avctx, c->arg);
48 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
49 release_sem(c->done_sem);
56 * free what has been allocated by avcodec_thread_init().
57 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
59 void avcodec_thread_free(AVCodecContext *s){
60 ThreadContext *c= s->thread_opaque;
64 for(i=0; i<s->thread_count; i++){
67 release_sem(c[i].work_sem);
68 wait_for_thread(c[i].thread, &ret);
69 if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
70 if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
73 av_freep(&s->thread_opaque);
76 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
77 ThreadContext *c= s->thread_opaque;
80 assert(s == c->avctx);
81 assert(count <= s->thread_count);
83 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
85 for(i=0; i<count; i++){
90 release_sem(c[i].work_sem);
92 for(i=0; i<count; i++){
93 acquire_sem(c[i].done_sem);
96 if(ret) ret[i]= c[i].ret;
101 int avcodec_thread_init(AVCodecContext *s, int thread_count){
105 s->thread_count= thread_count;
107 assert(!s->thread_opaque);
108 c= av_mallocz(sizeof(ThreadContext)*thread_count);
111 for(i=0; i<thread_count; i++){
112 //printf("init semaphors %d\n", i); fflush(stdout);
115 if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
117 if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
120 //printf("create thread %d\n", i); fflush(stdout);
121 c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
122 if( c[i].thread < B_OK ) goto fail;
123 resume_thread(c[i].thread );
125 //printf("init done\n"); fflush(stdout);
127 s->execute= avcodec_thread_execute;
131 avcodec_thread_free(s);