]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
avcodec/nvenc: correctly set inputPitch
[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 };
125
126 enum {
127     NVENC_LOWLATENCY = 1,
128     NVENC_LOSSLESS   = 2,
129     NVENC_ONE_PASS   = 4,
130     NVENC_TWO_PASSES = 8,
131 };
132
133 enum {
134     LIST_DEVICES = -2,
135     ANY_DEVICE,
136 };
137
138 typedef struct NvencContext
139 {
140     AVClass *avclass;
141
142     NvencDynLoadFunctions nvenc_dload_funcs;
143
144     NV_ENC_INITIALIZE_PARAMS init_encode_params;
145     NV_ENC_CONFIG encode_config;
146     CUcontext cu_context;
147     CUcontext cu_context_internal;
148
149     int nb_surfaces;
150     NvencSurface *surfaces;
151
152     AVFifoBuffer *output_surface_queue;
153     AVFifoBuffer *output_surface_ready_queue;
154     AVFifoBuffer *timestamp_list;
155
156     struct {
157         CUdeviceptr ptr;
158         NV_ENC_REGISTERED_PTR regptr;
159         int mapped;
160     } registered_frames[MAX_REGISTERED_FRAMES];
161     int nb_registered_frames;
162
163     /* the actual data pixel format, different from
164      * AVCodecContext.pix_fmt when using hwaccel frames on input */
165     enum AVPixelFormat data_pix_fmt;
166
167     /* timestamps of the first two frames, for computing the first dts
168      * when B-frames are present */
169     int64_t initial_pts[2];
170     int first_packet_output;
171
172     void *nvencoder;
173
174     int preset;
175     int profile;
176     int level;
177     int tier;
178     int rc;
179     int cbr;
180     int twopass;
181     int device;
182     int flags;
183     int async_depth;
184     int rc_lookahead;
185 } NvencContext;
186
187 int ff_nvenc_encode_init(AVCodecContext *avctx);
188
189 int ff_nvenc_encode_close(AVCodecContext *avctx);
190
191 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
192                           const AVFrame *frame, int *got_packet);
193
194 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
195
196 #endif /* AVCODEC_NVENC_H */