yading@10: /* yading@10: * Copyright (c) 2004 Roman Shaposhnik yading@10: * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com) yading@10: * yading@10: * Many thanks to Steven M. Schultz for providing clever ideas and yading@10: * to Michael Niedermayer for writing initial yading@10: * implementation. yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Multithreading support functions yading@10: * @see doc/multithreading.txt yading@10: */ yading@10: yading@10: #include "config.h" yading@10: yading@10: #if HAVE_SCHED_GETAFFINITY yading@10: #ifndef _GNU_SOURCE yading@10: # define _GNU_SOURCE yading@10: #endif yading@10: #include yading@10: #endif yading@10: #if HAVE_GETPROCESSAFFINITYMASK yading@10: #include yading@10: #endif yading@10: #if HAVE_SYSCTL yading@10: #if HAVE_SYS_PARAM_H yading@10: #include yading@10: #endif yading@10: #include yading@10: #include yading@10: #include yading@10: #endif yading@10: #if HAVE_SYSCONF yading@10: #include yading@10: #endif yading@10: yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: #include "thread.h" yading@10: #include "libavutil/avassert.h" yading@10: #include "libavutil/common.h" yading@10: yading@10: #if HAVE_PTHREADS yading@10: #include yading@10: #elif HAVE_W32THREADS yading@10: #include "w32pthreads.h" yading@10: #elif HAVE_OS2THREADS yading@10: #include "os2threads.h" yading@10: #endif yading@10: yading@10: typedef int (action_func)(AVCodecContext *c, void *arg); yading@10: typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr); yading@10: yading@10: typedef struct ThreadContext { yading@10: pthread_t *workers; yading@10: action_func *func; yading@10: action_func2 *func2; yading@10: void *args; yading@10: int *rets; yading@10: int rets_count; yading@10: int job_count; yading@10: int job_size; yading@10: yading@10: pthread_cond_t last_job_cond; yading@10: pthread_cond_t current_job_cond; yading@10: pthread_mutex_t current_job_lock; yading@10: int current_job; yading@10: unsigned int current_execute; yading@10: int done; yading@10: } ThreadContext; yading@10: yading@10: /** yading@10: * Context used by codec threads and stored in their AVCodecContext thread_opaque. yading@10: */ yading@10: typedef struct PerThreadContext { yading@10: struct FrameThreadContext *parent; yading@10: yading@10: pthread_t thread; yading@10: int thread_init; yading@10: pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread. yading@10: pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change. yading@10: pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish. yading@10: yading@10: pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext. yading@10: pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond. yading@10: yading@10: AVCodecContext *avctx; ///< Context used to decode packets passed to this thread. yading@10: yading@10: AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding). yading@10: uint8_t *buf; ///< backup storage for packet data when the input packet is not refcounted yading@10: int allocated_buf_size; ///< Size allocated for buf yading@10: yading@10: AVFrame frame; ///< Output frame (for decoding) or input (for encoding). yading@10: int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call. yading@10: int result; ///< The result of the last codec decode/encode() call. yading@10: yading@10: enum { yading@10: STATE_INPUT_READY, ///< Set when the thread is awaiting a packet. yading@10: STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup(). yading@10: STATE_GET_BUFFER, /**< yading@10: * Set when the codec calls get_buffer(). yading@10: * State is returned to STATE_SETTING_UP afterwards. yading@10: */ yading@10: STATE_GET_FORMAT, /**< yading@10: * Set when the codec calls get_format(). yading@10: * State is returned to STATE_SETTING_UP afterwards. yading@10: */ yading@10: STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup(). yading@10: } state; yading@10: yading@10: /** yading@10: * Array of frames passed to ff_thread_release_buffer(). yading@10: * Frames are released after all threads referencing them are finished. yading@10: */ yading@10: AVFrame *released_buffers; yading@10: int num_released_buffers; yading@10: int released_buffers_allocated; yading@10: yading@10: AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer() yading@10: int requested_flags; ///< flags passed to get_buffer() for requested_frame yading@10: yading@10: const enum AVPixelFormat *available_formats; ///< Format array for get_format() yading@10: enum AVPixelFormat result_format; ///< get_format() result yading@10: } PerThreadContext; yading@10: yading@10: /** yading@10: * Context stored in the client AVCodecContext thread_opaque. yading@10: */ yading@10: typedef struct FrameThreadContext { yading@10: PerThreadContext *threads; ///< The contexts for each thread. yading@10: PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on. yading@10: yading@10: pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer(). yading@10: yading@10: int next_decoding; ///< The next context to submit a packet to. yading@10: int next_finished; ///< The next context to return output from. yading@10: yading@10: int delaying; /**< yading@10: * Set for the first N packets, where N is the number of threads. yading@10: * While it is set, ff_thread_en/decode_frame won't return any results. yading@10: */ yading@10: yading@10: int die; ///< Set when threads should exit. yading@10: } FrameThreadContext; yading@10: yading@10: yading@10: /* H264 slice threading seems to be buggy with more than 16 threads, yading@10: * limit the number of threads to 16 for automatic detection */ yading@10: #define MAX_AUTO_THREADS 16 yading@10: yading@10: int ff_get_logical_cpus(AVCodecContext *avctx) yading@10: { yading@10: int ret, nb_cpus = 1; yading@10: #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) yading@10: cpu_set_t cpuset; yading@10: yading@10: CPU_ZERO(&cpuset); yading@10: yading@10: ret = sched_getaffinity(0, sizeof(cpuset), &cpuset); yading@10: if (!ret) { yading@10: nb_cpus = CPU_COUNT(&cpuset); yading@10: } yading@10: #elif HAVE_GETPROCESSAFFINITYMASK yading@10: DWORD_PTR proc_aff, sys_aff; yading@10: ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff); yading@10: if (ret) yading@10: nb_cpus = av_popcount64(proc_aff); yading@10: #elif HAVE_SYSCTL && defined(HW_NCPU) yading@10: int mib[2] = { CTL_HW, HW_NCPU }; yading@10: size_t len = sizeof(nb_cpus); yading@10: yading@10: ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0); yading@10: if (ret == -1) yading@10: nb_cpus = 0; yading@10: #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) yading@10: nb_cpus = sysconf(_SC_NPROC_ONLN); yading@10: #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) yading@10: nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); yading@10: #endif yading@10: av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); yading@10: yading@10: if (avctx->height) yading@10: nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16); yading@10: yading@10: return nb_cpus; yading@10: } yading@10: yading@10: yading@10: static void* attribute_align_arg worker(void *v) yading@10: { yading@10: AVCodecContext *avctx = v; yading@10: ThreadContext *c = avctx->thread_opaque; yading@10: int our_job = c->job_count; yading@10: int last_execute = 0; yading@10: int thread_count = avctx->thread_count; yading@10: int self_id; yading@10: yading@10: pthread_mutex_lock(&c->current_job_lock); yading@10: self_id = c->current_job++; yading@10: for (;;){ yading@10: while (our_job >= c->job_count) { yading@10: if (c->current_job == thread_count + c->job_count) yading@10: pthread_cond_signal(&c->last_job_cond); yading@10: yading@10: while (last_execute == c->current_execute && !c->done) yading@10: pthread_cond_wait(&c->current_job_cond, &c->current_job_lock); yading@10: last_execute = c->current_execute; yading@10: our_job = self_id; yading@10: yading@10: if (c->done) { yading@10: pthread_mutex_unlock(&c->current_job_lock); yading@10: return NULL; yading@10: } yading@10: } yading@10: pthread_mutex_unlock(&c->current_job_lock); yading@10: yading@10: c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size): yading@10: c->func2(avctx, c->args, our_job, self_id); yading@10: yading@10: pthread_mutex_lock(&c->current_job_lock); yading@10: our_job = c->current_job++; yading@10: } yading@10: } yading@10: yading@10: static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count) yading@10: { yading@10: while (c->current_job != thread_count + c->job_count) yading@10: pthread_cond_wait(&c->last_job_cond, &c->current_job_lock); yading@10: pthread_mutex_unlock(&c->current_job_lock); yading@10: } yading@10: yading@10: static void thread_free(AVCodecContext *avctx) yading@10: { yading@10: ThreadContext *c = avctx->thread_opaque; yading@10: int i; yading@10: yading@10: pthread_mutex_lock(&c->current_job_lock); yading@10: c->done = 1; yading@10: pthread_cond_broadcast(&c->current_job_cond); yading@10: pthread_mutex_unlock(&c->current_job_lock); yading@10: yading@10: for (i=0; ithread_count; i++) yading@10: pthread_join(c->workers[i], NULL); yading@10: yading@10: pthread_mutex_destroy(&c->current_job_lock); yading@10: pthread_cond_destroy(&c->current_job_cond); yading@10: pthread_cond_destroy(&c->last_job_cond); yading@10: av_free(c->workers); yading@10: av_freep(&avctx->thread_opaque); yading@10: } yading@10: yading@10: static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size) yading@10: { yading@10: ThreadContext *c= avctx->thread_opaque; yading@10: int dummy_ret; yading@10: yading@10: if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1) yading@10: return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size); yading@10: yading@10: if (job_count <= 0) yading@10: return 0; yading@10: yading@10: pthread_mutex_lock(&c->current_job_lock); yading@10: yading@10: c->current_job = avctx->thread_count; yading@10: c->job_count = job_count; yading@10: c->job_size = job_size; yading@10: c->args = arg; yading@10: c->func = func; yading@10: if (ret) { yading@10: c->rets = ret; yading@10: c->rets_count = job_count; yading@10: } else { yading@10: c->rets = &dummy_ret; yading@10: c->rets_count = 1; yading@10: } yading@10: c->current_execute++; yading@10: pthread_cond_broadcast(&c->current_job_cond); yading@10: yading@10: avcodec_thread_park_workers(c, avctx->thread_count); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count) yading@10: { yading@10: ThreadContext *c= avctx->thread_opaque; yading@10: c->func2 = func2; yading@10: return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0); yading@10: } yading@10: yading@10: static int thread_init(AVCodecContext *avctx) yading@10: { yading@10: int i; yading@10: ThreadContext *c; yading@10: int thread_count = avctx->thread_count; yading@10: yading@10: if (!thread_count) { yading@10: int nb_cpus = ff_get_logical_cpus(avctx); yading@10: // use number of cores + 1 as thread count if there is more than one yading@10: if (nb_cpus > 1) yading@10: thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); yading@10: else yading@10: thread_count = avctx->thread_count = 1; yading@10: } yading@10: yading@10: if (thread_count <= 1) { yading@10: avctx->active_thread_type = 0; yading@10: return 0; yading@10: } yading@10: yading@10: c = av_mallocz(sizeof(ThreadContext)); yading@10: if (!c) yading@10: return -1; yading@10: yading@10: c->workers = av_mallocz(sizeof(pthread_t)*thread_count); yading@10: if (!c->workers) { yading@10: av_free(c); yading@10: return -1; yading@10: } yading@10: yading@10: avctx->thread_opaque = c; yading@10: c->current_job = 0; yading@10: c->job_count = 0; yading@10: c->job_size = 0; yading@10: c->done = 0; yading@10: pthread_cond_init(&c->current_job_cond, NULL); yading@10: pthread_cond_init(&c->last_job_cond, NULL); yading@10: pthread_mutex_init(&c->current_job_lock, NULL); yading@10: pthread_mutex_lock(&c->current_job_lock); yading@10: for (i=0; iworkers[i], NULL, worker, avctx)) { yading@10: avctx->thread_count = i; yading@10: pthread_mutex_unlock(&c->current_job_lock); yading@10: ff_thread_free(avctx); yading@10: return -1; yading@10: } yading@10: } yading@10: yading@10: avcodec_thread_park_workers(c, thread_count); yading@10: yading@10: avctx->execute = avcodec_thread_execute; yading@10: avctx->execute2 = avcodec_thread_execute2; yading@10: return 0; yading@10: } yading@10: yading@10: /** yading@10: * Codec worker thread. yading@10: * yading@10: * Automatically calls ff_thread_finish_setup() if the codec does yading@10: * not provide an update_thread_context method, or if the codec returns yading@10: * before calling it. yading@10: */ yading@10: static attribute_align_arg void *frame_worker_thread(void *arg) yading@10: { yading@10: PerThreadContext *p = arg; yading@10: FrameThreadContext *fctx = p->parent; yading@10: AVCodecContext *avctx = p->avctx; yading@10: const AVCodec *codec = avctx->codec; yading@10: yading@10: pthread_mutex_lock(&p->mutex); yading@10: while (1) { yading@10: while (p->state == STATE_INPUT_READY && !fctx->die) yading@10: pthread_cond_wait(&p->input_cond, &p->mutex); yading@10: yading@10: if (fctx->die) break; yading@10: yading@10: if (!codec->update_thread_context && (avctx->thread_safe_callbacks || ( yading@10: #if FF_API_GET_BUFFER yading@10: !avctx->get_buffer && yading@10: #endif yading@10: avctx->get_buffer2 == avcodec_default_get_buffer2))) yading@10: ff_thread_finish_setup(avctx); yading@10: yading@10: avcodec_get_frame_defaults(&p->frame); yading@10: p->got_frame = 0; yading@10: p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt); yading@10: yading@10: /* many decoders assign whole AVFrames, thus overwriting extended_data; yading@10: * make sure it's set correctly */ yading@10: p->frame.extended_data = p->frame.data; yading@10: yading@10: if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx); yading@10: yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: #if 0 //BUFREF-FIXME yading@10: for (i = 0; i < MAX_BUFFERS; i++) yading@10: if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) { yading@10: p->progress[i][0] = INT_MAX; yading@10: p->progress[i][1] = INT_MAX; yading@10: } yading@10: #endif yading@10: p->state = STATE_INPUT_READY; yading@10: yading@10: pthread_cond_broadcast(&p->progress_cond); yading@10: pthread_cond_signal(&p->output_cond); yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: pthread_mutex_unlock(&p->mutex); yading@10: yading@10: return NULL; yading@10: } yading@10: yading@10: /** yading@10: * Update the next thread's AVCodecContext with values from the reference thread's context. yading@10: * yading@10: * @param dst The destination context. yading@10: * @param src The source context. yading@10: * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread yading@10: */ yading@10: static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user) yading@10: { yading@10: int err = 0; yading@10: yading@10: if (dst != src) { yading@10: dst->time_base = src->time_base; yading@10: dst->width = src->width; yading@10: dst->height = src->height; yading@10: dst->pix_fmt = src->pix_fmt; yading@10: yading@10: dst->coded_width = src->coded_width; yading@10: dst->coded_height = src->coded_height; yading@10: yading@10: dst->has_b_frames = src->has_b_frames; yading@10: dst->idct_algo = src->idct_algo; yading@10: yading@10: dst->bits_per_coded_sample = src->bits_per_coded_sample; yading@10: dst->sample_aspect_ratio = src->sample_aspect_ratio; yading@10: dst->dtg_active_format = src->dtg_active_format; yading@10: yading@10: dst->profile = src->profile; yading@10: dst->level = src->level; yading@10: yading@10: dst->bits_per_raw_sample = src->bits_per_raw_sample; yading@10: dst->ticks_per_frame = src->ticks_per_frame; yading@10: dst->color_primaries = src->color_primaries; yading@10: yading@10: dst->color_trc = src->color_trc; yading@10: dst->colorspace = src->colorspace; yading@10: dst->color_range = src->color_range; yading@10: dst->chroma_sample_location = src->chroma_sample_location; yading@10: yading@10: dst->hwaccel = src->hwaccel; yading@10: dst->hwaccel_context = src->hwaccel_context; yading@10: } yading@10: yading@10: if (for_user) { yading@10: dst->delay = src->thread_count - 1; yading@10: dst->coded_frame = src->coded_frame; yading@10: } else { yading@10: if (dst->codec->update_thread_context) yading@10: err = dst->codec->update_thread_context(dst, src); yading@10: } yading@10: yading@10: return err; yading@10: } yading@10: yading@10: /** yading@10: * Update the next thread's AVCodecContext with values set by the user. yading@10: * yading@10: * @param dst The destination context. yading@10: * @param src The source context. yading@10: * @return 0 on success, negative error code on failure yading@10: */ yading@10: static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src) yading@10: { yading@10: #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s); yading@10: dst->flags = src->flags; yading@10: yading@10: dst->draw_horiz_band= src->draw_horiz_band; yading@10: dst->get_buffer2 = src->get_buffer2; yading@10: #if FF_API_GET_BUFFER yading@10: dst->get_buffer = src->get_buffer; yading@10: dst->release_buffer = src->release_buffer; yading@10: #endif yading@10: yading@10: dst->opaque = src->opaque; yading@10: dst->debug = src->debug; yading@10: dst->debug_mv = src->debug_mv; yading@10: yading@10: dst->slice_flags = src->slice_flags; yading@10: dst->flags2 = src->flags2; yading@10: yading@10: copy_fields(skip_loop_filter, subtitle_header); yading@10: yading@10: dst->frame_number = src->frame_number; yading@10: dst->reordered_opaque = src->reordered_opaque; yading@10: dst->thread_safe_callbacks = src->thread_safe_callbacks; yading@10: yading@10: if (src->slice_count && src->slice_offset) { yading@10: if (dst->slice_count < src->slice_count) { yading@10: int *tmp = av_realloc(dst->slice_offset, src->slice_count * yading@10: sizeof(*dst->slice_offset)); yading@10: if (!tmp) { yading@10: av_free(dst->slice_offset); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: dst->slice_offset = tmp; yading@10: } yading@10: memcpy(dst->slice_offset, src->slice_offset, yading@10: src->slice_count * sizeof(*dst->slice_offset)); yading@10: } yading@10: dst->slice_count = src->slice_count; yading@10: return 0; yading@10: #undef copy_fields yading@10: } yading@10: yading@10: /// Releases the buffers that this decoding thread was the last user of. yading@10: static void release_delayed_buffers(PerThreadContext *p) yading@10: { yading@10: FrameThreadContext *fctx = p->parent; yading@10: yading@10: while (p->num_released_buffers > 0) { yading@10: AVFrame *f; yading@10: yading@10: pthread_mutex_lock(&fctx->buffer_mutex); yading@10: yading@10: // fix extended data in case the caller screwed it up yading@10: av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO); yading@10: f = &p->released_buffers[--p->num_released_buffers]; yading@10: f->extended_data = f->data; yading@10: av_frame_unref(f); yading@10: yading@10: pthread_mutex_unlock(&fctx->buffer_mutex); yading@10: } yading@10: } yading@10: yading@10: static int submit_packet(PerThreadContext *p, AVPacket *avpkt) yading@10: { yading@10: FrameThreadContext *fctx = p->parent; yading@10: PerThreadContext *prev_thread = fctx->prev_thread; yading@10: const AVCodec *codec = p->avctx->codec; yading@10: yading@10: if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0; yading@10: yading@10: pthread_mutex_lock(&p->mutex); yading@10: yading@10: release_delayed_buffers(p); yading@10: yading@10: if (prev_thread) { yading@10: int err; yading@10: if (prev_thread->state == STATE_SETTING_UP) { yading@10: pthread_mutex_lock(&prev_thread->progress_mutex); yading@10: while (prev_thread->state == STATE_SETTING_UP) yading@10: pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex); yading@10: pthread_mutex_unlock(&prev_thread->progress_mutex); yading@10: } yading@10: yading@10: err = update_context_from_thread(p->avctx, prev_thread->avctx, 0); yading@10: if (err) { yading@10: pthread_mutex_unlock(&p->mutex); yading@10: return err; yading@10: } yading@10: } yading@10: yading@10: av_buffer_unref(&p->avpkt.buf); yading@10: p->avpkt = *avpkt; yading@10: if (avpkt->buf) yading@10: p->avpkt.buf = av_buffer_ref(avpkt->buf); yading@10: else { yading@10: av_fast_malloc(&p->buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE); yading@10: p->avpkt.data = p->buf; yading@10: memcpy(p->buf, avpkt->data, avpkt->size); yading@10: memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); yading@10: } yading@10: yading@10: p->state = STATE_SETTING_UP; yading@10: pthread_cond_signal(&p->input_cond); yading@10: pthread_mutex_unlock(&p->mutex); yading@10: yading@10: /* yading@10: * If the client doesn't have a thread-safe get_buffer(), yading@10: * then decoding threads call back to the main thread, yading@10: * and it calls back to the client here. yading@10: */ yading@10: yading@10: if (!p->avctx->thread_safe_callbacks && ( yading@10: p->avctx->get_format != avcodec_default_get_format || yading@10: #if FF_API_GET_BUFFER yading@10: p->avctx->get_buffer || yading@10: #endif yading@10: p->avctx->get_buffer2 != avcodec_default_get_buffer2)) { yading@10: while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) { yading@10: int call_done = 1; yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: while (p->state == STATE_SETTING_UP) yading@10: pthread_cond_wait(&p->progress_cond, &p->progress_mutex); yading@10: yading@10: switch (p->state) { yading@10: case STATE_GET_BUFFER: yading@10: p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags); yading@10: break; yading@10: case STATE_GET_FORMAT: yading@10: p->result_format = p->avctx->get_format(p->avctx, p->available_formats); yading@10: break; yading@10: default: yading@10: call_done = 0; yading@10: break; yading@10: } yading@10: if (call_done) { yading@10: p->state = STATE_SETTING_UP; yading@10: pthread_cond_signal(&p->progress_cond); yading@10: } yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: } yading@10: yading@10: fctx->prev_thread = p; yading@10: fctx->next_decoding++; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: int ff_thread_decode_frame(AVCodecContext *avctx, yading@10: AVFrame *picture, int *got_picture_ptr, yading@10: AVPacket *avpkt) yading@10: { yading@10: FrameThreadContext *fctx = avctx->thread_opaque; yading@10: int finished = fctx->next_finished; yading@10: PerThreadContext *p; yading@10: int err; yading@10: yading@10: /* yading@10: * Submit a packet to the next decoding thread. yading@10: */ yading@10: yading@10: p = &fctx->threads[fctx->next_decoding]; yading@10: err = update_context_from_user(p->avctx, avctx); yading@10: if (err) return err; yading@10: err = submit_packet(p, avpkt); yading@10: if (err) return err; yading@10: yading@10: /* yading@10: * If we're still receiving the initial packets, don't return a frame. yading@10: */ yading@10: yading@10: if (fctx->delaying) { yading@10: if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0; yading@10: yading@10: *got_picture_ptr=0; yading@10: if (avpkt->size) yading@10: return avpkt->size; yading@10: } yading@10: yading@10: /* yading@10: * Return the next available frame from the oldest thread. yading@10: * If we're at the end of the stream, then we have to skip threads that yading@10: * didn't output a frame, because we don't want to accidentally signal yading@10: * EOF (avpkt->size == 0 && *got_picture_ptr == 0). yading@10: */ yading@10: yading@10: do { yading@10: p = &fctx->threads[finished++]; yading@10: yading@10: if (p->state != STATE_INPUT_READY) { yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: while (p->state != STATE_INPUT_READY) yading@10: pthread_cond_wait(&p->output_cond, &p->progress_mutex); yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: yading@10: av_frame_move_ref(picture, &p->frame); yading@10: *got_picture_ptr = p->got_frame; yading@10: picture->pkt_dts = p->avpkt.dts; yading@10: yading@10: /* yading@10: * A later call with avkpt->size == 0 may loop over all threads, yading@10: * including this one, searching for a frame to return before being yading@10: * stopped by the "finished != fctx->next_finished" condition. yading@10: * Make sure we don't mistakenly return the same frame again. yading@10: */ yading@10: p->got_frame = 0; yading@10: yading@10: if (finished >= avctx->thread_count) finished = 0; yading@10: } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished); yading@10: yading@10: update_context_from_thread(avctx, p->avctx, 1); yading@10: yading@10: if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0; yading@10: yading@10: fctx->next_finished = finished; yading@10: yading@10: /* return the size of the consumed packet if no error occurred */ yading@10: return (p->result >= 0) ? avpkt->size : p->result; yading@10: } yading@10: yading@10: void ff_thread_report_progress(ThreadFrame *f, int n, int field) yading@10: { yading@10: PerThreadContext *p; yading@10: volatile int *progress = f->progress ? (int*)f->progress->data : NULL; yading@10: yading@10: if (!progress || progress[field] >= n) return; yading@10: yading@10: p = f->owner->thread_opaque; yading@10: yading@10: if (f->owner->debug&FF_DEBUG_THREADS) yading@10: av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field); yading@10: yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: progress[field] = n; yading@10: pthread_cond_broadcast(&p->progress_cond); yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: yading@10: void ff_thread_await_progress(ThreadFrame *f, int n, int field) yading@10: { yading@10: PerThreadContext *p; yading@10: volatile int *progress = f->progress ? (int*)f->progress->data : NULL; yading@10: yading@10: if (!progress || progress[field] >= n) return; yading@10: yading@10: p = f->owner->thread_opaque; yading@10: yading@10: if (f->owner->debug&FF_DEBUG_THREADS) yading@10: av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress); yading@10: yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: while (progress[field] < n) yading@10: pthread_cond_wait(&p->progress_cond, &p->progress_mutex); yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: yading@10: void ff_thread_finish_setup(AVCodecContext *avctx) { yading@10: PerThreadContext *p = avctx->thread_opaque; yading@10: yading@10: if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return; yading@10: yading@10: if(p->state == STATE_SETUP_FINISHED){ yading@10: av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n"); yading@10: } yading@10: yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: p->state = STATE_SETUP_FINISHED; yading@10: pthread_cond_broadcast(&p->progress_cond); yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: yading@10: /// Waits for all threads to finish. yading@10: static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count) yading@10: { yading@10: int i; yading@10: yading@10: for (i = 0; i < thread_count; i++) { yading@10: PerThreadContext *p = &fctx->threads[i]; yading@10: yading@10: if (p->state != STATE_INPUT_READY) { yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: while (p->state != STATE_INPUT_READY) yading@10: pthread_cond_wait(&p->output_cond, &p->progress_mutex); yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: } yading@10: p->got_frame = 0; yading@10: } yading@10: } yading@10: yading@10: static void frame_thread_free(AVCodecContext *avctx, int thread_count) yading@10: { yading@10: FrameThreadContext *fctx = avctx->thread_opaque; yading@10: const AVCodec *codec = avctx->codec; yading@10: int i; yading@10: yading@10: park_frame_worker_threads(fctx, thread_count); yading@10: yading@10: if (fctx->prev_thread && fctx->prev_thread != fctx->threads) yading@10: if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) { yading@10: av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n"); yading@10: fctx->prev_thread->avctx->internal->is_copy = fctx->threads->avctx->internal->is_copy; yading@10: fctx->threads->avctx->internal->is_copy = 1; yading@10: } yading@10: yading@10: fctx->die = 1; yading@10: yading@10: for (i = 0; i < thread_count; i++) { yading@10: PerThreadContext *p = &fctx->threads[i]; yading@10: yading@10: pthread_mutex_lock(&p->mutex); yading@10: pthread_cond_signal(&p->input_cond); yading@10: pthread_mutex_unlock(&p->mutex); yading@10: yading@10: if (p->thread_init) yading@10: pthread_join(p->thread, NULL); yading@10: p->thread_init=0; yading@10: yading@10: if (codec->close) yading@10: codec->close(p->avctx); yading@10: yading@10: avctx->codec = NULL; yading@10: yading@10: release_delayed_buffers(p); yading@10: av_frame_unref(&p->frame); yading@10: } yading@10: yading@10: for (i = 0; i < thread_count; i++) { yading@10: PerThreadContext *p = &fctx->threads[i]; yading@10: yading@10: pthread_mutex_destroy(&p->mutex); yading@10: pthread_mutex_destroy(&p->progress_mutex); yading@10: pthread_cond_destroy(&p->input_cond); yading@10: pthread_cond_destroy(&p->progress_cond); yading@10: pthread_cond_destroy(&p->output_cond); yading@10: av_buffer_unref(&p->avpkt.buf); yading@10: av_freep(&p->buf); yading@10: av_freep(&p->released_buffers); yading@10: yading@10: if (i) { yading@10: av_freep(&p->avctx->priv_data); yading@10: av_freep(&p->avctx->internal); yading@10: av_freep(&p->avctx->slice_offset); yading@10: } yading@10: yading@10: av_freep(&p->avctx); yading@10: } yading@10: yading@10: av_freep(&fctx->threads); yading@10: pthread_mutex_destroy(&fctx->buffer_mutex); yading@10: av_freep(&avctx->thread_opaque); yading@10: } yading@10: yading@10: static int frame_thread_init(AVCodecContext *avctx) yading@10: { yading@10: int thread_count = avctx->thread_count; yading@10: const AVCodec *codec = avctx->codec; yading@10: AVCodecContext *src = avctx; yading@10: FrameThreadContext *fctx; yading@10: int i, err = 0; yading@10: yading@10: if (!thread_count) { yading@10: int nb_cpus = ff_get_logical_cpus(avctx); yading@10: if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv) yading@10: nb_cpus = 1; yading@10: // use number of cores + 1 as thread count if there is more than one yading@10: if (nb_cpus > 1) yading@10: thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); yading@10: else yading@10: thread_count = avctx->thread_count = 1; yading@10: } yading@10: yading@10: if (thread_count <= 1) { yading@10: avctx->active_thread_type = 0; yading@10: return 0; yading@10: } yading@10: yading@10: avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext)); yading@10: yading@10: fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count); yading@10: pthread_mutex_init(&fctx->buffer_mutex, NULL); yading@10: fctx->delaying = 1; yading@10: yading@10: for (i = 0; i < thread_count; i++) { yading@10: AVCodecContext *copy = av_malloc(sizeof(AVCodecContext)); yading@10: PerThreadContext *p = &fctx->threads[i]; yading@10: yading@10: pthread_mutex_init(&p->mutex, NULL); yading@10: pthread_mutex_init(&p->progress_mutex, NULL); yading@10: pthread_cond_init(&p->input_cond, NULL); yading@10: pthread_cond_init(&p->progress_cond, NULL); yading@10: pthread_cond_init(&p->output_cond, NULL); yading@10: yading@10: p->parent = fctx; yading@10: p->avctx = copy; yading@10: yading@10: if (!copy) { yading@10: err = AVERROR(ENOMEM); yading@10: goto error; yading@10: } yading@10: yading@10: *copy = *src; yading@10: copy->thread_opaque = p; yading@10: copy->pkt = &p->avpkt; yading@10: yading@10: if (!i) { yading@10: src = copy; yading@10: yading@10: if (codec->init) yading@10: err = codec->init(copy); yading@10: yading@10: update_context_from_thread(avctx, copy, 1); yading@10: } else { yading@10: copy->priv_data = av_malloc(codec->priv_data_size); yading@10: if (!copy->priv_data) { yading@10: err = AVERROR(ENOMEM); yading@10: goto error; yading@10: } yading@10: memcpy(copy->priv_data, src->priv_data, codec->priv_data_size); yading@10: copy->internal = av_malloc(sizeof(AVCodecInternal)); yading@10: if (!copy->internal) { yading@10: err = AVERROR(ENOMEM); yading@10: goto error; yading@10: } yading@10: *copy->internal = *src->internal; yading@10: copy->internal->is_copy = 1; yading@10: yading@10: if (codec->init_thread_copy) yading@10: err = codec->init_thread_copy(copy); yading@10: } yading@10: yading@10: if (err) goto error; yading@10: yading@10: err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p)); yading@10: p->thread_init= !err; yading@10: if(!p->thread_init) yading@10: goto error; yading@10: } yading@10: yading@10: return 0; yading@10: yading@10: error: yading@10: frame_thread_free(avctx, i+1); yading@10: yading@10: return err; yading@10: } yading@10: yading@10: void ff_thread_flush(AVCodecContext *avctx) yading@10: { yading@10: int i; yading@10: FrameThreadContext *fctx = avctx->thread_opaque; yading@10: yading@10: if (!avctx->thread_opaque) return; yading@10: yading@10: park_frame_worker_threads(fctx, avctx->thread_count); yading@10: if (fctx->prev_thread) { yading@10: if (fctx->prev_thread != &fctx->threads[0]) yading@10: update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0); yading@10: if (avctx->codec->flush) yading@10: avctx->codec->flush(fctx->threads[0].avctx); yading@10: } yading@10: yading@10: fctx->next_decoding = fctx->next_finished = 0; yading@10: fctx->delaying = 1; yading@10: fctx->prev_thread = NULL; yading@10: for (i = 0; i < avctx->thread_count; i++) { yading@10: PerThreadContext *p = &fctx->threads[i]; yading@10: // Make sure decode flush calls with size=0 won't return old frames yading@10: p->got_frame = 0; yading@10: av_frame_unref(&p->frame); yading@10: yading@10: release_delayed_buffers(p); yading@10: } yading@10: } yading@10: yading@10: int ff_thread_can_start_frame(AVCodecContext *avctx) yading@10: { yading@10: PerThreadContext *p = avctx->thread_opaque; yading@10: if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP && yading@10: (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && ( yading@10: #if FF_API_GET_BUFFER yading@10: avctx->get_buffer || yading@10: #endif yading@10: avctx->get_buffer2 != avcodec_default_get_buffer2)))) { yading@10: return 0; yading@10: } yading@10: return 1; yading@10: } yading@10: yading@10: static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags) yading@10: { yading@10: PerThreadContext *p = avctx->thread_opaque; yading@10: int err; yading@10: yading@10: f->owner = avctx; yading@10: yading@10: ff_init_buffer_info(avctx, f->f); yading@10: yading@10: if (!(avctx->active_thread_type & FF_THREAD_FRAME)) yading@10: return ff_get_buffer(avctx, f->f, flags); yading@10: yading@10: if (p->state != STATE_SETTING_UP && yading@10: (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && ( yading@10: #if FF_API_GET_BUFFER yading@10: avctx->get_buffer || yading@10: #endif yading@10: avctx->get_buffer2 != avcodec_default_get_buffer2)))) { yading@10: av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n"); yading@10: return -1; yading@10: } yading@10: yading@10: if (avctx->internal->allocate_progress) { yading@10: int *progress; yading@10: f->progress = av_buffer_alloc(2 * sizeof(int)); yading@10: if (!f->progress) { yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: progress = (int*)f->progress->data; yading@10: yading@10: progress[0] = progress[1] = -1; yading@10: } yading@10: yading@10: pthread_mutex_lock(&p->parent->buffer_mutex); yading@10: yading@10: if (avctx->thread_safe_callbacks || ( yading@10: #if FF_API_GET_BUFFER yading@10: !avctx->get_buffer && yading@10: #endif yading@10: avctx->get_buffer2 == avcodec_default_get_buffer2)) { yading@10: err = ff_get_buffer(avctx, f->f, flags); yading@10: } else { yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: p->requested_frame = f->f; yading@10: p->requested_flags = flags; yading@10: p->state = STATE_GET_BUFFER; yading@10: pthread_cond_broadcast(&p->progress_cond); yading@10: yading@10: while (p->state != STATE_SETTING_UP) yading@10: pthread_cond_wait(&p->progress_cond, &p->progress_mutex); yading@10: yading@10: err = p->result; yading@10: yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: yading@10: if (!avctx->codec->update_thread_context) yading@10: ff_thread_finish_setup(avctx); yading@10: } yading@10: yading@10: if (err) yading@10: av_buffer_unref(&f->progress); yading@10: yading@10: pthread_mutex_unlock(&p->parent->buffer_mutex); yading@10: yading@10: return err; yading@10: } yading@10: yading@10: enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt) yading@10: { yading@10: enum AVPixelFormat res; yading@10: PerThreadContext *p = avctx->thread_opaque; yading@10: if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks || yading@10: avctx->get_format == avcodec_default_get_format) yading@10: return avctx->get_format(avctx, fmt); yading@10: if (p->state != STATE_SETTING_UP) { yading@10: av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n"); yading@10: return -1; yading@10: } yading@10: pthread_mutex_lock(&p->progress_mutex); yading@10: p->available_formats = fmt; yading@10: p->state = STATE_GET_FORMAT; yading@10: pthread_cond_broadcast(&p->progress_cond); yading@10: yading@10: while (p->state != STATE_SETTING_UP) yading@10: pthread_cond_wait(&p->progress_cond, &p->progress_mutex); yading@10: yading@10: res = p->result_format; yading@10: yading@10: pthread_mutex_unlock(&p->progress_mutex); yading@10: yading@10: return res; yading@10: } yading@10: yading@10: int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags) yading@10: { yading@10: int ret = thread_get_buffer_internal(avctx, f, flags); yading@10: if (ret < 0) yading@10: av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n"); yading@10: return ret; yading@10: } yading@10: yading@10: void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f) yading@10: { yading@10: PerThreadContext *p = avctx->thread_opaque; yading@10: FrameThreadContext *fctx; yading@10: AVFrame *dst, *tmp; yading@10: int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) || yading@10: avctx->thread_safe_callbacks || yading@10: ( yading@10: #if FF_API_GET_BUFFER yading@10: !avctx->get_buffer && yading@10: #endif yading@10: avctx->get_buffer2 == avcodec_default_get_buffer2); yading@10: yading@10: if (!f->f->data[0]) yading@10: return; yading@10: yading@10: if (avctx->debug & FF_DEBUG_BUFFERS) yading@10: av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f); yading@10: yading@10: av_buffer_unref(&f->progress); yading@10: f->owner = NULL; yading@10: yading@10: if (can_direct_free) { yading@10: av_frame_unref(f->f); yading@10: return; yading@10: } yading@10: yading@10: fctx = p->parent; yading@10: pthread_mutex_lock(&fctx->buffer_mutex); yading@10: yading@10: if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers)) yading@10: goto fail; yading@10: tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated, yading@10: (p->num_released_buffers + 1) * yading@10: sizeof(*p->released_buffers)); yading@10: if (!tmp) yading@10: goto fail; yading@10: p->released_buffers = tmp; yading@10: yading@10: dst = &p->released_buffers[p->num_released_buffers]; yading@10: av_frame_move_ref(dst, f->f); yading@10: yading@10: p->num_released_buffers++; yading@10: yading@10: fail: yading@10: pthread_mutex_unlock(&fctx->buffer_mutex); yading@10: } yading@10: yading@10: /** yading@10: * Set the threading algorithms used. yading@10: * yading@10: * Threading requires more than one thread. yading@10: * Frame threading requires entire frames to be passed to the codec, yading@10: * and introduces extra decoding delay, so is incompatible with low_delay. yading@10: * yading@10: * @param avctx The context. yading@10: */ yading@10: static void validate_thread_parameters(AVCodecContext *avctx) yading@10: { yading@10: int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS) yading@10: && !(avctx->flags & CODEC_FLAG_TRUNCATED) yading@10: && !(avctx->flags & CODEC_FLAG_LOW_DELAY) yading@10: && !(avctx->flags2 & CODEC_FLAG2_CHUNKS); yading@10: if (avctx->thread_count == 1) { yading@10: avctx->active_thread_type = 0; yading@10: } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) { yading@10: avctx->active_thread_type = FF_THREAD_FRAME; yading@10: } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS && yading@10: avctx->thread_type & FF_THREAD_SLICE) { yading@10: avctx->active_thread_type = FF_THREAD_SLICE; yading@10: } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) { yading@10: avctx->thread_count = 1; yading@10: avctx->active_thread_type = 0; yading@10: } yading@10: yading@10: if (avctx->thread_count > MAX_AUTO_THREADS) yading@10: av_log(avctx, AV_LOG_WARNING, yading@10: "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n", yading@10: avctx->thread_count, MAX_AUTO_THREADS); yading@10: } yading@10: yading@10: int ff_thread_init(AVCodecContext *avctx) yading@10: { yading@10: if (avctx->thread_opaque) { yading@10: av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n"); yading@10: return -1; yading@10: } yading@10: yading@10: #if HAVE_W32THREADS yading@10: w32thread_init(); yading@10: #endif yading@10: yading@10: if (avctx->codec) { yading@10: validate_thread_parameters(avctx); yading@10: yading@10: if (avctx->active_thread_type&FF_THREAD_SLICE) yading@10: return thread_init(avctx); yading@10: else if (avctx->active_thread_type&FF_THREAD_FRAME) yading@10: return frame_thread_init(avctx); yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: void ff_thread_free(AVCodecContext *avctx) yading@10: { yading@10: if (avctx->active_thread_type&FF_THREAD_FRAME) yading@10: frame_thread_free(avctx, avctx->thread_count); yading@10: else yading@10: thread_free(avctx); yading@10: }