pub mod bitstream;
pub mod cabac;
mod color_convert;
mod ctu;
pub mod deblock;
pub mod debug;
pub mod intra;
pub mod params;
mod picture;
pub mod residual;
pub mod sao;
pub mod slice;
pub mod tile;
pub mod transform;
mod transform_simd;
pub use color_convert::is_ycbcr_matrix;
pub use picture::{DecodedFrame, UNINIT_SAMPLE};
use crate::internal::bpg_hevc_decode::error::HevcError;
use crate::internal::bpg_hevc_decode::heif::HevcDecoderConfig;
use alloc::vec::Vec;
type Result<T> = core::result::Result<T, HevcError>;
pub fn decode(data: &[u8]) -> Result<DecodedFrame> {
let nal_units = bitstream::parse_nal_units(data)?;
decode_nal_units(&nal_units)
}
pub fn decode_with_config(config: &HevcDecoderConfig, image_data: &[u8]) -> Result<DecodedFrame> {
let mut nal_units = Vec::new();
for nal_data in &config.nal_units {
if let Ok(nal) = bitstream::parse_single_nal(nal_data) {
nal_units.push(nal);
}
}
let length_size = (config.length_size_minus_one + 1) as usize;
let mut slice_nals = bitstream::parse_length_prefixed_ext(image_data, length_size)?;
nal_units.append(&mut slice_nals);
decode_nal_units(&nal_units)
}
pub fn get_info_from_config(config: &HevcDecoderConfig) -> Result<ImageInfo> {
for nal_data in &config.nal_units {
if let Ok(nal) = bitstream::parse_single_nal(nal_data)
&& nal.nal_type == bitstream::NalType::SpsNut
{
let sps = params::parse_sps(&nal.payload)?;
let (width, height) = get_cropped_dimensions(&sps);
return Ok(ImageInfo { width, height });
}
}
Err(HevcError::MissingParameterSet("SPS"))
}
fn decode_nal_units(nal_units: &[bitstream::NalUnit<'_>]) -> Result<DecodedFrame> {
let mut _vps = None;
let mut sps = None;
let mut pps = None;
for nal in nal_units {
match nal.nal_type {
bitstream::NalType::VpsNut => {
_vps = Some(params::parse_vps(&nal.payload)?);
}
bitstream::NalType::SpsNut => {
sps = Some(params::parse_sps(&nal.payload)?);
}
bitstream::NalType::PpsNut => {
pps = Some(params::parse_pps(&nal.payload)?);
}
_ => {}
}
}
let sps = sps.ok_or(HevcError::MissingParameterSet("SPS"))?;
let pps = pps.ok_or(HevcError::MissingParameterSet("PPS"))?;
let w = sps.pic_width_in_luma_samples;
let h = sps.pic_height_in_luma_samples;
if w == 0 || h == 0 || w > 32768 || h > 32768 {
return Err(HevcError::InvalidParameterSet {
kind: "SPS",
msg: alloc::format!("invalid dimensions {}x{}", w, h),
});
}
if w.checked_mul(h).is_none() {
return Err(HevcError::InvalidParameterSet {
kind: "SPS",
msg: alloc::format!("dimensions {}x{} overflow u32", w, h),
});
}
let (sub_width_c, sub_height_c) = match sps.chroma_format_idc {
0 => (1u32, 1u32), 1 => (2, 2), 2 => (2, 1), 3 => (1, 1), _ => (2, 2),
};
let min_cb = 1u32 << sps.log2_min_cb_size();
let pic_w = sps.pic_width_in_luma_samples;
let pic_h = sps.pic_height_in_luma_samples;
let coded_w = pic_w.div_ceil(min_cb) * min_cb;
let coded_h = pic_h.div_ceil(min_cb) * min_cb;
let mut frame =
DecodedFrame::with_params(coded_w, coded_h, sps.bit_depth_y(), sps.chroma_format_idc);
frame.full_range = sps.video_full_range_flag;
frame.matrix_coeffs = sps.matrix_coeffs;
let mut crop_left = 0;
let mut crop_right = coded_w - pic_w;
let mut crop_top = 0;
let mut crop_bottom = coded_h - pic_h;
if sps.conformance_window_flag {
crop_left += sps.conf_win_offset.0 * sub_width_c;
crop_right += sps.conf_win_offset.1 * sub_width_c;
crop_top += sps.conf_win_offset.2 * sub_height_c;
crop_bottom += sps.conf_win_offset.3 * sub_height_c;
}
frame.set_crop(crop_left, crop_right, crop_top, crop_bottom);
for nal in nal_units {
if nal.nal_type.is_slice() && nal.nuh_layer_id == 0 {
decode_slice(nal, &sps, &pps, &mut frame)?;
}
}
Ok(frame)
}
pub fn get_info(data: &[u8]) -> Result<ImageInfo> {
let nal_units = bitstream::parse_nal_units(data)?;
for nal in &nal_units {
if nal.nal_type == bitstream::NalType::SpsNut {
let sps = params::parse_sps(&nal.payload)?;
let (width, height) = get_cropped_dimensions(&sps);
return Ok(ImageInfo { width, height });
}
}
Err(HevcError::MissingParameterSet("SPS"))
}
fn get_cropped_dimensions(sps: ¶ms::Sps) -> (u32, u32) {
if sps.conformance_window_flag {
let (sub_width_c, sub_height_c) = match sps.chroma_format_idc {
0 => (1, 1), 1 => (2, 2), 2 => (2, 1), 3 => (1, 1), _ => (2, 2), };
let crop_left = sps.conf_win_offset.0.saturating_mul(sub_width_c);
let crop_right = sps.conf_win_offset.1.saturating_mul(sub_width_c);
let crop_top = sps.conf_win_offset.2.saturating_mul(sub_height_c);
let crop_bottom = sps.conf_win_offset.3.saturating_mul(sub_height_c);
let w = sps
.pic_width_in_luma_samples
.saturating_sub(crop_left)
.saturating_sub(crop_right)
.max(1);
let h = sps
.pic_height_in_luma_samples
.saturating_sub(crop_top)
.saturating_sub(crop_bottom)
.max(1);
(w, h)
} else {
(
sps.pic_width_in_luma_samples,
sps.pic_height_in_luma_samples,
)
}
}
#[derive(Debug, Clone, Copy)]
pub struct ImageInfo {
pub width: u32,
pub height: u32,
}
fn decode_slice(
nal: &bitstream::NalUnit<'_>,
sps: ¶ms::Sps,
pps: ¶ms::Pps,
frame: &mut DecodedFrame,
) -> Result<()> {
let parse_result = slice::SliceHeader::parse(nal, sps, pps)?;
let slice_header = parse_result.header;
let data_offset = parse_result.data_offset;
if !slice_header.slice_type.is_intra() {
return Err(HevcError::Unsupported(
"only I-slices supported for still images",
));
}
let slice_data = &nal.payload[data_offset..];
let mut ctx = ctu::SliceContext::new(sps, pps, &slice_header, slice_data)?;
ctx.decode_slice(frame)?;
#[cfg(feature = "std")]
let skip_all = std::env::var("HEIC_NOFILTER").is_ok();
#[cfg(not(feature = "std"))]
let skip_all = false;
let skip_deblock = skip_all || {
#[cfg(feature = "std")]
{
std::env::var("HEIC_NODEBLOCK").is_ok()
}
#[cfg(not(feature = "std"))]
{
false
}
};
let skip_sao = skip_all || {
#[cfg(feature = "std")]
{
std::env::var("HEIC_NOSAO").is_ok()
}
#[cfg(not(feature = "std"))]
{
false
}
};
if !skip_deblock && !slice_header.slice_deblocking_filter_disabled_flag {
let beta_offset = slice_header.slice_beta_offset_div2 as i32 * 2;
let tc_offset = slice_header.slice_tc_offset_div2 as i32 * 2;
let cb_qp_offset = pps.pps_cb_qp_offset as i32;
let cr_qp_offset = pps.pps_cr_qp_offset as i32;
deblock::apply_deblocking_filter(frame, beta_offset, tc_offset, cb_qp_offset, cr_qp_offset);
}
if !skip_sao && (slice_header.slice_sao_luma_flag || slice_header.slice_sao_chroma_flag) {
sao::apply_sao(frame, &ctx.sao_map, sps.ctb_size());
}
Ok(())
}