FFmpeg
Loading...
Searching...
No Matches
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
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 */
50{
51 int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
52 && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
53 && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS);
54 if (avctx->thread_count == 1) {
55 avctx->active_thread_type = 0;
56 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
58 } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS &&
61 } else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) {
62 avctx->thread_count = 1;
63 avctx->active_thread_type = 0;
64 }
65
66 if (avctx->thread_count > MAX_AUTO_THREADS)
68 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
70}
71
73{
75
77 return ff_slice_thread_init(avctx);
78 else if (avctx->active_thread_type&FF_THREAD_FRAME)
79 return ff_frame_thread_init(avctx);
80
81 return 0;
82}
83
85{
88 else
90}
91
92av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
93{
94 unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
95 const unsigned *cur_offset = offsets;
96
97 *(unsigned*)((char*)obj + offsets[0]) = 0;
98
99 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
100 pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
101 for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
102 pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
103}
104
105av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
106{
107 const unsigned *cur_offset = offsets;
108 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 }
122
123fail:
124 *(unsigned*)((char*)obj + offsets[0]) = cnt;
125 return err;
126}
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1590
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition avcodec.h:1591
static av_always_inline const FFCodec * ffcodec(const AVCodec *codec)
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
#define fail
Definition test.h:479
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition avcodec.h:314
#define AV_CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.
Definition avcodec.h:351
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
static const int offsets[]
Definition hevc_pel.c:34
static av_cold void validate_thread_parameters(AVCodecContext *avctx)
Set the threading algorithms used.
Definition pthread.c:49
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition pthread.c:92
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:105
av_cold int ff_thread_init(AVCodecContext *avctx)
Definition pthread.c:72
#define PTHREAD_INIT_LOOP(type)
av_cold void ff_thread_free(AVCodecContext *avctx)
Definition pthread.c:84
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition os2threads.h:144
_fmutex pthread_mutex_t
Definition os2threads.h:53
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition os2threads.h:112
int(* cond)(enum AVPixelFormat pix_fmt)
av_cold int ff_frame_thread_init(AVCodecContext *avctx)
av_cold void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
void ff_slice_thread_free(AVCodecContext *avctx)
#define THREAD_SENTINEL
#define MAX_AUTO_THREADS
int ff_slice_thread_init(AVCodecContext *avctx)
static AVMutex mutex
Definition resman.c:61
main external API structure.
Definition avcodec.h:443
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
int active_thread_type
Which multithreading methods are in use by the codec.
Definition avcodec.h:1598
const struct AVCodec * codec
Definition avcodec.h:452
int thread_type
Which multithreading methods to use.
Definition avcodec.h:1589
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
int capabilities
Codec capabilities.
Definition codec.h:194
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
#define av_log(a,...)