FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/frame_thread_encoder.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 124 153 81.0%
Branches: 65 92 70.7%

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 #if FF_API_THREAD_SAFE_CALLBACKS
52 pthread_mutex_t buffer_mutex;
53 #endif
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 #if FF_API_THREAD_SAFE_CALLBACKS
74 DEFINE_OFFSET_ARRAY(ThreadContext, thread_ctx, pthread_init_cnt,
75 (OFF(buffer_mutex), OFF(task_fifo_mutex), OFF(finished_task_mutex)),
76 (OFF(task_fifo_cond), OFF(finished_task_cond)));
77 #else
78 DEFINE_OFFSET_ARRAY(ThreadContext, thread_ctx, pthread_init_cnt,
79 (OFF(task_fifo_mutex), OFF(finished_task_mutex)),
80 (OFF(task_fifo_cond), OFF(finished_task_cond)));
81 #endif
82 #undef OFF
83
84 12196 static void * attribute_align_arg worker(void *v){
85 12196 AVCodecContext *avctx = v;
86 12196 ThreadContext *c = avctx->internal->frame_thread_encoder;
87
88
2/2
✓ Branch 0 taken 66247 times.
✓ Branch 1 taken 793 times.
67040 while (!atomic_load(&c->exit)) {
89 int ret;
90 AVPacket *pkt;
91 AVFrame *frame;
92 Task *task;
93 unsigned task_index;
94
95 66247 pthread_mutex_lock(&c->task_fifo_mutex);
96
3/4
✓ Branch 0 taken 74903 times.
✓ Branch 1 taken 54844 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54844 times.
129747 while (c->next_task_index == c->task_index || atomic_load(&c->exit)) {
97
2/2
✓ Branch 0 taken 11403 times.
✓ Branch 1 taken 63500 times.
74903 if (atomic_load(&c->exit)) {
98 11403 pthread_mutex_unlock(&c->task_fifo_mutex);
99 11403 goto end;
100 }
101 63500 pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
102 }
103 54844 task_index = c->next_task_index;
104 54844 c->next_task_index = (c->next_task_index + 1) % c->max_tasks;
105 54844 pthread_mutex_unlock(&c->task_fifo_mutex);
106 /* The main thread ensures that any two outstanding tasks have
107 * different indices, ergo each worker thread owns its element
108 * of c->tasks with the exception of finished, which is shared
109 * with the main thread and guarded by finished_task_mutex. */
110 54844 task = &c->tasks[task_index];
111 54844 frame = task->indata;
112 54844 pkt = task->outdata;
113
114 54844 ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet);
115 #if FF_API_THREAD_SAFE_CALLBACKS
116 54844 pthread_mutex_lock(&c->buffer_mutex);
117 54844 av_frame_unref(frame);
118 54844 pthread_mutex_unlock(&c->buffer_mutex);
119 #endif
120 54844 pthread_mutex_lock(&c->finished_task_mutex);
121 54844 task->return_code = ret;
122 54844 task->finished = 1;
123 54844 pthread_cond_signal(&c->finished_task_cond);
124 54844 pthread_mutex_unlock(&c->finished_task_mutex);
125 }
126 793 end:
127 #if FF_API_THREAD_SAFE_CALLBACKS
128 12196 pthread_mutex_lock(&c->buffer_mutex);
129 #endif
130 12196 avcodec_close(avctx);
131 #if FF_API_THREAD_SAFE_CALLBACKS
132 12196 pthread_mutex_unlock(&c->buffer_mutex);
133 #endif
134 12196 av_freep(&avctx);
135 12196 return NULL;
136 }
137
138 18425 av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
139 {
140 18425 int i=0;
141 ThreadContext *c;
142 18425 AVCodecContext *thread_avctx = NULL;
143 int ret;
144
145
2/2
✓ Branch 0 taken 18389 times.
✓ Branch 1 taken 36 times.
18425 if( !(avctx->thread_type & FF_THREAD_FRAME)
146
2/2
✓ Branch 0 taken 1601 times.
✓ Branch 1 taken 16788 times.
18389 || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
147 1637 return 0;
148
149
2/2
✓ Branch 0 taken 1523 times.
✓ Branch 1 taken 15265 times.
16788 if( !avctx->thread_count
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1523 times.
1523 && avctx->codec_id == AV_CODEC_ID_MJPEG
151 && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
152 av_log(avctx, AV_LOG_DEBUG,
153 "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
154 "or a constant quantizer if you want to use multiple cpu cores\n");
155 avctx->thread_count = 1;
156 }
157
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16785 times.
16788 if( avctx->thread_count > 1
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 && avctx->codec_id == AV_CODEC_ID_MJPEG
159 && !(avctx->flags & AV_CODEC_FLAG_QSCALE))
160 av_log(avctx, AV_LOG_WARNING,
161 "MJPEG CBR encoding works badly with frame multi-threading, consider "
162 "using -threads 1, -thread_type slice or a constant quantizer.\n");
163
164
2/2
✓ Branch 0 taken 16776 times.
✓ Branch 1 taken 12 times.
16788 if (avctx->codec_id == AV_CODEC_ID_HUFFYUV ||
165
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 16756 times.
16776 avctx->codec_id == AV_CODEC_ID_FFVHUFF) {
166 32 int warn = 0;
167 int64_t tmp;
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (avctx->flags & AV_CODEC_FLAG_PASS1)
170 warn = 1;
171
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 &&
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 tmp > 0) {
173 warn = av_opt_get_int(avctx->priv_data, "non_deterministic", 0, &tmp) < 0
174 || !tmp;
175 }
176 // huffyuv does not support these with multiple frame threads currently
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (warn) {
178 av_log(avctx, AV_LOG_WARNING,
179 "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
180 avctx->thread_count = 1;
181 }
182 }
183
184
2/2
✓ Branch 0 taken 1523 times.
✓ Branch 1 taken 15265 times.
16788 if(!avctx->thread_count) {
185 1523 avctx->thread_count = av_cpu_count();
186 1523 avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
187 }
188
189
2/2
✓ Branch 0 taken 15262 times.
✓ Branch 1 taken 1526 times.
16788 if(avctx->thread_count <= 1)
190 15262 return 0;
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1526 times.
1526 if(avctx->thread_count > MAX_THREADS)
193 return AVERROR(EINVAL);
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1526 times.
1526 av_assert0(!avctx->internal->frame_thread_encoder);
196 1526 c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1526 times.
1526 if(!c)
198 return AVERROR(ENOMEM);
199
200 1526 c->parent_avctx = avctx;
201
202 1526 ret = ff_pthread_init(c, thread_ctx_offsets);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1526 times.
1526 if (ret < 0)
204 goto fail;
205 1526 atomic_init(&c->exit, 0);
206
207 1526 c->max_tasks = avctx->thread_count + 2;
208
2/2
✓ Branch 0 taken 15248 times.
✓ Branch 1 taken 1526 times.
16774 for (unsigned j = 0; j < c->max_tasks; j++) {
209
1/2
✓ Branch 1 taken 15248 times.
✗ Branch 2 not taken.
15248 if (!(c->tasks[j].indata = av_frame_alloc()) ||
210
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15248 times.
15248 !(c->tasks[j].outdata = av_packet_alloc())) {
211 ret = AVERROR(ENOMEM);
212 goto fail;
213 }
214 }
215
216
2/2
✓ Branch 0 taken 12196 times.
✓ Branch 1 taken 1526 times.
13722 for(i=0; i<avctx->thread_count ; i++){
217 void *tmpv;
218 12196 thread_avctx = avcodec_alloc_context3(avctx->codec);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12196 times.
12196 if (!thread_avctx) {
220 ret = AVERROR(ENOMEM);
221 goto fail;
222 }
223 12196 tmpv = thread_avctx->priv_data;
224 12196 *thread_avctx = *avctx;
225 12196 thread_avctx->priv_data = tmpv;
226 12196 thread_avctx->internal = NULL;
227 12196 thread_avctx->hw_frames_ctx = NULL;
228 12196 ret = av_opt_copy(thread_avctx, avctx);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12196 times.
12196 if (ret < 0)
230 goto fail;
231
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 12064 times.
12196 if (avctx->codec->priv_class) {
232 132 ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0)
234 goto fail;
235 }
236 12196 thread_avctx->thread_count = 1;
237 12196 thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
238
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12196 times.
12196 if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0)
240 goto fail;
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12196 times.
12196 av_assert0(!thread_avctx->internal->frame_thread_encoder);
242 12196 thread_avctx->internal->frame_thread_encoder = c;
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12196 times.
12196 if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) {
244 ret = AVERROR(ret);
245 goto fail;
246 }
247 }
248
249 1526 avctx->active_thread_type = FF_THREAD_FRAME;
250
251 1526 return 0;
252 fail:
253 avcodec_close(thread_avctx);
254 av_freep(&thread_avctx);
255 avctx->thread_count = i;
256 av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
257 ff_frame_thread_encoder_free(avctx);
258 return ret;
259 }
260
261 1526 av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx)
262 {
263 1526 ThreadContext *c= avctx->internal->frame_thread_encoder;
264
265 /* In case initializing the mutexes/condition variables failed,
266 * they must not be used. In this case the thread_count is zero
267 * as no thread has been initialized yet. */
268
1/2
✓ Branch 0 taken 1526 times.
✗ Branch 1 not taken.
1526 if (avctx->thread_count > 0) {
269 1526 pthread_mutex_lock(&c->task_fifo_mutex);
270 1526 atomic_store(&c->exit, 1);
271 1526 pthread_cond_broadcast(&c->task_fifo_cond);
272 1526 pthread_mutex_unlock(&c->task_fifo_mutex);
273
274
2/2
✓ Branch 0 taken 12196 times.
✓ Branch 1 taken 1526 times.
13722 for (int i = 0; i < avctx->thread_count; i++)
275 12196 pthread_join(c->worker[i], NULL);
276 }
277
278
2/2
✓ Branch 0 taken 15248 times.
✓ Branch 1 taken 1526 times.
16774 for (unsigned i = 0; i < c->max_tasks; i++) {
279 15248 av_frame_free(&c->tasks[i].indata);
280 15248 av_packet_free(&c->tasks[i].outdata);
281 }
282
283 1526 ff_pthread_free(c, thread_ctx_offsets);
284 1526 av_freep(&avctx->internal->frame_thread_encoder);
285 1526 }
286
287 59374 int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
288 AVFrame *frame, int *got_packet_ptr)
289 {
290 59374 ThreadContext *c = avctx->internal->frame_thread_encoder;
291 Task *outtask;
292
293 av_assert1(!*got_packet_ptr);
294
295
2/2
✓ Branch 0 taken 54844 times.
✓ Branch 1 taken 4530 times.
59374 if(frame){
296 54844 av_frame_move_ref(c->tasks[c->task_index].indata, frame);
297
298 54844 pthread_mutex_lock(&c->task_fifo_mutex);
299 54844 c->task_index = (c->task_index + 1) % c->max_tasks;
300 54844 pthread_cond_signal(&c->task_fifo_cond);
301 54844 pthread_mutex_unlock(&c->task_fifo_mutex);
302 }
303
304 59374 outtask = &c->tasks[c->finished_task_index];
305 59374 pthread_mutex_lock(&c->finished_task_mutex);
306 /* The access to task_index in the following code is ok,
307 * because it is only ever changed by the main thread. */
308
4/4
✓ Branch 0 taken 57848 times.
✓ Branch 1 taken 1526 times.
✓ Branch 2 taken 54844 times.
✓ Branch 3 taken 3004 times.
59374 if (c->task_index == c->finished_task_index ||
309
2/2
✓ Branch 0 taken 3340 times.
✓ Branch 1 taken 51504 times.
54844 (frame && !outtask->finished &&
310
2/2
✓ Branch 0 taken 3004 times.
✓ Branch 1 taken 336 times.
3340 (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
311 4530 pthread_mutex_unlock(&c->finished_task_mutex);
312 4530 return 0;
313 }
314
2/2
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 54844 times.
56344 while (!outtask->finished) {
315 1500 pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
316 }
317 54844 pthread_mutex_unlock(&c->finished_task_mutex);
318 /* We now own outtask completely: No worker thread touches it any more,
319 * because there is no outstanding task with this index. */
320 54844 outtask->finished = 0;
321 54844 av_packet_move_ref(pkt, outtask->outdata);
322 54844 *got_packet_ptr = outtask->got_packet;
323 54844 c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
324
325 54844 return outtask->return_code;
326 }
327