FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/frame_thread_encoder.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 129 162 79.6%
Functions: 4 4 100.0%
Branches: 71 110 64.5%

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 12748 static void * attribute_align_arg worker(void *v){
79 12748 AVCodecContext *avctx = v;
80 12748 ThreadContext *c = avctx->internal->frame_thread_encoder;
81
82
2/2
✓ Branch 0 taken 67860 times.
✓ Branch 1 taken 1126 times.
68986 while (!atomic_load(&c->exit)) {
83 int ret;
84 AVPacket *pkt;
85 AVFrame *frame;
86 Task *task;
87 unsigned task_index;
88
89 67860 pthread_mutex_lock(&c->task_fifo_mutex);
90
3/4
✓ Branch 0 taken 73778 times.
✓ Branch 1 taken 56238 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56238 times.
130016 while (c->next_task_index == c->task_index || atomic_load(&c->exit)) {
91
2/2
✓ Branch 0 taken 11622 times.
✓ Branch 1 taken 62156 times.
73778 if (atomic_load(&c->exit)) {
92 11622 pthread_mutex_unlock(&c->task_fifo_mutex);
93 11622 goto end;
94 }
95 62156 pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
96 }
97 56238 task_index = c->next_task_index;
98 56238 c->next_task_index = (c->next_task_index + 1) % c->max_tasks;
99 56238 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 56238 task = &c->tasks[task_index];
105 56238 frame = task->indata;
106 56238 pkt = task->outdata;
107
108 56238 ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet);
109 56238 pthread_mutex_lock(&c->finished_task_mutex);
110 56238 task->return_code = ret;
111 56238 task->finished = 1;
112 56238 pthread_cond_signal(&c->finished_task_cond);
113 56238 pthread_mutex_unlock(&c->finished_task_mutex);
114 }
115 1126 end:
116 12748 avcodec_free_context(&avctx);
117 12748 return NULL;
118 }
119
120 19238 av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
121 {
122 19238 int i=0;
123 ThreadContext *c;
124 19238 AVCodecContext *thread_avctx = NULL;
125 19238 AVCodecParameters *par = NULL;
126 int ret;
127
128
2/2
✓ Branch 0 taken 19200 times.
✓ Branch 1 taken 38 times.
19238 if( !(avctx->thread_type & FF_THREAD_FRAME)
129
2/2
✓ Branch 0 taken 1679 times.
✓ Branch 1 taken 17521 times.
19200 || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
130 1717 return 0;
131
132
2/2
✓ Branch 0 taken 1592 times.
✓ Branch 1 taken 15929 times.
17521 if( !avctx->thread_count
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1592 times.
1592 && avctx->codec_id == AV_CODEC_ID_MJPEG
134 && !(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 0 taken 3 times.
✓ Branch 1 taken 17518 times.
17521 if( avctx->thread_count > 1
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 && avctx->codec_id == AV_CODEC_ID_MJPEG
142 && !(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 0 taken 17509 times.
✓ Branch 1 taken 12 times.
17521 if (avctx->codec_id == AV_CODEC_ID_HUFFYUV ||
148
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 17489 times.
17509 avctx->codec_id == AV_CODEC_ID_FFVHUFF) {
149 32 int warn = 0;
150 int64_t tmp;
151
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (avctx->flags & AV_CODEC_FLAG_PASS1)
153 warn = 1;
154
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 &&
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 tmp > 0) {
156 warn = av_opt_get_int(avctx->priv_data, "non_deterministic", 0, &tmp) < 0
157 || !tmp;
158 }
159 // huffyuv does not support these with multiple frame threads currently
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 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 0 taken 1592 times.
✓ Branch 1 taken 15929 times.
17521 if(!avctx->thread_count) {
168 1592 avctx->thread_count = av_cpu_count();
169 1592 avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
170 }
171
172
2/2
✓ Branch 0 taken 15926 times.
✓ Branch 1 taken 1595 times.
17521 if(avctx->thread_count <= 1)
173 15926 return 0;
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1595 times.
1595 if(avctx->thread_count > MAX_THREADS)
176 return AVERROR(EINVAL);
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1595 times.
1595 av_assert0(!avctx->internal->frame_thread_encoder);
179 1595 c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1595 times.
1595 if(!c)
181 return AVERROR(ENOMEM);
182
183 1595 c->parent_avctx = avctx;
184
185 1595 ret = ff_pthread_init(c, thread_ctx_offsets);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1595 times.
1595 if (ret < 0)
187 goto fail;
188 1595 atomic_init(&c->exit, 0);
189
190 1595 c->max_tasks = avctx->thread_count + 2;
191
2/2
✓ Branch 0 taken 15938 times.
✓ Branch 1 taken 1595 times.
17533 for (unsigned j = 0; j < c->max_tasks; j++) {
192
1/2
✓ Branch 1 taken 15938 times.
✗ Branch 2 not taken.
15938 if (!(c->tasks[j].indata = av_frame_alloc()) ||
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15938 times.
15938 !(c->tasks[j].outdata = av_packet_alloc())) {
194 ret = AVERROR(ENOMEM);
195 goto fail;
196 }
197 }
198
199 1595 par = avcodec_parameters_alloc();
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1595 times.
1595 if (!par) {
201 ret = AVERROR(ENOMEM);
202 goto fail;
203 }
204
205 1595 ret = avcodec_parameters_from_context(par, avctx);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1595 times.
1595 if (ret < 0)
207 goto fail;
208
209
2/2
✓ Branch 0 taken 12748 times.
✓ Branch 1 taken 1595 times.
14343 for(i=0; i<avctx->thread_count ; i++){
210 12748 thread_avctx = avcodec_alloc_context3(avctx->codec);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12748 times.
12748 if (!thread_avctx) {
212 ret = AVERROR(ENOMEM);
213 goto fail;
214 }
215
216 12748 ret = avcodec_parameters_to_context(thread_avctx, par);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12748 times.
12748 if (ret < 0)
218 goto fail;
219
220 12748 ret = av_opt_copy(thread_avctx, avctx);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12748 times.
12748 if (ret < 0)
222 goto fail;
223
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12608 times.
12748 if (avctx->codec->priv_class) {
224 140 ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (ret < 0)
226 goto fail;
227 }
228 12748 thread_avctx->thread_count = 1;
229 12748 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 0 not taken.
✓ Branch 1 taken 12748 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12748 DUP_MATRIX(intra_matrix);
240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12748 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12748 DUP_MATRIX(chroma_intra_matrix);
241
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12748 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12748 DUP_MATRIX(inter_matrix);
242
243 #undef DUP_MATRIX
244
245 12748 thread_avctx->opaque = avctx->opaque;
246 12748 thread_avctx->get_encode_buffer = avctx->get_encode_buffer;
247 12748 thread_avctx->execute = avctx->execute;
248 12748 thread_avctx->execute2 = avctx->execute2;
249 12748 thread_avctx->stats_in = avctx->stats_in;
250
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12748 times.
12748 if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0)
252 goto fail;
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12748 times.
12748 av_assert0(!thread_avctx->internal->frame_thread_encoder);
254 12748 thread_avctx->internal->frame_thread_encoder = c;
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12748 times.
12748 if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) {
256 ret = AVERROR(ret);
257 goto fail;
258 }
259 }
260
261 1595 avcodec_parameters_free(&par);
262
263 1595 avctx->active_thread_type = FF_THREAD_FRAME;
264
265 1595 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 1595 av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx)
276 {
277 1595 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 0 taken 1595 times.
✗ Branch 1 not taken.
1595 if (avctx->thread_count > 0) {
283 1595 pthread_mutex_lock(&c->task_fifo_mutex);
284 1595 atomic_store(&c->exit, 1);
285 1595 pthread_cond_broadcast(&c->task_fifo_cond);
286 1595 pthread_mutex_unlock(&c->task_fifo_mutex);
287
288
2/2
✓ Branch 0 taken 12748 times.
✓ Branch 1 taken 1595 times.
14343 for (int i = 0; i < avctx->thread_count; i++)
289 12748 pthread_join(c->worker[i], NULL);
290 }
291
292
2/2
✓ Branch 0 taken 15938 times.
✓ Branch 1 taken 1595 times.
17533 for (unsigned i = 0; i < c->max_tasks; i++) {
293 15938 av_frame_free(&c->tasks[i].indata);
294 15938 av_packet_free(&c->tasks[i].outdata);
295 }
296
297 1595 ff_pthread_free(c, thread_ctx_offsets);
298 1595 av_freep(&avctx->internal->frame_thread_encoder);
299 1595 }
300
301 63532 int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
302 AVFrame *frame, int *got_packet_ptr)
303 {
304 63532 ThreadContext *c = avctx->internal->frame_thread_encoder;
305 Task *outtask;
306
307 av_assert1(!*got_packet_ptr);
308
309
2/2
✓ Branch 0 taken 56238 times.
✓ Branch 1 taken 7294 times.
63532 if(frame){
310 56238 av_frame_move_ref(c->tasks[c->task_index].indata, frame);
311
312 56238 pthread_mutex_lock(&c->task_fifo_mutex);
313 56238 c->task_index = (c->task_index + 1) % c->max_tasks;
314 56238 pthread_cond_signal(&c->task_fifo_cond);
315 56238 pthread_mutex_unlock(&c->task_fifo_mutex);
316 }
317
318 63532 outtask = &c->tasks[c->finished_task_index];
319 63532 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 0 taken 61937 times.
✓ Branch 1 taken 1595 times.
✓ Branch 2 taken 56238 times.
✓ Branch 3 taken 5699 times.
63532 if (c->task_index == c->finished_task_index ||
323
2/2
✓ Branch 0 taken 6534 times.
✓ Branch 1 taken 49704 times.
56238 (frame && !outtask->finished &&
324
2/2
✓ Branch 0 taken 5699 times.
✓ Branch 1 taken 835 times.
6534 (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
325 7294 pthread_mutex_unlock(&c->finished_task_mutex);
326 7294 return 0;
327 }
328
2/2
✓ Branch 0 taken 3301 times.
✓ Branch 1 taken 56238 times.
59539 while (!outtask->finished) {
329 3301 pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
330 }
331 56238 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 56238 outtask->finished = 0;
335 56238 av_packet_move_ref(pkt, outtask->outdata);
336 56238 *got_packet_ptr = outtask->got_packet;
337 56238 c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
338
339 56238 return outtask->return_code;
340 }
341