• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26 #include "libavutil/hwcontext_d3d11va.h"
27 #else
28 typedef void ID3D11Device;
29 #endif
30 
31 #include <ffnvcodec/nvEncodeAPI.h>
32 
33 #include "compat/cuda/dynlink_loader.h"
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;
76     AVFrame *in_ref;
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;
92     int nvenc_device_count;
93 } NvencDynLoadFunctions;
94 
95 enum {
96     PRESET_DEFAULT = 0,
97     PRESET_SLOW,
98     PRESET_MEDIUM,
99     PRESET_FAST,
100     PRESET_HP,
101     PRESET_HQ,
102     PRESET_BD ,
103     PRESET_LOW_LATENCY_DEFAULT ,
104     PRESET_LOW_LATENCY_HQ ,
105     PRESET_LOW_LATENCY_HP,
106     PRESET_LOSSLESS_DEFAULT,
107     PRESET_LOSSLESS_HP,
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 {
120     NV_ENC_H264_PROFILE_BASELINE,
121     NV_ENC_H264_PROFILE_MAIN,
122     NV_ENC_H264_PROFILE_HIGH,
123     NV_ENC_H264_PROFILE_HIGH_444P,
124 };
125 
126 enum {
127     NV_ENC_HEVC_PROFILE_MAIN,
128     NV_ENC_HEVC_PROFILE_MAIN_10,
129     NV_ENC_HEVC_PROFILE_REXT,
130 };
131 
132 enum {
133     NVENC_LOWLATENCY = 1,
134     NVENC_LOSSLESS   = 2,
135     NVENC_ONE_PASS   = 4,
136     NVENC_TWO_PASSES = 8,
137 
138     NVENC_DEPRECATED_PRESET = 0x8000,
139 };
140 
141 enum {
142     LIST_DEVICES = -2,
143     ANY_DEVICE,
144 };
145 
146 typedef struct NvencContext
147 {
148     AVClass *avclass;
149 
150     NvencDynLoadFunctions nvenc_dload_funcs;
151 
152     NV_ENC_INITIALIZE_PARAMS init_encode_params;
153     NV_ENC_CONFIG encode_config;
154     CUcontext cu_context;
155     CUcontext cu_context_internal;
156     CUstream cu_stream;
157     ID3D11Device *d3d11_device;
158 
159     AVFrame *frame;
160 
161     int nb_surfaces;
162     NvencSurface *surfaces;
163 
164     AVFifoBuffer *unused_surface_queue;
165     AVFifoBuffer *output_surface_queue;
166     AVFifoBuffer *output_surface_ready_queue;
167     AVFifoBuffer *timestamp_list;
168 
169     struct {
170         void *ptr;
171         int ptr_index;
172         NV_ENC_REGISTERED_PTR regptr;
173         int mapped;
174         NV_ENC_MAP_INPUT_RESOURCE in_map;
175     } registered_frames[MAX_REGISTERED_FRAMES];
176     int nb_registered_frames;
177 
178     /* the actual data pixel format, different from
179      * AVCodecContext.pix_fmt when using hwaccel frames on input */
180     enum AVPixelFormat data_pix_fmt;
181 
182     int support_dyn_bitrate;
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;
195     int async_depth;
196     int rc_lookahead;
197     int aq;
198     int no_scenecut;
199     int forced_idr;
200     int b_adapt;
201     int temporal_aq;
202     int zerolatency;
203     int nonref_p;
204     int strict_gop;
205     int aq_strength;
206     float quality;
207     int aud;
208     int bluray_compat;
209     int init_qp_p;
210     int init_qp_b;
211     int init_qp_i;
212     int cqp;
213     int weighted_pred;
214     int coder;
215     int b_ref_mode;
216     int a53_cc;
217     int s12m_tc;
218     int dpb_size;
219     int tuning_info;
220     int multipass;
221     int ldkfs;
222 } NvencContext;
223 
224 int ff_nvenc_encode_init(AVCodecContext *avctx);
225 
226 int ff_nvenc_encode_close(AVCodecContext *avctx);
227 
228 int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
229 
230 void ff_nvenc_encode_flush(AVCodecContext *avctx);
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 */
236