[x265] [PATCH] add fanout validation module to check param compatibility

sagar at multicorewareinc.com sagar at multicorewareinc.com
Tue Sep 16 09:36:54 CEST 2014


# HG changeset patch
# User Sagar Kotecha <sagar at multicorewareinc.com>
# Date 1410852729 -19800
#      Tue Sep 16 13:02:09 2014 +0530
# Node ID b9b5032d1608b04e6969cad794e9d31a07813168
# Parent  67ee212bbf78f7192e5c5af6b53304468bfa55b1
add fanout validation module to check param compatibility

diff -r 67ee212bbf78 -r b9b5032d1608 source/common/param.cpp
--- a/source/common/param.cpp	Mon Sep 15 16:09:52 2014 +0530
+++ b/source/common/param.cpp	Tue Sep 16 13:02:09 2014 +0530
@@ -1187,7 +1187,7 @@
 {
     char *buf, *s;
 
-    buf = s = X265_MALLOC(char, 2000);
+    buf = s = X265_MALLOC(char, MAXPARAMSIZE);
     if (!buf)
         return NULL;
 
diff -r 67ee212bbf78 -r b9b5032d1608 source/common/param.h
--- a/source/common/param.h	Mon Sep 15 16:09:52 2014 +0530
+++ b/source/common/param.h	Tue Sep 16 13:02:09 2014 +0530
@@ -38,6 +38,8 @@
 
 /* this table is kept internal to avoid confusion, since log level indices start at -1 */
 static const char * const logLevelNames[] = { "none", "error", "warning", "info", "debug", "full", 0 };
+
+#define MAXPARAMSIZE 2000
 }
 
 #endif // ifndef X265_PARAM_H
diff -r 67ee212bbf78 -r b9b5032d1608 source/x265.cpp
--- a/source/x265.cpp	Mon Sep 15 16:09:52 2014 +0530
+++ b/source/x265.cpp	Tue Sep 16 13:02:09 2014 +0530
@@ -227,6 +227,7 @@
     uint32_t framesToBeEncoded; // number of frames to encode
     uint64_t totalbytes;
     size_t   analysisRecordSize; // number of bytes read from or dumped into file
+    size_t   analysisHeaderSize;
 
     int64_t startTime;
     int64_t prevUpdateTime;
@@ -262,6 +263,7 @@
     bool parseQPFile(x265_picture &pic_org);
     void readAnalysisFile(x265_picture* pic, x265_param*);
     void writeAnalysisFile(x265_picture* pic, x265_param*);
+    bool validateFanout(x265_param*);
 };
 
 void CLIOptions::destroy()
@@ -755,6 +757,92 @@
     return false;
 }
 
