37 #include "libavutil/avassert.h"
38 #include "libavutil/common.h"
39 #include "libavutil/cpu.h"
154 #define MAX_AUTO_THREADS 16
160 unsigned last_execute = 0;
185 c->
func2(avctx, c->
args, our_job, self_id);
241 c->
rets = &dummy_ret;
276 if (thread_count <= 1) {
300 for (i=0; i<thread_count; i++) {
316 #define THREAD_SAFE_CALLBACKS(avctx) \
317 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
335 while (p->
state == STATE_INPUT_READY && !fctx->
die)
338 if (fctx->
die)
break;
355 for (i = 0; i < MAX_BUFFERS; i++)
357 p->progress[i][0] = INT_MAX;
358 p->progress[i][1] = INT_MAX;
361 p->
state = STATE_INPUT_READY;
440 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
445 #if FF_API_GET_BUFFER
518 if (prev_thread->
state == STATE_SETTING_UP) {
520 while (prev_thread->
state == STATE_SETTING_UP)
543 p->
state = STATE_SETTING_UP;
561 while (p->
state != STATE_SETUP_FINISHED && p->
state != STATE_INPUT_READY) {
564 while (p->
state == STATE_SETTING_UP)
568 case STATE_GET_BUFFER:
571 case STATE_GET_FORMAT:
579 p->
state = STATE_SETTING_UP;
593 AVFrame *picture,
int *got_picture_ptr,
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)
707 if(p->
state == STATE_SETUP_FINISHED){
712 p->
state = STATE_SETUP_FINISHED;
722 for (i = 0; i < thread_count; i++) {
725 if (p->
state != STATE_INPUT_READY) {
727 while (p->
state != STATE_INPUT_READY)
752 for (i = 0; i < thread_count; i++) {
772 for (i = 0; i < thread_count; i++) {
817 if (thread_count <= 1) {
828 for (i = 0; i < thread_count; i++) {
854 err = codec->
init(copy);
942 if (p->
state != STATE_SETTING_UP &&
944 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
956 progress[0] = progress[1] = -1;
973 p->
state = STATE_GET_BUFFER;
976 while (p->
state != STATE_SETTING_UP)
1002 if (p->
state != STATE_SETTING_UP) {
1003 av_log(avctx,
AV_LOG_ERROR,
"get_format() cannot be called after ff_thread_finish_setup()\n");
1008 p->
state = STATE_GET_FORMAT;
1011 while (p->
state != STATE_SETTING_UP)
1053 if (can_direct_free) {
1108 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
1152 if (!entries || !field)
return;
1157 while ((entries[field - 1] - entries[field]) < shift){
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
int ff_thread_can_start_frame(AVCodecContext *avctx)
static int shift(int a, int b)
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Set after the codec has called ff_thread_finish_setup().
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
This structure describes decoded (raw) audio or video data.
Set when the codec calls get_buffer().
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Context used by codec threads and stored in their AVCodecContext thread_opaque.
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
int coded_width
Bitstream width / height, may be different from width/height e.g.
void(* flush)(AVCodecContext *)
Flush buffers.
#define AV_LOG_WARNING
Something somehow does not look correct.
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
uint8_t ** extended_data
pointers to the data planes/channels.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
os2threads to pthreads wrapper
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
static void *attribute_align_arg worker(void *v)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
pthread_cond_t last_job_cond
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
attribute_deprecated int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
int * slice_offset
slice offsets in the frame in bytes
enum AVPixelFormat * available_formats
Format array for get_format()
Set when the thread is awaiting a packet.
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
attribute_deprecated void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
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 void validate_thread_parameters(AVCodecContext *avctx)
Set the threading algorithms used.
#define CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
enum AVSampleFormat sample_fmt
audio sample format
Multithreading support functions.
#define THREAD_SAFE_CALLBACKS(avctx)
int requested_flags
flags passed to get_buffer() for requested_frame
int next_decoding
The next context to submit a packet to.
struct AVCodecInternal * internal
Private context used for internal data.
AVFrame frame
Output frame (for decoding) or input (for encoding).
int ff_thread_init(AVCodecContext *avctx)
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
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.
int slice_count
slice count
int(* close)(AVCodecContext *)
static int avcodec_thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
#define CODEC_FLAG_TRUNCATED
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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.
uint8_t * buf
backup storage for packet data when the input packet is not refcounted
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
int active_thread_type
Which multithreading methods are in use by the codec.
#define FF_API_GET_BUFFER
int capabilities
Codec capabilities.
int result
The result of the last codec decode/encode() call.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Set when the codec calls get_format().
static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
int die
Set when threads should exit.
static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
static void thread_free(AVCodecContext *avctx)
void ff_thread_free(AVCodecContext *avctx)
Libavcodec external API header.
void * thread_opaque
thread opaque Can be used by execute() to store some per AVCodecContext stuff.
AVPixelFormat
Pixel format.
#define CODEC_FLAG_LOW_DELAY
Force low delay.
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
uint64_t channel_layout
Audio channel layout.
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
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...
uint8_t * data
The data buffer.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
void * opaque
Private data of the user, can be used to carry app specific stuff.
int width
picture width / height.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
pthread_mutex_t current_job_lock
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
int ff_alloc_entries(AVCodecContext *avctx, int count)
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
void ff_reset_entries(AVCodecContext *avctx)
#define FF_DEBUG_VIS_MB_TYPE
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
enum PerThreadContext::@81 state
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().
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 av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
void * hwaccel_context
Hardware accelerator context.
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
enum AVMediaType codec_type
static int thread_init_internal(AVCodecContext *avctx)
int sample_rate
samples per second
main external API structure.
#define FF_THREAD_FRAME
Decode more than one frame at once.
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
does needed setup of pkt_pts/pos and such for (re)get_buffer();
int slice_flags
slice flags
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
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.
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
AVFrame * coded_frame
the picture in the bitstream
enum AVPixelFormat result_format
get_format() result
int delaying
Set for the first N packets, where N is the number of threads.
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
int(* func)(AVBPrint *dst, const char *in, const char *arg)
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
This callback is called at the beginning of each frame to get data buffer(s) for it.
PerThreadContext * threads
The contexts for each thread.
int allocate_progress
Whether to allocate progress for frame threading.
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
AVFrame * released_buffers
Array of frames passed to ff_thread_release_buffer().
struct FrameThreadContext * parent
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
pthread_cond_t * progress_cond
#define FF_DISABLE_DEPRECATION_WARNINGS
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
int released_buffers_allocated
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
int(* update_thread_context)(AVCodecContext *dst, const AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
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.
#define FF_ENABLE_DEPRECATION_WARNINGS
pthread_mutex_t * progress_mutex
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 AVHWAccel * hwaccel
Hardware accelerator in use.
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
int channels
number of audio channels
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
int( action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr)
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...
int frame_number
Frame counter, set by libavcodec.
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
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 next_finished
The next context to return output from.
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
int( action_func)(AVCodecContext *c, void *arg)
int(* init)(AVCodecContext *)
#define copy_fields(s, e)
This structure stores compressed data.
int allocated_buf_size
Size allocated for buf.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int frame_thread_init(AVCodecContext *avctx)
Set before the codec has called ff_thread_finish_setup().
int thread_type
Which multithreading methods to use.
static void frame_thread_free(AVCodecContext *avctx, int thread_count)
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)