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.

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