nvenc.h 5.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_NVENC_H
#define AVCODEC_NVENC_H

22 23
#include "config.h"

24 25 26 27 28 29 30
#if CONFIG_D3D11VA
#define COBJMACROS
#include "libavutil/hwcontext_d3d11va.h"
#else
typedef void ID3D11Device;
#endif

31
#include <ffnvcodec/nvEncodeAPI.h>
32

33
#include "compat/cuda/dynlink_loader.h"
34 35
#include "libavutil/fifo.h"
#include "libavutil/opt.h"
36
#include "hwconfig.h"
37 38 39 40

#include "avcodec.h"

#define MAX_REGISTERED_FRAMES 64
41 42
#define RC_MODE_DEPRECATED 0x800000
#define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
43

44 45 46
#define NVENCAPI_CHECK_VERSION(major, minor) \
    ((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))

47
// SDK 8.1 compile time feature checks
48
#if NVENCAPI_CHECK_VERSION(8, 1)
49 50 51 52
#define NVENC_HAVE_BFRAME_REF_MODE
#define NVENC_HAVE_QP_MAP_MODE
#endif

53 54 55 56 57
// SDK 9.0 compile time feature checks
#if NVENCAPI_CHECK_VERSION(9, 0)
#define NVENC_HAVE_HEVC_BFRAME_REF_MODE
#endif

58 59 60
// SDK 9.1 compile time feature checks
#if NVENCAPI_CHECK_VERSION(9, 1)
#define NVENC_HAVE_MULTIPLE_REF_FRAMES
61
#define NVENC_HAVE_CUSTREAM_PTR
62
#define NVENC_HAVE_GETLASTERRORSTRING
63 64
#endif

65 66 67 68 69 70 71
typedef struct NvencSurface
{
    NV_ENC_INPUT_PTR input_surface;
    AVFrame *in_ref;
    int reg_idx;
    int width;
    int height;
72
    int pitch;
73 74 75 76 77 78 79 80

    NV_ENC_OUTPUT_PTR output_surface;
    NV_ENC_BUFFER_FORMAT format;
    int size;
} NvencSurface;

typedef struct NvencDynLoadFunctions
{
81 82
    CudaFunctions *cuda_dl;
    NvencFunctions *nvenc_dl;
83 84 85 86 87

    NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
    int nvenc_device_count;
} NvencDynLoadFunctions;

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
enum {
    PRESET_DEFAULT = 0,
    PRESET_SLOW,
    PRESET_MEDIUM,
    PRESET_FAST,
    PRESET_HP,
    PRESET_HQ,
    PRESET_BD ,
    PRESET_LOW_LATENCY_DEFAULT ,
    PRESET_LOW_LATENCY_HQ ,
    PRESET_LOW_LATENCY_HP,
    PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
    PRESET_LOSSLESS_HP,
};

103 104 105 106 107 108 109
enum {
    NV_ENC_H264_PROFILE_BASELINE,
    NV_ENC_H264_PROFILE_MAIN,
    NV_ENC_H264_PROFILE_HIGH,
    NV_ENC_H264_PROFILE_HIGH_444P,
};

110 111 112
enum {
    NV_ENC_HEVC_PROFILE_MAIN,
    NV_ENC_HEVC_PROFILE_MAIN_10,
113
    NV_ENC_HEVC_PROFILE_REXT,
114 115
};

116 117 118 119 120 121 122
enum {
    NVENC_LOWLATENCY = 1,
    NVENC_LOSSLESS   = 2,
    NVENC_ONE_PASS   = 4,
    NVENC_TWO_PASSES = 8,
};

123 124 125 126 127
enum {
    LIST_DEVICES = -2,
    ANY_DEVICE,
};

128 129 130 131 132 133 134 135 136 137
typedef struct NvencContext
{
    AVClass *avclass;

    NvencDynLoadFunctions nvenc_dload_funcs;

    NV_ENC_INITIALIZE_PARAMS init_encode_params;
    NV_ENC_CONFIG encode_config;
    CUcontext cu_context;
    CUcontext cu_context_internal;
138
    CUstream cu_stream;
139
    ID3D11Device *d3d11_device;
140

141
    int nb_surfaces;
142 143
    NvencSurface *surfaces;

144
    AVFifoBuffer *unused_surface_queue;
145 146 147 148
    AVFifoBuffer *output_surface_queue;
    AVFifoBuffer *output_surface_ready_queue;
    AVFifoBuffer *timestamp_list;

149 150
    int encoder_flushing;

151
    struct {
152 153
        void *ptr;
        int ptr_index;
154 155
        NV_ENC_REGISTERED_PTR regptr;
        int mapped;
156
        NV_ENC_MAP_INPUT_RESOURCE in_map;
157 158 159 160 161 162 163
    } registered_frames[MAX_REGISTERED_FRAMES];
    int nb_registered_frames;

    /* the actual data pixel format, different from
     * AVCodecContext.pix_fmt when using hwaccel frames on input */
    enum AVPixelFormat data_pix_fmt;

164 165
    int support_dyn_bitrate;

166 167
    void *nvencoder;

168
    int preset;
169
    int profile;
170
    int level;
171
    int tier;
172
    int rc;
173 174
    int cbr;
    int twopass;
175
    int device;
176
    int flags;
177
    int async_depth;
178
    int rc_lookahead;
179 180
    int aq;
    int no_scenecut;
181
    int forced_idr;
182 183 184 185 186 187
    int b_adapt;
    int temporal_aq;
    int zerolatency;
    int nonref_p;
    int strict_gop;
    int aq_strength;
188
    float quality;
189
    int aud;
190
    int bluray_compat;
191 192 193
    int init_qp_p;
    int init_qp_b;
    int init_qp_i;
194
    int cqp;
195
    int weighted_pred;
196
    int coder;
197
    int b_ref_mode;
198
    int a53_cc;
199
    int dpb_size;
200 201 202 203 204 205
} NvencContext;

int ff_nvenc_encode_init(AVCodecContext *avctx);

int ff_nvenc_encode_close(AVCodecContext *avctx);

206 207 208 209
int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame);

int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);

210 211 212
int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                          const AVFrame *frame, int *got_packet);

213 214
void ff_nvenc_encode_flush(AVCodecContext *avctx);

215
extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
216
extern const AVCodecHWConfigInternal *ff_nvenc_hw_configs[];
217 218

#endif /* AVCODEC_NVENC_H */