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

codec-operations.mddocs/

Codec Operations (Encoding/Decoding)

Audio and video encoding/decoding operations with support for all major codecs including H.264, H.265, VP9, AV1, AAC, MP3, and many others.

Capabilities

Codec Discovery

Finding Codecs

/**
 * Find a registered encoder with matching codec ID
 * @param id Codec ID to search for
 * @return AVCodec encoder or null if not found
 */
AVCodec avcodec_find_encoder(int id);

/**
 * Find a registered decoder with matching codec ID
 * @param id Codec ID to search for  
 * @return AVCodec decoder or null if not found
 */
AVCodec avcodec_find_decoder(int id);

/**
 * Find encoder by name
 * @param name Codec name
 * @return AVCodec encoder or null if not found
 */
AVCodec avcodec_find_encoder_by_name(String name);

/**
 * Find decoder by name
 * @param name Codec name
 * @return AVCodec decoder or null if not found
 */
AVCodec avcodec_find_decoder_by_name(String name);

/**
 * Iterate over all registered codecs
 * @param opaque Iterator state (pass null initially)
 * @return Next codec or null when done
 */
AVCodec av_codec_iterate(Pointer opaque);

Usage Example:

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

// Find H.264 encoder
AVCodec h264Encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
if (h264Encoder == null) {
    throw new RuntimeException("H.264 encoder not available");
}

// Find AAC decoder by name
AVCodec aacDecoder = avcodec_find_decoder_by_name("aac");
if (aacDecoder != null) {
    System.out.println("AAC decoder: " + aacDecoder.long_name().getString());
}

Context Management

Codec Context Operations

/**
 * Allocate an AVCodecContext and set its fields to default values
 * @param codec Codec to allocate context for (can be null)
 * @return Allocated context or null on failure
 */
AVCodecContext avcodec_alloc_context3(AVCodec codec);

/**
 * Free codec context and everything associated with it
 * @param avctx Pointer to context to free
 */
void avcodec_free_context(@ByPtrPtr AVCodecContext avctx);

/**
 * Initialize codec context to use the given codec
 * @param avctx Context to initialize
 * @param codec Codec to use
 * @param options Dictionary of codec options
 * @return Zero on success, negative error code on failure
 */
int avcodec_open2(AVCodecContext avctx, AVCodec codec, PointerPointer options);

/**
 * Close codec context and free all data associated with it
 * @param avctx Context to close
 * @return Zero on success
 */
int avcodec_close(AVCodecContext avctx);

Parameter Handling

Codec Parameters

/**
 * Copy codec parameters from codec context to codec parameters
 * @param par Destination codec parameters
 * @param codec Source codec context
 * @return >= 0 on success
 */
int avcodec_parameters_from_context(AVCodecParameters par, AVCodecContext codec);

/**
 * Copy codec parameters to codec context
 * @param codec Destination codec context
 * @param par Source codec parameters
 * @return >= 0 on success
 */
int avcodec_parameters_to_context(AVCodecContext codec, AVCodecParameters par);

/**
 * Copy codec parameters
 * @param dst Destination parameters
 * @param src Source parameters
 * @return >= 0 on success
 */
int avcodec_parameters_copy(AVCodecParameters dst, AVCodecParameters src);

Decoding Operations

Decoding Audio/Video

/**
 * Supply raw packet data as input to a decoder
 * @param avctx Codec context
 * @param avpkt Input packet (null to signal end of stream)
 * @return 0 on success, negative AVERROR on failure
 */
int avcodec_send_packet(AVCodecContext avctx, AVPacket avpkt);

/**
 * Return decoded frame from decoder
 * @param avctx Codec context
 * @param frame Frame to fill with decoded data
 * @return 0 on success, AVERROR(EAGAIN) if more input needed
 */
int avcodec_receive_frame(AVCodecContext avctx, AVFrame frame);

/**
 * Reset internal codec state
 * @param avctx Codec context to reset
 */
void avcodec_flush_buffers(AVCodecContext avctx);

Usage Example:

// Set up decoder context
AVCodecContext decoderContext = avcodec_alloc_context3(null);
avcodec_parameters_to_context(decoderContext, stream.codecpar());

