26 #include <nvEncodeAPI.h> 29 #define CUDA_LIBNAME "libcuda.so" 34 #define NVENC_LIBNAME "libnvidia-encode.so" 40 #define NVENC_LIBNAME "nvEncodeAPI64.dll" 42 #define NVENC_LIBNAME "nvEncodeAPI.dll" 45 #define dlopen(filename, flags) LoadLibrary((filename)) 46 #define dlsym(handle, symbol) GetProcAddress(handle, symbol) 47 #define dlclose(handle) FreeLibrary(handle) 62 #define NVENC_CAP 0x30 63 #define BITSTREAM_BUFFER_SIZE 1024 * 1024 64 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \ 65 rc == NV_ENC_PARAMS_RC_2_PASS_QUALITY || \ 66 rc == NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP) 68 #define LOAD_LIBRARY(l, path) \ 70 if (!((l) = dlopen(path, RTLD_LAZY))) { \ 71 av_log(avctx, AV_LOG_ERROR, \ 74 return AVERROR_UNKNOWN; \ 78 #define LOAD_SYMBOL(fun, lib, symbol) \ 80 if (!((fun) = dlsym(lib, symbol))) { \ 81 av_log(avctx, AV_LOG_ERROR, \ 84 return AVERROR_UNKNOWN; \ 103 { NV_ENC_SUCCESS, 0,
"success" },
104 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
105 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
106 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
107 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
108 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
109 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
110 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
111 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
112 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
113 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
114 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
115 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
116 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EBUSY),
"lock busy" },
117 { NV_ENC_ERR_NOT_ENOUGH_BUFFER,
AVERROR(ENOBUFS),
"not enough buffer" },
118 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
119 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
123 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EBUSY),
"encoder busy" },
124 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
126 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
127 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
128 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
129 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
130 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
144 *desc =
"unknown error";
149 const char *error_string)
182 "cuDeviceComputeCapability");
191 "NvEncodeAPICreateInstance");
193 nvel->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
196 if (err != NV_ENC_SUCCESS)
204 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
209 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
210 params.apiVersion = NVENCAPI_VERSION;
212 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
214 ret = nv->nvEncOpenEncodeSessionEx(¶ms, &ctx->
nvenc_ctx);
215 if (ret != NV_ENC_SUCCESS) {
227 int i, ret, count = 0;
230 ret = nv->nvEncGetEncodeGUIDCount(ctx->
nvenc_ctx, &count);
232 if (ret != NV_ENC_SUCCESS || !count)
239 ret = nv->nvEncGetEncodeGUIDs(ctx->
nvenc_ctx, guids, count, &count);
240 if (ret != NV_ENC_SUCCESS) {
246 for (i = 0; i < count; i++) {
247 if (!memcmp(&guids[i], &ctx->
params.encodeGUID,
sizeof(*guids))) {
263 NV_ENC_CAPS_PARAM params = { 0 };
266 params.version = NV_ENC_CAPS_PARAM_VER;
267 params.capsToQuery = cap;
269 ret = nv->nvEncGetEncodeCaps(ctx->
nvenc_ctx, ctx->
params.encodeGUID, ¶ms, &val);
271 if (ret == NV_ENC_SUCCESS)
294 if (ret < avctx->
width) {
301 if (ret < avctx->
height) {
308 if (ret < avctx->max_b_frames) {
322 char name[128] = { 0 };
323 int major, minor, ret;
334 "Cannot access the CUDA device %d\n",
347 av_log(avctx, loglevel,
"Device %d [%s] ", cu_device, name);
368 av_log(avctx, loglevel,
"supports NVENC\n");
383 av_log(avctx, loglevel,
"does not support NVENC (major %d minor %d)\n",
396 ctx->
params.encodeGUID = NV_ENC_CODEC_H264_GUID;
399 ctx->
params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
430 int i, nb_devices = 0;
434 "Cannot init CUDA\n");
440 "Cannot enumerate the CUDA devices\n");
445 for (i = 0; i < nb_devices; ++i) {
467 { NV_ENC_PRESET_DEFAULT_GUID },
468 { NV_ENC_PRESET_HP_GUID },
469 { NV_ENC_PRESET_HQ_GUID },
470 { NV_ENC_PRESET_BD_GUID },
472 { NV_ENC_PRESET_LOW_LATENCY_HP_GUID, NVENC_LOWLATENCY },
473 { NV_ENC_PRESET_LOW_LATENCY_HQ_GUID, NVENC_LOWLATENCY },
475 { NV_ENC_PRESET_LOSSLESS_HP_GUID, NVENC_LOSSLESS },
489 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
497 if (avctx->
qmin >= 0) {
499 rc->minQP.qpInterB = avctx->
qmin;
500 rc->minQP.qpInterP = avctx->
qmin;
501 rc->minQP.qpIntra = avctx->
qmin;
504 if (avctx->
qmax >= 0) {
506 rc->maxQP.qpInterB = avctx->
qmax;
507 rc->maxQP.qpInterP = avctx->
qmax;
508 rc->maxQP.qpIntra = avctx->
qmax;
514 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
515 rc->constQP.qpInterB = 0;
516 rc->constQP.qpInterP = 0;
517 rc->constQP.qpIntra = 0;
521 NV_ENC_RC_PARAMS *rc)
526 case NV_ENC_PARAMS_RC_CONSTQP:
529 "The constant quality rate-control requires " 530 "the 'global_quality' option set.\n");
535 case NV_ENC_PARAMS_RC_2_PASS_VBR:
536 case NV_ENC_PARAMS_RC_VBR:
537 if (avctx->
qmin < 0 && avctx->
qmax < 0) {
539 "The variable bitrate rate-control requires " 540 "the 'qmin' and/or 'qmax' option set.\n");
543 case NV_ENC_PARAMS_RC_VBR_MINQP:
544 if (avctx->
qmin < 0) {
546 "The variable bitrate rate-control requires " 547 "the 'qmin' option set.\n");
552 case NV_ENC_PARAMS_RC_CBR:
554 case NV_ENC_PARAMS_RC_2_PASS_QUALITY:
555 case NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP:
558 "The multipass rate-control requires " 559 "a low-latency preset.\n");
564 rc->rateControlMode = ctx->
rc;
570 NV_ENC_RC_PARAMS *rc = &ctx->
config.rcParams;
573 rc->averageBitRate = avctx->
bit_rate;
584 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
585 rc->rateControlMode = NV_ENC_PARAMS_RC_VBR;
592 if (rc->averageBitRate > 0)
593 avctx->
bit_rate = rc->averageBitRate;
599 NV_ENC_CONFIG *cc = &ctx->
config;
600 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
601 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
609 vui->transferCharacteristics = avctx->
color_trc;
613 vui->videoSignalTypePresentFlag = vui->colourDescriptionPresentFlag ||
614 vui->videoFullRangeFlag;
620 h264->maxNumRefFrames = avctx->
refs;
621 h264->idrPeriod = cc->gopLength;
627 h264->qpPrimeYZeroTransformBypassFlag = 1;
629 if (
IS_CBR(cc->rcParams.rateControlMode)) {
630 h264->outputBufferingPeriodSEI = 1;
631 h264->outputPictureTimingSEI = 1;
638 h264->chromaFormatIDC = 3;
640 h264->chromaFormatIDC = 1;
644 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
647 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
650 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
653 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
656 cc->profileGUID = NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID;
660 h264->level = ctx->
level;
668 NV_ENC_CONFIG *cc = &ctx->
config;
669 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
670 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
678 vui->transferCharacteristics = avctx->
color_trc;
682 vui->videoSignalTypePresentFlag = vui->colourDescriptionPresentFlag ||
683 vui->videoFullRangeFlag;
689 hevc->maxNumRefFramesInDPB = avctx->
refs;
690 hevc->idrPeriod = cc->gopLength;
692 if (
IS_CBR(cc->rcParams.rateControlMode)) {
693 hevc->outputBufferingPeriodSEI = 1;
694 hevc->outputPictureTimingSEI = 1;
698 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
705 hevc->level = ctx->
level;
707 hevc->level = NV_ENC_LEVEL_AUTOSELECT;
711 hevc->tier = ctx->
tier;
731 NV_ENC_PRESET_CONFIG preset_cfg = { 0 };
735 ctx->
params.version = NV_ENC_INITIALIZE_PARAMS_VER;
758 ctx->
params.darWidth * 44,
759 ctx->
params.darHeight * 45,
766 ctx->
params.enableEncodeAsync = 0;
767 ctx->
params.enablePTD = 1;
773 preset_cfg.version = NV_ENC_PRESET_CONFIG_VER;
774 preset_cfg.presetCfg.version = NV_ENC_CONFIG_VER;
776 ret = nv->nvEncGetEncodePresetConfig(ctx->
nvenc_ctx,
780 if (ret != NV_ENC_SUCCESS)
783 memcpy(&ctx->
config, &preset_cfg.presetCfg,
sizeof(ctx->
config));
785 ctx->
config.version = NV_ENC_CONFIG_VER;
795 ctx->
config.frameIntervalP = 1;
799 ctx->
config.frameIntervalP = 0;
800 ctx->
config.gopLength = 1;
803 if (ctx->
config.frameIntervalP > 1)
812 ctx->
config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
814 ctx->
config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
821 if (ret != NV_ENC_SUCCESS)
840 NV_ENC_CREATE_BITSTREAM_BUFFER out_buffer = { 0 };
850 ctx->
frames[idx].
format = NV_ENC_BUFFER_FORMAT_YUV444_PL;
861 NV_ENC_CREATE_INPUT_BUFFER in_buffer = { 0 };
863 in_buffer.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
865 in_buffer.width = avctx->
width;
866 in_buffer.height = avctx->
height;
869 in_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED;
871 ret = nv->nvEncCreateInputBuffer(ctx->
nvenc_ctx, &in_buffer);
872 if (ret != NV_ENC_SUCCESS)
875 ctx->
frames[idx].
in = in_buffer.inputBuffer;
878 out_buffer.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
883 out_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED;
885 ret = nv->nvEncCreateBitstreamBuffer(ctx->
nvenc_ctx, &out_buffer);
886 if (ret != NV_ENC_SUCCESS)
889 ctx->
frames[idx].
out = out_buffer.bitstreamBuffer;
926 #define EXTRADATA_SIZE 512 932 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
939 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
944 ret = nv->nvEncGetSequenceParams(ctx->
nvenc_ctx, &payload);
945 if (ret != NV_ENC_SUCCESS)
959 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
960 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
962 nv->nvEncEncodePicture(ctx->
nvenc_ctx, ¶ms);
1015 "hw_frames_ctx must be set when using GPU frames as input\n");
1060 uint8_t *buf = in->bufferDataPtr;
1061 int off = frame->
height * in->pitch;
1122 nv->nvEncUnregisterResource(ctx->
nvenc_ctx,
1142 NV_ENC_REGISTER_RESOURCE reg;
1154 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
1155 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1156 reg.width = frames_ctx->
width;
1157 reg.height = frames_ctx->
height;
1160 reg.resourceToRegister = frame->
data[0];
1162 ret = nv->nvEncRegisterResource(ctx->
nvenc_ctx, ®);
1163 if (ret != NV_ENC_SUCCESS) {
1194 nvenc_frame->
in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
1197 ret = nv->nvEncMapInputResource(ctx->
nvenc_ctx, &nvenc_frame->
in_map);
1198 if (ret != NV_ENC_SUCCESS) {
1204 nvenc_frame->
reg_idx = reg_idx;
1205 nvenc_frame->
in = nvenc_frame->
in_map.mappedResource;
1207 NV_ENC_LOCK_INPUT_BUFFER params = { 0 };
1209 params.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1210 params.inputBuffer = nvenc_frame->
in;
1212 ret = nv->nvEncLockInputBuffer(ctx->
nvenc_ctx, ¶ms);
1213 if (ret != NV_ENC_SUCCESS)
1218 nv->nvEncUnlockInputBuffer(ctx->
nvenc_ctx, nvenc_frame->
in);
1222 ret = nv->nvEncUnlockInputBuffer(ctx->
nvenc_ctx, nvenc_frame->
in);
1223 if (ret != NV_ENC_SUCCESS)
1231 NV_ENC_PIC_PARAMS *params)
1237 params->codecPicParams.h264PicParams.sliceMode =
1238 ctx->
config.encodeCodecConfig.h264Config.sliceMode;
1239 params->codecPicParams.h264PicParams.sliceModeData =
1240 ctx->
config.encodeCodecConfig.h264Config.sliceModeData;
1243 params->codecPicParams.hevcPicParams.sliceMode =
1244 ctx->
config.encodeCodecConfig.hevcConfig.sliceMode;
1245 params->codecPicParams.hevcPicParams.sliceModeData =
1246 ctx->
config.encodeCodecConfig.hevcConfig.sliceModeData;
1262 NV_ENC_LOCK_BITSTREAM *params,
1267 pkt->
pts = params->outputTimeStamp;
1268 pkt->
duration = params->outputDuration;
1277 if ((ts0 < 0 && ts1 > INT64_MAX + ts0) ||
1278 (ts0 > 0 && ts1 < INT64_MIN + ts0))
1282 if ((delta < 0 && ts0 > INT64_MAX + delta) ||
1283 (delta > 0 && ts0 < INT64_MIN + delta))
1297 NV_ENC_LOCK_BITSTREAM params = { 0 };
1305 params.version = NV_ENC_LOCK_BITSTREAM_VER;
1306 params.outputBitstream = frame->
out;
1308 ret = nv->nvEncLockBitstream(ctx->
nvenc_ctx, ¶ms);
1316 memcpy(pkt->
data, params.bitstreamBufferPtr, pkt->
size);
1318 ret = nv->nvEncUnlockBitstream(ctx->
nvenc_ctx, frame->
out);
1323 nv->nvEncUnmapInputResource(ctx->
nvenc_ctx, frame->
in_map.mappedResource);
1336 switch (params.pictureType) {
1337 case NV_ENC_PIC_TYPE_IDR:
1339 #if FF_API_CODED_FRAME 1341 case NV_ENC_PIC_TYPE_INTRA_REFRESH:
1342 case NV_ENC_PIC_TYPE_I:
1345 case NV_ENC_PIC_TYPE_P:
1348 case NV_ENC_PIC_TYPE_B:
1351 case NV_ENC_PIC_TYPE_BI:
1364 int nb_ready, nb_pending;
1375 return nb_ready > 0;
1376 return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->
async_depth);
1380 const AVFrame *frame,
int *got_packet)
1384 NV_ENC_PIC_PARAMS params = { 0 };
1388 params.version = NV_ENC_PIC_PARAMS_VER;
1401 params.inputBuffer = nvenc_frame->
in;
1402 params.bufferFmt = nvenc_frame->
format;
1403 params.inputWidth = frame->
width;
1404 params.inputHeight = frame->
height;
1405 params.outputBitstream = nvenc_frame->
out;
1406 params.inputTimeStamp = frame->
pts;
1410 params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
1412 params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
1414 params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
1428 params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1431 enc_ret = nv->nvEncEncodePicture(ctx->
nvenc_ctx, ¶ms);
1432 if (enc_ret != NV_ENC_SUCCESS &&
1433 enc_ret != NV_ENC_ERR_NEED_MORE_INPUT)
1443 if (enc_ret == NV_ENC_SUCCESS) {
static int nvenc_setup_extradata(AVCodecContext *avctx)
const struct AVCodec * codec
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
static int nvenc_setup_encoder(AVCodecContext *avctx)
This structure describes decoded (raw) audio or video data.
static void set_vbr(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_LOG_WARNING
Something somehow does not look correct.
memory handling functions
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...
static int nvec_map_preset(NVENCContext *ctx)
enum AVColorRange color_range
MPEG vs JPEG YUV range.
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
int width
The allocated dimensions of the frames in this pool.
struct NVENCContext::@60 registered_frames[MAX_REGISTERED_FRAMES]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
NV_ENC_REGISTERED_PTR regptr
#define FF_ARRAY_ELEMS(a)
PCUDEVICEGET cu_device_get
int min_bitrate
Minimum bitrate of the stream, in bits per second.
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int nvenc_setup_h264_config(AVCodecContext *avctx)
NV_ENC_MAP_INPUT_RESOURCE in_map
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability
enum AVPixelFormat ff_nvenc_pix_fmts[]
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
static int nvenc_setup_device(AVCodecContext *avctx)
#define FF_PROFILE_HEVC_MAIN
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
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.
static void nvenc_setup_rate_control(AVCodecContext *avctx)
#define LOAD_LIBRARY(l, path)
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
static int nvenc_open_session(AVCodecContext *avctx)
PCUDEVICEGETNAME cu_device_get_name
#define AV_LOG_VERBOSE
Detailed information.
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
static int nvenc_check_capabilities(AVCodecContext *avctx)
static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame)
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
static const struct @55 nvenc_errors[]
static int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
An API-specific header for AV_HWDEVICE_TYPE_CUDA.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static int nvenc_dequeue_timestamp(AVFifoBuffer *f, int64_t *pts)
NV_ENC_BUFFER_FORMAT format
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define LOAD_SYMBOL(fun, lib, symbol)
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int qmax
maximum quantizer
static int nvenc_map_error(NVENCSTATUS err, const char **desc)
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
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...
int rc_max_rate
maximum bitrate
static int nvenc_copy_frame(NV_ENC_LOCK_INPUT_BUFFER *in, const AVFrame *frame)
int flags
A combination of AV_PKT_FLAG values.
#define BITSTREAM_BUFFER_SIZE
int rc_buffer_size
decoder bitstream buffer size
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static int nvenc_check_codec_support(AVCodecContext *avctx)
int refs
number of reference frames
int bit_rate
the average bitrate
enum AVPictureType pict_type
Picture type of the frame.
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
int width
picture width / height.
PCUCTXDESTROY cu_ctx_destroy
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
static NVENCFrame * get_free_frame(NVENCContext *ctx)
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
HW acceleration through CUDA.
static void set_constqp(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
the normal 2^n-1 "JPEG" YUV ranges
static int nvenc_print_error(void *log_ctx, NVENCSTATUS err, const char *error_string)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
This structure describes the bitrate properties of an encoded bitstream.
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
NV_ENC_INITIALIZE_PARAMS params
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
static int nvenc_setup_surfaces(AVCodecContext *avctx)
uint8_t * data
The data buffer.
int qmin
minimum quantizer
static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, NVENCFrame *nvenc_frame)
static int nvenc_setup_hevc_config(AVCodecContext *avctx)
static int nvenc_get_output(AVCodecContext *avctx, AVPacket *pkt)
CUcontext cu_context_internal
#define AVERROR_BUG
Bug detected, please report the issue.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
This struct is allocated as AVHWDeviceContext.hwctx.
static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
This struct describes a set or pool of "hardware" frames (i.e.
static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *params)
static int nvenc_enqueue_timestamp(AVFifoBuffer *f, int64_t pts)
static void nvenc_override_rate_control(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int global_quality
Global quality for codecs which cannot change it per frame.
static int64_t pts
Global timestamp for the audio frames.
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
int av_fifo_size(AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
common internal and external API header
PCUCTXPOPCURRENT cu_ctx_pop_current
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
PCUCTXCREATE cu_ctx_create
NVENCSTATUS(NVENCAPI * PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList)
static int output_ready(AVCodecContext *avctx, int flush)
PCUDEVICEGETCOUNT cu_device_get_count
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
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...
int slices
Number of slices.
static int nvenc_set_timestamp(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
#define FF_ENABLE_DEPRECATION_WARNINGS
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
int top_field_first
If the content is interlaced, is top field displayed first.
static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
int avg_bitrate
Average bitrate of the stream, in bits per second.
static void set_lossless(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static int nvenc_check_device(AVCodecContext *avctx, int idx)
static void * av_mallocz_array(size_t nmemb, size_t size)
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.
int rc_min_rate
minimum bitrate
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
AVPixelFormat
Pixel format.
AVFifoBuffer * timestamps
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...
static int nvenc_setup_codec_config(AVCodecContext *avctx)
#define AV_NOPTS_VALUE
Undefined timestamp value.
enum AVPixelFormat data_pix_fmt