xref: /third_party/ffmpeg/libavcodec/nvenc.h (revision cabdff1a)
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
28typedef 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#define NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
72#endif
73
74// SDK 11.1 compile time feature checks
75#if NVENCAPI_CHECK_VERSION(11, 1)
76#define NVENC_HAVE_QP_CHROMA_OFFSETS
77#define NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
78#endif
79
80typedef struct NvencSurface
81{
82    NV_ENC_INPUT_PTR input_surface;
83    AVFrame *in_ref;
84    int reg_idx;
85    int width;
86    int height;
87    int pitch;
88
89    NV_ENC_OUTPUT_PTR output_surface;
90    NV_ENC_BUFFER_FORMAT format;
91} NvencSurface;
92
93typedef struct NvencDynLoadFunctions
94{
95    CudaFunctions *cuda_dl;
96    NvencFunctions *nvenc_dl;
97
98    NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
99    int nvenc_device_count;
100} NvencDynLoadFunctions;
101
102enum {
103    PRESET_DEFAULT = 0,
104    PRESET_SLOW,
105    PRESET_MEDIUM,
106    PRESET_FAST,
107    PRESET_HP,
108    PRESET_HQ,
109    PRESET_BD ,
110    PRESET_LOW_LATENCY_DEFAULT ,
111    PRESET_LOW_LATENCY_HQ ,
112    PRESET_LOW_LATENCY_HP,
113    PRESET_LOSSLESS_DEFAULT,
114    PRESET_LOSSLESS_HP,
115#ifdef NVENC_HAVE_NEW_PRESETS
116    PRESET_P1,
117    PRESET_P2,
118    PRESET_P3,
119    PRESET_P4,
120    PRESET_P5,
121    PRESET_P6,
122    PRESET_P7,
123#endif
124};
125
126enum {
127    NV_ENC_H264_PROFILE_BASELINE,
128    NV_ENC_H264_PROFILE_MAIN,
129    NV_ENC_H264_PROFILE_HIGH,
130    NV_ENC_H264_PROFILE_HIGH_444P,
131};
132
133enum {
134    NV_ENC_HEVC_PROFILE_MAIN,
135    NV_ENC_HEVC_PROFILE_MAIN_10,
136    NV_ENC_HEVC_PROFILE_REXT,
137};
138
139enum {
140    NVENC_LOWLATENCY = 1,
141    NVENC_LOSSLESS   = 2,
142    NVENC_ONE_PASS   = 4,
143    NVENC_TWO_PASSES = 8,
144
145    NVENC_DEPRECATED_PRESET = 0x8000,
146};
147
148enum {
149    LIST_DEVICES = -2,
150    ANY_DEVICE,
151};
152
153typedef struct NvencContext
154{
155    AVClass *avclass;
156
157    NvencDynLoadFunctions nvenc_dload_funcs;
158
159    NV_ENC_INITIALIZE_PARAMS init_encode_params;
160    NV_ENC_CONFIG encode_config;
161    CUcontext cu_context;
162    CUcontext cu_context_internal;
163    CUstream cu_stream;
164    ID3D11Device *d3d11_device;
165
166    AVFrame *frame;
167
168    int nb_surfaces;
169    NvencSurface *surfaces;
170
171    AVFifo *unused_surface_queue;
172    AVFifo *output_surface_queue;
173    AVFifo *output_surface_ready_queue;
174    AVFifo *timestamp_list;
175
176    NV_ENC_SEI_PAYLOAD *sei_data;
177    int sei_data_size;
178
179    struct {
180        void *ptr;
181        int ptr_index;
182        NV_ENC_REGISTERED_PTR regptr;
183        int mapped;
184        NV_ENC_MAP_INPUT_RESOURCE in_map;
185    } registered_frames[MAX_REGISTERED_FRAMES];
186    int nb_registered_frames;
187
188    /* the actual data pixel format, different from
189     * AVCodecContext.pix_fmt when using hwaccel frames on input */
190    enum AVPixelFormat data_pix_fmt;
191
192    int support_dyn_bitrate;
193
194    void *nvencoder;
195
196    int preset;
197    int profile;
198    int level;
199    int tier;
200    int rc;
201    int cbr;
202    int twopass;
203    int device;
204    int flags;
205    int async_depth;
206    int rc_lookahead;
207    int aq;
208    int no_scenecut;
209    int forced_idr;
210    int b_adapt;
211    int temporal_aq;
212    int zerolatency;
213    int nonref_p;
214    int strict_gop;
215    int aq_strength;
216    float quality;
217    int aud;
218    int bluray_compat;
219    int init_qp_p;
220    int init_qp_b;
221    int init_qp_i;
222    int cqp;
223    int qp_cb_offset;
224    int qp_cr_offset;
225    int weighted_pred;
226    int coder;
227    int b_ref_mode;
228    int a53_cc;
229    int s12m_tc;
230    int dpb_size;
231    int tuning_info;
232    int multipass;
233    int ldkfs;
234    int extra_sei;
235    int intra_refresh;
236    int single_slice_intra_refresh;
237    int constrained_encoding;
238    int udu_sei;
239} NvencContext;
240
241int ff_nvenc_encode_init(AVCodecContext *avctx);
242
243int ff_nvenc_encode_close(AVCodecContext *avctx);
244
245int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
246
247void ff_nvenc_encode_flush(AVCodecContext *avctx);
248
249extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
250extern const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[];
251
252#endif /* AVCODEC_NVENC_H */
253