CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-bytedeco--ffmpeg

JavaCPP bindings for FFmpeg multimedia framework providing comprehensive Java access to audio/video encoding, decoding, filtering, and format conversion

Pending
Overview
Eval results
Files

hardware-acceleration.mddocs/

Hardware Acceleration

GPU and specialized hardware acceleration support for high-performance video encoding, decoding, and processing operations with CUDA, VAAPI, DXVA2, VideoToolbox, and other acceleration APIs.

Capabilities

Hardware Device Management

Device Type Discovery

/**
 * Find hardware device type by name
 * @param name Device type name (e.g., "cuda", "vaapi", "dxva2")
 * @return Hardware device type constant or AV_HWDEVICE_TYPE_NONE if not found
 */
int av_hwdevice_find_type_by_name(String name);

/**
 * Get hardware device type name
 * @param type Hardware device type constant
 * @return Device type name string
 */
BytePointer av_hwdevice_get_type_name(int type);

/**
 * Iterate through available hardware device types
 * @param prev Previous device type (use AV_HWDEVICE_TYPE_NONE to start)
 * @return Next available device type or AV_HWDEVICE_TYPE_NONE when done
 */
int av_hwdevice_iterate_types(int prev);

Usage Example:

import org.bytedeco.ffmpeg.avutil.*;
import static org.bytedeco.ffmpeg.global.avutil.*;

// Find CUDA device type
int cudaType = av_hwdevice_find_type_by_name("cuda");
if (cudaType != AV_HWDEVICE_TYPE_NONE) {
    System.out.println("CUDA acceleration available");
}

// List all available hardware acceleration types
int deviceType = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
while (deviceType != AV_HWDEVICE_TYPE_NONE) {
    BytePointer typeName = av_hwdevice_get_type_name(deviceType);
    System.out.println("Available: " + typeName.getString());
    deviceType = av_hwdevice_iterate_types(deviceType);
}

Hardware Device Context Management

Creating Device Contexts

/**
 * Allocate hardware device context for specified type
 * @param type Hardware device type
 * @return Reference to device context buffer or null on failure
 */
AVBufferRef av_hwdevice_ctx_alloc(int type);

/**
 * Initialize hardware device context
 * @param ref Reference to allocated device context
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwdevice_ctx_init(AVBufferRef ref);

/**
 * Create hardware device context from device reference
 * @param device_ctx Pointer to store created context reference
 * @param type Hardware device type
 * @param device Device identifier (can be null for default)
 * @param opts Options dictionary for device creation
 * @param flags Creation flags (reserved, pass 0)
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwdevice_ctx_create(@ByPtrPtr AVBufferRef device_ctx, int type,
                          String device, AVDictionary opts, int flags);

Usage Example:

import org.bytedeco.ffmpeg.avutil.*;
import static org.bytedeco.ffmpeg.global.avutil.*;

// Create CUDA device context
AVBufferRef cudaDeviceRef = new AVBufferRef(null);
int result = av_hwdevice_ctx_create(cudaDeviceRef, AV_HWDEVICE_TYPE_CUDA, null, null, 0);

if (result >= 0) {
    System.out.println("CUDA device context created successfully");
    
    // Use device context for hardware acceleration
    try {
        // Hardware operations here
    } finally {
        av_buffer_unref(cudaDeviceRef); // Clean up when done
    }
} else {
    System.err.println("Failed to create CUDA context: " + result);
}

Hardware Frames Management

Frame Context Operations

/**
 * Allocate hardware frames context
 * @param device_ref Reference to hardware device context
 * @return Reference to frames context buffer or null on failure
 */
AVBufferRef av_hwframe_ctx_alloc(AVBufferRef device_ref);

/**
 * Initialize hardware frames context
 * @param ref Reference to allocated frames context
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwframe_ctx_init(AVBufferRef ref);

/**
 * Get hardware frames buffer
 * @param frame AVFrame to receive hardware buffer
 * @param hwframe_ctx Hardware frames context reference
 * @param flags Allocation flags (reserved, pass 0)
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwframe_get_buffer(AVFrame frame, AVBufferRef hwframe_ctx, int flags);

Hardware Frame Transfer Operations

Data Transfer Methods

/**
 * Transfer data from hardware frame to software frame
 * @param dst Destination software frame
 * @param src Source hardware frame
 * @param flags Transfer flags (reserved, pass 0)
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwframe_transfer_data(AVFrame dst, AVFrame src, int flags);

/**
 * Transfer data from software frame to hardware frame
 * @param dst Destination hardware frame
 * @param src Source software frame
 * @param flags Transfer flags (reserved, pass 0)
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwframe_transfer_data(AVFrame dst, AVFrame src, int flags);

/**
 * Map hardware frame to accessible memory
 * @param dst Destination frame for mapped data
 * @param src Source hardware frame
 * @param flags Mapping flags (AV_HWFRAME_MAP_*)
 * @return 0 on success, negative AVERROR on failure
 */
