Commit b0ca0c2c authored by xiaomings@google.com's avatar xiaomings@google.com

Add unittest for H264BitReader.

BUG=

TBR=jam@chromium.org

Review URL: https://chromiumcodereview.appspot.com/10834071

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149463 0039d316-1c4b-4281-b951-d872f2087c98
parent 991206b5
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/gpu/media/h264_bit_reader.h"
#include "base/logging.h"
namespace content {
H264BitReader::H264BitReader()
: data_(NULL), bytes_left_(0), curr_byte_(0),
num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0) {
}
H264BitReader::~H264BitReader() {}
bool H264BitReader::Initialize(const uint8* data, off_t size) {
DCHECK(data);
if (size < 1)
return false;
data_ = data;
bytes_left_ = size;
num_remaining_bits_in_curr_byte_ = 0;
// Initially set to 0xffff to accept all initial two-byte sequences.
prev_two_bytes_ = 0xffff;
return true;
}
bool H264BitReader::UpdateCurrByte() {
if (bytes_left_ < 1)
return false;
// Emulation prevention three-byte detection.
// If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
// Detected 0x000003, skip last byte.
++data_;
--bytes_left_;
// Need another full three bytes before we can detect the sequence again.
prev_two_bytes_ = 0xffff;
if (bytes_left_ < 1)
return false;
}
// Load a new byte and advance pointers.
curr_byte_ = *data_++ & 0xff;
--bytes_left_;
num_remaining_bits_in_curr_byte_ = 8;
prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
return true;
}
// Read |num_bits| (1 to 31 inclusive) from the stream and return them
// in |out|, with first bit in the stream as MSB in |out| at position
// (|num_bits| - 1).
bool H264BitReader::ReadBits(int num_bits, int *out) {
int bits_left = num_bits;
*out = 0;
DCHECK(num_bits <= 31);
while (num_remaining_bits_in_curr_byte_ < bits_left) {
// Take all that's left in current byte, shift to make space for the rest.
*out = (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
bits_left -= num_remaining_bits_in_curr_byte_;
if (!UpdateCurrByte())
return false;
}
*out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
*out &= ((1 << num_bits) - 1);
num_remaining_bits_in_curr_byte_ -= bits_left;
return true;
}
off_t H264BitReader::NumBitsLeft() {
return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
}
bool H264BitReader::HasMoreRBSPData() {
// Make sure we have more bits, if we are at 0 bits in current byte
// and updating current byte fails, we don't have more data anyway.
if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
return false;
// On last byte?
if (bytes_left_)
return true;
// Last byte, look for stop bit;
// We have more RBSP data if the last non-zero bit we find is not the
// first available bit.
return (curr_byte_ &
((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
}
} // namespace content
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file contains an implementation of an H264 Annex-B video stream parser.
#ifndef CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_
#define CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_
#include <sys/types.h>
#include "base/basictypes.h"
#include "content/common/content_export.h"
namespace content {
// A class to provide bit-granularity reading of H.264 streams.
// This is not a generic bit reader class, as it takes into account
// H.264 stream-specific constraints, such as skipping emulation-prevention
// bytes and stop bits. See spec for more details.
class CONTENT_EXPORT H264BitReader {
public:
H264BitReader();
~H264BitReader();
// Initialize the reader to start reading at |data|, |size| being size
// of |data| in bytes.
// Return false on insufficient size of stream..
// TODO(posciak,fischman): consider replacing Initialize() with
// heap-allocating and creating bit readers on demand instead.
bool Initialize(const uint8* data, off_t size);
// Read |num_bits| next bits from stream and return in |*out|, first bit
// from the stream starting at |num_bits| position in |*out|.
// |num_bits| may be 1-32, inclusive.
// Return false if the given number of bits cannot be read (not enough
// bits in the stream), true otherwise.
bool ReadBits(int num_bits, int *out);
// Return the number of bits left in the stream.
off_t NumBitsLeft();
// See the definition of more_rbsp_data() in spec.
bool HasMoreRBSPData();
private:
// Advance to the next byte, loading it into curr_byte_.
// Return false on end of stream.
bool UpdateCurrByte();
// Pointer to the next unread (not in curr_byte_) byte in the stream.
const uint8* data_;
// Bytes left in the stream (without the curr_byte_).
off_t bytes_left_;
// Contents of the current byte; first unread bit starting at position
// 8 - num_remaining_bits_in_curr_byte_ from MSB.
int curr_byte_;
// Number of bits remaining in curr_byte_
int num_remaining_bits_in_curr_byte_;
// Used in emulation prevention three byte detection (see spec).
// Initially set to 0xffff to accept all initial two-byte sequences.
int prev_two_bytes_;
DISALLOW_COPY_AND_ASSIGN(H264BitReader);
};
} // namespace content
#endif // CONTENT_COMMON_GPU_MEDIA_H264_BIT_READER_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "testing/gtest/include/gtest/gtest.h"
#include "content/common/gpu/media/h264_bit_reader.h"
using content::H264BitReader;
TEST(H264BitReaderTest, ReadStreamWithoutEscapeAndTrailingZeroBytes) {
H264BitReader reader;
const unsigned char rbsp[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xa0};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
EXPECT_TRUE(reader.ReadBits(1, &dummy));
EXPECT_EQ(dummy, 0x00);
EXPECT_EQ(reader.NumBitsLeft(), 47);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(8, &dummy));
EXPECT_EQ(dummy, 0x02);
EXPECT_EQ(reader.NumBitsLeft(), 39);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(31, &dummy));
EXPECT_EQ(dummy, 0x23456789);
EXPECT_EQ(reader.NumBitsLeft(), 8);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(1, &dummy));
EXPECT_EQ(dummy, 1);
EXPECT_EQ(reader.NumBitsLeft(), 7);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(1, &dummy));
EXPECT_EQ(dummy, 0);
EXPECT_EQ(reader.NumBitsLeft(), 6);
EXPECT_FALSE(reader.HasMoreRBSPData());
}
TEST(H264BitReaderTest, EmptyStream) {
H264BitReader reader;
const unsigned char rbsp[] = {0x80, 0x00, 0x00};
EXPECT_FALSE(reader.Initialize(rbsp, 0));
EXPECT_FALSE(reader.Initialize(rbsp, 1));
EXPECT_FALSE(reader.Initialize(rbsp, sizeof(rbsp)));
}
TEST(H264BitReaderTest, SingleByteStream) {
H264BitReader reader;
const unsigned char rbsp[] = {0x18};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
EXPECT_EQ(reader.NumBitsLeft(), 8);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(4, &dummy));
EXPECT_EQ(dummy, 0x01);
EXPECT_EQ(reader.NumBitsLeft(), 4);
EXPECT_FALSE(reader.HasMoreRBSPData());
}
TEST(H264BitReaderTest, StopBitOccupyFullByte) {
H264BitReader reader;
const unsigned char rbsp[] = {0xab, 0x80};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
EXPECT_EQ(reader.NumBitsLeft(), 16);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(8, &dummy));
EXPECT_EQ(dummy, 0xab);
EXPECT_EQ(reader.NumBitsLeft(), 8);
EXPECT_FALSE(reader.HasMoreRBSPData());
}
TEST(H264BitReaderTest, ReadFailure) {
H264BitReader reader;
const unsigned char rbsp[] = {0x18};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, 1));
EXPECT_FALSE(reader.ReadBits(5, &dummy));
}
TEST(H264BitReaderTest, MalformedStream) {
const unsigned char rbsp[] = {0x00, 0x00, 0x03, 0x00, 0x00};
H264BitReader reader;
EXPECT_FALSE(reader.Initialize(rbsp, 1));
EXPECT_FALSE(reader.Initialize(rbsp, sizeof(rbsp)));
}
TEST(H264BitReaderTest, EscapeSequence) {
H264BitReader reader;
const unsigned char rbsp[] =
{0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x03};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
EXPECT_TRUE(reader.ReadBits(8, &dummy));
EXPECT_EQ(dummy, 0x00);
EXPECT_EQ(reader.NumBitsLeft(), 80);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(24, &dummy));
EXPECT_EQ(dummy, 0x00);
EXPECT_EQ(reader.NumBitsLeft(), 48);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(15, &dummy));
EXPECT_EQ(dummy, 0x01);
EXPECT_EQ(reader.NumBitsLeft(), 25);
EXPECT_FALSE(reader.HasMoreRBSPData());
}
TEST(H264BitReaderTest, NonEscapeFollowedByStopBit) {
H264BitReader reader;
const unsigned char rbsp[] = {0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
EXPECT_TRUE(reader.ReadBits(23, &dummy));
EXPECT_EQ(dummy, 0x03);
EXPECT_EQ(reader.NumBitsLeft(), 33);
EXPECT_FALSE(reader.HasMoreRBSPData());
}
TEST(H264BitReaderTest, TrailingZero) {
H264BitReader reader;
const unsigned char rbsp[] = {0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00};
int dummy = 0;
EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
EXPECT_TRUE(reader.ReadBits(8, &dummy));
EXPECT_EQ(dummy, 0x01);
EXPECT_TRUE(reader.HasMoreRBSPData());
EXPECT_TRUE(reader.ReadBits(15, &dummy));
EXPECT_EQ(dummy, 0x01);
EXPECT_FALSE(reader.HasMoreRBSPData());
}
......@@ -50,103 +50,6 @@ H264SEIMessage::H264SEIMessage() {
memset(this, 0, sizeof(*this));
}
H264Parser::H264BitReader::H264BitReader()
: data_(NULL),
bytes_left_(0),
curr_byte_(0),
num_remaining_bits_in_curr_byte_(0),
prev_two_bytes_(0) {
}
H264Parser::H264BitReader::~H264BitReader() {}
bool H264Parser::H264BitReader::Initialize(const uint8* data, off_t size) {
DCHECK(data);
if (size < 1)
return false;
data_ = data;
bytes_left_ = size;
num_remaining_bits_in_curr_byte_ = 0;
// Initially set to 0xffff to accept all initial two-byte sequences.
prev_two_bytes_ = 0xffff;
return true;
}
bool H264Parser::H264BitReader::UpdateCurrByte() {
if (bytes_left_ < 1)
return false;
// Emulation prevention three-byte detection.
// If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
// Detected 0x000003, skip last byte.
++data_;
--bytes_left_;
// Need another full three bytes before we can detect the sequence again.
prev_two_bytes_ = 0xffff;
if (bytes_left_ < 1)
return false;
}
// Load a new byte and advance pointers.
curr_byte_ = *data_++ & 0xff;
--bytes_left_;
num_remaining_bits_in_curr_byte_ = 8;
prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
return true;
}
// Read |num_bits| (1 to 31 inclusive) from the stream and return them
// in |out|, with first bit in the stream as MSB in |out| at position
// (|num_bits| - 1).
bool H264Parser::H264BitReader::ReadBits(int num_bits, int *out) {
int bits_left = num_bits;
*out = 0;
DCHECK(num_bits <= 31);
while (num_remaining_bits_in_curr_byte_ < bits_left) {
// Take all that's left in current byte, shift to make space for the rest.
*out = (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
bits_left -= num_remaining_bits_in_curr_byte_;
if (!UpdateCurrByte())
return false;
}
*out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
*out &= ((1 << num_bits) - 1);
num_remaining_bits_in_curr_byte_ -= bits_left;
return true;
}
off_t H264Parser::H264BitReader::NumBitsLeft() {
return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
}
bool H264Parser::H264BitReader::HasMoreRBSPData() {
// Make sure we have more bits, if we are at 0 bits in current byte
// and updating current byte fails, we don't have more data anyway.
if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
return false;
// On last byte?
if (bytes_left_)
return true;
// Last byte, look for stop bit;
// We have more RBSP data if the last non-zero bit we find is not the
// first available bit.
return (curr_byte_ &
((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
}
#define READ_BITS_OR_RETURN(num_bits, out) \
do { \
int _out; \
......
......@@ -13,6 +13,7 @@
#include "base/basictypes.h"
#include "content/common/content_export.h"
#include "content/common/gpu/media/h264_bit_reader.h"
namespace content {
......@@ -301,61 +302,6 @@ class CONTENT_EXPORT H264Parser {
Result ParseSEI(H264SEIMessage* sei_msg);
private:
// A class to provide bit-granularity reading of H.264 streams.
// This is not a generic bit reader class, as it takes into account
// H.264 stream-specific constraints, such as skipping emulation-prevention
// bytes and stop bits. See spec for more details.
// TODO(posciak): need separate unittests for this class.
class H264BitReader {
public:
H264BitReader();
~H264BitReader();
// Initialize the reader to start reading at |data|, |size| being size
// of |data| in bytes.
// Return false on insufficient size of stream..
// TODO(posciak,fischman): consider replacing Initialize() with
// heap-allocating and creating bit readers on demand instead.
bool Initialize(const uint8* data, off_t size);
// Read |num_bits| next bits from stream and return in |*out|, first bit
// from the stream starting at |num_bits| position in |*out|.
// |num_bits| may be 1-32, inclusive.
// Return false if the given number of bits cannot be read (not enough
// bits in the stream), true otherwise.
bool ReadBits(int num_bits, int *out);
// Return the number of bits left in the stream.
off_t NumBitsLeft();
// See the definition of more_rbsp_data() in spec.
bool HasMoreRBSPData();
private:
// Advance to the next byte, loading it into curr_byte_.
// Return false on end of stream.
bool UpdateCurrByte();
// Pointer to the next unread (not in curr_byte_) byte in the stream.
const uint8* data_;
// Bytes left in the stream (without the curr_byte_).
off_t bytes_left_;
// Contents of the current byte; first unread bit starting at position
// 8 - num_remaining_bits_in_curr_byte_ from MSB.
int curr_byte_;
// Number of bits remaining in curr_byte_
int num_remaining_bits_in_curr_byte_;
// Used in emulation prevention three byte detection (see spec).
// Initially set to 0xffff to accept all initial two-byte sequences.
int prev_two_bytes_;
DISALLOW_COPY_AND_ASSIGN(H264BitReader);
};
// 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(int* val);
......
......@@ -91,4 +91,3 @@ int main(int argc, char **argv) {
return RUN_ALL_TESTS();
}
......@@ -222,6 +222,8 @@
'common/gpu/image_transport_surface_win.cc',
'common/gpu/media/avc_config_record_builder.cc',
'common/gpu/media/avc_config_record_builder.h',
'common/gpu/media/h264_bit_reader.cc',
'common/gpu/media/h264_bit_reader.h',
'common/gpu/media/h264_parser.cc',
'common/gpu/media/h264_parser.h',
'common/gpu/media/mac_video_decode_accelerator.h',
......
......@@ -738,6 +738,7 @@
'../testing/gtest.gyp:gtest',
],
'sources': [
'common/gpu/media/h264_bit_reader_unittest.cc',
'common/gpu/media/h264_parser_unittest.cc',
],
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment