]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
vaapi_h264: Add support for VUI parameters
[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 <nvEncodeAPI.h>
23
24 #include "config.h"
25
26 #include "libavutil/fifo.h"
27 #include "libavutil/opt.h"
28
29 #include "avcodec.h"
30
31 #if CONFIG_CUDA
32 #include <cuda.h>
33 #else
34
35 #if defined(_WIN32)
36 #define CUDAAPI __stdcall
37 #else
38 #define CUDAAPI
39 #endif
40
41 typedef enum cudaError_enum {
42     CUDA_SUCCESS = 0
43 } CUresult;
44 typedef int CUdevice;
45 typedef void* CUcontext;
46 typedef void* CUdeviceptr;
47 #endif
48
49 #define MAX_REGISTERED_FRAMES 64
50
51 typedef struct NVENCFrame {
52     NV_ENC_INPUT_PTR  in;
53     AVFrame          *in_ref;
54     NV_ENC_MAP_INPUT_RESOURCE in_map;
55     int reg_idx;
56
57     NV_ENC_OUTPUT_PTR out;
58     NV_ENC_BUFFER_FORMAT format;
59     int locked;
60 } NVENCFrame;
61
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);
70
71 typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
72
73 typedef struct NVENCLibraryContext
74 {
75 #if !CONFIG_CUDA
76     void *cuda;
77 #endif
78     void *nvenc;
79
80     PCUINIT cu_init;
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;
88
89     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
90 } NVENCLibraryContext;
91
92 enum {
93     PRESET_DEFAULT,
94     PRESET_HP,
95     PRESET_HQ,
96     PRESET_BD ,
97     PRESET_LOW_LATENCY_DEFAULT ,
98     PRESET_LOW_LATENCY_HQ ,
99     PRESET_LOW_LATENCY_HP,
100     PRESET_LOSSLESS_DEFAULT,
101     PRESET_LOSSLESS_HP,
102 };
103
104 enum {
105     NV_ENC_H264_PROFILE_BASELINE,
106     NV_ENC_H264_PROFILE_MAIN,
107     NV_ENC_H264_PROFILE_HIGH,
108     NV_ENC_H264_PROFILE_HIGH_444,
109     NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
110 };
111
112 enum {
113     NVENC_LOWLATENCY = 1,
114     NVENC_LOSSLESS,
115 };
116
117 enum {
118     LIST_DEVICES = -2,
119     ANY_DEVICE,
120 };
121
122 typedef struct NVENCContext {
123     AVClass *class;
124     NVENCLibraryContext nvel;
125
126     NV_ENC_INITIALIZE_PARAMS params;
127     NV_ENC_CONFIG config;
128
129     CUcontext cu_context;
130     CUcontext cu_context_internal;
131
132     int nb_surfaces;
133     NVENCFrame *frames;
134     AVFifoBuffer *timestamps;
135     AVFifoBuffer *pending, *ready;
136
137     struct {
138         CUdeviceptr ptr;
139         NV_ENC_REGISTERED_PTR regptr;
140         int mapped;
141     } registered_frames[MAX_REGISTERED_FRAMES];
142     int nb_registered_frames;
143
144     /* the actual data pixel format, different from
145      * AVCodecContext.pix_fmt when using hwaccel frames on input */
146     enum AVPixelFormat data_pix_fmt;
147
148     /* timestamps of the first two frames, for computing the first dts
149      * when B-frames are present */
150     int64_t initial_pts[2];
151     int first_packet_output;
152
153     void *nvenc_ctx;
154
155     int preset;
156     int profile;
157     int level;
158     int tier;
159     int rc;
160     int device;
161     int flags;
162     int async_depth;
163 } NVENCContext;
164
165 int ff_nvenc_encode_init(AVCodecContext *avctx);
166
167 int ff_nvenc_encode_close(AVCodecContext *avctx);
168
169 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
170                           const AVFrame *frame, int *got_packet);
171
172 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
173
174 #endif /* AVCODEC_NVENC_H */