h264_encoder_impl.h (4524B)
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 #include <cstddef> 21 #include <cstdint> 22 #include <memory> 23 #include <optional> 24 #include <vector> 25 26 #include "absl/container/inlined_vector.h" 27 #include "api/environment/environment.h" 28 #include "api/scoped_refptr.h" 29 #include "api/video/encoded_image.h" 30 #include "api/video/i420_buffer.h" 31 #include "api/video/video_codec_constants.h" 32 #include "api/video/video_frame.h" 33 #include "api/video/video_frame_type.h" 34 #include "api/video_codecs/scalability_mode.h" 35 #include "api/video_codecs/video_codec.h" 36 #include "api/video_codecs/video_encoder.h" 37 #include "common_video/h264/h264_bitstream_parser.h" 38 #include "modules/video_coding/codecs/h264/include/h264.h" 39 #include "modules/video_coding/codecs/h264/include/h264_globals.h" 40 #include "modules/video_coding/svc/scalable_video_controller.h" 41 #include "modules/video_coding/utility/frame_sampler.h" 42 #include "third_party/openh264/src/codec/api/wels/codec_app_def.h" 43 44 #if defined(WEBRTC_WIN) && !defined(__clang__) 45 #error "See: bugs.webrtc.org/9213#c13." 46 #endif 47 48 class ISVCEncoder; 49 50 namespace webrtc { 51 52 class H264EncoderImpl : public VideoEncoder { 53 public: 54 struct LayerConfig { 55 int simulcast_idx = 0; 56 int width = -1; 57 int height = -1; 58 bool sending = true; 59 bool key_frame_request = false; 60 float max_frame_rate = 0; 61 uint32_t target_bps = 0; 62 uint32_t max_bps = 0; 63 bool frame_dropping_on = false; 64 int key_frame_interval = 0; 65 int num_temporal_layers = 1; 66 67 void SetStreamState(bool send_stream); 68 }; 69 70 H264EncoderImpl(const Environment& env, H264EncoderSettings settings); 71 72 ~H264EncoderImpl() override; 73 74 // `settings.max_payload_size` is ignored. 75 // The following members of `codec_settings` are used. The rest are ignored. 76 // - codecType (must be kVideoCodecH264) 77 // - targetBitrate 78 // - maxFramerate 79 // - width 80 // - height 81 int32_t InitEncode(const VideoCodec* codec_settings, 82 const VideoEncoder::Settings& settings) override; 83 int32_t Release() override; 84 85 int32_t RegisterEncodeCompleteCallback( 86 EncodedImageCallback* callback) override; 87 void SetRates(const RateControlParameters& parameters) override; 88 89 // The result of encoding - an EncodedImage and CodecSpecificInfo - are 90 // passed to the encode complete callback. 91 int32_t Encode(const VideoFrame& frame, 92 const std::vector<VideoFrameType>* frame_types) override; 93 94 EncoderInfo GetEncoderInfo() const override; 95 96 // Exposed for testing. 97 H264PacketizationMode PacketizationModeForTesting() const { 98 return packetization_mode_; 99 } 100 101 private: 102 SEncParamExt CreateEncoderParams(size_t i) const; 103 104 H264BitstreamParser h264_bitstream_parser_; 105 // Reports statistics with histograms. 106 void ReportInit(); 107 void ReportError(); 108 109 std::vector<ISVCEncoder*> encoders_; 110 std::vector<SSourcePicture> pictures_; 111 std::vector<scoped_refptr<I420Buffer>> downscaled_buffers_; 112 std::vector<LayerConfig> configurations_; 113 std::vector<EncodedImage> encoded_images_; 114 std::vector<std::unique_ptr<ScalableVideoController>> svc_controllers_; 115 absl::InlinedVector<std::optional<ScalabilityMode>, kMaxSimulcastStreams> 116 scalability_modes_; 117 118 const Environment env_; 119 VideoCodec codec_; 120 H264PacketizationMode packetization_mode_; 121 size_t max_payload_size_; 122 int32_t number_of_cores_; 123 std::optional<int> encoder_thread_limit_; 124 EncodedImageCallback* encoded_image_callback_; 125 126 bool has_reported_init_; 127 bool has_reported_error_; 128 129 std::vector<uint8_t> tl0sync_limit_; 130 131 // Determine whether the frame should be sampled for PSNR. 132 FrameSampler psnr_frame_sampler_; 133 // TODO(webrtc:388070060): Remove after rollout. 134 const bool calculate_psnr_; 135 }; 136 137 } // namespace webrtc 138 139 #endif // WEBRTC_USE_H264 140 141 #endif // MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_