jetson_nv_encoder.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // #ifndef MODULES_NV_VIDEO_CODING_CODECS_H264_ENCODER_IMPL_H_
  2. // #define MODULES_NV_VIDEO_CODING_CODECS_H264_ENCODER_IMPL_H_
  3. // #include <memory>
  4. // #include <vector>
  5. // #include <string.h>
  6. // #include "api/video/i420_buffer.h"
  7. // #include "common_video/h264/h264_bitstream_parser.h"
  8. // #include "modules/video_coding/codecs/h264/include/h264.h"
  9. // #include "modules/video_coding/utility/quality_scaler.h"
  10. // #include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
  11. // // #include <dxgi.h>
  12. // // #include <d3d11.h>
  13. // // #include <dxgi1_2.h>
  14. // // #include "encoder/nvidia_d3d11_encoder.h"
  15. // namespace webrtc {
  16. // class NvEncoder : public VideoEncoder {
  17. // public:
  18. // struct LayerConfig
  19. // {
  20. // int simulcast_idx = 0;
  21. // int width = -1;
  22. // int height = -1;
  23. // bool sending = true;
  24. // bool key_frame_request = false;
  25. // float max_frame_rate = 0;
  26. // uint32_t target_bps = 0;
  27. // uint32_t max_bps = 0;
  28. // bool frame_dropping_on = false;
  29. // int key_frame_interval = 0;
  30. // void SetStreamState(bool send_stream);
  31. // };
  32. // public:
  33. // explicit NvEncoder(const cricket::VideoCodec& codec);
  34. // ~NvEncoder() override;
  35. // int32_t InitEncode(const VideoCodec* codec_settings,
  36. // int32_t number_of_cores,
  37. // size_t max_payload_size) override;
  38. // int32_t Release() override;
  39. // int32_t RegisterEncodeCompleteCallback(
  40. // EncodedImageCallback* callback) override;
  41. // void SetRates(const RateControlParameters& parameters) override;
  42. // // The result of encoding - an EncodedImage and RTPFragmentationHeader - are
  43. // // passed to the encode complete callback.
  44. // int32_t Encode(const VideoFrame& frame,
  45. // const std::vector<VideoFrameType>* frame_types) override;
  46. // EncoderInfo GetEncoderInfo() const override;
  47. // // Exposed for testing.
  48. // H264PacketizationMode PacketizationModeForTesting() const {
  49. // return packetization_mode_;
  50. // }
  51. // private:
  52. // webrtc::H264BitstreamParser h264_bitstream_parser_;
  53. // // Reports statistics with histograms.
  54. // void ReportInit();
  55. // void ReportError();
  56. // bool EncodeFrame(int index,const VideoFrame& input_frame,
  57. // std::vector<uint8_t>& frame_packet);
  58. // std::vector<void*> nv_encoders_;
  59. // std::vector<LayerConfig> configurations_;
  60. // std::vector<EncodedImage> encoded_images_;
  61. // VideoCodec codec_;
  62. // H264PacketizationMode packetization_mode_;
  63. // size_t max_payload_size_;
  64. // int32_t number_of_cores_;
  65. // EncodedImageCallback* encoded_image_callback_;
  66. // bool has_reported_init_;
  67. // bool has_reported_error_;
  68. // int video_format_;
  69. // int num_temporal_layers_;
  70. // uint8_t tl0sync_limit_;
  71. // std::unique_ptr<uint8_t[]> image_buffer_;
  72. // };
  73. // } // namespace webrtc
  74. // #endif // MODULES_NV_VIDEO_CODING_CODECS_H264_ENCODER_IMPL_H_
  75. /*
  76. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  77. *
  78. * Use of this source code is governed by a BSD-style license
  79. * that can be found in the LICENSE file in the root of the source
  80. * tree. An additional intellectual property rights grant can be found
  81. * in the file PATENTS. All contributing project authors may
  82. * be found in the AUTHORS file in the root of the source tree.
  83. *
  84. */
  85. #ifndef JETSON_NV_ENCODER_H_
  86. #define JETSON_NV_ENCODER_H_
  87. // Everything declared in this header is only required when WebRTC is
  88. // build with H264 support, please do not move anything out of the
  89. // #ifdef unless needed and tested.
  90. // #ifdef WEBRTC_USE_H264
  91. #if defined(WEBRTC_WIN) && !defined(__clang__)
  92. #error "See: bugs.webrtc.org/9213#c13."
  93. #endif
  94. #include <memory>
  95. #include <vector>
  96. #include "api/video/i420_buffer.h"
  97. #include "api/video_codecs/video_encoder.h"
  98. #include "common_video/h264/h264_bitstream_parser.h"
  99. #include "modules/video_coding/codecs/h264/include/h264.h"
  100. #include "modules/video_coding/utility/quality_scaler.h"
  101. #include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
  102. #include "api/video/i420_buffer.h"
  103. #include "modules/video_coding/include/video_error_codes.h"
  104. #include "modules/video_coding/h264_sprop_parameter_sets.h"
  105. class ISVCEncoder;
  106. namespace webrtc {
  107. class H264EncoderImpl_ : public H264Encoder {
  108. public:
  109. struct LayerConfig {
  110. int simulcast_idx = 0;
  111. int width = -1;
  112. int height = -1;
  113. bool sending = true;
  114. bool key_frame_request = false;
  115. float max_frame_rate = 0;
  116. uint32_t target_bps = 0;
  117. uint32_t max_bps = 0;
  118. bool frame_dropping_on = false;
  119. int key_frame_interval = 0;
  120. int num_temporal_layers = 1;
  121. void SetStreamState(bool send_stream);
  122. };
  123. public:
  124. explicit H264EncoderImpl_(const cricket::VideoCodec& codec);
  125. ~H264EncoderImpl_() override;
  126. // |settings.max_payload_size| is ignored.
  127. // The following members of |codec_settings| are used. The rest are ignored.
  128. // - codecType (must be kVideoCodecH264)
  129. // - targetBitrate
  130. // - maxFramerate
  131. // - width
  132. // - height
  133. int32_t InitEncode(const VideoCodec* codec_settings,
  134. const VideoEncoder::Settings& settings) override;
  135. int32_t Release() override;
  136. int32_t RegisterEncodeCompleteCallback(
  137. EncodedImageCallback* callback) override;
  138. void SetRates(const RateControlParameters& parameters) override;
  139. // The result of encoding - an EncodedImage and CodecSpecificInfo - are
  140. // passed to the encode complete callback.
  141. int32_t Encode(const VideoFrame& frame,
  142. const std::vector<VideoFrameType>* frame_types) override;
  143. EncoderInfo GetEncoderInfo() const override;
  144. // Exposed for testing.
  145. H264PacketizationMode PacketizationModeForTesting() const {
  146. return packetization_mode_;
  147. }
  148. private:
  149. SEncParamExt CreateEncoderParams(size_t i) const;
  150. webrtc::H264BitstreamParser h264_bitstream_parser_;
  151. // Reports statistics with histograms.
  152. void ReportInit();
  153. void ReportError();
  154. std::vector<ISVCEncoder*> encoders_;
  155. std::vector<SSourcePicture> pictures_;
  156. std::vector<rtc::scoped_refptr<I420Buffer>> downscaled_buffers_;
  157. std::vector<LayerConfig> configurations_;
  158. std::vector<EncodedImage> encoded_images_;
  159. VideoCodec codec_;
  160. H264PacketizationMode packetization_mode_;
  161. size_t max_payload_size_;
  162. int32_t number_of_cores_;
  163. EncodedImageCallback* encoded_image_callback_;
  164. bool has_reported_init_;
  165. bool has_reported_error_;
  166. std::vector<uint8_t> tl0sync_limit_;
  167. };
  168. } // namespace webrtc
  169. // #endif // WEBRTC_USE_H264
  170. #endif // JETSON_NV_ENCODER_H_