34 #if HAVE_SCHED_GETAFFINITY
38 #if HAVE_GETPROCESSAFFINITYMASK
43 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/sysctl.h>
85 #define MAX_BUFFERS (32+1)
160 #define MAX_AUTO_THREADS 16
164 int ret, nb_cpus = 1;
165 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
170 ret = sched_getaffinity(0,
sizeof(cpuset), &cpuset);
172 nb_cpus = CPU_COUNT(&cpuset);
174 #elif HAVE_GETPROCESSAFFINITYMASK
175 DWORD_PTR proc_aff, sys_aff;
176 ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff);
178 nb_cpus = av_popcount64(proc_aff);
179 #elif HAVE_SYSCTL && defined(HW_NCPU)
180 int mib[2] = { CTL_HW, HW_NCPU };
181 size_t len =
sizeof(nb_cpus);
183 ret = sysctl(mib, 2, &nb_cpus, &len,
NULL, 0);
186 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
187 nb_cpus = sysconf(_SC_NPROC_ONLN);
188 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
189 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
200 unsigned last_execute = 0;
225 c->
func2(avctx, c->
args, our_job, self_id);
281 c->
rets = &dummy_ret;
314 if (thread_count <= 1) {
338 for (i=0; i<thread_count; i++) {
369 if (p->
state == STATE_INPUT_READY && !fctx->
die) {
371 while (p->
state == STATE_INPUT_READY && !fctx->
die)
376 if (fctx->
die)
break;
392 p->
state = STATE_INPUT_READY;
463 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
541 if (prev_thread->
state == STATE_SETTING_UP) {
543 while (prev_thread->
state == STATE_SETTING_UP)
558 memcpy(buf, avpkt->
data, avpkt->
size);
561 p->
state = STATE_SETTING_UP;
573 while (p->
state != STATE_SETUP_FINISHED && p->
state != STATE_INPUT_READY) {
575 while (p->
state == STATE_SETTING_UP)
578 if (p->
state == STATE_GET_BUFFER) {
580 p->
state = STATE_SETTING_UP;
632 p = &fctx->
threads[finished++];
634 if (p->
state != STATE_INPUT_READY) {
636 while (p->
state != STATE_INPUT_READY)
671 if (!progress || progress[field] >= n)
return;
689 if (!progress || progress[field] >= n)
return;
697 while (progress[field] < n)
708 p->
state = STATE_SETUP_FINISHED;
718 for (i = 0; i < thread_count; i++) {
721 if (p->
state != STATE_INPUT_READY) {
723 while (p->
state != STATE_INPUT_READY)
743 for (i = 0; i < thread_count; i++) {
761 for (i = 0; i < thread_count; i++) {
804 if (thread_count <= 1) {
815 for (i = 0; i < thread_count; i++) {
841 err = codec->
init(copy);
911 if (i == MAX_BUFFERS) {
933 if (p->
state != STATE_SETTING_UP &&
935 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
955 p->
state = STATE_GET_BUFFER;
959 while (p->
state != STATE_SETTING_UP)
1001 memset(f->
data, 0,
sizeof(f->
data));
1033 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
const struct AVCodec * codec
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
struct ThreadContext ThreadContext
This structure describes decoded (raw) audio or video data.
static void pthread_join(pthread_t thread, void **value_ptr)
Context used by codec threads and stored in their AVCodecContext thread_opaque.
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
int coded_width
Bitstream width / height, may be different from width/height.
void(* flush)(AVCodecContext *)
Flush buffers.
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
Update the next thread's AVCodecContext with values from the reference thread's context.
static int avcodec_thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
AVFrame * coded_frame
the picture in the bitstream
enum AVColorRange color_range
MPEG vs JPEG YUV range.
struct AVCodecContext * owner
the AVCodecContext which ff_thread_get_buffer() was last called on
void * thread_opaque
used by multithreading to store frame-specific info
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void ff_thread_free(AVCodecContext *avctx)
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
pthread_cond_t last_job_cond
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
static int * allocate_progress(PerThreadContext *p)
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
#define MAX_BUFFERS
Max number of frame buffers that can be allocated when using frame threads.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
#define CODEC_FLAG_TRUNCATED
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
int(* init_thread_copy)(AVCodecContext *)
If defined, called on thread contexts when they are created.
static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
int( action_func)(AVCodecContext *c, void *arg)
CRITICAL_SECTION pthread_mutex_t
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
int next_decoding
The next context to submit a packet to.
AVFrame frame
Output frame (for decoding) or input (for encoding).
static int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static void validate_thread_parameters(AVCodecContext *avctx)
Set the threading algorithms used.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Context stored in the client AVCodecContext thread_opaque.
AVCodecContext * avctx
Context used to decode packets passed to this thread.
static void pthread_cond_signal(pthread_cond_t *cond)
int slice_count
slice count
int(* close)(AVCodecContext *)
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
int has_b_frames
Size of the frame reordering buffer in the decoder.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
Multithreading support functions.
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
static void frame_thread_free(AVCodecContext *avctx, int thread_count)
AVFrame released_buffers[MAX_BUFFERS]
Array of frames passed to ff_thread_release_buffer().
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
int active_thread_type
Which multithreading methods are in use by the codec.
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
static int pthread_mutex_unlock(pthread_mutex_t *m)
static int get_logical_cpus(AVCodecContext *avctx)
int capabilities
Codec capabilities.
int ff_thread_init(AVCodecContext *avctx)
int result
The result of the last codec decode/encode() call.
#define CODEC_FLAG_LOW_DELAY
void av_log(void *avcl, int level, const char *fmt,...)
int die
Set when threads should exit.
void * thread_opaque
thread opaque Can be used by execute() to store some per AVCodecContext stuff.
int progress[MAX_BUFFERS][2]
Array of progress values used by ff_thread_get_buffer().
#define CODEC_CAP_AUTO_THREADS
int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
static int pthread_mutex_destroy(pthread_mutex_t *m)
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band...
int width
picture width / height.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
pthread_mutex_t current_job_lock
#define CODEC_CAP_FRAME_THREADS
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
static void *attribute_align_arg worker(void *v)
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
#define CODEC_FLAG2_CHUNKS
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Set when the codec calls get_buffer().
Set after the codec has called ff_thread_finish_setup().
pthread_cond_t current_job_cond
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
#define attribute_align_arg
static int pthread_mutex_lock(pthread_mutex_t *m)
struct PerThreadContext PerThreadContext
Context used by codec threads and stored in their AVCodecContext thread_opaque.
void ff_thread_await_progress(AVFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
static void pthread_cond_destroy(pthread_cond_t *cond)
main external API structure.
enum PerThreadContext::@52 state
void avcodec_default_free_buffers(AVCodecContext *s)
int slice_flags
slice flags
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
static void thread_free(AVCodecContext *avctx)
Set before the codec has called ff_thread_finish_setup().
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
int delaying
Set for the first N packets, where N is the number of threads.
#define FF_INPUT_BUFFER_PADDING_SIZE
#define copy_fields(s, e)
Set when the thread is awaiting a packet.
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
PerThreadContext * threads
The contexts for each thread.
static void free_progress(AVFrame *f)
#define CODEC_CAP_SLICE_THREADS
struct FrameThreadContext * parent
static void pthread_cond_broadcast(pthread_cond_t *cond)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int64_t pkt_dts
dts copied from the AVPacket that triggered returning this frame
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
common internal api header.
common internal and external API header
static int thread_init(AVCodecContext *avctx)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
static int frame_thread_init(AVCodecContext *avctx)
struct FrameThreadContext FrameThreadContext
Context stored in the client AVCodecContext thread_opaque.
int(* update_thread_context)(AVCodecContext *dst, const AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
static void w32thread_init(void)
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
struct AVCodecInternal * internal
Private context used for internal data.
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
w32threads to pthreads wrapper
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
int * slice_offset
slice offsets in the frame in bytes
int frame_number
Frame counter, set by libavcodec.
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic)
void ff_thread_report_progress(AVFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around get_buffer() for frame-multithreaded codecs.
int( action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr)
int next_finished
The next context to return output from.
static enum AVDiscard skip_loop_filter
int(* init)(AVCodecContext *)
uint8_t ** extended_data
pointers to the data planes/channels.
This structure stores compressed data.
int allocated_buf_size
Size allocated for avpkt.data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
void * opaque
Private data of the user, can be used to carry app specific stuff.
int thread_type
Which multithreading methods to use.
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
uint8_t progress_used[MAX_BUFFERS]