FFmpeg
Data Structures | Macros | Enumerations | Functions
pthread_frame.c File Reference
#include <stdatomic.h>
#include "avcodec.h"
#include "avcodec_internal.h"
#include "codec_desc.h"
#include "codec_internal.h"
#include "decode.h"
#include "hwaccel_internal.h"
#include "hwconfig.h"
#include "internal.h"
#include "packet_internal.h"
#include "pthread_internal.h"
#include "refstruct.h"
#include "thread.h"
#include "threadframe.h"
#include "version_major.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   DecodedFrames
 
 
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  OFF(member)   offsetof(FrameThreadContext, member)
 
#define  OFF(member)   offsetof(PerThreadContext, member)
 

Enumerations

 
 

Functions

static int  hwaccel_serial (const AVCodecContext *avctx)
 
static void  async_lock (FrameThreadContext *fctx)
 
static void  async_unlock (FrameThreadContext *fctx)
 
static void  thread_set_name (PerThreadContext *p)
 
 
 
 
 
  Codec worker thread. More...
 
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. More...
 
  Update the next thread's AVCodecContext with values set by the user. More...
 
static int  submit_packet (PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *in_pkt)
 
  Submit available packets for decoding to worker threads, return a decoded frame if available. More...
 
  Notify later decoding threads when part of their reference picture is ready. More...
 
void  ff_thread_await_progress (const ThreadFrame *f, int n, int field)
  Wait for earlier decoding threads to finish reference pictures. More...
 
  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, pthread_init_cnt,(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),(OFF(async_cond)))
 
  DEFINE_OFFSET_ARRAY (PerThreadContext, per_thread, pthread_init_cnt,(OFF(progress_mutex), OFF(mutex)),(OFF(input_cond), OFF(progress_cond), OFF(output_cond)))
 
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, const FFCodec *codec, int first)
 
 
  Wait for decoding threads to finish and reset internal state. More...
 
 
 
  Wrapper around get_buffer() for frame-multithreaded codecs. More...
 
  Wrapper around ff_get_buffer() for frame-multithreaded codecs. More...
 
  Unref a ThreadFrame. More...
 
  Allows to synchronize objects whose lifetime is the whole decoding process among all frame threads. More...
 
  Get a packet for decoding. More...
 

Detailed Description

Frame multithreading support functions

See also
doc/multithreading.txt

Definition in file pthread_frame.c.

Macro Definition Documentation

OFF [1/2]

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

Definition at line 743 of file pthread_frame.c.

OFF [2/2]

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

Definition at line 743 of file pthread_frame.c.

Enumeration Type Documentation

anonymous enum

anonymous enum
Enumerator
STATE_INPUT_READY 

Set when the thread is awaiting a packet.

STATE_SETTING_UP 

Set before the codec has called ff_thread_finish_setup().

STATE_SETUP_FINISHED 

Set after the codec has called ff_thread_finish_setup().

Definition at line 53 of file pthread_frame.c.

anonymous enum

anonymous enum
Enumerator
UNINITIALIZED 

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

NEEDS_CLOSE 

FFCodec->close needs to be called.

INITIALIZED 

Thread has been properly set up.

Definition at line 62 of file pthread_frame.c.

Function Documentation

hwaccel_serial()

static int hwaccel_serial ( const AVCodecContextavctx )
static

Definition at line 161 of file pthread_frame.c.

Referenced by ff_thread_finish_setup(), and frame_worker_thread().

async_lock()

static void async_lock ( FrameThreadContextfctx )
static

async_unlock()

static void async_unlock ( FrameThreadContextfctx )
static

thread_set_name()

static void thread_set_name ( PerThreadContextp )
static

Definition at line 184 of file pthread_frame.c.

Referenced by frame_worker_thread().

decoded_frames_get_free()

static AVFrame* decoded_frames_get_free ( DecodedFramesdf )
static

Definition at line 196 of file pthread_frame.c.

Referenced by frame_worker_thread().

decoded_frames_pop()

static void decoded_frames_pop ( DecodedFramesdf,
AVFramedst 
)
static

Definition at line 217 of file pthread_frame.c.

Referenced by ff_thread_receive_frame().

