FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pthread_slice.c
Go to the documentation of this file.
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);
42 
43 typedef struct SliceThreadContext {
48  void *args;
49  int *rets;
50  int job_size;
51 
52  int *entries;
58 
59 static void main_function(void *priv) {
60  AVCodecContext *avctx = priv;
62  c->mainfunc(avctx);
63 }
64 
65 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
66 {
67  AVCodecContext *avctx = priv;
69  int ret;
70 
71  ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
72  : c->func2(avctx, c->args, jobnr, threadnr);
73  if (c->rets)
74  c->rets[jobnr] = ret;
75 }
76 
78 {
80  int i;
81 
83 
84  for (i = 0; i < c->thread_count; i++) {
87  }
88 
89  av_freep(&c->entries);
92  av_freep(&avctx->internal->thread_ctx);
93 }
94 
95 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
96 {
98 
99  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
100  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
101 
102  if (job_count <= 0)
103  return 0;
104 
105  c->job_size = job_size;
106  c->args = arg;
107  c->func = func;
108  c->rets = ret;
109 
110  avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc );
111  return 0;
112 }
113 
114 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
115 {
117  c->func2 = func2;
118  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
119 }
120 
121 int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count)
122 {
124  c->func2 = func2;
125  c->mainfunc = mainfunc;
126  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
127 }
128 
130 {
132  int thread_count = avctx->thread_count;
133  static void (*mainfunc)(void *);
134 
135 #if HAVE_W32THREADS
136  w32thread_init();
137 #endif
138 
139  // We cannot do this in the encoder init as the threads are created before
140  if (av_codec_is_encoder(avctx->codec) &&
141  avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
142  avctx->height > 2800)
143  thread_count = avctx->thread_count = 1;
144 
145  if (!thread_count) {
146  int nb_cpus = av_cpu_count();
147  if (avctx->height)
148  nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
149  // use number of cores + 1 as thread count if there is more than one
150  if (nb_cpus > 1)
151  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
152  else
153  thread_count = avctx->thread_count = 1;
154  }
155 
156  if (thread_count <= 1) {
157  avctx->active_thread_type = 0;
158  return 0;
159  }
160 
161  avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
163  if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, mainfunc, thread_count)) <= 1) {
164  if (c)
166  av_freep(&avctx->internal->thread_ctx);
167  avctx->thread_count = 1;
168  avctx->active_thread_type = 0;
169  return 0;
170  }
171  avctx->thread_count = thread_count;
172 
173  avctx->execute = thread_execute;
174  avctx->execute2 = thread_execute2;
175  return 0;
176 }
177 
178 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
179 {
181  int *entries = p->entries;
182 
183  pthread_mutex_lock(&p->progress_mutex[thread]);
184  entries[field] +=n;
185  pthread_cond_signal(&p->progress_cond[thread]);
187 }
188 
189 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
190 {
192  int *entries = p->entries;
193 
194  if (!entries || !field) return;
195 
196  thread = thread ? thread - 1 : p->thread_count - 1;
197 
198  pthread_mutex_lock(&p->progress_mutex[thread]);
199  while ((entries[field - 1] - entries[field]) < shift){
200  pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
201  }
203 }
204 
206 {
207  int i;
208 
209  if (avctx->active_thread_type & FF_THREAD_SLICE) {
211 
212  if (p->entries) {
213  av_assert0(p->thread_count == avctx->thread_count);
214  av_freep(&p->entries);
215  }
216 
217  p->thread_count = avctx->thread_count;
218  p->entries = av_mallocz_array(count, sizeof(int));
219 
220  if (!p->progress_mutex) {
223  }
224 
225  if (!p->entries || !p->progress_mutex || !p->progress_cond) {
226  av_freep(&p->entries);
228  av_freep(&p->progress_cond);
229  return AVERROR(ENOMEM);
230  }
231  p->entries_count = count;
232 
233  for (i = 0; i < p->thread_count; i++) {
236  }
237  }
238 
239  return 0;
240 }
241 
243 {
245  memset(p->entries, 0, p->entries_count * sizeof(int));
246 }
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:397
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1511
int ff_slice_thread_init(AVCodecContext *avctx)
static int shift(int a, int b)
Definition: sonic.c:82
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:106
#define pthread_mutex_lock(a)
Definition: ffprobe.c:61
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:164
int av_cpu_count(void)
Definition: cpu.c:263
Memory handling functions.
int( action_func)(AVCodecContext *c, void *arg)
Definition: pthread_slice.c:39
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition: pthread_slice.c:65
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:138
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:171
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
HMTX pthread_mutex_t
Definition: os2threads.h:49
void * thread_ctx
Definition: internal.h:161
Multithreading support functions.
pthread_mutex_t * progress_mutex
Definition: pthread_slice.c:56
static void main_function(void *priv)
Definition: pthread_slice.c:59
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:146
#define AVERROR(e)
Definition: error.h:43
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2772
AVSliceThread * thread
Definition: pthread_slice.c:44
action_func2 * func2
Definition: pthread_slice.c:46
const char * arg
Definition: jacosubdec.c:66
simple assert() macros that are a bit more flexible than ISO C assert().
GLsizei count
Definition: opengl_enc.c:109
action_func * func
Definition: pthread_slice.c:45
int ff_alloc_entries(AVCodecContext *avctx, int count)
static int thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
#define FFMIN(a, b)
Definition: common.h:96
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
Definition: slicethread.c:254
void ff_slice_thread_free(AVCodecContext *avctx)
Definition: pthread_slice.c:77
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:98
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2765
pthread_cond_t * progress_cond
Definition: pthread_slice.c:55
int n
Definition: avisynth_c.h:684
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:65
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2753
void ff_reset_entries(AVCodecContext *avctx)
Libavcodec external API header.
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
enum AVCodecID codec_id
Definition: avcodec.h:1512
int avpriv_slicethread_create(AVSliceThread **pctx, void *priv, void(*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads), void(*main_func)(void *priv), int nb_threads)
Create slice threading context.
Definition: slicethread.c:240
main external API structure.
Definition: avcodec.h:1502
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
Definition: slicethread.c:249
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:67
#define MAX_AUTO_THREADS
int( main_func)(AVCodecContext *c)
Definition: pthread_slice.c:41
#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF
Codec initializes slice-based threading with a main function.
Definition: internal.h:70
int
common internal api header.
common internal and external API header
main_func * mainfunc
Definition: pthread_slice.c:47
static double c[64]
int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2 *func2, main_func *mainfunc, void *arg, int *ret, int job_count)
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:127
int caps_internal
Internal codec capabilities.
Definition: avcodec.h:3488
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2793
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2813
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1537
#define av_freep(p)
#define av_malloc_array(a, b)
int( action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr)
Definition: pthread_slice.c:40
static int thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
Definition: pthread_slice.c:95
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
Definition: utils.c:520
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191