[x265] [PATCH] NAL : std::ostringstream replaced

Gopu Govindaswamy gopu at multicorewareinc.com
Thu Sep 12 07:35:24 CEST 2013


# HG changeset patch
# User Gopu Govindaswamy <gopu at multicorewareinc.com>
# Date 1378964109 -19800
# Node ID 0482a5c72c21dc14d6be5f4b9c4ffe2ae574ebc3
# Parent  ea0aa1f8abda53da2473d3d4c444690d2099c01a
NAL : std::ostringstream replaced

diff -r ea0aa1f8abda -r 0482a5c72c21 source/Lib/TLibCommon/NAL.h
--- a/source/Lib/TLibCommon/NAL.h	Wed Sep 11 14:50:24 2013 -0500
+++ b/source/Lib/TLibCommon/NAL.h	Thu Sep 12 11:05:09 2013 +0530
@@ -105,7 +105,8 @@
  */
 struct NALUnitEBSP : public NALUnit
 {
-    std::ostringstream m_nalUnitData;
+    UInt m_packetSize;
+    uint8_t *m_nalUnitData;
 
     /**
      * convert the OutputNALUnit #nalu# into EBSP format by writing out
diff -r ea0aa1f8abda -r 0482a5c72c21 source/Lib/TLibEncoder/NALwrite.cpp
--- a/source/Lib/TLibEncoder/NALwrite.cpp	Wed Sep 11 14:50:24 2013 -0500
+++ b/source/Lib/TLibEncoder/NALwrite.cpp	Thu Sep 12 11:05:09 2013 +0530
@@ -48,26 +48,24 @@
 
 static const char emulation_prevention_three_byte[] = { 3 };
 
-void writeNalUnitHeader(ostream& out, OutputNALUnit& nalu)       // nal_unit_header()
+/**
+ * write nalu to bytestream out, performing RBSP anti startcode
+ * emulation as required.  nalu.m_RBSPayload must be byte aligned.
+ */
+void write(uint8_t*& out, OutputNALUnit& nalu, UInt &packetSize)
 {
+    packetSize = 0;
+
     TComOutputBitstream bsNALUHeader;
-
     bsNALUHeader.write(0, 1);                 // forbidden_zero_bit
     bsNALUHeader.write(nalu.m_nalUnitType, 6); // nal_unit_type
     bsNALUHeader.write(nalu.m_reservedZero6Bits, 6);                 // nuh_reserved_zero_6bits
     bsNALUHeader.write(nalu.m_temporalId + 1, 3); // nuh_temporal_id_plus1
-
-    out.write(bsNALUHeader.getByteStream(), bsNALUHeader.getByteStreamLength());
-}
-
-/**
- * write nalu to bytestream out, performing RBSP anti startcode
- * emulation as required.  nalu.m_RBSPayload must be byte aligned.
- */
-void write(ostream& out, OutputNALUnit& nalu)
-{
-    writeNalUnitHeader(out, nalu);
-
+    
+    packetSize += bsNALUHeader.getByteStreamLength();
+    out = (uint8_t *) malloc(packetSize);
+    ::memcpy(out, bsNALUHeader.getByteStream(), packetSize);
+    
     /* write out rsbp_byte's, inserting any required
      * emulation_prevention_three_byte's */
 
@@ -118,8 +116,10 @@
             it = rbsp.insert(found, emulation_prevention_three_byte[0]);
         }
     }
-
-    out.write((char*)&(*rbsp.begin()), rbsp.end() - rbsp.begin());
+    UInt i = packetSize;
+    out = (uint8_t *) realloc (out, (rbsp.end() - rbsp.begin()) + 4 );
+    memcpy(out + packetSize, &(*rbsp.begin()), rbsp.end() - rbsp.begin());
+    packetSize += rbsp.end() - rbsp.begin();
 
     /* 7.4.1.1
      * ... when the last byte of the RBSP data is equal to 0x00 (which can
@@ -128,7 +128,8 @@
      */
     if (rbsp.back() == 0x00)
     {
-        out.write(emulation_prevention_three_byte, 1);
+        out[i] = 3;
+        packetSize += 1;
     }
 }
 
