2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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 NVENCFrame {
54 NV_ENC_MAP_INPUT_RESOURCE in_map;
57 NV_ENC_OUTPUT_PTR out;
58 NV_ENC_BUFFER_FORMAT format;
62 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
63 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
64 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
65 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
66 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
67 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
68 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
69 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
71 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
73 typedef struct NVENCLibraryContext
81 PCUDEVICEGETCOUNT cu_device_get_count;
82 PCUDEVICEGET cu_device_get;
83 PCUDEVICEGETNAME cu_device_get_name;
84 PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
85 PCUCTXCREATE cu_ctx_create;
86 PCUCTXPOPCURRENT cu_ctx_pop_current;
87 PCUCTXDESTROY cu_ctx_destroy;
89 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
90 } NVENCLibraryContext;
100 PRESET_LOW_LATENCY_DEFAULT ,
101 PRESET_LOW_LATENCY_HQ ,
102 PRESET_LOW_LATENCY_HP,
103 PRESET_LOSSLESS_DEFAULT,
108 NV_ENC_H264_PROFILE_BASELINE,
109 NV_ENC_H264_PROFILE_MAIN,
110 NV_ENC_H264_PROFILE_HIGH,
111 NV_ENC_H264_PROFILE_HIGH_444,
112 NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
116 NV_ENC_HEVC_PROFILE_MAIN,
117 NV_ENC_HEVC_PROFILE_MAIN_10,
118 NV_ENC_HEVC_PROFILE_REXT,
122 NVENC_LOWLATENCY = 1,
125 NVENC_TWO_PASSES = 8,
133 typedef struct NVENCContext {
135 NVENCLibraryContext nvel;
137 NV_ENC_INITIALIZE_PARAMS params;
138 NV_ENC_CONFIG config;
140 CUcontext cu_context;
141 CUcontext cu_context_internal;
145 AVFifoBuffer *timestamps;
146 AVFifoBuffer *pending, *ready;
150 NV_ENC_REGISTERED_PTR regptr;
152 } registered_frames[MAX_REGISTERED_FRAMES];
153 int nb_registered_frames;
155 /* the actual data pixel format, different from
156 * AVCodecContext.pix_fmt when using hwaccel frames on input */
157 enum AVPixelFormat data_pix_fmt;
159 /* timestamps of the first two frames, for computing the first dts
160 * when B-frames are present */
161 int64_t initial_pts[2];
162 int first_packet_output;
186 int ff_nvenc_encode_init(AVCodecContext *avctx);
188 int ff_nvenc_encode_close(AVCodecContext *avctx);
190 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
191 const AVFrame *frame, int *got_packet);
193 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
195 #endif /* AVCODEC_NVENC_H */