34 #if HAVE_SCHED_GETAFFINITY
38 #if HAVE_GETPROCESSAFFINITYMASK
43 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/sysctl.h>
84 #define MAX_BUFFERS (32+1)
159 #define MAX_AUTO_THREADS 16
163 int ret, nb_cpus = 1;
164 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
169 ret = sched_getaffinity(0,
sizeof(cpuset), &cpuset);
171 nb_cpus = CPU_COUNT(&cpuset);
173 #elif HAVE_GETPROCESSAFFINITYMASK
174 DWORD_PTR proc_aff, sys_aff;
175 ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff);
177 nb_cpus = av_popcount64(proc_aff);
178 #elif HAVE_SYSCTL && defined(HW_NCPU)
179 int mib[2] = { CTL_HW, HW_NCPU };
180 size_t len =
sizeof(nb_cpus);
182 ret = sysctl(mib, 2, &nb_cpus, &len,
NULL, 0);
185 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
186 nb_cpus = sysconf(_SC_NPROC_ONLN);
187 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
188 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
199 unsigned last_execute = 0;
224 c->
func2(avctx, c->
args, our_job, self_id);
280 c->
rets = &dummy_ret;
313 if (thread_count <= 1) {
368 if (p->
state == STATE_INPUT_READY && !fctx->
die) {
370 while (p->
state == STATE_INPUT_READY && !fctx->
die)
375 if (fctx->
die)
break;
387 p->
state = STATE_INPUT_READY;
459 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
538 if (prev_thread->
state == STATE_SETTING_UP) {
540 while (prev_thread->
state == STATE_SETTING_UP)
555 memcpy(buf, avpkt->
data, avpkt->
size);
558 p->
state = STATE_SETTING_UP;
570 while (p->
state != STATE_SETUP_FINISHED && p->
state != STATE_INPUT_READY) {
572 while (p->
state == STATE_SETTING_UP)
575 if (p->
state == STATE_GET_BUFFER) {
577 p->
state = STATE_SETTING_UP;
628 p = &fctx->
threads[finished++];
630 if (p->
state != STATE_INPUT_READY) {
632 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;
721 if (p->
state != STATE_INPUT_READY) {
723 while (p->
state != STATE_INPUT_READY)
804 if (thread_count <= 1) {
841 err = codec->
init(copy);
903 if (i == MAX_BUFFERS) {
925 if (p->
state != STATE_SETTING_UP &&
927 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
947 p->
state = STATE_GET_BUFFER;
951 while (p->
state != STATE_SETTING_UP)
989 memset(f->
data, 0,
sizeof(f->
data));
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
struct ThreadContext ThreadContext
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 if lowres enabled.
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)
static void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
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...
void ff_thread_free(AVCodecContext *avctx)
static int bidir_refine(MpegEncContext *s, int mb_x, int mb_y)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
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.
Set before the codec has called ff_thread_finish_setup().
static int * allocate_progress(PerThreadContext *p)
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
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 ...
Set when the thread is awaiting a packet.
unsigned dsp_mask
dsp_mask could be add used to disable unwanted CPU features CPU features (i.e.
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 width
width and height of the video frame
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.
int sub_id
Some codecs need additional format info.
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.
enum PerThreadContext::@43 state
void av_log(void *avcl, int level, const char *fmt,...)
int die
Set when threads should exit.
#define CODEC_FLAG_TRUNCATED
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().
void avcodec_default_free_buffers(AVCodecContext *s)
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...
#define FF_THREAD_FRAME
Decode more than one frame at once.
int width
picture width / height.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
pthread_mutex_t current_job_lock
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
static void *attribute_align_arg worker(void *v)
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
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.
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().
int format
format of the frame, -1 if unknown or unset Values correspond to enum PixelFormat for video frames...
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
#define CODEC_FLAG_LOW_DELAY
Force low delay.
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.
AVRational sample_aspect_ratio
sample aspect ratio for the video frame, 0/1 if unknown
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int slice_flags
slice flags
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void thread_free(AVCodecContext *avctx)
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 copy_fields(s, e)
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
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)
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 from the last AVPacket that has been input into the decoder
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static int thread_init(AVCodecContext *avctx)
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic)
#define CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called from another thread, which allows faster multithreaded decoding.
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.
Set after the codec has called ff_thread_finish_setup().
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
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
audio or video frame number
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
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.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
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 *)
int allocated_buf_size
Size allocated for avpkt.data.
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Set when the codec calls get_buffer().
void * opaque
Private data of the user, can be used to carry app specific stuff.
int thread_type
Which multithreading methods to use.
uint8_t progress_used[MAX_BUFFERS]