Skip to content

Commit 6fda3aa

Browse files
xingriphiln
authored andcommitted
WebRTC HEVC RFC 7798 RTP Payload Format Implementation
https://bugs.webkit.org/show_bug.cgi?id=258794 Reviewed by Youenn Fablet. The current WebRTC HEVC is using generic packetization instead of RFC 7789 Packetization. So this is about implementing the RFC 7798 Packetization. Fix HEVC depacketizer issues. (#185) Enalbing low latency mode for RTC (#169) Enable HEVC support. (#165) Fix out-of-bounds write in H265VpsSpsPpsTracker (#163) Apply fix bitstream logic to RtpVideoStreamReceiver2 (#142) Add missing CODEC_H265 switch case (#136) Add HEVC support for iOS/Android (#68) H265 packetization_mode setting fix (#53) Add H.265 QP parsing logic (#47) This patch is extracted from following Open WebRTC Toolkit code changes: <open-webrtc-toolkit/owt-deps-webrtc#185> <open-webrtc-toolkit/owt-deps-webrtc#169> <open-webrtc-toolkit/owt-deps-webrtc#165> <open-webrtc-toolkit/owt-deps-webrtc#163> <open-webrtc-toolkit/owt-deps-webrtc#142> <open-webrtc-toolkit/owt-deps-webrtc#136> <open-webrtc-toolkit/owt-deps-webrtc#68> <open-webrtc-toolkit/owt-deps-webrtc#53> <open-webrtc-toolkit/owt-deps-webrtc#47> co-authoured by: taste1981 <jianlin.qiu@intel.com> jianjunz <jianjun.zhu@intel.com> Cyril Lashkevich <notorca@gmail.com> Piasy <xz4215@gmail.com> ShiJinCheng <874042641@qq.com> Andreas Unterhuber <andreas.unterhuber@keepinmind.info> dong-heun <63987238+dong-heun@users.noreply.github.com> * Source/ThirdParty/libwebrtc/Configurations/libwebrtc.xcconfig: * Source/ThirdParty/libwebrtc/Source/webrtc/api/video/video_codec_type.h: * Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_codec.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_codec.h: (webrtc::VideoCodecH265::operator!= const): * Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_decoder_software_fallback_wrapper.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_encoder.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_encoder.h: * Source/ThirdParty/libwebrtc/Source/webrtc/call/rtp_payload_params.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_common.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_common.h: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_pps_parser.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_pps_parser.h: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_sps_parser.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_sps_parser.h: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_vps_parser.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_vps_parser.h: * Source/ThirdParty/libwebrtc/Source/webrtc/logging/rtc_event_log/rtc_event_log_parser.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/media/base/media_constants.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/media/base/media_constants.h: * Source/ThirdParty/libwebrtc/Source/webrtc/media/engine/internal_decoder_factory.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/rtp_rtcp/source/create_video_rtp_depacketizer.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/rtp_rtcp/source/rtp_format.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/rtp_rtcp/source/rtp_format_h265.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/rtp_rtcp/source/rtp_format_h265.h: (webrtc::RtpPacketizerH265::PacketUnit::PacketUnit): (webrtc::VideoRtpDepacketizerH265::~VideoRtpDepacketizerH265): Deleted. * Source/ThirdParty/libwebrtc/Source/webrtc/modules/rtp_rtcp/source/rtp_sender_video.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/rtp_rtcp/source/rtp_video_header.h: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/video_coding/codecs/h265/include/h265_globals.h: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/video_coding/encoded_frame.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/video_coding/h265_vps_sps_pps_tracker.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/video_coding/h265_vps_sps_pps_tracker.h: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/video_coding/include/video_codec_interface.h: * Source/ThirdParty/libwebrtc/Source/webrtc/modules/video_coding/packet_buffer.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/rtc_base/experiments/min_video_bitrate_experiment.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/sdk/objc/components/video_codec/nalu_rewriter.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/sdk/objc/components/video_codec/nalu_rewriter.h: * Source/ThirdParty/libwebrtc/Source/webrtc/test/scenario/video_stream.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/test/video_codec_settings.h: (webrtc::test::CodecSettings): * Source/ThirdParty/libwebrtc/Source/webrtc/video/config/video_encoder_config.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/video/config/video_encoder_config.h: * Source/ThirdParty/libwebrtc/Source/webrtc/video/encoder_overshoot_detector.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/video/rtp_video_stream_receiver2.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/video/rtp_video_stream_receiver2.h: * Source/ThirdParty/libwebrtc/Source/webrtc/video/send_statistics_proxy.cc: * Source/ThirdParty/libwebrtc/Source/webrtc/video/video_stream_encoder.cc: * Source/ThirdParty/libwebrtc/libwebrtc.xcodeproj/project.pbxproj: Canonical link: https://commits.webkit.org/267677@main
1 parent 0a89cc4 commit 6fda3aa

