jetson_h264_encode.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifndef JETSON_H264_ENCODE_H_
  12. #define JETSON_H264_ENCODE_H_
  13. // Everything declared in this header is only required when WebRTC is
  14. // build with H264 support, please do not move anything out of the
  15. // #ifdef unless needed and tested.
  16. #if defined(WEBRTC_WIN) && !defined(__clang__)
  17. #error "See: bugs.webrtc.org/9213#c13."
  18. #endif
  19. #include <memory>
  20. #include <vector>
  21. #include "api/video/i420_buffer.h"
  22. #include "api/video_codecs/video_encoder.h"
  23. #include "common_video/h264/h264_bitstream_parser.h"
  24. #include "modules/video_coding/codecs/h264/include/h264.h"
  25. #include "modules/video_coding/utility/quality_scaler.h"
  26. #include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
  27. #include "api/video/i420_buffer.h"
  28. #include "modules/video_coding/include/video_error_codes.h"
  29. #include "modules/video_coding/h264_sprop_parameter_sets.h"
  30. #include <fstream>
  31. #include "NvVideoEncoder.h"
  32. #include <sstream>
  33. #include <stdint.h>
  34. #include <semaphore.h>
  35. #include "NvBufSurface.h"
  36. // #include "../jetson_encode_pkg/JetsonEnc.h"
  37. #define CRC32_POLYNOMIAL 0xEDB88320L
  38. #define MAX_OUT_BUFFERS 32
  39. typedef struct RPS_List
  40. {
  41. uint32_t nFrameId;
  42. bool bLTRefFrame;
  43. } RPS_List;
  44. typedef struct RPS_param
  45. {
  46. sem_t sema;
  47. uint32_t m_numTemperalLayers;
  48. uint32_t nActiveRefFrames;
  49. RPS_List rps_list[V4L2_MAX_REF_FRAMES];
  50. } RPS_param;
  51. typedef struct CrcRec
  52. {
  53. unsigned int CRCTable[256];
  54. unsigned int CrcValue;
  55. }Crc;
  56. typedef struct
  57. {
  58. NvVideoEncoder *enc;
  59. uint32_t encoder_pixfmt;
  60. uint32_t raw_pixfmt;
  61. char *in_file_path;
  62. std::ifstream *in_file;
  63. uint32_t width;
  64. uint32_t height;
  65. char *out_file_path;
  66. std::ofstream *out_file;
  67. char *ROI_Param_file_path;
  68. char *Recon_Ref_file_path;
  69. char *RPS_Param_file_path;
  70. char *hints_Param_file_path;
  71. char *GDR_Param_file_path;
  72. char *GDR_out_file_path;
  73. std::ifstream *roi_Param_file;
  74. std::ifstream *recon_Ref_file;
  75. std::ifstream *rps_Param_file;
  76. std::ifstream *hints_Param_file;
  77. std::ifstream *gdr_Param_file;
  78. std::ofstream *gdr_out_file;
  79. uint32_t bitrate;
  80. uint32_t peak_bitrate;
  81. uint32_t profile;
  82. enum v4l2_mpeg_video_bitrate_mode ratecontrol;
  83. uint32_t iframe_interval;
  84. uint32_t idr_interval;
  85. uint32_t level;
  86. uint32_t fps_n;
  87. uint32_t fps_d;
  88. uint32_t gdr_start_frame_number; /* Frame number where GDR has to be started */
  89. uint32_t gdr_num_frames; /* Number of frames where GDR to be applied */
  90. uint32_t gdr_out_frame_number; /* Frames number from where encoded buffers are to be dumped */
  91. enum v4l2_enc_temporal_tradeoff_level_type temporal_tradeoff_level;
  92. enum v4l2_enc_hw_preset_type hw_preset_type;
  93. v4l2_enc_slice_length_type slice_length_type;
  94. uint32_t slice_length;
  95. uint32_t virtual_buffer_size;
  96. uint32_t num_reference_frames;
  97. uint32_t slice_intrarefresh_interval;
  98. uint32_t num_b_frames;
  99. uint32_t nMinQpI; /* Minimum QP value to use for index frames */
  100. uint32_t nMaxQpI; /* Maximum QP value to use for index frames */
  101. uint32_t nMinQpP; /* Minimum QP value to use for P frames */
  102. uint32_t nMaxQpP; /* Maximum QP value to use for P frames */
  103. uint32_t nMinQpB; /* Minimum QP value to use for B frames */
  104. uint32_t nMaxQpB; /* Maximum QP value to use for B frames */
  105. uint32_t sMaxQp; /* Session Maximum QP value */
  106. uint32_t sar_width;
  107. uint32_t sar_height;
  108. uint32_t IinitQP;
  109. uint32_t PinitQP;
  110. uint32_t BinitQP;
  111. uint32_t log2_num_av1rows;
  112. uint32_t log2_num_av1cols;
  113. uint8_t bit_depth;
  114. uint8_t enable_av1ssimrdo;
  115. uint8_t disable_av1cdfupdate;
  116. uint8_t chroma_format_idc;
  117. int output_plane_fd[32];
  118. int capture_plane_fd[32];
  119. bool insert_sps_pps_at_idr;
  120. bool enable_slice_level_encode;
  121. bool disable_cabac;
  122. bool insert_vui;
  123. bool enable_extended_colorformat;
  124. bool insert_aud;
  125. bool alliframes;
  126. bool is_semiplanar;
  127. bool enable_initQP;
  128. bool enable_ratecontrol;
  129. bool enable_av1tile;
  130. enum v4l2_memory output_memory_type;
  131. enum v4l2_memory capture_memory_type;
  132. enum v4l2_colorspace cs;
  133. bool report_metadata;
  134. bool input_metadata;
  135. bool copy_timestamp;
  136. uint32_t start_ts;
  137. bool dump_mv;
  138. bool enableGDR;
  139. bool bGapsInFrameNumAllowed;
  140. bool bnoIframe;
  141. uint32_t nH264FrameNumBits;
  142. uint32_t nH265PocLsbBits;
  143. bool externalRCHints;
  144. bool enableROI;
  145. bool b_use_enc_cmd;
  146. bool enableLossless;
  147. bool got_eos;
  148. bool externalRPS;
  149. bool RPS_threeLayerSvc;
  150. RPS_param rps_par;
  151. bool use_gold_crc;
  152. char gold_crc[20];
  153. Crc *pBitStreamCrc;
  154. bool bReconCrc;
  155. uint32_t rl; /* Reconstructed surface Left cordinate */
  156. uint32_t rt; /* Reconstructed surface Top cordinate */
  157. uint32_t rw; /* Reconstructed surface width */
  158. uint32_t rh; /* Reconstructed surface height */
  159. uint64_t timestamp;
  160. uint64_t timestampincr;
  161. bool stats;
  162. std::stringstream *runtime_params_str;
  163. uint32_t next_param_change_frame;
  164. bool got_error;
  165. int stress_test;
  166. uint32_t endofstream_capture;
  167. uint32_t endofstream_output;
  168. uint32_t input_frames_queued_count;
  169. uint32_t startf;
  170. uint32_t endf;
  171. uint32_t num_output_buffers;
  172. int32_t num_frames_to_encode;
  173. uint32_t poc_type;
  174. v4l2_enc_ppe_init_params ppe_init_params; // Configuration params for preprocessing enhancements module
  175. int max_perf;
  176. int blocking_mode; //Set if running in blocking mode
  177. sem_t pollthread_sema; // Polling thread waits on this to be signalled to issue Poll
  178. sem_t encoderthread_sema; // Encoder thread waits on this to be signalled to continue q/dq loop
  179. pthread_t enc_pollthread; // Polling thread, created if running in non-blocking mode.
  180. pthread_t enc_capture_loop; // Encoder capture thread
  181. } context_enc_t;
  182. class ISVCEncoder;
  183. namespace webrtc {
  184. class JetH264Encoder : public H264Encoder {
  185. public:
  186. struct LayerConfig {
  187. int simulcast_idx = 0;
  188. int width = -1;
  189. int height = -1;
  190. bool sending = true;
  191. bool key_frame_request = false;
  192. float max_frame_rate = 0;
  193. uint32_t target_bps = 0;
  194. uint32_t max_bps = 0;
  195. bool frame_dropping_on = false;
  196. int key_frame_interval = 0;
  197. int num_temporal_layers = 1;
  198. void SetStreamState(bool send_stream);
  199. };
  200. public:
  201. explicit JetH264Encoder(const cricket::VideoCodec& codec);
  202. ~JetH264Encoder() override;
  203. // |settings.max_payload_size| is ignored.
  204. // The following members of |codec_settings| are used. The rest are ignored.
  205. // - codecType (must be kVideoCodecH264)
  206. // - targetBitrate
  207. // - maxFramerate
  208. // - width
  209. // - height
  210. int32_t InitEncode(const VideoCodec* codec_settings,
  211. const VideoEncoder::Settings& settings) override;
  212. int32_t Release() override;
  213. int32_t RegisterEncodeCompleteCallback(
  214. EncodedImageCallback* callback) override;
  215. void SetRates(const RateControlParameters& parameters) override;
  216. // The result of encoding - an EncodedImage and CodecSpecificInfo - are
  217. // passed to the encode complete callback.
  218. int32_t Encode(const VideoFrame& frame,
  219. const std::vector<VideoFrameType>* frame_types) override;
  220. EncoderInfo GetEncoderInfo() const override;
  221. // Exposed for testing.
  222. H264PacketizationMode PacketizationModeForTesting() const {
  223. return packetization_mode_;
  224. }
  225. private:
  226. bool OpenEncoder(context_enc_t *ctx, LayerConfig &io_param); //增加
  227. // static void set_defaults(context_enc_t * ctx); //增加
  228. SEncParamExt CreateEncoderParams(size_t i) const;
  229. webrtc::H264BitstreamParser h264_bitstream_parser_;
  230. // Reports statistics with histograms.
  231. void ReportInit();
  232. void ReportError();
  233. // void copyFrame(AVFrame *frame, const webrtc::I420BufferInterface *buffer);
  234. std::vector<context_enc_t*> encoders_; //jetson encoder 实例
  235. std::vector<SSourcePicture> pictures_;
  236. std::vector<rtc::scoped_refptr<I420Buffer>> downscaled_buffers_;
  237. std::vector<LayerConfig> configurations_;
  238. std::vector<EncodedImage> encoded_images_;
  239. VideoCodec codec_;
  240. H264PacketizationMode packetization_mode_;
  241. size_t max_payload_size_;
  242. int32_t number_of_cores_;
  243. EncodedImageCallback* encoded_image_callback_;
  244. bool has_reported_init_;
  245. bool has_reported_error_;
  246. std::vector<uint8_t> tl0sync_limit_;
  247. void CloseEncoder(context_enc_t *ctx); //增加
  248. };
  249. } // namespace webrtc
  250. #endif // JETSON_H264_ENCODE_H_