int av_hwframe_map(AVFrame dst, AVFrame src, int flags);

Usage Example:

// Transfer hardware frame to software for CPU processing
AVFrame hwFrame = /* hardware decoded frame */;
AVFrame swFrame = av_frame_alloc();

// Configure software frame
swFrame.format(AV_PIX_FMT_YUV420P);
swFrame.width(hwFrame.width());
swFrame.height(hwFrame.height());
av_frame_get_buffer(swFrame, 32);

// Transfer from hardware to software
int result = av_hwframe_transfer_data(swFrame, hwFrame, 0);
if (result >= 0) {
    // Process software frame on CPU
    System.out.println("Frame transferred to CPU memory");
} else {
    System.err.println("Transfer failed: " + result);
}

av_frame_free(swFrame);

Hardware Device Types

Supported Acceleration APIs

// Hardware device type constants
int AV_HWDEVICE_TYPE_NONE = 0;        // No hardware acceleration
int AV_HWDEVICE_TYPE_VDPAU = 1;       // NVIDIA VDPAU (Linux)
int AV_HWDEVICE_TYPE_CUDA = 2;        // NVIDIA CUDA
int AV_HWDEVICE_TYPE_VAAPI = 3;       // Intel/AMD VA-API (Linux)
int AV_HWDEVICE_TYPE_DXVA2 = 4;       // Microsoft DirectX Video Acceleration 2
int AV_HWDEVICE_TYPE_QSV = 5;         // Intel Quick Sync Video
int AV_HWDEVICE_TYPE_VIDEOTOOLBOX = 6; // Apple VideoToolbox (macOS/iOS)
int AV_HWDEVICE_TYPE_D3D11VA = 7;     // Direct3D 11 Video Acceleration
int AV_HWDEVICE_TYPE_DRM = 8;         // Direct Rendering Manager (Linux)
int AV_HWDEVICE_TYPE_OPENCL = 9;      // OpenCL acceleration
int AV_HWDEVICE_TYPE_MEDIACODEC = 10; // Android MediaCodec
int AV_HWDEVICE_TYPE_VULKAN = 11;     // Vulkan API

Platform-Specific Acceleration

PlatformRecommended TypesDescription
WindowsDXVA2, D3D11VA, CUDADirectX-based or NVIDIA GPU
LinuxVAAPI, VDPAU, CUDAIntel/AMD integrated or NVIDIA
macOSVIDEOTOOLBOXApple hardware acceleration
AndroidMEDIACODECAndroid hardware codecs

Codec Hardware Acceleration Setup

Hardware Decoder Configuration

/**
 * Get codec hardware configuration at specified index
 * @param codec Target codec
 * @param index Configuration index (0-based)
 * @return Hardware configuration or null if not available
 */
AVCodecHWConfig avcodec_get_hw_config(AVCodec codec, int index);

Usage Example:

import org.bytedeco.ffmpeg.avcodec.*;
import static org.bytedeco.ffmpeg.global.avcodec.*;

// Find H.264 decoder with hardware support
AVCodec h264Decoder = avcodec_find_decoder(AV_CODEC_ID_H264);

// Check hardware configurations
AVCodecHWConfig hwConfig;
int configIndex = 0;
while ((hwConfig = avcodec_get_hw_config(h264Decoder, configIndex)) != null) {
    int deviceType = hwConfig.device_type();
    int pixelFormat = hwConfig.pix_fmt();
    
    System.out.println("Hardware config " + configIndex + ":");
    System.out.println("  Device type: " + av_hwdevice_get_type_name(deviceType).getString());
    System.out.println("  Pixel format: " + pixelFormat);
    
    configIndex++;
}

Setting Up Hardware Decoding

import org.bytedeco.ffmpeg.avcodec.*;
import org.bytedeco.ffmpeg.avutil.*;
import static org.bytedeco.ffmpeg.global.avcodec.*;
import static org.bytedeco.ffmpeg.global.avutil.*;

