Statistics
| Branch: | Revision:

ffmpeg / libavcodec / w32thread.c @ 2912e87a

History | View | Annotate | Download (5.27 KB)

1
/*
2
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav 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
 * Libav 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 Libav; 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
#include "thread.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 job_sem;
34
    HANDLE done_sem;
35
    int (*func)(AVCodecContext *c, void *arg);
36
    int (*func2)(AVCodecContext *c, void *arg, int, int);
37
    void *arg;
38
    int argsize;
39
    int *jobnr;
40
    int *ret;
41
    int threadnr;
42
}ThreadContext;
43

    
44

    
45
static unsigned WINAPI attribute_align_arg thread_func(void *v){
46
    ThreadContext *c= v;
47

    
48
    for(;;){
49
        int ret, jobnr;
50
//printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
51
        WaitForSingleObject(c->work_sem, INFINITE);
52
        // avoid trying to access jobnr if we should quit
53
        if (!c->func && !c->func2)
54
            break;
55
        WaitForSingleObject(c->job_sem, INFINITE);
56
        jobnr = (*c->jobnr)++;
57
        ReleaseSemaphore(c->job_sem, 1, 0);
58
//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
59
        if(c->func)
60
            ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
61
        else
62
            ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
63
        if (c->ret)
64
            c->ret[jobnr] = ret;
65
//printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
66
        ReleaseSemaphore(c->done_sem, 1, 0);
67
    }
68

    
69
    return 0;
70
}
71

    
72
/**
73
 * Free what has been allocated by ff_thread_init().
74
 * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
75
 */
76
void ff_thread_free(AVCodecContext *s){
77
    ThreadContext *c= s->thread_opaque;
78
    int i;
79

    
80
    for(i=0; i<s->thread_count; i++){
81

    
82
        c[i].func= NULL;
83
        c[i].func2= NULL;
84
    }
85
    ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
86
    for(i=0; i<s->thread_count; i++){
87
        WaitForSingleObject(c[i].thread, INFINITE);
88
        if(c[i].thread)   CloseHandle(c[i].thread);
89
    }
90
    if(c[0].work_sem) CloseHandle(c[0].work_sem);
91
    if(c[0].job_sem)  CloseHandle(c[0].job_sem);
92
    if(c[0].done_sem) CloseHandle(c[0].done_sem);
93

    
94
    av_freep(&s->thread_opaque);
95
}
96

    
97
static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
98
    ThreadContext *c= s->thread_opaque;
99
    int i;
100
    int jobnr = 0;
101

    
102
    assert(s == c->avctx);
103

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

    
106
    for(i=0; i<s->thread_count; i++){
107
        c[i].arg= arg;
108
        c[i].argsize= size;
109
        c[i].func= func;
110
        c[i].ret= ret;
111
        c[i].jobnr = &jobnr;
112
    }
113
    ReleaseSemaphore(c[0].work_sem, count, 0);
114
    for(i=0; i<count; i++)
115
        WaitForSingleObject(c[0].done_sem, INFINITE);
116

    
117
    return 0;
118
}
119

    
120
static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
121
    ThreadContext *c= s->thread_opaque;
122
    int i;
123
    for(i=0; i<s->thread_count; i++)
124
        c[i].func2 = func;
125
    avcodec_thread_execute(s, NULL, arg, ret, count, 0);
126
}
127

    
128
int ff_thread_init(AVCodecContext *s, int thread_count){
129
    int i;
130
    ThreadContext *c;
131
    uint32_t threadid;
132

    
133
    if(!(s->thread_type & FF_THREAD_SLICE)){
134
        av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
135
        return 0;
136
    }
137

    
138
    s->thread_count= thread_count;
139
    s->active_thread_type= FF_THREAD_SLICE;
140

    
141
    if (thread_count <= 1)
142
        return 0;
143

    
144
    assert(!s->thread_opaque);
145
    c= av_mallocz(sizeof(ThreadContext)*thread_count);
146
    s->thread_opaque= c;
147
    if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
148
        goto fail;
149
    if(!(c[0].job_sem  = CreateSemaphore(NULL, 1, 1, NULL)))
150
        goto fail;
151
    if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
152
        goto fail;
153

    
154
    for(i=0; i<thread_count; i++){
155
//printf("init semaphors %d\n", i); fflush(stdout);
156
        c[i].avctx= s;
157
        c[i].work_sem = c[0].work_sem;
158
        c[i].job_sem  = c[0].job_sem;
159
        c[i].done_sem = c[0].done_sem;
160
        c[i].threadnr = i;
161

    
162
//printf("create thread %d\n", i); fflush(stdout);
163
        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
164
        if( !c[i].thread ) goto fail;
165
    }
166
//printf("init done\n"); fflush(stdout);
167

    
168
    s->execute= avcodec_thread_execute;
169
    s->execute2= avcodec_thread_execute2;
170

    
171
    return 0;
172
fail:
173
    ff_thread_free(s);
174
    return -1;
175
}