<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic H264 Encoder: bitstream buffer expansion code in Intel® Integrated Performance Primitives</title>
    <link>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994926#M22818</link>
    <description>&lt;P&gt;Dear Experts,&lt;/P&gt;
&lt;P&gt;If ALT_BITSTREAM_ALLOC flag is set. under certain conditions the encoder may attempt to expand previously allocated bitstream buffer for a particular slice (umc_h264_core_enc_tmpl.cpp.h). The memory is released by making a "free" call, then re-allocated. There seems to be a problem with this though because as far as I understand, the pointer being freed may not be the one previously obtained by making an "alloc" call: H264CoreEncoder_Init function (umc_h264_gen_enc_tmpl.cpp.h) allocates a single large buffer and assigns pointers into this large buffer for each slice. Am I correct in believing that this code may cause heap corruption?&lt;/P&gt;
&lt;P&gt;The encoder crash I was experiencing seemed to go away after I disabled ALT_BITSTREAM_ALLOC and allowed a bigger bitstream buffer, but, on the other hand, my attempt to keep the resizing code and add changes such that individual buffers are allocated for each slice didn't work well.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 26 Sep 2012 18:48:43 GMT</pubDate>
    <dc:creator>Serguei_S_1</dc:creator>
    <dc:date>2012-09-26T18:48:43Z</dc:date>
    <item>
      <title>H264 Encoder: bitstream buffer expansion code</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994926#M22818</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;
&lt;P&gt;If ALT_BITSTREAM_ALLOC flag is set. under certain conditions the encoder may attempt to expand previously allocated bitstream buffer for a particular slice (umc_h264_core_enc_tmpl.cpp.h). The memory is released by making a "free" call, then re-allocated. There seems to be a problem with this though because as far as I understand, the pointer being freed may not be the one previously obtained by making an "alloc" call: H264CoreEncoder_Init function (umc_h264_gen_enc_tmpl.cpp.h) allocates a single large buffer and assigns pointers into this large buffer for each slice. Am I correct in believing that this code may cause heap corruption?&lt;/P&gt;
&lt;P&gt;The encoder crash I was experiencing seemed to go away after I disabled ALT_BITSTREAM_ALLOC and allowed a bigger bitstream buffer, but, on the other hand, my attempt to keep the resizing code and add changes such that individual buffers are allocated for each slice didn't work well.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2012 18:48:43 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994926#M22818</guid>
      <dc:creator>Serguei_S_1</dc:creator>
      <dc:date>2012-09-26T18:48:43Z</dc:date>
    </item>
    <item>
      <title>Good day.</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994927#M22819</link>
      <description>Good day.

Yes it seems that this code doesn't work properly.
I made some modifications and tested it with small buffer setting, works fine now.

You need to separate buffer allocations for each slice and update destructor:
[cpp]
template&lt;TYPENAME coeffstype=""&gt;
Status H264CoreEncoder_Init(void* state, BaseCodecParams* init, MemoryAllocator* pMemAlloc)
{
...
    //Ipp32u bsSize = core_enc-&amp;gt;m_PaddedSize.width; // small buffer test
    Ipp32u bsSize = core_enc-&amp;gt;m_PaddedSize.width * core_enc-&amp;gt;m_PaddedSize.height * sizeof(PIXTYPE);
    bsSize += (bsSize &amp;gt;&amp;gt; 1) + 4096;
    // TBD: see if buffer size can be reduced

    core_enc-&amp;gt;m_pbitstreams = (H264BsReal**)H264_Malloc(numOfSliceEncs * sizeof(H264BsReal*));
    if(!core_enc-&amp;gt;m_pbitstreams)
        return UMC_ERR_ALLOC;

    for (i = 0; i &amp;lt; numOfSliceEncs; i++)
    {
        Ipp8u* pTmpBuffer = (Ipp8u*)H264_Malloc(bsSize + DATA_ALIGN);
        if(!pTmpBuffer)
            return UMC_ERR_ALLOC;

        core_enc-&amp;gt;m_pbitstreams&lt;I&gt; = (H264BsReal*)H264_Malloc(sizeof(H264BsReal));
        if (!core_enc-&amp;gt;m_pbitstreams&lt;I&gt;)
            return UMC_ERR_ALLOC;

        H264BsReal_Create(core_enc-&amp;gt;m_pbitstreams&lt;I&gt;, pTmpBuffer, bsSize, core_enc-&amp;gt;m_params.chroma_format_idc, status);
        if (status != UMC_OK)
            return status;

        core_enc-&amp;gt;m_Slices&lt;I&gt;.m_pbitstream = (H264BsBase*)core_enc-&amp;gt;m_pbitstreams&lt;I&gt;;
    }
    core_enc-&amp;gt;m_bs1 = core_enc-&amp;gt;m_pbitstreams[0]; // core_enc-&amp;gt;m_bs1 is the main stream.
...
}

...

