blob: 60cfab08f34d1bc66c234ee4dc92e786713b5cf3 [file] [log] [blame]
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:22 +00001/*
2 * Copyright (c) 2012 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
Niels Möllera46bd4b2018-06-08 14:03:44 +020011#include "api/video_codecs/video_codec.h"
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:22 +000012
13#include <string.h>
eladalond0244c22017-06-08 04:19:13 -070014#include <algorithm>
danilchapef8d7732017-04-19 02:59:48 -070015#include <limits>
Niels Möllera46bd4b2018-06-08 14:03:44 +020016#include <string>
danilchapef8d7732017-04-19 02:59:48 -070017#include <type_traits>
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:22 +000018
Niels Möller3c7d5992018-10-19 15:29:54 +020019#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Erik Språngbb60a3a2018-03-19 18:25:10 +010021#include "rtc_base/strings/string_builder.h"
magjed10165ab2016-11-22 10:16:57 -080022
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:22 +000023namespace webrtc {
24
Niels Möllerdef1ef52018-03-19 13:48:44 +010025bool VideoCodecVP8::operator==(const VideoCodecVP8& other) const {
Niels Möllerdef1ef52018-03-19 13:48:44 +010026 return (complexity == other.complexity &&
Niels Möllerdef1ef52018-03-19 13:48:44 +010027 numberOfTemporalLayers == other.numberOfTemporalLayers &&
28 denoisingOn == other.denoisingOn &&
29 automaticResizeOn == other.automaticResizeOn &&
30 frameDroppingOn == other.frameDroppingOn &&
31 keyFrameInterval == other.keyFrameInterval);
32}
33
34bool VideoCodecVP9::operator==(const VideoCodecVP9& other) const {
35 return (complexity == other.complexity &&
Niels Möllerdef1ef52018-03-19 13:48:44 +010036 numberOfTemporalLayers == other.numberOfTemporalLayers &&
37 denoisingOn == other.denoisingOn &&
38 frameDroppingOn == other.frameDroppingOn &&
39 keyFrameInterval == other.keyFrameInterval &&
40 adaptiveQpMode == other.adaptiveQpMode &&
41 automaticResizeOn == other.automaticResizeOn &&
42 numberOfSpatialLayers == other.numberOfSpatialLayers &&
43 flexibleMode == other.flexibleMode);
44}
45
46bool VideoCodecH264::operator==(const VideoCodecH264& other) const {
47 return (frameDroppingOn == other.frameDroppingOn &&
48 keyFrameInterval == other.keyFrameInterval &&
Yves Gerey665174f2018-06-19 15:03:05 +020049 spsLen == other.spsLen && ppsLen == other.ppsLen &&
Niels Möllerdef1ef52018-03-19 13:48:44 +010050 profile == other.profile &&
51 (spsLen == 0 || memcmp(spsData, other.spsData, spsLen) == 0) &&
52 (ppsLen == 0 || memcmp(ppsData, other.ppsData, ppsLen) == 0));
53}
54
55bool SpatialLayer::operator==(const SpatialLayer& other) const {
Yves Gerey665174f2018-06-19 15:03:05 +020056 return (width == other.width && height == other.height &&
Niels Möllerdef1ef52018-03-19 13:48:44 +010057 numberOfTemporalLayers == other.numberOfTemporalLayers &&
58 maxBitrate == other.maxBitrate &&
59 targetBitrate == other.targetBitrate &&
Yves Gerey665174f2018-06-19 15:03:05 +020060 minBitrate == other.minBitrate && qpMax == other.qpMax &&
Niels Möllerdef1ef52018-03-19 13:48:44 +010061 active == other.active);
62}
63
hta257dc392016-10-25 09:05:06 -070064VideoCodec::VideoCodec()
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +020065 : codecType(kVideoCodecGeneric),
hta257dc392016-10-25 09:05:06 -070066 plType(0),
67 width(0),
68 height(0),
69 startBitrate(0),
70 maxBitrate(0),
71 minBitrate(0),
72 targetBitrate(0),
73 maxFramerate(0),
Seth Hampsonf6464c92018-01-17 13:55:14 -080074 active(true),
hta257dc392016-10-25 09:05:06 -070075 qpMax(0),
76 numberOfSimulcastStreams(0),
77 simulcastStream(),
78 spatialLayers(),
Niels Möllere3cf3d02018-06-13 11:52:16 +020079 mode(VideoCodecMode::kRealtimeVideo),
Erik Språng08127a92016-11-16 16:41:30 +010080 expect_encode_from_texture(false),
ilnik04f4d122017-06-19 07:18:55 -070081 timing_frame_thresholds({0, 0}),
hta527d3472016-11-16 23:23:04 -080082 codec_specific_() {}
hta257dc392016-10-25 09:05:06 -070083
84VideoCodecVP8* VideoCodec::VP8() {
85 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-16 23:23:04 -080086 return &codec_specific_.VP8;
hta257dc392016-10-25 09:05:06 -070087}
88
89const VideoCodecVP8& VideoCodec::VP8() const {
90 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-16 23:23:04 -080091 return codec_specific_.VP8;
hta257dc392016-10-25 09:05:06 -070092}
93
94VideoCodecVP9* VideoCodec::VP9() {
95 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-16 23:23:04 -080096 return &codec_specific_.VP9;
hta257dc392016-10-25 09:05:06 -070097}
98
99const VideoCodecVP9& VideoCodec::VP9() const {
100 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-16 23:23:04 -0800101 return codec_specific_.VP9;
hta257dc392016-10-25 09:05:06 -0700102}
103
104VideoCodecH264* VideoCodec::H264() {
105 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-16 23:23:04 -0800106 return &codec_specific_.H264;
hta257dc392016-10-25 09:05:06 -0700107}
108
109const VideoCodecH264& VideoCodec::H264() const {
110 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-16 23:23:04 -0800111 return codec_specific_.H264;
hta257dc392016-10-25 09:05:06 -0700112}
113
Erik Språng08127a92016-11-16 16:41:30 +0100114static const char* kPayloadNameVp8 = "VP8";
115static const char* kPayloadNameVp9 = "VP9";
116static const char* kPayloadNameH264 = "H264";
117static const char* kPayloadNameI420 = "I420";
Erik Språng08127a92016-11-16 16:41:30 +0100118static const char* kPayloadNameGeneric = "Generic";
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800119static const char* kPayloadNameMultiplex = "Multiplex";
Erik Språng08127a92016-11-16 16:41:30 +0100120
Niels Möller3c7d5992018-10-19 15:29:54 +0200121// TODO(nisse): Delete this wrapper.
magjed10165ab2016-11-22 10:16:57 -0800122static bool CodecNamesEq(const char* name1, const char* name2) {
Niels Möller3c7d5992018-10-19 15:29:54 +0200123 return absl::EqualsIgnoreCase(name1, name2);
magjed10165ab2016-11-22 10:16:57 -0800124}
125
kthelgason1cdddc92017-08-24 03:52:48 -0700126const char* CodecTypeToPayloadString(VideoCodecType type) {
Erik Språng08127a92016-11-16 16:41:30 +0100127 switch (type) {
128 case kVideoCodecVP8:
kthelgason1cdddc92017-08-24 03:52:48 -0700129 return kPayloadNameVp8;
Erik Språng08127a92016-11-16 16:41:30 +0100130 case kVideoCodecVP9:
kthelgason1cdddc92017-08-24 03:52:48 -0700131 return kPayloadNameVp9;
Erik Språng08127a92016-11-16 16:41:30 +0100132 case kVideoCodecH264:
kthelgason1cdddc92017-08-24 03:52:48 -0700133 return kPayloadNameH264;
Erik Språng08127a92016-11-16 16:41:30 +0100134 case kVideoCodecI420:
kthelgason1cdddc92017-08-24 03:52:48 -0700135 return kPayloadNameI420;
Emircan Uysaler0a375472017-12-11 12:21:02 +0530136 // Other codecs default to generic.
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +0200137 default:
kthelgason1cdddc92017-08-24 03:52:48 -0700138 return kPayloadNameGeneric;
Erik Språng08127a92016-11-16 16:41:30 +0100139 }
140}
141
kthelgason1cdddc92017-08-24 03:52:48 -0700142VideoCodecType PayloadStringToCodecType(const std::string& name) {
magjed10165ab2016-11-22 10:16:57 -0800143 if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
kthelgason1cdddc92017-08-24 03:52:48 -0700144 return kVideoCodecVP8;
magjed10165ab2016-11-22 10:16:57 -0800145 if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
kthelgason1cdddc92017-08-24 03:52:48 -0700146 return kVideoCodecVP9;
magjed10165ab2016-11-22 10:16:57 -0800147 if (CodecNamesEq(name.c_str(), kPayloadNameH264))
kthelgason1cdddc92017-08-24 03:52:48 -0700148 return kVideoCodecH264;
magjed10165ab2016-11-22 10:16:57 -0800149 if (CodecNamesEq(name.c_str(), kPayloadNameI420))
kthelgason1cdddc92017-08-24 03:52:48 -0700150 return kVideoCodecI420;
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800151 if (CodecNamesEq(name.c_str(), kPayloadNameMultiplex))
152 return kVideoCodecMultiplex;
kthelgason1cdddc92017-08-24 03:52:48 -0700153 return kVideoCodecGeneric;
154}
155
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:22 +0000156} // namespace webrtc