FFmpeg
Loading...
Searching...
No Matches
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"
30
31#include "avfilter.h"
32#include "avfilter_internal.h"
33
34typedef struct ThreadContext {
38
39 /* per-execute parameters */
41 void *arg;
42 int *rets;
44
45static int 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 return 0;
52}
53
55{
56 avpriv_slicethread_free(&c->thread);
57}
58
60 void *arg, int *ret, int nb_jobs)
61{
63
64 if (nb_jobs <= 0)
65 return 0;
66 c->ctx = ctx;
67 c->arg = arg;
68 c->func = func;
69 c->rets = ret;
70
71 avpriv_slicethread_execute2(c->thread, nb_jobs, 0);
72 return 0;
73}
74
75static int thread_init_internal(ThreadContext *c, int nb_threads)
76{
77 nb_threads = avpriv_slicethread_create2(&c->thread, c, worker_func, NULL, nb_threads);
78 if (nb_threads <= 1)
79 avpriv_slicethread_free(&c->thread);
80 return FFMAX(nb_threads, 1);
81}
82
84{
85 AVFilterGraph *graph = &graphi->p;
86 int ret;
87
88 if (graph->nb_threads == 1) {
89 graph->thread_type = 0;
90 return 0;
91 }
92
93 graphi->thread = av_mallocz(sizeof(ThreadContext));
94 if (!graphi->thread)
95 return AVERROR(ENOMEM);
96
97 ret = thread_init_internal(graphi->thread, graph->nb_threads);
98 if (ret <= 1) {
99 av_freep(&graphi->thread);
100 graph->thread_type = 0;
101 graph->nb_threads = 1;
102 return (ret < 0) ? ret : 0;
103 }
104 graph->nb_threads = ret;
105
107
108 return 0;
109}
110
112{
113 if (graph->thread)
115 av_freep(&graph->thread);
116}
Main libavfilter public API header.
static FFFilterGraph * fffiltergraph(AVFilterGraph *graph)
#define NULL
Definition coverity.c:32
error code definitions
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:544
#define AVERROR(e)
Definition error.h:45
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:66
const char * arg
Definition jacosubdec.c:65
static int worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition pthread.c:45
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition pthread.c:59
void ff_graph_thread_free(FFFilterGraph *graph)
Definition pthread.c:111
static void slice_thread_uninit(ThreadContext *c)
Definition pthread.c:54
int ff_graph_thread_init(FFFilterGraph *graphi)
Definition pthread.c:83
static int thread_init_internal(ThreadContext *c, int nb_threads)
Definition pthread.c:75
Utility Preprocessor macros.
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
static int thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
static int worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
int avpriv_slicethread_execute2(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
int avpriv_slicethread_create2(AVSliceThread **pctx, void *priv, int(*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads), int(*main_func)(void *priv), int nb_threads)
Create slice threading context.
struct AVSliceThread AVSliceThread
Definition slicethread.h:25
An instance of a filter.
Definition avfilter.h:273
int thread_type
Type of multithreading allowed for filters in this graph.
Definition avfilter.h:580
int nb_threads
Maximum number of threads used by filters in this graph.
Definition avfilter.h:587
avfilter_execute_func * thread_execute
AVFilterGraph p
The public AVFilterGraph.
AVFilterContext * ctx
Definition pthread.c:40
AVSliceThread * thread
Definition pthread.c:36
AVFilterGraph * graph
Definition pthread.c:35
avfilter_action_func * func
Definition pthread.c:37
int * rets
Definition pthread.c:42
void * arg
Definition pthread.c:41
#define av_mallocz(s)
#define av_freep(p)
static AVFormatContext * ctx
Definition movenc.c:49
static double c[64]