diff --git a/packager/media/filters/avc_decoder_configuration.cc b/packager/media/filters/avc_decoder_configuration.cc index ae78e725be..57fd6441b6 100644 --- a/packager/media/filters/avc_decoder_configuration.cc +++ b/packager/media/filters/avc_decoder_configuration.cc @@ -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); } diff --git a/packager/media/filters/h264_byte_to_unit_stream_converter.cc b/packager/media/filters/h264_byte_to_unit_stream_converter.cc index 3fd734e45c..4f2693926e 100644 --- a/packager/media/filters/h264_byte_to_unit_stream_converter.cc +++ b/packager/media/filters/h264_byte_to_unit_stream_converter.cc @@ -16,7 +16,7 @@ namespace edash_packager { namespace media { H264ByteToUnitStreamConverter::H264ByteToUnitStreamConverter() - : H26xByteToUnitStreamConverter(NaluReader::kH264) {} + : H26xByteToUnitStreamConverter(Nalu::kH264) {} H264ByteToUnitStreamConverter::~H264ByteToUnitStreamConverter() {} bool H264ByteToUnitStreamConverter::GetDecoderConfigurationRecord( diff --git a/packager/media/filters/h264_parser_unittest.cc b/packager/media/filters/h264_parser_unittest.cc index 7f76df081d..1c8f02f6e7 100644 --- a/packager/media/filters/h264_parser_unittest.cc +++ b/packager/media/filters/h264_parser_unittest.cc @@ -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; diff --git a/packager/media/filters/h265_byte_to_unit_stream_converter.cc b/packager/media/filters/h265_byte_to_unit_stream_converter.cc index 95b3d195a9..a518667c54 100644 --- a/packager/media/filters/h265_byte_to_unit_stream_converter.cc +++ b/packager/media/filters/h265_byte_to_unit_stream_converter.cc @@ -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); diff --git a/packager/media/filters/h265_parser_unittest.cc b/packager/media/filters/h265_parser_unittest.cc index 0f1d9b4c0c..3e133043d8 100644 --- a/packager/media/filters/h265_parser_unittest.cc +++ b/packager/media/filters/h265_parser_unittest.cc @@ -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; diff --git a/packager/media/filters/h26x_byte_to_unit_stream_converter.cc b/packager/media/filters/h26x_byte_to_unit_stream_converter.cc index 38c39db074..4ec2d3dbc2 100644 --- a/packager/media/filters/h26x_byte_to_unit_stream_converter.cc +++ b/packager/media/filters/h26x_byte_to_unit_stream_converter.cc @@ -21,7 +21,7 @@ const size_t kStreamConversionOverhead = 100; } H26xByteToUnitStreamConverter::H26xByteToUnitStreamConverter( - NaluReader::CodecType type) + Nalu::CodecType type) : type_(type) {} H26xByteToUnitStreamConverter::~H26xByteToUnitStreamConverter() {} diff --git a/packager/media/filters/h26x_byte_to_unit_stream_converter.h b/packager/media/filters/h26x_byte_to_unit_stream_converter.h index 3cbc0c6306..781f353e4e 100644 --- a/packager/media/filters/h26x_byte_to_unit_stream_converter.h +++ b/packager/media/filters/h26x_byte_to_unit_stream_converter.h @@ -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); }; diff --git a/packager/media/filters/hevc_decoder_configuration.cc b/packager/media/filters/hevc_decoder_configuration.cc index d505b7cdc2..3feba2745d 100644 --- a/packager/media/filters/hevc_decoder_configuration.cc +++ b/packager/media/filters/hevc_decoder_configuration.cc @@ -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); } diff --git a/packager/media/filters/nal_unit_to_byte_stream_converter.cc b/packager/media/filters/nal_unit_to_byte_stream_converter.cc index 06009d5fbb..c8ebdb43b3 100644 --- a/packager/media/filters/nal_unit_to_byte_stream_converter.cc +++ b/packager/media/filters/nal_unit_to_byte_stream_converter.cc @@ -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); diff --git a/packager/media/filters/nalu_reader.cc b/packager/media/filters/nalu_reader.cc index bfb50f20ba..335789280d 100644 --- a/packager/media/filters/nalu_reader.cc +++ b/packager/media/filters/nalu_reader.cc @@ -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; } diff --git a/packager/media/filters/nalu_reader.h b/packager/media/filters/nalu_reader.h index ac3b0ac001..64f570c04f 100644 --- a/packager/media/filters/nalu_reader.h +++ b/packager/media/filters/nalu_reader.h @@ -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_; diff --git a/packager/media/filters/nalu_reader_unittest.cc b/packager/media/filters/nalu_reader_unittest.cc index c48762f33f..a57b61771b 100644 --- a/packager/media/filters/nalu_reader_unittest.cc +++ b/packager/media/filters/nalu_reader_unittest.cc @@ -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 diff --git a/packager/media/formats/mp2t/es_parser_h264.cc b/packager/media/formats/mp2t/es_parser_h264.cc index c6d59c3fef..6e94fc6960 100644 --- a/packager/media/formats/mp2t/es_parser_h264.cc +++ b/packager/media/formats/mp2t/es_parser_h264.cc @@ -182,8 +182,7 @@ bool EsParserH264::ParseInternal() { int access_unit_size = base::checked_cast( 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; diff --git a/packager/media/formats/mp4/encrypting_fragmenter.cc b/packager/media/formats/mp4/encrypting_fragmenter.cc index 8185e74862..5d8a4ebb66 100644 --- a/packager/media/formats/mp4/encrypting_fragmenter.cc +++ b/packager/media/formats/mp4/encrypting_fragmenter.cc @@ -271,10 +271,10 @@ Status EncryptingFragmenter::EncryptSample(scoped_refptr 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()); diff --git a/packager/media/formats/mp4/video_slice_header_parser_unittest.cc b/packager/media/formats/mp4/video_slice_header_parser_unittest.cc index 1f1c07dfbd..78613f68f2 100644 --- a/packager/media/formats/mp4/video_slice_header_parser_unittest.cc +++ b/packager/media/formats/mp4/video_slice_header_parser_unittest.cc @@ -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)); }