decoded_frames_flush()

static void decoded_frames_flush ( DecodedFramesdf )
static

Definition at line 225 of file pthread_frame.c.

Referenced by ff_thread_flush().

decoded_frames_free()

static void decoded_frames_free ( DecodedFramesdf )
static

Definition at line 232 of file pthread_frame.c.

Referenced by ff_frame_thread_free().

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 248 of file pthread_frame.c.

Referenced by init_thread().

update_context_from_thread()

static int update_context_from_thread ( AVCodecContextdst,
const AVCodecContextsrc,
int  for_user 
)
static

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

Parameters
dst The destination context.
src The source context.
for_user 0 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 350 of file pthread_frame.c.

Referenced by ff_thread_flush(), ff_thread_receive_frame(), init_thread(), and submit_packet().

update_context_from_user()

static int update_context_from_user ( AVCodecContextdst,
const AVCodecContextsrc 
)
static

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

Parameters
dst The destination context.
src The source context.
Returns
0 on success, negative error code on failure

Definition at line 472 of file pthread_frame.c.

Referenced by submit_packet().

submit_packet()

static int submit_packet ( PerThreadContextp,
AVCodecContextuser_avctx,
AVPacketin_pkt 
)
static

Definition at line 502 of file pthread_frame.c.

Referenced by ff_thread_receive_frame().

ff_thread_receive_frame()

int ff_thread_receive_frame ( struct AVCodecContextavctx,
AVFrameframe 
)

Submit available packets for decoding to worker threads, return a decoded frame if available.

Returns AVERROR(EAGAIN) if none is available.

Parameters are the same as FFCodec.receive_frame.

Definition at line 566 of file pthread_frame.c.

ff_thread_report_progress()

void ff_thread_report_progress ( ThreadFramef,
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
f The picture being decoded.
progress Value, in arbitrary units, of how much of the picture has decoded.
field The field being decoded, for field-picture codecs. 0 for top field or frame pictures, 1 for bottom field.

Definition at line 627 of file pthread_frame.c.

Referenced by decode_finish_row(), decode_nal_units(), ff_h264_field_end(), ff_h264_queue_decode_slice(), and h264_field_start().

ff_thread_await_progress()

void ff_thread_await_progress ( const ThreadFramef,
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
f The picture being referenced.
progress Value, in arbitrary units, to wait for.
field The field being referenced, for field-picture codecs. 0 for top field or frame pictures, 1 for bottom field.

Definition at line 650 of file pthread_frame.c.

Referenced by await_reference_mb_row(), await_references(), guess_mv(), and h264_field_start().

ff_thread_finish_setup()

void ff_thread_finish_setup ( AVCodecContextavctx )

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
avctx The context.

Definition at line 671 of file pthread_frame.c.

Referenced by frame_worker_thread().

park_frame_worker_threads()

static void park_frame_worker_threads ( FrameThreadContextfctx,
int  thread_count 
)
static

Waits for all threads to finish.

Definition at line 717 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  ,
pthread_init_cnt  ,
(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex))  ,
(OFF(async_cond))   
)

DEFINE_OFFSET_ARRAY() [2/2]

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

ff_frame_thread_free()

void ff_frame_thread_free ( AVCodecContextavctx,
int  thread_count 
)

Definition at line 749 of file pthread_frame.c.

Referenced by ff_frame_thread_init(), and ff_thread_free().

init_thread()

static av_cold int init_thread ( PerThreadContextp,
int *  threads_to_free,
FrameThreadContextfctx,
AVCodecContextavctx,
const FFCodeccodec,
int  first 
)
static

Definition at line 817 of file pthread_frame.c.

Referenced by ff_frame_thread_init().

ff_frame_thread_init()

int ff_frame_thread_init ( AVCodecContextavctx )

Definition at line 921 of file pthread_frame.c.

Referenced by ff_thread_init().

ff_thread_flush()

void ff_thread_flush ( struct AVCodecContextavctx )

Wait for decoding threads to finish and reset internal state.

Called by avcodec_flush_buffers().

Parameters
avctx The context.

Definition at line 984 of file pthread_frame.c.

Referenced by avcodec_flush_buffers().

ff_thread_can_start_frame()

