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