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