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 while (1) {
00375 int i;
00376 if (p->state == STATE_INPUT_READY && !fctx->die) {
00377 pthread_mutex_lock(&p->mutex);
00378 while (p->state == STATE_INPUT_READY && !fctx->die)
00379 pthread_cond_wait(&p->input_cond, &p->mutex);
00380 pthread_mutex_unlock(&p->mutex);
00381 }
00382
00383 if (fctx->die) break;
00384
00385 if (!codec->update_thread_context && (avctx->thread_safe_callbacks || avctx->get_buffer == avcodec_default_get_buffer))
00386 ff_thread_finish_setup(avctx);
00387
00388 pthread_mutex_lock(&p->mutex);
00389 avcodec_get_frame_defaults(&p->frame);
00390 p->got_frame = 0;
00391 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
00392
00393 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
00394
00395 p->state = STATE_INPUT_READY;
00396
00397 pthread_mutex_lock(&p->progress_mutex);
00398 for (i = 0; i < MAX_BUFFERS; i++)
00399 if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != CODEC_ID_H264)) {
00400 p->progress[i][0] = INT_MAX;
00401 p->progress[i][1] = INT_MAX;
00402 }
00403 pthread_cond_broadcast(&p->progress_cond);
00404 pthread_cond_signal(&p->output_cond);
00405 pthread_mutex_unlock(&p->progress_mutex);
00406
00407 pthread_mutex_unlock(&p->mutex);
00408 }
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->sub_id = src->sub_id;
00426 dst->time_base = src->time_base;
00427 dst->width = src->width;
00428 dst->height = src->height;
00429 dst->pix_fmt = src->pix_fmt;
00430
00431 dst->coded_width = src->coded_width;
00432 dst->coded_height = src->coded_height;
00433
00434 dst->has_b_frames = src->has_b_frames;
00435 dst->idct_algo = src->idct_algo;
00436
00437 dst->bits_per_coded_sample = src->bits_per_coded_sample;
00438 dst->sample_aspect_ratio = src->sample_aspect_ratio;
00439 dst->dtg_active_format = src->dtg_active_format;
00440
00441 dst->profile = src->profile;
00442 dst->level = src->level;
00443
00444 dst->bits_per_raw_sample = src->bits_per_raw_sample;
00445 dst->ticks_per_frame = src->ticks_per_frame;
00446 dst->color_primaries = src->color_primaries;
00447
00448 dst->color_trc = src->color_trc;
00449 dst->colorspace = src->colorspace;
00450 dst->color_range = src->color_range;
00451 dst->chroma_sample_location = src->chroma_sample_location;
00452 }
00453
00454 if (for_user) {
00455 dst->delay = src->thread_count - 1;
00456 dst->coded_frame = src->coded_frame;
00457 } else {
00458 if (dst->codec->update_thread_context)
00459 err = dst->codec->update_thread_context(dst, src);
00460 }
00461
00462 return err;
00463 }
00464
00472 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
00473 {
00474 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
00475 dst->flags = src->flags;
00476
00477 dst->draw_horiz_band= src->draw_horiz_band;
00478 dst->get_buffer = src->get_buffer;
00479 dst->release_buffer = src->release_buffer;
00480
00481 dst->opaque = src->opaque;
00482 dst->dsp_mask = src->dsp_mask;
00483 dst->debug = src->debug;
00484 dst->debug_mv = src->debug_mv;
00485
00486 dst->slice_flags = src->slice_flags;
00487 dst->flags2 = src->flags2;
00488
00489 copy_fields(skip_loop_filter, bidir_refine);
00490
00491 dst->frame_number = src->frame_number;
00492 dst->reordered_opaque = src->reordered_opaque;
00493 dst->thread_safe_callbacks = src->thread_safe_callbacks;
00494
00495 if (src->slice_count && src->slice_offset) {
00496 if (dst->slice_count < src->slice_count) {
00497 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
00498 sizeof(*dst->slice_offset));
00499 if (!tmp) {
00500 av_free(dst->slice_offset);
00501 return AVERROR(ENOMEM);
00502 }
00503 dst->slice_offset = tmp;
00504 }
00505 memcpy(dst->slice_offset, src->slice_offset,
00506 src->slice_count * sizeof(*dst->slice_offset));
00507 }
00508 dst->slice_count = src->slice_count;
00509 return 0;
00510 #undef copy_fields
00511 }
00512
00513 static void free_progress(AVFrame *f)
00514 {
00515 PerThreadContext *p = f->owner->thread_opaque;
00516 int *progress = f->thread_opaque;
00517
00518 p->progress_used[(progress - p->progress[0]) / 2] = 0;
00519 }
00520
00522 static void release_delayed_buffers(PerThreadContext *p)
00523 {
00524 FrameThreadContext *fctx = p->parent;
00525
00526 while (p->num_released_buffers > 0) {
00527 AVFrame *f;
00528
00529 pthread_mutex_lock(&fctx->buffer_mutex);
00530 f = &p->released_buffers[--p->num_released_buffers];
00531 free_progress(f);
00532 f->thread_opaque = NULL;
00533
00534 f->owner->release_buffer(f->owner, f);
00535 pthread_mutex_unlock(&fctx->buffer_mutex);
00536 }
00537 }
00538
00539 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
00540 {
00541 FrameThreadContext *fctx = p->parent;
00542 PerThreadContext *prev_thread = fctx->prev_thread;
00543 AVCodec *codec = p->avctx->codec;
00544 uint8_t *buf = p->avpkt.data;
00545
00546 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
00547
00548 pthread_mutex_lock(&p->mutex);
00549
00550 release_delayed_buffers(p);
00551
00552 if (prev_thread) {
00553 int err;
00554 if (prev_thread->state == STATE_SETTING_UP) {
00555 pthread_mutex_lock(&prev_thread->progress_mutex);
00556 while (prev_thread->state == STATE_SETTING_UP)
00557 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
00558 pthread_mutex_unlock(&prev_thread->progress_mutex);
00559 }
00560
00561 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
00562 if (err) {
00563 pthread_mutex_unlock(&p->mutex);
00564 return err;
00565 }
00566 }
00567
00568 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00569 p->avpkt = *avpkt;
00570 p->avpkt.data = buf;
00571 memcpy(buf, avpkt->data, avpkt->size);
00572 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00573
00574 p->state = STATE_SETTING_UP;
00575 pthread_cond_signal(&p->input_cond);
00576 pthread_mutex_unlock(&p->mutex);
00577
00578
00579
00580
00581
00582
00583
00584 if (!p->avctx->thread_safe_callbacks &&
00585 p->avctx->get_buffer != avcodec_default_get_buffer) {
00586 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
00587 pthread_mutex_lock(&p->progress_mutex);
00588 while (p->state == STATE_SETTING_UP)
00589 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00590
00591 if (p->state == STATE_GET_BUFFER) {
00592 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
00593 p->state = STATE_SETTING_UP;
00594 pthread_cond_signal(&p->progress_cond);
00595 }
00596 pthread_mutex_unlock(&p->progress_mutex);
00597 }
00598 }
00599
00600 fctx->prev_thread = p;
00601 fctx->next_decoding++;
00602
00603 return 0;
00604 }
00605
00606 int ff_thread_decode_frame(AVCodecContext *avctx,
00607 AVFrame *picture, int *got_picture_ptr,
00608 AVPacket *avpkt)
00609 {
00610 FrameThreadContext *fctx = avctx->thread_opaque;
00611 int finished = fctx->next_finished;
00612 PerThreadContext *p;
00613 int err;
00614
00615
00616
00617
00618
00619 p = &fctx->threads[fctx->next_decoding];
00620 err = update_context_from_user(p->avctx, avctx);
00621 if (err) return err;
00622 err = submit_packet(p, avpkt);
00623 if (err) return err;
00624
00625
00626
00627
00628
00629 if (fctx->delaying && avpkt->size) {
00630 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
00631
00632 *got_picture_ptr=0;
00633 return avpkt->size;
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643 do {
00644 p = &fctx->threads[finished++];
00645
00646 if (p->state != STATE_INPUT_READY) {
00647 pthread_mutex_lock(&p->progress_mutex);
00648 while (p->state != STATE_INPUT_READY)
00649 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
00650 pthread_mutex_unlock(&p->progress_mutex);
00651 }
00652
00653 *picture = p->frame;
00654 *got_picture_ptr = p->got_frame;
00655 picture->pkt_dts = p->avpkt.dts;
00656 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
00657 picture->width = avctx->width;
00658 picture->height = avctx->height;
00659 picture->format = avctx->pix_fmt;
00660
00661
00662
00663
00664
00665
00666
00667 p->got_frame = 0;
00668
00669 if (finished >= avctx->thread_count) finished = 0;
00670 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
00671
00672 update_context_from_thread(avctx, p->avctx, 1);
00673
00674 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
00675
00676 fctx->next_finished = finished;
00677
00678
00679 return (p->result >= 0) ? avpkt->size : p->result;
00680 }
00681
00682 void ff_thread_report_progress(AVFrame *f, int n, int field)
00683 {
00684 PerThreadContext *p;
00685 int *progress = f->thread_opaque;
00686
00687 if (!progress || progress[field] >= n) return;
00688
00689 p = f->owner->thread_opaque;
00690
00691 if (f->owner->debug&FF_DEBUG_THREADS)
00692 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
00693
00694 pthread_mutex_lock(&p->progress_mutex);
00695 progress[field] = n;
00696 pthread_cond_broadcast(&p->progress_cond);
00697 pthread_mutex_unlock(&p->progress_mutex);
00698 }
00699
00700 void ff_thread_await_progress(AVFrame *f, int n, int field)
00701 {
00702 PerThreadContext *p;
00703 int *progress = f->thread_opaque;
00704
00705 if (!progress || progress[field] >= n) return;
00706
00707 p = f->owner->thread_opaque;
00708
00709 if (f->owner->debug&FF_DEBUG_THREADS)
00710 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
00711
00712 pthread_mutex_lock(&p->progress_mutex);
00713 while (progress[field] < n)
00714 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00715 pthread_mutex_unlock(&p->progress_mutex);
00716 }
00717
00718 void ff_thread_finish_setup(AVCodecContext *avctx) {
00719 PerThreadContext *p = avctx->thread_opaque;
00720
00721 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
00722
00723 if(p->state == STATE_SETUP_FINISHED){
00724 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
00725 }
00726
00727 pthread_mutex_lock(&p->progress_mutex);
00728 p->state = STATE_SETUP_FINISHED;
00729 pthread_cond_broadcast(&p->progress_cond);
00730 pthread_mutex_unlock(&p->progress_mutex);
00731 }
00732
00734 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
00735 {
00736 int i;
00737
00738 for (i = 0; i < thread_count; i++) {
00739 PerThreadContext *p = &fctx->threads[i];
00740
00741 if (p->state != STATE_INPUT_READY) {
00742 pthread_mutex_lock(&p->progress_mutex);
00743 while (p->state != STATE_INPUT_READY)
00744 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
00745 pthread_mutex_unlock(&p->progress_mutex);
00746 }
00747 p->got_frame = 0;
00748 }
00749 }
00750
00751 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
00752 {
00753 FrameThreadContext *fctx = avctx->thread_opaque;
00754 AVCodec *codec = avctx->codec;
00755 int i;
00756
00757 park_frame_worker_threads(fctx, thread_count);
00758
00759 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
00760 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
00761
00762 fctx->die = 1;
00763
00764 for (i = 0; i < thread_count; i++) {
00765 PerThreadContext *p = &fctx->threads[i];
00766
00767 pthread_mutex_lock(&p->mutex);
00768 pthread_cond_signal(&p->input_cond);
00769 pthread_mutex_unlock(&p->mutex);
00770
00771 if (p->thread_init)
00772 pthread_join(p->thread, NULL);
00773 p->thread_init=0;
00774
00775 if (codec->close)
00776 codec->close(p->avctx);
00777
00778 avctx->codec = NULL;
00779
00780 release_delayed_buffers(p);
00781 }
00782
00783 for (i = 0; i < thread_count; i++) {
00784 PerThreadContext *p = &fctx->threads[i];
00785
00786 avcodec_default_free_buffers(p->avctx);
00787
00788 pthread_mutex_destroy(&p->mutex);
00789 pthread_mutex_destroy(&p->progress_mutex);
00790 pthread_cond_destroy(&p->input_cond);
00791 pthread_cond_destroy(&p->progress_cond);
00792 pthread_cond_destroy(&p->output_cond);
00793 av_freep(&p->avpkt.data);
00794
00795 if (i) {
00796 av_freep(&p->avctx->priv_data);
00797 av_freep(&p->avctx->internal);
00798 av_freep(&p->avctx->slice_offset);
00799 }
00800
00801 av_freep(&p->avctx);
00802 }
00803
00804 av_freep(&fctx->threads);
00805 pthread_mutex_destroy(&fctx->buffer_mutex);
00806 av_freep(&avctx->thread_opaque);
00807 }
00808
00809 static int frame_thread_init(AVCodecContext *avctx)
00810 {
00811 int thread_count = avctx->thread_count;
00812 AVCodec *codec = avctx->codec;
00813 AVCodecContext *src = avctx;
00814 FrameThreadContext *fctx;
00815 int i, err = 0;
00816
00817 if (!thread_count) {
00818 int nb_cpus = get_logical_cpus(avctx);
00819 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
00820 nb_cpus = 1;
00821
00822 if (nb_cpus > 1)
00823 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
00824 else
00825 thread_count = avctx->thread_count = 1;
00826 }
00827
00828 if (thread_count <= 1) {
00829 avctx->active_thread_type = 0;
00830 return 0;
00831 }
00832
00833 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
00834
00835 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
00836 pthread_mutex_init(&fctx->buffer_mutex, NULL);
00837 fctx->delaying = 1;
00838
00839 for (i = 0; i < thread_count; i++) {
00840 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
00841 PerThreadContext *p = &fctx->threads[i];
00842
00843 pthread_mutex_init(&p->mutex, NULL);
00844 pthread_mutex_init(&p->progress_mutex, NULL);
00845 pthread_cond_init(&p->input_cond, NULL);
00846 pthread_cond_init(&p->progress_cond, NULL);
00847 pthread_cond_init(&p->output_cond, NULL);
00848
00849 p->parent = fctx;
00850 p->avctx = copy;
00851
00852 if (!copy) {
00853 err = AVERROR(ENOMEM);
00854 goto error;
00855 }
00856
00857 *copy = *src;
00858 copy->thread_opaque = p;
00859 copy->pkt = &p->avpkt;
00860
00861 if (!i) {
00862 src = copy;
00863
00864 if (codec->init)
00865 err = codec->init(copy);
00866
00867 update_context_from_thread(avctx, copy, 1);
00868 } else {
00869 copy->priv_data = av_malloc(codec->priv_data_size);
00870 if (!copy->priv_data) {
00871 err = AVERROR(ENOMEM);
00872 goto error;
00873 }
00874 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
00875 copy->internal = av_malloc(sizeof(AVCodecInternal));
00876 if (!copy->internal) {
00877 err = AVERROR(ENOMEM);
00878 goto error;
00879 }
00880 *copy->internal = *src->internal;
00881 copy->internal->is_copy = 1;
00882
00883 if (codec->init_thread_copy)
00884 err = codec->init_thread_copy(copy);
00885 }
00886
00887 if (err) goto error;
00888
00889 p->thread_init= !pthread_create(&p->thread, NULL, frame_worker_thread, p);
00890 if(!p->thread_init)
00891 goto error;
00892 }
00893
00894 return 0;
00895
00896 error:
00897 frame_thread_free(avctx, i+1);
00898
00899 return err;
00900 }
00901
00902 void ff_thread_flush(AVCodecContext *avctx)
00903 {
00904 FrameThreadContext *fctx = avctx->thread_opaque;
00905
00906 if (!avctx->thread_opaque) return;
00907
00908 park_frame_worker_threads(fctx, avctx->thread_count);
00909 if (fctx->prev_thread) {
00910 if (fctx->prev_thread != &fctx->threads[0])
00911 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
00912 if (avctx->codec->flush)
00913 avctx->codec->flush(fctx->threads[0].avctx);
00914 }
00915
00916 fctx->next_decoding = fctx->next_finished = 0;
00917 fctx->delaying = 1;
00918 fctx->prev_thread = NULL;
00919 }
00920
00921 static int *allocate_progress(PerThreadContext *p)
00922 {
00923 int i;
00924
00925 for (i = 0; i < MAX_BUFFERS; i++)
00926 if (!p->progress_used[i]) break;
00927
00928 if (i == MAX_BUFFERS) {
00929 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
00930 return NULL;
00931 }
00932
00933 p->progress_used[i] = 1;
00934
00935 return p->progress[i];
00936 }
00937
00938 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
00939 {
00940 PerThreadContext *p = avctx->thread_opaque;
00941 int *progress, err;
00942
00943 f->owner = avctx;
00944
00945 ff_init_buffer_info(avctx, f);
00946
00947 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
00948 f->thread_opaque = NULL;
00949 return avctx->get_buffer(avctx, f);
00950 }
00951
00952 if (p->state != STATE_SETTING_UP &&
00953 (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks &&
00954 avctx->get_buffer != avcodec_default_get_buffer))) {
00955 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
00956 return -1;
00957 }
00958
00959 pthread_mutex_lock(&p->parent->buffer_mutex);
00960 f->thread_opaque = progress = allocate_progress(p);
00961
00962 if (!progress) {
00963 pthread_mutex_unlock(&p->parent->buffer_mutex);
00964 return -1;
00965 }
00966
00967 progress[0] =
00968 progress[1] = -1;
00969
00970 if (avctx->thread_safe_callbacks ||
00971 avctx->get_buffer == avcodec_default_get_buffer) {
00972 err = avctx->get_buffer(avctx, f);
00973 } else {
00974 p->requested_frame = f;
00975 p->state = STATE_GET_BUFFER;
00976 pthread_mutex_lock(&p->progress_mutex);
00977 pthread_cond_broadcast(&p->progress_cond);
00978
00979 while (p->state != STATE_SETTING_UP)
00980 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00981
00982 err = p->result;
00983
00984 pthread_mutex_unlock(&p->progress_mutex);
00985
00986 if (!avctx->codec->update_thread_context)
00987 ff_thread_finish_setup(avctx);
00988 }
00989
00990 pthread_mutex_unlock(&p->parent->buffer_mutex);
00991
00992 return err;
00993 }
00994
00995 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
00996 {
00997 PerThreadContext *p = avctx->thread_opaque;
00998 FrameThreadContext *fctx;
00999
01000 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
01001 avctx->release_buffer(avctx, f);
01002 return;
01003 }
01004
01005 if (p->num_released_buffers >= MAX_BUFFERS) {
01006 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
01007 return;
01008 }
01009
01010 if(avctx->debug & FF_DEBUG_BUFFERS)
01011 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
01012
01013 fctx = p->parent;
01014 pthread_mutex_lock(&fctx->buffer_mutex);
01015 p->released_buffers[p->num_released_buffers++] = *f;
01016 pthread_mutex_unlock(&fctx->buffer_mutex);
01017 memset(f->data, 0, sizeof(f->data));
01018 }
01019
01029 static void validate_thread_parameters(AVCodecContext *avctx)
01030 {
01031 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
01032 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
01033 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
01034 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
01035 if (avctx->thread_count == 1) {
01036 avctx->active_thread_type = 0;
01037 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
01038 avctx->active_thread_type = FF_THREAD_FRAME;
01039 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
01040 avctx->thread_type & FF_THREAD_SLICE) {
01041 avctx->active_thread_type = FF_THREAD_SLICE;
01042 } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) {
01043 avctx->thread_count = 1;
01044 avctx->active_thread_type = 0;
01045 }
01046 }
01047
01048 int ff_thread_init(AVCodecContext *avctx)
01049 {
01050 if (avctx->thread_opaque) {
01051 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
01052 return -1;
01053 }
01054
01055 #if HAVE_W32THREADS
01056 w32thread_init();
01057 #endif
01058
01059 if (avctx->codec) {
01060 validate_thread_parameters(avctx);
01061
01062 if (avctx->active_thread_type&FF_THREAD_SLICE)
01063 return thread_init(avctx);
01064 else if (avctx->active_thread_type&FF_THREAD_FRAME)
01065 return frame_thread_init(avctx);
01066 }
01067
01068 return 0;
01069 }
01070
01071 void ff_thread_free(AVCodecContext *avctx)
01072 {
01073 if (avctx->active_thread_type&FF_THREAD_FRAME)
01074 frame_thread_free(avctx, avctx->thread_count);
01075 else
01076 thread_free(avctx);
01077 }