use get_format
[libav.git] / libavcodec / pthread.c
1 /*
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19 #include <semaphore.h>
20 #include <pthread.h>
21
22 //#define DEBUG
23
24 #include "avcodec.h"
25 #include "common.h"
26
27
28 typedef struct ThreadContext{
29 AVCodecContext *avctx;
30 pthread_t thread;
31 sem_t work_sem;
32 sem_t done_sem;
33 int (*func)(AVCodecContext *c, void *arg);
34 void *arg;
35 int ret;
36 }ThreadContext;
37
38 static void * thread_func(void *v){
39 ThreadContext *c= v;
40
41 for(;;){
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);
45 if(c->func)
46 c->ret= c->func(c->avctx, c->arg);
47 else
48 return NULL;
49 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
50 sem_post(&c->done_sem);
51 }
52
53 return NULL;
54 }
55
56 /**
57 * free what has been allocated by avcodec_pthread_init().
58 * must be called after decoding has finished, especially dont call while avcodec_pthread_execute() is running
59 */
60 void avcodec_pthread_free(AVCodecContext *s){
61 ThreadContext *c= s->thread_opaque;
62 int i;
63
64 for(i=0; i<s->thread_count; i++){
65 int val;
66
67 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
68 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
69
70 c[i].func= NULL;
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);
75 }
76
77 av_freep(&s->thread_opaque);
78 }
79
80 int avcodec_pthread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
81 ThreadContext *c= s->thread_opaque;
82 int i, val;
83
84 assert(s == c->avctx);
85 assert(count <= s->thread_count);
86
87 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
88
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);
92
93 c[i].arg= arg[i];
94 c[i].func= func;
95 c[i].ret= 12345;
96 sem_post(&c[i].work_sem);
97 }
98 for(i=0; i<count; i++){
99 sem_wait(&c[i].done_sem);
100
101 sem_getvalue(&c[i].work_sem, &val); assert(val == 0);
102 sem_getvalue(&c[i].done_sem, &val); assert(val == 0);
103
104 c[i].func= NULL;
105 if(ret) ret[i]= c[i].ret;
106 }
107 return 0;
108 }
109
110 int avcodec_pthread_init(AVCodecContext *s, int thread_count){
111 int i;
112 ThreadContext *c;
113
114 s->thread_count= thread_count;
115
116 assert(!s->thread_opaque);
117 c= av_mallocz(sizeof(ThreadContext)*thread_count);
118 s->thread_opaque= c;
119
120 for(i=0; i<thread_count; i++){
121 //printf("init semaphors %d\n", i); fflush(stdout);
122 c[i].avctx= s;
123 if(sem_init(&c[i].work_sem, 0, 0))
124 goto fail;
125 if(sem_init(&c[i].done_sem, 0, 0))
126 goto fail;
127 //printf("create thread %d\n", i); fflush(stdout);
128 if(pthread_create(&c[i].thread, NULL, thread_func, &c[i]))
129 goto fail;
130 }
131 //printf("init done\n"); fflush(stdout);
132
133 s->execute= avcodec_pthread_execute;
134
135 return 0;
136 fail:
137 avcodec_pthread_free(s);
138 return -1;
139 }