FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/frame_thread_encoder.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 130 162 80.2%
Branches: 68 100 68.0%

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