/* * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. * */ #ifndef JETSON_H264_ENCODE_H_ #define JETSON_H264_ENCODE_H_ // Everything declared in this header is only required when WebRTC is // build with H264 support, please do not move anything out of the // #ifdef unless needed and tested. #if defined(WEBRTC_WIN) && !defined(__clang__) #error "See: bugs.webrtc.org/9213#c13." #endif #include #include #include "api/video/i420_buffer.h" #include "api/video_codecs/video_encoder.h" #include "common_video/h264/h264_bitstream_parser.h" #include "modules/video_coding/codecs/h264/include/h264.h" #include "modules/video_coding/utility/quality_scaler.h" #include "third_party/openh264/src/codec/api/svc/codec_app_def.h" #include "api/video/i420_buffer.h" #include "modules/video_coding/include/video_error_codes.h" #include "modules/video_coding/h264_sprop_parameter_sets.h" #include #include "NvVideoEncoder.h" #include #include #include #include "NvBufSurface.h" // #include "../jetson_encode_pkg/JetsonEnc.h" #define CRC32_POLYNOMIAL 0xEDB88320L #define MAX_OUT_BUFFERS 32 typedef struct RPS_List { uint32_t nFrameId; bool bLTRefFrame; } RPS_List; typedef struct RPS_param { sem_t sema; uint32_t m_numTemperalLayers; uint32_t nActiveRefFrames; RPS_List rps_list[V4L2_MAX_REF_FRAMES]; } RPS_param; typedef struct CrcRec { unsigned int CRCTable[256]; unsigned int CrcValue; }Crc; typedef struct { NvVideoEncoder *enc; uint32_t encoder_pixfmt; uint32_t raw_pixfmt; char *in_file_path; std::ifstream *in_file; uint32_t width; uint32_t height; char *out_file_path; std::ofstream *out_file; char *ROI_Param_file_path; char *Recon_Ref_file_path; char *RPS_Param_file_path; char *hints_Param_file_path; char *GDR_Param_file_path; char *GDR_out_file_path; std::ifstream *roi_Param_file; std::ifstream *recon_Ref_file; std::ifstream *rps_Param_file; std::ifstream *hints_Param_file; std::ifstream *gdr_Param_file; std::ofstream *gdr_out_file; uint32_t bitrate; uint32_t peak_bitrate; uint32_t profile; enum v4l2_mpeg_video_bitrate_mode ratecontrol; uint32_t iframe_interval; uint32_t idr_interval; uint32_t level; uint32_t fps_n; uint32_t fps_d; uint32_t gdr_start_frame_number; /* Frame number where GDR has to be started */ uint32_t gdr_num_frames; /* Number of frames where GDR to be applied */ uint32_t gdr_out_frame_number; /* Frames number from where encoded buffers are to be dumped */ enum v4l2_enc_temporal_tradeoff_level_type temporal_tradeoff_level; enum v4l2_enc_hw_preset_type hw_preset_type; v4l2_enc_slice_length_type slice_length_type; uint32_t slice_length; uint32_t virtual_buffer_size; uint32_t num_reference_frames; uint32_t slice_intrarefresh_interval; uint32_t num_b_frames; uint32_t nMinQpI; /* Minimum QP value to use for index frames */ uint32_t nMaxQpI; /* Maximum QP value to use for index frames */ uint32_t nMinQpP; /* Minimum QP value to use for P frames */ uint32_t nMaxQpP; /* Maximum QP value to use for P frames */ uint32_t nMinQpB; /* Minimum QP value to use for B frames */ uint32_t nMaxQpB; /* Maximum QP value to use for B frames */ uint32_t sMaxQp; /* Session Maximum QP value */ uint32_t sar_width; uint32_t sar_height; uint32_t IinitQP; uint32_t PinitQP; uint32_t BinitQP; uint32_t log2_num_av1rows; uint32_t log2_num_av1cols; uint8_t bit_depth; uint8_t enable_av1ssimrdo; uint8_t disable_av1cdfupdate; uint8_t chroma_format_idc; int output_plane_fd[32]; int capture_plane_fd[32]; bool insert_sps_pps_at_idr; bool enable_slice_level_encode; bool disable_cabac; bool insert_vui; bool enable_extended_colorformat; bool insert_aud; bool alliframes; bool is_semiplanar; bool enable_initQP; bool enable_ratecontrol; bool enable_av1tile; enum v4l2_memory output_memory_type; enum v4l2_memory capture_memory_type; enum v4l2_colorspace cs; bool report_metadata; bool input_metadata; bool copy_timestamp; uint32_t start_ts; bool dump_mv; bool enableGDR; bool bGapsInFrameNumAllowed; bool bnoIframe; uint32_t nH264FrameNumBits; uint32_t nH265PocLsbBits; bool externalRCHints; bool enableROI; bool b_use_enc_cmd; bool enableLossless; bool got_eos; bool externalRPS; bool RPS_threeLayerSvc; RPS_param rps_par; bool use_gold_crc; char gold_crc[20]; Crc *pBitStreamCrc; bool bReconCrc; uint32_t rl; /* Reconstructed surface Left cordinate */ uint32_t rt; /* Reconstructed surface Top cordinate */ uint32_t rw; /* Reconstructed surface width */ uint32_t rh; /* Reconstructed surface height */ uint64_t timestamp; uint64_t timestampincr; bool stats; std::stringstream *runtime_params_str; uint32_t next_param_change_frame; bool got_error; int stress_test; uint32_t endofstream_capture; uint32_t endofstream_output; uint32_t input_frames_queued_count; uint32_t startf; uint32_t endf; uint32_t num_output_buffers; int32_t num_frames_to_encode; uint32_t poc_type; v4l2_enc_ppe_init_params ppe_init_params; // Configuration params for preprocessing enhancements module int max_perf; int blocking_mode; //Set if running in blocking mode sem_t pollthread_sema; // Polling thread waits on this to be signalled to issue Poll sem_t encoderthread_sema; // Encoder thread waits on this to be signalled to continue q/dq loop pthread_t enc_pollthread; // Polling thread, created if running in non-blocking mode. pthread_t enc_capture_loop; // Encoder capture thread } context_enc_t; class ISVCEncoder; namespace webrtc { class JetH264Encoder : public H264Encoder { public: struct LayerConfig { int simulcast_idx = 0; int width = -1; int height = -1; bool sending = true; bool key_frame_request = false; float max_frame_rate = 0; uint32_t target_bps = 0; uint32_t max_bps = 0; bool frame_dropping_on = false; int key_frame_interval = 0; int num_temporal_layers = 1; void SetStreamState(bool send_stream); }; public: explicit JetH264Encoder(const cricket::VideoCodec& codec); ~JetH264Encoder() override; // |settings.max_payload_size| is ignored. // The following members of |codec_settings| are used. The rest are ignored. // - codecType (must be kVideoCodecH264) // - targetBitrate // - maxFramerate // - width // - height int32_t InitEncode(const VideoCodec* codec_settings, const VideoEncoder::Settings& settings) override; int32_t Release() override; int32_t RegisterEncodeCompleteCallback( EncodedImageCallback* callback) override; void SetRates(const RateControlParameters& parameters) override; // The result of encoding - an EncodedImage and CodecSpecificInfo - are // passed to the encode complete callback. int32_t Encode(const VideoFrame& frame, const std::vector* frame_types) override; EncoderInfo GetEncoderInfo() const override; // Exposed for testing. H264PacketizationMode PacketizationModeForTesting() const { return packetization_mode_; } private: bool OpenEncoder(context_enc_t *ctx, LayerConfig &io_param); //增加 // static void set_defaults(context_enc_t * ctx); //增加 SEncParamExt CreateEncoderParams(size_t i) const; webrtc::H264BitstreamParser h264_bitstream_parser_; // Reports statistics with histograms. void ReportInit(); void ReportError(); // void copyFrame(AVFrame *frame, const webrtc::I420BufferInterface *buffer); std::vector encoders_; //jetson encoder 实例 std::vector pictures_; std::vector> downscaled_buffers_; std::vector configurations_; std::vector encoded_images_; VideoCodec codec_; H264PacketizationMode packetization_mode_; size_t max_payload_size_; int32_t number_of_cores_; EncodedImageCallback* encoded_image_callback_; bool has_reported_init_; bool has_reported_error_; std::vector tl0sync_limit_; void CloseEncoder(context_enc_t *ctx); //增加 }; } // namespace webrtc #endif // JETSON_H264_ENCODE_H_