int ff_thread_can_start_frame ( AVCodecContextavctx )

Definition at line 1013 of file pthread_frame.c.

Referenced by ff_mpv_frame_start(), and h264_frame_start().

thread_get_buffer_internal()

static int thread_get_buffer_internal ( AVCodecContextavctx,
AVFramef,
int  flags 
)
static

Definition at line 1026 of file pthread_frame.c.

Referenced by ff_thread_get_buffer().

ff_thread_get_buffer()

int ff_thread_get_buffer ( AVCodecContextavctx,
AVFramef,
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
avctx The current context.
f The frame to write into.

Definition at line 1049 of file pthread_frame.c.

Referenced by aic_decode_frame(), alloc_frame(), alloc_picture(), bitpacked_decode_yuv422p10(), cfhd_decode(), cllc_decode_frame(), cri_decode_frame(), decode_blocks(), decode_element(), decode_entropy_coded_image(), decode_frame(), decode_idat_chunk(), dnxhd_decode_frame(), dvvideo_decode_frame(), dxtory_decode_v1_410(), dxtory_decode_v1_420(), dxtory_decode_v1_444(), dxtory_decode_v1_rgb(), dxtory_decode_v2(), dxv_decode(), ff_progress_frame_get_buffer(), ff_thread_get_ext_buffer(), flac_decode_frame(), hap_decode(), hdr_decode_frame(), hevc_frame_start(), hqx_decode_frame(), init_image(), jpeg2000_decode_frame(), lag_decode_frame(), magy_decode_frame(), photocd_decode_frame(), pixlet_decode_frame(), qoi_decode_frame(), speedhq_decode_frame(), tak_decode_frame(), tta_decode_frame(), v410_decode_frame(), vble_decode_frame(), wavpack_decode_block(), and wbmp_decode_frame().

ff_thread_get_ext_buffer()

int ff_thread_get_ext_buffer ( AVCodecContextavctx,
ThreadFramef,
int  flags 
)

Wrapper around ff_get_buffer() for frame-multithreaded codecs.

Call this function instead of ff_get_buffer() if you might need to wait for progress on this frame. Cannot be called after the codec has called ff_thread_finish_setup().

Parameters
avctx The current context.
f The frame to write into.
Note
: It is fine to call this with codecs that do not support frame threading.

Definition at line 1057 of file pthread_frame.c.

Referenced by alloc_picture().

ff_thread_release_ext_buffer()

void ff_thread_release_ext_buffer ( ThreadFramef )

Unref a ThreadFrame.

This is basically a wrapper around av_frame_unref() and should be called instead of it.

Parameters
avctx The current context.
f The picture being released.

Definition at line 1078 of file pthread_frame.c.

Referenced by ff_h264_unref_picture(), and h264_field_start().

ff_thread_sync_ref()

enum ThreadingStatus ff_thread_sync_ref ( AVCodecContextavctx,
size_t  offset 
)

Allows to synchronize objects whose lifetime is the whole decoding process among all frame threads.

When called from a non-copy thread, do nothing. When called from another thread, place a new RefStruct reference at the given offset in the calling thread's private data from the RefStruct reference in the private data of the first decoding thread. The first thread must have a valid RefStruct reference at the given offset in its private data; the calling thread must not have a reference at this offset in its private data (must be NULL).

Parameters
avctx an AVCodecContext
offset offset of the RefStruct reference in avctx's private data
Return values
FF_THREAD_IS_COPY if frame-threading is in use and the calling thread is a copy; in this case, the RefStruct reference will be set.
FF_THREAD_IS_MAIN_THREAD if frame-threading is in use and the calling thread is the main thread.
FF_THREAD_NO_FRAME_THREADING if frame-threading is not in use.

Definition at line 1086 of file pthread_frame.c.

ff_thread_get_packet()

int ff_thread_get_packet ( struct AVCodecContextavctx,
AVPacketpkt 
)

Get a packet for decoding.

This gets invoked by the worker threads.

Definition at line 1106 of file pthread_frame.c.


Generated on Fri Aug 22 2025 13:59:58 for FFmpeg by   doxygen 1.8.17

AltStyle によって変換されたページ (->オリジナル) /