21 #include <stdatomic.h>
33 #define MAX_THREADS 64
38 #define BUFFER_SIZE (MAX_THREADS + 2)
72 int got_packet = 0, ret;
79 while (
c->next_task_index ==
c->task_index ||
atomic_load(&
c->exit)) {
86 task_index =
c->next_task_index;
87 c->next_task_index = (
c->next_task_index + 1) %
c->max_tasks;
93 task = &
c->tasks[task_index];
100 if (ret >= 0 && ret2 < 0)
137 "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
138 "or a constant quantizer if you want to use multiple cpu cores\n");
145 "MJPEG CBR encoding works badly with frame multi-threading, consider "
146 "using -threads 1, -thread_type slice or a constant quantizer.\n");
151 int context_model = 0;
154 if (con && con->
value)
155 context_model = atoi(con->
value);
159 else if(context_model > 0) {
162 warn = !t || !t->
value || !atoi(t->
value) ? 1 : 0;
167 "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
188 c->parent_avctx = avctx;
198 for (
unsigned i = 0;
i <
c->max_tasks;
i++) {
212 *thread_avctx = *avctx;
265 for (
unsigned i = 0;
i <
c->max_tasks;
i++) {
290 c->task_index = (
c->task_index + 1) %
c->max_tasks;
295 outtask = &
c->tasks[
c->finished_task_index];
299 if (
c->task_index ==
c->finished_task_index ||
301 (
c->task_index -
c->finished_task_index +
c->max_tasks) %
c->max_tasks <= avctx->
thread_count)) {
315 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 atomic_store(object, desired)
#define atomic_load(object)
#define atomic_init(obj, value)
const OptionDef options[]
#define pthread_mutex_lock(a)
#define pthread_mutex_unlock(a)
int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options)
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet_ptr)
void ff_frame_thread_encoder_free(AVCodecContext *avctx)
static void *attribute_align_arg worker(void *v)
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#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.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
av_cold int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
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_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
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.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
common internal API header
#define attribute_align_arg
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_cond_destroy(pthread_cond_t *cond)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
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_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
main external API structure.
int active_thread_type
Which multithreading methods are in use by the codec.
const struct AVCodec * codec
int thread_type
Which multithreading methods to use.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
int flags
AV_CODEC_FLAG_*.
struct AVCodecInternal * internal
Private context used for internal data.
void * frame_thread_encoder
const AVClass * priv_class
AVClass for the private context.
int(* encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt, const struct AVFrame *frame, int *got_packet_ptr)
Encode data to an AVPacket.
int capabilities
Codec capabilities.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
pthread_mutex_t task_fifo_mutex
pthread_mutex_t finished_task_mutex
pthread_mutex_t buffer_mutex
unsigned finished_task_index
pthread_cond_t finished_task_cond
pthread_cond_t task_fifo_cond
AVCodecContext * parent_avctx