nvenc: H264 and HEVC encoders
[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 "libavutil/fifo.h"
26 #include "libavutil/opt.h"
27
28 #include "avcodec.h"
29
30 typedef struct NVENCInputSurface {
31 NV_ENC_INPUT_PTR in;
32 NV_ENC_BUFFER_FORMAT format;
33 int locked;
34 } NVENCInputSurface;
35
36 typedef struct NVENCOutputSurface {
37 NV_ENC_OUTPUT_PTR out;
38 NVENCInputSurface *in;
39 int busy;
40 } NVENCOutputSurface;
41
42 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
43 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
44 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
45 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
46 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
47 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
48 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
49 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
50
51 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
52
53 typedef struct NVENCLibraryContext
54 {
55 void *cuda;
56 void *nvenc;
57
58 PCUINIT cu_init;
59 PCUDEVICEGETCOUNT cu_device_get_count;
60 PCUDEVICEGET cu_device_get;
61 PCUDEVICEGETNAME cu_device_get_name;
62 PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
63 PCUCTXCREATE cu_ctx_create;
64 PCUCTXPOPCURRENT cu_ctx_pop_current;
65 PCUCTXDESTROY cu_ctx_destroy;
66
67 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
68 } NVENCLibraryContext;
69
70 enum {
71 PRESET_DEFAULT,
72 PRESET_HP,
73 PRESET_HQ,
74 PRESET_BD ,
75 PRESET_LOW_LATENCY_DEFAULT ,
76 PRESET_LOW_LATENCY_HQ ,
77 PRESET_LOW_LATENCY_HP,
78 PRESET_LOSSLESS_DEFAULT,
79 PRESET_LOSSLESS_HP,
80 };
81
82 enum {
83 NV_ENC_H264_PROFILE_BASELINE,
84 NV_ENC_H264_PROFILE_MAIN,
85 NV_ENC_H264_PROFILE_HIGH,
86 NV_ENC_H264_PROFILE_HIGH_444,
87 NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
88 };
89
90 enum {
91 NVENC_LOWLATENCY = 1,
92 NVENC_LOSSLESS,
93 };
94
95 enum {
96 LIST_DEVICES = -2,
97 ANY_DEVICE,
98 };
99
100 typedef struct NVENCContext {
101 AVClass *class;
102 NVENCLibraryContext nvel;
103
104 NV_ENC_INITIALIZE_PARAMS params;
105 NV_ENC_CONFIG config;
106
107 CUcontext cu_context;
108
109 int nb_surfaces;
110 NVENCInputSurface *in;
111 NVENCOutputSurface *out;
112 AVFifoBuffer *timestamps;
113 AVFifoBuffer *pending, *ready;
114
115 int64_t last_dts;
116
117 void *nvenc_ctx;
118
119 int preset;
120 int profile;
121 int level;
122 int tier;
123 int rc;
124 int device;
125 int flags;
126 } NVENCContext;
127
128 int ff_nvenc_encode_init(AVCodecContext *avctx);
129
130 int ff_nvenc_encode_close(AVCodecContext *avctx);
131
132 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
133 const AVFrame *frame, int *got_packet);
134
135 #endif /* AVCODEC_NVENC_H */