use std::ffi::{CStr, c_char, c_int, c_void};
use std::ptr;
use std::sync::{Mutex, Once, PoisonError};
use crate::codec::{Codec, VideoEncoder};
use crate::{
AV1_TEMPORAL_LAYERS, Av1Options, EncodedFrame, EncoderConfig, Error, I420Frame, Result,
av1_cpu_supported,
};
mod ffi {
#![allow(
non_upper_case_globals,
non_camel_case_types,
non_snake_case,
dead_code,
unsafe_op_in_unsafe_fn,
clippy::all
)]
include!(concat!(env!("OUT_DIR"), "/svt.rs"));
}
const PRESET: i8 = 10;
const SCREEN_CONTENT_PRESET: i8 = 8;
static LIFECYCLE: Mutex<()> = Mutex::new(());
static LOG_CALLBACK: Once = Once::new();
type VaList = <ffi::SvtAv1LogCallback as LastArgument>::Type;
trait LastArgument {
type Type;
}
impl<A, B, C, D, E> LastArgument for Option<unsafe extern "C" fn(A, B, C, D, E)> {
type Type = E;
}
unsafe extern "C" {
fn vsnprintf(buffer: *mut c_char, size: usize, format: *const c_char, args: VaList) -> c_int;
}
unsafe extern "C" fn svt_log(
_context: *mut c_void,
level: ffi::SvtAv1LogLevel,
tag: *const c_char,
format: *const c_char,
args: VaList,
) {
if level > ffi::SVT_AV1_LOG_ERROR {
return;
}
let mut buffer = [0 as c_char; 512];
let message = unsafe {
if vsnprintf(buffer.as_mut_ptr(), buffer.len(), format, args) < 0 {
return;
}
CStr::from_ptr(buffer.as_ptr()).to_string_lossy()
};
let tag = if tag.is_null() {
"SVT-AV1".into()
} else {
unsafe { CStr::from_ptr(tag) }.to_string_lossy()
};
log::error!("{tag}: {}", message.trim_end());
}
pub struct Av1Encoder {
handle: *mut ffi::EbComponentType,
config: EncoderConfig,
options: Av1Options,
pending_bitrate: bool,
pending_fps: bool,
}
unsafe impl Send for Av1Encoder {}
impl Av1Encoder {
pub fn new(config: EncoderConfig, options: Av1Options) -> Result<Self> {
if !av1_cpu_supported() {
return Err(codec_error(
"encoder init",
"the CPU has no AVX2, and SVT-AV1's code for such CPUs crashes".to_owned(),
));
}
validate(&config)?;
let handle = open(&config, options)?;
Ok(Self {
handle,
config,
options,
pending_bitrate: false,
pending_fps: false,
})
}
pub fn config(&self) -> EncoderConfig {
self.config
}
pub fn options(&self) -> Av1Options {
self.options
}
pub fn encode(
&mut self,
frame: &I420Frame,
pts: i64,
force_keyframe: bool,
) -> Result<Vec<EncodedFrame>> {
if frame.width() != self.config.width || frame.height() != self.config.height {
return Err(Error::InvalidFrame(format!(
"frame is {}x{}, encoder is set to {}x{}",
frame.width(),
frame.height(),
self.config.width,
self.config.height
)));
}
let (w, h) = (frame.width() as usize, frame.height() as usize);
let chroma_w = w.div_ceil(2);
let luma = w * h;
let chroma = chroma_w * h.div_ceil(2);
let base = frame.data().as_ptr() as *mut u8;
let mut rate = ffi::SvtAv1RateInfo {
seq_qp: 0,
target_bit_rate: bits_per_second(self.config.bitrate_kbps),
};
let mut frame_rate = ffi::SvtAv1FrameRateInfo {
frame_rate_numerator: self.config.fps,
frame_rate_denominator: 1,
};
let mut events = Vec::with_capacity(2);
if self.pending_bitrate {
events.push(event(ffi::RATE_CHANGE_EVENT, &mut rate));
}
if self.pending_fps {
events.push(event(ffi::FRAME_RATE_CHANGE_EVENT, &mut frame_rate));
}
for index in 1..events.len() {
let next: *mut ffi::EbPrivDataNode = &mut events[index];
events[index - 1].next = next;
}
unsafe {
let mut planes = ffi::EbSvtIOFormat {
luma: base,
cb: base.add(luma),
cr: base.add(luma + chroma),
y_stride: w as u32,
cr_stride: chroma_w as u32,
cb_stride: chroma_w as u32,
};
let mut input = std::mem::zeroed::<ffi::EbBufferHeaderType>();
input.size = std::mem::size_of::<ffi::EbBufferHeaderType>() as u32;
input.p_buffer = (&mut planes as *mut ffi::EbSvtIOFormat).cast();
input.n_filled_len = (luma + 2 * chroma) as u32;
input.n_alloc_len = input.n_filled_len;
input.pts = pts;
input.pic_type = if force_keyframe {
ffi::EB_AV1_KEY_PICTURE
} else {
ffi::EB_AV1_INVALID_PICTURE
};
input.p_app_private = events.first_mut().map_or(ptr::null_mut(), |first| {
(first as *mut ffi::EbPrivDataNode).cast()
});
check(
ffi::svt_av1_enc_send_picture(self.handle, &mut input),
"encode",
)?;
self.pending_bitrate = false;
self.pending_fps = false;
let mut packet: *mut ffi::EbBufferHeaderType = ptr::null_mut();
check(
ffi::svt_av1_enc_get_packet(self.handle, &mut packet, 0),
"get packet",
)?;
if packet.is_null() {
return Err(codec_error("get packet", "no packet".into()));
}
let out = &*packet;
let result = if out.flags & ffi::EB_BUFFERFLAG_ERROR_MASK != 0 {
Err(codec_error(
"encode",
format!("error flags {:#x}", out.flags),
))
} else {
Ok(EncodedFrame {
data: std::slice::from_raw_parts(out.p_buffer, out.n_filled_len as usize)
.to_vec(),
keyframe: out.pic_type == ffi::EB_AV1_KEY_PICTURE,
temporal_layer: out.temporal_layer_index,
pts: out.pts,
})
};
ffi::svt_av1_enc_release_out_buffer(&mut packet);
let frame = result?;
if frame.temporal_layer >= layers(&self.config) {
return Err(codec_error(
"read temporal layer id",
format!("unexpected layer id {}", frame.temporal_layer),
));
}
Ok(vec![frame])
}
}
pub fn set_resolution(&mut self, width: u32, height: u32) -> Result<()> {
self.reopen(EncoderConfig {
width,
height,
..self.config
})
}
pub fn set_bitrate(&mut self, bitrate_kbps: u32) -> Result<()> {
validate(&EncoderConfig {
bitrate_kbps,
..self.config
})?;
self.config.bitrate_kbps = bitrate_kbps;
self.pending_bitrate = true;
Ok(())
}
pub fn set_fps(&mut self, fps: u32) -> Result<()> {
let config = EncoderConfig { fps, ..self.config };
validate(&config)?;
self.config = config;
self.pending_fps = true;
Ok(())
}
pub fn set_options(&mut self, options: Av1Options) -> Result<()> {
if options == self.options {
return Ok(());
}
let handle = open(&self.config, options)?;
close(self.handle);
self.handle = handle;
self.options = options;
Ok(())
}
fn reopen(&mut self, config: EncoderConfig) -> Result<()> {
validate(&config)?;
let handle = open(&config, self.options)?;
close(self.handle);
self.handle = handle;
self.config = config;
self.pending_bitrate = false;
self.pending_fps = false;
Ok(())
}
}
impl VideoEncoder for Av1Encoder {
fn codec(&self) -> Codec {
Codec::Av1
}
fn config(&self) -> EncoderConfig {
Av1Encoder::config(self)
}
fn temporal_layers(&self) -> u8 {
layers(&self.config)
}
fn encode(
&mut self,
frame: &I420Frame,
pts: i64,
force_keyframe: bool,
) -> Result<Vec<EncodedFrame>> {
Av1Encoder::encode(self, frame, pts, force_keyframe)
}
fn set_resolution(&mut self, width: u32, height: u32) -> Result<()> {
Av1Encoder::set_resolution(self, width, height)
}
fn set_bitrate(&mut self, bitrate_kbps: u32) -> Result<()> {
Av1Encoder::set_bitrate(self, bitrate_kbps)
}
fn set_fps(&mut self, fps: u32) -> Result<()> {
Av1Encoder::set_fps(self, fps)
}
}
impl Drop for Av1Encoder {
fn drop(&mut self) {
close(self.handle);
}
}
fn bits_per_second(kbps: u32) -> u32 {
kbps.saturating_mul(1000)
}
fn event<T>(node_type: ffi::PrivDataType, data: &mut T) -> ffi::EbPrivDataNode {
ffi::EbPrivDataNode {
node_type,
data: (data as *mut T).cast(),
size: std::mem::size_of::<T>() as u32,
next: ptr::null_mut(),
}
}
fn validate(config: &EncoderConfig) -> Result<()> {
if config.width < 8 || config.height < 8 {
return Err(Error::InvalidConfig(format!(
"size {}x{} (AV1 needs even sizes of at least 8x8)",
config.width, config.height
)));
}
config.validate_common(AV1_TEMPORAL_LAYERS)?;
if let Some(threads) = config.threads {
return Err(Error::InvalidConfig(format!(
"{threads} threads (SVT-AV1 chooses its threads itself)"
)));
}
Ok(())
}
fn layers(config: &EncoderConfig) -> u8 {
config.temporal_layers.unwrap_or(AV1_TEMPORAL_LAYERS)
}
fn open(config: &EncoderConfig, options: Av1Options) -> Result<*mut ffi::EbComponentType> {
let _lifecycle = LIFECYCLE.lock().unwrap_or_else(PoisonError::into_inner);
LOG_CALLBACK
.call_once(|| unsafe { ffi::svt_av1_set_log_callback(Some(svt_log), ptr::null_mut()) });
unsafe {
let mut handle = ptr::null_mut();
let mut cfg = std::mem::zeroed::<ffi::EbSvtAv1EncConfiguration>();
check(
ffi::svt_av1_enc_init_handle(&mut handle, &mut cfg),
"encoder init",
)?;
configure(&mut cfg, config, options);
let started = check(
ffi::svt_av1_enc_set_parameter(handle, &mut cfg),
"configure encoder",
)
.and_then(|()| check(ffi::svt_av1_enc_init(handle), "start encoder"));
if let Err(err) = started {
ffi::svt_av1_enc_deinit_handle(handle);
return Err(err);
}
Ok(handle)
}
}
fn configure(cfg: &mut ffi::EbSvtAv1EncConfiguration, config: &EncoderConfig, options: Av1Options) {
cfg.enc_mode = if options.screen_content {
SCREEN_CONTENT_PRESET
} else {
PRESET
};
cfg.source_width = config.width;
cfg.source_height = config.height;
cfg.frame_rate_numerator = config.fps;
cfg.frame_rate_denominator = 1;
cfg.encoder_bit_depth = 8;
cfg.rtc = true;
cfg.pred_structure = ffi::LOW_DELAY;
cfg.hierarchical_levels = u32::from(layers(config)) - 1;
cfg.look_ahead_distance = 0;
cfg.enable_tf = 0;
cfg.scene_change_detection = 0;
cfg.intra_period_length = -1;
cfg.intra_refresh_type = ffi::SVT_AV1_KF_REFRESH;
cfg.rate_control_mode = ffi::SVT_AV1_RC_MODE_CBR as u8;
cfg.target_bit_rate = bits_per_second(config.bitrate_kbps);
cfg.starting_buffer_level_ms = 500;
cfg.optimal_buffer_level_ms = 600;
cfg.maximum_buffer_size_ms = 700;
cfg.under_shoot_pct = 50;
cfg.over_shoot_pct = 50;
cfg.screen_content_mode = u32::from(options.screen_content);
cfg.level_of_parallelism = parallelism();
}
fn close(handle: *mut ffi::EbComponentType) {
unsafe {
let mut eos = std::mem::zeroed::<ffi::EbBufferHeaderType>();
eos.size = std::mem::size_of::<ffi::EbBufferHeaderType>() as u32;
eos.flags = ffi::EB_BUFFERFLAG_EOS;
eos.pic_type = ffi::EB_AV1_INVALID_PICTURE;
if ffi::svt_av1_enc_send_picture(handle, &mut eos) == ffi::EB_ErrorNone {
loop {
let mut packet: *mut ffi::EbBufferHeaderType = ptr::null_mut();
let status = ffi::svt_av1_enc_get_packet(handle, &mut packet, 1);
if status != ffi::EB_ErrorNone || packet.is_null() {
break;
}
let last = (*packet).flags & ffi::EB_BUFFERFLAG_EOS != 0;
ffi::svt_av1_enc_release_out_buffer(&mut packet);
if last {
break;
}
}
}
let _lifecycle = LIFECYCLE.lock().unwrap_or_else(PoisonError::into_inner);
let status = ffi::svt_av1_enc_deinit(handle);
if status != ffi::EB_ErrorNone {
log::error!("SVT-AV1 encoder shutdown failed: {status:#x}");
}
ffi::svt_av1_enc_deinit_handle(handle);
}
}
fn parallelism() -> u32 {
if std::thread::available_parallelism().map_or(1, |n| n.get()) > 8 {
3
} else {
2
}
}
fn check(status: ffi::EbErrorType, operation: &'static str) -> Result<()> {
if status == ffi::EB_ErrorNone {
return Ok(());
}
Err(codec_error(operation, format!("error {status:#x}")))
}
fn codec_error(operation: &'static str, message: String) -> Error {
Error::Codec {
library: "SVT-AV1",
operation,
message,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn config() -> EncoderConfig {
EncoderConfig::new(320, 240, 30, 500)
}
fn layers_of(encoder: &mut Av1Encoder, count: i64) -> Vec<u8> {
let frame =
I420Frame::new(320, 240, vec![128; I420Frame::buffer_len(320, 240)]).expect("frame");
(0..count)
.map(|index| {
let out = encoder.encode(&frame, index * 33, false).expect("encode");
out[0].temporal_layer
})
.collect()
}
#[test]
fn validates_the_settings() {
validate(&config()).expect("defaults");
validate(&EncoderConfig {
fps: 120,
..config()
})
.expect("120 fps");
for layers in 1..=AV1_TEMPORAL_LAYERS {
validate(&config().with_temporal_layers(layers)).expect("layers");
}
for invalid in [
EncoderConfig { fps: 0, ..config() },
EncoderConfig {
fps: 121,
..config()
},
config().with_temporal_layers(0),
config().with_temporal_layers(4),
config().with_threads(2),
config().with_max_size(320, 239),
] {
assert!(matches!(validate(&invalid), Err(Error::InvalidConfig(_))));
}
}
#[test]
fn produces_the_default_and_the_requested_layers() {
if !av1_cpu_supported() {
return;
}
let mut encoder = Av1Encoder::new(config(), Av1Options::default()).expect("encoder");
assert_eq!(encoder.temporal_layers(), AV1_TEMPORAL_LAYERS);
assert_eq!(layers_of(&mut encoder, 5), [0, 2, 1, 2, 0]);
let one = config().with_temporal_layers(1);
let mut encoder = Av1Encoder::new(one, Av1Options::default()).expect("encoder");
assert_eq!(encoder.temporal_layers(), 1);
assert_eq!(layers_of(&mut encoder, 4), [0, 0, 0, 0]);
let two = config().with_temporal_layers(2);
let mut encoder = Av1Encoder::new(two, Av1Options::default()).expect("encoder");
assert_eq!(layers_of(&mut encoder, 4), [0, 1, 0, 1]);
encoder.set_resolution(640, 480).expect("resize");
assert_eq!(encoder.temporal_layers(), 2);
assert!(matches!(
encoder.set_resolution(7682, 480),
Err(Error::InvalidConfig(_))
));
}
#[test]
fn rejects_a_thread_count_when_opened() {
if !av1_cpu_supported() {
return;
}
assert!(matches!(
Av1Encoder::new(config().with_threads(2), Av1Options::default()),
Err(Error::InvalidConfig(_))
));
}
}