| 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 | * Libavfilter multithreading support | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <stddef.h> | ||
| 25 | |||
| 26 | #include "libavutil/error.h" | ||
| 27 | #include "libavutil/macros.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/slicethread.h" | ||
| 30 | |||
| 31 | #include "avfilter.h" | ||
| 32 | #include "avfilter_internal.h" | ||
| 33 | |||
| 34 | typedef struct ThreadContext { | ||
| 35 | AVFilterGraph *graph; | ||
| 36 | AVSliceThread *thread; | ||
| 37 | avfilter_action_func *func; | ||
| 38 | |||
| 39 | /* per-execute parameters */ | ||
| 40 | AVFilterContext *ctx; | ||
| 41 | void *arg; | ||
| 42 | int *rets; | ||
| 43 | } ThreadContext; | ||
| 44 | |||
| 45 | 29483 | static int worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads) | |
| 46 | { | ||
| 47 | 29483 | ThreadContext *c = priv; | |
| 48 | 29483 | int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs); | |
| 49 |
2/2✓ Branch 0 taken 369 times.
✓ Branch 1 taken 29114 times.
|
29483 | if (c->rets) |
| 50 | 369 | c->rets[jobnr] = ret; | |
| 51 | 29483 | return 0; | |
| 52 | } | ||
| 53 | |||
| 54 | 3911 | static void slice_thread_uninit(ThreadContext *c) | |
| 55 | { | ||
| 56 | 3911 | avpriv_slicethread_free(&c->thread); | |
| 57 | 3911 | } | |
| 58 | |||
| 59 | 3729 | static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, | |
| 60 | void *arg, int *ret, int nb_jobs) | ||
| 61 | { | ||
| 62 | 3729 | ThreadContext *c = fffiltergraph(ctx->graph)->thread; | |
| 63 | |||
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3729 times.
|
3729 | if (nb_jobs <= 0) |
| 65 | ✗ | return 0; | |
| 66 | 3729 | c->ctx = ctx; | |
| 67 | 3729 | c->arg = arg; | |
| 68 | 3729 | c->func = func; | |
| 69 | 3729 | c->rets = ret; | |
| 70 | |||
| 71 | 3729 | avpriv_slicethread_execute2(c->thread, nb_jobs, 0); | |
| 72 | 3729 | return 0; | |
| 73 | } | ||
| 74 | |||
| 75 | 3911 | static int thread_init_internal(ThreadContext *c, int nb_threads) | |
| 76 | { | ||
| 77 | 3911 | nb_threads = avpriv_slicethread_create2(&c->thread, c, worker_func, NULL, nb_threads); | |
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3911 times.
|
3911 | if (nb_threads <= 1) |
| 79 | ✗ | avpriv_slicethread_free(&c->thread); | |
| 80 | 3911 | return FFMAX(nb_threads, 1); | |
| 81 | } | ||
| 82 | |||
| 83 | 16580 | int ff_graph_thread_init(FFFilterGraph *graphi) | |
| 84 | { | ||
| 85 | 16580 | AVFilterGraph *graph = &graphi->p; | |
| 86 | int ret; | ||
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 12669 times.
✓ Branch 1 taken 3911 times.
|
16580 | if (graph->nb_threads == 1) { |
| 89 | 12669 | graph->thread_type = 0; | |
| 90 | 12669 | return 0; | |
| 91 | } | ||
| 92 | |||
| 93 | 3911 | graphi->thread = av_mallocz(sizeof(ThreadContext)); | |
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3911 times.
|
3911 | if (!graphi->thread) |
| 95 | ✗ | return AVERROR(ENOMEM); | |
| 96 | |||
| 97 | 3911 | ret = thread_init_internal(graphi->thread, graph->nb_threads); | |
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3911 times.
|
3911 | if (ret <= 1) { |
| 99 | ✗ | av_freep(&graphi->thread); | |
| 100 | ✗ | graph->thread_type = 0; | |
| 101 | ✗ | graph->nb_threads = 1; | |
| 102 | ✗ | return (ret < 0) ? ret : 0; | |
| 103 | } | ||
| 104 | 3911 | graph->nb_threads = ret; | |
| 105 | |||
| 106 | 3911 | graphi->thread_execute = thread_execute; | |
| 107 | |||
| 108 | 3911 | return 0; | |
| 109 | } | ||
| 110 | |||
| 111 | 16580 | void ff_graph_thread_free(FFFilterGraph *graph) | |
| 112 | { | ||
| 113 |
2/2✓ Branch 0 taken 3911 times.
✓ Branch 1 taken 12669 times.
|
16580 | if (graph->thread) |
| 114 | 3911 | slice_thread_uninit(graph->thread); | |
| 115 | 16580 | av_freep(&graph->thread); | |
| 116 | 16580 | } | |
| 117 |