AVCodec decoder = avcodec_find_decoder(decoderContext.codec_id());
int result = avcodec_open2(decoderContext, decoder, (PointerPointer)null);

AVFrame frame = av_frame_alloc();
AVPacket packet = av_packet_alloc();

// Decode loop
while (av_read_frame(formatContext, packet) >= 0) {
    if (packet.stream_index() == videoStreamIndex) {
        // Send packet to decoder
        result = avcodec_send_packet(decoderContext, packet);
        if (result < 0) {
            System.err.println("Error sending packet to decoder");
            continue;
        }
        
        // Receive decoded frames
        while (result >= 0) {
            result = avcodec_receive_frame(decoderContext, frame);
            if (result == AVERROR_EAGAIN() || result == AVERROR_EOF()) {
                break;
            } else if (result < 0) {
                System.err.println("Error during decoding");
                break;
            }
            
            // Process decoded frame
            System.out.println("Decoded frame: " + frame.width() + "x" + frame.height());
            av_frame_unref(frame);
        }
    }
    av_packet_unref(packet);
}

// Cleanup
av_frame_free(frame);
av_packet_free(packet);
avcodec_free_context(decoderContext);

Encoding Operations

Encoding Audio/Video

/**
 * Supply raw frame data as input to an encoder
 * @param avctx Codec context
 * @param frame Input frame (null to signal end of stream)
 * @return 0 on success, negative AVERROR on failure
 */
int avcodec_send_frame(AVCodecContext avctx, AVFrame frame);

/**
 * Read encoded data from encoder
 * @param avctx Codec context
 * @param avpkt Packet to fill with encoded data
 * @return 0 on success, AVERROR(EAGAIN) if more input needed
 */
int avcodec_receive_packet(AVCodecContext avctx, AVPacket avpkt);

Usage Example:

// Set up encoder context
AVCodecContext encoderContext = avcodec_alloc_context3(null);
encoderContext.codec_id(AV_CODEC_ID_H264);
encoderContext.codec_type(AVMEDIA_TYPE_VIDEO);
encoderContext.width(1920);
encoderContext.height(1080);
encoderContext.time_base(av_make_q(1, 30)); // 30 FPS
encoderContext.framerate(av_make_q(30, 1));
encoderContext.pix_fmt(AV_PIX_FMT_YUV420P);

AVCodec encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
int result = avcodec_open2(encoderContext, encoder, (PointerPointer)null);

AVFrame inputFrame = av_frame_alloc();
AVPacket outputPacket = av_packet_alloc();

// Encode frames
for (int i = 0; i < 100; i++) {
    // Fill frame with data
    inputFrame.width(1920);
    inputFrame.height(1080);
    inputFrame.format(AV_PIX_FMT_YUV420P);
    inputFrame.pts(i);
    
    // Send frame to encoder
    result = avcodec_send_frame(encoderContext, inputFrame);
    if (result < 0) {
        System.err.println("Error sending frame to encoder");
        break;
    }
    
    // Receive encoded packets
    while (result >= 0) {
        result = avcodec_receive_packet(encoderContext, outputPacket);
        if (result == AVERROR_EAGAIN() || result == AVERROR_EOF()) {
            break;
        } else if (result < 0) {
            System.err.println("Error during encoding");
            break;
        }
        
        // Write packet to output
        System.out.println("Encoded packet size: " + outputPacket.size());
        av_packet_unref(outputPacket);
    }
}

// Flush encoder
avcodec_send_frame(encoderContext, null);
while (avcodec_receive_packet(encoderContext, outputPacket) == 0) {
    // Handle final packets
    av_packet_unref(outputPacket);
}

// Cleanup
av_frame_free(inputFrame);
av_packet_free(outputPacket);
avcodec_free_context(encoderContext);

Packet Operations

Packet Management

/**
 * Allocate an AVPacket and set its fields to default values
 * @return Allocated packet or null on failure
 */
AVPacket av_packet_alloc();

/**
 * Free packet and its contents
 * @param pkt Pointer to packet to free
 */
void av_packet_free(AVPacket pkt);

/**
 * Create a reference to packet data
 * @param dst Destination packet
 * @param src Source packet
 * @return 0 on success, negative AVERROR on failure
 */
