Moved common code out of H264Parser.

This moves common parsing code out of the H.264 parser.  This is in
preparation for the H.265 parser to use.

Change-Id: Id05b55f6adc2ea31e97c8a9f22023e851808a8a7
This commit is contained in:
Jacob Trimble 2016-03-03 16:19:36 -08:00
parent 0803e31836
commit 0ca1160474
5 changed files with 54 additions and 57 deletions

View File

@ -86,6 +86,48 @@ bool H264BitReader::ReadBits(int num_bits, int* out) {
return true;
}
bool H264BitReader::ReadUE(int* val) {
int num_bits = -1;
int bit;
int rest;
// Count the number of contiguous zero bits.
do {
if (!ReadBits(1, &bit))
return false;
num_bits++;
} while (bit == 0);
if (num_bits > 31)
return false;
// Calculate exp-Golomb code value of size num_bits.
*val = (1 << num_bits) - 1;
if (num_bits > 0) {
if (!ReadBits(num_bits, &rest))
return false;
*val += rest;
}
return true;
}
bool H264BitReader::ReadSE(int* val) {
int ue;
// See Chapter 9 in the spec.
if (!ReadUE(&ue))
return false;
if (ue % 2 == 0)
*val = -(ue / 2);
else
*val = ue / 2 + 1;
return true;
}
off_t H264BitReader::NumBitsLeft() {
return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
}

View File

@ -36,6 +36,13 @@ class H264BitReader {
// bits in the stream), true otherwise.
bool ReadBits(int num_bits, int* out);
// Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
// Read one unsigned exp-Golomb code from the stream and return in |*val|.
bool ReadUE(int* val);
// Read one signed exp-Golomb code from the stream and return in |*val|.
bool ReadSE(int* val);
// Return the number of bits left in the stream.
off_t NumBitsLeft();

View File

@ -115,13 +115,11 @@ H264SEIMessage::H264SEIMessage() {
#define READ_BITS_OR_RETURN(num_bits, out) \
do { \
int _out; \
if (!br->ReadBits(num_bits, &_out)) { \
if (!br->ReadBits(num_bits, (out))) { \
DVLOG(1) \
<< "Error in stream: unexpected EOS while trying to read " #out; \
return kInvalidStream; \
} \
*out = _out; \
} while (0)
#define READ_BOOL_OR_RETURN(out) \
@ -132,12 +130,12 @@ H264SEIMessage::H264SEIMessage() {
<< "Error in stream: unexpected EOS while trying to read " #out; \
return kInvalidStream; \
} \
*out = _out != 0; \
*(out) = _out != 0; \
} while (0)
#define READ_UE_OR_RETURN(out) \
do { \
if (ReadUE(br, out) != kOk) { \
if (!br->ReadUE(out)) { \
DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
return kInvalidStream; \
} \
@ -145,7 +143,7 @@ H264SEIMessage::H264SEIMessage() {
#define READ_SE_OR_RETURN(out) \
do { \
if (ReadSE(br, out) != kOk) { \
if (!br->ReadSE(out)) { \
DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
return kInvalidStream; \
} \
@ -199,48 +197,6 @@ const H264SPS* H264Parser::GetSPS(int sps_id) {
return active_SPSes_[sps_id];
}
H264Parser::Result H264Parser::ReadUE(H264BitReader* br, int* val) {
int num_bits = -1;
int bit;
int rest;
// Count the number of contiguous zero bits.
do {
READ_BITS_OR_RETURN(1, &bit);
num_bits++;
} while (bit == 0);
if (num_bits > 31)
return kInvalidStream;
// Calculate exp-Golomb code value of size num_bits.
*val = (1 << num_bits) - 1;
if (num_bits > 0) {
READ_BITS_OR_RETURN(num_bits, &rest);
*val += rest;
}
return kOk;
}
H264Parser::Result H264Parser::ReadSE(H264BitReader* br, int* val) {
int ue;
Result res;
// See Chapter 9 in the spec.
res = ReadUE(br, &ue);
if (res != kOk)
return res;
if (ue % 2 == 0)
*val = -(ue / 2);
else
*val = ue / 2 + 1;
return kOk;
}
// Default scaling lists (per spec).
static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, };

View File

@ -285,13 +285,6 @@ class H264Parser {
Result ParseSEI(const Nalu& nalu, H264SEIMessage* sei_msg);
private:
// Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
// Read one unsigned exp-Golomb code from the stream and return in |*val|.
Result ReadUE(H264BitReader* br, int* val);
// Read one signed exp-Golomb code from the stream and return in |*val|.
Result ReadSE(H264BitReader* br, int* val);
// Parse scaling lists (see spec).
Result ParseScalingList(H264BitReader* br,
int size,

View File

@ -137,8 +137,7 @@ NaluReader::Result NaluReader::Advance(Nalu* nalu) {
DVLOG(4) << "NALU type: " << static_cast<int>(nalu->type())
<< " at: " << reinterpret_cast<const void*>(nalu->data())
<< " data size: " << nalu->payload_size()
<< " ref: " << static_cast<int>(nalu->ref_idc());
<< " data size: " << nalu->payload_size();
return NaluReader::kOk;
}