]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
Merge commit '2e5bde956519ae19cedfa482e199518e495bcaf5'
[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 "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
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     void *cuda;
81 #endif
82     void *nvenc;
83
84     PCUINIT cu_init;
85     PCUDEVICEGETCOUNT cu_device_get_count;
86     PCUDEVICEGET cu_device_get;
87     PCUDEVICEGETNAME cu_device_get_name;
88     PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
89     PCUCTXCREATE cu_ctx_create;
90     PCUCTXPOPCURRENT cu_ctx_pop_current;
91     PCUCTXDESTROY cu_ctx_destroy;
92
93     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
94     int nvenc_device_count;
95 } NvencDynLoadFunctions;
96
97 enum {
98     PRESET_DEFAULT = 0,
99     PRESET_SLOW,
100     PRESET_MEDIUM,
101     PRESET_FAST,
102     PRESET_HP,
103     PRESET_HQ,
104     PRESET_BD ,
105     PRESET_LOW_LATENCY_DEFAULT ,
106     PRESET_LOW_LATENCY_HQ ,
107     PRESET_LOW_LATENCY_HP,
108     PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
109     PRESET_LOSSLESS_HP,
110 };
111
112 enum {
113     NV_ENC_H264_PROFILE_BASELINE,
114     NV_ENC_H264_PROFILE_MAIN,
115     NV_ENC_H264_PROFILE_HIGH,
116     NV_ENC_H264_PROFILE_HIGH_444P,
117 };
118
119 enum {
120     NVENC_LOWLATENCY = 1,
121     NVENC_LOSSLESS   = 2,
122     NVENC_ONE_PASS   = 4,
123     NVENC_TWO_PASSES = 8,
124 };
125
126 enum {
127     LIST_DEVICES = -2,
128     ANY_DEVICE,
129 };
130
131 typedef struct NvencContext
132 {
133     AVClass *avclass;
134
135     NvencDynLoadFunctions nvenc_dload_funcs;
136
137     NV_ENC_INITIALIZE_PARAMS init_encode_params;
138     NV_ENC_CONFIG encode_config;
139     CUcontext cu_context;
140     CUcontext cu_context_internal;
141
142     int nb_surfaces;
143     NvencSurface *surfaces;
144
145     AVFifoBuffer *output_surface_queue;
146     AVFifoBuffer *output_surface_ready_queue;
147     AVFifoBuffer *timestamp_list;
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 *nvencoder;
166
167     int preset;
168     int profile;
169     int level;
170     int tier;
171     int rc;
172     int cbr;
173     int twopass;
174     int device;
175     int flags;
176     int async_depth;
177 } NvencContext;
178
179 int ff_nvenc_encode_init(AVCodecContext *avctx);
180
181 int ff_nvenc_encode_close(AVCodecContext *avctx);
182
183 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
184                           const AVFrame *frame, int *got_packet);
185
186 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
187
188 #endif /* AVCODEC_NVENC_H */