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