[x265-commits] [x265] nal: add more padding for corner cases

Steve Borho steve at borho.org
Fri Jun 6 05:23:43 CEST 2014


details:   http://hg.videolan.org/x265/rev/ecbe8796febc
branches:  
changeset: 7018:ecbe8796febc
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 14:50:09 2014 -0500
description:
nal: add more padding for corner cases
Subject: [x265] merge TComBitCounter into TComBitStream.h

details:   http://hg.videolan.org/x265/rev/ac044bf2f850
branches:  
changeset: 7019:ac044bf2f850
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 16:55:09 2014 -0500
description:
merge TComBitCounter into TComBitStream.h
Subject: [x265] always include CommonDef.h via common.h, avoid include loops

details:   http://hg.videolan.org/x265/rev/0cdfd02d6b1a
branches:  
changeset: 7020:0cdfd02d6b1a
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 16:22:03 2014 -0500
description:
always include CommonDef.h via common.h, avoid include loops

Remove some spurious stdlib includes not in common.h
Subject: [x265] TComSlice: remove layer violation include

details:   http://hg.videolan.org/x265/rev/9f6dca97e01f
branches:  
changeset: 7021:9f6dca97e01f
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 16:58:12 2014 -0500
description:
TComSlice: remove layer violation include

files in common/ shouldn't include encoder/ headers
Subject: [x265] bitstream: simplify TComBitStream interface and code

details:   http://hg.videolan.org/x265/rev/467d0e89a52f
branches:  
changeset: 7022:467d0e89a52f
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 18:17:17 2014 -0500
description:
bitstream: simplify TComBitStream interface and code
Subject: [x265] nal: add back comments now that member variables are gone

details:   http://hg.videolan.org/x265/rev/420122606888
branches:  
changeset: 7023:420122606888
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 18:23:22 2014 -0500
description:
nal: add back comments now that member variables are gone
Subject: [x265] bitstream: fast-path for appending bitstreams

details:   http://hg.videolan.org/x265/rev/47fdb8041e9d
branches:  
changeset: 7024:47fdb8041e9d
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 20:06:32 2014 -0500
description:
bitstream: fast-path for appending bitstreams
Subject: [x265] common: include cctype for isdigit

details:   http://hg.videolan.org/x265/rev/25b8aa22e4c8
branches:  
changeset: 7025:25b8aa22e4c8
user:      Steve Borho <steve at borho.org>
date:      Thu Jun 05 21:40:56 2014 -0500
description:
common: include cctype for isdigit

diffstat:

 source/Lib/TLibCommon/CommonDef.h                 |    2 -
 source/Lib/TLibCommon/TComBitCounter.h            |   77 --------
 source/Lib/TLibCommon/TComBitStream.cpp           |  190 ++++++++-------------
 source/Lib/TLibCommon/TComBitStream.h             |  124 ++++----------
 source/Lib/TLibCommon/TComDataCU.h                |    2 +-
 source/Lib/TLibCommon/TComLoopFilter.h            |    2 +-
 source/Lib/TLibCommon/TComMotionInfo.h            |    1 -
 source/Lib/TLibCommon/TComPattern.h               |    2 +-
 source/Lib/TLibCommon/TComPic.h                   |    2 +-
 source/Lib/TLibCommon/TComPicSym.h                |    2 +-
 source/Lib/TLibCommon/TComPicYuv.h                |    2 +-
 source/Lib/TLibCommon/TComRom.cpp                 |    5 +-
 source/Lib/TLibCommon/TComRom.h                   |    4 +-
 source/Lib/TLibCommon/TComSampleAdaptiveOffset.h  |    2 +-
 source/Lib/TLibCommon/TComSlice.cpp               |    4 +-
 source/Lib/TLibCommon/TComSlice.h                 |    3 +-
 source/Lib/TLibCommon/TComTrQuant.h               |    2 +-
 source/Lib/TLibCommon/TComYuv.cpp                 |    2 +-
 source/Lib/TLibCommon/TComYuv.h                   |    2 +-
 source/Lib/TLibEncoder/SEIwrite.cpp               |    1 -
 source/Lib/TLibEncoder/SyntaxElementWriter.cpp    |    2 +-
 source/Lib/TLibEncoder/SyntaxElementWriter.h      |    2 +-
 source/Lib/TLibEncoder/TEncCu.h                   |    3 +-
 source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.h |    1 -
 source/common/CMakeLists.txt                      |    1 -
 source/common/common.cpp                          |    3 +-
 source/common/common.h                            |    3 +-
 source/encoder/cturow.h                           |    1 -
 source/encoder/frameencoder.cpp                   |    4 +-
 source/encoder/frameencoder.h                     |    1 -
 source/encoder/level.cpp                          |    1 -
 source/encoder/level.h                            |    1 -
 source/encoder/nal.cpp                            |   20 +-
 source/test/testharness.h                         |    1 -
 34 files changed, 144 insertions(+), 331 deletions(-)

diffs (truncated from 944 to 300 lines):