int av_packet_ref(AVPacket dst, AVPacket src);

/**
 * Unreference packet data
 * @param pkt Packet to unreference
 */
void av_packet_unref(AVPacket pkt);

/**
 * Create new packet with copy of data
 * @param pkt Packet to clone
 * @return New packet or null on failure
 */
AVPacket av_packet_clone(AVPacket pkt);

Hardware Acceleration

Hardware Acceleration Support

/**
 * Iterate over supported hardware configurations
 * @param codec Codec to query
 * @param index Configuration index
 * @return Hardware configuration or null
 */
AVCodecHWConfig avcodec_get_hw_config(AVCodec codec, int index);

/**
 * Create hardware device context
 * @param device_ctx Pointer to receive context
 * @param type Hardware device type
 * @param device Device identifier (optional)
 * @param opts Options dictionary
 * @param flags Creation flags
 * @return >= 0 on success
 */
int av_hwdevice_ctx_create(AVBufferRef device_ctx, int type, String device, 
    AVDictionary opts, int flags);

/**
 * Allocate hardware frame
 * @param hwframe_ctx Hardware frame context
 * @param frame Frame to allocate
 * @param flags Allocation flags
 * @return >= 0 on success
 */
int av_hwframe_get_buffer(AVBufferRef hwframe_ctx, AVFrame frame, int flags);

Codec Information

Codec Properties and Capabilities

/**
 * AVCodec structure containing codec information
 */
class AVCodec extends Pointer {
    /** Codec name */
    BytePointer name();
    
    /** Descriptive codec name */
    BytePointer long_name();
    
    /** Codec type (video, audio, etc.) */
    int type();
    
    /** Codec ID */
    int id();
    
    /** Codec capabilities flags */
    int capabilities();
    
    /** Supported sample rates (for audio) */
    IntPointer supported_samplerates();
    
    /** Supported pixel formats (for video) */
    IntPointer pix_fmts();
    
    /** Supported sample formats (for audio) */
    IntPointer sample_fmts();
}

/**
 * AVCodecContext structure for encoding/decoding
 */
class AVCodecContext extends Pointer {
    /** Codec information */
    AVCodec codec();
    
    /** Codec type */
    int codec_type();
    
    /** Codec ID */
    int codec_id();
    
    /** Video dimensions */
    int width();
    int height();
    
    /** Pixel format */
    int pix_fmt();
    
    /** Audio sample rate */
    int sample_rate();
    
    /** Number of audio channels */
    int channels();
    
    /** Audio sample format */
    int sample_fmt();
    
    /** Bitrate */
    long bit_rate();
    
    /** Time base */
    AVRational time_base();
    
    /** Frame rate */
    AVRational framerate();
}

Constants

Codec IDs

// Video codecs
int AV_CODEC_ID_H264 = 27;
int AV_CODEC_ID_HEVC = 173;
int AV_CODEC_ID_VP8 = 139;
int AV_CODEC_ID_VP9 = 167;
int AV_CODEC_ID_AV1 = 225;
int AV_CODEC_ID_MPEG4 = 12;
int AV_CODEC_ID_H263 = 5;

// Audio codecs  
int AV_CODEC_ID_AAC = 86018;
int AV_CODEC_ID_MP3 = 86017;
int AV_CODEC_ID_AC3 = 86019;
int AV_CODEC_ID_OPUS = 86076;
int AV_CODEC_ID_VORBIS = 86021;
int AV_CODEC_ID_FLAC = 86028;
int AV_CODEC_ID_PCM_S16LE = 65536;

Codec Capabilities

