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.

160 lines
4.0KB

  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. typedef struct NvencContext
  90. {
  91. AVClass *avclass;
  92. NvencDynLoadFunctions nvenc_dload_funcs;
  93. NV_ENC_INITIALIZE_PARAMS init_encode_params;
  94. NV_ENC_CONFIG encode_config;
  95. CUcontext cu_context;
  96. CUcontext cu_context_internal;
  97. int max_surface_count;
  98. NvencSurface *surfaces;
  99. AVFifoBuffer *output_surface_queue;
  100. AVFifoBuffer *output_surface_ready_queue;
  101. AVFifoBuffer *timestamp_list;
  102. struct {
  103. CUdeviceptr ptr;
  104. NV_ENC_REGISTERED_PTR regptr;
  105. int mapped;
  106. } registered_frames[MAX_REGISTERED_FRAMES];
  107. int nb_registered_frames;
  108. /* the actual data pixel format, different from
  109. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  110. enum AVPixelFormat data_pix_fmt;
  111. int64_t last_dts;
  112. void *nvencoder;
  113. char *preset;
  114. char *profile;
  115. char *level;
  116. char *tier;
  117. int cbr;
  118. int twopass;
  119. int gpu;
  120. int buffer_delay;
  121. } NvencContext;
  122. int ff_nvenc_encode_init(AVCodecContext *avctx);
  123. int ff_nvenc_encode_close(AVCodecContext *avctx);
  124. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  125. const AVFrame *frame, int *got_packet);
  126. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  127. #endif /* AVCODEC_NVENC_H */