Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2012 Michael Niedermayer <michaelni@gmx.at> | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #include <stdatomic.h> | ||
22 | |||
23 | #include "frame_thread_encoder.h" | ||
24 | |||
25 | #include "libavutil/avassert.h" | ||
26 | #include "libavutil/cpu.h" | ||
27 | #include "libavutil/opt.h" | ||
28 | #include "libavutil/thread.h" | ||
29 | #include "avcodec.h" | ||
30 | #include "encode.h" | ||
31 | #include "internal.h" | ||
32 | #include "pthread_internal.h" | ||
33 | |||
34 | #define MAX_THREADS 64 | ||
35 | /* There can be as many as MAX_THREADS + 1 outstanding tasks. | ||
36 | * An additional + 1 is needed so that one can distinguish | ||
37 | * the case of zero and MAX_THREADS + 1 outstanding tasks modulo | ||
38 | * the number of buffers. */ | ||
39 | #define BUFFER_SIZE (MAX_THREADS + 2) | ||
40 | |||
41 | typedef struct{ | ||
42 | AVFrame *indata; | ||
43 | AVPacket *outdata; | ||
44 | int return_code; | ||
45 | int finished; | ||
46 | int got_packet; | ||
47 | } Task; | ||
48 | |||
49 | typedef struct{ | ||
50 | AVCodecContext *parent_avctx; | ||
51 | |||
52 | pthread_mutex_t task_fifo_mutex; /* Used to guard (next_)task_index */ | ||
53 | pthread_cond_t task_fifo_cond; | ||
54 | |||
55 | unsigned pthread_init_cnt; | ||
56 | unsigned max_tasks; | ||
57 | Task tasks[BUFFER_SIZE]; | ||
58 | pthread_mutex_t finished_task_mutex; /* Guards tasks[i].finished */ | ||
59 | pthread_cond_t finished_task_cond; | ||
60 | |||
61 | unsigned next_task_index; | ||
62 | unsigned task_index; | ||
63 | unsigned finished_task_index; | ||
64 | |||
65 | pthread_t worker[MAX_THREADS]; | ||
66 | atomic_int exit; | ||
67 | } ThreadContext; | ||
68 | |||
69 | #define OFF(member) offsetof(ThreadContext, member) | ||
70 | DEFINE_OFFSET_ARRAY(ThreadContext, thread_ctx, pthread_init_cnt, | ||
71 | (OFF(task_fifo_mutex), OFF(finished_task_mutex)), | ||
72 | (OFF(task_fifo_cond), OFF(finished_task_cond))); | ||
73 | #undef OFF | ||
74 | |||
75 | 12476 | static void * attribute_align_arg worker(void *v){ | |
76 | 12476 | AVCodecContext *avctx = v; | |
77 | 12476 | ThreadContext *c = avctx->internal->frame_thread_encoder; | |
78 | |||
79 |
2/2✓ Branch 0 taken 66866 times.
✓ Branch 1 taken 773 times.
|
67639 | while (!atomic_load(&c->exit)) { |
80 | int ret; | ||
81 | AVPacket *pkt; | ||
82 | AVFrame *frame; | ||
83 | Task *task; | ||
84 | unsigned task_index; | ||
85 | |||
86 | 66866 | pthread_mutex_lock(&c->task_fifo_mutex); | |
87 |
3/4✓ Branch 0 taken 75828 times.
✓ Branch 1 taken 55163 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 55163 times.
|
130991 | while (c->next_task_index == c->task_index || atomic_load(&c->exit)) { |
88 |
2/2✓ Branch 0 taken 11703 times.
✓ Branch 1 taken 64125 times.
|
75828 | if (atomic_load(&c->exit)) { |
89 | 11703 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
90 | 11703 | goto end; | |
91 | } | ||
92 | 64125 | pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex); | |
93 | } | ||
94 | 55163 | task_index = c->next_task_index; | |
95 | 55163 | c->next_task_index = (c->next_task_index + 1) % c->max_tasks; | |
96 | 55163 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
97 | /* The main thread ensures that any two outstanding tasks have | ||
98 | * different indices, ergo each worker thread owns its element | ||
99 | * of c->tasks with the exception of finished, which is shared | ||
100 | * with the main thread and guarded by finished_task_mutex. */ | ||
101 | 55163 | task = &c->tasks[task_index]; | |
102 | 55163 | frame = task->indata; | |
103 | 55163 | pkt = task->outdata; | |
104 | |||
105 | 55163 | ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet); | |
106 | 55163 | pthread_mutex_lock(&c->finished_task_mutex); | |
107 | 55163 | task->return_code = ret; | |
108 | 55163 | task->finished = 1; | |
109 | 55163 | pthread_cond_signal(&c->finished_task_cond); | |
110 | 55163 | pthread_mutex_unlock(&c->finished_task_mutex); | |
111 | } | ||
112 | 773 | end: | |
113 | 12476 | avcodec_close(avctx); | |
114 | 12476 | av_freep(&avctx); | |
115 | 12476 | return NULL; | |
116 | } | ||
117 | |||
118 | 18852 | av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx) | |
119 | { | ||
120 | 18852 | int i=0; | |
121 | ThreadContext *c; | ||
122 | 18852 | AVCodecContext *thread_avctx = NULL; | |
123 | int ret; | ||
124 | |||
125 |
2/2✓ Branch 0 taken 18814 times.
✓ Branch 1 taken 38 times.
|
18852 | if( !(avctx->thread_type & FF_THREAD_FRAME) |
126 |
2/2✓ Branch 0 taken 1627 times.
✓ Branch 1 taken 17187 times.
|
18814 | || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)) |
127 | 1665 | return 0; | |
128 | |||
129 |
2/2✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 15629 times.
|
17187 | if( !avctx->thread_count |
130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1558 times.
|
1558 | && avctx->codec_id == AV_CODEC_ID_MJPEG |
131 | ✗ | && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) { | |
132 | ✗ | av_log(avctx, AV_LOG_DEBUG, | |
133 | "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice " | ||
134 | "or a constant quantizer if you want to use multiple cpu cores\n"); | ||
135 | ✗ | avctx->thread_count = 1; | |
136 | } | ||
137 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17184 times.
|
17187 | if( avctx->thread_count > 1 |
138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | && avctx->codec_id == AV_CODEC_ID_MJPEG |
139 | ✗ | && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) | |
140 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
141 | "MJPEG CBR encoding works badly with frame multi-threading, consider " | ||
142 | "using -threads 1, -thread_type slice or a constant quantizer.\n"); | ||
143 | |||
144 |
2/2✓ Branch 0 taken 17175 times.
✓ Branch 1 taken 12 times.
|
17187 | if (avctx->codec_id == AV_CODEC_ID_HUFFYUV || |
145 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 17155 times.
|
17175 | avctx->codec_id == AV_CODEC_ID_FFVHUFF) { |
146 | 32 | int warn = 0; | |
147 | int64_t tmp; | ||
148 | |||
149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (avctx->flags & AV_CODEC_FLAG_PASS1) |
150 | ✗ | warn = 1; | |
151 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 12 times.
|
32 | else if (av_opt_get_int(avctx->priv_data, "context", 0, &tmp) >= 0 && |
152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | tmp > 0) { |
153 | ✗ | warn = av_opt_get_int(avctx->priv_data, "non_deterministic", 0, &tmp) < 0 | |
154 | ✗ | || !tmp; | |
155 | } | ||
156 | // huffyuv does not support these with multiple frame threads currently | ||
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (warn) { |
158 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
159 | "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n"); | ||
160 | ✗ | avctx->thread_count = 1; | |
161 | } | ||
162 | } | ||
163 | |||
164 |
2/2✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 15629 times.
|
17187 | if(!avctx->thread_count) { |
165 | 1558 | avctx->thread_count = av_cpu_count(); | |
166 | 1558 | avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS); | |
167 | } | ||
168 | |||
169 |
2/2✓ Branch 0 taken 15626 times.
✓ Branch 1 taken 1561 times.
|
17187 | if(avctx->thread_count <= 1) |
170 | 15626 | return 0; | |
171 | |||
172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
|
1561 | if(avctx->thread_count > MAX_THREADS) |
173 | ✗ | return AVERROR(EINVAL); | |
174 | |||
175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
|
1561 | av_assert0(!avctx->internal->frame_thread_encoder); |
176 | 1561 | c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext)); | |
177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
|
1561 | if(!c) |
178 | ✗ | return AVERROR(ENOMEM); | |
179 | |||
180 | 1561 | c->parent_avctx = avctx; | |
181 | |||
182 | 1561 | ret = ff_pthread_init(c, thread_ctx_offsets); | |
183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
|
1561 | if (ret < 0) |
184 | ✗ | goto fail; | |
185 | 1561 | atomic_init(&c->exit, 0); | |
186 | |||
187 | 1561 | c->max_tasks = avctx->thread_count + 2; | |
188 |
2/2✓ Branch 0 taken 15598 times.
✓ Branch 1 taken 1561 times.
|
17159 | for (unsigned j = 0; j < c->max_tasks; j++) { |
189 |
1/2✓ Branch 1 taken 15598 times.
✗ Branch 2 not taken.
|
15598 | if (!(c->tasks[j].indata = av_frame_alloc()) || |
190 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 15598 times.
|
15598 | !(c->tasks[j].outdata = av_packet_alloc())) { |
191 | ✗ | ret = AVERROR(ENOMEM); | |
192 | ✗ | goto fail; | |
193 | } | ||
194 | } | ||
195 | |||
196 |
2/2✓ Branch 0 taken 12476 times.
✓ Branch 1 taken 1561 times.
|
14037 | for(i=0; i<avctx->thread_count ; i++){ |
197 | void *tmpv; | ||
198 | 12476 | thread_avctx = avcodec_alloc_context3(avctx->codec); | |
199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12476 times.
|
12476 | if (!thread_avctx) { |
200 | ✗ | ret = AVERROR(ENOMEM); | |
201 | ✗ | goto fail; | |
202 | } | ||
203 | 12476 | tmpv = thread_avctx->priv_data; | |
204 | 12476 | *thread_avctx = *avctx; | |
205 | 12476 | thread_avctx->priv_data = tmpv; | |
206 | 12476 | thread_avctx->internal = NULL; | |
207 | 12476 | thread_avctx->hw_frames_ctx = NULL; | |
208 | 12476 | ret = av_opt_copy(thread_avctx, avctx); | |
209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12476 times.
|
12476 | if (ret < 0) |
210 | ✗ | goto fail; | |
211 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 12344 times.
|
12476 | if (avctx->codec->priv_class) { |
212 | 132 | ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data); | |
213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
|
132 | if (ret < 0) |
214 | ✗ | goto fail; | |
215 | } | ||
216 | 12476 | thread_avctx->thread_count = 1; | |
217 | 12476 | thread_avctx->active_thread_type &= ~FF_THREAD_FRAME; | |
218 | |||
219 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12476 times.
|
12476 | if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0) |
220 | ✗ | goto fail; | |
221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12476 times.
|
12476 | av_assert0(!thread_avctx->internal->frame_thread_encoder); |
222 | 12476 | thread_avctx->internal->frame_thread_encoder = c; | |
223 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12476 times.
|
12476 | if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) { |
224 | ✗ | ret = AVERROR(ret); | |
225 | ✗ | goto fail; | |
226 | } | ||
227 | } | ||
228 | |||
229 | 1561 | avctx->active_thread_type = FF_THREAD_FRAME; | |
230 | |||
231 | 1561 | return 0; | |
232 | ✗ | fail: | |
233 | ✗ | avcodec_close(thread_avctx); | |
234 | ✗ | av_freep(&thread_avctx); | |
235 | ✗ | avctx->thread_count = i; | |
236 | ✗ | av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n"); | |
237 | ✗ | ff_frame_thread_encoder_free(avctx); | |
238 | ✗ | return ret; | |
239 | } | ||
240 | |||
241 | 1561 | av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx) | |
242 | { | ||
243 | 1561 | ThreadContext *c= avctx->internal->frame_thread_encoder; | |
244 | |||
245 | /* In case initializing the mutexes/condition variables failed, | ||
246 | * they must not be used. In this case the thread_count is zero | ||
247 | * as no thread has been initialized yet. */ | ||
248 |
1/2✓ Branch 0 taken 1561 times.
✗ Branch 1 not taken.
|
1561 | if (avctx->thread_count > 0) { |
249 | 1561 | pthread_mutex_lock(&c->task_fifo_mutex); | |
250 | 1561 | atomic_store(&c->exit, 1); | |
251 | 1561 | pthread_cond_broadcast(&c->task_fifo_cond); | |
252 | 1561 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
253 | |||
254 |
2/2✓ Branch 0 taken 12476 times.
✓ Branch 1 taken 1561 times.
|
14037 | for (int i = 0; i < avctx->thread_count; i++) |
255 | 12476 | pthread_join(c->worker[i], NULL); | |
256 | } | ||
257 | |||
258 |
2/2✓ Branch 0 taken 15598 times.
✓ Branch 1 taken 1561 times.
|
17159 | for (unsigned i = 0; i < c->max_tasks; i++) { |
259 | 15598 | av_frame_free(&c->tasks[i].indata); | |
260 | 15598 | av_packet_free(&c->tasks[i].outdata); | |
261 | } | ||
262 | |||
263 | 1561 | ff_pthread_free(c, thread_ctx_offsets); | |
264 | 1561 | av_freep(&avctx->internal->frame_thread_encoder); | |
265 | 1561 | } | |
266 | |||
267 | 59953 | int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
268 | AVFrame *frame, int *got_packet_ptr) | ||
269 | { | ||
270 | 59953 | ThreadContext *c = avctx->internal->frame_thread_encoder; | |
271 | Task *outtask; | ||
272 | |||
273 | av_assert1(!*got_packet_ptr); | ||
274 | |||
275 |
2/2✓ Branch 0 taken 55163 times.
✓ Branch 1 taken 4790 times.
|
59953 | if(frame){ |
276 | 55163 | av_frame_move_ref(c->tasks[c->task_index].indata, frame); | |
277 | |||
278 | 55163 | pthread_mutex_lock(&c->task_fifo_mutex); | |
279 | 55163 | c->task_index = (c->task_index + 1) % c->max_tasks; | |
280 | 55163 | pthread_cond_signal(&c->task_fifo_cond); | |
281 | 55163 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
282 | } | ||
283 | |||
284 | 59953 | outtask = &c->tasks[c->finished_task_index]; | |
285 | 59953 | pthread_mutex_lock(&c->finished_task_mutex); | |
286 | /* The access to task_index in the following code is ok, | ||
287 | * because it is only ever changed by the main thread. */ | ||
288 |
4/4✓ Branch 0 taken 58392 times.
✓ Branch 1 taken 1561 times.
✓ Branch 2 taken 55163 times.
✓ Branch 3 taken 3229 times.
|
59953 | if (c->task_index == c->finished_task_index || |
289 |
2/2✓ Branch 0 taken 3579 times.
✓ Branch 1 taken 51584 times.
|
55163 | (frame && !outtask->finished && |
290 |
2/2✓ Branch 0 taken 3229 times.
✓ Branch 1 taken 350 times.
|
3579 | (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) { |
291 | 4790 | pthread_mutex_unlock(&c->finished_task_mutex); | |
292 | 4790 | return 0; | |
293 | } | ||
294 |
2/2✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 55163 times.
|
56996 | while (!outtask->finished) { |
295 | 1833 | pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex); | |
296 | } | ||
297 | 55163 | pthread_mutex_unlock(&c->finished_task_mutex); | |
298 | /* We now own outtask completely: No worker thread touches it any more, | ||
299 | * because there is no outstanding task with this index. */ | ||
300 | 55163 | outtask->finished = 0; | |
301 | 55163 | av_packet_move_ref(pkt, outtask->outdata); | |
302 | 55163 | *got_packet_ptr = outtask->got_packet; | |
303 | 55163 | c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks; | |
304 | |||
305 | 55163 | return outtask->return_code; | |
306 | } | ||
307 |