| 1 | #ifndef WEBRTC_NVIDIA_H264_ENCODER_IMPL_H_ |
| 2 | #define WEBRTC_NVIDIA_H264_ENCODER_IMPL_H_ |
| 3 | |
| 4 | #include <cuda.h> |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "NvEncoder/NvEncoder.h" |
| 10 | #include "NvEncoder/NvEncoderCuda.h" |
| 11 | |
| 12 | #include "absl/container/inlined_vector.h" |
| 13 | #include "api/transport/rtp/dependency_descriptor.h" |
| 14 | #include "api/video/i420_buffer.h" |
| 15 | #include "api/video/video_codec_constants.h" |
| 16 | #include "api/video_codecs/scalability_mode.h" |
| 17 | #include "api/video_codecs/video_encoder.h" |
| 18 | #include "common_video/h264/h264_bitstream_parser.h" |
| 19 | #include "modules/video_coding/codecs/h264/include/h264.h" |
| 20 | #include "modules/video_coding/svc/scalable_video_controller.h" |
| 21 | #include "modules/video_coding/utility/quality_scaler.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | class NvidiaH264EncoderImpl : public VideoEncoder { |
| 26 | public: |
| 27 | struct LayerConfig { |
| 28 | int simulcast_idx = 0; |
| 29 | int width = -1; |
| 30 | int height = -1; |
| 31 | bool sending = true; |
| 32 | bool key_frame_request = false; |
| 33 | float max_frame_rate = 0; |
| 34 | uint32_t target_bps = 0; |
| 35 | uint32_t max_bps = 0; |
| 36 | bool frame_dropping_on = false; |
| 37 | int key_frame_interval = 0; |
| 38 | int num_temporal_layers = 1; |
| 39 | |
| 40 | void SetStreamState(bool send_stream); |
| 41 | }; |
| 42 | |
| 43 | public: |
| 44 | NvidiaH264EncoderImpl(const webrtc::Environment& env, |
| 45 | CUcontext context, |
| 46 | CUmemorytype memory_type, |
| 47 | NV_ENC_BUFFER_FORMAT nv_format, |
| 48 | const SdpVideoFormat& format); |
| 49 | ~NvidiaH264EncoderImpl() override; |
| 50 | |
| 51 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 52 | const Settings& settings) override; |
| 53 | |
| 54 | int32_t RegisterEncodeCompleteCallback( |
| 55 | EncodedImageCallback* callback) override; |
| 56 | |
| 57 | int32_t Release() override; |
| 58 | |
| 59 | int32_t Encode(const VideoFrame& frame, |
| 60 | const std::vector<VideoFrameType>* frame_types) override; |
| 61 | |
| 62 | void SetRates(const RateControlParameters& rc_parameters) override; |
| 63 | |
| 64 | EncoderInfo GetEncoderInfo() const override; |
| 65 | |
| 66 | private: |
| 67 | int32_t ProcessEncodedFrame(std::vector<uint8_t>& packet, |
| 68 | const ::webrtc::VideoFrame& inputFrame); |
| 69 | private: |
| 70 | const webrtc::Environment& env_; |
| 71 | EncodedImageCallback* encoded_image_callback_ = nullptr; |
| 72 | |
| 73 | std::unique_ptr<NvEncoder> encoder_; |
| 74 | CUcontext cu_context_; |
| 75 | CUmemorytype cu_memory_type_; |
| 76 | CUarray cu_scaled_array_; |
| 77 | NV_ENC_BUFFER_FORMAT nv_format_; |
| 78 | NV_ENC_INITIALIZE_PARAMS nv_initialize_params_; |
| 79 | NV_ENC_CONFIG nv_encode_config_; |
| 80 | GUID nv_profile_guid_; |
| 81 | NV_ENC_LEVEL nv_enc_level_; |
| 82 | |
| 83 | LayerConfig configuration_; |
| 84 | EncodedImage encoded_image_; |
| 85 | H264PacketizationMode packetization_mode_; |
| 86 | VideoCodec codec_; |
| 87 | void ReportInit(); |
| 88 | void ReportError(); |
| 89 | bool has_reported_init_ = false; |
| 90 | bool has_reported_error_ = false; |
| 91 | webrtc::H264BitstreamParser h264_bitstream_parser_; |
| 92 | const SdpVideoFormat format_; |
| 93 | H264Profile profile_ = H264Profile::kProfileConstrainedBaseline; |
| 94 | H264Level level_ = H264Level::kLevel1_b; |
| 95 | }; |
| 96 | |
| 97 | } // namespace webrtc |
| 98 | |
| 99 | #endif // WEBRTC_NVIDIA_H264_ENCODER_IMPL_H_ |