changeset 11919:5b0d7c668968 draft

API: 'x265_set_analysis_data' to capture analysis information
author Praveen Tiwari <praveen@multicorewareinc.com>
date Mon, 13 Nov 2017 12:20:50 +0530
parents 8b12db6c5f6b
children 06979c042350
files doc/reST/api.rst source/CMakeLists.txt source/encoder/api.cpp source/encoder/encoder.cpp source/encoder/encoder.h source/x265.def.in source/x265.h
diffstat 7 files changed, 112 insertions(+-), 1 deletions(-) [+]
line wrap: on
line diff
--- a/doc/reST/api.rst	Thu Nov 16 10:25:50 2017 +0530
+++ b/doc/reST/api.rst	Mon Nov 13 12:20:50 2017 +0530
@@ -215,6 +215,13 @@ changes made to the parameters for auto-
         *    the encoder will wait for this copy to complete if enabled.
         */
 
+**x265_set_analysis_data()** may be used to recive analysis information from external application::
+
+    /* x265_set_analysis_data:
+     *     set the analysis data,
+     *     returns negative on error, 0 access unit were output. */
+     int x265_set_analysis_data(x265_encoder *encoder, x265_analysis_data *analysis_data, int poc, uint32_t cuBytes);
+
 Pictures
 ========
 
--- a/source/CMakeLists.txt	Thu Nov 16 10:25:50 2017 +0530
+++ b/source/CMakeLists.txt	Mon Nov 13 12:20:50 2017 +0530
@@ -29,7 +29,7 @@ option(NATIVE_BUILD "Target the build CP
 option(STATIC_LINK_CRT "Statically link C runtime for release builds" OFF)
 mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)
 # X265_BUILD must be incremented each time the public API is changed
