]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
lavc: introduce a new decoding/encoding API with decoupled input/output
[ffmpeg] / 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 #define MAX_REGISTERED_FRAMES 64
33
34 typedef struct NVENCFrame {
35     NV_ENC_INPUT_PTR  in;
36     AVFrame          *in_ref;
37     NV_ENC_MAP_INPUT_RESOURCE in_map;
38     int reg_idx;
39
40     NV_ENC_OUTPUT_PTR out;
41     NV_ENC_BUFFER_FORMAT format;
42     int locked;
43 } NVENCFrame;
44
45 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
46 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
47 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
48 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
49 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
50 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
51 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
52 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
53
54 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
55
56 typedef struct NVENCLibraryContext
57 {
58 #if !CONFIG_CUDA
59     void *cuda;
60 #endif
61     void *nvenc;
62
63     PCUINIT cu_init;
64     PCUDEVICEGETCOUNT cu_device_get_count;
65     PCUDEVICEGET cu_device_get;
66     PCUDEVICEGETNAME cu_device_get_name;
67     PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
68     PCUCTXCREATE cu_ctx_create;
69     PCUCTXPOPCURRENT cu_ctx_pop_current;
70     PCUCTXDESTROY cu_ctx_destroy;
71
72     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
73 } NVENCLibraryContext;
74
75 enum {
76     PRESET_DEFAULT,
77     PRESET_HP,
78     PRESET_HQ,
79     PRESET_BD ,
80     PRESET_LOW_LATENCY_DEFAULT ,
81     PRESET_LOW_LATENCY_HQ ,
82     PRESET_LOW_LATENCY_HP,
83     PRESET_LOSSLESS_DEFAULT,
84     PRESET_LOSSLESS_HP,
85 };
86
87 enum {
88     NV_ENC_H264_PROFILE_BASELINE,
89     NV_ENC_H264_PROFILE_MAIN,
90     NV_ENC_H264_PROFILE_HIGH,
91     NV_ENC_H264_PROFILE_HIGH_444,
92     NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
93 };
94
95 enum {
96     NVENC_LOWLATENCY = 1,
97     NVENC_LOSSLESS,
98 };
99
100 enum {
101     LIST_DEVICES = -2,
102     ANY_DEVICE,
103 };
104
105 typedef struct NVENCContext {
106     AVClass *class;
107     NVENCLibraryContext nvel;
108
109     NV_ENC_INITIALIZE_PARAMS params;
110     NV_ENC_CONFIG config;
111
112     CUcontext cu_context;
113     CUcontext cu_context_internal;
114
115     int nb_surfaces;
116     NVENCFrame *frames;
117     AVFifoBuffer *timestamps;
118     AVFifoBuffer *pending, *ready;
119
120     struct {
121         CUdeviceptr ptr;
122         NV_ENC_REGISTERED_PTR regptr;
123         int mapped;
124     } registered_frames[MAX_REGISTERED_FRAMES];
125     int nb_registered_frames;
126
127     /* the actual data pixel format, different from
128      * AVCodecContext.pix_fmt when using hwaccel frames on input */
129     enum AVPixelFormat data_pix_fmt;
130
131     /* timestamps of the first two frames, for computing the first dts
132      * when b-frames are present */
133     int64_t initial_pts[2];
134     int first_packet_output;
135
136     void *nvenc_ctx;
137
138     int preset;
139     int profile;
140     int level;
141     int tier;
142     int rc;
143     int device;
144     int flags;
145 } NVENCContext;
146
147 int ff_nvenc_encode_init(AVCodecContext *avctx);
148
149 int ff_nvenc_encode_close(AVCodecContext *avctx);
150
151 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
152                           const AVFrame *frame, int *got_packet);
153
154 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
155
156 #endif /* AVCODEC_NVENC_H */