Line | Branch | Exec | Source |
---|---|---|---|
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 { | ||
35 | AVFilterGraph *graph; | ||
36 | AVSliceThread *thread; | ||
37 | avfilter_action_func *func; | ||
38 | |||
39 | /* per-execute parameters */ | ||
40 | AVFilterContext *ctx; | ||
41 | void *arg; | ||
42 | int *rets; | ||
43 | } ThreadContext; | ||
44 | |||
45 | 27005 | static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads) | |
46 | { | ||
47 | 27005 | ThreadContext *c = priv; | |
48 | 27005 | int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs); | |
49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27005 times.
|
27005 | if (c->rets) |
50 | ✗ | c->rets[jobnr] = ret; | |
51 | 27005 | } | |
52 | |||
53 | 3434 | static void slice_thread_uninit(ThreadContext *c) | |
54 | { | ||
55 | 3434 | avpriv_slicethread_free(&c->thread); | |
56 | 3434 | } | |
57 | |||
58 | 3452 | static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, | |
59 | void *arg, int *ret, int nb_jobs) | ||
60 | { | ||
61 | 3452 | ThreadContext *c = fffiltergraph(ctx->graph)->thread; | |
62 | |||
63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
|
3452 | if (nb_jobs <= 0) |
64 | ✗ | return 0; | |
65 | 3452 | c->ctx = ctx; | |
66 | 3452 | c->arg = arg; | |
67 | 3452 | c->func = func; | |
68 | 3452 | c->rets = ret; | |
69 | |||
70 | 3452 | avpriv_slicethread_execute(c->thread, nb_jobs, 0); | |
71 | 3452 | return 0; | |
72 | } | ||
73 | |||
74 | 3434 | static int thread_init_internal(ThreadContext *c, int nb_threads) | |
75 | { | ||
76 | 3434 | nb_threads = avpriv_slicethread_create(&c->thread, c, worker_func, NULL, nb_threads); | |
77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3434 times.
|
3434 | if (nb_threads <= 1) |
78 | ✗ | avpriv_slicethread_free(&c->thread); | |
79 | 3434 | return FFMAX(nb_threads, 1); | |
80 | } | ||
81 | |||
82 | 15229 | int ff_graph_thread_init(FFFilterGraph *graphi) | |
83 | { | ||
84 | 15229 | AVFilterGraph *graph = &graphi->p; | |
85 | int ret; | ||
86 | |||
87 |
2/2✓ Branch 0 taken 11795 times.
✓ Branch 1 taken 3434 times.
|
15229 | if (graph->nb_threads == 1) { |
88 | 11795 | graph->thread_type = 0; | |
89 | 11795 | return 0; | |
90 | } | ||
91 | |||
92 | 3434 | graphi->thread = av_mallocz(sizeof(ThreadContext)); | |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3434 times.
|
3434 | if (!graphi->thread) |
94 | ✗ | return AVERROR(ENOMEM); | |
95 | |||
96 | 3434 | ret = thread_init_internal(graphi->thread, graph->nb_threads); | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3434 times.
|
3434 | 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 | 3434 | graph->nb_threads = ret; | |
104 | |||
105 | 3434 | graphi->thread_execute = thread_execute; | |
106 | |||
107 | 3434 | return 0; | |
108 | } | ||
109 | |||
110 | 15229 | void ff_graph_thread_free(FFFilterGraph *graph) | |
111 | { | ||
112 |
2/2✓ Branch 0 taken 3434 times.
✓ Branch 1 taken 11795 times.
|
15229 | if (graph->thread) |
113 | 3434 | slice_thread_uninit(graph->thread); | |
114 | 15229 | av_freep(&graph->thread); | |
115 | 15229 | } | |
116 |