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

Gopu Govindaswamy gopu at multicorewareinc.com
Wed Sep 11 08:47:05 CEST 2013


# HG changeset patch
# User Gopu Govindaswamy <gopu at multicorewareinc.com>
# Date 1378882010 -19800
# Node ID 275f2d5f9b0a782fd3ec36d95268545d96473e2e
# Parent  af8cddab103e87cb7817e4809780a64573d4dad2
NAL : std::ostringstream replaced

diff -r af8cddab103e -r 275f2d5f9b0a source/Lib/TLibCommon/NAL.h
--- a/source/Lib/TLibCommon/NAL.h	Wed Sep 11 00:03:09 2013 -0500
+++ b/source/Lib/TLibCommon/NAL.h	Wed Sep 11 12:16:50 2013 +0530
@@ -64,7 +64,7 @@
 
     /** default constructor - no initialization; must be perfomed by user */
     NALUnit() {}
-
+    
     /** returns true if the NALunit is a slice NALunit */
     bool isSlice()
     {
@@ -105,7 +105,9 @@
  */
 struct NALUnitEBSP : public NALUnit
 {
-    std::ostringstream m_nalUnitData;
+    
+    UInt m_packetSize;
+    unsigned char *m_nalUnitData;
 
     /**
      * convert the OutputNALUnit #nalu# into EBSP format by writing out
diff -r af8cddab103e -r 275f2d5f9b0a source/Lib/TLibEncoder/NALwrite.cpp
--- a/source/Lib/TLibEncoder/NALwrite.cpp	Wed Sep 11 00:03:09 2013 -0500
+++ b/source/Lib/TLibEncoder/NALwrite.cpp	Wed Sep 11 12:16:50 2013 +0530
@@ -48,26 +48,23 @@
 
 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(unsigned char*& 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 = (unsigned char *) malloc(packetSize * sizeof(unsigned char));
+    ::memcpy(out, bsNALUHeader.getByteStream(), packetSize);
+    
     /* write out rsbp_byte's, inserting any required
      * emulation_prevention_three_byte's */
 
@@ -118,8 +115,11 @@
             it = rbsp.insert(found, emulation_prevention_three_byte[0]);
         }
     }
-
-    out.write((char*)&(*rbsp.begin()), rbsp.end() - rbsp.begin());
+   
+    UInt i = packetSize;
+    out = (unsigned char *) 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 af8cddab103e -r 275f2d5f9b0a source/Lib/TLibEncoder/NALwrite.h
--- a/source/Lib/TLibEncoder/NALwrite.h	Wed Sep 11 00:03:09 2013 -0500
+++ b/source/Lib/TLibEncoder/NALwrite.h	Wed Sep 11 12:16:50 2013 +0530
@@ -74,13 +74,13 @@
     TComOutputBitstream m_Bitstream;
 };
 
-void write(std::ostream& out, OutputNALUnit& nalu);
+void write(unsigned char*& 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 af8cddab103e -r 275f2d5f9b0a source/Lib/TLibEncoder/TEncTop.cpp
--- a/source/Lib/TLibEncoder/TEncTop.cpp	Wed Sep 11 00:03:09 2013 -0500
+++ b/source/Lib/TLibEncoder/TEncTop.cpp	Wed Sep 11 12:16:50 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 af8cddab103e -r 275f2d5f9b0a source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Wed Sep 11 00:03:09 2013 -0500
+++ b/source/encoder/encoder.cpp	Wed Sep 11 12:16:50 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++;
         }
 
@@ -368,7 +368,7 @@
         {
             const NALUnitEBSP& nalu = **it;
             int size = 0; /* size of annexB unit in bytes */
-
+              
             static const char start_code_prefix[] = { 0, 0, 0, 1 };
             if (it == au.begin() || nalu.m_nalUnitType == NAL_UNIT_SPS || nalu.m_nalUnitType == NAL_UNIT_PPS)
             {
@@ -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++;
         }
 
@@ -449,7 +450,7 @@
         {
             const NALUnitEBSP& nalu = **it;
             int size = 0; /* size of annexB unit in bytes */
-
+            
             static const char start_code_prefix[] = { 0, 0, 0, 1 };
             if (it == au.begin() || nalu.m_nalUnitType == NAL_UNIT_SPS || nalu.m_nalUnitType == NAL_UNIT_PPS)
             {
@@ -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 */
@@ -493,6 +495,7 @@
     }
     else if (pi_nal)
         *pi_nal = 0;
+    
     return numEncoded;
 }
 


More information about the x265-devel mailing list