template&lt;TYPENAME coeffstype=""&gt;
Status H264CoreEncoder_Close(void* state)
{
...
    if (core_enc-&amp;gt;m_pbitstreams)
    {
        Ipp32s i;
        for (i = 0; i &amp;lt; core_enc-&amp;gt;m_params.num_slices*((core_enc-&amp;gt;m_params.coding_type == 1) + 1); i++)
        {  //TODO fix for PicAFF/AFRM
            if (core_enc-&amp;gt;m_pbitstreams&lt;I&gt;)
            {
                H264_Free(core_enc-&amp;gt;m_pbitstreams&lt;I&gt;-&amp;gt;m_base.m_pbsBase);
                H264_Free(core_enc-&amp;gt;m_pbitstreams&lt;I&gt;);
                core_enc-&amp;gt;m_pbitstreams&lt;I&gt; = NULL;
            }
        }
        H264_Free(core_enc-&amp;gt;m_pbitstreams);
        core_enc-&amp;gt;m_pbitstreams = NULL;
...
}
[/cpp]

And fix data shifts in reallocator:
[cpp]
template&lt;TYPENAME coeffstype=""&gt;
Status H264CoreEncoder_Compress_Slice(void* state, H264Slice&lt;COEFFSTYPE&gt; *curr_slice, bool is_first_mb)
{
...
#ifdef ALT_BITSTREAM_ALLOC
            //Expand buffer if it is nearly full
            Ipp32u bytesInBuffer = H264BsBase_GetBsSize(&amp;amp;(pBitstream-&amp;gt;m_base));

            if (bytesInBuffer &amp;gt;= 3 * (pBitstream-&amp;gt;m_base.m_maxBsSize &amp;gt;&amp;gt; 2))
            {
                pBitstream-&amp;gt;m_base.m_maxBsSize &amp;lt;&amp;lt;= 1;
                size_t iRBSPShift = pBitstream-&amp;gt;m_pbsRBSPBase - pBitstream-&amp;gt;m_base.m_pbsBase;
                Ipp8u* tmpBitstreamBuf = (Ipp8u*)H264_Malloc(pBitstream-&amp;gt;m_base.m_maxBsSize + DATA_ALIGN);
                ippsSet_8u(0, (Ipp8u*)tmpBitstreamBuf, pBitstream-&amp;gt;m_base.m_maxBsSize);
                ippsCopy_8u(pBitstream-&amp;gt;m_base.m_pbsBase,tmpBitstreamBuf,bytesInBuffer);
                H264_Free(pBitstream-&amp;gt;m_base.m_pbsBase);
                pBitstream-&amp;gt;m_base.m_pbsBase = tmpBitstreamBuf;
                pBitstream-&amp;gt;m_pbsRBSPBase = tmpBitstreamBuf + iRBSPShift;
                pBitstream-&amp;gt;m_base.m_pbs = pBitstream-&amp;gt;m_base.m_pbsBase + ((pBitstream-&amp;gt;m_base.m_bitOffset)?bytesInBuffer - 1:bytesInBuffer);
            }
#endif
...
}
[/cpp]&lt;/COEFFSTYPE&gt;&lt;/TYPENAME&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/TYPENAME&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/TYPENAME&gt;</description>
      <pubDate>Wed, 10 Oct 2012 13:33:16 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994927#M22819</guid>
      <dc:creator>Pavel_V_Intel</dc:creator>
      <dc:date>2012-10-10T13:33:16Z</dc:date>
    </item>
    <item>
      <title>Dear Expert,</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994928#M22820</link>
      <description>Dear Expert,

Thank you for your response. I'd like to suggest a couple more fixes in addition to the changes you posted.

1) Original code used to align the allocated pointer before using it. I made changes, based on your modifications, to align the pointer (in Init() and Compress_Slice() methods). I also redefined m_pAllocEncoderInst to serve as an array of unaligned pointers as returned by malloc() so that free() can be called on the pointers once they are no longer needed when reallocating a buffer or releasing as a part of Close().

2) In the version of the code I am working with, there are pointers to the bitstream buffer initialized prior to the point where it may be decided that the buffer needs to be expanded. I made changes to the Compress_Slice() method to ensure that the pointers are reassigned appropriately in relation to the new (expanded) buffer if the expansion occurs. 

After making the fixes mentioned above (especially the latter) I seem to be no longer experiencing the issue with the encoder crashing. I'm attaching the clip I was using to reproduce the issue, just in case if someone feels like experimenting with it.

Thanks!</description>
      <pubDate>Sat, 10 Nov 2012 00:13:37 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994928#M22820</guid>
      <dc:creator>Serguei_S_1</dc:creator>
      <dc:date>2012-11-10T00:13:37Z</dc:date>
    </item>
    <item>
      <title>the issue is escalated. here</title>
      <link>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994929#M22821</link>
      <description>the issue is escalated. here is the number of this problem for your information - DPD200306434</description>
      <pubDate>Tue, 13 Nov 2012 07:57:28 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Integrated-Performance/H264-Encoder-bitstream-buffer-expansion-code/m-p/994929#M22821</guid>
      <dc:creator>Gennady_F_Intel</dc:creator>
      <dc:date>2012-11-13T07:57:28Z</dc:date>
    </item>
  </channel>
</rss>

