You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.8KB

  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 "compat/nvenc/nvEncodeAPI.h"
  21. #include "config.h"
  22. #include "libavutil/fifo.h"
  23. #include "libavutil/opt.h"
  24. #include "avcodec.h"
  25. #if CONFIG_CUDA
  26. #include "libavutil/hwcontext_cuda.h"
  27. #else
  28. #if defined(_WIN32)
  29. #define CUDAAPI __stdcall
  30. #else
  31. #define CUDAAPI
  32. #endif
  33. typedef enum cudaError_enum {
  34. CUDA_SUCCESS = 0
  35. } CUresult;
  36. typedef int CUdevice;
  37. typedef void* CUcontext;
  38. typedef void* CUdeviceptr;
  39. #endif
  40. #define MAX_REGISTERED_FRAMES 64
  41. typedef struct NvencSurface
  42. {
  43. NV_ENC_INPUT_PTR input_surface;
  44. AVFrame *in_ref;
  45. NV_ENC_MAP_INPUT_RESOURCE in_map;
  46. int reg_idx;
  47. int width;
  48. int height;
  49. NV_ENC_OUTPUT_PTR output_surface;
  50. NV_ENC_BUFFER_FORMAT format;
  51. int size;
  52. int lockCount;
  53. } NvencSurface;
  54. typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
  55. typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
  56. typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
  57. typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
  58. typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
  59. typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
  60. typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
  61. typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
  62. typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPIGETMAXSUPPORTEDVERSION)(uint32_t* version);
  63. typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
  64. typedef struct NvencDynLoadFunctions
  65. {
  66. #if !CONFIG_CUDA
  67. void *cuda;
  68. #endif
  69. void *nvenc;
  70. PCUINIT cu_init;
  71. PCUDEVICEGETCOUNT cu_device_get_count;
  72. PCUDEVICEGET cu_device_get;
  73. PCUDEVICEGETNAME cu_device_get_name;
  74. PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
  75. PCUCTXCREATE cu_ctx_create;
  76. PCUCTXPOPCURRENT cu_ctx_pop_current;
  77. PCUCTXDESTROY cu_ctx_destroy;
  78. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  79. int nvenc_device_count;
  80. } NvencDynLoadFunctions;
  81. enum {
  82. PRESET_DEFAULT = 0,
  83. PRESET_SLOW,
  84. PRESET_MEDIUM,
  85. PRESET_FAST,
  86. PRESET_HP,
  87. PRESET_HQ,
  88. PRESET_BD ,
  89. PRESET_LOW_LATENCY_DEFAULT ,
  90. PRESET_LOW_LATENCY_HQ ,
  91. PRESET_LOW_LATENCY_HP,
  92. PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
  93. PRESET_LOSSLESS_HP,
  94. };
  95. enum {
  96. NV_ENC_H264_PROFILE_BASELINE,
  97. NV_ENC_H264_PROFILE_MAIN,
  98. NV_ENC_H264_PROFILE_HIGH,
  99. NV_ENC_H264_PROFILE_HIGH_444P,
  100. };
  101. enum {
  102. NV_ENC_HEVC_PROFILE_MAIN,
  103. NV_ENC_HEVC_PROFILE_MAIN_10,
  104. };
  105. enum {
  106. NVENC_LOWLATENCY = 1,
  107. NVENC_LOSSLESS = 2,
  108. NVENC_ONE_PASS = 4,
  109. NVENC_TWO_PASSES = 8,
  110. };
  111. enum {
  112. LIST_DEVICES = -2,
  113. ANY_DEVICE,
  114. };
  115. typedef struct NvencContext
  116. {
  117. AVClass *avclass;
  118. NvencDynLoadFunctions nvenc_dload_funcs;
  119. NV_ENC_INITIALIZE_PARAMS init_encode_params;
  120. NV_ENC_CONFIG encode_config;
  121. CUcontext cu_context;
  122. CUcontext cu_context_internal;
  123. int nb_surfaces;
  124. NvencSurface *surfaces;
  125. AVFifoBuffer *output_surface_queue;
  126. AVFifoBuffer *output_surface_ready_queue;
  127. AVFifoBuffer *timestamp_list;
  128. struct {
  129. CUdeviceptr ptr;
  130. NV_ENC_REGISTERED_PTR regptr;
  131. int mapped;
  132. } registered_frames[MAX_REGISTERED_FRAMES];
  133. int nb_registered_frames;
  134. /* the actual data pixel format, different from
  135. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  136. enum AVPixelFormat data_pix_fmt;
  137. /* timestamps of the first two frames, for computing the first dts
  138. * when B-frames are present */
  139. int64_t initial_pts[2];
  140. int first_packet_output;
  141. void *nvencoder;
  142. int preset;
  143. int profile;
  144. int level;
  145. int tier;
  146. int rc;
  147. int cbr;
  148. int twopass;
  149. int device;
  150. int flags;
  151. int async_depth;
  152. int rc_lookahead;
  153. } NvencContext;
  154. int ff_nvenc_encode_init(AVCodecContext *avctx);
  155. int ff_nvenc_encode_close(AVCodecContext *avctx);
  156. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  157. const AVFrame *frame, int *got_packet);
  158. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  159. #endif /* AVCODEC_NVENC_H */