Mercurial > libavcodec.hg
annotate w32thread.c @ 5197:953a9fd3b5f5 libavcodec
Ignore .ho files.
author | diego |
---|---|
date | Mon, 02 Jul 2007 10:13:19 +0000 |
parents | 4dbe6578f811 |
children | 2b72f9bc4f06 |
rev | line source |
---|---|
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
1 /* |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
3 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
4 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
5 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
6 * FFmpeg is free software; you can redistribute it and/or |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
7 * modify it under the terms of the GNU Lesser General Public |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
8 * License as published by the Free Software Foundation; either |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
9 * version 2.1 of the License, or (at your option) any later version. |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
10 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
11 * FFmpeg is distributed in the hope that it will be useful, |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
14 * Lesser General Public License for more details. |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
15 * |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
16 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
17 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
19 * |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
20 */ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
21 //#define DEBUG |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
22 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
23 #include "avcodec.h" |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
24 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
25 #define WIN32_LEAN_AND_MEAN |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
26 #include <windows.h> |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
27 #include <process.h> |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
28 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
29 typedef struct ThreadContext{ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
30 AVCodecContext *avctx; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
31 HANDLE thread; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
32 HANDLE work_sem; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
33 HANDLE done_sem; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
34 int (*func)(AVCodecContext *c, void *arg); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
35 void *arg; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
36 int ret; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
37 }ThreadContext; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
38 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
39 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
40 static unsigned __stdcall thread_func(void *v){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
41 ThreadContext *c= v; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
42 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
43 for(;;){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
44 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
45 WaitForSingleObject(c->work_sem, INFINITE); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
46 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
47 if(c->func) |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
48 c->ret= c->func(c->avctx, c->arg); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
49 else |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
50 return 0; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
51 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
52 ReleaseSemaphore(c->done_sem, 1, 0); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
53 } |
2967 | 54 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
55 return 0; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
56 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
57 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
58 /** |
5127 | 59 * Free what has been allocated by avcodec_thread_init(). |
60 * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running. | |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
61 */ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
62 void avcodec_thread_free(AVCodecContext *s){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
63 ThreadContext *c= s->thread_opaque; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
64 int i; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
65 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
66 for(i=0; i<s->thread_count; i++){ |
2967 | 67 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
68 c[i].func= NULL; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
69 ReleaseSemaphore(c[i].work_sem, 1, 0); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
70 WaitForSingleObject(c[i].thread, INFINITE); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
71 if(c[i].work_sem) CloseHandle(c[i].work_sem); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
72 if(c[i].done_sem) CloseHandle(c[i].done_sem); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
73 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
74 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
75 av_freep(&s->thread_opaque); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
76 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
77 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
78 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
79 ThreadContext *c= s->thread_opaque; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
80 int i; |
2967 | 81 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
82 assert(s == c->avctx); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
83 assert(count <= s->thread_count); |
2967 | 84 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
85 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
86 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
87 for(i=0; i<count; i++){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
88 c[i].arg= arg[i]; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
89 c[i].func= func; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
90 c[i].ret= 12345; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
91 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
92 ReleaseSemaphore(c[i].work_sem, 1, 0); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
93 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
94 for(i=0; i<count; i++){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
95 WaitForSingleObject(c[i].done_sem, INFINITE); |
2967 | 96 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
97 c[i].func= NULL; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
98 if(ret) ret[i]= c[i].ret; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
99 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
100 return 0; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
101 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
102 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
103 int avcodec_thread_init(AVCodecContext *s, int thread_count){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
104 int i; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
105 ThreadContext *c; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
106 uint32_t threadid; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
107 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
108 s->thread_count= thread_count; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
109 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
110 assert(!s->thread_opaque); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
111 c= av_mallocz(sizeof(ThreadContext)*thread_count); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
112 s->thread_opaque= c; |
2967 | 113 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
114 for(i=0; i<thread_count; i++){ |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
115 //printf("init semaphors %d\n", i); fflush(stdout); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
116 c[i].avctx= s; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
117 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
118 if(!(c[i].work_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL))) |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
119 goto fail; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
120 if(!(c[i].done_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL))) |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
121 goto fail; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
122 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
123 //printf("create thread %d\n", i); fflush(stdout); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
124 c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid ); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
125 if( !c[i].thread ) goto fail; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
126 } |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
127 //printf("init done\n"); fflush(stdout); |
2967 | 128 |
1822
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
129 s->execute= avcodec_thread_execute; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
130 |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
131 return 0; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
132 fail: |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
133 avcodec_thread_free(s); |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
134 return -1; |
7366bb5c363f
w32threads by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
diff
changeset
|
135 } |