]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
Merge commit 'c513fcd7d235aa4cef45a6c3125bd4dcc03bf276'
[ffmpeg] / libavcodec / nvenc.h
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
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;
60     AVFrame *in_ref;
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;
77     int nvenc_device_count;
78 } NvencDynLoadFunctions;
79
80 enum {
81     PRESET_DEFAULT = 0,
82     PRESET_SLOW,
83     PRESET_MEDIUM,
84     PRESET_FAST,
85     PRESET_HP,
86     PRESET_HQ,
87     PRESET_BD ,
88     PRESET_LOW_LATENCY_DEFAULT ,
89     PRESET_LOW_LATENCY_HQ ,
90     PRESET_LOW_LATENCY_HP,
91     PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
92     PRESET_LOSSLESS_HP,
93 };
94
95 enum {
96     NV_ENC_H264_PROFILE_BASELINE,
97     NV_ENC_H264_PROFILE_MAIN,
98     NV_ENC_H264_PROFILE_HIGH,
99     NV_ENC_H264_PROFILE_HIGH_444P,
100 };
101
102 enum {
103     NV_ENC_HEVC_PROFILE_MAIN,
104     NV_ENC_HEVC_PROFILE_MAIN_10,
105     NV_ENC_HEVC_PROFILE_REXT,
106 };
107
108 enum {
109     NVENC_LOWLATENCY = 1,
110     NVENC_LOSSLESS   = 2,
111     NVENC_ONE_PASS   = 4,
112     NVENC_TWO_PASSES = 8,
113 };
114
115 enum {
116     LIST_DEVICES = -2,
117     ANY_DEVICE,
118 };
119
120 typedef struct NvencContext
121 {
122     AVClass *avclass;
123
124     NvencDynLoadFunctions nvenc_dload_funcs;
125
126     NV_ENC_INITIALIZE_PARAMS init_encode_params;
127     NV_ENC_CONFIG encode_config;
128     CUcontext cu_context;
129     CUcontext cu_context_internal;
130     ID3D11Device *d3d11_device;
131
132     int nb_surfaces;
133     NvencSurface *surfaces;
134
135     AVFifoBuffer *unused_surface_queue;
136     AVFifoBuffer *output_surface_queue;
137     AVFifoBuffer *output_surface_ready_queue;
138     AVFifoBuffer *timestamp_list;
139
140     int encoder_flushing;
141
142     struct {
143         void *ptr;
144         int ptr_index;
145         NV_ENC_REGISTERED_PTR regptr;
146         int mapped;
147         NV_ENC_MAP_INPUT_RESOURCE in_map;
148     } registered_frames[MAX_REGISTERED_FRAMES];
149     int nb_registered_frames;
150
151     /* the actual data pixel format, different from
152      * AVCodecContext.pix_fmt when using hwaccel frames on input */
153     enum AVPixelFormat data_pix_fmt;
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];
158     int first_packet_output;
159
160     int support_dyn_bitrate;
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;
173     int async_depth;
174     int rc_lookahead;
175     int aq;
176     int no_scenecut;
177     int forced_idr;
178     int b_adapt;
179     int temporal_aq;
180     int zerolatency;
181     int nonref_p;
182     int strict_gop;
183     int aq_strength;
184     float quality;
185     int aud;
186     int bluray_compat;
187     int init_qp_p;
188     int init_qp_b;
189     int init_qp_i;
190     int cqp;
191     int weighted_pred;
192     int coder;
193     int b_ref_mode;
194     int a53_cc;
195 } NvencContext;
196
197 int ff_nvenc_encode_init(AVCodecContext *avctx);
198
199 int ff_nvenc_encode_close(AVCodecContext *avctx);
200
201 int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame);
202
203 int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
204
205 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
206                           const AVFrame *frame, int *got_packet);
207
208 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
209
210 #endif /* AVCODEC_NVENC_H */