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.

190 lines
4.6KB

  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 <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 <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 *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
  63. typedef struct NvencDynLoadFunctions
  64. {
  65. #if !CONFIG_CUDA
  66. #if defined(_WIN32)
  67. HMODULE cuda_lib;
  68. #else
  69. void* cuda_lib;
  70. #endif
  71. #endif
  72. #if defined(_WIN32)
  73. HMODULE nvenc_lib;
  74. #else
  75. void* nvenc_lib;
  76. #endif
  77. PCUINIT cu_init;
  78. PCUDEVICEGETCOUNT cu_device_get_count;
  79. PCUDEVICEGET cu_device_get;
  80. PCUDEVICEGETNAME cu_device_get_name;
  81. PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
  82. PCUCTXCREATE cu_ctx_create;
  83. PCUCTXPOPCURRENT cu_ctx_pop_current;
  84. PCUCTXDESTROY cu_ctx_destroy;
  85. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  86. int nvenc_device_count;
  87. CUdevice nvenc_devices[16];
  88. } NvencDynLoadFunctions;
  89. enum {
  90. PRESET_DEFAULT = 0,
  91. PRESET_SLOW,
  92. PRESET_MEDIUM,
  93. PRESET_FAST,
  94. PRESET_HP,
  95. PRESET_HQ,
  96. PRESET_BD ,
  97. PRESET_LOW_LATENCY_DEFAULT ,
  98. PRESET_LOW_LATENCY_HQ ,
  99. PRESET_LOW_LATENCY_HP,
  100. PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
  101. PRESET_LOSSLESS_HP,
  102. };
  103. enum {
  104. NV_ENC_H264_PROFILE_BASELINE,
  105. NV_ENC_H264_PROFILE_MAIN,
  106. NV_ENC_H264_PROFILE_HIGH,
  107. NV_ENC_H264_PROFILE_HIGH_444P,
  108. };
  109. enum {
  110. NVENC_LOWLATENCY = 1,
  111. NVENC_LOSSLESS = 2,
  112. NVENC_ONE_PASS = 4,
  113. NVENC_TWO_PASSES = 8,
  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 max_surface_count;
  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. int64_t last_dts;
  138. void *nvencoder;
  139. int preset;
  140. int profile;
  141. int level;
  142. char *tier;
  143. int cbr;
  144. int twopass;
  145. int gpu;
  146. int flags;
  147. int buffer_delay;
  148. } NvencContext;
  149. int ff_nvenc_encode_init(AVCodecContext *avctx);
  150. int ff_nvenc_encode_close(AVCodecContext *avctx);
  151. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  152. const AVFrame *frame, int *got_packet);
  153. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  154. #endif /* AVCODEC_NVENC_H */