Statistics
| Branch: | Revision:

ffmpeg / libavcodec / w32thread.c @ 043d2ff2

History | View | Annotate | Download (5.25 KB)

1 16806499 Michael Niedermayer
/*
2
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 16806499 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 16806499 Michael Niedermayer
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 16806499 Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 16806499 Michael Niedermayer
 */
20
//#define DEBUG
21
22
#include "avcodec.h"
23
24
#define WIN32_LEAN_AND_MEAN
25
#include <windows.h>
26
#include <process.h>
27
28
typedef struct ThreadContext{
29
    AVCodecContext *avctx;
30
    HANDLE thread;
31
    HANDLE work_sem;
32 1e6ff470 Reimar Döffinger
    HANDLE job_sem;
33 16806499 Michael Niedermayer
    HANDLE done_sem;
34
    int (*func)(AVCodecContext *c, void *arg);
35 7537726f Reimar Döffinger
    int (*func2)(AVCodecContext *c, void *arg, int, int);
36 16806499 Michael Niedermayer
    void *arg;
37 1e6ff470 Reimar Döffinger
    int argsize;
38
    int *jobnr;
39
    int *ret;
40 7537726f Reimar Döffinger
    int threadnr;
41 16806499 Michael Niedermayer
}ThreadContext;
42
43
44 5dea9409 Ramiro Polla
static unsigned WINAPI attribute_align_arg thread_func(void *v){
45 16806499 Michael Niedermayer
    ThreadContext *c= v;
46
47
    for(;;){
48 1e6ff470 Reimar Döffinger
        int ret, jobnr;
49 16806499 Michael Niedermayer
//printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
50
        WaitForSingleObject(c->work_sem, INFINITE);
51 9fe08942 Reimar Döffinger
        // avoid trying to access jobnr if we should quit
52
        if (!c->func && !c->func2)
53
            break;
54 1e6ff470 Reimar Döffinger
        WaitForSingleObject(c->job_sem, INFINITE);
55
        jobnr = (*c->jobnr)++;
56
        ReleaseSemaphore(c->job_sem, 1, 0);
57 16806499 Michael Niedermayer
//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
58
        if(c->func)
59 1e6ff470 Reimar Döffinger
            ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
60 16806499 Michael Niedermayer
        else
61 9fe08942 Reimar Döffinger
            ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
62 1e6ff470 Reimar Döffinger
        if (c->ret)
63
            c->ret[jobnr] = ret;
64 16806499 Michael Niedermayer
//printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
65
        ReleaseSemaphore(c->done_sem, 1, 0);
66
    }
67 115329f1 Diego Biurrun
68 16806499 Michael Niedermayer
    return 0;
69
}
70
71
/**
72 043d2ff2 Alexander Strange
 * Free what has been allocated by ff_thread_init().
73 755bfeab Diego Biurrun
 * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
74 16806499 Michael Niedermayer
 */
75 043d2ff2 Alexander Strange
void ff_thread_free(AVCodecContext *s){
76 16806499 Michael Niedermayer
    ThreadContext *c= s->thread_opaque;
77
    int i;
78
79
    for(i=0; i<s->thread_count; i++){
80 115329f1 Diego Biurrun
81 16806499 Michael Niedermayer
        c[i].func= NULL;
82 7537726f Reimar Döffinger
        c[i].func2= NULL;
83 1e6ff470 Reimar Döffinger
    }
84
    ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
85
    for(i=0; i<s->thread_count; i++){
86 16806499 Michael Niedermayer
        WaitForSingleObject(c[i].thread, INFINITE);
87 51fdb6f0 Shehzad Salim
        if(c[i].thread)   CloseHandle(c[i].thread);
88 16806499 Michael Niedermayer
    }
89 1e6ff470 Reimar Döffinger
    if(c[0].work_sem) CloseHandle(c[0].work_sem);
90
    if(c[0].job_sem)  CloseHandle(c[0].job_sem);
91
    if(c[0].done_sem) CloseHandle(c[0].done_sem);
92 16806499 Michael Niedermayer
93
    av_freep(&s->thread_opaque);
94
}
95
96 4a287145 Benoit Fouet
static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
97 16806499 Michael Niedermayer
    ThreadContext *c= s->thread_opaque;
