FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 45 46 97.8%
Functions: 5 5 100.0%
Branches: 35 42 83.3%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2004 Roman Shaposhnik
3 * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
4 *
5 * Many thanks to Steven M. Schultz for providing clever ideas and
6 * to Michael Niedermayer <michaelni@gmx.at> for writing initial
7 * implementation.
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 /**
27 * @file
28 * Multithreading support functions
29 * @see doc/multithreading.txt
30 */
31
32 #include "libavutil/thread.h"
33
34 #include "avcodec.h"
35 #include "codec_internal.h"
36 #include "pthread_internal.h"
37 #include "thread.h"
38
39 /**
40 * Set the threading algorithms used.
41 *
42 * Threading requires more than one thread.
43 * Frame threading requires entire frames to be passed to the codec,
44 * and introduces extra decoding delay, so is incompatible with low_delay.
45 *
46 * @param avctx The context.
47 */
48 32041 static void validate_thread_parameters(AVCodecContext *avctx)
49 {
50 64082 int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
51
1/2
✓ Branch 0 taken 19007 times.
✗ Branch 1 not taken.
19007 && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
52
4/4
✓ Branch 0 taken 19007 times.
✓ Branch 1 taken 13034 times.
✓ Branch 2 taken 19006 times.
✓ Branch 3 taken 1 times.
51048 && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS);
53
2/2
✓ Branch 0 taken 30896 times.
✓ Branch 1 taken 1145 times.
32041 if (avctx->thread_count == 1) {
54 30896 avctx->active_thread_type = 0;
55
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1134 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1 times.
1145 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
56 10 avctx->active_thread_type = FF_THREAD_FRAME;
57
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1113 times.
1135 } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS &&
58
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 avctx->thread_type & FF_THREAD_SLICE) {
59 22 avctx->active_thread_type = FF_THREAD_SLICE;
60
1/2
✓ Branch 1 taken 1113 times.
✗ Branch 2 not taken.
1113 } else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) {
61 1113 avctx->thread_count = 1;
62 1113 avctx->active_thread_type = 0;
63 }
64
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32041 times.
32041 if (avctx->thread_count > MAX_AUTO_THREADS)
66 av_log(avctx, AV_LOG_WARNING,
67 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
68 avctx->thread_count, MAX_AUTO_THREADS);
69 32041 }
70
71 32041 int ff_thread_init(AVCodecContext *avctx)
72 {
73 32041 validate_thread_parameters(avctx);
74
75
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 32019 times.
32041 if (avctx->active_thread_type&FF_THREAD_SLICE)
76 22 return ff_slice_thread_init(avctx);
77
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 32009 times.
32019 else if (avctx->active_thread_type&FF_THREAD_FRAME)
78 10 return ff_frame_thread_init(avctx);
79
80 32009 return 0;
81 }
82
83 29 void ff_thread_free(AVCodecContext *avctx)
84 {
85
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 19 times.
29 if (avctx->active_thread_type&FF_THREAD_FRAME)
86 10 ff_frame_thread_free(avctx, avctx->thread_count);
87 else
88 19 ff_slice_thread_free(avctx);
89 29 }
90
91 2161 av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
92 {
93 2161 unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
94 2161 const unsigned *cur_offset = offsets;
95
96 2161 *(unsigned*)((char*)obj + offsets[0]) = 0;
97
98
4/4
✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 1681 times.
✓ Branch 2 taken 3372 times.
✓ Branch 3 taken 480 times.
5533 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
99 3372 pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
100
3/4
✓ Branch 0 taken 3428 times.
✓ Branch 1 taken 2161 times.
✓ Branch 2 taken 3428 times.
✗ Branch 3 not taken.
5589 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
101 3428 pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
102 2161 }
103
104 1681 av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
105 {
106 1681 const unsigned *cur_offset = offsets;
107 1681 unsigned cnt = 0;
108 int err;
109
110 #define PTHREAD_INIT_LOOP(type) \
111 for (; *(++cur_offset) != THREAD_SENTINEL; cnt++) { \
112 pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \
113 err = pthread_ ## type ## _init(dst, NULL); \
114 if (err) { \
115 err = AVERROR(err); \
116 goto fail; \
117 } \
118 }
119
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3372 times.
✓ Branch 3 taken 3372 times.
✓ Branch 4 taken 1681 times.
5053 PTHREAD_INIT_LOOP(mutex)
120
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3428 times.
✓ Branch 3 taken 3428 times.
✓ Branch 4 taken 1681 times.
5109 PTHREAD_INIT_LOOP(cond)
121
122 1681 fail:
123 1681 *(unsigned*)((char*)obj + offsets[0]) = cnt;
124 1681 return err;
125 }
126