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"
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;
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
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;
93 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
94 int nvenc_device_count;
95 } NvencDynLoadFunctions;
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
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,
120 NVENC_LOWLATENCY = 1,
123 NVENC_TWO_PASSES = 8,
131 typedef struct NvencContext
135 NvencDynLoadFunctions nvenc_dload_funcs;
137 NV_ENC_INITIALIZE_PARAMS init_encode_params;
138 NV_ENC_CONFIG encode_config;
139 CUcontext cu_context;
140 CUcontext cu_context_internal;
143 NvencSurface *surfaces;
145 AVFifoBuffer *output_surface_queue;
146 AVFifoBuffer *output_surface_ready_queue;
147 AVFifoBuffer *timestamp_list;
151 NV_ENC_REGISTERED_PTR regptr;
153 } registered_frames[MAX_REGISTERED_FRAMES];
154 int nb_registered_frames;
156 /* the actual data pixel format, different from
157 * AVCodecContext.pix_fmt when using hwaccel frames on input */
158 enum AVPixelFormat data_pix_fmt;
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;
179 int ff_nvenc_encode_init(AVCodecContext *avctx);
181 int ff_nvenc_encode_close(AVCodecContext *avctx);
183 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
184 const AVFrame *frame, int *got_packet);
186 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
188 #endif /* AVCODEC_NVENC_H */