]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvenc.h
Merge commit 'a46a4f722d2fac07c57990f0f548777622599f59'
[ffmpeg] / libavcodec / nvenc.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "compat/nvenc/nvEncodeAPI.h"
23
24 #include "config.h"
25
26 #include "compat/cuda/dynlink_loader.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/opt.h"
29
30 #include "avcodec.h"
31
32 #define MAX_REGISTERED_FRAMES 64
33 #define RC_MODE_DEPRECATED 0x800000
34 #define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
35
36 typedef struct NvencSurface
37 {
38     NV_ENC_INPUT_PTR input_surface;
39     AVFrame *in_ref;
40     NV_ENC_MAP_INPUT_RESOURCE in_map;
41     int reg_idx;
42     int width;
43     int height;
44     int pitch;
45
46     NV_ENC_OUTPUT_PTR output_surface;
47     NV_ENC_BUFFER_FORMAT format;
48     int size;
49 } NvencSurface;
50
51 typedef struct NvencDynLoadFunctions
52 {
53     CudaFunctions *cuda_dl;
54     NvencFunctions *nvenc_dl;
55
56     NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
57     int nvenc_device_count;
58 } NvencDynLoadFunctions;
59
60 enum {
61     PRESET_DEFAULT = 0,
62     PRESET_SLOW,
63     PRESET_MEDIUM,
64     PRESET_FAST,
65     PRESET_HP,
66     PRESET_HQ,
67     PRESET_BD ,
68     PRESET_LOW_LATENCY_DEFAULT ,
69     PRESET_LOW_LATENCY_HQ ,
70     PRESET_LOW_LATENCY_HP,
71     PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
72     PRESET_LOSSLESS_HP,
73 };
74
75 enum {
76     NV_ENC_H264_PROFILE_BASELINE,
77     NV_ENC_H264_PROFILE_MAIN,
78     NV_ENC_H264_PROFILE_HIGH,
79     NV_ENC_H264_PROFILE_HIGH_444P,
80 };
81
82 enum {
83     NV_ENC_HEVC_PROFILE_MAIN,
84     NV_ENC_HEVC_PROFILE_MAIN_10,
85     NV_ENC_HEVC_PROFILE_REXT,
86 };
87
88 enum {
89     NVENC_LOWLATENCY = 1,
90     NVENC_LOSSLESS   = 2,
91     NVENC_ONE_PASS   = 4,
92     NVENC_TWO_PASSES = 8,
93 };
94
95 enum {
96     LIST_DEVICES = -2,
97     ANY_DEVICE,
98 };
99
100 typedef struct NvencContext
101 {
102     AVClass *avclass;
103
104     NvencDynLoadFunctions nvenc_dload_funcs;
105
106     NV_ENC_INITIALIZE_PARAMS init_encode_params;
107     NV_ENC_CONFIG encode_config;
108     CUcontext cu_context;
109     CUcontext cu_context_internal;
110
111     int nb_surfaces;
112     NvencSurface *surfaces;
113
114     AVFifoBuffer *unused_surface_queue;
115     AVFifoBuffer *output_surface_queue;
116     AVFifoBuffer *output_surface_ready_queue;
117     AVFifoBuffer *timestamp_list;
118
119     int encoder_flushing;
120
121     struct {
122         CUdeviceptr ptr;
123         NV_ENC_REGISTERED_PTR regptr;
124         int mapped;
125     } registered_frames[MAX_REGISTERED_FRAMES];
126     int nb_registered_frames;
127
128     /* the actual data pixel format, different from
129      * AVCodecContext.pix_fmt when using hwaccel frames on input */
130     enum AVPixelFormat data_pix_fmt;
131
132     /* timestamps of the first two frames, for computing the first dts
133      * when B-frames are present */
134     int64_t initial_pts[2];
135     int first_packet_output;
136
137     void *nvencoder;
138
139     int preset;
140     int profile;
141     int level;
142     int tier;
143     int rc;
144     int cbr;
145     int twopass;
146     int device;
147     int flags;
148     int async_depth;
149     int rc_lookahead;
150     int aq;
151     int no_scenecut;
152     int forced_idr;
153     int b_adapt;
154     int temporal_aq;
155     int zerolatency;
156     int nonref_p;
157     int strict_gop;
158     int aq_strength;
159     float quality;
160     int aud;
161     int bluray_compat;
162     int init_qp_p;
163     int init_qp_b;
164     int init_qp_i;
165     int cqp;
166     int weighted_pred;
167     int coder;
168 } NvencContext;
169
170 int ff_nvenc_encode_init(AVCodecContext *avctx);
171
172 int ff_nvenc_encode_close(AVCodecContext *avctx);
173
174 int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame);
175
176 int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
177
178 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
179                           const AVFrame *frame, int *got_packet);
180
181 extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
182
183 #endif /* AVCODEC_NVENC_H */