2 * H.264 hardware encoding using nvidia nvenc
3 * Copyright (c) 2014 Timo Rothenpieler <timo@rothenpieler.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <nvEncodeAPI.h>
30 #include "libavutil/internal.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/mem.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 {
57 typedef void* CUcontext;
59 typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
60 typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
61 typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
62 typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
63 typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
64 typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
65 typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
66 typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
68 typedef NVENCSTATUS (NVENCAPI* PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
70 typedef struct NvencInputSurface
72 NV_ENC_INPUT_PTR input_surface;
78 NV_ENC_BUFFER_FORMAT format;
81 typedef struct NvencOutputSurface
83 NV_ENC_OUTPUT_PTR output_surface;
86 NvencInputSurface* input_surface;
91 typedef struct NvencData
95 NvencOutputSurface *surface;
99 typedef struct NvencDataList
108 typedef struct NvencDynLoadFunctions
111 PCUDEVICEGETCOUNT cu_device_get_count;
112 PCUDEVICEGET cu_device_get;
113 PCUDEVICEGETNAME cu_device_get_name;
114 PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
115 PCUCTXCREATE cu_ctx_create;
116 PCUCTXPOPCURRENT cu_ctx_pop_current;
117 PCUCTXDESTROY cu_ctx_destroy;
119 NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
120 int nvenc_device_count;
121 CUdevice nvenc_devices[16];
130 } NvencDynLoadFunctions;
132 typedef struct NvencContext
136 NvencDynLoadFunctions nvenc_dload_funcs;
138 NV_ENC_INITIALIZE_PARAMS init_encode_params;
139 NV_ENC_CONFIG encode_config;
140 CUcontext cu_context;
142 int max_surface_count;
143 NvencInputSurface *input_surfaces;
144 NvencOutputSurface *output_surfaces;
146 NvencDataList output_surface_queue;
147 NvencDataList output_surface_ready_queue;
148 NvencDataList timestamp_list;
160 static NvencData* data_queue_dequeue(NvencDataList* queue)
166 av_assert0(queue->size);
167 av_assert0(queue->data);
172 /* Size always is a multiple of two */
173 mask = queue->size - 1;
174 read_pos = (queue->pos - queue->count) & mask;
177 return &queue->data[read_pos];
180 static int data_queue_enqueue(NvencDataList* queue, NvencData *data)
182 NvencDataList new_queue;
187 /* size always has to be a multiple of two */
192 queue->data = av_malloc(queue->size * sizeof(*(queue->data)));
196 return AVERROR(ENOMEM);
200 if (queue->count == queue->size) {
201 new_queue.size = queue->size << 1;
204 new_queue.data = av_malloc(new_queue.size * sizeof(*(queue->data)));
207 return AVERROR(ENOMEM);
209 while (tmp_data = data_queue_dequeue(queue))
210 data_queue_enqueue(&new_queue, tmp_data);
212 av_free(queue->data);
216 mask = queue->size - 1;
218 queue->data[queue->pos] = *data;
219 queue->pos = (queue->pos + 1) & mask;
225 static int out_surf_queue_enqueue(NvencDataList* queue, NvencOutputSurface* surface)
228 data.surface = surface;
230 return data_queue_enqueue(queue, &data);
233 static NvencOutputSurface* out_surf_queue_dequeue(NvencDataList* queue)
235 NvencData* res = data_queue_dequeue(queue);
243 static int timestamp_queue_enqueue(NvencDataList* queue, int64_t timestamp)
246 data.timestamp = timestamp;
248 return data_queue_enqueue(queue, &data);
251 static int64_t timestamp_queue_dequeue(NvencDataList* queue)
253 NvencData* res = data_queue_dequeue(queue);
256 return AV_NOPTS_VALUE;
258 return res->timestamp;
261 #define CHECK_LOAD_FUNC(t, f, s) \
263 (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \
265 av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \
270 static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx)
272 NvencContext *ctx = avctx->priv_data;
273 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
279 dl_fn->cuda_lib = LoadLibrary(TEXT("nvcuda.dll"));
281 dl_fn->cuda_lib = dlopen("libcuda.so", RTLD_LAZY);
284 if (!dl_fn->cuda_lib) {
285 av_log(avctx, AV_LOG_FATAL, "Failed loading CUDA library\n");
289 CHECK_LOAD_FUNC(PCUINIT, dl_fn->cu_init, "cuInit");
290 CHECK_LOAD_FUNC(PCUDEVICEGETCOUNT, dl_fn->cu_device_get_count, "cuDeviceGetCount");
291 CHECK_LOAD_FUNC(PCUDEVICEGET, dl_fn->cu_device_get, "cuDeviceGet");
292 CHECK_LOAD_FUNC(PCUDEVICEGETNAME, dl_fn->cu_device_get_name, "cuDeviceGetName");
293 CHECK_LOAD_FUNC(PCUDEVICECOMPUTECAPABILITY, dl_fn->cu_device_compute_capability, "cuDeviceComputeCapability");
294 CHECK_LOAD_FUNC(PCUCTXCREATE, dl_fn->cu_ctx_create, "cuCtxCreate_v2");
295 CHECK_LOAD_FUNC(PCUCTXPOPCURRENT, dl_fn->cu_ctx_pop_current, "cuCtxPopCurrent_v2");
296 CHECK_LOAD_FUNC(PCUCTXDESTROY, dl_fn->cu_ctx_destroy, "cuCtxDestroy_v2");
303 DL_CLOSE_FUNC(dl_fn->cuda_lib);
305 dl_fn->cuda_lib = NULL;
310 static av_cold int check_cuda_errors(AVCodecContext *avctx, CUresult err, const char *func)
312 if (err != CUDA_SUCCESS) {
313 av_log(avctx, AV_LOG_FATAL, ">> %s - failed with error code 0x%x\n", func, err);
318 #define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error
320 static av_cold int nvenc_check_cuda(AVCodecContext *avctx)
322 int device_count = 0;
323 CUdevice cu_device = 0;
325 int smminor = 0, smmajor = 0;
326 int i, smver, target_smver;
328 NvencContext *ctx = avctx->priv_data;
329 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
331 switch (avctx->codec->id) {
332 case AV_CODEC_ID_H264:
335 case AV_CODEC_ID_H265:
339 av_log(avctx, AV_LOG_FATAL, "nvenc: Unknown codec name\n");
343 if (!nvenc_dyload_cuda(avctx))
346 if (dl_fn->nvenc_device_count > 0)
349 check_cuda_errors(dl_fn->cu_init(0));
351 check_cuda_errors(dl_fn->cu_device_get_count(&device_count));
354 av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
358 av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", device_count);
360 dl_fn->nvenc_device_count = 0;
362 for (i = 0; i < device_count; ++i) {
363 check_cuda_errors(dl_fn->cu_device_get(&cu_device, i));
364 check_cuda_errors(dl_fn->cu_device_get_name(gpu_name, sizeof(gpu_name), cu_device));
365 check_cuda_errors(dl_fn->cu_device_compute_capability(&smmajor, &smminor, cu_device));
367 smver = (smmajor << 4) | smminor;
369 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");
371 if (smver >= target_smver)
372 dl_fn->nvenc_devices[dl_fn->nvenc_device_count++] = cu_device;
375 if (!dl_fn->nvenc_device_count) {
376 av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
384 dl_fn->nvenc_device_count = 0;
389 static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx)
391 PNVENCODEAPICREATEINSTANCE nvEncodeAPICreateInstance = 0;
392 NVENCSTATUS nvstatus;
394 NvencContext *ctx = avctx->priv_data;
395 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
397 if (!nvenc_check_cuda(avctx))
400 if (dl_fn->nvenc_lib)
404 if (sizeof(void*) == 8) {
405 dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI64.dll"));
407 dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI.dll"));
410 dl_fn->nvenc_lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
413 if (!dl_fn->nvenc_lib) {
414 av_log(avctx, AV_LOG_FATAL, "Failed loading the nvenc library\n");
418 nvEncodeAPICreateInstance = (PNVENCODEAPICREATEINSTANCE)LOAD_FUNC(dl_fn->nvenc_lib, "NvEncodeAPICreateInstance");
420 if (!nvEncodeAPICreateInstance) {
421 av_log(avctx, AV_LOG_FATAL, "Failed to load nvenc entrypoint\n");
425 dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
427 nvstatus = nvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
429 if (nvstatus != NV_ENC_SUCCESS) {
430 av_log(avctx, AV_LOG_FATAL, "Failed to create nvenc instance\n");
434 av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
439 if (dl_fn->nvenc_lib)
440 DL_CLOSE_FUNC(dl_fn->nvenc_lib);
442 dl_fn->nvenc_lib = NULL;
447 static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
449 NvencContext *ctx = avctx->priv_data;
450 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
452 DL_CLOSE_FUNC(dl_fn->nvenc_lib);
453 dl_fn->nvenc_lib = NULL;
455 dl_fn->nvenc_device_count = 0;
457 DL_CLOSE_FUNC(dl_fn->cuda_lib);
458 dl_fn->cuda_lib = NULL;
460 dl_fn->cu_init = NULL;
461 dl_fn->cu_device_get_count = NULL;
462 dl_fn->cu_device_get = NULL;
463 dl_fn->cu_device_get_name = NULL;
464 dl_fn->cu_device_compute_capability = NULL;
465 dl_fn->cu_ctx_create = NULL;
466 dl_fn->cu_ctx_pop_current = NULL;
467 dl_fn->cu_ctx_destroy = NULL;
469 av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
472 static av_cold int nvenc_encode_init(AVCodecContext *avctx)
474 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
475 NV_ENC_PRESET_CONFIG preset_config = { 0 };
476 CUcontext cu_context_curr;
478 GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
480 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
481 int surfaceCount = 0;
487 NvencContext *ctx = avctx->priv_data;
488 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
489 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
491 if (!nvenc_dyload_nvenc(avctx))
492 return AVERROR_EXTERNAL;
494 avctx->coded_frame = av_frame_alloc();
495 if (!avctx->coded_frame) {
496 res = AVERROR(ENOMEM);
500 ctx->last_dts = AV_NOPTS_VALUE;
502 ctx->encode_config.version = NV_ENC_CONFIG_VER;
503 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
504 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
505 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
506 encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
507 encode_session_params.apiVersion = NVENCAPI_VERSION;
509 if (ctx->gpu >= dl_fn->nvenc_device_count) {
510 av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count);
511 res = AVERROR(EINVAL);
515 ctx->cu_context = NULL;
516 cu_res = dl_fn->cu_ctx_create(&ctx->cu_context, 0, dl_fn->nvenc_devices[ctx->gpu]);
518 if (cu_res != CUDA_SUCCESS) {
519 av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
520 res = AVERROR_EXTERNAL;
524 cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr);
526 if (cu_res != CUDA_SUCCESS) {
527 av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
528 res = AVERROR_EXTERNAL;
532 encode_session_params.device = ctx->cu_context;
533 encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
535 nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->nvencoder);
536 if (nv_status != NV_ENC_SUCCESS) {
537 ctx->nvencoder = NULL;
538 av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x - invalid license key?\n", (int)nv_status);
539 res = AVERROR_EXTERNAL;
544 if (!strcmp(ctx->preset, "hp")) {
545 encoder_preset = NV_ENC_PRESET_HP_GUID;
546 } else if (!strcmp(ctx->preset, "hq")) {
547 encoder_preset = NV_ENC_PRESET_HQ_GUID;
548 } else if (!strcmp(ctx->preset, "bd")) {
549 encoder_preset = NV_ENC_PRESET_BD_GUID;
550 } else if (!strcmp(ctx->preset, "ll")) {
551 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
553 } else if (!strcmp(ctx->preset, "llhp")) {
554 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
556 } else if (!strcmp(ctx->preset, "llhq")) {
557 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
559 } else if (!strcmp(ctx->preset, "default")) {
560 encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
562 av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: hp, hq, bd, ll, llhp, llhq, default\n", ctx->preset);
563 res = AVERROR(EINVAL);
568 switch (avctx->codec->id) {
569 case AV_CODEC_ID_H264:
570 codec = NV_ENC_CODEC_H264_GUID;
572 case AV_CODEC_ID_H265:
573 codec = NV_ENC_CODEC_HEVC_GUID;
576 av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
577 res = AVERROR(EINVAL);
581 nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
582 if (nv_status != NV_ENC_SUCCESS) {
583 av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
584 res = AVERROR_EXTERNAL;
588 ctx->init_encode_params.encodeGUID = codec;
589 ctx->init_encode_params.encodeHeight = avctx->height;
590 ctx->init_encode_params.encodeWidth = avctx->width;
592 if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
593 (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
595 avctx->width * avctx->sample_aspect_ratio.num,
596 avctx->height * avctx->sample_aspect_ratio.den,
598 ctx->init_encode_params.darHeight = dh;
599 ctx->init_encode_params.darWidth = dw;
601 ctx->init_encode_params.darHeight = avctx->height;
602 ctx->init_encode_params.darWidth = avctx->width;
605 // De-compensate for hardware, dubiously, trying to compensate for
606 // playback at 704 pixel width.
607 if (avctx->width == 720 &&
608 (avctx->height == 480 || avctx->height == 576)) {
610 ctx->init_encode_params.darWidth * 44,
611 ctx->init_encode_params.darHeight * 45,
613 ctx->init_encode_params.darHeight = dh;
614 ctx->init_encode_params.darWidth = dw;
617 ctx->init_encode_params.frameRateNum = avctx->time_base.den;
618 ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
620 num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
621 ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
623 ctx->init_encode_params.enableEncodeAsync = 0;
624 ctx->init_encode_params.enablePTD = 1;
626 ctx->init_encode_params.presetGUID = encoder_preset;
628 ctx->init_encode_params.encodeConfig = &ctx->encode_config;
629 memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
630 ctx->encode_config.version = NV_ENC_CONFIG_VER;
632 if (avctx->refs >= 0) {
633 /* 0 means "let the hardware decide" */
634 switch (avctx->codec->id) {
635 case AV_CODEC_ID_H264:
636 ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
638 case AV_CODEC_ID_H265:
639 ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
641 /* Earlier switch/case will return if unknown codec is passed. */
645 if (avctx->gop_size > 0) {
646 if (avctx->max_b_frames >= 0) {
647 /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
648 ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
651 ctx->encode_config.gopLength = avctx->gop_size;
652 switch (avctx->codec->id) {
653 case AV_CODEC_ID_H264:
654 ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
656 case AV_CODEC_ID_H265:
657 ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
659 /* Earlier switch/case will return if unknown codec is passed. */
661 } else if (avctx->gop_size == 0) {
662 ctx->encode_config.frameIntervalP = 0;
663 ctx->encode_config.gopLength = 1;
664 switch (avctx->codec->id) {
665 case AV_CODEC_ID_H264:
666 ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
668 case AV_CODEC_ID_H265:
669 ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
671 /* Earlier switch/case will return if unknown codec is passed. */
675 /* when there're b frames, set dts offset */
676 if (ctx->encode_config.frameIntervalP >= 2)
679 if (avctx->bit_rate > 0)
680 ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
682 if (avctx->rc_max_rate > 0)
683 ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
687 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
688 } else if (ctx->twopass == 1 || isLL) {
689 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
691 if (avctx->codec->id == AV_CODEC_ID_H264) {
692 ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
693 ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
697 av_log(avctx, AV_LOG_WARNING, "Twopass mode is only known to work with low latency (ll, llhq, llhp) presets.\n");
699 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
701 } else if (avctx->global_quality > 0) {
702 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
703 ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
704 ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
705 ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
709 } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
710 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
712 ctx->encode_config.rcParams.enableMinQP = 1;
713 ctx->encode_config.rcParams.enableMaxQP = 1;
715 ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
716 ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
717 ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
719 ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
720 ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
721 ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
724 if (avctx->rc_buffer_size > 0)
725 ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
727 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
728 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
730 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
733 switch (avctx->codec->id) {
734 case AV_CODEC_ID_H264:
735 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1;
736 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
738 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace;
739 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries;
740 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc;
742 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
744 ctx->encode_config.encodeCodecConfig.h264Config.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
745 ctx->encode_config.encodeCodecConfig.h264Config.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
748 switch (avctx->profile) {
749 case FF_PROFILE_H264_BASELINE:
750 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
752 case FF_PROFILE_H264_MAIN:
753 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
755 case FF_PROFILE_H264_HIGH:
756 case FF_PROFILE_UNKNOWN:
757 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
760 av_log(avctx, AV_LOG_WARNING, "Unsupported profile requested, falling back to high\n");
761 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
765 if (!strcmp(ctx->profile, "high")) {
766 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
767 avctx->profile = FF_PROFILE_H264_HIGH;
768 } else if (!strcmp(ctx->profile, "main")) {
769 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
770 avctx->profile = FF_PROFILE_H264_MAIN;
771 } else if (!strcmp(ctx->profile, "baseline")) {
772 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
773 avctx->profile = FF_PROFILE_H264_BASELINE;
775 av_log(avctx, AV_LOG_FATAL, "Profile \"%s\" is unknown! Supported profiles: high, main, baseline\n", ctx->profile);
776 res = AVERROR(EINVAL);
781 case AV_CODEC_ID_H265:
782 ctx->encode_config.encodeCodecConfig.hevcConfig.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
783 ctx->encode_config.encodeCodecConfig.hevcConfig.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
785 /* No other profile is supported in the current SDK version 5 */
786 ctx->encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
787 avctx->profile = FF_PROFILE_HEVC_MAIN;
789 /* Earlier switch/case will return if unknown codec is passed. */
792 nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
793 if (nv_status != NV_ENC_SUCCESS) {
794 av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status);
795 res = AVERROR_EXTERNAL;
799 ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
801 if (!ctx->input_surfaces) {
802 res = AVERROR(ENOMEM);
806 ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
808 if (!ctx->output_surfaces) {
809 res = AVERROR(ENOMEM);
813 for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) {
814 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
815 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
816 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
817 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
819 allocSurf.width = (avctx->width + 31) & ~31;
820 allocSurf.height = (avctx->height + 31) & ~31;
822 allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
824 switch (avctx->pix_fmt) {
825 case AV_PIX_FMT_YUV420P:
826 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
829 case AV_PIX_FMT_NV12:
830 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
833 case AV_PIX_FMT_YUV444P:
834 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
838 av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n");
839 res = AVERROR(EINVAL);
843 nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
844 if (nv_status != NV_ENC_SUCCESS) {
845 av_log(avctx, AV_LOG_FATAL, "CreateInputBuffer failed\n");
846 res = AVERROR_EXTERNAL;
850 ctx->input_surfaces[surfaceCount].lockCount = 0;
851 ctx->input_surfaces[surfaceCount].input_surface = allocSurf.inputBuffer;
852 ctx->input_surfaces[surfaceCount].format = allocSurf.bufferFmt;
853 ctx->input_surfaces[surfaceCount].width = allocSurf.width;
854 ctx->input_surfaces[surfaceCount].height = allocSurf.height;
856 /* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */
857 allocOut.size = 1024 * 1024;
859 allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
861 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
862 if (nv_status != NV_ENC_SUCCESS) {
863 av_log(avctx, AV_LOG_FATAL, "CreateBitstreamBuffer failed\n");
864 ctx->output_surfaces[surfaceCount++].output_surface = NULL;
865 res = AVERROR_EXTERNAL;
869 ctx->output_surfaces[surfaceCount].output_surface = allocOut.bitstreamBuffer;
870 ctx->output_surfaces[surfaceCount].size = allocOut.size;
871 ctx->output_surfaces[surfaceCount].busy = 0;
874 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
875 uint32_t outSize = 0;
877 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
878 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
880 payload.spsppsBuffer = tmpHeader;
881 payload.inBufferSize = sizeof(tmpHeader);
882 payload.outSPSPPSPayloadSize = &outSize;
884 nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
885 if (nv_status != NV_ENC_SUCCESS) {
886 av_log(avctx, AV_LOG_FATAL, "GetSequenceParams failed\n");
890 avctx->extradata_size = outSize;
891 avctx->extradata = av_mallocz(outSize + FF_INPUT_BUFFER_PADDING_SIZE);
893 if (!avctx->extradata) {
894 res = AVERROR(ENOMEM);
898 memcpy(avctx->extradata, tmpHeader, outSize);
901 if (ctx->encode_config.frameIntervalP > 1)
902 avctx->has_b_frames = 2;
904 if (ctx->encode_config.rcParams.averageBitRate > 0)
905 avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
911 for (i = 0; i < surfaceCount; ++i) {
912 p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
913 if (ctx->output_surfaces[i].output_surface)
914 p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
918 p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
921 dl_fn->cu_ctx_destroy(ctx->cu_context);
923 av_frame_free(&avctx->coded_frame);
925 nvenc_unload_nvenc(avctx);
927 ctx->nvencoder = NULL;
928 ctx->cu_context = NULL;
933 static av_cold int nvenc_encode_close(AVCodecContext *avctx)
935 NvencContext *ctx = avctx->priv_data;
936 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
937 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
940 av_freep(&ctx->timestamp_list.data);
941 av_freep(&ctx->output_surface_ready_queue.data);
942 av_freep(&ctx->output_surface_queue.data);
944 for (i = 0; i < ctx->max_surface_count; ++i) {
945 p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
946 p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
948 ctx->max_surface_count = 0;
950 p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
951 ctx->nvencoder = NULL;
953 dl_fn->cu_ctx_destroy(ctx->cu_context);
954 ctx->cu_context = NULL;
956 nvenc_unload_nvenc(avctx);
958 av_frame_free(&avctx->coded_frame);
963 static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, AVFrame *coded_frame, NvencOutputSurface *tmpoutsurf)
965 NvencContext *ctx = avctx->priv_data;
966 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
967 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
969 uint32_t slice_mode_data;
970 uint32_t *slice_offsets;
971 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
972 NVENCSTATUS nv_status;
975 switch (avctx->codec->id) {
976 case AV_CODEC_ID_H264:
977 slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
979 case AV_CODEC_ID_H265:
980 slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
983 av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
984 res = AVERROR(EINVAL);
987 slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
990 return AVERROR(ENOMEM);
992 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
994 lock_params.doNotWait = 0;
995 lock_params.outputBitstream = tmpoutsurf->output_surface;
996 lock_params.sliceOffsets = slice_offsets;
998 nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
999 if (nv_status != NV_ENC_SUCCESS) {
1000 av_log(avctx, AV_LOG_ERROR, "Failed locking bitstream buffer\n");
1001 res = AVERROR_EXTERNAL;
1005 if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes)) {
1006 p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1010 memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1012 nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1013 if (nv_status != NV_ENC_SUCCESS)
1014 av_log(avctx, AV_LOG_ERROR, "Failed unlocking bitstream buffer, expect the gates of mordor to open\n");
1016 switch (lock_params.pictureType) {
1017 case NV_ENC_PIC_TYPE_IDR:
1018 pkt->flags |= AV_PKT_FLAG_KEY;
1019 case NV_ENC_PIC_TYPE_I:
1020 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
1022 case NV_ENC_PIC_TYPE_P:
1023 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
1025 case NV_ENC_PIC_TYPE_B:
1026 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
1028 case NV_ENC_PIC_TYPE_BI:
1029 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_BI;
1032 av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
1033 av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
1034 res = AVERROR_EXTERNAL;
1038 pkt->pts = lock_params.outputTimeStamp;
1039 pkt->dts = timestamp_queue_dequeue(&ctx->timestamp_list);
1041 /* when there're b frame(s), set dts offset */
1042 if (ctx->encode_config.frameIntervalP >= 2)
1045 if (pkt->dts > pkt->pts)
1046 pkt->dts = pkt->pts;
1048 if (ctx->last_dts != AV_NOPTS_VALUE && pkt->dts <= ctx->last_dts)
1049 pkt->dts = ctx->last_dts + 1;
1051 ctx->last_dts = pkt->dts;
1053 av_free(slice_offsets);
1059 av_free(slice_offsets);
1060 timestamp_queue_dequeue(&ctx->timestamp_list);
1065 static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1066 const AVFrame *frame, int *got_packet)
1068 NVENCSTATUS nv_status;
1069 NvencOutputSurface *tmpoutsurf;
1072 NvencContext *ctx = avctx->priv_data;
1073 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1074 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1076 NV_ENC_PIC_PARAMS pic_params = { 0 };
1077 pic_params.version = NV_ENC_PIC_PARAMS_VER;
1080 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1081 NvencInputSurface *inSurf = NULL;
1083 for (i = 0; i < ctx->max_surface_count; ++i) {
1084 if (!ctx->input_surfaces[i].lockCount) {
1085 inSurf = &ctx->input_surfaces[i];
1092 inSurf->lockCount = 1;
1094 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1095 lockBufferParams.inputBuffer = inSurf->input_surface;
1097 nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
1098 if (nv_status != NV_ENC_SUCCESS) {
1099 av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n");
1103 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
1104 uint8_t *buf = lockBufferParams.bufferDataPtr;
1106 av_image_copy_plane(buf, lockBufferParams.pitch,
1107 frame->data[0], frame->linesize[0],
1108 avctx->width, avctx->height);
1110 buf += inSurf->height * lockBufferParams.pitch;
1112 av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
1113 frame->data[2], frame->linesize[2],
1114 avctx->width >> 1, avctx->height >> 1);
1116 buf += (inSurf->height * lockBufferParams.pitch) >> 2;
1118 av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
1119 frame->data[1], frame->linesize[1],
1120 avctx->width >> 1, avctx->height >> 1);
1121 } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
1122 uint8_t *buf = lockBufferParams.bufferDataPtr;
1124 av_image_copy_plane(buf, lockBufferParams.pitch,
1125 frame->data[0], frame->linesize[0],
1126 avctx->width, avctx->height);
1128 buf += inSurf->height * lockBufferParams.pitch;
1130 av_image_copy_plane(buf, lockBufferParams.pitch,
1131 frame->data[1], frame->linesize[1],
1132 avctx->width, avctx->height >> 1);
1133 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
1134 uint8_t *buf = lockBufferParams.bufferDataPtr;
1136 av_image_copy_plane(buf, lockBufferParams.pitch,
1137 frame->data[0], frame->linesize[0],
1138 avctx->width, avctx->height);
1140 buf += inSurf->height * lockBufferParams.pitch;
1142 av_image_copy_plane(buf, lockBufferParams.pitch,
1143 frame->data[1], frame->linesize[1],
1144 avctx->width, avctx->height);
1146 buf += inSurf->height * lockBufferParams.pitch;
1148 av_image_copy_plane(buf, lockBufferParams.pitch,
1149 frame->data[2], frame->linesize[2],
1150 avctx->width, avctx->height);
1152 av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
1153 return AVERROR(EINVAL);
1156 nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, inSurf->input_surface);
1157 if (nv_status != NV_ENC_SUCCESS) {
1158 av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n");
1159 return AVERROR_EXTERNAL;
1162 for (i = 0; i < ctx->max_surface_count; ++i)
1163 if (!ctx->output_surfaces[i].busy)
1166 if (i == ctx->max_surface_count) {
1167 inSurf->lockCount = 0;
1168 av_log(avctx, AV_LOG_FATAL, "No free output surface found!\n");
1169 return AVERROR_EXTERNAL;
1172 ctx->output_surfaces[i].input_surface = inSurf;
1174 pic_params.inputBuffer = inSurf->input_surface;
1175 pic_params.bufferFmt = inSurf->format;
1176 pic_params.inputWidth = avctx->width;
1177 pic_params.inputHeight = avctx->height;
1178 pic_params.outputBitstream = ctx->output_surfaces[i].output_surface;
1179 pic_params.completionEvent = 0;
1181 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
1182 if (frame->top_field_first) {
1183 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
1185 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
1188 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
1191 pic_params.encodePicFlags = 0;
1192 pic_params.inputTimeStamp = frame->pts;
1193 pic_params.inputDuration = 0;
1194 switch (avctx->codec->id) {
1195 case AV_CODEC_ID_H264:
1196 pic_params.codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1197 pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1199 case AV_CODEC_ID_H265:
1200 pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1201 pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1204 av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
1205 return AVERROR(EINVAL);
1208 res = timestamp_queue_enqueue(&ctx->timestamp_list, frame->pts);
1213 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1216 nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
1218 if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT) {
1219 res = out_surf_queue_enqueue(&ctx->output_surface_queue, &ctx->output_surfaces[i]);
1224 ctx->output_surfaces[i].busy = 1;
1227 if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1228 av_log(avctx, AV_LOG_ERROR, "EncodePicture failed!\n");
1229 return AVERROR_EXTERNAL;
1232 if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1233 while (ctx->output_surface_queue.count) {
1234 tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_queue);
1235 res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, tmpoutsurf);
1242 res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, &ctx->output_surfaces[i]);
1247 ctx->output_surfaces[i].busy = 1;
1251 if (ctx->output_surface_ready_queue.count) {
1252 tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_ready_queue);
1254 res = process_output_surface(avctx, pkt, avctx->coded_frame, tmpoutsurf);
1259 tmpoutsurf->busy = 0;
1260 av_assert0(tmpoutsurf->input_surface->lockCount);
1261 tmpoutsurf->input_surface->lockCount--;
1271 static enum AVPixelFormat pix_fmts_nvenc[] = {
1276 #define OFFSET(x) offsetof(NvencContext, x)
1277 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1278 static const AVOption options[] = {
1279 { "preset", "Set the encoding preset (one of hq, hp, bd, ll, llhq, llhp, default)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "hq" }, 0, 0, VE },
1280 { "profile", "Set the encoding profile (high, main or baseline)", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
1281 { "cbr", "Use cbr encoding mode", OFFSET(cbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1282 { "2pass", "Use 2pass cbr encoding mode (low latency mode only)", OFFSET(twopass), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
1283 { "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 },
1287 static const AVCodecDefault nvenc_defaults[] = {
1297 #if CONFIG_NVENC_ENCODER
1298 static const AVClass nvenc_class = {
1299 .class_name = "nvenc",
1300 .item_name = av_default_item_name,
1302 .version = LIBAVUTIL_VERSION_INT,
1305 AVCodec ff_nvenc_encoder = {
1307 .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h264 encoder"),
1308 .type = AVMEDIA_TYPE_VIDEO,
1309 .id = AV_CODEC_ID_H264,
1310 .priv_data_size = sizeof(NvencContext),
1311 .init = nvenc_encode_init,
1312 .encode2 = nvenc_encode_frame,
1313 .close = nvenc_encode_close,
1314 .capabilities = CODEC_CAP_DELAY,
1315 .priv_class = &nvenc_class,
1316 .defaults = nvenc_defaults,
1317 .pix_fmts = pix_fmts_nvenc,
1321 #if CONFIG_NVENC_H265_ENCODER
1322 static const AVClass nvenc_h265_class = {
1323 .class_name = "nvenc_h265",
1324 .item_name = av_default_item_name,
1326 .version = LIBAVUTIL_VERSION_INT,
1329 AVCodec ff_nvenc_h265_encoder = {
1330 .name = "nvenc_h265",
1331 .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h265 encoder"),
1332 .type = AVMEDIA_TYPE_VIDEO,
1333 .id = AV_CODEC_ID_H265,
1334 .priv_data_size = sizeof(NvencContext),
1335 .init = nvenc_encode_init,
1336 .encode2 = nvenc_encode_frame,
1337 .close = nvenc_encode_close,
1338 .capabilities = CODEC_CAP_DELAY,
1339 .priv_class = &nvenc_h265_class,
1340 .defaults = nvenc_defaults,
1341 .pix_fmts = pix_fmts_nvenc,