[x265] [PATCH] Tcombitstream: Encoded Bit stream storage (fifo) moved from std::vector to Non STL Class
Gopu Govindaswamy
gopu at multicorewareinc.com
Thu Oct 3 14:50:20 CEST 2013
# HG changeset patch
# User Gopu Govindaswamy <gopu at multicorewareinc.com>
# Date 1380804454 -19800
# Node ID 32a0b4fe9a342a1530949865e3b5a5651b6a9700
# Parent 4f68ed1126b6f0b0f24e9959ee3c3e5ade65c822
Tcombitstream: Encoded Bit stream storage (fifo) moved from std::vector to Non STL Class
1.Removed std::vector Class from Tcombitstream
2.Removed std::search_n algorithm from Tcombitstream
3.Implemented fifo using Pointers to an array - Encoded Bit stream storage
diff -r 4f68ed1126b6 -r 32a0b4fe9a34 source/Lib/TLibCommon/TComBitStream.cpp
--- a/source/Lib/TLibCommon/TComBitStream.cpp Tue Oct 01 13:46:27 2013 +0530
+++ b/source/Lib/TLibCommon/TComBitStream.cpp Thu Oct 03 18:17:34 2013 +0530
@@ -40,6 +40,7 @@
#include "TComBitStream.h"
#include <string.h>
#include <memory.h>
+#include "common.h"
using namespace std;
using namespace x265;
@@ -53,13 +54,13 @@
TComOutputBitstream::TComOutputBitstream()
{
- m_fifo = new vector<uint8_t>;
+ m_fifo = (uint8_t *)X265_MALLOC(uint8_t, MIN_FIFO_SIZE);
clear();
}
TComOutputBitstream::~TComOutputBitstream()
{
- delete m_fifo;
+ X265_FREE(m_fifo);
}
// ====================================================================================================================
@@ -68,19 +69,20 @@
char* TComOutputBitstream::getByteStream() const
{
- return (char*)&m_fifo->front();
+ return (char*)m_fifo;
}
UInt TComOutputBitstream::getByteStreamLength()
{
- return UInt(m_fifo->size());
+ return m_fsize;
}
void TComOutputBitstream::clear()
{
- m_fifo->clear();
m_held_bits = 0;
m_num_held_bits = 0;
+ m_fsize = 0;
+ buffsize = MIN_FIFO_SIZE;
}
void TComOutputBitstream::write(UInt uiBits, UInt uiNumberOfBits)
@@ -117,10 +119,10 @@
switch (num_total_bits >> 3)
{
- case 4: m_fifo->push_back(write_bits >> 24);
- case 3: m_fifo->push_back(write_bits >> 16);
- case 2: m_fifo->push_back(write_bits >> 8);
- case 1: m_fifo->push_back(write_bits);
+ 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;
@@ -140,7 +142,7 @@
{
return;
}
- m_fifo->push_back(m_held_bits);
+ push_back(m_held_bits);
m_held_bits = 0;
m_num_held_bits = 0;
}
@@ -153,14 +155,14 @@
void TComOutputBitstream::addSubstream(TComOutputBitstream* pcSubstream)
{
UInt uiNumBits = pcSubstream->getNumberOfWrittenBits();
-
- const vector<uint8_t>& rbsp = pcSubstream->getFIFO();
-
- for (vector<uint8_t>::const_iterator it = rbsp.begin(); it != rbsp.end(); )
+
+ const uint8_t* rbsp = pcSubstream->getFIFO();
+
+ for (UInt count = 0; count < pcSubstream->m_fsize; count++)
{
- write(*it++, 8);
+ write(rbsp[count], 8);
}
-
+
if (uiNumBits & 0x7)
{
write(pcSubstream->getHeldBits() >> (8 - (uiNumBits & 0x7)), uiNumBits & 0x7);
@@ -176,36 +178,49 @@
int TComOutputBitstream::countStartCodeEmulations()
{
UInt cnt = 0;
-
- vector<uint8_t>& rbsp = getFIFO();
- for (vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end(); )
+ uint8_t *rbsp = getFIFO();
+ UInt fsize = getByteStreamLength();
+
+ for (UInt count = 0; count < fsize; count++)
{
- vector<uint8_t>::iterator found = it;
- do
- {
- // find the next emulated 00 00 {00,01,02,03}
- // NB, end()-1, prevents finding a trailing two byte sequence
- found = search_n(found, rbsp.end() - 1, 2, 0);
- found++;
- // if not found, found == end, otherwise found = second zero byte
- if (found == rbsp.end())
- {
- break;
- }
- if (*(++found) <= 3)
- {
- break;
- }
- }
- while (true);
- it = found;
- if (found != rbsp.end())
+ if ((rbsp[count + 2] == 0x00 || rbsp[count + 2] == 0x01 || rbsp[count + 2] == 0x02 || rbsp[count + 2] == 0x03)
+ && rbsp[count + 1] == 0x00 && rbsp[count] == 0x00)
{
cnt++;
+ count = count + 3;
}
}
-
return cnt;
}
+void TComOutputBitstream::push_back(uint8_t val)
+{
+
+ /** Chenck FIFO Size if not reached MIN_FIFO_SIZE and Check Allocated m_fifo Buffer
+ before push the encoded bit stream to m_fifo */
+ if (m_fsize < buffsize && m_fifo)
+ {
+ m_fifo[m_fsize] = val;
+ m_fsize++;
+ }
+ else
+ {
+ buffsize += MIN_FIFO_SIZE;
+ /** FIFO size is Reached into MIN_FIFO_SIZE then Reallocate the FIFO and Copy the fifo to new memory
+ location and continue to push encoded bit streams */
+ uint8_t *temp = (uint8_t *)X265_MALLOC(uint8_t, buffsize);
+
+ /** check Allocated buffer before copy the encoder bitstream and push into FIFO */
+ if (temp)
+ {
+ ::memcpy(temp, m_fifo, m_fsize);
+ temp[m_fsize] = val;
+ m_fsize++;
+ X265_FREE(m_fifo);
+
+ /** point the reallocated buffer from temp to fifo, this can be free'd in Distructor */
+ m_fifo = temp;
+ }
+ }
+}
//! \}
diff -r 4f68ed1126b6 -r 32a0b4fe9a34 source/Lib/TLibCommon/TComBitStream.h
--- a/source/Lib/TLibCommon/TComBitStream.h Tue Oct 01 13:46:27 2013 +0530
+++ b/source/Lib/TLibCommon/TComBitStream.h Thu Oct 03 18:17:34 2013 +0530
@@ -82,7 +82,9 @@
* - &fifo.front() to get a pointer to the data array.
* NB, this pointer is only valid until the next push_back()/clear()
*/
- std::vector<uint8_t> *m_fifo;
+ uint8_t *m_fifo;
+ UInt m_fsize;
+ UInt buffsize;
UInt m_num_held_bits; /// number of bits not flushed to bytestream.
UChar m_held_bits; /// the bits held and not flushed to bytestream.
@@ -140,23 +142,24 @@
/**
* Return the number of bits that have been written since the last clear()
*/
- UInt getNumberOfWrittenBits() const { return UInt(m_fifo->size()) * 8 + m_num_held_bits; }
+ UInt getNumberOfWrittenBits() const { return m_fsize * 8 + m_num_held_bits; }
/**
* Return a reference to the internal fifo
*/
- std::vector<uint8_t>& getFIFO() { return *m_fifo; }
+ uint8_t* getFIFO() { return m_fifo; }
UChar getHeldBits() { return m_held_bits; }
/** Return a reference to the internal fifo */
- std::vector<uint8_t>& getFIFO() const { return *m_fifo; }
+ uint8_t* getFIFO() const { return m_fifo; }
void addSubstream(TComOutputBitstream* pcSubstream);
void writeByteAlignment();
//! returns the number of start code emulations contained in the current buffer
int countStartCodeEmulations();
+ void push_back(uint8_t val);
};
}
//! \}
diff -r 4f68ed1126b6 -r 32a0b4fe9a34 source/Lib/TLibEncoder/NALwrite.cpp
--- a/source/Lib/TLibEncoder/NALwrite.cpp Tue Oct 01 13:46:27 2013 +0530
+++ b/source/Lib/TLibEncoder/NALwrite.cpp Thu Oct 03 18:17:34 2013 +0530
@@ -39,6 +39,7 @@
#include <algorithm>
#include <ostream>
#include <cstring>
+#include "common.h"
using namespace std;
@@ -56,7 +57,6 @@
void write(uint8_t*& 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
@@ -88,49 +88,47 @@
* - 0x00000302
* - 0x00000303
*/
- vector<uint8_t>& rbsp = nalu.m_Bitstream.getFIFO();
-
- for (vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end(); )
+ UInt fsize = nalu.m_Bitstream.getByteStreamLength();
+ uint8_t* fifo = nalu.m_Bitstream.getFIFO();
+ uint8_t* emulation = (uint8_t *)X265_MALLOC(uint8_t, fsize + EMULATION_SIZE);
+ UInt nalsize = 0;
+
+ if (emulation)
{
- /* 1) find the next emulated 00 00 {00,01,02,03}
- * 2a) if not found, write all remaining bytes out, stop.
- * 2b) otherwise, write all non-emulated bytes out
- * 3) insert emulation_prevention_three_byte
- */
- vector<uint8_t>::iterator found = it;
- do
+ for (int count = 0; count < fsize; count++)
{
- /* NB, end()-1, prevents finding a trailing two byte sequence */
- found = search_n(found, rbsp.end() - 1, 2, 0);
- found++;
- /* if not found, found == end, otherwise found = second zero byte */
- if (found == rbsp.end())
- break;
- if (*(++found) <= 3)
- break;
+ uint8_t val = fifo[count];
+ if (count > 3 && (emulation[nalsize - 1] == 0x00 || emulation[nalsize - 1] == 0x01|| emulation[nalsize - 1] == 0x02 || emulation[nalsize - 1] == 0x03)
+ && emulation[nalsize - 2] == 0x00 && emulation[nalsize - 3] == 0x00)
+ {
+ uint8_t tmp = emulation[nalsize - 1];
+ emulation[nalsize - 1] = emulation_prevention_three_byte[0];
+ emulation[nalsize] = tmp;
+ emulation[nalsize + 1] = val;
+ nalsize++;
+ }
+ else
+ {
+ emulation[nalsize] = val;
+ }
+ nalsize++;
}
- while (true);
-
- it = found;
- if (found != rbsp.end())
+ UInt i = packetSize;
+ out = (uint8_t *) realloc (out, nalsize + 4 );
+ memcpy(out + packetSize, emulation, nalsize);
+ packetSize += nalsize;
+
+ /* 7.4.1.1
+ * ... when the last byte of the RBSP data is equal to 0x00 (which can
+ * only occur when the RBSP ends in a cabac_zero_word), a final byte equal
+ * to 0x03 is appended to the end of the data.
+ */
+ if (out[packetSize - 1] == 0x00)
{
- it = rbsp.insert(found, emulation_prevention_three_byte[0]);
+ out[i] = 3;
+ packetSize += 1;
}
- }
- UInt i = packetSize;
- out = (uint8_t *) 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
- * only occur when the RBSP ends in a cabac_zero_word), a final byte equal
- * to 0x03 is appended to the end of the data.
- */
- if (rbsp.back() == 0x00)
- {
- out[i] = 3;
- packetSize += 1;
+ X265_FREE(emulation);
}
}
diff -r 4f68ed1126b6 -r 32a0b4fe9a34 source/common/common.h
--- a/source/common/common.h Tue Oct 01 13:46:27 2013 +0530
+++ b/source/common/common.h Thu Oct 03 18:17:34 2013 +0530
@@ -96,6 +96,8 @@
#define X265_MAX4(a, b, c, d) X265_MAX((a), X265_MAX3((b), (c), (d)))
#define QP_BD_OFFSET (6*(X265_DEPTH-8))
#define MAX_NAL_UNITS 5
+#define MIN_FIFO_SIZE 1000
+#define EMULATION_SIZE 1000
#define CHECKED_MALLOC(var, type, count)\
{\
More information about the x265-devel
mailing list