2016-02-05 19:08:07 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-05-25 18:03:17 +00:00
|
|
|
#include "packager/media/codecs/nalu_reader.h"
|
2016-02-05 19:08:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-02-05 19:08:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
TEST(NaluReaderTest, StartCodeSearch) {
|
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
0x01, 0x00, 0x00, 0x04, 0x23, 0x56,
|
|
|
|
// First NALU
|
|
|
|
0x00, 0x00, 0x01, 0x14, 0x34, 0x56, 0x78,
|
|
|
|
// Second NALU
|
|
|
|
0x00, 0x00, 0x00, 0x01, 0x67, 0xbb, 0xcc, 0xdd,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, kIsAnnexbByteStream, kNaluData,
|
2016-02-26 22:34:14 +00:00
|
|
|
arraysize(kNaluData));
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(kNaluData + 9, nalu.data());
|
2016-02-26 22:49:57 +00:00
|
|
|
EXPECT_EQ(3u, nalu.payload_size());
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(0, nalu.ref_idc());
|
2016-03-22 00:39:22 +00:00
|
|
|
EXPECT_EQ(0x14, nalu.type());
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(kNaluData + 17, nalu.data());
|
2016-02-26 22:49:57 +00:00
|
|
|
EXPECT_EQ(3u, nalu.payload_size());
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(3, nalu.ref_idc());
|
|
|
|
EXPECT_EQ(7, nalu.type());
|
|
|
|
|
|
|
|
EXPECT_EQ(NaluReader::kEOStream, reader.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
2016-03-22 00:39:22 +00:00
|
|
|
TEST(NaluReaderTest, StartCodeSearchWithStartCodeInsideNalUnit) {
|
|
|
|
const uint8_t kNaluData[] = {
|
|
|
|
0x01, 0x00, 0x00, 0x04, 0x23, 0x56,
|
|
|
|
// First NALU
|
|
|
|
0x00, 0x00, 0x01, 0x14, 0x34, 0x56, 0x78,
|
|
|
|
// This is part of the first NALU as it is not a valid NALU.
|
|
|
|
0x00, 0x00, 0x00, 0x01, 0x07, 0xbb, 0xcc, 0xdd,
|
|
|
|
// Second NALU
|
|
|
|
0x00, 0x00, 0x01, 0x67, 0x03, 0x04,
|
|
|
|
// This is part of the second NALU.
|
|
|
|
0x00, 0x00, 0x01,
|
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, kIsAnnexbByteStream, kNaluData,
|
2016-03-22 00:39:22 +00:00
|
|
|
arraysize(kNaluData));
|
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
|
|
|
EXPECT_EQ(kNaluData + 9, nalu.data());
|
|
|
|
EXPECT_EQ(11u, nalu.payload_size());
|
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
|
|
|
EXPECT_EQ(0, nalu.ref_idc());
|
|
|
|
EXPECT_EQ(0x14, nalu.type());
|
|
|
|
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
|
|
|
EXPECT_EQ(kNaluData + 24, nalu.data());
|
|
|
|
EXPECT_EQ(5u, nalu.payload_size());
|
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
|
|
|
EXPECT_EQ(3, nalu.ref_idc());
|
|
|
|
EXPECT_EQ(7, nalu.type());
|
|
|
|
|
|
|
|
EXPECT_EQ(NaluReader::kEOStream, reader.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
2016-02-05 19:08:07 +00:00
|
|
|
TEST(NaluReaderTest, OneByteNaluLength) {
|
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0x05, 0x06, 0x01, 0x02, 0x03, 0x04,
|
|
|
|
// Second NALU
|
|
|
|
0x06, 0x67, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, 1, kNaluData, arraysize(kNaluData));
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(kNaluData + 1, nalu.data());
|
2016-02-26 22:49:57 +00:00
|
|
|
EXPECT_EQ(4u, nalu.payload_size());
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(0, nalu.ref_idc());
|
2016-03-22 00:39:22 +00:00
|
|
|
EXPECT_EQ(6, nalu.type());
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(kNaluData + 7, nalu.data());
|
2016-02-26 22:49:57 +00:00
|
|
|
EXPECT_EQ(5u, nalu.payload_size());
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(3, nalu.ref_idc());
|
|
|
|
EXPECT_EQ(7, nalu.type());
|
|
|
|
|
|
|
|
EXPECT_EQ(NaluReader::kEOStream, reader.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
2016-02-22 19:11:29 +00:00
|
|
|
TEST(NaluReaderTest, FourByteNaluLength) {
|
2016-02-05 19:08:07 +00:00
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0x00, 0x00, 0x00, 0x07, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
|
|
|
// Second NALU
|
|
|
|
0x00, 0x00, 0x00, 0x03, 0x67, 0x0a, 0x0b,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, 4, kNaluData, arraysize(kNaluData));
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(kNaluData + 4, nalu.data());
|
2016-02-26 22:49:57 +00:00
|
|
|
EXPECT_EQ(6u, nalu.payload_size());
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(0, nalu.ref_idc());
|
2016-03-22 00:39:22 +00:00
|
|
|
EXPECT_EQ(6, nalu.type());
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(kNaluData + 15, nalu.data());
|
2016-02-26 22:49:57 +00:00
|
|
|
EXPECT_EQ(2u, nalu.payload_size());
|
2016-02-22 19:11:29 +00:00
|
|
|
EXPECT_EQ(1u, nalu.header_size());
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(3, nalu.ref_idc());
|
|
|
|
EXPECT_EQ(7, nalu.type());
|
|
|
|
|
|
|
|
EXPECT_EQ(NaluReader::kEOStream, reader.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NaluReaderTest, ErrorForNotEnoughForNaluLength) {
|
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0x00,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, 3, kNaluData, arraysize(kNaluData));
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
EXPECT_EQ(NaluReader::kInvalidStream, reader.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NaluReaderTest, ErrorForNaluLengthExceedsRemainingData) {
|
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0xFF, 0x08, 0x00,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, 1, kNaluData, arraysize(kNaluData));
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
EXPECT_EQ(NaluReader::kInvalidStream, reader.Advance(&nalu));
|
|
|
|
|
|
|
|
// Another test for off by one.
|
|
|
|
const uint8_t kNaluData2[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0x04, 0x08, 0x00, 0x00,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader2(Nalu::kH264, 1, kNaluData2, arraysize(kNaluData2));
|
2016-02-05 19:08:07 +00:00
|
|
|
EXPECT_EQ(NaluReader::kInvalidStream, reader2.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NaluReaderTest, ErrorForForbiddenBitSet) {
|
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0x03, 0x80, 0x00, 0x00,
|
2016-02-05 19:08:07 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 20:35:49 +00:00
|
|
|
NaluReader reader(Nalu::kH264, 1, kNaluData, arraysize(kNaluData));
|
2016-02-05 19:08:07 +00:00
|
|
|
|
|
|
|
Nalu nalu;
|
|
|
|
EXPECT_EQ(NaluReader::kInvalidStream, reader.Advance(&nalu));
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:34:14 +00:00
|
|
|
TEST(NaluReaderTest, ErrorForZeroSize) {
|
|
|
|
const uint8_t kNaluData[] = {
|
2016-03-22 00:39:22 +00:00
|
|
|
// First NALU
|
|
|
|
0x03, 0x80, 0x00, 0x00,
|
2016-02-26 22:34:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Nalu nalu;
|
2016-04-21 20:35:49 +00:00
|
|
|
EXPECT_FALSE(nalu.Initialize(Nalu::kH264, kNaluData, 0));
|
|
|
|
EXPECT_FALSE(nalu.Initialize(Nalu::kH265, kNaluData, 0));
|
2016-02-26 22:34:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 19:08:07 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|