00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "thread.h"
00024
00025 #define WIN32_LEAN_AND_MEAN
00026 #include <windows.h>
00027 #include <process.h>
00028
00029 typedef struct ThreadContext{
00030 AVCodecContext *avctx;
00031 HANDLE thread;
00032 HANDLE work_sem;
00033 HANDLE job_sem;
00034 HANDLE done_sem;
00035 int (*func)(AVCodecContext *c, void *arg);
00036 int (*func2)(AVCodecContext *c, void *arg, int, int);
00037 void *arg;
00038 int argsize;
00039 int *jobnr;
00040 int *ret;
00041 int threadnr;
00042 }ThreadContext;
00043
00044
00045 static unsigned WINAPI attribute_align_arg thread_func(void *v){
00046 ThreadContext *c= v;
00047
00048 for(;;){
00049 int ret, jobnr;
00050
00051 WaitForSingleObject(c->work_sem, INFINITE);
00052
00053 if (!c->func && !c->func2)
00054 break;
00055 WaitForSingleObject(c->job_sem, INFINITE);
00056 jobnr = (*c->jobnr)++;
00057 ReleaseSemaphore(c->job_sem, 1, 0);
00058
00059 if(c->func)
00060 ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
00061 else
00062 ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
00063 if (c->ret)
00064 c->ret[jobnr] = ret;
00065
00066 ReleaseSemaphore(c->done_sem, 1, 0);
00067 }
00068
00069 return 0;
00070 }
00071
00076 void ff_thread_free(AVCodecContext *s){
00077 ThreadContext *c= s->thread_opaque;
00078 int i;
00079
00080 for(i=0; i<s->thread_count; i++){
00081
00082 c[i].func= NULL;
00083 c[i].func2= NULL;
00084 }
00085 ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
00086 for(i=0; i<s->thread_count; i++){
00087 WaitForSingleObject(c[i].thread, INFINITE);
00088 if(c[i].thread) CloseHandle(c[i].thread);
00089 }
00090 if(c[0].work_sem) CloseHandle(c[0].work_sem);
00091 if(c[0].job_sem) CloseHandle(c[0].job_sem);
00092 if(c[0].done_sem) CloseHandle(c[0].done_sem);
00093
00094 av_freep(&s->thread_opaque);
00095 }
00096
00097 static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00098 ThreadContext *c= s->thread_opaque;
00099 int i;
00100 int jobnr = 0;
00101
00102 assert(s == c->avctx);
00103
00104
00105
00106 for(i=0; i<s->thread_count; i++){
00107 c[i].arg= arg;
00108 c[i].argsize= size;
00109 c[i].func= func;
00110 c[i].ret= ret;
00111 c[i].jobnr = &jobnr;
00112 }
00113 ReleaseSemaphore(c[0].work_sem, count, 0);
00114 for(i=0; i<count; i++)
00115 WaitForSingleObject(c[0].done_sem, INFINITE);
00116
00117 return 0;
00118 }
00119
00120 static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
00121 ThreadContext *c= s->thread_opaque;
00122 int i;
00123 for(i=0; i<s->thread_count; i++)
00124 c[i].func2 = func;
00125 avcodec_thread_execute(s, NULL, arg, ret, count, 0);
00126 }
00127
00128 int ff_thread_init(AVCodecContext *s){
00129 int i;
00130 ThreadContext *c;
00131 uint32_t threadid;
00132
00133 if(!(s->thread_type & FF_THREAD_SLICE)){
00134 av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
00135 return 0;
00136 }
00137
00138 if (s->thread_count <= 1)
00139 return 0;
00140
00141 s->active_thread_type= FF_THREAD_SLICE;
00142
00143 assert(!s->thread_opaque);
00144 c= av_mallocz(sizeof(ThreadContext)*s->thread_count);
00145 s->thread_opaque= c;
00146 if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
00147 goto fail;
00148 if(!(c[0].job_sem = CreateSemaphore(NULL, 1, 1, NULL)))
00149 goto fail;
00150 if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
00151 goto fail;
00152
00153 for(i=0; i<s->thread_count; i++){
00154
00155 c[i].avctx= s;
00156 c[i].work_sem = c[0].work_sem;
00157 c[i].job_sem = c[0].job_sem;
00158 c[i].done_sem = c[0].done_sem;
00159 c[i].threadnr = i;
00160
00161
00162 c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
00163 if( !c[i].thread ) goto fail;
00164 }
00165
00166
00167 s->execute= avcodec_thread_execute;
00168 s->execute2= avcodec_thread_execute2;
00169
00170 return 0;
00171 fail:
00172 ff_thread_free(s);
00173 return -1;
00174 }