Libav
Data Structures | Macros | Functions | Variables
nvenc.c File Reference
#include "config.h"
#include <nvEncodeAPI.h>
#include <string.h>
#include "libavutil/common.h"
#include "libavutil/hwcontext.h"
#include "libavutil/imgutils.h"
#include "libavutil/mem.h"
#include "avcodec.h"
#include "internal.h"
#include "nvenc.h"

Go to the source code of this file.

Data Structures

struct  GUIDTuple
 

Macros

#define CUDA_LIBNAME   "libcuda.so"
 
#define NVENC_CAP   0x30
 
#define BITSTREAM_BUFFER_SIZE   1024 * 1024
 
#define IS_CBR(rc)
 
#define LOAD_LIBRARY(l, path)
 
#define LOAD_SYMBOL(fun, lib, symbol)
 
#define EXTRADATA_SIZE   512
 

Functions

static int nvenc_map_error (NVENCSTATUS err, const char **desc)
 
static int nvenc_print_error (void *log_ctx, NVENCSTATUS err, const char *error_string)
 
static av_cold int nvenc_load_libraries (AVCodecContext *avctx)
 
static int nvenc_open_session (AVCodecContext *avctx)
 
static int nvenc_check_codec_support (AVCodecContext *avctx)
 
static int nvenc_check_cap (AVCodecContext *avctx, NV_ENC_CAPS cap)
 
static int nvenc_check_capabilities (AVCodecContext *avctx)
 
static int nvenc_check_device (AVCodecContext *avctx, int idx)
 
static int nvenc_setup_device (AVCodecContext *avctx)
 
static int nvec_map_preset (NVENCContext *ctx)
 
