FFmpeg
Data Structures | Macros | Enumerations | Functions
pthread_frame.c File Reference
#include "config.h"
#include <stdatomic.h>
#include <stdint.h>
#include "avcodec.h"
#include "hwconfig.h"
#include "internal.h"
#include "pthread_internal.h"
#include "thread.h"
#include "version.h"
#include "libavutil/avassert.h"
#include "libavutil/buffer.h"
#include "libavutil/common.h"
#include "libavutil/cpu.h"
#include "libavutil/frame.h"
#include "libavutil/internal.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/thread.h"

Go to the source code of this file.

Data Structures

struct  PerThreadContext
 Context used by codec threads and stored in their AVCodecInternal thread_ctx. More...
 
struct  FrameThreadContext
 Context stored in the client AVCodecInternal thread_ctx. More...
 

Macros

#define THREAD_SAFE_CALLBACKS(avctx)   ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
 
#define SENTINEL   0
 
#define OFFSET_ARRAY(...)   __VA_ARGS__, SENTINEL
 
#define DEFINE_OFFSET_ARRAY(type, name, mutexes, conds)
 
#define OFF(member)   offsetof(FrameThreadContext, member)
 
#define OFF(member)   offsetof(PerThreadContext, member)
 
#define PTHREAD_INIT_LOOP(type)
 

Enumerations

enum  {
  STATE_INPUT_READY, STATE_SETTING_UP, STATE_GET_BUFFER, STATE_GET_FORMAT,
  STATE_SETUP_FINISHED
}
 
enum  { UNINITIALIZED, NEEDS_CLOSE, INITIALIZED }
 

Functions

static void async_lock (FrameThreadContext *fctx)
 
static void async_unlock (FrameThreadContext *fctx)
 
static attribute_align_arg void * frame_worker_thread (void *arg)
 Codec worker thread. More...
 
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. More...
 
static int update_context_from_user (AVCodecContext *dst, AVCodecContext *src)
 Update the next thread's AVCodecContext with values set by the user. More...
 
static void release_delayed_buffers (PerThreadContext *p)
 Releases the buffers that this decoding thread was the last user of. More...
 
static int submit_packet (PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
 
int ff_thread_decode_frame (AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
 Submit a new frame to a decoding thread. More...
 
void ff_thread_report_progress (ThreadFrame *f, int n, int field)
 Notify later decoding threads when part of their reference picture is ready. More...
 
void ff_thread_await_progress (ThreadFrame *f, int n, int field)
 Wait for earlier decoding threads to finish reference pictures. More...
 
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 start decoding the next frame. More...
 
static void park_frame_worker_threads (FrameThreadContext *fctx, int thread_count)
 Waits for all threads to finish. More...
 
 DEFINE_OFFSET_ARRAY (FrameThreadContext, thread_ctx,(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),(OFF(async_cond)))
 
 DEFINE_OFFSET_ARRAY (PerThreadContext, per_thread,(OFF(progress_mutex), OFF(mutex)),(OFF(input_cond), OFF(progress_cond), OFF(output_cond)))
 
static av_cold void free_pthread (void *obj, const unsigned offsets[])
 
static av_cold int init_pthread (void *obj, const unsigned offsets[])
 
void ff_frame_thread_free (AVCodecContext *avctx, int thread_count)
 
static av_cold int init_thread (PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, AVCodecContext *src, const AVCodec *codec, int first)
 
int ff_frame_thread_init (AVCodecContext *avctx)
 
void ff_thread_flush (AVCodecContext *avctx)
 Wait for decoding threads to finish and reset internal state. More...
 
int ff_thread_can_start_frame (AVCodecContext *avctx)
 
static int thread_get_buffer_internal (AVCodecContext *avctx, ThreadFrame *f, int flags)
 
FF_DISABLE_DEPRECATION_WARNINGS enum AVPixelFormat ff_thread_get_format (AVCodecContext *avctx, const enum AVPixelFormat *fmt)
 Wrapper around get_format() for frame-multithreaded codecs. More...
 
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer (AVCodecContext *avctx, ThreadFrame *f, int flags)
 Wrapper around get_buffer() for frame-multithreaded codecs. More...
 
void ff_thread_release_buffer (AVCodecContext *avctx, ThreadFrame *f)
 Wrapper around release_buffer() frame-for multithreaded codecs. More...
 

Detailed Description

Frame multithreading support functions

See also
doc/multithreading.txt

Definition in file pthread_frame.c.

Macro Definition Documentation

◆ THREAD_SAFE_CALLBACKS

#define THREAD_SAFE_CALLBACKS (   avctx)    ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)

Definition at line 151 of file pthread_frame.c.

◆ SENTINEL

#define SENTINEL   0

Definition at line 685 of file pthread_frame.c.

◆ OFFSET_ARRAY

#define OFFSET_ARRAY (   ...)    __VA_ARGS__, SENTINEL

Definition at line 686 of file pthread_frame.c.

◆ DEFINE_OFFSET_ARRAY

#define DEFINE_OFFSET_ARRAY (   type,
  name,
  mutexes,
  conds 
)
Value:
static const unsigned name ## _offsets[] = { offsetof(type, pthread_init_cnt),\
OFFSET_ARRAY mutexes, \
OFFSET_ARRAY conds }

