FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions
pthread_frame.c File Reference

Frame multithreading support functions. More...

#include "config.h"
#include <stdint.h>
#include "avcodec.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"

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_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
 
#define copy_fields(s, e)   memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
 

Functions

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...
 
void ff_frame_thread_free (AVCodecContext *avctx, int thread_count)
 
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)
 
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...
 

Detailed Description

Frame multithreading support functions.

See Also
doc/multithreading.txt

Definition in file pthread_frame.c.

Macro Definition Documentation

#define THREAD_SAFE_CALLBACKS (   avctx)    ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
#define copy_fields (   s,
 
)    memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);

Function Documentation

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

Referenced by ff_frame_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 198 of file pthread_frame.c.

Referenced by ff_frame_thread_free(), ff_frame_thread_init(), ff_thread_decode_frame(), ff_thread_flush(), 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 267 of file pthread_frame.c.

Referenced by ff_thread_decode_frame().

static void release_delayed_buffers ( PerThreadContext p)
static

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

Definition at line 310 of file pthread_frame.c.

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

static int submit_packet ( PerThreadContext p,
AVPacket avpkt 
)
static

Definition at line 330 of file pthread_frame.c.

Referenced by 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 411 of file pthread_frame.c.

Referenced by avcodec_decode_audio4(), and avcodec_decode_video2().

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

Referenced by decode(), decode_finish_row(), decode_frame(), decode_frame_common(), decode_nal_units(), ff_h264_decode_slice_header(), ff_h264_field_end(), 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(), handle_p_frame_apng(), mimic_decode_frame(), rv34_decode_slice(), vp3_draw_horiz_band(), vp78_decode_frame(), vp78_decode_mb_row_sliced(), and vp9_decode_frame().

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

Referenced by await_reference_mb_row(), await_reference_row(), await_references(), decode(), decode_frame(), decode_mb_row_no_filter(), decode_mode(), decode_slice(), ff_er_frame_end(), find_ref_mvs(), guess_mv(), handle_p_frame_apng(), handle_p_frame_png(), hevc_await_progress(), is_intra_more_likely(), mc_chroma_scaled(), mc_chroma_unscaled(), mc_luma_scaled(), mc_luma_unscaled(), mpeg4_decode_mb(), mpv_decode_mb_internal(), rv34_decode_mv(), rv34_mc(), temporal_luma_motion_vector(), vp78_decode_frame(), vp8_mc_chroma(), and vp8_mc_luma().

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

Referenced by decode_frame(), decode_idat_chunk(), decode_postinit(), ff_h263_decode_frame(), ff_rv34_decode_frame(), frame_worker_thread(), hap_decode(), hevc_frame_start(), mimic_decode_frame(), mpeg_field_start(), tak_decode_frame(), thread_get_buffer_internal(), vp78_decode_frame(), and vp9_decode_frame().

static void park_frame_worker_threads ( FrameThreadContext fctx,
int  thread_count 
)
static

Waits for all threads to finish.

Definition at line 548 of file pthread_frame.c.

Referenced by ff_frame_thread_free(), and ff_thread_flush().

void ff_frame_thread_free ( AVCodecContext avctx,
int  thread_count 
)

Definition at line 565 of file pthread_frame.c.

Referenced by ff_frame_thread_init(), and ff_thread_free().

int ff_frame_thread_init ( AVCodecContext avctx)

Definition at line 630 of file pthread_frame.c.

Referenced by ff_thread_init().

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

Referenced by avcodec_flush_buffers().

int ff_thread_can_start_frame ( AVCodecContext avctx)

Definition at line 773 of file pthread_frame.c.

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

Definition at line 785 of file pthread_frame.c.

Referenced by ff_thread_get_buffer().

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

Referenced by get_pixel_format(), mpeg_get_pixelformat(), and set_sps().

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

Referenced by alloc_frame(), alloc_frame_buffer(), alloc_picture(), decode_element(), decode_entropy_coded_image(), decode_frame(), decode_idat_chunk(), dnxhd_decode_frame(), flac_decode_frame(), hap_decode(), init_image(), jpeg2000_decode_frame(), lag_decode_frame(), libopenjpeg_decode_frame(), mimic_decode_frame(), tak_decode_frame(), tta_decode_frame(), vp8_alloc_frame(), vp9_alloc_frame(), and wavpack_decode_block().

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

Referenced by decode_frame(), decode_idat_chunk(), ff_ffv1_close(), ff_h264_unref_picture(), ff_hevc_unref_frame(), ff_mpeg_unref_picture(), mimic_decode_end(), mimic_decode_frame(), mimic_decode_update_thread_context(), png_dec_end(), ref_frame(), update_frames(), update_thread_context(), vp3_decode_flush(), vp8_alloc_frame(), vp8_release_frame(), vp9_alloc_frame(), vp9_decode_flush(), vp9_decode_frame(), vp9_decode_free(), vp9_decode_update_thread_context(), and vp9_unref_frame().