[x265] [PATCH] Encoder: Removed STL Containers and Replaced with Non STL classes

Steve Borho steve at borho.org
Wed Sep 4 18:17:38 CEST 2013


On Wed, Sep 4, 2013 at 4:46 AM, Gopu Govindaswamy <gopu at multicorewareinc.com
> wrote:

> # HG changeset patch
> # User Gopu Govindaswamy <gopu at multicorewareinc.com>
> # Date 1378287999 -19800
> # Node ID 177f9c0d7fe169f9d343c116b12d0aac75161c33
> # Parent  06862133aeccae74f24b792e1df45e81188f6d0e
> Encoder: Removed STL Containers and Replaced with Non STL classes
>
> diff -r 06862133aecc -r 177f9c0d7fe1 source/Lib/TLibCommon/CommonDef.h
> --- a/source/Lib/TLibCommon/CommonDef.h Wed Sep 04 13:25:03 2013 +0530
> +++ b/source/Lib/TLibCommon/CommonDef.h Wed Sep 04 15:16:39 2013 +0530
> @@ -148,10 +148,12 @@
>  // for use in HM, replaces old xMalloc/xFree macros
>  #define X265_MALLOC(type, count)    x265_malloc(sizeof(type) * (count))
>  #define X265_FREE(ptr)              x265_free(ptr)
> +#define X265_REALLOC(Address, type, count)  x265_realloc(Address,
> sizeof(type) * (count))
>
>  // new code can use these functions directly
>  extern void  x265_free(void *);
>  extern void *x265_malloc(size_t size);
> +extern void *x265_realloc(void *, size_t size);
>
>  //
> ====================================================================================================================
>  // Coding tool configuration
> diff -r 06862133aecc -r 177f9c0d7fe1 source/common/common.cpp
> --- a/source/common/common.cpp  Wed Sep 04 13:25:03 2013 +0530
> +++ b/source/common/common.cpp  Wed Sep 04 15:16:39 2013 +0530
> @@ -61,6 +61,11 @@
>      return _aligned_malloc(size, ALIGNBYTES);
>  }
>
> +void *x265_realloc(void *startAddr, size_t size)
> +{
> +    return _aligned_realloc(startAddr,size, ALIGNBYTES);
> +}
>

Unfortunately you've stepped into a mine field here.  You didn't add a
POSIX version of x265_realloc and in fact POSIX does not support an aligned
realloc function, so this would have to be implemented by hand, but it is
pretty hard to implement realloc yourself without passing in the size of
the data to be moved into the new buffer.

In the case of encoder.cpp you really do not need to use realloc, you can
discard (free) the old buffer and allocate a new one at the required size
and not use our aligned malloc macros.

Otherwise, this looks good


