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.

175 lines
4.3KB

  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 *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. PCUCTXDESTROY cu_ctx_destroy;
  73. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  74. } NVENCLibraryContext;
  75. enum {
  76. PRESET_DEFAULT,
  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,
  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_444,
  91. NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
  92. };
  93. enum {
  94. NVENC_LOWLATENCY = 1,
  95. NVENC_LOSSLESS,
  96. };
  97. enum {
  98. LIST_DEVICES = -2,
  99. ANY_DEVICE,
  100. };
  101. typedef struct NVENCContext {
  102. AVClass *class;
  103. NVENCLibraryContext nvel;
  104. NV_ENC_INITIALIZE_PARAMS params;
  105. NV_ENC_CONFIG config;
  106. CUcontext cu_context;
  107. CUcontext cu_context_internal;
  108. int nb_surfaces;
  109. NVENCFrame *frames;
  110. AVFifoBuffer *timestamps;
  111. AVFifoBuffer *pending, *ready;
  112. struct {
  113. CUdeviceptr ptr;
  114. NV_ENC_REGISTERED_PTR regptr;
  115. int mapped;
  116. } registered_frames[MAX_REGISTERED_FRAMES];
  117. int nb_registered_frames;
  118. /* the actual data pixel format, different from
  119. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  120. enum AVPixelFormat data_pix_fmt;
  121. /* timestamps of the first two frames, for computing the first dts
  122. * when B-frames are present */
  123. int64_t initial_pts[2];
  124. int first_packet_output;
  125. void *nvenc_ctx;
  126. int preset;
  127. int profile;
  128. int level;
  129. int tier;
  130. int rc;
  131. int device;
  132. int flags;
  133. int async_depth;
  134. } NVENCContext;
  135. int ff_nvenc_encode_init(AVCodecContext *avctx);
  136. int ff_nvenc_encode_close(AVCodecContext *avctx);
  137. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  138. const AVFrame *frame, int *got_packet);
  139. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  140. #endif /* AVCODEC_NVENC_H */