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