| 1 | #ifndef WEBRTC_NVIDIA_AV1_ENCODER_IMPL_H_ |
| 2 | #define WEBRTC_NVIDIA_AV1_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 "api/environment/environment.h" |
| 13 | #include "api/video/i420_buffer.h" |
| 14 | #include "api/video/video_codec_constants.h" |
| 15 | #include "api/video_codecs/scalability_mode.h" |
| 16 | #include "api/video_codecs/sdp_video_format.h" |
| 17 | #include "api/video_codecs/video_encoder.h" |
| 18 | #include "modules/video_coding/svc/scalable_video_controller_no_layering.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | class NvidiaAV1EncoderImpl : public VideoEncoder { |
| 23 | public: |
| 24 | struct LayerConfig { |
| 25 | int simulcast_idx = 0; |
| 26 | int width = -1; |
| 27 | int height = -1; |
| 28 | bool sending = true; |
| 29 | bool key_frame_request = false; |
| 30 | float max_frame_rate = 0; |
| 31 | uint32_t target_bps = 0; |
| 32 | uint32_t max_bps = 0; |
| 33 | bool frame_dropping_on = false; |
| 34 | int key_frame_interval = 0; |
| 35 | int num_temporal_layers = 1; |
| 36 | |
| 37 | void SetStreamState(bool send_stream); |
| 38 | }; |
| 39 | |
| 40 | public: |
| 41 | NvidiaAV1EncoderImpl(const webrtc::Environment& env, |
| 42 | CUcontext context, |
| 43 | CUmemorytype memory_type, |
| 44 | NV_ENC_BUFFER_FORMAT nv_format, |
| 45 | const SdpVideoFormat& format); |
| 46 | ~NvidiaAV1EncoderImpl() override; |
| 47 | |
| 48 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 49 | const Settings& settings) override; |
| 50 | |
| 51 | int32_t RegisterEncodeCompleteCallback( |
| 52 | EncodedImageCallback* callback) override; |
| 53 | |
| 54 | int32_t Release() override; |
| 55 | |
| 56 | int32_t Encode(const VideoFrame& frame, |
| 57 | const std::vector<VideoFrameType>* frame_types) override; |
| 58 | |
| 59 | void SetRates(const RateControlParameters& rc_parameters) override; |
| 60 | |
| 61 | EncoderInfo GetEncoderInfo() const override; |
| 62 | |
| 63 | private: |
| 64 | int32_t ProcessEncodedFrame(const std::vector<uint8_t>& packet, |
| 65 | const ::webrtc::VideoFrame& input_frame, |
| 66 | bool is_keyframe); |
| 67 | |
| 68 | const webrtc::Environment& env_; |
| 69 | EncodedImageCallback* encoded_image_callback_ = nullptr; |
| 70 | |
| 71 | std::unique_ptr<NvEncoder> encoder_; |
| 72 | const CUcontext cu_context_; |
| 73 | const CUmemorytype cu_memory_type_; |
| 74 | CUarray cu_scaled_array_ = nullptr; |
| 75 | const NV_ENC_BUFFER_FORMAT nv_format_; |
| 76 | NV_ENC_INITIALIZE_PARAMS nv_initialize_params_; |
| 77 | NV_ENC_CONFIG nv_encode_config_; |
| 78 | |
| 79 | LayerConfig configuration_; |
| 80 | EncodedImage encoded_image_; |
| 81 | VideoCodec codec_; |
| 82 | void ReportInit(); |
| 83 | void ReportError(); |
| 84 | bool has_reported_init_ = false; |
| 85 | bool has_reported_error_ = false; |
| 86 | bool sent_decodable_keyframe_ = false; |
| 87 | std::vector<uint8_t> cached_sequence_header_obu_; |
| 88 | ScalableVideoControllerNoLayering svc_controller_; |
| 89 | const SdpVideoFormat format_; |
| 90 | }; |
| 91 | |
| 92 | } // namespace webrtc |
| 93 | |
| 94 | #endif // WEBRTC_NVIDIA_AV1_ENCODER_IMPL_H_ |