pthread.c File Reference

Multithreading support functions. More...

#include "config.h"
#include "avcodec.h"
#include "internal.h"
#include "thread.h"
#include "libavutil/avassert.h"
#include "libavutil/common.h"
Include dependency graph for pthread.c:

Go to the source code of this file.

Data Structures

struct  ThreadContext
 
struct  PerThreadContext
 Context used by codec threads and stored in their AVCodecContext thread_opaque. More...
 
struct  FrameThreadContext
 Context stored in the client AVCodecContext thread_opaque. More...
 

Macros

#define MAX_AUTO_THREADS   16
 
#define copy_fields(s, e)   memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
 

Typedefs

typedef int( action_func) (AVCodecContext *c, void *arg)
 
typedef int( action_func2) (AVCodecContext *c, void *arg, int jobnr, int threadnr)
 
typedef struct ThreadContext ThreadContext
 
typedef struct PerThreadContext PerThreadContext
 Context used by codec threads and stored in their AVCodecContext thread_opaque. More...
 
typedef struct FrameThreadContext FrameThreadContext
 Context stored in the client AVCodecContext thread_opaque. More...
 

Functions

int ff_get_logical_cpus (AVCodecContext *avctx)
 
static void *attribute_align_arg worker (void *v)
 
static av_always_inline void avcodec_thread_park_workers (ThreadContext *c, int thread_count)
 
static void thread_free (AVCodecContext *avctx)
 
static int avcodec_thread_execute (AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
 
static int avcodec_thread_execute2 (AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
 
static int thread_init (AVCodecContext *avctx)
 
static attribute_align_arg voidframe_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, 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...
 
static void frame_thread_free (AVCodecContext *avctx, int thread_count)
 
static int 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)
 
enum AVPixelFormat ff_thread_get_format (AVCodecContext *avctx, const enum AVPixelFormat *fmt)
 Wrapper around get_format() for frame-multithreaded codecs. More...
 
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...
 
static void validate_thread_parameters (AVCodecContext *avctx)
 Set the threading algorithms used. More...
 
int ff_thread_init (AVCodecContext *avctx)
 
void ff_thread_free (AVCodecContext *avctx)
 

Detailed Description

Multithreading support functions.

See also
doc/multithreading.txt

Definition in file pthread.c.

Macro Definition Documentation

#define copy_fields (   s,
 
)    memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
#define MAX_AUTO_THREADS   16

Definition at line 167 of file pthread.c.

Referenced by frame_thread_init(), thread_init(), and validate_thread_parameters().

Typedef Documentation

typedef int( action_func) (AVCodecContext *c, void *arg)

Definition at line 69 of file pthread.c.

typedef int( action_func2) (AVCodecContext *c, void *arg, int jobnr, int threadnr)

Definition at line 70 of file pthread.c.

Context stored in the client AVCodecContext thread_opaque.

Context used by codec threads and stored in their AVCodecContext thread_opaque.

typedef struct ThreadContext ThreadContext

Function Documentation

static int avcodec_thread_execute ( AVCodecContext avctx,
action_func func,
void arg,
int *  ret,
int  job_count,
int  job_size 
)
static

Definition at line 270 of file pthread.c.

Referenced by avcodec_thread_execute2(), and thread_init().

static int avcodec_thread_execute2 ( AVCodecContext avctx,
action_func2 func2,
void arg,
int *  ret,
int  job_count 
)
static

Definition at line 303 of file pthread.c.

Referenced by thread_init().

static av_always_inline void avcodec_thread_park_workers ( ThreadContext c,
int  thread_count 
)
static

Definition at line 243 of file pthread.c.

Referenced by avcodec_thread_execute(), and thread_init().

int ff_get_logical_cpus ( AVCodecContext avctx)
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 723 of file pthread.c.

int ff_thread_can_start_frame ( AVCodecContext avctx)

Definition at line 959 of file pthread.c.

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 632 of file pthread.c.

Referenced by avcodec_decode_video2().

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 741 of file pthread.c.

Referenced by frame_worker_thread(), and thread_get_buffer_internal().

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 931 of file pthread.c.

Referenced by avcodec_flush_buffers().

void ff_thread_free ( AVCodecContext avctx)

Definition at line 1178 of file pthread.c.

Referenced by avcodec_close(), and thread_init().

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 1066 of file pthread.c.

Referenced by alloc_frame_buffer(), alloc_picture(), decode_frame(), dnxhd_decode_frame(), lag_decode_frame(), libopenjpeg_decode_frame(), mimic_decode_frame(), vp3_decode_frame(), and vp8_alloc_frame().

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 1040 of file pthread.c.

Referenced by get_pixel_format(), and mpeg_get_pixelformat().

int ff_thread_init ( AVCodecContext avctx)

Definition at line 1155 of file pthread.c.

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 1074 of file pthread.c.

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 705 of file pthread.c.

Referenced by decode(), decode_finish_row(), decode_nal_units(), decode_slice_header(), ff_MPV_frame_end(), ff_MPV_frame_start(), ff_MPV_report_decode_progress(), ff_rv34_decode_frame(), field_end(), finish_frame(), mimic_decode_frame(), rv34_decode_slice(), vp3_decode_frame(), vp3_draw_horiz_band(), vp8_decode_frame(), and vp8_decode_mb_row_sliced().

static void frame_thread_free ( AVCodecContext avctx,
int  thread_count 
)
static

Definition at line 774 of file pthread.c.

Referenced by ff_thread_free(), and frame_thread_init().

static int frame_thread_init ( AVCodecContext avctx)
static

Definition at line 837 of file pthread.c.

Referenced by ff_thread_init().

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 372 of file pthread.c.

Referenced by frame_thread_init().

static void park_frame_worker_threads ( FrameThreadContext fctx,
int  thread_count 
)
static

Waits for all threads to finish.

Definition at line 757 of file pthread.c.

Referenced by ff_thread_flush(), and frame_thread_free().

static void release_delayed_buffers ( PerThreadContext p)
static

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

Definition at line 527 of file pthread.c.

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

static int submit_packet ( PerThreadContext p,
AVPacket avpkt 
)
static

Definition at line 546 of file pthread.c.

Referenced by ff_thread_decode_frame().

static void thread_free ( AVCodecContext avctx)
static

Definition at line 250 of file pthread.c.

Referenced by ff_thread_free().

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

Definition at line 973 of file pthread.c.

Referenced by ff_thread_get_buffer().

static int thread_init ( AVCodecContext avctx)
static

Definition at line 310 of file pthread.c.

Referenced by ff_thread_init().

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

Definition at line 429 of file pthread.c.

Referenced by ff_thread_decode_frame(), ff_thread_flush(), frame_thread_free(), frame_thread_init(), and submit_packet().

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 483 of file pthread.c.

Referenced by ff_thread_decode_frame().

static void validate_thread_parameters ( AVCodecContext avctx)
static

Set the threading algorithms used.

Threading requires more than one thread. Frame threading requires entire frames to be passed to the codec, and introduces extra decoding delay, so is incompatible with low_delay.

Parameters
avctxThe context.

Definition at line 1131 of file pthread.c.

Referenced by ff_thread_init().

static void* attribute_align_arg worker ( void v)
static

Definition at line 207 of file pthread.c.