<div dir="ltr"><div># HG changeset patch</div><div># User ZhengWang <<a href="mailto:zheng@multicorewareinc.com">zheng@multicorewareinc.com</a>></div><div># Date 1477292081 -28800</div><div>#      Mon Oct 24 14:54:41 2016 +0800</div><div># Node ID 19d3c5ea8bc10d3728ba2ae06190d188fe6093a3</div><div># Parent  0e9e5264054606a38a3fe6c87272a1737b340b1a</div><div>Store commonly-used RPS in SPS  in 2 pass mode.</div><div>Add new param --[no]-multi-pass-opt-rps to control it, default disabled.</div><div><br></div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 doc/reST/cli.rst</div><div>--- a/doc/reST/cli.rst<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/doc/reST/cli.rst<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -1852,6 +1852,10 @@</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">  </span>Discard optional VUI HRD info from bitstream. Default enabled when</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">   </span>:option:`--hrd` is enabled.</div><div> </div><div>+.. option:: --[no]-multi-pass-opt-rps</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Enable storing commonly RPS in SPS in multi pass mode. Default disabled.</div><div>+</div><div> </div><div> Debugging options</div><div> =================</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/CMakeLists.txt</div><div>--- a/source/CMakeLists.txt<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/CMakeLists.txt<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -30,7 +30,7 @@</div><div> mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)</div><div> </div><div> # X265_BUILD must be incremented each time the public API is changed</div><div>-set(X265_BUILD 98)</div><div>+set(X265_BUILD 99)</div><div> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265.def.in">x265.def.in</a>"</div><div>                "${PROJECT_BINARY_DIR}/x265.def")</div><div> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265_config.h.in">x265_config.h.in</a>"</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/common/common.h</div><div>--- a/source/common/common.h<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/common/common.h<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -312,6 +312,7 @@</div><div> </div><div> #define MAX_NUM_REF_PICS            16 // max. number of pictures used for reference</div><div> #define MAX_NUM_REF                 16 // max. number of entries in picture reference list</div><div>+#define MAX_NUM_SHORT_TERM_RPS      64 // max. number of short term reference picture set in SPS</div><div> </div><div> #define REF_NOT_VALID               -1</div><div> </div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/common/framedata.cpp</div><div>--- a/source/common/framedata.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/common/framedata.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -37,6 +37,9 @@</div><div>     m_slice  = new Slice;</div><div>     m_picCTU = new CUData[sps.numCUsInFrame];</div><div>     m_picCsp = csp;</div><div>+    m_spsrpsIdx = -1;</div><div>+    if (param.rc.bStatWrite)</div><div>+        m_spsrps = const_cast<RPS*>(sps.spsrps);</div><div> </div><div>     m_cuMemPool.create(0, param.internalCsp, sps.numCUsInFrame);</div><div>     for (uint32_t ctuAddr = 0; ctuAddr < sps.numCUsInFrame; ctuAddr++)</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/common/framedata.h</div><div>--- a/source/common/framedata.h<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/common/framedata.h<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -106,6 +106,9 @@</div><div>     CUDataMemPool  m_cuMemPool;</div><div>     CUData*        m_picCTU;</div><div> </div><div>+    RPS*           m_spsrps;</div><div>+    int            m_spsrpsIdx;</div><div>+</div><div>     /* Rate control data used during encode and by references */</div><div>     struct RCStatCU</div><div>     {</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/common/param.cpp</div><div>--- a/source/common/param.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/common/param.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -198,6 +198,7 @@</div><div>     param->bCULossless = 0;</div><div>     param->bEnableTemporalSubLayers = 0;</div><div>     param->bEnableRdRefine = 0;</div><div>+    param->bMultiPassOptRPS = 0;</div><div> </div><div>     /* Rate control options */</div><div>     param->rc.vbvMaxBitrate = 0;</div><div>@@ -915,6 +916,8 @@</div><div>         OPT("limit-tu") p->limitTU = atoi(value);</div><div>         OPT("opt-qp-pps") p->bOptQpPPS = atobool(value);</div><div>         OPT("opt-ref-list-length-pps") p->bOptRefListLengthPPS = atobool(value);</div><div>+        OPT("multi-pass-opt-rps") p->bMultiPassOptRPS = atobool(value);</div><div>+</div><div>         else</div><div>             return X265_PARAM_BAD_NAME;</div><div>     }</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/common/slice.h</div><div>--- a/source/common/slice.h<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/common/slice.h<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -239,6 +239,10 @@</div><div>     uint32_t maxLatencyIncrease;</div><div>     int      numReorderPics;</div><div> </div><div>+    RPS      spsrps[MAX_NUM_SHORT_TERM_RPS];</div><div>+    int      spsrpsNum;</div><div>+    int      numGOPBegin;</div><div>+</div><div>     bool     bUseSAO; // use param</div><div>     bool     bUseAMP; // use param</div><div>     bool     bUseStrongIntraSmoothing; // use param</div><div>@@ -337,6 +341,7 @@</div><div>     int         m_sliceQp;</div><div>     int         m_poc;</div><div>     int         m_lastIDR;</div><div>+    int         m_rpsIdx;</div><div> </div><div>     uint32_t    m_colRefIdx;       // never modified</div><div> </div><div>@@ -352,6 +357,7 @@</div><div> </div><div>     int         m_iPPSQpMinus26;</div><div>     int         numRefIdxDefault[2];</div><div>+    int         m_iNumRPSInSPS;</div><div> </div><div>     Slice()</div><div>     {</div><div>@@ -365,6 +371,7 @@</div><div>         m_iPPSQpMinus26 = 0;</div><div>         numRefIdxDefault[0] = 1;</div><div>         numRefIdxDefault[1] = 1;</div><div>+        m_rpsIdx = -1;</div><div>     }</div><div> </div><div>     void disableWeights();</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/api.cpp</div><div>--- a/source/encoder/api.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/api.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -141,6 +141,11 @@</div><div>         Encoder *encoder = static_cast<Encoder*>(enc);</div><div>         Entropy sbacCoder;</div><div>         Bitstream bs;</div><div>+        if (encoder->m_param->rc.bStatRead && encoder->m_param->bMultiPassOptRPS)</div><div>+        {</div><div>+            if (!encoder->computeSPSRPSIndex())</div><div>+                return -1;</div><div>+        }</div><div>         encoder->getStreamHeaders(encoder->m_nalList, sbacCoder, bs);</div><div>         *pp_nal = &encoder->m_nalList.m_nal[0];</div><div>         if (pi_nal) *pi_nal = encoder->m_nalList.m_numNal;</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/encoder.cpp</div><div>--- a/source/encoder/encoder.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/encoder.cpp<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -77,6 +77,7 @@</div><div>     m_iFrameNum = 0;</div><div>     m_iPPSQpMinus26 = 0;</div><div>     m_iLastSliceQp = 0;</div><div>+    m_rpsInSpsCount = 0;</div><div>     for (int i = 0; i < X265_MAX_FRAME_THREADS; i++)</div><div>         m_frameEncoder[i] = NULL;</div><div> </div><div>@@ -905,6 +906,7 @@</div><div>             frameEnc->m_encData->m_slice->m_iPPSQpMinus26 = m_iPPSQpMinus26;</div><div>             frameEnc->m_encData->m_slice->numRefIdxDefault[0] = m_pps.numRefIdxDefault[0];</div><div>             frameEnc->m_encData->m_slice->numRefIdxDefault[1] = m_pps.numRefIdxDefault[1];</div><div>+            frameEnc->m_encData->m_slice->m_iNumRPSInSPS = m_sps.spsrpsNum;</div><div> </div><div>             curEncoder->m_rce.encodeOrder = frameEnc->m_encodeOrder = m_encodedFrameNum++;</div><div>             if (m_bframeDelay)</div><div>@@ -1068,6 +1070,13 @@</div><div> </div><div>         x265_log(m_param, X265_LOG_INFO, "lossless compression ratio %.2f::1\n", uncompressed / m_analyzeAll.m_accBits);</div><div>     }</div><div>+    if (m_param->bMultiPassOptRPS && m_param->rc.bStatRead)</div><div>+    {</div><div>+        x265_log(m_param, X265_LOG_INFO, "RPS in SPS: %d frames (%.2f%%), RPS not in SPS: %d frames (%.2f%%)\n", </div><div>+            m_rpsInSpsCount, (float)100.0 * m_rpsInSpsCount / m_rateControl->m_numEntries, </div><div>+            m_rateControl->m_numEntries - m_rpsInSpsCount, </div><div>+            (float)100.0 * (m_rateControl->m_numEntries - m_rpsInSpsCount) / m_rateControl->m_numEntries);</div><div>+    }</div><div> </div><div>     if (m_analyzeAll.m_numPics)</div><div>     {</div><div>@@ -2433,3 +2442,203 @@</div><div>     TOOLCMP(oldParam->maxNumMergeCand, newParam->maxNumMergeCand, "max-merge=%d to %d\n");</div><div>     TOOLCMP(oldParam->bIntraInBFrames, newParam->bIntraInBFrames, "b-intra=%d to %d\n");</div><div> }</div><div>+</div><div>+bool Encoder::computeSPSRPSIndex()</div><div>+{</div><div>+    RPS* rpsInSPS = m_sps.spsrps;</div><div>+    int* rpsNumInPSP = &m_sps.spsrpsNum;</div><div>+    int  beginNum = m_sps.numGOPBegin;</div><div>+    int  endNum;</div><div>+    RPS* rpsInRec;</div><div>+    RPS* rpsInIdxList;</div><div>+    RPS* thisRpsInSPS;</div><div>+    RPS* thisRpsInList;</div><div>+    RPSListNode* headRpsIdxList = NULL;</div><div>+    RPSListNode* tailRpsIdxList = NULL;</div><div>+    RPSListNode* rpsIdxListIter = NULL;</div><div>+    RateControlEntry *rce2Pass = m_rateControl->m_rce2Pass;</div><div>+    int numEntries = m_rateControl->m_numEntries;</div><div>+    RateControlEntry *rce;</div><div>+    int idx = 0;</div><div>+    int pos = 0;</div><div>+    int resultIdx[64];</div><div>+    memset(rpsInSPS, 0, sizeof(RPS) * MAX_NUM_SHORT_TERM_RPS);</div><div>+</div><div>+    // find out all RPS date in current GOP</div><div>+    beginNum++;</div><div>+    endNum = beginNum;</div><div>+    if (!m_param->bRepeatHeaders)</div><div>+    {</div><div>+        endNum = numEntries;</div><div>+    }</div><div>+    else</div><div>+    {</div><div>+        while (endNum < numEntries)</div><div>+        {</div><div>+            rce = &rce2Pass[endNum];</div><div>+            if (rce->sliceType == I_SLICE)</div><div>+            {</div><div>+                break;</div><div>+            }</div><div>+            endNum++;</div><div>+        }</div><div>+    }</div><div>+    m_sps.numGOPBegin = endNum;</div><div>+</div><div>+    // find out all kinds of RPS</div><div>+    for (int i = beginNum; i < endNum; i++)</div><div>+    {</div><div>+        rce = &rce2Pass[i];</div><div>+        rpsInRec = &rce->rpsData;</div><div>+        rpsIdxListIter = headRpsIdxList;</div><div>+        // i frame don't recode RPS info</div><div>+        if (rce->sliceType != I_SLICE)</div><div>+        {</div><div>+            while (rpsIdxListIter)</div><div>+            {</div><div>+                rpsInIdxList = rpsIdxListIter->rps;</div><div>+                if (rpsInRec->numberOfPictures == rpsInIdxList->numberOfPictures</div><div>+                    && rpsInRec->numberOfNegativePictures == rpsInIdxList->numberOfNegativePictures</div><div>+                    && rpsInRec->numberOfPositivePictures == rpsInIdxList->numberOfPositivePictures)</div><div>+                {</div><div>+                    for (pos = 0; pos < rpsInRec->numberOfPictures; pos++)</div><div>+                    {</div><div>+                        if (rpsInRec->deltaPOC[pos] != rpsInIdxList->deltaPOC[pos]</div><div>+                            || rpsInRec->bUsed[pos] != rpsInIdxList->bUsed[pos])</div><div>+                            break;</div><div>+                    }</div><div>+                    if (pos == rpsInRec->numberOfPictures)    // if this type of RPS has exist</div><div>+                    {</div><div>+                        rce->rpsIdx = rpsIdxListIter->idx;</div><div>+                        rpsIdxListIter->count++;</div><div>+                        // sort RPS type link after reset RPS type count.</div><div>+                        RPSListNode* next = rpsIdxListIter->next;</div><div>+                        RPSListNode* prior = rpsIdxListIter->prior;</div><div>+                        RPSListNode* iter = prior;</div><div>+                        if (iter)</div><div>+                        {</div><div>+                            while (iter)</div><div>+                            {</div><div>+                                if (iter->count > rpsIdxListIter->count)</div><div>+                                    break;</div><div>+                                iter = iter->prior;</div><div>+                            }</div><div>+                            if (iter)</div><div>+                            {</div><div>+                                prior->next = next;</div><div>+                                if (next)</div><div>+                                    next->prior = prior;</div><div>+                                else</div><div>+                                    tailRpsIdxList = prior;</div><div>+                                rpsIdxListIter->next = iter->next;</div><div>+                                rpsIdxListIter->prior = iter;</div><div>+                                iter->next->prior = rpsIdxListIter;</div><div>+                                iter->next = rpsIdxListIter;</div><div>+                            }</div><div>+                            else</div><div>+                            {</div><div>+                                prior->next = next;</div><div>+                                if (next)</div><div>+                                    next->prior = prior;</div><div>+                                else</div><div>+                                    tailRpsIdxList = prior;</div><div>+                                headRpsIdxList->prior = rpsIdxListIter;</div><div>+                                rpsIdxListIter->next = headRpsIdxList;</div><div>+                                rpsIdxListIter->prior = NULL;</div><div>+                                headRpsIdxList = rpsIdxListIter;</div><div>+                            }</div><div>+                        }</div><div>+                        break;</div><div>+                    }</div><div>+                }</div><div>+                rpsIdxListIter = rpsIdxListIter->next;</div><div>+            }</div><div>+            if (!rpsIdxListIter)  // add new type of RPS</div><div>+            {</div><div>+                RPSListNode* newIdxNode = new RPSListNode();</div><div>+                if (newIdxNode == NULL)</div><div>+                    goto fail;</div><div>+                newIdxNode->rps = rpsInRec;</div><div>+                newIdxNode->idx = idx++;</div><div>+                newIdxNode->count = 1;</div><div>+                newIdxNode->next = NULL;</div><div>+                newIdxNode->prior = NULL;</div><div>+                if (!tailRpsIdxList)</div><div>+                    tailRpsIdxList = headRpsIdxList = newIdxNode;</div><div>+                else</div><div>+                {</div><div>+                    tailRpsIdxList->next = newIdxNode;</div><div>+                    newIdxNode->prior = tailRpsIdxList;</div><div>+                    tailRpsIdxList = newIdxNode;</div><div>+                }</div><div>+                rce->rpsIdx = newIdxNode->idx;</div><div>+            }</div><div>+        }</div><div>+        else</div><div>+        {</div><div>+            rce->rpsIdx = -1;</div><div>+        }</div><div>+    }</div><div>+</div><div>+    // get commonly RPS set</div><div>+    memset(resultIdx, 0, sizeof(resultIdx));</div><div>+    if (idx > MAX_NUM_SHORT_TERM_RPS)</div><div>+        idx = MAX_NUM_SHORT_TERM_RPS;</div><div>+</div><div>+    *rpsNumInPSP = idx;</div><div>+    rpsIdxListIter = headRpsIdxList;</div><div>+    for (int i = 0; i < idx; i++)</div><div>+    {</div><div>+        resultIdx[i] = rpsIdxListIter->idx;</div><div>+        m_rpsInSpsCount += rpsIdxListIter->count;</div><div>+        thisRpsInSPS = rpsInSPS + i;</div><div>+        thisRpsInList = rpsIdxListIter->rps;</div><div>+        thisRpsInSPS->numberOfPictures = thisRpsInList->numberOfPictures;</div><div>+        thisRpsInSPS->numberOfNegativePictures = thisRpsInList->numberOfNegativePictures;</div><div>+        thisRpsInSPS->numberOfPositivePictures = thisRpsInList->numberOfPositivePictures;</div><div>+        for (pos = 0; pos < thisRpsInList->numberOfPictures; pos++)</div><div>+        {</div><div>+            thisRpsInSPS->deltaPOC[pos] = thisRpsInList->deltaPOC[pos];</div><div>+            thisRpsInSPS->bUsed[pos] = thisRpsInList->bUsed[pos];</div><div>+        }</div><div>+        rpsIdxListIter = rpsIdxListIter->next;</div><div>+    }</div><div>+</div><div>+    //reset every frame's RPS index</div><div>+    for (int i = beginNum; i < endNum; i++)</div><div>+    {</div><div>+        int j;</div><div>+        rce = &rce2Pass[i];</div><div>+        for (j = 0; j < idx; j++)</div><div>+        {</div><div>+            if (rce->rpsIdx == resultIdx[j])</div><div>+            {</div><div>+                rce->rpsIdx = j;</div><div>+                break;</div><div>+            }</div><div>+        }</div><div>+</div><div>+        if (j == idx)</div><div>+            rce->rpsIdx = -1;</div><div>+    }</div><div>+</div><div>+    rpsIdxListIter = headRpsIdxList;</div><div>+    while (rpsIdxListIter)</div><div>+    {</div><div>+        RPSListNode* freeIndex = rpsIdxListIter;</div><div>+        rpsIdxListIter = rpsIdxListIter->next;</div><div>+        delete freeIndex;</div><div>+    }</div><div>+    return true;</div><div>+</div><div>+fail:</div><div>+    rpsIdxListIter = headRpsIdxList;</div><div>+    while (rpsIdxListIter)</div><div>+    {</div><div>+        RPSListNode* freeIndex = rpsIdxListIter;</div><div>+        rpsIdxListIter = rpsIdxListIter->next;</div><div>+        delete freeIndex;</div><div>+    }</div><div>+    return false;</div><div>+}</div><div>+</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/encoder.h</div><div>--- a/source/encoder/encoder.h<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/encoder.h<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -79,6 +79,15 @@</div><div>     int numRefIdxl1[MAX_NUM_REF_IDX];</div><div> };</div><div> </div><div>+struct RPSListNode</div><div>+{</div><div>+    int idx;</div><div>+    int count;</div><div>+    RPS* rps;</div><div>+    RPSListNode* next;</div><div>+    RPSListNode* prior;</div><div>+};</div><div>+</div><div> class FrameEncoder;</div><div> class DPB;</div><div> class Lookahead;</div><div>@@ -156,6 +165,9 @@</div><div>     Lock               m_sliceRefIdxLock;</div><div>     RefIdxLastGOP      m_refIdxLastGOP;</div><div> </div><div>+    Lock               m_rpsInSpsLock;</div><div>+    int                m_rpsInSpsCount;</div><div>+</div><div>     Encoder();</div><div>     ~Encoder() {}</div><div> </div><div>@@ -196,6 +208,7 @@</div><div>     void initRefIdx();</div><div>     void analyseRefIdx(int *numRefIdx);</div><div>     void updateRefIdx();</div><div>+    bool computeSPSRPSIndex();</div><div> </div><div> protected:</div><div> </div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/entropy.cpp</div><div>--- a/source/encoder/entropy.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/entropy.cpp<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -312,7 +312,9 @@</div><div>     WRITE_FLAG(sps.bUseSAO, "sample_adaptive_offset_enabled_flag");</div><div> </div><div>     WRITE_FLAG(0, "pcm_enabled_flag");</div><div>-    WRITE_UVLC(0, "num_short_term_ref_pic_sets");</div><div>+    WRITE_UVLC(sps.spsrpsNum, "num_short_term_ref_pic_sets");</div><div>+    for (int i = 0; i < sps.spsrpsNum; i++)</div><div>+        codeShortTermRefPicSet(sps.spsrps[i], i);</div><div>     WRITE_FLAG(0, "long_term_ref_pics_present_flag");</div><div> </div><div>     WRITE_FLAG(sps.bTemporalMVPEnabled, "sps_temporal_mvp_enable_flag");</div><div>@@ -614,8 +616,21 @@</div><div>             }</div><div> #endif</div><div> </div><div>-        WRITE_FLAG(0, "short_term_ref_pic_set_sps_flag");</div><div>-        codeShortTermRefPicSet(slice.m_rps);</div><div>+        if (slice.m_rpsIdx < 0)</div><div>+        {</div><div>+            WRITE_FLAG(0, "short_term_ref_pic_set_sps_flag");</div><div>+            codeShortTermRefPicSet(slice.m_rps, slice.m_sps->spsrpsNum);</div><div>+        }</div><div>+        else</div><div>+        {</div><div>+            WRITE_FLAG(1, "short_term_ref_pic_set_sps_flag");</div><div>+            int numBits = 0;</div><div>+            while ((1 << numBits) < slice.m_iNumRPSInSPS)</div><div>+                numBits++;</div><div>+</div><div>+            if (numBits > 0)</div><div>+                WRITE_CODE(slice.m_rpsIdx, numBits, "short_term_ref_pic_set_idx");</div><div>+        }</div><div> </div><div>         if (slice.m_sps->bTemporalMVPEnabled)</div><div>             WRITE_FLAG(1, "slice_temporal_mvp_enable_flag");</div><div>@@ -707,8 +722,11 @@</div><div>         WRITE_CODE(substreamSizes[i] - 1, offsetLen, "entry_point_offset_minus1");</div><div> }</div><div> </div><div>-void Entropy::codeShortTermRefPicSet(const RPS& rps)</div><div>+void Entropy::codeShortTermRefPicSet(const RPS& rps, int idx)</div><div> {</div><div>+    if (idx > 0)</div><div>+        WRITE_FLAG(0, "inter_ref_pic_set_prediction_flag");</div><div>+</div><div>     WRITE_UVLC(rps.numberOfNegativePictures, "num_negative_pics");</div><div>     WRITE_UVLC(rps.numberOfPositivePictures, "num_positive_pics");</div><div>     int prev = 0;</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/entropy.h</div><div>--- a/source/encoder/entropy.h<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/entropy.h<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -149,7 +149,7 @@</div><div> </div><div>     void codeSliceHeader(const Slice& slice, FrameData& encData, uint32_t slice_addr, uint32_t slice_addr_bits, int sliceQp);</div><div>     void codeSliceHeaderWPPEntryPoints(const uint32_t *substreamSizes, uint32_t numSubStreams, uint32_t maxOffset);</div><div>-    void codeShortTermRefPicSet(const RPS& rps);</div><div>+    void codeShortTermRefPicSet(const RPS& rps, int idx);</div><div>     void finishSlice()                 { encodeBinTrm(1); finish(); dynamic_cast<Bitstream*>(m_bitIf)->writeByteAlignment(); }</div><div> </div><div>     void encodeCTU(const CUData& cu, const CUGeom& cuGeom);</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/frameencoder.cpp</div><div>--- a/source/encoder/frameencoder.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/frameencoder.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -359,9 +359,23 @@</div><div>             ScopedLock refIdxLock(m_top->m_sliceRefIdxLock);</div><div>             m_top->updateRefIdx();</div><div>         }</div><div>-        m_top->getStreamHeaders(m_nalList, m_entropyCoder, m_bs);</div><div>+        if (m_top->m_param->rc.bStatRead  && m_top->m_param->bMultiPassOptRPS)</div><div>+        {</div><div>+            ScopedLock refIdxLock(m_top->m_rpsInSpsLock);</div><div>+            if (!m_top->computeSPSRPSIndex())</div><div>+            {</div><div>+                x265_log(m_param, X265_LOG_ERROR, "compute commonly RPS failed!\n");</div><div>+                m_top->m_aborted = true;</div><div>+            }</div><div>+            m_top->getStreamHeaders(m_nalList, m_entropyCoder, m_bs);</div><div>+        }</div><div>+        else</div><div>+            m_top->getStreamHeaders(m_nalList, m_entropyCoder, m_bs);</div><div>     }</div><div> </div><div>+    if (m_top->m_param->rc.bStatRead && m_top->m_param->bMultiPassOptRPS)</div><div>+        m_frame->m_encData->m_slice->m_rpsIdx = (m_top->m_rateControl->m_rce2Pass + m_frame->m_encodeOrder)->rpsIdx;</div><div>+</div><div>     // Weighted Prediction parameters estimation.</div><div>     bool bUseWeightP = slice->m_sliceType == P_SLICE && slice->m_pps->bUseWeightPred;</div><div>     bool bUseWeightB = slice->m_sliceType == B_SLICE && slice->m_pps->bUseWeightedBiPred;</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/ratecontrol.cpp</div><div>--- a/source/encoder/ratecontrol.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/ratecontrol.cpp<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -544,10 +544,27 @@</div><div>                 }</div><div>                 rce = &m_rce2Pass[encodeOrder];</div><div>                 m_encOrder[frameNumber] = encodeOrder;</div><div>-                e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",</div><div>-                       &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,</div><div>-                       &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,</div><div>-                       &rce->skipCuCount);</div><div>+                if (!m_param->bMultiPassOptRPS)</div><div>+                {</div><div>+                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",</div><div>+                        &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,</div><div>+                        &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,</div><div>+                        &rce->skipCuCount);</div><div>+                }</div><div>+                else</div><div>+                {</div><div>+                    char deltaPOC[128];</div><div>+                    char bUsed[40];</div><div>+                    memset(deltaPOC, 0, sizeof(deltaPOC));</div><div>+                    memset(bUsed, 0, sizeof(bUsed));</div><div>+                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf nump:%d numnegp:%d numposp:%d deltapoc:%s bused:%s",</div><div>+                        &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,</div><div>+                        &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,</div><div>+                        &rce->skipCuCount, &rce->rpsData.numberOfPictures, &rce->rpsData.numberOfNegativePictures, &rce->rpsData.numberOfPositivePictures, deltaPOC, bUsed);</div><div>+                    splitdeltaPOC(deltaPOC, rce);</div><div>+                    splitbUsed(bUsed, rce);</div><div>+                    rce->rpsIdx = -1;</div><div>+                }</div><div>                 rce->keptAsRef = true;</div><div>                 rce->isIdr = false;</div><div>                 if (picType == 'b' || picType == 'p')</div><div>@@ -2632,18 +2649,55 @@</div><div>     char cType = rce->sliceType == I_SLICE ? (curFrame->m_lowres.sliceType == X265_TYPE_IDR ? 'I' : 'i')</div><div>         : rce->sliceType == P_SLICE ? 'P'</div><div>         : IS_REFERENCED(curFrame) ? 'B' : 'b';</div><div>-    if (fprintf(m_statFileOut,</div><div>-                "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",</div><div>-                rce->poc, rce->encodeOrder,</div><div>-                cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,</div><div>-                rce->qpNoVbv, rce->qRceq,</div><div>-                curFrame->m_encData->m_frameStats.coeffBits,</div><div>-                curFrame->m_encData->m_frameStats.mvBits,</div><div>-                curFrame->m_encData->m_frameStats.miscBits,</div><div>-                curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,</div><div>-                curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,</div><div>-                curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu) < 0)</div><div>-        goto writeFailure;</div><div>+    </div><div>+    if (!curEncData.m_param->bMultiPassOptRPS)</div><div>+    {</div><div>+        if (fprintf(m_statFileOut,</div><div>+            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",</div><div>+            rce->poc, rce->encodeOrder,</div><div>+            cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,</div><div>+            rce->qpNoVbv, rce->qRceq,</div><div>+            curFrame->m_encData->m_frameStats.coeffBits,</div><div>+            curFrame->m_encData->m_frameStats.mvBits,</div><div>+            curFrame->m_encData->m_frameStats.miscBits,</div><div>+            curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,</div><div>+            curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,</div><div>+            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu) < 0)</div><div>+            goto writeFailure;</div><div>+    }</div><div>+    else{</div><div>+        RPS* rpsWriter = &curFrame->m_encData->m_slice->m_rps;</div><div>+        int i, num = rpsWriter->numberOfPictures;</div><div>+        char deltaPOC[128];</div><div>+        char bUsed[40];</div><div>+        memset(deltaPOC, 0, sizeof(deltaPOC));</div><div>+        memset(bUsed, 0, sizeof(bUsed));</div><div>+        sprintf(deltaPOC, "deltapoc:~");</div><div>+        sprintf(bUsed, "bused:~");</div><div>+</div><div>+        for (i = 0; i < num; i++)</div><div>+        {</div><div>+            sprintf(deltaPOC, "%s%d~", deltaPOC, rpsWriter->deltaPOC[i]);</div><div>+            sprintf(bUsed, "%s%d~", bUsed, rpsWriter->bUsed[i]);</div><div>+        }</div><div>+</div><div>+        if (fprintf(m_statFileOut,</div><div>+            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f nump:%d numnegp:%d numposp:%d %s %s ;\n",</div><div>+            rce->poc, rce->encodeOrder,</div><div>+            cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,</div><div>+            rce->qpNoVbv, rce->qRceq,</div><div>+            curFrame->m_encData->m_frameStats.coeffBits,</div><div>+            curFrame->m_encData->m_frameStats.mvBits,</div><div>+            curFrame->m_encData->m_frameStats.miscBits,</div><div>+            curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,</div><div>+            curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,</div><div>+            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu,</div><div>+            rpsWriter->numberOfPictures,</div><div>+            rpsWriter->numberOfNegativePictures,</div><div>+            rpsWriter->numberOfPositivePictures,</div><div>+            deltaPOC, bUsed) < 0)</div><div>+            goto writeFailure;</div><div>+    }</div><div>     /* Don't re-write the data in multi-pass mode. */</div><div>     if (m_param->rc.cuTree && IS_REFERENCED(curFrame) && !m_param->rc.bStatRead)</div><div>     {</div><div>@@ -2736,3 +2790,48 @@</div><div>     X265_FREE(m_param->rc.zones);</div><div> }</div><div> </div><div>+void RateControl::splitdeltaPOC(char deltapoc[], RateControlEntry *rce)</div><div>+{</div><div>+    int idx = 0, length = 0;</div><div>+    char tmpStr[128];</div><div>+    char* src = deltapoc;</div><div>+    char* buf = strstr(src, "~");</div><div>+    while (buf)</div><div>+    {</div><div>+        memset(tmpStr, 0, sizeof(tmpStr));</div><div>+        length = (int)(buf - src);</div><div>+        if (length != 0)</div><div>+        {</div><div>+            strncpy(tmpStr, src, length);</div><div>+            rce->rpsData.deltaPOC[idx] = atoi(tmpStr);</div><div>+            idx++;</div><div>+            if (idx == rce->rpsData.numberOfPictures)</div><div>+                break;</div><div>+        }</div><div>+        src += (length + 1);</div><div>+        buf = strstr(src, "~");</div><div>+    }</div><div>+}</div><div>+</div><div>+void RateControl::splitbUsed(char bused[], RateControlEntry *rce)</div><div>+{</div><div>+    int idx = 0, length = 0;</div><div>+    char tmpStr[128];</div><div>+    char* src = bused;</div><div>+    char* buf = strstr(src, "~");</div><div>+    while (buf)</div><div>+    {</div><div>+        memset(tmpStr, 0, sizeof(tmpStr));</div><div>+        length = (int)(buf - src);</div><div>+        if (length != 0)</div><div>+        {</div><div>+            strncpy(tmpStr, src, length);</div><div>+            rce->rpsData.bUsed[idx] = atoi(tmpStr) > 0;</div><div>+            idx++;</div><div>+            if (idx == rce->rpsData.numberOfPictures)</div><div>+                break;</div><div>+        }</div><div>+        src += (length + 1);</div><div>+        buf = strstr(src, "~");</div><div>+    }</div><div>+}</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/encoder/ratecontrol.h</div><div>--- a/source/encoder/ratecontrol.h<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/encoder/ratecontrol.h<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -111,6 +111,8 @@</div><div>     bool     isIdr;</div><div>     SEIPictureTiming *picTimingSEI;</div><div>     HRDTiming        *hrdTiming;</div><div>+    int      rpsIdx;</div><div>+    RPS      rpsData;</div><div> };</div><div> </div><div> class RateControl</div><div>@@ -282,6 +284,8 @@</div><div>     bool   findUnderflow(double *fills, int *t0, int *t1, int over, int framesCount);</div><div>     bool   fixUnderflow(int t0, int t1, double adjustment, double qscaleMin, double qscaleMax);</div><div>     double tuneQScaleForGrain(double rcOverflow);</div><div>+    void   splitdeltaPOC(char deltapoc[], RateControlEntry *rce);</div><div>+    void   splitbUsed(char deltapoc[], RateControlEntry *rce);</div><div> };</div><div> }</div><div> #endif // ifndef X265_RATECONTROL_H</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/x265.h</div><div>--- a/source/x265.h<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/x265.h<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -1323,6 +1323,9 @@</div><div>     /* Opitmize ref list length in PPS based on stats from previous GOP*/</div><div>     int bOptRefListLengthPPS;</div><div> </div><div>+    /* Enable storing commonly RPS in SPS in multi pass mode */</div><div>+    int       bMultiPassOptRPS;</div><div>+</div><div> } x265_param;</div><div> </div><div> /* x265_param_alloc:</div><div>diff -r 0e9e52640546 -r 19d3c5ea8bc1 source/x265cli.h</div><div>--- a/source/x265cli.h<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>Wed Oct 12 17:58:49 2016 +0530</div><div>+++ b/source/x265cli.h<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>Mon Oct 24 14:54:41 2016 +0800</div><div>@@ -236,6 +236,8 @@</div><div>     { "pass",           required_argument, NULL, 0 },</div><div>     { "slow-firstpass",       no_argument, NULL, 0 },</div><div>     { "no-slow-firstpass",    no_argument, NULL, 0 },</div><div>+    { "multi-pass-opt-rps",   no_argument, NULL, 0 },</div><div>+    { "no-multi-pass-opt-rps", no_argument, NULL, 0 },</div><div>     { "analysis-mode",  required_argument, NULL, 0 },</div><div>     { "analysis-file",  required_argument, NULL, 0 },</div><div>     { "strict-cbr",           no_argument, NULL, 0 },</div><div>@@ -460,6 +462,7 @@</div><div>     H0("   --[no-]vui-hrd-info           Discard optional HRD timing information from the bistream. Default %s\n", OPT(param->bEmitVUIHRDInfo));</div><div>     H0("   --[no-]opt-qp-pps             Discard optional HRD timing information from the bistream. Default %s\n", OPT(param->bOptQpPPS));</div><div>     H0("   --[no-]opt-ref-list-length-pps  Discard optional HRD timing information from the bistream. Default %s\n", OPT(param->bOptRefListLengthPPS));</div><div>+    H0("   --[no-]multi-pass-opt-rps     Enable storing commonly RPS in SPS in multi pass mode. Default %s\n", OPT(param->bMultiPassOptRPS));</div><div>     H1("\nReconstructed video options (debugging):\n");</div><div>     H1("-r/--recon <filename>            Reconstructed raw image YUV or Y4M output file name\n");</div><div>     H1("   --recon-depth <integer>       Bit-depth of reconstructed raw image file. Defaults to input bit depth, or 8 if Y4M\n");</div><div><br></div></div>