FFmpeg
pthread.c
Go to the documentation of this file.
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  */
49 {
50  int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
53  if (avctx->thread_count == 1) {
55  } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
61  avctx->thread_count = 1;
63  }
64 
67  "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
69 }
70 
72 {
74 
79 
80  return 0;
81 }
82 
84 {
87  else
89 }
90 
91 av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
92 {
93  unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
94  const unsigned *cur_offset = offsets;
95 
96  *(unsigned*)((char*)obj + offsets[0]) = 0;
97 
98  for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
99  pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
100  for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
101  pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
102 }
103 
104 av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
105 {
106  const unsigned *cur_offset = offsets;
107  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  }
121 
122 fail:
123  *(unsigned*)((char*)obj + offsets[0]) = cnt;
124  return err;
125 }
ff_slice_thread_init
int ff_slice_thread_init(AVCodecContext *avctx)
Definition: pthread_slice.c:134
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
thread.h
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
ff_thread_init
int ff_thread_init(AVCodecContext *avctx)
Definition: pthread.c:71
thread.h
ff_pthread_free
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition: pthread.c:91
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:696
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:454
fail
#define fail()
Definition: checkasm.h:179
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1582
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
AV_CODEC_FLAG_LOW_DELAY
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:334
av_cold
#define av_cold
Definition: attributes.h:90
offsets
static const int offsets[]
Definition: hevc_pel.c:34
AVCodecContext::thread_type
int thread_type
Which multithreading methods to use.
Definition: avcodec.h:1592
validate_thread_parameters
static void validate_thread_parameters(AVCodecContext *avctx)
Set the threading algorithms used.
Definition: pthread.c:48
ff_thread_free
void ff_thread_free(AVCodecContext *avctx)
Definition: pthread.c:83
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
pthread_internal.h
FFPsyPreprocessContext::avctx
AVCodecContext * avctx
Definition: psymodel.c:94
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:509
codec_internal.h
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:306
FF_THREAD_SLICE
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1594
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:114
pthread_cond_destroy
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1593
pthread_mutex_destroy
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
pthread_cond_t
Definition: os2threads.h:58
THREAD_SENTINEL
#define THREAD_SENTINEL
Definition: pthread_internal.h:34
avcodec.h
ff_frame_thread_init
int ff_frame_thread_init(AVCodecContext *avctx)
Definition: pthread_frame.c:836
AV_CODEC_FLAG2_CHUNKS
#define AV_CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.
Definition: avcodec.h:371
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1601
ff_slice_thread_free
void ff_slice_thread_free(AVCodecContext *avctx)
Definition: pthread_slice.c:82
PTHREAD_INIT_LOOP
#define PTHREAD_INIT_LOOP(type)
ff_pthread_init
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
Definition: pthread.c:104
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:73
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
cond
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
mutex
static AVMutex mutex
Definition: log.c:46