Statistics
| Branch: | Revision:

ffmpeg / libavcodec / w32thread.c @ 755bfeab

History | View | Annotate | Download (3.79 KB)

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

    
25
#define WIN32_LEAN_AND_MEAN
26
#include <windows.h>
27
#include <process.h>
28

    
29
typedef struct ThreadContext{
30
    AVCodecContext *avctx;
31
    HANDLE thread;
32
    HANDLE work_sem;
33
    HANDLE done_sem;
34
    int (*func)(AVCodecContext *c, void *arg);
35
    void *arg;
36
    int ret;
37
}ThreadContext;
38

    
39

    
40
static unsigned __stdcall thread_func(void *v){
41
    ThreadContext *c= v;
42

    
43
    for(;;){
44
//printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
45
        WaitForSingleObject(c->work_sem, INFINITE);
46
//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
47
        if(c->func)
48
            c->ret= c->func(c->avctx, c->arg);
49
        else
50
            return 0;
51
//printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
52
        ReleaseSemaphore(c->done_sem, 1, 0);
53
    }
54

    
55
    return 0;
56
}
57

    
58
/**
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.
61
 */
62
void avcodec_thread_free(AVCodecContext *s){
63
    ThreadContext *c= s->thread_opaque;
64
    int i;
65

    
66
    for(i=0; i<s->thread_count; i++){
67

    
68
        c[i].func= NULL;
69
        ReleaseSemaphore(c[i].work_sem, 1, 0);
70
        WaitForSingleObject(c[i].thread, INFINITE);
71
        if(c[i].work_sem) CloseHandle(c[i].work_sem);
72
        if(c[i].done_sem) CloseHandle(c[i].done_sem);
73
    }
74

    
75
    av_freep(&s->thread_opaque);
76
}
77

    
78
int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
79
    ThreadContext *c= s->thread_opaque;
80
    int i;
81

    
82
    assert(s == c->avctx);
83
    assert(count <= s->thread_count);
84

    
85
    /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
86

    
87
    for(i=0; i<count; i++){
88
        c[i].arg= arg[i];
89
        c[i].func= func;
90
        c[i].ret= 12345;
91

    
92
        ReleaseSemaphore(c[i].work_sem, 1, 0);
93
    }
94
    for(i=0; i<count; i++){
95
        WaitForSingleObject(c[i].done_sem, INFINITE);
96

    
97
        c[i].func= NULL;
98
        if(ret) ret[i]= c[i].ret;
99
    }
100
    return 0;
101
}
102

    
103
int avcodec_thread_init(AVCodecContext *s, int thread_count){
104
    int i;
105
    ThreadContext *c;
106
    uint32_t threadid;
107

    
108
    s->thread_count= thread_count;
109

    
110
    assert(!s->thread_opaque);
111
    c= av_mallocz(sizeof(ThreadContext)*thread_count);
112
    s->thread_opaque= c;
113

    
114
    for(i=0; i<thread_count; i++){
115
//printf("init semaphors %d\n", i); fflush(stdout);
116
        c[i].avctx= s;
117

    
118
        if(!(c[i].work_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL)))
119
            goto fail;
120
        if(!(c[i].done_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL)))
121
            goto fail;
122

    
123
//printf("create thread %d\n", i); fflush(stdout);
124
        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
125
        if( !c[i].thread ) goto fail;
126
    }
127
//printf("init done\n"); fflush(stdout);
128

    
129
    s->execute= avcodec_thread_execute;
130

    
131
    return 0;
132
fail:
133
    avcodec_thread_free(s);
134
    return -1;
135
}