]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
avcodec/nvenc: convert levels to AVOptions
[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 <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 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
60     NV_ENC_OUTPUT_PTR output_surface;
61     NV_ENC_BUFFER_FORMAT format;
62     int size;
63     int lockCount;
64 } NvencSurface;
65
66 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
67 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
68 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
69 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
70 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
71 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
72 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
73 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
74
75 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
76
77 typedef struct NvencDynLoadFunctions
78 {
79 #if !CONFIG_CUDA
80 #if defined(_WIN32)
81     HMODULE cuda_lib;
82 #else
83     void* cuda_lib;
84 #endif
85 #endif
86 #if defined(_WIN32)
87     HMODULE nvenc_lib;
88 #else
89     void* nvenc_lib;
90 #endif
91
92     PCUINIT cu_init;
93     PCUDEVICEGETCOUNT cu_device_get_count;
94     PCUDEVICEGET cu_device_get;
95     PCUDEVICEGETNAME cu_device_get_name;
96     PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
97     PCUCTXCREATE cu_ctx_create;
98     PCUCTXPOPCURRENT cu_ctx_pop_current;
99     PCUCTXDESTROY cu_ctx_destroy;
100
101     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
102     int nvenc_device_count;
103     CUdevice nvenc_devices[16];
104
105 } NvencDynLoadFunctions;
106
107 enum {
108     PRESET_DEFAULT = 0,
109     PRESET_SLOW,
110     PRESET_MEDIUM,
111     PRESET_FAST,
112     PRESET_HP,
113     PRESET_HQ,
114     PRESET_BD ,
115     PRESET_LOW_LATENCY_DEFAULT ,
116     PRESET_LOW_LATENCY_HQ ,
117     PRESET_LOW_LATENCY_HP,
118     PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
119     PRESET_LOSSLESS_HP,
120 };
121
122 enum {
123     NV_ENC_H264_PROFILE_BASELINE,
124     NV_ENC_H264_PROFILE_MAIN,
125     NV_ENC_H264_PROFILE_HIGH,
126     NV_ENC_H264_PROFILE_HIGH_444P,
127 };
128
129 enum {
130     NVENC_LOWLATENCY = 1,
131     NVENC_LOSSLESS   = 2,
132     NVENC_ONE_PASS   = 4,
133     NVENC_TWO_PASSES = 8,
134 };
135
136 typedef struct NvencContext
137 {
138     AVClass *avclass;
139
140     NvencDynLoadFunctions nvenc_dload_funcs;
141
142     NV_ENC_INITIALIZE_PARAMS init_encode_params;
143     NV_ENC_CONFIG encode_config;
144     CUcontext cu_context;
145     CUcontext cu_context_internal;
146
147     int max_surface_count;
148     NvencSurface *surfaces;
149
150     AVFifoBuffer *output_surface_queue;
151     AVFifoBuffer *output_surface_ready_queue;
152     AVFifoBuffer *timestamp_list;
153
154     struct {
155         CUdeviceptr ptr;
156         NV_ENC_REGISTERED_PTR regptr;
157         int mapped;
158     } registered_frames[MAX_REGISTERED_FRAMES];
159     int nb_registered_frames;
160
161     /* the actual data pixel format, different from
162      * AVCodecContext.pix_fmt when using hwaccel frames on input */
163     enum AVPixelFormat data_pix_fmt;
164
165     int64_t last_dts;
166
167     void *nvencoder;
168
169     int preset;
170     int profile;
171     int level;
172     char *tier;
173     int cbr;
174     int twopass;
175     int gpu;
176     int flags;
177     int buffer_delay;
178 } NvencContext;
179
180 int ff_nvenc_encode_init(AVCodecContext *avctx);
181
182 int ff_nvenc_encode_close(AVCodecContext *avctx);
183
184 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
185                           const AVFrame *frame, int *got_packet);
186
187 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
188
189 #endif /* AVCODEC_NVENC_H */