blob: 6a692fbc57f9182b6595803aa18a6e7117cb2456 [file] [log] [blame]
Zeke Chin71f6f442015-06-29 14:34:58 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020012#include "modules/video_coding/codecs/h264/include/h264.h"
Zeke Chin71f6f442015-06-29 14:34:58 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <string>
15
Steve Anton40d55332019-01-07 10:21:47 -080016#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "absl/types/optional.h"
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020018#include "api/video_codecs/sdp_video_format.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "common_types.h" // NOLINT(build/include)
Magnus Jedvert8deb8182017-10-05 13:13:32 +020020#include "media/base/h264_profile_level_id.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "media/base/media_constants.h"
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020022
hbos9dc59282016-02-03 05:09:37 -080023#if defined(WEBRTC_USE_H264)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/video_coding/codecs/h264/h264_decoder_impl.h"
25#include "modules/video_coding/codecs/h264/h264_encoder_impl.h"
hbosbab934b2016-01-27 01:36:03 -080026#endif
Zeke Chin71f6f442015-06-29 14:34:58 -070027
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
29#include "rtc_base/logging.h"
Zeke Chin71f6f442015-06-29 14:34:58 -070030
31namespace webrtc {
32
hbos9dc59282016-02-03 05:09:37 -080033namespace {
34
35#if defined(WEBRTC_USE_H264)
36bool g_rtc_use_h264 = true;
37#endif
38
Magnus Jedvert8deb8182017-10-05 13:13:32 +020039// If H.264 OpenH264/FFmpeg codec is supported.
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020040bool IsH264CodecSupported() {
41#if defined(WEBRTC_USE_H264)
42 return g_rtc_use_h264;
43#else
44 return false;
45#endif
46}
47
Taylor Brandstetter28deb902018-05-30 14:56:50 -070048SdpVideoFormat CreateH264Format(H264::Profile profile,
49 H264::Level level,
50 const std::string& packetization_mode) {
Danil Chapovalov0040b662018-06-18 10:48:16 +020051 const absl::optional<std::string> profile_string =
Magnus Jedvert8deb8182017-10-05 13:13:32 +020052 H264::ProfileLevelIdToString(H264::ProfileLevelId(profile, level));
53 RTC_CHECK(profile_string);
Taylor Brandstetter28deb902018-05-30 14:56:50 -070054 return SdpVideoFormat(
55 cricket::kH264CodecName,
56 {{cricket::kH264FmtpProfileLevelId, *profile_string},
57 {cricket::kH264FmtpLevelAsymmetryAllowed, "1"},
58 {cricket::kH264FmtpPacketizationMode, packetization_mode}});
Magnus Jedvert8deb8182017-10-05 13:13:32 +020059}
60
hbos9dc59282016-02-03 05:09:37 -080061} // namespace
62
63void DisableRtcUseH264() {
64#if defined(WEBRTC_USE_H264)
65 g_rtc_use_h264 = false;
66#endif
67}
68
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020069std::vector<SdpVideoFormat> SupportedH264Codecs() {
70 if (!IsH264CodecSupported())
71 return std::vector<SdpVideoFormat>();
Magnus Jedvert8deb8182017-10-05 13:13:32 +020072 // 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 Brandstetter28deb902018-05-30 14:56:50 -070078 //
79 // We support both packetization modes 0 (mandatory) and 1 (optional,
80 // preferred).
81 return {
82 CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1, "1"),
83 CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1, "0"),
84 CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1, "1"),
85 CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1,
86 "0")};
Zeke Chin71f6f442015-06-29 14:34:58 -070087}
88
Magnus Jedvert46a27652017-11-13 14:10:02 +010089std::unique_ptr<H264Encoder> H264Encoder::Create(
90 const cricket::VideoCodec& codec) {
henrikg91d6ede2015-09-17 00:24:34 -070091 RTC_DCHECK(H264Encoder::IsSupported());
hbos9dc59282016-02-03 05:09:37 -080092#if defined(WEBRTC_USE_H264)
93 RTC_CHECK(g_rtc_use_h264);
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_INFO) << "Creating H264EncoderImpl.";
Karl Wiberg918f50c2018-07-05 11:40:33 +020095 return absl::make_unique<H264EncoderImpl>(codec);
Zeke Chin71f6f442015-06-29 14:34:58 -070096#else
97 RTC_NOTREACHED();
98 return nullptr;
99#endif
100}
101
102bool H264Encoder::IsSupported() {
103 return IsH264CodecSupported();
104}
105
Magnus Jedvert46a27652017-11-13 14:10:02 +0100106std::unique_ptr<H264Decoder> H264Decoder::Create() {
henrikg91d6ede2015-09-17 00:24:34 -0700107 RTC_DCHECK(H264Decoder::IsSupported());
hbos9dc59282016-02-03 05:09:37 -0800108#if defined(WEBRTC_USE_H264)
109 RTC_CHECK(g_rtc_use_h264);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100110 RTC_LOG(LS_INFO) << "Creating H264DecoderImpl.";
Karl Wiberg918f50c2018-07-05 11:40:33 +0200111 return absl::make_unique<H264DecoderImpl>();
Zeke Chin71f6f442015-06-29 14:34:58 -0700112#else
113 RTC_NOTREACHED();
114 return nullptr;
115#endif
116}
117
118bool H264Decoder::IsSupported() {
119 return IsH264CodecSupported();
120}
121
122} // namespace webrtc