Statistics
| Branch: | Revision:

ffmpeg / libavcodec / beosthread.c @ 4a287145

History | View | Annotate | Download (4.76 KB)

1 2450cff2 François Revol
/*
2
 * Copyright (c) 2004 François Revol <revol@free.fr>
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 2450cff2 François Revol
 * 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 2450cff2 François Revol
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 2450cff2 François Revol
 * 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 2450cff2 François Revol
 */
20
//#define DEBUG
21
22
#include "avcodec.h"
23
24
#include <OS.h>
25
26
typedef struct ThreadContext{
27
    AVCodecContext *avctx;
28
    thread_id thread;
29
    sem_id work_sem;
30
    sem_id done_sem;
31
    int (*func)(AVCodecContext *c, void *arg);
32
    void *arg;
33
    int ret;
34
}ThreadContext;
35
36 f4ff1b92 François Revol
// it's odd Be never patented that :D
37
struct benaphore {
38 bb270c08 Diego Biurrun
        vint32 atom;
39
        sem_id sem;
40 f4ff1b92 François Revol
};
41
static inline int lock_ben(struct benaphore *ben)
42
{
43 bb270c08 Diego Biurrun
        if (atomic_add(&ben->atom, 1) > 0)
44
                return acquire_sem(ben->sem);
45
        return B_OK;
46 f4ff1b92 François Revol
}
47
static inline int unlock_ben(struct benaphore *ben)
48
{
49 bb270c08 Diego Biurrun
        if (atomic_add(&ben->atom, -1) > 1)
50
                return release_sem(ben->sem);
51
        return B_OK;
52 f4ff1b92 François Revol
}
53
54
static struct benaphore av_thread_lib_ben;
55 2450cff2 François Revol
56
static int32 ff_thread_func(void *v){
57
    ThreadContext *c= v;
58
59
    for(;;){
60
//printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
61
        acquire_sem(c->work_sem);
62
//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
63
        if(c->func)
64
            c->ret= c->func(c->avctx, c->arg);
65
        else
66
            return 0;
67
//printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
68
        release_sem(c->done_sem);
69
    }
70 115329f1 Diego Biurrun
71 2450cff2 François Revol
    return B_OK;
72
}
73
74
/**
75 755bfeab Diego Biurrun
 * Free what has been allocated by avcodec_thread_init().
76
 * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
77 2450cff2 François Revol
 */
78
void avcodec_thread_free(AVCodecContext *s){
79
    ThreadContext *c= s->thread_opaque;
80
    int i;
81
    int32 ret;
82
83
    for(i=0; i<s->thread_count; i++){
84 115329f1 Diego Biurrun
85 2450cff2 François Revol
        c[i].func= NULL;
86
        release_sem(c[i].work_sem);
87
        wait_for_thread(c[i].thread, &ret);
88
        if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
89
        if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
90
    }
91
92
    av_freep(&s->thread_opaque);
93
}
94
95 4a287145 Benoit Fouet
static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
96 2450cff2 François Revol
    ThreadContext *c= s->thread_opaque;
97
    int i;
98 115329f1 Diego Biurrun
99 2450cff2 François Revol
    assert(s == c->avctx);
100
    assert(count <= s->thread_count);
101 115329f1 Diego Biurrun
102 2450cff2 François Revol
    /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
103
104
    for(i=0; i<count; i++){
105 3a84713a Roman Shaposhnik
        c[i].arg= (char*)arg + i*size;
106 2450cff2 François Revol
        c[i].func= func;
107
        c[i].ret= 12345;
108
109
        release_sem(c[i].work_sem);
110
    }
111
    for(i=0; i<count; i++){
112
        acquire_sem(c[i].done_sem);
113 115329f1 Diego Biurrun
114 2450cff2 François Revol
        c[i].func= NULL;
115
        if(ret) ret[i]= c[i].ret;
116
    }
117
    return 0;
118
}
119
120
int avcodec_thread_init(AVCodecContext *s, int thread_count){
121
    int i;
122
    ThreadContext *c;
123
124
    s->thread_count= thread_count;
125
126 68cf92ee Måns Rullgård
    if (thread_count <= 1)
127
        return 0;
128
129 2450cff2 François Revol
    assert(!s->thread_opaque);
130
    c= av_mallocz(sizeof(ThreadContext)*thread_count);
131
    s->thread_opaque= c;
132 115329f1 Diego Biurrun
133 2450cff2 François Revol
    for(i=0; i<thread_count; i++){
134
//printf("init semaphors %d\n", i); fflush(stdout);
135
        c[i].avctx= s;
136
137
        if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
138
            goto fail;
139
        if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
140
            goto fail;
141
142
//printf("create thread %d\n", i); fflush(stdout);
143
        c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
144
        if( c[i].thread < B_OK ) goto fail;
145
        resume_thread(c[i].thread );
146
    }
147
//printf("init done\n"); fflush(stdout);
148 115329f1 Diego Biurrun
149 2450cff2 François Revol
    s->execute= avcodec_thread_execute;
150
151
    return 0;
152
fail:
153
    avcodec_thread_free(s);
154
    return -1;
155
}
156 f4ff1b92 François Revol
157
/* provide a mean to serialize calls to avcodec_*() for thread safety. */
158
159
int avcodec_thread_lock_lib(void)
160
{
161 bb270c08 Diego Biurrun
        return lock_ben(&av_thread_lib_ben);
162 f4ff1b92 François Revol
}
163
164
int avcodec_thread_unlock_lib(void)
165
{
166 bb270c08 Diego Biurrun
        return unlock_ben(&av_thread_lib_ben);
167 f4ff1b92 François Revol
}
168
169
/* our versions of _init and _fini (which are called by those actually from crt.o) */
170
171
void initialize_after(void)
172
{
173 bb270c08 Diego Biurrun
        av_thread_lib_ben.atom = 0;
174
        av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
175 f4ff1b92 François Revol
}
176
177
void uninitialize_before(void)
178
{
179 bb270c08 Diego Biurrun
        delete_sem(av_thread_lib_ben.sem);
180 f4ff1b92 François Revol
}
181
182