Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "modules/video_coding/codecs/h264/include/h264.h" |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 13 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 14 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <string> |
| 16 | |
| 17 | #include "absl/types/optional.h" |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 18 | #include "api/video_codecs/sdp_video_format.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "media/base/media_constants.h" |
Markus Handell | fccb052 | 2021-06-02 13:28:10 +0200 | [diff] [blame] | 20 | #include "rtc_base/trace_event.h" |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 21 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 22 | #if defined(WEBRTC_USE_H264) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/video_coding/codecs/h264/h264_decoder_impl.h" |
| 24 | #include "modules/video_coding/codecs/h264/h264_encoder_impl.h" |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 25 | #endif |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 26 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
| 28 | #include "rtc_base/logging.h" |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | #if defined(WEBRTC_USE_H264) |
| 35 | bool g_rtc_use_h264 = true; |
| 36 | #endif |
| 37 | |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 38 | // If H.264 OpenH264/FFmpeg codec is supported. |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 39 | bool IsH264CodecSupported() { |
| 40 | #if defined(WEBRTC_USE_H264) |
| 41 | return g_rtc_use_h264; |
| 42 | #else |
| 43 | return false; |
| 44 | #endif |
| 45 | } |
| 46 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 47 | } // namespace |
| 48 | |
Johannes Kron | c3fcee7 | 2021-04-19 09:09:26 +0200 | [diff] [blame] | 49 | SdpVideoFormat CreateH264Format(H264Profile profile, |
| 50 | H264Level level, |
Taylor Brandstetter | 28deb90 | 2018-05-30 14:56:50 -0700 | [diff] [blame] | 51 | const std::string& packetization_mode) { |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 52 | const absl::optional<std::string> profile_string = |
Johannes Kron | c3fcee7 | 2021-04-19 09:09:26 +0200 | [diff] [blame] | 53 | H264ProfileLevelIdToString(H264ProfileLevelId(profile, level)); |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 54 | RTC_CHECK(profile_string); |
Taylor Brandstetter | 28deb90 | 2018-05-30 14:56:50 -0700 | [diff] [blame] | 55 | return SdpVideoFormat( |
| 56 | cricket::kH264CodecName, |
| 57 | {{cricket::kH264FmtpProfileLevelId, *profile_string}, |
| 58 | {cricket::kH264FmtpLevelAsymmetryAllowed, "1"}, |
| 59 | {cricket::kH264FmtpPacketizationMode, packetization_mode}}); |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 60 | } |
| 61 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 62 | void DisableRtcUseH264() { |
| 63 | #if defined(WEBRTC_USE_H264) |
| 64 | g_rtc_use_h264 = false; |
| 65 | #endif |
| 66 | } |
| 67 | |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 68 | std::vector<SdpVideoFormat> SupportedH264Codecs() { |
Markus Handell | fccb052 | 2021-06-02 13:28:10 +0200 | [diff] [blame] | 69 | TRACE_EVENT0("webrtc", __func__); |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 70 | if (!IsH264CodecSupported()) |
| 71 | return std::vector<SdpVideoFormat>(); |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 72 | // We only support encoding Constrained Baseline Profile (CBP), but the |
| 73 | // decoder supports more profiles. We can list all profiles here that are |
| 74 | // supported by the decoder and that are also supersets of CBP, i.e. the |
| 75 | // decoder for that profile is required to be able to decode CBP. This means |
| 76 | // we can encode and send CBP even though we negotiated a potentially |
| 77 | // higher profile. See the H264 spec for more information. |
Taylor Brandstetter | 28deb90 | 2018-05-30 14:56:50 -0700 | [diff] [blame] | 78 | // |
| 79 | // We support both packetization modes 0 (mandatory) and 1 (optional, |
| 80 | // preferred). |
Johannes Kron | c3fcee7 | 2021-04-19 09:09:26 +0200 | [diff] [blame] | 81 | return {CreateH264Format(H264Profile::kProfileBaseline, H264Level::kLevel3_1, |
| 82 | "1"), |
| 83 | CreateH264Format(H264Profile::kProfileBaseline, H264Level::kLevel3_1, |
| 84 | "0"), |
| 85 | CreateH264Format(H264Profile::kProfileConstrainedBaseline, |
| 86 | H264Level::kLevel3_1, "1"), |
| 87 | CreateH264Format(H264Profile::kProfileConstrainedBaseline, |
| 88 | H264Level::kLevel3_1, "0")}; |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 91 | std::unique_ptr<H264Encoder> H264Encoder::Create( |
| 92 | const cricket::VideoCodec& codec) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 93 | RTC_DCHECK(H264Encoder::IsSupported()); |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 94 | #if defined(WEBRTC_USE_H264) |
| 95 | RTC_CHECK(g_rtc_use_h264); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 96 | RTC_LOG(LS_INFO) << "Creating H264EncoderImpl."; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 97 | return std::make_unique<H264EncoderImpl>(codec); |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 98 | #else |
| 99 | RTC_NOTREACHED(); |
| 100 | return nullptr; |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | bool H264Encoder::IsSupported() { |
| 105 | return IsH264CodecSupported(); |
| 106 | } |
| 107 | |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 108 | std::unique_ptr<H264Decoder> H264Decoder::Create() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 109 | RTC_DCHECK(H264Decoder::IsSupported()); |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 110 | #if defined(WEBRTC_USE_H264) |
| 111 | RTC_CHECK(g_rtc_use_h264); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG(LS_INFO) << "Creating H264DecoderImpl."; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 113 | return std::make_unique<H264DecoderImpl>(); |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 114 | #else |
| 115 | RTC_NOTREACHED(); |
| 116 | return nullptr; |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | bool H264Decoder::IsSupported() { |
| 121 | return IsH264CodecSupported(); |
| 122 | } |
| 123 | |
| 124 | } // namespace webrtc |