42#define BUFFER_SIZE (MAX_THREADS + 2)
72#define OFF(member) offsetof(ThreadContext, member)
74 (
OFF(task_fifo_mutex),
OFF(finished_task_mutex)),
75 (
OFF(task_fifo_cond),
OFF(finished_task_cond)));
90 while (
c->next_task_index ==
c->task_index ||
atomic_load(&
c->exit)) {
97 task_index =
c->next_task_index;
98 c->next_task_index = (
c->next_task_index + 1) %
c->max_tasks;
104 task = &
c->tasks[task_index];
136 "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
137 "or a constant quantizer if you want to use multiple cpu cores\n");
144 "MJPEG CBR encoding works badly with frame multi-threading, consider "
145 "using -threads 1, -thread_type slice or a constant quantizer.\n");
162 "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
183 c->parent_avctx = avctx;
191 for (
unsigned j = 0; j <
c->max_tasks; j++) {
231#define DUP_MATRIX(m) \
233 thread_avctx->m = av_memdup(avctx->m, 64 * sizeof(*avctx->m)); \
234 if (!thread_avctx->m) { \
235 ret = AVERROR(ENOMEM); \
292 for (
unsigned i = 0;
i <
c->max_tasks;
i++) {
313 c->task_index = (
c->task_index + 1) %
c->max_tasks;
318 outtask = &
c->tasks[
c->finished_task_index];
322 if (
c->task_index ==
c->finished_task_index ||
324 (
c->task_index -
c->finished_task_index +
c->max_tasks) %
c->max_tasks <= avctx->
thread_count)) {
337 c->finished_task_index = (
c->finished_task_index + 1) %
c->max_tasks;
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.
#define i(width, name, range_min, range_max)
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
AVCodecParameters * avcodec_parameters_alloc(void)
void avcodec_parameters_free(AVCodecParameters **ppar)
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
#define atomic_store(object, desired)
#define atomic_load(object)
#define atomic_init(obj, value)
int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, AVFrame *frame, int *got_packet)
static void *attribute_align_arg worker(void *v)
av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
Initialize frame thread encoder.
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet_ptr)
av_cold void ff_frame_thread_encoder_free(AVCodecContext *avctx)
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
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.
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.
#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.
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
common internal api header.
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.
#define attribute_align_arg
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)
#define DEFINE_OFFSET_ARRAY(type, name, cnt_variable, mutexes, conds)
main external API structure.
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
int active_thread_type
Which multithreading methods are in use by the codec.
int(* get_encode_buffer)(struct AVCodecContext *s, AVPacket *pkt, int flags)
This callback is called at the beginning of each packet to get a data buffer for it.
const struct AVCodec * codec
int thread_type
Which multithreading methods to use.
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
void * opaque
Private data of the user, can be used to carry app specific stuff.
int flags
AV_CODEC_FLAG_*.
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
struct AVCodecInternal * internal
Private context used for internal data.
void * frame_thread_encoder
This struct describes the properties of an encoded stream.
const AVClass * priv_class
AVClass for the private context.
int capabilities
Codec capabilities.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
pthread_mutex_t task_fifo_mutex
pthread_mutex_t finished_task_mutex
pthread_t worker[MAX_THREADS]
unsigned finished_task_index
unsigned pthread_init_cnt
pthread_cond_t finished_task_cond
pthread_cond_t task_fifo_cond
AVCodecContext * parent_avctx