nvenc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. typedef struct NvencSurface
  54. {
  55. NV_ENC_INPUT_PTR input_surface;
  56. AVFrame *in_ref;
  57. int reg_idx;
  58. int width;
  59. int height;
  60. int pitch;
  61. NV_ENC_OUTPUT_PTR output_surface;
  62. NV_ENC_BUFFER_FORMAT format;
  63. int size;
  64. } NvencSurface;
  65. typedef struct NvencDynLoadFunctions
  66. {
  67. CudaFunctions *cuda_dl;
  68. NvencFunctions *nvenc_dl;
  69. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  70. int nvenc_device_count;
  71. } NvencDynLoadFunctions;
  72. enum {
  73. PRESET_DEFAULT = 0,
  74. PRESET_SLOW,
  75. PRESET_MEDIUM,
  76. PRESET_FAST,
  77. PRESET_HP,
  78. PRESET_HQ,
  79. PRESET_BD ,
  80. PRESET_LOW_LATENCY_DEFAULT ,
  81. PRESET_LOW_LATENCY_HQ ,
  82. PRESET_LOW_LATENCY_HP,
  83. PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
  84. PRESET_LOSSLESS_HP,
  85. };
  86. enum {
  87. NV_ENC_H264_PROFILE_BASELINE,
  88. NV_ENC_H264_PROFILE_MAIN,
  89. NV_ENC_H264_PROFILE_HIGH,
  90. NV_ENC_H264_PROFILE_HIGH_444P,
  91. };
  92. enum {
  93. NV_ENC_HEVC_PROFILE_MAIN,
  94. NV_ENC_HEVC_PROFILE_MAIN_10,
  95. NV_ENC_HEVC_PROFILE_REXT,
  96. };
  97. enum {
  98. NVENC_LOWLATENCY = 1,
  99. NVENC_LOSSLESS = 2,
  100. NVENC_ONE_PASS = 4,
  101. NVENC_TWO_PASSES = 8,
  102. };
  103. enum {
  104. LIST_DEVICES = -2,
  105. ANY_DEVICE,
  106. };
  107. typedef struct NvencContext
  108. {
  109. AVClass *avclass;
  110. NvencDynLoadFunctions nvenc_dload_funcs;
  111. NV_ENC_INITIALIZE_PARAMS init_encode_params;
  112. NV_ENC_CONFIG encode_config;
  113. CUcontext cu_context;
  114. CUcontext cu_context_internal;
  115. CUstream cu_stream;
  116. ID3D11Device *d3d11_device;
  117. int nb_surfaces;
  118. NvencSurface *surfaces;
  119. AVFifoBuffer *unused_surface_queue;
  120. AVFifoBuffer *output_surface_queue;
  121. AVFifoBuffer *output_surface_ready_queue;
  122. AVFifoBuffer *timestamp_list;
  123. int encoder_flushing;
  124. struct {
  125. void *ptr;
  126. int ptr_index;
  127. NV_ENC_REGISTERED_PTR regptr;
  128. int mapped;
  129. NV_ENC_MAP_INPUT_RESOURCE in_map;
  130. } registered_frames[MAX_REGISTERED_FRAMES];
  131. int nb_registered_frames;
  132. /* the actual data pixel format, different from
  133. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  134. enum AVPixelFormat data_pix_fmt;
  135. /* timestamps of the first two frames, for computing the first dts
  136. * when B-frames are present */
  137. int64_t initial_pts[2];
  138. int first_packet_output;
  139. int support_dyn_bitrate;
  140. void *nvencoder;
  141. int preset;
  142. int profile;
  143. int level;
  144. int tier;
  145. int rc;
  146. int cbr;
  147. int twopass;
  148. int device;
  149. int flags;
  150. int async_depth;
  151. int rc_lookahead;
  152. int aq;
  153. int no_scenecut;
  154. int forced_idr;
  155. int b_adapt;
  156. int temporal_aq;
  157. int zerolatency;
  158. int nonref_p;
  159. int strict_gop;
  160. int aq_strength;
  161. float quality;
  162. int aud;
  163. int bluray_compat;
  164. int init_qp_p;
  165. int init_qp_b;
  166. int init_qp_i;
  167. int cqp;
  168. int weighted_pred;
  169. int coder;
  170. int b_ref_mode;
  171. int a53_cc;
  172. int dpb_size;
  173. } NvencContext;
  174. int ff_nvenc_encode_init(AVCodecContext *avctx);
  175. int ff_nvenc_encode_close(AVCodecContext *avctx);
  176. int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame);
  177. int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
  178. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  179. const AVFrame *frame, int *got_packet);
  180. void ff_nvenc_encode_flush(AVCodecContext *avctx);
  181. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  182. extern const AVCodecHWConfigInternal *ff_nvenc_hw_configs[];
  183. #endif /* AVCODEC_NVENC_H */