Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/pthread.c |
Date: | 2022-07-07 01:21:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 46 | 47 | 97.9% |
Branches: | 35 | 44 | 79.5% |
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 | 30052 | static void validate_thread_parameters(AVCodecContext *avctx) | |
49 | { | ||
50 | 60104 | int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) | |
51 | #if FF_API_FLAG_TRUNCATED | ||
52 |
1/2✓ Branch 0 taken 17930 times.
✗ Branch 1 not taken.
|
17930 | && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED) |
53 | #endif | ||
54 |
1/2✓ Branch 0 taken 17930 times.
✗ Branch 1 not taken.
|
17930 | && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY) |
55 |
4/4✓ Branch 0 taken 17930 times.
✓ Branch 1 taken 12122 times.
✓ Branch 2 taken 17929 times.
✓ Branch 3 taken 1 times.
|
47982 | && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS); |
56 |
2/2✓ Branch 0 taken 28947 times.
✓ Branch 1 taken 1105 times.
|
30052 | if (avctx->thread_count == 1) { |
57 | 28947 | avctx->active_thread_type = 0; | |
58 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1097 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
1105 | } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) { |
59 | 8 | avctx->active_thread_type = FF_THREAD_FRAME; | |
60 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1080 times.
|
1097 | } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS && |
61 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 | avctx->thread_type & FF_THREAD_SLICE) { |
62 | 17 | avctx->active_thread_type = FF_THREAD_SLICE; | |
63 |
1/2✓ Branch 1 taken 1080 times.
✗ Branch 2 not taken.
|
1080 | } else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) { |
64 | 1080 | avctx->thread_count = 1; | |
65 | 1080 | avctx->active_thread_type = 0; | |
66 | } | ||
67 | |||
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30052 times.
|
30052 | if (avctx->thread_count > MAX_AUTO_THREADS) |
69 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
70 | "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n", | ||
71 | avctx->thread_count, MAX_AUTO_THREADS); | ||
72 | 30052 | } | |
73 | |||
74 | 30052 | int ff_thread_init(AVCodecContext *avctx) | |
75 | { | ||
76 | 30052 | validate_thread_parameters(avctx); | |
77 | |||
78 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 30035 times.
|
30052 | if (avctx->active_thread_type&FF_THREAD_SLICE) |
79 | 17 | return ff_slice_thread_init(avctx); | |
80 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 30027 times.
|
30035 | else if (avctx->active_thread_type&FF_THREAD_FRAME) |
81 | 8 | return ff_frame_thread_init(avctx); | |
82 | |||
83 | 30027 | return 0; | |
84 | } | ||
85 | |||
86 | 25 | void ff_thread_free(AVCodecContext *avctx) | |
87 | { | ||
88 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17 times.
|
25 | if (avctx->active_thread_type&FF_THREAD_FRAME) |
89 | 8 | ff_frame_thread_free(avctx, avctx->thread_count); | |
90 | else | ||
91 | 17 | ff_slice_thread_free(avctx); | |
92 | 25 | } | |
93 | |||
94 | 2051 | av_cold void ff_pthread_free(void *obj, const unsigned offsets[]) | |
95 | { | ||
96 | 2051 | unsigned cnt = *(unsigned*)((char*)obj + offsets[0]); | |
97 | 2051 | const unsigned *cur_offset = offsets; | |
98 | |||
99 | 2051 | *(unsigned*)((char*)obj + offsets[0]) = 0; | |
100 | |||
101 |
4/4✓ Branch 0 taken 5157 times.
✓ Branch 1 taken 1582 times.
✓ Branch 2 taken 4688 times.
✓ Branch 3 taken 469 times.
|
6739 | for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--) |
102 | 4688 | pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset)); | |
103 |
3/4✓ Branch 0 taken 3214 times.
✓ Branch 1 taken 2051 times.
✓ Branch 2 taken 3214 times.
✗ Branch 3 not taken.
|
5265 | for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--) |
104 | 3214 | pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset)); | |
105 | 2051 | } | |
106 | |||
107 | 1582 | av_cold int ff_pthread_init(void *obj, const unsigned offsets[]) | |
108 | { | ||
109 | 1582 | const unsigned *cur_offset = offsets; | |
110 | 1582 | unsigned cnt = 0; | |
111 | int err; | ||
112 | |||
113 | #define PTHREAD_INIT_LOOP(type) \ | ||
114 | for (; *(++cur_offset) != THREAD_SENTINEL; cnt++) { \ | ||
115 | pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \ | ||
116 | err = pthread_ ## type ## _init(dst, NULL); \ | ||
117 | if (err) { \ | ||
118 | err = AVERROR(err); \ | ||
119 | goto fail; \ | ||
120 | } \ | ||
121 | } | ||
122 |
3/4✗ Branch 1 not taken.
✓ Branch 2 taken 4688 times.
✓ Branch 3 taken 4688 times.
✓ Branch 4 taken 1582 times.
|
6270 | PTHREAD_INIT_LOOP(mutex) |
123 |
3/4✗ Branch 1 not taken.
✓ Branch 2 taken 3214 times.
✓ Branch 3 taken 3214 times.
✓ Branch 4 taken 1582 times.
|
4796 | PTHREAD_INIT_LOOP(cond) |
124 | |||
125 | 1582 | fail: | |
126 | 1582 | *(unsigned*)((char*)obj + offsets[0]) = cnt; | |
127 | 1582 | return err; | |
128 | } | ||
129 |