Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Unified Diff: media/base/bit_reader_unittest.cc

Issue 10710002: Add HE AAC support to ISO BMFF. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add unittest for 5.1 channel. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/base/bit_reader_unittest.cc
diff --git a/media/base/bit_reader_unittest.cc b/media/base/bit_reader_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..85241ce8837df2d9cfad3b2bcdc916ccd67a2b06
--- /dev/null
+++ b/media/base/bit_reader_unittest.cc
@@ -0,0 +1,213 @@
+// 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 "media/base/bit_reader.h"
+#include "base/compiler_specific.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// This class implements an H264 RBSP reader that takes escape sequence and
+// stop bit into account. This is used to test the customization ability of
+// BitReader.
+class H264BitReader : public media::BitReader {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Please remove this. It appears to be duplicate cod
+ public:
+ H264BitReader();
+ virtual ~H264BitReader() OVERRIDE;
+
+ private:
+ // This function handles the H.264 escape sequence.
+ virtual void UpdateCurrByte() OVERRIDE;
+};
+
+H264BitReader::H264BitReader() {
+}
+
+H264BitReader::~H264BitReader() {}
+
+void H264BitReader::UpdateCurrByte() {
+ DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0);
+
+ if (bytes_left_ >= 1) {
+ // Emulation prevention three-byte detection.
+ // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
+ if (*data_ == 0x03 && Tell() >= 16 && data_[-1] == 0 && data_[-2] == 0) {
+ // Detected 0x000003, skip last byte.
+ ++data_;
+ --bytes_left_;
+ position_ += 8;
+ }
+ }
+
+ if (bytes_left_ >= 1) {
+ // Load a new byte and advance pointers.
+ curr_byte_ = *data_;
+ ++data_;
+ --bytes_left_;
+ num_remaining_bits_in_curr_byte_ = 8;
+ }
+
+ // Check if this is the end of RBSP data.
+ if (bytes_left_ == 0) {
+ while (num_remaining_bits_in_curr_byte_ != 0 && curr_byte_ % 2 == 0) {
+ --num_remaining_bits_in_curr_byte_;
+ curr_byte_ /= 2;
+ }
+ }
+}
+
+} // anonymous namespace
+
+namespace media {
+
+TEST(BitReaderTest, EmptyStreamTest) {
+ BitReader reader(NULL, 0);
+ uint8 value8 = 0xff;
+
+ ASSERT_FALSE(reader.HasMoreData());
+ ASSERT_EQ(reader.Tell(), 0);
+ ASSERT_TRUE(reader.ReadBits(0, &value8));
+ ASSERT_TRUE(reader.SkipBits(0));
+ ASSERT_FALSE(reader.ReadBits(1, &value8));
+ ASSERT_FALSE(reader.SkipBits(1));
+ ASSERT_EQ(value8, 0);
+ ASSERT_EQ(reader.Tell(), 0);
+ ASSERT_FALSE(reader.HasMoreData());
+}
+
+TEST(BitReaderTest, NormalOperationTest) {
+ // 0101 0101 1001 1001 repeats 4 times
+ uint8 buffer[] = {0x55, 0x99, 0x55, 0x99, 0x55, 0x99, 0x55, 0x99};
+ BitReader reader(buffer, 6); // Initialize with 6 bytes only
+ uint8 value8;
+ uint64 value64;
+
+ ASSERT_TRUE(reader.HasMoreData());
+ ASSERT_EQ(reader.Tell(), 0);
+ ASSERT_FALSE(reader.ReadBits(64, &value64));
+ ASSERT_TRUE(reader.ReadBits(1, &value8));
+ ASSERT_EQ(value8, 0);
+ ASSERT_TRUE(reader.ReadBits(8, &value8));
+ ASSERT_EQ(value8, 0xab); // 1010 1011
+ ASSERT_EQ(reader.Tell(), 9);
+ ASSERT_TRUE(reader.HasMoreData());
+ ASSERT_TRUE(reader.SkipBits(7));
+ ASSERT_EQ(reader.Tell(), 16);
+ ASSERT_TRUE(reader.ReadBits(32, &value64));
+ ASSERT_EQ(value64, 0x55995599u);
+ ASSERT_EQ(reader.Tell(), 48);
+ ASSERT_FALSE(reader.HasMoreData());
+ ASSERT_FALSE(reader.SkipBits(1));
+ ASSERT_FALSE(reader.ReadBits(1, &value8));
+ ASSERT_TRUE(reader.SkipBits(0));
+ value8 = 0xff;
+ ASSERT_TRUE(reader.ReadBits(0, &value8));
+ ASSERT_EQ(value8, 0);
+
+ reader.Initialize(buffer, 8);
+ ASSERT_TRUE(reader.ReadBits(64, &value64));
+ EXPECT_EQ(value64, 0x5599559955995599u);
strobe_ 2012/06/28 14:58:22 Android try bot: this should be 0x55..99ul
+ EXPECT_FALSE(reader.HasMoreData());
+ EXPECT_EQ(reader.Tell(), 64);
+ EXPECT_FALSE(reader.ReadBits(1, &value8));
+ EXPECT_FALSE(reader.SkipBits(1));
+ EXPECT_TRUE(reader.ReadBits(0, &value8));
+ EXPECT_TRUE(reader.SkipBits(0));
+}
+
+TEST(BitReaderTest, H264StreamTest) {
acolwell GONE FROM CHROMIUM 2012/06/28 17:31:25 Move this to a file in content/common/gpu/media/
+ // This stream contains an escape sequence. Its last byte only has 4 bits.
+ // 0001 0010 0011 0100 0000 0000 0000 0000 0000 0011 0101 0110 0111 0000
+ int8 buffer[] = {0x12, 0x34, 0x00, 0x00, 0x03, 0x56, 0x70};
+ H264BitReader reader;
+ uint8 value8;
+ uint32 value32;
+
+ reader.Initialize(buffer, sizeof(buffer));
+ EXPECT_EQ(reader.Tell(), 0);
+ EXPECT_TRUE(reader.ReadBits(4, &value8));
+ EXPECT_EQ(value8, 1u);
+ EXPECT_EQ(reader.Tell(), 4);
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.ReadBits(8, &value8));
+ EXPECT_EQ(value8, 0x23u);
+ EXPECT_EQ(reader.Tell(), 12);
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.ReadBits(24, &value32));
+ EXPECT_EQ(value32, 0x400005u);
+ EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_FALSE(reader.ReadBits(9, &value32));
+ EXPECT_EQ(value32, 0u);
+ EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.ReadBits(8, &value8));
+ EXPECT_EQ(value8, 0x67u);
+ EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte
+ EXPECT_FALSE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.ReadBits(0, &value8));
+ EXPECT_FALSE(reader.ReadBits(1, &value8));
+ EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte
+ EXPECT_FALSE(reader.HasMoreData());
+
+ // Do it again using SkipBits
+ reader.Initialize(buffer, sizeof(buffer));
+ EXPECT_EQ(reader.Tell(), 0);
+ EXPECT_TRUE(reader.SkipBits(4));
+ EXPECT_EQ(reader.Tell(), 4);
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.SkipBits(8));
+ EXPECT_EQ(reader.Tell(), 12);
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.SkipBits(24));
+ EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_FALSE(reader.SkipBits(9));
+ EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte
+ EXPECT_TRUE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.SkipBits(8));
+ EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte
+ EXPECT_FALSE(reader.HasMoreData());
+
+ EXPECT_TRUE(reader.SkipBits(0));
+ EXPECT_FALSE(reader.SkipBits(1));
+ EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte
+ EXPECT_FALSE(reader.HasMoreData());
+}
+
+TEST(BitReaderTest, LongSkipTest) {
+ int8 buffer[] = {
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 1
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 2
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 3
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 4
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 5
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 6
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 7
+ 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 8
+ 0x87, 0x65
+ };
+ BitReader reader(buffer, sizeof(buffer));
+ uint8 value8;
+
+ EXPECT_TRUE(reader.SkipBits(64 * 8 + 8));
+ EXPECT_EQ(reader.Tell(), 64 * 8 + 8);
+ EXPECT_TRUE(reader.ReadBits(8, &value8));
+ EXPECT_EQ(value8, 0x65);
+ EXPECT_EQ(reader.Tell(), 64 * 8 + 16);
+ EXPECT_FALSE(reader.HasMoreData());
+ EXPECT_EQ(reader.NumBitsLeft(), 0);
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698