Go to the documentation of this file.
21 #include <stdatomic.h>
34 #define MAX_THREADS 64
35 #define BUFFER_SIZE (2*MAX_THREADS)
63 static void * attribute_align_arg
worker(
void *v){
69 int got_packet = 0,
ret;
92 if (
ret >= 0 && ret2 < 0)
105 c->finished_tasks[task.
index].return_code =
ret;
131 "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
132 "or a constant quantizer if you want to use multiple cpu cores\n");
139 "MJPEG CBR encoding works badly with frame multi-threading, consider "
140 "using -threads 1, -thread_type slice or a constant quantizer.\n");
145 int context_model = 0;
148 if (con && con->
value)
149 context_model = atoi(con->
value);
153 else if(context_model > 0) {
156 warn = !t || !t->
value || !atoi(t->
value) ? 1 : 0;
161 "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
182 c->parent_avctx = avctx;
205 *thread_avctx = *avctx;
271 if (
c->finished_tasks[
i].outdata !=
NULL) {
274 c->finished_tasks[
i].outdata =
NULL;
304 task.
index =
c->task_index;
315 if (
c->task_index ==
c->finished_task_index ||
316 (
frame && !
c->finished_tasks[
c->finished_task_index].outdata &&
317 (
c->task_index -
c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)) {
322 while (!
c->finished_tasks[
c->finished_task_index].outdata) {
325 task =
c->finished_tasks[
c->finished_task_index];
329 av_freep(&
c->finished_tasks[
c->finished_task_index].outdata);
330 c->finished_task_index = (
c->finished_task_index+1) %
BUFFER_SIZE;
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define atomic_store(object, desired)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
const AVClass * priv_class
AVClass for the private context.
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static av_cold int end(AVCodecContext *avctx)
This structure describes decoded (raw) audio or video data.
int capabilities
Codec capabilities.
void * frame_thread_encoder
int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options)
Initialize frame thread encoder.
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
pthread_mutex_t buffer_mutex
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
const struct AVCodec * codec
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
AVCodecContext * parent_avctx
int flags
AV_CODEC_FLAG_*.
unsigned finished_task_index
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
void ff_frame_thread_encoder_free(AVCodecContext *avctx)
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int thread_type
Which multithreading methods to use.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define atomic_load(object)
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
pthread_mutex_t task_fifo_mutex
struct AVCodecInternal * internal
Private context used for internal data.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
#define pthread_mutex_unlock(a)
const OptionDef options[]
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int(* encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt, const struct AVFrame *frame, int *got_packet_ptr)
Encode data to an AVPacket.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
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.
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
#define FF_THREAD_FRAME
Decode more than one frame at once.
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
pthread_cond_t finished_task_cond
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr)
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static void *attribute_align_arg worker(void *v)
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
pthread_mutex_t finished_task_mutex
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
main external API structure.
int active_thread_type
Which multithreading methods are in use by the codec.
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
This structure stores compressed data.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
#define atomic_init(obj, value)
pthread_cond_t task_fifo_cond
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#define pthread_mutex_lock(a)