avcodec/pthread_slice: Remove rets_count
[ffmpeg.git] / libavcodec / pthread_slice.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * Slice multithreading support functions
22  * @see doc/multithreading.txt
23  */
24
25 #include "config.h"
26
27 #if HAVE_PTHREADS
28 #include <pthread.h>
29 #elif HAVE_W32THREADS
30 #include "compat/w32pthreads.h"
31 #elif HAVE_OS2THREADS
32 #include "compat/os2threads.h"
33 #endif
34
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "pthread_internal.h"
38 #include "thread.h"
39
40 #include "libavutil/common.h"
41 #include "libavutil/cpu.h"
42 #include "libavutil/mem.h"
43
44 typedef int (action_func)(AVCodecContext *c, void *arg);
45 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
46
47 typedef struct SliceThreadContext {
48     pthread_t *workers;
49     action_func *func;
50     action_func2 *func2;
51     void *args;
52     int *rets;
53     int job_count;
54     int job_size;
55
56     pthread_cond_t last_job_cond;
57     pthread_cond_t current_job_cond;
58     pthread_mutex_t current_job_lock;
59     unsigned current_execute;
60     int current_job;
61     int done;
62
63     int *entries;
64     int entries_count;
65     int thread_count;
66     pthread_cond_t *progress_cond;
67     pthread_mutex_t *progress_mutex;
68 } SliceThreadContext;
69
70 static void* attribute_align_arg worker(void *v)
71 {
72     AVCodecContext *avctx = v;
73     SliceThreadContext *c = avctx->internal->thread_ctx;
74     unsigned last_execute = 0;
75     int our_job = c->job_count;
76     int thread_count = avctx->thread_count;
77     int self_id;
78
79     pthread_mutex_lock(&c->current_job_lock);
80     self_id = c->current_job++;
81     for (;;){
82         int ret;
83         while (our_job >= c->job_count) {
84             if (c->current_job == thread_count + c->job_count)
85                 pthread_cond_signal(&c->last_job_cond);
86
87             while (last_execute == c->current_execute && !c->done)
88                 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
89             last_execute = c->current_execute;
90             our_job = self_id;
91
92             if (c->done) {
93                 pthread_mutex_unlock(&c->current_job_lock);
94                 return NULL;
95             }
96         }
97         pthread_mutex_unlock(&c->current_job_lock);
98
99         ret = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
100                                 c->func2(avctx, c->args, our_job, self_id);
101         if (c->rets)
102             c->rets[our_job%c->job_count] = ret;
103
104         pthread_mutex_lock(&c->current_job_lock);
105         our_job = c->current_job++;
106     }
107 }
108
109 void ff_slice_thread_free(AVCodecContext *avctx)
110 {
111     SliceThreadContext *c = avctx->internal->thread_ctx;
112     int i;
113
114     pthread_mutex_lock(&c->current_job_lock);
115     c->done = 1;
116     pthread_cond_broadcast(&c->current_job_cond);
117     for (i = 0; i < c->thread_count; i++)
118         pthread_cond_broadcast(&c->progress_cond[i]);
119     pthread_mutex_unlock(&c->current_job_lock);
120
121     for (i=0; i<avctx->thread_count; i++)
122          pthread_join(c->workers[i], NULL);
123
124     for (i = 0; i < c->thread_count; i++) {
125         pthread_mutex_destroy(&c->progress_mutex[i]);
126         pthread_cond_destroy(&c->progress_cond[i]);
127     }
128
129     pthread_mutex_destroy(&c->current_job_lock);
130     pthread_cond_destroy(&c->current_job_cond);
131     pthread_cond_destroy(&c->last_job_cond);
132
133     av_freep(&c->entries);
134     av_freep(&c->progress_mutex);
135     av_freep(&c->progress_cond);
136
137     av_freep(&c->workers);
138     av_freep(&avctx->internal->thread_ctx);
139 }
140
141 static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
142 {
143     while (c->current_job != thread_count + c->job_count)
144         pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
145     pthread_mutex_unlock(&c->current_job_lock);
146 }
147
148 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
149 {
150     SliceThreadContext *c = avctx->internal->thread_ctx;
151
152     if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
153         return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
154
155     if (job_count <= 0)
156         return 0;
157
158     pthread_mutex_lock(&c->current_job_lock);
159
160     c->current_job = avctx->thread_count;
161     c->job_count = job_count;
162     c->job_size = job_size;
163     c->args = arg;
164     c->func = func;
165     if (ret) {
166         c->rets = ret;
167     } else {
168         c->rets = NULL;
169     }
170     c->current_execute++;
171     pthread_cond_broadcast(&c->current_job_cond);
172
173     thread_park_workers(c, avctx->thread_count);
174
175     return 0;
176 }
177
178 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
179 {
180     SliceThreadContext *c = avctx->internal->thread_ctx;
181     c->func2 = func2;
182     return thread_execute(avctx, NULL, arg, ret, job_count, 0);
183 }
184
185 int ff_slice_thread_init(AVCodecContext *avctx)
186 {
187     int i;
188     SliceThreadContext *c;
189     int thread_count = avctx->thread_count;
190
191 #if HAVE_W32THREADS
192     w32thread_init();
193 #endif
194
195     if (!thread_count) {
196         int nb_cpus = av_cpu_count();
197         if  (avctx->height)
198             nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
199         // use number of cores + 1 as thread count if there is more than one
200         if (nb_cpus > 1)
201             thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
202         else
203             thread_count = avctx->thread_count = 1;
204     }
205
206     if (thread_count <= 1) {
207         avctx->active_thread_type = 0;
208         return 0;
209     }
210
211     c = av_mallocz(sizeof(SliceThreadContext));
212     if (!c)
213         return -1;
214
215     c->workers = av_mallocz_array(thread_count, sizeof(pthread_t));
216     if (!c->workers) {
217         av_free(c);
218         return -1;
219     }
220
221     avctx->internal->thread_ctx = c;
222     c->current_job = 0;
223     c->job_count = 0;
224     c->job_size = 0;
225     c->done = 0;
226     pthread_cond_init(&c->current_job_cond, NULL);
227     pthread_cond_init(&c->last_job_cond, NULL);
228     pthread_mutex_init(&c->current_job_lock, NULL);
229     pthread_mutex_lock(&c->current_job_lock);
230     for (i=0; i<thread_count; i++) {
231         if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
232            avctx->thread_count = i;
233            pthread_mutex_unlock(&c->current_job_lock);
234            ff_thread_free(avctx);
235            return -1;
236         }
237     }
238
239     thread_park_workers(c, thread_count);
240
241     avctx->execute = thread_execute;
242     avctx->execute2 = thread_execute2;
243     return 0;
244 }
245
246 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
247 {
248     SliceThreadContext *p = avctx->internal->thread_ctx;
249     int *entries = p->entries;
250
251     pthread_mutex_lock(&p->progress_mutex[thread]);
252     entries[field] +=n;
253     pthread_cond_signal(&p->progress_cond[thread]);
254     pthread_mutex_unlock(&p->progress_mutex[thread]);
255 }
256
257 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
258 {
259     SliceThreadContext *p  = avctx->internal->thread_ctx;
260     int *entries      = p->entries;
261
262     if (!entries || !field) return;
263
264     thread = thread ? thread - 1 : p->thread_count - 1;
265
266     pthread_mutex_lock(&p->progress_mutex[thread]);
267     while ((entries[field - 1] - entries[field]) < shift){
268         pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
269     }
270     pthread_mutex_unlock(&p->progress_mutex[thread]);
271 }
272
273 int ff_alloc_entries(AVCodecContext *avctx, int count)
274 {
275     int i;
276
277     if (avctx->active_thread_type & FF_THREAD_SLICE)  {
278         SliceThreadContext *p = avctx->internal->thread_ctx;
279         p->thread_count  = avctx->thread_count;
280         p->entries       = av_mallocz_array(count, sizeof(int));
281
282         p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
283         p->progress_cond  = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
284
285         if (!p->entries || !p->progress_mutex || !p->progress_cond) {
286             av_freep(&p->entries);
287             av_freep(&p->progress_mutex);
288             av_freep(&p->progress_cond);
289             return AVERROR(ENOMEM);
290         }
291         p->entries_count  = count;
292
293         for (i = 0; i < p->thread_count; i++) {
294             pthread_mutex_init(&p->progress_mutex[i], NULL);
295             pthread_cond_init(&p->progress_cond[i], NULL);
296         }
297     }
298
299     return 0;
300 }
301
302 void ff_reset_entries(AVCodecContext *avctx)
303 {
304     SliceThreadContext *p = avctx->internal->thread_ctx;
305     memset(p->entries, 0, p->entries_count * sizeof(int));
306 }