From e1b4d243e288b9517e9ea66596dae4e597a08f0a Mon Sep 17 00:00:00 2001 From: "jianjun.zhu" Date: Mon, 15 Aug 2016 18:56:18 -0700 Subject: [PATCH] Skip AUD while extracting SPS and PPS on iOS. H.264 frames may have AUD before SPS. This change detects AUD and try to extract SPS and PPS behind AUD. BUG=webrtc:6173 Review-Url: https://codereview.webrtc.org/2209143002 Cr-Commit-Position: refs/heads/master@{#13765} --- .../codecs/h264/h264_video_toolbox_nalu.cc | 27 ++++++++++++++++--- .../h264/h264_video_toolbox_nalu_unittest.cc | 8 ++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.cc b/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.cc index b0c4bd326d..c6c0406128 100644 --- a/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.cc +++ b/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.cc @@ -19,9 +19,15 @@ #include "webrtc/base/checks.h" #include "webrtc/base/logging.h" +#include "webrtc/common_video/h264/h264_common.h" namespace webrtc { +using H264::NaluType; +using H264::kAud; +using H264::kSps; +using H264::ParseNaluType; + const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1}; const size_t kAvccHeaderByteSize = sizeof(uint32_t); @@ -253,9 +259,17 @@ bool H264AnnexBBufferHasVideoFormatDescription(const uint8_t* annexb_buffer, // embedded by the RTP depacketizer. Extract NALU information. // TODO(tkchin): handle potential case where sps and pps are delivered // separately. - uint8_t first_nalu_type = annexb_buffer[4] & 0x1f; - bool is_first_nalu_type_sps = first_nalu_type == 0x7; - return is_first_nalu_type_sps; + NaluType first_nalu_type = ParseNaluType(annexb_buffer[4]); + bool is_first_nalu_type_sps = first_nalu_type == kSps; + if (is_first_nalu_type_sps) + return true; + bool is_first_nalu_type_aud = first_nalu_type == kAud; + // Start code + access unit delimiter + start code = 4 + 2 + 4 = 10. + if (!is_first_nalu_type_aud || annexb_buffer_size <= 10u) + return false; + NaluType second_nalu_type = ParseNaluType(annexb_buffer[10]); + bool is_second_nalu_type_sps = second_nalu_type == kSps; + return is_second_nalu_type_sps; } CMVideoFormatDescriptionRef CreateVideoFormatDescription( @@ -271,6 +285,13 @@ CMVideoFormatDescriptionRef CreateVideoFormatDescription( // Parse the SPS and PPS into a CMVideoFormatDescription. const uint8_t* param_set_ptrs[2] = {}; size_t param_set_sizes[2] = {}; + // Skip AUD. + if (ParseNaluType(annexb_buffer[4]) == kAud) { + if (!reader.ReadNalu(¶m_set_ptrs[0], ¶m_set_sizes[0])) { + LOG(LS_ERROR) << "Failed to read AUD"; + return nullptr; + } + } if (!reader.ReadNalu(¶m_set_ptrs[0], ¶m_set_sizes[0])) { LOG(LS_ERROR) << "Failed to read SPS"; return nullptr; diff --git a/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu_unittest.cc b/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu_unittest.cc index 4ac9cd0db1..a263707f4a 100644 --- a/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu_unittest.cc +++ b/webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu_unittest.cc @@ -27,9 +27,17 @@ TEST(H264VideoToolboxNaluTest, TestHasVideoFormatDescription) { const uint8_t sps_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x27}; EXPECT_TRUE(H264AnnexBBufferHasVideoFormatDescription(sps_buffer, arraysize(sps_buffer))); + const uint8_t aud_sps_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x29, + 0x00, 0x00, 0x00, 0x01, 0x27}; + EXPECT_TRUE(H264AnnexBBufferHasVideoFormatDescription( + aud_sps_buffer, arraysize(aud_sps_buffer))); const uint8_t other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x28}; EXPECT_FALSE(H264AnnexBBufferHasVideoFormatDescription( other_buffer, arraysize(other_buffer))); + const uint8_t aud_other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x29, + 0x00, 0x00, 0x00, 0x01, 0x28}; + EXPECT_FALSE(H264AnnexBBufferHasVideoFormatDescription( + aud_other_buffer, arraysize(aud_other_buffer))); } TEST(H264VideoToolboxNaluTest, TestCreateVideoFormatDescription) {