../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
129 / 162
79.6%
Branches:
71 / 110
64.5%
Functions:
4 / 4
100.0%

libavcodec/frame_thread_encoder.c
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/mem.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/thread.h"
30 #include "avcodec.h"
31 #include "avcodec_internal.h"
32 #include "codec_par.h"
33 #include "encode.h"
34 #include "internal.h"
35 #include "pthread_internal.h"
36
37 #define MAX_THREADS 64
38 /* There can be as many as MAX_THREADS + 1 outstanding tasks.
39 * An additional + 1 is needed so that one can distinguish
40 * the case of zero and MAX_THREADS + 1 outstanding tasks modulo
41 * the number of buffers. */
42 #define BUFFER_SIZE (MAX_THREADS + 2)
43
44 typedef struct{
45 AVFrame *indata;
46 AVPacket *outdata;
47 int return_code;
48 int finished;
49 int got_packet;
50 } Task;
51
52 typedef struct{
53 AVCodecContext *parent_avctx;
54
55 pthread_mutex_t task_fifo_mutex; /* Used to guard (next_)task_index */
56 pthread_cond_t task_fifo_cond;
57
58 unsigned pthread_init_cnt;
59 unsigned max_tasks;
60 Task tasks[BUFFER_SIZE];
61 pthread_mutex_t finished_task_mutex; /* Guards tasks[i].finished */
62 pthread_cond_t finished_task_cond;
63
64 unsigned next_task_index;
65 unsigned task_index;
66 unsigned finished_task_index;
67
68 pthread_t worker[MAX_THREADS];
69 atomic_int exit;
70 } ThreadContext;
71
72 #define OFF(member) offsetof(ThreadContext, member)
73 DEFINE_OFFSET_ARRAY(ThreadContext, thread_ctx, pthread_init_cnt,
74 (OFF(task_fifo_mutex), OFF(finished_task_mutex)),
75 (OFF(task_fifo_cond), OFF(finished_task_cond)));
76 #undef OFF
77
78 14180 static void * attribute_align_arg worker(void *v){
79 14180 AVCodecContext *avctx = v;
80 14180 ThreadContext *c = avctx->internal->frame_thread_encoder;
81
82
2/2
✓ Branch 16 → 3 taken 73755 times.
✓ Branch 16 → 17 taken 34 times.
73789 while (!atomic_load(&c->exit)) {
83 int ret;
84 AVPacket *pkt;
85 AVFrame *frame;
86 Task *task;
87 unsigned task_index;
88
89 73755 pthread_mutex_lock(&c->task_fifo_mutex);
90
3/4
✓ Branch 9 → 5 taken 87696 times.
✓ Branch 9 → 10 taken 59609 times.
✗ Branch 10 → 5 not taken.
✓ Branch 10 → 11 taken 59609 times.
147305 while (c->next_task_index == c->task_index || atomic_load(&c->exit)) {
91
2/2
✓ Branch 5 → 6 taken 14146 times.
✓ Branch 5 → 8 taken 73550 times.
87696 if (atomic_load(&c->exit)) {
92 14146 pthread_mutex_unlock(&c->task_fifo_mutex);
93 14146 goto end;
94 }
95 73550 pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
96 }
97 59609 task_index = c->next_task_index;
98 59609 c->next_task_index = (c->next_task_index + 1) % c->max_tasks;
99 59609 pthread_mutex_unlock(&c->task_fifo_mutex);
100 /* The main thread ensures that any two outstanding tasks have
101 * different indices, ergo each worker thread owns its element
102 * of c->tasks with the exception of finished, which is shared
103 * with the main thread and guarded by finished_task_mutex. */
104 59609 task = &c->tasks[task_index];
105 59609 frame = task->indata;
106 59609 pkt = task->outdata;
107
108 59609 ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet);
109 59609 pthread_mutex_lock(&c->finished_task_mutex);
110 59609 task->return_code = ret;
111 59609 task->finished = 1;
112 59609 pthread_cond_signal(&c->finished_task_cond);
113 59609 pthread_mutex_unlock(&c->finished_task_mutex);
114 }
115 34 end:
116 14180 avcodec_free_context(&avctx);
117 14180 return NULL;
118 }
119
120 22967 av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
121 {
122 22967 int i=0;
123 ThreadContext *c;
124 22967 AVCodecContext *thread_avctx = NULL;
125 22967 AVCodecParameters *par = NULL;
126 int ret;
127
128
2/2
✓ Branch 2 → 3 taken 22911 times.
✓ Branch 2 → 4 taken 56 times.
22967 if( !(avctx->thread_type & FF_THREAD_FRAME)
129
2/2
✓ Branch 3 → 4 taken 2172 times.
✓ Branch 3 → 5 taken 20739 times.
22911 || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
130 2228 return 0;
131
132
2/2
✓ Branch 5 → 6 taken 1771 times.
✓ Branch 5 → 10 taken 18968 times.
20739 if( !avctx->thread_count
133
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 1771 times.
1771 && avctx->codec_id == AV_CODEC_ID_MJPEG
134
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
135 ✗ av_log(avctx, AV_LOG_DEBUG,
136 "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
137 "or a constant quantizer if you want to use multiple cpu cores\n");
138 ✗ avctx->thread_count = 1;
139 }
140
2/2
✓ Branch 10 → 11 taken 3 times.
✓ Branch 10 → 14 taken 20736 times.
20739 if( avctx->thread_count > 1
141
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 3 times.
3 && avctx->codec_id == AV_CODEC_ID_MJPEG
142
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ && !(avctx->flags & AV_CODEC_FLAG_QSCALE))
143 ✗ av_log(avctx, AV_LOG_WARNING,
144 "MJPEG CBR encoding works badly with frame multi-threading, consider "
145 "using -threads 1, -thread_type slice or a constant quantizer.\n");
146
147
2/2
✓ Branch 14 → 15 taken 20727 times.
✓ Branch 14 → 16 taken 12 times.
20739 if (avctx->codec_id == AV_CODEC_ID_HUFFYUV ||
148
2/2
✓ Branch 15 → 16 taken 20 times.
✓ Branch 15 → 31 taken 20707 times.
20727 avctx->codec_id == AV_CODEC_ID_FFVHUFF) {
149 32 int warn = 0;
150 int64_t tmp;
151
152
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 32 times.
32 if (avctx->flags & AV_CODEC_FLAG_PASS1)
153 ✗ warn = 1;
154
2/2
✓ Branch 19 → 20 taken 20 times.
✓ Branch 19 → 27 taken 12 times.
32 else if (av_opt_get_int(avctx->priv_data, "context", 0, &tmp) >= 0 &&
155
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 27 taken 20 times.
20 tmp > 0) {
156 ✗ warn = av_opt_get_int(avctx->priv_data, "non_deterministic", 0, &tmp) < 0
157
0/4
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ || !tmp;
158 }
159 // huffyuv does not support these with multiple frame threads currently
160
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 32 times.
32 if (warn) {
161 ✗ av_log(avctx, AV_LOG_WARNING,
162 "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
163 ✗ avctx->thread_count = 1;
164 }
165 }
166
167
2/2
✓ Branch 31 → 32 taken 1771 times.
✓ Branch 31 → 34 taken 18968 times.
20739 if(!avctx->thread_count) {
168 1771 avctx->thread_count = av_cpu_count();
169 1771 avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
170 }
171
172
2/2
✓ Branch 34 → 35 taken 18965 times.
✓ Branch 34 → 36 taken 1774 times.
20739 if(avctx->thread_count <= 1)
173 18965 return 0;
174
175
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 1774 times.
1774 if(avctx->thread_count > MAX_THREADS)
176 ✗ return AVERROR(EINVAL);
177
178
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 41 taken 1774 times.
1774 av_assert0(!avctx->internal->frame_thread_encoder);
179 1774 c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
180
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 1774 times.
1774 if(!c)
181 ✗ return AVERROR(ENOMEM);
182
183 1774 c->parent_avctx = avctx;
184
185 1774 ret = ff_pthread_init(c, thread_ctx_offsets);
186
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 1774 times.
1774 if (ret < 0)
187 ✗ goto fail;
188 1774 atomic_init(&c->exit, 0);
189
190 1774 c->max_tasks = avctx->thread_count + 2;
191
2/2
✓ Branch 54 → 48 taken 17728 times.
✓ Branch 54 → 55 taken 1774 times.
19502 for (unsigned j = 0; j < c->max_tasks; j++) {
192
1/2
✓ Branch 49 → 50 taken 17728 times.
✗ Branch 49 → 52 not taken.
17728 if (!(c->tasks[j].indata = av_frame_alloc()) ||
193
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 17728 times.
17728 !(c->tasks[j].outdata = av_packet_alloc())) {
194 ✗ ret = AVERROR(ENOMEM);
195 ✗ goto fail;
196 }
197 }
198
199 1774 par = avcodec_parameters_alloc();
200
1/2
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 58 taken 1774 times.
1774 if (!par) {
201 ✗ ret = AVERROR(ENOMEM);
202 ✗ goto fail;
203 }
204
205 1774 ret = avcodec_parameters_from_context(par, avctx);
206
1/2
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 61 taken 1774 times.
1774 if (ret < 0)
207 ✗ goto fail;
208
209
2/2
✓ Branch 97 → 62 taken 14180 times.
✓ Branch 97 → 98 taken 1774 times.
15954 for(i=0; i<avctx->thread_count ; i++){
210 14180 thread_avctx = avcodec_alloc_context3(avctx->codec);
211
1/2
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 14180 times.
14180 if (!thread_avctx) {
212 ✗ ret = AVERROR(ENOMEM);
213 ✗ goto fail;
214 }
215
216 14180 ret = avcodec_parameters_to_context(thread_avctx, par);
217
1/2
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 68 taken 14180 times.
14180 if (ret < 0)
218 ✗ goto fail;
219
220 14180 ret = av_opt_copy(thread_avctx, avctx);
221
1/2
✗ Branch 69 → 70 not taken.
✓ Branch 69 → 71 taken 14180 times.
14180 if (ret < 0)
222 ✗ goto fail;
223
2/2
✓ Branch 71 → 72 taken 156 times.
✓ Branch 71 → 75 taken 14024 times.
14180 if (avctx->codec->priv_class) {
224 156 ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
225
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 156 times.
156 if (ret < 0)
226 ✗ goto fail;
227 }
228 14180 thread_avctx->thread_count = 1;
229 14180 thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
230
231 #define DUP_MATRIX(m) \
232 if (avctx->m) { \
233 thread_avctx->m = av_memdup(avctx->m, 64 * sizeof(*avctx->m)); \
234 if (!thread_avctx->m) { \
235 ret = AVERROR(ENOMEM); \
236 goto fail; \
237 } \
238 }
239
1/4
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 79 taken 14180 times.
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 79 not taken.
14180 DUP_MATRIX(intra_matrix);
240
1/4
✗ Branch 79 → 80 not taken.
✓ Branch 79 → 83 taken 14180 times.
✗ Branch 81 → 82 not taken.
✗ Branch 81 → 83 not taken.
14180 DUP_MATRIX(chroma_intra_matrix);
241
1/4
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 87 taken 14180 times.
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 87 not taken.
14180 DUP_MATRIX(inter_matrix);
242
243 #undef DUP_MATRIX
244
245 14180 thread_avctx->opaque = avctx->opaque;
246 14180 thread_avctx->get_encode_buffer = avctx->get_encode_buffer;
247 14180 thread_avctx->execute = avctx->execute;
248 14180 thread_avctx->execute2 = avctx->execute2;
249 14180 thread_avctx->stats_in = avctx->stats_in;
250
251
1/2
✗ Branch 88 → 89 not taken.
✓ Branch 88 → 90 taken 14180 times.
14180 if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0)
252 ✗ goto fail;
253
1/2
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 93 taken 14180 times.
14180 av_assert0(!thread_avctx->internal->frame_thread_encoder);
254 14180 thread_avctx->internal->frame_thread_encoder = c;
255
1/2
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 96 taken 14180 times.
14180 if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) {
256 ✗ ret = AVERROR(ret);
257 ✗ goto fail;
258 }
259 }
260
261 1774 avcodec_parameters_free(&par);
262
263 1774 avctx->active_thread_type = FF_THREAD_FRAME;
264
265 1774 return 0;
266 ✗ fail:
267 ✗ avcodec_parameters_free(&par);
268 ✗ avcodec_free_context(&thread_avctx);
269 ✗ avctx->thread_count = i;
270 ✗ av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
271 ✗ ff_frame_thread_encoder_free(avctx);
272 ✗ return ret;
273 }
274
275 1774 av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx)
276 {
277 1774 ThreadContext *c= avctx->internal->frame_thread_encoder;
278
279 /* In case initializing the mutexes/condition variables failed,
280 * they must not be used. In this case the thread_count is zero
281 * as no thread has been initialized yet. */
282
1/2
✓ Branch 2 → 3 taken 1774 times.
✗ Branch 2 → 10 not taken.
1774 if (avctx->thread_count > 0) {
283 1774 pthread_mutex_lock(&c->task_fifo_mutex);
284 1774 atomic_store(&c->exit, 1);
285 1774 pthread_cond_broadcast(&c->task_fifo_cond);
286 1774 pthread_mutex_unlock(&c->task_fifo_mutex);
287
288
2/2
✓ Branch 9 → 7 taken 14180 times.
✓ Branch 9 → 10 taken 1774 times.
15954 for (int i = 0; i < avctx->thread_count; i++)
289 14180 pthread_join(c->worker[i], NULL);
290 }
291
292
2/2
✓ Branch 14 → 11 taken 17728 times.
✓ Branch 14 → 15 taken 1774 times.
19502 for (unsigned i = 0; i < c->max_tasks; i++) {
293 17728 av_frame_free(&c->tasks[i].indata);
294 17728 av_packet_free(&c->tasks[i].outdata);
295 }
296
297 1774 ff_pthread_free(c, thread_ctx_offsets);
298 1774 av_freep(&avctx->internal->frame_thread_encoder);
299 1774 }
300
301 64489 int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
302 AVFrame *frame, int *got_packet_ptr)
303 {
304 64489 ThreadContext *c = avctx->internal->frame_thread_encoder;
305 Task *outtask;
306
307 av_assert1(!*got_packet_ptr);
308
309
2/2
✓ Branch 2 → 3 taken 59609 times.
✓ Branch 2 → 7 taken 4880 times.
64489 if(frame){
310 59609 av_frame_move_ref(c->tasks[c->task_index].indata, frame);
311
312 59609 pthread_mutex_lock(&c->task_fifo_mutex);
313 59609 c->task_index = (c->task_index + 1) % c->max_tasks;
314 59609 pthread_cond_signal(&c->task_fifo_cond);
315 59609 pthread_mutex_unlock(&c->task_fifo_mutex);
316 }
317
318 64489 outtask = &c->tasks[c->finished_task_index];
319 64489 pthread_mutex_lock(&c->finished_task_mutex);
320 /* The access to task_index in the following code is ok,
321 * because it is only ever changed by the main thread. */
322
4/4
✓ Branch 8 → 9 taken 62715 times.
✓ Branch 8 → 12 taken 1774 times.
✓ Branch 9 → 10 taken 59609 times.
✓ Branch 9 → 14 taken 3106 times.
64489 if (c->task_index == c->finished_task_index ||
323
2/2
✓ Branch 10 → 11 taken 3326 times.
✓ Branch 10 → 14 taken 56283 times.
59609 (frame && !outtask->finished &&
324
2/2
✓ Branch 11 → 12 taken 3106 times.
✓ Branch 11 → 14 taken 220 times.
3326 (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
325 4880 pthread_mutex_unlock(&c->finished_task_mutex);
326 4880 return 0;
327 }
328
2/2
✓ Branch 16 → 15 taken 1444 times.
✓ Branch 16 → 17 taken 59609 times.
61053 while (!outtask->finished) {
329 1444 pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
330 }
331 59609 pthread_mutex_unlock(&c->finished_task_mutex);
332 /* We now own outtask completely: No worker thread touches it any more,
333 * because there is no outstanding task with this index. */
334 59609 outtask->finished = 0;
335 59609 av_packet_move_ref(pkt, outtask->outdata);
336 59609 *got_packet_ptr = outtask->got_packet;
337 59609 c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
338
339 59609 return outtask->return_code;
340 }
341