]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
3602f16e83b14b4ff2df57381e2ff99784e44e73
[ffmpeg] / libavcodec / nvenc.h
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 <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 <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 NVENCFrame {
52     NV_ENC_INPUT_PTR  in;
53     AVFrame          *in_ref;
54     NV_ENC_MAP_INPUT_RESOURCE in_map;
55     int reg_idx;
56
57     NV_ENC_OUTPUT_PTR out;
58     NV_ENC_BUFFER_FORMAT format;
59     int locked;
60 } NVENCFrame;
61
62 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
63 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
64 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
65 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
66 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
67 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
68 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
69 typedef CUresult(CUDAAPI *PCUCTXPUSHCURRENT)(CUcontext ctx);
70 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
71
72 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
73
74 typedef struct NVENCLibraryContext
75 {
76 #if !CONFIG_CUDA
77     void *cuda;
78 #endif
79     void *nvenc;
80
81     PCUINIT cu_init;
82     PCUDEVICEGETCOUNT cu_device_get_count;
83     PCUDEVICEGET cu_device_get;
84     PCUDEVICEGETNAME cu_device_get_name;
85     PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
86     PCUCTXCREATE cu_ctx_create;
87     PCUCTXPOPCURRENT cu_ctx_pop_current;
88     PCUCTXPUSHCURRENT cu_ctx_push_current;
89     PCUCTXDESTROY cu_ctx_destroy;
90
91     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
92 } NVENCLibraryContext;
93
94 enum {
95     PRESET_DEFAULT,
96     PRESET_SLOW,
97     PRESET_MEDIUM,
98     PRESET_FAST,
99     PRESET_HP,
100     PRESET_HQ,
101     PRESET_BD ,
102     PRESET_LOW_LATENCY_DEFAULT ,
103     PRESET_LOW_LATENCY_HQ ,
104     PRESET_LOW_LATENCY_HP,
105     PRESET_LOSSLESS_DEFAULT,
106     PRESET_LOSSLESS_HP,
107 };
108
109 enum {
110     NV_ENC_H264_PROFILE_BASELINE,
111     NV_ENC_H264_PROFILE_MAIN,
112     NV_ENC_H264_PROFILE_HIGH,
113     NV_ENC_H264_PROFILE_HIGH_444,
114     NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
115 };
116
117 enum {
118     NV_ENC_HEVC_PROFILE_MAIN,
119     NV_ENC_HEVC_PROFILE_MAIN_10,
120     NV_ENC_HEVC_PROFILE_REXT,
121 };
122
123 enum {
124     NVENC_LOWLATENCY = 1,
125     NVENC_LOSSLESS   = 2,
126     NVENC_ONE_PASS   = 4,
127     NVENC_TWO_PASSES = 8,
128 };
129
130 enum {
131     LIST_DEVICES = -2,
132     ANY_DEVICE,
133 };
134
135 typedef struct NVENCContext {
136     AVClass *class;
137     NVENCLibraryContext nvel;
138
139     NV_ENC_INITIALIZE_PARAMS params;
140     NV_ENC_CONFIG config;
141
142     CUcontext cu_context;
143     CUcontext cu_context_internal;
144
145     int nb_surfaces;
146     NVENCFrame *frames;
147     AVFifoBuffer *timestamps;
148     AVFifoBuffer *pending, *ready;
149
150     struct {
151         CUdeviceptr ptr;
152         NV_ENC_REGISTERED_PTR regptr;
153         int mapped;
154     } registered_frames[MAX_REGISTERED_FRAMES];
155     int nb_registered_frames;
156
157     /* the actual data pixel format, different from
158      * AVCodecContext.pix_fmt when using hwaccel frames on input */
159     enum AVPixelFormat data_pix_fmt;
160
161     /* timestamps of the first two frames, for computing the first dts
162      * when B-frames are present */
163     int64_t initial_pts[2];
164     int first_packet_output;
165
166     void *nvenc_ctx;
167
168     int preset;
169     int profile;
170     int level;
171     int tier;
172     int rc;
173     int device;
174     int flags;
175     int async_depth;
176     int rc_lookahead;
177     int aq;
178     int no_scenecut;
179     int b_adapt;
180     int temporal_aq;
181     int zerolatency;
182     int nonref_p;
183     int strict_gop;
184     int aq_strength;
185     int quality;
186     int init_qp_p;
187     int init_qp_b;
188     int init_qp_i;
189 } NVENCContext;
190
191 int ff_nvenc_encode_init(AVCodecContext *avctx);
192
193 int ff_nvenc_encode_close(AVCodecContext *avctx);
194
195 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
196                           const AVFrame *frame, int *got_packet);
197
198 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
199
200 #endif /* AVCODEC_NVENC_H */