xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/h264/h264_encoder_impl.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
12 #ifndef MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
13 #define MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
14 
15 // Everything declared in this header is only required when WebRTC is
16 // build with H264 support, please do not move anything out of the
17 // #ifdef unless needed and tested.
18 #ifdef WEBRTC_USE_H264
19 
20 #if defined(WEBRTC_WIN) && !defined(__clang__)
21 #error "See: bugs.webrtc.org/9213#c13."
22 #endif
23 
24 #include <memory>
25 #include <vector>
26 
27 #include "api/video/i420_buffer.h"
28 #include "api/video_codecs/video_encoder.h"
29 #include "common_video/h264/h264_bitstream_parser.h"
30 #include "modules/video_coding/codecs/h264/include/h264.h"
31 #include "modules/video_coding/svc/scalable_video_controller.h"
32 #include "modules/video_coding/utility/quality_scaler.h"
33 #include "third_party/openh264/src/codec/api/wels/codec_app_def.h"
34 
35 class ISVCEncoder;
36 
37 namespace webrtc {
38 
39 class H264EncoderImpl : public H264Encoder {
40  public:
41   struct LayerConfig {
42     int simulcast_idx = 0;
43     int width = -1;
44     int height = -1;
45     bool sending = true;
46     bool key_frame_request = false;
47     float max_frame_rate = 0;
48     uint32_t target_bps = 0;
49     uint32_t max_bps = 0;
50     bool frame_dropping_on = false;
51     int key_frame_interval = 0;
52     int num_temporal_layers = 1;
53 
54     void SetStreamState(bool send_stream);
55   };
56 
57  public:
58   explicit H264EncoderImpl(const cricket::VideoCodec& codec);
59   ~H264EncoderImpl() override;
60 
61   // `settings.max_payload_size` is ignored.
62   // The following members of `codec_settings` are used. The rest are ignored.
63   // - codecType (must be kVideoCodecH264)
64   // - targetBitrate
65   // - maxFramerate
66   // - width
67   // - height
68   int32_t InitEncode(const VideoCodec* codec_settings,
69                      const VideoEncoder::Settings& settings) override;
70   int32_t Release() override;
71 
72   int32_t RegisterEncodeCompleteCallback(
73       EncodedImageCallback* callback) override;
74   void SetRates(const RateControlParameters& parameters) override;
75 
76   // The result of encoding - an EncodedImage and CodecSpecificInfo - are
77   // passed to the encode complete callback.
78   int32_t Encode(const VideoFrame& frame,
79                  const std::vector<VideoFrameType>* frame_types) override;
80 
81   EncoderInfo GetEncoderInfo() const override;
82 
83   // Exposed for testing.
PacketizationModeForTesting()84   H264PacketizationMode PacketizationModeForTesting() const {
85     return packetization_mode_;
86   }
87 
88  private:
89   SEncParamExt CreateEncoderParams(size_t i) const;
90 
91   webrtc::H264BitstreamParser h264_bitstream_parser_;
92   // Reports statistics with histograms.
93   void ReportInit();
94   void ReportError();
95 
96   std::vector<ISVCEncoder*> encoders_;
97   std::vector<SSourcePicture> pictures_;
98   std::vector<rtc::scoped_refptr<I420Buffer>> downscaled_buffers_;
99   std::vector<LayerConfig> configurations_;
100   std::vector<EncodedImage> encoded_images_;
101   std::vector<std::unique_ptr<ScalableVideoController>> svc_controllers_;
102 
103   VideoCodec codec_;
104   H264PacketizationMode packetization_mode_;
105   size_t max_payload_size_;
106   int32_t number_of_cores_;
107   EncodedImageCallback* encoded_image_callback_;
108 
109   bool has_reported_init_;
110   bool has_reported_error_;
111 
112   std::vector<uint8_t> tl0sync_limit_;
113 };
114 
115 }  // namespace webrtc
116 
117 #endif  // WEBRTC_USE_H264
118 
119 #endif  // MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
120