/src/ffmpeg/libavcodec/pthread_slice.c
Line | Count | 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 | | * 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 { |
42 | | AVSliceThread *thread; |
43 | | action_func *func; |
44 | | action_func2 *func2; |
45 | | main_func *mainfunc; |
46 | | void *args; |
47 | | int *rets; |
48 | | int job_size; |
49 | | } SliceThreadContext; |
50 | | |
51 | 0 | static void main_function(void *priv) { |
52 | 0 | AVCodecContext *avctx = priv; |
53 | 0 | SliceThreadContext *c = avctx->internal->thread_ctx; |
54 | 0 | c->mainfunc(avctx); |
55 | 0 | } |
56 | | |
57 | | static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads) |
58 | 0 | { |
59 | 0 | AVCodecContext *avctx = priv; |
60 | 0 | SliceThreadContext *c = avctx->internal->thread_ctx; |
61 | 0 | int ret; |
62 | |
|
63 | 0 | ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr) |
64 | 0 | : c->func2(avctx, c->args, jobnr, threadnr); |
65 | 0 | if (c->rets) |
66 | 0 | c->rets[jobnr] = ret; |
67 | 0 | } |
68 | | |
69 | | av_cold void ff_slice_thread_free(AVCodecContext *avctx) |
70 | 0 | { |
71 | 0 | SliceThreadContext *c = avctx->internal->thread_ctx; |
72 | |
|
73 | 0 | avpriv_slicethread_free(&c->thread); |
74 | |
|
75 | 0 | av_freep(&avctx->internal->thread_ctx); |
76 | 0 | } |
77 | | |
78 | | static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size) |
79 | 0 | { |
80 | 0 | SliceThreadContext *c = avctx->internal->thread_ctx; |
81 | |
|
82 | 0 | if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1) |
83 | 0 | return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size); |
84 | | |
85 | 0 | if (job_count <= 0) |
86 | 0 | return 0; |
87 | | |
88 | 0 | c->job_size = job_size; |
89 | 0 | c->args = arg; |
90 | 0 | c->func = func; |
91 | 0 | c->rets = ret; |
92 | |
|
93 | 0 | avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc ); |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | | static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count) |
98 | 0 | { |
99 | 0 | SliceThreadContext *c = avctx->internal->thread_ctx; |
100 | 0 | c->func2 = func2; |
101 | 0 | return thread_execute(avctx, NULL, arg, ret, job_count, 0); |
102 | 0 | } |
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 | 0 | { |
106 | 0 | SliceThreadContext *c = avctx->internal->thread_ctx; |
107 | 0 | c->func2 = func2; |
108 | 0 | c->mainfunc = mainfunc; |
109 | 0 | return thread_execute(avctx, NULL, arg, ret, job_count, 0); |
110 | 0 | } |
111 | | |
112 | | av_cold int ff_slice_thread_init(AVCodecContext *avctx) |
113 | 0 | { |
114 | 0 | SliceThreadContext *c; |
115 | 0 | int thread_count = avctx->thread_count; |
116 | 0 | void (*mainfunc)(void *); |
117 | |
|
118 | 0 | if (!thread_count) { |
119 | 0 | int nb_cpus = av_cpu_count(); |
120 | 0 | if (avctx->height) |
121 | 0 | nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16); |
122 | | // use number of cores + 1 as thread count if there is more than one |
123 | 0 | if (nb_cpus > 1) |
124 | 0 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); |
125 | 0 | else |
126 | 0 | thread_count = avctx->thread_count = 1; |
127 | 0 | } |
128 | |
|
129 | 0 | if (thread_count <= 1) { |
130 | 0 | avctx->active_thread_type = 0; |
131 | 0 | return 0; |
132 | 0 | } |
133 | | |
134 | 0 | avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c)); |
135 | 0 | if (!c) |
136 | 0 | return AVERROR(ENOMEM); |
137 | 0 | mainfunc = ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF ? &main_function : NULL; |
138 | 0 | thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, |
139 | 0 | mainfunc, thread_count); |
140 | 0 | if (thread_count <= 1) { |
141 | 0 | ff_slice_thread_free(avctx); |
142 | 0 | avctx->thread_count = 1; |
143 | 0 | avctx->active_thread_type = 0; |
144 | 0 | return thread_count < 0 ? thread_count : 0; |
145 | 0 | } |
146 | 0 | avctx->thread_count = thread_count; |
147 | |
|
148 | 0 | avctx->execute = thread_execute; |
149 | 0 | avctx->execute2 = thread_execute2; |
150 | 0 | return 0; |
151 | 0 | } |