Go to the documentation of this file.
27 #include <stdatomic.h>
140 #define THREAD_SAFE_CALLBACKS(avctx) \
141 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
206 "free the frame on failure. This is a bug, please report it.\n");
291 if (
src->hw_frames_ctx) {
303 #if FF_API_CODED_FRAME
325 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
344 if (
src->slice_count &&
src->slice_offset) {
373 f->extended_data =
f->data;
400 memory_order_relaxed);
473 AVFrame *picture,
int *got_picture_ptr,
517 p = &fctx->
threads[finished++];
541 }
while (!avpkt->
size && !*got_picture_ptr && err >= 0 && finished != fctx->
next_finished);
566 p =
f->owner[
field]->internal->thread_ctx;
570 "%p finished %d field %d\n", progress,
n,
field);
589 p =
f->owner[
field]->internal->thread_ctx;
593 "thread awaiting %d field %d from %p\n",
n,
field, progress);
637 for (
i = 0;
i < thread_count;
i++) {
667 for (
i = 0;
i < thread_count;
i++) {
686 for (
i = 0;
i < thread_count;
i++) {
734 if ((avctx->
debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->
debug_mv)
744 if (thread_count <= 1) {
767 for (
i = 0;
i < thread_count;
i++) {
795 if (!
copy->internal) {
800 *
copy->internal = *
src->internal;
801 copy->internal->thread_ctx = p;
802 copy->internal->last_pkt_props = &p->
avpkt;
813 if (!
copy->priv_data) {
818 copy->internal->is_copy = 1;
887 f->owner[0] =
f->owner[1] = avctx;
894 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
946 av_log(avctx,
AV_LOG_ERROR,
"get_format() cannot be called after ff_thread_finish_setup()\n");
980 if (!
f->f || !
f->f->buf[0])
987 f->owner[0] =
f->owner[1] =
NULL;
989 if (can_direct_free) {
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
#define HWACCEL_CAP_ASYNC_SAFE
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
#define FF_ENABLE_DEPRECATION_WARNINGS
void * hwaccel_context
Hardware accelerator context.
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
#define atomic_store(object, desired)
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
uint64_t channel_layout
Audio channel layout.
enum AVColorSpace colorspace
YUV colorspace type.
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
int sample_rate
samples per second
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
const AVClass * priv_class
AVClass for the private context.
int next_decoding
The next context to submit a packet to.
uint8_t * data
The data buffer.
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
int caps_internal
Internal hwaccel capabilities.
int ff_thread_can_start_frame(AVCodecContext *avctx)
Context stored in the client AVCodecInternal thread_ctx.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
This structure describes decoded (raw) audio or video data.
#define THREAD_SAFE_CALLBACKS(avctx)
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
int * slice_offset
slice offsets in the frame in bytes
int capabilities
Codec capabilities.
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
int debug_mv
debug motion vectors
void * av_mallocz_array(size_t nmemb, size_t size)
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
int die
Set when the thread should exit.
int next_finished
The next context to return output from.
int slice_count
slice count
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it.
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
const struct AVCodec * codec
#define copy_fields(s, e)
enum AVPixelFormat * available_formats
Format array for get_format()
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
int flags
AV_CODEC_FLAG_*.
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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.
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int has_b_frames
Size of the frame reordering buffer in the decoder.
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
int requested_flags
flags passed to get_buffer() for requested_frame
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
int(* update_thread_context)(AVCodecContext *dst, const AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
#define atomic_load(object)
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
int(* init_thread_copy)(AVCodecContext *)
If defined, called on thread contexts when they are created.
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
int slice_flags
slice flags
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
struct AVCodecInternal * internal
Private context used for internal data.
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
pthread_mutex_t async_mutex
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...
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when hwaccel is used.
AVCodecContext * avctx
Context used to decode packets passed to this thread.
void av_opt_free(void *obj)
Free all allocated objects in obj.
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
@ STATE_GET_FORMAT
Set when the codec calls get_format().
int(* init)(AVCodecContext *)
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
#define atomic_load_explicit(object, order)
#define pthread_mutex_unlock(a)
static void error(const char *err)
static void async_lock(FrameThreadContext *fctx)
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int result
The result of the last codec decode/encode() call.
int flags2
AV_CODEC_FLAG2_*.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
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 copy(const float *p1, float *p2, const int length)
void * hwaccel_priv_data
hwaccel-specific private data
int allocate_progress
Whether to allocate progress for frame threading.
static void async_unlock(FrameThreadContext *fctx)
enum AVSampleFormat sample_fmt
audio sample format
pthread_cond_t async_cond
@ STATE_GET_BUFFER
Set when the codec calls get_buffer().
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
struct FrameThreadContext * parent
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
#define FF_THREAD_FRAME
Decode more than one frame at once.
int channels
number of audio channels
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
#define i(width, name, range_min, range_max)
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
#define atomic_store_explicit(object, desired, order)
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
int ff_frame_thread_init(AVCodecContext *avctx)
void * opaque
Private data of the user, can be used to carry app specific stuff.
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
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.
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
main external API structure.
int active_thread_type
Which multithreading methods are in use by the codec.
AVFrame * released_buffers
Array of frames passed to ff_thread_release_buffer().
AVFrame * frame
Output frame (for decoding) or input (for encoding).
enum AVPixelFormat result_format
get_format() result
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
#define FF_DISABLE_DEPRECATION_WARNINGS
int(* close)(AVCodecContext *)
int coded_width
Bitstream width / height, may be different from width/height e.g.
enum AVMediaType codec_type
int delaying
Set for the first N packets, where N is the number of threads.
int frame_number
Frame counter, set by libavcodec.
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
This structure stores compressed data.
void(* flush)(AVCodecContext *)
Flush buffers.
int64_t reordered_opaque
opaque 64-bit number (generally a PTS) that will be reordered and output in AVFrame....
int width
picture width / height.
#define flags(name, subs,...)
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
#define atomic_init(obj, value)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
int released_buffers_allocated
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
PerThreadContext * threads
The contexts for each thread.
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.
#define pthread_mutex_lock(a)