<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">From 24f9dea36f39856a4642263ec86ccaf757003150 Mon Sep 17 00:00:00 2001<br>From: Min Chen <<a href="mailto:chenm003@163.com">chenm003@163.com</a>><br>Date: Thu, 14 Nov 2024 21:53:51 -0800<br>Subject: [PATCH] Fix bug with --abr-ladder<br><br>---<br> source/abrEncApp.cpp       |  2 ++<br> source/common/frame.cpp    |  2 +-<br> source/encoder/api.cpp     |  2 +-<br> source/encoder/encoder.cpp |  1 +<br> source/x265.cpp            | 17 +++++++++++------<br> source/x265cli.cpp         |  9 +++------<br> source/x265cli.h           |  5 +++++<br> 7 files changed, 24 insertions(+), 14 deletions(-)<br><br>diff --git a/source/abrEncApp.cpp b/source/abrEncApp.cpp<br>index 17dcf9045..1907e0bfb 100644<br>--- a/source/abrEncApp.cpp<br>+++ b/source/abrEncApp.cpp<br>@@ -172,6 +172,7 @@ namespace X265_NS {<br>             {<br>                 X265_FREE(m_inputPicBuffer[pass][index]->planes[0]);<br>                 x265_picture_free(m_inputPicBuffer[pass][index]);<br>+                X265_FREE(m_analysisBuffer[pass][index].wt);<br>             }<br>             X265_FREE(m_inputPicBuffer[pass]);<br> <br>@@ -388,6 +389,7 @@ namespace X265_NS {<br> <br>         x265_free_analysis_data(m_param, m_analysisInfo);<br>         memcpy(m_analysisInfo, src, sizeof(x265_analysis_data));<br>+        m_analysisInfo->wt = NULL;<br>         x265_alloc_analysis_data(m_param, m_analysisInfo);<br> <br>         bool isVbv = m_param->rc.vbvBufferSize && m_param->rc.vbvMaxBitrate;<br>diff --git a/source/common/frame.cpp b/source/common/frame.cpp<br>index f497979f1..3a98c23d7 100644<br>--- a/source/common/frame.cpp<br>+++ b/source/common/frame.cpp<br>@@ -132,7 +132,7 @@ bool Frame::create(x265_param *param, float* quantOffsets)<br>         }<br>     }<br> <br>-    if (param->bAnalysisType == AVC_INFO)<br>+    //if (param->bAnalysisType == AVC_INFO)<br>     {<br>         m_analysisData.wt = NULL;<br>         m_analysisData.intraData = NULL;<br>diff --git a/source/encoder/api.cpp b/source/encoder/api.cpp<br>index 3ea67168d..4c70ec3af 100644<br>--- a/source/encoder/api.cpp<br>+++ b/source/encoder/api.cpp<br>@@ -933,7 +933,7 @@ void x265_free_analysis_data(x265_param *param, x265_analysis_data* analysis)<br> <br>     /* Early exit freeing weights alone if level is 1 (when there is no analysis inter/intra) */<br>     if (!isMultiPassOpt && analysis->wt && !(param->bAnalysisType == AVC_INFO))<br>-        X265_FREE(analysis->wt);<br>+        X265_FREE_ZERO(analysis->wt);<br> <br>     //Free memory for intraData pointers<br>     if (analysis->intraData)<br>diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp<br>index 609264067..e85e09d82 100644<br>--- a/source/encoder/encoder.cpp<br>+++ b/source/encoder/encoder.cpp<br>@@ -2361,6 +2361,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>             if (strlen(m_param->analysisSave) && !strlen(m_param->analysisLoad))<br>             {<br>                 x265_analysis_data* analysis = &frameEnc[0]->m_analysisData;<br>+                memset(analysis, 0, sizeof(x265_analysis_data));<br>                 analysis->poc = frameEnc[0]->m_poc;<br>                 analysis->sliceType = frameEnc[0]->m_lowres.sliceType;<br>                 uint32_t widthInCU       = (m_param->sourceWidth  + m_param->maxCUSize - 1) >> m_param->maxLog2CUSize;<br>diff --git a/source/x265.cpp b/source/x265.cpp<br>index f392d27bf..357f5d6ea 100644<br>--- a/source/x265.cpp<br>+++ b/source/x265.cpp<br>@@ -154,11 +154,13 @@ static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEnco<br>     char line[1024];<br>     char* argLine;<br> <br>-    char **argv = (char**)alloca(256 * sizeof(char *));<br>-    char *strPool = (char*)alloca(256 * X265_MAX_STRING_SIZE * sizeof(char));<br>+    char **argv = (char**)malloc(256 * sizeof(char *));<br>+    char *strPool = (char*)malloc(256 * X265_MAX_STRING_SIZE * sizeof(char));<br>     int strPoolSize = 256 * X265_MAX_STRING_SIZE;<br>     for (uint32_t i = 0; i < numEncodes; i++)<br>     {<br>+        cliopt[i].stringPool = (i == 0 ? strPool : NULL);<br>+        cliopt[i].argString = (i == 0 ? argv : NULL);<br>         if (fgets(line, sizeof(line), abrConfig) == NULL) {<br>             fprintf(stderr, "Error reading line from configuration file.\n");<br>             return false;<br>@@ -203,11 +205,12 @@ static bool parseAbrConfig(FILE* abrConfig, CLIOptions cliopt[], uint8_t numEnco<br>         char* token = strtok(start, " ");<br>         while (token)<br>         {<br>-            argv[argc++] = strPool;<br>-            strPool += strlen(token);<br>-            strPoolSize -= strlen(token);<br>-            strcpy(argv[argc++], token);<br>+            argv[argc] = strPool;<br>+            strPool += strlen(token) + 1;<br>+            strPoolSize -= strlen(token) + 1;<br>+            strcpy(argv[argc], token);<br>             token = strtok(NULL, " ");<br>+            argc++;<br>         }<br>         argv[argc] = NULL;<br>         if (cliopt[i].parse(argc++, argv))<br>@@ -287,6 +290,8 @@ int main(int argc, char **argv)<br>         numEncodes = getNumAbrEncodes(abrConfig);<br> <br>     CLIOptions* cliopt = new CLIOptions[numEncodes];<br>+    cliopt[0].orgArgv = argv;<br>+    cliopt[0].argString = argv;<br> <br>     if (isAbrLadder)<br>     {<br>diff --git a/source/x265cli.cpp b/source/x265cli.cpp<br>index 5bd83a143..f8cdf759a 100755<br>--- a/source/x265cli.cpp<br>+++ b/source/x265cli.cpp<br>@@ -447,12 +447,10 @@ namespace X265_NS {<br> <br>     void CLIOptions::destroy()<br>     {<br>-        if (isAbrLadderConfig)<br>-        {<br>-            for (int idx = 1; idx < argCnt; idx++)<br>-                free(argString[idx]);<br>+        if(argString && argString != orgArgv)<br>             free(argString);<br>-        }<br>+        if (stringPool)<br>+            free(stringPool);<br> <br>         for (int i = 0; i < MAX_VIEWS; i++)<br>         {<br>@@ -637,7 +635,6 @@ namespace X265_NS {<br>         const char *profile = NULL;<br>         int svtEnabled = 0;<br>         argCnt = argc;<br>-        argString = argv;<br> <br>         if (argc <= 1)<br>         {<br>diff --git a/source/x265cli.h b/source/x265cli.h<br>index 86fd14caf..108cc8e2d 100644<br>--- a/source/x265cli.h<br>+++ b/source/x265cli.h<br>@@ -431,7 +431,9 @@ static const struct option long_options[] =<br>         int64_t prevUpdateTime;<br> <br>         int argCnt;<br>+        char** orgArgv;<br>         char** argString;<br>+        char *stringPool;<br> <br>         /* ABR ladder settings */<br>         bool isAbrLadderConfig;<br>@@ -481,6 +483,9 @@ static const struct option long_options[] =<br>             saveLevel = 0;<br>             numRefs = 0;<br>             argCnt = 0;<br>+            orgArgv = NULL;<br>+            argString = NULL;<br>+            stringPool = NULL;<br>         }<br> <br>         void destroy();<br>-- <br>2.41.0.windows.1<br><br></div></div>