use crate::internal::bpg_format::{BpgHeader, ColorSpaceCode, PixelFormat};
use crate::internal::bpg_hevc::{HevcError, build_modified_hevc};
use crate::internal::bpg_image::{ChromaFormat, ColorSpace, Image};
pub mod heic;
#[derive(Debug, Clone, Default)]
pub struct EncoderTuning {
pub x265_tune: Option<String>,
pub x265_params: Vec<(String, String)>,
}
impl EncoderTuning {
pub fn neutral() -> Self {
Self {
x265_tune: Some("ssim".to_string()),
x265_params: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct HevcEncodeParams {
pub width: u32,
pub height: u32,
pub chroma_format: ChromaFormat,
pub bit_depth: u8,
pub qp: u8,
pub compress_level: u8,
pub sei_decoded_picture_hash: bool,
pub verbose: bool,
pub tuning: EncoderTuning,
}
#[derive(Debug, Clone, Copy)]
pub struct HevcBackendCaps {
pub bit_depths: &'static [u8],
pub chroma_formats: &'static [ChromaFormat],
pub supports_sao: bool,
pub supports_deblock: bool,
pub supports_lossless: bool,
pub supports_alpha: bool,
}
impl HevcBackendCaps {
pub fn supports_bit_depth(&self, bit_depth: u8) -> bool {
self.bit_depths.contains(&bit_depth)
}
pub fn supports_chroma_format(&self, chroma_format: ChromaFormat) -> bool {
self.chroma_formats.contains(&chroma_format)
}
}
pub trait HevcEncoder {
fn encode_still(
&self,
image: &Image,
params: &HevcEncodeParams,
) -> Result<Vec<u8>, EncodeError>;
fn caps(&self) -> HevcBackendCaps;
}
#[derive(Debug)]
pub enum EncodeError {
Backend(String),
Hevc(HevcError),
Unsupported(&'static str),
Container(String),
}
impl From<HevcError> for EncodeError {
fn from(e: HevcError) -> Self {
EncodeError::Hevc(e)
}
}
impl std::fmt::Display for EncodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EncodeError::Backend(m) => write!(f, "HEVC backend error: {m}"),
EncodeError::Hevc(e) => write!(f, "HEVC rewrite error: {e:?}"),
EncodeError::Unsupported(w) => write!(f, "unsupported: {w}"),
EncodeError::Container(m) => write!(f, "container error: {m}"),
}
}
}
impl std::error::Error for EncodeError {}
pub const CB_SIZE: u32 = 8;
#[derive(Debug)]
pub(crate) struct EncodedHevcStill {
pub annexb: Vec<u8>,
pub display_width: u32,
pub display_height: u32,
pub coded_width: u32,
pub coded_height: u32,
pub chroma_format: ChromaFormat,
pub bit_depth: u8,
pub color_space: ColorSpace,
pub limited_range: bool,
}
fn encode_annexb_still(
mut image: Image,
backend: &dyn HevcEncoder,
qp: u8,
compress_level: u8,
tuning: EncoderTuning,
) -> Result<EncodedHevcStill, EncodeError> {
if ![8, 10, 12].contains(&image.bit_depth) {
return Err(EncodeError::Unsupported("bit depth must be 8, 10, or 12"));
}
if image.has_alpha {
return Err(EncodeError::Unsupported("alpha not supported"));
}
let display_width = image.width;
let display_height = image.height;
image.pad_to_cb_size(CB_SIZE);
let params = HevcEncodeParams {
width: image.width,
height: image.height,
chroma_format: image.chroma_format,
bit_depth: image.bit_depth,
qp,
compress_level,
sei_decoded_picture_hash: false,
verbose: false,
tuning,
};
let annexb = backend.encode_still(&image, ¶ms)?;
Ok(EncodedHevcStill {
annexb,
display_width,
display_height,
coded_width: image.width,
coded_height: image.height,
chroma_format: image.chroma_format,
bit_depth: image.bit_depth,
color_space: image.color_space,
limited_range: image.limited_range,
})
}
pub fn encode_still_image(
image: Image,
backend: &dyn HevcEncoder,
qp: u8,
compress_level: u8,
tuning: EncoderTuning,
) -> Result<Vec<u8>, EncodeError> {
let encoded = encode_annexb_still(image, backend, qp, compress_level, tuning)?;
let payload = build_modified_hevc(&encoded.annexb)?;
let header = BpgHeader {
pixel_format: pixel_format_of(encoded.chroma_format),
alpha1_flag: false,
bit_depth_minus_8: encoded.bit_depth - 8,
color_space: color_space_code_of(encoded.color_space),
alpha2_flag: false,
limited_range: encoded.limited_range,
animation_flag: false,
width: encoded.display_width,
height: encoded.display_height,
};
let mut out = Vec::with_capacity(16 + payload.len());
header.write(&mut out, 0, None);
out.extend_from_slice(&payload);
Ok(out)
}
fn pixel_format_of(cf: ChromaFormat) -> PixelFormat {
match cf {
ChromaFormat::Gray => PixelFormat::Gray,
ChromaFormat::Yuv420 => PixelFormat::Yuv420,
ChromaFormat::Yuv422 => PixelFormat::Yuv422,
ChromaFormat::Yuv444 => PixelFormat::Yuv444,
}
}
fn color_space_code_of(cs: ColorSpace) -> ColorSpaceCode {
match cs {
ColorSpace::YCbCr => ColorSpaceCode::YCbCr,
ColorSpace::Rgb => ColorSpaceCode::Rgb,
ColorSpace::YCgCo => ColorSpaceCode::YCgCo,
ColorSpace::YCbCrBt709 | ColorSpace::Srgb => ColorSpaceCode::YCbCrBt709,
ColorSpace::YCbCrBt2020 => ColorSpaceCode::YCbCrBt2020,
}
}