46 files changed

Lines changed: 1297 additions & 719 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Source/ThirdParty/libwebrtc/Configurations/libwebrtc.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ INSTALLHDRS_SCRIPT_PHASE = YES;
7676
WARNING_CFLAGS = -Wno-deprecated-declarations $(inherited);
7777

7878
// FIXME: Set WEBRTC_USE_BUILTIN_ISAC_FIX and WEBRTC_USE_BUILTIN_ISAC_FLOAT for iOS and Mac
79-
GCC_PREPROCESSOR_DEFINITIONS = GTEST_RELATIVE_PATH WEBRTC_OPUS_SUPPORT_120MS_PTIME=0 WEBRTC_POSIX WEBRTC_MAC SSL_USE_OPENSSL FEATURE_ENABLE_SSL HAVE_SRTP HAVE_NETINET_IN_H HAVE_OPENSSL_SSL_H SCTP_PROCESS_LEVEL_LOCKS SCTP_SIMPLE_ALLOCATOR SCTP_USE_OPENSSL_SHA1 __Userspace__ HAVE_SA_LEN HAVE_SCONN_LEN __APPLE_USE_RFC_2292 __Userspace_os_Darwin NON_WINDOWS_DEFINE HAVE_WEBRTC_VIDEO HAVE_WEBRTC_VOICE WEBRTC_INTELLIGIBILITY_ENHANCER=0 WEBRTC_APM_DEBUG_DUMP=0 WEBRTC_NS_FLOAT WEBRTC_USE_BUILTIN_OPUS WEBRTC_CODEC_OPUS WEBRTC_CODEC_ISAC WEBRTC_CODEC_RED HAVE_STDINT_H HAVE_STDLIB_H HAVE_UINT64_T OPENSSL HAVE_CONFIG_H WEBRTC_WEBKIT_BUILD HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE HAVE_SCTP WEBRTC_CODEC_G711 WEBRTC_CODEC_G722 WEBRTC_OPUS_VARIABLE_COMPLEXITY=0 WEBRTC_USE_BUILTIN_ISAC_FIX=1 WEBRTC_USE_BUILTIN_ISAC_FLOAT=0 USE_BUILTIN_SW_CODECS WEBRTC_WEBKIT_BUILD RTC_ENABLE_VP9 NO_MAIN_THREAD_WRAPPING RTC_DISABLE_TRACE_EVENTS WEBRTC_HAVE_SCTP WEBRTC_EXCLUDE_TRANSIENT_SUPPRESSOR WEBRTC_HAVE_DCSCTP $(inherited);
79+
GCC_PREPROCESSOR_DEFINITIONS = GTEST_RELATIVE_PATH WEBRTC_OPUS_SUPPORT_120MS_PTIME=0 WEBRTC_POSIX WEBRTC_MAC SSL_USE_OPENSSL FEATURE_ENABLE_SSL HAVE_SRTP HAVE_NETINET_IN_H HAVE_OPENSSL_SSL_H SCTP_PROCESS_LEVEL_LOCKS SCTP_SIMPLE_ALLOCATOR SCTP_USE_OPENSSL_SHA1 __Userspace__ HAVE_SA_LEN HAVE_SCONN_LEN __APPLE_USE_RFC_2292 __Userspace_os_Darwin NON_WINDOWS_DEFINE HAVE_WEBRTC_VIDEO HAVE_WEBRTC_VOICE WEBRTC_INTELLIGIBILITY_ENHANCER=0 WEBRTC_APM_DEBUG_DUMP=0 WEBRTC_NS_FLOAT WEBRTC_USE_H265 WEBRTC_USE_BUILTIN_OPUS WEBRTC_CODEC_OPUS WEBRTC_CODEC_ISAC WEBRTC_CODEC_RED HAVE_STDINT_H HAVE_STDLIB_H HAVE_UINT64_T OPENSSL HAVE_CONFIG_H WEBRTC_WEBKIT_BUILD HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE HAVE_SCTP WEBRTC_CODEC_G711 WEBRTC_CODEC_G722 WEBRTC_OPUS_VARIABLE_COMPLEXITY=0 WEBRTC_USE_BUILTIN_ISAC_FIX=1 WEBRTC_USE_BUILTIN_ISAC_FLOAT=0 USE_BUILTIN_SW_CODECS WEBRTC_WEBKIT_BUILD RTC_ENABLE_VP9 NO_MAIN_THREAD_WRAPPING RTC_DISABLE_TRACE_EVENTS WEBRTC_HAVE_SCTP WEBRTC_EXCLUDE_TRANSIENT_SUPPRESSOR WEBRTC_HAVE_DCSCTP $(inherited);
8080

