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 
37 #include "avcodec.h"
38 
39 #define MAX_REGISTERED_FRAMES 64
40 #define RC_MODE_DEPRECATED 0x800000
41 #define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
42 
43 #define NVENCAPI_CHECK_VERSION(major, minor) \
44  ((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))
45 
46 // SDK 8.1 compile time feature checks
47 #if NVENCAPI_CHECK_VERSION(8, 1)
48 #define NVENC_HAVE_BFRAME_REF_MODE
49 #define NVENC_HAVE_QP_MAP_MODE
50 #endif
51 
52 // SDK 9.0 compile time feature checks
53 #if NVENCAPI_CHECK_VERSION(9, 0)
54 #define NVENC_HAVE_HEVC_BFRAME_REF_MODE
55 #endif
56 
57 typedef struct NvencSurface
58 {
59  NV_ENC_INPUT_PTR input_surface;
61  int reg_idx;
62  int width;
63  int height;
64  int pitch;
65 
66  NV_ENC_OUTPUT_PTR output_surface;
67  NV_ENC_BUFFER_FORMAT format;
68  int size;
69 } NvencSurface;
70 
71 typedef struct NvencDynLoadFunctions
72 {
73  CudaFunctions *cuda_dl;
74  NvencFunctions *nvenc_dl;
75 
76  NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
79 
80 enum {
91  PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
93 };
94 
95 enum {
100 };
101 
102 enum {
106 };
107 
108 enum {
113 };
114 
115 enum {
118 };
119 
120 typedef struct NvencContext
121 {
123 
125 
126  NV_ENC_INITIALIZE_PARAMS init_encode_params;
127  NV_ENC_CONFIG encode_config;
128  CUcontext cu_context;
131 
134 
139 
141 
142  struct {
143  void *ptr;
145  NV_ENC_REGISTERED_PTR regptr;
146  int mapped;
147  NV_ENC_MAP_INPUT_RESOURCE in_map;
150 
151  /* the actual data pixel format, different from
152  * AVCodecContext.pix_fmt when using hwaccel frames on input */
154 
155  /* timestamps of the first two frames, for computing the first dts
156  * when B-frames are present */
157  int64_t initial_pts[2];
159 
161 
162  void *nvencoder;
163 
164  int preset;
165  int profile;
166  int level;
167  int tier;
168  int rc;
169  int cbr;
170  int twopass;
171  int device;
172  int flags;
175  int aq;
178  int b_adapt;
181  int nonref_p;
184  float quality;
185  int aud;
190  int cqp;
192  int coder;
194  int a53_cc;
195 } NvencContext;
196 
198 
200 
201 int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame);
202 
204 
206  const AVFrame *frame, int *got_packet);
207 
208 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
209 
210 #endif /* AVCODEC_NVENC_H */
NvencContext::twopass
int twopass
Definition: nvenc.h:170
NvencContext::b_adapt
int b_adapt
Definition: nvenc.h:178
NvencContext::zerolatency
int zerolatency
Definition: nvenc.h:180
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
PRESET_FAST
@ PRESET_FAST
Definition: nvenc.h:84
NvencContext::output_surface_ready_queue
AVFifoBuffer * output_surface_ready_queue
Definition: nvenc.h:137
PRESET_SLOW
@ PRESET_SLOW
Definition: nvenc.h:82
ff_nvenc_send_frame
int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: nvenc.c:2044
opt.h
PRESET_HP
@ PRESET_HP
Definition: nvenc.h:85
NvencContext::bluray_compat
int bluray_compat
Definition: nvenc.h:186
NvencContext::preset
int preset
Definition: nvenc.h:164
NV_ENC_HEVC_PROFILE_REXT
@ NV_ENC_HEVC_PROFILE_REXT
Definition: nvenc.h:105
PRESET_LOW_LATENCY_HQ
@ PRESET_LOW_LATENCY_HQ
Definition: nvenc.h:89
NvencContext
Definition: nvenc.h:120
PRESET_LOW_LATENCY_DEFAULT
@ PRESET_LOW_LATENCY_DEFAULT
Definition: nvenc.h:88
NvencSurface::in_ref
AVFrame * in_ref
Definition: nvenc.h:60
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
NvencContext::nb_surfaces
int nb_surfaces
Definition: nvenc.h:132
NvencContext::ptr_index
int ptr_index
Definition: nvenc.h:144
NvencContext::encode_config
NV_ENC_CONFIG encode_config
Definition: nvenc.h:127
NvencContext::nvenc_dload_funcs
NvencDynLoadFunctions nvenc_dload_funcs
Definition: nvenc.h:124
NvencSurface
Definition: nvenc.h:57
NvencSurface::size
int size
Definition: nvenc.h:68
NvencContext::profile
int profile
Definition: nvenc.h:165
PRESET_BD
@ PRESET_BD
Definition: nvenc.h:87
NvencContext::regptr
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:145
PRESET_LOSSLESS_DEFAULT
@ PRESET_LOSSLESS_DEFAULT
Definition: nvenc.h:91
AVFifoBuffer
Definition: fifo.h:31
NvencContext::no_scenecut
int no_scenecut
Definition: nvenc.h:176
PRESET_LOW_LATENCY_HP
@ PRESET_LOW_LATENCY_HP
Definition: nvenc.h:90
NvencContext::cbr
int cbr
Definition: nvenc.h:169
NvencDynLoadFunctions::nvenc_device_count
int nvenc_device_count
Definition: nvenc.h:77
fifo.h
NvencContext::nvencoder
void * nvencoder
Definition: nvenc.h:162
NvencContext::temporal_aq
int temporal_aq
Definition: nvenc.h:179
NvencSurface::format
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:67
NvencContext::init_encode_params
NV_ENC_INITIALIZE_PARAMS init_encode_params
Definition: nvenc.h:126
NvencContext::registered_frames
struct NvencContext::@126 registered_frames[MAX_REGISTERED_FRAMES]
NvencContext::device
int device
Definition: nvenc.h:171
NV_ENC_H264_PROFILE_HIGH
@ NV_ENC_H264_PROFILE_HIGH
Definition: nvenc.h:98
NvencContext::surfaces
NvencSurface * surfaces
Definition: nvenc.h:133
PRESET_MEDIUM
@ PRESET_MEDIUM
Definition: nvenc.h:83
NvencContext::b_ref_mode
int b_ref_mode
Definition: nvenc.h:193
NvencContext::cu_context
CUcontext cu_context
Definition: nvenc.h:128
NvencContext::initial_pts
int64_t initial_pts[2]
Definition: nvenc.h:157
NvencDynLoadFunctions
Definition: nvenc.h:71
NvencContext::cqp
int cqp
Definition: nvenc.h:190
LIST_DEVICES
@ LIST_DEVICES
Definition: nvenc.h:116
NvencDynLoadFunctions::nvenc_dl
NvencFunctions * nvenc_dl
Definition: nvenc.h:74
NvencSurface::pitch
int pitch
Definition: nvenc.h:64
NvencSurface::input_surface
NV_ENC_INPUT_PTR input_surface
Definition: nvenc.h:59
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NvencSurface::reg_idx
int reg_idx
Definition: nvenc.h:61
NV_ENC_H264_PROFILE_HIGH_444P
@ NV_ENC_H264_PROFILE_HIGH_444P
Definition: nvenc.h:99
NvencDynLoadFunctions::cuda_dl
CudaFunctions * cuda_dl
Definition: nvenc.h:73
NvencContext::encoder_flushing
int encoder_flushing
Definition: nvenc.h:140
NvencContext::avclass
AVClass * avclass
Definition: nvenc.h:122
NvencContext::aq
int aq
Definition: nvenc.h:175
ff_nvenc_encode_init
int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1489
PRESET_HQ
@ PRESET_HQ
Definition: nvenc.h:86
NvencContext::in_map
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:147
NvencContext::timestamp_list
AVFifoBuffer * timestamp_list
Definition: nvenc.h:138
MAX_REGISTERED_FRAMES
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:39
NvencContext::init_qp_b
int init_qp_b
Definition: nvenc.h:188
NvencContext::data_pix_fmt
enum AVPixelFormat data_pix_fmt
Definition: nvenc.h:153
NvencContext::quality
float quality
Definition: nvenc.h:184
NvencContext::aq_strength
int aq_strength
Definition: nvenc.h:183
PRESET_DEFAULT
@ PRESET_DEFAULT
Definition: nvenc.h:81
NvencContext::flags
int flags
Definition: nvenc.h:172
PRESET_LOSSLESS_HP
@ PRESET_LOSSLESS_HP
Definition: nvenc.h:92
NVENC_ONE_PASS
@ NVENC_ONE_PASS
Definition: nvenc.h:111
NvencContext::support_dyn_bitrate
int support_dyn_bitrate
Definition: nvenc.h:160
NvencContext::init_qp_i
int init_qp_i
Definition: nvenc.h:189
ff_nvenc_encode_frame
int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: nvenc.c:2208
NvencContext::a53_cc
int a53_cc
Definition: nvenc.h:194
NvencContext::ptr
void * ptr
Definition: nvenc.h:143
NvencContext::init_qp_p
int init_qp_p
Definition: nvenc.h:187
NvencContext::forced_idr
int forced_idr
Definition: nvenc.h:177
NvencContext::rc
int rc
Definition: nvenc.h:168
NVENC_LOWLATENCY
@ NVENC_LOWLATENCY
Definition: nvenc.h:109
NvencContext::rc_lookahead
int rc_lookahead
Definition: nvenc.h:174
NvencContext::nonref_p
int nonref_p
Definition: nvenc.h:181
NVENC_LOSSLESS
@ NVENC_LOSSLESS
Definition: nvenc.h:110
NvencContext::unused_surface_queue
AVFifoBuffer * unused_surface_queue
Definition: nvenc.h:135
NV_ENC_HEVC_PROFILE_MAIN_10
@ NV_ENC_HEVC_PROFILE_MAIN_10
Definition: nvenc.h:104
NvencSurface::width
int width
Definition: nvenc.h:62
avcodec.h
ff_nvenc_receive_packet
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: nvenc.c:2172
NvencContext::mapped
int mapped
Definition: nvenc.h:146
NvencContext::level
int level
Definition: nvenc.h:166
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
NvencContext::weighted_pred
int weighted_pred
Definition: nvenc.h:191
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
ff_nvenc_encode_close
int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:1414
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
NVENC_TWO_PASSES
@ NVENC_TWO_PASSES
Definition: nvenc.h:112
NvencSurface::height
int height
Definition: nvenc.h:63
config.h
NvencContext::first_packet_output
int first_packet_output
Definition: nvenc.h:158
NvencSurface::output_surface
NV_ENC_OUTPUT_PTR output_surface
Definition: nvenc.h:66
NvencContext::async_depth
int async_depth
Definition: nvenc.h:173
NV_ENC_H264_PROFILE_MAIN
@ NV_ENC_H264_PROFILE_MAIN
Definition: nvenc.h:97
NvencDynLoadFunctions::nvenc_funcs
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:76
NV_ENC_H264_PROFILE_BASELINE
@ NV_ENC_H264_PROFILE_BASELINE
Definition: nvenc.h:96
NvencContext::nb_registered_frames
int nb_registered_frames
Definition: nvenc.h:149
NvencContext::aud
int aud
Definition: nvenc.h:185
NvencContext::coder
int coder
Definition: nvenc.h:192
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
NvencContext::tier
int tier
Definition: nvenc.h:167
ff_nvenc_pix_fmts
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:42
ID3D11Device
void ID3D11Device
Definition: nvenc.h:28
NvencContext::cu_context_internal
CUcontext cu_context_internal
Definition: nvenc.h:129
NV_ENC_HEVC_PROFILE_MAIN
@ NV_ENC_HEVC_PROFILE_MAIN
Definition: nvenc.h:103
NvencContext::strict_gop
int strict_gop
Definition: nvenc.h:182
hwcontext_d3d11va.h
ANY_DEVICE
@ ANY_DEVICE
Definition: nvenc.h:117
NvencContext::output_surface_queue
AVFifoBuffer * output_surface_queue
Definition: nvenc.h:136
NvencContext::d3d11_device
ID3D11Device * d3d11_device
Definition: nvenc.h:130