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 NvencValuePair
138 typedef struct NvencContext
142 NvencDynLoadFunctions nvenc_dload_funcs;
144 NV_ENC_INITIALIZE_PARAMS init_encode_params;
145 NV_ENC_CONFIG encode_config;
146 CUcontext cu_context;
148 int max_surface_count;
149 NvencInputSurface *input_surfaces;
150 NvencOutputSurface *output_surfaces;
152 NvencDataList output_surface_queue;
153 NvencDataList output_surface_ready_queue;
154 NvencDataList timestamp_list;
168 static const NvencValuePair nvenc_h264_level_pairs[] = {
169 { "auto", NV_ENC_LEVEL_AUTOSELECT },
170 { "1" , NV_ENC_LEVEL_H264_1 },
171 { "1.0" , NV_ENC_LEVEL_H264_1 },
172 { "1b" , NV_ENC_LEVEL_H264_1b },
173 { "1.0b", NV_ENC_LEVEL_H264_1b },
174 { "1.1" , NV_ENC_LEVEL_H264_11 },
175 { "1.2" , NV_ENC_LEVEL_H264_12 },
176 { "1.3" , NV_ENC_LEVEL_H264_13 },
177 { "2" , NV_ENC_LEVEL_H264_2 },
178 { "2.0" , NV_ENC_LEVEL_H264_2 },
179 { "2.1" , NV_ENC_LEVEL_H264_21 },
180 { "2.2" , NV_ENC_LEVEL_H264_22 },
181 { "3" , NV_ENC_LEVEL_H264_3 },
182 { "3.0" , NV_ENC_LEVEL_H264_3 },
183 { "3.1" , NV_ENC_LEVEL_H264_31 },
184 { "3.2" , NV_ENC_LEVEL_H264_32 },
185 { "4" , NV_ENC_LEVEL_H264_4 },
186 { "4.0" , NV_ENC_LEVEL_H264_4 },
187 { "4.1" , NV_ENC_LEVEL_H264_41 },
188 { "4.2" , NV_ENC_LEVEL_H264_42 },
189 { "5" , NV_ENC_LEVEL_H264_5 },
190 { "5.0" , NV_ENC_LEVEL_H264_5 },
191 { "5.1" , NV_ENC_LEVEL_H264_51 },
195 static const NvencValuePair nvenc_h265_level_pairs[] = {
196 { "auto", NV_ENC_LEVEL_AUTOSELECT },
197 { "1" , NV_ENC_LEVEL_HEVC_1 },
198 { "1.0" , NV_ENC_LEVEL_HEVC_1 },
199 { "2" , NV_ENC_LEVEL_HEVC_2 },
200 { "2.0" , NV_ENC_LEVEL_HEVC_2 },
201 { "2.1" , NV_ENC_LEVEL_HEVC_21 },
202 { "3" , NV_ENC_LEVEL_HEVC_3 },
203 { "3.0" , NV_ENC_LEVEL_HEVC_3 },
204 { "3.1" , NV_ENC_LEVEL_HEVC_31 },
205 { "4" , NV_ENC_LEVEL_HEVC_4 },
206 { "4.0" , NV_ENC_LEVEL_HEVC_4 },
207 { "4.1" , NV_ENC_LEVEL_HEVC_41 },
208 { "5" , NV_ENC_LEVEL_HEVC_5 },
209 { "5.0" , NV_ENC_LEVEL_HEVC_5 },
210 { "5.1" , NV_ENC_LEVEL_HEVC_51 },
211 { "5.2" , NV_ENC_LEVEL_HEVC_52 },
212 { "6" , NV_ENC_LEVEL_HEVC_6 },
213 { "6.0" , NV_ENC_LEVEL_HEVC_6 },
214 { "6.1" , NV_ENC_LEVEL_HEVC_61 },
215 { "6.2" , NV_ENC_LEVEL_HEVC_62 },
219 static int input_string_to_uint32(AVCodecContext *avctx, const NvencValuePair *pair, const char *input, uint32_t *output)
221 for (; pair->str; ++pair) {
222 if (!strcmp(input, pair->str)) {
228 return AVERROR(EINVAL);
231 static NvencData* data_queue_dequeue(NvencDataList* queue)
237 av_assert0(queue->size);
238 av_assert0(queue->data);
243 /* Size always is a multiple of two */
244 mask = queue->size - 1;
245 read_pos = (queue->pos - queue->count) & mask;
248 return &queue->data[read_pos];
251 static int data_queue_enqueue(NvencDataList* queue, NvencData *data)
253 NvencDataList new_queue;
258 /* size always has to be a multiple of two */
263 queue->data = av_malloc(queue->size * sizeof(*(queue->data)));
267 return AVERROR(ENOMEM);
271 if (queue->count == queue->size) {
272 new_queue.size = queue->size << 1;
275 new_queue.data = av_malloc(new_queue.size * sizeof(*(queue->data)));
278 return AVERROR(ENOMEM);
280 while (tmp_data = data_queue_dequeue(queue))
281 data_queue_enqueue(&new_queue, tmp_data);
283 av_free(queue->data);
287 mask = queue->size - 1;
289 queue->data[queue->pos] = *data;
290 queue->pos = (queue->pos + 1) & mask;
296 static int out_surf_queue_enqueue(NvencDataList* queue, NvencOutputSurface* surface)
299 data.surface = surface;
301 return data_queue_enqueue(queue, &data);
304 static NvencOutputSurface* out_surf_queue_dequeue(NvencDataList* queue)
306 NvencData* res = data_queue_dequeue(queue);
314 static int timestamp_queue_enqueue(NvencDataList* queue, int64_t timestamp)
317 data.timestamp = timestamp;
319 return data_queue_enqueue(queue, &data);
322 static int64_t timestamp_queue_dequeue(NvencDataList* queue)
324 NvencData* res = data_queue_dequeue(queue);
327 return AV_NOPTS_VALUE;
329 return res->timestamp;
332 #define CHECK_LOAD_FUNC(t, f, s) \
334 (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \
336 av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \
341 static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx)
343 NvencContext *ctx = avctx->priv_data;
344 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
350 dl_fn->cuda_lib = LoadLibrary(TEXT("nvcuda.dll"));
352 dl_fn->cuda_lib = dlopen("libcuda.so", RTLD_LAZY);
355 if (!dl_fn->cuda_lib) {
356 av_log(avctx, AV_LOG_FATAL, "Failed loading CUDA library\n");
360 CHECK_LOAD_FUNC(PCUINIT, dl_fn->cu_init, "cuInit");
361 CHECK_LOAD_FUNC(PCUDEVICEGETCOUNT, dl_fn->cu_device_get_count, "cuDeviceGetCount");
362 CHECK_LOAD_FUNC(PCUDEVICEGET, dl_fn->cu_device_get, "cuDeviceGet");
363 CHECK_LOAD_FUNC(PCUDEVICEGETNAME, dl_fn->cu_device_get_name, "cuDeviceGetName");
364 CHECK_LOAD_FUNC(PCUDEVICECOMPUTECAPABILITY, dl_fn->cu_device_compute_capability, "cuDeviceComputeCapability");
365 CHECK_LOAD_FUNC(PCUCTXCREATE, dl_fn->cu_ctx_create, "cuCtxCreate_v2");
366 CHECK_LOAD_FUNC(PCUCTXPOPCURRENT, dl_fn->cu_ctx_pop_current, "cuCtxPopCurrent_v2");
367 CHECK_LOAD_FUNC(PCUCTXDESTROY, dl_fn->cu_ctx_destroy, "cuCtxDestroy_v2");
374 DL_CLOSE_FUNC(dl_fn->cuda_lib);
376 dl_fn->cuda_lib = NULL;
381 static av_cold int check_cuda_errors(AVCodecContext *avctx, CUresult err, const char *func)
383 if (err != CUDA_SUCCESS) {
384 av_log(avctx, AV_LOG_FATAL, ">> %s - failed with error code 0x%x\n", func, err);
389 #define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error
391 static av_cold int nvenc_check_cuda(AVCodecContext *avctx)
393 int device_count = 0;
394 CUdevice cu_device = 0;
396 int smminor = 0, smmajor = 0;
397 int i, smver, target_smver;
399 NvencContext *ctx = avctx->priv_data;
400 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
402 switch (avctx->codec->id) {
403 case AV_CODEC_ID_H264:
406 case AV_CODEC_ID_H265:
410 av_log(avctx, AV_LOG_FATAL, "nvenc: Unknown codec name\n");
414 if (!nvenc_dyload_cuda(avctx))
417 if (dl_fn->nvenc_device_count > 0)
420 check_cuda_errors(dl_fn->cu_init(0));
422 check_cuda_errors(dl_fn->cu_device_get_count(&device_count));
425 av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
429 av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", device_count);
431 dl_fn->nvenc_device_count = 0;
433 for (i = 0; i < device_count; ++i) {
434 check_cuda_errors(dl_fn->cu_device_get(&cu_device, i));
435 check_cuda_errors(dl_fn->cu_device_get_name(gpu_name, sizeof(gpu_name), cu_device));
436 check_cuda_errors(dl_fn->cu_device_compute_capability(&smmajor, &smminor, cu_device));
438 smver = (smmajor << 4) | smminor;
440 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");
442 if (smver >= target_smver)
443 dl_fn->nvenc_devices[dl_fn->nvenc_device_count++] = cu_device;
446 if (!dl_fn->nvenc_device_count) {
447 av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
455 dl_fn->nvenc_device_count = 0;
460 static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx)
462 PNVENCODEAPICREATEINSTANCE nvEncodeAPICreateInstance = 0;
463 NVENCSTATUS nvstatus;
465 NvencContext *ctx = avctx->priv_data;
466 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
468 if (!nvenc_check_cuda(avctx))
471 if (dl_fn->nvenc_lib)
475 if (sizeof(void*) == 8) {
476 dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI64.dll"));
478 dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI.dll"));
481 dl_fn->nvenc_lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
484 if (!dl_fn->nvenc_lib) {
485 av_log(avctx, AV_LOG_FATAL, "Failed loading the nvenc library\n");
489 nvEncodeAPICreateInstance = (PNVENCODEAPICREATEINSTANCE)LOAD_FUNC(dl_fn->nvenc_lib, "NvEncodeAPICreateInstance");
491 if (!nvEncodeAPICreateInstance) {
492 av_log(avctx, AV_LOG_FATAL, "Failed to load nvenc entrypoint\n");
496 dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
498 nvstatus = nvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
500 if (nvstatus != NV_ENC_SUCCESS) {
501 av_log(avctx, AV_LOG_FATAL, "Failed to create nvenc instance\n");
505 av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
510 if (dl_fn->nvenc_lib)
511 DL_CLOSE_FUNC(dl_fn->nvenc_lib);
513 dl_fn->nvenc_lib = NULL;
518 static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
520 NvencContext *ctx = avctx->priv_data;
521 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
523 DL_CLOSE_FUNC(dl_fn->nvenc_lib);
524 dl_fn->nvenc_lib = NULL;
526 dl_fn->nvenc_device_count = 0;
528 DL_CLOSE_FUNC(dl_fn->cuda_lib);
529 dl_fn->cuda_lib = NULL;
531 dl_fn->cu_init = NULL;
532 dl_fn->cu_device_get_count = NULL;
533 dl_fn->cu_device_get = NULL;
534 dl_fn->cu_device_get_name = NULL;
535 dl_fn->cu_device_compute_capability = NULL;
536 dl_fn->cu_ctx_create = NULL;
537 dl_fn->cu_ctx_pop_current = NULL;
538 dl_fn->cu_ctx_destroy = NULL;
540 av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
543 static av_cold int nvenc_encode_init(AVCodecContext *avctx)
545 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
546 NV_ENC_PRESET_CONFIG preset_config = { 0 };
547 CUcontext cu_context_curr;
549 GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
551 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
552 int surfaceCount = 0;
558 NvencContext *ctx = avctx->priv_data;
559 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
560 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
562 if (!nvenc_dyload_nvenc(avctx))
563 return AVERROR_EXTERNAL;
565 avctx->coded_frame = av_frame_alloc();
566 if (!avctx->coded_frame) {
567 res = AVERROR(ENOMEM);
571 ctx->last_dts = AV_NOPTS_VALUE;
573 ctx->encode_config.version = NV_ENC_CONFIG_VER;
574 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
575 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
576 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
577 encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
578 encode_session_params.apiVersion = NVENCAPI_VERSION;
580 if (ctx->gpu >= dl_fn->nvenc_device_count) {
581 av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count);
582 res = AVERROR(EINVAL);
586 ctx->cu_context = NULL;
587 cu_res = dl_fn->cu_ctx_create(&ctx->cu_context, 0, dl_fn->nvenc_devices[ctx->gpu]);
589 if (cu_res != CUDA_SUCCESS) {
590 av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
591 res = AVERROR_EXTERNAL;
595 cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr);
597 if (cu_res != CUDA_SUCCESS) {
598 av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
599 res = AVERROR_EXTERNAL;
603 encode_session_params.device = ctx->cu_context;
604 encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
606 nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->nvencoder);
607 if (nv_status != NV_ENC_SUCCESS) {
608 ctx->nvencoder = NULL;
609 av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x - invalid license key?\n", (int)nv_status);
610 res = AVERROR_EXTERNAL;
615 if (!strcmp(ctx->preset, "hp")) {
616 encoder_preset = NV_ENC_PRESET_HP_GUID;
617 } else if (!strcmp(ctx->preset, "hq")) {
618 encoder_preset = NV_ENC_PRESET_HQ_GUID;
619 } else if (!strcmp(ctx->preset, "bd")) {
620 encoder_preset = NV_ENC_PRESET_BD_GUID;
621 } else if (!strcmp(ctx->preset, "ll")) {
622 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
624 } else if (!strcmp(ctx->preset, "llhp")) {
625 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
627 } else if (!strcmp(ctx->preset, "llhq")) {
628 encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
630 } else if (!strcmp(ctx->preset, "default")) {
631 encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
633 av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: hp, hq, bd, ll, llhp, llhq, default\n", ctx->preset);
634 res = AVERROR(EINVAL);
639 switch (avctx->codec->id) {
640 case AV_CODEC_ID_H264:
641 codec = NV_ENC_CODEC_H264_GUID;
643 case AV_CODEC_ID_H265:
644 codec = NV_ENC_CODEC_HEVC_GUID;
647 av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
648 res = AVERROR(EINVAL);
652 nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
653 if (nv_status != NV_ENC_SUCCESS) {
654 av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
655 res = AVERROR_EXTERNAL;
659 ctx->init_encode_params.encodeGUID = codec;
660 ctx->init_encode_params.encodeHeight = avctx->height;
661 ctx->init_encode_params.encodeWidth = avctx->width;
663 if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
664 (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
666 avctx->width * avctx->sample_aspect_ratio.num,
667 avctx->height * avctx->sample_aspect_ratio.den,
669 ctx->init_encode_params.darHeight = dh;
670 ctx->init_encode_params.darWidth = dw;
672 ctx->init_encode_params.darHeight = avctx->height;
673 ctx->init_encode_params.darWidth = avctx->width;
676 // De-compensate for hardware, dubiously, trying to compensate for
677 // playback at 704 pixel width.
678 if (avctx->width == 720 &&
679 (avctx->height == 480 || avctx->height == 576)) {
681 ctx->init_encode_params.darWidth * 44,
682 ctx->init_encode_params.darHeight * 45,
684 ctx->init_encode_params.darHeight = dh;
685 ctx->init_encode_params.darWidth = dw;
688 ctx->init_encode_params.frameRateNum = avctx->time_base.den;
689 ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
691 num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
692 ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
694 ctx->init_encode_params.enableEncodeAsync = 0;
695 ctx->init_encode_params.enablePTD = 1;
697 ctx->init_encode_params.presetGUID = encoder_preset;
699 ctx->init_encode_params.encodeConfig = &ctx->encode_config;
700 memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
701 ctx->encode_config.version = NV_ENC_CONFIG_VER;
703 if (avctx->refs >= 0) {
704 /* 0 means "let the hardware decide" */
705 switch (avctx->codec->id) {
706 case AV_CODEC_ID_H264:
707 ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
709 case AV_CODEC_ID_H265:
710 ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
712 /* Earlier switch/case will return if unknown codec is passed. */
716 if (avctx->gop_size > 0) {
717 if (avctx->max_b_frames >= 0) {
718 /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
719 ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
722 ctx->encode_config.gopLength = avctx->gop_size;
723 switch (avctx->codec->id) {
724 case AV_CODEC_ID_H264:
725 ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
727 case AV_CODEC_ID_H265:
728 ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
730 /* Earlier switch/case will return if unknown codec is passed. */
732 } else if (avctx->gop_size == 0) {
733 ctx->encode_config.frameIntervalP = 0;
734 ctx->encode_config.gopLength = 1;
735 switch (avctx->codec->id) {
736 case AV_CODEC_ID_H264:
737 ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
739 case AV_CODEC_ID_H265:
740 ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
742 /* Earlier switch/case will return if unknown codec is passed. */
746 /* when there're b frames, set dts offset */
747 if (ctx->encode_config.frameIntervalP >= 2)
750 if (avctx->bit_rate > 0)
751 ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
753 if (avctx->rc_max_rate > 0)
754 ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
758 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
759 } else if (ctx->twopass == 1 || isLL) {
760 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
762 if (avctx->codec->id == AV_CODEC_ID_H264) {
763 ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
764 ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
768 av_log(avctx, AV_LOG_WARNING, "Twopass mode is only known to work with low latency (ll, llhq, llhp) presets.\n");
770 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
772 } else if (avctx->global_quality > 0) {
773 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
774 ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
775 ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
776 ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
780 } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
781 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
783 ctx->encode_config.rcParams.enableMinQP = 1;
784 ctx->encode_config.rcParams.enableMaxQP = 1;
786 ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
787 ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
788 ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
790 ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
791 ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
792 ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
795 if (avctx->rc_buffer_size > 0)
796 ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
798 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
799 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
801 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
804 switch (avctx->codec->id) {
805 case AV_CODEC_ID_H264:
806 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1;
807 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
809 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace;
810 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries;
811 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc;
813 ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
815 ctx->encode_config.encodeCodecConfig.h264Config.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
816 ctx->encode_config.encodeCodecConfig.h264Config.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
819 switch (avctx->profile) {
820 case FF_PROFILE_H264_BASELINE:
821 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
823 case FF_PROFILE_H264_MAIN:
824 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
826 case FF_PROFILE_H264_HIGH:
827 case FF_PROFILE_UNKNOWN:
828 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
831 av_log(avctx, AV_LOG_WARNING, "Unsupported profile requested, falling back to high\n");
832 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
836 if (!strcmp(ctx->profile, "high")) {
837 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
838 avctx->profile = FF_PROFILE_H264_HIGH;
839 } else if (!strcmp(ctx->profile, "main")) {
840 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
841 avctx->profile = FF_PROFILE_H264_MAIN;
842 } else if (!strcmp(ctx->profile, "baseline")) {
843 ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
844 avctx->profile = FF_PROFILE_H264_BASELINE;
846 av_log(avctx, AV_LOG_FATAL, "Profile \"%s\" is unknown! Supported profiles: high, main, baseline\n", ctx->profile);
847 res = AVERROR(EINVAL);
853 res = input_string_to_uint32(avctx, nvenc_h264_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.h264Config.level);
856 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);
860 ctx->encode_config.encodeCodecConfig.h264Config.level = NV_ENC_LEVEL_AUTOSELECT;
864 case AV_CODEC_ID_H265:
865 ctx->encode_config.encodeCodecConfig.hevcConfig.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
866 ctx->encode_config.encodeCodecConfig.hevcConfig.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
868 /* No other profile is supported in the current SDK version 5 */
869 ctx->encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
870 avctx->profile = FF_PROFILE_HEVC_MAIN;
873 res = input_string_to_uint32(avctx, nvenc_h265_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.hevcConfig.level);
876 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);
880 ctx->encode_config.encodeCodecConfig.hevcConfig.level = NV_ENC_LEVEL_AUTOSELECT;
884 if (!strcmp(ctx->tier, "main")) {
885 ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_MAIN;
886 } else if (!strcmp(ctx->tier, "high")) {
887 ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_HIGH;
889 av_log(avctx, AV_LOG_FATAL, "Tier \"%s\" is unknown! Supported tiers: main, high\n", ctx->tier);
890 res = AVERROR(EINVAL);
896 /* Earlier switch/case will return if unknown codec is passed. */
899 nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
900 if (nv_status != NV_ENC_SUCCESS) {
901 av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status);
902 res = AVERROR_EXTERNAL;
906 ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
908 if (!ctx->input_surfaces) {
909 res = AVERROR(ENOMEM);
913 ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
915 if (!ctx->output_surfaces) {
916 res = AVERROR(ENOMEM);
920 for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) {
921 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
922 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
923 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
924 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
926 allocSurf.width = (avctx->width + 31) & ~31;
927 allocSurf.height = (avctx->height + 31) & ~31;
929 allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
931 switch (avctx->pix_fmt) {
932 case AV_PIX_FMT_YUV420P:
933 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
936 case AV_PIX_FMT_NV12:
937 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
940 case AV_PIX_FMT_YUV444P:
941 allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
945 av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n");
946 res = AVERROR(EINVAL);
950 nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
951 if (nv_status != NV_ENC_SUCCESS) {
952 av_log(avctx, AV_LOG_FATAL, "CreateInputBuffer failed\n");
953 res = AVERROR_EXTERNAL;
957 ctx->input_surfaces[surfaceCount].lockCount = 0;
958 ctx->input_surfaces[surfaceCount].input_surface = allocSurf.inputBuffer;
959 ctx->input_surfaces[surfaceCount].format = allocSurf.bufferFmt;
960 ctx->input_surfaces[surfaceCount].width = allocSurf.width;
961 ctx->input_surfaces[surfaceCount].height = allocSurf.height;
963 /* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */
964 allocOut.size = 1024 * 1024;
966 allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
968 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
969 if (nv_status != NV_ENC_SUCCESS) {
970 av_log(avctx, AV_LOG_FATAL, "CreateBitstreamBuffer failed\n");
971 ctx->output_surfaces[surfaceCount++].output_surface = NULL;
972 res = AVERROR_EXTERNAL;
976 ctx->output_surfaces[surfaceCount].output_surface = allocOut.bitstreamBuffer;
977 ctx->output_surfaces[surfaceCount].size = allocOut.size;
978 ctx->output_surfaces[surfaceCount].busy = 0;
981 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
982 uint32_t outSize = 0;
984 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
985 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
987 payload.spsppsBuffer = tmpHeader;
988 payload.inBufferSize = sizeof(tmpHeader);
989 payload.outSPSPPSPayloadSize = &outSize;
991 nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
992 if (nv_status != NV_ENC_SUCCESS) {
993 av_log(avctx, AV_LOG_FATAL, "GetSequenceParams failed\n");
997 avctx->extradata_size = outSize;
998 avctx->extradata = av_mallocz(outSize + FF_INPUT_BUFFER_PADDING_SIZE);
1000 if (!avctx->extradata) {
1001 res = AVERROR(ENOMEM);
1005 memcpy(avctx->extradata, tmpHeader, outSize);
1008 if (ctx->encode_config.frameIntervalP > 1)
1009 avctx->has_b_frames = 2;
1011 if (ctx->encode_config.rcParams.averageBitRate > 0)
1012 avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
1018 for (i = 0; i < surfaceCount; ++i) {
1019 p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
1020 if (ctx->output_surfaces[i].output_surface)
1021 p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
1025 p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1027 if (ctx->cu_context)
1028 dl_fn->cu_ctx_destroy(ctx->cu_context);
1030 av_frame_free(&avctx->coded_frame);
1032 nvenc_unload_nvenc(avctx);
1034 ctx->nvencoder = NULL;
1035 ctx->cu_context = NULL;
1040 static av_cold int nvenc_encode_close(AVCodecContext *avctx)
1042 NvencContext *ctx = avctx->priv_data;
1043 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1044 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1047 av_freep(&ctx->timestamp_list.data);
1048 av_freep(&ctx->output_surface_ready_queue.data);
1049 av_freep(&ctx->output_surface_queue.data);
1051 for (i = 0; i < ctx->max_surface_count; ++i) {
1052 p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface);
1053 p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface);
1055 ctx->max_surface_count = 0;
1057 p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
1058 ctx->nvencoder = NULL;
1060 dl_fn->cu_ctx_destroy(ctx->cu_context);
1061 ctx->cu_context = NULL;
1063 nvenc_unload_nvenc(avctx);
1065 av_frame_free(&avctx->coded_frame);
1070 static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, AVFrame *coded_frame, 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 uint32_t slice_mode_data;
1077 uint32_t *slice_offsets;
1078 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
1079 NVENCSTATUS nv_status;
1082 switch (avctx->codec->id) {
1083 case AV_CODEC_ID_H264:
1084 slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1086 case AV_CODEC_ID_H265:
1087 slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1090 av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
1091 res = AVERROR(EINVAL);
1094 slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
1097 return AVERROR(ENOMEM);
1099 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
1101 lock_params.doNotWait = 0;
1102 lock_params.outputBitstream = tmpoutsurf->output_surface;
1103 lock_params.sliceOffsets = slice_offsets;
1105 nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
1106 if (nv_status != NV_ENC_SUCCESS) {
1107 av_log(avctx, AV_LOG_ERROR, "Failed locking bitstream buffer\n");
1108 res = AVERROR_EXTERNAL;
1112 if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes)) {
1113 p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1117 memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1119 nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1120 if (nv_status != NV_ENC_SUCCESS)
1121 av_log(avctx, AV_LOG_ERROR, "Failed unlocking bitstream buffer, expect the gates of mordor to open\n");
1123 switch (lock_params.pictureType) {
1124 case NV_ENC_PIC_TYPE_IDR:
1125 pkt->flags |= AV_PKT_FLAG_KEY;
1126 case NV_ENC_PIC_TYPE_I:
1127 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
1129 case NV_ENC_PIC_TYPE_P:
1130 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
1132 case NV_ENC_PIC_TYPE_B:
1133 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
1135 case NV_ENC_PIC_TYPE_BI:
1136 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_BI;
1139 av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
1140 av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
1141 res = AVERROR_EXTERNAL;
1145 pkt->pts = lock_params.outputTimeStamp;
1146 pkt->dts = timestamp_queue_dequeue(&ctx->timestamp_list);
1148 /* when there're b frame(s), set dts offset */
1149 if (ctx->encode_config.frameIntervalP >= 2)
1152 if (pkt->dts > pkt->pts)
1153 pkt->dts = pkt->pts;
1155 if (ctx->last_dts != AV_NOPTS_VALUE && pkt->dts <= ctx->last_dts)
1156 pkt->dts = ctx->last_dts + 1;
1158 ctx->last_dts = pkt->dts;
1160 av_free(slice_offsets);
1166 av_free(slice_offsets);
1167 timestamp_queue_dequeue(&ctx->timestamp_list);
1172 static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1173 const AVFrame *frame, int *got_packet)
1175 NVENCSTATUS nv_status;
1176 NvencOutputSurface *tmpoutsurf;
1179 NvencContext *ctx = avctx->priv_data;
1180 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1181 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1183 NV_ENC_PIC_PARAMS pic_params = { 0 };
1184 pic_params.version = NV_ENC_PIC_PARAMS_VER;
1187 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1188 NvencInputSurface *inSurf = NULL;
1190 for (i = 0; i < ctx->max_surface_count; ++i) {
1191 if (!ctx->input_surfaces[i].lockCount) {
1192 inSurf = &ctx->input_surfaces[i];
1199 inSurf->lockCount = 1;
1201 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1202 lockBufferParams.inputBuffer = inSurf->input_surface;
1204 nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
1205 if (nv_status != NV_ENC_SUCCESS) {
1206 av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n");
1210 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
1211 uint8_t *buf = lockBufferParams.bufferDataPtr;
1213 av_image_copy_plane(buf, lockBufferParams.pitch,
1214 frame->data[0], frame->linesize[0],
1215 avctx->width, avctx->height);
1217 buf += inSurf->height * lockBufferParams.pitch;
1219 av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
1220 frame->data[2], frame->linesize[2],
1221 avctx->width >> 1, avctx->height >> 1);
1223 buf += (inSurf->height * lockBufferParams.pitch) >> 2;
1225 av_image_copy_plane(buf, lockBufferParams.pitch >> 1,
1226 frame->data[1], frame->linesize[1],
1227 avctx->width >> 1, avctx->height >> 1);
1228 } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
1229 uint8_t *buf = lockBufferParams.bufferDataPtr;
1231 av_image_copy_plane(buf, lockBufferParams.pitch,
1232 frame->data[0], frame->linesize[0],
1233 avctx->width, avctx->height);
1235 buf += inSurf->height * lockBufferParams.pitch;
1237 av_image_copy_plane(buf, lockBufferParams.pitch,
1238 frame->data[1], frame->linesize[1],
1239 avctx->width, avctx->height >> 1);
1240 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
1241 uint8_t *buf = lockBufferParams.bufferDataPtr;
1243 av_image_copy_plane(buf, lockBufferParams.pitch,
1244 frame->data[0], frame->linesize[0],
1245 avctx->width, avctx->height);
1247 buf += inSurf->height * lockBufferParams.pitch;
1249 av_image_copy_plane(buf, lockBufferParams.pitch,
1250 frame->data[1], frame->linesize[1],
1251 avctx->width, avctx->height);
1253 buf += inSurf->height * lockBufferParams.pitch;
1255 av_image_copy_plane(buf, lockBufferParams.pitch,
1256 frame->data[2], frame->linesize[2],
1257 avctx->width, avctx->height);
1259 av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
1260 return AVERROR(EINVAL);
1263 nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, inSurf->input_surface);
1264 if (nv_status != NV_ENC_SUCCESS) {
1265 av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n");
1266 return AVERROR_EXTERNAL;
1269 for (i = 0; i < ctx->max_surface_count; ++i)
1270 if (!ctx->output_surfaces[i].busy)
1273 if (i == ctx->max_surface_count) {
1274 inSurf->lockCount = 0;
1275 av_log(avctx, AV_LOG_FATAL, "No free output surface found!\n");
1276 return AVERROR_EXTERNAL;
1279 ctx->output_surfaces[i].input_surface = inSurf;
1281 pic_params.inputBuffer = inSurf->input_surface;
1282 pic_params.bufferFmt = inSurf->format;
1283 pic_params.inputWidth = avctx->width;
1284 pic_params.inputHeight = avctx->height;
1285 pic_params.outputBitstream = ctx->output_surfaces[i].output_surface;
1286 pic_params.completionEvent = 0;
1288 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
1289 if (frame->top_field_first) {
1290 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
1292 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
1295 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
1298 pic_params.encodePicFlags = 0;
1299 pic_params.inputTimeStamp = frame->pts;
1300 pic_params.inputDuration = 0;
1301 switch (avctx->codec->id) {
1302 case AV_CODEC_ID_H264:
1303 pic_params.codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1304 pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1306 case AV_CODEC_ID_H265:
1307 pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1308 pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1311 av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
1312 return AVERROR(EINVAL);
1315 res = timestamp_queue_enqueue(&ctx->timestamp_list, frame->pts);
1320 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1323 nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
1325 if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT) {
1326 res = out_surf_queue_enqueue(&ctx->output_surface_queue, &ctx->output_surfaces[i]);
1331 ctx->output_surfaces[i].busy = 1;
1334 if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1335 av_log(avctx, AV_LOG_ERROR, "EncodePicture failed!\n");
1336 return AVERROR_EXTERNAL;
1339 if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
1340 while (ctx->output_surface_queue.count) {
1341 tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_queue);
1342 res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, tmpoutsurf);
1349 res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, &ctx->output_surfaces[i]);
1354 ctx->output_surfaces[i].busy = 1;
1358 if (ctx->output_surface_ready_queue.count) {
1359 tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_ready_queue);
1361 res = process_output_surface(avctx, pkt, avctx->coded_frame, tmpoutsurf);
1366 tmpoutsurf->busy = 0;
1367 av_assert0(tmpoutsurf->input_surface->lockCount);
1368 tmpoutsurf->input_surface->lockCount--;
1378 static enum AVPixelFormat pix_fmts_nvenc[] = {
1383 #define OFFSET(x) offsetof(NvencContext, x)
1384 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1385 static const AVOption options[] = {
1386 { "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 },
1387 { "profile", "Set the encoding profile (high, main or baseline)", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
1388 { "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, { 0 }, 0, 0, VE },
1389 { "tier", "Set the encoding tier (main or high)", OFFSET(tier), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
1390 { "cbr", "Use cbr encoding mode", OFFSET(cbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1391 { "2pass", "Use 2pass cbr encoding mode (low latency mode only)", OFFSET(twopass), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
1392 { "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 },
1396 static const AVCodecDefault nvenc_defaults[] = {
1406 #if CONFIG_NVENC_ENCODER
1407 static const AVClass nvenc_class = {
1408 .class_name = "nvenc",
1409 .item_name = av_default_item_name,
1411 .version = LIBAVUTIL_VERSION_INT,
1414 AVCodec ff_nvenc_encoder = {
1416 .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h264 encoder"),
1417 .type = AVMEDIA_TYPE_VIDEO,
1418 .id = AV_CODEC_ID_H264,
1419 .priv_data_size = sizeof(NvencContext),
1420 .init = nvenc_encode_init,
1421 .encode2 = nvenc_encode_frame,
1422 .close = nvenc_encode_close,
1423 .capabilities = CODEC_CAP_DELAY,
1424 .priv_class = &nvenc_class,
1425 .defaults = nvenc_defaults,
1426 .pix_fmts = pix_fmts_nvenc,
1430 #if CONFIG_NVENC_H265_ENCODER
1431 static const AVClass nvenc_h265_class = {
1432 .class_name = "nvenc_h265",
1433 .item_name = av_default_item_name,
1435 .version = LIBAVUTIL_VERSION_INT,
1438 AVCodec ff_nvenc_h265_encoder = {
1439 .name = "nvenc_h265",
1440 .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h265 encoder"),
1441 .type = AVMEDIA_TYPE_VIDEO,
1442 .id = AV_CODEC_ID_H265,
1443 .priv_data_size = sizeof(NvencContext),
1444 .init = nvenc_encode_init,
1445 .encode2 = nvenc_encode_frame,
1446 .close = nvenc_encode_close,
1447 .capabilities = CODEC_CAP_DELAY,
1448 .priv_class = &nvenc_h265_class,
1449 .defaults = nvenc_defaults,
1450 .pix_fmts = pix_fmts_nvenc,