Libav
|
00001 /* 00002 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 //#define DEBUG 00021 00022 // Ported by Vlad Stelmahovsky 00023 00024 #include "avcodec.h" 00025 00026 #define INCL_DOS 00027 #define INCL_DOSERRORS 00028 #define INCL_DOSDEVIOCTL 00029 #include <os2.h> 00030 00031 typedef struct ThreadContext{ 00032 AVCodecContext *avctx; 00033 int thread; 00034 HEV work_sem; 00035 HEV done_sem; 00036 int (*func)(AVCodecContext *c, void *arg); 00037 void *arg; 00038 int ret; 00039 }ThreadContext; 00040 00041 00042 static void attribute_align_arg thread_func(void *v){ 00043 ThreadContext *c= v; 00044 00045 for(;;){ 00046 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout); 00047 DosWaitEventSem(c->work_sem, SEM_INDEFINITE_WAIT); 00048 // WaitForSingleObject(c->work_sem, INFINITE); 00049 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout); 00050 if(c->func) 00051 c->ret= c->func(c->avctx, c->arg); 00052 else 00053 return; 00054 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout); 00055 DosPostEventSem(c->done_sem); 00056 // ReleaseSemaphore(c->done_sem, 1, 0); 00057 } 00058 00059 return; 00060 } 00061 00066 void avcodec_thread_free(AVCodecContext *s){ 00067 ThreadContext *c= s->thread_opaque; 00068 int i; 00069 00070 for(i=0; i<s->thread_count; i++){ 00071 00072 c[i].func= NULL; 00073 DosPostEventSem(c[i].work_sem); 00074 // ReleaseSemaphore(c[i].work_sem, 1, 0); 00075 DosWaitThread((PTID)&c[i].thread,DCWW_WAIT); 00076 // WaitForSingleObject(c[i].thread, INFINITE); 00077 if(c[i].work_sem) DosCloseEventSem(c[i].work_sem);//CloseHandle(c[i].work_sem); 00078 if(c[i].done_sem) DosCloseEventSem(c[i].done_sem);//CloseHandle(c[i].done_sem); 00079 } 00080 00081 av_freep(&s->thread_opaque); 00082 } 00083 00084 static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ 00085 ThreadContext *c= s->thread_opaque; 00086 int i; 00087 00088 assert(s == c->avctx); 00089 assert(count <= s->thread_count); 00090 00091 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */ 00092 00093 for(i=0; i<count; i++){ 00094 00095 c[i].arg= (char*)arg + i*size; 00096 c[i].func= func; 00097 c[i].ret= 12345; 00098 00099 DosPostEventSem(c[i].work_sem); 00100 // ReleaseSemaphore(c[i].work_sem, 1, 0); 00101 } 00102 for(i=0; i<count; i++){ 00103 DosWaitEventSem(c[i].done_sem,SEM_INDEFINITE_WAIT); 00104 // WaitForSingleObject(c[i].done_sem, INFINITE); 00105 00106 c[i].func= NULL; 00107 if(ret) ret[i]= c[i].ret; 00108 } 00109 return 0; 00110 } 00111 00112 int avcodec_thread_init(AVCodecContext *s, int thread_count){ 00113 int i; 00114 ThreadContext *c; 00115 uint32_t threadid; 00116 00117 s->thread_count= thread_count; 00118 00119 if (thread_count <= 1) 00120 return 0; 00121 00122 assert(!s->thread_opaque); 00123 c= av_mallocz(sizeof(ThreadContext)*thread_count); 00124 s->thread_opaque= c; 00125 00126 for(i=0; i<thread_count; i++){ 00127 //printf("init semaphors %d\n", i); fflush(stdout); 00128 c[i].avctx= s; 00129 00130 if (DosCreateEventSem(NULL,&c[i].work_sem,DC_SEM_SHARED,0)) 00131 goto fail; 00132 if (DosCreateEventSem(NULL,&c[i].done_sem,DC_SEM_SHARED,0)) 00133 goto fail; 00134 00135 //printf("create thread %d\n", i); fflush(stdout); 00136 // c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid ); 00137 c[i].thread = _beginthread(thread_func, NULL, 0x10000, &c[i]); 00138 if( c[i].thread <= 0 ) goto fail; 00139 } 00140 //printf("init done\n"); fflush(stdout); 00141 00142 s->execute= avcodec_thread_execute; 00143 00144 return 0; 00145 fail: 00146 avcodec_thread_free(s); 00147 return -1; 00148 }