| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @file | ||
| 21 | * Slice multithreading support functions | ||
| 22 | * @see doc/multithreading.txt | ||
| 23 | */ | ||
| 24 | |||
| 25 | #include "avcodec.h" | ||
| 26 | #include "codec_internal.h" | ||
| 27 | #include "internal.h" | ||
| 28 | #include "pthread_internal.h" | ||
| 29 | #include "thread.h" | ||
| 30 | |||
| 31 | #include "libavutil/attributes.h" | ||
| 32 | #include "libavutil/cpu.h" | ||
| 33 | #include "libavutil/macros.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "libavutil/slicethread.h" | ||
| 36 | |||
| 37 | typedef int (action_func)(AVCodecContext *c, void *arg); | ||
| 38 | typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr); | ||
| 39 | typedef int (main_func)(AVCodecContext *c); | ||
| 40 | |||
| 41 | typedef struct SliceThreadContext { | ||
| 42 | AVSliceThread *thread; | ||
| 43 | action_func *func; | ||
| 44 | action_func2 *func2; | ||
| 45 | main_func *mainfunc; | ||
| 46 | void *args; | ||
| 47 | int *rets; | ||
| 48 | int job_size; | ||
| 49 | } SliceThreadContext; | ||
| 50 | |||
| 51 | ✗ | static void main_function(void *priv) { | |
| 52 | ✗ | AVCodecContext *avctx = priv; | |
| 53 | ✗ | SliceThreadContext *c = avctx->internal->thread_ctx; | |
| 54 | ✗ | c->mainfunc(avctx); | |
| 55 | ✗ | } | |
| 56 | |||
| 57 | 15950 | static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads) | |
| 58 | { | ||
| 59 | 15950 | AVCodecContext *avctx = priv; | |
| 60 | 15950 | SliceThreadContext *c = avctx->internal->thread_ctx; | |
| 61 | int ret; | ||
| 62 | |||
| 63 | 15250 | ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr) | |
| 64 |
2/2✓ Branch 0 taken 15250 times.
✓ Branch 1 taken 700 times.
|
15950 | : c->func2(avctx, c->args, jobnr, threadnr); |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15950 times.
|
15950 | if (c->rets) |
| 66 | ✗ | c->rets[jobnr] = ret; | |
| 67 | 15950 | } | |
| 68 | |||
| 69 | 26 | av_cold void ff_slice_thread_free(AVCodecContext *avctx) | |
| 70 | { | ||
| 71 | 26 | SliceThreadContext *c = avctx->internal->thread_ctx; | |
| 72 | |||
| 73 | 26 | avpriv_slicethread_free(&c->thread); | |
| 74 | |||
| 75 | 26 | av_freep(&avctx->internal->thread_ctx); | |
| 76 | 26 | } | |
| 77 | |||
| 78 | 3182 | static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size) | |
| 79 | { | ||
| 80 | 3182 | SliceThreadContext *c = avctx->internal->thread_ctx; | |
| 81 | |||
| 82 |
2/4✓ Branch 0 taken 3182 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3182 times.
|
3182 | if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1) |
| 83 | ✗ | return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size); | |
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3176 times.
|
3182 | if (job_count <= 0) |
| 86 | 6 | return 0; | |
| 87 | |||
| 88 | 3176 | c->job_size = job_size; | |
| 89 | 3176 | c->args = arg; | |
| 90 | 3176 | c->func = func; | |
| 91 | 3176 | c->rets = ret; | |
| 92 | |||
| 93 | 3176 | avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc ); | |
| 94 | 3176 | return 0; | |
| 95 | } | ||
| 96 | |||
| 97 | 100 | static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count) | |
| 98 | { | ||
| 99 | 100 | SliceThreadContext *c = avctx->internal->thread_ctx; | |
| 100 | 100 | c->func2 = func2; | |
| 101 | 100 | return thread_execute(avctx, NULL, arg, ret, job_count, 0); | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count) | |
| 105 | { | ||
| 106 | ✗ | SliceThreadContext *c = avctx->internal->thread_ctx; | |
| 107 | ✗ | c->func2 = func2; | |
| 108 | ✗ | c->mainfunc = mainfunc; | |
| 109 | ✗ | return thread_execute(avctx, NULL, arg, ret, job_count, 0); | |
| 110 | } | ||
| 111 | |||
| 112 | 29 | av_cold int ff_slice_thread_init(AVCodecContext *avctx) | |
| 113 | { | ||
| 114 | SliceThreadContext *c; | ||
| 115 | 29 | int thread_count = avctx->thread_count; | |
| 116 | void (*mainfunc)(void *); | ||
| 117 | |||
| 118 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 24 times.
|
29 | if (!thread_count) { |
| 119 | 5 | int nb_cpus = av_cpu_count(); | |
| 120 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (avctx->height) |
| 121 | 5 | nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16); | |
| 122 | // use number of cores + 1 as thread count if there is more than one | ||
| 123 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (nb_cpus > 1) |
| 124 | 2 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); | |
| 125 | else | ||
| 126 | 3 | thread_count = avctx->thread_count = 1; | |
| 127 | } | ||
| 128 | |||
| 129 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 26 times.
|
29 | if (thread_count <= 1) { |
| 130 | 3 | avctx->active_thread_type = 0; | |
| 131 | 3 | return 0; | |
| 132 | } | ||
| 133 | |||
| 134 | 26 | avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c)); | |
| 135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (!c) |
| 136 | ✗ | return AVERROR(ENOMEM); | |
| 137 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
|
26 | mainfunc = ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF ? &main_function : NULL; |
| 138 | 26 | thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, | |
| 139 | mainfunc, thread_count); | ||
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (thread_count <= 1) { |
| 141 | ✗ | ff_slice_thread_free(avctx); | |
| 142 | ✗ | avctx->thread_count = 1; | |
| 143 | ✗ | avctx->active_thread_type = 0; | |
| 144 | ✗ | return thread_count < 0 ? thread_count : 0; | |
| 145 | } | ||
| 146 | 26 | avctx->thread_count = thread_count; | |
| 147 | |||
| 148 | 26 | avctx->execute = thread_execute; | |
| 149 | 26 | avctx->execute2 = thread_execute2; | |
| 150 | 26 | return 0; | |
| 151 | } | ||
| 152 |