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