[x265-commits] [x265] fix checked build warning

Steve Borho steve at borho.org
Fri Jun 20 08:04:12 CEST 2014


details:   http://hg.videolan.org/x265/rev/91fef2cf2e08
branches:  
changeset: 7142:91fef2cf2e08
user:      Steve Borho <steve at borho.org>
date:      Fri Jun 20 00:23:03 2014 -0500
description:
fix checked build warning
Subject: [x265] nal: allow the concatenated substream buffer to be re-used from frame-to-frame

details:   http://hg.videolan.org/x265/rev/51dd3c429cdb
branches:  
changeset: 7143:51dd3c429cdb
user:      Steve Borho <steve at borho.org>
date:      Fri Jun 20 00:55:46 2014 -0500
description:
nal: allow the concatenated substream buffer to be re-used from frame-to-frame

this removes another malloc/free from the processing of every frame. This commit
also moves the NALList constructor to nal.cpp since it is no longer quite so
trivial. Lastly, it adds a check to prevent crashes in case one or more of the
WPP substreams had a malloc failure.
Subject: [x265] nal: nits

details:   http://hg.videolan.org/x265/rev/57f26a8b7ecb
branches:  
changeset: 7144:57f26a8b7ecb
user:      Steve Borho <steve at borho.org>
date:      Fri Jun 20 00:56:00 2014 -0500
description:
nal: nits

diffstat:

 source/encoder/frameencoder.cpp |   7 +-
 source/encoder/nal.cpp          |  91 ++++++++++++++++++++++++++--------------
 source/encoder/nal.h            |  12 +++-
 3 files changed, 69 insertions(+), 41 deletions(-)

diffs (221 lines):

diff -r 73ece35100df -r 57f26a8b7ecb source/encoder/frameencoder.cpp
--- a/source/encoder/frameencoder.cpp	Fri Jun 20 00:17:02 2014 -0500
+++ b/source/encoder/frameencoder.cpp	Fri Jun 20 00:56:00 2014 -0500
@@ -592,8 +592,8 @@ void FrameEncoder::compressFrame()
         m_outStreams[i].writeByteAlignment();
     }
 
-    uint32_t totalBytes;
-    uint8_t *concatStreams = m_nalList.serializeMultiple(slice->getSubstreamSizes(), totalBytes, numSubstreams, m_outStreams);
+    // serialize each row, record final lenghts in slice header
+    m_nalList.serializeSubstreams(slice->getSubstreamSizes(), numSubstreams, m_outStreams);
 
     // complete the slice header by writing WPP row-starts
     entropyCoder->setEntropyCoder(&m_sbacCoder, slice);
@@ -601,8 +601,7 @@ void FrameEncoder::compressFrame()
     entropyCoder->encodeTilesWPPEntryPoint(slice);
     m_bs.writeByteAlignment();
 
