]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
lavf/segment: fix writing separate header with auto BSF
[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 "compat/nvenc/nvEncodeAPI.h"
23
24 #include "config.h"
25
26 #include "libavutil/fifo.h"
27 #include "libavutil/opt.h"
28
29 #include "avcodec.h"
30
31 #if CONFIG_CUDA
32 #include "libavutil/hwcontext_cuda.h"
33 #else
34
35 #if defined(_WIN32)
36 #define CUDAAPI __stdcall
37 #else
38 #define CUDAAPI
39 #endif
40
41 typedef enum cudaError_enum {
42     CUDA_SUCCESS = 0
43 } CUresult;
44 typedef int CUdevice;
45 typedef void* CUcontext;
46 typedef void* CUdeviceptr;
47 #endif
48
49 #define MAX_REGISTERED_FRAMES 64
50
51 typedef struct NvencSurface
52 {
53     NV_ENC_INPUT_PTR input_surface;
54     AVFrame *in_ref;
55     NV_ENC_MAP_INPUT_RESOURCE in_map;
56     int reg_idx;
57     int width;
58     int height;
59     int pitch;
60
61     NV_ENC_OUTPUT_PTR output_surface;
62     NV_ENC_BUFFER_FORMAT format;
63     int size;
64     int lockCount;
65 } NvencSurface;
66
67 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
68 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
69 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
70 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
71 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
72 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
73 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
74 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
75
76 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPIGETMAXSUPPORTEDVERSION)(uint32_t* version);
77 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
78
79 typedef struct NvencDynLoadFunctions
80 {
81 #if !CONFIG_CUDA
82     void *cuda;
83 #endif
84     void *nvenc;
85
86     PCUINIT cu_init;
87     PCUDEVICEGETCOUNT cu_device_get_count;
88     PCUDEVICEGET cu_device_get;
89     PCUDEVICEGETNAME cu_device_get_name;
90     PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
91     PCUCTXCREATE cu_ctx_create;
92     PCUCTXPOPCURRENT cu_ctx_pop_current;
93     PCUCTXDESTROY cu_ctx_destroy;
94
95     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
96     int nvenc_device_count;
97 } NvencDynLoadFunctions;
98
99 enum {
100     PRESET_DEFAULT = 0,
101     PRESET_SLOW,
102     PRESET_MEDIUM,
103     PRESET_FAST,
104     PRESET_HP,
105     PRESET_HQ,
106     PRESET_BD ,
107     PRESET_LOW_LATENCY_DEFAULT ,
108     PRESET_LOW_LATENCY_HQ ,
109     PRESET_LOW_LATENCY_HP,
110     PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
111     PRESET_LOSSLESS_HP,
112 };
113
114 enum {
115     NV_ENC_H264_PROFILE_BASELINE,
116     NV_ENC_H264_PROFILE_MAIN,
117     NV_ENC_H264_PROFILE_HIGH,
118     NV_ENC_H264_PROFILE_HIGH_444P,
119 };
120
121 enum {
122     NV_ENC_HEVC_PROFILE_MAIN,
123     NV_ENC_HEVC_PROFILE_MAIN_10,
124     NV_ENC_HEVC_PROFILE_REXT,
125 };
126
127 enum {
128     NVENC_LOWLATENCY = 1,
129     NVENC_LOSSLESS   = 2,
130     NVENC_ONE_PASS   = 4,
131     NVENC_TWO_PASSES = 8,
132 };
133
134 enum {
135     LIST_DEVICES = -2,
136     ANY_DEVICE,
137 };
138
139 typedef struct NvencContext
140 {
141     AVClass *avclass;
142
143     NvencDynLoadFunctions nvenc_dload_funcs;
144
145     NV_ENC_INITIALIZE_PARAMS init_encode_params;
146     NV_ENC_CONFIG encode_config;
147     CUcontext cu_context;
148     CUcontext cu_context_internal;
149
150     int nb_surfaces;
151     NvencSurface *surfaces;
152
153     AVFifoBuffer *output_surface_queue;
154     AVFifoBuffer *output_surface_ready_queue;
155     AVFifoBuffer *timestamp_list;
156
157     struct {
158         CUdeviceptr ptr;
159         NV_ENC_REGISTERED_PTR regptr;
160         int mapped;
161     } registered_frames[MAX_REGISTERED_FRAMES];
162     int nb_registered_frames;
163
164     /* the actual data pixel format, different from
165      * AVCodecContext.pix_fmt when using hwaccel frames on input */
166     enum AVPixelFormat data_pix_fmt;
167
168     /* timestamps of the first two frames, for computing the first dts
169      * when B-frames are present */
170     int64_t initial_pts[2];
171     int first_packet_output;
172
173     void *nvencoder;
174
175     int preset;
176     int profile;
177     int level;
178     int tier;
179     int rc;
180     int cbr;
181     int twopass;
182     int device;
183     int flags;
184     int async_depth;
185     int rc_lookahead;
186     int aq;
187     int no_scenecut;
188     int forced_idr;
189     int b_adapt;
190     int temporal_aq;
191     int zerolatency;
192     int nonref_p;
193     int strict_gop;
194     int aq_strength;
195     int quality;
196 } NvencContext;
197
198 int ff_nvenc_encode_init(AVCodecContext *avctx);
199
200 int ff_nvenc_encode_close(AVCodecContext *avctx);
201
202 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
203                           const AVFrame *frame, int *got_packet);
204
205 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
206
207 #endif /* AVCODEC_NVENC_H */