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/utility/quality_scaler.h"
32 #include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
33 
34 class ISVCEncoder;
35 
36 namespace webrtc {
37 
38 class H264EncoderImpl : public H264Encoder {
39  public:
40   struct LayerConfig {
41     int simulcast_idx = 0;
42     int width = -1;
43     int height = -1;
44     bool sending = true;
45     bool key_frame_request = false;
46     float max_frame_rate = 0;
47     uint32_t target_bps = 0;
48     uint32_t max_bps = 0;
49     bool frame_dropping_on = false;
50     int key_frame_interval = 0;
51     int num_temporal_layers = 1;
52 
53     void SetStreamState(bool send_stream);
54   };
55 
56  public:
57   explicit H264EncoderImpl(const cricket::VideoCodec& codec);
58   ~H264EncoderImpl() override;
59 
60   // |settings.max_payload_size| is ignored.
61   // The following members of |codec_settings| are used. The rest are ignored.
62   // - codecType (must be kVideoCodecH264)
63   // - targetBitrate
64   // - maxFramerate
65   // - width
66   // - height
67   int32_t InitEncode(const VideoCodec* codec_settings,
68                      const VideoEncoder::Settings& settings) override;
69   int32_t Release() override;
70 
71   int32_t RegisterEncodeCompleteCallback(
72       EncodedImageCallback* callback) override;
73   void SetRates(const RateControlParameters& parameters) override;
74 
75   // The result of encoding - an EncodedImage and CodecSpecificInfo - are
76   // passed to the encode complete callback.
77   int32_t Encode(const VideoFrame& frame,
78                  const std::vector<VideoFrameType>* frame_types) override;
79 
80   EncoderInfo GetEncoderInfo() const override;
81 
82   // Exposed for testing.
PacketizationModeForTesting()83   H264PacketizationMode PacketizationModeForTesting() const {
84     return packetization_mode_;
85   }
86 
87  private:
88   SEncParamExt CreateEncoderParams(size_t i) const;
89 
90   webrtc::H264BitstreamParser h264_bitstream_parser_;
91   // Reports statistics with histograms.
92   void ReportInit();
93   void ReportError();
94 
95   std::vector<ISVCEncoder*> encoders_;
96   std::vector<SSourcePicture> pictures_;
97   std::vector<rtc::scoped_refptr<I420Buffer>> downscaled_buffers_;
98   std::vector<LayerConfig> configurations_;
99   std::vector<EncodedImage> encoded_images_;
100 
101   VideoCodec codec_;
102   H264PacketizationMode packetization_mode_;
103   size_t max_payload_size_;
104   int32_t number_of_cores_;
105   EncodedImageCallback* encoded_image_callback_;
106 
107   bool has_reported_init_;
108   bool has_reported_error_;
109 
110   std::vector<uint8_t> tl0sync_limit_;
111 };
112 
113 }  // namespace webrtc
114 
115 #endif  // WEBRTC_USE_H264
116 
117 #endif  // MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
118