| 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 | 13260 | static void * attribute_align_arg worker(void *v){ | |
| 79 | 13260 | AVCodecContext *avctx = v; | |
| 80 | 13260 | ThreadContext *c = avctx->internal->frame_thread_encoder; | |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 71114 times.
✓ Branch 1 taken 115 times.
|
71229 | while (!atomic_load(&c->exit)) { |
| 83 | int ret; | ||
| 84 | AVPacket *pkt; | ||
| 85 | AVFrame *frame; | ||
| 86 | Task *task; | ||
| 87 | unsigned task_index; | ||
| 88 | |||
| 89 | 71114 | pthread_mutex_lock(&c->task_fifo_mutex); | |
| 90 |
3/4✓ Branch 0 taken 83496 times.
✓ Branch 1 taken 57969 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 57969 times.
|
141465 | while (c->next_task_index == c->task_index || atomic_load(&c->exit)) { |
| 91 |
2/2✓ Branch 0 taken 13145 times.
✓ Branch 1 taken 70351 times.
|
83496 | if (atomic_load(&c->exit)) { |
| 92 | 13145 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
| 93 | 13145 | goto end; | |
| 94 | } | ||
| 95 | 70351 | pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex); | |
| 96 | } | ||
| 97 | 57969 | task_index = c->next_task_index; | |
| 98 | 57969 | c->next_task_index = (c->next_task_index + 1) % c->max_tasks; | |
| 99 | 57969 | 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 | 57969 | task = &c->tasks[task_index]; | |
| 105 | 57969 | frame = task->indata; | |
| 106 | 57969 | pkt = task->outdata; | |
| 107 | |||
| 108 | 57969 | ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet); | |
| 109 | 57969 | pthread_mutex_lock(&c->finished_task_mutex); | |
| 110 | 57969 | task->return_code = ret; | |
| 111 | 57969 | task->finished = 1; | |
| 112 | 57969 | pthread_cond_signal(&c->finished_task_cond); | |
| 113 | 57969 | pthread_mutex_unlock(&c->finished_task_mutex); | |
| 114 | } | ||
| 115 | 115 | end: | |
| 116 | 13260 | avcodec_free_context(&avctx); | |
| 117 | 13260 | return NULL; | |
| 118 | } | ||
| 119 | |||
| 120 | 21498 | av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx) | |
| 121 | { | ||
| 122 | 21498 | int i=0; | |
| 123 | ThreadContext *c; | ||
| 124 | 21498 | AVCodecContext *thread_avctx = NULL; | |
| 125 | 21498 | AVCodecParameters *par = NULL; | |
| 126 | int ret; | ||
| 127 | |||
| 128 |
2/2✓ Branch 0 taken 21451 times.
✓ Branch 1 taken 47 times.
|
21498 | if( !(avctx->thread_type & FF_THREAD_FRAME) |
| 129 |
2/2✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 19664 times.
|
21451 | || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)) |
| 130 | 1834 | return 0; | |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 18008 times.
|
19664 | if( !avctx->thread_count |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1656 times.
|
1656 | && 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 19661 times.
|
19664 | 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 19652 times.
✓ Branch 1 taken 12 times.
|
19664 | if (avctx->codec_id == AV_CODEC_ID_HUFFYUV || |
| 148 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 19632 times.
|
19652 | 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 1656 times.
✓ Branch 1 taken 18008 times.
|
19664 | if(!avctx->thread_count) { |
| 168 | 1656 | avctx->thread_count = av_cpu_count(); | |
| 169 | 1656 | avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS); | |
| 170 | } | ||
| 171 | |||
| 172 |
2/2✓ Branch 0 taken 18005 times.
✓ Branch 1 taken 1659 times.
|
19664 | if(avctx->thread_count <= 1) |
| 173 | 18005 | return 0; | |
| 174 | |||
| 175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
|
1659 | if(avctx->thread_count > MAX_THREADS) |
| 176 | ✗ | return AVERROR(EINVAL); | |
| 177 | |||
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
|
1659 | av_assert0(!avctx->internal->frame_thread_encoder); |
| 179 | 1659 | c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext)); | |
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
|
1659 | if(!c) |
| 181 | ✗ | return AVERROR(ENOMEM); | |
| 182 | |||
| 183 | 1659 | c->parent_avctx = avctx; | |
| 184 | |||
| 185 | 1659 | ret = ff_pthread_init(c, thread_ctx_offsets); | |
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
|
1659 | if (ret < 0) |
| 187 | ✗ | goto fail; | |
| 188 | 1659 | atomic_init(&c->exit, 0); | |
| 189 | |||
| 190 | 1659 | c->max_tasks = avctx->thread_count + 2; | |
| 191 |
2/2✓ Branch 0 taken 16578 times.
✓ Branch 1 taken 1659 times.
|
18237 | for (unsigned j = 0; j < c->max_tasks; j++) { |
| 192 |
1/2✓ Branch 1 taken 16578 times.
✗ Branch 2 not taken.
|
16578 | if (!(c->tasks[j].indata = av_frame_alloc()) || |
| 193 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16578 times.
|
16578 | !(c->tasks[j].outdata = av_packet_alloc())) { |
| 194 | ✗ | ret = AVERROR(ENOMEM); | |
| 195 | ✗ | goto fail; | |
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | 1659 | par = avcodec_parameters_alloc(); | |
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
|
1659 | if (!par) { |
| 201 | ✗ | ret = AVERROR(ENOMEM); | |
| 202 | ✗ | goto fail; | |
| 203 | } | ||
| 204 | |||
| 205 | 1659 | ret = avcodec_parameters_from_context(par, avctx); | |
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
|
1659 | if (ret < 0) |
| 207 | ✗ | goto fail; | |
| 208 | |||
| 209 |
2/2✓ Branch 0 taken 13260 times.
✓ Branch 1 taken 1659 times.
|
14919 | for(i=0; i<avctx->thread_count ; i++){ |
| 210 | 13260 | thread_avctx = avcodec_alloc_context3(avctx->codec); | |
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13260 times.
|
13260 | if (!thread_avctx) { |
| 212 | ✗ | ret = AVERROR(ENOMEM); | |
| 213 | ✗ | goto fail; | |
| 214 | } | ||
| 215 | |||
| 216 | 13260 | ret = avcodec_parameters_to_context(thread_avctx, par); | |
| 217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13260 times.
|
13260 | if (ret < 0) |
| 218 | ✗ | goto fail; | |
| 219 | |||
| 220 | 13260 | ret = av_opt_copy(thread_avctx, avctx); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13260 times.
|
13260 | if (ret < 0) |
| 222 | ✗ | goto fail; | |
| 223 |
2/2✓ Branch 0 taken 148 times.
✓ Branch 1 taken 13112 times.
|
13260 | if (avctx->codec->priv_class) { |
| 224 | 148 | ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data); | |
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
|
148 | if (ret < 0) |
| 226 | ✗ | goto fail; | |
| 227 | } | ||
| 228 | 13260 | thread_avctx->thread_count = 1; | |
| 229 | 13260 | 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 13260 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
13260 | DUP_MATRIX(intra_matrix); |
| 240 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 13260 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
13260 | DUP_MATRIX(chroma_intra_matrix); |
| 241 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 13260 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
13260 | DUP_MATRIX(inter_matrix); |
| 242 | |||
| 243 | #undef DUP_MATRIX | ||
| 244 | |||
| 245 | 13260 | thread_avctx->opaque = avctx->opaque; | |
| 246 | 13260 | thread_avctx->get_encode_buffer = avctx->get_encode_buffer; | |
| 247 | 13260 | thread_avctx->execute = avctx->execute; | |
| 248 | 13260 | thread_avctx->execute2 = avctx->execute2; | |
| 249 | 13260 | thread_avctx->stats_in = avctx->stats_in; | |
| 250 | |||
| 251 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13260 times.
|
13260 | if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0) |
| 252 | ✗ | goto fail; | |
| 253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13260 times.
|
13260 | av_assert0(!thread_avctx->internal->frame_thread_encoder); |
| 254 | 13260 | thread_avctx->internal->frame_thread_encoder = c; | |
| 255 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13260 times.
|
13260 | if ((ret = pthread_create(&c->worker[i], NULL, worker, thread_avctx))) { |
| 256 | ✗ | ret = AVERROR(ret); | |
| 257 | ✗ | goto fail; | |
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | 1659 | avcodec_parameters_free(&par); | |
| 262 | |||
| 263 | 1659 | avctx->active_thread_type = FF_THREAD_FRAME; | |
| 264 | |||
| 265 | 1659 | 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 | 1659 | av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx) | |
| 276 | { | ||
| 277 | 1659 | 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 1659 times.
✗ Branch 1 not taken.
|
1659 | if (avctx->thread_count > 0) { |
| 283 | 1659 | pthread_mutex_lock(&c->task_fifo_mutex); | |
| 284 | 1659 | atomic_store(&c->exit, 1); | |
| 285 | 1659 | pthread_cond_broadcast(&c->task_fifo_cond); | |
| 286 | 1659 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
| 287 | |||
| 288 |
2/2✓ Branch 0 taken 13260 times.
✓ Branch 1 taken 1659 times.
|
14919 | for (int i = 0; i < avctx->thread_count; i++) |
| 289 | 13260 | pthread_join(c->worker[i], NULL); | |
| 290 | } | ||
| 291 | |||
| 292 |
2/2✓ Branch 0 taken 16578 times.
✓ Branch 1 taken 1659 times.
|
18237 | for (unsigned i = 0; i < c->max_tasks; i++) { |
| 293 | 16578 | av_frame_free(&c->tasks[i].indata); | |
| 294 | 16578 | av_packet_free(&c->tasks[i].outdata); | |
| 295 | } | ||
| 296 | |||
| 297 | 1659 | ff_pthread_free(c, thread_ctx_offsets); | |
| 298 | 1659 | av_freep(&avctx->internal->frame_thread_encoder); | |
| 299 | 1659 | } | |
| 300 | |||
| 301 | 62754 | int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
| 302 | AVFrame *frame, int *got_packet_ptr) | ||
| 303 | { | ||
| 304 | 62754 | ThreadContext *c = avctx->internal->frame_thread_encoder; | |
| 305 | Task *outtask; | ||
| 306 | |||
| 307 | av_assert1(!*got_packet_ptr); | ||
| 308 | |||
| 309 |
2/2✓ Branch 0 taken 57969 times.
✓ Branch 1 taken 4785 times.
|
62754 | if(frame){ |
| 310 | 57969 | av_frame_move_ref(c->tasks[c->task_index].indata, frame); | |
| 311 | |||
| 312 | 57969 | pthread_mutex_lock(&c->task_fifo_mutex); | |
| 313 | 57969 | c->task_index = (c->task_index + 1) % c->max_tasks; | |
| 314 | 57969 | pthread_cond_signal(&c->task_fifo_cond); | |
| 315 | 57969 | pthread_mutex_unlock(&c->task_fifo_mutex); | |
| 316 | } | ||
| 317 | |||
| 318 | 62754 | outtask = &c->tasks[c->finished_task_index]; | |
| 319 | 62754 | 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 61095 times.
✓ Branch 1 taken 1659 times.
✓ Branch 2 taken 57969 times.
✓ Branch 3 taken 3126 times.
|
62754 | if (c->task_index == c->finished_task_index || |
| 323 |
2/2✓ Branch 0 taken 3253 times.
✓ Branch 1 taken 54716 times.
|
57969 | (frame && !outtask->finished && |
| 324 |
2/2✓ Branch 0 taken 3126 times.
✓ Branch 1 taken 127 times.
|
3253 | (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) { |
| 325 | 4785 | pthread_mutex_unlock(&c->finished_task_mutex); | |
| 326 | 4785 | return 0; | |
| 327 | } | ||
| 328 |
2/2✓ Branch 0 taken 1331 times.
✓ Branch 1 taken 57969 times.
|
59300 | while (!outtask->finished) { |
| 329 | 1331 | pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex); | |
| 330 | } | ||
| 331 | 57969 | 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 | 57969 | outtask->finished = 0; | |
| 335 | 57969 | av_packet_move_ref(pkt, outtask->outdata); | |
| 336 | 57969 | *got_packet_ptr = outtask->got_packet; | |
| 337 | 57969 | c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks; | |
| 338 | |||
| 339 | 57969 | return outtask->return_code; | |
| 340 | } | ||
| 341 |