00001 /* 00002 * Copyright (c) 2004 Roman Shaposhnik 00003 * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com) 00004 * 00005 * Many thanks to Steven M. Schultz for providing clever ideas and 00006 * to Michael Niedermayer <michaelni@gmx.at> for writing initial 00007 * implementation. 00008 * 00009 * This file is part of FFmpeg. 00010 * 00011 * FFmpeg is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU Lesser General Public 00013 * License as published by the Free Software Foundation; either 00014 * version 2.1 of the License, or (at your option) any later version. 00015 * 00016 * FFmpeg is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with FFmpeg; if not, write to the Free Software 00023 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00024 */ 00025 00032 #include "config.h" 00033 #include "avcodec.h" 00034 #include "internal.h" 00035 #include "thread.h" 00036 00037 #if HAVE_PTHREADS 00038 #include <pthread.h> 00039 #elif HAVE_W32THREADS 00040 #include "w32pthreads.h" 00041 #elif HAVE_OS2THREADS 00042 #include "os2threads.h" 00043 #endif 00044 00045 typedef int (action_func)(AVCodecContext *c, void *arg); 00046 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr); 00047 00048 typedef struct ThreadContext { 00049 pthread_t *workers; 00050 action_func *func; 00051 action_func2 *func2; 00052 void *args; 00053 int *rets; 00054 int rets_count; 00055 int job_count; 00056 int job_size; 00057 00058 pthread_cond_t last_job_cond; 00059 pthread_cond_t current_job_cond; 00060 pthread_mutex_t current_job_lock; 00061 int current_job; 00062 int done; 00063 } ThreadContext; 00064 00066 #define MAX_BUFFERS (32+1) 00067 00071 typedef struct PerThreadContext { 00072 struct FrameThreadContext *parent; 00073 00074 pthread_t thread; 00075 int thread_init; 00076 pthread_cond_t input_cond; 00077 pthread_cond_t progress_cond; 00078 pthread_cond_t output_cond; 00079 00080 pthread_mutex_t mutex; 00081 pthread_mutex_t progress_mutex; 00082 00083 AVCodecContext *avctx; 00084 00085 AVPacket avpkt; 00086 int allocated_buf_size; 00087 00088 AVFrame frame; 00089 int got_frame; 00090 int result; 00091 00092 enum { 00093 STATE_INPUT_READY, 00094 STATE_SETTING_UP, 00095 STATE_GET_BUFFER, 00099 STATE_SETUP_FINISHED 00100 } state; 00101 00106 AVFrame released_buffers[MAX_BUFFERS]; 00107 int num_released_buffers; 00108 00112 int progress[MAX_BUFFERS][2]; 00113 uint8_t progress_used[MAX_BUFFERS]; 00114 00115 AVFrame *requested_frame; 00116 } PerThreadContext; 00117 00121 typedef struct FrameThreadContext { 00122 PerThreadContext *threads; 00123 PerThreadContext *prev_thread; 00124 00125 pthread_mutex_t buffer_mutex; 00126 00127 int next_decoding; 00128 int next_finished; 00129 00130 int delaying; 00135 int die; 00136 } FrameThreadContext; 00137 00138 static void* attribute_align_arg worker(void *v) 00139 { 00140 AVCodecContext *avctx = v; 00141 ThreadContext *c = avctx->thread_opaque; 00142 int our_job = c->job_count; 00143 int thread_count = avctx->thread_count; 00144 int self_id; 00145 00146 pthread_mutex_lock(&c->current_job_lock); 00147 self_id = c->current_job++; 00148 for (;;){ 00149 while (our_job >= c->job_count) { 00150 if (c->current_job == thread_count + c->job_count) 00151 pthread_cond_signal(&c->last_job_cond); 00152 00153 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock); 00154 our_job = self_id; 00155 00156 if (c->done) { 00157 pthread_mutex_unlock(&c->current_job_lock); 00158 return NULL; 00159 } 00160 } 00161 pthread_mutex_unlock(&c->current_job_lock); 00162 00163 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size): 00164 c->func2(avctx, c->args, our_job, self_id); 00165 00166 pthread_mutex_lock(&c->current_job_lock); 00167 our_job = c->current_job++; 00168 } 00169 } 00170 00171 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count) 00172 { 00173 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock); 00174 pthread_mutex_unlock(&c->current_job_lock); 00175 } 00176 00177 static void thread_free(AVCodecContext *avctx) 00178 { 00179 ThreadContext *c = avctx->thread_opaque; 00180 int i; 00181 00182 pthread_mutex_lock(&c->current_job_lock); 00183 c->done = 1; 00184 pthread_cond_broadcast(&c->current_job_cond); 00185 pthread_mutex_unlock(&c->current_job_lock); 00186 00187 for (i=0; i<avctx->thread_count; i++) 00188 pthread_join(c->workers[i], NULL); 00189 00190 pthread_mutex_destroy(&c->current_job_lock); 00191 pthread_cond_destroy(&c->current_job_cond); 00192 pthread_cond_destroy(&c->last_job_cond); 00193 av_free(c->workers); 00194 av_freep(&avctx->thread_opaque); 00195 } 00196 00197 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size) 00198 { 00199 ThreadContext *c= avctx->thread_opaque; 00200 int dummy_ret; 00201 00202 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1) 00203 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size); 00204 00205 if (job_count <= 0) 00206 return 0; 00207 00208 pthread_mutex_lock(&c->current_job_lock); 00209 00210 c->current_job = avctx->thread_count; 00211 c->job_count = job_count; 00212 c->job_size = job_size; 00213 c->args = arg; 00214 c->func = func; 00215 if (ret) { 00216 c->rets = ret; 00217 c->rets_count = job_count; 00218 } else { 00219 c->rets = &dummy_ret; 00220 c->rets_count = 1; 00221 } 00222 pthread_cond_broadcast(&c->current_job_cond); 00223 00224 avcodec_thread_park_workers(c, avctx->thread_count); 00225 00226 return 0; 00227 } 00228 00229 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count) 00230 { 00231 ThreadContext *c= avctx->thread_opaque; 00232 c->func2 = func2; 00233 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0); 00234 } 00235 00236 static int thread_init(AVCodecContext *avctx) 00237 { 00238 int i; 00239 ThreadContext *c; 00240 int thread_count = avctx->thread_count; 00241 00242 if (thread_count <= 1) { 00243 avctx->active_thread_type = 0; 00244 return 0; 00245 } 00246 00247 c = av_mallocz(sizeof(ThreadContext)); 00248 if (!c) 00249 return -1; 00250 00251 c->workers = av_mallocz(sizeof(pthread_t)*thread_count); 00252 if (!c->workers) { 00253 av_free(c); 00254 return -1; 00255 } 00256 00257 avctx->thread_opaque = c; 00258 c->current_job = 0; 00259 c->job_count = 0; 00260 c->job_size = 0; 00261 c->done = 0; 00262 pthread_cond_init(&c->current_job_cond, NULL); 00263 pthread_cond_init(&c->last_job_cond, NULL); 00264 pthread_mutex_init(&c->current_job_lock, NULL); 00265 pthread_mutex_lock(&c->current_job_lock); 00266 for (i=0; i<thread_count; i++) { 00267 if(pthread_create(&c->workers[i], NULL, worker, avctx)) { 00268 avctx->thread_count = i; 00269 pthread_mutex_unlock(&c->current_job_lock); 00270 ff_thread_free(avctx); 00271 return -1; 00272 } 00273 } 00274 00275 avcodec_thread_park_workers(c, thread_count); 00276 00277 avctx->execute = avcodec_thread_execute; 00278 avctx->execute2 = avcodec_thread_execute2; 00279 return 0; 00280 } 00281 00289 static attribute_align_arg void *frame_worker_thread(void *arg) 00290 { 00291 PerThreadContext *p = arg; 00292 FrameThreadContext *fctx = p->parent; 00293 AVCodecContext *avctx = p->avctx; 00294 AVCodec *codec = avctx->codec; 00295 00296 while (1) { 00297 if (p->state == STATE_INPUT_READY && !fctx->die) { 00298 pthread_mutex_lock(&p->mutex); 00299 while (p->state == STATE_INPUT_READY && !fctx->die) 00300 pthread_cond_wait(&p->input_cond, &p->mutex); 00301 pthread_mutex_unlock(&p->mutex); 00302 } 00303 00304 if (fctx->die) break; 00305 00306 if (!codec->update_thread_context && (avctx->thread_safe_callbacks || avctx->get_buffer == avcodec_default_get_buffer)) 00307 ff_thread_finish_setup(avctx); 00308 00309 pthread_mutex_lock(&p->mutex); 00310 avcodec_get_frame_defaults(&p->frame); 00311 p->got_frame = 0; 00312 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt); 00313 00314 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx); 00315 00316 p->state = STATE_INPUT_READY; 00317 00318 pthread_mutex_lock(&p->progress_mutex); 00319 pthread_cond_signal(&p->output_cond); 00320 pthread_mutex_unlock(&p->progress_mutex); 00321 00322 pthread_mutex_unlock(&p->mutex); 00323 } 00324 00325 return NULL; 00326 } 00327 00335 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user) 00336 { 00337 int err = 0; 00338 00339 if (dst != src) { 00340 dst->sub_id = src->sub_id; 00341 dst->time_base = src->time_base; 00342 dst->width = src->width; 00343 dst->height = src->height; 00344 dst->pix_fmt = src->pix_fmt; 00345 00346 dst->coded_width = src->coded_width; 00347 dst->coded_height = src->coded_height; 00348 00349 dst->has_b_frames = src->has_b_frames; 00350 dst->idct_algo = src->idct_algo; 00351 dst->slice_count = src->slice_count; 00352 00353 dst->bits_per_coded_sample = src->bits_per_coded_sample; 00354 dst->sample_aspect_ratio = src->sample_aspect_ratio; 00355 dst->dtg_active_format = src->dtg_active_format; 00356 00357 dst->profile = src->profile; 00358 dst->level = src->level; 00359 00360 dst->bits_per_raw_sample = src->bits_per_raw_sample; 00361 dst->ticks_per_frame = src->ticks_per_frame; 00362 dst->color_primaries = src->color_primaries; 00363 00364 dst->color_trc = src->color_trc; 00365 dst->colorspace = src->colorspace; 00366 dst->color_range = src->color_range; 00367 dst->chroma_sample_location = src->chroma_sample_location; 00368 } 00369 00370 if (for_user) { 00371 dst->delay = src->thread_count - 1; 00372 dst->coded_frame = src->coded_frame; 00373 } else { 00374 if (dst->codec->update_thread_context) 00375 err = dst->codec->update_thread_context(dst, src); 00376 } 00377 00378 return err; 00379 } 00380 00387 static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src) 00388 { 00389 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s); 00390 dst->flags = src->flags; 00391 00392 dst->draw_horiz_band= src->draw_horiz_band; 00393 dst->get_buffer = src->get_buffer; 00394 dst->release_buffer = src->release_buffer; 00395 00396 dst->opaque = src->opaque; 00397 dst->dsp_mask = src->dsp_mask; 00398 dst->debug = src->debug; 00399 dst->debug_mv = src->debug_mv; 00400 00401 dst->slice_flags = src->slice_flags; 00402 dst->flags2 = src->flags2; 00403 00404 copy_fields(skip_loop_filter, bidir_refine); 00405 00406 dst->frame_number = src->frame_number; 00407 dst->reordered_opaque = src->reordered_opaque; 00408 dst->thread_safe_callbacks = src->thread_safe_callbacks; 00409 #undef copy_fields 00410 } 00411 00412 static void free_progress(AVFrame *f) 00413 { 00414 PerThreadContext *p = f->owner->thread_opaque; 00415 int *progress = f->thread_opaque; 00416 00417 p->progress_used[(progress - p->progress[0]) / 2] = 0; 00418 } 00419 00421 static void release_delayed_buffers(PerThreadContext *p) 00422 { 00423 FrameThreadContext *fctx = p->parent; 00424 00425 while (p->num_released_buffers > 0) { 00426 AVFrame *f; 00427 00428 pthread_mutex_lock(&fctx->buffer_mutex); 00429 f = &p->released_buffers[--p->num_released_buffers]; 00430 free_progress(f); 00431 f->thread_opaque = NULL; 00432 00433 f->owner->release_buffer(f->owner, f); 00434 pthread_mutex_unlock(&fctx->buffer_mutex); 00435 } 00436 } 00437 00438 static int submit_packet(PerThreadContext *p, AVPacket *avpkt) 00439 { 00440 FrameThreadContext *fctx = p->parent; 00441 PerThreadContext *prev_thread = fctx->prev_thread; 00442 AVCodec *codec = p->avctx->codec; 00443 uint8_t *buf = p->avpkt.data; 00444 00445 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0; 00446 00447 pthread_mutex_lock(&p->mutex); 00448 00449 release_delayed_buffers(p); 00450 00451 if (prev_thread) { 00452 int err; 00453 if (prev_thread->state == STATE_SETTING_UP) { 00454 pthread_mutex_lock(&prev_thread->progress_mutex); 00455 while (prev_thread->state == STATE_SETTING_UP) 00456 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex); 00457 pthread_mutex_unlock(&prev_thread->progress_mutex); 00458 } 00459 00460 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0); 00461 if (err) { 00462 pthread_mutex_unlock(&p->mutex); 00463 return err; 00464 } 00465 } 00466 00467 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE); 00468 p->avpkt = *avpkt; 00469 p->avpkt.data = buf; 00470 memcpy(buf, avpkt->data, avpkt->size); 00471 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 00472 00473 p->state = STATE_SETTING_UP; 00474 pthread_cond_signal(&p->input_cond); 00475 pthread_mutex_unlock(&p->mutex); 00476 00477 /* 00478 * If the client doesn't have a thread-safe get_buffer(), 00479 * then decoding threads call back to the main thread, 00480 * and it calls back to the client here. 00481 */ 00482 00483 if (!p->avctx->thread_safe_callbacks && 00484 p->avctx->get_buffer != avcodec_default_get_buffer) { 00485 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) { 00486 pthread_mutex_lock(&p->progress_mutex); 00487 while (p->state == STATE_SETTING_UP) 00488 pthread_cond_wait(&p->progress_cond, &p->progress_mutex); 00489 00490 if (p->state == STATE_GET_BUFFER) { 00491 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame); 00492 p->state = STATE_SETTING_UP; 00493 pthread_cond_signal(&p->progress_cond); 00494 } 00495 pthread_mutex_unlock(&p->progress_mutex); 00496 } 00497 } 00498 00499 fctx->prev_thread = p; 00500 fctx->next_decoding++; 00501 00502 return 0; 00503 } 00504 00505 int ff_thread_decode_frame(AVCodecContext *avctx, 00506 AVFrame *picture, int *got_picture_ptr, 00507 AVPacket *avpkt) 00508 { 00509 FrameThreadContext *fctx = avctx->thread_opaque; 00510 int finished = fctx->next_finished; 00511 PerThreadContext *p; 00512 int err; 00513 00514 /* 00515 * Submit a packet to the next decoding thread. 00516 */ 00517 00518 p = &fctx->threads[fctx->next_decoding]; 00519 update_context_from_user(p->avctx, avctx); 00520 err = submit_packet(p, avpkt); 00521 if (err) return err; 00522 00523 /* 00524 * If we're still receiving the initial packets, don't return a frame. 00525 */ 00526 00527 if (fctx->delaying && avpkt->size) { 00528 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0; 00529 00530 *got_picture_ptr=0; 00531 return avpkt->size; 00532 } 00533 00534 /* 00535 * Return the next available frame from the oldest thread. 00536 * If we're at the end of the stream, then we have to skip threads that 00537 * didn't output a frame, because we don't want to accidentally signal 00538 * EOF (avpkt->size == 0 && *got_picture_ptr == 0). 00539 */ 00540 00541 do { 00542 p = &fctx->threads[finished++]; 00543 00544 if (p->state != STATE_INPUT_READY) { 00545 pthread_mutex_lock(&p->progress_mutex); 00546 while (p->state != STATE_INPUT_READY) 00547 pthread_cond_wait(&p->output_cond, &p->progress_mutex); 00548 pthread_mutex_unlock(&p->progress_mutex); 00549 } 00550 00551 *picture = p->frame; 00552 *got_picture_ptr = p->got_frame; 00553 picture->pkt_dts = p->avpkt.dts; 00554 00555 /* 00556 * A later call with avkpt->size == 0 may loop over all threads, 00557 * including this one, searching for a frame to return before being 00558 * stopped by the "finished != fctx->next_finished" condition. 00559 * Make sure we don't mistakenly return the same frame again. 00560 */ 00561 p->got_frame = 0; 00562 00563 if (finished >= avctx->thread_count) finished = 0; 00564 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished); 00565 00566 update_context_from_thread(avctx, p->avctx, 1); 00567 00568 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0; 00569 00570 fctx->next_finished = finished; 00571 00572 /* return the size of the consumed packet if no error occurred */ 00573 return (p->result >= 0) ? avpkt->size : p->result; 00574 } 00575 00576 void ff_thread_report_progress(AVFrame *f, int n, int field) 00577 { 00578 PerThreadContext *p; 00579 int *progress = f->thread_opaque; 00580 00581 if (!progress || progress[field] >= n) return; 00582 00583 p = f->owner->thread_opaque; 00584 00585 if (f->owner->debug&FF_DEBUG_THREADS) 00586 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field); 00587 00588 pthread_mutex_lock(&p->progress_mutex); 00589 progress[field] = n; 00590 pthread_cond_broadcast(&p->progress_cond); 00591 pthread_mutex_unlock(&p->progress_mutex); 00592 } 00593 00594 void ff_thread_await_progress(AVFrame *f, int n, int field) 00595 { 00596 PerThreadContext *p; 00597 int *progress = f->thread_opaque; 00598 00599 if (!progress || progress[field] >= n) return; 00600 00601 p = f->owner->thread_opaque; 00602 00603 if (f->owner->debug&FF_DEBUG_THREADS) 00604 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress); 00605 00606 pthread_mutex_lock(&p->progress_mutex); 00607 while (progress[field] < n) 00608 pthread_cond_wait(&p->progress_cond, &p->progress_mutex); 00609 pthread_mutex_unlock(&p->progress_mutex); 00610 } 00611 00612 void ff_thread_finish_setup(AVCodecContext *avctx) { 00613 PerThreadContext *p = avctx->thread_opaque; 00614 00615 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return; 00616 00617 if(p->state == STATE_SETUP_FINISHED){ 00618 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n"); 00619 } 00620 00621 pthread_mutex_lock(&p->progress_mutex); 00622 p->state = STATE_SETUP_FINISHED; 00623 pthread_cond_broadcast(&p->progress_cond); 00624 pthread_mutex_unlock(&p->progress_mutex); 00625 } 00626 00628 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count) 00629 { 00630 int i; 00631 00632 for (i = 0; i < thread_count; i++) { 00633 PerThreadContext *p = &fctx->threads[i]; 00634 00635 if (p->state != STATE_INPUT_READY) { 00636 pthread_mutex_lock(&p->progress_mutex); 00637 while (p->state != STATE_INPUT_READY) 00638 pthread_cond_wait(&p->output_cond, &p->progress_mutex); 00639 pthread_mutex_unlock(&p->progress_mutex); 00640 } 00641 } 00642 } 00643 00644 static void frame_thread_free(AVCodecContext *avctx, int thread_count) 00645 { 00646 FrameThreadContext *fctx = avctx->thread_opaque; 00647 AVCodec *codec = avctx->codec; 00648 int i; 00649 00650 park_frame_worker_threads(fctx, thread_count); 00651 00652 if (fctx->prev_thread && fctx->prev_thread != fctx->threads) 00653 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0); 00654 00655 fctx->die = 1; 00656 00657 for (i = 0; i < thread_count; i++) { 00658 PerThreadContext *p = &fctx->threads[i]; 00659 00660 pthread_mutex_lock(&p->mutex); 00661 pthread_cond_signal(&p->input_cond); 00662 pthread_mutex_unlock(&p->mutex); 00663 00664 if (p->thread_init) 00665 pthread_join(p->thread, NULL); 00666 p->thread_init=0; 00667 00668 if (codec->close) 00669 codec->close(p->avctx); 00670 00671 avctx->codec = NULL; 00672 00673 release_delayed_buffers(p); 00674 } 00675 00676 for (i = 0; i < thread_count; i++) { 00677 PerThreadContext *p = &fctx->threads[i]; 00678 00679 avcodec_default_free_buffers(p->avctx); 00680 00681 pthread_mutex_destroy(&p->mutex); 00682 pthread_mutex_destroy(&p->progress_mutex); 00683 pthread_cond_destroy(&p->input_cond); 00684 pthread_cond_destroy(&p->progress_cond); 00685 pthread_cond_destroy(&p->output_cond); 00686 av_freep(&p->avpkt.data); 00687 00688 if (i) { 00689 av_freep(&p->avctx->priv_data); 00690 av_freep(&p->avctx->internal); 00691 } 00692 00693 av_freep(&p->avctx); 00694 } 00695 00696 av_freep(&fctx->threads); 00697 pthread_mutex_destroy(&fctx->buffer_mutex); 00698 av_freep(&avctx->thread_opaque); 00699 } 00700 00701 static int frame_thread_init(AVCodecContext *avctx) 00702 { 00703 int thread_count = avctx->thread_count; 00704 AVCodec *codec = avctx->codec; 00705 AVCodecContext *src = avctx; 00706 FrameThreadContext *fctx; 00707 int i, err = 0; 00708 00709 if (thread_count <= 1) { 00710 avctx->active_thread_type = 0; 00711 return 0; 00712 } 00713 00714 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext)); 00715 00716 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count); 00717 pthread_mutex_init(&fctx->buffer_mutex, NULL); 00718 fctx->delaying = 1; 00719 00720 for (i = 0; i < thread_count; i++) { 00721 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext)); 00722 PerThreadContext *p = &fctx->threads[i]; 00723 00724 pthread_mutex_init(&p->mutex, NULL); 00725 pthread_mutex_init(&p->progress_mutex, NULL); 00726 pthread_cond_init(&p->input_cond, NULL); 00727 pthread_cond_init(&p->progress_cond, NULL); 00728 pthread_cond_init(&p->output_cond, NULL); 00729 00730 p->parent = fctx; 00731 p->avctx = copy; 00732 00733 if (!copy) { 00734 err = AVERROR(ENOMEM); 00735 goto error; 00736 } 00737 00738 *copy = *src; 00739 copy->thread_opaque = p; 00740 copy->pkt = &p->avpkt; 00741 00742 if (!i) { 00743 src = copy; 00744 00745 if (codec->init) 00746 err = codec->init(copy); 00747 00748 update_context_from_thread(avctx, copy, 1); 00749 } else { 00750 copy->priv_data = av_malloc(codec->priv_data_size); 00751 if (!copy->priv_data) { 00752 err = AVERROR(ENOMEM); 00753 goto error; 00754 } 00755 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size); 00756 copy->internal = av_malloc(sizeof(AVCodecInternal)); 00757 if (!copy->internal) { 00758 err = AVERROR(ENOMEM); 00759 goto error; 00760 } 00761 *(copy->internal) = *(src->internal); 00762 copy->internal->is_copy = 1; 00763 00764 if (codec->init_thread_copy) 00765 err = codec->init_thread_copy(copy); 00766 } 00767 00768 if (err) goto error; 00769 00770 p->thread_init= !pthread_create(&p->thread, NULL, frame_worker_thread, p); 00771 if(!p->thread_init) 00772 goto error; 00773 } 00774 00775 return 0; 00776 00777 error: 00778 frame_thread_free(avctx, i+1); 00779 00780 return err; 00781 } 00782 00783 void ff_thread_flush(AVCodecContext *avctx) 00784 { 00785 FrameThreadContext *fctx = avctx->thread_opaque; 00786 00787 if (!avctx->thread_opaque) return; 00788 00789 park_frame_worker_threads(fctx, avctx->thread_count); 00790 if (fctx->prev_thread) { 00791 if (fctx->prev_thread != &fctx->threads[0]) 00792 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0); 00793 if (avctx->codec->flush) 00794 avctx->codec->flush(fctx->threads[0].avctx); 00795 } 00796 00797 fctx->next_decoding = fctx->next_finished = 0; 00798 fctx->delaying = 1; 00799 fctx->prev_thread = NULL; 00800 } 00801 00802 static int *allocate_progress(PerThreadContext *p) 00803 { 00804 int i; 00805 00806 for (i = 0; i < MAX_BUFFERS; i++) 00807 if (!p->progress_used[i]) break; 00808 00809 if (i == MAX_BUFFERS) { 00810 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n"); 00811 return NULL; 00812 } 00813 00814 p->progress_used[i] = 1; 00815 00816 return p->progress[i]; 00817 } 00818 00819 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f) 00820 { 00821 PerThreadContext *p = avctx->thread_opaque; 00822 int *progress, err; 00823 00824 f->owner = avctx; 00825 00826 ff_init_buffer_info(avctx, f); 00827 00828 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) { 00829 f->thread_opaque = NULL; 00830 return avctx->get_buffer(avctx, f); 00831 } 00832 00833 if (p->state != STATE_SETTING_UP && 00834 (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && 00835 avctx->get_buffer != avcodec_default_get_buffer))) { 00836 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n"); 00837 return -1; 00838 } 00839 00840 pthread_mutex_lock(&p->parent->buffer_mutex); 00841 f->thread_opaque = progress = allocate_progress(p); 00842 00843 if (!progress) { 00844 pthread_mutex_unlock(&p->parent->buffer_mutex); 00845 return -1; 00846 } 00847 00848 progress[0] = 00849 progress[1] = -1; 00850 00851 if (avctx->thread_safe_callbacks || 00852 avctx->get_buffer == avcodec_default_get_buffer) { 00853 err = avctx->get_buffer(avctx, f); 00854 } else { 00855 p->requested_frame = f; 00856 p->state = STATE_GET_BUFFER; 00857 pthread_mutex_lock(&p->progress_mutex); 00858 pthread_cond_signal(&p->progress_cond); 00859 00860 while (p->state != STATE_SETTING_UP) 00861 pthread_cond_wait(&p->progress_cond, &p->progress_mutex); 00862 00863 err = p->result; 00864 00865 pthread_mutex_unlock(&p->progress_mutex); 00866 00867 if (!avctx->codec->update_thread_context) 00868 ff_thread_finish_setup(avctx); 00869 } 00870 00871 pthread_mutex_unlock(&p->parent->buffer_mutex); 00872 00873 /* 00874 * Buffer age is difficult to keep track of between 00875 * multiple threads, and the optimizations it allows 00876 * are not worth the effort. It is disabled for now. 00877 */ 00878 f->age = INT_MAX; 00879 00880 return err; 00881 } 00882 00883 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f) 00884 { 00885 PerThreadContext *p = avctx->thread_opaque; 00886 FrameThreadContext *fctx; 00887 00888 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) { 00889 avctx->release_buffer(avctx, f); 00890 return; 00891 } 00892 00893 if (p->num_released_buffers >= MAX_BUFFERS) { 00894 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n"); 00895 return; 00896 } 00897 00898 if(avctx->debug & FF_DEBUG_BUFFERS) 00899 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f); 00900 00901 fctx = p->parent; 00902 pthread_mutex_lock(&fctx->buffer_mutex); 00903 p->released_buffers[p->num_released_buffers++] = *f; 00904 pthread_mutex_unlock(&fctx->buffer_mutex); 00905 memset(f->data, 0, sizeof(f->data)); 00906 } 00907 00917 static void validate_thread_parameters(AVCodecContext *avctx) 00918 { 00919 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS) 00920 && !(avctx->flags & CODEC_FLAG_TRUNCATED) 00921 && !(avctx->flags & CODEC_FLAG_LOW_DELAY) 00922 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS); 00923 if (avctx->thread_count == 1) { 00924 avctx->active_thread_type = 0; 00925 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) { 00926 avctx->active_thread_type = FF_THREAD_FRAME; 00927 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS && 00928 avctx->thread_type & FF_THREAD_SLICE) { 00929 avctx->active_thread_type = FF_THREAD_SLICE; 00930 } 00931 } 00932 00933 int ff_thread_init(AVCodecContext *avctx) 00934 { 00935 if (avctx->thread_opaque) { 00936 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n"); 00937 return -1; 00938 } 00939 00940 #if HAVE_W32THREADS 00941 w32thread_init(); 00942 #endif 00943 00944 if (avctx->codec) { 00945 validate_thread_parameters(avctx); 00946 00947 if (avctx->active_thread_type&FF_THREAD_SLICE) 00948 return thread_init(avctx); 00949 else if (avctx->active_thread_type&FF_THREAD_FRAME) 00950 return frame_thread_init(avctx); 00951 } 00952 00953 return 0; 00954 } 00955 00956 void ff_thread_free(AVCodecContext *avctx) 00957 { 00958 if (avctx->active_thread_type&FF_THREAD_FRAME) 00959 frame_thread_free(avctx, avctx->thread_count); 00960 else 00961 thread_free(avctx); 00962 }