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 "config.h"
26 
27 #include "avcodec.h"
28 #include "codec_internal.h"
29 #include "internal.h"
30 #include "pthread_internal.h"
31 #include "thread.h"
32 
33 #include "libavutil/avassert.h"
34 #include "libavutil/common.h"
35 #include "libavutil/cpu.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/thread.h"
38 #include "libavutil/slicethread.h"
39 
40 typedef int (action_func)(AVCodecContext *c, void *arg);
41 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
42 typedef int (main_func)(AVCodecContext *c);
43 
44 typedef struct SliceThreadContext {
49  void *args;
50  int *rets;
51  int job_size;
53 
54 static void main_function(void *priv) {
55  AVCodecContext *avctx = priv;
57  c->mainfunc(avctx);
58 }
59 
60 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
61 {
62  AVCodecContext *avctx = priv;
64  int ret;
65 
66  ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
67  : c->func2(avctx, c->args, jobnr, threadnr);
68  if (c->rets)
69  c->rets[jobnr] = ret;
70 }
71 
73 {
75 
76  avpriv_slicethread_free(&c->thread);
77 
78  av_freep(&avctx->internal->thread_ctx);
79 }
80 
81 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
82 {
84 
85  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
86  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
87 
88  if (job_count <= 0)
89  return 0;
90 
91  c->job_size = job_size;
92  c->args = arg;
93  c->func = func;
94  c->rets = ret;
95 
96  avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc );
97  return 0;
98 }
99 
100 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
101 {
103  c->func2 = func2;
104  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
105 }
106 
107 int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count)
108 {
110  c->func2 = func2;
111  c->mainfunc = mainfunc;
112  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
113 }
114 
116 {
118  int thread_count = avctx->thread_count;
119  void (*mainfunc)(void *);
120 
121  // We cannot do this in the encoder init as the threads are created before
122  if (av_codec_is_encoder(avctx->codec) &&
123  avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
124  avctx->height > 2800)
125  thread_count = avctx->thread_count = 1;
126 
127  if (!thread_count) {
128  int nb_cpus = av_cpu_count();
129  if (avctx->height)
130  nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
131  // use number of cores + 1 as thread count if there is more than one
132  if (nb_cpus > 1)
133  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
134  else
135  thread_count = avctx->thread_count = 1;
136  }
137 
138  if (thread_count <= 1) {
139  avctx->active_thread_type = 0;
140  return 0;
141  }
142 
143  avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
145  if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, mainfunc, 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 }
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:68
SliceThreadContext::args
void * args
Definition: pthread_slice.c:49
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:65
thread.h
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:48
internal.h
ff_slice_thread_free
void ff_slice_thread_free(AVCodecContext *avctx)
Definition: pthread_slice.c:72
AVSliceThread
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
thread.h
main_function
static void main_function(void *priv)
Definition: pthread_slice.c:54
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
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:47
avassert.h
func2
static double(*const func2[])(void *, double, double)
Definition: af_afftfilt.c:99
SliceThreadContext::job_size
int job_size
Definition: pthread_slice.c:51
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:50
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
SliceThreadContext::thread
AVSliceThread * thread
Definition: pthread_slice.c:45
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:60
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:44
thread_execute2
static int thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
Definition: pthread_slice.c:100
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:81
SliceThreadContext::func
action_func * func
Definition: pthread_slice.c:46
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:330
FF_THREAD_SLICE
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1613
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
slicethread.h
ff_slice_thread_init
int ff_slice_thread_init(AVCodecContext *avctx)
Definition: pthread_slice.c:115
av_codec_is_encoder
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:78
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:107
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
main_func
int() main_func(AVCodecContext *c)
Definition: pthread_slice.c:42
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:41
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
mem.h
action_func
int() action_func(AVCodecContext *c, void *arg)
Definition: pthread_slice.c:40
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