FFmpeg
pthread.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  * Libavfilter multithreading support
22  */
23 
24 #include <stddef.h>
25 
26 #include "libavutil/error.h"
27 #include "libavutil/macros.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/slicethread.h"
30 
31 #include "avfilter.h"
32 #include "avfilter_internal.h"
33 
34 typedef struct ThreadContext {
38 
39  /* per-execute parameters */
41  void *arg;
42  int *rets;
44 
45 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
46 {
47  ThreadContext *c = priv;
48  int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs);
49  if (c->rets)
50  c->rets[jobnr] = ret;
51 }
52 
54 {
55  avpriv_slicethread_free(&c->thread);
56 }
57 
59  void *arg, int *ret, int nb_jobs)
60 {
62 
63  if (nb_jobs <= 0)
64  return 0;
65  c->ctx = ctx;
66  c->arg = arg;
67  c->func = func;
68  c->rets = ret;
69 
70  avpriv_slicethread_execute(c->thread, nb_jobs, 0);
71  return 0;
72 }
73 
74 static int thread_init_internal(ThreadContext *c, int nb_threads)
75 {
76  nb_threads = avpriv_slicethread_create(&c->thread, c, worker_func, NULL, nb_threads);
77  if (nb_threads <= 1)
78  avpriv_slicethread_free(&c->thread);
79  return FFMAX(nb_threads, 1);
80 }
81 
83 {
84  AVFilterGraph *graph = &graphi->p;
85  int ret;
86 
87  if (graph->nb_threads == 1) {
88  graph->thread_type = 0;
89  return 0;
90  }
91 
92  graphi->thread = av_mallocz(sizeof(ThreadContext));
93  if (!graphi->thread)
94  return AVERROR(ENOMEM);
95 
96  ret = thread_init_internal(graphi->thread, graph->nb_threads);
97  if (ret <= 1) {
98  av_freep(&graphi->thread);
99  graph->thread_type = 0;
100  graph->nb_threads = 1;
101  return (ret < 0) ? ret : 0;
102  }
103  graph->nb_threads = ret;
104 
105  graphi->thread_execute = thread_execute;
106 
107  return 0;
108 }
109 
111 {
112  if (graph->thread)
113  slice_thread_uninit(graph->thread);
114  av_freep(&graph->thread);
115 }
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:68
ThreadContext::rets
int * rets
Definition: pthread.c:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFilterGraph::nb_threads
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:807
avfilter_action_func
int() avfilter_action_func(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
A function pointer passed to the AVFilterGraph::execute callback to be executed multiple times,...
Definition: avfilter.h:764
ThreadContext::ctx
AVFilterContext * ctx
Definition: pthread.c:40
FFFilterGraph
Definition: avfilter_internal.h:134
avpriv_slicethread_execute
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
Definition: slicethread.c:271
worker_func
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition: pthread.c:45
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVSliceThread
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
ff_graph_thread_free
void ff_graph_thread_free(FFFilterGraph *graph)
Definition: pthread.c:110
thread_execute
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: pthread.c:58
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
ThreadContext::func
avfilter_action_func * func
Definition: pthread.c:37
FFFilterGraph::thread_execute
avfilter_execute_func * thread_execute
Definition: avfilter_internal.h:146
ctx
AVFormatContext * ctx
Definition: movenc.c:49
arg
const char * arg
Definition: jacosubdec.c:67
NULL
#define NULL
Definition: coverity.c:32
fffiltergraph
static FFFilterGraph * fffiltergraph(AVFilterGraph *graph)
Definition: avfilter_internal.h:150
ThreadContext::thread
AVSliceThread * thread
Definition: pthread.c:36
avfilter_internal.h
AVFilterGraph
Definition: avfilter.h:781
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
error.h
thread_init_internal
static int thread_init_internal(ThreadContext *c, int nb_threads)
Definition: pthread.c:74
slicethread.h
FFFilterGraph::thread
void * thread
Definition: avfilter_internal.h:145
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
AVFilterGraph::thread_type
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:800
ThreadContext::arg
void * arg
Definition: pthread.c:41
ThreadContext::graph
AVFilterGraph * graph
Definition: pthread.c:35
ret
ret
Definition: filter_design.txt:187
slice_thread_uninit
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:53
ThreadContext
Definition: frame_thread_encoder.c:52
avfilter.h
FFFilterGraph::p
AVFilterGraph p
The public AVFilterGraph.
Definition: avfilter_internal.h:138
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
avpriv_slicethread_free
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
Definition: slicethread.c:276
ff_graph_thread_init
int ff_graph_thread_init(FFFilterGraph *graphi)
Definition: pthread.c:82