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 <pthread.h>
00033
00034 #include "avcodec.h"
00035 #include "thread.h"
00036
00037 typedef int (action_func)(AVCodecContext *c, void *arg);
00038 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
00039
00040 typedef struct ThreadContext {
00041 pthread_t *workers;
00042 action_func *func;
00043 action_func2 *func2;
00044 void *args;
00045 int *rets;
00046 int rets_count;
00047 int job_count;
00048 int job_size;
00049
00050 pthread_cond_t last_job_cond;
00051 pthread_cond_t current_job_cond;
00052 pthread_mutex_t current_job_lock;
00053 int current_job;
00054 int done;
00055 } ThreadContext;
00056
00058 #define MAX_BUFFERS (32+1)
00059
00063 typedef struct PerThreadContext {
00064 struct FrameThreadContext *parent;
00065
00066 pthread_t thread;
00067 pthread_cond_t input_cond;
00068 pthread_cond_t progress_cond;
00069 pthread_cond_t output_cond;
00070
00071 pthread_mutex_t mutex;
00072 pthread_mutex_t progress_mutex;
00073
00074 AVCodecContext *avctx;
00075
00076 AVPacket avpkt;
00077 int allocated_buf_size;
00078
00079 AVFrame frame;
00080 int got_frame;
00081 int result;
00082
00083 enum {
00084 STATE_INPUT_READY,
00085 STATE_SETTING_UP,
00086 STATE_GET_BUFFER,
00090 STATE_SETUP_FINISHED
00091 } state;
00092
00097 AVFrame released_buffers[MAX_BUFFERS];
00098 int num_released_buffers;
00099
00103 int progress[MAX_BUFFERS][2];
00104 uint8_t progress_used[MAX_BUFFERS];
00105
00106 AVFrame *requested_frame;
00107 } PerThreadContext;
00108
00112 typedef struct FrameThreadContext {
00113 PerThreadContext *threads;
00114 PerThreadContext *prev_thread;
00115
00116 pthread_mutex_t buffer_mutex;
00117
00118 int next_decoding;
00119 int next_finished;
00120
00121 int delaying;
00126 int die;
00127 } FrameThreadContext;
00128
00129 static void* attribute_align_arg worker(void *v)
00130 {
00131 AVCodecContext *avctx = v;
00132 ThreadContext *c = avctx->thread_opaque;
00133 int our_job = c->job_count;
00134 int thread_count = avctx->thread_count;
00135 int self_id;
00136
00137 pthread_mutex_lock(&c->current_job_lock);
00138 self_id = c->current_job++;
00139 for (;;){
00140 while (our_job >= c->job_count) {
00141 if (c->current_job == thread_count + c->job_count)
00142 pthread_cond_signal(&c->last_job_cond);
00143
00144 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
00145 our_job = self_id;
00146
00147 if (c->done) {
00148 pthread_mutex_unlock(&c->current_job_lock);
00149 return NULL;
00150 }
00151 }
00152 pthread_mutex_unlock(&c->current_job_lock);
00153
00154 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
00155 c->func2(avctx, c->args, our_job, self_id);
00156
00157 pthread_mutex_lock(&c->current_job_lock);
00158 our_job = c->current_job++;
00159 }
00160 }
00161
00162 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
00163 {
00164 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
00165 pthread_mutex_unlock(&c->current_job_lock);
00166 }
00167
00168 static void thread_free(AVCodecContext *avctx)
00169 {
00170 ThreadContext *c = avctx->thread_opaque;
00171 int i;
00172
00173 pthread_mutex_lock(&c->current_job_lock);
00174 c->done = 1;
00175 pthread_cond_broadcast(&c->current_job_cond);
00176 pthread_mutex_unlock(&c->current_job_lock);
00177
00178 for (i=0; i<avctx->thread_count; i++)
00179 pthread_join(c->workers[i], NULL);
00180
00181 pthread_mutex_destroy(&c->current_job_lock);
00182 pthread_cond_destroy(&c->current_job_cond);
00183 pthread_cond_destroy(&c->last_job_cond);
00184 av_free(c->workers);
00185 av_freep(&avctx->thread_opaque);
00186 }
00187
00188 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
00189 {
00190 ThreadContext *c= avctx->thread_opaque;
00191 int dummy_ret;
00192
00193 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
00194 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
00195
00196 if (job_count <= 0)
00197 return 0;
00198
00199 pthread_mutex_lock(&c->current_job_lock);
00200
00201 c->current_job = avctx->thread_count;
00202 c->job_count = job_count;
00203 c->job_size = job_size;
00204 c->args = arg;
00205 c->func = func;
00206 if (ret) {
00207 c->rets = ret;
00208 c->rets_count = job_count;
00209 } else {
00210 c->rets = &dummy_ret;
00211 c->rets_count = 1;
00212 }
00213 pthread_cond_broadcast(&c->current_job_cond);
00214
00215 avcodec_thread_park_workers(c, avctx->thread_count);
00216
00217 return 0;
00218 }
00219
00220 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
00221 {
00222 ThreadContext *c= avctx->thread_opaque;
00223 c->func2 = func2;
00224 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
00225 }
00226
00227 static int thread_init(AVCodecContext *avctx)
00228 {
00229 int i;
00230 ThreadContext *c;
00231 int thread_count = avctx->thread_count;
00232
00233 if (thread_count <= 1)
00234 return 0;
00235
00236 c = av_mallocz(sizeof(ThreadContext));
00237 if (!c)
00238 return -1;
00239
00240 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
00241 if (!c->workers) {
00242 av_free(c);
00243 return -1;
00244 }
00245
00246 avctx->thread_opaque = c;
00247 c->current_job = 0;
00248 c->job_count = 0;
00249 c->job_size = 0;
00250 c->done = 0;
00251 pthread_cond_init(&c->current_job_cond, NULL);
00252 pthread_cond_init(&c->last_job_cond, NULL);
00253 pthread_mutex_init(&c->current_job_lock, NULL);
00254 pthread_mutex_lock(&c->current_job_lock);
00255 for (i=0; i<thread_count; i++) {
00256 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
00257 avctx->thread_count = i;
00258 pthread_mutex_unlock(&c->current_job_lock);
00259 ff_thread_free(avctx);
00260 return -1;
00261 }
00262 }
00263
00264 avcodec_thread_park_workers(c, thread_count);
00265
00266 avctx->execute = avcodec_thread_execute;
00267 avctx->execute2 = avcodec_thread_execute2;
00268 return 0;
00269 }
00270
00278 static attribute_align_arg void *frame_worker_thread(void *arg)
00279 {
00280 PerThreadContext *p = arg;
00281 FrameThreadContext *fctx = p->parent;
00282 AVCodecContext *avctx = p->avctx;
00283 AVCodec *codec = avctx->codec;
00284
00285 while (1) {
00286 if (p->state == STATE_INPUT_READY && !fctx->die) {
00287 pthread_mutex_lock(&p->mutex);
00288 while (p->state == STATE_INPUT_READY && !fctx->die)
00289 pthread_cond_wait(&p->input_cond, &p->mutex);
00290 pthread_mutex_unlock(&p->mutex);
00291 }
00292
00293 if (fctx->die) break;
00294
00295 if (!codec->update_thread_context && avctx->thread_safe_callbacks)
00296 ff_thread_finish_setup(avctx);
00297
00298 pthread_mutex_lock(&p->mutex);
00299 avcodec_get_frame_defaults(&p->frame);
00300 p->got_frame = 0;
00301 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
00302
00303 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
00304
00305 p->state = STATE_INPUT_READY;
00306
00307 pthread_mutex_lock(&p->progress_mutex);
00308 pthread_cond_signal(&p->output_cond);
00309 pthread_mutex_unlock(&p->progress_mutex);
00310
00311 pthread_mutex_unlock(&p->mutex);
00312 }
00313
00314 return NULL;
00315 }
00316
00324 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
00325 {
00326 int err = 0;
00327
00328 if (dst != src) {
00329 dst->sub_id = src->sub_id;
00330 dst->time_base = src->time_base;
00331 dst->width = src->width;
00332 dst->height = src->height;
00333 dst->pix_fmt = src->pix_fmt;
00334
00335 dst->coded_width = src->coded_width;
00336 dst->coded_height = src->coded_height;
00337
00338 dst->has_b_frames = src->has_b_frames;
00339 dst->idct_algo = src->idct_algo;
00340 dst->slice_count = src->slice_count;
00341
00342 dst->bits_per_coded_sample = src->bits_per_coded_sample;
00343 dst->sample_aspect_ratio = src->sample_aspect_ratio;
00344 dst->dtg_active_format = src->dtg_active_format;
00345
00346 dst->profile = src->profile;
00347 dst->level = src->level;
00348
00349 dst->bits_per_raw_sample = src->bits_per_raw_sample;
00350 dst->ticks_per_frame = src->ticks_per_frame;
00351 dst->color_primaries = src->color_primaries;
00352
00353 dst->color_trc = src->color_trc;
00354 dst->colorspace = src->colorspace;
00355 dst->color_range = src->color_range;
00356 dst->chroma_sample_location = src->chroma_sample_location;
00357 }
00358
00359 if (for_user) {
00360 dst->coded_frame = src->coded_frame;
00361 dst->has_b_frames += src->thread_count - 1;
00362 } else {
00363 if (dst->codec->update_thread_context)
00364 err = dst->codec->update_thread_context(dst, src);
00365 }
00366
00367 return err;
00368 }
00369
00376 static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
00377 {
00378 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
00379 dst->flags = src->flags;
00380
00381 dst->draw_horiz_band= src->draw_horiz_band;
00382 dst->get_buffer = src->get_buffer;
00383 dst->release_buffer = src->release_buffer;
00384
00385 dst->opaque = src->opaque;
00386 dst->dsp_mask = src->dsp_mask;
00387 dst->debug = src->debug;
00388 dst->debug_mv = src->debug_mv;
00389
00390 dst->slice_flags = src->slice_flags;
00391 dst->flags2 = src->flags2;
00392
00393 copy_fields(skip_loop_filter, bidir_refine);
00394
00395 dst->frame_number = src->frame_number;
00396 dst->reordered_opaque = src->reordered_opaque;
00397 #undef copy_fields
00398 }
00399
00400 static void free_progress(AVFrame *f)
00401 {
00402 PerThreadContext *p = f->owner->thread_opaque;
00403 int *progress = f->thread_opaque;
00404
00405 p->progress_used[(progress - p->progress[0]) / 2] = 0;
00406 }
00407
00409 static void release_delayed_buffers(PerThreadContext *p)
00410 {
00411 FrameThreadContext *fctx = p->parent;
00412
00413 while (p->num_released_buffers > 0) {
00414 AVFrame *f;
00415
00416 pthread_mutex_lock(&fctx->buffer_mutex);
00417 f = &p->released_buffers[--p->num_released_buffers];
00418 free_progress(f);
00419 f->thread_opaque = NULL;
00420
00421 f->owner->release_buffer(f->owner, f);
00422 pthread_mutex_unlock(&fctx->buffer_mutex);
00423 }
00424 }
00425
00426 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
00427 {
00428 FrameThreadContext *fctx = p->parent;
00429 PerThreadContext *prev_thread = fctx->prev_thread;
00430 AVCodec *codec = p->avctx->codec;
00431 uint8_t *buf = p->avpkt.data;
00432
00433 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
00434
00435 pthread_mutex_lock(&p->mutex);
00436
00437 release_delayed_buffers(p);
00438
00439 if (prev_thread) {
00440 int err;
00441 if (prev_thread->state == STATE_SETTING_UP) {
00442 pthread_mutex_lock(&prev_thread->progress_mutex);
00443 while (prev_thread->state == STATE_SETTING_UP)
00444 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
00445 pthread_mutex_unlock(&prev_thread->progress_mutex);
00446 }
00447
00448 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
00449 if (err) {
00450 pthread_mutex_unlock(&p->mutex);
00451 return err;
00452 }
00453 }
00454
00455 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00456 p->avpkt = *avpkt;
00457 p->avpkt.data = buf;
00458 memcpy(buf, avpkt->data, avpkt->size);
00459 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00460
00461 p->state = STATE_SETTING_UP;
00462 pthread_cond_signal(&p->input_cond);
00463 pthread_mutex_unlock(&p->mutex);
00464
00465
00466
00467
00468
00469
00470
00471 if (!p->avctx->thread_safe_callbacks &&
00472 p->avctx->get_buffer != avcodec_default_get_buffer) {
00473 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
00474 pthread_mutex_lock(&p->progress_mutex);
00475 while (p->state == STATE_SETTING_UP)
00476 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00477
00478 if (p->state == STATE_GET_BUFFER) {
00479 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
00480 p->state = STATE_SETTING_UP;
00481 pthread_cond_signal(&p->progress_cond);
00482 }
00483 pthread_mutex_unlock(&p->progress_mutex);
00484 }
00485 }
00486
00487 fctx->prev_thread = p;
00488
00489 return 0;
00490 }
00491
00492 int ff_thread_decode_frame(AVCodecContext *avctx,
00493 AVFrame *picture, int *got_picture_ptr,
00494 AVPacket *avpkt)
00495 {
00496 FrameThreadContext *fctx = avctx->thread_opaque;
00497 int finished = fctx->next_finished;
00498 PerThreadContext *p;
00499 int err;
00500
00501
00502
00503
00504
00505 p = &fctx->threads[fctx->next_decoding];
00506 update_context_from_user(p->avctx, avctx);
00507 err = submit_packet(p, avpkt);
00508 if (err) return err;
00509
00510 fctx->next_decoding++;
00511
00512
00513
00514
00515
00516 if (fctx->delaying && avpkt->size) {
00517 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
00518
00519 *got_picture_ptr=0;
00520 return 0;
00521 }
00522
00523
00524
00525
00526
00527
00528
00529
00530 do {
00531 p = &fctx->threads[finished++];
00532
00533 if (p->state != STATE_INPUT_READY) {
00534 pthread_mutex_lock(&p->progress_mutex);
00535 while (p->state != STATE_INPUT_READY)
00536 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
00537 pthread_mutex_unlock(&p->progress_mutex);
00538 }
00539
00540 *picture = p->frame;
00541 *got_picture_ptr = p->got_frame;
00542 picture->pkt_dts = p->avpkt.dts;
00543
00544
00545
00546
00547
00548
00549
00550 p->got_frame = 0;
00551
00552 if (finished >= avctx->thread_count) finished = 0;
00553 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
00554
00555 update_context_from_thread(avctx, p->avctx, 1);
00556
00557 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
00558
00559 fctx->next_finished = finished;
00560
00561 return p->result;
00562 }
00563
00564 void ff_thread_report_progress(AVFrame *f, int n, int field)
00565 {
00566 PerThreadContext *p;
00567 int *progress = f->thread_opaque;
00568
00569 if (!progress || progress[field] >= n) return;
00570
00571 p = f->owner->thread_opaque;
00572
00573 if (f->owner->debug&FF_DEBUG_THREADS)
00574 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
00575
00576 pthread_mutex_lock(&p->progress_mutex);
00577 progress[field] = n;
00578 pthread_cond_broadcast(&p->progress_cond);
00579 pthread_mutex_unlock(&p->progress_mutex);
00580 }
00581
00582 void ff_thread_await_progress(AVFrame *f, int n, int field)
00583 {
00584 PerThreadContext *p;
00585 int *progress = f->thread_opaque;
00586
00587 if (!progress || progress[field] >= n) return;
00588
00589 p = f->owner->thread_opaque;
00590
00591 if (f->owner->debug&FF_DEBUG_THREADS)
00592 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
00593
00594 pthread_mutex_lock(&p->progress_mutex);
00595 while (progress[field] < n)
00596 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00597 pthread_mutex_unlock(&p->progress_mutex);
00598 }
00599
00600 void ff_thread_finish_setup(AVCodecContext *avctx) {
00601 PerThreadContext *p = avctx->thread_opaque;
00602
00603 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
00604
00605 pthread_mutex_lock(&p->progress_mutex);
00606 p->state = STATE_SETUP_FINISHED;
00607 pthread_cond_broadcast(&p->progress_cond);
00608 pthread_mutex_unlock(&p->progress_mutex);
00609 }
00610
00612 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
00613 {
00614 int i;
00615
00616 for (i = 0; i < thread_count; i++) {
00617 PerThreadContext *p = &fctx->threads[i];
00618
00619 if (p->state != STATE_INPUT_READY) {
00620 pthread_mutex_lock(&p->progress_mutex);
00621 while (p->state != STATE_INPUT_READY)
00622 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
00623 pthread_mutex_unlock(&p->progress_mutex);
00624 }
00625 }
00626 }
00627
00628 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
00629 {
00630 FrameThreadContext *fctx = avctx->thread_opaque;
00631 AVCodec *codec = avctx->codec;
00632 int i;
00633
00634 park_frame_worker_threads(fctx, thread_count);
00635
00636 if (fctx->prev_thread)
00637 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
00638
00639 fctx->die = 1;
00640
00641 for (i = 0; i < thread_count; i++) {
00642 PerThreadContext *p = &fctx->threads[i];
00643
00644 pthread_mutex_lock(&p->mutex);
00645 pthread_cond_signal(&p->input_cond);
00646 pthread_mutex_unlock(&p->mutex);
00647
00648 pthread_join(p->thread, NULL);
00649
00650 if (codec->close)
00651 codec->close(p->avctx);
00652
00653 avctx->codec = NULL;
00654
00655 release_delayed_buffers(p);
00656 }
00657
00658 for (i = 0; i < thread_count; i++) {
00659 PerThreadContext *p = &fctx->threads[i];
00660
00661 avcodec_default_free_buffers(p->avctx);
00662
00663 pthread_mutex_destroy(&p->mutex);
00664 pthread_mutex_destroy(&p->progress_mutex);
00665 pthread_cond_destroy(&p->input_cond);
00666 pthread_cond_destroy(&p->progress_cond);
00667 pthread_cond_destroy(&p->output_cond);
00668 av_freep(&p->avpkt.data);
00669
00670 if (i)
00671 av_freep(&p->avctx->priv_data);
00672
00673 av_freep(&p->avctx);
00674 }
00675
00676 av_freep(&fctx->threads);
00677 pthread_mutex_destroy(&fctx->buffer_mutex);
00678 av_freep(&avctx->thread_opaque);
00679 }
00680
00681 static int frame_thread_init(AVCodecContext *avctx)
00682 {
00683 int thread_count = avctx->thread_count;
00684 AVCodec *codec = avctx->codec;
00685 AVCodecContext *src = avctx;
00686 FrameThreadContext *fctx;
00687 int i, err = 0;
00688
00689 if (thread_count <= 1) {
00690 avctx->active_thread_type = 0;
00691 return 0;
00692 }
00693
00694 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
00695
00696 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
00697 pthread_mutex_init(&fctx->buffer_mutex, NULL);
00698 fctx->delaying = 1;
00699
00700 for (i = 0; i < thread_count; i++) {
00701 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
00702 PerThreadContext *p = &fctx->threads[i];
00703
00704 pthread_mutex_init(&p->mutex, NULL);
00705 pthread_mutex_init(&p->progress_mutex, NULL);
00706 pthread_cond_init(&p->input_cond, NULL);
00707 pthread_cond_init(&p->progress_cond, NULL);
00708 pthread_cond_init(&p->output_cond, NULL);
00709
00710 p->parent = fctx;
00711 p->avctx = copy;
00712
00713 *copy = *src;
00714 copy->thread_opaque = p;
00715 copy->pkt = &p->avpkt;
00716
00717 if (!i) {
00718 src = copy;
00719
00720 if (codec->init)
00721 err = codec->init(copy);
00722
00723 update_context_from_thread(avctx, copy, 1);
00724 } else {
00725 copy->is_copy = 1;
00726 copy->priv_data = av_malloc(codec->priv_data_size);
00727 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
00728
00729 if (codec->init_thread_copy)
00730 err = codec->init_thread_copy(copy);
00731 }
00732
00733 if (err) goto error;
00734
00735 pthread_create(&p->thread, NULL, frame_worker_thread, p);
00736 }
00737
00738 return 0;
00739
00740 error:
00741 frame_thread_free(avctx, i+1);
00742
00743 return err;
00744 }
00745
00746 void ff_thread_flush(AVCodecContext *avctx)
00747 {
00748 FrameThreadContext *fctx = avctx->thread_opaque;
00749
00750 if (!avctx->thread_opaque) return;
00751
00752 park_frame_worker_threads(fctx, avctx->thread_count);
00753 if (fctx->prev_thread) {
00754 if (fctx->prev_thread != &fctx->threads[0])
00755 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
00756 if (avctx->codec->flush)
00757 avctx->codec->flush(fctx->threads[0].avctx);
00758 }
00759
00760 fctx->next_decoding = fctx->next_finished = 0;
00761 fctx->delaying = 1;
00762 fctx->prev_thread = NULL;
00763 }
00764
00765 static int *allocate_progress(PerThreadContext *p)
00766 {
00767 int i;
00768
00769 for (i = 0; i < MAX_BUFFERS; i++)
00770 if (!p->progress_used[i]) break;
00771
00772 if (i == MAX_BUFFERS) {
00773 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
00774 return NULL;
00775 }
00776
00777 p->progress_used[i] = 1;
00778
00779 return p->progress[i];
00780 }
00781
00782 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
00783 {
00784 PerThreadContext *p = avctx->thread_opaque;
00785 int *progress, err;
00786
00787 f->owner = avctx;
00788
00789 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
00790 f->thread_opaque = NULL;
00791 return avctx->get_buffer(avctx, f);
00792 }
00793
00794 if (p->state != STATE_SETTING_UP &&
00795 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
00796 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
00797 return -1;
00798 }
00799
00800 pthread_mutex_lock(&p->parent->buffer_mutex);
00801 f->thread_opaque = progress = allocate_progress(p);
00802
00803 if (!progress) {
00804 pthread_mutex_unlock(&p->parent->buffer_mutex);
00805 return -1;
00806 }
00807
00808 progress[0] =
00809 progress[1] = -1;
00810
00811 if (avctx->thread_safe_callbacks ||
00812 avctx->get_buffer == avcodec_default_get_buffer) {
00813 err = avctx->get_buffer(avctx, f);
00814 } else {
00815 p->requested_frame = f;
00816 p->state = STATE_GET_BUFFER;
00817 pthread_mutex_lock(&p->progress_mutex);
00818 pthread_cond_signal(&p->progress_cond);
00819
00820 while (p->state != STATE_SETTING_UP)
00821 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
00822
00823 err = p->result;
00824
00825 pthread_mutex_unlock(&p->progress_mutex);
00826
00827 if (!avctx->codec->update_thread_context)
00828 ff_thread_finish_setup(avctx);
00829 }
00830
00831 pthread_mutex_unlock(&p->parent->buffer_mutex);
00832
00833
00834
00835
00836
00837
00838 f->age = INT_MAX;
00839
00840 return err;
00841 }
00842
00843 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
00844 {
00845 PerThreadContext *p = avctx->thread_opaque;
00846 FrameThreadContext *fctx;
00847
00848 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
00849 avctx->release_buffer(avctx, f);
00850 return;
00851 }
00852
00853 if (p->num_released_buffers >= MAX_BUFFERS) {
00854 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
00855 return;
00856 }
00857
00858 if(avctx->debug & FF_DEBUG_BUFFERS)
00859 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
00860 f, f->owner->internal_buffer_count);
00861
00862 fctx = p->parent;
00863 pthread_mutex_lock(&fctx->buffer_mutex);
00864 p->released_buffers[p->num_released_buffers++] = *f;
00865 pthread_mutex_unlock(&fctx->buffer_mutex);
00866 memset(f->data, 0, sizeof(f->data));
00867 }
00868
00878 static void validate_thread_parameters(AVCodecContext *avctx)
00879 {
00880 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
00881 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
00882 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
00883 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
00884 if (avctx->thread_count == 1) {
00885 avctx->active_thread_type = 0;
00886 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
00887 avctx->active_thread_type = FF_THREAD_FRAME;
00888 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
00889 avctx->thread_type & FF_THREAD_SLICE) {
00890 avctx->active_thread_type = FF_THREAD_SLICE;
00891 }
00892 }
00893
00894 int ff_thread_init(AVCodecContext *avctx)
00895 {
00896 if (avctx->thread_opaque) {
00897 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
00898 return -1;
00899 }
00900
00901 if (avctx->codec) {
00902 validate_thread_parameters(avctx);
00903
00904 if (avctx->active_thread_type&FF_THREAD_SLICE)
00905 return thread_init(avctx);
00906 else if (avctx->active_thread_type&FF_THREAD_FRAME)
00907 return frame_thread_init(avctx);
00908 }
00909
00910 return 0;
00911 }
00912
00913 void ff_thread_free(AVCodecContext *avctx)
00914 {
00915 if (avctx->active_thread_type&FF_THREAD_FRAME)
00916 frame_thread_free(avctx, avctx->thread_count);
00917 else
00918 thread_free(avctx);
00919 }