]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
avcodec/tiff: Replace width overriding for bayer by assert
[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 #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 typedef struct NvencSurface
66 {
67     NV_ENC_INPUT_PTR input_surface;
68     AVFrame *in_ref;
69     int reg_idx;
70     int width;
71     int height;
72     int pitch;
73
74     NV_ENC_OUTPUT_PTR output_surface;
75     NV_ENC_BUFFER_FORMAT format;
76     int size;
77 } NvencSurface;
78
79 typedef struct NvencDynLoadFunctions
80 {
81     CudaFunctions *cuda_dl;
82     NvencFunctions *nvenc_dl;
83
84     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
85     int nvenc_device_count;
86 } NvencDynLoadFunctions;
87
88 enum {
89     PRESET_DEFAULT = 0,
90     PRESET_SLOW,
91     PRESET_MEDIUM,
92     PRESET_FAST,
93     PRESET_HP,
94     PRESET_HQ,
95     PRESET_BD ,
96     PRESET_LOW_LATENCY_DEFAULT ,
97     PRESET_LOW_LATENCY_HQ ,
98     PRESET_LOW_LATENCY_HP,
99     PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
100     PRESET_LOSSLESS_HP,
101 };
102
103 enum {
104     NV_ENC_H264_PROFILE_BASELINE,
105     NV_ENC_H264_PROFILE_MAIN,
106     NV_ENC_H264_PROFILE_HIGH,
107     NV_ENC_H264_PROFILE_HIGH_444P,
108 };
109
110 enum {
111     NV_ENC_HEVC_PROFILE_MAIN,
112     NV_ENC_HEVC_PROFILE_MAIN_10,
113     NV_ENC_HEVC_PROFILE_REXT,
114 };
115
116 enum {
117     NVENC_LOWLATENCY = 1,
118     NVENC_LOSSLESS   = 2,
119     NVENC_ONE_PASS   = 4,
120     NVENC_TWO_PASSES = 8,
121 };
122
123 enum {
124     LIST_DEVICES = -2,
125     ANY_DEVICE,
126 };
127
128 typedef struct NvencContext
129 {
130     AVClass *avclass;
131
132     NvencDynLoadFunctions nvenc_dload_funcs;
133
134     NV_ENC_INITIALIZE_PARAMS init_encode_params;
135     NV_ENC_CONFIG encode_config;
136     CUcontext cu_context;
137     CUcontext cu_context_internal;
138     CUstream cu_stream;
139     ID3D11Device *d3d11_device;
140
141     int nb_surfaces;
142     NvencSurface *surfaces;
143
144     AVFifoBuffer *unused_surface_queue;
145     AVFifoBuffer *output_surface_queue;
146     AVFifoBuffer *output_surface_ready_queue;
147     AVFifoBuffer *timestamp_list;
148
149     int encoder_flushing;
150
151     struct {
152         void *ptr;
153         int ptr_index;
154         NV_ENC_REGISTERED_PTR regptr;
155         int mapped;
156         NV_ENC_MAP_INPUT_RESOURCE in_map;
157     } registered_frames[MAX_REGISTERED_FRAMES];
158     int nb_registered_frames;
159
160     /* the actual data pixel format, different from
161      * AVCodecContext.pix_fmt when using hwaccel frames on input */
162     enum AVPixelFormat data_pix_fmt;
163
164     /* timestamps of the first two frames, for computing the first dts
165      * when B-frames are present */
166     int64_t initial_pts[2];
167     int first_packet_output;
168
169     int support_dyn_bitrate;
170
171     void *nvencoder;
172
173     int preset;
174     int profile;
175     int level;
176     int tier;
177     int rc;
178     int cbr;
179     int twopass;
180     int device;
181     int flags;
182     int async_depth;
183     int rc_lookahead;
184     int aq;
185     int no_scenecut;
186     int forced_idr;
187     int b_adapt;
188     int temporal_aq;
189     int zerolatency;
190     int nonref_p;
191     int strict_gop;
192     int aq_strength;
193     float quality;
194     int aud;
195     int bluray_compat;
196     int init_qp_p;
197     int init_qp_b;
198     int init_qp_i;
199     int cqp;
200     int weighted_pred;
201     int coder;
202     int b_ref_mode;
203     int a53_cc;
204     int dpb_size;
205 } NvencContext;
206
207 int ff_nvenc_encode_init(AVCodecContext *avctx);
208
209 int ff_nvenc_encode_close(AVCodecContext *avctx);
210
211 int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame);
212
213 int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
214
215 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
216                           const AVFrame *frame, int *got_packet);
217
218 void ff_nvenc_encode_flush(AVCodecContext *avctx);
219
220 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
221 extern const AVCodecHWConfigInternal *ff_nvenc_hw_configs[];
222
223 #endif /* AVCODEC_NVENC_H */