00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00032 #include "config.h"
00033
00034 #if HAVE_SCHED_GETAFFINITY
00035 #define _GNU_SOURCE
00036 #include <sched.h>
00037 #endif
00038 #if HAVE_GETPROCESSAFFINITYMASK
00039 #include <windows.h>
00040 #endif
00041 #if HAVE_SYSCTL
00042 #if HAVE_SYS_PARAM_H
00043 #include <sys/param.h>
00044 #endif
00045 #include <sys/types.h>
00046 #include <sys/param.h>
00047 #include <sys/sysctl.h>
00048 #endif
00049 #if HAVE_SYSCONF
00050 #include <unistd.h>
00051 #endif
00052
00053 #include "avcodec.h"
00054 #include "internal.h"
00055 #include "thread.h"
00056 #include "libavutil/common.h"
00057
00058 #if HAVE_PTHREADS
00059 #include <pthread.h>
00060 #elif HAVE_W32THREADS
00061 #include "w32pthreads.h"
00062 #elif HAVE_OS2THREADS
00063 #include "os2threads.h"
00064 #endif
00065
00066 typedef int (action_func)(AVCodecContext *c, void *arg);
00067 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
00068
00069 typedef struct ThreadContext {
00070 pthread_t *workers;
00071 action_func *func;
00072 action_func2 *func2;
00073 void *args;
00074 int *rets;
00075 int rets_count;
00076 int job_count;
00077 int job_size;
00078
00079 pthread_cond_t last_job_cond;
00080 pthread_cond_t current_job_cond;
00081 pthread_mutex_t current_job_lock;
00082 int current_job;
00083 unsigned int current_execute;
00084 int done;
00085 } ThreadContext;
00086
00088 #define MAX_BUFFERS (32+1)
00089
00093 typedef struct PerThreadContext {
00094 struct FrameThreadContext *parent;
00095
00096 pthread_t thread;
00097 int thread_init;
00098 pthread_cond_t input_cond;
00099 pthread_cond_t progress_cond;
00100 pthread_cond_t output_cond;
00101
00102 pthread_mutex_t mutex;
00103 pthread_mutex_t progress_mutex;
00104
00105 AVCodecContext *avctx;
00106
00107 AVPacket avpkt;
00108 int allocated_buf_size;
00109
00110 AVFrame frame;
00111 int got_frame;
00112 int result;
00113
00114 enum {
00115 STATE_INPUT_READY,
00116 STATE_SETTING_UP,
00117 STATE_GET_BUFFER,
00121 STATE_SETUP_FINISHED
00122 } state;
00123
00128 AVFrame released_buffers[MAX_BUFFERS];
00129 int num_released_buffers;
00130
00134 volatile int progress[MAX_BUFFERS][2];
00135 volatile uint8_t progress_used[MAX_BUFFERS];
00136
00137 AVFrame *requested_frame;
00138 } PerThreadContext;
00139
00143 typedef struct FrameThreadContext {
00144 PerThreadContext *threads;
00145 PerThreadContext *prev_thread;
00146
00147 pthread_mutex_t buffer_mutex;
00148
00149 int next_decoding;
00150 int next_finished;
00151
00152 int delaying;
00157 int die;
00158 } FrameThreadContext;
00159
00160
00161
00162
00163 #define MAX_AUTO_THREADS 16
00164
00165 int ff_get_logical_cpus(AVCodecContext *avctx)
00166 {
00167 int ret, nb_cpus = 1;
00168 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
00169 cpu_set_t cpuset;
00170
00171 CPU_ZERO(&cpuset);
00172
00173 ret = sched_getaffinity(0, sizeof(cpuset), &cpuset);
00174 if (!ret) {
00175 nb_cpus = CPU_COUNT(&cpuset);
00176 }
00177 #elif HAVE_GETPROCESSAFFINITYMASK
00178 DWORD_PTR proc_aff, sys_aff;
00179 ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff);
00180 if (ret)
00181 nb_cpus = av_popcount64(proc_aff);
00182 #elif HAVE_SYSCTL && defined(HW_NCPU)
00183 int mib[2] = { CTL_HW, HW_NCPU };
00184 size_t len = sizeof(nb_cpus);
00185
00186 ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0);
00187 if (ret == -1)
00188 nb_cpus = 0;
00189 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
00190 nb_cpus = sysconf(_SC_NPROC_ONLN);
00191 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
00192 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
00193 #endif
00194 av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
00195
00196 if (avctx->height)
00197 nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
00198
00199 return nb_cpus;
00200 }
00201
00202
00203 static void* attribute_align_arg worker(void *v)
00204 {
00205 AVCodecContext *avctx = v;
00206 ThreadContext *c = avctx->thread_opaque;
00207 int our_job = c->job_count;
00208 int last_execute = 0;
00209 int thread_count = avctx->thread_count;
00210 int self_id;
00211
00212 pthread_mutex_lock(&c->current_job_lock);
00213 self_id = c->current_job++;
00214 for (;;){
00215 while (our_job >= c->job_count) {
00216 if (c->current_job == thread_count + c->job_count)
00217 pthread_cond_signal(&c->last_job_cond);
00218
00219 while (last_execute == c->current_execute && !c->done)
00220 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
00221 last_execute = c->current_execute;
00222 our_job = self_id;
00223
00224 if (c->done) {
00225 pthread_mutex_unlock(&c->current_job_lock);
00226 return NULL;
00227 }
00228 }
00229 pthread_mutex_unlock(&c->current_job_lock);
00230
00231 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
00232 c->func2(avctx, c->args, our_job, self_id);
00233
00234 pthread_mutex_lock(&c->current_job_lock);
00235 our_job = c->current_job++;
00236 }
00237 }
00238
00239 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
00240 {
00241 while (c->current_job != thread_count + c->job_count)
00242 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
00243 pthread_mutex_unlock(&c->current_job_lock);
00244 }
00245
00246 static void thread_free(AVCodecContext *avctx)
00247 {
00248 ThreadContext *c = avctx->thread_opaque;
00249 int i;
00250
00251 pthread_mutex_lock(&c->current_job_lock);
00252 c->done = 1;
00253 pthread_cond_broadcast(&c->current_job_cond);
00254 pthread_mutex_unlock(&c->current_job_lock);
00255
00256 for (i=0; i<avctx->thread_count; i++)
00257 pthread_join(c->workers[i], NULL);
00258
00259 pthread_mutex_destroy(&c->current_job_lock);
00260 pthread_cond_destroy(&c->current_job_cond);
00261 pthread_cond_destroy(&c->last_job_cond);
00262 av_free(c->workers);
00263 av_freep(&avctx->thread_opaque);
00264 }
00265
00266 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
00267 {
00268 ThreadContext *c= avctx->thread_opaque;
00269 int dummy_ret;
00270
00271 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
00272 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
00273
00274 if (job_count <= 0)
00275 return 0;
00276
00277 pthread_mutex_lock(&c->current_job_lock);
00278
00279 c->current_job = avctx->thread_count;
00280 c->job_count = job_count;
00281 c->job_size = job_size;
00282 c->args = arg;
00283 c->func = func;
00284 if (ret) {
00285 c->rets = ret;
00286 c->rets_count = job_count;
00287 } else {
00288 c->rets = &dummy_ret;
00289 c->rets_count = 1;
00290 }
00291 c->current_execute++;
00292 pthread_cond_broadcast(&c->current_job_cond);
00293
00294 avcodec_thread_park_workers(c, avctx->thread_count);
00295
00296 return 0;
00297 }
00298
00299 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
00300 {
00301 ThreadContext *c= avctx->thread_opaque;
00302 c->func2 = func2;
00303 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
00304 }
00305
00306 static int thread_init(AVCodecContext *avctx)
00307 {
00308 int i;
00309 ThreadContext *c;
00310 int thread_count = avctx->thread_count;
00311
00312 if (!thread_count) {
00313 int nb_cpus = ff_get_logical_cpus(avctx);
00314
00315 if (nb_cpus > 1)
00316 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
00317 else
00318 thread_count = avctx->thread_count = 1;
00319 }
00320
00321 if (thread_count <= 1) {
00322 avctx->active_thread_type = 0;
00323 return 0;
00324 }
00325
00326 c = av_mallocz(sizeof(ThreadContext));
00327 if (!c)
00328 return -1;
00329
00330 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
00331 if (!c->workers) {
00332 av_free(c);
00333 return -1;
00334 }
00335
00336 avctx->thread_opaque = c;
00337 c->current_job = 0;
00338 c->job_count = 0;
00339 c->job_size = 0;
00340 c->done = 0;
00341 pthread_cond_init(&c->current_job_cond, NULL);
00342 pthread_cond_init(&c->last_job_cond, NULL);
00343 pthread_mutex_init(&c->current_job_lock, NULL);
00344 pthread_mutex_lock(&c->current_job_lock);
00345 for (i=0; i<thread_count; i++) {
00346 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
00347 avctx->thread_count = i;
00348 pthread_mutex_unlock(&c->current_job_lock);
00349 ff_thread_free(avctx);
00350 return -1;
00351 }
00352 }
00353
00354 avcodec_thread_park_workers(c, thread_count);
00355
00356 avctx->execute = avcodec_thread_execute;
00357 avctx->execute2 = avcodec_thread_execute2;
00358 return 0;
00359 }
00360
00368 static attribute_align_arg void *frame_worker_thread(void *arg)
00369 {
00370 PerThreadContext *p = arg;
00371 FrameThreadContext *fctx = p->parent;
00372 AVCodecContext *avctx = p->avctx;
00373 const AVCodec *codec = avctx->codec;
00374
00375 pthread_mutex_lock(&p->mutex);
00376 while (1) {
00377 int i;
00378 while (p->state == STATE_INPUT_READY && !fctx->die)
00379 pthread_cond_wait(&p->input_cond, &p->mutex);
00380
00381 if (fctx->die) break;
00382
00383 if (!codec->update_thread_context && (avctx->thread_safe_callbacks || avctx->get_buffer == avcodec_default_get_buffer))
00384 ff_thread_finish_setup(avctx);
00385
00386 avcodec_get_frame_defaults(&p->frame);
00387 p->got_frame = 0;
00388 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
00389
00390
00391
00392 p->frame.extended_data = p->frame.data;
00393
00394 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
00395
00396 pthread_mutex_lock(&p->progress_mutex);
00397 for (i = 0; i < MAX_BUFFERS; i++)
00398 if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
00399 p->progress[i][0] = INT_MAX;
00400 p->progress[i][1] = INT_MAX;
00401 }
00402 p->state = STATE_INPUT_READY;
00403
00404 pthread_cond_broadcast(&p->progress_cond);
00405 pthread_cond_signal(&p->output_cond);
00406 pthread_mutex_unlock(&p->progress_mutex);
00407 }
00408 pthread_mutex_unlock(&p->mutex);
00409
00410 return NULL;
00411 }
00412
00420 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
00421 {
00422 int err = 0;
00423
00424 if (dst != src) {
00425 dst->time_base = src->time_base;
00426 dst->width = src->width;
00427 dst->height = src->height;
00428 dst->pix_fmt = src->pix_fmt;
00429
00430 dst->coded_width = src->coded_width;
00431 dst->coded_height = src->coded_height;
00432
00433 dst->has_b_frames = src->has_b_frames;
00434 dst->idct_algo = src->idct_algo;
00435
00436 dst->bits_per_coded_sample = src->bits_per_coded_sample;
00437 dst->sample_aspect_ratio = src->sample_aspect_ratio;
00438 dst->dtg_active_format = src->dtg_active_format;
00439
00440 dst->profile = src->profile;
00441 dst->level = src->level;
00442
00443 dst->bits_per_raw_sample = src->bits_per_raw_sample;
00444 dst->ticks_per_frame = src->ticks_per_frame;
00445 dst->color_primaries = src->color_primaries;
00446
00447 dst->color_trc = src->color_trc;
00448 dst->colorspace = src->colorspace;
00449 dst->color_range = src->color_range;
00450 dst->chroma_sample_location = src->chroma_sample_location;
00451 }
00452
00453 if (for_user) {
00454 dst->delay = src->thread_count - 1;
00455 dst->coded_frame = src->coded_frame;
00456 } else {
00457 if (dst->codec->update_thread_context)
00458 err = dst->codec->update_thread_context(dst, src);
00459 }
00460
00461 return err;
00462 }
00463
00471 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
00472 {
00473 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
00474 dst->flags = src->flags;
00475
00476 dst->draw_horiz_band= src->draw_horiz_band;
00477 dst->get_buffer = src->get_buffer;
00478 dst->release_buffer = src->release_buffer;
00479
00480 dst->opaque = src->opaque;
00481 dst->debug = src->debug;
00482 dst->debug_mv = src->debug_mv;
00483
00484 dst->slice_flags = src->slice_flags;
00485 dst->flags2 = src->flags2;
00486
00487 copy_fields(skip_loop_filter, subtitle_header);
00488
00489 dst->frame_number = src->frame_number;
00490 dst->reordered_opaque = src->reordered_opaque;
00491 dst->thread_safe_callbacks = src->thread_safe_callbacks;
00492
00493 if (src->slice_count && src->slice_offset) {
00494 if (dst->slice_count < src->slice_count) {
00495 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
00496 sizeof(*dst->slice_offset));
00497 if (!tmp) {
00498 av_free(dst->slice_offset);
00499 return AVERROR(ENOMEM);
00500 }
00501 dst->slice_offset = tmp;
00502 }
00503 memcpy(dst->slice_offset, src->slice_offset,
00504 src->slice_count * sizeof(*dst->slice_offset));
00505 }
00506 dst->slice_count = src->slice_count;
00507 return 0;
00508 #undef copy_fields
00509 }
00510
00511 static void free_progress(AVFrame *f)
00512 {
00513 PerThreadContext *p = f->owner->thread_opaque;
00514 volatile int *progress = f->thread_opaque;
00515
00516 p->progress_used[(progress - p->progress[0]) / 2] = 0;
00517 }
00518
00520 static void release_delayed_buffers(PerThreadContext *p)
00521 {
00522 FrameThreadContext *fctx = p->parent;
00523
00524 while (p->num_released_buffers > 0) {
00525 AVFrame *f;
00526
00527 pthread_mutex_lock(&fctx->buffer_mutex);
00528 f = &p->released_buffers[--p->num_released_buffers];
00529 free_progress(f);
00530 f->thread_opaque = NULL;
00531
00532 f->owner->release_buffer(f->owner, f);
00533 pthread_mutex_unlock(&fctx->buffer_mutex);
00534 }
00535 }
00536
00537 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
00538 {
00539 FrameThreadContext *fctx = p->parent;
00540 PerThreadContext *prev_thread = fctx->prev_thread;
00541 const AVCodec *codec = p->avctx->codec;
00542 uint8_t *buf = p->avpkt.data;
00543
00544 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
00545
00546 pthread_mutex_lock(&p->mutex);
00547
00548 release_delayed_buffers(p);
00549
00550 if (prev_thread) {
00551 int err;
00552 if (prev_thread->state == STATE_SETTING_UP) {
00553 pthread_mutex_lock(&prev_thread->progress_mutex);
00554 while (prev_thread->state == STATE_SETTING_UP)
00555 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
00556 pthread_mutex_unlock(&prev_thread->progress_mutex);
00557 }
00558
00559 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
00560 if (err) {
00561 pthread_mutex_unlock(&p->mutex);
00562 return err;
00563 }
00564 }
00565
00566 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00567 p->avpkt = *avpkt;
00568 p->avpkt.data = buf;
00569 memcpy(buf, avpkt->data, avpkt->size);
00570 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00571
00572 p->state = STATE_SETTING_UP;
00573 pthread_cond_signal(&p->input_cond);
00574 pthread_mutex_unlock(&p->mutex);
00575
00576
00577
00578
00579
00580
00581
00582 if (!p->avctx->thread_safe_callbacks &&
00583 p->avctx->get_buffer != avcodec_default_get_buffer) {
00584 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
00585 pthread_mutex_lock(&p->progress_mutex);
00586 while (p->state == STATE_SETTING_UP)
00587 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00588
00589 if (p->state == STATE_GET_BUFFER) {
00590 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
00591 p->state = STATE_SETTING_UP;
00592 pthread_cond_signal(&p->progress_cond);
00593 }
00594 pthread_mutex_unlock(&p->progress_mutex);
00595 }
00596 }
00597
00598 fctx->prev_thread = p;
00599 fctx->next_decoding++;
00600
00601 return 0;
00602 }
00603
00604 int ff_thread_decode_frame(AVCodecContext *avctx,
00605 AVFrame *picture, int *got_picture_ptr,
00606 AVPacket *avpkt)
00607 {
00608 FrameThreadContext *fctx = avctx->thread_opaque;
00609 int finished = fctx->next_finished;
00610 PerThreadContext *p;
00611 int err;
00612
00613
00614
00615
00616
00617 p = &fctx->threads[fctx->next_decoding];
00618 err = update_context_from_user(p->avctx, avctx);
00619 if (err) return err;
00620 err = submit_packet(p, avpkt);
00621 if (err) return err;
00622
00623
00624
00625
00626
00627 if (fctx->delaying) {
00628 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
00629
00630 *got_picture_ptr=0;
00631 if (avpkt->size)
00632 return avpkt->size;
00633 }
00634
00635
00636
00637
00638
00639
00640
00641
00642 do {
00643 p = &fctx->threads[finished++];
00644
00645 if (p->state != STATE_INPUT_READY) {
00646 pthread_mutex_lock(&p->progress_mutex);
00647 while (p->state != STATE_INPUT_READY)
00648 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
00649 pthread_mutex_unlock(&p->progress_mutex);
00650 }
00651
00652 *picture = p->frame;
00653 *got_picture_ptr = p->got_frame;
00654 picture->pkt_dts = p->avpkt.dts;
00655
00656
00657
00658
00659
00660
00661
00662 p->got_frame = 0;
00663
00664 if (finished >= avctx->thread_count) finished = 0;
00665 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
00666
00667 update_context_from_thread(avctx, p->avctx, 1);
00668
00669 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
00670
00671 fctx->next_finished = finished;
00672
00673
00674 return (p->result >= 0) ? avpkt->size : p->result;
00675 }
00676
00677 void ff_thread_report_progress(AVFrame *f, int n, int field)
00678 {
00679 PerThreadContext *p;
00680 volatile int *progress = f->thread_opaque;
00681
00682 if (!progress || progress[field] >= n) return;
00683
00684 p = f->owner->thread_opaque;
00685
00686 if (f->owner->debug&FF_DEBUG_THREADS)
00687 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
00688
00689 pthread_mutex_lock(&p->progress_mutex);
00690 progress[field] = n;
00691 pthread_cond_broadcast(&p->progress_cond);
00692 pthread_mutex_unlock(&p->progress_mutex);
00693 }
00694
00695 void ff_thread_await_progress(AVFrame *f, int n, int field)
00696 {
00697 PerThreadContext *p;
00698 volatile int *progress = f->thread_opaque;
00699
00700 if (!progress || progress[field] >= n) return;
00701
00702 p = f->owner->thread_opaque;
00703
00704 if (f->owner->debug&FF_DEBUG_THREADS)
00705 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
00706
00707 pthread_mutex_lock(&p->progress_mutex);
00708 while (progress[field] < n)
00709 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00710 pthread_mutex_unlock(&p->progress_mutex);
00711 }
00712
00713 void ff_thread_finish_setup(AVCodecContext *avctx) {
00714 PerThreadContext *p = avctx->thread_opaque;
00715
00716 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
00717
00718 if(p->state == STATE_SETUP_FINISHED){
00719 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
00720 }
00721
00722 pthread_mutex_lock(&p->progress_mutex);
00723 p->state = STATE_SETUP_FINISHED;
00724 pthread_cond_broadcast(&p->progress_cond);
00725 pthread_mutex_unlock(&p->progress_mutex);
00726 }
00727
00729 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
00730 {
00731 int i;
00732
00733 for (i = 0; i < thread_count; i++) {
00734 PerThreadContext *p = &fctx->threads[i];
00735
00736 if (p->state != STATE_INPUT_READY) {
00737 pthread_mutex_lock(&p->progress_mutex);
00738 while (p->state != STATE_INPUT_READY)
00739 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
00740 pthread_mutex_unlock(&p->progress_mutex);
00741 }
00742 p->got_frame = 0;
00743 }
00744 }
00745
00746 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
00747 {
00748 FrameThreadContext *fctx = avctx->thread_opaque;
00749 const AVCodec *codec = avctx->codec;
00750 int i;
00751
00752 park_frame_worker_threads(fctx, thread_count);
00753
00754 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
00755 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
00756
00757 fctx->die = 1;
00758
00759 for (i = 0; i < thread_count; i++) {
00760 PerThreadContext *p = &fctx->threads[i];
00761
00762 pthread_mutex_lock(&p->mutex);
00763 pthread_cond_signal(&p->input_cond);
00764 pthread_mutex_unlock(&p->mutex);
00765
00766 if (p->thread_init)
00767 pthread_join(p->thread, NULL);
00768 p->thread_init=0;
00769
00770 if (codec->close)
00771 codec->close(p->avctx);
00772
00773 avctx->codec = NULL;
00774
00775 release_delayed_buffers(p);
00776 }
00777
00778 for (i = 0; i < thread_count; i++) {
00779 PerThreadContext *p = &fctx->threads[i];
00780
00781 avcodec_default_free_buffers(p->avctx);
00782
00783 pthread_mutex_destroy(&p->mutex);
00784 pthread_mutex_destroy(&p->progress_mutex);
00785 pthread_cond_destroy(&p->input_cond);
00786 pthread_cond_destroy(&p->progress_cond);
00787 pthread_cond_destroy(&p->output_cond);
00788 av_freep(&p->avpkt.data);
00789
00790 if (i) {
00791 av_freep(&p->avctx->priv_data);
00792 av_freep(&p->avctx->internal);
00793 av_freep(&p->avctx->slice_offset);
00794 }
00795
00796 av_freep(&p->avctx);
00797 }
00798
00799 av_freep(&fctx->threads);
00800 pthread_mutex_destroy(&fctx->buffer_mutex);
00801 av_freep(&avctx->thread_opaque);
00802 }
00803
00804 static int frame_thread_init(AVCodecContext *avctx)
00805 {
00806 int thread_count = avctx->thread_count;
00807 const AVCodec *codec = avctx->codec;
00808 AVCodecContext *src = avctx;
00809 FrameThreadContext *fctx;
00810 int i, err = 0;
00811
00812 if (!thread_count) {
00813 int nb_cpus = ff_get_logical_cpus(avctx);
00814 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
00815 nb_cpus = 1;
00816
00817 if (nb_cpus > 1)
00818 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
00819 else
00820 thread_count = avctx->thread_count = 1;
00821 }
00822
00823 if (thread_count <= 1) {
00824 avctx->active_thread_type = 0;
00825 return 0;
00826 }
00827
00828 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
00829
00830 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
00831 pthread_mutex_init(&fctx->buffer_mutex, NULL);
00832 fctx->delaying = 1;
00833
00834 for (i = 0; i < thread_count; i++) {
00835 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
00836 PerThreadContext *p = &fctx->threads[i];
00837
00838 pthread_mutex_init(&p->mutex, NULL);
00839 pthread_mutex_init(&p->progress_mutex, NULL);
00840 pthread_cond_init(&p->input_cond, NULL);
00841 pthread_cond_init(&p->progress_cond, NULL);
00842 pthread_cond_init(&p->output_cond, NULL);
00843
00844 p->parent = fctx;
00845 p->avctx = copy;
00846
00847 if (!copy) {
00848 err = AVERROR(ENOMEM);
00849 goto error;
00850 }
00851
00852 *copy = *src;
00853 copy->thread_opaque = p;
00854 copy->pkt = &p->avpkt;
00855
00856 if (!i) {
00857 src = copy;
00858
00859 if (codec->init)
00860 err = codec->init(copy);
00861
00862 update_context_from_thread(avctx, copy, 1);
00863 } else {
00864 copy->priv_data = av_malloc(codec->priv_data_size);
00865 if (!copy->priv_data) {
00866 err = AVERROR(ENOMEM);
00867 goto error;
00868 }
00869 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
00870 copy->internal = av_malloc(sizeof(AVCodecInternal));
00871 if (!copy->internal) {
00872 err = AVERROR(ENOMEM);
00873 goto error;
00874 }
00875 *copy->internal = *src->internal;
00876 copy->internal->is_copy = 1;
00877
00878 if (codec->init_thread_copy)
00879 err = codec->init_thread_copy(copy);
00880 }
00881
00882 if (err) goto error;
00883
00884 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
00885 p->thread_init= !err;
00886 if(!p->thread_init)
00887 goto error;
00888 }
00889
00890 return 0;
00891
00892 error:
00893 frame_thread_free(avctx, i+1);
00894
00895 return err;
00896 }
00897
00898 void ff_thread_flush(AVCodecContext *avctx)
00899 {
00900 int i;
00901 FrameThreadContext *fctx = avctx->thread_opaque;
00902
00903 if (!avctx->thread_opaque) return;
00904
00905 park_frame_worker_threads(fctx, avctx->thread_count);
00906 if (fctx->prev_thread) {
00907 if (fctx->prev_thread != &fctx->threads[0])
00908 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
00909 if (avctx->codec->flush)
00910 avctx->codec->flush(fctx->threads[0].avctx);
00911 }
00912
00913 fctx->next_decoding = fctx->next_finished = 0;
00914 fctx->delaying = 1;
00915 fctx->prev_thread = NULL;
00916 for (i = 0; i < avctx->thread_count; i++) {
00917 PerThreadContext *p = &fctx->threads[i];
00918
00919 p->got_frame = 0;
00920
00921 release_delayed_buffers(p);
00922 }
00923 }
00924
00925 static volatile int *allocate_progress(PerThreadContext *p)
00926 {
00927 int i;
00928
00929 for (i = 0; i < MAX_BUFFERS; i++)
00930 if (!p->progress_used[i]) break;
00931
00932 if (i == MAX_BUFFERS) {
00933 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
00934 return NULL;
00935 }
00936
00937 p->progress_used[i] = 1;
00938
00939 return p->progress[i];
00940 }
00941
00942 int ff_thread_can_start_frame(AVCodecContext *avctx)
00943 {
00944 PerThreadContext *p = avctx->thread_opaque;
00945 if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
00946 (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks &&
00947 avctx->get_buffer != avcodec_default_get_buffer))) {
00948 return 0;
00949 }
00950 return 1;
00951 }
00952
00953 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
00954 {
00955 PerThreadContext *p = avctx->thread_opaque;
00956 int err;
00957 volatile int *progress;
00958
00959 f->owner = avctx;
00960
00961 ff_init_buffer_info(avctx, f);
00962
00963 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
00964 f->thread_opaque = NULL;
00965 return avctx->get_buffer(avctx, f);
00966 }
00967
00968 if (p->state != STATE_SETTING_UP &&
00969 (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks &&
00970 avctx->get_buffer != avcodec_default_get_buffer))) {
00971 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
00972 return -1;
00973 }
00974
00975 pthread_mutex_lock(&p->parent->buffer_mutex);
00976 f->thread_opaque = (int*)(progress = allocate_progress(p));
00977
00978 if (!progress) {
00979 pthread_mutex_unlock(&p->parent->buffer_mutex);
00980 return -1;
00981 }
00982
00983 progress[0] =
00984 progress[1] = -1;
00985
00986 if (avctx->thread_safe_callbacks ||
00987 avctx->get_buffer == avcodec_default_get_buffer) {
00988 err = avctx->get_buffer(avctx, f);
00989 } else {
00990 pthread_mutex_lock(&p->progress_mutex);
00991 p->requested_frame = f;
00992 p->state = STATE_GET_BUFFER;
00993 pthread_cond_broadcast(&p->progress_cond);
00994
00995 while (p->state != STATE_SETTING_UP)
00996 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00997
00998 err = p->result;
00999
01000 pthread_mutex_unlock(&p->progress_mutex);
01001
01002 if (!avctx->codec->update_thread_context)
01003 ff_thread_finish_setup(avctx);
01004 }
01005
01006 if (err) {
01007 free_progress(f);
01008 f->thread_opaque = NULL;
01009 }
01010 pthread_mutex_unlock(&p->parent->buffer_mutex);
01011
01012 return err;
01013 }
01014
01015 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
01016 {
01017 PerThreadContext *p = avctx->thread_opaque;
01018 FrameThreadContext *fctx;
01019
01020 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
01021 avctx->release_buffer(avctx, f);
01022 return;
01023 }
01024
01025 if (p->num_released_buffers >= MAX_BUFFERS) {
01026 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
01027 return;
01028 }
01029
01030 if(avctx->debug & FF_DEBUG_BUFFERS)
01031 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
01032
01033 fctx = p->parent;
01034 pthread_mutex_lock(&fctx->buffer_mutex);
01035 p->released_buffers[p->num_released_buffers++] = *f;
01036 pthread_mutex_unlock(&fctx->buffer_mutex);
01037 memset(f->data, 0, sizeof(f->data));
01038 }
01039
01049 static void validate_thread_parameters(AVCodecContext *avctx)
01050 {
01051 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
01052 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
01053 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
01054 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
01055 if (avctx->thread_count == 1) {
01056 avctx->active_thread_type = 0;
01057 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
01058 avctx->active_thread_type = FF_THREAD_FRAME;
01059 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
01060 avctx->thread_type & FF_THREAD_SLICE) {
01061 avctx->active_thread_type = FF_THREAD_SLICE;
01062 } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) {
01063 avctx->thread_count = 1;
01064 avctx->active_thread_type = 0;
01065 }
01066
01067 if (avctx->thread_count > MAX_AUTO_THREADS)
01068 av_log(avctx, AV_LOG_WARNING,
01069 "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
01070 avctx->thread_count, MAX_AUTO_THREADS);
01071 }
01072
01073 int ff_thread_init(AVCodecContext *avctx)
01074 {
01075 if (avctx->thread_opaque) {
01076 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
01077 return -1;
01078 }
01079
01080 #if HAVE_W32THREADS
01081 w32thread_init();
01082 #endif
01083
01084 if (avctx->codec) {
01085 validate_thread_parameters(avctx);
01086
01087 if (avctx->active_thread_type&FF_THREAD_SLICE)
01088 return thread_init(avctx);
01089 else if (avctx->active_thread_type&FF_THREAD_FRAME)
01090 return frame_thread_init(avctx);
01091 }
01092
01093 return 0;
01094 }
01095
01096 void ff_thread_free(AVCodecContext *avctx)
01097 {
01098 if (avctx->active_thread_type&FF_THREAD_FRAME)
01099 frame_thread_free(avctx, avctx->thread_count);
01100 else
01101 thread_free(avctx);
01102 }