1 /*
2  *  Copyright (c) 2018 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 #ifndef API_VIDEO_CODECS_VIDEO_CODEC_H_
12 #define API_VIDEO_CODECS_VIDEO_CODEC_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <string>
18 
19 #include "absl/types/optional.h"
20 #include "api/video/video_bitrate_allocation.h"
21 #include "api/video/video_codec_type.h"
22 #include "common_types.h"  // NOLINT(build/include_directory)
23 #include "rtc_base/system/rtc_export.h"
24 
25 namespace webrtc {
26 
27 // The VideoCodec class represents an old defacto-apis, which we're migrating
28 // away from slowly.
29 
30 // Video codec
31 enum class VideoCodecComplexity {
32   kComplexityNormal = 0,
33   kComplexityHigh = 1,
34   kComplexityHigher = 2,
35   kComplexityMax = 3
36 };
37 
38 // VP8 specific
39 struct VideoCodecVP8 {
40   bool operator==(const VideoCodecVP8& other) const;
41   bool operator!=(const VideoCodecVP8& other) const {
42     return !(*this == other);
43   }
44   VideoCodecComplexity complexity;
45   unsigned char numberOfTemporalLayers;
46   bool denoisingOn;
47   bool automaticResizeOn;
48   bool frameDroppingOn;
49   int keyFrameInterval;
50 };
51 
52 enum class InterLayerPredMode : int {
53   kOff = 0,      // Inter-layer prediction is disabled.
54   kOn = 1,       // Inter-layer prediction is enabled.
55   kOnKeyPic = 2  // Inter-layer prediction is enabled but limited to key frames.
56 };
57 
58 // VP9 specific.
59 struct VideoCodecVP9 {
60   bool operator==(const VideoCodecVP9& other) const;
61   bool operator!=(const VideoCodecVP9& other) const {
62     return !(*this == other);
63   }
64   VideoCodecComplexity complexity;
65   unsigned char numberOfTemporalLayers;
66   bool denoisingOn;
67   bool frameDroppingOn;
68   int keyFrameInterval;
69   bool adaptiveQpMode;
70   bool automaticResizeOn;
71   unsigned char numberOfSpatialLayers;
72   bool flexibleMode;
73   InterLayerPredMode interLayerPred;
74 };
75 
76 // H264 specific.
77 struct VideoCodecH264 {
78   bool operator==(const VideoCodecH264& other) const;
79   bool operator!=(const VideoCodecH264& other) const {
80     return !(*this == other);
81   }
82   bool frameDroppingOn;
83   int keyFrameInterval;
84   uint8_t numberOfTemporalLayers;
85 };
86 
87 // Translates from name of codec to codec type and vice versa.
88 RTC_EXPORT const char* CodecTypeToPayloadString(VideoCodecType type);
89 RTC_EXPORT VideoCodecType PayloadStringToCodecType(const std::string& name);
90 
91 union VideoCodecUnion {
92   VideoCodecVP8 VP8;
93   VideoCodecVP9 VP9;
94   VideoCodecH264 H264;
95 };
96 
97 enum class VideoCodecMode { kRealtimeVideo, kScreensharing };
98 
99 // Common video codec properties
100 class RTC_EXPORT VideoCodec {
101  public:
102   VideoCodec();
103 
104   // Public variables. TODO(hta): Make them private with accessors.
105   VideoCodecType codecType;
106   unsigned char plType;
107 
108   // TODO(nisse): Change to int, for consistency.
109   uint16_t width;
110   uint16_t height;
111 
112   unsigned int startBitrate;  // kilobits/sec.
113   unsigned int maxBitrate;    // kilobits/sec.
114   unsigned int minBitrate;    // kilobits/sec.
115 
116   uint32_t maxFramerate;
117 
118   // This enables/disables encoding and sending when there aren't multiple
119   // simulcast streams,by allocating 0 bitrate if inactive.
120   bool active;
121 
122   unsigned int qpMax;
123   unsigned char numberOfSimulcastStreams;
124   SimulcastStream simulcastStream[kMaxSimulcastStreams];
125   SpatialLayer spatialLayers[kMaxSpatialLayers];
126 
127   VideoCodecMode mode;
128   bool expect_encode_from_texture;
129 
130   // The size of pool which is used to store video frame buffers inside decoder.
131   // If value isn't present some codec-default value will be used.
132   // If value is present and decoder doesn't have buffer pool the
133   // value will be ignored.
134   absl::optional<int> buffer_pool_size;
135 
136   // Timing frames configuration. There is delay of delay_ms between two
137   // consequent timing frames, excluding outliers. Frame is always made a
138   // timing frame if it's at least outlier_ratio in percent of "ideal" average
139   // frame given bitrate and framerate, i.e. if it's bigger than
140   // |outlier_ratio / 100.0 * bitrate_bps / fps| in bits. This way, timing
141   // frames will not be sent too often usually. Yet large frames will always
142   // have timing information for debug purposes because they are more likely to
143   // cause extra delays.
144   struct TimingFrameTriggerThresholds {
145     int64_t delay_ms;
146     uint16_t outlier_ratio_percent;
147   } timing_frame_thresholds;
148 
149   bool operator==(const VideoCodec& other) const = delete;
150   bool operator!=(const VideoCodec& other) const = delete;
151 
152   // Accessors for codec specific information.
153   // There is a const version of each that returns a reference,
154   // and a non-const version that returns a pointer, in order
155   // to allow modification of the parameters.
156   VideoCodecVP8* VP8();
157   const VideoCodecVP8& VP8() const;
158   VideoCodecVP9* VP9();
159   const VideoCodecVP9& VP9() const;
160   VideoCodecH264* H264();
161   const VideoCodecH264& H264() const;
162 
163  private:
164   // TODO(hta): Consider replacing the union with a pointer type.
165   // This will allow removing the VideoCodec* types from this file.
166   VideoCodecUnion codec_specific_;
167 };
168 
169 }  // namespace webrtc
170 #endif  // API_VIDEO_CODECS_VIDEO_CODEC_H_
171