diff -r 9dddc48aecc7 -r 25b8aa22e4c8 source/Lib/TLibCommon/CommonDef.h
--- a/source/Lib/TLibCommon/CommonDef.h	Thu Jun 05 13:05:33 2014 +0530
+++ b/source/Lib/TLibCommon/CommonDef.h	Thu Jun 05 21:40:56 2014 -0500
@@ -38,8 +38,6 @@
 #ifndef X265_COMMONDEF_H
 #define X265_COMMONDEF_H
 
-#include <cstdlib>
-#include "common.h"
 #include "TypeDef.h"
 
 //! \ingroup TLibCommon
diff -r 9dddc48aecc7 -r 25b8aa22e4c8 source/Lib/TLibCommon/TComBitCounter.h
--- a/source/Lib/TLibCommon/TComBitCounter.h	Thu Jun 05 13:05:33 2014 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,77 +0,0 @@
-/* The copyright in this software is being made available under the BSD
- * License, included below. This software may be subject to other third party
- * and contributor rights, including patent rights, and no such rights are
- * granted under this license.
- *
- * Copyright (c) 2010-2013, ITU/ISO/IEC
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *  * Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
- *    be used to endorse or promote products derived from this software without
- *    specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** \file     TComBitCounter.h
-    \brief    Class for counting bits (header)
-*/
-
-#ifndef X265_COMBITCOUNTER_H
-#define X265_COMBITCOUNTER_H
-
-#include "TComBitStream.h"
-
-namespace x265 {
-// private namespace
-
-//! \ingroup TLibCommon
-//! \{
-
-// ====================================================================================================================
-// Class definition
-// ====================================================================================================================
-
-/// class for counting bits
-class TComBitCounter : public TComBitIf
-{
-protected:
-
-    uint32_t  m_uiBitCounter;
-
-public:
-
-    TComBitCounter()            {}
-
-    virtual ~TComBitCounter()   {}
-
-    void        write(uint32_t /*uiBits*/, uint32_t uiNumberOfBits)  { m_uiBitCounter += uiNumberOfBits; }
-
-    void        writeByte(uint32_t /*val*/)                      { m_uiBitCounter += 8; }
-
-    void        resetBits()                                  { m_uiBitCounter = 0; }
-
-    uint32_t getNumberOfWrittenBits() const { return m_uiBitCounter; }
-};
-}
-//! \}
-
-#endif // ifndef X265_COMBITCOUNTER_H
diff -r 9dddc48aecc7 -r 25b8aa22e4c8 source/Lib/TLibCommon/TComBitStream.cpp
--- a/source/Lib/TLibCommon/TComBitStream.cpp	Thu Jun 05 13:05:33 2014 +0530
+++ b/source/Lib/TLibCommon/TComBitStream.cpp	Thu Jun 05 21:40:56 2014 -0500
@@ -31,141 +31,101 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/** \file     TComBitStream.cpp
-    \brief    class for handling bitstream
-*/
-
+#include "common.h"
 #include "TComBitStream.h"
-#include "common.h"
 
 using namespace x265;
 
-//! \ingroup TLibCommon
-//! \{
-
-// ====================================================================================================================
-// Constructor / destructor / create / destroy
-// ====================================================================================================================
+#define MIN_FIFO_SIZE 1000
 
 TComOutputBitstream::TComOutputBitstream()
 {
     m_fifo = X265_MALLOC(uint8_t, MIN_FIFO_SIZE);
-    m_buffsize = MIN_FIFO_SIZE;
+    m_byteAlloc = MIN_FIFO_SIZE;
     clear();
 }
 
-TComOutputBitstream::~TComOutputBitstream()
+void TComOutputBitstream::push_back(uint8_t val)
 {
-    X265_FREE(m_fifo);
+    if (!m_fifo)
+        return;
+
+    if (m_byteOccupancy >= m_byteAlloc)
+    {
+        /** reallocate buffer with doubled size */
+        uint8_t *temp = X265_MALLOC(uint8_t, m_byteAlloc * 2);
+        if (temp)
+        {
+            ::memcpy(temp, m_fifo, m_byteOccupancy);
+            X265_FREE(m_fifo);
+            m_fifo = temp;
+            m_byteAlloc *= 2;
+        }
+        else
+        {
+            x265_log(NULL, X265_LOG_ERROR, "Unable to realloc bitstream buffer");
+            return;
+        }
+    }
+    m_fifo[m_byteOccupancy++] = val;
 }
 