static void set_constqp (AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
 
static void set_vbr (AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
 
static void set_lossless (AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
 
static void nvenc_override_rate_control (AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
 
static void nvenc_setup_rate_control (AVCodecContext *avctx)
 
static int nvenc_setup_h264_config (AVCodecContext *avctx)
 
static int nvenc_setup_hevc_config (AVCodecContext *avctx)
 
static int nvenc_setup_codec_config (AVCodecContext *avctx)
 
static int nvenc_setup_encoder (AVCodecContext *avctx)
 
static int nvenc_alloc_surface (AVCodecContext *avctx, int idx)
 
static int nvenc_setup_surfaces (AVCodecContext *avctx)
 
static int nvenc_setup_extradata (AVCodecContext *avctx)
 
av_cold int ff_nvenc_encode_close (AVCodecContext *avctx)
 
av_cold int ff_nvenc_encode_init (AVCodecContext *avctx)
 
static NVENCFrameget_free_frame (NVENCContext *ctx)
 
static int nvenc_copy_frame (NV_ENC_LOCK_INPUT_BUFFER *in, const AVFrame *frame)
 
static int nvenc_find_free_reg_resource (AVCodecContext *avctx)
 
static int nvenc_register_frame (AVCodecContext *avctx, const AVFrame *frame)
 
static int nvenc_upload_frame (AVCodecContext *avctx, const AVFrame *frame, NVENCFrame *nvenc_frame)
 
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 int nvenc_dequeue_timestamp (AVFifoBuffer *f, int64_t *pts)
 
static int nvenc_set_timestamp (AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *params, AVPacket *pkt)
 
static int nvenc_get_output (AVCodecContext *avctx, AVPacket *pkt)
 
static int output_ready (AVCodecContext *avctx, int flush)
 
int ff_nvenc_encode_frame (AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
 

Variables

enum AVPixelFormat ff_nvenc_pix_fmts []
 
struct {
   NVENCSTATUS   nverr
 
   int   averr
 
   const char *   desc
 
nvenc_errors []
 

Macro Definition Documentation

◆ CUDA_LIBNAME

#define CUDA_LIBNAME   "libcuda.so"

Definition at line 29 of file nvenc.c.

Referenced by nvenc_load_libraries().

◆ NVENC_CAP

#define NVENC_CAP   0x30

Definition at line 62 of file nvenc.c.

Referenced by nvenc_check_device().

◆ BITSTREAM_BUFFER_SIZE

#define BITSTREAM_BUFFER_SIZE   1024 * 1024

Definition at line 63 of file nvenc.c.

Referenced by nvenc_alloc_surface().

◆ IS_CBR

#define IS_CBR (   rc)
Value:
(rc == NV_ENC_PARAMS_RC_CBR || \
rc == NV_ENC_PARAMS_RC_2_PASS_QUALITY || \
rc == NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP)

Definition at line 64 of file nvenc.c.

Referenced by nvenc_setup_h264_config(), and nvenc_setup_hevc_config().

◆ LOAD_LIBRARY

#define LOAD_LIBRARY (   l,
  path 
)
Value:
do { \
if (!((l) = dlopen(path, RTLD_LAZY))) { \
av_log(avctx, AV_LOG_ERROR, \
"Cannot load %s\n", \
path); \
return AVERROR_UNKNOWN; \
} \
} while (0)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61

Definition at line 68 of file nvenc.c.

Referenced by nvenc_load_libraries().

◆ LOAD_SYMBOL

#define LOAD_SYMBOL (   fun,
  lib,
  symbol 
)
Value:
do { \
if (!((fun) = dlsym(lib, symbol))) { \
av_log(avctx, AV_LOG_ERROR, \
"Cannot load %s\n", \
symbol); \
return AVERROR_UNKNOWN; \
} \
} while (0)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61

Definition at line 78 of file nvenc.c.

Referenced by nvenc_load_libraries().

◆ EXTRADATA_SIZE

#define EXTRADATA_SIZE   512

Definition at line 926 of file nvenc.c.

Referenced by nvenc_setup_extradata().

Function Documentation

◆ nvenc_map_error()

static int nvenc_map_error ( NVENCSTATUS  err,
const char **  desc 
)
static

Definition at line 133 of file nvenc.c.

Referenced by nvenc_print_error().

◆ nvenc_print_error()

static int nvenc_print_error ( void log_ctx,
NVENCSTATUS  err,
const char *  error_string 
)
static

◆ nvenc_load_libraries()

static av_cold int nvenc_load_libraries ( AVCodecContext avctx)
static

Definition at line 158 of file nvenc.c.

Referenced by ff_nvenc_encode_init().

◆ nvenc_open_session()

static int nvenc_open_session ( AVCodecContext avctx)
static

Definition at line 202 of file nvenc.c.

Referenced by nvenc_check_device(), and nvenc_setup_device().

◆ nvenc_check_codec_support()

static int nvenc_check_codec_support ( AVCodecContext avctx)
static

Definition at line 223 of file nvenc.c.

Referenced by nvenc_check_capabilities().

◆ nvenc_check_cap()

static int nvenc_check_cap ( AVCodecContext avctx,
NV_ENC_CAPS  cap 
)
static

Definition at line 259 of file nvenc.c.

Referenced by nvenc_check_capabilities().

◆ nvenc_check_capabilities()

static int nvenc_check_capabilities ( AVCodecContext avctx)
static

Definition at line 276 of file nvenc.c.

Referenced by nvenc_check_device(), and nvenc_setup_device().

◆ nvenc_check_device()

static int nvenc_check_device ( AVCodecContext avctx,
int  idx 
)
static

Definition at line 318 of file nvenc.c.

Referenced by nvenc_setup_device().

◆ nvenc_setup_device()

static int nvenc_setup_device ( AVCodecContext avctx)
static

Definition at line 389 of file nvenc.c.

Referenced by ff_nvenc_encode_init().

◆ nvec_map_preset()

static int nvec_map_preset ( NVENCContext ctx)
static

Definition at line 464 of file nvenc.c.

Referenced by nvenc_setup_encoder().

◆ set_constqp()

static void set_constqp ( AVCodecContext avctx,
NV_ENC_RC_PARAMS *  rc 
)
static

Definition at line 487 of file nvenc.c.

Referenced by nvenc_override_rate_control(), and nvenc_setup_rate_control().

◆ set_vbr()

static void set_vbr ( AVCodecContext avctx,
NV_ENC_RC_PARAMS *  rc 
)
static

Definition at line 495 of file nvenc.c.

Referenced by nvenc_override_rate_control(), and nvenc_setup_rate_control().

◆ set_lossless()

static void set_lossless ( AVCodecContext avctx,
NV_ENC_RC_PARAMS *  rc 
)
static

Definition at line 512 of file nvenc.c.

Referenced by nvenc_setup_rate_control().

◆ nvenc_override_rate_control()

static void nvenc_override_rate_control ( AVCodecContext avctx,
NV_ENC_RC_PARAMS *  rc 
)
static

Definition at line 520 of file nvenc.c.

Referenced by nvenc_setup_rate_control().

◆ nvenc_setup_rate_control()

static void nvenc_setup_rate_control ( AVCodecContext avctx)
static

Definition at line 567 of file nvenc.c.

Referenced by nvenc_setup_encoder().

◆ nvenc_setup_h264_config()

static int nvenc_setup_h264_config ( AVCodecContext avctx)
static

Definition at line 596 of file nvenc.c.

Referenced by nvenc_setup_codec_config().

◆ nvenc_setup_hevc_config()

static int nvenc_setup_hevc_config ( AVCodecContext avctx)
static

Definition at line 665 of file nvenc.c.

Referenced by nvenc_setup_codec_config().

◆ nvenc_setup_codec_config()

static int nvenc_setup_codec_config ( AVCodecContext avctx)
static

Definition at line 716 of file nvenc.c.

Referenced by nvenc_setup_encoder().

◆ nvenc_setup_encoder()

static int nvenc_setup_encoder ( AVCodecContext avctx)
static

Definition at line 727 of file nvenc.c.

Referenced by ff_nvenc_encode_init().

◆ nvenc_alloc_surface()

static int nvenc_alloc_surface ( AVCodecContext avctx,
int  idx 
)
static

Definition at line 835 of file nvenc.c.

Referenced by nvenc_setup_surfaces().

◆ nvenc_setup_surfaces()

static int nvenc_setup_surfaces ( AVCodecContext avctx)
static

Definition at line 894 of file nvenc.c.

Referenced by ff_nvenc_encode_init().

◆ nvenc_setup_extradata()

static int nvenc_setup_extradata ( AVCodecContext avctx)
static

Definition at line 928 of file nvenc.c.

Referenced by ff_nvenc_encode_init().

◆ ff_nvenc_encode_close()

av_cold int ff_nvenc_encode_close ( AVCodecContext avctx)

Definition at line 951 of file nvenc.c.

◆ ff_nvenc_encode_init()

av_cold int ff_nvenc_encode_init ( AVCodecContext avctx)

Definition at line 1006 of file nvenc.c.

Referenced by nvenc_old_init().

◆ get_free_frame()

static NVENCFrame* get_free_frame ( NVENCContext ctx)
static

Definition at line 1044 of file nvenc.c.

Referenced by ff_nvenc_encode_frame().

◆ nvenc_copy_frame()

static int nvenc_copy_frame ( NV_ENC_LOCK_INPUT_BUFFER *  in,
const AVFrame frame 
)
static

Definition at line 1058 of file nvenc.c.

Referenced by nvenc_upload_frame().

◆ nvenc_find_free_reg_resource()

static int nvenc_find_free_reg_resource ( AVCodecContext avctx)
static

Definition at line 1112 of file nvenc.c.

Referenced by nvenc_register_frame().

◆ nvenc_register_frame()

static int nvenc_register_frame ( AVCodecContext avctx,
const AVFrame frame 
)
static

Definition at line 1137 of file nvenc.c.

Referenced by nvenc_upload_frame().

◆ nvenc_upload_frame()

static int nvenc_upload_frame ( AVCodecContext avctx,
const AVFrame frame,
NVENCFrame nvenc_frame 
)
static

Definition at line 1173 of file nvenc.c.

Referenced by ff_nvenc_encode_frame().

◆ nvenc_codec_specific_pic_params()

static void nvenc_codec_specific_pic_params ( AVCodecContext avctx,
NV_ENC_PIC_PARAMS *  params 
)
static

Definition at line 1230 of file nvenc.c.

Referenced by ff_nvenc_encode_frame().

◆ nvenc_enqueue_timestamp()

static int nvenc_enqueue_timestamp ( AVFifoBuffer f,
int64_t  pts 
)
inlinestatic

Definition at line 1251 of file nvenc.c.

Referenced by ff_nvenc_encode_frame().

◆ nvenc_dequeue_timestamp()

static int nvenc_dequeue_timestamp ( AVFifoBuffer f,
int64_t *  pts 
)
inlinestatic

Definition at line 1256 of file nvenc.c.

Referenced by nvenc_set_timestamp().

◆ nvenc_set_timestamp()

static int nvenc_set_timestamp ( AVCodecContext avctx,
NV_ENC_LOCK_BITSTREAM *  params,
AVPacket pkt 
)
static

Definition at line 1261 of file nvenc.c.

Referenced by nvenc_get_output().

◆ nvenc_get_output()

static int nvenc_get_output ( AVCodecContext avctx,
AVPacket pkt 
)
static

Definition at line 1293 of file nvenc.c.

Referenced by ff_nvenc_encode_frame().

◆ output_ready()

static int output_ready ( AVCodecContext avctx,
int  flush 
)
static

Definition at line 1361 of file nvenc.c.

Referenced by ff_nvenc_encode_frame().

◆ ff_nvenc_encode_frame()

int ff_nvenc_encode_frame ( AVCodecContext avctx,
AVPacket pkt,
const AVFrame frame,
int *  got_packet 
)

Definition at line 1379 of file nvenc.c.

Variable Documentation

◆ ff_nvenc_pix_fmts

enum AVPixelFormat ff_nvenc_pix_fmts[]
Initial value:
= {
}
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:64
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:86
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59

Definition at line 88 of file nvenc.c.

◆ nverr

NVENCSTATUS nverr

Definition at line 99 of file nvenc.c.

Referenced by nvenc_map_error().

◆ averr

int averr

Definition at line 100 of file nvenc.c.

◆ desc

const char* desc

Definition at line 101 of file nvenc.c.

Referenced by asf_read_picture(), asf_write_header1(), av_bsf_init(), av_image_alloc(), av_image_copy(), av_image_copy_to_buffer(), av_image_fill_linesizes(), av_image_fill_pointers(), av_image_get_buffer_size(), av_image_get_linesize(), av_picture_crop(), av_picture_pad(), av_pix_fmt_count_planes(), av_pix_fmt_get_chroma_sub_sample(), avcodec_align_dimensions(), avcodec_descriptor_get_by_name(), avcodec_get_chroma_sub_sample(), avcodec_get_type(), avcodec_profile_name(), avcodec_string(), build_frame_code(), check_image_pointers(), config_input(), config_props(), copy_picture_field(), dxva2_create_decoder(), ff_all_formats(), ff_draw_horiz_band(), ff_flac_parse_picture(), ff_get_format(), ff_h264_draw_horiz_band(), ff_hevc_output_frame(), ff_id3v2_write_apic(), ff_sws_init_output_funcs(), ffv1_encode_init(), filter_frame(), find_codec_or_die(), flush_encoders(), frame_end(), get_aspect_ratio(), get_deinterleaved_format(), get_format(), get_sockaddr(), get_video_buffer(), getSubSampleFactors(), hScale16To15_c(), hScale16To19_c(), hwdownload_query_formats(), init(), init_muxer(), init_video_param(), is16BPS(), is9_OR_10BPS(), is_hwaccel_pix_fmt(), isALPHA(), isBE(), isPacked(), isPackedRGB(), isPlanar(), isPlanarRGB(), isPlanarYUV(), isRGB(), isYUV(), libopenjpeg_create_image(), libopenjpeg_decode_frame(), libopenjpeg_ispacked(), libopenjpeg_matches_pix_fmt(), list_formats(), map_pixel_format(), nvenc_print_error(), print_codecs(), print_digraph(), qsv_decode_init(), qsv_init_pool(), query_formats(), raw_decode(), raw_encode_init(), raw_init_decoder(), read_gab2_sub(), rv10_write_header(), show_codecs(), show_help_codec(), show_help_muxer(), show_stream(), shuffleplanes_config_input(), sws_format_name(), swscale(), transcode_init(), unsupported_codec(), usePal(), verify_md5(), wav_write_header(), write_streamheader(), xwd_encode_frame(), and yuv2gbrp_full_X_c().

◆ nvenc_errors

const { ... } nvenc_errors[]
Initial value:
= {
{ NV_ENC_SUCCESS, 0, "success" },
{ NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" },
{ NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" },
{ NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" },
{ NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" },
{ NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" },
{ NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" },
{ NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" },
{ NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" },
{ NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" },
{ NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" },
{ NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" },
{ NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" },
{ NV_ENC_ERR_LOCK_BUSY, AVERROR(EBUSY), "lock busy" },
{ NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOBUFS), "not enough buffer" },
{ NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" },
{ NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" },
{ NV_ENC_ERR_NEED_MORE_INPUT, AVERROR_UNKNOWN, "need more input" },
{ NV_ENC_ERR_ENCODER_BUSY, AVERROR(EBUSY), "encoder busy" },
{ NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" },
{ NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" },
{ NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" },
{ NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" },
{ NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" },
{ NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" },
{ NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" },
}
#define AVERROR(e)
Definition: error.h:43
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61

Referenced by nvenc_map_error().