FFmpeg
nvenc.h
Go to the documentation of this file.
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 
19 #ifndef AVCODEC_NVENC_H
20 #define AVCODEC_NVENC_H
21 
22 #include "config.h"
23 
24 #if CONFIG_D3D11VA
25 #define COBJMACROS
27 #else
28 typedef void ID3D11Device;
29 #endif
30 
31 #include <ffnvcodec/nvEncodeAPI.h>
32 
34 #include "libavutil/buffer.h"
35 #include "libavutil/fifo.h"
36 #include "libavutil/opt.h"
37 #include "hwconfig.h"
38 
39 #include "avcodec.h"
40 
41 #define MAX_REGISTERED_FRAMES 64
42 #define RC_MODE_DEPRECATED 0x800000
43 #define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
44 
45 #define NVENCAPI_CHECK_VERSION(major, minor) \
46  ((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))
47 
48 // SDK 8.1 compile time feature checks
49 #if NVENCAPI_CHECK_VERSION(8, 1)
50 #define NVENC_HAVE_BFRAME_REF_MODE
51 #define NVENC_HAVE_QP_MAP_MODE
52 #endif
53 
54 // SDK 9.0 compile time feature checks
55 #if NVENCAPI_CHECK_VERSION(9, 0)
56 #define NVENC_HAVE_HEVC_BFRAME_REF_MODE
57 #endif
58 
59 // SDK 9.1 compile time feature checks
60 #if NVENCAPI_CHECK_VERSION(9, 1)
61 #define NVENC_HAVE_MULTIPLE_REF_FRAMES
62 #define NVENC_HAVE_CUSTREAM_PTR
63 #define NVENC_HAVE_GETLASTERRORSTRING
64 #endif
65 
66 // SDK 10.0 compile time feature checks
67 #if NVENCAPI_CHECK_VERSION(10, 0)
68 #define NVENC_HAVE_NEW_PRESETS
69 #define NVENC_HAVE_MULTIPASS
70 #define NVENC_HAVE_LDKFS
71 #define NVENC_HAVE_H264_LVL6
72 #define NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
73 #endif
74 
75 // SDK 11.1 compile time feature checks
76 #if NVENCAPI_CHECK_VERSION(11, 1)
77 #define NVENC_HAVE_QP_CHROMA_OFFSETS
78 #define NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
79 #endif
80 
81 // SDK 12.1 compile time feature checks
82 #if NVENCAPI_CHECK_VERSION(12, 1)
83 #define NVENC_NO_DEPRECATED_RC
84 #endif
85 
86 // SDK 12.2 compile time feature checks
87 #if NVENCAPI_CHECK_VERSION(12, 2)
88 #define NVENC_HAVE_NEW_BIT_DEPTH_API
89 #endif
90 
91 typedef struct NvencSurface
92 {
93  NV_ENC_INPUT_PTR input_surface;
95  int reg_idx;
96  int width;
97  int height;
98  int pitch;
99 
100  NV_ENC_OUTPUT_PTR output_surface;
101  NV_ENC_BUFFER_FORMAT format;
102 } NvencSurface;
103 
104 typedef struct NvencFrameData
105 {
107 
111 
112 typedef struct NvencDynLoadFunctions
113 {
114  CudaFunctions *cuda_dl;
115  NvencFunctions *nvenc_dl;
116 
117  NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
120 
121 enum {
134 #ifdef NVENC_HAVE_NEW_PRESETS
135  PRESET_P1,
136  PRESET_P2,
137  PRESET_P3,
138  PRESET_P4,
139  PRESET_P5,
140  PRESET_P6,
141  PRESET_P7,
142 #endif
143 };
144 
145 enum {
150 };
151 
152 enum {
156 };
157 
158 enum {
163 
165 };
166 
167 enum {
170 };
171 
172 enum {
176 };
177 
178 typedef struct NvencContext
179 {
181 
183 
184  NV_ENC_INITIALIZE_PARAMS init_encode_params;
185  NV_ENC_CONFIG encode_config;
186  CUcontext cu_context;
188  CUstream cu_stream;
190 
192 
195 
199 
204 
205  NV_ENC_SEI_PAYLOAD *sei_data;
207 
208  struct {
209  void *ptr;
211  NV_ENC_REGISTERED_PTR regptr;
212  int mapped;
213  NV_ENC_MAP_INPUT_RESOURCE in_map;
216 
217  /* the actual data pixel format, different from
218  * AVCodecContext.pix_fmt when using hwaccel frames on input */
220 
222 
223  void *nvencoder;
224 
225  int preset;
226  int profile;
227  int level;
228  int tier;
229  int rc;
230  int cbr;
233  int twopass;
234  int device;
235  int flags;
238  int aq;
241  int b_adapt;
244  int nonref_p;
247  float quality;
248  int aud;
253  int cqp;
257  int coder;
259  int a53_cc;
260  int s12m_tc;
261  int dpb_size;
264  int ldkfs;
269  int udu_sei;
273  int rgb_mode;
274 } NvencContext;
275 
277 
279 
281 
283 
284 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
285 extern const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[];
286 
287 #endif /* AVCODEC_NVENC_H */
NvencContext::twopass
int twopass
Definition: nvenc.h:233
hwconfig.h
PRESET_LOW_LATENCY_DEFAULT
@ PRESET_LOW_LATENCY_DEFAULT
Definition: nvenc.h:129
NvencContext::b_adapt
int b_adapt
Definition: nvenc.h:241
NvencContext::zerolatency
int zerolatency
Definition: nvenc.h:243
NvencContext::multipass
int multipass
Definition: nvenc.h:263
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
NvencContext::output_surface_ready_queue
AVFifo * output_surface_ready_queue
Definition: nvenc.h:202
NvencContext::extra_sei
int extra_sei
Definition: nvenc.h:265
NvencContext::highbitdepth
int highbitdepth
Definition: nvenc.h:271
PRESET_HP
@ PRESET_HP
Definition: nvenc.h:126
opt.h
NvencContext::bluray_compat
int bluray_compat
Definition: nvenc.h:249
NvencContext::preset
int preset
Definition: nvenc.h:225
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:153
NvencContext
Definition: nvenc.h:178
int64_t
long long int64_t
Definition: coverity.c:34
NvencContext::frame_data_array_nb
int frame_data_array_nb
Definition: nvenc.h:197
NvencSurface::in_ref
AVFrame * in_ref
Definition: nvenc.h:94
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:162
NvencContext::nb_surfaces
int nb_surfaces
Definition: nvenc.h:193
PRESET_FAST
@ PRESET_FAST
Definition: nvenc.h:125
NvencContext::ptr_index
int ptr_index
Definition: nvenc.h:210
NvencContext::encode_config
NV_ENC_CONFIG encode_config
Definition: nvenc.h:185
NvencFrameData
Definition: nvenc.h:104
NvencContext::nvenc_dload_funcs
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.h:182
NvencContext::rgb_mode
int rgb_mode
Definition: nvenc.h:273
NvencSurface
Definition: nvenc.h:91
PRESET_LOSSLESS_DEFAULT
@ PRESET_LOSSLESS_DEFAULT
Definition: nvenc.h:132
NvencContext::profile
int profile
Definition: nvenc.h:226
PRESET_DEFAULT
@ PRESET_DEFAULT
Definition: nvenc.h:122
NvencContext::regptr
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:211
NvencContext::tile_cols
int tile_cols
Definition: nvenc.h:232
NvencContext::no_scenecut
int no_scenecut
Definition: nvenc.h:239
NvencContext::cbr
int cbr
Definition: nvenc.h:230
NvencContext::single_slice_intra_refresh
int single_slice_intra_refresh
Definition: nvenc.h:267
NvencDynLoadFunctions::nvenc_device_count
int nvenc_device_count
Definition: nvenc.h:118
fifo.h
NvencContext::nvencoder
void * nvencoder
Definition: nvenc.h:223
NvencContext::temporal_aq
int temporal_aq
Definition: nvenc.h:242
NvencContext::sei_data_size
int sei_data_size
Definition: nvenc.h:206
NvencSurface::format
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:101
NvencContext::init_encode_params
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.h:184
NvencContext::cu_stream
CUstream cu_stream
Definition: nvenc.h:188
PRESET_MEDIUM
@ PRESET_MEDIUM
Definition: nvenc.h:124
NvencContext::device
int device
Definition: nvenc.h:234
pkt
AVPacket * pkt
Definition: movenc.c:59
NvencContext::surfaces
NvencSurface * surfaces
Definition: nvenc.h:194
NvencFrameData::frame_opaque
void * frame_opaque
Definition: nvenc.h:108
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:155
NvencContext::b_ref_mode
int b_ref_mode
Definition: nvenc.h:258
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:146
NvencContext::max_slice_size
int max_slice_size
Definition: nvenc.h:272
NvencContext::cu_context
CUcontext cu_context
Definition: nvenc.h:186
PRESET_LOW_LATENCY_HP
@ PRESET_LOW_LATENCY_HP
Definition: nvenc.h:131
NvencDynLoadFunctions
Definition: nvenc.h:112
NvencContext::intra_refresh
int intra_refresh
Definition: nvenc.h:266
NvencContext::ldkfs
int ldkfs
Definition: nvenc.h:264
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:148
NvencFrameData::duration
int64_t duration
Definition: nvenc.h:106
NVENC_RGB_MODE_DISABLED
@ NVENC_RGB_MODE_DISABLED
Definition: nvenc.h:173
NvencContext::dpb_size
int dpb_size
Definition: nvenc.h:261
NvencContext::cqp
int cqp
Definition: nvenc.h:253
NvencDynLoadFunctions::nvenc_dl
NvencFunctions * nvenc_dl
Definition: nvenc.h:115
NvencSurface::pitch
int pitch
Definition: nvenc.h:98
NvencSurface::input_surface
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:93
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:168
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NvencFrameData::frame_opaque_ref
AVBufferRef * frame_opaque_ref
Definition: nvenc.h:109
NvencSurface::reg_idx
int reg_idx
Definition: nvenc.h:95
NvencContext::s12m_tc
int s12m_tc
Definition: nvenc.h:260
NvencContext::tile_rows
int tile_rows
Definition: nvenc.h:231
PRESET_LOSSLESS_HP
@ PRESET_LOSSLESS_HP
Definition: nvenc.h:133
NvencDynLoadFunctions::cuda_dl
CudaFunctions * cuda_dl
Definition: nvenc.h:114
NvencContext::avclass
AVClass * avclass
Definition: nvenc.h:180
NvencContext::aq
int aq
Definition: nvenc.h:238
ff_nvenc_encode_init
int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1954
PRESET_LOW_LATENCY_HQ
@ PRESET_LOW_LATENCY_HQ
Definition: nvenc.h:130
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:161
NvencContext::constrained_encoding
int constrained_encoding
Definition: nvenc.h:268
NvencContext::in_map
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:213
NvencContext::frame
AVFrame * frame
Definition: nvenc.h:191
NvencContext::qp_cb_offset
int qp_cb_offset
Definition: nvenc.h:254
AVFifo
Definition: fifo.c:35
MAX_REGISTERED_FRAMES
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:41
NvencContext::init_qp_b
int init_qp_b
Definition: nvenc.h:251
NvencContext::data_pix_fmt
enum AVPixelFormat data_pix_fmt
Definition: nvenc.h:219
NvencContext::sei_data
NV_ENC_SEI_PAYLOAD * sei_data
Definition: nvenc.h:205
NvencContext::quality
float quality
Definition: nvenc.h:247
NvencContext::aq_strength
int aq_strength
Definition: nvenc.h:246
PRESET_HQ
@ PRESET_HQ
Definition: nvenc.h:127
AVCodecHWConfigInternal
Definition: hwconfig.h:25
NvencContext::flags
int flags
Definition: nvenc.h:235
buffer.h
NvencContext::output_surface_queue
AVFifo * output_surface_queue
Definition: nvenc.h:201
NvencContext::support_dyn_bitrate
int support_dyn_bitrate
Definition: nvenc.h:221
NvencContext::init_qp_i
int init_qp_i
Definition: nvenc.h:252
NvencContext::a53_cc
int a53_cc
Definition: nvenc.h:259
ff_nvenc_encode_flush
void ff_nvenc_encode_flush(AVCodecContext *avctx)
Definition: nvenc.c:2805
NvencContext::qp_cr_offset
int qp_cr_offset
Definition: nvenc.h:255
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:169
NVENC_RGB_MODE_444
@ NVENC_RGB_MODE_444
Definition: nvenc.h:175
NvencContext::ptr
void * ptr
Definition: nvenc.h:209
NvencContext::init_qp_p
int init_qp_p
Definition: nvenc.h:250
NvencContext::forced_idr
int forced_idr
Definition: nvenc.h:240
NvencContext::timestamp_list
AVFifo * timestamp_list
Definition: nvenc.h:203
NvencContext::rc
int rc
Definition: nvenc.h:229
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:160
NvencContext::frame_data_array
NvencFrameData * frame_data_array
Definition: nvenc.h:196
NvencContext::rc_lookahead
int rc_lookahead
Definition: nvenc.h:237
NvencContext::nonref_p
int nonref_p
Definition: nvenc.h:244
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:154
NVENC_DEPRECATED_PRESET
@ NVENC_DEPRECATED_PRESET
Definition: nvenc.h:164
NvencSurface::width
int width
Definition: nvenc.h:96
avcodec.h
ff_nvenc_receive_packet
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:2754
NvencContext::mapped
int mapped
Definition: nvenc.h:212
NvencContext::level
int level
Definition: nvenc.h:227
NvencContext::weighted_pred
int weighted_pred
Definition: nvenc.h:256
NvencContext::tuning_info
int tuning_info
Definition: nvenc.h:262
AVCodecContext
main external API structure.
Definition: avcodec.h:445
ff_nvenc_encode_close
int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1869
NvencContext::registered_frames
struct NvencContext::@134 registered_frames[MAX_REGISTERED_FRAMES]
PRESET_BD
@ PRESET_BD
Definition: nvenc.h:128
NvencSurface::height
int height
Definition: nvenc.h:97
NvencContext::frame_data_array_pos
int frame_data_array_pos
Definition: nvenc.h:198
NvencSurface::output_surface
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:100
NvencContext::async_depth
int async_depth
Definition: nvenc.h:236
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:159
NVENC_RGB_MODE_420
@ NVENC_RGB_MODE_420
Definition: nvenc.h:174
NvencDynLoadFunctions::nvenc_funcs
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:117
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
NvencContext::udu_sei
int udu_sei
Definition: nvenc.h:269
NvencContext::nb_registered_frames
int nb_registered_frames
Definition: nvenc.h:215
NvencContext::aud
int aud
Definition: nvenc.h:248
NvencContext::coder
int coder
Definition: nvenc.h:257
AVPacket
This structure stores compressed data.
Definition: packet.h:499
NvencContext::tier
int tier
Definition: nvenc.h:228
ff_nvenc_pix_fmts
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:56
ID3D11Device
void ID3D11Device
Definition: nvenc.h:28
PRESET_SLOW
@ PRESET_SLOW
Definition: nvenc.h:123
NvencContext::unused_surface_queue
AVFifo * unused_surface_queue
Definition: nvenc.h:200
NvencContext::cu_context_internal
CUcontext cu_context_internal
Definition: nvenc.h:187
NvencContext::strict_gop
int strict_gop
Definition: nvenc.h:245
hwcontext_d3d11va.h
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:149
NvencContext::timing_info
int timing_info
Definition: nvenc.h:270
ff_nvenc_hw_configs
const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[]
Definition: nvenc.c:78
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:147
NvencContext::d3d11_device
ID3D11Device * d3d11_device
Definition: nvenc.h:189