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/fifo.h"
35 #include "libavutil/opt.h"
36 #include "hwconfig.h"
37 
38 #include "avcodec.h"
39 
40 #define MAX_REGISTERED_FRAMES 64
41 #define RC_MODE_DEPRECATED 0x800000
42 #define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
43 
44 #define NVENCAPI_CHECK_VERSION(major, minor) \
45  ((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))
46 
47 // SDK 8.1 compile time feature checks
48 #if NVENCAPI_CHECK_VERSION(8, 1)
49 #define NVENC_HAVE_BFRAME_REF_MODE
50 #define NVENC_HAVE_QP_MAP_MODE
51 #endif
52 
53 // SDK 9.0 compile time feature checks
54 #if NVENCAPI_CHECK_VERSION(9, 0)
55 #define NVENC_HAVE_HEVC_BFRAME_REF_MODE
56 #endif
57 
58 // SDK 9.1 compile time feature checks
59 #if NVENCAPI_CHECK_VERSION(9, 1)
60 #define NVENC_HAVE_MULTIPLE_REF_FRAMES
61 #define NVENC_HAVE_CUSTREAM_PTR
62 #define NVENC_HAVE_GETLASTERRORSTRING
63 #endif
64 
65 // SDK 10.0 compile time feature checks
66 #if NVENCAPI_CHECK_VERSION(10, 0)
67 #define NVENC_HAVE_NEW_PRESETS
68 #define NVENC_HAVE_MULTIPASS
69 #define NVENC_HAVE_LDKFS
70 #define NVENC_HAVE_H264_LVL6
71 #endif
72 
73 typedef struct NvencSurface
74 {
75  NV_ENC_INPUT_PTR input_surface;
77  int reg_idx;
78  int width;
79  int height;
80  int pitch;
81 
82  NV_ENC_OUTPUT_PTR output_surface;
83  NV_ENC_BUFFER_FORMAT format;
84 } NvencSurface;
85 
86 typedef struct NvencDynLoadFunctions
87 {
88  CudaFunctions *cuda_dl;
89  NvencFunctions *nvenc_dl;
90 
91  NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
94 
95 enum {
108 #ifdef NVENC_HAVE_NEW_PRESETS
109  PRESET_P1,
110  PRESET_P2,
111  PRESET_P3,
112  PRESET_P4,
113  PRESET_P5,
114  PRESET_P6,
115  PRESET_P7,
116 #endif
117 };
118 
119 enum {
124 };
125 
126 enum {
130 };
131 
132 enum {
137 
139 };
140 
141 enum {
144 };
145 
146 typedef struct NvencContext
147 {
149 
151 
152  NV_ENC_INITIALIZE_PARAMS init_encode_params;
153  NV_ENC_CONFIG encode_config;
154  CUcontext cu_context;
156  CUstream cu_stream;
158 
160 
163 
168 
169  struct {
170  void *ptr;
172  NV_ENC_REGISTERED_PTR regptr;
173  int mapped;
174  NV_ENC_MAP_INPUT_RESOURCE in_map;
177 
178  /* the actual data pixel format, different from
179  * AVCodecContext.pix_fmt when using hwaccel frames on input */
181 
183 
184  void *nvencoder;
185 
186  int preset;
187  int profile;
188  int level;
189  int tier;
190  int rc;
191  int cbr;
192  int twopass;
193  int device;
194  int flags;
197  int aq;
200  int b_adapt;
203  int nonref_p;
206  float quality;
207  int aud;
212  int cqp;
214  int coder;
216  int a53_cc;
217  int s12m_tc;
218  int dpb_size;
221  int ldkfs;
222 } NvencContext;
223 
225 
227 
229 
231 
232 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
233 extern const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[];
234 
235 #endif /* AVCODEC_NVENC_H */
NvencContext::twopass
int twopass
Definition: nvenc.h:192
hwconfig.h
NvencContext::b_adapt
int b_adapt
Definition: nvenc.h:200
NvencContext::zerolatency
int zerolatency
Definition: nvenc.h:202
NvencContext::multipass
int multipass
Definition: nvenc.h:220
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
NvencContext::output_surface_ready_queue
AVFifoBuffer * output_surface_ready_queue
Definition: nvenc.h:166
opt.h
NvencContext::bluray_compat
int bluray_compat
Definition: nvenc.h:208
NvencContext::preset
int preset
Definition: nvenc.h:186
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:122
NvencContext
Definition: nvenc.h:146
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:123
NvencSurface::in_ref
AVFrame * in_ref
Definition: nvenc.h:76
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
NvencContext::registered_frames
struct NvencContext::@108 registered_frames[MAX_REGISTERED_FRAMES]
NvencContext::nb_surfaces
int nb_surfaces
Definition: nvenc.h:161
PRESET_MEDIUM
@ PRESET_MEDIUM
Definition: nvenc.h:98
NvencContext::ptr_index
int ptr_index
Definition: nvenc.h:171
NvencContext::encode_config
NV_ENC_CONFIG encode_config
Definition: nvenc.h:153
PRESET_LOSSLESS_HP
@ PRESET_LOSSLESS_HP
Definition: nvenc.h:107
NvencContext::nvenc_dload_funcs
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.h:150
NvencSurface
Definition: nvenc.h:73
NvencContext::profile
int profile
Definition: nvenc.h:187
NvencContext::regptr
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:172
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:120
PRESET_HQ
@ PRESET_HQ
Definition: nvenc.h:101
AVFifoBuffer
Definition: fifo.h:31
NvencContext::no_scenecut
int no_scenecut
Definition: nvenc.h:198
NvencContext::cbr
int cbr
Definition: nvenc.h:191
NvencDynLoadFunctions::nvenc_device_count
int nvenc_device_count
Definition: nvenc.h:92
fifo.h
NvencContext::nvencoder
void * nvencoder
Definition: nvenc.h:184
NvencContext::temporal_aq
int temporal_aq
Definition: nvenc.h:201
NvencSurface::format
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:83
NvencContext::init_encode_params
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.h:152
NvencContext::cu_stream
CUstream cu_stream
Definition: nvenc.h:156
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:143
NvencContext::device
int device
Definition: nvenc.h:193
pkt
AVPacket * pkt
Definition: movenc.c:59
NvencContext::surfaces
NvencSurface * surfaces
Definition: nvenc.h:162
NvencContext::b_ref_mode
int b_ref_mode
Definition: nvenc.h:215
NvencContext::cu_context
CUcontext cu_context
Definition: nvenc.h:154
PRESET_DEFAULT
@ PRESET_DEFAULT
Definition: nvenc.h:96
NvencDynLoadFunctions
Definition: nvenc.h:86
NvencContext::ldkfs
int ldkfs
Definition: nvenc.h:221
NVENC_DEPRECATED_PRESET
@ NVENC_DEPRECATED_PRESET
Definition: nvenc.h:138
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:127
NvencContext::dpb_size
int dpb_size
Definition: nvenc.h:218
NvencContext::cqp
int cqp
Definition: nvenc.h:212
NvencDynLoadFunctions::nvenc_dl
NvencFunctions * nvenc_dl
Definition: nvenc.h:89
NvencSurface::pitch
int pitch
Definition: nvenc.h:80
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:128
NvencSurface::input_surface
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:75
PRESET_HP
@ PRESET_HP
Definition: nvenc.h:100
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NvencSurface::reg_idx
int reg_idx
Definition: nvenc.h:77
NvencContext::s12m_tc
int s12m_tc
Definition: nvenc.h:217
NvencDynLoadFunctions::cuda_dl
CudaFunctions * cuda_dl
Definition: nvenc.h:88
NvencContext::avclass
AVClass * avclass
Definition: nvenc.h:148
NvencContext::aq
int aq
Definition: nvenc.h:197
ff_nvenc_encode_init
int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1643
NvencContext::in_map
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:174
NvencContext::frame
AVFrame * frame
Definition: nvenc.h:159
NvencContext::timestamp_list
AVFifoBuffer * timestamp_list
Definition: nvenc.h:167
MAX_REGISTERED_FRAMES
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:40
NvencContext::init_qp_b
int init_qp_b
Definition: nvenc.h:210
NvencContext::data_pix_fmt
enum AVPixelFormat data_pix_fmt
Definition: nvenc.h:180
NvencContext::quality
float quality
Definition: nvenc.h:206
NvencContext::aq_strength
int aq_strength
Definition: nvenc.h:205
AVCodecHWConfigInternal
Definition: hwconfig.h:29
PRESET_LOW_LATENCY_DEFAULT
@ PRESET_LOW_LATENCY_DEFAULT
Definition: nvenc.h:103
NvencContext::flags
int flags
Definition: nvenc.h:194
NvencContext::support_dyn_bitrate
int support_dyn_bitrate
Definition: nvenc.h:182
NvencContext::init_qp_i
int init_qp_i
Definition: nvenc.h:211
NvencContext::a53_cc
int a53_cc
Definition: nvenc.h:216
ff_nvenc_encode_flush
void ff_nvenc_encode_flush(AVCodecContext *avctx)
Definition: nvenc.c:2353
NvencContext::ptr
void * ptr
Definition: nvenc.h:170
NvencContext::init_qp_p
int init_qp_p
Definition: nvenc.h:209
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:142
NvencContext::forced_idr
int forced_idr
Definition: nvenc.h:199
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:133
NvencContext::rc
int rc
Definition: nvenc.h:190
NvencContext::rc_lookahead
int rc_lookahead
Definition: nvenc.h:196
NvencContext::nonref_p
int nonref_p
Definition: nvenc.h:203
NvencContext::unused_surface_queue
AVFifoBuffer * unused_surface_queue
Definition: nvenc.h:164
NvencSurface::width
int width
Definition: nvenc.h:78
avcodec.h
ff_nvenc_receive_packet
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:2302
NvencContext::mapped
int mapped
Definition: nvenc.h:173
PRESET_LOW_LATENCY_HP
@ PRESET_LOW_LATENCY_HP
Definition: nvenc.h:105
NvencContext::level
int level
Definition: nvenc.h:188
NvencContext::weighted_pred
int weighted_pred
Definition: nvenc.h:213
NvencContext::tuning_info
int tuning_info
Definition: nvenc.h:219
AVCodecContext
main external API structure.
Definition: avcodec.h:501
ff_nvenc_encode_close
int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1566
NvencSurface::height
int height
Definition: nvenc.h:79
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:135
PRESET_LOSSLESS_DEFAULT
@ PRESET_LOSSLESS_DEFAULT
Definition: nvenc.h:106
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:136
NvencSurface::output_surface
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:82
NvencContext::async_depth
int async_depth
Definition: nvenc.h:195
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:129
NvencDynLoadFunctions::nvenc_funcs
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:91
PRESET_FAST
@ PRESET_FAST
Definition: nvenc.h:99
NvencContext::nb_registered_frames
int nb_registered_frames
Definition: nvenc.h:176
PRESET_BD
@ PRESET_BD
Definition: nvenc.h:102
NvencContext::aud
int aud
Definition: nvenc.h:207
NvencContext::coder
int coder
Definition: nvenc.h:214
AVPacket
This structure stores compressed data.
Definition: packet.h:342
PRESET_SLOW
@ PRESET_SLOW
Definition: nvenc.h:97
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:121
PRESET_LOW_LATENCY_HQ
@ PRESET_LOW_LATENCY_HQ
Definition: nvenc.h:104
NvencContext::tier
int tier
Definition: nvenc.h:189
ff_nvenc_pix_fmts
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:46
ID3D11Device
void ID3D11Device
Definition: nvenc.h:28
NvencContext::cu_context_internal
CUcontext cu_context_internal
Definition: nvenc.h:155
NvencContext::strict_gop
int strict_gop
Definition: nvenc.h:204
hwcontext_d3d11va.h
ff_nvenc_hw_configs
const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[]
Definition: nvenc.c:64
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:134
NvencContext::output_surface_queue
AVFifoBuffer * output_surface_queue
Definition: nvenc.h:165
NvencContext::d3d11_device
ID3D11Device * d3d11_device
Definition: nvenc.h:157