diff -r ea0aa1f8abda -r 0482a5c72c21 source/Lib/TLibEncoder/NALwrite.h
--- a/source/Lib/TLibEncoder/NALwrite.h	Wed Sep 11 14:50:24 2013 -0500
+++ b/source/Lib/TLibEncoder/NALwrite.h	Thu Sep 12 11:05:09 2013 +0530
@@ -74,13 +74,13 @@
     TComOutputBitstream m_Bitstream;
 };
 
-void write(std::ostream& out, OutputNALUnit& nalu);
+void write(uint8_t*& out, OutputNALUnit& nalu, UInt& packetSize);
 void writeRBSPTrailingBits(TComOutputBitstream& bs);
 
 inline NALUnitEBSP::NALUnitEBSP(OutputNALUnit& nalu)
     : NALUnit(nalu)
 {
-    write(m_nalUnitData, nalu);
+    write(m_nalUnitData, nalu, m_packetSize);
 }
 
 void copyNaluData(OutputNALUnit& naluDest, const OutputNALUnit& naluSrc);
diff -r ea0aa1f8abda -r 0482a5c72c21 source/Lib/TLibEncoder/TEncTop.cpp
--- a/source/Lib/TLibEncoder/TEncTop.cpp	Wed Sep 11 14:50:24 2013 -0500
+++ b/source/Lib/TLibEncoder/TEncTop.cpp	Thu Sep 12 11:05:09 2013 +0530
@@ -550,7 +550,7 @@
     UInt numRBSPBytes = 0;
     for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
     {
-        UInt numRBSPBytes_nal = UInt((*it)->m_nalUnitData.str().size());
+        UInt numRBSPBytes_nal = (*it)->m_packetSize;
 #if VERBOSE_RATE
         printf("*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal);
 #endif
diff -r ea0aa1f8abda -r 0482a5c72c21 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Wed Sep 11 14:50:24 2013 -0500
+++ b/source/encoder/encoder.cpp	Thu Sep 12 11:05:09 2013 +0530
@@ -348,7 +348,7 @@
         for (AccessUnit::const_iterator t = au.begin(); t != au.end(); t++)
         {
             const NALUnitEBSP& temp = **t;
-            memsize += (UInt)temp.m_nalUnitData.str().size() + 4;
+            memsize += temp.m_packetSize + 4;
             nalcount++;
         }
 
@@ -389,14 +389,15 @@
                 size += 3;
             }
             memsize += size;
-            size_t nalSize = nalu.m_nalUnitData.str().size();
-            ::memcpy(encoder->m_packetData + memsize, nalu.m_nalUnitData.str().c_str(), nalSize);
+            size_t nalSize = nalu.m_packetSize;
+            ::memcpy(encoder->m_packetData + memsize, nalu.m_nalUnitData, nalSize);
             size += (int)nalSize;
             memsize += (int)nalSize;
 
             encoder->m_nals[nalcount].i_type = nalu.m_nalUnitType;
             encoder->m_nals[nalcount].i_payload = size;
             nalcount++;
+            free(nalu.m_nalUnitData);
         }
 
         /* Setup payload pointers, now that we're done adding content to m_packetData */
@@ -429,7 +430,7 @@
         for (AccessUnit::const_iterator t = au.begin(); t != au.end(); t++)
         {
             const NALUnitEBSP& temp = **t;
-            memsize += (UInt)temp.m_nalUnitData.str().size() + 4;
+            memsize += temp.m_packetSize + 4;
             nalcount++;
         }
 
@@ -470,14 +471,15 @@
                 size += 3;
             }
             memsize += size;
-            size_t nalSize = nalu.m_nalUnitData.str().size();
-            ::memcpy(encoder->m_packetData + memsize, nalu.m_nalUnitData.str().c_str(), nalSize);
+            size_t nalSize = nalu.m_packetSize;
+            ::memcpy(encoder->m_packetData + memsize, nalu.m_nalUnitData, nalSize);
             size += (int)nalSize;
             memsize += (int)nalSize;
 
             encoder->m_nals[nalcount].i_type = nalu.m_nalUnitType;
             encoder->m_nals[nalcount].i_payload = size;
             nalcount++;
+            free(nalu.m_nalUnitData);
         }
 
         /* Setup payload pointers, now that we're done adding content to m_packetData */


More information about the x265-devel mailing list