FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread.c
Date: 2024-07-26 21:54:09
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 32233 static void validate_thread_parameters(AVCodecContext *avctx)
49 {
50 64466 int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
51
1/2
✓ Branch 0 taken 19199 times.
✗ Branch 1 not taken.
19199 && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
52
4/4
✓ Branch 0 taken 19199 times.
✓ Branch 1 taken 13034 times.
✓ Branch 2 taken 19198 times.
✓ Branch 3 taken 1 times.
51432 && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS);
53
2/2
✓ Branch 0 taken 31088 times.
✓ Branch 1 taken 1145 times.
32233 if (avctx->thread_count == 1) {
54 31088 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 23 times.
✓ Branch 1 taken 1112 times.
1135 } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS &&
58
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 avctx->thread_type & FF_THREAD_SLICE) {
59 23 avctx->active_thread_type = FF_THREAD_SLICE;
60
1/2
✓ Branch 1 taken 1112 times.
✗ Branch 2 not taken.
1112 } else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) {
61 1112 avctx->thread_count = 1;
62 1112 avctx->active_thread_type = 0;
63 }
64
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32233 times.
32233 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 32233 }
70
71 32233 int ff_thread_init(AVCodecContext *avctx)
72 {
73 32233 validate_thread_parameters(avctx);
74
75
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 32210 times.
32233 if (avctx->active_thread_type&FF_THREAD_SLICE)
76 23 return ff_slice_thread_init(avctx);
77
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 32200 times.
32210 else if (avctx->active_thread_type&FF_THREAD_FRAME)
78 10 return ff_frame_thread_init(avctx);
79
80 32200 return 0;
81 }
82
83 30 void ff_thread_free(AVCodecContext *avctx)
84 {
85
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 if (avctx->active_thread_type&FF_THREAD_FRAME)
86 10 ff_frame_thread_free(avctx, avctx->thread_count);
87 else
88 20 ff_slice_thread_free(avctx);
89 30 }
90
91 6074 av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
92 {
93 6074 unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
94 6074 const unsigned *cur_offset = offsets;
95
96 6074 *(unsigned*)((char*)obj + offsets[0]) = 0;
97
98
4/4
✓ Branch 0 taken 7772 times.
✓ Branch 1 taken 1702 times.
✓ Branch 2 taken 3400 times.
✓ Branch 3 taken 4372 times.
9474 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
99 3400 pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
100
3/4
✓ Branch 0 taken 3456 times.
✓ Branch 1 taken 6074 times.
✓ Branch 2 taken 3456 times.
✗ Branch 3 not taken.
9530 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
101 3456 pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
102 6074 }
103
104 1702 av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
105 {
106 1702 const unsigned *cur_offset = offsets;
107 1702 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 3400 times.
✓ Branch 3 taken 3400 times.
✓ Branch 4 taken 1702 times.
5102 PTHREAD_INIT_LOOP(mutex)
120
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3456 times.
✓ Branch 3 taken 3456 times.
✓ Branch 4 taken 1702 times.
5158 PTHREAD_INIT_LOOP(cond)
121
122 1702 fail:
123 1702 *(unsigned*)((char*)obj + offsets[0]) = cnt;
124 1702 return err;
125 }
126