2 * Copyright (c) 2004 François Revol <revol@free.fr>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 typedef struct ThreadContext
{
28 AVCodecContext
*avctx
;
32 int (*func
)(AVCodecContext
*c
, void *arg
);
37 // it's odd Be never patented that :D
42 static inline int lock_ben(struct benaphore
*ben
)
44 if (atomic_add(&ben
->atom
, 1) > 0)
45 return acquire_sem(ben
->sem
);
48 static inline int unlock_ben(struct benaphore
*ben
)
50 if (atomic_add(&ben
->atom
, -1) > 1)
51 return release_sem(ben
->sem
);
55 static struct benaphore av_thread_lib_ben
;
57 static int32
ff_thread_func(void *v
){
61 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
62 acquire_sem(c
->work_sem
);
63 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
65 c
->ret
= c
->func(c
->avctx
, c
->arg
);
68 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
69 release_sem(c
->done_sem
);
76 * free what has been allocated by avcodec_thread_init().
77 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
79 void avcodec_thread_free(AVCodecContext
*s
){
80 ThreadContext
*c
= s
->thread_opaque
;
84 for(i
=0; i
<s
->thread_count
; i
++){
87 release_sem(c
[i
].work_sem
);
88 wait_for_thread(c
[i
].thread
, &ret
);
89 if(c
[i
].work_sem
> B_OK
) delete_sem(c
[i
].work_sem
);
90 if(c
[i
].done_sem
> B_OK
) delete_sem(c
[i
].done_sem
);
93 av_freep(&s
->thread_opaque
);
96 int avcodec_thread_execute(AVCodecContext
*s
, int (*func
)(AVCodecContext
*c2
, void *arg2
),void **arg
, int *ret
, int count
){
97 ThreadContext
*c
= s
->thread_opaque
;
100 assert(s
== c
->avctx
);
101 assert(count
<= s
->thread_count
);
103 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
105 for(i
=0; i
<count
; i
++){
110 release_sem(c
[i
].work_sem
);
112 for(i
=0; i
<count
; i
++){
113 acquire_sem(c
[i
].done_sem
);
116 if(ret
) ret
[i
]= c
[i
].ret
;
121 int avcodec_thread_init(AVCodecContext
*s
, int thread_count
){
125 s
->thread_count
= thread_count
;
127 assert(!s
->thread_opaque
);
128 c
= av_mallocz(sizeof(ThreadContext
)*thread_count
);
131 for(i
=0; i
<thread_count
; i
++){
132 //printf("init semaphors %d\n", i); fflush(stdout);
135 if((c
[i
].work_sem
= create_sem(0, "ff work sem")) < B_OK
)
137 if((c
[i
].done_sem
= create_sem(0, "ff done sem")) < B_OK
)
140 //printf("create thread %d\n", i); fflush(stdout);
141 c
[i
].thread
= spawn_thread(ff_thread_func
, "libavcodec thread", B_LOW_PRIORITY
, &c
[i
] );
142 if( c
[i
].thread
< B_OK
) goto fail
;
143 resume_thread(c
[i
].thread
);
145 //printf("init done\n"); fflush(stdout);
147 s
->execute
= avcodec_thread_execute
;
151 avcodec_thread_free(s
);
155 /* provide a mean to serialize calls to avcodec_*() for thread safety. */
157 int avcodec_thread_lock_lib(void)
159 return lock_ben(&av_thread_lib_ben
);
162 int avcodec_thread_unlock_lib(void)
164 return unlock_ben(&av_thread_lib_ben
);
167 /* our versions of _init and _fini (which are called by those actually from crt.o) */
169 void initialize_after(void)
171 av_thread_lib_ben
.atom
= 0;
172 av_thread_lib_ben
.sem
= create_sem(0, "libavcodec benaphore");
175 void uninitialize_before(void)
177 delete_sem(av_thread_lib_ben
.sem
);