-// ====================================================================================================================
-// Public member functions
-// ====================================================================================================================
-
-char* TComOutputBitstream::getByteStream() const
-{
-    return (char*)m_fifo;
-}
-
-uint32_t TComOutputBitstream::getByteStreamLength() const
-{
-    return m_fsize;
-}
-
-void TComOutputBitstream::clear()
-{
-    m_held_bits = 0;
-    m_num_held_bits = 0;
-    m_fsize = 0;
-}
-
-void TComOutputBitstream::write(uint32_t bits, uint32_t numBits)
+void TComOutputBitstream::write(uint32_t val, uint32_t numBits)
 {
     X265_CHECK(numBits <= 32, "numBits out of range\n");
-    X265_CHECK(numBits == 32 || (bits & (~0 << numBits)) == 0, "numBits & bits out of range\n");
+    X265_CHECK(numBits == 32 || (val & (~0 << numBits)) == 0, "numBits & val out of range\n");
 
-    /* any modulo 8 remainder of num_total_bits cannot be written this time,
-     * and will be held until next time. */
-    uint32_t num_total_bits = numBits + m_num_held_bits;
-    uint32_t next_num_held_bits = num_total_bits & 7;
+    uint32_t totalPartialBits = m_partialByteBits + numBits;
+    uint32_t nextPartialBits = totalPartialBits & 7;
+    uint8_t  nextHeldByte = val << (8 - nextPartialBits);
+    uint32_t writeBytes = totalPartialBits >> 3;
 
-    /* form a byte aligned word (write_bits), by concatenating any held bits
-     * with the new bits, discarding the bits that will form the next_held_bits.
-     * eg: H = held bits, V = n new bits        /---- next_held_bits
-     * len(H)=7, len(V)=1: ... ---- HHHH HHHV . 0000 0000, next_num_held_bits=0
-     * len(H)=7, len(V)=2: ... ---- HHHH HHHV . V000 0000, next_num_held_bits=1
-     * if total_bits < 8, the value of v_ is not used */
-    uint8_t next_held_bits = bits << (8 - next_num_held_bits);
+    if (writeBytes)
+    {
+        /* topword aligns m_partialByte with the msb of val */
+        uint32_t topword = (numBits - nextPartialBits) & ~7;
+        uint32_t write_bits = (m_partialByte << topword) | (val >> nextPartialBits);
 
-    if (!(num_total_bits >> 3))
+        switch (writeBytes)
+        {
+        case 4: push_back(write_bits >> 24);
+        case 3: push_back(write_bits >> 16);
+        case 2: push_back(write_bits >> 8);
+        case 1: push_back(write_bits);
+        }
+
+        m_partialByte = nextHeldByte;
+        m_partialByteBits = nextPartialBits;
+    }
+    else
     {
-        /* insufficient bits accumulated to write out, append new_held_bits to
-         * current held_bits */
-        /* NB, this requires that v only contains 0 in bit positions {31..n} */
-        m_held_bits |= next_held_bits;
-        m_num_held_bits = next_num_held_bits;
-        return;
+        m_partialByte |= nextHeldByte;
+        m_partialByteBits = nextPartialBits;
     }
-
-    /* topword serves to justify held_bits to align with the msb of uiBits */
-    uint32_t topword = (numBits - next_num_held_bits) & ~((1 << 3) - 1);
-    uint32_t write_bits = (m_held_bits << topword) | (bits >> next_num_held_bits);
-
-    switch (num_total_bits >> 3)
-    {
-    case 4: push_back(write_bits >> 24);
-    case 3: push_back(write_bits >> 16);
-    case 2: push_back(write_bits >> 8);
-    case 1: push_back(write_bits);
-    }
-
-    m_held_bits = next_held_bits;
-    m_num_held_bits = next_num_held_bits;
 }
 
 void TComOutputBitstream::writeByte(uint32_t val)
 {
-    // NOTE: we are here only in Cabac
-    X265_CHECK(!m_num_held_bits, "expecting m_num_held_bits = 0\n");
+    // Only CABAC will call writeByte, the fifo must be byte aligned
+    X265_CHECK(!m_partialByteBits, "expecting m_partialByteBits = 0\n");
 
     push_back(val);
 }
 
 void TComOutputBitstream::writeAlignOne()
 {
-    uint32_t numBits = getNumBitsUntilByteAligned();
+    uint32_t numBits = (8 - m_partialByteBits) & 0x7;
 
     write((1 << numBits) - 1, numBits);
 }
 
 void TComOutputBitstream::writeAlignZero()
 {
-    if (!m_num_held_bits)
-        return;
-
-    push_back(m_held_bits);
-    m_held_bits = 0;
-    m_num_held_bits = 0;
-}
-
-/**
- * add substream to the end of the current bitstream
- */
-void TComOutputBitstream::addSubstream(TComOutputBitstream* substream)
-{
-    uint32_t numBits = substream->getNumberOfWrittenBits();
-
-    const uint8_t* rbsp = substream->getFIFO();
-
-    for (uint32_t count = 0; count < substream->m_fsize; count++)
+    if (m_partialByteBits)
     {
-        write(rbsp[count], 8);
-    }
-
-    if (numBits & 0x7)
-    {
-        write(substream->getHeldBits() >> (8 - (numBits & 0x7)), numBits & 0x7);
+        push_back(m_partialByte);
+        m_partialByte = 0;
+        m_partialByteBits = 0;
     }
 }
 
@@ -178,12 +138,10 @@ void TComOutputBitstream::writeByteAlign
 int TComOutputBitstream::countStartCodeEmulations()
 {
     int numStartCodes = 0;
-    uint8_t *rbsp = getFIFO();


More information about the x265-commits mailing list