180 int idx = p - p->parent->threads;
191 if (
df->nb_f ==
df->nb_f_allocated) {
199 if (!
df->f[
df->nb_f])
202 df->nb_f_allocated++;
207 return df->f[
df->nb_f];
214 memmove(
df->f,
df->f + 1, (
df->nb_f - 1) *
sizeof(*
df->f));
215 df->f[--
df->nb_f] = tmp_frame;
220 for (
size_t i = 0;
i <
df->nb_f;
i++)
227 for (
size_t i = 0;
i <
df->nb_f_allocated;
i++)
231 df->nb_f_allocated = 0;
273 p->hwaccel_serializing = 1;
291 else if (ret < 0 && frame->buf[0])
294 p->result = (ret ==
AVERROR(EAGAIN)) ? 0 : ret;
301 if (p->hwaccel_serializing) {
310 p->hwaccel_serializing = 0;
316 if (p->async_serializing) {
317 p->async_serializing = 0;
349 dst->time_base =
src->time_base;
350 dst->framerate =
src->framerate;
352 dst->height =
src->height;
353 dst->pix_fmt =
src->pix_fmt;
354 dst->sw_pix_fmt =
src->sw_pix_fmt;
356 dst->coded_width =
src->coded_width;
357 dst->coded_height =
src->coded_height;
359 dst->has_b_frames =
src->has_b_frames;
360 dst->idct_algo =
src->idct_algo;
362 dst->bits_per_coded_sample =
src->bits_per_coded_sample;
363 dst->sample_aspect_ratio =
src->sample_aspect_ratio;
365 dst->profile =
src->profile;
368 dst->bits_per_raw_sample =
src->bits_per_raw_sample;
369 dst->color_primaries =
src->color_primaries;
371 dst->alpha_mode =
src->alpha_mode;
373 dst->color_trc =
src->color_trc;
374 dst->colorspace =
src->colorspace;
375 dst->color_range =
src->color_range;
376 dst->chroma_sample_location =
src->chroma_sample_location;
378 dst->sample_rate =
src->sample_rate;
379 dst->sample_fmt =
src->sample_fmt;
384 if (!!
dst->hw_frames_ctx != !!
src->hw_frames_ctx ||
385 (
dst->hw_frames_ctx &&
dst->hw_frames_ctx->data !=
src->hw_frames_ctx->data)) {
388 if (
src->hw_frames_ctx) {
390 if (!
dst->hw_frames_ctx)
395 dst->hwaccel_flags =
src->hwaccel_flags;
416 (!
dst->hwaccel && !
dst->internal->hwaccel_priv_data));
430 dst->internal->hwaccel_priv_data =
432 if (!
dst->internal->hwaccel_priv_data)
435 dst->hwaccel =
src->hwaccel;
439 if (
hwaccel->update_thread_context) {
467 dst->draw_horiz_band=
src->draw_horiz_band;
468 dst->get_buffer2 =
src->get_buffer2;
470 dst->opaque =
src->opaque;
473 dst->slice_flags =
src->slice_flags;
474 dst->flags2 =
src->flags2;
475 dst->export_side_data =
src->export_side_data;
477 dst->skip_loop_filter =
src->skip_loop_filter;
478 dst->skip_idct =
src->skip_idct;
479 dst->skip_frame =
src->skip_frame;
481 dst->frame_num =
src->frame_num;
496 const AVCodec *codec = p->avctx->codec;
505 p->avctx->internal->draining = 1;
514 memory_order_relaxed);
527 if (!p->avctx->internal->draining ||
538 av_assert0(!p->avctx->hwaccel || p->hwaccel_threadsafe);
539 if (!p->hwaccel_threadsafe) {
570 goto wait_for_result;
630 p =
f->owner[field]->internal->thread_ctx;
634 "%p finished %d field %d\n", progress, n, field);
653 p =
f->owner[field]->internal->thread_ctx;
657 "thread awaiting %d field %d from %p\n", n, field, progress);
672 p->hwaccel_threadsafe = avctx->
hwaccel &&
677 p->hwaccel_serializing = 1;
683 p->async_serializing = 1;
694 p->parent->stash_hwaccel = avctx->
hwaccel;
717 for (
i = 0;
i < thread_count;
i++) {
731#define OFF(member) offsetof(FrameThreadContext, member)
733 (
OFF(buffer_mutex),
OFF(hwaccel_mutex),
OFF(async_mutex)),
737#define OFF(member) offsetof(PerThreadContext, member)
740 (
OFF(input_cond),
OFF(progress_cond),
OFF(output_cond)));
751 for (
i = 0;
i < thread_count;
i++) {
771 if (
ctx->priv_data) {
784 &
ctx->nb_decoded_side_data);
813 const FFCodec *codec,
int first)
825 copy->nb_decoded_side_data = 0;
829 (*threads_to_free)++;
838 copy->internal->thread_ctx = p;
845 if (!
copy->priv_data)
863 copy->internal->is_frame_mt = 1;
865 copy->internal->is_copy = 1;
868 if (!
copy->internal->in_pkt)
872 if (!
copy->internal->last_pkt_props)
891 for (
int i = 0;
i <
src->nb_decoded_side_data;
i++) {
893 &
dst->nb_decoded_side_data,
894 src->decoded_side_data[
i], 0);
925 if (thread_count <= 1) {
956 for (;
i < thread_count; ) {
1025 av_log(avctx,
AV_LOG_ERROR,
"get_buffer() cannot be called after ff_thread_finish_setup()\n");
1049 f->owner[0] =
f->owner[1] = avctx;
1069 f->owner[0] =
f->owner[1] =
NULL;
1087 memcpy(&
ref, (
const char*)p->parent->threads[0].avctx->priv_data +
offset,
sizeof(
ref));
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
void ff_decode_internal_uninit(struct AVCodecContext *avctx)
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.
void ff_decode_internal_sync(struct AVCodecContext *dst, const struct AVCodecContext *src)
struct AVCodecInternal * ff_decode_internal_alloc(void)
refcounted data buffer API
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static av_always_inline const FFCodec * ffcodec(const AVCodec *codec)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
void ff_hwaccel_uninit(AVCodecContext *avctx)
#define ff_thread_get_packet(avctx, pkt)
#define ff_thread_receive_frame(avctx, frame, flags)
#define atomic_store(object, desired)
#define atomic_load_explicit(object, order)
#define atomic_load(object)
#define atomic_store_explicit(object, desired, order)
#define atomic_init(obj, value)
static const char * hwaccel
reference-counted frame API
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_RECEIVE_FRAME_FLAG_SYNCHRONOUS
The decoder will bypass frame threading and return the next frame as soon as possible.
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AVERROR_EOF
End of file.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
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...
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
void av_opt_free(void *obj)
Free all allocated objects in obj.
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
#define HWACCEL_CAP_ASYNC_SAFE
Header providing the internals of AVHWAccel.
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_CAP_THREAD_SAFE
common internal api header.
#define AVPACKET_IS_EMPTY(pkt)
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
Multithreading API for decoders.
@ FF_THREAD_IS_FIRST_THREAD
@ FF_THREAD_NO_FRAME_THREADING
common internal API header
#define attribute_align_arg
static int ff_thread_setname(const char *name)
#define FFSWAP(type, a, b)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static void decoded_frames_flush(DecodedFrames *df)
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
static void async_unlock(FrameThreadContext *fctx)
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
av_cold int ff_frame_thread_init(AVCodecContext *avctx)
static av_cold void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
void ff_thread_release_ext_buffer(ThreadFrame *f)
Unref a ThreadFrame.
av_cold 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.
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
static void decoded_frames_pop(DecodedFrames *df, AVFrame *dst)
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around ff_get_buffer() for frame-multithreaded codecs.
@ NEEDS_CLOSE
FFCodec->close needs to be called.
@ INITIALIZED
Thread has been properly set up.
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
static int update_context_from_user(AVCodecContext *dst, const AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
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.
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *in_pkt)
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...
@ STATE_SETTING_UP
Set before the codec has called ff_thread_finish_setup().
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
@ STATE_SETUP_FINISHED
Set after the codec has called ff_thread_finish_setup().
static AVFrame * decoded_frames_get_free(DecodedFrames *df)
static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, const FFCodec *codec, int first)
static void async_lock(FrameThreadContext *fctx)
av_cold void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
static void thread_set_name(PerThreadContext *p)
static int hwaccel_serial(const AVCodecContext *avctx)
int ff_thread_can_start_frame(AVCodecContext *avctx)
static void decoded_frames_free(DecodedFrames *df)
av_cold void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define DEFINE_OFFSET_ARRAY(type, name, cnt_variable, mutexes, conds)
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
Describe the class of an AVClass context structure.
main external API structure.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
int active_thread_type
Which multithreading methods are in use by the codec.
const struct AVCodec * codec
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
void * hwaccel_context
Legacy hardware accelerator context.
struct AVCodecInternal * internal
Private context used for internal data.
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
void * hwaccel_priv_data
hwaccel-specific private data
int draining
decoding: AVERROR_EOF has been returned from ff_decode_get_packet(); must not be used by decoders tha...
struct AVRefStructPool * progress_frame_pool
const AVClass * priv_class
AVClass for the private context.
const char * name
Name of the codec implementation.
int capabilities
Codec capabilities.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
AVCodec p
The public AVCodec.
int(* init)(struct AVCodecContext *)
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
int(* close)(struct AVCodecContext *)
int caps_internal
Internal hwaccel capabilities.
Context stored in the client AVCodecInternal thread_ctx.
pthread_mutex_t async_mutex
int next_decoding
The next context to submit a packet to.
PerThreadContext * threads
The contexts for each thread.
const AVHWAccel * stash_hwaccel
pthread_cond_t async_cond
AVPacket * next_pkt
Packet to be submitted to the next thread for decoding.
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
int next_finished
The next context to return output from.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
void * stash_hwaccel_context
void * stash_hwaccel_priv
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when thread-unsafe hwaccel is used.
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
struct FrameThreadContext * parent
AVCodecContext * avctx
Context used to decode packets passed to this thread.
DecodedFrames df
Decoded frames from a single decode iteration.
int die
Set when the thread should exit.
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
int result
The result of the last codec decode/encode() call.
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
static void error(const char *err)
static int ref[MAX_W *MAX_W]
static AVFormatContext * ctx
static void copy(const float *p1, float *p2, const int length)