FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/frame_thread_encoder.c
Date: 2024-11-20 23:03:26
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 13052 static void * attribute_align_arg worker(void *v){
79 13052 AVCodecContext *avctx = v;
80 13052 ThreadContext *c = avctx->internal->frame_thread_encoder;
81
82
2/2
✓ Branch 0 taken 70397 times.
✓ Branch 1 taken 149 times.
70546 while (!atomic_load(&c->exit)) {
83 int ret;
84 AVPacket *pkt;
85 AVFrame *frame;
86 Task *task;
87 unsigned task_index;
88
89 70397 pthread_mutex_lock(&c->task_fifo_mutex);
90
3/4
✓ Branch 0 taken 81572 times.
✓ Branch 1 taken 57494 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 57494 times.
139066 while (c->next_task_index == c->task_index || atomic_load(&c->exit)) {
91
2/2
✓ Branch 0 taken 12903 times.
✓ Branch 1 taken 68669 times.
81572 if (atomic_load(&c->exit)) {
92 12903 pthread_mutex_unlock(&c->task_fifo_mutex);
93 12903 goto end;
94 }
95 68669 pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
96 }
97 57494 task_index = c->next_task_index;
98 57494 c->next_task_index = (c->next_task_index + 1) % c->max_tasks;
99 57494 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 57494 task = &c->tasks[task_index];
105 57494 frame = task->indata;
106 57494 pkt = task->outdata;
107
108 57494 ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet);
109 57494 pthread_mutex_lock(&c->finished_task_mutex);
110 57494 task->return_code = ret;
111 57494 task->finished = 1;
112 57494 pthread_cond_signal(&c->finished_task_cond);
113 57494 pthread_mutex_unlock(&c->finished_task_mutex);
114 }
115 149 end:
116 13052 avcodec_free_context(&avctx);
117 13052 return NULL;
118 }
119
120 20766 av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
121 {
122 20766 int i=0;
123 ThreadContext *c;
124 20766 AVCodecContext *thread_avctx = NULL;
125 20766 AVCodecParameters *par = NULL;
126 int ret;
127
128
2/2
✓ Branch 0 taken 20728 times.
✓ Branch 1 taken 38 times.
20766 if( !(avctx->thread_type & FF_THREAD_FRAME)
129
2/2
✓ Branch 0 taken 1766 times.
✓ Branch 1 taken 18962 times.
20728 || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
130 1804 return 0;
131
132
2/2
✓ Branch 0 taken 1630 times.
✓ Branch 1 taken 17332 times.
18962 if( !avctx->thread_count
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1630 times.
1630 && 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 18959 times.
18962 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 18950 times.
✓ Branch 1 taken 12 times.
18962 if (avctx->codec_id == AV_CODEC_ID_HUFFYUV ||
148
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 18930 times.
18950 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 1630 times.
✓ Branch 1 taken 17332 times.
18962 if(!avctx->thread_count) {
168 1630 avctx->thread_count = av_cpu_count();
169 1630 avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
170 }
171
172
2/2
✓ Branch 0 taken 17329 times.
✓ Branch 1 taken 1633 times.
18962 if(avctx->thread_count <= 1)
173 17329 return 0;
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1633 times.
1633 if(avctx->thread_count > MAX_THREADS)
176 return AVERROR(EINVAL);
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1633 times.
1633 av_assert0(!avctx->internal->frame_thread_encoder);
179 1633 c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1633 times.
1633 if(!c)
181 return AVERROR(ENOMEM);
182
183 1633 c->parent_avctx = avctx;
184
185 1633 ret = ff_pthread_init(c, thread_ctx_offsets);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1633 times.
1633 if (ret < 0)
187 goto fail;
188 1633 atomic_init(&c->exit, 0);
189
190 1633 c->max_tasks = avctx->thread_count + 2;
191
2/2
✓ Branch 0 taken 16318 times.
✓ Branch 1 taken 1633 times.
17951 for (unsigned j = 0; j < c->max_tasks; j++) {
192
1/2
✓ Branch 1 taken 16318 times.
✗ Branch 2 not taken.
16318 if (!(c->tasks[j].indata = av_frame_alloc()) ||
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16318 times.
16318 !(c->tasks[j].outdata = av_packet_alloc())) {
194 ret = AVERROR(ENOMEM);
195 goto fail;
196 }
197 }
198
199 1633 par = avcodec_parameters_alloc();
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1633 times.
1633 if (!par) {
201 ret = AVERROR(ENOMEM);
202 goto fail;
203 }
204
205 1633 ret = avcodec_parameters_from_context(par, avctx);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1633 times.
1633 if (ret < 0)
207 goto fail;
208
209
2/2
✓ Branch 0 taken 13052 times.
✓ Branch 1 taken 1633 times.
14685 for(i=0; i<avctx->thread_count ; i++){
210 13052 thread_avctx = avcodec_alloc_context3(avctx->codec);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13052 times.
13052 if (!thread_avctx) {
212 ret = AVERROR(ENOMEM);
213 goto fail;
214 }
215
216 13052 ret = avcodec_parameters_to_context(thread_avctx, par);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13052 times.
13052 if (ret < 0)
218 goto fail;
219
220 13052 ret = av_opt_copy(thread_avctx, avctx);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13052 times.
13052 if (ret < 0)
222 goto fail;
223
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12912 times.
13052 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 13052 thread_avctx->thread_count = 1;
229 13052 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 13052 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
13052 DUP_MATRIX(intra_matrix);
240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 13052 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
13052 DUP_MATRIX(chroma_intra_matrix);
241
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 13052 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
13052 DUP_MATRIX(inter_matrix);
242
243 #undef DUP_MATRIX
244
245 13052 thread_avctx->opaque = avctx->opaque;
246 13052 thread_avctx->get_encode_buffer = avctx->get_encode_buffer;
247 13052 thread_avctx->execute = avctx->execute;
248 13052 thread_avctx->execute2 = avctx->execute2;
249 13052 thread_avctx->stats_in = avctx->stats_in;
250
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13052 times.
13052 if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0)
252 goto fail;
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13052 times.
13052 av_assert0(!thread_avctx->internal->frame_thread_encoder);
254 13052 thread_avctx->internal->frame_thread_encoder = c;
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13052 times.
13052 if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) {
256 ret = AVERROR(ret);
257 goto fail;
258 }
259 }
260
261 1633 avcodec_parameters_free(&par);
262
263 1633 avctx->active_thread_type = FF_THREAD_FRAME;
264
265 1633 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 1633 av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx)
276 {
277 1633 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 1633 times.
✗ Branch 1 not taken.
1633 if (avctx->thread_count > 0) {
283 1633 pthread_mutex_lock(&c->task_fifo_mutex);
284 1633 atomic_store(&c->exit, 1);
285 1633 pthread_cond_broadcast(&c->task_fifo_cond);
286 1633 pthread_mutex_unlock(&c->task_fifo_mutex);
287
288
2/2
✓ Branch 0 taken 13052 times.
✓ Branch 1 taken 1633 times.
14685 for (int i = 0; i < avctx->thread_count; i++)
289 13052 pthread_join(c->worker[i], NULL);
290 }
291
292
2/2
✓ Branch 0 taken 16318 times.
✓ Branch 1 taken 1633 times.
17951 for (unsigned i = 0; i < c->max_tasks; i++) {
293 16318 av_frame_free(&c->tasks[i].indata);
294 16318 av_packet_free(&c->tasks[i].outdata);
295 }
296
297 1633 ff_pthread_free(c, thread_ctx_offsets);
298 1633 av_freep(&avctx->internal->frame_thread_encoder);
299 1633 }
300
301 63227 int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
302 AVFrame *frame, int *got_packet_ptr)
303 {
304 63227 ThreadContext *c = avctx->internal->frame_thread_encoder;
305 Task *outtask;
306
307 av_assert1(!*got_packet_ptr);
308
309
2/2
✓ Branch 0 taken 57494 times.
✓ Branch 1 taken 5733 times.
63227 if(frame){
310 57494 av_frame_move_ref(c->tasks[c->task_index].indata, frame);
311
312 57494 pthread_mutex_lock(&c->task_fifo_mutex);
313 57494 c->task_index = (c->task_index + 1) % c->max_tasks;
314 57494 pthread_cond_signal(&c->task_fifo_cond);
315 57494 pthread_mutex_unlock(&c->task_fifo_mutex);
316 }
317
318 63227 outtask = &c->tasks[c->finished_task_index];
319 63227 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 61594 times.
✓ Branch 1 taken 1633 times.
✓ Branch 2 taken 57494 times.
✓ Branch 3 taken 4100 times.
63227 if (c->task_index == c->finished_task_index ||
323
2/2
✓ Branch 0 taken 4311 times.
✓ Branch 1 taken 53183 times.
57494 (frame && !outtask->finished &&
324
2/2
✓ Branch 0 taken 4100 times.
✓ Branch 1 taken 211 times.
4311 (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
325 5733 pthread_mutex_unlock(&c->finished_task_mutex);
326 5733 return 0;
327 }
328
2/2
✓ Branch 0 taken 1580 times.
✓ Branch 1 taken 57494 times.
59074 while (!outtask->finished) {
329 1580 pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
330 }
331 57494 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 57494 outtask->finished = 0;
335 57494 av_packet_move_ref(pkt, outtask->outdata);
336 57494 *got_packet_ptr = outtask->got_packet;
337 57494 c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
338
339 57494 return outtask->return_code;
340 }
341