+bool CLIOptions::validateFanout(x265_param *param)
+{
+#define CMP_OPT_FANOUT(opt, param_val)\
+    {\
+        bErr = 0;\
+        p = strstr(opts, opt "=");\
+        char* q = strstr(opts, "no-"opt);\
+        if (p && sscanf(p, opt "=%d" , &i) && param_val != i)\
+            bErr = 1;\
+        else if (!param_val && !q)\
+            bErr = 1;\
+        else if (param_val && (q || !strstr(opts, opt)))\
+            bErr = 1;\
+        if (bErr)\
+        {\
+            x265_log(param, X265_LOG_ERROR, "different " opt " setting than given in analysis file (%d vs %d)\n", param_val, i);\
+            X265_FREE(opts);\
+            return false;\
+        }\
+    }
+
+    char *p = NULL, *paramBuf, *opts;
+    int i, j;
+    uint32_t k , l;
+    bool bErr = false;
+
+    opts = paramBuf = X265_MALLOC(char, MAXPARAMSIZE);
+    if (!paramBuf)
+        return false;
+
+    fread(paramBuf, 1, MAXPARAMSIZE, this->analysisFile);
+
+    /* check whether fanout options are compatible */
+    if (strncmp(paramBuf, "#options:", 9))
+    {
+        x265_log(param, X265_LOG_ERROR, "options list in analysis file is not valid\n");
+        return false;
+    }
+
+    paramBuf = strchr(opts, '\n');
+    fseek(this->analysisFile, long(strlen(opts) + 1), SEEK_SET);
+    if (!paramBuf)
+    {
+        x265_log(param, X265_LOG_ERROR, "Malformed analysis file\n");
+        return false;
+    }
+
+    if (sscanf(opts, "#options: %dx%d", &i, &j) != 2)
+    {
+        x265_log(param, X265_LOG_ERROR, "Resolution specified in analysis file is not valid\n");
+        X265_FREE(opts);
+        return false;
+    }
+    if ((p = strstr(opts, " fps=")) == 0 || sscanf(p, " fps=%u/%u", &k, &l) != 2)
+    {
+        x265_log(param, X265_LOG_ERROR, "fps specified in analysis file is not valid\n");
+        X265_FREE(opts);
+        return false;
+    }
+    if (k != param->fpsNum || l != param->fpsDenom)
+    {
+        x265_log(param, X265_LOG_ERROR, "fps mismatch than given in analysis file (%u/%u vs %u/%u)\n",
+            param->fpsNum, param->fpsDenom, k, l);
+        X265_FREE(opts);
+        return false;
+    }
+
+    CMP_OPT_FANOUT("bitdepth", param->internalBitDepth);
+    CMP_OPT_FANOUT("weightp", param->bEnableWeightedPred);
+    CMP_OPT_FANOUT("bframes", param->bframes);
+    CMP_OPT_FANOUT("b-pyramid", param->bBPyramid);
+    CMP_OPT_FANOUT("b-adapt", param->bFrameAdaptive);
+    CMP_OPT_FANOUT("open-gop", param->bOpenGOP);
+    CMP_OPT_FANOUT("keyint", param->keyframeMax);
+    CMP_OPT_FANOUT("min-keyint", param->keyframeMin);
+    CMP_OPT_FANOUT("scenecut", param->scenecutThreshold);
+    CMP_OPT_FANOUT("ctu", (int)param->maxCUSize);
+    CMP_OPT_FANOUT("ref", param->maxNumReferences);
+    CMP_OPT_FANOUT("rc-lookahead", param->lookaheadDepth);
+
+#undef CMP_OPT_FANOUT
+
+    X265_FREE(opts);
+    return true;
+}
+
 void CLIOptions::readAnalysisFile(x265_picture* pic, x265_param* p)
 {
     int poc, width, height;
@@ -788,7 +876,7 @@
 
 void CLIOptions::writeAnalysisFile(x265_picture* pic, x265_param *p)
 {
-    uint64_t seekTo = pic->poc * this->analysisRecordSize;
+    uint64_t seekTo = pic->poc * this->analysisRecordSize + this->analysisHeaderSize;
     fseeko(this->analysisFile, seekTo, SEEK_SET);
     fwrite(&p->sourceWidth, sizeof(int), 1, this->analysisFile);
     fwrite(&p->sourceHeight, sizeof(int), 1, this->analysisFile);
@@ -912,6 +1000,26 @@
 
         cliopt.analysisRecordSize = ((sizeof(int) * 4 + sizeof(uint32_t) * 2) + sizeof(x265_inter_data) * numCU * 85 +
         sizeof(uint8_t) * 2 * numPart * numCU + sizeof(char) * numPart * numCU + sizeof(int) * numCU  + sizeof(uint32_t) * numCU);
+        if (param->analysisMode == X265_ANALYSIS_SAVE)
+        {
+            char *p = x265_param2string(param);
+            if (p)
+            {
+                fprintf(cliopt.analysisFile, "#options: %s\n", p);
+                cliopt.analysisHeaderSize = strlen(p) + 1 + strlen("#options: \n");
+                X265_FREE(p);
+            }
+            else
+            {
+                x265_log(NULL, X265_LOG_ERROR, "analysis: buffer allocation failure, aborting");
+                goto fail;
+            }
+        }
+        else
+        {
+            if (!cliopt.validateFanout(param))
+                goto fail;
+        }
     }
 
     if (cliopt.bDither)


More information about the x265-devel mailing list