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 #define NVENC_HAVE_FILLER_DATA
65 #endif
66 
67 // SDK 10.0 compile time feature checks
68 #if NVENCAPI_CHECK_VERSION(10, 0)
69 #define NVENC_HAVE_NEW_PRESETS
70 #define NVENC_HAVE_MULTIPASS
71 #define NVENC_HAVE_LDKFS
72 #define NVENC_HAVE_H264_LVL6
73 #define NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
74 #endif
75 
76 // SDK 11.1 compile time feature checks
77 #if NVENCAPI_CHECK_VERSION(11, 1)
78 #define NVENC_HAVE_QP_CHROMA_OFFSETS
79 #define NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
80 #endif
81 
82 // SDK 12.0 compile time feature checks
83 #if NVENCAPI_CHECK_VERSION(12, 0)
84 #define NVENC_HAVE_HEVC_OUTPUT_RECOVERY_POINT_SEI
85 #endif
86 
87 // SDK 12.1 compile time feature checks
88 #if NVENCAPI_CHECK_VERSION(12, 1)
89 #define NVENC_NO_DEPRECATED_RC
90 #define NVENC_HAVE_SPLIT_FRAME_ENCODING
91 #endif
92 
93 // SDK 12.2 compile time feature checks
94 #if NVENCAPI_CHECK_VERSION(12, 2)
95 #define NVENC_HAVE_NEW_BIT_DEPTH_API
96 #define NVENC_HAVE_TEMPORAL_FILTER
97 #define NVENC_HAVE_LOOKAHEAD_LEVEL
98 #define NVENC_HAVE_UHQ_TUNING
99 #define NVENC_HAVE_UNIDIR_B
100 #define NVENC_HAVE_TIME_CODE // added in 12.0, but incomplete until 12.2
101 #endif
102 
103 // SDK 13.0 compile time feature checks
104 #if NVENCAPI_CHECK_VERSION(13, 0)
105 #define NVENC_HAVE_H264_10BIT_SUPPORT
106 #define NVENC_HAVE_422_SUPPORT
107 #define NVENC_HAVE_SFE_FOUR_WAYS_SUPPORT
108 #define NVENC_HAVE_AV1_UHQ_TUNING
109 #define NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
110 #define NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
111 #define NVENC_HAVE_MVHEVC
112 #endif
113 
114 // SDK 13.1 compile time feature checks
115 #if NVENCAPI_CHECK_VERSION(13, 1)
116 #define NVENC_NEW_COUNTING_TYPE
117 #endif
118 
119 typedef struct NvencSurface
120 {
121  NV_ENC_INPUT_PTR input_surface;
123  int reg_idx;
124  int width;
125  int height;
126  int pitch;
127 
128  NV_ENC_OUTPUT_PTR output_surface;
129  NV_ENC_BUFFER_FORMAT format;
130 } NvencSurface;
131 
132 typedef struct NvencFrameData
133 {
135 
139 
140 typedef struct NvencDynLoadFunctions
141 {
142  CudaFunctions *cuda_dl;
143  NvencFunctions *nvenc_dl;
144 
145  NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
148 
149 enum {
162 #ifdef NVENC_HAVE_NEW_PRESETS
163  PRESET_P1,
164  PRESET_P2,
165  PRESET_P3,
166  PRESET_P4,
167  PRESET_P5,
168  PRESET_P6,
169  PRESET_P7,
170 #endif
171 };
172 
173 enum {
177 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
178  NV_ENC_H264_PROFILE_HIGH_10,
179 #endif
180 #ifdef NVENC_HAVE_422_SUPPORT
181  NV_ENC_H264_PROFILE_HIGH_422,
182 #endif
184 };
185 
186 enum {
190 #ifdef NVENC_HAVE_MVHEVC
191  NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN,
192 #endif
193 
195 };
196 
197 enum {
202 
204 };
205 
206 enum {
209 };
210 
211 enum {
215 };
216 
217 typedef struct NvencContext
218 {
220 
222 
223  NV_ENC_INITIALIZE_PARAMS init_encode_params;
224  NV_ENC_CONFIG encode_config;
225  CUcontext cu_context;
227  CUstream cu_stream;
229 
231 
234 
238 
243  // This is for DTS calculating, reset after flush
246 
247  NV_ENC_SEI_PAYLOAD *sei_data;
249 
250  struct {
251  void *ptr;
253  NV_ENC_REGISTERED_PTR regptr;
254  int mapped;
255  NV_ENC_MAP_INPUT_RESOURCE in_map;
258 
259  /* the actual data pixel format, different from
260  * AVCodecContext.pix_fmt when using hwaccel frames on input */
262 
264 
265  void *nvencoder;
266 
268  uint32_t next_view_id;
269 
270  int preset;
271  int profile;
272  int level;
273  int tier;
274  int rc;
275  int cbr;
278  int twopass;
279  int device;
280  int flags;
283  int aq;
286  int b_adapt;
289  int nonref_p;
292  float quality;
293  int aud;
295  int qmin;
296  int qmax;
300  int cqp;
304  int coder;
306  int a53_cc;
307  int s12m_tc;
308  int dpb_size;
311  int ldkfs;
316  int udu_sei;
320  int rgb_mode;
321  int tf_level;
323  int unidir_b;
325  int mdm, cll;
329 } NvencContext;
330 
332 
334 
336 
338 
339 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
340 extern const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[];
341 
342 #endif /* AVCODEC_NVENC_H */
NvencContext::twopass
int twopass
Definition: nvenc.h:278
hwconfig.h
NvencContext::b_adapt
int b_adapt
Definition: nvenc.h:286
NvencContext::multiview
int multiview
Definition: nvenc.h:327
NvencContext::zerolatency
int zerolatency
Definition: nvenc.h:288
NvencContext::multipass
int multipass
Definition: nvenc.h:310
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
NvencContext::output_surface_ready_queue
AVFifo * output_surface_ready_queue
Definition: nvenc.h:241
NvencContext::extra_sei
int extra_sei
Definition: nvenc.h:312
NvencContext::highbitdepth
int highbitdepth
Definition: nvenc.h:318
PRESET_DEFAULT
@ PRESET_DEFAULT
Definition: nvenc.h:150
opt.h
NvencContext::bluray_compat
int bluray_compat
Definition: nvenc.h:294
NvencContext::preset
int preset
Definition: nvenc.h:270
NvencContext::cll
int cll
Definition: nvenc.h:325
NVENC_DEPRECATED_PRESET
@ NVENC_DEPRECATED_PRESET
Definition: nvenc.h:203
NvencContext::mdm
int mdm
Definition: nvenc.h:325
NvencContext
Definition: nvenc.h:217
int64_t
long long int64_t
Definition: coverity.c:34
NvencContext::frame_data_array_nb
int frame_data_array_nb
Definition: nvenc.h:236
NvencSurface::in_ref
AVFrame * in_ref
Definition: nvenc.h:122
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:188
NvencContext::nb_surfaces
int nb_surfaces
Definition: nvenc.h:232
NvencContext::ptr_index
int ptr_index
Definition: nvenc.h:252
NvencContext::encode_config
NV_ENC_CONFIG encode_config
Definition: nvenc.h:224
NvencFrameData
Definition: nvenc.h:132
NvencContext::nvenc_dload_funcs
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.h:221
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:207
NVENC_RGB_MODE_DISABLED
@ NVENC_RGB_MODE_DISABLED
Definition: nvenc.h:212
NvencContext::rgb_mode
int rgb_mode
Definition: nvenc.h:320
PRESET_LOW_LATENCY_DEFAULT
@ PRESET_LOW_LATENCY_DEFAULT
Definition: nvenc.h:157
NvencSurface
Definition: nvenc.h:119
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:187
NvencContext::profile
int profile
Definition: nvenc.h:271
NvencContext::output_frame_num
uint64_t output_frame_num
Definition: nvenc.h:244
PRESET_MEDIUM
@ PRESET_MEDIUM
Definition: nvenc.h:152
NvencContext::next_view_id
uint32_t next_view_id
Definition: nvenc.h:268
NvencContext::regptr
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:253
NvencContext::tile_cols
int tile_cols
Definition: nvenc.h:277
NvencContext::no_scenecut
int no_scenecut
Definition: nvenc.h:284
NvencContext::qmax
int qmax
Definition: nvenc.h:296
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:176
NvencContext::cbr
int cbr
Definition: nvenc.h:275
NvencContext::single_slice_intra_refresh
int single_slice_intra_refresh
Definition: nvenc.h:314
NvencDynLoadFunctions::nvenc_device_count
int nvenc_device_count
Definition: nvenc.h:146
fifo.h
NvencContext::frame_idx_counter
uint32_t frame_idx_counter
Definition: nvenc.h:267
NvencContext::nvencoder
void * nvencoder
Definition: nvenc.h:265
NvencContext::temporal_aq
int temporal_aq
Definition: nvenc.h:287
NvencContext::sei_data_size
int sei_data_size
Definition: nvenc.h:248
NvencSurface::format
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:129
NvencContext::init_encode_params
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.h:223
NvencContext::cu_stream
CUstream cu_stream
Definition: nvenc.h:227
PRESET_LOW_LATENCY_HQ
@ PRESET_LOW_LATENCY_HQ
Definition: nvenc.h:158
NvencContext::device
int device
Definition: nvenc.h:279
NvencContext::cbr_padding
int cbr_padding
Definition: nvenc.h:326
NvencContext::surfaces
NvencSurface * surfaces
Definition: nvenc.h:233
NvencFrameData::frame_opaque
void * frame_opaque
Definition: nvenc.h:136
NvencContext::b_ref_mode
int b_ref_mode
Definition: nvenc.h:305
NvencContext::lookahead_level
int lookahead_level
Definition: nvenc.h:322
PRESET_FAST
@ PRESET_FAST
Definition: nvenc.h:153
NvencContext::max_slice_size
int max_slice_size
Definition: nvenc.h:319
NvencContext::cu_context
CUcontext cu_context
Definition: nvenc.h:225
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:174
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:200
NvencDynLoadFunctions
Definition: nvenc.h:140
NvencContext::intra_refresh
int intra_refresh
Definition: nvenc.h:313
NvencContext::ldkfs
int ldkfs
Definition: nvenc.h:311
NV_ENC_HEVC_PROFILE_COUNT
@ NV_ENC_HEVC_PROFILE_COUNT
Definition: nvenc.h:194
NvencFrameData::duration
int64_t duration
Definition: nvenc.h:134
NvencContext::display_sei_sent
int display_sei_sent
Definition: nvenc.h:328
NvencContext::dpb_size
int dpb_size
Definition: nvenc.h:308
NvencContext::cqp
int cqp
Definition: nvenc.h:300
NvencDynLoadFunctions::nvenc_dl
NvencFunctions * nvenc_dl
Definition: nvenc.h:143
NvencSurface::pitch
int pitch
Definition: nvenc.h:126
NvencSurface::input_surface
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:121
NvencContext::split_encode_mode
int split_encode_mode
Definition: nvenc.h:324
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NvencFrameData::frame_opaque_ref
AVBufferRef * frame_opaque_ref
Definition: nvenc.h:137
NvencSurface::reg_idx
int reg_idx
Definition: nvenc.h:123
NvencContext::s12m_tc
int s12m_tc
Definition: nvenc.h:307
NvencContext::tile_rows
int tile_rows
Definition: nvenc.h:276
NvencContext::initial_delay_time
int64_t initial_delay_time
Definition: nvenc.h:245
NvencDynLoadFunctions::cuda_dl
CudaFunctions * cuda_dl
Definition: nvenc.h:142
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:208
NvencContext::avclass
AVClass * avclass
Definition: nvenc.h:219
NvencContext::aq
int aq
Definition: nvenc.h:283
ff_nvenc_encode_init
int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:2285
NvencContext::constrained_encoding
int constrained_encoding
Definition: nvenc.h:315
NvencContext::in_map
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:255
NvencContext::frame
AVFrame * frame
Definition: nvenc.h:230
NvencContext::qp_cb_offset
int qp_cb_offset
Definition: nvenc.h:301
AVFifo
Definition: fifo.c:35
PRESET_HP
@ PRESET_HP
Definition: nvenc.h:154
MAX_REGISTERED_FRAMES
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:41
NvencContext::init_qp_b
int init_qp_b
Definition: nvenc.h:298
NvencContext::data_pix_fmt
enum AVPixelFormat data_pix_fmt
Definition: nvenc.h:261
NVENC_RGB_MODE_420
@ NVENC_RGB_MODE_420
Definition: nvenc.h:213
NvencContext::sei_data
NV_ENC_SEI_PAYLOAD * sei_data
Definition: nvenc.h:247
NvencContext::quality
float quality
Definition: nvenc.h:292
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:201
PRESET_BD
@ PRESET_BD
Definition: nvenc.h:156
NvencContext::aq_strength
int aq_strength
Definition: nvenc.h:291
AVCodecHWConfigInternal
Definition: hwconfig.h:25
NvencContext::flags
int flags
Definition: nvenc.h:280
buffer.h
NvencContext::output_surface_queue
AVFifo * output_surface_queue
Definition: nvenc.h:240
NvencContext::support_dyn_bitrate
int support_dyn_bitrate
Definition: nvenc.h:263
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:189
PRESET_HQ
@ PRESET_HQ
Definition: nvenc.h:155
NvencContext::init_qp_i
int init_qp_i
Definition: nvenc.h:299
NvencContext::a53_cc
int a53_cc
Definition: nvenc.h:306
NvencContext::unidir_b
int unidir_b
Definition: nvenc.h:323
ff_nvenc_encode_flush
void ff_nvenc_encode_flush(AVCodecContext *avctx)
Definition: nvenc.c:3367
PRESET_SLOW
@ PRESET_SLOW
Definition: nvenc.h:151
NvencContext::qp_cr_offset
int qp_cr_offset
Definition: nvenc.h:302
NvencContext::ptr
void * ptr
Definition: nvenc.h:251
NvencContext::init_qp_p
int init_qp_p
Definition: nvenc.h:297
NvencContext::forced_idr
int forced_idr
Definition: nvenc.h:285
NvencContext::timestamp_list
AVFifo * timestamp_list
Definition: nvenc.h:242
NvencContext::rc
int rc
Definition: nvenc.h:274
NvencContext::frame_data_array
NvencFrameData * frame_data_array
Definition: nvenc.h:235
NvencContext::rc_lookahead
int rc_lookahead
Definition: nvenc.h:282
NvencContext::nonref_p
int nonref_p
Definition: nvenc.h:289
PRESET_LOW_LATENCY_HP
@ PRESET_LOW_LATENCY_HP
Definition: nvenc.h:159
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:175
NvencSurface::width
int width
Definition: nvenc.h:124
avcodec.h
ff_nvenc_receive_packet
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:3316
NvencContext::mapped
int mapped
Definition: nvenc.h:254
NvencContext::level
int level
Definition: nvenc.h:272
NvencContext::qmin
int qmin
Definition: nvenc.h:295
PRESET_LOSSLESS_HP
@ PRESET_LOSSLESS_HP
Definition: nvenc.h:161
NvencContext::weighted_pred
int weighted_pred
Definition: nvenc.h:303
NvencContext::tuning_info
int tuning_info
Definition: nvenc.h:309
AVCodecContext
main external API structure.
Definition: avcodec.h:443
ff_nvenc_encode_close
int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:2200
NvencSurface::height
int height
Definition: nvenc.h:125
NvencContext::frame_data_array_pos
int frame_data_array_pos
Definition: nvenc.h:237
NvencSurface::output_surface
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:128
NvencContext::async_depth
int async_depth
Definition: nvenc.h:281
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:198
NvencContext::tf_level
int tf_level
Definition: nvenc.h:321
NvencContext::registered_frames
struct NvencContext::@236 registered_frames[MAX_REGISTERED_FRAMES]
NvencDynLoadFunctions::nvenc_funcs
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:145
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
NvencContext::udu_sei
int udu_sei
Definition: nvenc.h:316
NvencContext::nb_registered_frames
int nb_registered_frames
Definition: nvenc.h:257
NvencContext::aud
int aud
Definition: nvenc.h:293
NvencContext::coder
int coder
Definition: nvenc.h:304
AVPacket
This structure stores compressed data.
Definition: packet.h:580
NvencContext::multiview_supported
int multiview_supported
Definition: nvenc.h:327
NvencContext::tier
int tier
Definition: nvenc.h:273
ff_nvenc_pix_fmts
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:59
ID3D11Device
void ID3D11Device
Definition: nvenc.h:28
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:199
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:183
NvencContext::unused_surface_queue
AVFifo * unused_surface_queue
Definition: nvenc.h:239
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
NvencContext::cu_context_internal
CUcontext cu_context_internal
Definition: nvenc.h:226
PRESET_LOSSLESS_DEFAULT
@ PRESET_LOSSLESS_DEFAULT
Definition: nvenc.h:160
NvencContext::strict_gop
int strict_gop
Definition: nvenc.h:290
hwcontext_d3d11va.h
NvencContext::timing_info
int timing_info
Definition: nvenc.h:317
ff_nvenc_hw_configs
const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[]
Definition: nvenc.c:88
NVENC_RGB_MODE_444
@ NVENC_RGB_MODE_444
Definition: nvenc.h:214
NvencContext::d3d11_device
ID3D11Device * d3d11_device
Definition: nvenc.h:228