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.

157 lines
4.0KB

  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 <cuda.h>
  21. #include <nvEncodeAPI.h>
  22. #include "config.h"
  23. #include "libavutil/fifo.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #define MAX_REGISTERED_FRAMES 64
  27. typedef struct NVENCFrame {
  28. NV_ENC_INPUT_PTR in;
  29. AVFrame *in_ref;
  30. NV_ENC_MAP_INPUT_RESOURCE in_map;
  31. int reg_idx;
  32. NV_ENC_OUTPUT_PTR out;
  33. NV_ENC_BUFFER_FORMAT format;
  34. int locked;
  35. } NVENCFrame;
  36. typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
  37. typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
  38. typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
  39. typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
  40. typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
  41. typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
  42. typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
  43. typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
  44. typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
  45. typedef struct NVENCLibraryContext
  46. {
  47. #if !CONFIG_CUDA
  48. void *cuda;
  49. #endif
  50. void *nvenc;
  51. PCUINIT cu_init;
  52. PCUDEVICEGETCOUNT cu_device_get_count;
  53. PCUDEVICEGET cu_device_get;
  54. PCUDEVICEGETNAME cu_device_get_name;
  55. PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
  56. PCUCTXCREATE cu_ctx_create;
  57. PCUCTXPOPCURRENT cu_ctx_pop_current;
  58. PCUCTXDESTROY cu_ctx_destroy;
  59. NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
  60. } NVENCLibraryContext;
  61. enum {
  62. PRESET_DEFAULT,
  63. PRESET_HP,
  64. PRESET_HQ,
  65. PRESET_BD ,
  66. PRESET_LOW_LATENCY_DEFAULT ,
  67. PRESET_LOW_LATENCY_HQ ,
  68. PRESET_LOW_LATENCY_HP,
  69. PRESET_LOSSLESS_DEFAULT,
  70. PRESET_LOSSLESS_HP,
  71. };
  72. enum {
  73. NV_ENC_H264_PROFILE_BASELINE,
  74. NV_ENC_H264_PROFILE_MAIN,
  75. NV_ENC_H264_PROFILE_HIGH,
  76. NV_ENC_H264_PROFILE_HIGH_444,
  77. NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
  78. };
  79. enum {
  80. NVENC_LOWLATENCY = 1,
  81. NVENC_LOSSLESS,
  82. };
  83. enum {
  84. LIST_DEVICES = -2,
  85. ANY_DEVICE,
  86. };
  87. typedef struct NVENCContext {
  88. AVClass *class;
  89. NVENCLibraryContext nvel;
  90. NV_ENC_INITIALIZE_PARAMS params;
  91. NV_ENC_CONFIG config;
  92. CUcontext cu_context;
  93. CUcontext cu_context_internal;
  94. int nb_surfaces;
  95. NVENCFrame *frames;
  96. AVFifoBuffer *timestamps;
  97. AVFifoBuffer *pending, *ready;
  98. struct {
  99. CUdeviceptr ptr;
  100. NV_ENC_REGISTERED_PTR regptr;
  101. int mapped;
  102. } registered_frames[MAX_REGISTERED_FRAMES];
  103. int nb_registered_frames;
  104. /* the actual data pixel format, different from
  105. * AVCodecContext.pix_fmt when using hwaccel frames on input */
  106. enum AVPixelFormat data_pix_fmt;
  107. /* timestamps of the first two frames, for computing the first dts
  108. * when b-frames are present */
  109. int64_t initial_pts[2];
  110. int first_packet_output;
  111. void *nvenc_ctx;
  112. int preset;
  113. int profile;
  114. int level;
  115. int tier;
  116. int rc;
  117. int device;
  118. int flags;
  119. } NVENCContext;
  120. int ff_nvenc_encode_init(AVCodecContext *avctx);
  121. int ff_nvenc_encode_close(AVCodecContext *avctx);
  122. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  123. const AVFrame *frame, int *got_packet);
  124. extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
  125. #endif /* AVCODEC_NVENC_H */