> +
>  void x265_free(void *ptr)
>  {
>      if (ptr) _aligned_free(ptr);
> diff -r 06862133aecc -r 177f9c0d7fe1 source/encoder/encoder.cpp
> --- a/source/encoder/encoder.cpp        Wed Sep 04 13:25:03 2013 +0530
> +++ b/source/encoder/encoder.cpp        Wed Sep 04 15:16:39 2013 +0530
> @@ -40,18 +40,28 @@
>  struct x265_t : public TEncTop
>  {
>      x265_t();
> +    ~x265_t();
>
> -    std::vector<x265_nal_t> m_nals;
> -    std::string             m_packetData;
> -
> +    x265_nal_t *m_nals;
> +    char *m_packetData;
> +
>      void configure(x265_param_t *param);
>      void determineLevelAndProfile(x265_param_t *param);
>  };
>
>  x265_t::x265_t()
>  {
> -    m_packetData.reserve(4096);
> -    m_nals.reserve(4);
> +    m_nals = (x265_nal_t *) X265_MALLOC(x265_nal_t, 1);
> +    m_packetData = (char *) X265_MALLOC(char, 1);
> +}
> +
> +x265_t::~x265_t()
> +{
> +    if (m_packetData)
> +        X265_FREE(m_packetData);
> +
> +    if(m_nals)
> +        X265_FREE(m_nals);
>  }
>
>  void x265_t::determineLevelAndProfile(x265_param_t *_param)
> @@ -324,10 +334,20 @@
>
>      AccessUnit au;
>      if (encoder->getStreamHeaders(au) == 0)
> -    {
> -        encoder->m_nals.clear();
> -        encoder->m_packetData.clear();
> -
> +    {
> +        int memsize = 0;
> +        int nalcount = 0;
> +        for (AccessUnit::const_iterator t = au.begin(); t != au.end();
> t++)
> +        {
> +            const NALUnitEBSP& temp = **t;
> +            memsize += (int) temp.m_nalUnitData.str().size();
> +            nalcount++;
> +        }
> +        encoder->m_packetData = (char *)
> X265_REALLOC(encoder->m_packetData, char, memsize);
> +        encoder->m_nals = (x265_nal_t *) X265_REALLOC(encoder->m_nals,
> 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++)
>          {
> @@ -345,35 +365,34 @@
>                      *    unit of an access unit in decoding order, as
> specified by subclause
>                      *    7.4.1.2.3.
>                      */
> -                encoder->m_packetData.append(start_code_prefix, 4);
> +                ::memcpy(encoder->m_packetData + memsize,
> start_code_prefix, 4);
>                  size += 4;
>              }
>              else
>              {
> -                encoder->m_packetData.append(start_code_prefix + 1, 3);
> +                ::memcpy(encoder->m_packetData + memsize,
> start_code_prefix + 1, 3);
>                  size += 3;
>              }
> +            memsize += size;
>              size_t nalSize = nalu.m_nalUnitData.str().size();
> -
>  encoder->m_packetData.append(nalu.m_nalUnitData.str().c_str(), nalSize);
> -            size += (int)nalSize;
> +            ::memcpy(encoder->m_packetData + memsize,
> nalu.m_nalUnitData.str().c_str(), nalSize);
> +            size += (int)nalSize;
> +            memsize += (int)nalSize;
>
> -            x265_nal_t nal;
> -            nal.i_type = nalu.m_nalUnitType;
> -            nal.i_payload = size;
> -            encoder->m_nals.push_back(nal);
> +            encoder->m_nals[nalcount].i_type = nalu.m_nalUnitType;
> +            encoder->m_nals[nalcount].i_payload = size;
> +            nalcount++;
>          }
> -
> -        /* Setup payload pointers, now that we're done adding content to
> m_packetData */
> +
>          size_t offset = 0;
> -        for (size_t i = 0; i < encoder->m_nals.size(); i++)
> +        for (size_t i = 0; i < nalcount; i++)
>          {
> -            x265_nal_t& nal = encoder->m_nals[i];
> -            nal.p_payload = (uint8_t*)encoder->m_packetData.c_str() +
> offset;
> -            offset += nal.i_payload;
> +            encoder->m_nals[i].p_payload =
> (uint8_t*)encoder->m_packetData + offset;
> +            offset += encoder->m_nals[i].i_payload;
>          }
>
>          *pp_nal = &encoder->m_nals[0];
> -        if (pi_nal) *pi_nal = (int)encoder->m_nals.size();
> +        if (pi_nal) *pi_nal = nalcount;
>          return 0;
>      }
>      else
> @@ -388,9 +407,21 @@
>      int numEncoded = encoder->encode(!pic_in, pic_in, pic_out, au);
>      if (pp_nal && numEncoded)
>      {
> -        encoder->m_nals.clear();
> -        encoder->m_packetData.clear();
> +        int memsize = 0;
> +        int nalcount = 0;
> +
> +        for (AccessUnit::const_iterator t = au.begin(); t != au.end();
> t++)
> +        {
> +            const NALUnitEBSP& temp = **t;
> +            memsize += (int) temp.m_nalUnitData.str().size();
> +            nalcount++;
> +        }
> +        encoder->m_packetData = (char *)
> X265_REALLOC(encoder->m_packetData, char, memsize);
> +        encoder->m_nals = (x265_nal_t *) X265_REALLOC(encoder->m_nals,
> 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++)
>          {
> @@ -408,35 +439,34 @@
>                      *    unit of an access unit in decoding order, as
> specified by subclause
>                      *    7.4.1.2.3.
>                      */
> -                encoder->m_packetData.append(start_code_prefix, 4);
> +                ::memcpy(encoder->m_packetData + memsize,
> start_code_prefix, 4);
>                  size += 4;
>              }
>              else
>              {
> -                encoder->m_packetData.append(start_code_prefix + 1, 3);
> +                ::memcpy(encoder->m_packetData + memsize,
> start_code_prefix + 1, 3);
>                  size += 3;
>              }
> +            memsize += size;
>              size_t nalSize = nalu.m_nalUnitData.str().size();
> -
>  encoder->m_packetData.append(nalu.m_nalUnitData.str().c_str(), nalSize);
> +            ::memcpy(encoder->m_packetData + memsize,
> nalu.m_nalUnitData.str().c_str(), nalSize);
>              size += (int)nalSize;
> +            memsize += (int)nalSize;
>
> -            x265_nal_t nal;
> -            nal.i_type = nalu.m_nalUnitType;
> -            nal.i_payload = size;
> -            encoder->m_nals.push_back(nal);
> +            encoder->m_nals[nalcount].i_type = nalu.m_nalUnitType;
> +            encoder->m_nals[nalcount].i_payload = size;
> +            nalcount++;
>          }
> -
> -        /* Setup payload pointers, now that we're done adding content to
> m_packetData */
> +
>          size_t offset = 0;
> -        for (size_t i = 0; i < encoder->m_nals.size(); i++)
> +        for (size_t i = 0; i < nalcount; i++)
>          {
> -            x265_nal_t& nal = encoder->m_nals[i];
> -            nal.p_payload = (uint8_t*)encoder->m_packetData.c_str() +
> offset;
> -            offset += nal.i_payload;
> +            encoder->m_nals[i].p_payload =
> (uint8_t*)encoder->m_packetData + offset;
> +            offset += encoder->m_nals[i].i_payload;
>          }
> -
> +
>          *pp_nal = &encoder->m_nals[0];
> -        if (pi_nal) *pi_nal = (int)encoder->m_nals.size();
> +        if (pi_nal) *pi_nal = nalcount;
>      }
>      else if (pi_nal)
>          *pi_nal = 0;
> _______________________________________________
> x265-devel mailing list
> x265-devel at videolan.org
> https://mailman.videolan.org/listinfo/x265-devel
>



-- 
Steve Borho
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.videolan.org/private/x265-devel/attachments/20130904/0110607d/attachment-0001.html>


More information about the x265-devel mailing list