1 /*
2 * Copyright (c) 2012 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdatomic.h>
22
24
33
34 #define MAX_THREADS 64
35 #define BUFFER_SIZE (2*MAX_THREADS)
36
43
47
51
55
58
62
63 static void * attribute_align_arg
worker(
void *v){
67
69 int got_packet, ret;
72
74 if(!pkt) continue;
76
82 }
84 }
88
94 if(got_packet) {
96 if (ret >= 0 && ret2 < 0)
97 ret = ret2;
98 } else {
101 }
107 }
115 }
116
118 int i=0;
120
121
124 return 0;
125
130 "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
131 "or a constant quantizer if you want to use multiple cpu cores\n");
133 }
138 "MJPEG CBR encoding works badly with frame multi-threading, consider "
139 "using -threads 1, -thread_type slice or a constant quantizer.\n");
140
143 int warn = 0;
144 int context_model = 0;
146
147 if (con && con->
value)
148 context_model = atoi(con->
value);
149
151 warn = 1;
152 else if(context_model > 0) {
155 warn = !t || !t->
value || !atoi(t->
value) ? 1 : 0;
156 }
157 // huffyuv does not support these with multiple frame threads currently
158 if (warn) {
160 "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
162 }
163 }
164
168 }
169
171 return 0;
172
175
178 if(!c)
180
182
186
193
196 int ret;
197 void *tmpv;
199 if(!thread_avctx)
202 *thread_avctx = *avctx;
204 if (ret < 0)
210 if (ret < 0)
212 } else
216
222 }
228 }
229 }
230
232
233 return 0;
238 return -1;
239 }
240
242 int i;
244
249
252 }
253
261 }
262
266 int ret;
267
269
270 if(frame){
272 if(!new)
275 if(ret < 0) {
277 return ret;
278 }
279
286
288 }
289
295 return 0;
296 }
297
300 }
304 *got_packet_ptr = 1;
308
310 }
const struct AVCodec * codec
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr)
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
unsigned finished_task_index
This structure describes decoded (raw) audio or video data.
#define pthread_mutex_lock(a)
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
#define atomic_store(object, desired)
#define AV_LOG_WARNING
Something somehow does not look correct.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
AVCodecContext * parent_avctx
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
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.
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static av_cold int end(AVCodecContext *avctx)
Multithreading support functions.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Task finished_tasks[BUFFER_SIZE]
void * frame_thread_encoder
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
pthread_mutex_t buffer_mutex
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
#define atomic_load(object)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int active_thread_type
Which multithreading methods are in use by the codec.
int capabilities
Codec capabilities.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
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_cond_t finished_task_cond
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define FF_THREAD_FRAME
Decode more than one frame at once.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
pthread_mutex_t task_fifo_mutex
#define pthread_mutex_unlock(a)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Libavcodec external API header.
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 ...
main external API structure.
a very simple circular buffer FIFO implementation
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
pthread_t worker[MAX_THREADS]
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
const AVClass * priv_class
AVClass for the private context.
const OptionDef options[]
common internal api header.
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
pthread_mutex_t finished_task_mutex
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
struct AVCodecInternal * internal
Private context used for internal data.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
#define atomic_init(obj, value)
void ff_frame_thread_encoder_free(AVCodecContext *avctx)
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
static void *attribute_align_arg worker(void *v)
pthread_cond_t task_fifo_cond
This structure stores compressed data.
int thread_type
Which multithreading methods to use.