2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVCODEC_NVENC_H
20 #define AVCODEC_NVENC_H
22 #include "compat/nvenc/nvEncodeAPI.h"
26 #include "libavutil/fifo.h"
27 #include "libavutil/opt.h"
32 #include "libavutil/hwcontext_cuda.h"
36 #define CUDAAPI __stdcall
41 typedef enum cudaError_enum {
45 typedef void* CUcontext;
46 typedef void* CUdeviceptr;
49 #define MAX_REGISTERED_FRAMES 64
51 typedef struct NvencSurface
53 NV_ENC_INPUT_PTR input_surface;
55 NV_ENC_MAP_INPUT_RESOURCE in_map;
61 NV_ENC_OUTPUT_PTR output_surface;
62 NV_ENC_BUFFER_FORMAT format;
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);
76 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPIGETMAXSUPPORTEDVERSION)(uint32_t* version);
77 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
79 typedef struct NvencDynLoadFunctions
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;
95 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
96 int nvenc_device_count;
97 } NvencDynLoadFunctions;
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
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,
122 NV_ENC_HEVC_PROFILE_MAIN,
123 NV_ENC_HEVC_PROFILE_MAIN_10,
127 NVENC_LOWLATENCY = 1,
130 NVENC_TWO_PASSES = 8,
138 typedef struct NvencContext
142 NvencDynLoadFunctions nvenc_dload_funcs;
144 NV_ENC_INITIALIZE_PARAMS init_encode_params;
145 NV_ENC_CONFIG encode_config;
146 CUcontext cu_context;
147 CUcontext cu_context_internal;
150 NvencSurface *surfaces;
152 AVFifoBuffer *output_surface_queue;
153 AVFifoBuffer *output_surface_ready_queue;
154 AVFifoBuffer *timestamp_list;
158 NV_ENC_REGISTERED_PTR regptr;
160 } registered_frames[MAX_REGISTERED_FRAMES];
161 int nb_registered_frames;
163 /* the actual data pixel format, different from
164 * AVCodecContext.pix_fmt when using hwaccel frames on input */
165 enum AVPixelFormat data_pix_fmt;
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;
187 int ff_nvenc_encode_init(AVCodecContext *avctx);
189 int ff_nvenc_encode_close(AVCodecContext *avctx);
191 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
192 const AVFrame *frame, int *got_packet);
194 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
196 #endif /* AVCODEC_NVENC_H */