-    m_nalList.serialize(slice->getNalUnitType(), m_bs, concatStreams, totalBytes);
-    X265_FREE(concatStreams);
+    m_nalList.serialize(slice->getNalUnitType(), m_bs);
 
     if (m_param->decodedPictureHashSEI)
     {
diff -r 73ece35100df -r 57f26a8b7ecb source/encoder/nal.cpp
--- a/source/encoder/nal.cpp	Fri Jun 20 00:17:02 2014 -0500
+++ b/source/encoder/nal.cpp	Fri Jun 20 00:56:00 2014 -0500
@@ -27,6 +27,16 @@
 
 using namespace x265;
 
+NALList::NALList()
+    : m_numNal(0)
+    , m_buffer(NULL)
+    , m_occupancy(0)
+    , m_allocSize(0)
+    , m_extraBuffer(NULL)
+    , m_extraOccupancy(0)
+    , m_extraAllocSize(0)
+{}
+
 void NALList::takeContents(NALList& other)
 {
     /* take other NAL buffer, discard our old one */
@@ -45,7 +55,7 @@ void NALList::takeContents(NALList& othe
     other.m_buffer = X265_MALLOC(uint8_t, m_allocSize);
 }
 
-void NALList::serialize(NalUnitType nalUnitType, const Bitstream& bs, uint8_t* extra, uint32_t extraBytes)
+void NALList::serialize(NalUnitType nalUnitType, const Bitstream& bs)
 {
     static const char startCodePrefix[] = { 0, 0, 0, 1 };
 
@@ -54,7 +64,7 @@ void NALList::serialize(NalUnitType nalU
     if (!bpayload)
         return;
 
-    uint32_t nextSize = m_occupancy + sizeof(startCodePrefix) + 2 + payloadSize + (payloadSize >> 1) + extraBytes;
+    uint32_t nextSize = m_occupancy + sizeof(startCodePrefix) + 2 + payloadSize + (payloadSize >> 1) + m_extraOccupancy;
     if (nextSize > m_allocSize)
     {
         uint8_t *temp = X265_MALLOC(uint8_t, nextSize);
@@ -72,7 +82,7 @@ void NALList::serialize(NalUnitType nalU
         }
         else
         {
-            x265_log(NULL, X265_LOG_ERROR, "Unable to realloc access unit buffer");
+            x265_log(NULL, X265_LOG_ERROR, "Unable to realloc access unit buffer\n");
             return;
         }
     }
@@ -104,8 +114,7 @@ void NALList::serialize(NalUnitType nalU
      * any byte-aligned position:
      *  - 0x000000
      *  - 0x000001
-     *  - 0x000002
-     */
+     *  - 0x000002 */
     for (uint32_t i = 0; i < payloadSize; i++)
     {
         if (i > 2 && !out[bytes - 2] && !out[bytes - 3] && out[bytes - 1] <= 0x03)
@@ -119,24 +128,25 @@ void NALList::serialize(NalUnitType nalU
         out[bytes++] = bpayload[i];
     }
 
-    if (extra)
+    X265_CHECK(bytes <= 2 + payloadSize + (payloadSize >> 1), "NAL buffer overflow\n");
+
+    if (m_extraOccupancy)
     {
-        /* these bytes were escaped by serializeMultiple */
-        memcpy(out + bytes, extra, extraBytes);
-        bytes += extraBytes;
+        /* these bytes were escaped by serializeSubstreams */
+        memcpy(out + bytes, m_extraBuffer, m_extraOccupancy);
+        bytes += m_extraOccupancy;
+        m_extraOccupancy = 0;
     }
 
     /* 7.4.1.1
      * ... when the last byte of the RBSP data is equal to 0x00 (which can
      * only occur when the RBSP ends in a cabac_zero_word), a final byte equal
-     * to 0x03 is appended to the end of the data.
-     */
+     * to 0x03 is appended to the end of the data.  */
     if (!out[bytes - 1])
         out[bytes++] = 0x03;
     m_occupancy += bytes;
 
-    X265_CHECK(bytes <= 2 + payloadSize + (payloadSize >> 1) + extraBytes, "NAL buffer overflow\n");
-    X265_CHECK(m_numNal < MAX_NAL_UNITS, "NAL count overflow\n");
+    X265_CHECK(m_numNal < (uint32_t)MAX_NAL_UNITS, "NAL count overflow\n");
 
     x265_nal& nal = m_nal[m_numNal++];
     nal.type = nalUnitType;
@@ -144,44 +154,59 @@ void NALList::serialize(NalUnitType nalU
     nal.payload = out;
 }
 
-/* concatenate and escape multiple sub-streams, return final escaped lengths and
- * concatenated buffer. Caller is responsible for freeing the returned buffer */
-uint8_t *NALList::serializeMultiple(uint32_t* streamSizeBytes, uint32_t& totalBytes, uint32_t streamCount, const Bitstream* streams)
+/* concatenate and escape WPP sub-streams, return escaped row lengths.
+ * These streams will be appended to the next serialized NAL */
+void NALList::serializeSubstreams(uint32_t* streamSizeBytes, uint32_t streamCount, const Bitstream* streams)
 {
     uint32_t estSize = 0;
     for (uint32_t s = 0; s < streamCount; s++)
         estSize += streams[s].getNumberOfWrittenBytes();
-    totalBytes = 0;
+    estSize += estSize >> 1;
 
-    /* padded allocation for emulation prevention bytes */
-    uint8_t* out = X265_MALLOC(uint8_t, estSize + (estSize >> 1));
-    if (!out)
-        return NULL;
+    if (estSize > m_extraAllocSize)
+    {
+        uint8_t *temp = X265_MALLOC(uint8_t, estSize);
+        if (temp)
+        {
+            X265_FREE(m_extraBuffer);
+            m_extraBuffer = temp;
+            m_extraAllocSize = estSize;
+        }
+        else
+        {
+            x265_log(NULL, X265_LOG_ERROR, "Unable to realloc WPP substream concatenation buffer\n");
+            return;
+        }
+    }
 
+    uint32_t bytes = 0;
+    uint8_t *out = m_extraBuffer;
     for (uint32_t s = 0; s < streamCount; s++)
     {
         const Bitstream& stream = streams[s];
         uint32_t inSize = stream.getNumberOfWrittenBytes();
         const uint8_t *inBytes = stream.getFIFO();
-        uint32_t prevBufSize = totalBytes;
+        uint32_t prevBufSize = bytes;
 
-        for (uint32_t i = 0; i < inSize; i++)
+        if (inBytes)
         {
-            if (totalBytes > 2 && !out[totalBytes - 2] && !out[totalBytes - 3] && out[totalBytes - 1] <= 0x03)
+            for (uint32_t i = 0; i < inSize; i++)
             {
-                /* inject 0x03 to prevent emulating a start code */
-                out[totalBytes] = out[totalBytes - 1];
-                out[totalBytes - 1] = 0x03;
-                totalBytes++;
+                if (bytes > 2 && !out[bytes - 2] && !out[bytes - 3] && out[bytes - 1] <= 0x03)
+                {
+                    /* inject 0x03 to prevent emulating a start code */
+                    out[bytes] = out[bytes - 1];
+                    out[bytes - 1] = 0x03;
+                    bytes++;
+                }
+
+                out[bytes++] = inBytes[i];
             }
-
-            out[totalBytes++] = inBytes[i];
         }
 
         if (s < streamCount - 1)
-            streamSizeBytes[s] = (totalBytes - prevBufSize) << 3;
+            streamSizeBytes[s] = (bytes - prevBufSize) << 3;
     }
 
-    return out;
+    m_extraOccupancy = bytes;
 }
-
diff -r 73ece35100df -r 57f26a8b7ecb source/encoder/nal.h
--- a/source/encoder/nal.h	Fri Jun 20 00:17:02 2014 -0500
+++ b/source/encoder/nal.h	Fri Jun 20 00:56:00 2014 -0500
@@ -45,14 +45,18 @@ public:
     uint32_t    m_occupancy;
     uint32_t    m_allocSize;
 
-    NALList() : m_numNal(0), m_buffer(NULL), m_occupancy(0), m_allocSize(0) {}
-    ~NALList() { X265_FREE(m_buffer); }
+    uint8_t*    m_extraBuffer;
+    uint32_t    m_extraOccupancy;
+    uint32_t    m_extraAllocSize;
+
+    NALList();
+    ~NALList() { X265_FREE(m_buffer); X265_FREE(m_extraBuffer); }
 
     void takeContents(NALList& other);
 
-    void serialize(NalUnitType nalUnitType, const Bitstream& bs, uint8_t *extra = NULL, uint32_t extraBytes = 0);
+    void serialize(NalUnitType nalUnitType, const Bitstream& bs);
 
-    static uint8_t *serializeMultiple(uint32_t* streamSizeBytes, uint32_t& totalBytes, uint32_t streamCount, const Bitstream* streams);
+    void serializeSubstreams(uint32_t* streamSizeBytes, uint32_t streamCount, const Bitstream* streams);
 };
 
 }


More information about the x265-commits mailing list