Definition at line 687 of file pthread_frame.c.

◆ OFF [1/2]

#define OFF (   member)    offsetof(FrameThreadContext, member)

Definition at line 698 of file pthread_frame.c.

◆ OFF [2/2]

#define OFF (   member)    offsetof(PerThreadContext, member)

Definition at line 698 of file pthread_frame.c.

◆ PTHREAD_INIT_LOOP

#define PTHREAD_INIT_LOOP (   type)
Value:
for (; *(++cur_offset) != SENTINEL; cnt++) { \
pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \
err = pthread_ ## type ## _init(dst, NULL); \
if (err) { \
err = AVERROR(err); \
goto fail; \
} \
}

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
STATE_INPUT_READY 

Set when the thread is awaiting a packet.

Set before the codec has called ff_thread_finish_setup().

STATE_SETTING_UP 
STATE_GET_BUFFER 

Set when the codec calls get_buffer().

State is returned to STATE_SETTING_UP afterwards.

STATE_GET_FORMAT 

Set when the codec calls get_format().

State is returned to STATE_SETTING_UP afterwards. Set after the codec has called ff_thread_finish_setup().

STATE_SETUP_FINISHED 

Definition at line 48 of file pthread_frame.c.

◆ anonymous enum

anonymous enum
Enumerator
UNINITIALIZED 

Thread has not been created, AVCodec->close mustn't be called.

NEEDS_CLOSE 

AVCodec->close needs to be called.

INITIALIZED 

Thread has been properly set up.

Definition at line 67 of file pthread_frame.c.

Function Documentation

◆ async_lock()

static void async_lock ( FrameThreadContext fctx)
static

◆ async_unlock()

static void async_unlock ( FrameThreadContext fctx)
static

◆ frame_worker_thread()

static attribute_align_arg void* frame_worker_thread ( void *  arg)
static

Codec worker thread.

Automatically calls ff_thread_finish_setup() if the codec does not provide an update_thread_context method, or if the codec returns before calling it.

Definition at line 180 of file pthread_frame.c.

Referenced by init_thread().

◆ update_context_from_thread()

static int update_context_from_thread ( AVCodecContext dst,
AVCodecContext src,
int  for_user 
)
static

Update the next thread's AVCodecContext with values from the reference thread's context.

Parameters
dstThe destination context.
srcThe source context.
for_user0 if the destination is a codec thread, 1 if the destination is the user's thread
Returns
0 on success, negative error code on failure

Definition at line 263 of file pthread_frame.c.

Referenced by ff_frame_thread_free(), ff_thread_decode_frame(), ff_thread_flush(), init_thread(), and submit_packet().

◆ update_context_from_user()

static int update_context_from_user ( AVCodecContext dst,
AVCodecContext src 
)
static

Update the next thread's AVCodecContext with values set by the user.

Parameters
dstThe destination context.
srcThe source context.
Returns
0 on success, negative error code on failure

Definition at line 344 of file pthread_frame.c.

Referenced by submit_packet().

◆ release_delayed_buffers()

static void release_delayed_buffers ( PerThreadContext p)
static

Releases the buffers that this decoding thread was the last user of.

