1
0

nvenc-internal.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. int packet_priority;
  74. #ifdef _WIN32
  75. DARRAY(struct nv_texture) textures;
  76. ID3D11Device *device;
  77. ID3D11DeviceContext *context;
  78. #endif
  79. uint32_t cx;
  80. uint32_t cy;
  81. enum video_format in_format;
  82. uint8_t *header;
  83. size_t header_size;
  84. uint8_t *sei;
  85. size_t sei_size;
  86. int8_t *roi_map;
  87. size_t roi_map_size;
  88. uint32_t roi_increment;
  89. #ifdef NVENC_13_0_OR_LATER
  90. CONTENT_LIGHT_LEVEL *cll;
  91. MASTERING_DISPLAY_INFO *mdi;
  92. #endif
  93. struct nvenc_properties props;
  94. CUcontext cu_ctx;
  95. };
  96. /* ------------------------------------------------------------------------- */
  97. /* Resource data structures */
  98. /* Input texture handles */
  99. struct handle_tex {
  100. #ifdef _WIN32
  101. uint32_t handle;
  102. ID3D11Texture2D *tex;
  103. IDXGIKeyedMutex *km;
  104. #else
  105. GLuint tex_id;
  106. /* CUDA mappings */
  107. CUgraphicsResource res_y;
  108. CUgraphicsResource res_uv;
  109. #endif
  110. };
  111. /* Bitstream buffer */
  112. struct nv_bitstream {
  113. void *ptr;
  114. };
  115. /** Mapped resources **/
  116. /* CUDA Arrays */
  117. struct nv_cuda_surface {
  118. CUarray tex;
  119. NV_ENC_REGISTERED_PTR res;
  120. NV_ENC_INPUT_PTR *mapped_res;
  121. };
  122. #ifdef _WIN32
  123. /* DX11 textures */
  124. struct nv_texture {
  125. void *res;
  126. ID3D11Texture2D *tex;
  127. void *mapped_res;
  128. };
  129. #endif
  130. /* ------------------------------------------------------------------------- */
  131. /* Shared functions */
  132. bool nvenc_encode_base(struct nvenc_data *enc, struct nv_bitstream *bs, void *pic, int64_t pts,
  133. struct encoder_packet *packet, bool *received_packet);
  134. /* ------------------------------------------------------------------------- */
  135. /* Backend-specific functions */
  136. #ifdef _WIN32
  137. /** D3D11 **/
  138. bool d3d11_init(struct nvenc_data *enc, obs_data_t *settings);
  139. void d3d11_free(struct nvenc_data *enc);
  140. bool d3d11_init_textures(struct nvenc_data *enc);
  141. void d3d11_free_textures(struct nvenc_data *enc);
  142. bool d3d11_encode(void *data, struct encoder_texture *texture, int64_t pts, uint64_t lock_key, uint64_t *next_key,
  143. struct encoder_packet *packet, bool *received_packet);
  144. #endif
  145. /** CUDA **/
  146. bool cuda_ctx_init(struct nvenc_data *enc, obs_data_t *settings, bool texture);
  147. void cuda_ctx_free(struct nvenc_data *enc);
  148. bool cuda_init_surfaces(struct nvenc_data *enc);
  149. void cuda_free_surfaces(struct nvenc_data *enc);
  150. bool cuda_encode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet);
  151. #ifndef _WIN32
  152. /** CUDA OpenGL **/
  153. void cuda_opengl_free(struct nvenc_data *enc);
  154. bool cuda_opengl_encode(void *data, struct encoder_texture *tex, int64_t pts, uint64_t lock_key, uint64_t *next_key,
  155. struct encoder_packet *packet, bool *received_packet);
  156. #endif
  157. /* ------------------------------------------------------------------------- */
  158. /* Properties crap */
  159. void nvenc_properties_read(struct nvenc_properties *enc, obs_data_t *settings);
  160. void h264_nvenc_defaults(obs_data_t *settings);
  161. void hevc_nvenc_defaults(obs_data_t *settings);
  162. void av1_nvenc_defaults(obs_data_t *settings);
  163. obs_properties_t *h264_nvenc_properties(void *);
  164. obs_properties_t *hevc_nvenc_properties(void *);
  165. obs_properties_t *av1_nvenc_properties(void *);
  166. /* Custom argument parsing */
  167. bool apply_user_args(struct nvenc_data *enc);
  168. bool get_user_arg_int(struct nvenc_data *enc, const char *name, int *val);