123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- /*
- * 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 <memory>
- #include <vector>
- #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 <fstream>
- #include "NvVideoEncoder.h"
- #include <sstream>
- #include <stdint.h>
- #include <semaphore.h>
- #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<VideoFrameType>* 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<context_enc_t*> encoders_; //jetson encoder 实例
- std::vector<SSourcePicture> pictures_;
- std::vector<rtc::scoped_refptr<I420Buffer>> downscaled_buffers_;
- std::vector<LayerConfig> configurations_;
- std::vector<EncodedImage> 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<uint8_t> tl0sync_limit_;
- void CloseEncoder(context_enc_t *ctx); //增加
- };
- } // namespace webrtc
- #endif // JETSON_H264_ENCODE_H_
|