Crate scuffle_h264

Source
Expand description

A pure Rust implementation of the H.264 (header only) builder and parser.

This crate is designed to provide a simple and safe interface to build and parse H.264 headers.

§Why do we need this?

This crate aims to provides a simple and safe interface for h264.

§How is this different from other h264 crates?

This crate is only for encoding and decoding H.264 headers.

§Notable features

This crate is a completely safe implementation of encoding and decoding H.264 headers.

We mainly use this with scuffle-mp4 and scuffle-flv to work with mp4 and flv container formats respectively.

§Examples

§Parsing

use std::io;

use bytes::Bytes;

use scuffle_h264::{AVCDecoderConfigurationRecord, Sps};

// A sample h264 bytestream to parse

// Parsing
let result = AVCDecoderConfigurationRecord::parse(&mut io::Cursor::new(bytes)).unwrap();

// Do something with it!

// You can also parse an Sps from the Sps struct:
let sps = Sps::parse_with_emulation_prevention(io::Cursor::new(&result.sps[0]));

For more examples, check out the tests in the source code for the parse function.

§Building

use bytes::Bytes;

use scuffle_h264::{AVCDecoderConfigurationRecord, AvccExtendedConfig, Sps, SpsExtended};

let extended_config = AvccExtendedConfig {
    chroma_format_idc: 1,
    bit_depth_luma_minus8: 0,
    bit_depth_chroma_minus8: 0,
    sequence_parameter_set_ext: vec![SpsExtended {
        chroma_format_idc: 1,
        separate_color_plane_flag: false,
        bit_depth_luma_minus8: 2,
        bit_depth_chroma_minus8: 3,
        qpprime_y_zero_transform_bypass_flag: false,
        scaling_matrix: vec![],
    }],
};
let config = AVCDecoderConfigurationRecord {
    configuration_version: 1,
    profile_indication: 100,
    profile_compatibility: 0,
    level_indication: 31,
    length_size_minus_one: 3,
    sps: vec![
        Bytes::from_static(b"spsdata"),
    ],
    pps: vec![Bytes::from_static(b"ppsdata")],
    extended_config: Some(extended_config),
};

// Creating a buffer to store the built bytestream
let mut built = Vec::new();

// Building
config.build(&mut built).unwrap();

// Do something with it!

For more examples, check out the tests in the source code for the build function.

§Status

This crate is currently under development and is not yet stable.

Unit tests are not yet fully implemented. Use at your own risk.

§License

This project is licensed under the MIT or Apache-2.0 license. You can choose between one of them if you use this work.

SPDX-License-Identifier: MIT OR Apache-2.0

Structs§

AVCDecoderConfigurationRecord
The AVC (H.264) Decoder Configuration Record. ISO/IEC 14496-15:2022(E) - 5.3.2.1.2
AspectRatioIdc
The AspectRatioIdc is a nutype enum for aspect_ratio_idc as defined in ISO/IEC-14496-10-2022 - E.2.1 Table E-1.
AvccExtendedConfig
The AVC (H.264) Extended Configuration. ISO/IEC 14496-15:2022(E) - 5.3.2.1.2
NALUnitType
NAL (Network Abstraction Layer) unit types as defined by ISO/IEC 14496-10:2022 (Table 7-1).
Sps
The Sequence Parameter Set. ISO/IEC-14496-10-2022 - 7.3.2
SpsExtended
The Sequence Parameter Set extension. ISO/IEC-14496-10-2022 - 7.3.2
TimingInfo
TimingInfo contains the fields that are set when timing_info_present_flag == 1.
VideoFormat
The VideoFormat is a nutype enum for video_format as defined in ISO/IEC-14496-10-2022 - E.2.1 Table E-2.