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.
7 #include "base/basictypes.h"
8 #include "media/base/stream_parser_buffer.h"
9 #include "media/mp4/avc.h"
10 #include "media/mp4/box_definitions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gtest/include/gtest/gtest-param-test.h"
17 static const uint8 kNALU1
[] = { 0x01, 0x02, 0x03 };
18 static const uint8 kNALU2
[] = { 0x04, 0x05, 0x06, 0x07 };
19 static const uint8 kExpected
[] = {
20 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03,
21 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07 };
23 static const uint8 kExpectedParamSets
[] = {
24 0x00, 0x00, 0x00, 0x01, 0x67, 0x12,
25 0x00, 0x00, 0x00, 0x01, 0x67, 0x34,
26 0x00, 0x00, 0x00, 0x01, 0x68, 0x56, 0x78};
28 class AVCConversionTest
: public testing::TestWithParam
<int> {
30 void MakeInputForLength(int length_size
, std::vector
<uint8
>* buf
) {
32 for (int i
= 1; i
< length_size
; i
++)
34 buf
->push_back(sizeof(kNALU1
));
35 buf
->insert(buf
->end(), kNALU1
, kNALU1
+ sizeof(kNALU1
));
37 for (int i
= 1; i
< length_size
; i
++)
39 buf
->push_back(sizeof(kNALU2
));
40 buf
->insert(buf
->end(), kNALU2
, kNALU2
+ sizeof(kNALU2
));
44 TEST_P(AVCConversionTest
, ParseCorrectly
) {
45 std::vector
<uint8
> buf
;
46 MakeInputForLength(GetParam(), &buf
);
47 EXPECT_TRUE(AVC::ConvertFrameToAnnexB(GetParam(), &buf
));
48 EXPECT_EQ(buf
.size(), sizeof(kExpected
));
49 EXPECT_EQ(0, memcmp(kExpected
, &buf
[0], sizeof(kExpected
)));
52 TEST_P(AVCConversionTest
, ParsePartial
) {
53 std::vector
<uint8
> buf
;
54 MakeInputForLength(GetParam(), &buf
);
56 EXPECT_FALSE(AVC::ConvertFrameToAnnexB(GetParam(), &buf
));
57 // This tests a buffer ending in the middle of a NAL length. For length size
58 // of one, this can't happen, so we skip that case.
59 if (GetParam() != 1) {
60 MakeInputForLength(GetParam(), &buf
);
61 buf
.erase(buf
.end() - (sizeof(kNALU2
) + 1), buf
.end());
62 EXPECT_FALSE(AVC::ConvertFrameToAnnexB(GetParam(), &buf
));
66 TEST_P(AVCConversionTest
, ParseEmpty
) {
67 std::vector
<uint8
> buf
;
68 EXPECT_TRUE(AVC::ConvertFrameToAnnexB(GetParam(), &buf
));
69 EXPECT_EQ(0u, buf
.size());
72 INSTANTIATE_TEST_CASE_P(AVCConversionTestValues
,
74 ::testing::Values(1, 2, 4));
76 TEST_F(AVCConversionTest
, ConvertConfigToAnnexB
) {
77 AVCDecoderConfigurationRecord avc_config
;
78 avc_config
.sps_list
.resize(2);
79 avc_config
.sps_list
[0].push_back(0x67);
80 avc_config
.sps_list
[0].push_back(0x12);
81 avc_config
.sps_list
[1].push_back(0x67);
82 avc_config
.sps_list
[1].push_back(0x34);
83 avc_config
.pps_list
.resize(1);
84 avc_config
.pps_list
[0].push_back(0x68);
85 avc_config
.pps_list
[0].push_back(0x56);
86 avc_config
.pps_list
[0].push_back(0x78);
88 std::vector
<uint8
> buf
;
89 EXPECT_TRUE(AVC::ConvertConfigToAnnexB(avc_config
, &buf
));
90 EXPECT_EQ(0, memcmp(kExpectedParamSets
, &buf
[0],
91 sizeof(kExpectedParamSets
)));