[x265] [PATCH] Encoder: Removed Code Duplication and made the common function for nal data extraction

Gopu Govindaswamy gopu at multicorewareinc.com
Thu Sep 12 11:56:56 CEST 2013


# HG changeset patch
# User Gopu Govindaswamy <gopu at multicorewareinc.com>
# Date 1378979612 -19800
# Node ID 185918a342b05a9e420bcaeccc9b286a988c17ea
# Parent  7be0ceae34e059849737050c52a326becd8bbf1a
Encoder: Removed Code Duplication and made the common function for nal data extraction

diff -r 7be0ceae34e0 -r 185918a342b0 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Thu Sep 12 13:24:24 2013 +0530
+++ b/source/encoder/encoder.cpp	Thu Sep 12 15:23:32 2013 +0530
@@ -47,6 +47,7 @@
 
     void configure(x265_param_t *param);
     void determineLevelAndProfile(x265_param_t *param);
+    void x265_extract_naldata(AccessUnit &au, size_t &nalcount);
 };
 
 x265_t::x265_t()
@@ -343,70 +344,8 @@
     AccessUnit au;
     if (encoder->getStreamHeaders(au) == 0)
     {
-        UInt memsize = 0;
-        int nalcount = 0;
-        for (AccessUnit::const_iterator t = au.begin(); t != au.end(); t++)
-        {
-            const NALUnitEBSP& temp = **t;
-            memsize += temp.m_packetSize + 4;
-            nalcount++;
-        }
-
-        if (encoder->m_packetData)
-            X265_FREE(encoder->m_packetData);
-
-        if (encoder->m_nals)
-            X265_FREE(encoder->m_nals);
-
-        encoder->m_packetData = (char*)X265_MALLOC(char, memsize);
-        encoder->m_nals = (x265_nal_t*)X265_MALLOC(x265_nal_t, nalcount);
-        nalcount = 0;
-        memsize = 0;
-
-        /* Copy NAL output packets into x265_nal_t structures */
-        for (AccessUnit::const_iterator it = au.begin(); it != au.end(); it++)
-        {
-            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)
-            {
-                /* From AVC, When any of the following conditions are fulfilled, the
-                    * zero_byte syntax element shall be present:
-                    *  - the nal_unit_type within the nal_unit() is equal to 7 (sequence
-                    *    parameter set) or 8 (picture parameter set),
-                    *  - the byte stream NAL unit syntax structure contains the first NAL
-                    *    unit of an access unit in decoding order, as specified by subclause
-                    *    7.4.1.2.3.
-                    */
-                ::memcpy(encoder->m_packetData + memsize, start_code_prefix, 4);
-                size += 4;
-            }
-            else
-            {
-                ::memcpy(encoder->m_packetData + memsize, start_code_prefix + 1, 3);
-                size += 3;
-            }
-            memsize += size;
-            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 */
-        size_t offset = 0;
-        for (size_t i = 0; i < (size_t)nalcount; i++)
-        {
-            encoder->m_nals[i].p_payload = (uint8_t*)encoder->m_packetData + offset;
-            offset += encoder->m_nals[i].i_payload;
-        }
+        size_t nalcount;
+        encoder->x265_extract_naldata( au, nalcount);
 
         *pp_nal = &encoder->m_nals[0];
         if (pi_nal) *pi_nal = (int)nalcount;
@@ -425,70 +364,8 @@
 
     if (pp_nal && numEncoded)
     {
-        UInt memsize = 0;
-        int nalcount = 0;
-        for (AccessUnit::const_iterator t = au.begin(); t != au.end(); t++)
-        {
-            const NALUnitEBSP& temp = **t;
-            memsize += temp.m_packetSize + 4;
-            nalcount++;
-        }
-
-        if (encoder->m_nals)
-            X265_FREE(encoder->m_nals);
-
-        if (encoder->m_packetData)
-           X265_FREE(encoder->m_packetData);
-
-        encoder->m_packetData = (char*)X265_MALLOC(char, memsize);
-        encoder->m_nals = (x265_nal_t*)X265_MALLOC(x265_nal_t, nalcount);
-        nalcount = 0;
-        memsize = 0;
-
-        /* Copy NAL output packets into x265_nal_t structures */
-        for (AccessUnit::const_iterator it = au.begin(); it != au.end(); it++)
-        {
-            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)
-            {
-                /* From AVC, When any of the following conditions are fulfilled, the
-                    * zero_byte syntax element shall be present:
-                    *  - the nal_unit_type within the nal_unit() is equal to 7 (sequence
-                    *    parameter set) or 8 (picture parameter set),
-                    *  - the byte stream NAL unit syntax structure contains the first NAL
-                    *    unit of an access unit in decoding order, as specified by subclause
-                    *    7.4.1.2.3.
-                    */
-                ::memcpy(encoder->m_packetData + memsize, start_code_prefix, 4);
-                size += 4;
-            }
-            else
-            {
-                ::memcpy(encoder->m_packetData + memsize, start_code_prefix + 1, 3);
-                size += 3;
-            }
-            memsize += size;
-            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 */
-        size_t offset = 0;
-        for (size_t i = 0; i < (size_t)nalcount; i++)
-        {
-            encoder->m_nals[i].p_payload = (uint8_t*)encoder->m_packetData + offset;
-            offset += encoder->m_nals[i].i_payload;
-        }
+        size_t nalcount;
+        encoder->x265_extract_naldata( au, nalcount);
 
         *pp_nal = &encoder->m_nals[0];
         if (pi_nal) *pi_nal =(int) nalcount;
@@ -520,3 +397,71 @@
     destroyROM();
     BitCost::destroy();
 }
+
+void x265_t::x265_extract_naldata( AccessUnit &au, size_t &nalcount)
+{
+        UInt memsize = 0;
+        nalcount = 0;
+        for (AccessUnit::const_iterator t = au.begin(); t != au.end(); t++)
+        {
+            const NALUnitEBSP& temp = **t;
+            memsize += temp.m_packetSize + 4;
+            nalcount++;
+        }
+
+        if (m_packetData)
+            X265_FREE(m_packetData);
+
+        if (m_nals)
+            X265_FREE(m_nals);
+
+        m_packetData = (char*)X265_MALLOC(char, memsize);
+        m_nals = (x265_nal_t*)X265_MALLOC(x265_nal_t, nalcount);
+        nalcount = 0;
+        memsize = 0;
+
+        /* Copy NAL output packets into x265_nal_t structures */
+        for (AccessUnit::const_iterator it = au.begin(); it != au.end(); it++)
+        {
+            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)
+            {
+                /* From AVC, When any of the following conditions are fulfilled, the
+                    * zero_byte syntax element shall be present:
+                    *  - the nal_unit_type within the nal_unit() is equal to 7 (sequence
+                    *    parameter set) or 8 (picture parameter set),
+                    *  - the byte stream NAL unit syntax structure contains the first NAL
+                    *    unit of an access unit in decoding order, as specified by subclause
+                    *    7.4.1.2.3.
+                    */
+                ::memcpy(m_packetData + memsize, start_code_prefix, 4);
+                size += 4;
+            }
+            else
+            {
+                ::memcpy(m_packetData + memsize, start_code_prefix + 1, 3);
+                size += 3;
+            }
+            memsize += size;
+            size_t nalSize = nalu.m_packetSize;
+            ::memcpy(m_packetData + memsize, nalu.m_nalUnitData, nalSize);
+            size += (int)nalSize;
+            memsize += (int)nalSize;
+
+            m_nals[nalcount].i_type = nalu.m_nalUnitType;
+            m_nals[nalcount].i_payload = size;
+            nalcount++;
+            free(nalu.m_nalUnitData);
+        }
+         
+        /* Setup payload pointers, now that we're done adding content to m_packetData */
+        size_t offset = 0;
+        for (size_t i = 0; i < (size_t)nalcount; i++)
+        {
+            m_nals[i].p_payload = (uint8_t*)m_packetData + offset;
+            offset += m_nals[i].i_payload;
+        }
+}


More information about the x265-devel mailing list