nvenc-internal.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include "cuda-helpers.h"
  3. #include "nvenc-helpers.h"
  4. #include <util/deque.h>
  5. #include <opts-parser.h>
  6. #ifdef _WIN32
  7. #define INITGUID
  8. #include <dxgi.h>
  9. #include <d3d11.h>
  10. #include <d3d11_1.h>
  11. #else
  12. #include <glad/glad.h>
  13. #endif
  14. #define do_log(level, format, ...) \
  15. blog(level, "[obs-nvenc: '%s'] " format, obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  16. #define error(format, ...) do_log(LOG_ERROR, format, ##__VA_ARGS__)
  17. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  18. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  19. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  20. #define error_hr(msg) error("%s: %s: 0x%08lX", __FUNCTION__, msg, (uint32_t)hr);
  21. #define NV_FAIL(format, ...) nv_fail(enc->encoder, format, ##__VA_ARGS__)
  22. #define NV_FAILED(x) nv_failed(enc->encoder, x, __FUNCTION__, #x)
  23. /* ------------------------------------------------------------------------- */
  24. /* Main Implementation Structure */
  25. struct nvenc_properties {
  26. int64_t bitrate;
  27. int64_t max_bitrate;
  28. int64_t keyint_sec;
  29. int64_t cqp;
  30. int64_t device;
  31. int64_t bf;
  32. int64_t bframe_ref_mode;
  33. int64_t split_encode;
  34. int64_t target_quality;
  35. const char *rate_control;
  36. const char *preset;
  37. const char *profile;
  38. const char *tune;
  39. const char *multipass;
  40. const char *opts_str;
  41. bool adaptive_quantization;
  42. bool lookahead;
  43. bool disable_scenecut;
  44. bool repeat_headers;
  45. bool force_cuda_tex;
  46. struct obs_options opts;
  47. obs_data_t *data;
  48. };
  49. struct nvenc_data {
  50. obs_encoder_t *encoder;
  51. enum codec_type codec;
  52. GUID codec_guid;
  53. void *session;
  54. NV_ENC_INITIALIZE_PARAMS params;
  55. NV_ENC_CONFIG config;
  56. uint32_t buf_count;
  57. int output_delay;
  58. int buffers_queued;
  59. size_t next_bitstream;
  60. size_t cur_bitstream;
  61. bool encode_started;
  62. bool first_packet;
  63. bool can_change_bitrate;
  64. bool non_texture;
  65. DARRAY(struct handle_tex) input_textures;
  66. DARRAY(struct nv_bitstream) bitstreams;
  67. DARRAY(struct nv_cuda_surface) surfaces;
  68. NV_ENC_BUFFER_FORMAT surface_format;
  69. struct deque dts_list;
  70. DARRAY(uint8_t) packet_data;
  71. int64_t packet_pts;
  72. bool packet_keyframe;
  73. #ifdef _WIN32
  74. DARRAY(struct nv_texture) textures;
  75. ID3D11Device *device;
  76. ID3D11DeviceContext *context;
  77. #endif
  78. uint32_t cx;
  79. uint32_t cy;
  80. enum video_format in_format;
  81. uint8_t *header;
  82. size_t header_size;
  83. uint8_t *sei;
  84. size_t sei_size;
  85. int8_t *roi_map;
  86. size_t roi_map_size;
  87. uint32_t roi_increment;
  88. struct nvenc_properties props;
  89. CUcontext cu_ctx;
  90. };
  91. /* ------------------------------------------------------------------------- */
  92. /* Resource data structures */
  93. /* Input texture handles */
  94. struct handle_tex {
  95. #ifdef _WIN32
  96. uint32_t handle;
  97. ID3D11Texture2D *tex;
  98. IDXGIKeyedMutex *km;
  99. #else
  100. GLuint tex_id;
  101. /* CUDA mappings */
  102. CUgraphicsResource res_y;
  103. CUgraphicsResource res_uv;
  104. #endif
  105. };
  106. /* Bitstream buffer */
  107. struct nv_bitstream {
  108. void *ptr;
  109. };
  110. /** Mapped resources **/
  111. /* CUDA Arrays */
  112. struct nv_cuda_surface {
  113. CUarray tex;
  114. NV_ENC_REGISTERED_PTR res;
  115. NV_ENC_INPUT_PTR *mapped_res;
  116. };
  117. #ifdef _WIN32
  118. /* DX11 textures */
  119. struct nv_texture {
  120. void *res;
  121. ID3D11Texture2D *tex;
  122. void *mapped_res;
  123. };
  124. #endif
  125. /* ------------------------------------------------------------------------- */
  126. /* Shared functions */
  127. bool nvenc_encode_base(struct nvenc_data *enc, struct nv_bitstream *bs, void *pic, int64_t pts,
  128. struct encoder_packet *packet, bool *received_packet);
  129. /* ------------------------------------------------------------------------- */
  130. /* Backend-specific functions */
  131. #ifdef _WIN32
  132. /** D3D11 **/
  133. bool d3d11_init(struct nvenc_data *enc, obs_data_t *settings);
  134. void d3d11_free(struct nvenc_data *enc);
  135. bool d3d11_init_textures(struct nvenc_data *enc);
  136. void d3d11_free_textures(struct nvenc_data *enc);
  137. bool d3d11_encode(void *data, struct encoder_texture *texture, int64_t pts, uint64_t lock_key, uint64_t *next_key,
  138. struct encoder_packet *packet, bool *received_packet);
  139. #endif
  140. /** CUDA **/
  141. bool cuda_ctx_init(struct nvenc_data *enc, obs_data_t *settings, bool texture);
  142. void cuda_ctx_free(struct nvenc_data *enc);
  143. bool cuda_init_surfaces(struct nvenc_data *enc);
  144. void cuda_free_surfaces(struct nvenc_data *enc);
  145. bool cuda_encode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet);
  146. #ifndef _WIN32
  147. /** CUDA OpenGL **/
  148. void cuda_opengl_free(struct nvenc_data *enc);
  149. bool cuda_opengl_encode(void *data, struct encoder_texture *tex, int64_t pts, uint64_t lock_key, uint64_t *next_key,
  150. struct encoder_packet *packet, bool *received_packet);
  151. #endif
  152. /* ------------------------------------------------------------------------- */
  153. /* Properties crap */
  154. void nvenc_properties_read(struct nvenc_properties *enc, obs_data_t *settings);
  155. void h264_nvenc_defaults(obs_data_t *settings);
  156. void hevc_nvenc_defaults(obs_data_t *settings);
  157. void av1_nvenc_defaults(obs_data_t *settings);
  158. obs_properties_t *h264_nvenc_properties(void *);
  159. obs_properties_t *hevc_nvenc_properties(void *);
  160. obs_properties_t *av1_nvenc_properties(void *);
  161. /* Custom argument parsing */
  162. bool apply_user_args(struct nvenc_data *enc);
  163. bool get_user_arg_int(struct nvenc_data *enc, const char *name, int *val);