Replace Nalu::InitializeFrom* with Initialize.

Replaces the InitializeFromH264 and InitializeFromH265 methods with
a single Initialize method that accepts a CodecType.  Also moves the
CodecType enum from NaluReader to Nalu.

Change-Id: Ie33aa9c0d772a7f3c6a2e9c867e25be3edfe7828
This commit is contained in:
Jacob Trimble 2016-04-21 13:35:49 -07:00
parent 77fc3f6988
commit a789dc6d3c
15 changed files with 67 additions and 63 deletions

View File

@ -54,7 +54,7 @@ bool AVCDecoderConfiguration::ParseInternal() {
RCHECK(reader.SkipBytes(size));
Nalu nalu;
RCHECK(nalu.InitializeFromH264(nalu_data, size));
RCHECK(nalu.Initialize(Nalu::kH264, nalu_data, size));
RCHECK(nalu.type() == Nalu::H264_SPS);
AddNalu(nalu);
@ -80,7 +80,7 @@ bool AVCDecoderConfiguration::ParseInternal() {
RCHECK(reader.SkipBytes(size));
Nalu nalu;
RCHECK(nalu.InitializeFromH264(nalu_data, size));
RCHECK(nalu.Initialize(Nalu::kH264, nalu_data, size));
RCHECK(nalu.type() == Nalu::H264_PPS);
AddNalu(nalu);
}

View File

@ -16,7 +16,7 @@ namespace edash_packager {
namespace media {
H264ByteToUnitStreamConverter::H264ByteToUnitStreamConverter()
: H26xByteToUnitStreamConverter(NaluReader::kH264) {}
: H26xByteToUnitStreamConverter(Nalu::kH264) {}
H264ByteToUnitStreamConverter::~H264ByteToUnitStreamConverter() {}
bool H264ByteToUnitStreamConverter::GetDecoderConfigurationRecord(

View File

@ -18,7 +18,7 @@ TEST(H264ParserTest, StreamFileParsing) {
int num_nalus = 759;
H264Parser parser;
NaluReader reader(NaluReader::kH264, kIsAnnexbByteStream, buffer.data(),
NaluReader reader(Nalu::kH264, kIsAnnexbByteStream, buffer.data(),
buffer.size());
// Parse until the end of stream/unsupported stream/error in stream is found.
@ -74,7 +74,7 @@ TEST(H264ParserTest, ExtractResolutionFromSpsData) {
H264Parser parser;
int sps_id = 0;
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH264(kSps, arraysize(kSps)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH264, kSps, arraysize(kSps)));
ASSERT_EQ(H264Parser::kOk, parser.ParseSps(nalu, &sps_id));
uint32_t coded_width = 0;
@ -99,7 +99,7 @@ TEST(H264ParserTest, ExtractResolutionFromSpsDataWithCropping) {
H264Parser parser;
int sps_id = 0;
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH264(kSps, arraysize(kSps)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH264, kSps, arraysize(kSps)));
ASSERT_EQ(H264Parser::kOk, parser.ParseSps(nalu, &sps_id));
uint32_t coded_width = 0;

View File

@ -17,7 +17,7 @@ namespace edash_packager {
namespace media {
H265ByteToUnitStreamConverter::H265ByteToUnitStreamConverter()
: H26xByteToUnitStreamConverter(NaluReader::kH265) {}
: H26xByteToUnitStreamConverter(Nalu::kH265) {}
H265ByteToUnitStreamConverter::~H265ByteToUnitStreamConverter() {}
bool H265ByteToUnitStreamConverter::GetDecoderConfigurationRecord(
@ -33,7 +33,7 @@ bool H265ByteToUnitStreamConverter::GetDecoderConfigurationRecord(
int id;
Nalu nalu;
H265Parser parser;
RCHECK(nalu.InitializeFromH265(last_sps_.data(), last_sps_.size()));
RCHECK(nalu.Initialize(Nalu::kH265, last_sps_.data(), last_sps_.size()));
RCHECK(parser.ParseSps(nalu, &id) == H265Parser::kOk);
const H265Sps* sps = parser.GetSps(id);

View File

@ -36,13 +36,13 @@ TEST(H265ParserTest, ParseSliceHeader) {
int id;
Nalu nalu;
H265Parser parser;
ASSERT_TRUE(nalu.InitializeFromH265(kSpsData, arraysize(kSpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kSpsData, arraysize(kSpsData)));
ASSERT_EQ(H265Parser::kOk, parser.ParseSps(nalu, &id));
ASSERT_TRUE(nalu.InitializeFromH265(kPpsData, arraysize(kPpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kPpsData, arraysize(kPpsData)));
ASSERT_EQ(H265Parser::kOk, parser.ParsePps(nalu, &id));
// Parse the slice header.
ASSERT_TRUE(nalu.InitializeFromH265(kSliceData, arraysize(kSliceData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kSliceData, arraysize(kSliceData)));
ASSERT_EQ(Nalu::H265_IDR_W_RADL, nalu.type());
H265SliceHeader header;
@ -63,13 +63,14 @@ TEST(H265ParserTest, ParseSliceHeader_NonIDR) {
int id;
Nalu nalu;
H265Parser parser;
ASSERT_TRUE(nalu.InitializeFromH265(kSpsData, arraysize(kSpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kSpsData, arraysize(kSpsData)));
ASSERT_EQ(H265Parser::kOk, parser.ParseSps(nalu, &id));
ASSERT_TRUE(nalu.InitializeFromH265(kPpsData, arraysize(kPpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kPpsData, arraysize(kPpsData)));
ASSERT_EQ(H265Parser::kOk, parser.ParsePps(nalu, &id));
// Parse the slice header.
ASSERT_TRUE(nalu.InitializeFromH265(kSliceData2, arraysize(kSliceData2)));
ASSERT_TRUE(
nalu.Initialize(Nalu::kH265, kSliceData2, arraysize(kSliceData2)));
ASSERT_EQ(1 /* TRAIL_R */, nalu.type());
H265SliceHeader header;
@ -85,7 +86,7 @@ TEST(H265ParserTest, ParseSliceHeader_NonIDR) {
TEST(H265ParserTest, ParseSps) {
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH265(kSpsData, arraysize(kSpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kSpsData, arraysize(kSpsData)));
ASSERT_EQ(Nalu::H265_SPS, nalu.type());
int id = 12;
@ -108,7 +109,7 @@ TEST(H265ParserTest, ParseSps) {
TEST(H265ParserTest, ParsePps) {
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH265(kPpsData, arraysize(kPpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kPpsData, arraysize(kPpsData)));
ASSERT_EQ(Nalu::H265_PPS, nalu.type());
int id = 12;
@ -129,7 +130,7 @@ TEST(H265ParserTest, ExtractResolutionFromSpsData) {
H265Parser parser;
int sps_id = 0;
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH265(kSpsData, arraysize(kSpsData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH265, kSpsData, arraysize(kSpsData)));
ASSERT_EQ(H265Parser::kOk, parser.ParseSps(nalu, &sps_id));
uint32_t coded_width = 0;
@ -155,7 +156,8 @@ TEST(H265ParserTest, ExtractResolutionFromSpsDataWithCrop) {
H265Parser parser;
int sps_id = 0;
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH265(kSpsCropData, arraysize(kSpsCropData)));
ASSERT_TRUE(
nalu.Initialize(Nalu::kH265, kSpsCropData, arraysize(kSpsCropData)));
ASSERT_EQ(H265Parser::kOk, parser.ParseSps(nalu, &sps_id));
uint32_t coded_width = 0;

View File

@ -21,7 +21,7 @@ const size_t kStreamConversionOverhead = 100;
}
H26xByteToUnitStreamConverter::H26xByteToUnitStreamConverter(
NaluReader::CodecType type)
Nalu::CodecType type)
: type_(type) {}
H26xByteToUnitStreamConverter::~H26xByteToUnitStreamConverter() {}

View File

@ -23,7 +23,7 @@ class H26xByteToUnitStreamConverter {
public:
static const size_t kUnitStreamNaluLengthSize = 4;
H26xByteToUnitStreamConverter(NaluReader::CodecType type);
H26xByteToUnitStreamConverter(Nalu::CodecType type);
virtual ~H26xByteToUnitStreamConverter();
/// Converts a whole byte stream encoded video frame to NAL unit stream
@ -52,7 +52,7 @@ class H26xByteToUnitStreamConverter {
// not be copied to the buffer.
virtual bool ProcessNalu(const Nalu& nalu) = 0;
NaluReader::CodecType type_;
Nalu::CodecType type_;
DISALLOW_COPY_AND_ASSIGN(H26xByteToUnitStreamConverter);
};

View File

@ -122,7 +122,7 @@ bool HEVCDecoderConfiguration::ParseInternal() {
RCHECK(reader.SkipBytes(nalu_length));
Nalu nalu;
RCHECK(nalu.InitializeFromH265(data() + nalu_offset, nalu_length));
RCHECK(nalu.Initialize(Nalu::kH265, data() + nalu_offset, nalu_length));
RCHECK(nalu.type() == nal_unit_type);
AddNalu(nalu);
}

View File

@ -155,8 +155,7 @@ bool NalUnitToByteStreamConverter::ConvertUnitToByteStream(
if (is_key_frame)
buffer_writer.AppendVector(decoder_configuration_in_byte_stream_);
NaluReader nalu_reader(NaluReader::kH264, nalu_length_size_, sample,
sample_size);
NaluReader nalu_reader(Nalu::kH264, nalu_length_size_, sample, sample_size);
Nalu nalu;
NaluReader::Result result = nalu_reader.Advance(&nalu);

View File

@ -31,6 +31,17 @@ Nalu::Nalu()
type_(0),
is_video_slice_(false) {}
bool Nalu::Initialize(CodecType type,
const uint8_t* data,
uint64_t size) {
if (type == Nalu::kH264) {
return InitializeFromH264(data, size);
} else {
DCHECK_EQ(Nalu::kH265, type);
return InitializeFromH265(data, size);
}
}
// ITU-T H.264 (02/2014) 7.4.1 NAL unit semantics
bool Nalu::InitializeFromH264(const uint8_t* data, uint64_t size) {
DCHECK(data);
@ -142,7 +153,7 @@ bool Nalu::InitializeFromH265(const uint8_t* data, uint64_t size) {
return true;
}
NaluReader::NaluReader(CodecType type,
NaluReader::NaluReader(Nalu::CodecType type,
uint8_t nal_length_size,
const uint8_t* stream,
uint64_t stream_size)
@ -193,14 +204,8 @@ NaluReader::Result NaluReader::Advance(Nalu* nalu) {
}
const uint8_t* nalu_data = stream_ + nalu_length_size_or_start_code_size;
if (nalu_type_ == kH264) {
if (!nalu->InitializeFromH264(nalu_data, nalu_length))
return NaluReader::kInvalidStream;
} else {
DCHECK_EQ(kH265, nalu_type_);
if (!nalu->InitializeFromH265(nalu_data, nalu_length))
return NaluReader::kInvalidStream;
}
if (!nalu->Initialize(nalu_type_, nalu_data, nalu_length))
return NaluReader::kInvalidStream;
// Move parser state to after this NALU, so next time Advance
// is called, we will effectively be skipping it.
@ -302,9 +307,7 @@ bool NaluReader::LocateNaluByStartCode(uint64_t* nalu_size,
// If it is not a valid NAL unit, we will continue searching. This is to
// handle the case where emulation prevention are not applied.
Nalu nalu;
if (nalu_type_ == kH264
? nalu.InitializeFromH264(nalu_data, max_nalu_data_size)
: nalu.InitializeFromH265(nalu_data, max_nalu_data_size)) {
if (nalu.Initialize(nalu_type_, nalu_data, max_nalu_data_size)) {
nalu_data -= next_start_code_size;
break;
}

View File

@ -72,14 +72,16 @@ class Nalu {
H265_RSV_NVCL41 = 41,
};
enum CodecType {
kH264,
kH265,
};
Nalu();
bool InitializeFromH264(const uint8_t* data,
uint64_t size) WARN_UNUSED_RESULT;
bool InitializeFromH265(const uint8_t* data,
uint64_t size) WARN_UNUSED_RESULT;
bool Initialize(CodecType type,
const uint8_t* data,
uint64_t size) WARN_UNUSED_RESULT;
const uint8_t* data() const { return data_; }
uint64_t header_size() const { return header_size_; }
@ -96,6 +98,9 @@ class Nalu {
bool is_video_slice() const { return is_video_slice_; }
private:
bool InitializeFromH264(const uint8_t* data, uint64_t size);
bool InitializeFromH265(const uint8_t* data, uint64_t size);
// A pointer to the NALU (i.e. points to the header). This pointer is not
// owned by this instance.
const uint8_t* data_;
@ -125,15 +130,11 @@ class NaluReader {
kInvalidStream, // error in stream
kEOStream, // end of stream
};
enum CodecType {
kH264,
kH265,
};
/// @param nalu_length_size should be set to 0 for AnnexB byte streams;
/// otherwise, it indicates the size of NAL unit length for the NAL
/// unit stream.
NaluReader(CodecType type,
NaluReader(Nalu::CodecType type,
uint8_t nal_length_size,
const uint8_t* stream,
uint64_t stream_size);
@ -183,7 +184,7 @@ class NaluReader {
// The remaining size of the stream.
uint64_t stream_size_;
// The type of NALU being read.
CodecType nalu_type_;
Nalu::CodecType nalu_type_;
// The number of bytes the prefix length is; only valid if format is
// kAnnexbByteStreamFormat.
uint8_t nalu_length_size_;

View File

@ -20,7 +20,7 @@ TEST(NaluReaderTest, StartCodeSearch) {
0x00, 0x00, 0x00, 0x01, 0x67, 0xbb, 0xcc, 0xdd,
};
NaluReader reader(NaluReader::kH264, kIsAnnexbByteStream, kNaluData,
NaluReader reader(Nalu::kH264, kIsAnnexbByteStream, kNaluData,
arraysize(kNaluData));
Nalu nalu;
@ -54,7 +54,7 @@ TEST(NaluReaderTest, StartCodeSearchWithStartCodeInsideNalUnit) {
0x00, 0x00, 0x01,
};
NaluReader reader(NaluReader::kH264, kIsAnnexbByteStream, kNaluData,
NaluReader reader(Nalu::kH264, kIsAnnexbByteStream, kNaluData,
arraysize(kNaluData));
Nalu nalu;
@ -83,7 +83,7 @@ TEST(NaluReaderTest, OneByteNaluLength) {
0x06, 0x67, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
};
NaluReader reader(NaluReader::kH264, 1, kNaluData, arraysize(kNaluData));
NaluReader reader(Nalu::kH264, 1, kNaluData, arraysize(kNaluData));
Nalu nalu;
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
@ -111,7 +111,7 @@ TEST(NaluReaderTest, FourByteNaluLength) {
0x00, 0x00, 0x00, 0x03, 0x67, 0x0a, 0x0b,
};
NaluReader reader(NaluReader::kH264, 4, kNaluData, arraysize(kNaluData));
NaluReader reader(Nalu::kH264, 4, kNaluData, arraysize(kNaluData));
Nalu nalu;
ASSERT_EQ(NaluReader::kOk, reader.Advance(&nalu));
@ -137,7 +137,7 @@ TEST(NaluReaderTest, ErrorForNotEnoughForNaluLength) {
0x00,
};
NaluReader reader(NaluReader::kH264, 3, kNaluData, arraysize(kNaluData));
NaluReader reader(Nalu::kH264, 3, kNaluData, arraysize(kNaluData));
Nalu nalu;
EXPECT_EQ(NaluReader::kInvalidStream, reader.Advance(&nalu));
@ -149,7 +149,7 @@ TEST(NaluReaderTest, ErrorForNaluLengthExceedsRemainingData) {
0xFF, 0x08, 0x00,
};
NaluReader reader(NaluReader::kH264, 1, kNaluData, arraysize(kNaluData));
NaluReader reader(Nalu::kH264, 1, kNaluData, arraysize(kNaluData));
Nalu nalu;
EXPECT_EQ(NaluReader::kInvalidStream, reader.Advance(&nalu));
@ -160,7 +160,7 @@ TEST(NaluReaderTest, ErrorForNaluLengthExceedsRemainingData) {
0x04, 0x08, 0x00, 0x00,
};
NaluReader reader2(NaluReader::kH264, 1, kNaluData2, arraysize(kNaluData2));
NaluReader reader2(Nalu::kH264, 1, kNaluData2, arraysize(kNaluData2));
EXPECT_EQ(NaluReader::kInvalidStream, reader2.Advance(&nalu));
}
@ -170,7 +170,7 @@ TEST(NaluReaderTest, ErrorForForbiddenBitSet) {
0x03, 0x80, 0x00, 0x00,
};
NaluReader reader(NaluReader::kH264, 1, kNaluData, arraysize(kNaluData));
NaluReader reader(Nalu::kH264, 1, kNaluData, arraysize(kNaluData));
Nalu nalu;
EXPECT_EQ(NaluReader::kInvalidStream, reader.Advance(&nalu));
@ -183,8 +183,8 @@ TEST(NaluReaderTest, ErrorForZeroSize) {
};
Nalu nalu;
EXPECT_FALSE(nalu.InitializeFromH264(kNaluData, 0));
EXPECT_FALSE(nalu.InitializeFromH265(kNaluData, 0));
EXPECT_FALSE(nalu.Initialize(Nalu::kH264, kNaluData, 0));
EXPECT_FALSE(nalu.Initialize(Nalu::kH265, kNaluData, 0));
}
} // namespace media

View File

@ -182,8 +182,7 @@ bool EsParserH264::ParseInternal() {
int access_unit_size = base::checked_cast<int, int64_t>(
next_access_unit_pos_ - current_access_unit_pos_);
DCHECK_LE(access_unit_size, size);
NaluReader reader(NaluReader::kH264, kIsAnnexbByteStream, es,
access_unit_size);
NaluReader reader(Nalu::kH264, kIsAnnexbByteStream, es, access_unit_size);
while (true) {
Nalu nalu;

View File

@ -271,10 +271,10 @@ Status EncryptingFragmenter::EncryptSample(scoped_refptr<MediaSample> sample) {
data += frame.frame_size;
}
} else {
const NaluReader::CodecType nalu_type =
const Nalu::CodecType nalu_type =
(video_codec_ == kCodecHVC1 || video_codec_ == kCodecHEV1)
? NaluReader::kH265
: NaluReader::kH264;
? Nalu::kH265
: Nalu::kH264;
NaluReader reader(nalu_type, nalu_length_size_, data,
sample->data_size());

View File

@ -43,7 +43,7 @@ TEST(H264VideoSliceHeaderParserTest, BasicSupport) {
ASSERT_TRUE(parser.Initialize(extra_data));
Nalu nalu;
ASSERT_TRUE(nalu.InitializeFromH264(kData, arraysize(kData)));
ASSERT_TRUE(nalu.Initialize(Nalu::kH264, kData, arraysize(kData)));
// Real header size is 34 bits, but we round up to 5 bytes.
EXPECT_EQ(5, parser.GetHeaderSize(nalu));
}