Use subset profile matching for H.264 decoder support Bug: webrtc:347724928 Change-Id: Ia71b907b7f5af1e3acb9ed4959bd52378e80ccb9 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/432780 Reviewed-by: Sergey Silkin <ssilkin@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Commit-Queue: Sergey Silkin <ssilkin@webrtc.org> Cr-Commit-Position: refs/heads/main@{#47253}
diff --git a/api/video_codecs/h264_profile_level_id.cc b/api/video_codecs/h264_profile_level_id.cc index fc8555a..ad30598 100644 --- a/api/video_codecs/h264_profile_level_id.cc +++ b/api/video_codecs/h264_profile_level_id.cc
@@ -320,4 +320,46 @@ profile_level_id->level == other_profile_level_id->level; } +bool H264IsProfileSubsetOf(const CodecParameterMap& subset, + const CodecParameterMap& superset) { + const std::optional<H264ProfileLevelId> subset_profile_level_id = + ParseSdpForH264ProfileLevelId(subset); + const std::optional<H264ProfileLevelId> superset_profile_level_id = + ParseSdpForH264ProfileLevelId(superset); + + if (!subset_profile_level_id || !superset_profile_level_id) { + return false; + } + + const H264Profile subset_profile = subset_profile_level_id->profile; + const H264Profile superset_profile = superset_profile_level_id->profile; + + if (subset_profile == superset_profile) { + return true; + } + + // Evaluates whether the `subset` profile is a valid subset of the `superset` + // profile according to ITU-T H.264 Annex A.2. + switch (superset_profile) { + case H264Profile::kProfileConstrainedBaseline: + return false; + case H264Profile::kProfileBaseline: + case H264Profile::kProfileMain: + return subset_profile == H264Profile::kProfileConstrainedBaseline; + case H264Profile::kProfileConstrainedHigh: + return subset_profile == H264Profile::kProfileConstrainedBaseline; + case H264Profile::kProfileHigh: + return subset_profile == H264Profile::kProfileConstrainedBaseline || + subset_profile == H264Profile::kProfileMain || + subset_profile == H264Profile::kProfileConstrainedHigh; + case H264Profile::kProfilePredictiveHigh444: + return subset_profile == H264Profile::kProfileConstrainedBaseline || + subset_profile == H264Profile::kProfileMain || + subset_profile == H264Profile::kProfileConstrainedHigh || + subset_profile == H264Profile::kProfileHigh; + default: + return false; + } +} + } // namespace webrtc
diff --git a/api/video_codecs/h264_profile_level_id.h b/api/video_codecs/h264_profile_level_id.h index 1b21f18..8e77c55 100644 --- a/api/video_codecs/h264_profile_level_id.h +++ b/api/video_codecs/h264_profile_level_id.h
@@ -91,6 +91,11 @@ RTC_EXPORT bool H264IsSameProfileAndLevel(const CodecParameterMap& params1, const CodecParameterMap& params2); +// Returns true if the profile defined in `subset` is a valid subset of the +// profile defined in `superset` according to ITU-T H.264 Annex A.2. +RTC_EXPORT bool H264IsProfileSubsetOf(const CodecParameterMap& subset, + const CodecParameterMap& superset); + } // namespace webrtc #endif // API_VIDEO_CODECS_H264_PROFILE_LEVEL_ID_H_
diff --git a/api/video_codecs/test/h264_profile_level_id_unittest.cc b/api/video_codecs/test/h264_profile_level_id_unittest.cc index 044765b..68768f4 100644 --- a/api/video_codecs/test/h264_profile_level_id_unittest.cc +++ b/api/video_codecs/test/h264_profile_level_id_unittest.cc
@@ -168,4 +168,57 @@ EXPECT_FALSE(ParseSdpForH264ProfileLevelId(params)); } +TEST(H264ProfileLevelId, TestIsProfileSubsetOf) { + CodecParameterMap params_cb; + params_cb["profile-level-id"] = "42e01f"; + CodecParameterMap params_baseline; + params_baseline["profile-level-id"] = "42001f"; + CodecParameterMap params_main; + params_main["profile-level-id"] = "4d001f"; + CodecParameterMap params_constrained_high; + params_constrained_high["profile-level-id"] = "640c1f"; + CodecParameterMap params_high; + params_high["profile-level-id"] = "64001f"; + CodecParameterMap params_high444; + params_high444["profile-level-id"] = "f4001f"; + + // Same profile. + EXPECT_TRUE(H264IsProfileSubsetOf(params_cb, params_cb)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_baseline, params_baseline)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_main, params_main)); + + // Constrained Baseline is a subset of almost everything. + EXPECT_TRUE(H264IsProfileSubsetOf(params_cb, params_baseline)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_cb, params_main)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_cb, params_constrained_high)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_cb, params_high)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_cb, params_high444)); + + // Main is a subset of High and High 4:4:4 profiles. + EXPECT_TRUE(H264IsProfileSubsetOf(params_main, params_high)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_main, params_high444)); + + // Constrained High is a subset of High and High 4:4:4. + EXPECT_TRUE(H264IsProfileSubsetOf(params_constrained_high, params_high)); + EXPECT_TRUE(H264IsProfileSubsetOf(params_constrained_high, params_high444)); + + // High is a subset of High 4:4:4. + EXPECT_TRUE(H264IsProfileSubsetOf(params_high, params_high444)); + + // Standard Baseline is not a subset of Main/High. + EXPECT_FALSE(H264IsProfileSubsetOf(params_baseline, params_main)); + EXPECT_FALSE(H264IsProfileSubsetOf(params_baseline, params_constrained_high)); + EXPECT_FALSE(H264IsProfileSubsetOf(params_baseline, params_high)); + EXPECT_FALSE(H264IsProfileSubsetOf(params_baseline, params_high444)); + + // Main allows interlaced video, Constrained High does not. + EXPECT_FALSE(H264IsProfileSubsetOf(params_main, params_constrained_high)); + + // Higher profiles are not subsets of lower profiles. + EXPECT_FALSE(H264IsProfileSubsetOf(params_baseline, params_cb)); + EXPECT_FALSE(H264IsProfileSubsetOf(params_main, params_cb)); + EXPECT_FALSE(H264IsProfileSubsetOf(params_high, params_main)); + EXPECT_FALSE(H264IsProfileSubsetOf(params_high444, params_high)); +} + } // namespace webrtc
diff --git a/media/base/codec_comparators.cc b/media/base/codec_comparators.cc index cb67f67..942e3a2 100644 --- a/media/base/codec_comparators.cc +++ b/media/base/codec_comparators.cc
@@ -59,12 +59,6 @@ return GetFmtpParameterOrDefault(params, kH264FmtpPacketizationMode, "0"); } -bool H264IsSamePacketizationMode(const CodecParameterMap& left, - const CodecParameterMap& right) { - return H264GetPacketizationModeOrDefault(left) == - H264GetPacketizationModeOrDefault(right); -} - std::string AV1GetTierOrDefault(const CodecParameterMap& params) { // If the parameter is not present, the tier MUST be inferred to be 0. // https://aomediacodec.github.io/av1-rtp-spec/#72-sdp-parameters @@ -104,7 +98,7 @@ }; if (either_name_matches(kH264CodecName)) return H264IsSameProfile(params1, params2) && - H264IsSamePacketizationMode(params1, params2); + IsSameH264PacketizationMode(params1, params2); if (either_name_matches(kVp9CodecName)) return VP9IsSameProfile(params1, params2); // https://aomediacodec.github.io/av1-rtp-spec/#723-usage-with-the-sdp-offeranswer-model @@ -417,4 +411,10 @@ return params1 == params2; } +bool IsSameH264PacketizationMode(const CodecParameterMap& left, + const CodecParameterMap& right) { + return H264GetPacketizationModeOrDefault(left) == + H264GetPacketizationModeOrDefault(right); +} + } // namespace webrtc
diff --git a/media/base/codec_comparators.h b/media/base/codec_comparators.h index 1d242b1..bf14359 100644 --- a/media/base/codec_comparators.h +++ b/media/base/codec_comparators.h
@@ -44,6 +44,11 @@ // Similar to `IsSameRtpCodec` but ignoring the level related parameter. bool IsSameRtpCodecIgnoringLevel(const Codec& codec, const RtpCodec& rtp_codec); + +// Returns true if the two codecs have the same H.264 packetization-mode. +bool IsSameH264PacketizationMode(const CodecParameterMap& left, + const CodecParameterMap& right); + } // namespace webrtc #endif // MEDIA_BASE_CODEC_COMPARATORS_H_
diff --git a/media/engine/internal_decoder_factory.cc b/media/engine/internal_decoder_factory.cc index f81c0f5..ed05e71 100644 --- a/media/engine/internal_decoder_factory.cc +++ b/media/engine/internal_decoder_factory.cc
@@ -16,10 +16,12 @@ #include "absl/strings/match.h" #include "api/environment/environment.h" #include "api/video/video_codec_type.h" +#include "api/video_codecs/h264_profile_level_id.h" #include "api/video_codecs/sdp_video_format.h" #include "api/video_codecs/video_codec.h" #include "api/video_codecs/video_decoder.h" #include "api/video_codecs/video_decoder_factory.h" +#include "media/base/codec_comparators.h" #include "media/base/media_constants.h" #include "modules/video_coding/codecs/h264/include/h264.h" #include "modules/video_coding/codecs/vp8/include/vp8.h" @@ -75,14 +77,28 @@ } CodecSupport codec_support; - codec_support.is_supported = format.IsCodecInList(GetSupportedFormats()); + const std::vector<SdpVideoFormat>& supported_formats = GetSupportedFormats(); + // For H.264, stream profile can be subset of supported profile. + if (absl::EqualsIgnoreCase(format.name, kH264CodecName)) { + for (const SdpVideoFormat& supported : supported_formats) { + if (absl::EqualsIgnoreCase(supported.name, kH264CodecName) && + IsSameH264PacketizationMode(format.parameters, + supported.parameters) && + H264IsProfileSubsetOf(format.parameters, supported.parameters)) { + codec_support.is_supported = true; + break; + } + } + } else { + codec_support.is_supported = format.IsCodecInList(supported_formats); + } return codec_support; } std::unique_ptr<VideoDecoder> InternalDecoderFactory::Create( const Environment& env, const SdpVideoFormat& format) { - if (!format.IsCodecInList(GetSupportedFormats())) { + if (!QueryCodecSupport(format, false).is_supported) { RTC_LOG(LS_WARNING) << "Trying to create decoder for unsupported format. " << format.ToString(); return nullptr;
diff --git a/media/engine/internal_decoder_factory_unittest.cc b/media/engine/internal_decoder_factory_unittest.cc index f72110c..b10cc47 100644 --- a/media/engine/internal_decoder_factory_unittest.cc +++ b/media/engine/internal_decoder_factory_unittest.cc
@@ -149,6 +149,47 @@ #endif } +TEST(InternalDecoderFactoryTest, QueryCodecSupportH264Profiles) { + InternalDecoderFactory factory; + auto h264_support = kH264Enabled ? kSupported : kUnsupported; + + // The internal decoder factory explicitly lists Baseline, Constrained + // Baseline, Main, and High Predictive 4:4:4. Through profile subset matching, + // it should also support High and Constrained High profiles because they are + // subsets of High Predictive 4:4:4. + EXPECT_THAT( + factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "42e01f"}}), false), + Support(h264_support)); + EXPECT_THAT( + factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "42001f"}}), false), + Support(h264_support)); + EXPECT_THAT( + factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "4d001f"}}), false), + Support(h264_support)); + EXPECT_THAT( + factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "640c1f"}}), false), + Support(h264_support)); + EXPECT_THAT( + factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "64001f"}}), false), + Support(h264_support)); + + EXPECT_THAT( + factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "ff0000"}}), false), + Support(kUnsupported)); + + EXPECT_THAT(factory.QueryCodecSupport( + SdpVideoFormat("H264", {{"profile-level-id", "42e01f"}, + {"packetization-mode", "2"}}), + false), + Support(kUnsupported)); +} + TEST(InternalDecoderFactoryTest, QueryCodecSupportReferenceScaling) { InternalDecoderFactory factory; // VP9 and AV1 support for spatial layers.