../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
45 / 46
97.8%
Branches:
35 / 42
83.3%
Functions:
5 / 5
100.0%

libavcodec/pthread.c
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/attributes.h"
33 #include "libavutil/thread.h"
34
35 #include "avcodec.h"
36 #include "avcodec_internal.h"
37 #include "codec_internal.h"
38 #include "pthread_internal.h"
39
40 /**
41 * Set the threading algorithms used.
42 *
43 * Threading requires more than one thread.
44 * Frame threading requires entire frames to be passed to the codec,
45 * and introduces extra decoding delay, so is incompatible with low_delay.
46 *
47 * @param avctx The context.
48 */
49 39309 static av_cold void validate_thread_parameters(AVCodecContext *avctx)
50 {
51 78618 int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
52
1/2
✓ Branch 3 → 4 taken 22938 times.
✗ Branch 3 → 6 not taken.
22938 && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
53
4/4
✓ Branch 2 → 3 taken 22938 times.
✓ Branch 2 → 6 taken 16371 times.
✓ Branch 4 → 5 taken 22937 times.
✓ Branch 4 → 6 taken 1 time.
62247 && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS);
54
2/2
✓ Branch 7 → 8 taken 37684 times.
✓ Branch 7 → 9 taken 1625 times.
39309 if (avctx->thread_count == 1) {
55 37684 avctx->active_thread_type = 0;
56
4/4
✓ Branch 9 → 10 taken 29 times.
✓ Branch 9 → 12 taken 1596 times.
✓ Branch 10 → 11 taken 16 times.
✓ Branch 10 → 12 taken 13 times.
1625 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
57 16 avctx->active_thread_type = FF_THREAD_FRAME;
58
2/2
✓ Branch 12 → 13 taken 54 times.
✓ Branch 12 → 15 taken 1555 times.
1609 } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS &&
59
1/2
✓ Branch 13 → 14 taken 54 times.
✗ Branch 13 → 15 not taken.
54 avctx->thread_type & FF_THREAD_SLICE) {
60 54 avctx->active_thread_type = FF_THREAD_SLICE;
61
1/2
✓ Branch 16 → 17 taken 1555 times.
✗ Branch 16 → 18 not taken.
1555 } else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) {
62 1555 avctx->thread_count = 1;
63 1555 avctx->active_thread_type = 0;
64 }
65
66
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 39309 times.
39309 if (avctx->thread_count > MAX_AUTO_THREADS)
67 ✗ av_log(avctx, AV_LOG_WARNING,
68 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
69 avctx->thread_count, MAX_AUTO_THREADS);
70 39309 }
71
72 39309 av_cold int ff_thread_init(AVCodecContext *avctx)
73 {
74 39309 validate_thread_parameters(avctx);
75
76
2/2
✓ Branch 3 → 4 taken 54 times.
✓ Branch 3 → 6 taken 39255 times.
39309 if (avctx->active_thread_type&FF_THREAD_SLICE)
77 54 return ff_slice_thread_init(avctx);
78
2/2
✓ Branch 6 → 7 taken 16 times.
✓ Branch 6 → 9 taken 39239 times.
39255 else if (avctx->active_thread_type&FF_THREAD_FRAME)
79 16 return ff_frame_thread_init(avctx);
80
81 39239 return 0;
82 }
83
84 65 av_cold void ff_thread_free(AVCodecContext *avctx)
85 {
86
2/2
✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 4 taken 49 times.
65 if (avctx->active_thread_type&FF_THREAD_FRAME)
87 16 ff_frame_thread_free(avctx, avctx->thread_count);
88 else
89 49 ff_slice_thread_free(avctx);
90 65 }
91
92 6456 av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
93 {
94 6456 unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
95 6456 const unsigned *cur_offset = offsets;
96
97 6456 *(unsigned*)((char*)obj + offsets[0]) = 0;
98
99
4/4
✓ Branch 5 → 6 taken 8366 times.
✓ Branch 5 → 7 taken 1932 times.
✓ Branch 6 → 3 taken 3842 times.
✓ Branch 6 → 7 taken 4524 times.
10298 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
100 3842 pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
101
3/4
✓ Branch 10 → 11 taken 3907 times.
✓ Branch 10 → 12 taken 6456 times.
✓ Branch 11 → 8 taken 3907 times.
✗ Branch 11 → 12 not taken.
10363 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
102 3907 pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
103 6456 }
104
105 1932 av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
106 {
107 1932 const unsigned *cur_offset = offsets;
108 1932 unsigned cnt = 0;
109 int err;
110
111 #define PTHREAD_INIT_LOOP(type) \
112 for (; *(++cur_offset) != THREAD_SENTINEL; cnt++) { \
113 pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \
114 err = pthread_ ## type ## _init(dst, NULL); \
115 if (err) { \
116 err = AVERROR(err); \
117 goto fail; \
118 } \
119 }
120
3/4
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 3842 times.
✓ Branch 7 → 3 taken 3842 times.
✓ Branch 7 → 8 taken 1932 times.
5774 PTHREAD_INIT_LOOP(mutex)
121
3/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 3907 times.
✓ Branch 13 → 9 taken 3907 times.
✓ Branch 13 → 14 taken 1932 times.
5839 PTHREAD_INIT_LOOP(cond)
122
123 1932 fail:
124 1932 *(unsigned*)((char*)obj + offsets[0]) = cnt;
125 1932 return err;
126 }
127