Skip to content

Commit

Permalink
Merge pull request #70 from katajakasa/threading-fixes
Browse files Browse the repository at this point in the history
Add option for letting ffmpeg decide thread count
  • Loading branch information
katajakasa authored Aug 2, 2022
2 parents 14579d1 + 89f3598 commit d830bb7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/example_complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions include/kitchensink/kitlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions src/internal/kitdecoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/kitlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d830bb7

Please sign in to comment.