8181
GCC_PREPROCESSOR_DEFINITIONS[sdk=macosx*] = $(inherited) WEBRTC_USE_VTB_HARDWARE_ENCODER $(GCC_PREPROCESSOR_DEFINITIONS_$(WK_IS_CATALYST));
8282
GCC_PREPROCESSOR_DEFINITIONS_YES = WEBRTC_MAC_CATALYST WEBRTC_IOS;

Source/ThirdParty/libwebrtc/Source/webrtc/api/video/video_codec_type.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ enum VideoCodecType {
2121
kVideoCodecVP9,
2222
kVideoCodecAV1,
2323
kVideoCodecH264,
24-
#ifndef DISABLE_H265
2524
kVideoCodecH265,
26-
#endif
2725
kVideoCodecMultiplex,
2826
};
2927

Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_codec.cc

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ constexpr char kPayloadNameAv1[] = "AV1";
2626
// needed.
2727
constexpr char kPayloadNameAv1x[] = "AV1X";
2828
constexpr char kPayloadNameH264[] = "H264";
29-
#ifndef DISABLE_H265
29+
#ifdef WEBRTC_USE_H265
3030
constexpr char kPayloadNameH265[] = "H265";
3131
#endif
3232
constexpr char kPayloadNameGeneric[] = "Generic";
@@ -55,6 +55,15 @@ bool VideoCodecH264::operator==(const VideoCodecH264& other) const {
5555
numberOfTemporalLayers == other.numberOfTemporalLayers);
5656
}
5757

58+
bool VideoCodecH265::operator==(const VideoCodecH265& other) const {
59+
return (frameDroppingOn == other.frameDroppingOn &&
60+
keyFrameInterval == other.keyFrameInterval &&
61+
vpsLen == other.vpsLen && spsLen == other.spsLen &&
62+
ppsLen == other.ppsLen &&
63+
(spsLen == 0 || memcmp(spsData, other.spsData, spsLen) == 0) &&
64+
(ppsLen == 0 || memcmp(ppsData, other.ppsData, ppsLen) == 0));
65+
}
66+
5867
VideoCodec::VideoCodec()
5968
: codecType(kVideoCodecGeneric),
6069
width(0),
@@ -105,6 +114,18 @@ const VideoCodecH264& VideoCodec::H264() const {
105114
return codec_specific_.H264;
106115
}
107116