// Codec capability flags
int AV_CODEC_CAP_DRAW_HORIZ_BAND = 0x0001;    // Decoder can use draw_horiz_band callback
int AV_CODEC_CAP_DR1 = 0x0002;                // Codec uses get_buffer() for allocating buffers
int AV_CODEC_CAP_TRUNCATED = 0x0008;          // Input bitstream might be truncated
int AV_CODEC_CAP_DELAY = 0x0020;              // Encoder or decoder requires flushing
int AV_CODEC_CAP_SMALL_LAST_FRAME = 0x0040;  // Encoder supports small last frame
int AV_CODEC_CAP_SUBFRAMES = 0x0100;          // Codec can output multiple frames per packet
int AV_CODEC_CAP_EXPERIMENTAL = 0x0200;      // Codec is experimental
int AV_CODEC_CAP_CHANNEL_CONF = 0x0400;      // Codec should fill in channel configuration
int AV_CODEC_CAP_FRAME_THREADS = 0x1000;     // Codec supports frame-level multithreading
int AV_CODEC_CAP_SLICE_THREADS = 0x2000;     // Codec supports slice-level multithreading
int AV_CODEC_CAP_PARAM_CHANGE = 0x4000;      // Codec supports parameter changes during decoding
int AV_CODEC_CAP_AUTO_THREADS = 0x8000;      // Codec supports automatic thread selection
int AV_CODEC_CAP_VARIABLE_FRAME_SIZE = 0x10000; // Audio encoder supports variable frame sizes

Subtitle Processing

Subtitle Structures

/**
 * Subtitle data structure containing decoded subtitle information
 */
class AVSubtitle extends Pointer {
    /**
     * Format of subtitle: 0 = graphics, 1 = text
     */
    int format();
    
    /**
     * Relative start time (in milliseconds, 0 = now)
     */
    int start_display_time();
    
    /**
     * Relative end time (in milliseconds, 0 = duration unknown)
     */
    int end_display_time();
    
    /**
     * Number of subtitle rectangles
     */
    int num_rects();
    
    /**
     * Array of subtitle rectangles
     */
    AVSubtitleRect rects(int i);
    
    /**
     * Duration in pts units (0 if unknown)
     */
    long pts();
}

/**
 * Subtitle rectangle containing text or graphics data
 */
class AVSubtitleRect extends Pointer {
    /**
     * Position and dimensions
     */
    int x();
    int y(); 
    int w();
    int h();
    
    /**
     * Number of colors in palette (for graphics)
     */
    int nb_colors();
    
    /**
     * Subtitle type (graphics or text)
     */
    int type();
    
    /**
     * Text content (for text subtitles)
     */
    BytePointer text();
    
    /**
     * ASS formatted text (for advanced subtitles)
     */
    BytePointer ass();
    
    /**
     * Bitmap data (for graphics subtitles)
     */
    PointerPointer data();
    IntPointer linesize();
}

Subtitle Decoding

/**
 * Decode subtitle packet
 * @param avctx Codec context
 * @param sub Subtitle structure to fill
 * @param got_sub_ptr Pointer to set if subtitle was decoded
 * @param avpkt Input packet
 * @return Number of bytes consumed or negative error code
 */
int avcodec_decode_subtitle2(AVCodecContext avctx, AVSubtitle sub,
                             IntPointer got_sub_ptr, AVPacket avpkt);

/**
 * Free subtitle structure contents
 * @param sub Subtitle to free
 */
void avsubtitle_free(AVSubtitle sub);

Subtitle Decoding Example:

// Decode subtitle packets
AVCodecContext subtitleCtx = /* subtitle decoder context */;
AVSubtitle subtitle = new AVSubtitle();
IntPointer gotSubtitle = new IntPointer(1);

int result = avcodec_decode_subtitle2(subtitleCtx, subtitle, gotSubtitle, packet);

if (result >= 0 && gotSubtitle.get() != 0) {
    System.out.println("Subtitle decoded:");
    System.out.println("  Start: " + subtitle.start_display_time() + "ms");
    System.out.println("  End: " + subtitle.end_display_time() + "ms");
    
    // Process subtitle rectangles
    for (int i = 0; i < subtitle.num_rects(); i++) {
        AVSubtitleRect rect = subtitle.rects(i);
        
        if (rect.text() != null) {
            System.out.println("  Text: " + rect.text().getString());
        }
        
        if (rect.ass() != null) {
            System.out.println("  ASS: " + rect.ass().getString());
        }
    }
    
    // Free subtitle when done
    avsubtitle_free(subtitle);
}

Error Codes

// Common codec error codes
int AVERROR_EAGAIN();     // Resource temporarily unavailable
int AVERROR_EOF();        // End of file
int AVERROR_INVALIDDATA(); // Invalid data found
int AVERROR_PATCHWELCOME(); // Feature not implemented

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