Definition at line 386 of file pthread_frame.c.

Referenced by ff_frame_thread_free(), ff_thread_flush(), and submit_packet().

◆ submit_packet()

static int submit_packet ( PerThreadContext p,
AVCodecContext user_avctx,
AVPacket avpkt 
)
static

Definition at line 407 of file pthread_frame.c.

Referenced by ff_thread_decode_frame().

◆ ff_thread_decode_frame()

int ff_thread_decode_frame ( AVCodecContext avctx,
AVFrame picture,
int got_picture_ptr,
AVPacket avpkt 
)

Submit a new frame to a decoding thread.

Returns the next available frame in picture. *got_picture_ptr will be 0 if none is available. The return value on success is the size of the consumed packet for compatibility with avcodec_decode_video2(). This means the decoder has to consume the full packet.

Parameters are the same as avcodec_decode_video2().

Definition at line 505 of file pthread_frame.c.

Referenced by decode_simple_internal().

◆ ff_thread_report_progress()

void ff_thread_report_progress ( ThreadFrame f,
int  progress,
int  field 
)

Notify later decoding threads when part of their reference picture is ready.

Call this when some part of the picture is finished decoding. Later calls with lower values of progress have no effect.

Parameters
fThe picture being decoded.
progressValue, in arbitrary units, of how much of the picture has decoded.
fieldThe field being decoded, for field-picture codecs. 0 for top field or frame pictures, 1 for bottom field.

Definition at line 590 of file pthread_frame.c.

Referenced by decode(), decode_finish_row(), decode_frame(), decode_frame_common(), decode_nal_units(), decode_slice(), decode_tiles(), ff_h264_field_end(), ff_h264_queue_decode_slice(), ff_hevc_hls_filter(), ff_mpv_frame_end(), ff_mpv_frame_start(), ff_mpv_report_decode_progress(), ff_rv34_decode_frame(), finish_frame(), generate_missing_ref(), h264_field_start(), mimic_decode_frame(), rv34_decode_slice(), vp3_draw_horiz_band(), vp78_decode_frame(), vp78_decode_mb_row_sliced(), vp9_decode_frame(), and wavpack_decode_frame().

◆ ff_thread_await_progress()

void ff_thread_await_progress ( ThreadFrame f,
int  progress,
int  field 
)

Wait for earlier decoding threads to finish reference pictures.

Call this before accessing some part of a picture, with a given value for progress, and it will return after the responsible decoding thread calls ff_thread_report_progress() with the same or higher value for progress.

Parameters
fThe picture being referenced.
progressValue, in arbitrary units, to wait for.
fieldThe field being referenced, for field-picture codecs. 0 for top field or frame pictures, 1 for bottom field.

Definition at line 613 of file pthread_frame.c.

◆ ff_thread_finish_setup()

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 start decoding the next frame.

After calling it, do not change any variables read by the update_thread_context() method, or call ff_thread_get_buffer().

Parameters
avctxThe context.

Definition at line 634 of file pthread_frame.c.

Referenced by frame_worker_thread(), and thread_get_buffer_internal().

◆ park_frame_worker_threads()

static void park_frame_worker_threads ( FrameThreadContext fctx,
int  thread_count 
)
static

Waits for all threads to finish.

Definition at line 664 of file pthread_frame.c.

Referenced by ff_frame_thread_free(), and ff_thread_flush().

◆ DEFINE_OFFSET_ARRAY() [1/2]

DEFINE_OFFSET_ARRAY ( FrameThreadContext  ,
thread_ctx  ,
(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex))  ,
(OFF(async_cond))   
)

◆ DEFINE_OFFSET_ARRAY() [2/2]

DEFINE_OFFSET_ARRAY ( PerThreadContext  ,
per_thread  ,
(OFF(progress_mutex), OFF(mutex))  ,
(OFF(input_cond), OFF(progress_cond), OFF(output_cond))   
)

◆ free_pthread()

static av_cold void free_pthread ( void *  obj,
const unsigned  offsets[] 
)
static

Definition at line 704 of file pthread_frame.c.

Referenced by ff_frame_thread_free(), and ff_frame_thread_init().

◆ init_pthread()