117+
#ifdef WEBRTC_USE_H265
118+
VideoCodecH265* VideoCodec::H265() {
119+
RTC_DCHECK_EQ(codecType, kVideoCodecH265);
120+
return &codec_specific_.H265;
121+
}
122+
123+
const VideoCodecH265& VideoCodec::H265() const {
124+
RTC_DCHECK_EQ(codecType, kVideoCodecH265);
125+
return codec_specific_.H265;
126+
}
127+
#endif
128+
108129
const char* CodecTypeToPayloadString(VideoCodecType type) {
109130
switch (type) {
110131
case kVideoCodecVP8:
@@ -115,7 +136,7 @@ const char* CodecTypeToPayloadString(VideoCodecType type) {
115136
return kPayloadNameAv1;
116137
case kVideoCodecH264:
117138
return kPayloadNameH264;
118-
#ifndef DISABLE_H265
139+
#ifdef WEBRTC_USE_H265
119140
case kVideoCodecH265:
120141
return kPayloadNameH265;
121142
#endif
@@ -137,7 +158,7 @@ VideoCodecType PayloadStringToCodecType(const std::string& name) {
137158
return kVideoCodecAV1;
138159
if (absl::EqualsIgnoreCase(name, kPayloadNameH264))
139160
return kVideoCodecH264;
140-
#ifndef DISABLE_H265
161+
#ifdef WEBRTC_USE_H265
141162
if (absl::EqualsIgnoreCase(name, kPayloadNameH265))
142163
return kVideoCodecH265;
143164
#endif

Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_codec.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ struct VideoCodecH264 {
9797
uint8_t numberOfTemporalLayers;
9898
};
9999

100+
struct VideoCodecH265 {
101+
bool operator==(const VideoCodecH265& other) const;
102+
bool operator!=(const VideoCodecH265& other) const {
103+
return !(*this == other);
104+
}
105+
bool frameDroppingOn;
106+
int keyFrameInterval;
107+
const uint8_t* vpsData;
108+
size_t vpsLen;
109+
const uint8_t* spsData;
110+
size_t spsLen;
111+
const uint8_t* ppsData;
112+
size_t ppsLen;
113+
};
114+
100115
// Translates from name of codec to codec type and vice versa.
101116
RTC_EXPORT const char* CodecTypeToPayloadString(VideoCodecType type);
102117
RTC_EXPORT VideoCodecType PayloadStringToCodecType(const std::string& name);
@@ -105,6 +120,7 @@ union VideoCodecUnion {
105120
VideoCodecVP8 VP8;
106121
VideoCodecVP9 VP9;
107122
VideoCodecH264 H264;
123+
VideoCodecH265 H265;
108124
};
109125

110126
enum class VideoCodecMode { kRealtimeVideo, kScreensharing };
@@ -193,6 +209,8 @@ class RTC_EXPORT VideoCodec {
193209
const VideoCodecVP9& VP9() const;
194210
VideoCodecH264* H264();
195211
const VideoCodecH264& H264() const;
212+
VideoCodecH265* H265();
213+
const VideoCodecH265& H265() const;
196214

197215
private:
198216
// TODO(hta): Consider replacing the union with a pointer type.

Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_decoder_software_fallback_wrapper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void VideoDecoderSoftwareFallbackWrapper::UpdateFallbackDecoderHistograms() {
167167
RTC_HISTOGRAM_COUNTS_100000(kFallbackHistogramsUmaPrefix + "H264",
168168
hw_decoded_frames_since_last_fallback_);
169169
break;
170-
#ifndef DISABLE_H265
170+
#ifdef WEBRTC_USE_H265
171171
case kVideoCodecH265:
172172
RTC_HISTOGRAM_COUNTS_100000(kFallbackHistogramsUmaPrefix + "H265",
173173
hw_decoded_frames_since_last_fallback_);

Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_encoder.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ VideoCodecH264 VideoEncoder::GetDefaultH264Settings() {
5858
return h264_settings;
5959
}
6060

61+
#ifdef WEBRTC_USE_H265
62+
VideoCodecH265 VideoEncoder::GetDefaultH265Settings() {
63+
VideoCodecH265 h265_settings;
64+
memset(&h265_settings, 0, sizeof(h265_settings));
65+
66+
// h265_settings.profile = kProfileBase;
67+
h265_settings.frameDroppingOn = true;
68+
h265_settings.keyFrameInterval = 3000;
69+
h265_settings.spsData = nullptr;
70+
h265_settings.spsLen = 0;
71+
h265_settings.ppsData = nullptr;
72+
h265_settings.ppsLen = 0;
73+
74+
return h265_settings;
75+
}
76+
#endif
77+
6178
VideoEncoder::ScalingSettings::ScalingSettings() = default;
6279

6380
VideoEncoder::ScalingSettings::ScalingSettings(KOff) : ScalingSettings() {}

Source/ThirdParty/libwebrtc/Source/webrtc/api/video_codecs/video_encoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ class RTC_EXPORT VideoEncoder {
338338
static VideoCodecVP8 GetDefaultVp8Settings();
339339
static VideoCodecVP9 GetDefaultVp9Settings();
340340
static VideoCodecH264 GetDefaultH264Settings();
341+
#ifdef WEBRTC_USE_H265
342+
static VideoCodecH265 GetDefaultH265Settings();
343+
#endif
341344

342345
virtual ~VideoEncoder() {}
343346

Source/ThirdParty/libwebrtc/Source/webrtc/call/rtp_payload_params.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
9696
info.codecSpecific.H264.packetization_mode;
9797
return;
9898
}
99-
#ifndef DISABLE_H265
99+
#ifdef WEBRTC_USE_H265
100100
case kVideoCodecH265: {
101101
auto& h265_header = rtp->video_type_header.emplace<RTPVideoHeaderH265>();
102102
h265_header.packetization_mode =
103103
info.codecSpecific.H265.packetization_mode;
104-
rtp->simulcastIdx = spatial_index.value_or(0);
105104
return;
106105
}
107106
#endif
@@ -348,7 +347,7 @@ void RtpPayloadParams::SetGeneric(const CodecSpecificInfo* codec_specific_info,
348347
is_keyframe, rtp_video_header);
349348
}
350349
return;
351-
#ifndef DISABLE_H265
350+
#ifdef WEBRTC_USE_H265
352351
case VideoCodecType::kVideoCodecH265:
353352
// FIXME: Implement H265 to generic descriptor.
354353
return;
@@ -416,7 +415,7 @@ absl::optional<FrameDependencyStructure> RtpPayloadParams::GenericStructure(
416415
}
417416
case VideoCodecType::kVideoCodecAV1:
418417
case VideoCodecType::kVideoCodecH264:
419-
#ifndef DISABLE_H265
418+
#ifdef WEBRTC_USE_H265
420419
case VideoCodecType::kVideoCodecH265:
421420
#endif
422421
case VideoCodecType::kVideoCodecMultiplex:

Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_common.cc

Lines changed: 25 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,100 +10,50 @@
1010

1111
#include "common_video/h265/h265_common.h"
1212

13+
#include "common_video/h264/h264_common.h"
14+
1315
namespace webrtc {
1416
namespace H265 {
1517

1618
const uint8_t kNaluTypeMask = 0x7E;
1719

18-
std::vector<H264::NaluIndex> FindNaluIndices(const uint8_t* buffer,
20+
std::vector<NaluIndex> FindNaluIndices(const uint8_t* buffer,
1921
size_t buffer_size) {
20-
// This is sorta like Boyer-Moore, but with only the first optimization step:
21-
// given a 3-byte sequence we're looking at, if the 3rd byte isn't 1 or 0,
22-
// skip ahead to the next 3-byte sequence. 0s and 1s are relatively rare, so
23-
// this will skip the majority of reads/checks.
24-
std::vector<H264::NaluIndex> sequences;
25-
if (buffer_size < kNaluShortStartSequenceSize)
26-
return sequences;
27-
28-
const size_t end = buffer_size - kNaluShortStartSequenceSize;
29-
for (size_t i = 0; i < end;) {
30-
if (buffer[i + 2] > 1) {
31-
i += 3;
32-
} else if (buffer[i + 2] == 1 && buffer[i + 1] == 0 && buffer[i] == 0) {
33-
// We found a start sequence, now check if it was a 3 of 4 byte one.
34-
H264::NaluIndex index = {i, i + 3, 0};
35-
if (index.start_offset > 0 && buffer[index.start_offset - 1] == 0)
36-
--index.start_offset;
37-
38-
// Update length of previous entry.
39-
auto it = sequences.rbegin();
40-
if (it != sequences.rend())
41-
it->payload_size = index.start_offset - it->payload_start_offset;
42-
43-
sequences.push_back(index);
44-
45-
i += 3;
46-
} else {
47-
++i;
48-
}
22+
std::vector<H264::NaluIndex> indices =
23+
H264::FindNaluIndices(buffer, buffer_size);
24+
std::vector<NaluIndex> results;
25+
for (auto& index : indices) {
26+
results.push_back(
27+
{index.start_offset, index.payload_start_offset, index.payload_size});
4928
}
50-
51-
// Update length of last entry, if any.
52-
auto it = sequences.rbegin();
53-
if (it != sequences.rend())
54-
it->payload_size = buffer_size - it->payload_start_offset;
55-
56-
return sequences;
29+
return results;
5730
}
5831

5932
NaluType ParseNaluType(uint8_t data) {
6033
return static_cast<NaluType>((data & kNaluTypeMask) >> 1);
6134
}
6235

6336
std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length) {
64-
std::vector<uint8_t> out;
65-
out.reserve(length);
66-
67-
for (size_t i = 0; i < length;) {
68-
// Be careful about over/underflow here. byte_length_ - 3 can underflow, and
69-
// i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_
70-
// above, and that expression will produce the number of bytes left in
71-
// the stream including the byte at i.
72-
if (length - i >= 3 && !data[i] && !data[i + 1] && data[i + 2] == 3) {
73-
// Two rbsp bytes.
74-
out.push_back(data[i++]);
75-
out.push_back(data[i++]);
76-
// Skip the emulation byte.
77-
i++;
78-
} else {
79-
// Single rbsp byte.
80-
out.push_back(data[i++]);
81-
}
82-
}
83-
return out;
37+
return H264::ParseRbsp(data, length);
8438
}
8539

8640
void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) {
87-
static const uint8_t kZerosInStartSequence = 2;
88-
static const uint8_t kEmulationByte = 0x03u;
89-
size_t num_consecutive_zeros = 0;
90-
destination->EnsureCapacity(destination->size() + length);
41+
H264::WriteRbsp(bytes, length, destination);
42+
}
9143

92-
for (size_t i = 0; i < length; ++i) {
93-
uint8_t byte = bytes[i];
94-
if (byte <= kEmulationByte &&
95-
num_consecutive_zeros >= kZerosInStartSequence) {
96-
// Need to escape.
97-
destination->AppendData(kEmulationByte);
98-
num_consecutive_zeros = 0;
99-
}
100-
destination->AppendData(byte);
101-
if (byte == 0) {
102-
++num_consecutive_zeros;
103-
} else {
104-
num_consecutive_zeros = 0;
105-
}
44+
uint32_t Log2(uint32_t value) {
45+
uint32_t result = 0;
46+
// If value is not a power of two an additional bit is required
47+
// to account for the ceil() of log2() below.
48+
if ((value & (value - 1)) != 0) {
49+
++result;
50+
}
51+
while (value > 0) {
52+
value >>= 1;
53+
++result;
10654
}
55+
56+
return result;
10757
}
10858

10959
} // namespace H265

Source/ThirdParty/libwebrtc/Source/webrtc/common_video/h265/h265_common.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <memory>
1515
#include <vector>
1616

17-
#include "common_video/h264/h264_common.h"
1817
#include "rtc_base/buffer.h"
1918

2019
namespace webrtc {
@@ -28,8 +27,8 @@ const size_t kNaluLongStartSequenceSize = 4;
2827
// not the first NALU of an access unit or an SPS or PPS block.
2928
const size_t kNaluShortStartSequenceSize = 3;
3029

31-
// The size of the NALU type byte (1).
32-
const size_t kNaluTypeSize = 1;
30+
// The size of the NALU type byte (2).
31+
const size_t kNaluTypeSize = 2;
3332

3433
enum NaluType : uint8_t {
3534
kTrailN = 0,
@@ -57,10 +56,19 @@ enum NaluType : uint8_t {
5756
kFU = 49
5857
};
5958

60-
enum SliceType : uint8_t { kP = 0, kB = 1, kI = 2, kSp = 3, kSi = 4 };
59+
enum SliceType : uint8_t { kB = 0, kP = 1, kI = 2 };
60+
61+
struct NaluIndex {
62+
// Start index of NALU, including start sequence.
63+
size_t start_offset;
64+
// Start index of NALU payload, typically type header.
65+
size_t payload_start_offset;
66+
// Length of NALU payload, in bytes, counting from payload_start_offset.
67+
size_t payload_size;
68+
};
6169

6270
// Returns a vector of the NALU indices in the given buffer.
63-
std::vector<H264::NaluIndex> FindNaluIndices(const uint8_t* buffer,
71+
std::vector<NaluIndex> FindNaluIndices(const uint8_t* buffer,
6472
size_t buffer_size);
6573

6674
// Get the NAL type from the header byte immediately following start sequence.
@@ -86,6 +94,8 @@ std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length);
8694
// bytes in order to escape any data the could be interpreted as a start
8795
// sequence.
8896
void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination);
97+
98+
uint32_t Log2(uint32_t value);
8999
} // namespace H265
90100
} // namespace webrtc
91101

0 commit comments

Comments
 (0)