]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/w32thread.c
Implement ff_scale_vector_to_given_sum_of_squares()
[frescor/ffmpeg.git] / libavcodec / w32thread.c
1 /*
2  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 //#define DEBUG
21
22 #include "avcodec.h"
23
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26 #include <process.h>
27
28 typedef struct ThreadContext{
29     AVCodecContext *avctx;
30     HANDLE thread;
31     HANDLE work_sem;
32     HANDLE job_sem;
33     HANDLE done_sem;
34     int (*func)(AVCodecContext *c, void *arg);
35     int (*func2)(AVCodecContext *c, void *arg, int, int);
36     void *arg;
37     int argsize;
38     int *jobnr;
39     int *ret;
40     int threadnr;
41 }ThreadContext;
42
43
44 static unsigned WINAPI attribute_align_arg thread_func(void *v){
45     ThreadContext *c= v;
46
47     for(;;){
48         int ret, jobnr;
49 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
50         WaitForSingleObject(c->work_sem, INFINITE);
51         WaitForSingleObject(c->job_sem, INFINITE);
52         jobnr = (*c->jobnr)++;
53         ReleaseSemaphore(c->job_sem, 1, 0);
54 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
55         if(c->func)
56             ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
57         else if (c->func2)
58             ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
59         else
60             return 0;
61         if (c->ret)
62             c->ret[jobnr] = ret;
63 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
64         ReleaseSemaphore(c->done_sem, 1, 0);
65     }
66
67     return 0;
68 }
69
70 /**
71  * Free what has been allocated by avcodec_thread_init().
72  * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
73  */
74 void avcodec_thread_free(AVCodecContext *s){
75     ThreadContext *c= s->thread_opaque;
76     int i;
77
78     for(i=0; i<s->thread_count; i++){
79
80         c[i].func= NULL;
81         c[i].func2= NULL;
82     }
83     ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
84     for(i=0; i<s->thread_count; i++){
85         WaitForSingleObject(c[i].thread, INFINITE);
86         if(c[i].thread)   CloseHandle(c[i].thread);
87     }
88     if(c[0].work_sem) CloseHandle(c[0].work_sem);
89     if(c[0].job_sem)  CloseHandle(c[0].job_sem);
90     if(c[0].done_sem) CloseHandle(c[0].done_sem);
91
92     av_freep(&s->thread_opaque);
93 }
94
95 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
96     ThreadContext *c= s->thread_opaque;
97     int i;
98     int jobnr = 0;
99
100     assert(s == c->avctx);
101
102     /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
103
104     for(i=0; i<s->thread_count; i++){
105         c[i].arg= arg;
106         c[i].argsize= size;
107         c[i].func= func;
108         c[i].ret= ret;
109         c[i].jobnr = &jobnr;
110     }
111     ReleaseSemaphore(c[0].work_sem, count, 0);
112     for(i=0; i<count; i++)
113         WaitForSingleObject(c[0].done_sem, INFINITE);
114
115     return 0;
116 }
117
118 int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
119     ThreadContext *c= s->thread_opaque;
120     int i;
121     for(i=0; i<s->thread_count; i++)
122         c[i].func2 = func;
123     avcodec_thread_execute(s, NULL, arg, ret, count, 0);
124 }
125
126 int avcodec_thread_init(AVCodecContext *s, int thread_count){
127     int i;
128     ThreadContext *c;
129     uint32_t threadid;
130
131     s->thread_count= thread_count;
132
133     assert(!s->thread_opaque);
134     c= av_mallocz(sizeof(ThreadContext)*thread_count);
135     s->thread_opaque= c;
136     if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
137         goto fail;
138     if(!(c[0].job_sem  = CreateSemaphore(NULL, 1, 1, NULL)))
139         goto fail;
140     if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
141         goto fail;
142
143     for(i=0; i<thread_count; i++){
144 //printf("init semaphors %d\n", i); fflush(stdout);
145         c[i].avctx= s;
146         c[i].work_sem = c[0].work_sem;
147         c[i].job_sem  = c[0].job_sem;
148         c[i].done_sem = c[0].done_sem;
149         c[i].threadnr = i;
150
151 //printf("create thread %d\n", i); fflush(stdout);
152         c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
153         if( !c[i].thread ) goto fail;
154     }
155 //printf("init done\n"); fflush(stdout);
156
157     s->execute= avcodec_thread_execute;
158     s->execute2= avcodec_thread_execute2;
159
160     return 0;
161 fail:
162     avcodec_thread_free(s);
163     return -1;
164 }