Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 4bcbb0c

Browse files
jsunintellijing0010
authored andcommitted
Add the "-thread-count" parameter support (#330)
Signed-off-by: Jing Sun <[email protected]>
1 parent 11ade49 commit 4bcbb0c

File tree

8 files changed

+92
-24
lines changed

8 files changed

+92
-24
lines changed

Config/Sample.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,4 @@ NaluFile : Nalu.txt # SEI message. CEA 608/7
118118
AsmType : 1 # Assembly instruction set (0: non-AVX2, 1: up to AVX512 (Default: set based on platform capabilities))
119119
TargetSocket : -1 # For dual socket systems, this can specify which socket the encoder runs on (-1=Both Sockets, 0=Socket 0, 1=Socket 1)
120120
LogicalProcessors : 0 # The number of logical processor which encoder threads run on [0-N] (N is maximum number of logical processor)
121+
ThreadCount : 0 # The number of threads to get created and run [0-N] (0: Auto, 96: Min)

Docs/svt-hevc_encoder_user_guide.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ This token sets the number of logical processors which the encoder threads run o
231231
232232
For dual socket systems, this token specific which socket the encoder runs on.
233233

234+
>-thread-count integer **[Optional]**
235+
236+
This token sets the number of threads to get created and run.
234237

235238
For example, the following command encodes 100 frames of the YUV video sequence into the bin bit stream file. The picture is 1920 luma pixels wide and 1080 pixels high using the Sample.cfg configuration. The QP equals 30 and the md5 checksum is not included in the bit stream.
236239

@@ -313,6 +316,7 @@ The encoder parameters present in the Sample.cfg file are listed in this table b
313316
| **AsmType** | -asm | [0,1] | 1 | Assembly instruction set <br>(0: C Only, 1: Automatically select highest assembly instruction set supported) |
314317
| **LogicalProcessors** | -lp | [0, total number of logical processor] | 0 | The number of logical processor which encoder threads run on.Refer to Appendix A.2 |
315318
| **TargetSocket** | -ss | [-1,1] | -1 | For dual socket systems, this can specify which socket the encoder runs on.Refer to Appendix A.2 |
319+
| **ThreadCount** | -thread-count | [0,N] | 0 | The number of threads to get created and run, 0 = AUTO |
316320
| **SwitchThreadsToRtPriority** | -rt | [0,1] | 1 | Enables or disables threads to real time priority, 0 = OFF, 1 = ON (only works on Linux) |
317321
| **FPSInVPS** | -fpsinvps | [0,1] | 1 | Enables or disables the VPS timing info, 0 = OFF, 1 = ON |
318322
| **TileRowCount** | -tile_row_cnt | [1,16] | 1 | Tile count in the Row |

Source/API/EbApi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ extern "C" {
1818
#define EB_HME_SEARCH_AREA_COLUMN_MAX_COUNT 2
1919
#define EB_HME_SEARCH_AREA_ROW_MAX_COUNT 2
2020

21+
#define EB_THREAD_COUNT_MIN_CORE 48
22+
#define EB_THREAD_COUNT_FACTOR 2
23+
2124
#ifdef _WIN32
2225
#define EB_API __declspec(dllexport)
2326
#else
@@ -560,6 +563,8 @@ typedef struct EB_H265_ENC_CONFIGURATION
560563
* Default is 1. */
561564
uint8_t switchThreadsToRtPriority;
562565

566+
/* The total number of working threads to create. */
567+
uint32_t threadCount;
563568

564569
// ASM Type
565570

Source/App/EbAppConfig.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#define ASM_TYPE_TOKEN "-asm" // no Eval
100100
#define THREAD_MGMNT "-lp"
101101
#define TARGET_SOCKET "-ss"
102+
#define THREAD_COUNT "-thread-count"
102103
#define SWITCHTHREADSTOREALTIME_TOKEN "-rt"
103104
#define FPSINVPS_TOKEN "-fpsinvps"
104105
#define UNRESTRICTED_MOTION_VECTOR "-umv"
@@ -265,6 +266,7 @@ static void SetAsmType (const char *value, EbConfig_t *
265266
static void SetLogicalProcessors (const char *value, EbConfig_t *cfg) {cfg->logicalProcessors = (uint32_t)strtoul(value, NULL, 0);};
266267
static void SetTargetSocket (const char *value, EbConfig_t *cfg) {cfg->targetSocket = (int32_t)strtol(value, NULL, 0);};
267268
static void SetSwitchThreadsToRtPriority (const char *value, EbConfig_t *cfg) {cfg->switchThreadsToRtPriority = (EB_BOOL)strtol(value, NULL, 0);};
269+
static void SetThreadCount (const char *value, EbConfig_t *cfg) {cfg->threadCount = (uint32_t)strtoul(value, NULL, 0); };
268270
static void SetFpsInVps (const char *value, EbConfig_t *cfg) {cfg->fpsInVps = (EB_BOOL)strtol(value, NULL, 0);};
269271
static void SetUnrestrictedMotionVector (const char *value, EbConfig_t *cfg) {cfg->unrestrictedMotionVector = (EB_BOOL)strtol(value, NULL, 0);};
270272

@@ -415,6 +417,7 @@ config_entry_t config_entry[] = {
415417
{ SINGLE_INPUT, ASM_TYPE_TOKEN, "AsmType", SetAsmType },
416418
{ SINGLE_INPUT, TARGET_SOCKET, "TargetSocket", SetTargetSocket },
417419
{ SINGLE_INPUT, THREAD_MGMNT, "LogicalProcessors", SetLogicalProcessors },
420+
{ SINGLE_INPUT, THREAD_COUNT, "ThreadCount", SetThreadCount },
418421

419422
// Termination
420423
{ SINGLE_INPUT, NULL, NULL, NULL }
@@ -557,6 +560,7 @@ void EbConfigCtor(EbConfig_t *configPtr)
557560
configPtr->asmType = 1;
558561
configPtr->targetSocket = -1;
559562
configPtr->logicalProcessors = 0;
563+
configPtr->threadCount = 0;
560564

561565
// vbv
562566
configPtr->vbvMaxRate = 0;

Source/App/EbAppConfig.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,15 @@ typedef struct EbConfig_s
376376
/****************************************
377377
* Instance Info
378378
****************************************/
379-
uint32_t channelId;
380-
uint32_t activeChannelCount;
381-
uint32_t logicalProcessors;
382-
int32_t targetSocket;
383-
EB_BOOL stopEncoder; // to signal CTRL+C Event, need to stop encoding.
384-
385-
uint64_t processedFrameCount;
386-
uint64_t processedByteCount;
379+
uint32_t channelId;
380+
uint32_t activeChannelCount;
381+
uint32_t logicalProcessors;
382+
int32_t targetSocket;
383+
uint32_t threadCount;
384+
EB_BOOL stopEncoder; // to signal CTRL+C Event, need to stop encoding.
385+
386+
uint64_t processedFrameCount;
387+
uint64_t processedByteCount;
387388

388389
/****************************************
389390
* SEI parameters

Source/App/EbAppContext.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,21 @@ EB_ERRORTYPE CopyConfigurationParameters(
207207
callbackData->ebEncParameters.activeChannelCount = config->activeChannelCount;
208208
callbackData->ebEncParameters.logicalProcessors = config->logicalProcessors;
209209
callbackData->ebEncParameters.targetSocket = config->targetSocket;
210+
if ((config->threadCount > 0) && (config->threadCount < EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_FACTOR)) {
211+
callbackData->ebEncParameters.threadCount = EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_FACTOR;
212+
printf("\nWarning: the thread count %u is set too small and is forced to the min value %u\n",
213+
config->threadCount, callbackData->ebEncParameters.threadCount);
214+
} else {
215+
callbackData->ebEncParameters.threadCount = (config->threadCount + EB_THREAD_COUNT_MIN_CORE - 1)
216+
/ EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_MIN_CORE;
217+
if (callbackData->ebEncParameters.threadCount != config->threadCount)
218+
printf("\nInformation: the thread count %u is rounded to %u\n",
219+
config->threadCount, callbackData->ebEncParameters.threadCount);
220+
}
221+
210222
callbackData->ebEncParameters.unrestrictedMotionVector = config->unrestrictedMotionVector;
211-
callbackData->ebEncParameters.bitRateReduction = (uint8_t)config->bitRateReduction;
212-
callbackData->ebEncParameters.improveSharpness = (uint8_t)config->improveSharpness;
223+
callbackData->ebEncParameters.bitRateReduction = (uint8_t)config->bitRateReduction;
224+
callbackData->ebEncParameters.improveSharpness = (uint8_t)config->improveSharpness;
213225
callbackData->ebEncParameters.videoUsabilityInfo = config->videoUsabilityInfo;
214226
callbackData->ebEncParameters.highDynamicRangeInput = config->highDynamicRangeInput;
215227
callbackData->ebEncParameters.accessUnitDelimiter = config->accessUnitDelimiter;

Source/Lib/Codec/EbEncHandle.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,9 +2105,14 @@ void LoadDefaultBufferConfigurationSettings(
21052105

21062106
unsigned int lpCount = GetNumProcessors();
21072107
unsigned int coreCount = lpCount;
2108+
2109+
unsigned int totalThreadCount;
2110+
unsigned int threadUnit;
2111+
21082112
#if defined(_WIN32) || defined(__linux__)
21092113
if (sequenceControlSetPtr->staticConfig.targetSocket != -1)
21102114
coreCount /= numGroups;
2115+
21112116
if (sequenceControlSetPtr->staticConfig.logicalProcessors != 0)
21122117
coreCount = sequenceControlSetPtr->staticConfig.logicalProcessors < coreCount ?
21132118
sequenceControlSetPtr->staticConfig.logicalProcessors: coreCount;
@@ -2127,6 +2132,23 @@ void LoadDefaultBufferConfigurationSettings(
21272132
coreCount = lpCount;
21282133
#endif
21292134

2135+
// Thread count computation
2136+
if (sequenceControlSetPtr->staticConfig.threadCount != 0)
2137+
totalThreadCount = sequenceControlSetPtr->staticConfig.threadCount;
2138+
else
2139+
totalThreadCount = coreCount * EB_THREAD_COUNT_FACTOR;
2140+
2141+
if (totalThreadCount < EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_FACTOR) {
2142+
coreCount = EB_THREAD_COUNT_MIN_CORE;
2143+
totalThreadCount = coreCount * EB_THREAD_COUNT_FACTOR;
2144+
}
2145+
2146+
if (totalThreadCount % EB_THREAD_COUNT_MIN_CORE) {
2147+
totalThreadCount = (totalThreadCount + EB_THREAD_COUNT_MIN_CORE - 1)
2148+
/ EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_MIN_CORE;
2149+
}
2150+
threadUnit = totalThreadCount / EB_THREAD_COUNT_MIN_CORE;
2151+
21302152
sequenceControlSetPtr->inputOutputBufferFifoInitCount = inputPic + SCD_LAD;
21312153

21322154
// ME segments
@@ -2202,14 +2224,15 @@ void LoadDefaultBufferConfigurationSettings(
22022224

22032225
//#====================== Processes number ======================
22042226
sequenceControlSetPtr->totalProcessInitCount = 0;
2205-
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->pictureAnalysisProcessInitCount = MAX(15, coreCount / 6);
2206-
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->motionEstimationProcessInitCount = MAX(20, coreCount / 3);
2207-
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->sourceBasedOperationsProcessInitCount = MAX(3, coreCount / 12);
2208-
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->modeDecisionConfigurationProcessInitCount = MAX(3, coreCount / 12);
2209-
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->encDecProcessInitCount = MAX(40, coreCount);
2210-
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->entropyCodingProcessInitCount = MAX(3, coreCount / 6);
2211-
2227+
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->pictureAnalysisProcessInitCount = threadUnit * 4;
2228+
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->motionEstimationProcessInitCount = threadUnit * 8;
2229+
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->sourceBasedOperationsProcessInitCount = threadUnit * 2;
2230+
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->modeDecisionConfigurationProcessInitCount = threadUnit * 2;
2231+
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->entropyCodingProcessInitCount = threadUnit * 4;
22122232
sequenceControlSetPtr->totalProcessInitCount += 6; // single processes count
2233+
sequenceControlSetPtr->totalProcessInitCount += sequenceControlSetPtr->encDecProcessInitCount =
2234+
totalThreadCount - sequenceControlSetPtr->totalProcessInitCount;
2235+
22132236
SVT_LOG("Number of logical cores available: %u\nNumber of PPCS %u\n", coreCount, inputPic);
22142237

22152238
return;
@@ -3238,10 +3261,10 @@ EB_ERRORTYPE EbH265EncInitParameter(
32383261
// ASM Type
32393262
configPtr->asmType = 1;
32403263

3241-
32423264
// Channel info
32433265
configPtr->logicalProcessors = 0;
32443266
configPtr->targetSocket = -1;
3267+
configPtr->threadCount = 0;
32453268
configPtr->channelId = 0;
32463269
configPtr->activeChannelCount = 1;
32473270

ffmpeg_plugin/0001-lavc-svt_hevc-add-libsvt-hevc-encoder-wrapper.patch

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 80e3f410d1ad8807bd4a9f4d5cdf7a7b3c5962ce Mon Sep 17 00:00:00 2001
1+
From 3248aee7fc94304cd5f41afa8dc502d92ae080ef Mon Sep 17 00:00:00 2001
22
From: Jing Sun <[email protected]>
33
Date: Wed, 21 Nov 2018 11:33:04 +0800
44
Subject: [PATCH 1/1] lavc/svt_hevc: add libsvt hevc encoder wrapper
@@ -11,8 +11,8 @@ Signed-off-by: Jing Sun <[email protected]>
1111
configure | 4 +
1212
libavcodec/Makefile | 1 +
1313
libavcodec/allcodecs.c | 1 +
14-
libavcodec/libsvt_hevc.c | 497 +++++++++++++++++++++++++++++++++++++++++++++++
15-
4 files changed, 503 insertions(+)
14+
libavcodec/libsvt_hevc.c | 515 +++++++++++++++++++++++++++++++++++++++++++++++
15+
4 files changed, 521 insertions(+)
1616
create mode 100644 libavcodec/libsvt_hevc.c
1717

1818
diff --git a/configure b/configure
@@ -77,10 +77,10 @@ index d2f9a39..d8788a7 100644
7777
extern AVCodec ff_libvo_amrwbenc_encoder;
7878
diff --git a/libavcodec/libsvt_hevc.c b/libavcodec/libsvt_hevc.c
7979
new file mode 100644
80-
index 0000000..24ab0ff
80+
index 0000000..77eae48
8181
--- /dev/null
8282
+++ b/libavcodec/libsvt_hevc.c
83-
@@ -0,0 +1,497 @@
83+
@@ -0,0 +1,515 @@
8484
+/*
8585
+* Scalable Video Technology for HEVC encoder library plugin
8686
+*
@@ -142,6 +142,7 @@ index 0000000..24ab0ff
142142
+ int asm_type;
143143
+ int forced_idr;
144144
+ int la_depth;
145+
+ int thread_count;
145146
+} SvtContext;
146147
+
147148
+static int error_mapping(EB_ERRORTYPE svt_ret)
@@ -282,6 +283,20 @@ index 0000000..24ab0ff
282283
+ if (svt_enc->la_depth != -1)
283284
+ param->lookAheadDistance = svt_enc->la_depth;
284285
+
286+
+ if ((svt_enc->thread_count > 0) &&
287+
+ (svt_enc->thread_count < (EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_FACTOR))) {
288+
+ param->threadCount = EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_FACTOR;
289+
+ av_log(avctx, AV_LOG_WARNING, "Thread count is set too small, forced to %"PRId32"\n",
290+
+ param->threadCount);
291+
+ } else if (svt_enc->thread_count % EB_THREAD_COUNT_MIN_CORE) {
292+
+ param->threadCount = (svt_enc->thread_count + EB_THREAD_COUNT_MIN_CORE - 1)
293+
+ / EB_THREAD_COUNT_MIN_CORE * EB_THREAD_COUNT_MIN_CORE;
294+
+ av_log(avctx, AV_LOG_DEBUG, "Thread count is rounded to %"PRId32"\n",
295+
+ param->threadCount);
296+
+ } else {
297+
+ param->threadCount = svt_enc->thread_count;
298+
+ }
299+
+
285300
+ if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)
286301
+ param->codeVpsSpsPps = 0;
287302
+ else
@@ -525,6 +540,9 @@ index 0000000..24ab0ff
525540
+ { "sc_detection", "Scene change detection", OFFSET(scd),
526541
+ AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
527542
+
543+
+ { "thread_count", "Number of threads [0: Auto, 96: Min]", OFFSET(thread_count),
544+
+ AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE },
545+
+
528546
+ { "tier", "Set tier (general_tier_flag)", OFFSET(tier),
529547
+ AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "tier" },
530548
+ { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, VE, "tier" },
@@ -565,7 +583,7 @@ index 0000000..24ab0ff
565583
+ .init = eb_enc_init,
566584
+ .encode2 = eb_encode_frame,
567585
+ .close = eb_enc_close,
568-
+ .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
586+
+ .capabilities = AV_CODEC_CAP_DELAY,
569587
+ .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
570588
+ AV_PIX_FMT_YUV420P10,
571589
+ AV_PIX_FMT_YUV422P,

0 commit comments

Comments
 (0)