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.

201 lines
4.9KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 NVENCFrame {
  42. NV_ENC_INPUT_PTR in;
  43. AVFrame *in_ref;
  44. NV_ENC_MAP_INPUT_RESOURCE in_map;
  45. int reg_idx;
  46. NV_ENC_OUTPUT_PTR out;
  47. NV_ENC_BUFFER_FORMAT format;
  48. int locked;
  49. } NVENCFrame;
  50. typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
  51. typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
  52. typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
  53. typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
  54. typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
  55. typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
  56. typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
  57. typedef CUresult(CUDAAPI *PCUCTXPUSHCURRENT)(CUcontext ctx);
  58. typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
  59. typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
  60. typedef struct NVENCLibraryContext
  61. {
  62. #if !CONFIG_CUDA
  63. void *cuda;
  64. #endif
  65. void *nvenc;
  66. PCUINIT cu_init;
  67. PCUDEVICEGETCOUNT cu_device_get_count;
  68. PCUDEVICEGET cu_device_get;
  69. PCUDEVICEGETNAME cu_device_get_name;
  70. PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
  71. PCUCTXCREATE cu_ctx_create;
  72. PCUCTXPOPCURRENT cu_ctx_pop_current;
  73. PCUCTXPUSHCURRENT cu_ctx_push_current;
  74. PCUCTXDESTROY cu_ctx_destroy;
  75. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  76. } NVENCLibraryContext;
  77. enum {
  78. PRESET_DEFAULT,
  79. PRESET_SLOW,
  80. PRESET_MEDIUM,
  81. PRESET_FAST,
  82. PRESET_HP,
  83. PRESET_HQ,
  84. PRESET_BD ,
  85. PRESET_LOW_LATENCY_DEFAULT ,
  86. PRESET_LOW_LATENCY_HQ ,
  87. PRESET_LOW_LATENCY_HP,
  88. PRESET_LOSSLESS_DEFAULT,
  89. PRESET_LOSSLESS_HP,
  90. };
  91. enum {
  92. NV_ENC_H264_PROFILE_BASELINE,
  93. NV_ENC_H264_PROFILE_MAIN,
  94. NV_ENC_H264_PROFILE_HIGH,
  95. NV_ENC_H264_PROFILE_HIGH_444,
  96. NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
  97. };
  98. enum {
  99. NV_ENC_HEVC_PROFILE_MAIN,
  100. NV_ENC_HEVC_PROFILE_MAIN_10,
  101. NV_ENC_HEVC_PROFILE_REXT,
  102. };
  103. enum {
  104. NVENC_LOWLATENCY = 1,
  105. NVENC_LOSSLESS = 2,
  106. NVENC_ONE_PASS = 4,
  107. NVENC_TWO_PASSES = 8,
  108. };
  109. enum {
  110. LIST_DEVICES = -2,
  111. ANY_DEVICE,
  112. };
  113. typedef struct NVENCContext {
  114. AVClass *class;
  115. NVENCLibraryContext nvel;
  116. NV_ENC_INITIALIZE_PARAMS params;
  117. NV_ENC_CONFIG config;
  118. CUcontext cu_context;
  119. CUcontext cu_context_internal;
  120. int nb_surfaces;
  121. NVENCFrame *frames;
  122. AVFifoBuffer *timestamps;
  123. AVFifoBuffer *pending, *ready;
  124. struct {
  125. CUdeviceptr ptr;
  126. NV_ENC_REGISTERED_PTR regptr;
  127. int mapped;
  128. } registered_frames[MAX_REGISTERED_FRAMES];
  129. int nb_registered_frames;
  130. /* the actual data pixel format, different from
  131. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  132. enum AVPixelFormat data_pix_fmt;
  133. /* timestamps of the first two frames, for computing the first dts
  134. * when B-frames are present */
  135. int64_t initial_pts[2];
  136. int first_packet_output;
  137. void *nvenc_ctx;
  138. int preset;
  139. int profile;
  140. int level;
  141. int tier;
  142. int rc;
  143. int device;
  144. int flags;
  145. int async_depth;
  146. int rc_lookahead;
  147. int aq;
  148. int no_scenecut;
  149. int b_adapt;
  150. int temporal_aq;
  151. int zerolatency;
  152. int nonref_p;
  153. int strict_gop;
  154. int aq_strength;
  155. int quality;
  156. int init_qp_p;
  157. int init_qp_b;
  158. int init_qp_i;
  159. } NVENCContext;
  160. int ff_nvenc_encode_init(AVCodecContext *avctx);
  161. int ff_nvenc_encode_close(AVCodecContext *avctx);
  162. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  163. const AVFrame *frame, int *got_packet);
  164. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  165. #endif /* AVCODEC_NVENC_H */