// 1. Create hardware device context
AVBufferRef hwDeviceCtx = new AVBufferRef(null);
int result = av_hwdevice_ctx_create(hwDeviceCtx, AV_HWDEVICE_TYPE_CUDA, null, null, 0);
if (result < 0) {
    throw new RuntimeException("Failed to create hardware device context");
}

// 2. Set up codec context
AVCodec decoder = avcodec_find_decoder(AV_CODEC_ID_H264);
AVCodecContext codecCtx = avcodec_alloc_context3(decoder);

// 3. Configure hardware acceleration
codecCtx.hw_device_ctx(av_buffer_ref(hwDeviceCtx));

// 4. Open decoder with hardware acceleration
result = avcodec_open2(codecCtx, decoder, (PointerPointer)null);
if (result < 0) {
    throw new RuntimeException("Failed to open hardware decoder");
}

try {
    // Decode frames - output will be in GPU memory
    AVFrame hwFrame = av_frame_alloc();
    
    // Send packet for hardware decoding
    avcodec_send_packet(codecCtx, packet);
    
    // Receive hardware-decoded frame
    while (avcodec_receive_frame(codecCtx, hwFrame) >= 0) {
        // hwFrame contains GPU-accelerated decoded data
        // Transfer to CPU if needed for processing
        AVFrame swFrame = av_frame_alloc();
        av_hwframe_transfer_data(swFrame, hwFrame, 0);
        
        // Process swFrame on CPU or keep hwFrame on GPU
        
        av_frame_free(swFrame);
    }
    
    av_frame_free(hwFrame);
} finally {
    avcodec_free_context(codecCtx);
    av_buffer_unref(hwDeviceCtx);
}

Frame Mapping Operations

Memory Access Flags

// Frame mapping flags for av_hwframe_map()
int AV_HWFRAME_MAP_READ = 1 << 0;      // Map for reading
int AV_HWFRAME_MAP_WRITE = 1 << 1;     // Map for writing
int AV_HWFRAME_MAP_OVERWRITE = 1 << 2; // Overwrite existing data
int AV_HWFRAME_MAP_DIRECT = 1 << 3;    // Direct mapping (avoid copies)

Usage Example:

// Map hardware frame for read access
AVFrame mappedFrame = av_frame_alloc();
int result = av_hwframe_map(mappedFrame, hwFrame, AV_HWFRAME_MAP_READ);

if (result >= 0) {
    // mappedFrame provides CPU-accessible pointers to GPU data
    // Process mapped data without full transfer
    
    av_frame_unref(mappedFrame); // Unmap when done
}
av_frame_free(mappedFrame);

Performance Considerations

Best Practices

  1. Keep Data on GPU: Minimize transfers between GPU and CPU memory
  2. Batch Operations: Process multiple frames before transferring
  3. Use Appropriate Formats: Choose GPU-native pixel formats when possible
  4. Pool Frames: Reuse hardware frame buffers to avoid allocation overhead

Common Pixel Formats for Hardware

// Hardware-optimized pixel formats
AV_PIX_FMT_CUDA     // NVIDIA CUDA format
AV_PIX_FMT_VAAPI    // Intel VA-API format  
AV_PIX_FMT_DXVA2_VLD // DirectX Video Acceleration
AV_PIX_FMT_VIDEOTOOLBOX // Apple VideoToolbox format

Error Handling

Hardware acceleration can fail due to driver issues, insufficient GPU memory, or unsupported configurations:

// Always check for hardware acceleration support
int deviceType = av_hwdevice_find_type_by_name("cuda");
if (deviceType == AV_HWDEVICE_TYPE_NONE) {
    System.err.println("CUDA not available, falling back to software decoding");
    // Use software decoder as fallback
}

// Handle device creation failures
AVBufferRef deviceCtx = new AVBufferRef(null);
int result = av_hwdevice_ctx_create(deviceCtx, deviceType, null, null, 0);
if (result < 0) {
    System.err.println("Hardware device creation failed: " + result);
    // Implement software fallback
}

Integration with Encoding/Decoding Pipeline

Hardware acceleration integrates seamlessly with the standard FFmpeg encode/decode workflow, providing significant performance improvements for supported codecs and hardware configurations.

Install with Tessl CLI

npx tessl i tessl/maven-org-bytedeco--ffmpeg

docs

audio-processing.md

codec-operations.md

command-line-tools.md

constants-enums.md

cryptographic-security.md

device-io.md

format-handling.md

hardware-acceleration.md

index.md

media-filtering.md

postproc.md

scaling-conversion.md

tile.json