Expand description
Hardware video encoding into, and decoding of, H.264 elementary streams.
VideoEncoder compresses image tensors with the platform’s video
encoder, and VideoDecoder turns access units back into image tensors.
On Linux both are V4L2 stateful memory-to-memory devices (i.MX 8M Plus
vsi_v4l2, i.MX 95 Wave6, …), discovered by capability. Source tensors in
DMA-BUF memory are imported without a copy, and decoded pictures are
returned in the DMA-BUF buffers the device wrote; only the compressed
bitstream is copied.
use edgefirst_codec::video::{EncoderConfig, FrameOptions, VideoEncoder};
use edgefirst_tensor::{CpuAccess, PixelFormat, TensorDyn, TensorMemory};
let mut encoder = VideoEncoder::new(EncoderConfig::h264(1920, 1080, PixelFormat::Nv12, 30.0))?;
let frame = TensorDyn::image(1920, 1080, PixelFormat::Nv12, edgefirst_tensor::DType::U8,
Some(TensorMemory::DmaBuf), CpuAccess::Write)?;
let mut stream = Vec::new();
for pts in 0..30u64 {
let opts = FrameOptions { pts, ..FrameOptions::default() };
if let Some(au) = encoder.encode(&frame, &opts)? {
stream.extend_from_slice(&au.data);
}
}
for au in encoder.flush()? {
stream.extend_from_slice(&au.data);
}Decoding takes one access unit at a time; access_units splits a byte
stream into them. Each call returns everything the decoder has ready.
use edgefirst_codec::video::{access_units, DecodeEvent, DecoderConfig, VideoDecoder};
let stream = std::fs::read("clip.h264")?;
let mut decoder = VideoDecoder::new(DecoderConfig::h264())?;
let mut show = |event| match event {
DecodeEvent::Frame(frame) => println!("{} {:?}", frame.pts(), frame.tensor().shape()),
DecodeEvent::Reconfigured(info) => println!("{}x{}", info.width, info.height),
};
for (pts, au) in access_units(&stream).enumerate() {
decoder.decode(au, pts as u64)?.into_iter().for_each(&mut show);
}
decoder.flush()?.into_iter().for_each(&mut show);Structs§
- Access
Units - Iterator over the access units of a byte stream, from
access_units. - Decoded
Frame - A decoded picture, lent from the decoder’s pool.
- Decoder
Config - Configuration of a
VideoDecoder. - Encoded
Frame - One encoded access unit.
- Encoder
Config - Configuration of a
VideoEncoder. - Frame
Options - Per-frame options for
VideoEncoder::encode. - Stream
Info - Geometry and format of the decoded pictures, known once the decoder has parsed the stream’s headers.
- Video
Decoder - A hardware video decoder.
- Video
Encoder - A hardware video encoder.
Enums§
- Bitrate
- Target bitrate of an encoder.
- Decode
Event - What
VideoDecoder::decodeandVideoDecoder::flushproduce, in order. - H264
Level - H.264 level (ITU-T H.264 Table A-1).
- H264
Profile - H.264 profile (ITU-T H.264 Annex A).
- Output
Memory - Whose memory a decoder writes its pictures to. Either way the frames are DMA-BUF tensors and reach the GPU without a copy.
- Output
Pool - Where a
VideoDecoderwrites decoded pictures. - Video
Codec - A compressed video format.
Functions§
- access_
units - Splits an H.264 Annex B byte stream into access units, the input
VideoDecoder::decodetakes.