FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/pthread.c
Date: 2026-08-30 18:45:47
Exec Total Coverage
Lines: 39 46 84.8%
Functions: 6 6 100.0%
Branches: 10 14 71.4%

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 29486 static int worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
46 {
47 29486 ThreadContext *c = priv;
48 29486 int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs);
49
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 29117 times.
29486 if (c->rets)
50 369 c->rets[jobnr] = ret;
51 29486 return 0;
52 }
53
54 3942 static void slice_thread_uninit(ThreadContext *c)
55 {
56 3942 avpriv_slicethread_free(&c->thread);
57 3942 }
58
59 3731 static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
60 void *arg, int *ret, int nb_jobs)
61 {
62 3731 ThreadContext *c = fffiltergraph(ctx->graph)->thread;
63
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3731 times.
3731 if (nb_jobs <= 0)
65 return 0;
66 3731 c->ctx = ctx;
67 3731 c->arg = arg;
68 3731 c->func = func;
69 3731 c->rets = ret;
70
71 3731 avpriv_slicethread_execute2(c->thread, nb_jobs, 0);
72 3731 return 0;
73 }
74
75 3942 static int thread_init_internal(ThreadContext *c, int nb_threads)
76 {
77 3942 nb_threads = avpriv_slicethread_create2(&c->thread, c, worker_func, NULL, nb_threads);
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3942 times.
3942 if (nb_threads <= 1)
79 avpriv_slicethread_free(&c->thread);
80 3942 return FFMAX(nb_threads, 1);
81 }
82
83 16645 int ff_graph_thread_init(FFFilterGraph *graphi)
84 {
85 16645 AVFilterGraph *graph = &graphi->p;
86 int ret;
87
88
2/2
✓ Branch 0 taken 12703 times.
✓ Branch 1 taken 3942 times.
16645 if (graph->nb_threads == 1) {
89 12703 graph->thread_type = 0;
90 12703 return 0;
91 }
92
93 3942 graphi->thread = av_mallocz(sizeof(ThreadContext));
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3942 times.
3942 if (!graphi->thread)
95 return AVERROR(ENOMEM);
96
97 3942 ret = thread_init_internal(graphi->thread, graph->nb_threads);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3942 times.
3942 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 3942 graph->nb_threads = ret;
105
106 3942 graphi->thread_execute = thread_execute;
107
108 3942 return 0;
109 }
110
111 16645 void ff_graph_thread_free(FFFilterGraph *graph)
112 {
113
2/2
✓ Branch 0 taken 3942 times.
✓ Branch 1 taken 12703 times.
16645 if (graph->thread)
114 3942 slice_thread_uninit(graph->thread);
115 16645 av_freep(&graph->thread);
116 16645 }
117