2004-02-13 19:54:10 +02:00
|
|
|
/*
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2004 Roman Shaposhnik
|
2011-02-08 04:15:44 +02:00
|
|
|
* Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
|
2005-12-17 20:14:38 +02:00
|
|
|
*
|
2004-05-18 04:53:43 +03:00
|
|
|
* Many thanks to Steven M. Schultz for providing clever ideas and
|
|
|
|
* to Michael Niedermayer <michaelni@gmx.at> for writing initial
|
|
|
|
* implementation.
|
2004-02-13 19:54:10 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-02-13 19:54:10 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-02-13 19:54:10 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-02-13 19:54:10 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-02-13 19:54:10 +02:00
|
|
|
*/
|
2011-02-08 04:15:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Multithreading support functions
|
|
|
|
* @see doc/multithreading.txt
|
|
|
|
*/
|
|
|
|
|
2021-09-02 13:00:42 +02:00
|
|
|
#include "libavutil/thread.h"
|
|
|
|
|
2004-02-13 19:54:10 +02:00
|
|
|
#include "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2013-10-27 14:22:52 +03:00
|
|
|
#include "pthread_internal.h"
|
2011-02-08 04:15:44 +02:00
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the threading algorithms used.
|
|
|
|
*
|
|
|
|
* Threading requires more than one thread.
|
|
|
|
* Frame threading requires entire frames to be passed to the codec,
|
|
|
|
* and introduces extra decoding delay, so is incompatible with low_delay.
|
|
|
|
*
|
|
|
|
* @param avctx The context.
|
|
|
|
*/
|
|
|
|
static void validate_thread_parameters(AVCodecContext *avctx)
|
|
|
|
{
|
2015-07-07 02:41:27 +02:00
|
|
|
int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
|
2015-06-29 21:59:37 +02:00
|
|
|
&& !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
|
|
|
|
&& !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS);
|
2011-02-08 04:15:44 +02:00
|
|
|
if (avctx->thread_count == 1) {
|
|
|
|
avctx->active_thread_type = 0;
|
|
|
|
} else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
|
|
|
|
avctx->active_thread_type = FF_THREAD_FRAME;
|
2015-07-07 02:41:27 +02:00
|
|
|
} else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS &&
|
2011-04-20 21:14:42 +03:00
|
|
|
avctx->thread_type & FF_THREAD_SLICE) {
|
2011-02-08 04:15:44 +02:00
|
|
|
avctx->active_thread_type = FF_THREAD_SLICE;
|
2022-03-16 22:09:54 +02:00
|
|
|
} else if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) {
|
2012-01-01 16:21:50 +03:00
|
|
|
avctx->thread_count = 1;
|
|
|
|
avctx->active_thread_type = 0;
|
2011-02-08 04:15:44 +02:00
|
|
|
}
|
2012-03-19 08:08:14 +03:00
|
|
|
|
|
|
|
if (avctx->thread_count > MAX_AUTO_THREADS)
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
|
|
|
|
avctx->thread_count, MAX_AUTO_THREADS);
|
2011-02-08 04:15:44 +02:00
|
|
|
}
|
|
|
|
|
2011-03-26 14:55:30 +02:00
|
|
|
int ff_thread_init(AVCodecContext *avctx)
|
2011-02-08 04:15:44 +02:00
|
|
|
{
|
2013-05-11 18:32:52 +03:00
|
|
|
validate_thread_parameters(avctx);
|
2011-02-08 04:15:44 +02:00
|
|
|
|
2013-05-11 18:32:52 +03:00
|
|
|
if (avctx->active_thread_type&FF_THREAD_SLICE)
|
2013-10-27 14:22:52 +03:00
|
|
|
return ff_slice_thread_init(avctx);
|
2013-05-11 18:32:52 +03:00
|
|
|
else if (avctx->active_thread_type&FF_THREAD_FRAME)
|
2013-10-27 14:22:52 +03:00
|
|
|
return ff_frame_thread_init(avctx);
|
2011-02-08 04:15:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-08 04:15:45 +02:00
|
|
|
void ff_thread_free(AVCodecContext *avctx)
|
2011-02-08 04:15:44 +02:00
|
|
|
{
|
|
|
|
if (avctx->active_thread_type&FF_THREAD_FRAME)
|
2013-10-27 14:22:52 +03:00
|
|
|
ff_frame_thread_free(avctx, avctx->thread_count);
|
2011-02-08 04:15:44 +02:00
|
|
|
else
|
2013-10-27 14:22:52 +03:00
|
|
|
ff_slice_thread_free(avctx);
|
2013-10-20 23:48:03 +03:00
|
|
|
}
|
2021-09-02 13:00:42 +02:00
|
|
|
|
|
|
|
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
|
|
|
|
{
|
|
|
|
unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
|
|
|
|
const unsigned *cur_offset = offsets;
|
|
|
|
|
|
|
|
*(unsigned*)((char*)obj + offsets[0]) = 0;
|
|
|
|
|
|
|
|
for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
|
|
|
|
pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
|
|
|
|
for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--)
|
|
|
|
pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
|
|
|
|
{
|
|
|
|
const unsigned *cur_offset = offsets;
|
|
|
|
unsigned cnt = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
#define PTHREAD_INIT_LOOP(type) \
|
|
|
|
for (; *(++cur_offset) != THREAD_SENTINEL; cnt++) { \
|
|
|
|
pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \
|
|
|
|
err = pthread_ ## type ## _init(dst, NULL); \
|
|
|
|
if (err) { \
|
|
|
|
err = AVERROR(err); \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
PTHREAD_INIT_LOOP(mutex)
|
|
|
|
PTHREAD_INIT_LOOP(cond)
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*(unsigned*)((char*)obj + offsets[0]) = cnt;
|
|
|
|
return err;
|
|
|
|
}
|