NvCodec.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <cuda.h>
  3. #include <vector>
  4. #include <api/video_codecs/h264_profile_level_id.h>
  5. #include <api/video_codecs/sdp_video_format.h>
  6. #include <api/video_codecs/video_decoder.h>
  7. #include <api/video_codecs/video_decoder_factory.h>
  8. #include <api/video_codecs/video_encoder.h>
  9. #include <api/video_codecs/video_encoder_factory.h>
  10. #include <media/base/codec.h>
  11. #include "nvEncodeAPI.h"
  12. namespace unity
  13. {
  14. namespace webrtc
  15. {
  16. using namespace ::webrtc;
  17. int SupportedEncoderCount(CUcontext context);
  18. H264Level SupportedMaxH264Level(CUcontext context);
  19. std::vector<SdpVideoFormat> SupportedNvEncoderCodecs(CUcontext context);
  20. std::vector<SdpVideoFormat> SupportedNvDecoderCodecs(CUcontext context);
  21. class ProfilerMarkerFactory;
  22. class NvEncoder : public VideoEncoder
  23. {
  24. public:
  25. static std::unique_ptr<NvEncoder> Create(
  26. const cricket::VideoCodec& codec,
  27. CUcontext context,
  28. CUmemorytype memoryType,
  29. NV_ENC_BUFFER_FORMAT format,
  30. ProfilerMarkerFactory* profiler);
  31. static bool IsSupported();
  32. ~NvEncoder() override { }
  33. };
  34. class NvDecoder : public VideoDecoder
  35. {
  36. public:
  37. static std::unique_ptr<NvDecoder>
  38. Create(const cricket::VideoCodec& codec, CUcontext context, ProfilerMarkerFactory* profiler);
  39. static bool IsSupported();
  40. ~NvDecoder() override { }
  41. };
  42. class NvEncoderFactory : public VideoEncoderFactory
  43. {
  44. public:
  45. NvEncoderFactory(CUcontext context, NV_ENC_BUFFER_FORMAT format, ProfilerMarkerFactory* profiler);
  46. ~NvEncoderFactory() override;
  47. std::vector<SdpVideoFormat> GetSupportedFormats() const override;
  48. VideoEncoderFactory::CodecInfo QueryVideoEncoder(const SdpVideoFormat& format) const override;
  49. std::unique_ptr<VideoEncoder> CreateVideoEncoder(const SdpVideoFormat& format) override;
  50. private:
  51. CUcontext context_;
  52. NV_ENC_BUFFER_FORMAT format_;
  53. ProfilerMarkerFactory* profiler_;
  54. // Cache of capability to reduce calling SessionOpenAPI of NvEncoder
  55. std::vector<SdpVideoFormat> m_cachedSupportedFormats;
  56. };
  57. class NvDecoderFactory : public VideoDecoderFactory
  58. {
  59. public:
  60. NvDecoderFactory(CUcontext context, ProfilerMarkerFactory* profiler);
  61. ~NvDecoderFactory() override;
  62. std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override;
  63. std::unique_ptr<webrtc::VideoDecoder> CreateVideoDecoder(const webrtc::SdpVideoFormat& format) override;
  64. private:
  65. CUcontext context_;
  66. ProfilerMarkerFactory* profiler_;
  67. };
  68. #ifndef _WIN32
  69. static bool operator==(const GUID& a, const GUID& b) { return !std::memcmp(&a, &b, sizeof(GUID)); }
  70. #endif
  71. // todo(kazuki):: fix workaround
  72. #pragma clang diagnostic push
  73. #pragma clang diagnostic ignored "-Wunused-function"
  74. static absl::optional<H264Profile> GuidToProfile(GUID& guid)
  75. {
  76. if (guid == NV_ENC_H264_PROFILE_BASELINE_GUID)
  77. return H264Profile::kProfileBaseline;
  78. if (guid == NV_ENC_H264_PROFILE_MAIN_GUID)
  79. return H264Profile::kProfileMain;
  80. if (guid == NV_ENC_H264_PROFILE_HIGH_GUID)
  81. return H264Profile::kProfileHigh;
  82. if (guid == NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID)
  83. return H264Profile::kProfileConstrainedHigh;
  84. return absl::nullopt;
  85. }
  86. static absl::optional<GUID> ProfileToGuid(H264Profile profile)
  87. {
  88. if (profile == H264Profile::kProfileBaseline)
  89. return NV_ENC_H264_PROFILE_BASELINE_GUID;
  90. // Returns Baseline Profile instead because NVCodec is not supported Constrained Baseline Profile
  91. // officially, but WebRTC use the profile in default.
  92. if (profile == H264Profile::kProfileConstrainedBaseline)
  93. return NV_ENC_H264_PROFILE_BASELINE_GUID;
  94. if (profile == H264Profile::kProfileMain)
  95. return NV_ENC_H264_PROFILE_MAIN_GUID;
  96. if (profile == H264Profile::kProfileHigh)
  97. return NV_ENC_H264_PROFILE_HIGH_GUID;
  98. if (profile == H264Profile::kProfileConstrainedHigh)
  99. return NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID;
  100. return absl::nullopt;
  101. }
  102. #pragma clang diagnostic pop
  103. }
  104. }