pub mod animation;
pub mod apng;
pub mod bitstream;
pub mod decode;
#[cfg(feature = "gif")]
pub mod gif;
pub mod h264;
pub mod rate;
pub mod hevc;
pub mod mp4;
pub mod mp4_demux;
pub mod mp4_stream;
pub mod vp9;
pub mod webm;
pub use animation::{
encode_animation_rgba, encode_animation_rgba_with_progress, format_animation_progress,
AnimationEncodeError, AnimationEncodeOptions, AnimationExportPhase, AnimationExportProgress,
BrowserCodec,
};
pub use apng::ApngEncoder;
pub use bitstream::{emulation_prevention, rbsp_trailing_bits, BitWriter};
#[cfg(feature = "gif")]
pub use gif::{
decode_gif, encode_gif, DisposalMethod, DisposalMode, GifDecoder, GifEncoder, GifError,
GifFrame, GifFrameMeta, GifInfo, GifOptions, GifVersion, GifWriter, LzwClearMode, PaletteMode,
QuantizerKind,
};
#[cfg(feature = "gif")]
#[allow(deprecated)]
pub use gif::DecodedFrame;
pub use vp9::{encode_intra_frame, encode_intra_gray, Reconstruction};
pub use webm::{encode_gray_webm, encode_webm, mux_webm, WebmCodec, WebmFrame, WebmParams};
pub(crate) fn frames_in_flight(width: u32, height: u32) -> usize {
frames_by_memory(width, height).min(worker_threads())
}
pub(crate) fn frames_by_memory(width: u32, height: u32) -> usize {
(frame_memory_budget() / frame_working_set(width, height).max(1)).max(1)
}
pub(crate) fn frame_working_set(width: u32, height: u32) -> usize {
(width as usize * height as usize * 3 / 2) * 5 / 2
}
pub const DEFAULT_FRAME_MEMORY_BUDGET: usize = 256 << 20;
static FRAME_MEMORY_BUDGET: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(DEFAULT_FRAME_MEMORY_BUDGET);
pub fn frame_memory_budget() -> usize {
FRAME_MEMORY_BUDGET.load(std::sync::atomic::Ordering::Relaxed)
}
pub fn set_frame_memory_budget(bytes: usize) {
FRAME_MEMORY_BUDGET.store(bytes.max(1), std::sync::atomic::Ordering::Relaxed);
}
#[cfg(feature = "parallel")]
fn worker_threads() -> usize {
rayon::current_num_threads().max(1)
}
#[cfg(not(feature = "parallel"))]
fn worker_threads() -> usize {
1
}
#[cfg(feature = "parallel")]
pub(crate) fn map_frames<T, R>(frames: &[T], encode: impl Fn(&T) -> R + Sync + Send) -> Vec<R>
where
T: Sync,
R: Send,
{
use rayon::prelude::*;
frames.par_iter().map(encode).collect()
}
#[cfg(not(feature = "parallel"))]
pub(crate) fn map_frames<T, R>(frames: &[T], encode: impl Fn(&T) -> R + Sync + Send) -> Vec<R> {
frames.iter().map(encode).collect()
}
pub fn map_frames_pub<T, R>(frames: &[T], encode: impl Fn(&T) -> R + Sync + Send) -> Vec<R>
where
T: Sync,
R: Send,
{
map_frames(frames, encode)
}
#[cfg(feature = "parallel")]
pub(crate) fn map_indexed<T, R>(items: &[T], f: impl Fn(usize, &T) -> R + Sync + Send) -> Vec<R>
where
T: Sync,
R: Send,
{
use rayon::prelude::*;
items.par_iter().enumerate().map(|(i, t)| f(i, t)).collect()
}
#[cfg(not(feature = "parallel"))]
pub(crate) fn map_indexed<T, R>(items: &[T], f: impl Fn(usize, &T) -> R + Sync + Send) -> Vec<R> {
items.iter().enumerate().map(|(i, t)| f(i, t)).collect()
}