25
#include <stdatomic.h>
198
if
(
df
->nb_f ==
df
->nb_f_allocated) {
206
if
(!
df
->f[
df
->nb_f])
209
df
->nb_f_allocated++;
214
return
df
->f[
df
->nb_f];
221
memmove(
df
->f,
df
->f + 1, (
df
->nb_f - 1) *
sizeof
(*
df
->f));
222
df
->f[--
df
->nb_f] = tmp_frame;
227
for
(
size_t
i
= 0;
i
<
df
->nb_f;
i
++)
234
for
(
size_t
i
= 0;
i
<
df
->nb_f_allocated;
i
++)
238
df
->nb_f_allocated = 0;
263
if
(p->
die
)
break
;
298
else
if
(ret < 0 && frame->buf[0])
377
#if FF_API_TICKS_PER_FRAME
399
if
(
src
->hw_frames_ctx) {
437
if
(
hwaccel
->priv_data_size) {
449
if
(
hwaccel
->update_thread_context) {
450
err =
hwaccel
->update_thread_context(dst,
src
);
524
memory_order_relaxed);
635
p =
f
->owner[
field
]->internal->thread_ctx;
639
"%p finished %d field %d\n"
, progress, n,
field
);
658
p =
f
->owner[
field
]->internal->thread_ctx;
662
"thread awaiting %d field %d from %p\n"
, n,
field
, progress);
722
for
(
i
= 0;
i
< thread_count;
i
++) {
736
#define OFF(member) offsetof(FrameThreadContext, member)
738
(
OFF
(buffer_mutex),
OFF
(hwaccel_mutex),
OFF
(async_mutex)),
739
(
OFF
(async_cond)));
742
#define OFF(member) offsetof(PerThreadContext, member)
745
(
OFF
(input_cond),
OFF
(progress_cond),
OFF
(output_cond)));
756
for
(
i
= 0;
i
< thread_count;
i
++) {
760
if
(
ctx
->internal) {
788
&
ctx
->nb_decoded_side_data);
836
copy
->nb_decoded_side_data = 0;
840
(*threads_to_free)++;
846
if
(!
copy
->internal)
848
copy
->internal->thread_ctx = p;
855
if
(!
copy
->priv_data)
873
copy
->internal->is_frame_mt = 1;
875
copy
->internal->is_copy = 1;
878
if
(!
copy
->internal->in_pkt)
882
if
(!
copy
->internal->last_pkt_props)
899
for
(
int
i
= 0;
i
<
copy
->nb_decoded_side_data;
i
++) {
902
copy
->decoded_side_data[
i
], 0);
934
if
(thread_count <= 1) {
965
for
(;
i
< thread_count; ) {
1034
av_log
(avctx,
AV_LOG_ERROR
,
"get_buffer() cannot be called after ff_thread_finish_setup()\n"
);
1058
f
->owner[0] =
f
->owner[1] = avctx;
1078
f
->owner[0] =
f
->owner[1] =
NULL
;
static void error(const char *err)
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
#define FF_ENABLE_DEPRECATION_WARNINGS
static void thread_set_name(PerThreadContext *p)
void * hwaccel_context
Legacy 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.
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
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 just let it vf default minimum maximum flags name is the option name
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
#define atomic_store(object, desired)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
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 AVPictureType initial_pict_type
static int hwaccel_serial(const AVCodecContext *avctx)
enum AVColorSpace colorspace
YUV colorspace type.
AVFrameSideData ** decoded_side_data
Array containing static side data, such as HDR10 CLL / MDCV structures.
int sample_rate
samples per second
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
const AVClass * priv_class
AVClass for the private context.
int next_decoding
The next context to submit a packet to.
#define AVERROR_EOF
End of file.
uint8_t * data
The data buffer.
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, const FFCodec *codec, int first)
int ff_thread_can_start_frame(AVCodecContext *avctx)
static void decoded_frames_free(DecodedFrames *df)
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
@ FF_THREAD_IS_FIRST_THREAD
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.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
int capabilities
Codec capabilities.
enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
Allows to synchronize objects whose lifetime is the whole decoding process among all frame threads.
AVPacket * next_pkt
Packet to be submitted to the next thread for decoding.
int ff_thread_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Submit available packets for decoding to worker threads, return a decoded frame if available.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *src, unsigned int flags)
Add a new side data entry to an array based on existing side data, taking a reference towards the con...
void * stash_hwaccel_context
int die
Set when the thread should exit.
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
int next_finished
The next context to return output from.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
void ff_hwaccel_uninit(AVCodecContext *avctx)
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
struct FFRefStructPool * progress_frame_pool
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
AVCodec p
The public AVCodec.
const struct AVCodec * codec
AVChannelLayout ch_layout
Audio channel layout.
enum AVDiscard skip_frame
Skip decoding for selected frames.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
static int update_context_from_user(AVCodecContext *dst, const AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
void av_opt_free(void *obj)
Free all allocated objects in obj.
#define HWACCEL_CAP_THREAD_SAFE
int flags
AV_CODEC_FLAG_*.
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
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.
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
int has_b_frames
Size of the frame reordering buffer in the decoder.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
#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.
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
#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.
int(* init)(struct AVCodecContext *)
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
#define HWACCEL_CAP_ASYNC_SAFE
Header providing the internals of AVHWAccel.
Describe the class of an AVClass context structure.
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.
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
int slice_flags
slice flags
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int 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.
@ AV_PICTURE_TYPE_I
Intra.
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...
void ff_thread_release_ext_buffer(ThreadFrame *f)
Unref a ThreadFrame.
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when thread-unsafe hwaccel is used.
AVCodecContext * avctx
Context used to decode packets passed to this thread.
static void * ff_refstruct_allocz(size_t size)
Equivalent to ff_refstruct_alloc_ext(size, 0, NULL, NULL)
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
#define AVPACKET_IS_EMPTY(pkt)
int level
Encoding level descriptor.
#define atomic_load_explicit(object, order)
#define pthread_mutex_unlock(a)
AVPacket * last_pkt_props
Properties (timestamps+side data) extracted from the last packet passed for decoding.
DecodedFrames df
Decoded frames from a single decode iteration.
static void async_lock(FrameThreadContext *fctx)
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
#define attribute_align_arg
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.
const AVHWAccel * stash_hwaccel
int flags2
AV_CODEC_FLAG2_*.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static void copy(const float *p1, float *p2, const int length)
void * hwaccel_priv_data
hwaccel-specific private data
int intra_only_flag
The following two fields have the same semantics as the DecodeContext field.
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
static void async_unlock(FrameThreadContext *fctx)
enum AVSampleFormat sample_fmt
audio sample format
pthread_cond_t async_cond
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
static void decoded_frames_pop(DecodedFrames *df, AVFrame *dst)
@ AV_PICTURE_TYPE_NONE
Undefined.
struct FrameThreadContext * parent
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 just let it vf offset
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
@ FF_THREAD_NO_FRAME_THREADING
int ff_thread_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Get a packet for decoding.
int ff_decode_receive_frame_internal(struct AVCodecContext *avctx, AVFrame *frame)
Do the actual decoding and obtain a decoded frame from the decoder, if available.
@ STATE_SETUP_FINISHED
Set after the codec has called ff_thread_finish_setup().
static void decoded_frames_flush(DecodedFrames *df)
#define FF_THREAD_FRAME
Decode more than one frame at once.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
#define i(width, name, range_min, range_max)
unsigned properties
Properties of the stream that gets decoded.
void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
Free all side data entries and their contents, then zeroes out the values which the pointers are poin...
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define atomic_store_explicit(object, desired, order)
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.
const char * name
Name of the codec implementation.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around ff_get_buffer() for frame-multithreaded codecs.
void * av_calloc(size_t nmemb, size_t size)
@ NEEDS_CLOSE
FFCodec->close needs to be called.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
int64_t frame_num
Frame counter, set by libavcodec.
DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),(OFF(async_cond)))
#define FFSWAP(type, a, b)
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int ff_frame_thread_init(AVCodecContext *avctx)
struct AVCodecInternal * ff_decode_internal_alloc(void)
static AVFrame * decoded_frames_get_free(DecodedFrames *df)
void * opaque
Private data of the user, can be used to carry app specific stuff.
static const char * hwaccel
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
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.
main external API structure.
int active_thread_type
Which multithreading methods are in use by the codec.
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *in_pkt)
static int ref[MAX_W *MAX_W]
attribute_deprecated int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
int(* close)(struct AVCodecContext *)
@ INITIALIZED
Thread has been properly set up.
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
int draining
decoding: AVERROR_EOF has been returned from ff_decode_get_packet(); must not be used by decoders tha...
#define FF_DISABLE_DEPRECATION_WARNINGS
int coded_width
Bitstream width / height, may be different from width/height e.g.
enum AVMediaType codec_type
This structure stores compressed data.
int width
picture width / height.
#define flags(name, subs,...)
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
static int update_context_from_thread(AVCodecContext *dst, const AVCodecContext *src, int for_user)
Update the next thread's AVCodecContext with values from the reference thread's context.
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
#define atomic_init(obj, value)
@ STATE_SETTING_UP
Set before the codec has called ff_thread_finish_setup().
int caps_internal
Internal hwaccel capabilities.
void * priv_data
Format private data.
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
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 * stash_hwaccel_priv
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
PerThreadContext * threads
The contexts for each thread.
#define pthread_mutex_lock(a)
static int ff_thread_setname(const char *name)