File: webrtc_video_encoder_vpx.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 3,921 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_VPX_H_
#define REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_VPX_H_

#include <stdint.h>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "remoting/codec/encoder_bitrate_filter.h"
#include "remoting/codec/scoped_vpx_codec.h"
#include "remoting/codec/video_encoder_active_map.h"
#include "remoting/codec/webrtc_video_encoder.h"
#include "third_party/libvpx/source/libvpx/vpx/vp8cx.h"
#include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"

typedef struct vpx_image vpx_image_t;

namespace webrtc {
class DesktopRegion;
class DesktopSize;
}  // namespace webrtc

namespace remoting {

// This is a copy of VideoEncoderVpx with enhancements to encoder for use
// over WebRTC as transport. The original VideoEncoderVpx should be deleted
// once the old implementation is no longer in use.
class WebrtcVideoEncoderVpx : public WebrtcVideoEncoder {
 public:
  // Creates encoder for the specified protocol.
  static std::unique_ptr<WebrtcVideoEncoder> CreateForVP8();
  static std::unique_ptr<WebrtcVideoEncoder> CreateForVP9();

  WebrtcVideoEncoderVpx(const WebrtcVideoEncoderVpx&) = delete;
  WebrtcVideoEncoderVpx& operator=(const WebrtcVideoEncoderVpx&) = delete;

  ~WebrtcVideoEncoderVpx() override;

  void SetTickClockForTests(const base::TickClock* tick_clock);

  // WebrtcVideoEncoder interface.
  void SetLosslessColor(bool want_lossless) override;
  void SetEncoderSpeed(int encoder_speed) override;
  void Encode(std::unique_ptr<webrtc::DesktopFrame> frame,
              const FrameParams& params,
              EncodeCallback done) override;

 private:
  explicit WebrtcVideoEncoderVpx(bool use_vp9);

  // (Re)Configures this instance to encode frames of the specified |size|,
  // with the configured lossless color mode.
  void Configure(const webrtc::DesktopSize& size);

  // Updates codec configuration.
  void UpdateConfig(const FrameParams& params);

  // Prepares |image_| for encoding. Writes updated rectangles into
  // |updated_region|.
  void PrepareImage(const webrtc::DesktopFrame* frame,
                    webrtc::DesktopRegion* updated_region);

  // Clears active map.
  void ClearActiveMap();

  // Updates the active map according to |updated_region|. Active map is then
  // given to the encoder to speed up encoding.
  void SetActiveMapFromRegion(const webrtc::DesktopRegion& updated_region);

  // Adds areas changed in the most recent frame to |updated_region|. This
  // includes both content changes and areas enhanced by cyclic refresh.
  void UpdateRegionFromActiveMap(webrtc::DesktopRegion* updated_region);

  // True if the encoder is for VP9, false for VP8.
  const bool use_vp9_;

  // Controls VP9 color space and encode speed. Not used when configuring VP8.
  bool lossless_color_ = false;
  int vp9_encoder_speed_ = -1;

  // Holds the initialized & configured codec.
  ScopedVpxCodec codec_;

  vpx_codec_enc_cfg_t config_;

  // Used to generate zero-based frame timestamps.
  base::TimeTicks timestamp_base_;

  // vpx_image_t has a custom deallocator which needs to be called before
  // deletion.
  using scoped_vpx_image = std::unique_ptr<vpx_image_t, void (*)(vpx_image_t*)>;

  // VPX image descriptor and pixel buffer.
  scoped_vpx_image image_;

  // An active map is used to skip processing of unchanged macroblocks.
  VideoEncoderActiveMap active_map_data_;
  vpx_active_map_t active_map_;
  // TODO(joedow): Remove this flag after we're done with performance tuning.
  const bool use_active_map_ = true;

  raw_ptr<const base::TickClock> clock_;

  EncoderBitrateFilter bitrate_filter_;
};

}  // namespace remoting

#endif  // REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_VPX_H_