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