00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "frame_thread_encoder.h"
00022
00023 #include "libavutil/fifo.h"
00024 #include "libavutil/avassert.h"
00025 #include "libavutil/imgutils.h"
00026 #include "avcodec.h"
00027 #include "internal.h"
00028 #include "thread.h"
00029
00030 #if HAVE_PTHREADS
00031 #include <pthread.h>
00032 #elif HAVE_W32THREADS
00033 #include "w32pthreads.h"
00034 #elif HAVE_OS2THREADS
00035 #include "os2threads.h"
00036 #endif
00037
00038 #define MAX_THREADS 64
00039 #define BUFFER_SIZE (2*MAX_THREADS)
00040
00041 typedef struct{
00042 void *indata;
00043 void *outdata;
00044 int64_t return_code;
00045 unsigned index;
00046 } Task;
00047
00048 typedef struct{
00049 AVCodecContext *parent_avctx;
00050 pthread_mutex_t buffer_mutex;
00051
00052 AVFifoBuffer *task_fifo;
00053 pthread_mutex_t task_fifo_mutex;
00054 pthread_cond_t task_fifo_cond;
00055
00056 Task finished_tasks[BUFFER_SIZE];
00057 pthread_mutex_t finished_task_mutex;
00058 pthread_cond_t finished_task_cond;
00059
00060 unsigned task_index;
00061 unsigned finished_task_index;
00062
00063 pthread_t worker[MAX_THREADS];
00064 int exit;
00065 } ThreadContext;
00066
00067 static void * attribute_align_arg worker(void *v){
00068 AVCodecContext *avctx = v;
00069 ThreadContext *c = avctx->internal->frame_thread_encoder;
00070 AVPacket *pkt = NULL;
00071
00072 while(!c->exit){
00073 int got_packet, ret;
00074 AVFrame *frame;
00075 Task task;
00076
00077 if(!pkt) pkt= av_mallocz(sizeof(*pkt));
00078 if(!pkt) continue;
00079 av_init_packet(pkt);
00080
00081 pthread_mutex_lock(&c->task_fifo_mutex);
00082 while (av_fifo_size(c->task_fifo) <= 0 || c->exit) {
00083 if(c->exit){
00084 pthread_mutex_unlock(&c->task_fifo_mutex);
00085 goto end;
00086 }
00087 pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
00088 }
00089 av_fifo_generic_read(c->task_fifo, &task, sizeof(task), NULL);
00090 pthread_mutex_unlock(&c->task_fifo_mutex);
00091 frame = task.indata;
00092
00093 ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
00094 pthread_mutex_lock(&c->buffer_mutex);
00095 c->parent_avctx->release_buffer(c->parent_avctx, frame);
00096 pthread_mutex_unlock(&c->buffer_mutex);
00097 av_freep(&frame);
00098 if(!got_packet)
00099 continue;
00100 av_dup_packet(pkt);
00101 pthread_mutex_lock(&c->finished_task_mutex);
00102 c->finished_tasks[task.index].outdata = pkt; pkt = NULL;
00103 c->finished_tasks[task.index].return_code = ret;
00104 pthread_cond_signal(&c->finished_task_cond);
00105 pthread_mutex_unlock(&c->finished_task_mutex);
00106 }
00107 end:
00108 av_free(pkt);
00109 pthread_mutex_lock(&c->buffer_mutex);
00110 avcodec_close(avctx);
00111 pthread_mutex_unlock(&c->buffer_mutex);
00112 av_freep(&avctx);
00113 return NULL;
00114 }
00115
00116 int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
00117 int i=0;
00118 ThreadContext *c;
00119
00120
00121 if( !(avctx->thread_type & FF_THREAD_FRAME)
00122 || !(avctx->codec->capabilities & CODEC_CAP_INTRA_ONLY))
00123 return 0;
00124
00125 if(!avctx->thread_count) {
00126 avctx->thread_count = ff_get_logical_cpus(avctx);
00127 avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
00128 }
00129
00130 if(avctx->thread_count <= 1)
00131 return 0;
00132
00133 if(avctx->thread_count > MAX_THREADS)
00134 return AVERROR(EINVAL);
00135
00136 av_assert0(!avctx->internal->frame_thread_encoder);
00137 c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
00138 if(!c)
00139 return AVERROR(ENOMEM);
00140
00141 c->parent_avctx = avctx;
00142
00143 c->task_fifo = av_fifo_alloc(sizeof(Task) * BUFFER_SIZE);
00144 if(!c->task_fifo)
00145 goto fail;
00146
00147 pthread_mutex_init(&c->task_fifo_mutex, NULL);
00148 pthread_mutex_init(&c->finished_task_mutex, NULL);
00149 pthread_mutex_init(&c->buffer_mutex, NULL);
00150 pthread_cond_init(&c->task_fifo_cond, NULL);
00151 pthread_cond_init(&c->finished_task_cond, NULL);
00152
00153 for(i=0; i<avctx->thread_count ; i++){
00154 AVDictionary *tmp = NULL;
00155 AVCodecContext *thread_avctx = avcodec_alloc_context3(avctx->codec);
00156 if(!thread_avctx)
00157 goto fail;
00158 *thread_avctx = *avctx;
00159 thread_avctx->internal = NULL;
00160 thread_avctx->priv_data = av_malloc(avctx->codec->priv_data_size);
00161 if(!thread_avctx->priv_data) {
00162 av_freep(&thread_avctx);
00163 goto fail;
00164 }
00165 memcpy(thread_avctx->priv_data, avctx->priv_data, avctx->codec->priv_data_size);
00166 thread_avctx->thread_count = 1;
00167 thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
00168
00169 av_dict_copy(&tmp, options, 0);
00170 av_dict_set(&tmp, "threads", "1", 0);
00171 if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) {
00172 av_dict_free(&tmp);
00173 goto fail;
00174 }
00175 av_dict_free(&tmp);
00176 av_assert0(!thread_avctx->internal->frame_thread_encoder);
00177 thread_avctx->internal->frame_thread_encoder = c;
00178 if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) {
00179 goto fail;
00180 }
00181 }
00182
00183 avctx->active_thread_type = FF_THREAD_FRAME;
00184
00185 return 0;
00186 fail:
00187 avctx->thread_count = i;
00188 av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
00189 ff_frame_thread_encoder_free(avctx);
00190 return -1;
00191 }
00192
00193 void ff_frame_thread_encoder_free(AVCodecContext *avctx){
00194 int i;
00195 ThreadContext *c= avctx->internal->frame_thread_encoder;
00196
00197 pthread_mutex_lock(&c->task_fifo_mutex);
00198 c->exit = 1;
00199 pthread_cond_broadcast(&c->task_fifo_cond);
00200 pthread_mutex_unlock(&c->task_fifo_mutex);
00201
00202 for (i=0; i<avctx->thread_count; i++) {
00203 pthread_join(c->worker[i], NULL);
00204 }
00205
00206 pthread_mutex_destroy(&c->task_fifo_mutex);
00207 pthread_mutex_destroy(&c->finished_task_mutex);
00208 pthread_mutex_destroy(&c->buffer_mutex);
00209 pthread_cond_destroy(&c->task_fifo_cond);
00210 pthread_cond_destroy(&c->finished_task_cond);
00211 av_fifo_free(c->task_fifo); c->task_fifo = NULL;
00212 av_freep(&avctx->internal->frame_thread_encoder);
00213 }
00214
00215 int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr){
00216 ThreadContext *c = avctx->internal->frame_thread_encoder;
00217 Task task;
00218 int ret;
00219
00220 av_assert1(!*got_packet_ptr);
00221
00222 if(frame){
00223 if(!(avctx->flags & CODEC_FLAG_INPUT_PRESERVED)){
00224 AVFrame *new = avcodec_alloc_frame();
00225 if(!new)
00226 return AVERROR(ENOMEM);
00227 pthread_mutex_lock(&c->buffer_mutex);
00228 ret = c->parent_avctx->get_buffer(c->parent_avctx, new);
00229 pthread_mutex_unlock(&c->buffer_mutex);
00230 if(ret<0)
00231 return ret;
00232 new->pts = frame->pts;
00233 new->quality = frame->quality;
00234 new->pict_type = frame->pict_type;
00235 av_image_copy(new->data, new->linesize, (const uint8_t **)frame->data, frame->linesize,
00236 avctx->pix_fmt, avctx->width, avctx->height);
00237 frame = new;
00238 }
00239
00240 task.index = c->task_index;
00241 task.indata = (void*)frame;
00242 pthread_mutex_lock(&c->task_fifo_mutex);
00243 av_fifo_generic_write(c->task_fifo, &task, sizeof(task), NULL);
00244 pthread_cond_signal(&c->task_fifo_cond);
00245 pthread_mutex_unlock(&c->task_fifo_mutex);
00246
00247 c->task_index = (c->task_index+1) % BUFFER_SIZE;
00248
00249 if(!c->finished_tasks[c->finished_task_index].outdata && (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)
00250 return 0;
00251 }
00252
00253 if(c->task_index == c->finished_task_index)
00254 return 0;
00255
00256 pthread_mutex_lock(&c->finished_task_mutex);
00257 while (!c->finished_tasks[c->finished_task_index].outdata) {
00258 pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
00259 }
00260 task = c->finished_tasks[c->finished_task_index];
00261 *pkt = *(AVPacket*)(task.outdata);
00262 c->finished_tasks[c->finished_task_index].outdata= NULL;
00263 c->finished_task_index = (c->finished_task_index+1) % BUFFER_SIZE;
00264 pthread_mutex_unlock(&c->finished_task_mutex);
00265
00266 *got_packet_ptr = 1;
00267
00268 return task.return_code;
00269 }