xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/h264/h264.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  */
11 
12 #include "modules/video_coding/codecs/h264/include/h264.h"
13 
14 #include <memory>
15 #include <string>
16 
17 #include "absl/container/inlined_vector.h"
18 #include "absl/types/optional.h"
19 #include "api/video_codecs/sdp_video_format.h"
20 #include "media/base/media_constants.h"
21 #include "rtc_base/trace_event.h"
22 
23 #if defined(WEBRTC_USE_H264)
24 #include "modules/video_coding/codecs/h264/h264_decoder_impl.h"
25 #include "modules/video_coding/codecs/h264/h264_encoder_impl.h"
26 #endif
27 
28 #include "rtc_base/checks.h"
29 #include "rtc_base/logging.h"
30 
31 namespace webrtc {
32 
33 namespace {
34 
35 #if defined(WEBRTC_USE_H264)
36 bool g_rtc_use_h264 = true;
37 #endif
38 
39 // If H.264 OpenH264/FFmpeg codec is supported.
IsH264CodecSupported()40 bool IsH264CodecSupported() {
41 #if defined(WEBRTC_USE_H264)
42   return g_rtc_use_h264;
43 #else
44   return false;
45 #endif
46 }
47 
48 constexpr ScalabilityMode kSupportedScalabilityModes[] = {
49     ScalabilityMode::kL1T1, ScalabilityMode::kL1T2, ScalabilityMode::kL1T3};
50 
51 }  // namespace
52 
CreateH264Format(H264Profile profile,H264Level level,const std::string & packetization_mode,bool add_scalability_modes)53 SdpVideoFormat CreateH264Format(H264Profile profile,
54                                 H264Level level,
55                                 const std::string& packetization_mode,
56                                 bool add_scalability_modes) {
57   const absl::optional<std::string> profile_string =
58       H264ProfileLevelIdToString(H264ProfileLevelId(profile, level));
59   RTC_CHECK(profile_string);
60   absl::InlinedVector<ScalabilityMode, kScalabilityModeCount> scalability_modes;
61   if (add_scalability_modes) {
62     for (const auto scalability_mode : kSupportedScalabilityModes) {
63       scalability_modes.push_back(scalability_mode);
64     }
65   }
66   return SdpVideoFormat(
67       cricket::kH264CodecName,
68       {{cricket::kH264FmtpProfileLevelId, *profile_string},
69        {cricket::kH264FmtpLevelAsymmetryAllowed, "1"},
70        {cricket::kH264FmtpPacketizationMode, packetization_mode}},
71       scalability_modes);
72 }
73 
DisableRtcUseH264()74 void DisableRtcUseH264() {
75 #if defined(WEBRTC_USE_H264)
76   g_rtc_use_h264 = false;
77 #endif
78 }
79 
SupportedH264Codecs(bool add_scalability_modes)80 std::vector<SdpVideoFormat> SupportedH264Codecs(bool add_scalability_modes) {
81   TRACE_EVENT0("webrtc", __func__);
82   if (!IsH264CodecSupported())
83     return std::vector<SdpVideoFormat>();
84   // We only support encoding Constrained Baseline Profile (CBP), but the
85   // decoder supports more profiles. We can list all profiles here that are
86   // supported by the decoder and that are also supersets of CBP, i.e. the
87   // decoder for that profile is required to be able to decode CBP. This means
88   // we can encode and send CBP even though we negotiated a potentially
89   // higher profile. See the H264 spec for more information.
90   //
91   // We support both packetization modes 0 (mandatory) and 1 (optional,
92   // preferred).
93   return {CreateH264Format(H264Profile::kProfileBaseline, H264Level::kLevel3_1,
94                            "1", add_scalability_modes),
95           CreateH264Format(H264Profile::kProfileBaseline, H264Level::kLevel3_1,
96                            "0", add_scalability_modes),
97           CreateH264Format(H264Profile::kProfileConstrainedBaseline,
98                            H264Level::kLevel3_1, "1", add_scalability_modes),
99           CreateH264Format(H264Profile::kProfileConstrainedBaseline,
100                            H264Level::kLevel3_1, "0", add_scalability_modes),
101           CreateH264Format(H264Profile::kProfileMain, H264Level::kLevel3_1, "1",
102                            add_scalability_modes),
103           CreateH264Format(H264Profile::kProfileMain, H264Level::kLevel3_1, "0",
104                            add_scalability_modes)};
105 }
106 
SupportedH264DecoderCodecs()107 std::vector<SdpVideoFormat> SupportedH264DecoderCodecs() {
108   TRACE_EVENT0("webrtc", __func__);
109   if (!IsH264CodecSupported())
110     return std::vector<SdpVideoFormat>();
111 
112   std::vector<SdpVideoFormat> supportedCodecs = SupportedH264Codecs();
113 
114   // OpenH264 doesn't yet support High Predictive 4:4:4 encoding but it does
115   // support decoding.
116   supportedCodecs.push_back(CreateH264Format(
117       H264Profile::kProfilePredictiveHigh444, H264Level::kLevel3_1, "1"));
118   supportedCodecs.push_back(CreateH264Format(
119       H264Profile::kProfilePredictiveHigh444, H264Level::kLevel3_1, "0"));
120 
121   return supportedCodecs;
122 }
123 
Create(const cricket::VideoCodec & codec)124 std::unique_ptr<H264Encoder> H264Encoder::Create(
125     const cricket::VideoCodec& codec) {
126   RTC_DCHECK(H264Encoder::IsSupported());
127 #if defined(WEBRTC_USE_H264)
128   RTC_CHECK(g_rtc_use_h264);
129   RTC_LOG(LS_INFO) << "Creating H264EncoderImpl.";
130   return std::make_unique<H264EncoderImpl>(codec);
131 #else
132   RTC_DCHECK_NOTREACHED();
133   return nullptr;
134 #endif
135 }
136 
IsSupported()137 bool H264Encoder::IsSupported() {
138   return IsH264CodecSupported();
139 }
140 
SupportsScalabilityMode(ScalabilityMode scalability_mode)141 bool H264Encoder::SupportsScalabilityMode(ScalabilityMode scalability_mode) {
142   for (const auto& entry : kSupportedScalabilityModes) {
143     if (entry == scalability_mode) {
144       return true;
145     }
146   }
147   return false;
148 }
149 
Create()150 std::unique_ptr<H264Decoder> H264Decoder::Create() {
151   RTC_DCHECK(H264Decoder::IsSupported());
152 #if defined(WEBRTC_USE_H264)
153   RTC_CHECK(g_rtc_use_h264);
154   RTC_LOG(LS_INFO) << "Creating H264DecoderImpl.";
155   return std::make_unique<H264DecoderImpl>();
156 #else
157   RTC_DCHECK_NOTREACHED();
158   return nullptr;
159 #endif
160 }
161 
IsSupported()162 bool H264Decoder::IsSupported() {
163   return IsH264CodecSupported();
164 }
165 
166 }  // namespace webrtc
167