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 <nvEncodeAPI.h>
26 #include "libavutil/fifo.h"
27 #include "libavutil/opt.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;
60 NV_ENC_OUTPUT_PTR output_surface;
61 NV_ENC_BUFFER_FORMAT format;
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);
75 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
77 typedef struct NvencDynLoadFunctions
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;
101 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
102 int nvenc_device_count;
103 CUdevice nvenc_devices[16];
105 } NvencDynLoadFunctions;
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
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,
130 NVENC_LOWLATENCY = 1,
133 NVENC_TWO_PASSES = 8,
136 typedef struct NvencContext
140 NvencDynLoadFunctions nvenc_dload_funcs;
142 NV_ENC_INITIALIZE_PARAMS init_encode_params;
143 NV_ENC_CONFIG encode_config;
144 CUcontext cu_context;
145 CUcontext cu_context_internal;
147 int max_surface_count;
148 NvencSurface *surfaces;
150 AVFifoBuffer *output_surface_queue;
151 AVFifoBuffer *output_surface_ready_queue;
152 AVFifoBuffer *timestamp_list;
156 NV_ENC_REGISTERED_PTR regptr;
158 } registered_frames[MAX_REGISTERED_FRAMES];
159 int nb_registered_frames;
161 /* the actual data pixel format, different from
162 * AVCodecContext.pix_fmt when using hwaccel frames on input */
163 enum AVPixelFormat data_pix_fmt;
180 int ff_nvenc_encode_init(AVCodecContext *avctx);
182 int ff_nvenc_encode_close(AVCodecContext *avctx);
184 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
185 const AVFrame *frame, int *got_packet);
187 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
189 #endif /* AVCODEC_NVENC_H */