Libav
nvenc.h
Go to the documentation of this file.
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 {
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;
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);
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 
88 
89  NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
91 
92 enum {
102 };
103 
104 enum {
110 };
111 
112 enum {
115 };
116 
117 enum {
120 };
121 
122 typedef struct NVENCContext {
123  AVClass *class;
125 
126  NV_ENC_INITIALIZE_PARAMS params;
127  NV_ENC_CONFIG config;
128 
131 
135  AVFifoBuffer *pending, *ready;
136 
137  struct {
139  NV_ENC_REGISTERED_PTR regptr;
140  int mapped;
141  } registered_frames[MAX_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];
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;
163 } NVENCContext;
164 
166 
168 
170  const AVFrame *frame, int *got_packet);
171 
172 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
173 
174 #endif /* AVCODEC_NVENC_H */
enum AVPixelFormat ff_nvenc_pix_fmts[]
Definition: nvenc.c:88
int ff_nvenc_encode_init(AVCodecContext *avctx)
Definition: nvenc.c:1006
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
void * nvenc_ctx
Definition: nvenc.h:153
int locked
Definition: nvenc.h:59
CUresult(CUDAAPI * PCUDEVICEGET)(CUdevice *device, int ordinal)
Definition: nvenc.h:64
AVFifoBuffer * ready
Definition: nvenc.h:135
int CUdevice
Definition: nvenc.h:44
NV_ENC_REGISTERED_PTR regptr
Definition: nvenc.h:139
int nb_registered_frames
Definition: nvenc.h:142
PCUDEVICEGET cu_device_get
Definition: nvenc.h:82
NV_ENC_MAP_INPUT_RESOURCE in_map
Definition: nvenc.h:54
CUresult(CUDAAPI * PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev)
Definition: nvenc.h:67
int nb_surfaces
Definition: nvenc.h:132
AVOptions.
PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability
Definition: nvenc.h:84
int flags
Definition: nvenc.h:161
int device
Definition: nvenc.h:160
CUresult(CUDAAPI * PCUDEVICEGETCOUNT)(int *count)
Definition: nvenc.h:63
int ff_nvenc_encode_close(AVCodecContext *avctx)
Definition: nvenc.c:951
NVENCLibraryContext nvel
Definition: nvenc.h:124
#define MAX_REGISTERED_FRAMES
Definition: nvenc.h:49
int reg_idx
Definition: nvenc.h:55
static int flags
Definition: log.c:50
PCUDEVICEGETNAME cu_device_get_name
Definition: nvenc.h:83
AVFrame * in_ref
Definition: nvenc.h:53
#define CUDAAPI
Definition: nvenc.h:38
NV_ENC_BUFFER_FORMAT format
Definition: nvenc.h:58
NV_ENC_INPUT_PTR in
Definition: nvenc.h:52
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
Definition: nvenc.h:89
CUresult(CUDAAPI * PCUCTXDESTROY)(CUcontext ctx)
Definition: nvenc.h:69
int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: nvenc.c:1379
int async_depth
Definition: nvenc.h:162
int profile
Definition: nvenc.h:156
const char * name
Definition: qsvenc.c:44
CUresult(CUDAAPI * PCUCTXPOPCURRENT)(CUcontext *pctx)
Definition: nvenc.h:68
PCUINIT cu_init
Definition: nvenc.h:80
NV_ENC_OUTPUT_PTR out
Definition: nvenc.h:57
PCUCTXDESTROY cu_ctx_destroy
Definition: nvenc.h:87
AVFormatContext * ctx
Definition: movenc.c:48
CUresult
Definition: nvenc.h:41
int first_packet_output
Definition: nvenc.h:151
CUresult(CUDAAPI * PCUINIT)(unsigned int Flags)
Definition: nvenc.h:62
Libavcodec external API header.
NV_ENC_INITIALIZE_PARAMS params
Definition: nvenc.h:126
main external API structure.
Definition: avcodec.h:1409
a very simple circular buffer FIFO implementation
NVENCFrame * frames
Definition: nvenc.h:133
CUcontext cu_context_internal
Definition: nvenc.h:130
void * CUdeviceptr
Definition: nvenc.h:46
Describe the class of an AVClass context structure.
Definition: log.h:34
int tier
Definition: nvenc.h:158
CUdeviceptr ptr
Definition: nvenc.h:138
CUcontext cu_context
Definition: nvenc.h:129
int level
Definition: nvenc.h:157
CUresult(CUDAAPI * PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev)
Definition: nvenc.h:66
PCUCTXPOPCURRENT cu_ctx_pop_current
Definition: nvenc.h:86
PCUCTXCREATE cu_ctx_create
Definition: nvenc.h:85
NVENCSTATUS(NVENCAPI * PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList)
Definition: nvenc.h:71
CUresult(CUDAAPI * PCUDEVICEGETNAME)(char *name, int len, CUdevice dev)
Definition: nvenc.h:65
PCUDEVICEGETCOUNT cu_device_get_count
Definition: nvenc.h:81
void * nvenc
Definition: nvenc.h:78
int len
NV_ENC_CONFIG config
Definition: nvenc.h:127
int preset
Definition: nvenc.h:155
int rc
Definition: nvenc.h:159
void * CUcontext
Definition: nvenc.h:45
int mapped
Definition: nvenc.h:140
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
AVFifoBuffer * timestamps
Definition: nvenc.h:134
This structure stores compressed data.
Definition: avcodec.h:1323