FFmpeg
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 "avcodec.h"
26 #include "codec_internal.h"
27 #include "internal.h"
28 #include "pthread_internal.h"
29 #include "thread.h"
30 
31 #include "libavutil/attributes.h"
32 #include "libavutil/cpu.h"
33 #include "libavutil/macros.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/slicethread.h"
36 
37 typedef int (action_func)(AVCodecContext *c, void *arg);
38 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
39 typedef int (main_func)(AVCodecContext *c);
40 
41 typedef struct SliceThreadContext {
46  void *args;
47  int *rets;
48  int job_size;
50 
51 static void main_function(void *priv) {
52  AVCodecContext *avctx = priv;
54  c->mainfunc(avctx);
55 }
56 
57 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
58 {
59  AVCodecContext *avctx = priv;
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 
70 {
72 
73  avpriv_slicethread_free(&c->thread);
74 
75  av_freep(&avctx->internal->thread_ctx);
76 }
77 
78 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
79 {
81 
82  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
83  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
84 
85  if (job_count <= 0)
86  return 0;
87 
88  c->job_size = job_size;
89  c->args = arg;
90  c->func = func;
91  c->rets = ret;
92 
93  avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc );
94  return 0;
95 }
96 
97 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
98 {
100  c->func2 = func2;
101  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
102 }
103 
104 int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count)
105 {
107  c->func2 = func2;
108  c->mainfunc = mainfunc;
109  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
110 }
111 
113 {
115  int thread_count = avctx->thread_count;
116  void (*mainfunc)(void *);
117 
118  // We cannot do this in the encoder init as the threads are created before
119  if (ff_codec_is_encoder(avctx->codec) &&
120  avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
121  avctx->height > 2800)
122  thread_count = avctx->thread_count = 1;
123 
124  if (!thread_count) {
125  int nb_cpus = av_cpu_count();
126  if (avctx->height)
127  nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
128  // use number of cores + 1 as thread count if there is more than one
129  if (nb_cpus > 1)
130  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
131  else
132  thread_count = avctx->thread_count = 1;
133  }
134 
135  if (thread_count <= 1) {
136  avctx->active_thread_type = 0;
137  return 0;
138  }
139 
140  avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
142  if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, mainfunc, thread_count)) <= 1) {
143  if (c)
144  avpriv_slicethread_free(&c->thread);
145  av_freep(&avctx->internal->thread_ctx);
146  avctx->thread_count = 1;
147  avctx->active_thread_type = 0;
148  return 0;
149  }
150  avctx->thread_count = thread_count;
151 
152  avctx->execute = thread_execute;
153  avctx->execute2 = thread_execute2;
154  return 0;
155 }
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:68
SliceThreadContext::args
void * args
Definition: pthread_slice.c:46
FF_CODEC_CAP_SLICE_THREAD_HAS_MF
#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF
Codec initializes slice-based threading with a main function.
Definition: codec_internal.h:64
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
avpriv_slicethread_execute
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
Definition: slicethread.c:271
SliceThreadContext::mainfunc
main_func * mainfunc
Definition: pthread_slice.c:45
internal.h
AVSliceThread
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
thread.h
main_function
static void main_function(void *priv)
Definition: pthread_slice.c:51
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:460
avpriv_slicethread_create
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:262
macros.h
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1601
SliceThreadContext::func2
action_func2 * func2
Definition: pthread_slice.c:44
func2
static double(*const func2[])(void *, double, double)
Definition: af_afftfilt.c:99
av_cold
#define av_cold
Definition: attributes.h:90
ff_codec_is_encoder
static int ff_codec_is_encoder(const AVCodec *avcodec)
Internal version of av_codec_is_encoder().
Definition: codec_internal.h:293
SliceThreadContext::job_size
int job_size
Definition: pthread_slice.c:48
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
arg
const char * arg
Definition: jacosubdec.c:67
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
SliceThreadContext::rets
int * rets
Definition: pthread_slice.c:47
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
SliceThreadContext::thread
AVSliceThread * thread
Definition: pthread_slice.c:42
pthread_internal.h
worker_func
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition: pthread_slice.c:57
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:221
SliceThreadContext
Definition: pthread_slice.c:41
thread_execute2
static int thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
Definition: pthread_slice.c:97
codec_internal.h
cpu.h
thread_execute
static int thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
Definition: pthread_slice.c:78
SliceThreadContext::func
action_func * func
Definition: pthread_slice.c:43
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:284
FF_THREAD_SLICE
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1613
ff_slice_thread_free
av_cold void ff_slice_thread_free(AVCodecContext *avctx)
Definition: pthread_slice.c:69
avcodec_default_execute
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
Definition: avcodec.c:72
attributes.h
slicethread.h
ff_slice_thread_execute_with_mainfunc
int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2 *func2, main_func *mainfunc, void *arg, int *ret, int job_count)
Definition: pthread_slice.c:104
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
main_func
int() main_func(AVCodecContext *c)
Definition: pthread_slice.c:39
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodecContext::height
int height
Definition: avcodec.h:632
avcodec.h
ret
ret
Definition: filter_design.txt:187
action_func2
int() action_func2(AVCodecContext *c, void *arg, int jobnr, int threadnr)
Definition: pthread_slice.c:38
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1620
AVCodecContext::execute
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:1631
ff_slice_thread_init
av_cold int ff_slice_thread_init(AVCodecContext *avctx)
Definition: pthread_slice.c:112
mem.h
action_func
int() action_func(AVCodecContext *c, void *arg)
Definition: pthread_slice.c:37
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecInternal::thread_ctx
void * thread_ctx
Definition: internal.h:73
avpriv_slicethread_free
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
Definition: slicethread.c:276
AVCodecContext::execute2
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:1650