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 | 31462 | static void validate_thread_parameters(AVCodecContext *avctx) | |
49 | { | ||
50 | 62924 | int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) | |
51 |
1/2✓ Branch 0 taken 18640 times.
✗ Branch 1 not taken.
|
18640 | && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY) |
52 |
4/4✓ Branch 0 taken 18640 times.
✓ Branch 1 taken 12822 times.
✓ Branch 2 taken 18639 times.
✓ Branch 3 taken 1 times.
|
50102 | && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS); |
53 |
2/2✓ Branch 0 taken 30337 times.
✓ Branch 1 taken 1125 times.
|
31462 | if (avctx->thread_count == 1) { |
54 | 30337 | avctx->active_thread_type = 0; | |
55 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1115 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
1125 | } 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 19 times.
✓ Branch 1 taken 1096 times.
|
1115 | } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS && |
58 |
1/2✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 | avctx->thread_type & FF_THREAD_SLICE) { |
59 | 19 | avctx->active_thread_type = FF_THREAD_SLICE; | |
60 |
1/2✓ Branch 1 taken 1096 times.
✗ Branch 2 not taken.
|
1096 | } else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) { |
61 | 1096 | avctx->thread_count = 1; | |
62 | 1096 | avctx->active_thread_type = 0; | |
63 | } | ||
64 | |||
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31462 times.
|
31462 | 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 | 31462 | } | |
70 | |||
71 | 31462 | int ff_thread_init(AVCodecContext *avctx) | |
72 | { | ||
73 | 31462 | validate_thread_parameters(avctx); | |
74 | |||
75 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 31443 times.
|
31462 | if (avctx->active_thread_type&FF_THREAD_SLICE) |
76 | 19 | return ff_slice_thread_init(avctx); | |
77 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 31433 times.
|
31443 | else if (avctx->active_thread_type&FF_THREAD_FRAME) |
78 | 10 | return ff_frame_thread_init(avctx); | |
79 | |||
80 | 31433 | 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 | 2127 | av_cold void ff_pthread_free(void *obj, const unsigned offsets[]) | |
92 | { | ||
93 | 2127 | unsigned cnt = *(unsigned*)((char*)obj + offsets[0]); | |
94 | 2127 | const unsigned *cur_offset = offsets; | |
95 | |||
96 | 2127 | *(unsigned*)((char*)obj + offsets[0]) = 0; | |
97 | |||
98 |
4/4✓ Branch 0 taken 3784 times.
✓ Branch 1 taken 1647 times.
✓ Branch 2 taken 3304 times.
✓ Branch 3 taken 480 times.
|
5431 | for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--) |
99 | 3304 | pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset)); | |
100 |
3/4✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 2127 times.
✓ Branch 2 taken 3360 times.
✗ Branch 3 not taken.
|
5487 | for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--) |
101 | 3360 | pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset)); | |
102 | 2127 | } | |
103 | |||
104 | 1647 | av_cold int ff_pthread_init(void *obj, const unsigned offsets[]) | |
105 | { | ||
106 | 1647 | const unsigned *cur_offset = offsets; | |
107 | 1647 | 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 3304 times.
✓ Branch 3 taken 3304 times.
✓ Branch 4 taken 1647 times.
|
4951 | PTHREAD_INIT_LOOP(mutex) |
120 |
3/4✗ Branch 1 not taken.
✓ Branch 2 taken 3360 times.
✓ Branch 3 taken 3360 times.
✓ Branch 4 taken 1647 times.
|
5007 | PTHREAD_INIT_LOOP(cond) |
121 | |||
122 | 1647 | fail: | |
123 | 1647 | *(unsigned*)((char*)obj + offsets[0]) = cnt; | |
124 | 1647 | return err; | |
125 | } | ||
126 |