Skip to content

Instantly share code, notes, and snippets.

@tea
Created April 25, 2016 11:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tea/02d9df6e58a72f78096e7404e44967cc to your computer and use it in GitHub Desktop.
Save tea/02d9df6e58a72f78096e7404e44967cc to your computer and use it in GitHub Desktop.
nvenc sample
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#ifndef NVCPUOPSYS_H
#define NVCPUOPSYS_H
#if defined(_WIN32) || defined(_WIN16)
# define NV_WINDOWS
#endif
#if (defined(__unix__) || defined(__unix) ) && !defined(nvmacosx) && !defined(vxworks) && !defined(__DJGPP__) && !defined(NV_UNIX) && !defined(__QNX__) && !defined(__QNXNTO__)/* XXX until removed from Makefiles */
# define NV_UNIX
#endif /* defined(__unix__) */
#if defined(__linux__) && !defined(NV_LINUX) && !defined(NV_VMWARE)
# define NV_LINUX
#endif /* defined(__linux__) */
#endif
/*
* This copyright notice applies to this header file only:
*
* Copyright (c) 2010-2015 NVIDIA Corporation
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the software, and to permit persons to whom the
* software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file nvEncodeAPI.h
* NvEncodeAPI provides a NVENC Video Encoding interface to NVIDIA GPU devices based on the Kepler architecture.
* \date 2011-2015
* This file contains the interface constants, structure definitions and function prototypes.
*/
#ifndef _NV_ENCODEAPI_H_
#define _NV_ENCODEAPI_H_
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef _MSC_VER
#ifndef _STDINT
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
#endif
#else
#include <stdint.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures
* @{
*/
#ifdef _WIN32
#define NVENCAPI __stdcall
typedef RECT NVENC_RECT;
#else
#define NVENCAPI
// =========================================================================================
#ifndef GUID
/*!
* \struct GUID
* Abstracts the GUID structure for non-windows platforms.
*/
// =========================================================================================
typedef struct
{
uint32_t Data1; /**< [in]: Specifies the first 8 hexadecimal digits of the GUID. */
uint16_t Data2; /**< [in]: Specifies the first group of 4 hexadecimal digits. */
uint16_t Data3; /**< [in]: Specifies the second group of 4 hexadecimal digits. */
uint8_t Data4[8]; /**< [in]: Array of 8 bytes. The first 2 bytes contain the third group of 4 hexadecimal digits.
The remaining 6 bytes contain the final 12 hexadecimal digits. */
} GUID;
#endif // GUID
/**
* \struct _NVENC_RECT
* Defines a Rectangle. Used in ::NV_ENC_PREPROCESS_FRAME.
*/
typedef struct _NVENC_RECT
{
uint32_t left; /**< [in]: X coordinate of the upper left corner of rectangular area to be specified. */
uint32_t top; /**< [in]: Y coordinate of the upper left corner of the rectangular area to be specified. */
uint32_t right; /**< [in]: X coordinate of the bottom right corner of the rectangular area to be specified. */
uint32_t bottom; /**< [in]: Y coordinate of the bottom right corner of the rectangular area to be specified. */
} NVENC_RECT;
#endif // _WIN32
/** @} */ /* End of GUID and NVENC_RECT structure grouping*/
typedef void* NV_ENC_INPUT_PTR; /**< NVENCODE API input buffer */
typedef void* NV_ENC_OUTPUT_PTR; /**< NVENCODE API output buffer*/
typedef void* NV_ENC_REGISTERED_PTR; /**< A Resource that has been registered with NVENCODE API*/
#define NVENCAPI_MAJOR_VERSION 6
#define NVENCAPI_MINOR_VERSION 0
#define NVENCAPI_VERSION (NVENCAPI_MAJOR_VERSION | (NVENCAPI_MINOR_VERSION << 24))
/**
* Macro to generate per-structure version for use with API.
*/
#define NVENCAPI_STRUCT_VERSION(ver) ((uint32_t)NVENCAPI_VERSION | ((ver)<<16) | (0x7 << 28))
#define NVENC_INFINITE_GOPLENGTH 0xffffffff
#define NV_MAX_SEQ_HDR_LEN (512)
// =========================================================================================
// Encode Codec GUIDS supported by the NvEncodeAPI interface.
// =========================================================================================
// {6BC82762-4E63-4ca4-AA85-1E50F321F6BF}
static const GUID NV_ENC_CODEC_H264_GUID =
{ 0x6bc82762, 0x4e63, 0x4ca4, { 0xaa, 0x85, 0x1e, 0x50, 0xf3, 0x21, 0xf6, 0xbf } };
// {790CDC88-4522-4d7b-9425-BDA9975F7603}
static const GUID NV_ENC_CODEC_HEVC_GUID =
{ 0x790cdc88, 0x4522, 0x4d7b, { 0x94, 0x25, 0xbd, 0xa9, 0x97, 0x5f, 0x76, 0x3 } };
// =========================================================================================
// * Encode Profile GUIDS supported by the NvEncodeAPI interface.
// =========================================================================================
// {BFD6F8E7-233C-4341-8B3E-4818523803F4}
static const GUID NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID =
{ 0xbfd6f8e7, 0x233c, 0x4341, { 0x8b, 0x3e, 0x48, 0x18, 0x52, 0x38, 0x3, 0xf4 } };
// {0727BCAA-78C4-4c83-8C2F-EF3DFF267C6A}
static const GUID NV_ENC_H264_PROFILE_BASELINE_GUID =
{ 0x727bcaa, 0x78c4, 0x4c83, { 0x8c, 0x2f, 0xef, 0x3d, 0xff, 0x26, 0x7c, 0x6a } };
// {60B5C1D4-67FE-4790-94D5-C4726D7B6E6D}
static const GUID NV_ENC_H264_PROFILE_MAIN_GUID =
{ 0x60b5c1d4, 0x67fe, 0x4790, { 0x94, 0xd5, 0xc4, 0x72, 0x6d, 0x7b, 0x6e, 0x6d } };
// {E7CBC309-4F7A-4b89-AF2A-D537C92BE310}
static const GUID NV_ENC_H264_PROFILE_HIGH_GUID =
{ 0xe7cbc309, 0x4f7a, 0x4b89, { 0xaf, 0x2a, 0xd5, 0x37, 0xc9, 0x2b, 0xe3, 0x10 } };
// {7AC663CB-A598-4960-B844-339B261A7D52}
static const GUID NV_ENC_H264_PROFILE_HIGH_444_GUID =
{ 0x7ac663cb, 0xa598, 0x4960, { 0xb8, 0x44, 0x33, 0x9b, 0x26, 0x1a, 0x7d, 0x52 } };
// {40847BF5-33F7-4601-9084-E8FE3C1DB8B7}
static const GUID NV_ENC_H264_PROFILE_STEREO_GUID =
{ 0x40847bf5, 0x33f7, 0x4601, { 0x90, 0x84, 0xe8, 0xfe, 0x3c, 0x1d, 0xb8, 0xb7 } };
// {CE788D20-AAA9-4318-92BB-AC7E858C8D36}
static const GUID NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY =
{ 0xce788d20, 0xaaa9, 0x4318, { 0x92, 0xbb, 0xac, 0x7e, 0x85, 0x8c, 0x8d, 0x36 } };
// {B405AFAC-F32B-417B-89C4-9ABEED3E5978}
static const GUID NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID =
{ 0xb405afac, 0xf32b, 0x417b, { 0x89, 0xc4, 0x9a, 0xbe, 0xed, 0x3e, 0x59, 0x78 } };
// {AEC1BD87-E85B-48f2-84C3-98BCA6285072}
static const GUID NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID =
{ 0xaec1bd87, 0xe85b, 0x48f2, { 0x84, 0xc3, 0x98, 0xbc, 0xa6, 0x28, 0x50, 0x72 } };
// {B514C39A-B55B-40fa-878F-F1253B4DFDEC}
static const GUID NV_ENC_HEVC_PROFILE_MAIN_GUID =
{ 0xb514c39a, 0xb55b, 0x40fa, { 0x87, 0x8f, 0xf1, 0x25, 0x3b, 0x4d, 0xfd, 0xec } };
// =========================================================================================
// * Preset GUIDS supported by the NvEncodeAPI interface.
// =========================================================================================
// {B2DFB705-4EBD-4C49-9B5F-24A777D3E587}
static const GUID NV_ENC_PRESET_DEFAULT_GUID =
{ 0xb2dfb705, 0x4ebd, 0x4c49, { 0x9b, 0x5f, 0x24, 0xa7, 0x77, 0xd3, 0xe5, 0x87 } };
// {60E4C59F-E846-4484-A56D-CD45BE9FDDF6}
static const GUID NV_ENC_PRESET_HP_GUID =
{ 0x60e4c59f, 0xe846, 0x4484, { 0xa5, 0x6d, 0xcd, 0x45, 0xbe, 0x9f, 0xdd, 0xf6 } };
// {34DBA71D-A77B-4B8F-9C3E-B6D5DA24C012}
static const GUID NV_ENC_PRESET_HQ_GUID =
{ 0x34dba71d, 0xa77b, 0x4b8f, { 0x9c, 0x3e, 0xb6, 0xd5, 0xda, 0x24, 0xc0, 0x12 } };
// {82E3E450-BDBB-4e40-989C-82A90DF9EF32}
static const GUID NV_ENC_PRESET_BD_GUID =
{ 0x82e3e450, 0xbdbb, 0x4e40, { 0x98, 0x9c, 0x82, 0xa9, 0xd, 0xf9, 0xef, 0x32 } };
// {49DF21C5-6DFA-4feb-9787-6ACC9EFFB726}
static const GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID =
{ 0x49df21c5, 0x6dfa, 0x4feb, { 0x97, 0x87, 0x6a, 0xcc, 0x9e, 0xff, 0xb7, 0x26 } };
// {C5F733B9-EA97-4cf9-BEC2-BF78A74FD105}
static const GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID =
{ 0xc5f733b9, 0xea97, 0x4cf9, { 0xbe, 0xc2, 0xbf, 0x78, 0xa7, 0x4f, 0xd1, 0x5 } };
// {67082A44-4BAD-48FA-98EA-93056D150A58}
static const GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID =
{ 0x67082a44, 0x4bad, 0x48fa, { 0x98, 0xea, 0x93, 0x5, 0x6d, 0x15, 0xa, 0x58 } };
// {D5BFB716-C604-44e7-9BB8-DEA5510FC3AC}
static const GUID NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID =
{ 0xd5bfb716, 0xc604, 0x44e7, { 0x9b, 0xb8, 0xde, 0xa5, 0x51, 0xf, 0xc3, 0xac } };
// {149998E7-2364-411d-82EF-179888093409}
static const GUID NV_ENC_PRESET_LOSSLESS_HP_GUID =
{ 0x149998e7, 0x2364, 0x411d, { 0x82, 0xef, 0x17, 0x98, 0x88, 0x9, 0x34, 0x9 } };
/**
* \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures
* @{
*/
/**
* Input frame encode modes
*/
typedef enum _NV_ENC_PARAMS_FRAME_FIELD_MODE
{
NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME = 0x01, /**< Frame mode */
NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD = 0x02, /**< Field mode */
NV_ENC_PARAMS_FRAME_FIELD_MODE_MBAFF = 0x03 /**< MB adaptive frame/field */
} NV_ENC_PARAMS_FRAME_FIELD_MODE;
/**
* Rate Control Modes
*/
typedef enum _NV_ENC_PARAMS_RC_MODE
{
NV_ENC_PARAMS_RC_CONSTQP = 0x0, /**< Constant QP mode */
NV_ENC_PARAMS_RC_VBR = 0x1, /**< Variable bitrate mode */
NV_ENC_PARAMS_RC_CBR = 0x2, /**< Constant bitrate mode */
NV_ENC_PARAMS_RC_VBR_MINQP = 0x4, /**< Variable bitrate mode with MinQP */
NV_ENC_PARAMS_RC_2_PASS_QUALITY = 0x8, /**< Multi pass encoding optimized for image quality and works only with low latency mode */
NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP = 0x10, /**< Multi pass encoding optimized for maintaining frame size and works only with low latency mode */
NV_ENC_PARAMS_RC_2_PASS_VBR = 0x20 /**< Multi pass VBR */
} NV_ENC_PARAMS_RC_MODE;
#define NV_ENC_PARAMS_RC_CBR2 NV_ENC_PARAMS_RC_CBR /**< Deprecated */
/**
* Input picture structure
*/
typedef enum _NV_ENC_PIC_STRUCT
{
NV_ENC_PIC_STRUCT_FRAME = 0x01, /**< Progressive frame */
NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM = 0x02, /**< Field encoding top field first */
NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP = 0x03 /**< Field encoding bottom field first */
} NV_ENC_PIC_STRUCT;
/**
* Input picture type
*/
typedef enum _NV_ENC_PIC_TYPE
{
NV_ENC_PIC_TYPE_P = 0x0, /**< Forward predicted */
NV_ENC_PIC_TYPE_B = 0x01, /**< Bi-directionally predicted picture */
NV_ENC_PIC_TYPE_I = 0x02, /**< Intra predicted picture */
NV_ENC_PIC_TYPE_IDR = 0x03, /**< IDR picture */
NV_ENC_PIC_TYPE_BI = 0x04, /**< Bi-directionally predicted with only Intra MBs */
NV_ENC_PIC_TYPE_SKIPPED = 0x05, /**< Picture is skipped */
NV_ENC_PIC_TYPE_INTRA_REFRESH = 0x06, /**< First picture in intra refresh cycle */
NV_ENC_PIC_TYPE_UNKNOWN = 0xFF /**< Picture type unknown */
} NV_ENC_PIC_TYPE;
/**
* Motion vector precisions
*/
typedef enum _NV_ENC_MV_PRECISION
{
NV_ENC_MV_PRECISION_DEFAULT = 0x0, /**<Driver selects QuarterPel motion vector precision by default*/
NV_ENC_MV_PRECISION_FULL_PEL = 0x01, /**< FullPel motion vector precision */
NV_ENC_MV_PRECISION_HALF_PEL = 0x02, /**< HalfPel motion vector precision */
NV_ENC_MV_PRECISION_QUARTER_PEL = 0x03 /**< QuarterPel motion vector precision */
} NV_ENC_MV_PRECISION;
/**
* Input buffer formats
*/
typedef enum _NV_ENC_BUFFER_FORMAT
{
NV_ENC_BUFFER_FORMAT_UNDEFINED = 0x0, /**< Undefined buffer format. */
NV_ENC_BUFFER_FORMAT_NV12 = 0x1, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */
NV_ENC_BUFFER_FORMAT_YV12 = 0x10, /**< Planar YUV [Y plane followed by V and U planes] */
NV_ENC_BUFFER_FORMAT_IYUV = 0x100, /**< Planar YUV [Y plane followed by U and V planes] */
NV_ENC_BUFFER_FORMAT_YUV444 = 0x1000, /**< Planar YUV [Y plane followed by U and V planes] */
NV_ENC_BUFFER_FORMAT_ARGB = 0x1000000, /**< 8 bit Packed A8R8G8B8 */
NV_ENC_BUFFER_FORMAT_ARGB10 = 0x2000000, /**< 10 bit Packed A2R10G10B10 */
NV_ENC_BUFFER_FORMAT_AYUV = 0x4000000, /**< 8 bit Packed A8Y8U8V8 */
} NV_ENC_BUFFER_FORMAT;
#define NV_ENC_BUFFER_FORMAT_NV12_PL NV_ENC_BUFFER_FORMAT_NV12
#define NV_ENC_BUFFER_FORMAT_YV12_PL NV_ENC_BUFFER_FORMAT_YV12
#define NV_ENC_BUFFER_FORMAT_IYUV_PL NV_ENC_BUFFER_FORMAT_IYUV
#define NV_ENC_BUFFER_FORMAT_YUV444_PL NV_ENC_BUFFER_FORMAT_YUV444
/**
* Encoding levels
*/
typedef enum _NV_ENC_LEVEL
{
NV_ENC_LEVEL_AUTOSELECT = 0,
NV_ENC_LEVEL_H264_1 = 10,
NV_ENC_LEVEL_H264_1b = 9,
NV_ENC_LEVEL_H264_11 = 11,
NV_ENC_LEVEL_H264_12 = 12,
NV_ENC_LEVEL_H264_13 = 13,
NV_ENC_LEVEL_H264_2 = 20,
NV_ENC_LEVEL_H264_21 = 21,
NV_ENC_LEVEL_H264_22 = 22,
NV_ENC_LEVEL_H264_3 = 30,
NV_ENC_LEVEL_H264_31 = 31,
NV_ENC_LEVEL_H264_32 = 32,
NV_ENC_LEVEL_H264_4 = 40,
NV_ENC_LEVEL_H264_41 = 41,
NV_ENC_LEVEL_H264_42 = 42,
NV_ENC_LEVEL_H264_5 = 50,
NV_ENC_LEVEL_H264_51 = 51,
NV_ENC_LEVEL_H264_52 = 52,
NV_ENC_LEVEL_HEVC_1 = 30,
NV_ENC_LEVEL_HEVC_2 = 60,
NV_ENC_LEVEL_HEVC_21 = 63,
NV_ENC_LEVEL_HEVC_3 = 90,
NV_ENC_LEVEL_HEVC_31 = 93,
NV_ENC_LEVEL_HEVC_4 = 120,
NV_ENC_LEVEL_HEVC_41 = 123,
NV_ENC_LEVEL_HEVC_5 = 150,
NV_ENC_LEVEL_HEVC_51 = 153,
NV_ENC_LEVEL_HEVC_52 = 156,
NV_ENC_LEVEL_HEVC_6 = 180,
NV_ENC_LEVEL_HEVC_61 = 183,
NV_ENC_LEVEL_HEVC_62 = 186,
NV_ENC_TIER_HEVC_MAIN = 0,
NV_ENC_TIER_HEVC_HIGH = 1
} NV_ENC_LEVEL;
/**
* Error Codes
*/
typedef enum _NVENCSTATUS
{
/**
* This indicates that API call returned with no errors.
*/
NV_ENC_SUCCESS,
/**
* This indicates that no encode capable devices were detected.
*/
NV_ENC_ERR_NO_ENCODE_DEVICE,
/**
* This indicates that devices pass by the client is not supported.
*/
NV_ENC_ERR_UNSUPPORTED_DEVICE,
/**
* This indicates that the encoder device supplied by the client is not
* valid.
*/
NV_ENC_ERR_INVALID_ENCODERDEVICE,
/**
* This indicates that device passed to the API call is invalid.
*/
NV_ENC_ERR_INVALID_DEVICE,
/**
* This indicates that device passed to the API call is no longer available and
* needs to be reinitialized. The clients need to destroy the current encoder
* session by freeing the allocated input output buffers and destroying the device
* and create a new encoding session.
*/
NV_ENC_ERR_DEVICE_NOT_EXIST,
/**
* This indicates that one or more of the pointers passed to the API call
* is invalid.
*/
NV_ENC_ERR_INVALID_PTR,
/**
* This indicates that completion event passed in ::NvEncEncodePicture() call
* is invalid.
*/
NV_ENC_ERR_INVALID_EVENT,
/**
* This indicates that one or more of the parameter passed to the API call
* is invalid.
*/
NV_ENC_ERR_INVALID_PARAM,
/**
* This indicates that an API call was made in wrong sequence/order.
*/
NV_ENC_ERR_INVALID_CALL,
/**
* This indicates that the API call failed because it was unable to allocate
* enough memory to perform the requested operation.
*/
NV_ENC_ERR_OUT_OF_MEMORY,
/**
* This indicates that the encoder has not been initialized with
* ::NvEncInitializeEncoder() or that initialization has failed.
* The client cannot allocate input or output buffers or do any encoding
* related operation before successfully initializing the encoder.
*/
NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
/**
* This indicates that an unsupported parameter was passed by the client.
*/
NV_ENC_ERR_UNSUPPORTED_PARAM,
/**
* This indicates that the ::NvEncLockBitstream() failed to lock the output
* buffer. This happens when the client makes a non blocking lock call to
* access the output bitstream by passing NV_ENC_LOCK_BITSTREAM::doNotWait flag.
* This is not a fatal error and client should retry the same operation after
* few milliseconds.
*/
NV_ENC_ERR_LOCK_BUSY,
/**
* This indicates that the size of the user buffer passed by the client is
* insufficient for the requested operation.
*/
NV_ENC_ERR_NOT_ENOUGH_BUFFER,
/**
* This indicates that an invalid struct version was used by the client.
*/
NV_ENC_ERR_INVALID_VERSION,
/**
* This indicates that ::NvEncMapInputResource() API failed to map the client
* provided input resource.
*/
NV_ENC_ERR_MAP_FAILED,
/**
* This indicates encode driver requires more input buffers to produce an output
* bitstream. If this error is returned from ::NvEncEncodePicture() API, this
* is not a fatal error. If the client is encoding with B frames then,
* ::NvEncEncodePicture() API might be buffering the input frame for re-ordering.
*
* A client operating in synchronous mode cannot call ::NvEncLockBitstream()
* API on the output bitstream buffer if ::NvEncEncodePicture() returned the
* ::NV_ENC_ERR_NEED_MORE_INPUT error code.
* The client must continue providing input frames until encode driver returns
* ::NV_ENC_SUCCESS. After receiving ::NV_ENC_SUCCESS status the client can call
* ::NvEncLockBitstream() API on the output buffers in the same order in which
* it has called ::NvEncEncodePicture().
*/
NV_ENC_ERR_NEED_MORE_INPUT,
/**
* This indicates that the HW encoder is busy encoding and is unable to encode
* the input. The client should call ::NvEncEncodePicture() again after few
* milliseconds.
*/
NV_ENC_ERR_ENCODER_BUSY,
/**
* This indicates that the completion event passed in ::NvEncEncodePicture()
* API has not been registered with encoder driver using ::NvEncRegisterAsyncEvent().
*/
NV_ENC_ERR_EVENT_NOT_REGISTERD,
/**
* This indicates that an unknown internal error has occurred.
*/
NV_ENC_ERR_GENERIC,
/**
* This indicates that the client is attempting to use a feature
* that is not available for the license type for the current system.
*/
NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
/**
* This indicates that the client is attempting to use a feature
* that is not implemented for the current version.
*/
NV_ENC_ERR_UNIMPLEMENTED,
/**
* This indicates that the ::NvEncRegisterResource API failed to register the resource.
*/
NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
/**
* This indicates that the client is attempting to unregister a resource
* that has not been successfully registered.
*/
NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
/**
* This indicates that the client is attempting to unmap a resource
* that has not been successfully mapped.
*/
NV_ENC_ERR_RESOURCE_NOT_MAPPED,
} NVENCSTATUS;
/**
* Encode Picture encode flags.
*/
typedef enum _NV_ENC_PIC_FLAGS
{
NV_ENC_PIC_FLAG_FORCEINTRA = 0x1, /**< Encode the current picture as an Intra picture */
NV_ENC_PIC_FLAG_FORCEIDR = 0x2, /**< Encode the current picture as an IDR picture.
This flag is only valid when Picture type decision is taken by the Encoder
[_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */
NV_ENC_PIC_FLAG_OUTPUT_SPSPPS = 0x4, /**< Write the sequence and picture header in encoded bitstream of the current picture */
NV_ENC_PIC_FLAG_EOS = 0x8, /**< Indicates end of the input stream */
} NV_ENC_PIC_FLAGS;
/**
* Memory heap to allocate input and output buffers.
*/
typedef enum _NV_ENC_MEMORY_HEAP
{
NV_ENC_MEMORY_HEAP_AUTOSELECT = 0, /**< Memory heap to be decided by the encoder driver based on the usage */
NV_ENC_MEMORY_HEAP_VID = 1, /**< Memory heap is in local video memory */
NV_ENC_MEMORY_HEAP_SYSMEM_CACHED = 2, /**< Memory heap is in cached system memory */
NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED = 3 /**< Memory heap is in uncached system memory */
} NV_ENC_MEMORY_HEAP;
/**
* H.264 entropy coding modes.
*/
typedef enum _NV_ENC_H264_ENTROPY_CODING_MODE
{
NV_ENC_H264_ENTROPY_CODING_MODE_AUTOSELECT = 0x0, /**< Entropy coding mode is auto selected by the encoder driver */
NV_ENC_H264_ENTROPY_CODING_MODE_CABAC = 0x1, /**< Entropy coding mode is CABAC */
NV_ENC_H264_ENTROPY_CODING_MODE_CAVLC = 0x2 /**< Entropy coding mode is CAVLC */
} NV_ENC_H264_ENTROPY_CODING_MODE;
/**
* H.264 specific Bdirect modes
*/
typedef enum _NV_ENC_H264_BDIRECT_MODE
{
NV_ENC_H264_BDIRECT_MODE_AUTOSELECT = 0x0, /**< BDirect mode is auto selected by the encoder driver */
NV_ENC_H264_BDIRECT_MODE_DISABLE = 0x1, /**< Disable BDirect mode */
NV_ENC_H264_BDIRECT_MODE_TEMPORAL = 0x2, /**< Temporal BDirect mode */
NV_ENC_H264_BDIRECT_MODE_SPATIAL = 0x3 /**< Spatial BDirect mode */
} NV_ENC_H264_BDIRECT_MODE;
/**
* H.264 specific FMO usage
*/
typedef enum _NV_ENC_H264_FMO_MODE
{
NV_ENC_H264_FMO_AUTOSELECT = 0x0, /**< FMO usage is auto selected by the encoder driver */
NV_ENC_H264_FMO_ENABLE = 0x1, /**< Enable FMO */
NV_ENC_H264_FMO_DISABLE = 0x2, /**< Disble FMO */
} NV_ENC_H264_FMO_MODE;
/**
* H.264 specific Adaptive Transform modes
*/
typedef enum _NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE
{
NV_ENC_H264_ADAPTIVE_TRANSFORM_AUTOSELECT = 0x0, /**< Adaptive Transform 8x8 mode is auto selected by the encoder driver*/
NV_ENC_H264_ADAPTIVE_TRANSFORM_DISABLE = 0x1, /**< Adaptive Transform 8x8 mode disabled */
NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE = 0x2, /**< Adaptive Transform 8x8 mode should be used */
} NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE;
/**
* Stereo frame packing modes.
*/
typedef enum _NV_ENC_STEREO_PACKING_MODE
{
NV_ENC_STEREO_PACKING_MODE_NONE = 0x0, /**< No Stereo packing required */
NV_ENC_STEREO_PACKING_MODE_CHECKERBOARD = 0x1, /**< Checkerboard mode for packing stereo frames */
NV_ENC_STEREO_PACKING_MODE_COLINTERLEAVE = 0x2, /**< Column Interleave mode for packing stereo frames */
NV_ENC_STEREO_PACKING_MODE_ROWINTERLEAVE = 0x3, /**< Row Interleave mode for packing stereo frames */
NV_ENC_STEREO_PACKING_MODE_SIDEBYSIDE = 0x4, /**< Side-by-side mode for packing stereo frames */
NV_ENC_STEREO_PACKING_MODE_TOPBOTTOM = 0x5, /**< Top-Bottom mode for packing stereo frames */
NV_ENC_STEREO_PACKING_MODE_FRAMESEQ = 0x6 /**< Frame Sequential mode for packing stereo frames */
} NV_ENC_STEREO_PACKING_MODE;
/**
* Input Resource type
*/
typedef enum _NV_ENC_INPUT_RESOURCE_TYPE
{
NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX = 0x0, /**< input resource type is a directx9 surface*/
NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR = 0x1, /**< input resource type is a cuda device pointer surface*/
NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY = 0x2, /**< input resource type is a cuda array surface */
} NV_ENC_INPUT_RESOURCE_TYPE;
/**
* Encoder Device type
*/
typedef enum _NV_ENC_DEVICE_TYPE
{
NV_ENC_DEVICE_TYPE_DIRECTX = 0x0, /**< encode device type is a directx9 device */
NV_ENC_DEVICE_TYPE_CUDA = 0x1, /**< encode device type is a cuda device */
} NV_ENC_DEVICE_TYPE;
/**
* Encoder capabilities enumeration.
*/
typedef enum _NV_ENC_CAPS
{
/**
* Maximum number of B-Frames supported.
*/
NV_ENC_CAPS_NUM_MAX_BFRAMES,
/**
* Rate control modes supported.
* \n The API return value is a bitmask of the values in NV_ENC_PARAMS_RC_MODE.
*/
NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES,
/**
* Indicates HW support for field mode encoding.
* \n 0 : Interlaced mode encoding is not supported.
* \n 1 : Interlaced field mode encoding is supported.
* \n 2 : Interlaced frame encoding and field mode encoding are both supported.
*/
NV_ENC_CAPS_SUPPORT_FIELD_ENCODING,
/**
* Indicates HW support for monochrome mode encoding.
* \n 0 : Monochrome mode not supported.
* \n 1 : Monochrome mode supported.
*/
NV_ENC_CAPS_SUPPORT_MONOCHROME,
/**
* Indicates HW support for FMO.
* \n 0 : FMO not supported.
* \n 1 : FMO supported.
*/
NV_ENC_CAPS_SUPPORT_FMO,
/**
* Indicates HW capability for Quarter pel motion estimation.
* \n 0 : QuarterPel Motion Estimation not supported.
* \n 1 : QuarterPel Motion Estimation supported.
*/
NV_ENC_CAPS_SUPPORT_QPELMV,
/**
* H.264 specific. Indicates HW support for BDirect modes.
* \n 0 : BDirect mode encoding not supported.
* \n 1 : BDirect mode encoding supported.
*/
NV_ENC_CAPS_SUPPORT_BDIRECT_MODE,
/**
* H264 specific. Indicates HW support for CABAC entropy coding mode.
* \n 0 : CABAC entropy coding not supported.
* \n 1 : CABAC entropy coding supported.
*/
NV_ENC_CAPS_SUPPORT_CABAC,
/**
* Indicates HW support for Adaptive Transform.
* \n 0 : Adaptive Transform not supported.
* \n 1 : Adaptive Transform supported.
*/
NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM,
/**
* Reserved enum field.
*/
NV_ENC_CAPS_SUPPORT_RESERVED,
/**
* Indicates HW support for encoding Temporal layers.
* \n 0 : Encoding Temporal layers not supported.
* \n 1 : Encoding Temporal layers supported.
*/
NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS,
/**
* Indicates HW support for Hierarchical P frames.
* \n 0 : Hierarchical P frames not supported.
* \n 1 : Hierarchical P frames supported.
*/
NV_ENC_CAPS_SUPPORT_HIERARCHICAL_PFRAMES,
/**
* Indicates HW support for Hierarchical B frames.
* \n 0 : Hierarchical B frames not supported.
* \n 1 : Hierarchical B frames supported.
*/
NV_ENC_CAPS_SUPPORT_HIERARCHICAL_BFRAMES,
/**
* Maximum Encoding level supported (See ::NV_ENC_LEVEL for details).
*/
NV_ENC_CAPS_LEVEL_MAX,
/**
* Minimum Encoding level supported (See ::NV_ENC_LEVEL for details).
*/
NV_ENC_CAPS_LEVEL_MIN,
/**
* Indicates HW support for separate colour plane encoding.
* \n 0 : Separate colour plane encoding not supported.
* \n 1 : Separate colour plane encoding supported.
*/
NV_ENC_CAPS_SEPARATE_COLOUR_PLANE,
/**
* Maximum output width supported.
*/
NV_ENC_CAPS_WIDTH_MAX,
/**
* Maximum output height supported.
*/
NV_ENC_CAPS_HEIGHT_MAX,
/**
* Indicates Temporal Scalability Support.
* \n 0 : Temporal SVC encoding not supported.
* \n 1 : Temporal SVC encoding supported.
*/
NV_ENC_CAPS_SUPPORT_TEMPORAL_SVC,
/**
* Indicates Dynamic Encode Resolution Change Support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Dynamic Encode Resolution Change not supported.
* \n 1 : Dynamic Encode Resolution Change supported.
*/
NV_ENC_CAPS_SUPPORT_DYN_RES_CHANGE,
/**
* Indicates Dynamic Encode Bitrate Change Support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Dynamic Encode bitrate change not supported.
* \n 1 : Dynamic Encode bitrate change supported.
*/
NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE,
/**
* Indicates Forcing Constant QP On The Fly Support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Forcing constant QP on the fly not supported.
* \n 1 : Forcing constant QP on the fly supported.
*/
NV_ENC_CAPS_SUPPORT_DYN_FORCE_CONSTQP,
/**
* Indicates Dynamic rate control mode Change Support.
* \n 0 : Dynamic rate control mode change not supported.
* \n 1 : Dynamic rate control mode change supported.
*/
NV_ENC_CAPS_SUPPORT_DYN_RCMODE_CHANGE,
/**
* Indicates Subframe readback support for slice-based encoding.
* \n 0 : Subframe readback not supported.
* \n 1 : Subframe readback supported.
*/
NV_ENC_CAPS_SUPPORT_SUBFRAME_READBACK,
/**
* Indicates Constrained Encoding mode support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Constrained encoding mode not supported.
* \n 1 : Constarined encoding mode supported.
* If this mode is supported client can enable this during initialisation.
* Client can then force a picture to be coded as constrained picture where
* each slice in a constrained picture will have constrained_intra_pred_flag set to 1
* and disable_deblocking_filter_idc will be set to 2 and prediction vectors for inter
* macroblocks in each slice will be restricted to the slice region.
*/
NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING,
/**
* Indicates Intra Refresh Mode Support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Intra Refresh Mode not supported.
* \n 1 : Intra Refresh Mode supported.
*/
NV_ENC_CAPS_SUPPORT_INTRA_REFRESH,
/**
* Indicates Custom VBV Bufer Size support. It can be used for capping frame size.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Custom VBV buffer size specification from client, not supported.
* \n 1 : Custom VBV buffer size specification from client, supported.
*/
NV_ENC_CAPS_SUPPORT_CUSTOM_VBV_BUF_SIZE,
/**
* Indicates Dynamic Slice Mode Support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Dynamic Slice Mode not supported.
* \n 1 : Dynamic Slice Mode supported.
*/
NV_ENC_CAPS_SUPPORT_DYNAMIC_SLICE_MODE,
/**
* Indicates Reference Picture Invalidation Support.
* Support added from NvEncodeAPI version 2.0.
* \n 0 : Reference Picture Invalidation not supported.
* \n 1 : Reference Picture Invalidation supported.
*/
NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION,
/**
* Indicates support for PreProcessing.
* The API return value is a bitmask of the values defined in ::NV_ENC_PREPROC_FLAGS
*/
NV_ENC_CAPS_PREPROC_SUPPORT,
/**
* Indicates support Async mode.
* \n 0 : Async Encode mode not supported.
* \n 1 : Async Encode mode supported.
*/
NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT,
/**
* Maximum MBs per frame supported.
*/
NV_ENC_CAPS_MB_NUM_MAX,
/**
* Maximum aggregate throughput in MBs per sec.
*/
NV_ENC_CAPS_MB_PER_SEC_MAX,
/**
* Indicates HW support for YUV444 mode encoding.
* \n 0 : YUV444 mode encoding not supported.
* \n 1 : YUV444 mode encoding supported.
*/
NV_ENC_CAPS_SUPPORT_YUV444_ENCODE,
/**
* Indicates HW support for lossless encoding.
* \n 0 : lossless encoding not supported.
* \n 1 : lossless encoding supported.
*/
NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE,
/**
* Indicates HW support for Sample Adaptive Offset.
* \n 0 : SAO not supported.
* \n 1 : SAO encoding supported.
*/
NV_ENC_CAPS_SUPPORT_SAO,
/**
* Indicates HW support for MEOnly Mode.
* \n 0 : MEOnly Mode not supported.
* \n 1 : MEOnly Mode supported.
*/
NV_ENC_CAPS_SUPPORT_MEONLY_MODE,
/**
* Reserved - Not to be used by clients.
*/
NV_ENC_CAPS_EXPOSED_COUNT
} NV_ENC_CAPS;
/**
* HEVC CU SIZE
*/
typedef enum _NV_ENC_HEVC_CUSIZE
{
NV_ENC_HEVC_CUSIZE_AUTOSELECT = 0,
NV_ENC_HEVC_CUSIZE_8x8 = 1,
NV_ENC_HEVC_CUSIZE_16x16 = 2,
NV_ENC_HEVC_CUSIZE_32x32 = 3,
NV_ENC_HEVC_CUSIZE_64x64 = 4,
}NV_ENC_HEVC_CUSIZE;
/**
* Input struct for querying Encoding capabilities.
*/
typedef struct _NV_ENC_CAPS_PARAM
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CAPS_PARAM_VER */
NV_ENC_CAPS capsToQuery; /**< [in]: Specifies the encode capability to be queried. Client should pass a member for ::NV_ENC_CAPS enum. */
uint32_t reserved[62]; /**< [in]: Reserved and must be set to 0 */
} NV_ENC_CAPS_PARAM;
/** NV_ENC_CAPS_PARAM struct version. */
#define NV_ENC_CAPS_PARAM_VER NVENCAPI_STRUCT_VERSION(1)
/**
* Creation parameters for input buffer.
*/
typedef struct _NV_ENC_CREATE_INPUT_BUFFER
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_INPUT_BUFFER_VER */
uint32_t width; /**< [in]: Input buffer width */
uint32_t height; /**< [in]: Input buffer width */
NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Input buffer memory heap */
NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Input buffer format */
uint32_t reserved; /**< [in]: Reserved and must be set to 0 */
NV_ENC_INPUT_PTR inputBuffer; /**< [out]: Pointer to input buffer */
void* pSysMemBuffer; /**< [in]: Pointer to existing sysmem buffer */
uint32_t reserved1[57]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_CREATE_INPUT_BUFFER;
/** NV_ENC_CREATE_INPUT_BUFFER struct version. */
#define NV_ENC_CREATE_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
/**
* Creation parameters for output bitstream buffer.
*/
typedef struct _NV_ENC_CREATE_BITSTREAM_BUFFER
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_BITSTREAM_BUFFER_VER */
uint32_t size; /**< [in]: Size of the bitstream buffer to be created */
NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Output buffer memory heap */
uint32_t reserved; /**< [in]: Reserved and must be set to 0 */
NV_ENC_OUTPUT_PTR bitstreamBuffer; /**< [out]: Pointer to the output bitstream buffer */
void* bitstreamBufferPtr; /**< [out]: Reserved and should not be used */
uint32_t reserved1[58]; /**< [in]: Reserved and should be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and should be set to NULL */
} NV_ENC_CREATE_BITSTREAM_BUFFER;
/** NV_ENC_CREATE_BITSTREAM_BUFFER struct version. */
#define NV_ENC_CREATE_BITSTREAM_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
/**
* Structs needed for ME only mode.
*/
typedef struct _NV_ENC_MVECTOR
{
int16_t mvx; /**< the x component of MV in qpel units */
int16_t mvy; /**< the y component of MV in qpel units */
} NV_ENC_MVECTOR;
/**
* Description of MV structure per macroblock for ME only mode.
*/
typedef struct _NV_ENC_H264_MV_DATA
{
NV_ENC_MVECTOR MV[4]; /**< up to 4 vectors for 8x8 partition */
uint8_t mb_type; /**< 0 (I), 1 (P), 2 (IPCM), 3 (B) */
uint8_t partitionType; /**< Specifies the block partition type. 0:16x16, 1:8x8, 2:16x8, 3:8x16 */
uint16_t reserved; /**< reserved padding for alignment */
uint32_t MBCost;
} NV_ENC_H264_MV_DATA;
/**
* Creation parameters for output motion vector buffer for ME only mode.
*/
typedef struct _NV_ENC_CREATE_MV_BUFFER
{
uint32_t version; /**< [in]: Struct version. Must be set to NV_ENC_CREATE_MV_BUFFER_VER */
NV_ENC_OUTPUT_PTR MVBuffer; /**< [out]: Pointer to the output MV buffer */
uint32_t reserved1[255]; /**< [in]: Reserved and should be set to 0 */
void* reserved2[63]; /**< [in]: Reserved and should be set to NULL */
} NV_ENC_CREATE_MV_BUFFER;
/** NV_ENC_CREATE_MV_BUFFER struct version*/
#define NV_ENC_CREATE_MV_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
/**
* QP value for frames
*/
typedef struct _NV_ENC_QP
{
uint32_t qpInterP;
uint32_t qpInterB;
uint32_t qpIntra;
} NV_ENC_QP;
/**
* Rate Control Configuration Paramters
*/
typedef struct _NV_ENC_RC_PARAMS
{
uint32_t version;
NV_ENC_PARAMS_RC_MODE rateControlMode; /**< [in]: Specifies the rate control mode. Check support for various rate control modes using ::NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES caps. */
NV_ENC_QP constQP; /**< [in]: Specifies the initial QP to be used for encoding, these values would be used for all frames if in Constant QP mode. */
uint32_t averageBitRate; /**< [in]: Specifies the average bitrate(in bits/sec) used for encoding. */
uint32_t maxBitRate; /**< [in]: Specifies the maximum bitrate for the encoded output. This is used for VBR and ignored for CBR mode. */
uint32_t vbvBufferSize; /**< [in]: Specifies the VBV(HRD) buffer size. in bits. Set 0 to use the default VBV buffer size. */
uint32_t vbvInitialDelay; /**< [in]: Specifies the VBV(HRD) initial delay in bits. Set 0 to use the default VBV initial delay .*/
uint32_t enableMinQP :1; /**< [in]: Set this to 1 if minimum QP used for rate control. */
uint32_t enableMaxQP :1; /**< [in]: Set this to 1 if maximum QP used for rate control. */
uint32_t enableInitialRCQP :1; /**< [in]: Set this to 1 if user suppplied initial QP is used for rate control. */
uint32_t enableAQ :1; /**< [in]: Set this to 1 to enable adaptive quantization. */
uint32_t enableExtQPDeltaMap :1; /**< [in]: Set this to 1 to enable additional QP modifier for each MB supplied by client though signed byte array pointed to by NV_ENC_PIC_PARAMS::qpDeltaMap */
uint32_t reservedBitFields :27; /**< [in]: Reserved bitfields and must be set to 0 */
NV_ENC_QP minQP; /**< [in]: Specifies the minimum QP used for rate control. Client must set NV_ENC_CONFIG::enableMinQP to 1. */
NV_ENC_QP maxQP; /**< [in]: Specifies the maximum QP used for rate control. Client must set NV_ENC_CONFIG::enableMaxQP to 1. */
NV_ENC_QP initialRCQP; /**< [in]: Specifies the initial QP used for rate control. Client must set NV_ENC_CONFIG::enableInitialRCQP to 1. */
uint32_t temporallayerIdxMask; /**< [in]: Specifies the temporal layers (as a bitmask) whose QPs have changed. Valid max bitmask is [2^NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS - 1] */
uint8_t temporalLayerQP[8]; /**< [in]: Specifies the temporal layer QPs used for rate control. Temporal layer index is used as as the array index */
uint32_t reserved[10];
} NV_ENC_RC_PARAMS;
/** macro for constructing the version field of ::_NV_ENC_RC_PARAMS */
#define NV_ENC_RC_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
/**
* \struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS
* H264 Video Usability Info parameters
*/
typedef struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS
{
uint32_t overscanInfoPresentFlag; /**< [in]: if set to 1 , it specifies that the overscanInfo is present */
uint32_t overscanInfo; /**< [in]: Specifies the overscan info(as defined in Annex E of the ITU-T Specification). */
uint32_t videoSignalTypePresentFlag; /**< [in]: If set to 1, it specifies that the videoFormat, videoFullRangeFlag and colourDescriptionPresentFlag are present. */
uint32_t videoFormat; /**< [in]: Specifies the source video format(as defined in Annex E of the ITU-T Specification).*/
uint32_t videoFullRangeFlag; /**< [in]: Specifies the output range of the luma and chroma samples(as defined in Annex E of the ITU-T Specification). */
uint32_t colourDescriptionPresentFlag; /**< [in]: If set to 1, it specifies that the colourPrimaries, transferCharacteristics and colourMatrix are present. */
uint32_t colourPrimaries; /**< [in]: Specifies color primaries for converting to RGB(as defined in Annex E of the ITU-T Specification) */
uint32_t transferCharacteristics; /**< [in]: Specifies the opto-electronic transfer characteristics to use (as defined in Annex E of the ITU-T Specification) */
uint32_t colourMatrix; /**< [in]: Specifies the matrix coefficients used in deriving the luma and chroma from the RGB primaries (as defined in Annex E of the ITU-T Specification). */
uint32_t chromaSampleLocationFlag; /**< [in]: if set to 1 , it specifies that the chromaSampleLocationTop and chromaSampleLocationBot are present.*/
uint32_t chromaSampleLocationTop; /**< [in]: Specifies the chroma sample location for top field(as defined in Annex E of the ITU-T Specification) */
uint32_t chromaSampleLocationBot; /**< [in]: Specifies the chroma sample location for bottom field(as defined in Annex E of the ITU-T Specification) */
uint32_t bitstreamRestrictionFlag; /**< [in]: if set to 1, it specifies the bitstream restriction parameters are present in the bitstream.*/
uint32_t reserved[15];
}NV_ENC_CONFIG_H264_VUI_PARAMETERS;
typedef NV_ENC_CONFIG_H264_VUI_PARAMETERS NV_ENC_CONFIG_HEVC_VUI_PARAMETERS;
/**
* \struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE
* External motion vector hint counts per block type.
*/
typedef struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE
{
uint32_t numCandsPerBlk16x16 : 4; /**< [in]: Specifies the number of candidates per 16x16 block. */
uint32_t numCandsPerBlk16x8 : 4; /**< [in]: Specifies the number of candidates per 16x8 block. */
uint32_t numCandsPerBlk8x16 : 4; /**< [in]: Specifies the number of candidates per 8x16 block. */
uint32_t numCandsPerBlk8x8 : 4; /**< [in]: Specifies the number of candidates per 8x8 block. */
uint32_t reserved : 16; /**< [in]: Reserved for padding. */
uint32_t reserved1[3]; /**< [in]: Reserved for future use. */
} NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE;
/**
* \struct _NVENC_EXTERNAL_ME_HINT
* External Motion Vector hint structure.
*/
typedef struct _NVENC_EXTERNAL_ME_HINT
{
int32_t mvx : 12; /**< [in]: Specifies the x component of integer pixel MV (relative to current MB) S12.0. */
int32_t mvy : 10; /**< [in]: Specifies the y component of integer pixel MV (relative to current MB) S10.0 .*/
int32_t refidx : 5; /**< [in]: Specifies the reference index (31=invalid). Current we support only 1 reference frame per direction for external hints, so \p refidx must be 0. */
int32_t dir : 1; /**< [in]: Specifies the direction of motion estimation . 0=L0 1=L1.*/
int32_t partType : 2; /**< [in]: Specifies the block partition type.0=16x16 1=16x8 2=8x16 3=8x8 (blocks in partition must be consecutive).*/
int32_t lastofPart : 1; /**< [in]: Set to 1 for the last MV of (sub) partition */
int32_t lastOfMB : 1; /**< [in]: Set to 1 for the last MV of macroblock. */
} NVENC_EXTERNAL_ME_HINT;
/**
* \struct _NV_ENC_CONFIG_H264
* H264 encoder configuration parameters
*/
typedef struct _NV_ENC_CONFIG_H264
{
uint32_t enableTemporalSVC :1; /**< [in]: Set to 1 to enable SVC temporal*/
uint32_t enableStereoMVC :1; /**< [in]: Set to 1 to enable stereo MVC*/
uint32_t hierarchicalPFrames :1; /**< [in]: Set to 1 to enable hierarchical PFrames */
uint32_t hierarchicalBFrames :1; /**< [in]: Set to 1 to enable hierarchical BFrames */
uint32_t outputBufferingPeriodSEI :1; /**< [in]: Set to 1 to write SEI buffering period syntax in the bitstream */
uint32_t outputPictureTimingSEI :1; /**< [in]: Set to 1 to write SEI picture timing syntax in the bitstream */
uint32_t outputAUD :1; /**< [in]: Set to 1 to write access unit delimiter syntax in bitstream */
uint32_t disableSPSPPS :1; /**< [in]: Set to 1 to disable writing of Sequence and Picture parameter info in bitstream */
uint32_t outputFramePackingSEI :1; /**< [in]: Set to 1 to enable writing of frame packing arrangement SEI messages to bitstream */
uint32_t outputRecoveryPointSEI :1; /**< [in]: Set to 1 to enable writing of recovery point SEI message */
uint32_t enableIntraRefresh :1; /**< [in]: Set to 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */
uint32_t enableConstrainedEncoding :1; /**< [in]: Set this to 1 to enable constrainedFrame encoding where each slice in the constarined picture is independent of other slices
Check support for constrained encoding using ::NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING caps. */
uint32_t repeatSPSPPS :1; /**< [in]: Set to 1 to enable writing of Sequence and Picture parameter for every IDR frame */
uint32_t enableVFR :1; /**< [in]: Set to 1 to enable variable frame rate. */
uint32_t enableLTR :1; /**< [in]: Currently this feature is not available and must be set to 0. Set to 1 to enable LTR support and auto-mark the first */
uint32_t qpPrimeYZeroTransformBypassFlag :1; /**< [in]: To enable lossless encode set this to 1, set QP to 0 and RC_mode to NV_ENC_PARAMS_RC_CONSTQP and profile to HIGH_444_PREDICTIVE_PROFILE.
Check support for lossless encoding using ::NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE caps. */
uint32_t useConstrainedIntraPred :1; /**< [in]: Set 1 to enable constrained intra prediction. */
uint32_t reservedBitFields :15; /**< [in]: Reserved bitfields and must be set to 0 */
uint32_t level; /**< [in]: Specifies the encoding level. Client is recommended to set this to NV_ENC_LEVEL_AUTOSELECT in order to enable the NvEncodeAPI interface to select the correct level. */
uint32_t idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */
uint32_t separateColourPlaneFlag; /**< [in]: Set to 1 to enable 4:4:4 separate colour planes */
uint32_t disableDeblockingFilterIDC; /**< [in]: Specifies the deblocking filter mode. Permissible value range: [0,2] */
uint32_t numTemporalLayers; /**< [in]: Specifies max temporal layers to be used for hierarchical coding. Valid value range is [1,::NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS] */
uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header. Currently reserved and must be set to 0. */
uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header. Currently reserved and must be set to 0. */
NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE adaptiveTransformMode; /**< [in]: Specifies the AdaptiveTransform Mode. Check support for AdaptiveTransform mode using ::NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM caps. */
NV_ENC_H264_FMO_MODE fmoMode; /**< [in]: Specified the FMO Mode. Check support for FMO using ::NV_ENC_CAPS_SUPPORT_FMO caps. */
NV_ENC_H264_BDIRECT_MODE bdirectMode; /**< [in]: Specifies the BDirect mode. Check support for BDirect mode using ::NV_ENC_CAPS_SUPPORT_BDIRECT_MODE caps.*/
NV_ENC_H264_ENTROPY_CODING_MODE entropyCodingMode; /**< [in]: Specifies the entropy coding mode. Check support for CABAC mode using ::NV_ENC_CAPS_SUPPORT_CABAC caps. */
NV_ENC_STEREO_PACKING_MODE stereoMode; /**< [in]: Specifies the stereo frame packing mode which is to be signalled in frame packing arrangement SEI */
uint32_t intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set.
Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */
uint32_t intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */
uint32_t maxNumRefFrames; /**< [in]: Specifies the DPB size used for encoding. Setting it to 0 will let driver use the default dpb size.
The low latency application which wants to invalidate reference frame as an error resilience tool
is recommended to use a large DPB size so that the encoder can keep old reference frames which can be used if recent
frames are invalidated. */
uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture
When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting
When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */
uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For:
sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice)
sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice)
sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice)
sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */
NV_ENC_CONFIG_H264_VUI_PARAMETERS h264VUIParameters; /**< [in]: Specifies the H264 video usability info pamameters */
uint32_t ltrNumFrames; /**< [in]: Specifies the number of LTR frames used. Additionally, encoder will mark the first numLTRFrames base layer reference frames within each IDR interval as LTR */
uint32_t ltrTrustMode; /**< [in]: Specifies the LTR operating mode. Set to 0 to disallow encoding using LTR frames until later specified. Set to 1 to allow encoding using LTR frames unless later invalidated.*/
uint32_t chromaFormatIDC; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input.
Check support for YUV444 encoding using ::NV_ENC_CAPS_SUPPORT_YUV444_ENCODE caps.*/
uint32_t maxTemporalLayers; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */
uint32_t reserved1[270]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_CONFIG_H264;
/**
* \struct _NV_ENC_CONFIG_HEVC
* HEVC encoder configuration parameters to be set during initialization.
*/
typedef struct _NV_ENC_CONFIG_HEVC
{
uint32_t level; /**< [in]: Specifies the level of the encoded bitstream.*/
uint32_t tier; /**< [in]: Specifies the level tier of the encoded bitstream.*/
NV_ENC_HEVC_CUSIZE minCUSize; /**< [in]: Specifies the minimum size of luma coding unit.*/
NV_ENC_HEVC_CUSIZE maxCUSize; /**< [in]: Specifies the maximum size of luma coding unit. Currently NVENC SDK only supports maxCUSize equal to NV_ENC_HEVC_CUSIZE_32x32.*/
uint32_t useConstrainedIntraPred :1; /**< [in]: Set 1 to enable constrained intra prediction. */
uint32_t disableDeblockAcrossSliceBoundary :1; /**< [in]: Set 1 to disable in loop filtering across slice boundary.*/
uint32_t outputBufferingPeriodSEI :1; /**< [in]: Set 1 to write SEI buffering period syntax in the bitstream */
uint32_t outputPictureTimingSEI :1; /**< [in]: Set 1 to write SEI picture timing syntax in the bitstream */
uint32_t outputAUD :1; /**< [in]: Set 1 to write Access Unit Delimiter syntax. */
uint32_t enableLTR :1; /**< [in]: Set 1 to enable use of long term reference pictures for inter prediction. */
uint32_t disableSPSPPS :1; /**< [in]: Set 1 to disable VPS,SPS and PPS signalling in the bitstream. */
uint32_t repeatSPSPPS :1; /**< [in]: Set 1 to output VPS,SPS and PPS for every IDR frame.*/
uint32_t enableIntraRefresh :1; /**< [in]: Set 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */
uint32_t chromaFormatIDC :2; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input.*/
uint32_t reserved :21; /**< [in]: Reserved bitfields.*/
uint32_t idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */
uint32_t intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set.
Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */
uint32_t intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */
uint32_t maxNumRefFramesInDPB; /**< [in]: Specifies the maximum number of references frames in the DPB.*/
uint32_t ltrNumFrames; /**< [in]: Specifies the maximum number of long term references can be used for prediction */
uint32_t vpsId; /**< [in]: Specifies the VPS id of the video parameter set. Currently reserved and must be set to 0. */
uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header. Currently reserved and must be set to 0. */
uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header. Currently reserved and must be set to 0. */
uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture
When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */
uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For:
sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice)
sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice)
sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice)
sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */
uint32_t maxTemporalLayersMinus1; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */
NV_ENC_CONFIG_HEVC_VUI_PARAMETERS hevcVUIParameters; /**< [in]: Specifies the HEVC video usability info pamameters */
uint32_t reserved1[218]; /**< [in]: Reserved and must be set to 0.*/
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_CONFIG_HEVC;
/**
* \struct _NV_ENC_CODEC_CONFIG
* Codec-specific encoder configuration parameters to be set during initialization.
*/
typedef union _NV_ENC_CODEC_CONFIG
{
NV_ENC_CONFIG_H264 h264Config; /**< [in]: Specifies the H.264-specific encoder configuration. */
NV_ENC_CONFIG_HEVC hevcConfig; /**< [in]: Specifies the HEVC-specific encoder configuration. */
uint32_t reserved[256]; /**< [in]: Reserved and must be set to 0 */
} NV_ENC_CODEC_CONFIG;
/**
* \struct _NV_ENC_CONFIG
* Encoder configuration parameters to be set during initialization.
*/
typedef struct _NV_ENC_CONFIG
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CONFIG_VER. */
GUID profileGUID; /**< [in]: Specifies the codec profile guid. If client specifies \p NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID the NvEncodeAPI interface will select the appropriate codec profile. */
uint32_t gopLength; /**< [in]: Specifies the number of pictures in one GOP. Low latency application client can set goplength to NVENC_INFINITE_GOPLENGTH so that keyframes are not inserted automatically. */
int32_t frameIntervalP; /**< [in]: Specifies the GOP pattern as follows: \p frameIntervalP = 0: I, 1: IPP, 2: IBP, 3: IBBP If goplength is set to NVENC_INFINITE_GOPLENGTH \p frameIntervalP should be set to 1. */
uint32_t monoChromeEncoding; /**< [in]: Set this to 1 to enable monochrome encoding for this session. */
NV_ENC_PARAMS_FRAME_FIELD_MODE frameFieldMode; /**< [in]: Specifies the frame/field mode. Check support for field encoding using ::NV_ENC_CAPS_SUPPORT_FIELD_ENCODING caps. */
NV_ENC_MV_PRECISION mvPrecision; /**< [in]: Specifies the desired motion vector prediction precision. */
NV_ENC_RC_PARAMS rcParams; /**< [in]: Specifies the rate control parameters for the current encoding session. */
NV_ENC_CODEC_CONFIG encodeCodecConfig; /**< [in]: Specifies the codec specific config parameters through this union. */
uint32_t reserved [278]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_CONFIG;
/** macro for constructing the version field of ::_NV_ENC_CONFIG */
#define NV_ENC_CONFIG_VER (NVENCAPI_STRUCT_VERSION(6) | ( 1<<31 ))
/**
* \struct _NV_ENC_INITIALIZE_PARAMS
* Encode Session Initialization parameters.
*/
typedef struct _NV_ENC_INITIALIZE_PARAMS
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */
GUID encodeGUID; /**< [in]: Specifies the Encode GUID for which the encoder is being created. ::NvEncInitializeEncoder() API will fail if this is not set, or set to unsupported value. */
GUID presetGUID; /**< [in]: Specifies the preset for encoding. If the preset GUID is set then , the preset configuration will be applied before any other parameter. */
uint32_t encodeWidth; /**< [in]: Specifies the encode width. If not set ::NvEncInitializeEncoder() API will fail. */
uint32_t encodeHeight; /**< [in]: Specifies the encode height. If not set ::NvEncInitializeEncoder() API will fail. */
uint32_t darWidth; /**< [in]: Specifies the display aspect ratio Width. */
uint32_t darHeight; /**< [in]: Specifies the display aspect ratio height. */
uint32_t frameRateNum; /**< [in]: Specifies the numerator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */
uint32_t frameRateDen; /**< [in]: Specifies the denominator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */
uint32_t enableEncodeAsync; /**< [in]: Set this to 1 to enable asynchronous mode and is expected to use events to get picture completion notification. */
uint32_t enablePTD; /**< [in]: Set this to 1 to enable the Picture Type Decision is be taken by the NvEncodeAPI interface. */
uint32_t reportSliceOffsets :1; /**< [in]: Set this to 1 to enable reporting slice offsets in ::_NV_ENC_LOCK_BITSTREAM. NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync must be set to 0 to use this feature. Client must set this to 0 if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs */
uint32_t enableSubFrameWrite :1; /**< [in]: Set this to 1 to write out available bitstream to memory at subframe intervals */
uint32_t enableExternalMEHints :1; /**< [in]: Set to 1 to enable external ME hints for the current frame. Currently this feature is supported only if NV_ENC_INITIALIZE_PARAMS::enablePTD to 0 or\p frameIntervalP = 1 (i.e no B frames).*/
uint32_t enableMEOnlyMode :1; /**< [in]: Set to 1 to enable ME Only Mode .*/
uint32_t reservedBitFields :28; /**< [in]: Reserved bitfields and must be set to 0 */
uint32_t privDataSize; /**< [in]: Reserved private data buffer size and must be set to 0 */
void* privData; /**< [in]: Reserved private data buffer and must be set to NULL */
NV_ENC_CONFIG* encodeConfig; /**< [in]: Specifies the advanced codec specific structure. If client has sent a valid codec config structure, it will override parameters set by the NV_ENC_INITIALIZE_PARAMS::presetGUID parameter. If set to NULL the NvEncodeAPI interface will use the NV_ENC_INITIALIZE_PARAMS::presetGUID to set the codec specific parameters.
Client can also optionally query the NvEncodeAPI interface to get codec specific parameters for a presetGUID using ::NvEncGetEncodePresetConfig() API. It can then modify (if required) some of the codec config parameters and send down a custom config structure as part of ::_NV_ENC_INITIALIZE_PARAMS.
Even in this case client is recommended to pass the same preset guid it has used in ::NvEncGetEncodePresetConfig() API to query the config structure; as NV_ENC_INITIALIZE_PARAMS::presetGUID. This will not override the custom config structure but will be used to determine other Encoder HW specific parameters not exposed in the API. */
uint32_t maxEncodeWidth; /**< [in]: Maximum encode width to be used for current Encode session.
Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encoder will not allow dynamic resolution change. */
uint32_t maxEncodeHeight; /**< [in]: Maximum encode height to be allowed for current Encode session.
Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encode will not allow dynamic resolution change. */
NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE maxMEHintCountsPerBlock[2]; /**< [in]: If Client wants to pass external motion vectors in NV_ENC_PIC_PARAMS::meExternalHints buffer it must specify the maximum number of hint candidates per block per direction for the encode session.
The NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[0] is for L0 predictors and NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[1] is for L1 predictors.
This client must also set NV_ENC_INITIALIZE_PARAMS::enableExternalMEHints to 1. */
uint32_t reserved [289]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_INITIALIZE_PARAMS;
/** macro for constructing the version field of ::_NV_ENC_INITIALIZE_PARAMS */
#define NV_ENC_INITIALIZE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(5) | ( 1<<31 ))
/**
* \struct _NV_ENC_RECONFIGURE_PARAMS
* Encode Session Reconfigured parameters.
*/
typedef struct _NV_ENC_RECONFIGURE_PARAMS
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_RECONFIGURE_PARAMS_VER. */
NV_ENC_INITIALIZE_PARAMS reInitEncodeParams; /**< [in]: Encoder session re-initialization parameters. */
uint32_t resetEncoder :1; /**< [in]: This resets the rate control states and other internal encoder states. This should be used only with an IDR frame.
If NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1, encoder will force the frame type to IDR */
uint32_t forceIDR :1; /**< [in]: Encode the current picture as an IDR picture. This flag is only valid when Picture type decision is taken by the Encoder
[_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */
uint32_t reserved :30;
}NV_ENC_RECONFIGURE_PARAMS;
/** macro for constructing the version field of ::_NV_ENC_RECONFIGURE_PARAMS */
#define NV_ENC_RECONFIGURE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(1) | ( 1<<31 ))
/**
* \struct _NV_ENC_PRESET_CONFIG
* Encoder preset config
*/
typedef struct _NV_ENC_PRESET_CONFIG
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PRESET_CONFIG_VER. */
NV_ENC_CONFIG presetCfg; /**< [out]: preset config returned by the Nvidia Video Encoder interface. */
uint32_t reserved1[255]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
}NV_ENC_PRESET_CONFIG;
/** macro for constructing the version field of ::_NV_ENC_PRESET_CONFIG */
#define NV_ENC_PRESET_CONFIG_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 ))
/**
* \struct _NV_ENC_SEI_PAYLOAD
* User SEI message
*/
typedef struct _NV_ENC_SEI_PAYLOAD
{
uint32_t payloadSize; /**< [in] SEI payload size in bytes. SEI payload must be byte aligned, as described in Annex D */
uint32_t payloadType; /**< [in] SEI payload types and syntax can be found in Annex D of the H.264 Specification. */
uint8_t *payload; /**< [in] pointer to user data */
} NV_ENC_SEI_PAYLOAD;
#define NV_ENC_H264_SEI_PAYLOAD NV_ENC_SEI_PAYLOAD
/**
* \struct _NV_ENC_PIC_PARAMS_H264
* H264 specific enc pic params. sent on a per frame basis.
*/
typedef struct _NV_ENC_PIC_PARAMS_H264
{
uint32_t displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */
uint32_t reserved3; /**< [in]: Reserved and must be set to 0 */
uint32_t refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */
uint32_t colourPlaneId; /**< [in]: Specifies the colour plane ID associated with the current input. */
uint32_t forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt.
When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message
forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */
uint32_t constrainedFrame :1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame.
NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */
uint32_t sliceModeDataUpdate :1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter
When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */
uint32_t ltrMarkFrame :1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */
uint32_t ltrUseFrames :1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */
uint32_t reservedBitFields :28; /**< [in]: Reserved bit fields and must be set to 0 */
uint8_t* sliceTypeData; /**< [in]: Deprecated. */
uint32_t sliceTypeArrayCnt; /**< [in]: Deprecated. */
uint32_t seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */
NV_ENC_SEI_PAYLOAD* seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */
uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture
When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting
When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */
uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For:
sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice)
sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice)
sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice)
sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */
uint32_t ltrMarkFrameIdx; /**< [in]: Specifies the long term referenceframe index to use for marking this frame as LTR.*/
uint32_t ltrUseFrameBitmap; /**< [in]: Specifies the the associated bitmap of LTR frame indices when encoding this frame. */
uint32_t ltrUsageMode; /**< [in]: Specifies additional usage constraints for encoding using LTR frames from this point further. 0: no constraints, 1: no short term refs older than current, no previous LTR frames.*/
uint32_t reserved [243]; /**< [in]: Reserved and must be set to 0. */
void* reserved2[62]; /**< [in]: Reserved and must be set to NULL. */
} NV_ENC_PIC_PARAMS_H264;
/**
* \struct _NV_ENC_PIC_PARAMS_HEVC
* HEVC specific enc pic params. sent on a per frame basis.
*/
typedef struct _NV_ENC_PIC_PARAMS_HEVC
{
uint32_t displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */
uint32_t refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */
uint32_t temporalId; /**< [in]: Specifies the temporal id of the picture */
uint32_t forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt.
When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message
forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */
uint32_t constrainedFrame :1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame.
NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */
uint32_t sliceModeDataUpdate :1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter
When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */
uint32_t ltrMarkFrame :1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */
uint32_t ltrUseFrames :1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */
uint32_t reservedBitFields :28; /**< [in]: Reserved bit fields and must be set to 0 */
uint8_t* sliceTypeData; /**< [in]: Array which specifies the slice type used to force intra slice for a particular slice. Currently supported only for NV_ENC_CONFIG_H264::sliceMode == 3.
Client should allocate array of size sliceModeData where sliceModeData is specified in field of ::_NV_ENC_CONFIG_H264
Array element with index n corresponds to nth slice. To force a particular slice to intra client should set corresponding array element to NV_ENC_SLICE_TYPE_I
all other array elements should be set to NV_ENC_SLICE_TYPE_DEFAULT */
uint32_t sliceTypeArrayCnt; /**< [in]: Client should set this to the number of elements allocated in sliceTypeData array. If sliceTypeData is NULL then this should be set to 0 */
uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture
When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting
When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */
uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For:
sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice)
sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice)
sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice)
sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */
uint32_t ltrMarkFrameIdx; /**< [in]: Specifies the long term reference frame index to use for marking this frame as LTR.*/
uint32_t ltrUseFrameBitmap; /**< [in]: Specifies the associated bitmap of LTR frame indices when encoding this frame. */
uint32_t ltrUsageMode; /**< [in]: Specifies additional usage constraints for encoding using LTR frames from this point further. 0: no constraints, 1: no short term refs older than current, no previous LTR frames.*/
uint32_t seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */
uint32_t reserved; /**< [in]: Reserved and must be set to 0. */
NV_ENC_SEI_PAYLOAD* seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */
uint32_t reserved2 [244]; /**< [in]: Reserved and must be set to 0. */
void* reserved3[61]; /**< [in]: Reserved and must be set to NULL. */
} NV_ENC_PIC_PARAMS_HEVC;
/**
* Codec specific per-picture encoding parameters.
*/
typedef union _NV_ENC_CODEC_PIC_PARAMS
{
NV_ENC_PIC_PARAMS_H264 h264PicParams; /**< [in]: H264 encode picture params. */
NV_ENC_PIC_PARAMS_HEVC hevcPicParams; /**< [in]: HEVC encode picture params. Currently unsupported and must not to be used. */
uint32_t reserved[256]; /**< [in]: Reserved and must be set to 0. */
} NV_ENC_CODEC_PIC_PARAMS;
/**
* \struct _NV_ENC_PIC_PARAMS
* Encoding parameters that need to be sent on a per frame basis.
*/
typedef struct _NV_ENC_PIC_PARAMS
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PIC_PARAMS_VER. */
uint32_t inputWidth; /**< [in]: Specifies the input buffer width */
uint32_t inputHeight; /**< [in]: Specifies the input buffer height */
uint32_t inputPitch; /**< [in]: Specifies the input buffer pitch. If pitch value is not known, set this to inputWidth. */
uint32_t encodePicFlags; /**< [in]: Specifies bit-wise OR`ed encode pic flags. See ::NV_ENC_PIC_FLAGS enum. */
uint32_t frameIdx; /**< [in]: Specifies the frame index associated with the input frame [optional]. */
uint64_t inputTimeStamp; /**< [in]: Specifies presentation timestamp associated with the input picture. */
uint64_t inputDuration; /**< [in]: Specifies duration of the input picture */
NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource() APIs.*/
NV_ENC_OUTPUT_PTR outputBitstream; /**< [in]: Specifies the pointer to output buffer. Client should use a pointer obtained from ::NvEncCreateBitstreamBuffer() API. */
void* completionEvent; /**< [in]: Specifies an event to be signalled on completion of encoding of this Frame [only if operating in Asynchronous mode]. Each output buffer should be associated with a distinct event pointer. */
NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Specifies the input buffer format. */
NV_ENC_PIC_STRUCT pictureStruct; /**< [in]: Specifies structure of the input picture. */
NV_ENC_PIC_TYPE pictureType; /**< [in]: Specifies input picture type. Client required to be set explicitly by the client if the client has not set NV_ENC_INITALIZE_PARAMS::enablePTD to 1 while calling NvInitializeEncoder. */
NV_ENC_CODEC_PIC_PARAMS codecPicParams; /**< [in]: Specifies the codec specific per-picture encoding parameters. */
NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE meHintCountsPerBlock[2]; /**< [in]: Specifies the number of hint candidates per block per direction for the current frame. meHintCountsPerBlock[0] is for L0 predictors and meHintCountsPerBlock[1] is for L1 predictors.
The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder intialization. */
NVENC_EXTERNAL_ME_HINT *meExternalHints; /**< [in]: Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks multiplied by the total number of candidates per macroblock.
The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8
+ 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */
uint32_t reserved1[6]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[2]; /**< [in]: Reserved and must be set to NULL */
int8_t *qpDeltaMap; /**< [in]: Specifies the pointer to signed byte array containing QP delta value per MB in raster scan order in the current picture. This QP modifier is applied on top of the QP chosen by rate control. */
uint32_t qpDeltaMapSize; /**< [in]: Specifies the size in bytes of qpDeltaMap surface allocated by client and pointed to by NV_ENC_PIC_PARAMS::qpDeltaMap. Surface (array) should be picWidthInMbs * picHeightInMbs */
uint32_t reservedBitFields; /**< [in]: Reserved bitfields and must be set to 0 */
uint32_t reserved3[287]; /**< [in]: Reserved and must be set to 0 */
void* reserved4[60]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_PIC_PARAMS;
/** Macro for constructing the version field of ::_NV_ENC_PIC_PARAMS */
#define NV_ENC_PIC_PARAMS_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 ))
/**
* \struct _NV_ENC_MEONLY_PARAMS
* MEOnly parameters that need to be sent on a per motion estimation basis.
*/
typedef struct _NV_ENC_MEONLY_PARAMS
{
uint32_t version; /**< [in]: Struct version. Must be set to NV_ENC_MEONLY_PARAMS_VER.*/
uint32_t inputWidth; /**< [in]: Specifies the input buffer width */
uint32_t inputHeight; /**< [in]: Specifies the input buffer height */
NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from NvEncCreateInputBuffer() or NvEncMapInputResource() APIs. */
NV_ENC_INPUT_PTR referenceFrame; /**< [in]: Specifies the reference frame pointer */
NV_ENC_OUTPUT_PTR outputMV; /**< [in,out]: Specifies the pointer to output motion vector data buffer allocated by NvEncCreateMVBuffer. */
NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Specifies the input buffer format. */
uint32_t reserved1[252]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[61]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_MEONLY_PARAMS;
/** NV_ENC_MEONLY_PARAMS struct version*/
#define NV_ENC_MEONLY_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
/**
* \struct _NV_ENC_LOCK_BITSTREAM
* Bitstream buffer lock parameters.
*/
typedef struct _NV_ENC_LOCK_BITSTREAM
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_BITSTREAM_VER. */
uint32_t doNotWait :1; /**< [in]: If this flag is set, the NvEncodeAPI interface will return buffer pointer even if operation is not completed. If not set, the call will block until operation completes. */
uint32_t ltrFrame :1; /**< [out]: Flag indicating this frame is marked as LTR frame */
uint32_t reservedBitFields :30; /**< [in]: Reserved bit fields and must be set to 0 */
void* outputBitstream; /**< [in]: Pointer to the bitstream buffer being locked. */
uint32_t* sliceOffsets; /**< [in,out]: Array which receives the slice offsets. This is not supported if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs. Array size must be equal to size of frame in MBs. */
uint32_t frameIdx; /**< [out]: Frame no. for which the bitstream is being retrieved. */
uint32_t hwEncodeStatus; /**< [out]: The NvEncodeAPI interface status for the locked picture. */
uint32_t numSlices; /**< [out]: Number of slices in the encoded picture. Will be reported only if NV_ENC_INITIALIZE_PARAMS::reportSliceOffsets set to 1. */
uint32_t bitstreamSizeInBytes; /**< [out]: Actual number of bytes generated and copied to the memory pointed by bitstreamBufferPtr. */
uint64_t outputTimeStamp; /**< [out]: Presentation timestamp associated with the encoded output. */
uint64_t outputDuration; /**< [out]: Presentation duration associates with the encoded output. */
void* bitstreamBufferPtr; /**< [out]: Pointer to the generated output bitstream. */
NV_ENC_PIC_TYPE pictureType; /**< [out]: Picture type of the encoded picture. */
NV_ENC_PIC_STRUCT pictureStruct; /**< [out]: Structure of the generated output picture. */
uint32_t frameAvgQP; /**< [out]: Average QP of the frame. */
uint32_t frameSatd; /**< [out]: Total SATD cost for whole frame. */
uint32_t ltrFrameIdx; /**< [out]: Frame index associated with this LTR frame. */
uint32_t ltrFrameBitmap; /**< [out]: Bitmap of LTR frames indices which were used for encoding this frame. Value of 0 if no LTR frames were used. */
uint32_t reserved [236]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_LOCK_BITSTREAM;
/** Macro for constructing the version field of ::_NV_ENC_LOCK_BITSTREAM */
#define NV_ENC_LOCK_BITSTREAM_VER NVENCAPI_STRUCT_VERSION(1)
/**
* \struct _NV_ENC_LOCK_INPUT_BUFFER
* Uncompressed Input Buffer lock parameters.
*/
typedef struct _NV_ENC_LOCK_INPUT_BUFFER
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_INPUT_BUFFER_VER. */
uint32_t doNotWait :1; /**< [in]: Set to 1 to make ::NvEncLockInputBuffer() a unblocking call. If the encoding is not completed, driver will return ::NV_ENC_ERR_ENCODER_BUSY error code. */
uint32_t reservedBitFields :31; /**< [in]: Reserved bitfields and must be set to 0 */
NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Pointer to the input buffer to be locked, client should pass the pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource API. */
void* bufferDataPtr; /**< [out]: Pointed to the locked input buffer data. Client can only access input buffer using the \p bufferDataPtr. */
uint32_t pitch; /**< [out]: Pitch of the locked input buffer. */
uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_LOCK_INPUT_BUFFER;
/** Macro for constructing the version field of ::_NV_ENC_LOCK_INPUT_BUFFER */
#define NV_ENC_LOCK_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
/**
* \struct _NV_ENC_MAP_INPUT_RESOURCE
* Map an input resource to a Nvidia Encoder Input Buffer
*/
typedef struct _NV_ENC_MAP_INPUT_RESOURCE
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_MAP_INPUT_RESOURCE_VER. */
uint32_t subResourceIndex; /**< [in]: Deprecated. Do not use. */
void* inputResource; /**< [in]: Deprecated. Do not use. */
NV_ENC_REGISTERED_PTR registeredResource; /**< [in]: The Registered resource handle obtained by calling NvEncRegisterInputResource. */
NV_ENC_INPUT_PTR mappedResource; /**< [out]: Mapped pointer corresponding to the registeredResource. This pointer must be used in NV_ENC_PIC_PARAMS::inputBuffer parameter in ::NvEncEncodePicture() API. */
NV_ENC_BUFFER_FORMAT mappedBufferFmt; /**< [out]: Buffer format of the outputResource. This buffer format must be used in NV_ENC_PIC_PARAMS::bufferFmt if client using the above mapped resource pointer. */
uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0. */
void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_MAP_INPUT_RESOURCE;
/** Macro for constructing the version field of ::_NV_ENC_MAP_INPUT_RESOURCE */
#define NV_ENC_MAP_INPUT_RESOURCE_VER NVENCAPI_STRUCT_VERSION(4)
/**
* \struct _NV_ENC_REGISTER_RESOURCE
* Register a resource for future use with the Nvidia Video Encoder Interface.
*/
typedef struct _NV_ENC_REGISTER_RESOURCE
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_REGISTER_RESOURCE_VER. */
NV_ENC_INPUT_RESOURCE_TYPE resourceType; /**< [in]: Specifies the type of resource to be registered. Supported values are ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR. */
uint32_t width; /**< [in]: Input buffer Width. */
uint32_t height; /**< [in]: Input buffer Height. */
uint32_t pitch; /**< [in]: Input buffer Pitch. */
uint32_t subResourceIndex; /**< [in]: Subresource Index of the DirectX resource to be registered. Should be set to 0 for other interfaces. */
void* resourceToRegister; /**< [in]: Handle to the resource that is being registered. */
NV_ENC_REGISTERED_PTR registeredResource; /**< [out]: Registered resource handle. This should be used in future interactions with the Nvidia Video Encoder Interface. */
NV_ENC_BUFFER_FORMAT bufferFormat; /**< [in]: Buffer format of resource to be registered. */
uint32_t reserved1[248]; /**< [in]: Reserved and must be set to 0. */
void* reserved2[62]; /**< [in]: Reserved and must be set to NULL. */
} NV_ENC_REGISTER_RESOURCE;
/** Macro for constructing the version field of ::_NV_ENC_REGISTER_RESOURCE */
#define NV_ENC_REGISTER_RESOURCE_VER NVENCAPI_STRUCT_VERSION(3)
/**
* \struct _NV_ENC_STAT
* Encode Stats structure.
*/
typedef struct _NV_ENC_STAT
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_STAT_VER. */
uint32_t reserved; /**< [in]: Reserved and must be set to 0 */
NV_ENC_OUTPUT_PTR outputBitStream; /**< [out]: Specifies the pointer to output bitstream. */
uint32_t bitStreamSize; /**< [out]: Size of generated bitstream in bytes. */
uint32_t picType; /**< [out]: Picture type of encoded picture. See ::NV_ENC_PIC_TYPE. */
uint32_t lastValidByteOffset; /**< [out]: Offset of last valid bytes of completed bitstream */
uint32_t sliceOffsets[16]; /**< [out]: Offsets of each slice */
uint32_t picIdx; /**< [out]: Picture number */
uint32_t reserved1[233]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_STAT;
/** Macro for constructing the version field of ::_NV_ENC_STAT */
#define NV_ENC_STAT_VER NVENCAPI_STRUCT_VERSION(1)
/**
* \struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD
* Sequence and picture paramaters payload.
*/
typedef struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */
uint32_t inBufferSize; /**< [in]: Specifies the size of the spsppsBuffer provied by the client */
uint32_t spsId; /**< [in]: Specifies the SPS id to be used in sequence header. Default value is 0. */
uint32_t ppsId; /**< [in]: Specifies the PPS id to be used in picture header. Default value is 0. */
void* spsppsBuffer; /**< [in]: Specifies bitstream header pointer of size NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. It is the client's responsibility to manage this memory. */
uint32_t* outSPSPPSPayloadSize; /**< [out]: Size of the sequence and picture header in bytes written by the NvEncodeAPI interface to the SPSPPSBuffer. */
uint32_t reserved [250]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_SEQUENCE_PARAM_PAYLOAD;
/** Macro for constructing the version field of ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD */
#define NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER NVENCAPI_STRUCT_VERSION(1)
/**
* Event registration/unregistration parameters.
*/
typedef struct _NV_ENC_EVENT_PARAMS
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_EVENT_PARAMS_VER. */
uint32_t reserved; /**< [in]: Reserved and must be set to 0 */
void* completionEvent; /**< [in]: Handle to event to be registered/unregistered with the NvEncodeAPI interface. */
uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_EVENT_PARAMS;
/** Macro for constructing the version field of ::_NV_ENC_EVENT_PARAMS */
#define NV_ENC_EVENT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
/**
* Encoder Session Creation parameters
*/
typedef struct _NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS
{
uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER. */
NV_ENC_DEVICE_TYPE deviceType; /**< [in]: Specified the device Type */
void* device; /**< [in]: Pointer to client device. */
void* reserved; /**< [in]: Reserved and must be set to 0. */
uint32_t apiVersion; /**< [in]: API version. Should be set to NVENCAPI_VERSION. */
uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */
void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */
} NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS;
/** Macro for constructing the version field of ::_NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS */
#define NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
/** @} */ /* END ENCODER_STRUCTURE */
/**
* \addtogroup ENCODE_FUNC NvEncodeAPI Functions
* @{
*/
// NvEncOpenEncodeSession
/**
* \brief Opens an encoding session.
*
* Deprecated.
*
* \return
* ::NV_ENC_ERR_INVALID_CALL\n
*
*/
NVENCSTATUS NVENCAPI NvEncOpenEncodeSession (void* device, uint32_t deviceType, void** encoder);
// NvEncGetEncodeGuidCount
/**
* \brief Retrieves the number of supported encode GUIDs.
*
* The function returns the number of codec guids supported by the NvEncodeAPI
* interface.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [out] encodeGUIDCount
* Number of supported encode GUIDs.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDCount (void* encoder, uint32_t* encodeGUIDCount);
// NvEncGetEncodeGUIDs
/**
* \brief Retrieves an array of supported encoder codec GUIDs.
*
* The function returns an array of codec guids supported by the NvEncodeAPI interface.
* The client must allocate an array where the NvEncodeAPI interface can
* fill the supported guids and pass the pointer in \p *GUIDs parameter.
* The size of the array can be determined by using ::NvEncGetEncodeGUIDCount() API.
* The Nvidia Encoding interface returns the number of codec guids it has actually
* filled in the guid array in the \p GUIDCount parameter.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] guidArraySize
* Number of GUIDs to retrieved. Should be set to the number retrieved using
* ::NvEncGetEncodeGUIDCount.
* \param [out] GUIDs
* Array of supported Encode GUIDs.
* \param [out] GUIDCount
* Number of supported Encode GUIDs.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDs (void* encoder, GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount);
// NvEncGetEncodeProfileGuidCount
/**
* \brief Retrieves the number of supported profile GUIDs.
*
* The function returns the number of profile GUIDs supported for a given codec.
* The client must first enumerate the codec guids supported by the NvEncodeAPI
* interface. After determining the codec guid, it can query the NvEncodeAPI
* interface to determine the number of profile guids supported for a particular
* codec guid.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* The codec guid for which the profile guids are being enumerated.
* \param [out] encodeProfileGUIDCount
* Number of encode profiles supported for the given encodeGUID.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDCount (void* encoder, GUID encodeGUID, uint32_t* encodeProfileGUIDCount);
// NvEncGetEncodeProfileGUIDs
/**
* \brief Retrieves an array of supported encode profile GUIDs.
*
* The function returns an array of supported profile guids for a particular
* codec guid. The client must allocate an array where the NvEncodeAPI interface
* can populate the profile guids. The client can determine the array size using
* ::NvEncGetEncodeProfileGUIDCount() API. The client must also validiate that the
* NvEncodeAPI interface supports the GUID the client wants to pass as \p encodeGUID
* parameter.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* The encode guid whose profile guids are being enumerated.
* \param [in] guidArraySize
* Number of GUIDs to be retrieved. Should be set to the number retrieved using
* ::NvEncGetEncodeProfileGUIDCount.
* \param [out] profileGUIDs
* Array of supported Encode Profile GUIDs
* \param [out] GUIDCount
* Number of valid encode profile GUIDs in \p profileGUIDs array.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDs (void* encoder, GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount);
// NvEncGetInputFormatCount
/**
* \brief Retrieve the number of supported Input formats.
*
* The function returns the number of supported input formats. The client must
* query the NvEncodeAPI interface to determine the supported input formats
* before creating the input surfaces.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* Encode GUID, corresponding to which the number of supported input formats
* is to be retrieved.
* \param [out] inputFmtCount
* Number of input formats supported for specified Encode GUID.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*/
NVENCSTATUS NVENCAPI NvEncGetInputFormatCount (void* encoder, GUID encodeGUID, uint32_t* inputFmtCount);
// NvEncGetInputFormats
/**
* \brief Retrieves an array of supported Input formats
*
* Returns an array of supported input formats The client must use the input
* format to create input surface using ::NvEncCreateInputBuffer() API.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* Encode GUID, corresponding to which the number of supported input formats
* is to be retrieved.
*\param [in] inputFmtArraySize
* Size input format count array passed in \p inputFmts.
*\param [out] inputFmts
* Array of input formats supported for this Encode GUID.
*\param [out] inputFmtCount
* The number of valid input format types returned by the NvEncodeAPI
* interface in \p inputFmts array.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetInputFormats (void* encoder, GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount);
// NvEncGetEncodeCaps
/**
* \brief Retrieves the capability value for a specified encoder attribute.
*
* The function returns the capability value for a given encoder attribute. The
* client must validate the encodeGUID using ::NvEncGetEncodeGUIDs() API before
* calling this function. The encoder attribute being queried are enumerated in
* ::NV_ENC_CAPS_PARAM enum.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* Encode GUID, corresponding to which the capability attribute is to be retrieved.
* \param [in] capsParam
* Used to specify attribute being queried. Refer ::NV_ENC_CAPS_PARAM for more
* details.
* \param [out] capsVal
* The value corresponding to the capability attribute being queried.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*/
NVENCSTATUS NVENCAPI NvEncGetEncodeCaps (void* encoder, GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal);
// NvEncGetEncodePresetCount
/**
* \brief Retrieves the number of supported preset GUIDs.
*
* The function returns the number of preset GUIDs available for a given codec.
* The client must validate the codec guid using ::NvEncGetEncodeGUIDs() API
* before calling this function.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* Encode GUID, corresponding to which the number of supported presets is to
* be retrieved.
* \param [out] encodePresetGUIDCount
* Receives the number of supported preset GUIDs.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodePresetCount (void* encoder, GUID encodeGUID, uint32_t* encodePresetGUIDCount);
// NvEncGetEncodePresetGUIDs
/**
* \brief Receives an array of supported encoder preset GUIDs.
*
* The function returns an array of encode preset guids available for a given codec.
* The client can directly use one of the preset guids based upon the use case
* or target device. The preset guid chosen can be directly used in
* NV_ENC_INITIALIZE_PARAMS::presetGUID parameter to ::NvEncEncodePicture() API.
* Alternately client can also use the preset guid to retrieve the encoding config
* parameters being used by NvEncodeAPI interface for that given preset, using
* ::NvEncGetEncodePresetConfig() API. It can then modify preset config parameters
* as per its use case and send it to NvEncodeAPI interface as part of
* NV_ENC_INITIALIZE_PARAMS::encodeConfig parameter for NvEncInitializeEncoder()
* API.
*
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* Encode GUID, corresponding to which the list of supported presets is to be
* retrieved.
* \param [in] guidArraySize
* Size of array of preset guids passed in \p preset GUIDs
* \param [out] presetGUIDs
* Array of supported Encode preset GUIDs from the NvEncodeAPI interface
* to client.
* \param [out] encodePresetGUIDCount
* Receives the number of preset GUIDs returned by the NvEncodeAPI
* interface.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodePresetGUIDs (void* encoder, GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount);
// NvEncGetEncodePresetConfig
/**
* \brief Returns a preset config structure supported for given preset GUID.
*
* The function returns a preset config structure for a given preset guid. Before
* using this function the client must enumerate the preset guids available for
* a given codec. The preset config structure can be modified by the client depending
* upon its use case and can be then used to initialize the encoder using
* ::NvEncInitializeEncoder() API. The client can use this function only if it
* wants to modify the NvEncodeAPI preset configuration, otherwise it can
* directly use the preset guid.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] encodeGUID
* Encode GUID, corresponding to which the list of supported presets is to be
* retrieved.
* \param [in] presetGUID
* Preset GUID, corresponding to which the Encoding configurations is to be
* retrieved.
* \param [out] presetConfig
* The requested Preset Encoder Attribute set. Refer ::_NV_ENC_CONFIG for
* more details.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodePresetConfig (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig);
// NvEncInitializeEncoder
/**
* \brief Initialize the encoder.
*
* This API must be used to initialize the encoder. The initialization parameter
* is passed using \p *createEncodeParams The client must send the following
* fields of the _NV_ENC_INITIALIZE_PARAMS structure with a valid value.
* - NV_ENC_INITIALIZE_PARAMS::encodeGUID
* - NV_ENC_INITIALIZE_PARAMS::encodeWidth
* - NV_ENC_INITIALIZE_PARAMS::encodeHeight
*
* The client can pass a preset guid directly to the NvEncodeAPI interface using
* NV_ENC_INITIALIZE_PARAMS::presetGUID field. If the client doesn't pass
* NV_ENC_INITIALIZE_PARAMS::encodeConfig structure, the codec specific parameters
* will be selected based on the preset guid. The preset guid must have been
* validated by the client using ::NvEncGetEncodePresetGUIDs() API.
* If the client passes a custom ::_NV_ENC_CONFIG structure through
* NV_ENC_INITIALIZE_PARAMS::encodeConfig , it will override the codec specific parameters
* based on the preset guid. It is recommended that even if the client passes a custom config,
* it should also send a preset guid. In this case, the preset guid passed by the client
* will not override any of the custom config parameters programmed by the client,
* it is only used as a hint by the NvEncodeAPI interface to determine certain encoder parameters
* which are not exposed to the client.
*
* There are two modes of operation for the encoder namely:
* - Asynchronous mode
* - Synchronous mode
*
* The client can select asynchronous or synchronous mode by setting the \p
* enableEncodeAsync field in ::_NV_ENC_INITIALIZE_PARAMS to 1 or 0 respectively.
*\par Asynchronous mode of operation:
* The Asynchronous mode can be enabled by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1.
* The client operating in asynchronous mode must allocate completion event object
* for each output buffer and pass the completion event object in the
* ::NvEncEncodePicture() API. The client can create another thread and wait on
* the event object to be signalled by NvEncodeAPI interface on completion of the
* encoding process for the output frame. This should unblock the main thread from
* submitting work to the encoder. When the event is signalled the client can call
* NvEncodeAPI interfaces to copy the bitstream data using ::NvEncLockBitstream()
* API. This is the preferred mode of operation.
*
* NOTE: Asynchronous mode is not supported on Linux.
*
*\par Synchronous mode of operation:
* The client can select synchronous mode by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0.
* The client working in synchronous mode can work in a single threaded or multi
* threaded mode. The client need not allocate any event objects. The client can
* only lock the bitstream data after NvEncodeAPI interface has returned
* ::NV_ENC_SUCCESS from encode picture. The NvEncodeAPI interface can return
* ::NV_ENC_ERR_NEED_MORE_INPUT error code from ::NvEncEncodePicture() API. The
* client must not lock the output buffer in such case but should send the next
* frame for encoding. The client must keep on calling ::NvEncEncodePicture() API
* until it returns ::NV_ENC_SUCCESS. \n
* The client must always lock the bitstream data in order in which it has submitted.
* This is true for both asynchronous and synchronous mode.
*
*\par Picture type decision:
* If the client is taking the picture type decision and it must disable the picture
* type decision module in NvEncodeAPI by setting NV_ENC_INITIALIZE_PARAMS::enablePTD
* to 0. In this case the client is required to send the picture in encoding
* order to NvEncodeAPI by doing the re-ordering for B frames. \n
* If the client doesn't want to take the picture type decision it can enable
* picture type decision module in the NvEncodeAPI interface by setting
* NV_ENC_INITIALIZE_PARAMS::enablePTD to 1 and send the input pictures in display
* order.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] createEncodeParams
* Refer ::_NV_ENC_INITIALIZE_PARAMS for details.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncInitializeEncoder (void* encoder, NV_ENC_INITIALIZE_PARAMS* createEncodeParams);
// NvEncCreateInputBuffer
/**
* \brief Allocates Input buffer.
*
* This function is used to allocate an input buffer. The client must enumerate
* the input buffer format before allocating the input buffer resources. The
* NV_ENC_INPUT_PTR returned by the NvEncodeAPI interface in the
* NV_ENC_CREATE_INPUT_BUFFER::inputBuffer field can be directly used in
* ::NvEncEncodePicture() API. The number of input buffers to be allocated by the
* client must be at least 4 more than the number of B frames being used for encoding.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] createInputBufferParams
* Pointer to the ::NV_ENC_CREATE_INPUT_BUFFER structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncCreateInputBuffer (void* encoder, NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams);
// NvEncDestroyInputBuffer
/**
* \brief Release an input buffers.
*
* This function is used to free an input buffer. If the client has allocated
* any input buffer using ::NvEncCreateInputBuffer() API, it must free those
* input buffers by calling this function. The client must release the input
* buffers before destroying the encoder using ::NvEncDestroyEncoder() API.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] inputBuffer
* Pointer to the input buffer to be released.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncDestroyInputBuffer (void* encoder, NV_ENC_INPUT_PTR inputBuffer);
// NvEncCreateBitstreamBuffer
/**
* \brief Allocates an output bitstream buffer
*
* This function is used to allocate an output bitstream buffer and returns a
* NV_ENC_OUTPUT_PTR to bitstream buffer to the client in the
* NV_ENC_CREATE_BITSTREAM_BUFFER::bitstreamBuffer field.
* The client can only call this function after the encoder session has been
* initialized using ::NvEncInitializeEncoder() API. The minimum number of output
* buffers allocated by the client must be at least 4 more than the number of B
* B frames being used for encoding. The client can only access the output
* bitsteam data by locking the \p bitstreamBuffer using the ::NvEncLockBitstream()
* function.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] createBitstreamBufferParams
* Pointer ::NV_ENC_CREATE_BITSTREAM_BUFFER for details.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncCreateBitstreamBuffer (void* encoder, NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams);
// NvEncDestroyBitstreamBuffer
/**
* \brief Release a bitstream buffer.
*
* This function is used to release the output bitstream buffer allocated using
* the ::NvEncCreateBitstreamBuffer() function. The client must release the output
* bitstreamBuffer using this function before destroying the encoder session.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] bitstreamBuffer
* Pointer to the bitstream buffer being released.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncDestroyBitstreamBuffer (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer);
// NvEncEncodePicture
/**
* \brief Submit an input picture for encoding.
*
* This function is used to submit an input picture buffer for encoding. The
* encoding parameters are passed using \p *encodePicParams which is a pointer
* to the ::_NV_ENC_PIC_PARAMS structure.
*
* If the client has set NV_ENC_INITIALIZE_PARAMS::enablePTD to 0, then it must
* send a valid value for the following fields.
* - NV_ENC_PIC_PARAMS::pictureType
* - NV_ENC_PIC_PARAMS_H264::displayPOCSyntax (H264 only)
* - NV_ENC_PIC_PARAMS_H264::frameNumSyntax(H264 only)
* - NV_ENC_PIC_PARAMS_H264::refPicFlag(H264 only)
*
*
*\par Asynchronous Encoding
* If the client has enabled asynchronous mode of encoding by setting
* NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1 in the ::NvEncInitializeEncoder()
* API ,then the client must send a valid NV_ENC_PIC_PARAMS::completionEvent.
* Incase of asynchronous mode of operation, client can queue the ::NvEncEncodePicture()
* API commands from the main thread and then queue output buffers to be processed
* to a secondary worker thread. Before the locking the output buffers in the
* secondary thread , the client must wait on NV_ENC_PIC_PARAMS::completionEvent
* it has queued in ::NvEncEncodePicture() API call. The client must always process
* completion event and the output buffer in the same order in which they have been
* submitted for encoding. The NvEncodeAPI interface is responsible for any
* re-ordering required for B frames and will always ensure that encoded bitstream
* data is written in the same order in which output buffer is submitted.
*\code
The below example shows how asynchronous encoding in case of 1 B frames
------------------------------------------------------------------------
Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..)
and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to
keep a copy of the input buffers for re-ordering and it allocates following
internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI
and the client is not responsible for the allocating or freeing the memory of
the internal buffers.
a) The client main thread will queue the following encode frame calls.
Note the picture type is unknown to the client, the decision is being taken by
NvEncodeAPI interface. The client should pass ::_NV_ENC_PIC_PARAMS parameter
consisting of allocated input buffer, output buffer and output events in successive
::NvEncEncodePicture() API calls along with other required encode picture params.
For example:
1st EncodePicture parameters - (I1, O1, E1)
2nd EncodePicture parameters - (I2, O2, E2)
3rd EncodePicture parameters - (I3, O3, E3)
b) NvEncodeAPI SW will receive the following encode Commands from the client.
The left side shows input from client in the form (Input buffer, Output Buffer,
Output Event). The right hand side shows a possible picture type decision take by
the NvEncodeAPI interface.
(I1, O1, E1) ---P1 Frame
(I2, O2, E2) ---B2 Frame
(I3, O3, E3) ---P3 Frame
c) NvEncodeAPI interface will make a copy of the input buffers to its internal
buffersfor re-ordering. These copies are done as part of nvEncEncodePicture
function call from the client and NvEncodeAPI interface is responsible for
synchronization of copy operation with the actual encoding operation.
I1 --> NvI1
I2 --> NvI2
I3 --> NvI3
d) After returning from ::NvEncEncodePicture() call , the client must queue the output
bitstream processing work to the secondary thread. The output bitstream processing
for asynchronous mode consist of first waiting on completion event(E1, E2..)
and then locking the output bitstream buffer(O1, O2..) for reading the encoded
data. The work queued to the secondary thread by the client is in the following order
(I1, O1, E1)
(I2, O2, E2)
(I3, O3, E3)
Note they are in the same order in which client calls ::NvEncEncodePicture() API
in \p step a).
e) NvEncodeAPI interface will do the re-ordering such that Encoder HW will receive
the following encode commands:
(NvI1, O1, E1) ---P1 Frame
(NvI3, O2, E2) ---P3 Frame
(NvI2, O3, E3) ---B2 frame
f) After the encoding operations are completed, the events will be signalled
by NvEncodeAPI interface in the following order :
(O1, E1) ---P1 Frame ,output bitstream copied to O1 and event E1 signalled.
(O2, E2) ---P3 Frame ,output bitstream copied to O2 and event E2 signalled.
(O3, E3) ---B2 Frame ,output bitstream copied to O3 and event E3 signalled.
g) The client must lock the bitstream data using ::NvEncLockBitstream() API in
the order O1,O2,O3 to read the encoded data, after waiting for the events
to be signalled in the same order i.e E1, E2 and E3.The output processing is
done in the secondary thread in the following order:
Waits on E1, copies encoded bitstream from O1
Waits on E2, copies encoded bitstream from O2
Waits on E3, copies encoded bitstream from O3
-Note the client will receive the events signalling and output buffer in the
same order in which they have submitted for encoding.
-Note the LockBitstream will have picture type field which will notify the
output picture type to the clients.
-Note the input, output buffer and the output completion event are free to be
reused once NvEncodeAPI interfaced has signalled the event and the client has
copied the data from the output buffer.
* \endcode
*
*\par Synchronous Encoding
* The client can enable synchronous mode of encoding by setting
* NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0 in ::NvEncInitializeEncoder() API.
* The NvEncodeAPI interface may return ::NV_ENC_ERR_NEED_MORE_INPUT error code for
* some ::NvEncEncodePicture() API calls when NV_ENC_INITIALIZE_PARAMS::enablePTD
* is set to 1, but the client must not treat it as a fatal error. The NvEncodeAPI
* interface might not be able to submit an input picture buffer for encoding
* immediately due to re-ordering for B frames. The NvEncodeAPI interface cannot
* submit the input picture which is decided to be encoded as B frame as it waits
* for backward reference from temporally subsequent frames. This input picture
* is buffered internally and waits for more input picture to arrive. The client
* must not call ::NvEncLockBitstream() API on the output buffers whose
* ::NvEncEncodePicture() API returns ::NV_ENC_ERR_NEED_MORE_INPUT. The client must
* wait for the NvEncodeAPI interface to return ::NV_ENC_SUCCESS before locking the
* output bitstreams to read the encoded bitstream data. The following example
* explains the scenario with synchronous encoding with 2 B frames.
*\code
The below example shows how synchronous encoding works in case of 1 B frames
-----------------------------------------------------------------------------
Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..)
and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to
keep a copy of the input buffers for re-ordering and it allocates following
internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI
and the client is not responsible for the allocating or freeing the memory of
the internal buffers.
The client calls ::NvEncEncodePicture() API with input buffer I1 and output buffer O1.
The NvEncodeAPI decides to encode I1 as P frame and submits it to encoder
HW and returns ::NV_ENC_SUCCESS.
The client can now read the encoded data by locking the output O1 by calling
NvEncLockBitstream API.
The client calls ::NvEncEncodePicture() API with input buffer I2 and output buffer O2.
The NvEncodeAPI decides to encode I2 as B frame and buffers I2 by copying it
to internal buffer and returns ::NV_ENC_ERR_NEED_MORE_INPUT.
The error is not fatal and it notifies client that it cannot read the encoded
data by locking the output O2 by calling ::NvEncLockBitstream() API without submitting
more work to the NvEncodeAPI interface.
The client calls ::NvEncEncodePicture() with input buffer I3 and output buffer O3.
The NvEncodeAPI decides to encode I3 as P frame and it first submits I3 for
encoding which will be used as backward reference frame for I2.
The NvEncodeAPI then submits I2 for encoding and returns ::NV_ENC_SUCESS. Both
the submission are part of the same ::NvEncEncodePicture() function call.
The client can now read the encoded data for both the frames by locking the output
O2 followed by O3 ,by calling ::NvEncLockBitstream() API.
The client must always lock the output in the same order in which it has submitted
to receive the encoded bitstream in correct encoding order.
* \endcode
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] encodePicParams
* Pointer to the ::_NV_ENC_PIC_PARAMS structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_ENCODER_BUSY \n
* ::NV_ENC_ERR_NEED_MORE_INPUT \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncEncodePicture (void* encoder, NV_ENC_PIC_PARAMS* encodePicParams);
// NvEncLockBitstream
/**
* \brief Lock output bitstream buffer
*
* This function is used to lock the bitstream buffer to read the encoded data.
* The client can only access the encoded data by calling this function.
* The pointer to client accessible encoded data is returned in the
* NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr field. The size of the encoded data
* in the output buffer is returned in the NV_ENC_LOCK_BITSTREAM::bitstreamSizeInBytes
* The NvEncodeAPI interface also returns the output picture type and picture structure
* of the encoded frame in NV_ENC_LOCK_BITSTREAM::pictureType and
* NV_ENC_LOCK_BITSTREAM::pictureStruct fields respectively. If the client has
* set NV_ENC_LOCK_BITSTREAM::doNotWait to 1, the function might return
* ::NV_ENC_ERR_LOCK_BUSY if client is operating in synchronous mode. This is not
* a fatal failure if NV_ENC_LOCK_BITSTREAM::doNotWait is set to 1. In the above case the client can
* retry the function after few milliseconds.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] lockBitstreamBufferParams
* Pointer to the ::_NV_ENC_LOCK_BITSTREAM structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_LOCK_BUSY \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncLockBitstream (void* encoder, NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams);
// NvEncUnlockBitstream
/**
* \brief Unlock the output bitstream buffer
*
* This function is used to unlock the output bitstream buffer after the client
* has read the encoded data from output buffer. The client must call this function
* to unlock the output buffer which it has previously locked using ::NvEncLockBitstream()
* function. Using a locked bitstream buffer in ::NvEncEncodePicture() API will cause
* the function to fail.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] bitstreamBuffer
* bitstream buffer pointer being unlocked
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncUnlockBitstream (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer);
// NvLockInputBuffer
/**
* \brief Locks an input buffer
*
* This function is used to lock the input buffer to load the uncompressed YUV
* pixel data into input buffer memory. The client must pass the NV_ENC_INPUT_PTR
* it had previously allocated using ::NvEncCreateInputBuffer()in the
* NV_ENC_LOCK_INPUT_BUFFER::inputBuffer field.
* The NvEncodeAPI interface returns pointer to client accessible input buffer
* memory in NV_ENC_LOCK_INPUT_BUFFER::bufferDataPtr field.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] lockInputBufferParams
* Pointer to the ::_NV_ENC_LOCK_INPUT_BUFFER structure
*
* \return
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_LOCK_BUSY \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncLockInputBuffer (void* encoder, NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams);
// NvUnlockInputBuffer
/**
* \brief Unlocks the input buffer
*
* This function is used to unlock the input buffer memory previously locked for
* uploading YUV pixel data. The input buffer must be unlocked before being used
* again for encoding, otherwise NvEncodeAPI will fail the ::NvEncEncodePicture()
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] inputBuffer
* Pointer to the input buffer that is being unlocked.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*
*/
NVENCSTATUS NVENCAPI NvEncUnlockInputBuffer (void* encoder, NV_ENC_INPUT_PTR inputBuffer);
// NvEncGetEncodeStats
/**
* \brief Get encoding statistics.
*
* This function is used to retrieve the encoding statistics.
* This API is not supported when encode device type is CUDA.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] encodeStats
* Pointer to the ::_NV_ENC_STAT structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetEncodeStats (void* encoder, NV_ENC_STAT* encodeStats);
// NvEncGetSequenceParams
/**
* \brief Get encoded sequence and picture header.
*
* This function can be used to retrieve the sequence and picture header out of
* band. The client must call this function only after the encoder has been
* initialized using ::NvEncInitializeEncoder() function. The client must
* allocate the memory where the NvEncodeAPI interface can copy the bitstream
* header and pass the pointer to the memory in NV_ENC_SEQUENCE_PARAM_PAYLOAD::spsppsBuffer.
* The size of buffer is passed in the field NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize.
* The NvEncodeAPI interface will copy the bitstream header payload and returns
* the actual size of the bitstream header in the field
* NV_ENC_SEQUENCE_PARAM_PAYLOAD::outSPSPPSPayloadSize.
* The client must call ::NvEncGetSequenceParams() function from the same thread which is
* being used to call ::NvEncEncodePicture() function.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] sequenceParamPayload
* Pointer to the ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncGetSequenceParams (void* encoder, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload);
// NvEncRegisterAsyncEvent
/**
* \brief Register event for notification to encoding completion.
*
* This function is used to register the completion event with NvEncodeAPI
* interface. The event is required when the client has configured the encoder to
* work in asynchronous mode. In this mode the client needs to send a completion
* event with every output buffer. The NvEncodeAPI interface will signal the
* completion of the encoding process using this event. Only after the event is
* signalled the client can get the encoded data using ::NvEncLockBitstream() function.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] eventParams
* Pointer to the ::_NV_ENC_EVENT_PARAMS structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncRegisterAsyncEvent (void* encoder, NV_ENC_EVENT_PARAMS* eventParams);
// NvEncUnregisterAsyncEvent
/**
* \brief Unregister completion event.
*
* This function is used to unregister completion event which has been previously
* registered using ::NvEncRegisterAsyncEvent() function. The client must unregister
* all events before destroying the encoder using ::NvEncDestroyEncoder() function.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] eventParams
* Pointer to the ::_NV_ENC_EVENT_PARAMS structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncUnregisterAsyncEvent (void* encoder, NV_ENC_EVENT_PARAMS* eventParams);
// NvEncMapInputResource
/**
* \brief Map an externally created input resource pointer for encoding.
*
* Maps an externally allocated input resource [using and returns a NV_ENC_INPUT_PTR
* which can be used for encoding in the ::NvEncEncodePicture() function. The
* mapped resource is returned in the field NV_ENC_MAP_INPUT_RESOURCE::outputResourcePtr.
* The NvEncodeAPI interface also returns the buffer format of the mapped resource
* in the field NV_ENC_MAP_INPUT_RESOURCE::outbufferFmt.
* This function provides synchronization guarantee that any direct3d or cuda
* work submitted on the input buffer is completed before the buffer is used for encoding.
* The client should not access any input buffer while they are mapped by the encoder.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] mapInputResParams
* Pointer to the ::_NV_ENC_MAP_INPUT_RESOURCE structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n
* ::NV_ENC_ERR_MAP_FAILED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncMapInputResource (void* encoder, NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams);
// NvEncUnmapInputResource
/**
* \brief UnMaps a NV_ENC_INPUT_PTR which was mapped for encoding
*
*
* UnMaps an input buffer which was previously mapped using ::NvEncMapInputResource()
* API. The mapping created using ::NvEncMapInputResource() should be invalidated
* using this API before the external resource is destroyed by the client. The client
* must unmap the buffer after ::NvEncLockBitstream() API returns succuessfully for encode
* work submitted using the mapped input buffer.
*
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] mappedInputBuffer
* Pointer to the NV_ENC_INPUT_PTR
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n
* ::NV_ENC_ERR_RESOURCE_NOT_MAPPED \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncUnmapInputResource (void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer);
// NvEncDestroyEncoder
/**
* \brief Destroy Encoding Session
*
* Destroys the encoder session previously created using ::NvEncOpenEncodeSession()
* function. The client must flush the encoder before freeing any resources. In order
* to flush the encoder the client must pass a NULL encode picture packet and either
* wait for the ::NvEncEncodePicture() function to return in synchronous mode or wait
* for the flush event to be signaled by the encoder in asynchronous mode.
* The client must free all the input and output resources created using the
* NvEncodeAPI interface before destroying the encoder. If the client is operating
* in asynchronous mode, it must also unregister the completion events previously
* registered.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncDestroyEncoder (void* encoder);
// NvEncInvalidateRefFrames
/**
* \brief Invalidate reference frames
*
* Invalidates reference frame based on the time stamp provided by the client.
* The encoder marks any reference frames or any frames which have been reconstructed
* using the corrupt frame as invalid for motion estimation and uses older reference
* frames for motion estimation. The encoded forces the current frame to be encoded
* as an intra frame if no reference frames are left after invalidation process.
* This is useful for low latency application for error resiliency. The client
* is recommended to set NV_ENC_CONFIG_H264::maxNumRefFrames to a large value so
* that encoder can keep a backup of older reference frames in the DPB and can use them
* for motion estimation when the newer reference frames have been invalidated.
* This API can be called multiple times.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] invalidRefFrameTimeStamp
* Timestamp of the invalid reference frames which needs to be invalidated.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncInvalidateRefFrames(void* encoder, uint64_t invalidRefFrameTimeStamp);
// NvEncOpenEncodeSessionEx
/**
* \brief Opens an encoding session.
*
* Opens an encoding session and returns a pointer to the encoder interface in
* the \p **encoder parameter. The client should start encoding process by calling
* this API first.
* The client must pass a pointer to IDirect3DDevice9/CUDA interface in the \p *device parameter.
* If the creation of encoder session fails, the client must call ::NvEncDestroyEncoder API
* before exiting.
*
* \param [in] openSessionExParams
* Pointer to a ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS structure.
* \param [out] encoder
* Encode Session pointer to the NvEncodeAPI interface.
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_NO_ENCODE_DEVICE \n
* ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n
* ::NV_ENC_ERR_INVALID_DEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncOpenEncodeSessionEx (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS *openSessionExParams, void** encoder);
// NvEncRegisterResource
/**
* \brief Registers a resource with the Nvidia Video Encoder Interface.
*
* Registers a resource with the Nvidia Video Encoder Interface for book keeping.
* The client is expected to pass the registered resource handle as well, while calling ::NvEncMapInputResource API.
* This API is not implemented for the DirectX Interface.
* DirectX based clients need not change their implementation.
*
* \param [in] encoder
* Pointer to the NVEncodeAPI interface.
*
* \param [in] registerResParams
* Pointer to a ::_NV_ENC_REGISTER_RESOURCE structure
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_RESOURCE_REGISTER_FAILED \n
* ::NV_ENC_ERR_GENERIC \n
* ::NV_ENC_ERR_UNIMPLEMENTED \n
*
*/
NVENCSTATUS NVENCAPI NvEncRegisterResource (void* encoder, NV_ENC_REGISTER_RESOURCE* registerResParams);
// NvEncUnregisterResource
/**
* \brief Unregisters a resource previously registered with the Nvidia Video Encoder Interface.
*
* Unregisters a resource previously registered with the Nvidia Video Encoder Interface.
* The client is expected to unregister any resource that it has registered with the
* Nvidia Video Encoder Interface before destroying the resource.
* This API is not implemented for the DirectX Interface.
* DirectX based clients need not change their implementation.
*
* \param [in] encoder
* Pointer to the NVEncodeAPI interface.
*
* \param [in] registeredResource
* The registered resource pointer that was returned in ::NvEncRegisterResource.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n
* ::NV_ENC_ERR_GENERIC \n
* ::NV_ENC_ERR_UNIMPLEMENTED \n
*
*/
NVENCSTATUS NVENCAPI NvEncUnregisterResource (void* encoder, NV_ENC_REGISTERED_PTR registeredResource);
// NvEncReconfigureEncoder
/**
* \brief Reconfigure an existing encoding session.
*
* Reconfigure an existing encoding session.
* The client should call this API to change/reconfigure the parameter passed during
* NvEncInitializeEncoder API call.
* Currently Reconfiguration of following are not supported.
* Change in GOP structure.
* Change in sync-Async mode.
* Change in MaxWidth & MaxHeight.
* Change in PTDmode.
*
* Resolution change is possible only if maxEncodeWidth & maxEncodeHeight of NV_ENC_INITIALIZE_PARAMS
* is set while creating encoder session.
*
* \param [in] encoder
* Pointer to the NVEncodeAPI interface.
*
* \param [in] reInitEncodeParams
* Pointer to a ::NV_ENC_RECONFIGURE_PARAMS structure.
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_NO_ENCODE_DEVICE \n
* ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n
* ::NV_ENC_ERR_INVALID_DEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_GENERIC \n
*
*/
NVENCSTATUS NVENCAPI NvEncReconfigureEncoder (void *encoder, NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams);
// NvEncCreateMVBuffer
/**
* \brief Allocates output MV buffer for ME only mode.
*
* This function is used to allocate an output MV buffer. The size of the MVBuffer is
* dependent on the frame height and width of the last NVEncCreateInputBuffer call.
* The NV_ENC_OUTPUT_PTR returned by the NvEncodeAPI interface in the
* NV_ENC_CREATE_MV_BUFFER::MVBuffer field can be used in
* ::NvEncRunMotionEstimationOnly() API.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] createMVBufferParams
* Pointer to the ::NV_ENC_CREATE_MV_BUFFER structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_GENERIC \n
*/
NVENCSTATUS NVENCAPI NvEncCreateMVBuffer (void* encoder, NV_ENC_CREATE_MV_BUFFER* createMVBufferParams);
// NvEncDestroyMVBuffer
/**
* \brief Release an output MV buffer for ME only mode.
*
* This function is used to release the output MV buffer allocated using
* the :: NvEncCreateMVBuffer() function. The client must release the output
* MVBuffer using this function before destroying the encoder session.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in] MVBuffer
* Pointer to the MVBuffer being released.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*/
NVENCSTATUS NVENCAPI NvEncDestroyMVBuffer (void* encoder, NV_ENC_OUTPUT_PTR MVBuffer);
// NvEncRunMotionEstimationOnly
/**
* \brief Submit an input picture and reference frame for motion estimation in ME only mode.
*
* This function is used to submit the input frame and reference frame for motion
* estimation. The ME parameters are passed using *MEOnlyParams which is a pointer
* to ::NV_ENC_MEONLY_PARAMS structure. The output motion vector data will be returned
* to the buffer NV_ENC_MEONLY_PARAMS::outputMV.
*
* \param [in] encoder
* Pointer to the NvEncodeAPI interface.
* \param [in,out] MEOnlyParams
* Pointer to the ::_NV_ENC_MEONLY_PARAMS structure.
*
* \return
* ::NV_ENC_SUCCESS \n
* ::NV_ENC_ERR_INVALID_PTR \n
* ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n
* ::NV_ENC_ERR_DEVICE_NOT_EXIST \n
* ::NV_ENC_ERR_UNSUPPORTED_PARAM \n
* ::NV_ENC_ERR_OUT_OF_MEMORY \n
* ::NV_ENC_ERR_INVALID_PARAM \n
* ::NV_ENC_ERR_INVALID_VERSION \n
* ::NV_ENC_ERR_NEED_MORE_INPUT \n
* ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n
* ::NV_ENC_ERR_GENERIC \n
*/
NVENCSTATUS NVENCAPI NvEncRunMotionEstimationOnly (void* encoder, NV_ENC_MEONLY_PARAMS* MEOnlyParams);
/// \cond API PFN
/*
* Defines API function pointers
*/
typedef NVENCSTATUS (NVENCAPI* PNVENCOPENENCODESESSION) (void* device, uint32_t deviceType, void** encoder);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEGUIDCOUNT) (void* encoder, uint32_t* encodeGUIDCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEGUIDS) (void* encoder, GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPROFILEGUIDCOUNT) (void* encoder, GUID encodeGUID, uint32_t* encodeProfileGUIDCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPROFILEGUIDS) (void* encoder, GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETINPUTFORMATCOUNT) (void* encoder, GUID encodeGUID, uint32_t* inputFmtCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETINPUTFORMATS) (void* encoder, GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODECAPS) (void* encoder, GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCOUNT) (void* encoder, GUID encodeGUID, uint32_t* encodePresetGUIDCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETGUIDS) (void* encoder, GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCONFIG) (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig);
typedef NVENCSTATUS (NVENCAPI* PNVENCINITIALIZEENCODER) (void* encoder, NV_ENC_INITIALIZE_PARAMS* createEncodeParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEINPUTBUFFER) (void* encoder, NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYINPUTBUFFER) (void* encoder, NV_ENC_INPUT_PTR inputBuffer);
typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEBITSTREAMBUFFER) (void* encoder, NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYBITSTREAMBUFFER) (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer);
typedef NVENCSTATUS (NVENCAPI* PNVENCENCODEPICTURE) (void* encoder, NV_ENC_PIC_PARAMS* encodePicParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCLOCKBITSTREAM) (void* encoder, NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCUNLOCKBITSTREAM) (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer);
typedef NVENCSTATUS (NVENCAPI* PNVENCLOCKINPUTBUFFER) (void* encoder, NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCUNLOCKINPUTBUFFER) (void* encoder, NV_ENC_INPUT_PTR inputBuffer);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODESTATS) (void* encoder, NV_ENC_STAT* encodeStats);
typedef NVENCSTATUS (NVENCAPI* PNVENCGETSEQUENCEPARAMS) (void* encoder, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload);
typedef NVENCSTATUS (NVENCAPI* PNVENCREGISTERASYNCEVENT) (void* encoder, NV_ENC_EVENT_PARAMS* eventParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCUNREGISTERASYNCEVENT) (void* encoder, NV_ENC_EVENT_PARAMS* eventParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCMAPINPUTRESOURCE) (void* encoder, NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCUNMAPINPUTRESOURCE) (void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer);
typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYENCODER) (void* encoder);
typedef NVENCSTATUS (NVENCAPI* PNVENCINVALIDATEREFFRAMES) (void* encoder, uint64_t invalidRefFrameTimeStamp);
typedef NVENCSTATUS (NVENCAPI* PNVENCOPENENCODESESSIONEX) (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS *openSessionExParams, void** encoder);
typedef NVENCSTATUS (NVENCAPI* PNVENCREGISTERRESOURCE) (void* encoder, NV_ENC_REGISTER_RESOURCE* registerResParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCUNREGISTERRESOURCE) (void* encoder, NV_ENC_REGISTERED_PTR registeredRes);
typedef NVENCSTATUS (NVENCAPI* PNVENCRECONFIGUREENCODER) (void* encoder, NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEMVBUFFER) (void* encoder, NV_ENC_CREATE_MV_BUFFER* createMVBufferParams);
typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYMVBUFFER) (void* encoder, NV_ENC_OUTPUT_PTR MVBuffer);
typedef NVENCSTATUS (NVENCAPI* PNVENCRUNMOTIONESTIMATIONONLY) (void* encoder, NV_ENC_MEONLY_PARAMS* MEOnlyParams);
/// \endcond
/** @} */ /* END ENCODE_FUNC */
/**
* \ingroup ENCODER_STRUCTURE
* NV_ENCODE_API_FUNCTION_LIST
*/
typedef struct _NV_ENCODE_API_FUNCTION_LIST
{
uint32_t version; /**< [in]: Client should pass NV_ENCODE_API_FUNCTION_LIST_VER. */
uint32_t reserved; /**< [in]: Reserved and should be set to 0. */
PNVENCOPENENCODESESSION nvEncOpenEncodeSession; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */
PNVENCGETENCODEGUIDCOUNT nvEncGetEncodeGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeGUIDCount() API through this pointer. */
PNVENCGETENCODEPRESETCOUNT nvEncGetEncodeProfileGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDCount() API through this pointer.*/
PNVENCGETENCODEPRESETGUIDS nvEncGetEncodeProfileGUIDs; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDs() API through this pointer. */
PNVENCGETENCODEGUIDS nvEncGetEncodeGUIDs; /**< [out]: Client should access ::NvEncGetEncodeGUIDs() API through this pointer. */
PNVENCGETINPUTFORMATCOUNT nvEncGetInputFormatCount; /**< [out]: Client should access ::NvEncGetInputFormatCount() API through this pointer. */
PNVENCGETINPUTFORMATS nvEncGetInputFormats; /**< [out]: Client should access ::NvEncGetInputFormats() API through this pointer. */
PNVENCGETENCODECAPS nvEncGetEncodeCaps; /**< [out]: Client should access ::NvEncGetEncodeCaps() API through this pointer. */
PNVENCGETENCODEPRESETCOUNT nvEncGetEncodePresetCount; /**< [out]: Client should access ::NvEncGetEncodePresetCount() API through this pointer. */
PNVENCGETENCODEPRESETGUIDS nvEncGetEncodePresetGUIDs; /**< [out]: Client should access ::NvEncGetEncodePresetGUIDs() API through this pointer. */
PNVENCGETENCODEPRESETCONFIG nvEncGetEncodePresetConfig; /**< [out]: Client should access ::NvEncGetEncodePresetConfig() API through this pointer. */
PNVENCINITIALIZEENCODER nvEncInitializeEncoder; /**< [out]: Client should access ::NvEncInitializeEncoder() API through this pointer. */
PNVENCCREATEINPUTBUFFER nvEncCreateInputBuffer; /**< [out]: Client should access ::NvEncCreateInputBuffer() API through this pointer. */
PNVENCDESTROYINPUTBUFFER nvEncDestroyInputBuffer; /**< [out]: Client should access ::NvEncDestroyInputBuffer() API through this pointer. */
PNVENCCREATEBITSTREAMBUFFER nvEncCreateBitstreamBuffer; /**< [out]: Client should access ::NvEncCreateBitstreamBuffer() API through this pointer. */
PNVENCDESTROYBITSTREAMBUFFER nvEncDestroyBitstreamBuffer; /**< [out]: Client should access ::NvEncDestroyBitstreamBuffer() API through this pointer. */
PNVENCENCODEPICTURE nvEncEncodePicture; /**< [out]: Client should access ::NvEncEncodePicture() API through this pointer. */
PNVENCLOCKBITSTREAM nvEncLockBitstream; /**< [out]: Client should access ::NvEncLockBitstream() API through this pointer. */
PNVENCUNLOCKBITSTREAM nvEncUnlockBitstream; /**< [out]: Client should access ::NvEncUnlockBitstream() API through this pointer. */
PNVENCLOCKINPUTBUFFER nvEncLockInputBuffer; /**< [out]: Client should access ::NvEncLockInputBuffer() API through this pointer. */
PNVENCUNLOCKINPUTBUFFER nvEncUnlockInputBuffer; /**< [out]: Client should access ::NvEncUnlockInputBuffer() API through this pointer. */
PNVENCGETENCODESTATS nvEncGetEncodeStats; /**< [out]: Client should access ::NvEncGetEncodeStats() API through this pointer. */
PNVENCGETSEQUENCEPARAMS nvEncGetSequenceParams; /**< [out]: Client should access ::NvEncGetSequenceParams() API through this pointer. */
PNVENCREGISTERASYNCEVENT nvEncRegisterAsyncEvent; /**< [out]: Client should access ::NvEncRegisterAsyncEvent() API through this pointer. */
PNVENCUNREGISTERASYNCEVENT nvEncUnregisterAsyncEvent; /**< [out]: Client should access ::NvEncUnregisterAsyncEvent() API through this pointer. */
PNVENCMAPINPUTRESOURCE nvEncMapInputResource; /**< [out]: Client should access ::NvEncMapInputResource() API through this pointer. */
PNVENCUNMAPINPUTRESOURCE nvEncUnmapInputResource; /**< [out]: Client should access ::NvEncUnmapInputResource() API through this pointer. */
PNVENCDESTROYENCODER nvEncDestroyEncoder; /**< [out]: Client should access ::NvEncDestroyEncoder() API through this pointer. */
PNVENCINVALIDATEREFFRAMES nvEncInvalidateRefFrames; /**< [out]: Client should access ::NvEncInvalidateRefFrames() API through this pointer. */
PNVENCOPENENCODESESSIONEX nvEncOpenEncodeSessionEx; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */
PNVENCREGISTERRESOURCE nvEncRegisterResource; /**< [out]: Client should access ::NvEncRegisterResource() API through this pointer. */
PNVENCUNREGISTERRESOURCE nvEncUnregisterResource; /**< [out]: Client should access ::NvEncUnregisterResource() API through this pointer. */
PNVENCRECONFIGUREENCODER nvEncReconfigureEncoder; /**< [out]: Client should access ::NvEncReconfigureEncoder() API through this pointer. */
void* reserved1;
PNVENCCREATEMVBUFFER nvEncCreateMVBuffer; /**< [out]: Client should access ::NvEncCreateMVBuffer API through this pointer. */
PNVENCDESTROYMVBUFFER nvEncDestroyMVBuffer; /**< [out]: Client should access ::NvEncDestroyMVBuffer API through this pointer. */
PNVENCRUNMOTIONESTIMATIONONLY nvEncRunMotionEstimationOnly; /**< [out]: Client should access ::NvEncRunMotionEstimationOnly API through this pointer. */
void* reserved2[281]; /**< [in]: Reserved and must be set to NULL */
} NV_ENCODE_API_FUNCTION_LIST;
/** Macro for constructing the version field of ::_NV_ENCODEAPI_FUNCTION_LIST. */
#define NV_ENCODE_API_FUNCTION_LIST_VER NVENCAPI_STRUCT_VERSION(2)
// NvEncodeAPICreateInstance
/**
* \ingroup ENCODE_FUNC
* Entry Point to the NvEncodeAPI interface.
*
* Creates an instance of the NvEncodeAPI interface, and populates the
* pFunctionList with function pointers to the API routines implemented by the
* NvEncodeAPI interface.
*
* \param [out] functionList
*
* \return
* ::NV_ENC_SUCCESS
* ::NV_ENC_ERR_INVALID_PTR
*/
NVENCSTATUS NVENCAPI NvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST *functionList);
#ifdef __cplusplus
}
#endif
#endif
////////////////////////////////////////////////////////////////////////////
//
// Copyright 1993-2014 NVIDIA Corporation. All rights reserved.
//
// Please refer to the NVIDIA end user license agreement (EULA) associated
// with this source code for terms and conditions that govern your use of
// this software. Any use, reproduction, disclosure, or distribution of
// this software and related documentation outside the terms of the EULA
// is strictly prohibited.
//
////////////////////////////////////////////////////////////////////////////
#include "../common/inc/nvEncodeAPI.h"
#include "../common/inc/nvUtils.h"
#include "NvEncoderPerf.h"
#include <process.h>
//#define VERBOSE
#define BITSTREAM_BUFFER_SIZE 2 * 1024 * 1024
#define MAX_FRAMES_TO_PRELOAD 60
void CNvEncoderPerf::ConvertYUVpitchToNV12(unsigned char *yuv_luma, unsigned char *yuv_cb, unsigned char *yuv_cr, int width, int height, int index)
{
uint32_t lockedPitch;
unsigned char *pInputSurface;
m_pNvHWEncoder->NvEncLockInputBuffer(m_stEncodeBuffer[index].stInputBfr.hInputSurface, (void**)&pInputSurface, &lockedPitch);
unsigned char *pInputSurfaceCh = pInputSurface + (m_stEncodeBuffer[index].stInputBfr.dwHeight*lockedPitch);
int y;
int x;
if (width == 0)
width = width;
if (lockedPitch == 0)
lockedPitch = width;
for (y = 0; y < height; y++)
{
memcpy(pInputSurface + (lockedPitch*y), yuv_luma + (width*y), width);
}
for (y = 0; y < height / 2; y++)
{
for (x = 0; x < width; x = x + 2)
{
pInputSurfaceCh[(y*lockedPitch) + x] = yuv_cb[((width / 2)*y) + (x >> 1)];
pInputSurfaceCh[(y*lockedPitch) + (x + 1)] = yuv_cr[((width / 2)*y) + (x >> 1)];
}
}
m_pNvHWEncoder->NvEncUnlockInputBuffer(m_stEncodeBuffer[index].stInputBfr.hInputSurface);
}
void CNvEncoderPerf::ConvertYUVpitchToYUV444(unsigned char *yuv_luma, unsigned char *yuv_cb, unsigned char *yuv_cr, int width, int height, int index)
{
uint32_t lockedPitch;
unsigned char *pInputSurface;
m_pNvHWEncoder->NvEncLockInputBuffer(m_stEncodeBuffer[index].stInputBfr.hInputSurface, (void**)&pInputSurface, &lockedPitch);
if (lockedPitch == 0)
lockedPitch = width;
unsigned char *pInputSurfaceCb = pInputSurface + (m_stEncodeBuffer[index].stInputBfr.dwHeight*lockedPitch);
unsigned char *pInputSurfaceCr = pInputSurfaceCb + (m_stEncodeBuffer[index].stInputBfr.dwHeight*lockedPitch);
for (int h = 0; h < height; h++)
{
memcpy(pInputSurface + lockedPitch * h, yuv_luma + width * h, width);
memcpy(pInputSurfaceCb + lockedPitch * h, yuv_cb + width * h, width);
memcpy(pInputSurfaceCr + lockedPitch * h, yuv_cr + width * h, width);
}
m_pNvHWEncoder->NvEncUnlockInputBuffer(m_stEncodeBuffer[index].stInputBfr.hInputSurface);
}
CNvEncoderPerf::CNvEncoderPerf()
{
m_pNvHWEncoder = new CNvHWEncoder;
m_pDevice = NULL;
#if defined (NV_WINDOWS)
m_pD3D = NULL;
#endif
m_cuContext = NULL;
m_uEncodeBufferCount = 0;
memset(&m_stEncoderInput, 0, sizeof(m_stEncoderInput));
memset(&m_stEOSOutputBfr, 0, sizeof(m_stEOSOutputBfr));
memset(&m_stEncodeBuffer, 0, sizeof(m_stEncodeBuffer));
}
CNvEncoderPerf::~CNvEncoderPerf()
{
if (m_pNvHWEncoder)
{
delete m_pNvHWEncoder;
m_pNvHWEncoder = NULL;
}
}
NVENCSTATUS CNvEncoderPerf::InitCuda(uint32_t deviceID)
{
CUresult cuResult;
CUdevice device;
CUcontext cuContextCurr;
int deviceCount = 0;
int SMminor = 0, SMmajor = 0;
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
typedef HMODULE CUDADRIVER;
#else
typedef void *CUDADRIVER;
#endif
CUDADRIVER hHandleDriver = 0;
cuResult = cuInit(0, __CUDA_API_VERSION, hHandleDriver);
if (cuResult != CUDA_SUCCESS)
{
PRINTERR("cuInit error:0x%x\n", cuResult);
assert(0);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
cuResult = cuDeviceGetCount(&deviceCount);
if (cuResult != CUDA_SUCCESS)
{
PRINTERR("cuDeviceGetCount error:0x%x\n", cuResult);
assert(0);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
// If dev is negative value, we clamp to 0
if ((int)deviceID < 0)
deviceID = 0;
if (deviceID >(unsigned int)deviceCount - 1)
{
PRINTERR("Invalid Device Id = %d\n", deviceID);
return NV_ENC_ERR_INVALID_ENCODERDEVICE;
}
cuResult = cuDeviceGet(&device, deviceID);
if (cuResult != CUDA_SUCCESS)
{
PRINTERR("cuDeviceGet error:0x%x\n", cuResult);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
cuResult = cuDeviceComputeCapability(&SMmajor, &SMminor, deviceID);
if (cuResult != CUDA_SUCCESS)
{
PRINTERR("cuDeviceComputeCapability error:0x%x\n", cuResult);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
if (((SMmajor << 4) + SMminor) < 0x30)
{
PRINTERR("GPU %d does not have NVENC capabilities exiting\n", deviceID);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
cuResult = cuCtxCreate((CUcontext*)(&m_pDevice), 0, device);
if (cuResult != CUDA_SUCCESS)
{
PRINTERR("cuCtxCreate error:0x%x\n", cuResult);
assert(0);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
cuResult = cuCtxPopCurrent(&cuContextCurr);
if (cuResult != CUDA_SUCCESS)
{
PRINTERR("cuCtxPopCurrent error:0x%x\n", cuResult);
assert(0);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
return NV_ENC_SUCCESS;
}
#if defined(NV_WINDOWS)
NVENCSTATUS CNvEncoderPerf::InitD3D9(uint32_t deviceID)
{
D3DPRESENT_PARAMETERS d3dpp;
D3DADAPTER_IDENTIFIER9 adapterId;
unsigned int iAdapter = NULL;
HRESULT hr = S_OK;
m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (m_pD3D == NULL)
{
assert(m_pD3D);
return NV_ENC_ERR_OUT_OF_MEMORY;;
}
if (deviceID >= m_pD3D->GetAdapterCount())
{
PRINTERR("Invalid Device Id = %d. Please use DX10/DX11 to detect headless video devices.\n", deviceID);
return NV_ENC_ERR_INVALID_ENCODERDEVICE;
}
hr = m_pD3D->GetAdapterIdentifier(deviceID, 0, &adapterId);
if (hr != S_OK)
{
PRINTERR("Invalid Device Id = %d\n", deviceID);
return NV_ENC_ERR_INVALID_ENCODERDEVICE;
}
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferCount = 1;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.Flags = D3DPRESENTFLAG_VIDEO;//D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
DWORD dwBehaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_MULTITHREADED | D3DCREATE_HARDWARE_VERTEXPROCESSING;
hr = m_pD3D->CreateDevice(deviceID,
D3DDEVTYPE_HAL,
GetDesktopWindow(),
dwBehaviorFlags,
&d3dpp,
(IDirect3DDevice9**)(&m_pDevice));
if (FAILED(hr))
return NV_ENC_ERR_OUT_OF_MEMORY;
return NV_ENC_SUCCESS;
}
NVENCSTATUS CNvEncoderPerf::InitD3D10(uint32_t deviceID)
{
HRESULT hr;
IDXGIFactory * pFactory = NULL;
IDXGIAdapter * pAdapter;
if (CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory) != S_OK)
{
return NV_ENC_ERR_GENERIC;
}
if (pFactory->EnumAdapters(deviceID, &pAdapter) != DXGI_ERROR_NOT_FOUND)
{
hr = D3D10CreateDevice(pAdapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0,
D3D10_SDK_VERSION, (ID3D10Device**)(&m_pDevice));
if (FAILED(hr))
{
PRINTERR("Invalid Device Id = %d\n", deviceID);
return NV_ENC_ERR_OUT_OF_MEMORY;
}
}
else
{
PRINTERR("Invalid Device Id = %d\n", deviceID);
return NV_ENC_ERR_INVALID_ENCODERDEVICE;
}
return NV_ENC_SUCCESS;
}
NVENCSTATUS CNvEncoderPerf::InitD3D11(uint32_t deviceID)
{
HRESULT hr;
IDXGIFactory * pFactory = NULL;
IDXGIAdapter * pAdapter;
if (CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory) != S_OK)
{
return NV_ENC_ERR_GENERIC;
}
if (pFactory->EnumAdapters(deviceID, &pAdapter) != DXGI_ERROR_NOT_FOUND)
{
hr = D3D11CreateDevice(pAdapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0,
NULL, 0, D3D11_SDK_VERSION, (ID3D11Device**)(&m_pDevice), NULL, NULL);
if (FAILED(hr))
{
PRINTERR("Invalid Device Id = %d\n", deviceID);
return NV_ENC_ERR_OUT_OF_MEMORY;
}
}
else
{
PRINTERR("Invalid Device Id = %d\n", deviceID);
return NV_ENC_ERR_NO_ENCODE_DEVICE;
}
return NV_ENC_SUCCESS;
}
#endif
NVENCSTATUS CNvEncoderPerf::AllocateIOBuffers(uint32_t uInputWidth, uint32_t uInputHeight, int isYuv444)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
m_EncodeBufferQueue.Initialize(m_stEncodeBuffer, m_uEncodeBufferCount);
for (uint32_t i = 0; i < m_uEncodeBufferCount; i++)
{
nvStatus = m_pNvHWEncoder->NvEncCreateInputBuffer(uInputWidth, uInputHeight, &m_stEncodeBuffer[i].stInputBfr.hInputSurface, isYuv444);
if (nvStatus != NV_ENC_SUCCESS)
{
PRINTERR("Failed to allocate Input Buffer, Please reduce MAX_FRAMES_TO_PRELOAD\n");
return nvStatus;
}
if (isYuv444 == 0)
m_stEncodeBuffer[i].stInputBfr.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
else
m_stEncodeBuffer[i].stInputBfr.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
m_stEncodeBuffer[i].stInputBfr.dwWidth = uInputWidth;
m_stEncodeBuffer[i].stInputBfr.dwHeight = uInputHeight;
nvStatus = m_pNvHWEncoder->NvEncCreateBitstreamBuffer(BITSTREAM_BUFFER_SIZE, &m_stEncodeBuffer[i].stOutputBfr.hBitstreamBuffer);
if (nvStatus != NV_ENC_SUCCESS)
{
PRINTERR("Failed to allocate Output Buffer, Please reduce MAX_FRAMES_TO_PRELOAD\n");
return nvStatus;
}
m_stEncodeBuffer[i].stOutputBfr.dwBitstreamBufferSize = BITSTREAM_BUFFER_SIZE;
}
m_stEOSOutputBfr.bEOSFlag = TRUE;
return NV_ENC_SUCCESS;
}
NVENCSTATUS CNvEncoderPerf::ReleaseIOBuffers()
{
for (uint32_t i = 0; i < m_uEncodeBufferCount; i++)
{
m_pNvHWEncoder->NvEncDestroyInputBuffer(m_stEncodeBuffer[i].stInputBfr.hInputSurface);
char bu[128];
sprintf(bu, "%p\n", (void*)m_stEncodeBuffer[i].stInputBfr.hInputSurface);
OutputDebugStringA(bu);
m_stEncodeBuffer[i].stInputBfr.hInputSurface = NULL;
m_pNvHWEncoder->NvEncDestroyBitstreamBuffer(m_stEncodeBuffer[i].stOutputBfr.hBitstreamBuffer);
m_stEncodeBuffer[i].stOutputBfr.hBitstreamBuffer = NULL;
}
return NV_ENC_SUCCESS;
}
NVENCSTATUS CNvEncoderPerf::FlushEncoder()
{
NVENCSTATUS nvStatus = m_pNvHWEncoder->NvEncFlushEncoderQueue(nullptr);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
return nvStatus;
}
EncodeBuffer *pEncodeBufer = m_EncodeBufferQueue.GetPending();
while (pEncodeBufer)
{
m_pNvHWEncoder->ProcessOutput(pEncodeBufer);
pEncodeBufer = m_EncodeBufferQueue.GetPending();
}
return nvStatus;
}
NVENCSTATUS CNvEncoderPerf::Deinitialize(uint32_t devicetype)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
ReleaseIOBuffers();
nvStatus = m_pNvHWEncoder->NvEncDestroyEncoder();
if (m_pDevice)
{
switch (devicetype)
{
#if defined(NV_WINDOWS)
case NV_ENC_DX9:
((IDirect3DDevice9*)(m_pDevice))->Release();
break;
case NV_ENC_DX10:
((ID3D10Device*)(m_pDevice))->Release();
break;
case NV_ENC_DX11:
((ID3D11Device*)(m_pDevice))->Release();
break;
#endif
case NV_ENC_CUDA:
CUresult cuResult = CUDA_SUCCESS;
cuResult = cuCtxDestroy((CUcontext)m_pDevice);
if (cuResult != CUDA_SUCCESS)
PRINTERR("cuCtxDestroy error:0x%x\n", cuResult);
}
m_pDevice = NULL;
}
#if defined (NV_WINDOWS)
if (m_pD3D)
{
m_pD3D->Release();
m_pD3D = NULL;
}
#endif
return nvStatus;
}
void PrintHelp()
{
printf("Usage : NvEncoderPerf \n"
"-i <string> Specify input yuv420 file\n"
"-o <string> Specify output bitstream file\n"
"-size <int int> Specify input resolution <width height>\n"
"\n### Optional parameters ###\n"
"-codec <integer> Specify the codec \n"
" 0: H264\n"
" 1: HEVC\n"
"-preset <string> Specify the preset for encoder settings\n"
" hq : nvenc HQ \n"
" hp : nvenc HP \n"
" lowLatencyHP : nvenc low latency HP \n"
" lowLatencyHQ : nvenc low latency HQ \n"
"-startf <integer> Specify start index for encoding. Default is 0\n"
"-endf <integer> Specify end index for encoding. Default is end of file\n"
"-fps <integer> Specify encoding frame rate\n"
"-goplength <integer> Specify gop length\n"
"-numB <integer> Specify number of B frames\n"
"-bitrate <integer> Specify the encoding average bitrate\n"
"-vbvMaxBitrate <integer> Specify the vbv max bitrate\n"
"-vbvSize <integer> Specify the encoding vbv/hrd buffer size\n"
"-rcmode <integer> Specify the rate control mode\n"
" 0: Constant QP\n"
" 1: Single pass VBR\n"
" 2: Single pass CBR\n"
" 4: Single pass VBR minQP\n"
" 8: Two pass frame quality\n"
" 16: Two pass frame size cap\n"
" 32: Two pass VBR\n"
"-qp <integer> Specify qp for Constant QP mode\n"
"-i_qfactor <float> Specify qscale difference between I-frames and P-frames\n"
"-b_qfactor <float> Specify qscale difference between P-frames and B-frames\n"
"-i_qoffset <float> Specify qscale offset between I-frames and P-frames\n"
"-b_qoffset <float> Specify qscale offset between P-frames and B-frames\n"
"-devicetype <integer> Specify devicetype used for encoding\n"
" 0: DX9\n"
" 1: DX11\n"
" 2: Cuda\n"
" 3: DX10\n"
"-deviceID <integer> Specify the GPU device on which encoding will take place\n"
"-yuv444 <integer> Specify the input YUV format\n"
" 0: YUV 420\n"
" 1: YUV 444\n"
"-help Prints Help Information\n\n"
);
}
int CNvEncoderPerf::EncodeMain(std::atomic<int>& generation)
{
uint8_t *yuv[3] = { 0 };
unsigned long long lStart, lEnd, lFreq;
int numFramesEncoded = 0;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
bool bError = false;
double elapsedTime = 0.0f;
bool eof = false;
EncodeConfig encodeConfig;
uint32_t chromaFormatIDC = 0;
int32_t lumaPlaneSize = 0, chromaPlaneSize = 0;
memset(&encodeConfig, 0, sizeof(EncodeConfig));
encodeConfig.width = 736;
encodeConfig.height = 576;
encodeConfig.endFrameIdx = INT_MAX;
encodeConfig.bitrate = 5000000;
encodeConfig.rcMode = NV_ENC_PARAMS_RC_CONSTQP;
encodeConfig.gopLength = NVENC_INFINITE_GOPLENGTH;
encodeConfig.deviceType = NV_ENC_CUDA;
encodeConfig.codec = NV_ENC_H264;
encodeConfig.fps = 30;
encodeConfig.qp = 28;
encodeConfig.i_quant_factor = DEFAULT_I_QFACTOR;
encodeConfig.b_quant_factor = DEFAULT_B_QFACTOR;
encodeConfig.i_quant_offset = DEFAULT_I_QOFFSET;
encodeConfig.b_quant_offset = DEFAULT_B_QOFFSET;
encodeConfig.presetGUID = NV_ENC_PRESET_DEFAULT_GUID;
encodeConfig.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
nvStatus = m_pNvHWEncoder->ParseArguments(&encodeConfig, 0, nullptr);
if (nvStatus != NV_ENC_SUCCESS)
{
PrintHelp();
return 1;
}
if (encodeConfig.width == 0 || encodeConfig.height == 0)
{
PrintHelp();
return 1;
}
switch (encodeConfig.deviceType)
{
#if defined(NV_WINDOWS)
case NV_ENC_DX9:
InitD3D9(encodeConfig.deviceID);
break;
case NV_ENC_DX10:
InitD3D10(encodeConfig.deviceID);
break;
case NV_ENC_DX11:
InitD3D11(encodeConfig.deviceID);
break;
#endif
case NV_ENC_CUDA:
InitCuda(encodeConfig.deviceID);
break;
}
if (encodeConfig.deviceType != NV_ENC_CUDA)
nvStatus = m_pNvHWEncoder->Initialize(m_pDevice, NV_ENC_DEVICE_TYPE_DIRECTX);
else
nvStatus = m_pNvHWEncoder->Initialize(m_pDevice, NV_ENC_DEVICE_TYPE_CUDA);
if (nvStatus != NV_ENC_SUCCESS)
return 1;
encodeConfig.presetGUID = m_pNvHWEncoder->GetPresetGUID(encodeConfig.encoderPreset, encodeConfig.codec);
#ifdef VERBOSE
printf("Encoding input : \"%s\"\n", encodeConfig.inputFileName);
printf(" output : \"%s\"\n", encodeConfig.outputFileName);
printf(" codec : \"%s\"\n", encodeConfig.codec == NV_ENC_HEVC ? "HEVC" : "H264");
printf(" size : %dx%d\n", encodeConfig.width, encodeConfig.height);
printf(" bitrate : %d bits/sec\n", encodeConfig.bitrate);
printf(" vbvMaxBitrate : %d bits/sec\n", encodeConfig.vbvMaxBitrate);
printf(" vbvSize : %d bits\n", encodeConfig.vbvSize);
printf(" fps : %d frames/sec\n", encodeConfig.fps);
printf(" rcMode : %s\n", encodeConfig.rcMode == NV_ENC_PARAMS_RC_CONSTQP ? "CONSTQP" :
encodeConfig.rcMode == NV_ENC_PARAMS_RC_VBR ? "VBR" :
encodeConfig.rcMode == NV_ENC_PARAMS_RC_CBR ? "CBR" :
encodeConfig.rcMode == NV_ENC_PARAMS_RC_VBR_MINQP ? "VBR MINQP" :
encodeConfig.rcMode == NV_ENC_PARAMS_RC_2_PASS_QUALITY ? "TWO_PASS_QUALITY" :
encodeConfig.rcMode == NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP ? "TWO_PASS_FRAMESIZE_CAP" :
encodeConfig.rcMode == NV_ENC_PARAMS_RC_2_PASS_VBR ? "TWO_PASS_VBR" : "UNKNOWN");
if (encodeConfig.gopLength == NVENC_INFINITE_GOPLENGTH)
printf(" goplength : INFINITE GOP \n");
else
printf(" goplength : %d \n", encodeConfig.gopLength);
printf(" B frames : %d \n", encodeConfig.numB);
printf(" QP : %d \n", encodeConfig.qp);
printf(" preset : %s\n", (encodeConfig.presetGUID == NV_ENC_PRESET_LOW_LATENCY_HQ_GUID) ? "LOW_LATENCY_HQ" :
(encodeConfig.presetGUID == NV_ENC_PRESET_LOW_LATENCY_HP_GUID) ? "LOW_LATENCY_HP" :
(encodeConfig.presetGUID == NV_ENC_PRESET_HQ_GUID) ? "HQ_PRESET" :
(encodeConfig.presetGUID == NV_ENC_PRESET_HP_GUID) ? "HP_PRESET" :
(encodeConfig.presetGUID == NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID) ? "LOW_LATENCY_DEFAULT" : "DEFAULT");
printf(" devicetype : %s\n", encodeConfig.deviceType == NV_ENC_DX9 ? "DX9" :
encodeConfig.deviceType == NV_ENC_DX10 ? "DX10" :
encodeConfig.deviceType == NV_ENC_DX11 ? "DX11" :
encodeConfig.deviceType == NV_ENC_CUDA ? "CUDA" : "INVALID");
printf("\n");
#endif
nvStatus = m_pNvHWEncoder->CreateEncoder(&encodeConfig);
if (nvStatus != NV_ENC_SUCCESS)
return 1;
m_uEncodeBufferCount = MAX_FRAMES_TO_PRELOAD;
nvStatus = AllocateIOBuffers(encodeConfig.width, encodeConfig.height, encodeConfig.isYuv444);
if (nvStatus != NV_ENC_SUCCESS)
{
bError = true;
goto exit;
}
chromaFormatIDC = (encodeConfig.isYuv444 ? 3 : 1);
lumaPlaneSize = encodeConfig.width * encodeConfig.height;
chromaPlaneSize = (chromaFormatIDC == 3) ? lumaPlaneSize : (lumaPlaneSize >> 2);
yuv[0] = new uint8_t[lumaPlaneSize];
yuv[1] = new uint8_t[chromaPlaneSize];
yuv[2] = new uint8_t[chromaPlaneSize];
NvQueryPerformanceCounter(&lStart);
int gen = generation;
for (int frm = encodeConfig.startFrameIdx; frm <= encodeConfig.endFrameIdx; frm += MAX_FRAMES_TO_PRELOAD)
{
int numFramesLoaded = 0;
for (int frmCnt = frm; frmCnt <= MIN(frm + MAX_FRAMES_TO_PRELOAD - 1, encodeConfig.endFrameIdx); frmCnt++)
{
if (gen != generation)
{
eof = true;
break;
}
for(int y = 0; y < encodeConfig.height; ++y)
for (int x = 0; x < encodeConfig.width; ++x)
{
yuv[0][encodeConfig.width*y + x] = (x + y) % 256;
yuv[1][encodeConfig.width / 2 * (y / 2) + x / 2] = (3 * x + y) % 256;
yuv[2][encodeConfig.width / 2 * (y / 2) + x / 2] = (x + 3 * y) % 256;
}
ConvertYUVpitchToNV12(yuv[0], yuv[1], yuv[2], encodeConfig.width, encodeConfig.height, (frmCnt - frm));
numFramesLoaded++;
}
if (numFramesLoaded)
{
NvQueryPerformanceCounter(&lStart);
for (int frmCnt = 0; frmCnt < numFramesLoaded; frmCnt++)
{
EncodeFrame(false, encodeConfig.width, encodeConfig.height);
numFramesEncoded++;
}
nvStatus = EncodeFrame(true, encodeConfig.width, encodeConfig.height);
if (nvStatus != NV_ENC_SUCCESS)
{
bError = true;
goto exit;
}
NvQueryPerformanceCounter(&lEnd);
elapsedTime += (double)(lEnd - lStart);
}
if (eof == true)
{
break;
}
}
#ifdef VERBOSE
if (numFramesEncoded > 0)
{
NvQueryPerformanceFrequency(&lFreq);
printf("Encoded %d frames in %6.2fms\n", numFramesEncoded, (elapsedTime*1000.0) / lFreq);
printf("Average Encode Time : %6.2fms\n", ((elapsedTime*1000.0) / numFramesEncoded) / lFreq);
printf("Frames per second: %dfps\n", (int)((float)numFramesEncoded * 1000.0 /(float)((elapsedTime*1000.0) / lFreq)));
}
#endif
exit:
Deinitialize(encodeConfig.deviceType);
for (int i = 0; i < 3; i ++)
{
if (yuv[i])
{
delete [] yuv[i];
}
}
return bError ? 1 : 0;
}
NVENCSTATUS CNvEncoderPerf::EncodeFrame(bool bFlush, uint32_t width, uint32_t height)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
EncodeBuffer *pEncodeBuffer = NULL;
NV_ENC_PIC_PARAMS encPicParams;
memset(&encPicParams, 0, sizeof(encPicParams));
SET_VER(encPicParams, NV_ENC_PIC_PARAMS);
if (bFlush)
{
FlushEncoder();
return NV_ENC_SUCCESS;
}
pEncodeBuffer = m_EncodeBufferQueue.GetAvailable();
if(!pEncodeBuffer)
{
m_pNvHWEncoder->ProcessOutput(m_EncodeBufferQueue.GetPending());
pEncodeBuffer = m_EncodeBufferQueue.GetAvailable();
}
nvStatus = m_pNvHWEncoder->NvEncEncodeFrame(pEncodeBuffer, NULL, width, height);
return nvStatus;
}
class Encoder
{
public:
HANDLE hThread = INVALID_HANDLE_VALUE;
HANDLE hEvent = INVALID_HANDLE_VALUE;
std::atomic<int> generation{ 0 };
CNvEncoderPerf encoder;
Encoder()
{
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
~Encoder()
{
CloseHandle(hEvent);
}
static unsigned __stdcall threadFunc(void* pArguments)
{
((Encoder*)pArguments)->inThread();
return 0;
}
void inThread()
{
for (;;)
{
printf("<");
encoder.EncodeMain(generation);
printf(">");
SetEvent(hEvent);
}
}
void runInThread()
{
hThread = (HANDLE)_beginthreadex(NULL, 0, &Encoder::threadFunc, this, 0, nullptr);
}
void finalize()
{
++generation;
WaitForSingleObject(hEvent, INFINITE);
ResetEvent(hEvent);
}
};
int main(int argc, char **argv)
{
Encoder encoder;
Encoder encoder_a;
encoder_a.runInThread();
encoder.runInThread();
for (;;)
{
Sleep(1000);
encoder.finalize();
}
return 0;
}
////////////////////////////////////////////////////////////////////////////
//
// Copyright 1993-2014 NVIDIA Corporation. All rights reserved.
//
// Please refer to the NVIDIA end user license agreement (EULA) associated
// with this source code for terms and conditions that govern your use of
// this software. Any use, reproduction, disclosure, or distribution of
// this software and related documentation outside the terms of the EULA
// is strictly prohibited.
//
////////////////////////////////////////////////////////////////////////////
#if defined(NV_WINDOWS)
#include <d3d9.h>
#include <d3d10_1.h>
#include <d3d11.h>
#pragma warning(disable : 4996)
#endif
#include <atomic>
#include "../common/inc/NvHWEncoder.h"
#define MAX_ENCODE_QUEUE 100
#define SET_VER(configStruct, type) {configStruct.version = type##_VER;}
template<class T>
class CNvQueue {
T** m_pBuffer;
unsigned int m_uSize;
unsigned int m_uPendingCount;
unsigned int m_uAvailableIdx;
unsigned int m_uPendingndex;
public:
CNvQueue(): m_pBuffer(NULL), m_uSize(0), m_uPendingCount(0), m_uAvailableIdx(0),
m_uPendingndex(0)
{
}
~CNvQueue()
{
delete[] m_pBuffer;
}
bool Initialize(T *pItems, unsigned int uSize)
{
m_uSize = uSize;
m_uPendingCount = 0;
m_uAvailableIdx = 0;
m_uPendingndex = 0;
m_pBuffer = new T *[m_uSize];
for (unsigned int i = 0; i < m_uSize; i++)
{
m_pBuffer[i] = &pItems[i];
}
return true;
}
T * GetAvailable()
{
T *pItem = NULL;
if (m_uPendingCount == m_uSize)
{
return NULL;
}
pItem = m_pBuffer[m_uAvailableIdx];
m_uAvailableIdx = (m_uAvailableIdx+1)%m_uSize;
m_uPendingCount += 1;
return pItem;
}
T* GetPending()
{
if (m_uPendingCount == 0)
{
return NULL;
}
T *pItem = m_pBuffer[m_uPendingndex];
m_uPendingndex = (m_uPendingndex+1)%m_uSize;
m_uPendingCount -= 1;
return pItem;
}
};
typedef struct _EncodeFrameConfig
{
uint8_t *yuv[3];
uint32_t stride[3];
uint32_t width;
uint32_t height;
}EncodeFrameConfig;
typedef enum
{
NV_ENC_DX9 = 0,
NV_ENC_DX11 = 1,
NV_ENC_CUDA = 2,
NV_ENC_DX10 = 3,
} NvEncodeDeviceType;
class CNvEncoderPerf
{
public:
CNvEncoderPerf();
virtual ~CNvEncoderPerf();
int EncodeMain(std::atomic<int>& generation);
protected:
CNvHWEncoder *m_pNvHWEncoder;
uint32_t m_uEncodeBufferCount;
void* m_pDevice;
#if defined(NV_WINDOWS)
IDirect3D9 *m_pD3D;
#endif
CUcontext m_cuContext;
EncodeConfig m_stEncoderInput;
EncodeBuffer m_stEncodeBuffer[MAX_ENCODE_QUEUE];
CNvQueue<EncodeBuffer> m_EncodeBufferQueue;
EncodeOutputBuffer m_stEOSOutputBfr;
protected:
NVENCSTATUS Deinitialize(uint32_t devicetype);
NVENCSTATUS EncodeFrame(bool bFlush=false, uint32_t width=0, uint32_t height=0);
NVENCSTATUS InitD3D9(uint32_t deviceID = 0);
NVENCSTATUS InitD3D11(uint32_t deviceID = 0);
NVENCSTATUS InitD3D10(uint32_t deviceID = 0);
NVENCSTATUS InitCuda(uint32_t deviceID = 0);
NVENCSTATUS AllocateIOBuffers(uint32_t uInputWidth, uint32_t uInputHeight,int isYuv444);
NVENCSTATUS ReleaseIOBuffers();
unsigned char* LockInputBuffer(void * hInputSurface, uint32_t *pLockedPitch);
NVENCSTATUS FlushEncoder();
void ConvertYUVpitchToNV12(unsigned char *yuv_luma, unsigned char *yuv_cb, unsigned char *yuv_cr, int width, int height, int index);
void ConvertYUVpitchToYUV444(unsigned char *yuv_luma, unsigned char *yuv_cb, unsigned char *yuv_cr, int width, int height, int index);
};
// NVEncodeAPI entry point
typedef NVENCSTATUS (NVENCAPI *MYPROC)(NV_ENCODE_API_FUNCTION_LIST*);
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include "../inc/NvHWEncoder.h"
NVENCSTATUS CNvHWEncoder::NvEncOpenEncodeSession(void* device, uint32_t deviceType)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncOpenEncodeSession(device, deviceType, &m_hEncoder);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodeGUIDCount(uint32_t* encodeGUIDCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodeGUIDCount(m_hEncoder, encodeGUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodeProfileGUIDCount(GUID encodeGUID, uint32_t* encodeProfileGUIDCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodeProfileGUIDCount(m_hEncoder, encodeGUID, encodeProfileGUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodeProfileGUIDs(GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodeProfileGUIDs(m_hEncoder, encodeGUID, profileGUIDs, guidArraySize, GUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodeGUIDs(GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodeGUIDs(m_hEncoder, GUIDs, guidArraySize, GUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetInputFormatCount(GUID encodeGUID, uint32_t* inputFmtCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetInputFormatCount(m_hEncoder, encodeGUID, inputFmtCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetInputFormats(GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetInputFormats(m_hEncoder, encodeGUID, inputFmts, inputFmtArraySize, inputFmtCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodeCaps(GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodeCaps(m_hEncoder, encodeGUID, capsParam, capsVal);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodePresetCount(GUID encodeGUID, uint32_t* encodePresetGUIDCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodePresetCount(m_hEncoder, encodeGUID, encodePresetGUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodePresetGUIDs(GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodePresetGUIDs(m_hEncoder, encodeGUID, presetGUIDs, guidArraySize, encodePresetGUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodePresetConfig(GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodePresetConfig(m_hEncoder, encodeGUID, presetGUID, presetConfig);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncCreateInputBuffer(uint32_t width, uint32_t height, void** inputBuffer, uint32_t isYuv444)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_CREATE_INPUT_BUFFER createInputBufferParams;
memset(&createInputBufferParams, 0, sizeof(createInputBufferParams));
SET_VER(createInputBufferParams, NV_ENC_CREATE_INPUT_BUFFER);
createInputBufferParams.width = width;
createInputBufferParams.height = height;
createInputBufferParams.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
createInputBufferParams.bufferFmt = isYuv444 ? NV_ENC_BUFFER_FORMAT_YUV444_PL : NV_ENC_BUFFER_FORMAT_NV12_PL;
nvStatus = m_pEncodeAPI->nvEncCreateInputBuffer(m_hEncoder, &createInputBufferParams);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
*inputBuffer = createInputBufferParams.inputBuffer;
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncDestroyInputBuffer(NV_ENC_INPUT_PTR inputBuffer)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
if (inputBuffer)
{
nvStatus = m_pEncodeAPI->nvEncDestroyInputBuffer(m_hEncoder, inputBuffer);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncCreateMVBuffer(uint32_t size, void** bitstreamBuffer)
{
NVENCSTATUS status;
NV_ENC_CREATE_MV_BUFFER stAllocMVBuffer;
memset(&stAllocMVBuffer, 0, sizeof(stAllocMVBuffer));
SET_VER(stAllocMVBuffer, NV_ENC_CREATE_MV_BUFFER);
status = m_pEncodeAPI->nvEncCreateMVBuffer(m_hEncoder, &stAllocMVBuffer);
if (status != NV_ENC_SUCCESS)
{
assert(0);
}
*bitstreamBuffer = stAllocMVBuffer.MVBuffer;
return status;
}
NVENCSTATUS CNvHWEncoder::NvEncDestroyMVBuffer(NV_ENC_OUTPUT_PTR bitstreamBuffer)
{
NVENCSTATUS status;
NV_ENC_CREATE_MV_BUFFER stAllocMVBuffer;
memset(&stAllocMVBuffer, 0, sizeof(stAllocMVBuffer));
SET_VER(stAllocMVBuffer, NV_ENC_CREATE_MV_BUFFER);
status = m_pEncodeAPI->nvEncDestroyMVBuffer(m_hEncoder, bitstreamBuffer);
if (status != NV_ENC_SUCCESS)
{
assert(0);
}
bitstreamBuffer = NULL;
return status;
}
NVENCSTATUS CNvHWEncoder::NvRunMotionEstimationOnly(EncodeBuffer *pEncodeBuffer[2], MEOnlyConfig *pMEOnly)
{
NVENCSTATUS nvStatus;
NV_ENC_MEONLY_PARAMS stMEOnlyParams;
SET_VER(stMEOnlyParams,NV_ENC_MEONLY_PARAMS);
stMEOnlyParams.referenceFrame = pEncodeBuffer[0]->stInputBfr.hInputSurface;
stMEOnlyParams.inputBuffer = pEncodeBuffer[1]->stInputBfr.hInputSurface;
stMEOnlyParams.bufferFmt = pEncodeBuffer[1]->stInputBfr.bufferFmt;
stMEOnlyParams.inputWidth = pEncodeBuffer[1]->stInputBfr.dwWidth;
stMEOnlyParams.inputHeight = pEncodeBuffer[1]->stInputBfr.dwHeight;
stMEOnlyParams.outputMV = pEncodeBuffer[0]->stOutputBfr.hBitstreamBuffer;
nvStatus = m_pEncodeAPI->nvEncRunMotionEstimationOnly(m_hEncoder, &stMEOnlyParams);
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncCreateBitstreamBuffer(uint32_t size, void** bitstreamBuffer)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_CREATE_BITSTREAM_BUFFER createBitstreamBufferParams;
memset(&createBitstreamBufferParams, 0, sizeof(createBitstreamBufferParams));
SET_VER(createBitstreamBufferParams, NV_ENC_CREATE_BITSTREAM_BUFFER);
createBitstreamBufferParams.size = size;
createBitstreamBufferParams.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
nvStatus = m_pEncodeAPI->nvEncCreateBitstreamBuffer(m_hEncoder, &createBitstreamBufferParams);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
*bitstreamBuffer = createBitstreamBufferParams.bitstreamBuffer;
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncDestroyBitstreamBuffer(NV_ENC_OUTPUT_PTR bitstreamBuffer)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
if (bitstreamBuffer)
{
nvStatus = m_pEncodeAPI->nvEncDestroyBitstreamBuffer(m_hEncoder, bitstreamBuffer);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncLockBitstream(NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncLockBitstream(m_hEncoder, lockBitstreamBufferParams);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncUnlockBitstream(NV_ENC_OUTPUT_PTR bitstreamBuffer)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncUnlockBitstream(m_hEncoder, bitstreamBuffer);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncLockInputBuffer(void* inputBuffer, void** bufferDataPtr, uint32_t* pitch)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_LOCK_INPUT_BUFFER lockInputBufferParams;
memset(&lockInputBufferParams, 0, sizeof(lockInputBufferParams));
SET_VER(lockInputBufferParams, NV_ENC_LOCK_INPUT_BUFFER);
lockInputBufferParams.inputBuffer = inputBuffer;
nvStatus = m_pEncodeAPI->nvEncLockInputBuffer(m_hEncoder, &lockInputBufferParams);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
*bufferDataPtr = lockInputBufferParams.bufferDataPtr;
*pitch = lockInputBufferParams.pitch;
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncUnlockInputBuffer(NV_ENC_INPUT_PTR inputBuffer)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncUnlockInputBuffer(m_hEncoder, inputBuffer);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetEncodeStats(NV_ENC_STAT* encodeStats)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetEncodeStats(m_hEncoder, encodeStats);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncGetSequenceParams(NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
nvStatus = m_pEncodeAPI->nvEncGetSequenceParams(m_hEncoder, sequenceParamPayload);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncDestroyEncoder()
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
if (m_bEncoderInitialized)
{
nvStatus = m_pEncodeAPI->nvEncDestroyEncoder(m_hEncoder);
m_bEncoderInitialized = false;
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncInvalidateRefFrames(const NvEncPictureCommand *pEncPicCommand)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
for (uint32_t i = 0; i < pEncPicCommand->numRefFramesToInvalidate; i++)
{
nvStatus = m_pEncodeAPI->nvEncInvalidateRefFrames(m_hEncoder, pEncPicCommand->refFrameNumbers[i]);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncOpenEncodeSessionEx(void* device, NV_ENC_DEVICE_TYPE deviceType)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS openSessionExParams;
memset(&openSessionExParams, 0, sizeof(openSessionExParams));
SET_VER(openSessionExParams, NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS);
openSessionExParams.device = device;
openSessionExParams.deviceType = deviceType;
openSessionExParams.reserved = NULL;
openSessionExParams.apiVersion = NVENCAPI_VERSION;
nvStatus = m_pEncodeAPI->nvEncOpenEncodeSessionEx(&openSessionExParams, &m_hEncoder);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::NvEncReconfigureEncoder(const NvEncPictureCommand *pEncPicCommand)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
if (pEncPicCommand->bBitrateChangePending || pEncPicCommand->bResolutionChangePending)
{
if (pEncPicCommand->bResolutionChangePending)
{
m_uCurWidth = pEncPicCommand->newWidth;
m_uCurHeight = pEncPicCommand->newHeight;
if ((m_uCurWidth > m_uMaxWidth) || (m_uCurHeight > m_uMaxHeight))
{
return NV_ENC_ERR_INVALID_PARAM;
}
m_stCreateEncodeParams.encodeWidth = m_uCurWidth;
m_stCreateEncodeParams.encodeHeight = m_uCurHeight;
m_stCreateEncodeParams.darWidth = m_uCurWidth;
m_stCreateEncodeParams.darHeight = m_uCurHeight;
}
if (pEncPicCommand->bBitrateChangePending)
{
m_stEncodeConfig.rcParams.averageBitRate = pEncPicCommand->newBitrate;
m_stEncodeConfig.rcParams.maxBitRate = pEncPicCommand->newBitrate;
m_stEncodeConfig.rcParams.vbvBufferSize = pEncPicCommand->newVBVSize != 0 ? pEncPicCommand->newVBVSize : (pEncPicCommand->newBitrate * m_stCreateEncodeParams.frameRateDen) / m_stCreateEncodeParams.frameRateNum;
m_stEncodeConfig.rcParams.vbvInitialDelay = m_stEncodeConfig.rcParams.vbvBufferSize;
}
NV_ENC_RECONFIGURE_PARAMS stReconfigParams;
memset(&stReconfigParams, 0, sizeof(stReconfigParams));
memcpy(&stReconfigParams.reInitEncodeParams, &m_stCreateEncodeParams, sizeof(m_stCreateEncodeParams));
stReconfigParams.version = NV_ENC_RECONFIGURE_PARAMS_VER;
stReconfigParams.forceIDR = pEncPicCommand->bResolutionChangePending ? 1 : 0;
nvStatus = m_pEncodeAPI->nvEncReconfigureEncoder(m_hEncoder, &stReconfigParams);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
}
return nvStatus;
}
CNvHWEncoder::CNvHWEncoder()
{
m_hEncoder = NULL;
m_bEncoderInitialized = false;
m_pEncodeAPI = NULL;
m_hinstLib = NULL;
m_EncodeIdx = 0;
m_uCurWidth = 0;
m_uCurHeight = 0;
m_uMaxWidth = 0;
m_uMaxHeight = 0;
memset(&m_stCreateEncodeParams, 0, sizeof(m_stCreateEncodeParams));
SET_VER(m_stCreateEncodeParams, NV_ENC_INITIALIZE_PARAMS);
memset(&m_stEncodeConfig, 0, sizeof(m_stEncodeConfig));
SET_VER(m_stEncodeConfig, NV_ENC_CONFIG);
}
CNvHWEncoder::~CNvHWEncoder()
{
// clean up encode API resources here
if (m_pEncodeAPI)
{
delete m_pEncodeAPI;
m_pEncodeAPI = NULL;
}
if (m_hinstLib)
{
#if defined (NV_WINDOWS)
FreeLibrary(m_hinstLib);
#else
dlclose(m_hinstLib);
#endif
m_hinstLib = NULL;
}
}
NVENCSTATUS CNvHWEncoder::ValidateEncodeGUID (GUID inputCodecGuid)
{
unsigned int i, codecFound, encodeGUIDCount, encodeGUIDArraySize;
NVENCSTATUS nvStatus;
GUID *encodeGUIDArray;
nvStatus = m_pEncodeAPI->nvEncGetEncodeGUIDCount(m_hEncoder, &encodeGUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
return nvStatus;
}
encodeGUIDArray = new GUID[encodeGUIDCount];
memset(encodeGUIDArray, 0, sizeof(GUID)* encodeGUIDCount);
encodeGUIDArraySize = 0;
nvStatus = m_pEncodeAPI->nvEncGetEncodeGUIDs(m_hEncoder, encodeGUIDArray, encodeGUIDCount, &encodeGUIDArraySize);
if (nvStatus != NV_ENC_SUCCESS)
{
delete[] encodeGUIDArray;
assert(0);
return nvStatus;
}
assert(encodeGUIDArraySize <= encodeGUIDCount);
codecFound = 0;
for (i = 0; i < encodeGUIDArraySize; i++)
{
if (inputCodecGuid == encodeGUIDArray[i])
{
codecFound = 1;
break;
}
}
delete[] encodeGUIDArray;
if (codecFound)
return NV_ENC_SUCCESS;
else
return NV_ENC_ERR_INVALID_PARAM;
}
NVENCSTATUS CNvHWEncoder::ValidatePresetGUID(GUID inputPresetGuid, GUID inputCodecGuid)
{
uint32_t i, presetFound, presetGUIDCount, presetGUIDArraySize;
NVENCSTATUS nvStatus;
GUID *presetGUIDArray;
nvStatus = m_pEncodeAPI->nvEncGetEncodePresetCount(m_hEncoder, inputCodecGuid, &presetGUIDCount);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
return nvStatus;
}
presetGUIDArray = new GUID[presetGUIDCount];
memset(presetGUIDArray, 0, sizeof(GUID)* presetGUIDCount);
presetGUIDArraySize = 0;
nvStatus = m_pEncodeAPI->nvEncGetEncodePresetGUIDs(m_hEncoder, inputCodecGuid, presetGUIDArray, presetGUIDCount, &presetGUIDArraySize);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
delete[] presetGUIDArray;
return nvStatus;
}
assert(presetGUIDArraySize <= presetGUIDCount);
presetFound = 0;
for (i = 0; i < presetGUIDArraySize; i++)
{
if (inputPresetGuid == presetGUIDArray[i])
{
presetFound = 1;
break;
}
}
delete[] presetGUIDArray;
if (presetFound)
return NV_ENC_SUCCESS;
else
return NV_ENC_ERR_INVALID_PARAM;
}
NVENCSTATUS CNvHWEncoder::CreateEncoder(const EncodeConfig *pEncCfg)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
if (pEncCfg == NULL)
{
return NV_ENC_ERR_INVALID_PARAM;
}
m_uCurWidth = pEncCfg->width;
m_uCurHeight = pEncCfg->height;
m_uMaxWidth = (pEncCfg->maxWidth > 0 ? pEncCfg->maxWidth : pEncCfg->width);
m_uMaxHeight = (pEncCfg->maxHeight > 0 ? pEncCfg->maxHeight : pEncCfg->height);
if ((m_uCurWidth > m_uMaxWidth) || (m_uCurHeight > m_uMaxHeight)) {
return NV_ENC_ERR_INVALID_PARAM;
}
if (!pEncCfg->width || !pEncCfg->height)
{
return NV_ENC_ERR_INVALID_PARAM;
}
if (pEncCfg->isYuv444 && (pEncCfg->codec == NV_ENC_HEVC))
{
PRINTERR("444 is not supported with HEVC \n");
return NV_ENC_ERR_INVALID_PARAM;
}
GUID inputCodecGUID = pEncCfg->codec == NV_ENC_H264 ? NV_ENC_CODEC_H264_GUID : NV_ENC_CODEC_HEVC_GUID;
nvStatus = ValidateEncodeGUID(inputCodecGUID);
if (nvStatus != NV_ENC_SUCCESS)
{
PRINTERR("codec not supported \n");
return nvStatus;
}
codecGUID = inputCodecGUID;
m_stCreateEncodeParams.encodeGUID = inputCodecGUID;
m_stCreateEncodeParams.presetGUID = pEncCfg->presetGUID;
m_stCreateEncodeParams.encodeWidth = pEncCfg->width;
m_stCreateEncodeParams.encodeHeight = pEncCfg->height;
m_stCreateEncodeParams.darWidth = pEncCfg->width;
m_stCreateEncodeParams.darHeight = pEncCfg->height;
m_stCreateEncodeParams.frameRateNum = pEncCfg->fps;
m_stCreateEncodeParams.frameRateDen = 1;
m_stCreateEncodeParams.enableEncodeAsync = 0;
m_stCreateEncodeParams.enablePTD = 1;
m_stCreateEncodeParams.reportSliceOffsets = 0;
m_stCreateEncodeParams.enableSubFrameWrite = 0;
m_stCreateEncodeParams.encodeConfig = &m_stEncodeConfig;
m_stCreateEncodeParams.maxEncodeWidth = m_uMaxWidth;
m_stCreateEncodeParams.maxEncodeHeight = m_uMaxHeight;
// apply preset
NV_ENC_PRESET_CONFIG stPresetCfg;
memset(&stPresetCfg, 0, sizeof(NV_ENC_PRESET_CONFIG));
SET_VER(stPresetCfg, NV_ENC_PRESET_CONFIG);
SET_VER(stPresetCfg.presetCfg, NV_ENC_CONFIG);
nvStatus = m_pEncodeAPI->nvEncGetEncodePresetConfig(m_hEncoder, m_stCreateEncodeParams.encodeGUID, m_stCreateEncodeParams.presetGUID, &stPresetCfg);
if (nvStatus != NV_ENC_SUCCESS)
{
PRINTERR("nvEncGetEncodePresetConfig returned failure");
return nvStatus;
}
memcpy(&m_stEncodeConfig, &stPresetCfg.presetCfg, sizeof(NV_ENC_CONFIG));
m_stEncodeConfig.gopLength = pEncCfg->gopLength;
m_stEncodeConfig.frameIntervalP = pEncCfg->numB + 1;
if (pEncCfg->pictureStruct == NV_ENC_PIC_STRUCT_FRAME)
{
m_stEncodeConfig.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
}
else
{
m_stEncodeConfig.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
}
m_stEncodeConfig.mvPrecision = NV_ENC_MV_PRECISION_QUARTER_PEL;
if (pEncCfg->bitrate || pEncCfg->vbvMaxBitrate)
{
m_stEncodeConfig.rcParams.rateControlMode = (NV_ENC_PARAMS_RC_MODE)pEncCfg->rcMode;
m_stEncodeConfig.rcParams.averageBitRate = pEncCfg->bitrate;
m_stEncodeConfig.rcParams.maxBitRate = pEncCfg->vbvMaxBitrate;
m_stEncodeConfig.rcParams.vbvBufferSize = pEncCfg->vbvSize;
m_stEncodeConfig.rcParams.vbvInitialDelay = pEncCfg->vbvSize * 9 / 10;
}
else
{
m_stEncodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
}
if (pEncCfg->rcMode == 0)
{
m_stEncodeConfig.rcParams.constQP.qpInterP = pEncCfg->presetGUID == NV_ENC_PRESET_LOSSLESS_HP_GUID? 0 : pEncCfg->qp;
m_stEncodeConfig.rcParams.constQP.qpInterB = pEncCfg->presetGUID == NV_ENC_PRESET_LOSSLESS_HP_GUID? 0 : pEncCfg->qp;
m_stEncodeConfig.rcParams.constQP.qpIntra = pEncCfg->presetGUID == NV_ENC_PRESET_LOSSLESS_HP_GUID? 0 : pEncCfg->qp;
}
// set up initial QP value
if (pEncCfg->rcMode == NV_ENC_PARAMS_RC_VBR || pEncCfg->rcMode == NV_ENC_PARAMS_RC_VBR_MINQP ||
pEncCfg->rcMode == NV_ENC_PARAMS_RC_2_PASS_VBR) {
m_stEncodeConfig.rcParams.enableInitialRCQP = 1;
m_stEncodeConfig.rcParams.initialRCQP.qpInterP = pEncCfg->qp;
if(pEncCfg->i_quant_factor != 0.0 && pEncCfg->b_quant_factor != 0.0) {
m_stEncodeConfig.rcParams.initialRCQP.qpIntra = (int)(pEncCfg->qp * FABS(pEncCfg->i_quant_factor) + pEncCfg->i_quant_offset);
m_stEncodeConfig.rcParams.initialRCQP.qpInterB = (int)(pEncCfg->qp * FABS(pEncCfg->b_quant_factor) + pEncCfg->b_quant_offset);
} else {
m_stEncodeConfig.rcParams.initialRCQP.qpIntra = pEncCfg->qp;
m_stEncodeConfig.rcParams.initialRCQP.qpInterB = pEncCfg->qp;
}
}
if (pEncCfg->isYuv444)
{
m_stEncodeConfig.encodeCodecConfig.h264Config.chromaFormatIDC = 3;
}
else
{
m_stEncodeConfig.encodeCodecConfig.h264Config.chromaFormatIDC = 1;
}
if (pEncCfg->intraRefreshEnableFlag)
{
if (pEncCfg->codec == NV_ENC_HEVC)
{
m_stEncodeConfig.encodeCodecConfig.hevcConfig.enableIntraRefresh = 1;
m_stEncodeConfig.encodeCodecConfig.hevcConfig.intraRefreshPeriod = pEncCfg->intraRefreshPeriod;
m_stEncodeConfig.encodeCodecConfig.hevcConfig.intraRefreshCnt = pEncCfg->intraRefreshDuration;
}
else
{
m_stEncodeConfig.encodeCodecConfig.h264Config.enableIntraRefresh = 1;
m_stEncodeConfig.encodeCodecConfig.h264Config.intraRefreshPeriod = pEncCfg->intraRefreshPeriod;
m_stEncodeConfig.encodeCodecConfig.h264Config.intraRefreshCnt = pEncCfg->intraRefreshDuration;
}
}
if (pEncCfg->invalidateRefFramesEnableFlag)
{
if (pEncCfg->codec == NV_ENC_HEVC)
{
m_stEncodeConfig.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = 16;
}
else
{
m_stEncodeConfig.encodeCodecConfig.h264Config.maxNumRefFrames = 16;
}
}
if (pEncCfg->qpDeltaMapFile)
{
m_stEncodeConfig.rcParams.enableExtQPDeltaMap = 1;
}
if (pEncCfg->codec == NV_ENC_H264)
{
m_stEncodeConfig.encodeCodecConfig.h264Config.idrPeriod = pEncCfg->gopLength;
}
else if (pEncCfg->codec == NV_ENC_HEVC)
{
m_stEncodeConfig.encodeCodecConfig.hevcConfig.idrPeriod = pEncCfg->gopLength;
}
if (pEncCfg->enableMEOnly == 1 || pEncCfg->enableMEOnly == 2)
{
NV_ENC_CAPS_PARAM stCapsParam;
memset(&stCapsParam, 0, sizeof(NV_ENC_CAPS_PARAM));
SET_VER(stCapsParam, NV_ENC_CAPS_PARAM);
stCapsParam.capsToQuery = NV_ENC_CAPS_SUPPORT_MEONLY_MODE;
m_stCreateEncodeParams.enableMEOnlyMode = true;
int meonlyMode = 0;
nvStatus = m_pEncodeAPI->nvEncGetEncodeCaps(m_hEncoder, m_stCreateEncodeParams.encodeGUID, &stCapsParam, &meonlyMode);
if (nvStatus != NV_ENC_SUCCESS)
{
PRINTERR("Encode Session Initialization failed");
return nvStatus;
}
else
{
if (meonlyMode == 1)
{
printf("NV_ENC_CAPS_SUPPORT_MEONLY_MODE supported\n");
}
else
{
PRINTERR("NV_ENC_CAPS_SUPPORT_MEONLY_MODE not supported\n");
return NV_ENC_ERR_UNSUPPORTED_DEVICE;
}
}
}
nvStatus = m_pEncodeAPI->nvEncInitializeEncoder(m_hEncoder, &m_stCreateEncodeParams);
if (nvStatus != NV_ENC_SUCCESS)
{
PRINTERR("Encode Session Initialization failed");
return nvStatus;
}
m_bEncoderInitialized = true;
return nvStatus;
}
GUID CNvHWEncoder::GetPresetGUID(char* encoderPreset, int codec)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
GUID presetGUID = NV_ENC_PRESET_DEFAULT_GUID;
if (encoderPreset && (stricmp(encoderPreset, "hq") == 0))
{
presetGUID = NV_ENC_PRESET_HQ_GUID;
}
else if (encoderPreset && (stricmp(encoderPreset, "lowLatencyHP") == 0))
{
presetGUID = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
}
else if (encoderPreset && (stricmp(encoderPreset, "hp") == 0))
{
presetGUID = NV_ENC_PRESET_HP_GUID;
}
else if (encoderPreset && (stricmp(encoderPreset, "lowLatencyHQ") == 0))
{
presetGUID = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
}
else if (encoderPreset && (stricmp(encoderPreset, "lossless") == 0))
{
presetGUID = NV_ENC_PRESET_LOSSLESS_HP_GUID;
}
else
{
if (encoderPreset)
PRINTERR("Unsupported preset guid %s\n", encoderPreset);
presetGUID = NV_ENC_PRESET_DEFAULT_GUID;
}
GUID inputCodecGUID = codec == NV_ENC_H264 ? NV_ENC_CODEC_H264_GUID : NV_ENC_CODEC_HEVC_GUID;
nvStatus = ValidatePresetGUID(presetGUID, inputCodecGUID);
if (nvStatus != NV_ENC_SUCCESS)
{
presetGUID = NV_ENC_PRESET_DEFAULT_GUID;
PRINTERR("Unsupported preset guid %s\n", encoderPreset);
}
return presetGUID;
}
NVENCSTATUS CNvHWEncoder::ProcessOutput(const EncodeBuffer *pEncodeBuffer)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
if (pEncodeBuffer->stOutputBfr.hBitstreamBuffer == NULL && pEncodeBuffer->stOutputBfr.bEOSFlag == FALSE)
{
return NV_ENC_ERR_INVALID_PARAM;
}
if (pEncodeBuffer->stOutputBfr.bEOSFlag)
return NV_ENC_SUCCESS;
nvStatus = NV_ENC_SUCCESS;
NV_ENC_LOCK_BITSTREAM lockBitstreamData;
memset(&lockBitstreamData, 0, sizeof(lockBitstreamData));
SET_VER(lockBitstreamData, NV_ENC_LOCK_BITSTREAM);
lockBitstreamData.outputBitstream = pEncodeBuffer->stOutputBfr.hBitstreamBuffer;
lockBitstreamData.doNotWait = false;
nvStatus = m_pEncodeAPI->nvEncLockBitstream(m_hEncoder, &lockBitstreamData);
if (nvStatus == NV_ENC_SUCCESS)
{
nvStatus = m_pEncodeAPI->nvEncUnlockBitstream(m_hEncoder, pEncodeBuffer->stOutputBfr.hBitstreamBuffer);
}
else
{
PRINTERR("lock bitstream function failed \n");
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::Initialize(void* device, NV_ENC_DEVICE_TYPE deviceType)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
MYPROC nvEncodeAPICreateInstance; // function pointer to create instance in nvEncodeAPI
#if defined(NV_WINDOWS)
#if defined (_WIN64)
m_hinstLib = LoadLibrary(TEXT("nvEncodeAPI64.dll"));
#else
m_hinstLib = LoadLibrary(TEXT("nvEncodeAPI.dll"));
#endif
#else
m_hinstLib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
#endif
if (m_hinstLib == NULL)
return NV_ENC_ERR_OUT_OF_MEMORY;
#if defined(NV_WINDOWS)
nvEncodeAPICreateInstance = (MYPROC)GetProcAddress(m_hinstLib, "NvEncodeAPICreateInstance");
#else
nvEncodeAPICreateInstance = (MYPROC)dlsym(m_hinstLib, "NvEncodeAPICreateInstance");
#endif
if (nvEncodeAPICreateInstance == NULL)
return NV_ENC_ERR_OUT_OF_MEMORY;
m_pEncodeAPI = new NV_ENCODE_API_FUNCTION_LIST;
if (m_pEncodeAPI == NULL)
return NV_ENC_ERR_OUT_OF_MEMORY;
memset(m_pEncodeAPI, 0, sizeof(NV_ENCODE_API_FUNCTION_LIST));
m_pEncodeAPI->version = NV_ENCODE_API_FUNCTION_LIST_VER;
nvStatus = nvEncodeAPICreateInstance(m_pEncodeAPI);
if (nvStatus != NV_ENC_SUCCESS)
return nvStatus;
nvStatus = NvEncOpenEncodeSessionEx(device, deviceType);
if (nvStatus != NV_ENC_SUCCESS)
return nvStatus;
return NV_ENC_SUCCESS;
}
NVENCSTATUS CNvHWEncoder::NvEncEncodeFrame(EncodeBuffer *pEncodeBuffer, NvEncPictureCommand *encPicCommand,
uint32_t width, uint32_t height, NV_ENC_PIC_STRUCT ePicStruct,
int8_t *qpDeltaMapArray, uint32_t qpDeltaMapArraySize)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_PIC_PARAMS encPicParams;
memset(&encPicParams, 0, sizeof(encPicParams));
SET_VER(encPicParams, NV_ENC_PIC_PARAMS);
encPicParams.inputBuffer = pEncodeBuffer->stInputBfr.hInputSurface;
encPicParams.bufferFmt = pEncodeBuffer->stInputBfr.bufferFmt;
encPicParams.inputWidth = width;
encPicParams.inputHeight = height;
encPicParams.outputBitstream = pEncodeBuffer->stOutputBfr.hBitstreamBuffer;
encPicParams.completionEvent = nullptr;
encPicParams.inputTimeStamp = m_EncodeIdx;
encPicParams.pictureStruct = ePicStruct;
encPicParams.qpDeltaMap = qpDeltaMapArray;
encPicParams.qpDeltaMapSize = qpDeltaMapArraySize;
if (encPicCommand)
{
if (encPicCommand->bForceIDR)
{
encPicParams.encodePicFlags |= NV_ENC_PIC_FLAG_FORCEIDR;
}
if (encPicCommand->bForceIntraRefresh)
{
if (codecGUID == NV_ENC_CODEC_HEVC_GUID)
{
encPicParams.codecPicParams.hevcPicParams.forceIntraRefreshWithFrameCnt = encPicCommand->intraRefreshDuration;
}
else
{
encPicParams.codecPicParams.h264PicParams.forceIntraRefreshWithFrameCnt = encPicCommand->intraRefreshDuration;
}
}
}
nvStatus = m_pEncodeAPI->nvEncEncodePicture(m_hEncoder, &encPicParams);
if (nvStatus != NV_ENC_SUCCESS && nvStatus != NV_ENC_ERR_NEED_MORE_INPUT)
{
assert(0);
return nvStatus;
}
m_EncodeIdx++;
return NV_ENC_SUCCESS;
}
NVENCSTATUS CNvHWEncoder::NvEncFlushEncoderQueue(void *hEOSEvent)
{
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_PIC_PARAMS encPicParams;
memset(&encPicParams, 0, sizeof(encPicParams));
SET_VER(encPicParams, NV_ENC_PIC_PARAMS);
encPicParams.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
encPicParams.completionEvent = hEOSEvent;
nvStatus = m_pEncodeAPI->nvEncEncodePicture(m_hEncoder, &encPicParams);
if (nvStatus != NV_ENC_SUCCESS)
{
assert(0);
}
return nvStatus;
}
NVENCSTATUS CNvHWEncoder::ParseArguments(EncodeConfig *encodeConfig, int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
if (stricmp(argv[i], "-bmpfilePath") == 0)
{
if (++i >= argc)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
encodeConfig->inputFilePath = argv[i];
}
else if (stricmp(argv[i], "-i") == 0)
{
if (++i >= argc)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
encodeConfig->inputFileName = argv[i];
}
else if (stricmp(argv[i], "-o") == 0)
{
if (++i >= argc)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
encodeConfig->outputFileName = argv[i];
}
else if (stricmp(argv[i], "-size") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->width) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->height) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 2]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-maxSize") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->maxWidth) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->maxHeight) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 2]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-bitrate") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->bitrate) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-vbvMaxBitrate") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->vbvMaxBitrate) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-vbvSize") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->vbvSize) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-fps") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->fps) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-startf") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->startFrameIdx) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-endf") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->endFrameIdx) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-rcmode") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->rcMode) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-goplength") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->gopLength) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-numB") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->numB) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-qp") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->qp) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-i_qfactor") == 0)
{
if (++i >= argc || sscanf(argv[i], "%f", &encodeConfig->i_quant_factor) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-b_qfactor") == 0)
{
if (++i >= argc || sscanf(argv[i], "%f", &encodeConfig->b_quant_factor) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-i_qoffset") == 0)
{
if (++i >= argc || sscanf(argv[i], "%f", &encodeConfig->i_quant_offset) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-b_qoffset") == 0)
{
if (++i >= argc || sscanf(argv[i], "%f", &encodeConfig->b_quant_offset) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-preset") == 0)
{
if (++i >= argc)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
encodeConfig->encoderPreset = argv[i];
}
else if (stricmp(argv[i], "-devicetype") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->deviceType) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-codec") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->codec) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-encCmdFile") == 0)
{
if (++i >= argc)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
encodeConfig->encCmdFileName = argv[i];
}
else if (stricmp(argv[i], "-intraRefresh") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->intraRefreshEnableFlag) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-intraRefreshPeriod") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->intraRefreshPeriod) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-intraRefreshDuration") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->intraRefreshDuration) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-picStruct") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->pictureStruct) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-deviceID") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->deviceID) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-yuv444") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->isYuv444) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-qpDeltaMapFile") == 0)
{
if (++i >= argc)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
encodeConfig->qpDeltaMapFile = argv[i];
}
else if (stricmp(argv[i], "-meonly") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->enableMEOnly) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
if (encodeConfig->enableMEOnly != 1 && encodeConfig->enableMEOnly != 2)
{
PRINTERR("invalid enableMEOnly value = %d (permissive value 1 and 2)\n", encodeConfig->enableMEOnly);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-preloadedFrameCount") == 0)
{
if (++i >= argc || sscanf(argv[i], "%d", &encodeConfig->preloadedFrameCount) != 1)
{
PRINTERR("invalid parameter for %s\n", argv[i - 1]);
return NV_ENC_ERR_INVALID_PARAM;
}
if (encodeConfig->preloadedFrameCount <= 1)
{
PRINTERR("invalid preloadedFrameQueueSize value = %d (permissive value 2 and above)\n", encodeConfig->preloadedFrameCount);
return NV_ENC_ERR_INVALID_PARAM;
}
}
else if (stricmp(argv[i], "-help") == 0)
{
return NV_ENC_ERR_INVALID_PARAM;
}
else
{
PRINTERR("invalid parameter %s\n", argv[i++]);
return NV_ENC_ERR_INVALID_PARAM;
}
}
return NV_ENC_SUCCESS;
}
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "dynlink_cuda_cuda.h" // <cuda.h>
#include "nvEncodeAPI.h"
#include "nvUtils.h"
#define SET_VER(configStruct, type) {configStruct.version = type##_VER;}
#if defined (NV_WINDOWS)
#include "d3d9.h"
#define NVENCAPI __stdcall
#pragma warning(disable : 4996)
#elif defined (NV_UNIX)
#include <dlfcn.h>
#include <string.h>
#define NVENCAPI
#endif
#define DEFAULT_I_QFACTOR -0.8f
#define DEFAULT_B_QFACTOR 1.25f
#define DEFAULT_I_QOFFSET 0.f
#define DEFAULT_B_QOFFSET 1.25f
typedef struct _EncodeConfig
{
int width;
int height;
int maxWidth;
int maxHeight;
int fps;
int bitrate;
int vbvMaxBitrate;
int vbvSize;
int rcMode;
int qp;
float i_quant_factor;
float b_quant_factor;
float i_quant_offset;
float b_quant_offset;
GUID presetGUID;
int codec;
int invalidateRefFramesEnableFlag;
int intraRefreshEnableFlag;
int intraRefreshPeriod;
int intraRefreshDuration;
int deviceType;
int startFrameIdx;
int endFrameIdx;
int gopLength;
int numB;
int pictureStruct;
int deviceID;
int isYuv444;
char *qpDeltaMapFile;
char* inputFileName;
char* outputFileName;
char* encoderPreset;
char* inputFilePath;
char *encCmdFileName;
int enableMEOnly;
int preloadedFrameCount;
}EncodeConfig;
typedef struct _EncodeInputBuffer
{
unsigned int dwWidth;
unsigned int dwHeight;
CUdeviceptr pNV12devPtr;
uint32_t uNV12Stride;
CUdeviceptr pNV12TempdevPtr;
uint32_t uNV12TempStride;
NV_ENC_INPUT_PTR hInputSurface;
NV_ENC_BUFFER_FORMAT bufferFmt;
}EncodeInputBuffer;
typedef struct _EncodeOutputBuffer
{
unsigned int dwBitstreamBufferSize;
NV_ENC_OUTPUT_PTR hBitstreamBuffer;
bool bEOSFlag;
}EncodeOutputBuffer;
typedef struct _EncodeBuffer
{
EncodeOutputBuffer stOutputBfr;
EncodeInputBuffer stInputBfr;
}EncodeBuffer;
typedef struct _NvEncPictureCommand
{
bool bResolutionChangePending;
bool bBitrateChangePending;
bool bForceIDR;
bool bForceIntraRefresh;
bool bInvalidateRefFrames;
uint32_t newWidth;
uint32_t newHeight;
uint32_t newBitrate;
uint32_t newVBVSize;
uint32_t intraRefreshDuration;
uint32_t numRefFramesToInvalidate;
uint32_t refFrameNumbers[16];
}NvEncPictureCommand;
enum
{
NV_ENC_H264 = 0,
NV_ENC_HEVC = 1,
};
struct MEOnlyConfig
{
unsigned char *yuv[2][3];
unsigned int stride[3];
unsigned int width;
unsigned int height;
unsigned int inputFrameIndex;
unsigned int referenceFrameIndex;
};
class CNvHWEncoder
{
public:
uint32_t m_EncodeIdx;
uint32_t m_uMaxWidth;
uint32_t m_uMaxHeight;
uint32_t m_uCurWidth;
uint32_t m_uCurHeight;
protected:
bool m_bEncoderInitialized;
GUID codecGUID;
NV_ENCODE_API_FUNCTION_LIST* m_pEncodeAPI;
HINSTANCE m_hinstLib;
void *m_hEncoder;
NV_ENC_INITIALIZE_PARAMS m_stCreateEncodeParams;
NV_ENC_CONFIG m_stEncodeConfig;
public:
NVENCSTATUS NvEncOpenEncodeSession(void* device, uint32_t deviceType);
NVENCSTATUS NvEncGetEncodeGUIDCount(uint32_t* encodeGUIDCount);
NVENCSTATUS NvEncGetEncodeProfileGUIDCount(GUID encodeGUID, uint32_t* encodeProfileGUIDCount);
NVENCSTATUS NvEncGetEncodeProfileGUIDs(GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount);
NVENCSTATUS NvEncGetEncodeGUIDs(GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount);
NVENCSTATUS NvEncGetInputFormatCount(GUID encodeGUID, uint32_t* inputFmtCount);
NVENCSTATUS NvEncGetInputFormats(GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount);
NVENCSTATUS NvEncGetEncodeCaps(GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal);
NVENCSTATUS NvEncGetEncodePresetCount(GUID encodeGUID, uint32_t* encodePresetGUIDCount);
NVENCSTATUS NvEncGetEncodePresetGUIDs(GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount);
NVENCSTATUS NvEncGetEncodePresetConfig(GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig);
NVENCSTATUS NvEncCreateInputBuffer(uint32_t width, uint32_t height, void** inputBuffer, uint32_t isYuv444);
NVENCSTATUS NvEncDestroyInputBuffer(NV_ENC_INPUT_PTR inputBuffer);
NVENCSTATUS NvEncCreateBitstreamBuffer(uint32_t size, void** bitstreamBuffer);
NVENCSTATUS NvEncDestroyBitstreamBuffer(NV_ENC_OUTPUT_PTR bitstreamBuffer);
NVENCSTATUS NvEncCreateMVBuffer(uint32_t size, void** bitstreamBuffer);
NVENCSTATUS NvEncDestroyMVBuffer(NV_ENC_OUTPUT_PTR bitstreamBuffer);
NVENCSTATUS NvRunMotionEstimationOnly(EncodeBuffer *pEncodeBuffer[2], MEOnlyConfig *pMEOnly);
NVENCSTATUS NvEncLockBitstream(NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams);
NVENCSTATUS NvEncUnlockBitstream(NV_ENC_OUTPUT_PTR bitstreamBuffer);
NVENCSTATUS NvEncLockInputBuffer(void* inputBuffer, void** bufferDataPtr, uint32_t* pitch);
NVENCSTATUS NvEncUnlockInputBuffer(NV_ENC_INPUT_PTR inputBuffer);
NVENCSTATUS NvEncGetEncodeStats(NV_ENC_STAT* encodeStats);
NVENCSTATUS NvEncGetSequenceParams(NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload);
NVENCSTATUS NvEncDestroyEncoder();
NVENCSTATUS NvEncInvalidateRefFrames(const NvEncPictureCommand *pEncPicCommand);
NVENCSTATUS NvEncOpenEncodeSessionEx(void* device, NV_ENC_DEVICE_TYPE deviceType);
NVENCSTATUS NvEncReconfigureEncoder(const NvEncPictureCommand *pEncPicCommand);
NVENCSTATUS NvEncFlushEncoderQueue(void *hEOSEvent);
CNvHWEncoder();
virtual ~CNvHWEncoder();
NVENCSTATUS Initialize(void* device, NV_ENC_DEVICE_TYPE deviceType);
NVENCSTATUS Deinitialize();
NVENCSTATUS NvEncEncodeFrame(EncodeBuffer *pEncodeBuffer, NvEncPictureCommand *encPicCommand,
uint32_t width, uint32_t height,
NV_ENC_PIC_STRUCT ePicStruct = NV_ENC_PIC_STRUCT_FRAME,
int8_t *qpDeltaMapArray = NULL, uint32_t qpDeltaMapArraySize = 0);
NVENCSTATUS CreateEncoder(const EncodeConfig *pEncCfg);
GUID GetPresetGUID(char* encoderPreset, int codec);
NVENCSTATUS ProcessOutput(const EncodeBuffer *pEncodeBuffer);
NVENCSTATUS FlushEncoder();
NVENCSTATUS ValidateEncodeGUID(GUID inputCodecGuid);
NVENCSTATUS ValidatePresetGUID(GUID presetCodecGuid, GUID inputCodecGuid);
static NVENCSTATUS ParseArguments(EncodeConfig *encodeConfig, int argc, char *argv[]);
};
typedef NVENCSTATUS (NVENCAPI *MYPROC)(NV_ENCODE_API_FUNCTION_LIST*);
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#ifndef NVUTILS_H
#define NVUTILS_H
#include "nvCPUOPSys.h"
#if defined (NV_WINDOWS)
#include <windows.h>
#elif defined NV_UNIX
#include <sys/time.h>
#include <limits.h>
#define FALSE 0
#define TRUE 1
#define S_OK 0
#define INFINITE UINT_MAX
#define stricmp strcasecmp
#define FILE_BEGIN SEEK_SET
#define INVALID_SET_FILE_POINTER (-1)
#define INVALID_HANDLE_VALUE ((void *)(-1))
typedef void* HANDLE;
typedef void* HINSTANCE;
typedef unsigned long DWORD, *LPWORD;
typedef DWORD FILE_SIZE;
typedef DWORD HRESULT;
#endif
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define FABS(a) ((a) >= 0 ? (a) : -(a))
inline bool NvSleep(unsigned int mSec)
{
#if defined (NV_WINDOWS)
Sleep(mSec);
#elif defined NV_UNIX
usleep(mSec * 1000);
#else
#error NvSleep function unknown for this platform.
#endif
return true;
}
inline bool NvQueryPerformanceFrequency(unsigned long long *freq)
{
*freq = 0;
#if defined (NV_WINDOWS)
LARGE_INTEGER lfreq;
if (!QueryPerformanceFrequency(&lfreq)) {
return false;
}
*freq = lfreq.QuadPart;
#elif defined NV_UNIX
// We use system's gettimeofday() to return timer ticks in uSec
*freq = 1000000000;
#else
#error NvQueryPerformanceFrequency function not defined for this platform.
#endif
return true;
}
#define SEC_TO_NANO_ULL(sec) ((unsigned long long)sec * 1000000000)
#define MICRO_TO_NANO_ULL(sec) ((unsigned long long)sec * 1000)
inline bool NvQueryPerformanceCounter(unsigned long long *counter)
{
*counter = 0;
#if defined (NV_WINDOWS)
LARGE_INTEGER lcounter;
if (!QueryPerformanceCounter(&lcounter)) {
return false;
}
*counter = lcounter.QuadPart;
#elif defined NV_UNIX
struct timeval tv;
int ret;
ret = gettimeofday(&tv, NULL);
if (ret != 0) {
return false;
}
*counter = SEC_TO_NANO_ULL(tv.tv_sec) + MICRO_TO_NANO_ULL(tv.tv_usec);
#else
#error NvQueryPerformanceCounter function not defined for this platform.
#endif
return true;
}
#if defined NV_UNIX
__inline bool operator==(const GUID &guid1, const GUID &guid2)
{
if (guid1.Data1 == guid2.Data1 &&
guid1.Data2 == guid2.Data2 &&
guid1.Data3 == guid2.Data3 &&
guid1.Data4[0] == guid2.Data4[0] &&
guid1.Data4[1] == guid2.Data4[1] &&
guid1.Data4[2] == guid2.Data4[2] &&
guid1.Data4[3] == guid2.Data4[3] &&
guid1.Data4[4] == guid2.Data4[4] &&
guid1.Data4[5] == guid2.Data4[5] &&
guid1.Data4[6] == guid2.Data4[6] &&
guid1.Data4[7] == guid2.Data4[7])
{
return true;
}
return false;
}
__inline bool operator!=(const GUID &guid1, const GUID &guid2)
{
return !(guid1 == guid2);
}
#endif
#endif
#define PRINTERR(message, ...) \
fprintf(stderr, "%s line %d: " message, __FILE__, __LINE__, ##__VA_ARGS__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment