From 89f35981615c1b8147d642119e69b44cd98638a9 Mon Sep 17 00:00:00 2001 From: Tuomas Virtanen Date: Tue, 2 Aug 2022 20:19:35 +0300 Subject: [PATCH] Add option for letting ffmpeg decide thread count --- examples/example_complex.c | 4 ++-- include/kitchensink/kitlib.h | 4 ++-- src/internal/kitdecoder.c | 12 +++++++++--- src/kitlib.c | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/examples/example_complex.c b/examples/example_complex.c index 8af8c3a..3a57695 100644 --- a/examples/example_complex.c +++ b/examples/example_complex.c @@ -108,8 +108,8 @@ int main(int argc, char *argv[]) { return 1; } - // Allow Kit to use more threads - Kit_SetHint(KIT_HINT_THREAD_COUNT, SDL_GetCPUCount() <= 4 ? SDL_GetCPUCount() : 4); + // Set to 0 to allow ffmpeg decide thread count. + Kit_SetHint(KIT_HINT_THREAD_COUNT, 0); // Lots of buffers for smooth playback (will eat up more memory, too). Kit_SetHint(KIT_HINT_VIDEO_BUFFER_FRAMES, 5); diff --git a/include/kitchensink/kitlib.h b/include/kitchensink/kitlib.h index a5fe913..f562c5a 100644 --- a/include/kitchensink/kitlib.h +++ b/include/kitchensink/kitlib.h @@ -42,7 +42,7 @@ typedef struct Kit_Version { */ typedef enum Kit_HintType { KIT_HINT_FONT_HINTING, ///< Set font hinting mode (currently used for libass) - KIT_HINT_THREAD_COUNT, ///< Set thread count for ffmpeg (1 by default) + KIT_HINT_THREAD_COUNT, ///< Set thread count for ffmpeg (1 by default). Set to 0 for autodetect. KIT_HINT_VIDEO_BUFFER_FRAMES, ///< Video output buffer frames (3 by default) KIT_HINT_AUDIO_BUFFER_FRAMES, ///< Audio output buffers (64 by default) KIT_HINT_SUBTITLE_BUFFER_FRAMES ///< Subtitle output buffers (64 by default, used by image subtitles) @@ -90,7 +90,7 @@ KIT_API int Kit_Init(unsigned int flags); KIT_API void Kit_Quit(); /** - * @brief Sets a librarywide hint + * @brief Sets a library-wide hint * * This can be used to set hints on how the library should behave. See Kit_HintType * for all the options. diff --git a/src/internal/kitdecoder.c b/src/internal/kitdecoder.c index e8093b6..c20bd86 100644 --- a/src/internal/kitdecoder.c +++ b/src/internal/kitdecoder.c @@ -17,7 +17,7 @@ Kit_Decoder* Kit_CreateDecoder(const Kit_Source *src, int stream_index, int thread_count) { assert(src != NULL); assert(out_b_size > 0); - assert(thread_count > 0); + assert(thread_count >= 0); AVCodecContext *codec_ctx = NULL; AVDictionary *codec_opts = NULL; @@ -61,9 +61,15 @@ Kit_Decoder* Kit_CreateDecoder(const Kit_Source *src, int stream_index, codec_ctx->pkt_timebase = format_ctx->streams[stream_index]->time_base; - // Set thread count + // Set threading, if possible codec_ctx->thread_count = thread_count; - codec_ctx->thread_type = FF_THREAD_SLICE|FF_THREAD_FRAME; + if(codec->capabilities | AV_CODEC_CAP_FRAME_THREADS) { + codec_ctx->thread_type = FF_THREAD_FRAME; + } else if(codec->capabilities | AV_CODEC_CAP_SLICE_THREADS) { + codec_ctx->thread_type = FF_THREAD_SLICE; + } else { + codec_ctx->thread_count = 1; // Disable threading + } // This is required for ass_process_chunk() av_dict_set(&codec_opts, "sub_text_format", "ass", 0); diff --git a/src/kitlib.c b/src/kitlib.c index fd4c2bd..4fa1298 100644 --- a/src/kitlib.c +++ b/src/kitlib.c @@ -89,7 +89,7 @@ void Kit_SetHint(Kit_HintType type, int value) { Kit_LibraryState *state = Kit_GetLibraryState(); switch(type) { case KIT_HINT_THREAD_COUNT: - state->thread_count = max(value, 1); + state->thread_count = max(value, 0); break; case KIT_HINT_FONT_HINTING: state->font_hinting = max(min(value, KIT_FONT_HINTING_COUNT), 0);