### Install ffnvcodec (Make) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Installs the ffnvcodec library using the make build tool. This command is executed from the nv-codec-headers directory and specifies the installation prefix. ```bash make install PREFIX=/usr ``` -------------------------------- ### Install FFmpeg Libraries (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Installs the compiled FFmpeg libraries and binaries on the system after a successful build. This makes the FFmpeg executable with NVIDIA support available for use. ```shell sudo make install ``` -------------------------------- ### Install NVIDIA Codec Headers (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Installs the cloned NVIDIA codec headers after navigating into the directory. This makes the headers available for the FFmpeg compilation process on Linux. ```shell cd nv-codec-headers && sudo make install && cd – ``` -------------------------------- ### Install FFmpeg Build Dependencies (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Installs essential packages required for compiling FFmpeg on a Linux system. This includes build tools, libraries, and utilities necessary for the compilation process. ```shell sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev ``` -------------------------------- ### Install MSYS2 Packages (Pacman) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Installs necessary development packages (diffutils, make, pkg-config, yasm) using the pacman package manager within the MinGW64 environment. These are required for building FFmpeg. ```bash pacman -S diffutils make pkg-config yasm ``` -------------------------------- ### Selecting Encoder Profile Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index This section explains how to select a specific encoding profile for different scenarios like playback on iPhone/iPod or Blu-ray disc authoring. It involves querying the count and list of supported encoder profile GUIDs. ```APIDOC ## Selecting Encoder Profile ### Description Allows the client to specify an encoding profile for specific scenarios, such as iPhone/iPod playback or Blu-ray disc authoring. The process involves querying and selecting from a list of supported encoder profile GUIDs. ### Steps 1. Call `NvEncGetEncodeProfileGUIDCount` to get the number of supported Encoder GUIDs. 2. Allocate a buffer of sufficient size to hold the supported Encode Profile GUIDs. 3. Call `NvEncGetEncodeProfileGUIDs` to populate the list of GUIDs. 4. Select the profile GUID that best matches the requirement. ### Functions - `NvEncGetEncodeProfileGUIDCount()` - `NvEncGetEncodeProfileGUIDs()` ``` -------------------------------- ### Getting Supported Input Formats Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index This section describes how to retrieve a list of supported input formats for the NVENC API. The NVENC API accepts input frames in various formats like YUV and RGB, enumerated in `NV_ENC_BUFFER_FORMAT`. ```APIDOC ## Getting Supported List of Input Formats ### Description Retrieves the list of input formats supported by the NVENC API, which accepts frames in formats like YUV and RGB as enumerated in `NV_ENC_BUFFER_FORMAT`. ### Steps 1. Call `NvEncGetInputFormatCount` to get the number of supported input formats. 2. Allocate a buffer to hold the list of supported input buffer formats (`NV_ENC_BUFFER_FORMAT`). 3. Call `NvEncGetInputFormats` to retrieve the supported input buffer formats. 4. Select a format from the retrieved list for creating input buffers. ### Functions - `NvEncGetInputFormatCount()` - `NvEncGetInputFormats()` ``` -------------------------------- ### Video Encoding (720p YUV to MP4 H264) using FFmpeg NVENC Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index This FFmpeg command encodes a 720p YUV input file into an MP4 container with H.264 video using the NVENC hardware encoder. It's a basic example for video encoding quality testing. ```bash ffmpeg -y -vsync 0 –s 1280x720 –i input.yuv -c:v h264_nvenc output.mp4 ``` -------------------------------- ### FFmpeg Lookahead Configuration for Video Encoding Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index This example shows how to enable and configure the lookahead feature in FFmpeg for improved video encoder rate-control accuracy. The `-rc-lookahead N` option buffers frames to estimate complexity and allocate bits proportionally, enhancing visual quality. 'N' represents the number of frames to look ahead. ```bash # Example: Enable lookahead with 20 frames ffmpeg ... -rc-lookahead 20 ... ``` -------------------------------- ### FFmpeg Low Latency High Performance Transcoding (NVENC H.264) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index This command line is similar to the high-quality example but optimized for higher performance by using a faster NVENC preset ('p2'). It performs low-latency H.264 transcoding with CUDA hardware acceleration, maintaining input resolution and a 5Mbps target bitrate. Audio is copied directly from the input. ```bash ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:a copy -c:v h264_nvenc -preset p2 -tune ll -b:v 5M -bufsize 5M -maxrate 10M -qmin 0 -g 250 -bf 3 -b_ref_mode middle -temporal-aq 1 -rc-lookahead 20 -i_qfactor 0.75 -b_qfactor 1.1 output.mp4 ``` -------------------------------- ### Initializing the Hardware Encoder Session Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index This section details the process of initializing the hardware encoder session using the `NvEncInitializeEncoder` function with a valid encoder configuration. ```APIDOC ## Initializing the Hardware Encoder Session ### Description Initializes the hardware encoder session by calling `NvEncInitializeEncoder` with a valid encoder configuration specified through `NV_ENC_INITIALIZE_PARAMS` and a valid encoder handle. ### Function - `NvEncInitializeEncoder()` ### Structures - `NV_ENC_INITIALIZE_PARAMS` ``` -------------------------------- ### Session Parameters for Encoder Initialization Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index Details the common parameters available in the `NV_ENC_INITIALIZE_PARAMS` structure, such as input format, output dimensions, frame rate, and bitrate, which are essential for initializing the encode session. ```APIDOC ## Session Parameters for Encoder Initialization ### Description Defines common parameters for the encode session, including input format, output dimensions, display aspect ratio, frame rate, and average bitrate. These parameters are set using the `NV_ENC_INITIALIZE_PARAMS` structure. ### Structure - `NV_ENC_INITIALIZE_PARAMS` ### Required Members for Initialization - `NV_ENC_INITIALIZE_PARAMS::encodeGUID`: Select a suitable codec GUID. - `NV_ENC_INITIALIZE_PARAMS::encodeWidth`: Specify the desired width of the encoded video. - `NV_ENC_INITIALIZE_PARAMS::encodeHeight`: Specify the desired height of the encoded video. ### Optional Members - `NV_ENC_INITALIZE_PARAMS::reportSliceOffsets`: Enable reporting of slice offsets. Requires `enableEncodeAsync` to be 0 and does not work with MB-based and byte-based slicing on Kepler GPUs. ### Related Structures - `NV_ENC_CONFIG` - `NV_ENC_CONFIG_H264` - `NV_ENC_CONFIG_HEVC` ``` -------------------------------- ### Setting Encode Session Attributes Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index Explains how to finalize and initialize encode settings for a session using the `NvEncInitializeEncoder` function and the `NV_ENC_CONFIG` structure. ```APIDOC ## Setting Encode Session Attributes Once encode settings are finalized, populate a `NV_ENC_CONFIG` structure and use it as input to `NvEncInitializeEncoder` to freeze settings for the current encode session. ### Initialization - **Structure**: `NV_ENC_CONFIG` - **Function**: `NvEncInitializeEncoder` ### Changeable Settings Some settings, such as rate control mode, average bitrate, and resolution, can be changed on-the-fly. ### Required Initialization Parameters - Explicitly specify the following while initializing the Encode Session: - [List of required parameters to be extracted from context if available, otherwise leave general statement] ``` -------------------------------- ### Add Build Paths (Bash Environment) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Exports the necessary paths to the system's PATH environment variable. This includes the Visual Studio build tools and the NVIDIA CUDA toolkit's bin directory, allowing the build process to find required executables. ```bash export PATH="/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/BIN/amd64/":$PATH export PATH="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/bin/":$PATH ``` -------------------------------- ### Querying Encoder Capabilities Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index This section explains how to query the capabilities of the underlying hardware encoder on the system. It's a good practice to query for feature support before using a feature. ```APIDOC ## Querying Encoder Capabilities ### Description Allows applications to dynamically determine the capabilities of the underlying hardware encoder. It is recommended to query for feature support before utilizing it. ### Steps 1. Specify the capability attribute to be queried in `NV_ENC_CAPS_PARAM::capsToQuery` (a member of the `NV_ENC_CAPS` enum). 2. Call `NvEncGetEncodeCaps` to determine support for the required attribute. ### Functions - `NvEncGetEncodeCaps()` ### Structures - `NV_ENC_CAPS_PARAM` ### Enums - `NV_ENC_CAPS` (for interpretation of capability attributes) ``` -------------------------------- ### Configure FFmpeg Build (Bash) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Configures the FFmpeg build with specific options for NVIDIA GPU acceleration, including enabling CUDA SDK, libnpp, and specifying the toolchain as MSVC. It also points to the local NV SDK for headers and libraries. ```bash ./configure --enable-nonfree –disable-shared --enable-cuda-sdk --enable-libnpp –-toolchain=msvc --extra-cflags=-I../nv_sdk --extra-ldflags=-libpath:../nv_sdk ``` -------------------------------- ### C++ NVDEC Decoder Creation with Scaling Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvdec-video-decoder-api-prog-guide/index Demonstrates setting the target width and height for scaling during NVDEC decoder creation. This is crucial for outputting video at a desired resolution different from the source. The code snippet initializes `CUVIDDECODECREATEINFO` and sets `ulTargetWidth` and `ulTargetHeight` before calling `cuvidCreateDecoder`. ```cpp // Scaling. Source size is 1280x960. Scale to 1920x1080. CUresult rResult; unsigned int uScaleW, uScaleH; uScaleW = 1920; uScaleH = 1080; ... CUVIDDECODECREATEINFO stDecodeCreateInfo; memset(&stDecodeCreateInfo, 0, sizeof(CUVIDDECODECREATEINFO)); ... // Setup the remaining structure members stDecodeCreateInfo.ulTargetWidth = uScaleWidth; stDecodeCreateInfo.ulTargetHeight = uScaleHeight; rResult = cuvidCreateDecoder(&hDecoder, &stDecodeCreateInfo); ... ``` -------------------------------- ### C++ NVDEC Decoder Creation with Cropping Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvdec-video-decoder-api-prog-guide/index Illustrates setting cropping parameters for NVDEC decoder creation. This allows for removing unwanted borders or specific regions from the video frames. The code snippet shows the initialization of cropping variables before they would be applied to the `CUVIDDECODECREATEINFO` structure (though the full structure application is not shown in this snippet). ```cpp // Cropping. Source size is 1280x960 CUresult rResult; unsigned int uCropL, uCropR, uCropT, uCropB; uCropL = 30; uCropR = 700; uCropT = 20; ``` -------------------------------- ### Create NVIDIA Video Decoder with Cropping Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvdec-video-decoder-api-prog-guide/index Demonstrates the creation of a video decoder using CUVIDDECODECREATEINFO, specifying cropping parameters for the display area. This is essential for adjusting the visible portion of the decoded video. ```c unsigned int uCropL, uCropR, uCropT, uCropB; // ... setup uCropL, uCropR, uCropT, uCropB values ... CUVIDDECODECREATEINFO stDecodeCreateInfo; memset(&stDecodeCreateInfo, 0, sizeof(CUVIDDECODECREATEINFO)); // Setup the remaining structure members stDecodeCreateInfo.display_area.left = uCropL; stDecodeCreateInfo.display_area.right = uCropR; stDecodeCreateInfo.display_area.top = uCropT; stDecodeCreateInfo.display_area.bottom = uCropB; CUresult rResult = cuvidCreateDecoder(&hDecoder, &stDecodeCreateInfo); ``` -------------------------------- ### Query NVDEC Capabilities with C++ Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvdec-video-decoder-api-prog-guide/index This snippet demonstrates how to query the capabilities of the NVDEC hardware decoder using the `cuvidGetDecoderCaps()` function in C++. It shows how to initialize the `CUVIDDECODECAPS` structure with desired codec, chroma format, and bit depth, and then call the API to retrieve decoder information. ```cpp CUVIDDECODECAPS decodeCaps = {}; // set IN params for decodeCaps decodeCaps.eCodecType = cudaVideoCodec_HEVC; // HEVC decodeCaps.eChromaFormat = cudaVideoChromaFormat_420; // YUV 4:2:0 decodeCaps.nBitDepthMinus8 = 2; // 10 bit result = cuvidGetDecoderCaps(&decodeCaps); ``` -------------------------------- ### 1:N Transcode with Scaling (SW Decode->HW Scaling->HW Encode) using FFmpeg Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index This FFmpeg command demonstrates a 1:N transcoding scenario where input videos are decoded in software, scaled using hardware acceleration (NPP), and then encoded to different resolutions and bitrates using NVENC. It handles multiple input files and outputs simultaneously. ```bash ffmpeg -y -init_hw_device cuda=foo:bar -filter_hw_device foo \ -i input1.mp4 -i input2.mp4 \ -map 0:0 -vf hwupload,scale_npp=640:480 –c:v h264_nvenc -b:v 1M \ output11.mp4 \ -map 0:0 -vf hwupload,scale_npp=320:240 –c:v h264_nvenc -b:v 500k \ output12.mp4 \ -map 1:0 -vf hwupload,scale_npp=1280:720 –c:v h264_nvenc -b:v 2M \ output21.mp4 \ -map 1:0 -vf hwupload,scale_npp=640:480 –c:v h264_nvenc -b:v 1M \ output22.mp4 ``` -------------------------------- ### Clone FFmpeg Repository (Git) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Clones the main FFmpeg repository from its public GIT server. This is the core FFmpeg library that will be compiled with NVIDIA support. ```bash git clone https://git.ffmpeg.org/ffmpeg.git ``` -------------------------------- ### Configure FFmpeg Build with NVIDIA Support (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Configures the FFmpeg build process to enable NVIDIA-specific features using CUDA. It specifies include and library paths for the CUDA toolkit, essential for GPU acceleration. ```shell ./configure --enable-nonfree -–enable-cuda-sdk –enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 ``` -------------------------------- ### Rate Control Modes Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/nvenc-video-encoder-api-prog-guide/index This section describes the various rate control modes supported by NVENC, including Constant Bitrate (CBR), Variable Bitrate (VBR), Constant QP, and Target Quality. It details the parameters and behaviors associated with each mode. ```APIDOC ## Rate Control NVENC supports several rate control modes and provides control over various parameters related to the rate control algorithm via structure `NV_ENC_INITIALIZE_PARAMS::encodeConfig::rcParams`. ### Constant Bitrate (CBR) - **Description**: Specified by setting `rateControlMode` to `NV_ENC_PARAMS_RC_CBR`. Only `averageBitRate` is required. - **Control**: `NV_ENC_RC_PARAMS::lowDelayKeyFrameScale` for I/P frame ratio. `NV_ENC_CONFIG_H264/NV_ENC_CONFIG_HEVC::enableFillerDataInsertion` for strict bitrate adherence. ### Variable Bitrate (VBR) - **Description**: Specified by setting `rateControlMode` to `NV_ENC_PARAMS_RC_VBR`. Aims to conform to `averageBitRate` while not exceeding `maxBitRate`. - **Parameters**: `averageBitRate` is required. `maxBitRate` is recommended for better control. ### Constant QP - **Description**: Specified by setting `rateControlMode` to `NV_ENC_PARAMS_RC_CONSTQP`. The entire frame is encoded using QP specified in `NV_ENC_RC_PARAMS::constQP`. ### Target Quality - **Description**: Specified by setting `rateControlMode` to VBR and desired target quality in `targetQuality`. Aims to maintain constant quality per frame. - **Parameters**: `targetQuality` (0-51). `maxBitRate` can be specified as an upper bound. ``` -------------------------------- ### FFmpeg Temporal Adaptive Quantization (AQ) Configuration Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index This command shows how to enable Temporal Adaptive Quantization (AQ) in FFmpeg. Temporal AQ adjusts QP based on temporal characteristics of the sequence, improving quality for regions with low motion but high detail. It is enabled using the `-temporal_aq 1` option. Note that this may cause fluctuations in frame bitrates within a GOP. ```bash # Example: Enable temporal AQ ffmpeg ... -temporal_aq 1 ... ``` -------------------------------- ### FFmpeg Spatial Adaptive Quantization (AQ) Configuration Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index This command demonstrates enabling Spatial Adaptive Quantization (AQ) in FFmpeg, which adjusts QP based on spatial frame characteristics to improve perceptual quality. It uses `-spatial-aq 1` to enable the feature and `-aq-strength` to control the QP variations. The strength parameter ranges from 1 to 15. ```bash # Example: Enable spatial AQ with strength 8 ffmpeg ... -spatial-aq 1 -aq-strength 8 ... ``` -------------------------------- ### Clone NVIDIA Codec Headers for FFmpeg (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Clones the necessary NVIDIA codec headers repository from the VideoLAN git server. This is a prerequisite for compiling FFmpeg with NVIDIA GPU acceleration on Linux. ```shell git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git ``` -------------------------------- ### Clone FFmpeg Source Code Repository (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Clones the official FFmpeg source code repository. This provides the base code for compiling FFmpeg with NVIDIA GPU acceleration support on Linux. ```shell git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/ ``` -------------------------------- ### Compile FFmpeg with NVIDIA Support (Linux) Source: https://docs.nvidia.com/video-technologies/video-codec-sdk/11.0/ffmpeg-with-nvidia-gpu/index Compiles the FFmpeg source code with the configured settings, including NVIDIA GPU acceleration support. The '-j 8' flag utilizes 8 parallel jobs for faster compilation. ```shell make -j 8 ```