]> git.sesse.net Git - ffmpeg/blob - libavcodec/pthread_slice.c
avcodec: add Dolby E decoder
[ffmpeg] / 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 #include "avcodec.h"
28 #include "internal.h"
29 #include "pthread_internal.h"
30 #include "thread.h"
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/cpu.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/thread.h"
37 #include "libavutil/slicethread.h"
38
39 typedef int (action_func)(AVCodecContext *c, void *arg);
40 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
41
42 typedef struct SliceThreadContext {
43     AVSliceThread *thread;
44     action_func *func;
45     action_func2 *func2;
46     void *args;
47     int *rets;
48     int job_size;
49
50     int *entries;
51     int entries_count;
52     int thread_count;
53     pthread_cond_t *progress_cond;
54     pthread_mutex_t *progress_mutex;
55 } SliceThreadContext;
56
57 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
58 {
59     AVCodecContext *avctx = priv;
60     SliceThreadContext *c = avctx->internal->thread_ctx;
61     int ret;
62
63     ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
64                   : c->func2(avctx, c->args, jobnr, threadnr);
65     if (c->rets)
66         c->rets[jobnr] = ret;
67 }
68
69 void ff_slice_thread_free(AVCodecContext *avctx)
70 {
71     SliceThreadContext *c = avctx->internal->thread_ctx;
72     int i;
73
74     avpriv_slicethread_free(&c->thread);
75
76     for (i = 0; i < c->thread_count; i++) {
77         pthread_mutex_destroy(&c->progress_mutex[i]);
78         pthread_cond_destroy(&c->progress_cond[i]);
79     }
80
81     av_freep(&c->entries);
82     av_freep(&c->progress_mutex);
83     av_freep(&c->progress_cond);
84     av_freep(&avctx->internal->thread_ctx);
85 }
86
87 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
88 {
89     SliceThreadContext *c = avctx->internal->thread_ctx;
90
91     if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
92         return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
93
94     if (job_count <= 0)
95         return 0;
96
97     c->job_size = job_size;
98     c->args = arg;
99     c->func = func;
100     c->rets = ret;
101
102     avpriv_slicethread_execute(c->thread, job_count, 0);
103     return 0;
104 }
105
106 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
107 {
108     SliceThreadContext *c = avctx->internal->thread_ctx;
109     c->func2 = func2;
110     return thread_execute(avctx, NULL, arg, ret, job_count, 0);
111 }
112
113 int ff_slice_thread_init(AVCodecContext *avctx)
114 {
115     SliceThreadContext *c;
116     int thread_count = avctx->thread_count;
117
118 #if HAVE_W32THREADS
119     w32thread_init();
120 #endif
121
122     // We cannot do this in the encoder init as the threads are created before
123     if (av_codec_is_encoder(avctx->codec) &&
124         avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
125         avctx->height > 2800)
126         thread_count = avctx->thread_count = 1;
127
128     if (!thread_count) {
129         int nb_cpus = av_cpu_count();
130         if  (avctx->height)
131             nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
132         // use number of cores + 1 as thread count if there is more than one
133         if (nb_cpus > 1)
134             thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
135         else
136             thread_count = avctx->thread_count = 1;
137     }
138
139     if (thread_count <= 1) {
140         avctx->active_thread_type = 0;
141         return 0;
142     }
143
144     avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
145     if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, NULL, thread_count)) <= 1) {
146         if (c)
147             avpriv_slicethread_free(&c->thread);
148         av_freep(&avctx->internal->thread_ctx);
149         avctx->thread_count = 1;
150         avctx->active_thread_type = 0;
151         return 0;
152     }
153     avctx->thread_count = thread_count;
154
155     avctx->execute = thread_execute;
156     avctx->execute2 = thread_execute2;
157     return 0;
158 }
159
160 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
161 {
162     SliceThreadContext *p = avctx->internal->thread_ctx;
163     int *entries = p->entries;
164
165     pthread_mutex_lock(&p->progress_mutex[thread]);
166     entries[field] +=n;
167     pthread_cond_signal(&p->progress_cond[thread]);
168     pthread_mutex_unlock(&p->progress_mutex[thread]);
169 }
170
171 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
172 {
173     SliceThreadContext *p  = avctx->internal->thread_ctx;
174     int *entries      = p->entries;
175
176     if (!entries || !field) return;
177
178     thread = thread ? thread - 1 : p->thread_count - 1;
179
180     pthread_mutex_lock(&p->progress_mutex[thread]);
181     while ((entries[field - 1] - entries[field]) < shift){
182         pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
183     }
184     pthread_mutex_unlock(&p->progress_mutex[thread]);
185 }
186
187 int ff_alloc_entries(AVCodecContext *avctx, int count)
188 {
189     int i;
190
191     if (avctx->active_thread_type & FF_THREAD_SLICE)  {
192         SliceThreadContext *p = avctx->internal->thread_ctx;
193
194         if (p->entries) {
195             av_assert0(p->thread_count == avctx->thread_count);
196             av_freep(&p->entries);
197         }
198
199         p->thread_count  = avctx->thread_count;
200         p->entries       = av_mallocz_array(count, sizeof(int));
201
202         if (!p->progress_mutex) {
203             p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
204             p->progress_cond  = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
205         }
206
207         if (!p->entries || !p->progress_mutex || !p->progress_cond) {
208             av_freep(&p->entries);
209             av_freep(&p->progress_mutex);
210             av_freep(&p->progress_cond);
211             return AVERROR(ENOMEM);
212         }
213         p->entries_count  = count;
214
215         for (i = 0; i < p->thread_count; i++) {
216             pthread_mutex_init(&p->progress_mutex[i], NULL);
217             pthread_cond_init(&p->progress_cond[i], NULL);
218         }
219     }
220
221     return 0;
222 }
223
224 void ff_reset_entries(AVCodecContext *avctx)
225 {
226     SliceThreadContext *p = avctx->internal->thread_ctx;
227     memset(p->entries, 0, p->entries_count * sizeof(int));
228 }