static av_cold int init_pthread ( void *  obj,
const unsigned  offsets[] 
)
static

Definition at line 715 of file pthread_frame.c.

Referenced by ff_frame_thread_init(), and init_thread().

◆ ff_frame_thread_free()

void ff_frame_thread_free ( AVCodecContext avctx,
int  thread_count 
)

Definition at line 738 of file pthread_frame.c.

Referenced by ff_frame_thread_init(), and ff_thread_free().

◆ init_thread()

static av_cold int init_thread ( PerThreadContext p,
int threads_to_free,
FrameThreadContext fctx,
AVCodecContext avctx,
AVCodecContext src,
const AVCodec codec,
int  first 
)
static

Definition at line 813 of file pthread_frame.c.

Referenced by ff_frame_thread_init().

◆ ff_frame_thread_init()

int ff_frame_thread_init ( AVCodecContext avctx)

Definition at line 889 of file pthread_frame.c.

Referenced by ff_thread_init().

◆ ff_thread_flush()

void ff_thread_flush ( AVCodecContext avctx)

Wait for decoding threads to finish and reset internal state.

Called by avcodec_flush_buffers().

Parameters
avctxThe context.

Definition at line 950 of file pthread_frame.c.

Referenced by avcodec_flush_buffers().

◆ ff_thread_can_start_frame()

int ff_thread_can_start_frame ( AVCodecContext avctx)

Definition at line 982 of file pthread_frame.c.

◆ thread_get_buffer_internal()

static int thread_get_buffer_internal ( AVCodecContext avctx,
ThreadFrame f,
int  flags 
)
static

Definition at line 998 of file pthread_frame.c.

Referenced by ff_thread_get_buffer().

◆ ff_thread_get_format()

FF_DISABLE_DEPRECATION_WARNINGS enum AVPixelFormat ff_thread_get_format ( AVCodecContext avctx,
const enum AVPixelFormat fmt 
)

Wrapper around get_format() for frame-multithreaded codecs.

Call this function instead of avctx->get_format(). Cannot be called after the codec has called ff_thread_finish_setup().

Parameters
avctxThe current context.
fmtThe list of available formats.

Definition at line 1068 of file pthread_frame.c.

Referenced by get_format(), get_pixel_format(), mpeg_get_pixelformat(), and update_size().

◆ ff_thread_get_buffer()

FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer ( AVCodecContext avctx,
ThreadFrame f,
int  flags 
)

Wrapper around get_buffer() for frame-multithreaded codecs.

Call this function instead of ff_get_buffer(f). Cannot be called after the codec has called ff_thread_finish_setup().

Parameters
avctxThe current context.
fThe frame to write into.

Definition at line 1096 of file pthread_frame.c.

◆ ff_thread_release_buffer()

void ff_thread_release_buffer ( AVCodecContext avctx,
ThreadFrame f 
)

Wrapper around release_buffer() frame-for multithreaded codecs.

Call this function instead of avctx->release_buffer(f). The AVFrame will be copied and the actual release_buffer() call will be performed later. The contents of data pointed to by the AVFrame should not be changed until ff_thread_get_buffer() is called on it.

Parameters
avctxThe current context.
fThe picture being released.

Definition at line 1104 of file pthread_frame.c.

Referenced by av1_frame_unref(), decode_frame(), decode_idat_chunk(), ff_ffv1_close(), ff_h264_unref_picture(), ff_hevc_unref_frame(), ff_mpeg_unref_picture(), h264_field_start(), mimic_decode_end(), mimic_decode_frame(), png_dec_end(), update_frames(), vaapi_av1_decode_uninit(), vaapi_av1_end_frame(), vp3_decode_flush(), vp8_alloc_frame(), vp8_release_frame(), vp9_decode_flush(), vp9_decode_frame(), vp9_decode_free(), vp9_frame_unref(), wavpack_decode_block(), wavpack_decode_end(), and wavpack_decode_frame().

name
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
Definition: writing_filters.txt:88
AVERROR
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
fail
#define fail()
Definition: checkasm.h:133
type
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 type
Definition: writing_filters.txt:86
NULL
#define NULL
Definition: coverity.c:32
SENTINEL
#define SENTINEL
Definition: pthread_frame.c:685