Allow linking to CUDA dynamically instead of dlopen()ing it at runtime
[libav.git] / libavcodec / nvenc.h
1 /*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #ifndef AVCODEC_NVENC_H
20 #define AVCODEC_NVENC_H
21
22 #include <cuda.h>
23 #include <nvEncodeAPI.h>
24
25 #include "config.h"
26
27 #include "libavutil/fifo.h"
28 #include "libavutil/opt.h"
29
30 #include "avcodec.h"
31
32 typedef struct NVENCFrame {
33 NV_ENC_INPUT_PTR in;
34 NV_ENC_OUTPUT_PTR out;
35 NV_ENC_BUFFER_FORMAT format;
36 int locked;
37 } NVENCFrame;
38
39 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
40 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
41 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
42 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
43 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
44 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
45 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
46 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
47
48 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
49
50 typedef struct NVENCLibraryContext
51 {
52 #if !CONFIG_CUDA
53 void *cuda;
54 #endif
55 void *nvenc;
56
57 PCUINIT cu_init;
58 PCUDEVICEGETCOUNT cu_device_get_count;
59 PCUDEVICEGET cu_device_get;
60 PCUDEVICEGETNAME cu_device_get_name;
61 PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
62 PCUCTXCREATE cu_ctx_create;
63 PCUCTXPOPCURRENT cu_ctx_pop_current;
64 PCUCTXDESTROY cu_ctx_destroy;
65
66 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
67 } NVENCLibraryContext;
68
69 enum {
70 PRESET_DEFAULT,
71 PRESET_HP,
72 PRESET_HQ,
73 PRESET_BD ,
74 PRESET_LOW_LATENCY_DEFAULT ,
75 PRESET_LOW_LATENCY_HQ ,
76 PRESET_LOW_LATENCY_HP,
77 PRESET_LOSSLESS_DEFAULT,
78 PRESET_LOSSLESS_HP,
79 };
80
81 enum {
82 NV_ENC_H264_PROFILE_BASELINE,
83 NV_ENC_H264_PROFILE_MAIN,
84 NV_ENC_H264_PROFILE_HIGH,
85 NV_ENC_H264_PROFILE_HIGH_444,
86 NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
87 };
88
89 enum {
90 NVENC_LOWLATENCY = 1,
91 NVENC_LOSSLESS,
92 };
93
94 enum {
95 LIST_DEVICES = -2,
96 ANY_DEVICE,
97 };
98
99 typedef struct NVENCContext {
100 AVClass *class;
101 NVENCLibraryContext nvel;
102
103 NV_ENC_INITIALIZE_PARAMS params;
104 NV_ENC_CONFIG config;
105
106 CUcontext cu_context;
107
108 int nb_surfaces;
109 NVENCFrame *frames;
110 AVFifoBuffer *timestamps;
111 AVFifoBuffer *pending, *ready;
112
113 /* timestamps of the first two frames, for computing the first dts
114 * when b-frames are present */
115 int64_t initial_pts[2];
116 int first_packet_output;
117
118 void *nvenc_ctx;
119
120 int preset;
121 int profile;
122 int level;
123 int tier;
124 int rc;
125 int device;
126 int flags;
127 } NVENCContext;
128
129 int ff_nvenc_encode_init(AVCodecContext *avctx);
130
131 int ff_nvenc_encode_close(AVCodecContext *avctx);
132
133 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
134 const AVFrame *frame, int *got_packet);
135
136 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
137
138 #endif /* AVCODEC_NVENC_H */