OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/bit_reader.h" | |
6 #include "base/compiler_specific.h" | |
7 | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace { | |
11 | |
12 // This class implements an H264 RBSP reader that takes escape sequence and | |
13 // stop bit into account. This is used to test the customization ability of | |
14 // BitReader. | |
15 class H264BitReader : public media::BitReader { | |
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
Please remove this. It appears to be duplicate cod
| |
16 public: | |
17 H264BitReader(); | |
18 virtual ~H264BitReader() OVERRIDE; | |
19 | |
20 private: | |
21 // This function handles the H.264 escape sequence. | |
22 virtual void UpdateCurrByte() OVERRIDE; | |
23 }; | |
24 | |
25 H264BitReader::H264BitReader() { | |
26 } | |
27 | |
28 H264BitReader::~H264BitReader() {} | |
29 | |
30 void H264BitReader::UpdateCurrByte() { | |
31 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0); | |
32 | |
33 if (bytes_left_ >= 1) { | |
34 // Emulation prevention three-byte detection. | |
35 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | |
36 if (*data_ == 0x03 && Tell() >= 16 && data_[-1] == 0 && data_[-2] == 0) { | |
37 // Detected 0x000003, skip last byte. | |
38 ++data_; | |
39 --bytes_left_; | |
40 position_ += 8; | |
41 } | |
42 } | |
43 | |
44 if (bytes_left_ >= 1) { | |
45 // Load a new byte and advance pointers. | |
46 curr_byte_ = *data_; | |
47 ++data_; | |
48 --bytes_left_; | |
49 num_remaining_bits_in_curr_byte_ = 8; | |
50 } | |
51 | |
52 // Check if this is the end of RBSP data. | |
53 if (bytes_left_ == 0) { | |
54 while (num_remaining_bits_in_curr_byte_ != 0 && curr_byte_ % 2 == 0) { | |
55 --num_remaining_bits_in_curr_byte_; | |
56 curr_byte_ /= 2; | |
57 } | |
58 } | |
59 } | |
60 | |
61 } // anonymous namespace | |
62 | |
63 namespace media { | |
64 | |
65 TEST(BitReaderTest, EmptyStreamTest) { | |
66 BitReader reader(NULL, 0); | |
67 uint8 value8 = 0xff; | |
68 | |
69 ASSERT_FALSE(reader.HasMoreData()); | |
70 ASSERT_EQ(reader.Tell(), 0); | |
71 ASSERT_TRUE(reader.ReadBits(0, &value8)); | |
72 ASSERT_TRUE(reader.SkipBits(0)); | |
73 ASSERT_FALSE(reader.ReadBits(1, &value8)); | |
74 ASSERT_FALSE(reader.SkipBits(1)); | |
75 ASSERT_EQ(value8, 0); | |
76 ASSERT_EQ(reader.Tell(), 0); | |
77 ASSERT_FALSE(reader.HasMoreData()); | |
78 } | |
79 | |
80 TEST(BitReaderTest, NormalOperationTest) { | |
81 // 0101 0101 1001 1001 repeats 4 times | |
82 uint8 buffer[] = {0x55, 0x99, 0x55, 0x99, 0x55, 0x99, 0x55, 0x99}; | |
83 BitReader reader(buffer, 6); // Initialize with 6 bytes only | |
84 uint8 value8; | |
85 uint64 value64; | |
86 | |
87 ASSERT_TRUE(reader.HasMoreData()); | |
88 ASSERT_EQ(reader.Tell(), 0); | |
89 ASSERT_FALSE(reader.ReadBits(64, &value64)); | |
90 ASSERT_TRUE(reader.ReadBits(1, &value8)); | |
91 ASSERT_EQ(value8, 0); | |
92 ASSERT_TRUE(reader.ReadBits(8, &value8)); | |
93 ASSERT_EQ(value8, 0xab); // 1010 1011 | |
94 ASSERT_EQ(reader.Tell(), 9); | |
95 ASSERT_TRUE(reader.HasMoreData()); | |
96 ASSERT_TRUE(reader.SkipBits(7)); | |
97 ASSERT_EQ(reader.Tell(), 16); | |
98 ASSERT_TRUE(reader.ReadBits(32, &value64)); | |
99 ASSERT_EQ(value64, 0x55995599u); | |
100 ASSERT_EQ(reader.Tell(), 48); | |
101 ASSERT_FALSE(reader.HasMoreData()); | |
102 ASSERT_FALSE(reader.SkipBits(1)); | |
103 ASSERT_FALSE(reader.ReadBits(1, &value8)); | |
104 ASSERT_TRUE(reader.SkipBits(0)); | |
105 value8 = 0xff; | |
106 ASSERT_TRUE(reader.ReadBits(0, &value8)); | |
107 ASSERT_EQ(value8, 0); | |
108 | |
109 reader.Initialize(buffer, 8); | |
110 ASSERT_TRUE(reader.ReadBits(64, &value64)); | |
111 EXPECT_EQ(value64, 0x5599559955995599u); | |
strobe_
2012/06/28 14:58:22
Android try bot: this should be 0x55..99ul
| |
112 EXPECT_FALSE(reader.HasMoreData()); | |
113 EXPECT_EQ(reader.Tell(), 64); | |
114 EXPECT_FALSE(reader.ReadBits(1, &value8)); | |
115 EXPECT_FALSE(reader.SkipBits(1)); | |
116 EXPECT_TRUE(reader.ReadBits(0, &value8)); | |
117 EXPECT_TRUE(reader.SkipBits(0)); | |
118 } | |
119 | |
120 TEST(BitReaderTest, H264StreamTest) { | |
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
Move this to a file in content/common/gpu/media/
| |
121 // This stream contains an escape sequence. Its last byte only has 4 bits. | |
122 // 0001 0010 0011 0100 0000 0000 0000 0000 0000 0011 0101 0110 0111 0000 | |
123 int8 buffer[] = {0x12, 0x34, 0x00, 0x00, 0x03, 0x56, 0x70}; | |
124 H264BitReader reader; | |
125 uint8 value8; | |
126 uint32 value32; | |
127 | |
128 reader.Initialize(buffer, sizeof(buffer)); | |
129 EXPECT_EQ(reader.Tell(), 0); | |
130 EXPECT_TRUE(reader.ReadBits(4, &value8)); | |
131 EXPECT_EQ(value8, 1u); | |
132 EXPECT_EQ(reader.Tell(), 4); | |
133 EXPECT_TRUE(reader.HasMoreData()); | |
134 | |
135 EXPECT_TRUE(reader.ReadBits(8, &value8)); | |
136 EXPECT_EQ(value8, 0x23u); | |
137 EXPECT_EQ(reader.Tell(), 12); | |
138 EXPECT_TRUE(reader.HasMoreData()); | |
139 | |
140 EXPECT_TRUE(reader.ReadBits(24, &value32)); | |
141 EXPECT_EQ(value32, 0x400005u); | |
142 EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte | |
143 EXPECT_TRUE(reader.HasMoreData()); | |
144 | |
145 EXPECT_FALSE(reader.ReadBits(9, &value32)); | |
146 EXPECT_EQ(value32, 0u); | |
147 EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte | |
148 EXPECT_TRUE(reader.HasMoreData()); | |
149 | |
150 EXPECT_TRUE(reader.ReadBits(8, &value8)); | |
151 EXPECT_EQ(value8, 0x67u); | |
152 EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte | |
153 EXPECT_FALSE(reader.HasMoreData()); | |
154 | |
155 EXPECT_TRUE(reader.ReadBits(0, &value8)); | |
156 EXPECT_FALSE(reader.ReadBits(1, &value8)); | |
157 EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte | |
158 EXPECT_FALSE(reader.HasMoreData()); | |
159 | |
160 // Do it again using SkipBits | |
161 reader.Initialize(buffer, sizeof(buffer)); | |
162 EXPECT_EQ(reader.Tell(), 0); | |
163 EXPECT_TRUE(reader.SkipBits(4)); | |
164 EXPECT_EQ(reader.Tell(), 4); | |
165 EXPECT_TRUE(reader.HasMoreData()); | |
166 | |
167 EXPECT_TRUE(reader.SkipBits(8)); | |
168 EXPECT_EQ(reader.Tell(), 12); | |
169 EXPECT_TRUE(reader.HasMoreData()); | |
170 | |
171 EXPECT_TRUE(reader.SkipBits(24)); | |
172 EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte | |
173 EXPECT_TRUE(reader.HasMoreData()); | |
174 | |
175 EXPECT_FALSE(reader.SkipBits(9)); | |
176 EXPECT_EQ(reader.Tell(), 44); // Include the skipped escape byte | |
177 EXPECT_TRUE(reader.HasMoreData()); | |
178 | |
179 EXPECT_TRUE(reader.SkipBits(8)); | |
180 EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte | |
181 EXPECT_FALSE(reader.HasMoreData()); | |
182 | |
183 EXPECT_TRUE(reader.SkipBits(0)); | |
184 EXPECT_FALSE(reader.SkipBits(1)); | |
185 EXPECT_EQ(reader.Tell(), 52); // Include the skipped escape byte | |
186 EXPECT_FALSE(reader.HasMoreData()); | |
187 } | |
188 | |
189 TEST(BitReaderTest, LongSkipTest) { | |
190 int8 buffer[] = { | |
191 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 1 | |
192 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 2 | |
193 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 3 | |
194 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 4 | |
195 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 5 | |
196 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 6 | |
197 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 7 | |
198 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, // 64 * 8 | |
199 0x87, 0x65 | |
200 }; | |
201 BitReader reader(buffer, sizeof(buffer)); | |
202 uint8 value8; | |
203 | |
204 EXPECT_TRUE(reader.SkipBits(64 * 8 + 8)); | |
205 EXPECT_EQ(reader.Tell(), 64 * 8 + 8); | |
206 EXPECT_TRUE(reader.ReadBits(8, &value8)); | |
207 EXPECT_EQ(value8, 0x65); | |
208 EXPECT_EQ(reader.Tell(), 64 * 8 + 16); | |
209 EXPECT_FALSE(reader.HasMoreData()); | |
210 EXPECT_EQ(reader.NumBitsLeft(), 0); | |
211 } | |
212 | |
213 } // namespace media | |
OLD | NEW |