nvenc.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVCODEC_NVENC_H
  19. #define AVCODEC_NVENC_H
  20. #include "config.h"
  21. #if CONFIG_D3D11VA
  22. #define COBJMACROS
  23. #include "libavutil/hwcontext_d3d11va.h"
  24. #else
  25. typedef void ID3D11Device;
  26. #endif
  27. #include <ffnvcodec/nvEncodeAPI.h>
  28. #include "compat/cuda/dynlink_loader.h"
  29. #include "libavutil/fifo.h"
  30. #include "libavutil/opt.h"
  31. #include "hwconfig.h"
  32. #include "avcodec.h"
  33. #define MAX_REGISTERED_FRAMES 64
  34. #define RC_MODE_DEPRECATED 0x800000
  35. #define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
  36. #define NVENCAPI_CHECK_VERSION(major, minor) \
  37. ((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))
  38. // SDK 8.1 compile time feature checks
  39. #if NVENCAPI_CHECK_VERSION(8, 1)
  40. #define NVENC_HAVE_BFRAME_REF_MODE
  41. #define NVENC_HAVE_QP_MAP_MODE
  42. #endif
  43. // SDK 9.0 compile time feature checks
  44. #if NVENCAPI_CHECK_VERSION(9, 0)
  45. #define NVENC_HAVE_HEVC_BFRAME_REF_MODE
  46. #endif
  47. // SDK 9.1 compile time feature checks
  48. #if NVENCAPI_CHECK_VERSION(9, 1)
  49. #define NVENC_HAVE_MULTIPLE_REF_FRAMES
  50. #define NVENC_HAVE_CUSTREAM_PTR
  51. #define NVENC_HAVE_GETLASTERRORSTRING
  52. #endif
  53. // SDK 10.0 compile time feature checks
  54. #if NVENCAPI_CHECK_VERSION(10, 0)
  55. #define NVENC_HAVE_NEW_PRESETS
  56. #define NVENC_HAVE_MULTIPASS
  57. #define NVENC_HAVE_LDKFS
  58. #define NVENC_HAVE_H264_LVL6
  59. #endif
  60. typedef struct NvencSurface
  61. {
  62. NV_ENC_INPUT_PTR input_surface;
  63. AVFrame *in_ref;
  64. int reg_idx;
  65. int width;
  66. int height;
  67. int pitch;
  68. NV_ENC_OUTPUT_PTR output_surface;
  69. NV_ENC_BUFFER_FORMAT format;
  70. int size;
  71. } NvencSurface;
  72. typedef struct NvencDynLoadFunctions
  73. {
  74. CudaFunctions *cuda_dl;
  75. NvencFunctions *nvenc_dl;
  76. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  77. int nvenc_device_count;
  78. } NvencDynLoadFunctions;
  79. enum {
  80. PRESET_DEFAULT = 0,
  81. PRESET_SLOW,
  82. PRESET_MEDIUM,
  83. PRESET_FAST,
  84. PRESET_HP,
  85. PRESET_HQ,
  86. PRESET_BD ,
  87. PRESET_LOW_LATENCY_DEFAULT ,
  88. PRESET_LOW_LATENCY_HQ ,
  89. PRESET_LOW_LATENCY_HP,
  90. PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
  91. PRESET_LOSSLESS_HP,
  92. #ifdef NVENC_HAVE_NEW_PRESETS
  93. PRESET_P1,
  94. PRESET_P2,
  95. PRESET_P3,
  96. PRESET_P4,
  97. PRESET_P5,
  98. PRESET_P6,
  99. PRESET_P7,
  100. #endif
  101. };
  102. enum {
  103. NV_ENC_H264_PROFILE_BASELINE,
  104. NV_ENC_H264_PROFILE_MAIN,
  105. NV_ENC_H264_PROFILE_HIGH,
  106. NV_ENC_H264_PROFILE_HIGH_444P,
  107. };
  108. enum {
  109. NV_ENC_HEVC_PROFILE_MAIN,
  110. NV_ENC_HEVC_PROFILE_MAIN_10,
  111. NV_ENC_HEVC_PROFILE_REXT,
  112. };
  113. enum {
  114. NVENC_LOWLATENCY = 1,
  115. NVENC_LOSSLESS = 2,
  116. NVENC_ONE_PASS = 4,
  117. NVENC_TWO_PASSES = 8,
  118. };
  119. enum {
  120. LIST_DEVICES = -2,
  121. ANY_DEVICE,
  122. };
  123. typedef struct NvencContext
  124. {
  125. AVClass *avclass;
  126. NvencDynLoadFunctions nvenc_dload_funcs;
  127. NV_ENC_INITIALIZE_PARAMS init_encode_params;
  128. NV_ENC_CONFIG encode_config;
  129. CUcontext cu_context;
  130. CUcontext cu_context_internal;
  131. CUstream cu_stream;
  132. ID3D11Device *d3d11_device;
  133. AVFrame *frame;
  134. int nb_surfaces;
  135. NvencSurface *surfaces;
  136. AVFifoBuffer *unused_surface_queue;
  137. AVFifoBuffer *output_surface_queue;
  138. AVFifoBuffer *output_surface_ready_queue;
  139. AVFifoBuffer *timestamp_list;
  140. struct {
  141. void *ptr;
  142. int ptr_index;
  143. NV_ENC_REGISTERED_PTR regptr;
  144. int mapped;
  145. NV_ENC_MAP_INPUT_RESOURCE in_map;
  146. } registered_frames[MAX_REGISTERED_FRAMES];
  147. int nb_registered_frames;
  148. /* the actual data pixel format, different from
  149. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  150. enum AVPixelFormat data_pix_fmt;
  151. int support_dyn_bitrate;
  152. void *nvencoder;
  153. int preset;
  154. int profile;
  155. int level;
  156. int tier;
  157. int rc;
  158. int cbr;
  159. int twopass;
  160. int device;
  161. int flags;
  162. int async_depth;
  163. int rc_lookahead;
  164. int aq;
  165. int no_scenecut;
  166. int forced_idr;
  167. int b_adapt;
  168. int temporal_aq;
  169. int zerolatency;
  170. int nonref_p;
  171. int strict_gop;
  172. int aq_strength;
  173. float quality;
  174. int aud;
  175. int bluray_compat;
  176. int init_qp_p;
  177. int init_qp_b;
  178. int init_qp_i;
  179. int cqp;
  180. int weighted_pred;
  181. int coder;
  182. int b_ref_mode;
  183. int a53_cc;
  184. int s12m_tc;
  185. int dpb_size;
  186. int tuning_info;
  187. int multipass;
  188. int ldkfs;
  189. } NvencContext;
  190. int ff_nvenc_encode_init(AVCodecContext *avctx);
  191. int ff_nvenc_encode_close(AVCodecContext *avctx);
  192. int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
  193. void ff_nvenc_encode_flush(AVCodecContext *avctx);
  194. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  195. extern const AVCodecHWConfigInternal *ff_nvenc_hw_configs[];
  196. #endif /* AVCODEC_NVENC_H */