-set(X265_BUILD 145)
+set(X265_BUILD 146)
 configure_file("${PROJECT_SOURCE_DIR}/x265.def.in"
                "${PROJECT_BINARY_DIR}/x265.def")
 configure_file("${PROJECT_SOURCE_DIR}/x265_config.h.in"
--- a/source/encoder/api.cpp	Thu Nov 16 10:25:50 2017 +0530
+++ b/source/encoder/api.cpp	Mon Nov 13 12:20:50 2017 +0530
@@ -365,6 +365,18 @@ int x265_get_ref_frame_list(x265_encoder
     return encoder->getRefFrameList((PicYuv**)l0, (PicYuv**)l1, sliceType, poc);
 }
 
+int x265_set_analysis_data(x265_encoder *enc, x265_analysis_data *analysis_data, int poc, uint32_t cuBytes)
+{
+    if (!enc)
+        return -1;
+
+    Encoder *encoder = static_cast<Encoder*>(enc);
+    if (!encoder->setAnalysisData(analysis_data, poc, cuBytes))
+        return 0;
+
+    return -1;
+}
+
 void x265_cleanup(void)
 {
     BitCost::destroy();
@@ -444,6 +456,7 @@ static const x265_api libapi =
     &x265_csvlog_frame,
     &x265_csvlog_encode,
     &x265_dither_image,
+    &x265_set_analysis_data
 };
 
 typedef const x265_api* (*api_get_func)(int bitDepth);
--- a/source/encoder/encoder.cpp	Thu Nov 16 10:25:50 2017 +0530
+++ b/source/encoder/encoder.cpp	Mon Nov 13 12:20:50 2017 +0530
@@ -574,6 +574,88 @@ int Encoder::setAnalysisDataAfterZScan(x
     return 0;
 }
 
+int Encoder::setAnalysisData(x265_analysis_data *analysis_data, int poc, uint32_t cuBytes)
+{
+    uint32_t widthInCU = (m_param->sourceWidth + m_param->maxCUSize - 1) >> m_param->maxLog2CUSize;
+    uint32_t heightInCU = (m_param->sourceHeight + m_param->maxCUSize - 1) >> m_param->maxLog2CUSize;
+
+    Frame* curFrame = m_dpb->m_picList.getPOC(poc);
+    if (curFrame != NULL)
+    {
+        curFrame->m_analysisData = (*analysis_data);
+        curFrame->m_analysisData.numCUsInFrame = widthInCU * heightInCU;
+        curFrame->m_analysisData.numPartitions = m_param->num4x4Partitions;
+        allocAnalysis(&curFrame->m_analysisData);
+        if (m_param->maxCUSize == 16)
+        {
+            if (analysis_data->sliceType == X265_TYPE_IDR || analysis_data->sliceType == X265_TYPE_I)
+            {
+                curFrame->m_analysisData.sliceType = X265_TYPE_I;
+                if (m_param->analysisReuseLevel < 2)
+                    return -1;
+
+                curFrame->m_analysisData.numPartitions = m_param->num4x4Partitions;
+                size_t count = 0;
+                analysis_intra_data * currIntraData = (analysis_intra_data *)curFrame->m_analysisData.intraData;
+                analysis_intra_data * intraData = (analysis_intra_data *)analysis_data->intraData;
+                for (uint32_t d = 0; d < cuBytes; d++)
+                {
+                    int bytes = curFrame->m_analysisData.numPartitions >> ((intraData)->depth[d] * 2);
+                    memset(&(currIntraData)->depth[count], (intraData)->depth[d], bytes);
+                    memset(&(currIntraData)->chromaModes[count], (intraData)->chromaModes[d], bytes);
+                    memset(&(currIntraData)->partSizes[count], (intraData)->partSizes[d], bytes);
+                    memset(&(currIntraData)->partSizes[count], (intraData)->partSizes[d], bytes);
+                    count += bytes;
+                }
+                memcpy(&(currIntraData)->modes, (intraData)->modes, curFrame->m_analysisData.numPartitions * analysis_data->numCUsInFrame);
+            }
+            else
+            {
+                uint32_t numDir = analysis_data->sliceType == X265_TYPE_P ? 1 : 2;
+                if (m_param->analysisReuseLevel < 2)
+                    return -1;
+
+                curFrame->m_analysisData.numPartitions = m_param->num4x4Partitions;
+                size_t count = 0;
+                analysis_inter_data * currInterData = (analysis_inter_data *)curFrame->m_analysisData.interData;
+                analysis_inter_data * interData = (analysis_inter_data *)analysis_data->interData;
+                for (uint32_t d = 0; d < cuBytes; d++)
+                {
+                    int bytes = curFrame->m_analysisData.numPartitions >> ((interData)->depth[d] * 2);
+                    memset(&(currInterData)->depth[count], (interData)->depth[d], bytes);
+                    memset(&(currInterData)->modes[count], (interData)->modes[d], bytes);
+                    if (m_param->analysisReuseLevel > 4)
+                    {
+                        memset(&(currInterData)->partSize[count], (interData)->partSize[d], bytes);
+                        int numPU = nbPartsTable[(currInterData)->partSize[d]];
+                        for (int pu = 0; pu < numPU; pu++, d++)
+                        {
+                            (currInterData)->mergeFlag[count + pu] = (interData)->mergeFlag[d];
+                            if (m_param->analysisReuseLevel >= 7)
+                            {
+                                (currInterData)->interDir[count + pu] = (interData)->interDir[d];
+                                for (uint32_t i = 0; i < numDir; i++)
+                                {
+                                    (currInterData)->mvpIdx[i][count + pu] = (interData)->mvpIdx[i][d];
+                                    (currInterData)->refIdx[i][count + pu] = (interData)->refIdx[i][d];
+                                    memcpy(&(currInterData)->mv[i][count + pu], &(interData)->mv[i][d], sizeof(MV));
+                                }
+                            }
+                        }
+                    }
+                    count += bytes;
+                }
+            }
+        }
+        else
+            setAnalysisDataAfterZScan(analysis_data, curFrame);
+
+        curFrame->m_copyMVType.trigger();
+        return 0;
+    }
+    return -1;
+}
+
 void Encoder::destroy()
 {
 #if ENABLE_HDR10_PLUS
--- a/source/encoder/encoder.h	Thu Nov 16 10:25:50 2017 +0530
+++ b/source/encoder/encoder.h	Mon Nov 13 12:20:50 2017 +0530
@@ -212,6 +212,8 @@ public:
 
     int setAnalysisDataAfterZScan(x265_analysis_data *analysis_data, Frame* curFrame);
 
+    int setAnalysisData(x265_analysis_data *analysis_data, int poc, uint32_t cuBytes);
+
     void getStreamHeaders(NALList& list, Entropy& sbacCoder, Bitstream& bs);
 
     void fetchStats(x265_stats* stats, size_t statsSizeBytes);
--- a/source/x265.def.in	Thu Nov 16 10:25:50 2017 +0530
+++ b/source/x265.def.in	Mon Nov 13 12:20:50 2017 +0530
@@ -30,3 +30,4 @@ x265_csvlog_open
 x265_csvlog_frame
 x265_csvlog_encode
 x265_dither_image
+x265_set_analysis_data
--- a/source/x265.h	Thu Nov 16 10:25:50 2017 +0530
+++ b/source/x265.h	Mon Nov 13 12:20:50 2017 +0530
@@ -1746,6 +1746,11 @@ int x265_get_slicetype_poc_and_scenecut(
  *     This API must be called after(poc >= lookaheadDepth + bframes + 2) condition check */
 int x265_get_ref_frame_list(x265_encoder *encoder, x265_picyuv**, x265_picyuv**, int, int);
 
+/* x265_set_analysis_data:
+ *     set the analysis data,
+ *     returns negative on error, 0 access unit were output. */
+int x265_set_analysis_data(x265_encoder *encoder, x265_analysis_data *analysis_data, int poc, uint32_t cuBytes);
+
 void x265_cleanup(void);
 
 /* Open a CSV log file. On success it returns a file handle which must be passed
@@ -1819,6 +1824,7 @@ typedef struct x265_api
     void          (*csvlog_frame)(const x265_param*, const x265_picture*);
     void          (*csvlog_encode)(x265_encoder*, const x265_stats*, int, char**);
     void          (*dither_image)(x265_picture*, int, int, int16_t*, int);
+    int           (*set_analysis_data)(x265_encoder *encoder, x265_analysis_data *analysis_data, int poc, uint32_t cuBytes);
     /* add new pointers to the end, or increment X265_MAJOR_VERSION */
 } x265_api;