jetson_nv_encoder.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_NV_ENCODER_H_
  12. #define JETSON_NV_ENCODER_H_
  13. #include <chrono>
  14. #include <memory>
  15. #include <queue>
  16. // Linux
  17. #include <linux/videodev2.h>
  18. // WebRTC
  19. #include <api/video_codecs/video_encoder.h>
  20. #include <common_video/include/bitrate_adjuster.h>
  21. #include <media/base/codec.h>
  22. #include <modules/video_coding/codecs/h264/include/h264.h>
  23. #include <modules/video_coding/codecs/vp9/include/vp9_globals.h>
  24. #include <modules/video_coding/codecs/av1/scalable_video_controller.h>
  25. #include <rtc_base/synchronization/mutex.h>
  26. #include "jetson_jpeg_decoder.h"
  27. #include <condition_variable>
  28. #define CONVERTER_CAPTURE_NUM 2
  29. class NvBuffer;
  30. class NvV4l2Element;
  31. class NvVideoEncoder;
  32. struct v4l2_ctrl_videoenc_outputbuf_metadata_;
  33. namespace sora {
  34. class JetsonVideoEncoder : public webrtc::VideoEncoder {
  35. public:
  36. explicit JetsonVideoEncoder(const cricket::VideoCodec& codec);
  37. ~JetsonVideoEncoder() override;
  38. static bool IsSupported(webrtc::VideoCodecType codec);
  39. int32_t InitEncode(const webrtc::VideoCodec* codec_settings,
  40. int32_t number_of_cores,
  41. size_t max_payload_size) override;
  42. int32_t RegisterEncodeCompleteCallback(
  43. webrtc::EncodedImageCallback* callback) override;
  44. int32_t Release() override;
  45. int32_t Encode(
  46. const webrtc::VideoFrame& frame,
  47. const std::vector<webrtc::VideoFrameType>* frame_types) override;
  48. void SetRates(const RateControlParameters& parameters) override;
  49. webrtc::VideoEncoder::EncoderInfo GetEncoderInfo() const override;
  50. private:
  51. struct FrameParams {
  52. FrameParams(int32_t w,
  53. int32_t h,
  54. int64_t rtms,
  55. int64_t ntpms,
  56. int64_t tsus,
  57. int64_t rtpts,
  58. webrtc::VideoRotation r,
  59. absl::optional<webrtc::ColorSpace> c,
  60. std::shared_ptr<JetsonJpegDecoder> d)
  61. : width(w),
  62. height(h),
  63. render_time_ms(rtms),
  64. ntp_time_ms(ntpms),
  65. timestamp_us(tsus),
  66. timestamp_rtp(rtpts),
  67. rotation(r),
  68. color_space(c),
  69. decoder_(d) {}
  70. int32_t width;
  71. int32_t height;
  72. int64_t render_time_ms;
  73. int64_t ntp_time_ms;
  74. int64_t timestamp_us;
  75. int64_t timestamp_rtp;
  76. webrtc::VideoRotation rotation;
  77. absl::optional<webrtc::ColorSpace> color_space;
  78. std::shared_ptr<JetsonJpegDecoder> decoder_;
  79. };
  80. int32_t JetsonConfigure();
  81. void JetsonRelease();
  82. void SendEOS();
  83. static bool EncodeFinishedCallbackFunction(struct v4l2_buffer* v4l2_buf,
  84. NvBuffer* buffer,
  85. NvBuffer* shared_buffer,
  86. void* data);
  87. bool EncodeFinishedCallback(struct v4l2_buffer* v4l2_buf,
  88. NvBuffer* buffer,
  89. NvBuffer* shared_buffer);
  90. void SetFramerate(uint32_t framerate);
  91. void SetBitrateBps(uint32_t bitrate_bps);
  92. int32_t SendFrame(unsigned char* buffer,
  93. size_t size,
  94. std::unique_ptr<FrameParams> params,
  95. v4l2_ctrl_videoenc_outputbuf_metadata_* enc_metadata);
  96. webrtc::VideoCodec codec_;
  97. webrtc::EncodedImageCallback* callback_;
  98. NvVideoEncoder* encoder_;
  99. std::unique_ptr<webrtc::BitrateAdjuster> bitrate_adjuster_;
  100. uint32_t framerate_;
  101. int32_t configured_framerate_;
  102. uint32_t target_bitrate_bps_;
  103. uint32_t configured_bitrate_bps_;
  104. int key_frame_interval_;
  105. uint32_t decode_pixfmt_;
  106. uint32_t raw_width_;
  107. uint32_t raw_height_;
  108. int32_t width_;
  109. int32_t height_;
  110. bool use_native_;
  111. bool use_dmabuff_;
  112. int dmabuff_fd_[CONVERTER_CAPTURE_NUM];
  113. webrtc::GofInfoVP9 gof_;
  114. size_t gof_idx_;
  115. std::unique_ptr<webrtc::ScalableVideoController> svc_controller_;
  116. webrtc::Mutex frame_params_lock_;
  117. std::queue<std::unique_ptr<FrameParams>> frame_params_;
  118. std::mutex enc0_buffer_mtx_;
  119. std::condition_variable enc0_buffer_cond_;
  120. std::queue<NvBuffer*>* enc0_buffer_queue_;
  121. int output_plane_fd_[32];
  122. webrtc::EncodedImage encoded_image_;
  123. // webrtc::ScalabilityMode scalability_mode_;
  124. std::vector<uint8_t> obu_seq_header_;
  125. };
  126. } // namespace sora
  127. #endif //JETSON_NV_ENCODER_H_