2016-05-25 15:04:28 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2017-11-26 01:05:24 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2017-11-15 20:15:23 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
#define COBJMACROS
|
|
|
|
#include "libavutil/hwcontext_d3d11va.h"
|
|
|
|
#else
|
|
|
|
typedef void ID3D11Device;
|
|
|
|
#endif
|
|
|
|
|
2018-02-26 02:20:51 +02:00
|
|
|
#include <ffnvcodec/nvEncodeAPI.h>
|
2016-05-25 15:04:28 +02:00
|
|
|
|
2016-10-10 12:55:59 +02:00
|
|
|
#include "compat/cuda/dynlink_loader.h"
|
2023-06-16 21:35:45 +02:00
|
|
|
#include "libavutil/buffer.h"
|
2016-05-25 15:04:28 +02:00
|
|
|
#include "libavutil/fifo.h"
|
|
|
|
#include "libavutil/opt.h"
|
2020-04-13 17:33:18 +02:00
|
|
|
#include "hwconfig.h"
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
#define MAX_REGISTERED_FRAMES 64
|
2017-05-09 13:57:39 +02:00
|
|
|
#define RC_MODE_DEPRECATED 0x800000
|
|
|
|
#define RCD(rc_mode) ((rc_mode) | RC_MODE_DEPRECATED)
|
2016-05-25 15:04:28 +02:00
|
|
|
|
2018-04-13 09:36:49 +02:00
|
|
|
#define NVENCAPI_CHECK_VERSION(major, minor) \
|
|
|
|
((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))
|
|
|
|
|
2018-04-11 14:22:20 +02:00
|
|
|
// SDK 8.1 compile time feature checks
|
2018-04-13 09:36:49 +02:00
|
|
|
#if NVENCAPI_CHECK_VERSION(8, 1)
|
2018-04-11 14:22:20 +02:00
|
|
|
#define NVENC_HAVE_BFRAME_REF_MODE
|
|
|
|
#define NVENC_HAVE_QP_MAP_MODE
|
|
|
|
#endif
|
|
|
|
|
2019-02-14 14:20:25 +02:00
|
|
|
// SDK 9.0 compile time feature checks
|
|
|
|
#if NVENCAPI_CHECK_VERSION(9, 0)
|
|
|
|
#define NVENC_HAVE_HEVC_BFRAME_REF_MODE
|
|
|
|
#endif
|
|
|
|
|
2019-09-27 17:56:11 +02:00
|
|
|
// SDK 9.1 compile time feature checks
|
|
|
|
#if NVENCAPI_CHECK_VERSION(9, 1)
|
|
|
|
#define NVENC_HAVE_MULTIPLE_REF_FRAMES
|
2019-09-27 18:30:10 +02:00
|
|
|
#define NVENC_HAVE_CUSTREAM_PTR
|
2019-09-27 19:09:11 +02:00
|
|
|
#define NVENC_HAVE_GETLASTERRORSTRING
|
2019-09-27 17:56:11 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-19 10:35:29 +02:00
|
|
|
// SDK 10.0 compile time feature checks
|
|
|
|
#if NVENCAPI_CHECK_VERSION(10, 0)
|
|
|
|
#define NVENC_HAVE_NEW_PRESETS
|
|
|
|
#define NVENC_HAVE_MULTIPASS
|
|
|
|
#define NVENC_HAVE_LDKFS
|
2020-07-01 16:09:08 +02:00
|
|
|
#define NVENC_HAVE_H264_LVL6
|
2021-09-02 12:38:58 +02:00
|
|
|
#define NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
|
2020-03-19 10:35:29 +02:00
|
|
|
#endif
|
|
|
|
|
2021-08-03 17:02:26 +02:00
|
|
|
// SDK 11.1 compile time feature checks
|
|
|
|
#if NVENCAPI_CHECK_VERSION(11, 1)
|
|
|
|
#define NVENC_HAVE_QP_CHROMA_OFFSETS
|
2021-09-02 12:38:57 +02:00
|
|
|
#define NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
|
2021-08-03 17:02:26 +02:00
|
|
|
#endif
|
|
|
|
|
2023-06-01 23:46:46 +02:00
|
|
|
// SDK 12.1 compile time feature checks
|
|
|
|
#if NVENCAPI_CHECK_VERSION(12, 1)
|
|
|
|
#define NVENC_NO_DEPRECATED_RC
|
|
|
|
#endif
|
|
|
|
|
2024-03-31 18:39:49 +02:00
|
|
|
// SDK 12.2 compile time feature checks
|
|
|
|
#if NVENCAPI_CHECK_VERSION(12, 2)
|
|
|
|
#define NVENC_HAVE_NEW_BIT_DEPTH_API
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
typedef struct NvencSurface
|
|
|
|
{
|
|
|
|
NV_ENC_INPUT_PTR input_surface;
|
|
|
|
AVFrame *in_ref;
|
|
|
|
int reg_idx;
|
|
|
|
int width;
|
|
|
|
int height;
|
2016-09-07 18:15:22 +02:00
|
|
|
int pitch;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
NV_ENC_OUTPUT_PTR output_surface;
|
|
|
|
NV_ENC_BUFFER_FORMAT format;
|
|
|
|
} NvencSurface;
|
|
|
|
|
2023-06-16 21:35:45 +02:00
|
|
|
typedef struct NvencFrameData
|
|
|
|
{
|
|
|
|
int64_t duration;
|
|
|
|
|
|
|
|
void *frame_opaque;
|
|
|
|
AVBufferRef *frame_opaque_ref;
|
|
|
|
} NvencFrameData;
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
typedef struct NvencDynLoadFunctions
|
|
|
|
{
|
2016-10-10 12:55:59 +02:00
|
|
|
CudaFunctions *cuda_dl;
|
|
|
|
NvencFunctions *nvenc_dl;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
|
|
|
|
int nvenc_device_count;
|
|
|
|
} NvencDynLoadFunctions;
|
|
|
|
|
2016-05-25 16:05:50 +02:00
|
|
|
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,
|
2021-04-18 11:17:54 +02:00
|
|
|
PRESET_LOSSLESS_DEFAULT,
|
2016-05-25 16:05:50 +02:00
|
|
|
PRESET_LOSSLESS_HP,
|
2020-03-19 10:35:29 +02:00
|
|
|
#ifdef NVENC_HAVE_NEW_PRESETS
|
|
|
|
PRESET_P1,
|
|
|
|
PRESET_P2,
|
|
|
|
PRESET_P3,
|
|
|
|
PRESET_P4,
|
|
|
|
PRESET_P5,
|
|
|
|
PRESET_P6,
|
|
|
|
PRESET_P7,
|
|
|
|
#endif
|
2016-05-25 16:05:50 +02:00
|
|
|
};
|
|
|
|
|
2016-05-25 16:26:25 +02:00
|
|
|
enum {
|
|
|
|
NV_ENC_H264_PROFILE_BASELINE,
|
|
|
|
NV_ENC_H264_PROFILE_MAIN,
|
|
|
|
NV_ENC_H264_PROFILE_HIGH,
|
|
|
|
NV_ENC_H264_PROFILE_HIGH_444P,
|
|
|
|
};
|
|
|
|
|
2016-08-25 17:18:03 +02:00
|
|
|
enum {
|
|
|
|
NV_ENC_HEVC_PROFILE_MAIN,
|
|
|
|
NV_ENC_HEVC_PROFILE_MAIN_10,
|
2016-09-28 14:21:03 +02:00
|
|
|
NV_ENC_HEVC_PROFILE_REXT,
|
2016-08-25 17:18:03 +02:00
|
|
|
};
|
|
|
|
|
2016-05-25 16:05:50 +02:00
|
|
|
enum {
|
|
|
|
NVENC_LOWLATENCY = 1,
|
|
|
|
NVENC_LOSSLESS = 2,
|
|
|
|
NVENC_ONE_PASS = 4,
|
|
|
|
NVENC_TWO_PASSES = 8,
|
2020-10-21 17:52:43 +02:00
|
|
|
|
|
|
|
NVENC_DEPRECATED_PRESET = 0x8000,
|
2016-05-25 16:05:50 +02:00
|
|
|
};
|
|
|
|
|
2016-05-29 14:23:26 +02:00
|
|
|
enum {
|
|
|
|
LIST_DEVICES = -2,
|
|
|
|
ANY_DEVICE,
|
|
|
|
};
|
|
|
|
|
2023-10-03 17:31:11 +02:00
|
|
|
enum {
|
|
|
|
NVENC_RGB_MODE_DISABLED,
|
|
|
|
NVENC_RGB_MODE_420,
|
|
|
|
NVENC_RGB_MODE_444,
|
|
|
|
};
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
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;
|
2019-09-27 18:30:10 +02:00
|
|
|
CUstream cu_stream;
|
2017-11-11 17:51:58 +02:00
|
|
|
ID3D11Device *d3d11_device;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
2020-06-09 23:31:32 +02:00
|
|
|
AVFrame *frame;
|
|
|
|
|
2016-05-29 12:07:34 +02:00
|
|
|
int nb_surfaces;
|
2016-05-25 15:04:28 +02:00
|
|
|
NvencSurface *surfaces;
|
|
|
|
|
2023-06-16 21:35:45 +02:00
|
|
|
NvencFrameData *frame_data_array;
|
|
|
|
int frame_data_array_nb;
|
|
|
|
int frame_data_array_pos;
|
|
|
|
|
2022-01-06 17:59:49 +02:00
|
|
|
AVFifo *unused_surface_queue;
|
|
|
|
AVFifo *output_surface_queue;
|
|
|
|
AVFifo *output_surface_ready_queue;
|
2023-06-16 20:23:26 +02:00
|
|
|
AVFifo *timestamp_list;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
2021-05-25 12:11:57 +02:00
|
|
|
NV_ENC_SEI_PAYLOAD *sei_data;
|
|
|
|
int sei_data_size;
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
struct {
|
2017-11-11 17:51:58 +02:00
|
|
|
void *ptr;
|
|
|
|
int ptr_index;
|
2016-05-25 15:04:28 +02:00
|
|
|
NV_ENC_REGISTERED_PTR regptr;
|
|
|
|
int mapped;
|
2018-01-26 21:16:53 +02:00
|
|
|
NV_ENC_MAP_INPUT_RESOURCE in_map;
|
2016-05-25 15:04:28 +02:00
|
|
|
} 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;
|
|
|
|
|
2018-05-03 02:15:52 +02:00
|
|
|
int support_dyn_bitrate;
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
void *nvencoder;
|
|
|
|
|
2016-05-25 16:05:50 +02:00
|
|
|
int preset;
|
2016-05-25 16:26:25 +02:00
|
|
|
int profile;
|
2016-05-25 17:00:52 +02:00
|
|
|
int level;
|
2016-05-25 17:06:02 +02:00
|
|
|
int tier;
|
2016-05-25 18:39:54 +02:00
|
|
|
int rc;
|
2016-05-25 15:04:28 +02:00
|
|
|
int cbr;
|
2022-11-05 22:17:37 +02:00
|
|
|
int tile_rows;
|
|
|
|
int tile_cols;
|
2016-05-25 15:04:28 +02:00
|
|
|
int twopass;
|
2016-05-29 14:23:26 +02:00
|
|
|
int device;
|
2016-05-25 16:05:50 +02:00
|
|
|
int flags;
|
2016-05-29 12:07:34 +02:00
|
|
|
int async_depth;
|
2016-08-25 17:20:03 +02:00
|
|
|
int rc_lookahead;
|
2016-09-24 17:55:00 +02:00
|
|
|
int aq;
|
|
|
|
int no_scenecut;
|
2016-10-12 20:51:43 +02:00
|
|
|
int forced_idr;
|
2016-09-24 17:55:00 +02:00
|
|
|
int b_adapt;
|
|
|
|
int temporal_aq;
|
|
|
|
int zerolatency;
|
|
|
|
int nonref_p;
|
|
|
|
int strict_gop;
|
|
|
|
int aq_strength;
|
2017-05-10 08:41:17 +02:00
|
|
|
float quality;
|
2016-12-30 23:02:42 +02:00
|
|
|
int aud;
|
2016-12-30 23:04:31 +02:00
|
|
|
int bluray_compat;
|
2017-02-28 07:39:12 +02:00
|
|
|
int init_qp_p;
|
|
|
|
int init_qp_b;
|
|
|
|
int init_qp_i;
|
2017-03-23 18:01:40 +02:00
|
|
|
int cqp;
|
2021-08-03 17:02:26 +02:00
|
|
|
int qp_cb_offset;
|
|
|
|
int qp_cr_offset;
|
2017-05-09 16:24:54 +02:00
|
|
|
int weighted_pred;
|
2017-08-30 21:06:25 +02:00
|
|
|
int coder;
|
2018-04-11 14:22:20 +02:00
|
|
|
int b_ref_mode;
|
2018-08-21 14:28:15 +02:00
|
|
|
int a53_cc;
|
2020-06-30 19:03:56 +02:00
|
|
|
int s12m_tc;
|
2019-09-27 17:55:23 +02:00
|
|
|
int dpb_size;
|
2020-03-19 10:35:29 +02:00
|
|
|
int tuning_info;
|
|
|
|
int multipass;
|
|
|
|
int ldkfs;
|
2021-06-06 21:53:53 +02:00
|
|
|
int extra_sei;
|
2021-09-06 04:01:58 +02:00
|
|
|
int intra_refresh;
|
2021-09-02 12:38:57 +02:00
|
|
|
int single_slice_intra_refresh;
|
2021-09-02 12:38:58 +02:00
|
|
|
int constrained_encoding;
|
2021-12-24 06:32:47 +02:00
|
|
|
int udu_sei;
|
2022-11-05 22:17:37 +02:00
|
|
|
int timing_info;
|
|
|
|
int highbitdepth;
|
2023-07-27 04:26:23 +02:00
|
|
|
int max_slice_size;
|
2023-10-03 17:31:11 +02:00
|
|
|
int rgb_mode;
|
2016-05-25 15:04:28 +02:00
|
|
|
} NvencContext;
|
|
|
|
|
|
|
|
int ff_nvenc_encode_init(AVCodecContext *avctx);
|
|
|
|
|
|
|
|
int ff_nvenc_encode_close(AVCodecContext *avctx);
|
|
|
|
|
2017-09-02 19:30:21 +02:00
|
|
|
int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
|
|
|
|
|
2019-12-21 01:34:33 +02:00
|
|
|
void ff_nvenc_encode_flush(AVCodecContext *avctx);
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
|
2020-11-08 20:51:15 +02:00
|
|
|
extern const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[];
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
#endif /* AVCODEC_NVENC_H */
|