28 #include <nvEncodeAPI.h>
40 #define CUDAAPI __stdcall
46 #define LOAD_FUNC(l, s) GetProcAddress(l, s)
47 #define DL_CLOSE_FUNC(l) FreeLibrary(l)
49 #define LOAD_FUNC(l, s) dlsym(l, s)
50 #define DL_CLOSE_FUNC(l) dlclose(l)
53 typedef enum cudaError_enum {
170 {
"auto", NV_ENC_LEVEL_AUTOSELECT },
171 {
"1" , NV_ENC_LEVEL_H264_1 },
172 {
"1.0" , NV_ENC_LEVEL_H264_1 },
173 {
"1b" , NV_ENC_LEVEL_H264_1b },
174 {
"1.0b", NV_ENC_LEVEL_H264_1b },
175 {
"1.1" , NV_ENC_LEVEL_H264_11 },
176 {
"1.2" , NV_ENC_LEVEL_H264_12 },
177 {
"1.3" , NV_ENC_LEVEL_H264_13 },
178 {
"2" , NV_ENC_LEVEL_H264_2 },
179 {
"2.0" , NV_ENC_LEVEL_H264_2 },
180 {
"2.1" , NV_ENC_LEVEL_H264_21 },
181 {
"2.2" , NV_ENC_LEVEL_H264_22 },
182 {
"3" , NV_ENC_LEVEL_H264_3 },
183 {
"3.0" , NV_ENC_LEVEL_H264_3 },
184 {
"3.1" , NV_ENC_LEVEL_H264_31 },
185 {
"3.2" , NV_ENC_LEVEL_H264_32 },
186 {
"4" , NV_ENC_LEVEL_H264_4 },
187 {
"4.0" , NV_ENC_LEVEL_H264_4 },
188 {
"4.1" , NV_ENC_LEVEL_H264_41 },
189 {
"4.2" , NV_ENC_LEVEL_H264_42 },
190 {
"5" , NV_ENC_LEVEL_H264_5 },
191 {
"5.0" , NV_ENC_LEVEL_H264_5 },
192 {
"5.1" , NV_ENC_LEVEL_H264_51 },
197 {
"auto", NV_ENC_LEVEL_AUTOSELECT },
198 {
"1" , NV_ENC_LEVEL_HEVC_1 },
199 {
"1.0" , NV_ENC_LEVEL_HEVC_1 },
200 {
"2" , NV_ENC_LEVEL_HEVC_2 },
201 {
"2.0" , NV_ENC_LEVEL_HEVC_2 },
202 {
"2.1" , NV_ENC_LEVEL_HEVC_21 },
203 {
"3" , NV_ENC_LEVEL_HEVC_3 },
204 {
"3.0" , NV_ENC_LEVEL_HEVC_3 },
205 {
"3.1" , NV_ENC_LEVEL_HEVC_31 },
206 {
"4" , NV_ENC_LEVEL_HEVC_4 },
207 {
"4.0" , NV_ENC_LEVEL_HEVC_4 },
208 {
"4.1" , NV_ENC_LEVEL_HEVC_41 },
209 {
"5" , NV_ENC_LEVEL_HEVC_5 },
210 {
"5.0" , NV_ENC_LEVEL_HEVC_5 },
211 {
"5.1" , NV_ENC_LEVEL_HEVC_51 },
212 {
"5.2" , NV_ENC_LEVEL_HEVC_52 },
213 {
"6" , NV_ENC_LEVEL_HEVC_6 },
214 {
"6.0" , NV_ENC_LEVEL_HEVC_6 },
215 {
"6.1" , NV_ENC_LEVEL_HEVC_61 },
216 {
"6.2" , NV_ENC_LEVEL_HEVC_62 },
222 for (; pair->
str; ++pair) {
223 if (!strcmp(input, pair->
str)) {
245 mask = queue->
size - 1;
246 read_pos = (queue->
pos - queue->
count) & mask;
249 return &queue->
data[read_pos];
288 mask = queue->
size - 1;
291 queue->
pos = (queue->
pos + 1) & mask;
333 #define CHECK_LOAD_FUNC(t, f, s) \
335 (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \
337 av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \
353 dl_fn->
cuda_lib = dlopen(
"libcuda.so", RTLD_LAZY);
390 #define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error
394 int device_count = 0;
397 int smminor = 0, smmajor = 0;
398 int i, smver, target_smver;
434 for (i = 0; i < device_count; ++i) {
439 smver = (smmajor << 4) | smminor;
441 av_log(avctx,
AV_LOG_VERBOSE,
"[ GPU #%d - < %s > has Compute SM %d.%d, NVENC %s ]\n", i, gpu_name, smmajor, smminor, (smver >= target_smver) ?
"Available" :
"Not Available");
443 if (smver >= target_smver)
464 NVENCSTATUS nvstatus;
476 if (
sizeof(
void*) == 8) {
482 dl_fn->
nvenc_lib = dlopen(
"libnvidia-encode.so.1", RTLD_LAZY);
492 if (!nvEncodeAPICreateInstance) {
497 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
499 nvstatus = nvEncodeAPICreateInstance(&dl_fn->
nvenc_funcs);
501 if (nvstatus != NV_ENC_SUCCESS) {
546 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
547 NV_ENC_PRESET_CONFIG preset_config = { 0 };
550 GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
552 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
554 int surfaceCount = 0;
564 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
573 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
574 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
575 encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
576 encode_session_params.apiVersion = NVENCAPI_VERSION;
588 av_log(avctx,
AV_LOG_FATAL,
"Failed creating CUDA context for NVENC: 0x%x\n", (
int)cu_res);
601 encode_session_params.device = ctx->
cu_context;
602 encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
604 nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->
nvencoder);
605 if (nv_status != NV_ENC_SUCCESS) {
613 if (!strcmp(ctx->
preset,
"slow")) {
614 encoder_preset = NV_ENC_PRESET_HQ_GUID;
616 }
else if (!strcmp(ctx->
preset,
"medium")) {
617 encoder_preset = NV_ENC_PRESET_HQ_GUID;
619 }
else if (!strcmp(ctx->
preset,
"fast")) {
620 encoder_preset = NV_ENC_PRESET_HP_GUID;
622 }
else if (!strcmp(ctx->
preset,
"hq")) {
623 encoder_preset = NV_ENC_PRESET_HQ_GUID;
624 }
else if (!strcmp(ctx->
preset,
"hp")) {
625 encoder_preset = NV_ENC_PRESET_HP_GUID;
626 }
else if (!strcmp(ctx->
preset,
"bd")) {
627 encoder_preset = NV_ENC_PRESET_BD_GUID;
628 }
else if (!strcmp(ctx->
preset,
"ll")) {
629 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
631 }
else if (!strcmp(ctx->
preset,
"llhp")) {
632 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
634 }
else if (!strcmp(ctx->
preset,
"llhq")) {
635 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
637 }
else if (!strcmp(ctx->
preset,
"lossless")) {
638 encoder_preset = NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID;
640 }
else if (!strcmp(ctx->
preset,
"losslesshp")) {
641 encoder_preset = NV_ENC_PRESET_LOSSLESS_HP_GUID;
643 }
else if (!strcmp(ctx->
preset,
"default")) {
644 encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
646 av_log(avctx,
AV_LOG_FATAL,
"Preset \"%s\" is unknown! Supported presets: slow, medium, high, hp, hq, bd, ll, llhp, llhq, lossless, losslesshp, default\n", ctx->
preset);
658 codec = NV_ENC_CODEC_H264_GUID;
661 codec = NV_ENC_CODEC_HEVC_GUID;
669 nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->
nvencoder, codec, encoder_preset, &preset_config);
670 if (nv_status != NV_ENC_SUCCESS) {
695 if (avctx->
width == 720 &&
708 num_mbs = ((avctx->
width + 15) >> 4) * ((avctx->
height + 15) >> 4);
723 if (avctx->
refs >= 0) {
730 ctx->
encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->
refs;
757 ctx->
encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
760 ctx->
encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
781 ctx->
encode_config.encodeCodecConfig.h264Config.qpPrimeYZeroTransformBypassFlag = 1;
783 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
790 }
else if (ctx->
cbr) {
792 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
794 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
797 ctx->
encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
798 ctx->
encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
802 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
810 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
822 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
825 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_VBR;
827 ctx->
encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
828 ctx->
encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
831 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR_MINQP;
837 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_VBR;
839 ctx->
encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
844 ctx->
encode_config.rcParams.initialRCQP.qpInterP = qp_inter_p;
852 ctx->
encode_config.rcParams.initialRCQP.qpIntra = qp_inter_p;
853 ctx->
encode_config.rcParams.initialRCQP.qpInterB = qp_inter_p;
864 ctx->
encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
866 ctx->
encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
871 ctx->
encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1;
872 ctx->
encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
876 ctx->
encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->
color_trc;
880 ctx->
encode_config.encodeCodecConfig.h264Config.sliceMode = 3;
881 ctx->
encode_config.encodeCodecConfig.h264Config.sliceModeData = 1;
889 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
892 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
895 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
899 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
903 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
907 if (!strcmp(ctx->
profile,
"high")) {
908 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
910 }
else if (!strcmp(ctx->
profile,
"main")) {
911 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
913 }
else if (!strcmp(ctx->
profile,
"baseline")) {
914 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
916 }
else if (!strcmp(ctx->
profile,
"high444p")) {
917 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
928 ctx->
encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
938 av_log(avctx,
AV_LOG_FATAL,
"Level \"%s\" is unknown! Supported levels: auto, 1, 1b, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 3, 3.1, 3.2, 4, 4.1, 4.2, 5, 5.1\n", ctx->
level);
942 ctx->
encode_config.encodeCodecConfig.h264Config.level = NV_ENC_LEVEL_AUTOSELECT;
947 ctx->
encode_config.encodeCodecConfig.hevcConfig.sliceMode = 3;
948 ctx->
encode_config.encodeCodecConfig.hevcConfig.sliceModeData = 1;
954 ctx->
encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
961 av_log(avctx,
AV_LOG_FATAL,
"Level \"%s\" is unknown! Supported levels: auto, 1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 5.1, 5.2, 6, 6.1, 6.2\n", ctx->
level);
965 ctx->
encode_config.encodeCodecConfig.hevcConfig.level = NV_ENC_LEVEL_AUTOSELECT;
969 if (!strcmp(ctx->
tier,
"main")) {
970 ctx->
encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_MAIN;
971 }
else if (!strcmp(ctx->
tier,
"high")) {
972 ctx->
encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_HIGH;
985 if (nv_status != NV_ENC_SUCCESS) {
1006 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1007 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1008 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1009 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1011 allocSurf.width = (avctx->
width + 31) & ~31;
1012 allocSurf.height = (avctx->
height + 31) & ~31;
1014 allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
1018 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
1022 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
1026 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
1035 nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->
nvencoder, &allocSurf);
1036 if (nv_status != NV_ENC_SUCCESS) {
1049 allocOut.size = 1024 * 1024;
1051 allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
1053 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->
nvencoder, &allocOut);
1054 if (nv_status != NV_ENC_SUCCESS) {
1067 uint32_t outSize = 0;
1068 char tmpHeader[256];
1069 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1070 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1072 payload.spsppsBuffer = tmpHeader;
1073 payload.inBufferSize =
sizeof(tmpHeader);
1074 payload.outSPSPPSPayloadSize = &outSize;
1076 nv_status = p_nvenc->nvEncGetSequenceParams(ctx->
nvencoder, &payload);
1077 if (nv_status != NV_ENC_SUCCESS) {
1090 memcpy(avctx->
extradata, tmpHeader, outSize);
1110 for (i = 0; i < surfaceCount; ++i) {
1117 p_nvenc->nvEncDestroyEncoder(ctx->
nvencoder);
1134 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1147 p_nvenc->nvEncDestroyEncoder(ctx->
nvencoder);
1162 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1164 uint32_t slice_mode_data;
1165 uint32_t *slice_offsets;
1166 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
1167 NVENCSTATUS nv_status;
1172 slice_mode_data = ctx->
encode_config.encodeCodecConfig.h264Config.sliceModeData;
1175 slice_mode_data = ctx->
encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1182 slice_offsets =
av_mallocz(slice_mode_data *
sizeof(*slice_offsets));
1187 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
1189 lock_params.doNotWait = 0;
1191 lock_params.sliceOffsets = slice_offsets;
1193 nv_status = p_nvenc->nvEncLockBitstream(ctx->
nvencoder, &lock_params);
1194 if (nv_status != NV_ENC_SUCCESS) {
1200 if (res =
ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) {
1205 memcpy(pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1208 if (nv_status != NV_ENC_SUCCESS)
1209 av_log(avctx,
AV_LOG_ERROR,
"Failed unlocking bitstream buffer, expect the gates of mordor to open\n");
1211 switch (lock_params.pictureType) {
1212 case NV_ENC_PIC_TYPE_IDR:
1214 #if FF_API_CODED_FRAME
1216 case NV_ENC_PIC_TYPE_I:
1219 case NV_ENC_PIC_TYPE_P:
1222 case NV_ENC_PIC_TYPE_B:
1225 case NV_ENC_PIC_TYPE_BI:
1229 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
1230 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
1237 pkt->
pts = lock_params.outputTimeStamp;
1267 NVENCSTATUS nv_status;
1273 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1275 NV_ENC_PIC_PARAMS pic_params = { 0 };
1276 pic_params.version = NV_ENC_PIC_PARAMS_VER;
1279 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1293 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1296 nv_status = p_nvenc->nvEncLockInputBuffer(ctx->
nvencoder, &lockBufferParams);
1297 if (nv_status != NV_ENC_SUCCESS) {
1303 uint8_t *
buf = lockBufferParams.bufferDataPtr;
1309 buf += inSurf->
height * lockBufferParams.pitch;
1315 buf += (inSurf->
height * lockBufferParams.pitch) >> 2;
1321 uint8_t *
buf = lockBufferParams.bufferDataPtr;
1327 buf += inSurf->
height * lockBufferParams.pitch;
1333 uint8_t *
buf = lockBufferParams.bufferDataPtr;
1339 buf += inSurf->
height * lockBufferParams.pitch;
1345 buf += inSurf->
height * lockBufferParams.pitch;
1356 if (nv_status != NV_ENC_SUCCESS) {
1374 pic_params.bufferFmt = inSurf->
format;
1375 pic_params.inputWidth = avctx->
width;
1376 pic_params.inputHeight = avctx->
height;
1378 pic_params.completionEvent = 0;
1382 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
1384 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
1387 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
1390 pic_params.encodePicFlags = 0;
1391 pic_params.inputTimeStamp = frame->
pts;
1392 pic_params.inputDuration = 0;
1395 pic_params.codecPicParams.h264PicParams.sliceMode = ctx->
encode_config.encodeCodecConfig.h264Config.sliceMode;
1396 pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->
encode_config.encodeCodecConfig.h264Config.sliceModeData;
1399 pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->
encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1400 pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->
encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1412 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1415 nv_status = p_nvenc->nvEncEncodePicture(ctx->
nvencoder, &pic_params);
1417 if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT) {
1426 if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1431 if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1458 tmpoutsurf->
busy = 0;
1477 #define OFFSET(x) offsetof(NvencContext, x)
1478 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1480 {
"preset",
"Set the encoding preset (one of slow = hq 2pass, medium = hq, fast = hp, hq, hp, bd, ll, llhq, llhp, default)",
OFFSET(
preset),
AV_OPT_TYPE_STRING, { .str =
"medium" }, 0, 0,
VE },
1481 {
"profile",
"Set the encoding profile (high, main, baseline or high444p)",
OFFSET(
profile),
AV_OPT_TYPE_STRING, { .str =
"main" }, 0, 0,
VE },
1482 {
"level",
"Set the encoding level restriction (auto, 1.0, 1.0b, 1.1, 1.2, ..., 4.2, 5.0, 5.1)",
OFFSET(
level),
AV_OPT_TYPE_STRING, { .str =
"auto" }, 0, 0,
VE },
1486 {
"gpu",
"Selects which NVENC capable GPU to use. First GPU is 0, second is 1, and so on.",
OFFSET(gpu),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
1487 {
"delay",
"Delays frame output by the given amount of frames.",
OFFSET(buffer_delay),
AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX,
VE },
1503 #if CONFIG_NVENC_ENCODER
1504 static const AVClass nvenc_class = {
1521 .priv_class = &nvenc_class,
1528 #if CONFIG_NVENC_H264_ENCODER
1529 static const AVClass nvenc_h264_class = {
1536 AVCodec ff_nvenc_h264_encoder = {
1537 .
name =
"nvenc_h264",
1546 .priv_class = &nvenc_h264_class,
1552 #if CONFIG_NVENC_HEVC_ENCODER
1553 static const AVClass nvenc_hevc_class = {
1560 AVCodec ff_nvenc_hevc_encoder = {
1561 .
name =
"nvenc_hevc",
1570 .priv_class = &nvenc_hevc_class,
#define FF_PROFILE_H264_MAIN
const struct AVCodec * codec
static av_cold int nvenc_check_cuda(AVCodecContext *avctx)
NvencOutputSurface * surface
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
NvencDataList output_surface_queue
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
NvencInputSurface * input_surfaces
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
memory handling functions
static av_cold int init(AVCodecContext *avctx)
int max_bitrate
Maximum bitrate of the stream, in bits per second.
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
NVENCSTATUS(NVENCAPI * PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList)
enum AVColorRange color_range
MPEG vs JPEG YUV range.
PCUCTXDESTROY cu_ctx_destroy
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
NvencDynLoadFunctions nvenc_dload_funcs
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
PCUDEVICEGETNAME cu_device_get_name
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
CUresult(CUDAAPI * PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev)
static const AVOption options[]
NvencDataList timestamp_list
float i_quant_offset
qscale offset between P and I-frames
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
CUresult(CUDAAPI * PCUDEVICEGET)(CUdevice *device, int ordinal)
#define FF_PROFILE_H264_BASELINE
#define CHECK_LOAD_FUNC(t, f, s)
PCUDEVICEGETCOUNT cu_device_get_count
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Multithreading support functions.
#define FF_PROFILE_HEVC_MAIN
PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
NV_ENC_INITIALIZE_PARAMS init_encode_params
#define AV_LOG_VERBOSE
Detailed information.
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
#define check_cuda_errors(f)
NvencOutputSurface * output_surfaces
static av_cold int nvenc_encode_close(AVCodecContext *avctx)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
NvencDataList output_surface_ready_queue
static int input_string_to_uint32(AVCodecContext *avctx, const NvencValuePair *pair, const char *input, uint32_t *output)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
static const uint16_t mask[17]
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
PCUDEVICEGET cu_device_get
#define FF_PROFILE_H264_HIGH
int flags
AV_CODEC_FLAG_*.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
int flags
A combination of AV_PKT_FLAG values.
int rc_buffer_size
decoder bitstream buffer size
static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencOutputSurface *tmpoutsurf)
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
common internal API header
int refs
number of reference frames
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
#define FF_PROFILE_UNKNOWN
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
static const NvencValuePair nvenc_h264_level_pairs[]
CUresult(CUDAAPI * PCUDEVICEGETCOUNT)(int *count)
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
static int out_surf_queue_enqueue(NvencDataList *queue, NvencOutputSurface *surface)
CUresult(CUDAAPI * PCUDEVICEGETNAME)(char *name, int len, CUdevice dev)
the normal 2^n-1 "JPEG" YUV ranges
static av_cold int nvenc_encode_init(AVCodecContext *avctx)
static NvencOutputSurface * out_surf_queue_dequeue(NvencDataList *queue)
This structure describes the bitrate properties of an encoded bitstream.
PCUCTXCREATE cu_ctx_create
NV_ENC_CONFIG encode_config
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
main external API structure.
int qmin
minimum quantizer
static int timestamp_queue_enqueue(NvencDataList *queue, int64_t timestamp)
static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx)
Describe the class of an AVClass context structure.
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
static const NvencValuePair nvenc_hevc_level_pairs[]
int(* func)(AVBPrint *dst, const char *in, const char *arg)
float b_quant_offset
qscale offset between IP and B-frames
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static int64_t timestamp_queue_dequeue(NvencDataList *queue)
static enum AVPixelFormat pix_fmts[]
int global_quality
Global quality for codecs which cannot change it per frame.
static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx)
static const AVCodecDefault nvenc_defaults[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
CUresult(CUDAAPI * PCUCTXDESTROY)(CUcontext ctx)
PCUCTXPOPCURRENT cu_ctx_pop_current
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
CUdevice nvenc_devices[16]
static enum AVPixelFormat pix_fmts_nvenc[]
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
CUresult(CUDAAPI * PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev)
CUresult(CUDAAPI * PCUINIT)(unsigned int Flags)
#define FF_ENABLE_DEPRECATION_WARNINGS
int top_field_first
If the content is interlaced, is top field displayed first.
int avg_bitrate
Average bitrate of the stream, in bits per second.
NvencInputSurface * input_surface
static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
NV_ENC_OUTPUT_PTR output_surface
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
static NvencData * data_queue_dequeue(NvencDataList *queue)
static const AVCodecDefault defaults[]
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
#define AVERROR_EXTERNAL
Generic error in an external library.
AVPixelFormat
Pixel format.
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
CUresult(CUDAAPI * PCUCTXPOPCURRENT)(CUcontext *pctx)
#define AV_NOPTS_VALUE
Undefined timestamp value.
int64_t rc_max_rate
maximum bitrate
static int data_queue_enqueue(NvencDataList *queue, NvencData *data)