98
    int i;
99 1e6ff470 Reimar Döffinger
    int jobnr = 0;
100 115329f1 Diego Biurrun
101 16806499 Michael Niedermayer
    assert(s == c->avctx);
102 115329f1 Diego Biurrun
103 16806499 Michael Niedermayer
    /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
104
105 1e6ff470 Reimar Döffinger
    for(i=0; i<s->thread_count; i++){
106
        c[i].arg= arg;
107
        c[i].argsize= size;
108 16806499 Michael Niedermayer
        c[i].func= func;
109 1e6ff470 Reimar Döffinger
        c[i].ret= ret;
110
        c[i].jobnr = &jobnr;
111 16806499 Michael Niedermayer
    }
112 1e6ff470 Reimar Döffinger
    ReleaseSemaphore(c[0].work_sem, count, 0);
113
    for(i=0; i<count; i++)
114
        WaitForSingleObject(c[0].done_sem, INFINITE);
115 115329f1 Diego Biurrun
116 16806499 Michael Niedermayer
    return 0;
117
}
118
119 9b066ecb Andreas Öman
static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
120 7537726f Reimar Döffinger
    ThreadContext *c= s->thread_opaque;
121
    int i;
122
    for(i=0; i<s->thread_count; i++)
123
        c[i].func2 = func;
124
    avcodec_thread_execute(s, NULL, arg, ret, count, 0);
125
}
126
127 043d2ff2 Alexander Strange
int ff_thread_init(AVCodecContext *s, int thread_count){
128 16806499 Michael Niedermayer
    int i;
129
    ThreadContext *c;
130
    uint32_t threadid;
131
132 b38f008e Alexander Strange
    if(!(s->thread_type & FF_THREAD_SLICE)){
133
        av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
134
        return 0;
135
    }
136
137 16806499 Michael Niedermayer
    s->thread_count= thread_count;
138 b38f008e Alexander Strange
    s->active_thread_type= FF_THREAD_SLICE;
139 16806499 Michael Niedermayer
140 68cf92ee Måns Rullgård
    if (thread_count <= 1)
141
        return 0;
142
143 16806499 Michael Niedermayer
    assert(!s->thread_opaque);
144
    c= av_mallocz(sizeof(ThreadContext)*thread_count);
145
    s->thread_opaque= c;
146 1e6ff470 Reimar Döffinger
    if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
147
        goto fail;
148
    if(!(c[0].job_sem  = CreateSemaphore(NULL, 1, 1, NULL)))
149
        goto fail;
150
    if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
151
        goto fail;
152 115329f1 Diego Biurrun
153 16806499 Michael Niedermayer
    for(i=0; i<thread_count; i++){
154
//printf("init semaphors %d\n", i); fflush(stdout);
155
        c[i].avctx= s;
156 1e6ff470 Reimar Döffinger
        c[i].work_sem = c[0].work_sem;
157
        c[i].job_sem  = c[0].job_sem;
158
        c[i].done_sem = c[0].done_sem;
159 7537726f Reimar Döffinger
        c[i].threadnr = i;
160 16806499 Michael Niedermayer
161
//printf("create thread %d\n", i); fflush(stdout);
162
        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
163
        if( !c[i].thread ) goto fail;
164
    }
165
//printf("init done\n"); fflush(stdout);
166 115329f1 Diego Biurrun
167 16806499 Michael Niedermayer
    s->execute= avcodec_thread_execute;
168 7537726f Reimar Döffinger
    s->execute2= avcodec_thread_execute2;
169 16806499 Michael Niedermayer
170
    return 0;
171
fail:
172 043d2ff2 Alexander Strange
    ff_thread_free(s);
173 16806499 Michael Niedermayer
    return -1;
174
}