forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh264_parser_unittest.cc
277 lines (238 loc) · 9.36 KB
/
h264_parser_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2014 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 <limits>
#include <memory>
#include <vector>
#include "base/command_line.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/optional.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "media/base/subsample_entry.h"
#include "media/base/test_data_util.h"
#include "media/video/h264_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace media {
class H264SPSTest : public ::testing::Test {
public:
// An exact clone of an SPS from Big Buck Bunny 480p.
std::unique_ptr<H264SPS> MakeSPS_BBB480p() {
std::unique_ptr<H264SPS> sps = std::make_unique<H264SPS>();
sps->profile_idc = 100;
sps->level_idc = 30;
sps->chroma_format_idc = 1;
sps->log2_max_pic_order_cnt_lsb_minus4 = 2;
sps->max_num_ref_frames = 5;
sps->pic_width_in_mbs_minus1 = 52;
sps->pic_height_in_map_units_minus1 = 29;
sps->frame_mbs_only_flag = true;
sps->direct_8x8_inference_flag = true;
sps->vui_parameters_present_flag = true;
sps->timing_info_present_flag = true;
sps->num_units_in_tick = 1;
sps->time_scale = 48;
sps->fixed_frame_rate_flag = true;
sps->bitstream_restriction_flag = true;
// These next three fields are not part of our SPS struct yet.
// sps->motion_vectors_over_pic_boundaries_flag = true;
// sps->log2_max_mv_length_horizontal = 10;
// sps->log2_max_mv_length_vertical = 10;
sps->max_num_reorder_frames = 2;
sps->max_dec_frame_buffering = 5;
// Computed field, matches |chroma_format_idc| in this case.
// TODO(sandersd): Extract that computation from the parsing step.
sps->chroma_array_type = 1;
return sps;
}
};
TEST_F(H264SPSTest, GetCodedSize) {
std::unique_ptr<H264SPS> sps = MakeSPS_BBB480p();
EXPECT_EQ(gfx::Size(848, 480), sps->GetCodedSize());
// Overflow.
sps->pic_width_in_mbs_minus1 = std::numeric_limits<int>::max();
EXPECT_EQ(base::nullopt, sps->GetCodedSize());
}
TEST_F(H264SPSTest, GetVisibleRect) {
std::unique_ptr<H264SPS> sps = MakeSPS_BBB480p();
EXPECT_EQ(gfx::Rect(0, 0, 848, 480), sps->GetVisibleRect());
// Add some cropping.
sps->frame_cropping_flag = true;
sps->frame_crop_left_offset = 1;
sps->frame_crop_right_offset = 2;
sps->frame_crop_top_offset = 3;
sps->frame_crop_bottom_offset = 4;
EXPECT_EQ(gfx::Rect(2, 6, 848 - 6, 480 - 14), sps->GetVisibleRect());
// Not quite invalid.
sps->frame_crop_left_offset = 422;
sps->frame_crop_right_offset = 1;
sps->frame_crop_top_offset = 0;
sps->frame_crop_bottom_offset = 0;
EXPECT_EQ(gfx::Rect(844, 0, 2, 480), sps->GetVisibleRect());
// Invalid crop.
sps->frame_crop_left_offset = 423;
sps->frame_crop_right_offset = 1;
sps->frame_crop_top_offset = 0;
sps->frame_crop_bottom_offset = 0;
EXPECT_EQ(base::nullopt, sps->GetVisibleRect());
// Overflow.
sps->frame_crop_left_offset = std::numeric_limits<int>::max() / 2 + 1;
sps->frame_crop_right_offset = 0;
sps->frame_crop_top_offset = 0;
sps->frame_crop_bottom_offset = 0;
EXPECT_EQ(base::nullopt, sps->GetVisibleRect());
}
TEST(H264ParserTest, StreamFileParsing) {
base::FilePath file_path = GetTestDataFilePath("test-25fps.h264");
// Number of NALUs in the test stream to be parsed.
int num_nalus = 759;
base::MemoryMappedFile stream;
ASSERT_TRUE(stream.Initialize(file_path))
<< "Couldn't open stream file: " << file_path.MaybeAsASCII();
H264Parser parser;
parser.SetStream(stream.data(), stream.length());
// Parse until the end of stream/unsupported stream/error in stream is found.
int num_parsed_nalus = 0;
while (true) {
media::H264SliceHeader shdr;
media::H264SEIMessage sei_msg;
H264NALU nalu;
H264Parser::Result res = parser.AdvanceToNextNALU(&nalu);
if (res == H264Parser::kEOStream) {
DVLOG(1) << "Number of successfully parsed NALUs before EOS: "
<< num_parsed_nalus;
ASSERT_EQ(num_nalus, num_parsed_nalus);
return;
}
ASSERT_EQ(res, H264Parser::kOk);
++num_parsed_nalus;
int id;
switch (nalu.nal_unit_type) {
case H264NALU::kIDRSlice:
case H264NALU::kNonIDRSlice:
ASSERT_EQ(parser.ParseSliceHeader(nalu, &shdr), H264Parser::kOk);
break;
case H264NALU::kSPS:
ASSERT_EQ(parser.ParseSPS(&id), H264Parser::kOk);
break;
case H264NALU::kPPS:
ASSERT_EQ(parser.ParsePPS(&id), H264Parser::kOk);
break;
case H264NALU::kSEIMessage:
ASSERT_EQ(parser.ParseSEI(&sei_msg), H264Parser::kOk);
break;
default:
// Skip unsupported NALU.
DVLOG(4) << "Skipping unsupported NALU";
break;
}
}
}
TEST(H264ParserTest, ParseNALUsFromStreamFile) {
base::FilePath file_path = GetTestDataFilePath("test-25fps.h264");
// Number of NALUs in the test stream to be parsed.
const size_t num_nalus = 759;
base::MemoryMappedFile stream;
ASSERT_TRUE(stream.Initialize(file_path))
<< "Couldn't open stream file: " << file_path.MaybeAsASCII();
std::vector<H264NALU> nalus;
ASSERT_TRUE(H264Parser::ParseNALUs(stream.data(), stream.length(), &nalus));
ASSERT_EQ(num_nalus, nalus.size());
}
// Verify that GetCurrentSubsamples works.
TEST(H264ParserTest, GetCurrentSubsamplesNormal) {
const uint8_t kStream[] = {
// First NALU.
// Clear bytes = 4.
0x00, 0x00, 0x01, // start code.
0x65, // Nalu type = 5, IDR slice.
// Below is bogus data.
// Encrypted bytes = 15.
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06,
// Clear bytes = 5.
0x07, 0x00, 0x01, 0x02, 0x03,
// Encrypted until next NALU. Encrypted bytes = 20.
0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
// Note that this is still in the encrypted region but looks like a start
// code.
0x00, 0x00, 0x01, 0x03, 0x04, 0x05, 0x06, 0x07,
// Second NALU. Completely clear.
// Clear bytes = 10.
0x00, 0x00, 0x01, // start code.
0x06, // nalu type = 6, SEI.
// Bogus data.
0xff, 0xfe, 0xfd, 0xee, 0x12, 0x33,
};
std::vector<SubsampleEntry> subsamples;
subsamples.emplace_back(4u, 15u);
subsamples.emplace_back(5u, 20u);
subsamples.emplace_back(10u, 0u);
H264Parser parser;
parser.SetEncryptedStream(kStream, base::size(kStream), subsamples);
H264NALU nalu;
ASSERT_EQ(H264Parser::kOk, parser.AdvanceToNextNALU(&nalu));
auto nalu_subsamples = parser.GetCurrentSubsamples();
ASSERT_EQ(2u, nalu_subsamples.size());
// Note that nalu->data starts from the NALU header, i.e. does not include
// the start code.
EXPECT_EQ(1u, nalu_subsamples[0].clear_bytes);
EXPECT_EQ(15u, nalu_subsamples[0].cypher_bytes);
EXPECT_EQ(5u, nalu_subsamples[1].clear_bytes);
EXPECT_EQ(20u, nalu_subsamples[1].cypher_bytes);
// Make sure that it reached the next NALU.
EXPECT_EQ(H264Parser::kOk, parser.AdvanceToNextNALU(&nalu));
nalu_subsamples = parser.GetCurrentSubsamples();
ASSERT_EQ(1u, nalu_subsamples.size());
EXPECT_EQ(7u, nalu_subsamples[0].clear_bytes);
EXPECT_EQ(0u, nalu_subsamples[0].cypher_bytes);
}
// Verify that subsamples starting at non-NALU boundary also works.
TEST(H264ParserTest, GetCurrentSubsamplesSubsampleNotStartingAtNaluBoundary) {
const uint8_t kStream[] = {
// First NALU.
// Clear bytes = 4.
0x00, 0x00, 0x01, // start code.
0x65, // Nalu type = 5, IDR slice.
// Below is bogus data.
// Encrypted bytes = 24.
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
// Clear bytes = 18. The rest is in the clear. Note that this is not at
// a NALU boundary and a NALU starts below.
0xaa, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
// Second NALU. Completely clear.
0x00, 0x00, 0x01, // start code.
0x06, // nalu type = 6, SEI.
// Bogus data.
0xff, 0xfe, 0xfd, 0xee, 0x12, 0x33,
};
std::vector<SubsampleEntry> subsamples;
subsamples.emplace_back(4u, 24u);
subsamples.emplace_back(18, 0);
H264Parser parser;
parser.SetEncryptedStream(kStream, base::size(kStream), subsamples);
H264NALU nalu;
ASSERT_EQ(H264Parser::kOk, parser.AdvanceToNextNALU(&nalu));
auto nalu_subsamples = parser.GetCurrentSubsamples();
ASSERT_EQ(2u, nalu_subsamples.size());
// Note that nalu->data starts from the NALU header, i.e. does not include
// the start code.
EXPECT_EQ(1u, nalu_subsamples[0].clear_bytes);
EXPECT_EQ(24u, nalu_subsamples[0].cypher_bytes);
// The nalu ends with 8 more clear bytes. The last 10 bytes should be
// associated with the next nalu.
EXPECT_EQ(8u, nalu_subsamples[1].clear_bytes);
EXPECT_EQ(0u, nalu_subsamples[1].cypher_bytes);
ASSERT_EQ(H264Parser::kOk, parser.AdvanceToNextNALU(&nalu));
nalu_subsamples = parser.GetCurrentSubsamples();
ASSERT_EQ(1u, nalu_subsamples.size());
// Although the input had 10 more bytes, since nalu->data starts from the nalu
// header, there's only 7 more bytes left.
EXPECT_EQ(7u, nalu_subsamples[0].clear_bytes);
EXPECT_EQ(0u, nalu_subsamples[0].cypher_bytes);
}
} // namespace media