Mercurial > audlegacy-plugins
comparison src/ffmpeg/libavcodec/w32thread.c @ 808:e8776388b02a trunk
[svn] - add ffmpeg
| author | nenolod |
|---|---|
| date | Mon, 12 Mar 2007 11:18:54 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 807:0f9c8d4d3ac4 | 808:e8776388b02a |
|---|---|
| 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 */ | |
| 21 //#define DEBUG | |
| 22 | |
| 23 #include "avcodec.h" | |
| 24 #include "common.h" | |
| 25 | |
| 26 #define WIN32_LEAN_AND_MEAN | |
| 27 #include <windows.h> | |
| 28 #include <process.h> | |
| 29 | |
| 30 typedef struct ThreadContext{ | |
| 31 AVCodecContext *avctx; | |
| 32 HANDLE thread; | |
| 33 HANDLE work_sem; | |
| 34 HANDLE done_sem; | |
| 35 int (*func)(AVCodecContext *c, void *arg); | |
| 36 void *arg; | |
| 37 int ret; | |
| 38 }ThreadContext; | |
| 39 | |
| 40 | |
| 41 static unsigned __stdcall thread_func(void *v){ | |
| 42 ThreadContext *c= v; | |
| 43 | |
| 44 for(;;){ | |
| 45 //printf("thread_func %X enter wait\n", (int)v); fflush(stdout); | |
| 46 WaitForSingleObject(c->work_sem, INFINITE); | |
| 47 //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout); | |
| 48 if(c->func) | |
| 49 c->ret= c->func(c->avctx, c->arg); | |
| 50 else | |
| 51 return 0; | |
| 52 //printf("thread_func %X signal complete\n", (int)v); fflush(stdout); | |
| 53 ReleaseSemaphore(c->done_sem, 1, 0); | |
| 54 } | |
| 55 | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * free what has been allocated by avcodec_thread_init(). | |
| 61 * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running | |
| 62 */ | |
| 63 void avcodec_thread_free(AVCodecContext *s){ | |
| 64 ThreadContext *c= s->thread_opaque; | |
| 65 int i; | |
| 66 | |
| 67 for(i=0; i<s->thread_count; i++){ | |
| 68 | |
| 69 c[i].func= NULL; | |
| 70 ReleaseSemaphore(c[i].work_sem, 1, 0); | |
| 71 WaitForSingleObject(c[i].thread, INFINITE); | |
| 72 if(c[i].work_sem) CloseHandle(c[i].work_sem); | |
| 73 if(c[i].done_sem) CloseHandle(c[i].done_sem); | |
| 74 } | |
| 75 | |
| 76 av_freep(&s->thread_opaque); | |
| 77 } | |
| 78 | |
| 79 int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){ | |
| 80 ThreadContext *c= s->thread_opaque; | |
| 81 int i; | |
| 82 | |
| 83 assert(s == c->avctx); | |
| 84 assert(count <= s->thread_count); | |
| 85 | |
| 86 /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */ | |
| 87 | |
| 88 for(i=0; i<count; i++){ | |
| 89 c[i].arg= arg[i]; | |
| 90 c[i].func= func; | |
| 91 c[i].ret= 12345; | |
| 92 | |
| 93 ReleaseSemaphore(c[i].work_sem, 1, 0); | |
| 94 } | |
| 95 for(i=0; i<count; i++){ | |
| 96 WaitForSingleObject(c[i].done_sem, INFINITE); | |
| 97 | |
| 98 c[i].func= NULL; | |
| 99 if(ret) ret[i]= c[i].ret; | |
| 100 } | |
| 101 return 0; | |
| 102 } | |
| 103 | |
| 104 int avcodec_thread_init(AVCodecContext *s, int thread_count){ | |
| 105 int i; | |
| 106 ThreadContext *c; | |
| 107 uint32_t threadid; | |
| 108 | |
| 109 s->thread_count= thread_count; | |
| 110 | |
| 111 assert(!s->thread_opaque); | |
| 112 c= av_mallocz(sizeof(ThreadContext)*thread_count); | |
| 113 s->thread_opaque= c; | |
| 114 | |
| 115 for(i=0; i<thread_count; i++){ | |
| 116 //printf("init semaphors %d\n", i); fflush(stdout); | |
| 117 c[i].avctx= s; | |
| 118 | |
| 119 if(!(c[i].work_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL))) | |
| 120 goto fail; | |
| 121 if(!(c[i].done_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL))) | |
| 122 goto fail; | |
| 123 | |
| 124 //printf("create thread %d\n", i); fflush(stdout); | |
| 125 c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid ); | |
| 126 if( !c[i].thread ) goto fail; | |
| 127 } | |
| 128 //printf("init done\n"); fflush(stdout); | |
| 129 | |
| 130 s->execute= avcodec_thread_execute; | |
| 131 | |
| 132 return 0; | |
| 133 fail: | |
| 134 avcodec_thread_free(s); | |
| 135 return -1; | |
| 136 } |
