tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

video_codec.h (7758B)


      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 <optional>
     18 #include <string>
     19 
     20 #include "api/video/video_codec_constants.h"
     21 #include "api/video/video_codec_type.h"
     22 #include "api/video_codecs/scalability_mode.h"
     23 #include "api/video_codecs/simulcast_stream.h"
     24 #include "api/video_codecs/spatial_layer.h"
     25 #include "rtc_base/system/rtc_export.h"
     26 
     27 namespace webrtc {
     28 
     29 // The VideoCodec class represents an old defacto-apis, which we're migrating
     30 // away from slowly.
     31 
     32 // Video codec
     33 enum class VideoCodecComplexity {
     34  kComplexityLow = -1,
     35  kComplexityNormal = 0,
     36  kComplexityHigh = 1,
     37  kComplexityHigher = 2,
     38  kComplexityMax = 3
     39 };
     40 
     41 // VP8 specific
     42 struct VideoCodecVP8 {
     43  bool operator==(const VideoCodecVP8& other) const;
     44  bool operator!=(const VideoCodecVP8& other) const {
     45    return !(*this == other);
     46  }
     47  // Temporary utility method for transition deleting numberOfTemporalLayers
     48  // setting (replaced by ScalabilityMode).
     49  void SetNumberOfTemporalLayers(unsigned char n) {
     50    numberOfTemporalLayers = n;
     51  }
     52  unsigned char numberOfTemporalLayers;
     53  bool denoisingOn;
     54  bool automaticResizeOn;
     55  int keyFrameInterval;
     56 };
     57 
     58 enum class InterLayerPredMode : int {
     59  kOff = 0,      // Inter-layer prediction is disabled.
     60  kOn = 1,       // Inter-layer prediction is enabled.
     61  kOnKeyPic = 2  // Inter-layer prediction is enabled but limited to key frames.
     62 };
     63 
     64 // VP9 specific.
     65 struct VideoCodecVP9 {
     66  bool operator==(const VideoCodecVP9& other) const;
     67  bool operator!=(const VideoCodecVP9& other) const {
     68    return !(*this == other);
     69  }
     70  // Temporary utility method for transition deleting numberOfTemporalLayers
     71  // setting (replaced by ScalabilityMode).
     72  void SetNumberOfTemporalLayers(unsigned char n) {
     73    numberOfTemporalLayers = n;
     74  }
     75  unsigned char numberOfTemporalLayers;
     76  bool denoisingOn;
     77  int keyFrameInterval;
     78  bool adaptiveQpMode;
     79  bool automaticResizeOn;
     80  unsigned char numberOfSpatialLayers;
     81  bool flexibleMode;
     82  InterLayerPredMode interLayerPred;
     83 };
     84 
     85 // H264 specific.
     86 struct VideoCodecH264 {
     87  bool operator==(const VideoCodecH264& other) const;
     88  bool operator!=(const VideoCodecH264& other) const {
     89    return !(*this == other);
     90  }
     91  // Temporary utility method for transition deleting numberOfTemporalLayers
     92  // setting (replaced by ScalabilityMode).
     93  void SetNumberOfTemporalLayers(unsigned char n) {
     94    numberOfTemporalLayers = n;
     95  }
     96  int keyFrameInterval;
     97  uint8_t numberOfTemporalLayers;
     98 };
     99 
    100 struct VideoCodecAV1 {
    101  bool operator==(const VideoCodecAV1& other) const {
    102    return automatic_resize_on == other.automatic_resize_on;
    103  }
    104  bool operator!=(const VideoCodecAV1& other) const {
    105    return !(*this == other);
    106  }
    107  bool automatic_resize_on;
    108 };
    109 
    110 // Translates from name of codec to codec type and vice versa.
    111 RTC_EXPORT const char* CodecTypeToPayloadString(VideoCodecType type);
    112 RTC_EXPORT VideoCodecType PayloadStringToCodecType(const std::string& name);
    113 
    114 union VideoCodecUnion {
    115  VideoCodecVP8 VP8;
    116  VideoCodecVP9 VP9;
    117  VideoCodecH264 H264;
    118  VideoCodecAV1 AV1;
    119 };
    120 
    121 enum class VideoCodecMode { kRealtimeVideo, kScreensharing };
    122 
    123 // Common video codec properties
    124 class RTC_EXPORT VideoCodec {
    125 public:
    126  VideoCodec();
    127 
    128  // Scalability mode as described in
    129  // https://www.w3.org/TR/webrtc-svc/#scalabilitymodes*
    130  std::optional<ScalabilityMode> GetScalabilityMode() const {
    131    return scalability_mode_;
    132  }
    133  void SetScalabilityMode(ScalabilityMode scalability_mode) {
    134    scalability_mode_ = scalability_mode;
    135  }
    136  void UnsetScalabilityMode() { scalability_mode_ = std::nullopt; }
    137 
    138  VideoCodecComplexity GetVideoEncoderComplexity() const;
    139  void SetVideoEncoderComplexity(VideoCodecComplexity complexity_setting);
    140 
    141  bool GetFrameDropEnabled() const;
    142  void SetFrameDropEnabled(bool enabled);
    143 
    144  bool IsSinglecast() const { return numberOfSimulcastStreams <= 1; }
    145  bool IsSimulcast() const { return !IsSinglecast(); }
    146  // Returns true if the codec is a mixed-codec simulcast.
    147  bool IsMixedCodec() const;
    148 
    149  // Public variables. TODO(hta): Make them private with accessors.
    150  VideoCodecType codecType;
    151 
    152  // TODO(nisse): Change to int, for consistency.
    153  uint16_t width;
    154  uint16_t height;
    155 
    156  unsigned int startBitrate;  // kilobits/sec.
    157  unsigned int maxBitrate;    // kilobits/sec.
    158  unsigned int minBitrate;    // kilobits/sec.
    159 
    160  uint32_t maxFramerate;
    161 
    162  // This enables/disables encoding and sending when there aren't multiple
    163  // simulcast streams,by allocating 0 bitrate if inactive.
    164  bool active;
    165 
    166  unsigned int qpMax;
    167  // The actual number of simulcast streams. This is <= 1 in singlecast (it can
    168  // be 0 in old code paths), but it is also 1 in the {active,inactive,inactive}
    169  // "single RTP simulcast" use case and the legacy kSVC use case. In all other
    170  // cases this is the same as the number of encodings (which may include
    171  // inactive encodings). In other words:
    172  // - `numberOfSimulcastStreams <= 1` in singlecast and singlecast-like setups
    173  //   including legacy kSVC (encodings interpreted as spatial layers) or
    174  //   standard kSVC (1 active encoding).
    175  // - `numberOfSimulcastStreams > 1` in simulcast of 2+ active encodings.
    176  unsigned char numberOfSimulcastStreams;
    177  SimulcastStream simulcastStream[kMaxSimulcastStreams];
    178  SpatialLayer spatialLayers[kMaxSpatialLayers];
    179 
    180  VideoCodecMode mode;
    181  bool expect_encode_from_texture;
    182 
    183  // Timing frames configuration. There is delay of delay_ms between two
    184  // consequent timing frames, excluding outliers. Frame is always made a
    185  // timing frame if it's at least outlier_ratio in percent of "ideal" average
    186  // frame given bitrate and framerate, i.e. if it's bigger than
    187  // |outlier_ratio / 100.0 * bitrate_bps / fps| in bits. This way, timing
    188  // frames will not be sent too often usually. Yet large frames will always
    189  // have timing information for debug purposes because they are more likely to
    190  // cause extra delays.
    191  struct TimingFrameTriggerThresholds {
    192    int64_t delay_ms;
    193    uint16_t outlier_ratio_percent;
    194  } timing_frame_thresholds;
    195 
    196  // Legacy Google conference mode flag for simulcast screenshare
    197  bool legacy_conference_mode;
    198 
    199  bool operator==(const VideoCodec& other) const = delete;
    200  bool operator!=(const VideoCodec& other) const = delete;
    201  std::string ToString() const;
    202 
    203  // Accessors for codec specific information.
    204  // There is a const version of each that returns a reference,
    205  // and a non-const version that returns a pointer, in order
    206  // to allow modification of the parameters.
    207  VideoCodecVP8* VP8();
    208  const VideoCodecVP8& VP8() const;
    209  VideoCodecVP9* VP9();
    210  const VideoCodecVP9& VP9() const;
    211  VideoCodecH264* H264();
    212  const VideoCodecH264& H264() const;
    213  VideoCodecAV1* AV1();
    214  const VideoCodecAV1& AV1() const;
    215 
    216 private:
    217  // TODO(hta): Consider replacing the union with a pointer type.
    218  // This will allow removing the VideoCodec* types from this file.
    219  VideoCodecUnion codec_specific_;
    220  std::optional<ScalabilityMode> scalability_mode_;
    221  // 'complexity_' indicates the CPU capability of the client. It's used to
    222  // determine encoder CPU complexity (e.g., cpu_used for VP8, VP9. and AV1).
    223  VideoCodecComplexity complexity_;
    224  bool frame_drop_enabled_ = false;
    225 };
    226 
    227 }  // namespace webrtc
    228 #endif  // API_VIDEO_CODECS_VIDEO_CODEC_H_